Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seed the RNG with the time in seconds in HQ mode #3812

Merged
merged 8 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions include/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ static inline u16 Random(void)
return Random32() >> 16;
}

void SeedRng(u32 seed);
void SeedRng2(u32 seed);

static inline u16 Random2(void)
{
return Random2_32() >> 16;
Expand All @@ -74,6 +77,10 @@ typedef u32 rng_value_t;
u16 Random(void);
u16 Random2(void);

//Sets the initial seed value of the pseudorandom number generator
void SeedRng(u16 seed);
void SeedRng2(u16 seed);

//Returns a 32-bit pseudorandom number
#define Random32() (Random() | (Random() << 16))
#define Random2_32() (Random2() | (Random2() << 16))
Expand All @@ -94,10 +101,6 @@ static inline void AdvanceRandom(void)
extern rng_value_t gRngValue;
extern rng_value_t gRng2Value;

//Sets the initial seed value of the pseudorandom number generator
void SeedRng(u16 seed);
void SeedRng2(u16 seed);

void Shuffle8(void *data, size_t n);
void Shuffle16(void *data, size_t n);
void Shuffle32(void *data, size_t n);
Expand Down
19 changes: 16 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,22 @@ void EnableVCountIntrAtLine150(void)
#ifdef BUGFIX
static void SeedRngWithRtc(void)
{
u32 seed = RtcGetMinuteCount();
seed = (seed >> 16) ^ (seed & 0xFFFF);
SeedRng(seed);
#if HQ_RANDOM == FALSE
u32 seed = RtcGetMinuteCount();
seed = (seed >> 16) ^ (seed & 0xFFFF);
SeedRng(seed);
#else
#define BCD8(x) ((((x) >> 4) & 0xF) * 10 + ((x) & 0xF))
u32 seconds;
struct SiiRtcInfo rtc;
RtcGetInfo(&rtc);
seconds =
((HOURS_PER_DAY * RtcGetDayCount(&rtc) + BCD8(rtc.hour))
* MINUTES_PER_HOUR + BCD8(rtc.minute))
* SECONDS_PER_MINUTE + BCD8(rtc.second);
SeedRng(seconds);
#undef BCD8
#endif
}
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static inline u32 _SFC32_Next_Stream(struct Sfc32State *state, const u8 stream)
return result;
}

static void SFC32_Seed(struct Sfc32State *state, u16 seed, u8 stream)
static void SFC32_Seed(struct Sfc32State *state, u32 seed, u8 stream)
{
u32 i;
state->a = state->b = 0;
Expand Down Expand Up @@ -75,7 +75,7 @@ u32 Random2_32(void)
return _SFC32_Next_Stream(&gRng2Value, STREAM2);
}

void SeedRng(u16 seed)
void SeedRng(u32 seed)
{
struct Sfc32State state;
SFC32_Seed(&state, seed, STREAM1);
Expand All @@ -85,7 +85,7 @@ void SeedRng(u16 seed)
sRngLoopUnlocked = TRUE;
}

void SeedRng2(u16 seed)
void SeedRng2(u32 seed)
{
SFC32_Seed(&gRng2Value, seed, STREAM2);
}
Expand Down
Loading