Skip to content

Commit

Permalink
Use a 32 bit seed for new game seeding in HQ mode. (#4218)
Browse files Browse the repository at this point in the history
Also adjust some comments
  • Loading branch information
tertu-m authored Feb 28, 2024
1 parent 918a0be commit e3d9a19
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
32 changes: 27 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ 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\
push {r4, r5, r6}\n\
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\
Expand All @@ -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\
Expand Down

0 comments on commit e3d9a19

Please sign in to comment.