From 759fef3928acdf3f70b6933c1b097743ac0b70bf Mon Sep 17 00:00:00 2001 From: rozlette Date: Sun, 1 Mar 2020 14:57:16 -0600 Subject: [PATCH] Some loadfragment2.c progress --- Makefile | 2 +- include/functions.h | 2 +- linker_scripts/code_script.txt | 5 +- src/boot_O1/boot_0x80084940.c | 15 ++++ src/boot_O2/loadfragment2.c | 109 ++++++++++++++++++++++++++++++ tables/files.txt | 2 +- tables/files_with_nonmatching.txt | 2 + tables/functions.txt | 2 +- tables/objects.txt | 3 +- 9 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 src/boot_O1/boot_0x80084940.c create mode 100644 src/boot_O2/loadfragment2.c diff --git a/Makefile b/Makefile index 43344a9b01..130f29325b 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ build/src/boot_O2_g3_trapuv/%: OPTIMIZATION := -O2 -g3 build/src/code/%: CFLAGS := $(CFLAGS) -Wab,-r4300_mul build/src/actors/%: CFLAGS := $(CFLAGS) -Wab,-r4300_mul build/src/boot_O2_g3_trapuv/%: CFLAGS := $(CFLAGS) -trapuv -test.txt: OPTIMIZATION := -O2 -g3 +test.txt: OPTIMIZATION := -O2 test.txt: CC := $(QEMU_IRIX) -L $(IRIX_71_ROOT) $(IRIX_71_ROOT)/usr/bin/cc test.txt: CFLAGS := $(CFLAGS) -Wab,-r4300_mul diff --git a/include/functions.h b/include/functions.h index 3e3820870e..152ed3015e 100644 --- a/include/functions.h +++ b/include/functions.h @@ -112,7 +112,7 @@ void FaultDrawer_Init(void); // func_800848B8 void func_80084940(void); // func_80084940 void func_80084968(void); // func_80084968 void Load_Relocate(u32 allocatedVRamAddr, OverlayBlockSizes* overlayInfo, u32 vRamStart); // func_800849A0 -unsigned int Load_LoadOverlay(u32 vRomStart, int vRomEnd, u32 vRamStart, u32 allocatedVRamAddr, u32 allocatedBytes); // func_80084C0C +s32 Load_LoadOverlay(u32 vRomStart, u32 vRomEnd, u32 vRamStart, u32 allocatedVRamAddr, u32 allocatedBytes); // func_80084C0C void* Load_AllocateAndLoad(u32 vRomStart, u32 vRomEnd, u32 vRamStart); // func_80084CD0 void Load2_Relocate(u32 allocatedVRamAddr, OverlayBlockSizes* overlayInfo, u32 vRamStart); // func_80084DB0 s32 Load2_LoadOverlay(u32 vRomStart, u32 vRomEnd, u32 vRamStart, u32 vRamEnd, u32 allocatedVRamAddr); // func_8008501C diff --git a/linker_scripts/code_script.txt b/linker_scripts/code_script.txt index bdd93bd986..52fc0d4cfb 100644 --- a/linker_scripts/code_script.txt +++ b/linker_scripts/code_script.txt @@ -17,8 +17,9 @@ SECTIONS build/src/boot_O2_g3/boot_0x80081920.o(.text) build/src/boot_O2_g3_trapuv/fault.o(.text) build/src/boot_O2_g3_trapuv/fault_drawer.o(.text) + build/src/boot_O1/boot_0x80084940.o(.text) build/asm/loadfragment.o(.text) - build/asm/loadfragment2.o(.text) + build/src/boot_O2/loadfragment2.o(.text) build/asm/boot_0x80085130.o(.text) build/src/boot_O2/stackcheck.o(.text) build/asm/boot_0x80085570.o(.text) @@ -214,7 +215,7 @@ SECTIONS build/asm/boot_data_fault.o(.text) build/src/boot_O2_g3_trapuv/fault_drawer.o(.data) build/asm/boot_data_loadfragment.o(.text) - build/asm/boot_data_loadfragment2.o(.text) + build/src/boot_O2/loadfragment2.o(.data) build/src/boot_O2/stackcheck.o(.data) build/asm/boot_data_0x80085570.o(.text) build/asm/boot_data_0x800862E0.o(.text) diff --git a/src/boot_O1/boot_0x80084940.c b/src/boot_O1/boot_0x80084940.c new file mode 100644 index 0000000000..b11d8fc577 --- /dev/null +++ b/src/boot_O1/boot_0x80084940.c @@ -0,0 +1,15 @@ +#include +#include + +void func_80084940(void) { + u32 sp; + sp = __osSpGetStatus(); + osDpGetStatus(); +} + +void func_80084968(void) { + func_80084940(); + osDpSetStatus(0x28); + __osSpSetStatus(0x4082); + func_80084940(); +} diff --git a/src/boot_O2/loadfragment2.c b/src/boot_O2/loadfragment2.c new file mode 100644 index 0000000000..1c706b4767 --- /dev/null +++ b/src/boot_O2/loadfragment2.c @@ -0,0 +1,109 @@ +#include +#include + +UNK_TYPE4 D_80096C30 = 2; + +#ifdef NON_MATCHING +// This needs lots of work. Mostly regalloc and getting the address of D_80096C30 placed in s5 at the beginning of the function +void Load2_Relocate(u32 allocatedVRamAddr, OverlayBlockSizes* overlayInfo, u32 vRamStart) { + s32 sectionLocations [4]; + u32* regReferences[32]; + u32 regValues[32]; + u32 i; + u32 relocatedAddress; + s32 signedOffset; + u32* lastInst; + u32* inst; + u32 relocation; + u32 relocationIndex; + + sectionLocations[0] = 0; + sectionLocations[1] = allocatedVRamAddr; + sectionLocations[2] = overlayInfo->textSize + allocatedVRamAddr; + sectionLocations[3] = sectionLocations[2] + overlayInfo->dataSize; + for (i = 0, relocationIndex = 0; i < overlayInfo->amountOfRelocations; relocationIndex++) { + relocation = overlayInfo->relocations[relocationIndex]; + i++; + inst = (u32 *)(sectionLocations[relocation >> 0x1e] + (relocation & 0xffffff)); + + switch (relocation & 0x3f000000) { + case 0x2000000: + if ((*inst & 0xf000000) == 0) { + *inst = (*inst - vRamStart) + allocatedVRamAddr; + } else { + if (D_80096C30 > 2); + } + break; + case 0x4000000: + *inst = (*inst & 0xfc000000) | + (((((*inst & 0x3ffffff) << 2 | 0x80000000) - vRamStart) + allocatedVRamAddr & 0xfffffff) >> 2); + break; + case 0x5000000: + regReferences[*inst >> 0x10 & 0x1f] = inst; + regValues[*inst >> 0x10 & 0x1f] = *inst; + break; + case 0x6000000: + lastInst = regReferences[*inst >> 0x15 & 0x1f]; + signedOffset = (s16)*inst; + if ((signedOffset + *lastInst * 0x10000 & 0xf000000) == 0) { + relocatedAddress = ((signedOffset + regValues[*inst >> 0x15 & 0x1f] * 0x10000) - vRamStart) + allocatedVRamAddr; + *lastInst = (((relocatedAddress >> 0x10) & 0xFFFF) + ((relocatedAddress & 0x8000)? 1 : 0)) | + (*lastInst & 0xffff0000); + *inst = *inst & 0xffff0000 | relocatedAddress & 0xffff; + } + break; + } + } +} +#else +GLOBAL_ASM("./asm/non_matchings/loadfragment2/Load2_Relocate.asm") +#endif + +#ifdef NON_MATCHING +// Very minor stack stuff with a saved value +s32 Load2_LoadOverlay(u32 vRomStart, u32 vRomEnd, u32 vRamStart, u32 vRamEnd, u32 allocatedVRamAddr) { + int nbytes; + u32 pad; + u32 size; + void* end; + OverlayBlockSizes* overlayInfo; + + size = vRomEnd - vRomStart; + + if(1); + + Dmamgr_SendRequestAndWait(allocatedVRamAddr, vRomStart, size); + + end = (void*)(allocatedVRamAddr + size); + overlayInfo = (OverlayBlockSizes*)((int)end - *(int*)((int)end + -4)); + + if(1); + + Load2_Relocate(allocatedVRamAddr, overlayInfo, vRamStart); + + if (overlayInfo->bssSize != 0) { + bzero(end, overlayInfo->bssSize); + } + + nbytes = vRamEnd - vRamStart; + + osWritebackDCache((void*)allocatedVRamAddr, nbytes); + osInvalICache((void*)allocatedVRamAddr, nbytes); + return nbytes; +} +#else +GLOBAL_ASM("./asm/non_matchings/loadfragment2/Load2_LoadOverlay.asm") +#endif + +void* Load2_AllocateAndLoad(u32 vRomStart, u32 vRomEnd, u32 vRamStart, u32 vRamEnd) { + void* allocatedVRamAddr; + u32 size; + + size = vRamEnd - vRamStart; + allocatedVRamAddr = StartHeap_AllocR(size); + if (allocatedVRamAddr != NULL) { + Load2_LoadOverlay(vRomStart, vRomEnd, vRamStart, vRamEnd, (u32)allocatedVRamAddr); + } + + return allocatedVRamAddr; +} diff --git a/tables/files.txt b/tables/files.txt index 623d914bae..b936bb55f8 100644 --- a/tables/files.txt +++ b/tables/files.txt @@ -66,7 +66,7 @@ ('decomp/', 'ovl_En_Floormas', 0x808D0680, ((0x808D3880, 0x808D3E1F),),()), ('decomp/', 'ovl_En_Rd', 0x808D3E20, ((0x808D70C0, 0x808D754F),),()), ('decomp/', 'ovl_Bg_F40_Flift', 0x808D7550, ((0x808D7810, 0x808D78CF),),()), - ('decomp/', 'ovl_Obj_Mure', 0x808D78D0, ((0x808D8760, 0x808D893F),),()), # TODO figure this one out + ('decomp/', 'ovl_Obj_Mure', 0x808D78D0, ((0x808D8760, 0x808D893F),),()), ('decomp/', 'ovl_En_Sw', 0x808D8940, ((0x808DB9C0, 0x808DBE7F),),()), ('decomp/', 'ovl_Object_Kankyo', 0x808DBE80, ((0x808DE320, 0x808DE5AF),),((0x808DE5B0,0x808DE5BF),)), ('decomp/', 'ovl_En_Horse_Link_Child', 0x808DE5C0, ((0x808DFEA0, 0x808E019F),),()), diff --git a/tables/files_with_nonmatching.txt b/tables/files_with_nonmatching.txt index 964bd95279..63a28ba22f 100644 --- a/tables/files_with_nonmatching.txt +++ b/tables/files_with_nonmatching.txt @@ -3,6 +3,8 @@ boot_0x800968B0 fault fault_drawer idle +loadfragment +loadfragment2 stackcheck z_actor z_DLF diff --git a/tables/functions.txt b/tables/functions.txt index 4775a60327..f6c84c4633 100644 --- a/tables/functions.txt +++ b/tables/functions.txt @@ -109,7 +109,7 @@ 0x80084940:("func_80084940","void","void"), 0x80084968:("func_80084968","void","void"), 0x800849A0:("Load_Relocate","void","u32 allocatedVRamAddr, OverlayBlockSizes* overlayInfo, u32 vRamStart"), - 0x80084C0C:("Load_LoadOverlay","unsigned int","u32 vRomStart, int vRomEnd, u32 vRamStart, u32 allocatedVRamAddr, u32 allocatedBytes"), + 0x80084C0C:("Load_LoadOverlay","s32","u32 vRomStart, u32 vRomEnd, u32 vRamStart, u32 allocatedVRamAddr, u32 allocatedBytes"), 0x80084CD0:("Load_AllocateAndLoad","void*","u32 vRomStart, u32 vRomEnd, u32 vRamStart"), 0x80084DB0:("Load2_Relocate","void","u32 allocatedVRamAddr, OverlayBlockSizes* overlayInfo, u32 vRamStart"), 0x8008501C:("Load2_LoadOverlay","s32","u32 vRomStart, u32 vRomEnd, u32 vRamStart, u32 vRamEnd, u32 allocatedVRamAddr"), diff --git a/tables/objects.txt b/tables/objects.txt index 332dcd62be..828efa00e9 100644 --- a/tables/objects.txt +++ b/tables/objects.txt @@ -9,7 +9,8 @@ 0x80081920:"", 0x80081980:"fault", 0x80083EB0:"fault_drawer", - 0x80084940:"loadfragment", + 0x80084940:"", + 0x800849A0:"loadfragment", 0x80084DB0:"loadfragment2", 0x80085130:"", 0x80085320:"stackcheck",