From e3d9a19f455c5e753980f2149c9e9c478aaa2510 Mon Sep 17 00:00:00 2001 From: tertu Date: Wed, 28 Feb 2024 00:04:47 -0600 Subject: [PATCH] Use a 32 bit seed for new game seeding in HQ mode. (#4218) Also adjust some comments --- src/main.c | 32 +++++++++++++++++++++++++++----- src/random.c | 7 ++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index c56c796eac..79c4066107 100644 --- a/src/main.c +++ b/src/main.c @@ -215,15 +215,37 @@ void SetMainCallback2(MainCallback callback) void StartTimer1(void) { - REG_TM1CNT_H = 0x80; + if (HQ_RANDOM) + { + REG_TM2CNT_L = 0; + REG_TM2CNT_H = TIMER_ENABLE | TIMER_COUNTUP; + } + + REG_TM1CNT_H = TIMER_ENABLE; } void SeedRngAndSetTrainerId(void) { - u16 val = REG_TM1CNT_L; - SeedRng(val); - REG_TM1CNT_H = 0; - sTrainerId = val; + u32 val; + + if (HQ_RANDOM) + { + REG_TM1CNT_H = 0; + REG_TM2CNT_H = 0; + val = ((u32)REG_TM2CNT_L) << 16; + val |= REG_TM1CNT_L; + SeedRng(val); + sTrainerId = Random(); + } + else + { + // Do it exactly like it was originally done, including not stopping + // the timer beforehand. + val = REG_TM1CNT_L; + SeedRng((u16)val); + REG_TM1CNT_H = 0; + sTrainerId = val; + } } u16 GetGeneratedTrainerIdLower(void) diff --git a/src/random.c b/src/random.c index 303bbc9bdf..8e8dae8f3d 100644 --- a/src/random.c +++ b/src/random.c @@ -41,7 +41,8 @@ static void SFC32_Seed(struct Sfc32State *state, u32 seed, u8 stream) } /*This ASM implementation uses some shortcuts and is generally faster on the GBA. -* It's not necessarily faster if inlined, or on other platforms. */ +* It's not necessarily faster if inlined, or on other platforms. +* In addition, it's extremely non-portable. */ u32 NAKED Random32(void) { asm(".thumb\n\ @@ -49,7 +50,7 @@ u32 NAKED Random32(void) mov r6, #11\n\ ldr r5, =gRngValue\n\ ldmia r5!, {r1, r2, r3, r4}\n\ - @ e (result) = a + b + d++\n\ + @ result = a + b + (d+=STREAM1)\n\ add r1, r1, r2\n\ add r0, r1, r4\n\ add r4, r4, #" STR(STREAM1) "\n\ @@ -59,7 +60,7 @@ u32 NAKED Random32(void) @ b = c + (c << 3) [c * 9]\n\ lsl r2, r3, #3\n\ add r2, r2, r3\n\ - @ c = rol(c, 21) + e\n\ + @ c = rol(c, 21) + result\n\ ror r3, r3, r6\n\ add r3, r3, r0\n\ sub r5, r5, #16\n\