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 <Dragorn421@users.noreply.github.com>

* 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 <Dragorn421@users.noreply.github.com>
This commit is contained in:
Anghelo Carvajal 2023-11-26 09:47:21 -03:00 committed by GitHub
parent b25e64d1bd
commit 34492a4386
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 152 additions and 49 deletions

View File

@ -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

21
include/libc/assert.h Normal file
View File

@ -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

View File

@ -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)

View File

@ -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;

28
spec
View File

@ -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

View File

@ -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) {

3
src/buffers/audio_heap.c Normal file
View File

@ -0,0 +1,3 @@
#include "buffers.h"
u8 gAudioHeap[0x138000] ALIGNED(16);

View File

@ -0,0 +1,5 @@
#include "buffers.h"
// Don't add symbols here unless you know what you are doing.
BufferHigh gHiBuffer ALIGNED(64);

View File

@ -0,0 +1,5 @@
#include "buffers.h"
// Don't add symbols here unless you know what you are doing.
BufferLow gLoBuffer ALIGNED(64);

View File

@ -1,3 +1,3 @@
#include "buffers.h"
GfxPool gGfxPools[2];
GfxPool gGfxPools[2] ALIGNED(16);

View File

@ -1,3 +1,3 @@
#include "buffers.h"
STACK(gGfxSPTaskStack, 0x400);
STACK(gGfxSPTaskStack, 0x400) ALIGNED(16);

View File

@ -1,3 +1,3 @@
#include "buffers.h"
u8 gGfxSPTaskYieldBuffer[OS_YIELD_DATA_SIZE];
u8 gGfxSPTaskYieldBuffer[OS_YIELD_DATA_SIZE] ALIGNED(16);

View File

@ -1,5 +0,0 @@
#include "buffers.h"
u8 gAudioHeap[0x138000];
u8 gSystemHeap[UNK_SIZE];

View File

@ -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);

View File

@ -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);

View File

@ -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();

View File

@ -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;

View File

@ -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);

View File

@ -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":

View File

@ -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" }],

View File

@ -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),

View File

@ -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