From 4241057135a682a66690ecb89e762b2c4ed27cca Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Mon, 27 Dec 2021 09:56:55 +0200 Subject: [PATCH] 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; + } +}