From 6798b2db29ffb6f044f7dc3626f7f245f6467878 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 13 May 2023 01:44:40 -0700 Subject: [PATCH] Make anim dma table dynamic --- src/engine/level_script.c | 8 +++--- src/game/game_init.c | 54 ++++++++++++++++++--------------------- src/game/game_init.h | 2 +- src/game/level_update.c | 46 ++++++++++++++++----------------- src/game/memory.c | 54 ++++++++++++--------------------------- src/game/memory.h | 2 +- 6 files changed, 70 insertions(+), 96 deletions(-) diff --git a/src/engine/level_script.c b/src/engine/level_script.c index 9804698d..a5e79334 100644 --- a/src/engine/level_script.c +++ b/src/engine/level_script.c @@ -336,14 +336,14 @@ static void level_cmd_load_mario_head(void) { gSkipInterpolationTitleScreen = true; gGlobalTimer = 0; // TODO: Fix these hardcoded sizes - void *addr = main_pool_alloc(DOUBLE_SIZE_ON_64_BIT(0xE1000), MEMORY_POOL_LEFT); - if (addr != NULL) { - gdm_init(addr, DOUBLE_SIZE_ON_64_BIT(0xE1000)); + static void* sMarioHead = NULL; + if (!sMarioHead) { sMarioHead = calloc(1, DOUBLE_SIZE_ON_64_BIT(0xE1000)); } + if (sMarioHead != NULL) { + gdm_init(sMarioHead, DOUBLE_SIZE_ON_64_BIT(0xE1000)); gd_add_to_heap(gZBuffer, sizeof(gZBuffer)); // 0x25800 gd_add_to_heap(gFrameBuffer0, 3 * sizeof(gFrameBuffer0)); // 0x70800 gdm_setup(); gdm_maketestdl(CMD_GET(s16, 2)); - } else { } sCurrentCmd = CMD_NEXT; diff --git a/src/game/game_init.c b/src/game/game_init.c index 2db690cf..9662906d 100644 --- a/src/game/game_init.c +++ b/src/game/game_init.c @@ -32,29 +32,27 @@ // FIXME: I'm not sure all of these variables belong in this file, but I don't // know of a good way to split them -struct Controller gControllers[MAX_PLAYERS]; -struct SPTask *gGfxSPTask; -Gfx *gDisplayListHead; -u8 *gGfxPoolEnd; -struct GfxPool *gGfxPool; -OSContStatus gControllerStatuses[4]; -OSContPad gControllerPads[4]; -u8 gControllerBits; -s8 gEepromProbe; -OSMesgQueue gGameVblankQueue; -OSMesgQueue D_80339CB8; -OSMesg D_80339CD0; -OSMesg D_80339CD4; -struct VblankHandler gGameVblankHandler; -uintptr_t gPhysicalFrameBuffers[3]; -uintptr_t gPhysicalZBuffer; -void *D_80339CF0[MAX_PLAYERS]; -void *D_80339CF4; -struct MarioAnimation D_80339D10[MAX_PLAYERS]; -struct MarioAnimation gDemo; -UNUSED u8 filler80339D30[0x90]; +struct Controller gControllers[MAX_PLAYERS] = { 0 }; +struct SPTask *gGfxSPTask = NULL; +Gfx *gDisplayListHead = NULL; +u8 *gGfxPoolEnd = NULL; +struct GfxPool *gGfxPool = NULL; +OSContStatus gControllerStatuses[4] = { 0 }; +OSContPad gControllerPads[4] = { 0 }; +u8 gControllerBits = 0; +s8 gEepromProbe = 0; +OSMesgQueue gGameVblankQueue = { 0 }; +OSMesgQueue D_80339CB8 = { 0 }; +OSMesg D_80339CD0 = NULL; +OSMesg D_80339CD4 = NULL; +struct VblankHandler gGameVblankHandler = { 0 }; +uintptr_t gPhysicalFrameBuffers[3] = { 0 }; +uintptr_t gPhysicalZBuffer = 0; +void *D_80339CF0[MAX_PLAYERS] = { 0 }; +void *gDemoTargetAnim = NULL; +struct MarioAnimation D_80339D10[MAX_PLAYERS] = { 0 }; +struct MarioAnimation gDemo = { 0 }; -s32 unused8032C690 = 0; u32 gGlobalTimer = 0; static u16 sCurrFBNum = 0; @@ -556,8 +554,6 @@ void init_controllers(void) { } void setup_game_memory(void) { - UNUSED u8 pad[8]; - set_segment_base_addr(0, (void *) 0x80000000); osCreateMesgQueue(&D_80339CB8, &D_80339CD4, 1); osCreateMesgQueue(&gGameVblankQueue, &D_80339CD0, 1); @@ -566,13 +562,13 @@ void setup_game_memory(void) { gPhysicalFrameBuffers[1] = VIRTUAL_TO_PHYSICAL(gFrameBuffer1); gPhysicalFrameBuffers[2] = VIRTUAL_TO_PHYSICAL(gFrameBuffer2); for (s32 i = 0; i < MAX_PLAYERS; i++) { - D_80339CF0[i] = main_pool_alloc(0x4000, MEMORY_POOL_LEFT); + D_80339CF0[i] = calloc(1, 0x4000); set_segment_base_addr(17, (void *)D_80339CF0[i]); - func_80278A78(&D_80339D10[i], gMarioAnims, D_80339CF0[i]); + alloc_anim_dma_table(&D_80339D10[i], gMarioAnims, D_80339CF0[i]); } - D_80339CF4 = main_pool_alloc(2048, MEMORY_POOL_LEFT); - set_segment_base_addr(24, (void *) D_80339CF4); - func_80278A78(&gDemo, gDemoInputs, D_80339CF4); + gDemoTargetAnim = calloc(1, 2048); + set_segment_base_addr(24, (void *) gDemoTargetAnim); + alloc_anim_dma_table(&gDemo, gDemoInputs, gDemoTargetAnim); load_segment(0x10, _entrySegmentRomStart, _entrySegmentRomEnd, MEMORY_POOL_LEFT); load_segment_decompress(2, _segment2_mio0SegmentRomStart, _segment2_mio0SegmentRomEnd); } diff --git a/src/game/game_init.h b/src/game/game_init.h index 49bfd7d7..63f3e65f 100644 --- a/src/game/game_init.h +++ b/src/game/game_init.h @@ -33,7 +33,7 @@ extern struct VblankHandler gGameVblankHandler; extern uintptr_t gPhysicalFrameBuffers[3]; extern uintptr_t gPhysicalZBuffer; extern void *D_80339CF0[MAX_PLAYERS]; -extern void *D_80339CF4; +extern void *gDemoTargetAnim; extern struct SPTask *gGfxSPTask; extern Gfx *gDisplayListHead; extern u8 *gGfxPoolEnd; diff --git a/src/game/level_update.c b/src/game/level_update.c index 311eabda..d0179aed 100644 --- a/src/game/level_update.c +++ b/src/game/level_update.c @@ -67,10 +67,10 @@ s16 gChangeLevelTransition = -1; s16 gChangeActNum = -1; static bool sFirstCastleGroundsMenu = true; -bool isDemoActive = false; +static bool sIsDemoActive = false; bool gInPlayerMenu = false; static u16 gDemoCountdown = 0; -int demoNumber = -1; +static int sDemoNumber = -1; // TODO: Make these ifdefs better const char *credits01[] = { "1GAME DIRECTOR", "SHIGERU MIYAMOTO" }; @@ -1125,56 +1125,56 @@ void basic_update(UNUSED s16 *arg) { bool find_demo_number(void) { switch (gCurrLevelNum) { case LEVEL_BOWSER_1: - demoNumber = 0; + sDemoNumber = 0; return true; case LEVEL_WF: - demoNumber = 1; + sDemoNumber = 1; return true; case LEVEL_CCM: - demoNumber = 2; + sDemoNumber = 2; return true; case LEVEL_BBH: - demoNumber = 3; + sDemoNumber = 3; return true; case LEVEL_JRB: - demoNumber = 4; + sDemoNumber = 4; return true; case LEVEL_HMC: - demoNumber = 5; + sDemoNumber = 5; return true; case LEVEL_PSS: - demoNumber = 6; + sDemoNumber = 6; return true; default: - demoNumber = -1; + sDemoNumber = -1; } return false; } static void start_demo(void) { - if (isDemoActive) { - isDemoActive = false; + if (sIsDemoActive) { + sIsDemoActive = false; } else { - isDemoActive = true; + sIsDemoActive = true; if (find_demo_number()) { gChangeLevel = gCurrLevelNum; } - if (demoNumber <= 6 || demoNumber > -1) { + if (sDemoNumber <= 6 || sDemoNumber > -1) { gCurrDemoInput = NULL; - func_80278A78(&gDemo, gDemoInputs, D_80339CF4); - load_patchable_table(&gDemo, demoNumber); + alloc_anim_dma_table(&gDemo, gDemoInputs, gDemoTargetAnim); + load_patchable_table(&gDemo, sDemoNumber); gCurrDemoInput = ((struct DemoInput *) gDemo.targetAnim); } else { - isDemoActive = false; + sIsDemoActive = false; } } } void stop_demo(UNUSED struct DjuiBase* caller) { - if (isDemoActive) { - isDemoActive = false; + if (sIsDemoActive) { + sIsDemoActive = false; gCurrDemoInput = NULL; gChangeLevel = gCurrLevelNum; gDemoCountdown = 0; @@ -1201,12 +1201,12 @@ s32 play_mode_normal(void) { } } else { if (gDjuiInMainMenu && gCurrDemoInput == NULL && configMenuDemos && !gInPlayerMenu) { - if ((++gDemoCountdown) == PRESS_START_DEMO_TIMER && (find_demo_number() && (demoNumber <= 6 || demoNumber > -1))) { + if ((++gDemoCountdown) == PRESS_START_DEMO_TIMER && (find_demo_number() && (sDemoNumber <= 6 || sDemoNumber > -1))) { start_demo(); } } - if (((gCurrDemoInput != NULL) && (gPlayer1Controller->buttonPressed & END_DEMO || !isDemoActive || !gDjuiInMainMenu || gNetworkType != NT_NONE || gInPlayerMenu)) || (gCurrDemoInput == NULL && isDemoActive)) { + if (((gCurrDemoInput != NULL) && (gPlayer1Controller->buttonPressed & END_DEMO || !sIsDemoActive || !gDjuiInMainMenu || gNetworkType != NT_NONE || gInPlayerMenu)) || (gCurrDemoInput == NULL && sIsDemoActive)) { gPlayer1Controller->buttonPressed &= ~END_DEMO; stop_demo(NULL); } @@ -1436,7 +1436,7 @@ void update_menu_level(void) { // warp to level, this feels buggy if (gCurrLevelNum != curLevel) { - if (isDemoActive) { + if (sIsDemoActive) { stop_demo(NULL); } if (curLevel == LEVEL_JRB) { @@ -1450,7 +1450,7 @@ void update_menu_level(void) { } gDemoCountdown = 0; } - if (isDemoActive) { + if (sIsDemoActive) { return; } diff --git a/src/game/memory.c b/src/game/memory.c index f69346cc..1cd1ee8b 100644 --- a/src/game/memory.c +++ b/src/game/memory.c @@ -56,7 +56,6 @@ u8 *sPoolEnd; struct MainPoolBlock *sPoolListHeadL; struct MainPoolBlock *sPoolListHeadR; - static struct MainPoolState *gMainPoolState = NULL; uintptr_t set_segment_base_addr(s32 segment, void *addr) { @@ -79,7 +78,6 @@ void *virtual_to_segmented(UNUSED u32 segment, const void *addr) { void move_segment_table_to_dmem(void) { } - /** * Initialize the main memory pool. This pool is conceptually a pair of stacks * that grow inward from the left and right. It therefore only supports @@ -219,47 +217,27 @@ u32 main_pool_pop_state(void) { return sPoolFreeSpace; } -/** - * Perform a DMA read from ROM. The transfer is split into 4KB blocks, and this - * function blocks until completion. - */ -static void dma_read(u8 *dest, u8 *srcStart, u8 *srcEnd) { - memcpy(dest, srcStart, srcEnd - srcStart); -} +static struct MarioAnimDmaRelatedThing* func_802789F0(u8* srcAddr) { + u32 count = 0; + memcpy(&count, srcAddr, sizeof(u32)); + u32 size = sizeof(u32) + (sizeof(u8 *) - sizeof(u32)) + sizeof(u8 *) + count * sizeof(struct OffsetSizePair); -/** - * Perform a DMA read from ROM, allocating space in the memory pool to write to. - * Return the destination address. - */ -static void *dynamic_dma_read(u8 *srcStart, u8 *srcEnd, u32 side) { - void *dest; - u32 size = ALIGN16(srcEnd - srcStart); - - dest = main_pool_alloc(size, side); - if (dest != NULL) { - dma_read(dest, srcStart, srcEnd); - } - return dest; -} - -static struct MarioAnimDmaRelatedThing *func_802789F0(u8 *srcAddr) { - struct MarioAnimDmaRelatedThing *sp1C = dynamic_dma_read(srcAddr, srcAddr + sizeof(u32), - MEMORY_POOL_LEFT); - u32 size = sizeof(u32) + (sizeof(u8 *) - sizeof(u32)) + sizeof(u8 *) + - sp1C->count * sizeof(struct OffsetSizePair); - main_pool_free(sp1C); - - sp1C = dynamic_dma_read(srcAddr, srcAddr + size, MEMORY_POOL_LEFT); + struct MarioAnimDmaRelatedThing *sp1C = malloc(size); + memcpy(sp1C, srcAddr, size); sp1C->srcAddr = srcAddr; return sp1C; } -void func_80278A78(struct MarioAnimation *a, void *b, struct Animation *target) { - if (b != NULL) { - a->animDmaTable = func_802789F0(b); +void alloc_anim_dma_table(struct MarioAnimation* marioAnim, void* srcAddr, struct Animation* targetAnim) { + if (srcAddr) { + if (marioAnim->animDmaTable) { + free(marioAnim->animDmaTable); + marioAnim->animDmaTable = NULL; + } + marioAnim->animDmaTable = func_802789F0(srcAddr); } - a->currentAnimAddr = NULL; - a->targetAnim = target; + marioAnim->currentAnimAddr = NULL; + marioAnim->targetAnim = targetAnim; } // TODO: (Scrub C) @@ -275,7 +253,7 @@ s32 load_patchable_table(struct MarioAnimation *a, u32 index) { size = sp20->anim[index].size; } while (0); if (a->currentAnimAddr != addr) { - dma_read((u8 *) a->targetAnim, addr, addr + size); + memcpy(a->targetAnim, addr, size); a->currentAnimAddr = addr; ret = TRUE; } diff --git a/src/game/memory.h b/src/game/memory.h index a22b0657..8bb321f0 100644 --- a/src/game/memory.h +++ b/src/game/memory.h @@ -75,7 +75,7 @@ void growing_pool_free_pool(struct GrowingPool *pool); void alloc_display_list_reset(void); void *alloc_display_list(u32 size); -void func_80278A78(struct MarioAnimation *a, void *b, struct Animation *target); +void alloc_anim_dma_table(struct MarioAnimation* marioAnim, void *b, struct Animation *targetAnim); s32 load_patchable_table(struct MarioAnimation *a, u32 b); #endif // MEMORY_H