From e2b61f88e46efc994db334ac5f03e1174de67058 Mon Sep 17 00:00:00 2001 From: mzxrules Date: Fri, 13 Jan 2023 22:29:50 -0500 Subject: [PATCH] sys_cmpdata OK (#1065) * sys_cmpdata decomp * implement suggestions * sys_cmpdata OK * format * implement suggestion * make bss data public * format bleh --- include/color.h | 20 +++ include/functions.h | 8 +- include/variables.h | 3 +- spec | 2 +- src/boot_O2_g3/yaz0.c | 4 +- src/code/sys_cfb.c | 20 +++ src/code/sys_cmpdma.c | 115 +++++++++++++++--- .../ovl_kaleido_scope/z_kaleido_scope_NES.c | 17 +-- tools/disasm/files.txt | 3 +- tools/disasm/functions.txt | 10 +- tools/disasm/variables.txt | 4 +- tools/sizes/code_functions.csv | 10 +- 12 files changed, 169 insertions(+), 47 deletions(-) create mode 100644 src/code/sys_cfb.c diff --git a/include/color.h b/include/color.h index d0cdb62f58..b49ab8d78e 100644 --- a/include/color.h +++ b/include/color.h @@ -51,6 +51,26 @@ typedef union { u16 rgba; } Color_RGBA16; +typedef union { + struct { + u32 r : 5; + u32 g : 5; + u32 b : 5; + u32 a : 1; + }; + u16 rgba; +} Color_RGBA16_2; + +typedef union{ + struct { + u32 r : 3; + u32 g : 3; + u32 b : 3; + u32 a : 5; + }; + u16 rgba; +} Color_RGBA14; + #define RGBA8(r, g, b, a) ((((r) & 0xFF) << 24) | (((g) & 0xFF) << 16) | (((b) & 0xFF) << 8) | (((a) & 0xFF) << 0)) #endif diff --git a/include/functions.h b/include/functions.h index 5e74e635fd..88b8f25032 100644 --- a/include/functions.h +++ b/include/functions.h @@ -2565,12 +2565,8 @@ u32 SysCfb_GetZBuffer(void); // UNK_TYPE4 func_80178A24(void); // void func_80178A34(void); s32 func_80178A94(s32 param_1, s32 param_2); -// void func_80178AC0(void); -// void func_80178C80(UNK_TYPE1 param_1, UNK_TYPE1 param_2, UNK_TYPE1 param_3, UNK_TYPE1 param_4, UNK_TYPE4 param_5); -// void func_80178D7C(void); -// void func_80178DAC(void); -void func_80178E3C(u8*, s32, s32, s32); -void func_80178E7C(uintptr_t arg0, void* arg1, size_t size); +void CmpDma_LoadFile(uintptr_t segmentVrom, s32 id, void* dst, size_t size); +void CmpDma_LoadAllFiles(uintptr_t segmentVrom, void* dst, size_t size); // void Check_WriteRGBA16Pixel(u16* buffer, u32 x, u32 y, u32 value); // void Check_WriteI4Pixel(u16* buffer, u32 x, u32 y, u32 value); // void Check_DrawI4Texture(u16* buffer, u32 x, u32 y, u32 width, u32 height, u8* texture); diff --git a/include/variables.h b/include/variables.h index 9381fc7e2a..7cc0f3b286 100644 --- a/include/variables.h +++ b/include/variables.h @@ -308,7 +308,7 @@ extern u8* sYaz0CurDataEnd; extern u32 sYaz0CurRomStart; extern u32 sYaz0CurSize; extern u8* sYaz0MaxPtr; -extern u8* D_8009BE20; +extern void* gYaz0DecompressDstEnd; // extern UNK_TYPE4 D_8009BE30; // extern UNK_TYPE4 D_8009BE34; // extern FaultClient romInfoFaultClient; @@ -2574,7 +2574,6 @@ extern UNK_TYPE2 D_801FBBCE; // extern UNK_TYPE1 D_801FBBD0; // extern UNK_TYPE1 D_801FBBD2; // extern UNK_TYPE1 D_801FBBD4; -// extern UNK_TYPE1 D_801FBBE0; extern Vec3f D_801FBBF0; extern LineSegment Math3D_ColSphereTri_line; extern Vec3f Math3D_ColSphereTri_point; diff --git a/spec b/spec index ba217e15b1..9ce938a495 100644 --- a/spec +++ b/spec @@ -581,9 +581,9 @@ beginseg include "build/data/code/speed_meter.data.o" include "build/data/code/speed_meter.bss.o" include "build/src/code/su_mtx.o" + include "build/src/code/sys_cfb.o" include "build/data/code/sys_cfb.bss.o" include "build/src/code/sys_cmpdma.o" - include "build/data/code/sys_cmpdma.bss.o" include "build/src/code/sys_initial_check.o" include "build/src/code/sys_math.o" include "build/src/code/sys_math3d.o" diff --git a/src/boot_O2_g3/yaz0.c b/src/boot_O2_g3/yaz0.c index 63260e8853..aa7ecdfe12 100644 --- a/src/boot_O2_g3/yaz0.c +++ b/src/boot_O2_g3/yaz0.c @@ -6,7 +6,7 @@ u8* sYaz0CurDataEnd; uintptr_t sYaz0CurRomStart; u32 sYaz0CurSize; u8* sYaz0MaxPtr; -u8* D_8009BE20; +void* gYaz0DecompressDstEnd; void* Yaz0_FirstDMA() { u32 pad0; @@ -118,7 +118,7 @@ s32 Yaz0_DecompressImpl(u8* src, u8* dst) { bitIdx--; } while (dst != dstEnd); - D_8009BE20 = dstEnd; + gYaz0DecompressDstEnd = dstEnd; return 0; } diff --git a/src/code/sys_cfb.c b/src/code/sys_cfb.c new file mode 100644 index 0000000000..3f198ce8fd --- /dev/null +++ b/src/code/sys_cfb.c @@ -0,0 +1,20 @@ +#include "global.h" +#include "system_malloc.h" + +#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cfb/func_80178750.s") + +#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cfb/func_80178818.s") + +#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cfb/func_80178978.s") + +#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cfb/func_801789D4.s") + +#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cfb/SysCfb_GetFbPtr.s") + +#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cfb/SysCfb_GetZBuffer.s") + +#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cfb/func_80178A24.s") + +#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cfb/func_80178A34.s") + +#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cfb/func_80178A94.s") diff --git a/src/code/sys_cmpdma.c b/src/code/sys_cmpdma.c index 2533c44f5e..b2a17d5477 100644 --- a/src/code/sys_cmpdma.c +++ b/src/code/sys_cmpdma.c @@ -1,32 +1,117 @@ #include "global.h" #include "system_malloc.h" -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178750.s") +typedef struct { + union { + u32 dmaWord[2]; + u32 dataStart; + u32 dataSize; + struct { + u32 start; + u32 end; + } offset; + }; +} CmpDmaBuffer; -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178818.s") +CmpDmaBuffer sDmaBuffer; -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178978.s") +void func_80178AC0(u16* src, void* dst, size_t size) { + Color_RGBA8_u32 spC; + Color_RGBA16_2 tc; + Color_RGBA14 tc2; + u32* dstCur = dst; + u16* src16 = src; -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_801789D4.s") + while (((uintptr_t)dstCur) - size < ((uintptr_t)dst)) { + tc.rgba = *(src16++); + if (tc.a == 1) { + spC.r = (tc.r * 255) / 31; + spC.g = (tc.g * 255) / 31; + spC.b = (tc.b * 255) / 31; + spC.a = 255; + } else if (tc.rgba == 0) { + spC.rgba = 0; + } else { + tc2.rgba = tc.rgba; + tc.rgba = *(src16++); + spC.r = (tc.r << 3) | tc2.r; + spC.g = (tc.g << 3) | tc2.g; + spC.b = (tc.b << 3) | tc2.b; + spC.a = ((tc.rgba & 1) << 7) | ((tc2.a * 127) / 63); + } + *(dstCur++) = spC.rgba; + } +} -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/SysCfb_GetFbPtr.s") +void CmpDma_GetFileInfo(u8* segmentRom, s32 id, uintptr_t* outFileRom, size_t* size, s32* flag) { + u32 dataStart; + u32 refOff; -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/SysCfb_GetZBuffer.s") + DmaMgr_DmaRomToRam(segmentRom, &sDmaBuffer.dataStart, sizeof(sDmaBuffer.dataStart)); -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178A24.s") + dataStart = sDmaBuffer.dataStart; + refOff = id * sizeof(u32); -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178A34.s") + // if id is >= idMax + if (refOff > (dataStart - 4)) { + *outFileRom = segmentRom; + *size = 0; + } else if (refOff == 0) { + // get offset start of next file, i.e. size of first file + DmaMgr_DmaRomToRam(segmentRom + 4, &sDmaBuffer.dataSize, sizeof(sDmaBuffer.dataSize)); + *outFileRom = segmentRom + dataStart; + *size = sDmaBuffer.dataSize; + } else { + // get offset start, end from dataStart + DmaMgr_DmaRomToRam(refOff + segmentRom, &sDmaBuffer.offset, sizeof(sDmaBuffer.offset)); + *outFileRom = sDmaBuffer.offset.start + segmentRom + dataStart; + *size = sDmaBuffer.offset.end - sDmaBuffer.offset.start; + } + *flag = 0; +} -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178A94.s") +void CmpDma_Decompress(uintptr_t romStart, size_t size, void* dst) { + if (size != 0) { + Yaz0_Decompress(romStart, dst, size); + } +} -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178AC0.s") +void CmpDma_LoadFileImpl(uintptr_t segmentRom, s32 id, void* dst, size_t size) { + uintptr_t romStart; + size_t compressedSize; + s32 flag; -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178C80.s") + CmpDma_GetFileInfo(segmentRom, id, &romStart, &compressedSize, &flag); + if (flag & 1) { + void* tempBuf = SystemArena_Malloc(0x1000); -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178D7C.s") + CmpDma_Decompress(romStart, compressedSize, tempBuf); + func_80178AC0(tempBuf, dst, size); + SystemArena_Free(tempBuf); + } else { + CmpDma_Decompress(romStart, compressedSize, dst); + } +} -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178DAC.s") +void CmpDma_LoadFile(uintptr_t segmentVrom, s32 id, void* dst, size_t size) { + CmpDma_LoadFileImpl(DmaMgr_TranslateVromToRom(segmentVrom), id, dst, size); +} -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178E3C.s") +void CmpDma_LoadAllFiles(uintptr_t segmentVrom, void* dst, size_t size) { + uintptr_t rom = DmaMgr_TranslateVromToRom(segmentVrom); + u32 i; + u32 end; + void* nextDst; + u32 dataStart; -#pragma GLOBAL_ASM("asm/non_matchings/code/sys_cmpdma/func_80178E7C.s") + DmaMgr_DmaRomToRam(rom, &sDmaBuffer.dataStart, sizeof(sDmaBuffer.dataStart)); + + dataStart = sDmaBuffer.dataStart; + nextDst = dst; + end = (dataStart / sizeof(u32)) - 1; + + for (i = 0; i < end; i++) { + CmpDma_LoadFileImpl(rom, i, nextDst, 0); + nextDst = gYaz0DecompressDstEnd; + } +} diff --git a/src/overlays/kaleido_scope/ovl_kaleido_scope/z_kaleido_scope_NES.c b/src/overlays/kaleido_scope/ovl_kaleido_scope/z_kaleido_scope_NES.c index a56afd71b3..5a47289afb 100644 --- a/src/overlays/kaleido_scope/ovl_kaleido_scope/z_kaleido_scope_NES.c +++ b/src/overlays/kaleido_scope/ovl_kaleido_scope/z_kaleido_scope_NES.c @@ -461,15 +461,15 @@ f32 D_8082BE28[] = { s16 D_8082BE84 = 0; void func_80821900(void* segment, u32 texIndex) { - func_80178E3C(SEGMENT_ROM_START(map_name_static), texIndex, segment, 0x400); + CmpDma_LoadFile(SEGMENT_ROM_START(map_name_static), texIndex, segment, 0x400); } void func_8082192C(void* segment, u32 texIndex) { - func_80178E3C(SEGMENT_ROM_START(map_name_static), texIndex, segment, 0xA00); + CmpDma_LoadFile(SEGMENT_ROM_START(map_name_static), texIndex, segment, 0xA00); } void func_80821958(void* segment, u32 texIndex) { - func_80178E3C(SEGMENT_ROM_START(item_name_static), texIndex, segment, 0x400); + CmpDma_LoadFile(SEGMENT_ROM_START(item_name_static), texIndex, segment, 0x400); } #pragma GLOBAL_ASM("asm/non_matchings/overlays/ovl_kaleido_scope/KaleidoScope_MoveCursorToSpecialPos.s") @@ -611,7 +611,7 @@ void KaleidoScope_Update(PlayState* play) { pauseCtx->iconItemSegment = (void*)ALIGN16((uintptr_t)play->objectCtx.spaceStart); size0 = SEGMENT_ROM_SIZE(icon_item_static_old); - func_80178E7C((uintptr_t)SEGMENT_ROM_START(icon_item_static_test), pauseCtx->iconItemSegment, size0); + CmpDma_LoadAllFiles((uintptr_t)SEGMENT_ROM_START(icon_item_static_test), pauseCtx->iconItemSegment, size0); gSegments[0x08] = PHYSICAL_TO_VIRTUAL(pauseCtx->iconItemSegment); @@ -623,7 +623,8 @@ void KaleidoScope_Update(PlayState* play) { pauseCtx->iconItem24Segment = (void*)ALIGN16((uintptr_t)pauseCtx->iconItemSegment + size0); size1 = SEGMENT_ROM_SIZE(icon_item_24_static_old); - func_80178E7C((uintptr_t)SEGMENT_ROM_START(icon_item_24_static_test), pauseCtx->iconItem24Segment, size1); + CmpDma_LoadAllFiles((uintptr_t)SEGMENT_ROM_START(icon_item_24_static_test), pauseCtx->iconItem24Segment, + size1); pauseCtx->iconItemAltSegment = (void*)ALIGN16((uintptr_t)pauseCtx->iconItem24Segment + size1); if (func_8010A0A4(play)) { @@ -932,11 +933,11 @@ void KaleidoScope_Update(PlayState* play) { pauseCtx->iconItemSegment = (void*)(((uintptr_t)play->objectCtx.spaceStart + 0x30) & ~0x3F); // Messed up ALIGN64 size0 = SEGMENT_ROM_SIZE(icon_item_static_old); - func_80178E7C(SEGMENT_ROM_START(icon_item_static_test), pauseCtx->iconItemSegment, size0); + CmpDma_LoadAllFiles(SEGMENT_ROM_START(icon_item_static_test), pauseCtx->iconItemSegment, size0); pauseCtx->iconItem24Segment = (void*)ALIGN16((uintptr_t)pauseCtx->iconItemSegment + size0); size1 = SEGMENT_ROM_SIZE(icon_item_24_static_old); - func_80178E7C(SEGMENT_ROM_START(icon_item_24_static_test), pauseCtx->iconItem24Segment, size1); + CmpDma_LoadAllFiles(SEGMENT_ROM_START(icon_item_24_static_test), pauseCtx->iconItem24Segment, size1); pauseCtx->iconItemAltSegment = (void*)ALIGN16((uintptr_t)pauseCtx->iconItem24Segment + size1); size2 = SEGMENT_ROM_SIZE(icon_item_gameover_static); @@ -1179,7 +1180,7 @@ void KaleidoScope_Update(PlayState* play) { pauseCtx->iconItemSegment = (void*)ALIGN16((uintptr_t)play->objectCtx.spaceStart); size0 = SEGMENT_ROM_SIZE(icon_item_static_old); - func_80178E7C(SEGMENT_ROM_START(icon_item_static_test), pauseCtx->iconItemSegment, size0); + CmpDma_LoadAllFiles(SEGMENT_ROM_START(icon_item_static_test), pauseCtx->iconItemSegment, size0); pauseCtx->iconItemAltSegment = (void*)ALIGN16((uintptr_t)pauseCtx->iconItemSegment + size0); sInDungeonScene = false; diff --git a/tools/disasm/files.txt b/tools/disasm/files.txt index 340a773d63..2a79674bf6 100644 --- a/tools/disasm/files.txt +++ b/tools/disasm/files.txt @@ -466,7 +466,8 @@ 0x80176280 : "sched", 0x80177390 : "speed_meter", 0x801780F0 : "su_mtx", - 0x80178750 : "sys_cmpdma", + 0x80178750 : "sys_cfb", + 0x80178AC0 : "sys_cmpdma", 0x80178F30 : "sys_initial_check", 0x80179300 : "sys_math", 0x801795F0 : "sys_math3d", diff --git a/tools/disasm/functions.txt b/tools/disasm/functions.txt index 633b73c487..493f05f563 100644 --- a/tools/disasm/functions.txt +++ b/tools/disasm/functions.txt @@ -3235,11 +3235,11 @@ 0x80178A34:("func_80178A34",), 0x80178A94:("func_80178A94",), 0x80178AC0:("func_80178AC0",), - 0x80178C80:("func_80178C80",), - 0x80178D7C:("func_80178D7C",), - 0x80178DAC:("func_80178DAC",), - 0x80178E3C:("func_80178E3C",), - 0x80178E7C:("func_80178E7C",), + 0x80178C80:("CmpDma_GetFileInfo",), + 0x80178D7C:("CmpDma_Decompress",), + 0x80178DAC:("CmpDma_LoadFileImpl",), + 0x80178E3C:("CmpDma_LoadFile",), + 0x80178E7C:("CmpDma_LoadAllFiles",), 0x80178F30:("Check_WriteRGBA16Pixel",), 0x80178F60:("Check_WriteI4Pixel",), 0x80178FA0:("Check_DrawI4Texture",), diff --git a/tools/disasm/variables.txt b/tools/disasm/variables.txt index 4bdd58c066..ea77170f9a 100644 --- a/tools/disasm/variables.txt +++ b/tools/disasm/variables.txt @@ -311,7 +311,7 @@ 0x8009BE14:("sYaz0CurRomStart","u32","",0x4), 0x8009BE18:("sYaz0CurSize","u32","",0x4), 0x8009BE1C:("sYaz0MaxPtr","u8*","",0x4), - 0x8009BE20:("D_8009BE20","u8*","",0x4), + 0x8009BE20:("gYaz0DecompressDstEnd","void*","",0x4), 0x8009BE30:("D_8009BE30","UNK_TYPE4","",0x4), 0x8009BE34:("D_8009BE34","UNK_TYPE4","",0x4), 0x8009BE38:("romInfoFaultClient","FaultClient","",0x10), @@ -4139,7 +4139,7 @@ 0x801FBBD0:("D_801FBBD0","UNK_TYPE1","",0x1), 0x801FBBD2:("D_801FBBD2","UNK_TYPE1","",0x1), 0x801FBBD4:("D_801FBBD4","UNK_TYPE1","",0x1), - 0x801FBBE0:("D_801FBBE0","UNK_TYPE1","",0x1), + 0x801FBBE0:("sDmaBuffer","u32","[2]",0x8), 0x801FBBF0:("D_801FBBF0","Vec3f","",0xc), 0x801FBC00:("Math3D_ColSphereTri_line","LineSegment","",0x18), 0x801FBC18:("Math3D_ColSphereTri_point","Vec3f","",0xc), diff --git a/tools/sizes/code_functions.csv b/tools/sizes/code_functions.csv index afb03d3b76..a4d64acf1b 100644 --- a/tools/sizes/code_functions.csv +++ b/tools/sizes/code_functions.csv @@ -2749,11 +2749,11 @@ asm/non_matchings/code/sys_cmpdma/func_80178A24.s,func_80178A24,0x80178A24,0x4 asm/non_matchings/code/sys_cmpdma/func_80178A34.s,func_80178A34,0x80178A34,0x18 asm/non_matchings/code/sys_cmpdma/func_80178A94.s,func_80178A94,0x80178A94,0xB asm/non_matchings/code/sys_cmpdma/func_80178AC0.s,func_80178AC0,0x80178AC0,0x70 -asm/non_matchings/code/sys_cmpdma/func_80178C80.s,func_80178C80,0x80178C80,0x3F -asm/non_matchings/code/sys_cmpdma/func_80178D7C.s,func_80178D7C,0x80178D7C,0xC -asm/non_matchings/code/sys_cmpdma/func_80178DAC.s,func_80178DAC,0x80178DAC,0x24 -asm/non_matchings/code/sys_cmpdma/func_80178E3C.s,func_80178E3C,0x80178E3C,0x10 -asm/non_matchings/code/sys_cmpdma/func_80178E7C.s,func_80178E7C,0x80178E7C,0x2D +asm/non_matchings/code/sys_cmpdma/CmpDma_GetFileInfo.s,CmpDma_GetFileInfo,0x80178C80,0x3F +asm/non_matchings/code/sys_cmpdma/CmpDma_Decompress.s,CmpDma_Decompress,0x80178D7C,0xC +asm/non_matchings/code/sys_cmpdma/CmpDma_LoadFileImpl.s,CmpDma_LoadFileImpl,0x80178DAC,0x24 +asm/non_matchings/code/sys_cmpdma/CmpDma_LoadFile.s,CmpDma_LoadFile,0x80178E3C,0x10 +asm/non_matchings/code/sys_cmpdma/CmpDma_LoadAllFiles.s,CmpDma_LoadAllFiles,0x80178E7C,0x2D asm/non_matchings/code/sys_initial_check/Check_WriteRGBA16Pixel.s,Check_WriteRGBA16Pixel,0x80178F30,0xC asm/non_matchings/code/sys_initial_check/Check_WriteI4Pixel.s,Check_WriteI4Pixel,0x80178F60,0x10 asm/non_matchings/code/sys_initial_check/Check_DrawI4Texture.s,Check_DrawI4Texture,0x80178FA0,0x3C