From 4241057135a682a66690ecb89e762b2c4ed27cca Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Mon, 27 Dec 2021 09:56:55 +0200 Subject: [PATCH 1/6] Work on screenDemo.c --- asm/non_matching/demoScreen/sub_080A3198.inc | 34 ----- include/fileScreen.h | 10 +- include/functions.h | 3 + include/screen.h | 5 +- src/demoScreen.c | 137 ++++++++++++++++++- 5 files changed, 148 insertions(+), 41 deletions(-) delete mode 100644 asm/non_matching/demoScreen/sub_080A3198.inc diff --git a/asm/non_matching/demoScreen/sub_080A3198.inc b/asm/non_matching/demoScreen/sub_080A3198.inc deleted file mode 100644 index 449195ea..00000000 --- a/asm/non_matching/demoScreen/sub_080A3198.inc +++ /dev/null @@ -1,34 +0,0 @@ - .syntax unified - push {r4, r5, lr} - ldr r1, _080A31D0 @ =0x08127CC8 - lsls r0, r0, #1 - adds r0, r0, r1 - ldrh r4, [r0] - ldr r1, _080A31D4 @ =gChooseFileState - ldrh r0, [r1, #0x12] - cmp r0, r4 - beq _080A31CC - strh r4, [r1, #0x12] - ldr r5, _080A31D8 @ =something2 - ldr r0, [r5] - subs r0, #0x1e - movs r1, #0xc0 - lsls r1, r1, #1 - bl MemClear - cmp r4, #0 - beq _080A31C6DEMO - adds r0, r4, #0 - adds r1, r5, #0 - bl sub_0805F46C -_080A31C6DEMO: - ldr r1, _080A31DC @ =gScreen - movs r0, #1 - strh r0, [r1, #0xe] -_080A31CC: - pop {r4, r5, pc} - .align 2, 0 -_080A31D0: .4byte gUnk_08127CC8 -_080A31D4: .4byte gChooseFileState -_080A31D8: .4byte gUnk_08127C98 -_080A31DC: .4byte gScreen - .syntax divided diff --git a/include/fileScreen.h b/include/fileScreen.h index 04cfa4ba..50ea8c82 100644 --- a/include/fileScreen.h +++ b/include/fileScreen.h @@ -6,11 +6,17 @@ #include "save.h" typedef struct { - u8 filler0[0x5]; + u8 unk_0x0; + u8 filler0[0x4]; u8 state; u8 subState; u16 timer; - u8 fillerA[0x26]; + u8 fillerA[0x5]; + s16 unk_0x10; + u16 unk_0x12; + u8 fillerB[0xC]; + u8 unk_0x20; + u8 fillerC[0xF]; } ChooseFileState; static_assert(sizeof(ChooseFileState) == 0x30); diff --git a/include/functions.h b/include/functions.h index 7c875323..105228da 100644 --- a/include/functions.h +++ b/include/functions.h @@ -10,6 +10,7 @@ #include "room.h" #include "structures.h" #include "script.h" +#include "screen.h" // Identified - to be sorted into header files extern u32 CheckKinstoneFused(u32); @@ -223,4 +224,6 @@ extern void sub_080806BC(u32, u32, u32, u32); extern void LoadObjPalette(Entity*, u32); +extern void sub_080ADA04(OAMCommand*, void*); + #endif diff --git a/include/screen.h b/include/screen.h index 18962f80..53f56b89 100644 --- a/include/screen.h +++ b/include/screen.h @@ -70,15 +70,16 @@ typedef struct { /*0x78*/ u32 _78; } Screen; -struct OAMCommand { +typedef struct { u16 x; u16 y; u16 _4; u16 _6; u16 _8; -} extern gOamCmd; +} OAMCommand; extern BgControls gBgControls; extern Screen gScreen; +extern OAMCommand gOamCmd; #endif diff --git a/src/demoScreen.c b/src/demoScreen.c index b10bd35f..af34550a 100644 --- a/src/demoScreen.c +++ b/src/demoScreen.c @@ -21,6 +21,12 @@ extern void gUnk_089FD2F4; extern u8 gUnk_02000006; +extern u16 gUnk_08127CC8[4]; +extern void* gUnk_08127C98; +extern u8 gUnk_08A068BF[129]; + +extern s32 __modsi3(s32, s32); + void HandleChooseDemoScreen(void) { FlushSprites(); demoFunctions[gMain.funcIndex](); @@ -89,8 +95,133 @@ void sub_080A2F8C(void) { } } -ASM_FUNC("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void)) +// The condition on the left dpad is very convuluted for some reason (instead oj just an equality check). +// This causes the entire function heirarchy to change on a whim and I couldn't quite figure out +// the correct conditions/code to make it like the original function. +NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void)) { + u32 tmp; + u32 tmp2; + u32 val; + s32 tmp3; + u32 unk_0x0; + u32 tmp4; + u32 keys; -ASM_FUNC("asm/non_matching/demoScreen/sub_080A30AC.inc", void sub_080A30AC(void)) + if (gFadeControl.active == 0) { + val = 0; + keys = gInput.heldKeys; + if (keys == DPAD_RIGHT) { + if (gChooseFileState.unk_0x0 == 0) { + val = 1; + gChooseFileState.unk_0x20 = 4; + SoundReq(SFX_TEXTBOX_CHOICE); + } + } + else if ((keys) > 0x10) { + if ((keys) != 0x40) { + if ((keys) <= (s16)0x40) { + if (keys == DPAD_LEFT) { + if (gChooseFileState.unk_0x0 == 0) { + val = -1; + gChooseFileState.unk_0x20 = 0xfc; + SoundReq(SFX_TEXTBOX_CHOICE); + } + } + } + } + } + else if ((keys) == 0x1 || (keys) == 0x8) { + if (gChooseFileState.unk_0x0 == 0) { + gMain.screen = 2; + gMain.funcIndex = gChooseFileState.unk_0x0; + SoundReq(SFX_TEXTBOX_SELECT); + } + } + tmp4 = (0x80 << 0x12); + tmp = *(u8*)(tmp4 + 4); + tmp2 = val + 3; + tmp += tmp2; + *(u8*)(tmp4 + 4) = __modsi3(tmp, 3); -ASM_FUNC("asm/non_matching/demoScreen/sub_080A3198.inc", void sub_080A3198(u32 param_1, u32 param_2)) + tmp3 = gChooseFileState.unk_0x10; + + tmp = *(u8*)(tmp4 + 4); + tmp *= 0x68; + + if (tmp != tmp3) { + tmp4 = gChooseFileState.unk_0x20; + tmp4 <<= 18; + tmp4 >>= 18; + tmp4 += tmp3; + gChooseFileState.unk_0x10 = __modsi3(tmp4 + (0x9c << 1), 0x9c << 1); + unk_0x0 = 1; + } + else { + unk_0x0 = 0; + } + + gChooseFileState.unk_0x0 = unk_0x0; + tmp4 = (0x80 << 0x12); + sub_080A3198(*(u8*)(tmp4 + 4), 0); + } +} +END_NONMATCH + +NONMATCH("asm/non_matching/demoScreen/sub_080A30AC.inc", void sub_080A30AC(void)) { + u32 unk_0x10; + u8* ptr; + u8* currentPtr; + u32 offset; + u32 xoffset; + gOamCmd._4 = 0x2000; + gOamCmd._6 = 0; + gOamCmd._8 = 0xc00; + gOamCmd.y = 0x40; + unk_0x10 = gChooseFileState.unk_0x10; + gOamCmd.x = 0xFFFFFED8 - unk_0x10; + offset = *(u32*)gUnk_08A068BF; + ptr = gUnk_08A068BF - 0xc; + sub_080ADA04(&gOamCmd, ptr + offset); + // FIX: original assembly uses r1 for xoffset for no apparent reason. Could not make it compile to do the same. + // Maybe the original code was some sort of loop unrolling. + xoffset = -0xc0; + gOamCmd.x = xoffset - unk_0x10; + sub_080ADA04(&gOamCmd, ptr + *(u32*)(ptr + 4)); + xoffset = -0x58; + gOamCmd.x = xoffset - unk_0x10; + sub_080ADA04(&gOamCmd, ptr + *(u32*)(ptr + 8)); + xoffset = 0x10; + gOamCmd.x = xoffset - unk_0x10; + sub_080ADA04(&gOamCmd, ptr + *(u32*)(gUnk_08A068BF)); + xoffset = 0x78; + gOamCmd.x = xoffset - unk_0x10; + sub_080ADA04(&gOamCmd, ptr + *(u32*)(ptr + 4)); + xoffset = 0xe0; + gOamCmd.x = xoffset - unk_0x10; + sub_080ADA04(&gOamCmd, ptr + *(u32*)(ptr + 8)); + xoffset = 0xa4 << 1; + gOamCmd.x = xoffset - unk_0x10; + sub_080ADA04(&gOamCmd, ptr + *(u32*)(gUnk_08A068BF)); + xoffset = 0xd8 << 1; + gOamCmd.x = xoffset - unk_0x10; + sub_080ADA04(&gOamCmd, ptr + *(u32*)(ptr + 4)); + xoffset = 0x86 << 2; + gOamCmd.x = xoffset - unk_0x10; + sub_080ADA04(&gOamCmd, ptr + *(u32*)(ptr + 8)); +} +END_NONMATCH + +void sub_080A3198(u32 param_1, u32 param_2) { + u16 r4 = gUnk_08127CC8[param_1]; + + if (gChooseFileState.unk_0x12 != r4) { + gChooseFileState.unk_0x12 = r4; + MemClear(gUnk_08127C98 - 0x1e, 0x180); + + if (r4 != 0) { + sub_0805F46C(r4, &gUnk_08127C98); + } + + gScreen.bg0.updated = 1; + } +} From 930979a5cac624cac6cc5ea2385c52d1a532518f Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Mon, 27 Dec 2021 10:25:32 +0200 Subject: [PATCH 2/6] clang format --- include/fileScreen.h | 4 ++-- src/demoScreen.c | 13 +++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/include/fileScreen.h b/include/fileScreen.h index 50ea8c82..8d67cf82 100644 --- a/include/fileScreen.h +++ b/include/fileScreen.h @@ -14,9 +14,9 @@ typedef struct { u8 fillerA[0x5]; s16 unk_0x10; u16 unk_0x12; - u8 fillerB[0xC]; + u8 fillerB[0xC]; u8 unk_0x20; - u8 fillerC[0xF]; + u8 fillerC[0xF]; } ChooseFileState; static_assert(sizeof(ChooseFileState) == 0x30); diff --git a/src/demoScreen.c b/src/demoScreen.c index af34550a..a7fa34d9 100644 --- a/src/demoScreen.c +++ b/src/demoScreen.c @@ -116,8 +116,7 @@ NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void) gChooseFileState.unk_0x20 = 4; SoundReq(SFX_TEXTBOX_CHOICE); } - } - else if ((keys) > 0x10) { + } else if ((keys) > 0x10) { if ((keys) != 0x40) { if ((keys) <= (s16)0x40) { if (keys == DPAD_LEFT) { @@ -129,8 +128,7 @@ NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void) } } } - } - else if ((keys) == 0x1 || (keys) == 0x8) { + } else if ((keys) == 0x1 || (keys) == 0x8) { if (gChooseFileState.unk_0x0 == 0) { gMain.screen = 2; gMain.funcIndex = gChooseFileState.unk_0x0; @@ -144,7 +142,7 @@ NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void) *(u8*)(tmp4 + 4) = __modsi3(tmp, 3); tmp3 = gChooseFileState.unk_0x10; - + tmp = *(u8*)(tmp4 + 4); tmp *= 0x68; @@ -155,8 +153,7 @@ NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void) tmp4 += tmp3; gChooseFileState.unk_0x10 = __modsi3(tmp4 + (0x9c << 1), 0x9c << 1); unk_0x0 = 1; - } - else { + } else { unk_0x0 = 0; } @@ -221,7 +218,7 @@ void sub_080A3198(u32 param_1, u32 param_2) { if (r4 != 0) { sub_0805F46C(r4, &gUnk_08127C98); } - + gScreen.bg0.updated = 1; } } From b4bb2df30ad4f0156768735c26912c0c5b9a8359 Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Mon, 27 Dec 2021 11:31:57 +0200 Subject: [PATCH 3/6] Fix switch and mod --- src/demoScreen.c | 64 ++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/demoScreen.c b/src/demoScreen.c index a7fa34d9..c38296e4 100644 --- a/src/demoScreen.c +++ b/src/demoScreen.c @@ -25,8 +25,6 @@ extern u16 gUnk_08127CC8[4]; extern void* gUnk_08127C98; extern u8 gUnk_08A068BF[129]; -extern s32 __modsi3(s32, s32); - void HandleChooseDemoScreen(void) { FlushSprites(); demoFunctions[gMain.funcIndex](); @@ -99,61 +97,63 @@ void sub_080A2F8C(void) { // This causes the entire function heirarchy to change on a whim and I couldn't quite figure out // the correct conditions/code to make it like the original function. NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void)) { - u32 tmp; + s32 tmp; u32 tmp2; u32 val; s32 tmp3; u32 unk_0x0; u32 tmp4; + s32 tmp5; u32 keys; if (gFadeControl.active == 0) { val = 0; keys = gInput.heldKeys; - if (keys == DPAD_RIGHT) { - if (gChooseFileState.unk_0x0 == 0) { - val = 1; - gChooseFileState.unk_0x20 = 4; - SoundReq(SFX_TEXTBOX_CHOICE); - } - } else if ((keys) > 0x10) { - if ((keys) != 0x40) { - if ((keys) <= (s16)0x40) { - if (keys == DPAD_LEFT) { - if (gChooseFileState.unk_0x0 == 0) { - val = -1; - gChooseFileState.unk_0x20 = 0xfc; - SoundReq(SFX_TEXTBOX_CHOICE); - } - } + switch (keys) { + case DPAD_RIGHT: + { + if (gChooseFileState.unk_0x0 == 0) { + val = 1; + gChooseFileState.unk_0x20 = 4; + SoundReq(SFX_TEXTBOX_CHOICE); } } - } else if ((keys) == 0x1 || (keys) == 0x8) { - if (gChooseFileState.unk_0x0 == 0) { - gMain.screen = 2; - gMain.funcIndex = gChooseFileState.unk_0x0; - SoundReq(SFX_TEXTBOX_SELECT); + case DPAD_LEFT: + { + if (gChooseFileState.unk_0x0 == 0) { + val = -1; + gChooseFileState.unk_0x20 = 0xfc; + SoundReq(SFX_TEXTBOX_CHOICE); + } + } + case START_BUTTON: + case A_BUTTON: + { + if (gChooseFileState.unk_0x0 == 0) { + gMain.screen = 2; + gMain.funcIndex = gChooseFileState.unk_0x0; + SoundReq(SFX_TEXTBOX_SELECT); + } } } tmp4 = (0x80 << 0x12); tmp = *(u8*)(tmp4 + 4); tmp2 = val + 3; tmp += tmp2; - *(u8*)(tmp4 + 4) = __modsi3(tmp, 3); + *(u8*)(tmp4 + 4) = tmp % 3; tmp3 = gChooseFileState.unk_0x10; tmp = *(u8*)(tmp4 + 4); tmp *= 0x68; - if (tmp != tmp3) { - tmp4 = gChooseFileState.unk_0x20; - tmp4 <<= 18; - tmp4 >>= 18; - tmp4 += tmp3; - gChooseFileState.unk_0x10 = __modsi3(tmp4 + (0x9c << 1), 0x9c << 1); + if (tmp != tmp3) { + tmp5 = gChooseFileState.unk_0x20; + tmp5 += tmp3; + gChooseFileState.unk_0x10 = (tmp5 + (0x9c << 1)) % (0x9c << 1); unk_0x0 = 1; - } else { + } + else { unk_0x0 = 0; } From 5ddb348cebe6a32e178574b1e7e9392b93ff1b90 Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Mon, 27 Dec 2021 16:09:31 +0200 Subject: [PATCH 4/6] Use gSaveHeader --- src/demoScreen.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/demoScreen.c b/src/demoScreen.c index c38296e4..2d0ef585 100644 --- a/src/demoScreen.c +++ b/src/demoScreen.c @@ -136,15 +136,14 @@ NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void) } } } - tmp4 = (0x80 << 0x12); - tmp = *(u8*)(tmp4 + 4); + tmp = gSaveHeader->saveFileId; tmp2 = val + 3; tmp += tmp2; - *(u8*)(tmp4 + 4) = tmp % 3; + gSaveHeader->saveFileId = tmp % 3; tmp3 = gChooseFileState.unk_0x10; - tmp = *(u8*)(tmp4 + 4); + tmp = gSaveHeader->saveFileId; tmp *= 0x68; if (tmp != tmp3) { @@ -158,8 +157,7 @@ NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void) } gChooseFileState.unk_0x0 = unk_0x0; - tmp4 = (0x80 << 0x12); - sub_080A3198(*(u8*)(tmp4 + 4), 0); + sub_080A3198(gSaveHeader->saveFileId, 0); } } END_NONMATCH From f4b261415c39a1803c8bf089d047736c4b763b33 Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Tue, 28 Dec 2021 05:41:26 +0200 Subject: [PATCH 5/6] Move sub_080ADA04 to screen.h --- include/functions.h | 2 -- include/screen.h | 2 ++ src/demoScreen.c | 14 +++++--------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/include/functions.h b/include/functions.h index 105228da..77def4f0 100644 --- a/include/functions.h +++ b/include/functions.h @@ -224,6 +224,4 @@ extern void sub_080806BC(u32, u32, u32, u32); extern void LoadObjPalette(Entity*, u32); -extern void sub_080ADA04(OAMCommand*, void*); - #endif diff --git a/include/screen.h b/include/screen.h index 53f56b89..e057442a 100644 --- a/include/screen.h +++ b/include/screen.h @@ -82,4 +82,6 @@ extern BgControls gBgControls; extern Screen gScreen; extern OAMCommand gOamCmd; +extern void sub_080ADA04(OAMCommand*, void*); + #endif diff --git a/src/demoScreen.c b/src/demoScreen.c index 2d0ef585..efc3368d 100644 --- a/src/demoScreen.c +++ b/src/demoScreen.c @@ -110,16 +110,14 @@ NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void) val = 0; keys = gInput.heldKeys; switch (keys) { - case DPAD_RIGHT: - { + case DPAD_RIGHT: { if (gChooseFileState.unk_0x0 == 0) { val = 1; gChooseFileState.unk_0x20 = 4; SoundReq(SFX_TEXTBOX_CHOICE); } } - case DPAD_LEFT: - { + case DPAD_LEFT: { if (gChooseFileState.unk_0x0 == 0) { val = -1; gChooseFileState.unk_0x20 = 0xfc; @@ -127,8 +125,7 @@ NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void) } } case START_BUTTON: - case A_BUTTON: - { + case A_BUTTON: { if (gChooseFileState.unk_0x0 == 0) { gMain.screen = 2; gMain.funcIndex = gChooseFileState.unk_0x0; @@ -146,13 +143,12 @@ NONMATCH("asm/non_matching/demoScreen/sub_080A2FD0.inc", void sub_080A2FD0(void) tmp = gSaveHeader->saveFileId; tmp *= 0x68; - if (tmp != tmp3) { + if (tmp != tmp3) { tmp5 = gChooseFileState.unk_0x20; tmp5 += tmp3; gChooseFileState.unk_0x10 = (tmp5 + (0x9c << 1)) % (0x9c << 1); unk_0x0 = 1; - } - else { + } else { unk_0x0 = 0; } From 2fcf0b0a82e009652d6d158b00420c5da7994c8d Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Tue, 28 Dec 2021 05:42:53 +0200 Subject: [PATCH 6/6] Remove screen.h include from function.h --- include/functions.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/functions.h b/include/functions.h index 77def4f0..7c875323 100644 --- a/include/functions.h +++ b/include/functions.h @@ -10,7 +10,6 @@ #include "room.h" #include "structures.h" #include "script.h" -#include "screen.h" // Identified - to be sorted into header files extern u32 CheckKinstoneFused(u32);