Skip to content

Commit

Permalink
Seed the RNG with the time in seconds in HQ mode (#3812)
Browse files Browse the repository at this point in the history
  • Loading branch information
tertu-m authored Jan 19, 2024
1 parent d3dbfaf commit 179a0ea
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
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

0 comments on commit 179a0ea

Please sign in to comment.