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

This commit is contained in:
tertu 2024-01-19 04:27:42 -06:00 committed by GitHub
parent d3dbfaf1af
commit 179a0ea97a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 10 deletions

View File

@ -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;
@ -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))
@ -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);

View File

@ -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

View File

@ -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;
@ -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);
@ -85,7 +85,7 @@ void SeedRng(u16 seed)
sRngLoopUnlocked = TRUE;
}
void SeedRng2(u16 seed)
void SeedRng2(u32 seed)
{
SFC32_Seed(&gRng2Value, seed, STREAM2);
}