From 34492a4386446fc82220fe0684ff521d760e84bd Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Sun, 26 Nov 2023 09:47:21 -0300 Subject: [PATCH] Move the system heap and the framebuffers to their own segments (#1488) * make segments for the systemheap and the framebuffers * define in the makefile * undefined syms * Make segments for the pre boot buffers too * Update spec Co-authored-by: Parker <20159000+jpburnett@users.noreply.github.com> * review * Update spec Co-authored-by: Parker <20159000+jpburnett@users.noreply.github.com> * Update Makefile Co-authored-by: Dragorn421 * comments * comment * move comment about the hardcoded address to buffers.h * rewrite SYSTEM_HEAP_END_ADDR in terms of other symbols * Use `ALIGNED` on all the buffers * Rename SYSTEM_HEAP_END_ADDR to FRAMEBUFFERS_START_ADDR * Put ALIGNED at the right like the rest of the codebase * merge * gLoBuffer * gHiBuffer * Add a static assert to ensure the address of gHiBuffer haven't shifted without the user noticing * smol include cleanup --------- Co-authored-by: Parker <20159000+jpburnett@users.noreply.github.com> Co-authored-by: Dragorn421 --- include/buffers.h | 54 +++++++++++++++++++++++++++++++++--- include/libc/assert.h | 21 ++++++++++++++ include/segment_symbols.h | 4 +++ include/variables.h | 4 --- spec | 28 +++++++++++++++++-- src/boot/idle.c | 15 ++++++---- src/buffers/audio_heap.c | 3 ++ src/buffers/framebuffer_hi.c | 5 ++++ src/buffers/framebuffer_lo.c | 5 ++++ src/buffers/gfxpools.c | 2 +- src/buffers/gfxstack.c | 2 +- src/buffers/gfxyield.c | 2 +- src/buffers/heaps.c | 5 ---- src/buffers/system_heap.c | 6 ++++ src/code/main.c | 4 +-- src/code/sys_cfb.c | 9 ++---- src/code/z_play.c | 10 +++---- src/code/z_vr_box.c | 3 +- tools/disasm/disasm.py | 2 +- tools/disasm/files.txt | 4 ++- tools/disasm/variables.txt | 7 ++--- undefined_syms.txt | 6 ++-- 22 files changed, 152 insertions(+), 49 deletions(-) create mode 100644 include/libc/assert.h create mode 100644 src/buffers/audio_heap.c create mode 100644 src/buffers/framebuffer_hi.c create mode 100644 src/buffers/framebuffer_lo.c delete mode 100644 src/buffers/heaps.c create mode 100644 src/buffers/system_heap.c diff --git a/include/buffers.h b/include/buffers.h index 72615a717b..ed8b2152cb 100644 --- a/include/buffers.h +++ b/include/buffers.h @@ -1,9 +1,24 @@ #ifndef BUFFERS_H #define BUFFERS_H -#include "z64.h" +#include "libc/assert.h" +#include "gfx.h" #include "macros.h" #include "stack.h" +#include "z64save.h" + +typedef union { + u16 framebufferHiRes[HIRES_BUFFER_HEIGHT][HIRES_BUFFER_WIDTH] ALIGNED(64); + struct { + u16 framebuffer[SCREEN_HEIGHT][SCREEN_WIDTH] ALIGNED(64); + u8 skyboxBuffer[0x5A360] ALIGNED(16); + }; +} BufferLow; + +// Equivalent to gLoBuffer.framebufferHiRes, but a different symbol is required to match +extern u16 gFramebufferHiRes1[HIRES_BUFFER_WIDTH][HIRES_BUFFER_HEIGHT]; + +extern BufferLow gLoBuffer; extern u8 gGfxSPTaskYieldBuffer[OS_YIELD_DATA_SIZE]; @@ -12,9 +27,40 @@ extern GfxPool gGfxPools[2]; extern u8 gAudioHeap[0x138000]; extern u8 gSystemHeap[]; -extern u8 gPictoPhotoI8[PICTO_PHOTO_SIZE]; -extern u8 D_80784600[0x56200]; -extern u16 gFramebuffer0[SCREEN_HEIGHT][SCREEN_WIDTH]; + +typedef union { + u16 framebufferHiRes[HIRES_BUFFER_HEIGHT][HIRES_BUFFER_WIDTH] ALIGNED(64); + struct { + u8 pictoPhotoI8[PICTO_PHOTO_SIZE] ALIGNED(64); + u8 D_80784600[0x56200] ALIGNED(64); + u16 framebuffer[SCREEN_HEIGHT][SCREEN_WIDTH] ALIGNED(64); + }; +} BufferHigh; + +// Equivalent to gHiBuffer.framebufferHiRes, but a different symbol is required to match +extern u16 gFramebufferHiRes0[HIRES_BUFFER_HEIGHT][HIRES_BUFFER_WIDTH]; + +extern BufferHigh gHiBuffer; + +#ifndef FRAMEBUFFERS_START_ADDR +/** + * The `framebuffers` segment is located at a fixed location in RAM and has a + * fixed size. + * Those framebuffers are placed at the end of the RAM space. + * This address is calculated by doing `0x80800000 - (size of framebuffers)`, + * where 0x80800000 is the end of the Expansion Pak address range. + * In the vanilla game this value expands to `0x80780000`. + * + * Since the start of the `framebuffers` segment is the end of the not-fixed + * available RAM, then the `system_heap` covers all the remaining RAM that is + * not used by the non-relocatable code/data (i.e. `boot`, `code`, and other + * buffers) up to the start of the `framebuffers` segmemt. + * @see `Main` + */ +#define FRAMEBUFFERS_START_ADDR (PHYS_TO_K0(0x800000) - sizeof(BufferHigh)) + +static_assert(FRAMEBUFFERS_START_ADDR == 0x80780000, "The expected address of gHiBuffer shifted. Please update said address in buffers.h and in the spec file."); +#endif #endif diff --git a/include/libc/assert.h b/include/libc/assert.h new file mode 100644 index 0000000000..7fee0a815c --- /dev/null +++ b/include/libc/assert.h @@ -0,0 +1,21 @@ +#ifndef LIBC_ASSERT_H +#define LIBC_ASSERT_H + +// Static/compile-time assertions + +#if (__STDC_VERSION__ >= 202311L) +// static_assert is a keyword in C23, do not define it +#elif (__STDC_VERSION__ >= 201112L) +# define static_assert(cond, msg) _Static_assert(cond, msg) +#else +# ifndef GLUE +# define GLUE(a, b) a##b +# endif +# ifndef GLUE2 +# define GLUE2(a, b) GLUE(a, b) +# endif + +# define static_assert(cond, msg) typedef char GLUE2(static_assertion_failed, __LINE__)[(cond) ? 1 : -1] +#endif + +#endif diff --git a/include/segment_symbols.h b/include/segment_symbols.h index 4d58241385..c49c080263 100644 --- a/include/segment_symbols.h +++ b/include/segment_symbols.h @@ -39,6 +39,8 @@ #define ROM_FILE_UNSET \ { 0 } +DECLARE_SEGMENT(framebuffer_lo) + DECLARE_SEGMENT(boot) DECLARE_ROM_SEGMENT(boot) @@ -78,6 +80,8 @@ DECLARE_SEGMENT(code) DECLARE_ROM_SEGMENT(code) DECLARE_BSS_SEGMENT(code) +DECLARE_SEGMENT(system_heap) + DECLARE_OVERLAY_SEGMENT(kaleido_scope) DECLARE_OVERLAY_SEGMENT(player_actor) diff --git a/include/variables.h b/include/variables.h index 638a8656cb..dce2df6632 100644 --- a/include/variables.h +++ b/include/variables.h @@ -5,10 +5,6 @@ #include "segment_symbols.h" #include "macros.h" -// pre-boot variables -extern u16 gFramebuffer1[SCREEN_HEIGHT][SCREEN_WIDTH]; // at 0x80000500 -extern u8 D_80025D00[]; - // data extern size_t gDmaMgrDmaBuffSize; extern vs32 gIrqMgrResetStatus; diff --git a/spec b/spec index 2bf8013150..c36ef9edf2 100644 --- a/spec +++ b/spec @@ -4,15 +4,20 @@ beginseg name "makerom" - address 0x8007F000 include "build/asm/makerom/rom_header.o" include "build/asm/makerom/ipl3.o" include "build/asm/makerom/entry.o" endseg +beginseg + name "framebuffer_lo" + address 0x80000500 + flags NOLOAD + include "build/src/buffers/framebuffer_lo.o" +endseg + beginseg name "boot" - address 0x80080060 include "build/src/boot/boot_main.o" include "build/data/boot/rspboot.data.o" include "build/src/boot/idle.o" @@ -612,13 +617,30 @@ beginseg include "build/data/code/njpgdspMain.rodata.o" endseg +// The game expects all the segments after the `code` segment and before the first overlay to be `NOLOAD` ones + beginseg name "buffers" flags NOLOAD include "build/src/buffers/gfxyield.o" include "build/src/buffers/gfxstack.o" include "build/src/buffers/gfxpools.o" - include "build/data/code/buffers.bss.o" + include "build/src/buffers/audio_heap.o" +endseg + +beginseg + name "system_heap" + flags NOLOAD + // This segment is just a dummy that is used to know where the other buffers (non framebuffers) end + include "build/src/buffers/system_heap.o" +endseg + +beginseg + name "framebuffer_hi" + flags NOLOAD + // This has to be fixed location in VRAM. See the FRAMEBUFFERS_START_ADDR define on `buffers.h` for a more in-depth explanation + address 0x80780000 + include "build/src/buffers/framebuffer_hi.o" endseg beginseg diff --git a/src/boot/idle.c b/src/boot/idle.c index 08f6d6ec7c..cb35fafe87 100644 --- a/src/boot/idle.c +++ b/src/boot/idle.c @@ -1,4 +1,5 @@ #include "prevent_bss_reordering.h" +#include "buffers.h" #include "irqmgr.h" #include "main.h" #include "stack.h" @@ -41,10 +42,10 @@ void Main_InitFramebuffer(u32* framebuffer, size_t numBytes, u32 value) { } void Main_InitScreen(void) { - Main_InitFramebuffer((u32*)gFramebuffer1, sizeof(gFramebuffer1), + Main_InitFramebuffer((u32*)gLoBuffer.framebuffer, sizeof(gLoBuffer.framebuffer), (GPACK_RGBA5551(0, 0, 0, 1) << 16) | GPACK_RGBA5551(0, 0, 0, 1)); ViConfig_UpdateVi(false); - osViSwapBuffer(gFramebuffer1); + osViSwapBuffer(gLoBuffer.framebuffer); osViBlack(false); } @@ -52,9 +53,13 @@ void Main_InitMemory(void) { void* memStart = (void*)0x80000400; void* memEnd = OS_PHYSICAL_TO_K0(osMemSize); - Main_ClearMemory(memStart, gFramebuffer1); - Main_ClearMemory(D_80025D00, bootproc); - Main_ClearMemory(gGfxSPTaskYieldBuffer, memEnd); + Main_ClearMemory(memStart, SEGMENT_START(framebuffer_lo)); + + // Clear the rest of the buffer that was not initialized by Main_InitScreen + Main_ClearMemory(&gLoBuffer.skyboxBuffer, SEGMENT_END(framebuffer_lo)); + + // Clear all the buffers after the `code` segment, up to the end of the available RAM space + Main_ClearMemory(SEGMENT_END(code), memEnd); } void Main_Init(void) { diff --git a/src/buffers/audio_heap.c b/src/buffers/audio_heap.c new file mode 100644 index 0000000000..1fab091bcd --- /dev/null +++ b/src/buffers/audio_heap.c @@ -0,0 +1,3 @@ +#include "buffers.h" + +u8 gAudioHeap[0x138000] ALIGNED(16); diff --git a/src/buffers/framebuffer_hi.c b/src/buffers/framebuffer_hi.c new file mode 100644 index 0000000000..2ae30d39c3 --- /dev/null +++ b/src/buffers/framebuffer_hi.c @@ -0,0 +1,5 @@ +#include "buffers.h" + +// Don't add symbols here unless you know what you are doing. + +BufferHigh gHiBuffer ALIGNED(64); diff --git a/src/buffers/framebuffer_lo.c b/src/buffers/framebuffer_lo.c new file mode 100644 index 0000000000..1213725051 --- /dev/null +++ b/src/buffers/framebuffer_lo.c @@ -0,0 +1,5 @@ +#include "buffers.h" + +// Don't add symbols here unless you know what you are doing. + +BufferLow gLoBuffer ALIGNED(64); diff --git a/src/buffers/gfxpools.c b/src/buffers/gfxpools.c index ff4934f11a..f7b4ab5b7b 100644 --- a/src/buffers/gfxpools.c +++ b/src/buffers/gfxpools.c @@ -1,3 +1,3 @@ #include "buffers.h" -GfxPool gGfxPools[2]; +GfxPool gGfxPools[2] ALIGNED(16); diff --git a/src/buffers/gfxstack.c b/src/buffers/gfxstack.c index 8864d58ef5..b88e27eaa2 100644 --- a/src/buffers/gfxstack.c +++ b/src/buffers/gfxstack.c @@ -1,3 +1,3 @@ #include "buffers.h" -STACK(gGfxSPTaskStack, 0x400); +STACK(gGfxSPTaskStack, 0x400) ALIGNED(16); diff --git a/src/buffers/gfxyield.c b/src/buffers/gfxyield.c index 576cfc0bb4..866257eac3 100644 --- a/src/buffers/gfxyield.c +++ b/src/buffers/gfxyield.c @@ -1,3 +1,3 @@ #include "buffers.h" -u8 gGfxSPTaskYieldBuffer[OS_YIELD_DATA_SIZE]; +u8 gGfxSPTaskYieldBuffer[OS_YIELD_DATA_SIZE] ALIGNED(16); diff --git a/src/buffers/heaps.c b/src/buffers/heaps.c deleted file mode 100644 index 477eca03ef..0000000000 --- a/src/buffers/heaps.c +++ /dev/null @@ -1,5 +0,0 @@ -#include "buffers.h" - -u8 gAudioHeap[0x138000]; - -u8 gSystemHeap[UNK_SIZE]; diff --git a/src/buffers/system_heap.c b/src/buffers/system_heap.c new file mode 100644 index 0000000000..df3d79003b --- /dev/null +++ b/src/buffers/system_heap.c @@ -0,0 +1,6 @@ +#include "buffers.h" + +// Don't add symbols here unless you know what you are doing. + +// Dummy, marks the start of system_heap space whose actual size depends on the spec +u8 gSystemHeap[UNK_SIZE] ALIGNED(16); diff --git a/src/code/main.c b/src/code/main.c index 14ee841ce6..83df663961 100644 --- a/src/code/main.c +++ b/src/code/main.c @@ -53,8 +53,8 @@ void Main(void* arg) { Check_RegionIsSupported(); Check_ExpansionPak(); - sysHeap = (intptr_t)gSystemHeap; - fb = 0x80780000; + sysHeap = (intptr_t)SEGMENT_START(system_heap); + fb = FRAMEBUFFERS_START_ADDR; gSystemHeapSize = fb - sysHeap; SystemHeap_Init((void*)sysHeap, gSystemHeapSize); diff --git a/src/code/sys_cfb.c b/src/code/sys_cfb.c index 32600643aa..b559a44ee4 100644 --- a/src/code/sys_cfb.c +++ b/src/code/sys_cfb.c @@ -1,5 +1,5 @@ -#include "prevent_bss_reordering.h" #include "z64.h" +#include "buffers.h" #include "regs.h" #include "functions.h" #include "macros.h" @@ -41,9 +41,6 @@ u8 gSysCfbHiResEnabled; #include "system_malloc.h" #include "z64vimode.h" -extern u16 gFramebufferHiRes0[HIRES_BUFFER_WIDTH][HIRES_BUFFER_HEIGHT]; -extern u16 gFramebufferHiRes1[HIRES_BUFFER_WIDTH][HIRES_BUFFER_HEIGHT]; - void SysCfb_SetLoResMode(void) { gFramebuffers[1] = sCfbLoRes1; gFramebuffers[0] = sCfbLoRes0; @@ -93,8 +90,8 @@ void SysCfb_SetHiResMode(void) { } void SysCfb_Init(void) { - sCfbLoRes1 = gFramebuffer1; - sCfbLoRes0 = gFramebuffer0; + sCfbLoRes1 = gLoBuffer.framebuffer; + sCfbLoRes0 = gHiBuffer.framebuffer; sCfbHiRes1 = gFramebufferHiRes1; sCfbHiRes0 = gFramebufferHiRes0; SysCfb_SetLoResMode(); diff --git a/src/code/z_play.c b/src/code/z_play.c index 226980b4d0..ca4fb500dc 100644 --- a/src/code/z_play.c +++ b/src/code/z_play.c @@ -248,7 +248,7 @@ void Play_TriggerPictoPhoto(void) { void Play_TakePictoPhoto(PreRender* prerender) { PreRender_ApplyFilters(prerender); - Play_ConvertRgba16ToIntensityImage(gPictoPhotoI8, prerender->fbufSave, SCREEN_WIDTH, PICTO_PHOTO_TOPLEFT_X, + Play_ConvertRgba16ToIntensityImage(gHiBuffer.pictoPhotoI8, prerender->fbufSave, SCREEN_WIDTH, PICTO_PHOTO_TOPLEFT_X, PICTO_PHOTO_TOPLEFT_Y, (PICTO_PHOTO_TOPLEFT_X + PICTO_PHOTO_WIDTH) - 1, (PICTO_PHOTO_TOPLEFT_Y + PICTO_PHOTO_HEIGHT) - 1, 8); } @@ -2262,10 +2262,10 @@ void Play_Init(GameState* thisx) { PreRender_SetValues(&this->pauseBgPreRender, gCfbWidth, gCfbHeight, NULL, NULL); this->unk_18E64 = gWorkBuffer; - this->pictoPhotoI8 = gPictoPhotoI8; - this->unk_18E68 = D_80784600; - this->unk_18E58 = D_80784600; - this->unk_18E60 = D_80784600; + this->pictoPhotoI8 = gHiBuffer.pictoPhotoI8; + this->unk_18E68 = gHiBuffer.D_80784600; + this->unk_18E58 = gHiBuffer.D_80784600; + this->unk_18E60 = gHiBuffer.D_80784600; gTransitionTileState = TRANS_TILE_OFF; this->transitionMode = TRANS_MODE_OFF; D_801D0D54 = false; diff --git a/src/code/z_vr_box.c b/src/code/z_vr_box.c index 605dff84c4..23f00d2460 100644 --- a/src/code/z_vr_box.c +++ b/src/code/z_vr_box.c @@ -1,4 +1,5 @@ #include "global.h" +#include "buffers.h" u32 D_801C5E30[] = { 0, 0x2000, 0x4000, 0x6000, 0x8000, 0xC000 }; @@ -193,7 +194,7 @@ void Skybox_Setup(GameState* gameState, SkyboxContext* skyboxCtx, s16 skyboxId) switch (skyboxId) { case SKYBOX_NORMAL_SKY: // Send a DMA request for the cloudy sky texture - skyboxCtx->staticSegments[0] = &D_80025D00; + skyboxCtx->staticSegments[0] = gLoBuffer.skyboxBuffer; size = SEGMENT_ROM_SIZE(d2_cloud_static); segment = (void*)ALIGN8((uintptr_t)skyboxCtx->staticSegments[0] + size); DmaMgr_SendRequest0(skyboxCtx->staticSegments[0], SEGMENT_ROM_START(d2_cloud_static), size); diff --git a/tools/disasm/disasm.py b/tools/disasm/disasm.py index 78d66d337b..884c79f89b 100755 --- a/tools/disasm/disasm.py +++ b/tools/disasm/disasm.py @@ -2147,7 +2147,7 @@ for segment in files_spec: # Calculate the offset and size of this section relative to the section data_offset = off - segment[3][i][0] full_index = file_list.index(off) - if segment[0] == "code" and name == "buffers" and segment[3][i][2] == "bss": + if segment[0] == "code" and name == "framebuffer_hi" and segment[3][i][2] == "bss": # This is the end of code, hardcode it data_size = 0x80800000 - file_list[full_index] elif segment[0] == "boot" and name == "vimgr" and segment[3][i][2] == "bss": diff --git a/tools/disasm/files.txt b/tools/disasm/files.txt index e170e97e33..01a43df765 100644 --- a/tools/disasm/files.txt +++ b/tools/disasm/files.txt @@ -750,7 +750,9 @@ 0x801FFD00 : "sequence", 0x80208E90 : "jpegdecoder", 0x80208EA0 : "gfxbuffers", - 0x8024A4C0 : "buffers", + 0x8024A4C0 : "buffer_audio_heap", + 0x803824C0 : "buffer_system_heap", + 0x80780000 : "framebuffer_hi", }, ], ['ovl_title', 'baserom/', 'overlay', [], { 0x80800000 : "ovl_title" }], diff --git a/tools/disasm/variables.txt b/tools/disasm/variables.txt index 961b7c3fbf..9952b1ef36 100644 --- a/tools/disasm/variables.txt +++ b/tools/disasm/variables.txt @@ -8,8 +8,7 @@ 0x80000314:("osVersion","UNK_TYPE","",0x4), 0x80000318:("osMemSize","UNK_TYPE","",0x4), 0x8000031C:("osAppNMIBuffer","s32","[0x10]",0x40), - 0x80000500:("gFramebuffer1","u16","[SCREEN_HEIGHT][SCREEN_WIDTH]",0x25800), - 0x80025D00:("D_80025D00","u8","[]",0x1), # TODO size + 0x80000500:("gLoBuffer","BufferLow","",0x7FB60), 0x800969C0:("rspbootTextStart","u64","[]",0x160), 0x80096B20:("D_80096B20","u8","",0x1), 0x80096B24:("gViConfigUseDefault","vu8","",0x1), @@ -4286,9 +4285,7 @@ 0x80209EA0:("gGfxPools","GfxPool","[2]",0x40620), 0x8024A4C0:("gAudioHeap","u8","[0x138000]",0x138000), 0x803824C0:("gSystemHeap","u8","[UNK_SIZE]",0x3fdb40), - 0x80780000:("gPictoPhotoI8","u8","[0x4600]",0x4600), - 0x80784600:("D_80784600","u8","[0x56200]",0x56200), - 0x807DA800:("gFramebuffer0","u16","[SCREEN_HEIGHT][SCREEN_WIDTH]",0x25800), + 0x80780000:("gHiBuffer","BufferHigh","",0x80000), 0x80800860:("titleRotation","s16","",0x2), 0x80800868:("D_80800868","UNK_TYPE1","",0x1), 0x80800870:("D_80800870","UNK_TYPE1","",0x1), diff --git a/undefined_syms.txt b/undefined_syms.txt index 61a689856a..3f0ad69e75 100644 --- a/undefined_syms.txt +++ b/undefined_syms.txt @@ -124,10 +124,8 @@ D_A4800018 = 0xA4800018; // SI_STATUS_REG // sys_cfb buffers -gFramebuffer1 = 0x80000500; -gFramebufferHiRes0 = 0x80780000; -gFramebufferHiRes1 = 0x80000500; -D_80025D00 = 0x80025D00; +gFramebufferHiRes1 = gLoBuffer; +gFramebufferHiRes0 = gHiBuffer; // Ucode symbols