Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a 32 bit seed for new game seeding in HQ mode. #4218

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,35 @@ void SetMainCallback2(MainCallback callback)

void StartTimer1(void)
{
#if HQ_RANDOM == TRUE
REG_TM2CNT_L = 0;
// Set timer 2 to count timer 1 overflows.
REG_TM2CNT_H = 0x84;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Set timer 2 to count timer 1 overflows.
REG_TM2CNT_H = 0x84;
REG_TM2CNT_H = TIMER_ENABLE | TIMER_COUNTUP;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed both of these.

#endif

REG_TM1CNT_H = 0x80;
}

void SeedRngAndSetTrainerId(void)
{
u16 val = REG_TM1CNT_L;
#if HQ_RANDOM == TRUE
u32 val;
REG_TM1CNT_H = 0;
REG_TM2CNT_H = 0;
val = ((u32)REG_TM2CNT_L) << 16;
val |= REG_TM1CNT_L;
#else
u16 val = REG_TM1CNT_L;
#endif

SeedRng(val);
REG_TM1CNT_H = 0;
sTrainerId = val;

#if HQ_RANDOM == TRUE
sTrainerId = Random();
#else
REG_TM1CNT_H = 0;
sTrainerId = val;
#endif
}

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
Loading