From 632267e48fe07b5e6cb2acca38c7480704bb94fe Mon Sep 17 00:00:00 2001 From: tertu marybig Date: Sat, 23 Dec 2023 12:13:23 -0600 Subject: [PATCH 1/3] 32 bit seeding with seconds --- include/random.h | 12 ++++++++---- src/main.c | 17 ++++++++++++++--- src/random.c | 6 +++--- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/random.h b/include/random.h index a13b28674f7a..5a0e49890823 100644 --- a/include/random.h +++ b/include/random.h @@ -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,11 @@ typedef u32 rng_value_t; u16 Random(void); u16 Random2(void); +//Sets the initial seed value of the pseudorandom number generator +//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 +102,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); diff --git a/src/main.c b/src/main.c index 537af7191b02..f25364477147 100644 --- a/src/main.c +++ b/src/main.c @@ -239,9 +239,20 @@ 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 + u32 seconds; + struct SiiRtcInfo rtc; + RtcGetInfo(&rtc); + seconds = + ((HOURS_PER_DAY * MINUTES_PER_HOUR) * RtcGetDayCount(&rtc) + + MINUTES_PER_HOUR * ConvertBcdToBinary(rtc.hour) + ConvertBcdToBinary(rtc.minute)) + * SECONDS_PER_MINUTE + ConvertBcdToBinary(rtc.second); + SeedRng(seconds); + #endif } #endif diff --git a/src/random.c b/src/random.c index db82334aa930..303bbc9bdfcf 100644 --- a/src/random.c +++ b/src/random.c @@ -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); } From fd9daef73c3fdb5cae4a61f1209b75630a44a369 Mon Sep 17 00:00:00 2001 From: tertu marybig Date: Sat, 23 Dec 2023 12:15:01 -0600 Subject: [PATCH 2/3] Remove accidentally copy-pasted comment --- include/random.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/random.h b/include/random.h index 5a0e49890823..d8aa62ae37cf 100644 --- a/include/random.h +++ b/include/random.h @@ -77,7 +77,6 @@ typedef u32 rng_value_t; u16 Random(void); u16 Random2(void); -//Sets the initial seed value of the pseudorandom number generator //Sets the initial seed value of the pseudorandom number generator void SeedRng(u16 seed); void SeedRng2(u16 seed); From a925a3d41172ccbd32237cf0f87b8a62cb41cb29 Mon Sep 17 00:00:00 2001 From: tertu marybig Date: Thu, 28 Dec 2023 15:05:35 -0600 Subject: [PATCH 3/3] Use macros instead and assume the RTC data is good --- src/main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index f25364477147..fdfaa5e583fa 100644 --- a/src/main.c +++ b/src/main.c @@ -244,14 +244,16 @@ static void SeedRngWithRtc(void) 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 * MINUTES_PER_HOUR) * RtcGetDayCount(&rtc) - + MINUTES_PER_HOUR * ConvertBcdToBinary(rtc.hour) + ConvertBcdToBinary(rtc.minute)) - * SECONDS_PER_MINUTE + ConvertBcdToBinary(rtc.second); + ((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