From b3eae0e5b7bcc2c5d3c8a1d562f0737d80c7e35c Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Sat, 8 Jul 2023 14:41:25 -0400 Subject: [PATCH] Port OoT's docs for `fault.c` and `fault_drawer.c` (#1199) * fault.h * some docs stealing * fix building * fault_internal.h * pass * finish stealing docs * finish cleanup * format * warning * Update src/boot_O2_g3/fault.c Co-authored-by: Derek Hensley * Update src/boot_O2_g3/fault.c Co-authored-by: Derek Hensley * Update src/boot_O2_g3/fault.c Co-authored-by: Derek Hensley * review Co-authored-by: Derek Hensley * arggggg * arggggg part 2 * STACK * PHYS_TO_K0(0x400000) * format * fix * Instance * format * Neutral reset * variables.h cleanup * bss * frameBuffer * format * Update src/boot_O2_g3/fault.c Co-authored-by: Derek Hensley * review Co-authored-by: Derek Hensley * Update src/boot_O2_g3/fault.c Co-authored-by: Derek Hensley * bss * bss * bss * callback cleanup * fix function declarations * fix again * bss * bss * Update src/overlays/actors/ovl_En_Fishing/z_en_fishing.c Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> * Update src/boot_O2_g3/fault.c Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> * bss * bss * Update src/boot_O2_g3/fault.c Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> * review * import bss * format * minor cleanup * bss * review * fix * bss * bss * bss * bss * bss * format * a * Z_PRIORITY_FAULT * bss * fix * idle.c bss doesn't want to get fixed :c * review * bss --------- Co-authored-by: Derek Hensley Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> --- include/fault.h | 109 ++ include/fault_internal.h | 18 + include/functions.h | 69 +- include/ultra64/convert.h | 3 + include/ultra64/thread.h | 5 + include/variables.h | 169 +-- include/z64.h | 65 -- include/z64thread.h | 18 +- spec | 1 - src/boot_O2/assert.c | 1 + src/boot_O2_g3/CIC6105.c | 3 +- src/boot_O2_g3/fault.c | 972 +++++++++++------- src/boot_O2_g3/fault_drawer.c | 235 +++-- src/boot_O2_g3/yaz0.c | 1 + src/boot_O2_g3/z_std_dma.c | 1 + src/code/graph.c | 30 +- src/code/main.c | 7 +- src/code/padmgr.c | 4 +- src/code/sched.c | 24 +- src/code/sys_flashrom.c | 1 + src/code/sys_initial_check.c | 3 +- src/code/sys_matrix.c | 1 + src/code/z_actor.c | 1 + src/code/z_actor_dlftbls.c | 6 +- src/code/z_bgcheck.c | 14 +- src/code/z_collision_check.c | 1 + src/code/z_demo.c | 1 + src/code/z_kaleido_manager.c | 5 +- src/code/z_play.c | 1 + src/code/z_player_lib.c | 1 - src/libultra/os/createthread.c | 34 +- src/libultra/os/setthreadpri.c | 16 +- src/overlays/actors/ovl_Boss_02/z_boss_02.c | 1 + src/overlays/actors/ovl_En_Ot/z_en_ot.c | 1 - .../actors/ovl_Obj_Hunsui/z_obj_hunsui.c | 1 + .../actors/ovl_player_actor/z_player.c | 2 +- .../ovl_kaleido_scope/z_kaleido_scope_NES.c | 2 + tools/disasm/functions.txt | 20 +- tools/disasm/variables.txt | 12 +- tools/namefixer.py | 2 + tools/sizes/boot_functions.csv | 20 +- 41 files changed, 1007 insertions(+), 874 deletions(-) create mode 100644 include/fault.h create mode 100644 include/fault_internal.h diff --git a/include/fault.h b/include/fault.h new file mode 100644 index 0000000000..c93feee254 --- /dev/null +++ b/include/fault.h @@ -0,0 +1,109 @@ +#ifndef FAULT_H +#define FAULT_H + +#include "ultra64.h" +#include "unk.h" +#include "libc/stdarg.h" +#include "libc/stdint.h" +#include "io/controller.h" +#include "padmgr.h" +#include "stack.h" + +// These are the same as the 3-bit ansi color codes +#define FAULT_COLOR_BLACK 0 +#define FAULT_COLOR_RED 1 +#define FAULT_COLOR_GREEN 2 +#define FAULT_COLOR_YELLOW 3 +#define FAULT_COLOR_BLUE 4 +#define FAULT_COLOR_MAGENTA 5 +#define FAULT_COLOR_CYAN 6 +#define FAULT_COLOR_WHITE 7 +// Additional color codes +#define FAULT_COLOR_DARK_GRAY 8 +#define FAULT_COLOR_LIGHT_GRAY 9 + +#define FAULT_COLOR_STRINGIFY(s) #s +#define FAULT_COLOR_EXPAND_AND_STRINGIFY(s) FAULT_COLOR_STRINGIFY(s) + +#define FAULT_ESC '\x1A' +#define FAULT_COLOR(n) "\x1A" FAULT_COLOR_EXPAND_AND_STRINGIFY(FAULT_COLOR_ ## n) + + +// Address at the end of "non-expansion" memory space +#define FAULT_FB_ADDRESS (void*)((PHYS_TO_K0(0x400000) - SCREEN_HEIGHT * SCREEN_WIDTH * sizeof(u16))) + +typedef void (*FaultClientCallback)(void*, void*); + +typedef struct FaultClient { + /* 0x0 */ struct FaultClient* next; + /* 0x4 */ FaultClientCallback callback; + /* 0x8 */ void* arg0; + /* 0xC */ void* arg1; +} FaultClient; // size = 0x10 + + +typedef uintptr_t (*FaultAddrConvClientCallback)(uintptr_t, void*); + +typedef struct FaultAddrConvClient { + /* 0x0 */ struct FaultAddrConvClient* next; + /* 0x4 */ FaultAddrConvClientCallback callback; + /* 0x8 */ void* arg; +} FaultAddrConvClient; // size = 0xC + +typedef void(*FaultPadCallback)(Input* input); + + +// Initialization + +void Fault_Init(void); + +// Fatal Errors + +void Fault_AddHungupAndCrashImpl(const char* exp1, const char* exp2); +void Fault_AddHungupAndCrash(const char* file, s32 line); + +// Client Registration + +void Fault_AddClient(FaultClient* client, FaultClientCallback callback, void* arg0, void* arg1); +void Fault_RemoveClient(FaultClient* client); +void Fault_AddAddrConvClient(FaultAddrConvClient* client, FaultAddrConvClientCallback callback, void* arg); +void Fault_RemoveAddrConvClient(FaultAddrConvClient* client); + +// For use in Fault Client callbacks + +void Fault_WaitForInput(void); +void Fault_FillScreenBlack(void); +void Fault_SetFrameBuffer(void* fb, u16 w, u16 h); + +void FaultDrawer_SetForeColor(u16 color); +void FaultDrawer_SetBackColor(u16 color); +void FaultDrawer_SetFontColor(u16 color); +void FaultDrawer_SetCharPad(s8 padW, s8 padH); +void FaultDrawer_SetCursor(s32 x, s32 y); +s32 FaultDrawer_VPrintf(const char* fmt, va_list ap); +s32 FaultDrawer_Printf(const char* fmt, ...); +void FaultDrawer_DrawText(s32 x, s32 y, const char* fmt, ...); + + +typedef struct FaultMgr { + /* 0x000 */ OSThread thread; + /* 0x1B0 */ STACK(stack, 0x600); // Seems leftover from an earlier version. The thread actually uses a stack of this size at 0x8009BE60 + /* 0x7B0 */ OSMesgQueue queue; + /* 0x7C8 */ OSMesg msg[1]; + /* 0x7CC */ u8 exit; + /* 0x7CD */ u8 msgId; // 1 - CPU Break; 2 - Fault; 3 - Unknown + /* 0x7CE */ u8 faultHandlerEnabled; + /* 0x7CF */ u8 autoScroll; + /* 0x7D0 */ OSThread* faultedThread; + /* 0x7D4 */ FaultPadCallback padCallback; + /* 0x7D8 */ FaultClient* clients; + /* 0x7DC */ FaultAddrConvClient* addrConvClients; + /* 0x7E0 */ UNK_TYPE1 unk_7E0[0x4]; + /* 0x7E4 */ Input inputs[MAXCONTROLLERS]; + /* 0x844 */ void* fb; +} FaultMgr; // size = 0x848 + +extern FaultMgr gFaultMgr; + + +#endif diff --git a/include/fault_internal.h b/include/fault_internal.h new file mode 100644 index 0000000000..24379e629c --- /dev/null +++ b/include/fault_internal.h @@ -0,0 +1,18 @@ +#ifndef FAULT_INTERNAL_H +#define FAULT_INTERNAL_H + +#include "ultra64.h" + + +typedef void (*FaultDrawerCallback)(void); + +void FaultDrawer_SetOsSyncPrintfEnabled(u32 enabled); +void FaultDrawer_DrawRecImpl(s32 xStart, s32 yStart, s32 xEnd, s32 yEnd, u16 color); +void FaultDrawer_FillScreen(void); +void* FaultDrawer_FormatStringFunc(void* arg, const char* str, size_t count); +void FaultDrawer_SetDrawerFrameBuffer(void* frameBuffer, u16 w, u16 h); +void FaultDrawer_SetInputCallback(FaultDrawerCallback callback); +void FaultDrawer_Init(void); + + +#endif diff --git a/include/functions.h b/include/functions.h index 66365fefe5..55d59ec455 100644 --- a/include/functions.h +++ b/include/functions.h @@ -43,64 +43,7 @@ void func_800818F4(void); void __osSyncVPrintf(const char* fmt, ...); void osSyncPrintf(const char* fmt, ...); void rmonPrintf(const char* fmt, ...); -void Fault_SleepImpl(u32 duration); -void Fault_AddClient(FaultClient* client, void* callback, void* param0, void* param1); -void Fault_RemoveClient(FaultClient* client); -void Fault_AddAddrConvClient(FaultAddrConvClient* client, void* callback, void* param); -void Fault_RemoveAddrConvClient(FaultAddrConvClient* client); -void* Fault_ConvertAddress(void* addr); -void Fault_Sleep(u32 duration); -void Fault_PadCallback(Input* input); -void Fault_UpdatePadImpl(void); -u32 Fault_WaitForInputImpl(void); -void Fault_WaitForInput(void); -void Fault_DrawRec(s32 x, s32 y, s32 w, s32 h, u16 color); -void Fault_FillScreenBlack(void); -void Fault_FillScreenRed(void); -void Fault_DrawCornerRec(u16 color); -void Fault_PrintFReg(s32 idx, f32* value); -void osSyncPrintfFReg(s32 idx, f32* value); -void Fault_PrintFPCR(u32 value); -void osSyncPrintfFPCR(u32 value); -void Fault_PrintThreadContext(OSThread* t); -void osSyncPrintfThreadContext(OSThread* t); -OSThread* Fault_FindFaultedThread(void); -void Fault_Wait5Seconds(void); -void Fault_WaitForButtonCombo(void); -void Fault_DrawMemDumpPage(const char* title, u32* addr, u32 param_3); -void Fault_DrawMemDump(u32 pc, u32 sp, u32 unk0, u32 unk1); -void Fault_FindNextStackCall(uintptr_t* spPtr, uintptr_t* pcPtr, uintptr_t* raPtr); -void Fault_DrawStackTrace(OSThread* t, u32 flags); -void osSyncPrintfStackTrace(OSThread* t, u32 flags); -void Fault_ResumeThread(OSThread* t); -void Fault_CommitFB(void); -void Fault_ProcessClients(void); -void Fault_SetOptionsFromController3(void); -void Fault_SetOptions(void); -void Fault_ThreadEntry(void* arg); -void Fault_SetFB(void* fb, u16 w, u16 h); -void Fault_Start(void); -void Fault_HangupFaultClient(const char* arg0, char* arg1); -void Fault_AddHungupAndCrashImpl(const char* arg0, char* arg1); -void Fault_AddHungupAndCrash(const char* filename, u32 line); -void FaultDrawer_SetOsSyncPrintfEnabled(u32 enabled); -void FaultDrawer_DrawRecImpl(s32 xStart, s32 yStart, s32 xEnd, s32 yEnd, u16 color); -void FaultDrawer_DrawChar(char c); -s32 FaultDrawer_ColorToPrintColor(u16 color); -void FaultDrawer_UpdatePrintColor(void); -void FaultDrawer_SetForeColor(u16 color); -void FaultDrawer_SetBackColor(u16 color); -void FaultDrawer_SetFontColor(u16 color); -void FaultDrawer_SetCharPad(s8 padW, s8 padH); -void FaultDrawer_SetCursor(s32 x, s32 y); -void FaultDrawer_FillScreen(void); -void* FaultDrawer_FormatStringFunc(void* arg, const char* str, size_t count); -void FaultDrawer_VPrintf(const char* fmt, va_list ap); -void FaultDrawer_Printf(const char* fmt, ...); -void FaultDrawer_DrawText(s32 x, s32 y, const char* fmt, ...); -void FaultDrawer_SetDrawerFB(void* fb, u16 w, u16 h); -void FaultDrawer_SetInputCallback(FaultDrawerCallback callback); -void FaultDrawer_Init(void); + void func_80084940(void); void func_80084968(void); @@ -192,7 +135,7 @@ void __osSiRelAccess(void); s32 osContInit(OSMesgQueue* mq, u8* bitpattern, OSContStatus* data); void __osContGetInitData(u8* pattern, OSContStatus* data); void __osPackRequestData(u8 poll); -void osCreateThread(OSThread* t, OSId id, void* entry, void* arg, void* sp, OSPri p); +void osCreateThread(OSThread* thread, OSId id, void* entry, void* arg, void* sp, OSPri p); s32 osContStartReadData(OSMesgQueue* mq); void osContGetReadData(OSContPad* data); void __osPackReadData(void); @@ -270,7 +213,7 @@ s32 __osSpDeviceBusy(void); s32 __osSiDeviceBusy(void); void guMtxIdent(Mtx* mtx); s32 osJamMesg(OSMesgQueue* mq, OSMesg msg, s32 flag); -void osSetThreadPri(OSThread* t, OSPri p); +void osSetThreadPri(OSThread* thread, OSPri p); OSPri osGetThreadPri(OSThread* t); s32 __osEPiRawReadIo(OSPiHandle* handle, uintptr_t devAddr, u32* data); void osViSwapBuffer(void* frameBufPtr); @@ -718,7 +661,7 @@ void Actor_DrawDamageEffects(PlayState* play, Actor* actor, Vec3f limbPos[], s16 void Actor_SpawnIceEffects(PlayState* play, Actor* actor, Vec3f limbPos[], s32 limbPosCount, s32 effectsPerLimb, f32 scale, f32 scaleRange); void ActorOverlayTable_FaultClient(void* arg0, void* arg1); -void* ActorOverlayTable_FaultAddrConv(void* address, void* param); +uintptr_t ActorOverlayTable_FaultAddrConv(uintptr_t address, void* param); void ActorOverlayTable_Init(void); void ActorOverlayTable_Cleanup(void); @@ -1365,7 +1308,7 @@ void Message_FindCreditsMessage(PlayState* play, u16 textId); void func_8015E7EC(PlayState* play, UNK_PTR puParm2); // void func_8015F8A8(UNK_TYPE4 ctxt); -void* KaleidoManager_FaultAddrConv(void* address, void* param); +uintptr_t KaleidoManager_FaultAddrConv(uintptr_t address, void* param); void KaleidoManager_LoadOvl(KaleidoMgrOverlay* ovl); void KaleidoManager_ClearOvl(KaleidoMgrOverlay* ovl); void KaleidoManager_Init(PlayState* play); @@ -1498,7 +1441,7 @@ void Graph_FaultClient(void); void Graph_InitTHGA(TwoHeadGfxArena* arena, Gfx* buffer, s32 size); void Graph_SetNextGfxPool(GraphicsContext* gfxCtx); GameStateOverlay* Graph_GetNextGameState(GameState* gameState); -void* Graph_FaultAddrConv(void* address, void* param); +uintptr_t Graph_FaultAddrConv(uintptr_t address, void* param); void Graph_Init(GraphicsContext* gfxCtx); void Graph_Destroy(GraphicsContext* gfxCtx); void Graph_TaskSet00(GraphicsContext* gfxCtx, GameState* gameState); diff --git a/include/ultra64/convert.h b/include/ultra64/convert.h index 59d8b801c3..68e91a85ad 100644 --- a/include/ultra64/convert.h +++ b/include/ultra64/convert.h @@ -18,6 +18,9 @@ #define OS_PHYSICAL_TO_K0(x) (void*)(((u32)(x)+0x80000000)) #define OS_PHYSICAL_TO_K1(x) (void*)(((u32)(x)+0xA0000000)) +#define OS_MSEC_TO_CYCLES(n) OS_USEC_TO_CYCLES((n) * 1000) +#define OS_SEC_TO_CYCLES(n) OS_MSEC_TO_CYCLES((n) * 1000) + /* Functions */ extern uintptr_t osVirtualToPhysical(void*); diff --git a/include/ultra64/thread.h b/include/ultra64/thread.h index f060ca34b6..4adf025e1e 100644 --- a/include/ultra64/thread.h +++ b/include/ultra64/thread.h @@ -20,11 +20,16 @@ #define OS_PRIORITY_VIMGR 254 #define OS_PRIORITY_MAX 255 +#define OS_PRIORITY_THREADTAIL -1 + #define OS_STATE_STOPPED (1 << 0) #define OS_STATE_RUNNABLE (1 << 1) #define OS_STATE_RUNNING (1 << 2) #define OS_STATE_WAITING (1 << 3) +#define OS_FLAG_CPU_BREAK 1 +#define OS_FLAG_FAULT 2 + typedef s32 OSPri; typedef s32 OSId; diff --git a/include/variables.h b/include/variables.h index 7faaf58d40..6b2522e4b5 100644 --- a/include/variables.h +++ b/include/variables.h @@ -24,10 +24,7 @@ extern vs32 gIrqMgrResetStatus; extern volatile OSTime sIrqMgrResetTime; extern volatile OSTime sIrqMgrRetraceTime; extern s32 sIrqMgrRetraceCount; -extern const char* sCpuExceptions[18]; -extern const char* sFpuExceptions[6]; -extern FaultDrawer* sFaultDrawContext; -extern FaultDrawer sFaultDrawerDefault; + // extern UNK_TYPE1 sGfxPrintFontTLUT; // extern UNK_TYPE1 sGfxPrintRainbowTLUT; // extern UNK_TYPE1 sGfxPrintRainbowData; @@ -92,136 +89,7 @@ extern char yaz0String8009823C[]; extern char D_80098280[]; extern char D_80098290[]; extern char D_800982A4[]; -extern char D_800982B0[]; -extern char D_800982BC[]; -extern char D_800982D0[]; -extern char D_800982E8[]; -extern char D_80098300[]; -extern char D_80098318[]; -extern char D_80098330[]; -extern char D_80098344[]; -extern char D_80098358[]; -extern char D_80098370[]; -extern char D_80098388[]; -extern char D_800983A0[]; -extern char D_800983B8[]; -extern char D_800983CC[]; -extern char D_800983DC[]; -extern char D_800983F8[]; -extern char D_80098414[]; -extern char D_8009842C[]; -extern char D_80098448[]; -extern char D_80098460[]; -extern char D_80098474[]; -extern char D_80098488[]; -extern char D_80098494[]; -extern char D_800984A0[]; -extern char D_800984B4[55]; -extern char D_800984EC[54]; -extern char D_80098524[71]; -extern char D_8009856C[74]; -extern char D_800985B8[]; -extern char D_800985C8[]; -extern char D_800985DC[]; -extern char D_800985EC[]; -extern char D_80098600[]; -extern char D_80098610[]; -extern char D_80098618[]; -extern char D_8009861C[]; -extern char D_8009862C[]; -extern char D_80098634[]; -extern char D_80098648[]; -extern char D_80098664[]; -extern char D_80098680[]; -extern char D_8009869C[]; -extern char D_800986B8[]; -extern char D_800986D4[]; -extern char D_800986F0[]; -extern char D_8009870C[]; -extern char D_80098728[]; -extern char D_80098744[]; -extern char D_80098760[]; -extern char D_80098780[]; -extern char D_80098784[]; -extern char D_80098788[]; -extern char D_8009878C[]; -extern char D_80098790[]; -extern char D_80098794[]; -extern char D_80098798[]; -extern char D_8009879C[]; -extern char D_800987A0[]; -extern char D_800987A4[]; -extern char D_800987B0[]; -extern char D_800987B4[]; -extern char D_800987CC[]; -extern char D_800987EC[]; -extern char D_8009880C[]; -extern char D_8009882C[]; -extern char D_8009884C[]; -extern char D_8009886C[]; -extern char D_8009888C[]; -extern char D_800988AC[]; -extern char D_800988CC[]; -extern char D_800988EC[]; -extern char D_8009890C[]; -extern char D_8009892C[]; -extern char D_80098930[]; -extern char D_80098934[]; -extern char D_80098938[]; -extern char D_8009893C[]; -extern char D_80098940[]; -extern char D_80098944[]; -extern char D_80098948[]; -extern char D_8009894C[]; -extern char D_80098950[]; -extern char D_80098954[]; -extern char D_8009895C[]; -extern char D_80098968[]; -extern char D_80098970[]; -extern char D_80098978[]; -extern char D_80098980[]; -extern char D_8009898C[]; -extern char D_800989A4[]; -extern char D_800989B0[]; -extern char D_800989BC[]; -extern char D_800989CC[]; -extern char D_800989D8[]; -extern char D_800989F4[]; -extern char D_80098A00[]; -extern char D_80098A0C[]; -extern char D_80098A1C[]; -extern char D_80098A20[34]; -extern const char D_80098A44[]; -extern const char D_80098A68[]; -extern char D_80098A88[53]; -extern char D_80098AC0[49]; -extern char D_80098AF4[51]; -extern char D_80098B28[]; -extern char D_80098B4C[]; -extern char D_80098B68[]; -extern char D_80098B84[]; -extern char D_80098BA0[]; -extern char D_80098BBC[]; -extern const char faultThreadName[]; -extern char D_80098BE0[]; -extern char D_80098BF8[]; -extern char D_80098BFC[]; -extern char D_80098C04[]; -extern char D_80098C08[]; -extern char D_80098C10[]; -extern char D_80098C28[]; -extern char D_80098C2C[]; -extern char D_80098C34[]; -extern char D_80098C38[]; -extern char D_80098C40[]; -extern char D_80099050[]; -extern char D_80099054[]; -extern char D_8009905C[]; -extern char D_80099064[]; -extern char D_80099070[]; -extern const char D_80099078[]; -extern const char D_8009907C[]; -extern const char D_80099080[]; + extern char D_800990B0[]; extern f32 D_800990C0[9]; extern f32 D_800990E4; @@ -277,9 +145,6 @@ extern void* gYaz0DecompressDstEnd; // extern UNK_TYPE4 D_8009BE34; // extern FaultClient romInfoFaultClient; -extern FaultThreadStruct gFaultStruct; - -extern FaultDrawer sFaultDrawerStruct; // extern UNK_TYPE4 D_8009CD10; extern u32 sRandFloat; // extern UNK_TYPE4 sArenaLockMsg; @@ -2067,21 +1932,6 @@ extern f32 D_801ED8D0; // extern UNK_TYPE1 D_801ED8DC; extern Mtx D_801ED8E0; extern Actor* D_801ED920; -extern FaultClient sActorOverlayTableFaultClient; -extern FaultAddrConvClient sActorOverlayTableFaultAddrConvClient; -extern char D_801ED950[80]; -extern char D_801ED9A0[80]; -extern Vec3f D_801ED9F0[3]; -extern Vec3f D_801EDA18[3]; -extern MtxF sModelToWorldMtxF; -extern Vec3f D_801EDA80[3]; -extern char D_801EDAA8[80]; -extern char D_801EDAF8[80]; -extern Vec3f D_801EDB48[3]; -extern Vec3f D_801EDB70[3]; -extern Plane D_801EDB98; -extern Sphere16 D_801EDBA8; -extern TriNorm D_801EDBB0; extern Vec3f D_801EDE00; extern Vec3f D_801EDE10; @@ -2219,7 +2069,7 @@ extern s16 D_801F4E7A; // extern UNK_TYPE1 D_801F6B58; extern void (*sKaleidoScopeUpdateFunc)(PlayState* play); extern void (*sKaleidoScopeDrawFunc)(PlayState* play); -extern FaultAddrConvClient sKaleidoMgrFaultAddrConvClient; + extern s16 sTransitionFillTimer; extern Input D_801F6C18; extern TransitionTile sTransitionTile; @@ -2237,19 +2087,12 @@ extern struct_801F8010 D_801F8010; extern VisZbuf sVisZbuf; extern VisMono sMonoColors; extern ViMode D_801F8048; -extern FaultAddrConvClient sGraphFaultAddrConvClient; -extern FaultClient sGraphFaultClient; -extern GfxMasterList* gGfxMasterDL; -extern CfbInfo sGraphCfbInfos[3]; -extern OSTime sGraphTaskStartTime; -extern FaultClient sSchedFaultClient; -extern OSTime sRSPGFXStartTime; -extern OSTime sRSPAudioStartTime; -extern OSTime sRSPOtherStartTime; -extern OSTime sRDPStartTime; +extern GfxMasterList* gGfxMasterDL; + extern u64* gAudioSPDataPtr; extern u32 gAudioSPDataSize; + extern volatile OSTime D_801FBAE0; extern volatile OSTime D_801FBAE8; extern volatile OSTime D_801FBAF0; diff --git a/include/z64.h b/include/z64.h index d03b2fd44a..ba2d5ffc38 100644 --- a/include/z64.h +++ b/include/z64.h @@ -75,40 +75,6 @@ typedef struct { /* 0x14 */ OSMesgQueue messageQueue; } FlashromRequest; // size = 0x2C -// End of RDRAM without the Expansion Pak installed -#define NORMAL_RDRAM_END 0x80400000 -// End of RDRAM with the Expansion Pak installed -#define EXPANDED_RDRAM_END 0x80800000 -// Address at the end of normal RDRAM after which is room for a screen buffer -#define FAULT_FB_ADDRESS (NORMAL_RDRAM_END - sizeof(u16[SCREEN_HEIGHT][SCREEN_WIDTH])) - -typedef void (*FaultDrawerCallback)(void); - -typedef struct { - /* 0x00 */ u16* fb; - /* 0x04 */ u16 w; - /* 0x06 */ u16 h; - /* 0x08 */ u16 yStart; - /* 0x0A */ u16 yEnd; - /* 0x0C */ u16 xStart; - /* 0x0E */ u16 xEnd; - /* 0x10 */ u16 foreColor; - /* 0x12 */ u16 backColor; - /* 0x14 */ u16 cursorX; - /* 0x16 */ u16 cursorY; - /* 0x18 */ const u32* font; - /* 0x1C */ u8 charW; - /* 0x1D */ u8 charH; - /* 0x1E */ s8 charWPad; - /* 0x1F */ s8 charHPad; - /* 0x20 */ u16 printColors[10]; - /* 0x34 */ u8 escCode; - /* 0x35 */ u8 osSyncPrintfEnabled; - /* 0x38 */ FaultDrawerCallback inputCallback; -} FaultDrawer; // size = 0x3C - -typedef void(*fault_update_input_func)(Input* input); - typedef struct { /* 0x000 */ View view; /* 0x168 */ u8* iconItemSegment; @@ -315,37 +281,6 @@ typedef struct { /* 0x24 */ u32 flags; } PreRenderParams; // size = 0x28 -typedef struct FaultAddrConvClient { - /* 0x0 */ struct FaultAddrConvClient* next; - /* 0x4 */ void* (*callback)(void*, void*); - /* 0x8 */ void* param; -} FaultAddrConvClient; // size = 0xC - -typedef struct FaultClient { - /* 0x0 */ struct FaultClient* next; - /* 0x4 */ void (*callback)(void*, void*); - /* 0x8 */ void* param0; - /* 0xC */ void* param1; -} FaultClient; // size = 0x10 - -typedef struct { - /* 0x000 */ OSThread thread; - /* 0x1B0 */ u8 stack[1536]; // Seems leftover from an earlier version. The thread actually uses a stack of this size at 0x8009BE60 - /* 0x7B0 */ OSMesgQueue queue; - /* 0x7C8 */ OSMesg msg[1]; - /* 0x7CC */ u8 exitDebugger; - /* 0x7CD */ u8 msgId; // 1 - CPU Break; 2 - Fault; 3 - Unknown - /* 0x7CE */ u8 faultHandlerEnabled; - /* 0x7CF */ u8 faultActive; - /* 0x7D0 */ OSThread* faultedThread; - /* 0x7D4 */ fault_update_input_func padCallback; - /* 0x7D8 */ FaultClient* clients; - /* 0x7DC */ FaultAddrConvClient* addrConvClients; - /* 0x7E0 */ UNK_TYPE1 pad7E0[0x4]; - /* 0x7E4 */ Input padInput[MAXCONTROLLERS]; - /* 0x844 */ void* fb; -} FaultThreadStruct; // size = 0x848 - struct PlayState; typedef struct { diff --git a/include/z64thread.h b/include/z64thread.h index ce63cfb150..21a397fe51 100644 --- a/include/z64thread.h +++ b/include/z64thread.h @@ -1,16 +1,17 @@ #ifndef Z64THREAD_H #define Z64THREAD_H -#define Z_THREAD_ID_IDLE 1 -#define Z_THREAD_ID_SLOWLY 2 -#define Z_THREAD_ID_MAIN 3 -#define Z_THREAD_ID_GRAPH 4 -#define Z_THREAD_ID_SCHED 5 -#define Z_THREAD_ID_PADMGR 7 +#define Z_THREAD_ID_IDLE 1 +#define Z_THREAD_ID_SLOWLY 2 +#define Z_THREAD_ID_FAULT 2 +#define Z_THREAD_ID_MAIN 3 +#define Z_THREAD_ID_GRAPH 4 +#define Z_THREAD_ID_SCHED 5 +#define Z_THREAD_ID_PADMGR 7 #define Z_THREAD_ID_AUDIOMGR 10 #define Z_THREAD_ID_FLASHROM 13 -#define Z_THREAD_ID_DMAMGR 18 -#define Z_THREAD_ID_IRQMGR 19 +#define Z_THREAD_ID_DMAMGR 18 +#define Z_THREAD_ID_IRQMGR 19 #define Z_PRIORITY_SLOWLY 5 #define Z_PRIORITY_GRAPH 9 @@ -22,5 +23,6 @@ #define Z_PRIORITY_SCHED 16 #define Z_PRIORITY_DMAMGR 17 #define Z_PRIORITY_IRQMGR 18 +#define Z_PRIORITY_FAULT OS_PRIORITY_APPMAX #endif diff --git a/spec b/spec index 781efb202c..3e7b86be02 100644 --- a/spec +++ b/spec @@ -24,7 +24,6 @@ beginseg include "build/src/boot_O2_g3/CIC6105.o" include "build/src/boot_O2_g3/syncprintf.o" include "build/src/boot_O2_g3/fault.o" - include "build/data/boot/fault.bss.o" include "build/src/boot_O2_g3/fault_drawer.o" include "build/src/boot_O2/boot_80084940.o" include "build/src/boot_O2/loadfragment.o" diff --git a/src/boot_O2/assert.c b/src/boot_O2/assert.c index 9bf98c2db1..04fcf87fee 100644 --- a/src/boot_O2/assert.c +++ b/src/boot_O2/assert.c @@ -1,4 +1,5 @@ #include "global.h" +#include "fault.h" void __assert(const char* file, u32 lineNum) { osGetThreadId(NULL); diff --git a/src/boot_O2_g3/CIC6105.c b/src/boot_O2_g3/CIC6105.c index 78eed1a775..b45b193135 100644 --- a/src/boot_O2_g3/CIC6105.c +++ b/src/boot_O2_g3/CIC6105.c @@ -1,4 +1,5 @@ #include "global.h" +#include "fault.h" UNK_TYPE4 D_8009BE30; UNK_TYPE4 D_8009BE34; @@ -17,7 +18,7 @@ void CIC6105_PrintRomInfo(void) { } void CIC6105_AddRomInfoFaultPage(void) { - Fault_AddClient(&romInfoFaultClient, CIC6105_PrintRomInfo, 0, 0); + Fault_AddClient(&romInfoFaultClient, (void*)CIC6105_PrintRomInfo, NULL, NULL); } void CIC6105_RemoveRomInfoFaultPage(void) { diff --git a/src/boot_O2_g3/fault.c b/src/boot_O2_g3/fault.c index eb190859e0..30166d785f 100644 --- a/src/boot_O2_g3/fault.c +++ b/src/boot_O2_g3/fault.c @@ -1,15 +1,55 @@ -#include "ultra64.h" +/** + * @file fault.c + * + * This file implements the screen that may be viewed when the game crashes. + * This is the second known version of the crash screen, an evolved version from OoT's. + * + * When the game crashes, a red bar will be drawn to the top-left of the screen, indicating that the + * crash screen is available for use. Once this bar appears, it is possible to open the crash screen + * with the following button combination: + * + * (DPad-Left & L & R & C-Right) & Start + * + * When entering this button combination, buttons that are &'d together must all be pressed together. + * + * "Clients" may be registered with the crash screen to extend its functionality. There are + * two kinds of client, "Client" and "AddressConverterClient". Clients contribute one or + * more pages to the crash debugger, while Address Converter Clients allow the crash screen to look up + * the virtual addresses of dynamically allocated overlays. + * + * The crash screen has multiple pages: + * - Thread Context + * This page shows information about the thread on which the program crashed. It displays + * the cause of the crash, state of general-purpose registers, state of floating-point registers + * and the floating-point status register. If a floating-point exception caused the crash, it will + * be displayed next to the floating-point status register. + * - Stack Trace + * This page displays a full backtrace from the crashing function back to the start of the thread. It + * displays the Program Counter for each function and, if applicable, the Virtual Program Counter + * for relocated functions in overlays. + * - Client Pages + * After the stack trace page, currently registered clients are processed and their pages are displayed. + * - Memory Dump + * This page implements a scrollable memory dump. + * - End Screen + * This page informs you that there are no more pages to display. + * + * To navigate the pages, START and A may be used to advance to the next page, and L toggles whether to + * automatically scroll to the next page after some time has passed. + * DPad-Up may be pressed to enable sending fault pages over osSyncPrintf as well as displaying them on-screen. + * DPad-Down disables sending fault pages over osSyncPrintf. + */ + +#include "fault_internal.h" +#include "fault.h" +#include "prevent_bss_reordering.h" #include "global.h" -#include "stack.h" #include "vt.h" #include "stackcheck.h" +#include "z64thread.h" -extern FaultThreadStruct* sFaultContext; -extern f32 D_8009BE54; -extern u32 faultCustomOptions; -extern u32 faultCopyToLog; -extern STACK(sFaultStack, 0x600); -extern StackEntry sFaultStackInfo; +FaultMgr* sFaultInstance; +f32 sFaultTimeTotal; // read but not set anywhere // data const char* sCpuExceptions[] = { @@ -38,23 +78,30 @@ const char* sFpuExceptions[] = { }; void Fault_SleepImpl(u32 duration) { - u64 value = (duration * OS_CPU_COUNTER) / 1000ULL; + OSTime value = (duration * OS_CPU_COUNTER) / 1000ULL; + Sleep_Cycles(value); } -void Fault_AddClient(FaultClient* client, void* callback, void* param0, void* param1) { +/** + * Registers a fault client. + * + * Clients contribute at least one page to the crash screen, drawn by `callback`. + * Arguments are passed on to the callback through `arg0` and `arg1`. + */ +void Fault_AddClient(FaultClient* client, FaultClientCallback callback, void* arg0, void* arg1) { OSIntMask mask; - u32 alreadyExists = 0; + u32 alreadyExists = false; mask = osSetIntMask(1); + // Ensure the client is not already registered { + FaultClient* iter = sFaultInstance->clients; - FaultClient* iter = sFaultContext->clients; - - while (iter) { + while (iter != NULL) { if (iter == client) { - alreadyExists = 1; + alreadyExists = true; goto end; } iter = iter->next; @@ -62,10 +109,10 @@ void Fault_AddClient(FaultClient* client, void* callback, void* param0, void* pa } client->callback = callback; - client->param0 = param0; - client->param1 = param1; - client->next = sFaultContext->clients; - sFaultContext->clients = client; + client->arg0 = arg0; + client->arg1 = arg1; + client->next = sFaultInstance->clients; + sFaultInstance->clients = client; end: osSetIntMask(mask); @@ -75,26 +122,25 @@ end: } } +/** + * Removes a fault client so that the page is no longer displayed if a crash occurs. + */ void Fault_RemoveClient(FaultClient* client) { - FaultClient* iter; - FaultClient* lastIter; + FaultClient* iter = sFaultInstance->clients; + FaultClient* lastIter = NULL; OSIntMask mask; - u32 listIsEmpty; - - iter = sFaultContext->clients; - listIsEmpty = 0; - lastIter = NULL; + u32 listIsEmpty = false; mask = osSetIntMask(1); while (iter) { if (iter == client) { - if (lastIter) { + if (lastIter != NULL) { lastIter->next = client->next; } else { - sFaultContext->clients = client; - if (sFaultContext->clients) { - sFaultContext->clients = client->next; + sFaultInstance->clients = client; + if (sFaultInstance->clients) { + sFaultInstance->clients = client->next; } else { listIsEmpty = 1; } @@ -113,27 +159,38 @@ void Fault_RemoveClient(FaultClient* client) { } } -void Fault_AddAddrConvClient(FaultAddrConvClient* client, void* callback, void* param) { +/** + * Registers an address converter client. This enables the crash screen to look up virtual + * addresses of overlays relocated during runtime. Address conversion is carried out by + * `callback`, which either returns a virtual address or NULL if the address could not + * be converted. + * + * The callback is intended to be + * `uintptr_t (*callback)(uintptr_t addr, void* arg)` + * The callback may return 0 if it could not convert the address + */ +void Fault_AddAddrConvClient(FaultAddrConvClient* client, FaultAddrConvClientCallback callback, void* arg) { OSIntMask mask; - u32 alreadyExists = 0; + s32 alreadyExists = false; mask = osSetIntMask(1); { + FaultAddrConvClient* iter = sFaultInstance->addrConvClients; - FaultAddrConvClient* iter = sFaultContext->addrConvClients; - while (iter) { + while (iter != NULL) { if (iter == client) { - alreadyExists = 1; + alreadyExists = true; goto end; } iter = iter->next; } } + client->callback = callback; - client->param = param; - client->next = sFaultContext->addrConvClients; - sFaultContext->addrConvClients = client; + client->arg = arg; + client->next = sFaultInstance->addrConvClients; + sFaultInstance->addrConvClients = client; end: osSetIntMask(mask); @@ -144,27 +201,23 @@ end: } void Fault_RemoveAddrConvClient(FaultAddrConvClient* client) { - FaultAddrConvClient* iter; - FaultAddrConvClient* lastIter; + FaultAddrConvClient* iter = sFaultInstance->addrConvClients; + FaultAddrConvClient* lastIter = NULL; OSIntMask mask; - u32 listIsEmpty; - - iter = sFaultContext->addrConvClients; - listIsEmpty = 0; - lastIter = NULL; + bool listIsEmpty = false; mask = osSetIntMask(1); while (iter) { if (iter == client) { - if (lastIter) { + if (lastIter != NULL) { lastIter->next = client->next; } else { - sFaultContext->addrConvClients = client; - if (sFaultContext->addrConvClients) { - sFaultContext->addrConvClients = client->next; + sFaultInstance->addrConvClients = client; + if (sFaultInstance->addrConvClients) { + sFaultInstance->addrConvClients = client->next; } else { - listIsEmpty = 1; + listIsEmpty = true; } } break; @@ -182,75 +235,88 @@ void Fault_RemoveAddrConvClient(FaultAddrConvClient* client) { } } -void* Fault_ConvertAddress(void* addr) { - void* ret; - FaultAddrConvClient* iter = sFaultContext->addrConvClients; +/** + * Converts `addr` to a virtual address via the registered + * address converter clients + */ +uintptr_t Fault_ConvertAddress(uintptr_t addr) { + uintptr_t ret; + FaultAddrConvClient* iter = sFaultInstance->addrConvClients; - while (iter) { - if (iter->callback) { - ret = iter->callback(addr, iter->param); - if (ret != NULL) { + while (iter != NULL) { + if (iter->callback != NULL) { + ret = iter->callback(addr, iter->arg); + if (ret != 0) { return ret; } } iter = iter->next; } - return NULL; + return 0; } -void Fault_Sleep(u32 duration) { - Fault_SleepImpl(duration); +void Fault_Sleep(u32 msec) { + Fault_SleepImpl(msec); } void Fault_PadCallback(Input* input) { PadMgr_GetInput2(input, false); } -void Fault_UpdatePadImpl() { - sFaultContext->padCallback(sFaultContext->padInput); +void Fault_UpdatePadImpl(void) { + sFaultInstance->padCallback(sFaultInstance->inputs); } -u32 Fault_WaitForInputImpl() { - Input* curInput = &sFaultContext->padInput[0]; +/** + * Awaits user input + * + * L toggles auto-scroll + * DPad-Up enables osSyncPrintf output + * DPad-Down disables osSyncPrintf output + * A and DPad-Right continues and returns true + * DPad-Left continues and returns false + */ +u32 Fault_WaitForInputImpl(void) { + Input* input = &sFaultInstance->inputs[0]; s32 count = 600; - u32 kDown; + u32 pressedBtn; - while (1) { - Fault_Sleep(0x10); + while (true) { + Fault_Sleep(1000 / 60); Fault_UpdatePadImpl(); - kDown = curInput->press.button; + pressedBtn = input->press.button; - if (kDown == BTN_L) { - sFaultContext->faultActive = !sFaultContext->faultActive; + if (pressedBtn == BTN_L) { + sFaultInstance->autoScroll = !sFaultInstance->autoScroll; } - if (sFaultContext->faultActive) { + if (sFaultInstance->autoScroll) { if (count-- < 1) { - return 0; + return false; } } else { - if (kDown == BTN_A || kDown == BTN_DRIGHT) { - return 0; + if ((pressedBtn == BTN_A) || (pressedBtn == BTN_DRIGHT)) { + return false; } - if (kDown == BTN_DLEFT) { - return 1; + if (pressedBtn == BTN_DLEFT) { + return true; } - if (kDown == BTN_DUP) { - FaultDrawer_SetOsSyncPrintfEnabled(1); + if (pressedBtn == BTN_DUP) { + FaultDrawer_SetOsSyncPrintfEnabled(true); } - if (kDown == BTN_DDOWN) { - FaultDrawer_SetOsSyncPrintfEnabled(0); + if (pressedBtn == BTN_DDOWN) { + FaultDrawer_SetOsSyncPrintfEnabled(false); } } } } -void Fault_WaitForInput() { +void Fault_WaitForInput(void) { Fault_WaitForInputImpl(); } @@ -258,36 +324,37 @@ void Fault_DrawRec(s32 x, s32 y, s32 w, s32 h, u16 color) { FaultDrawer_DrawRecImpl(x, y, x + w - 1, y + h - 1, color); } -void Fault_FillScreenBlack() { - FaultDrawer_SetForeColor(0xFFFF); - FaultDrawer_SetBackColor(1); +void Fault_FillScreenBlack(void) { + FaultDrawer_SetForeColor(GPACK_RGBA5551(255, 255, 255, 1)); + FaultDrawer_SetBackColor(GPACK_RGBA5551(0, 0, 0, 1)); FaultDrawer_FillScreen(); - FaultDrawer_SetBackColor(0); + FaultDrawer_SetBackColor(GPACK_RGBA5551(0, 0, 0, 0)); } -void Fault_FillScreenRed() { - FaultDrawer_SetForeColor(0xFFFF); - FaultDrawer_SetBackColor(0xF001); +void Fault_FillScreenRed(void) { + FaultDrawer_SetForeColor(GPACK_RGBA5551(255, 255, 255, 1)); + FaultDrawer_SetBackColor(GPACK_RGBA5551(240, 0, 0, 1)); FaultDrawer_FillScreen(); - FaultDrawer_SetBackColor(0); + FaultDrawer_SetBackColor(GPACK_RGBA5551(0, 0, 0, 0)); } void Fault_DrawCornerRec(u16 color) { - Fault_DrawRec(0x16, 0x10, 8, 1, color); + Fault_DrawRec(22, 16, 8, 1, color); } -void Fault_PrintFReg(s32 idx, f32* value) { +void Fault_PrintFReg(s32 index, f32* value) { u32 raw = *(u32*)value; s32 v0 = ((raw & 0x7F800000) >> 0x17) - 0x7F; if ((v0 >= -0x7E && v0 < 0x80) || raw == 0) { - FaultDrawer_Printf("F%02d:%14.7e ", idx, *value); + FaultDrawer_Printf("F%02d:%14.7e ", index, *value); } else { - FaultDrawer_Printf("F%02d: %08x(16) ", idx, raw); + // Print subnormal floats as their IEEE-754 hex representation + FaultDrawer_Printf("F%02d: %08x(16) ", index, raw); } } -void osSyncPrintfFReg(s32 idx, f32* value) { +void Fault_LogFReg(s32 idx, f32* value) { u32 raw = *(u32*)value; s32 v0 = ((raw & 0x7F800000) >> 0x17) - 0x7F; @@ -303,6 +370,9 @@ void Fault_PrintFPCR(u32 value) { u32 flag = 0x20000; FaultDrawer_Printf("FPCSR:%08xH ", value); + + // Go through each of the six causes and print the name of + // the first cause that is set for (i = 0; i < ARRAY_COUNT(sFpuExceptions); i++) { if (value & flag) { FaultDrawer_Printf("(%s)", sFpuExceptions[i]); @@ -313,7 +383,7 @@ void Fault_PrintFPCR(u32 value) { FaultDrawer_Printf("\n"); } -void osSyncPrintfFPCR(u32 value) { +void Fault_LogFPCSR(u32 value) { s32 i; u32 flag = 0x20000; @@ -327,183 +397,205 @@ void osSyncPrintfFPCR(u32 value) { } } -void Fault_PrintThreadContext(OSThread* t) { - __OSThreadContext* ctx; - s32 causeStrIdx = (s32)((((u32)t->context.cause >> 2) & 0x1F) << 0x10) >> 0x10; - if (causeStrIdx == 0x17) { - causeStrIdx = 0x10; +void Fault_PrintThreadContext(OSThread* thread) { + __OSThreadContext* threadCtx; + s16 causeStrIdx = _SHIFTR((u32)thread->context.cause, 2, 5); + + if (causeStrIdx == 23) { // Watchpoint + causeStrIdx = 16; } - if (causeStrIdx == 0x1F) { - causeStrIdx = 0x11; + if (causeStrIdx == 31) { // Virtual coherency on data + causeStrIdx = 17; } FaultDrawer_FillScreen(); FaultDrawer_SetCharPad(-2, 4); - FaultDrawer_SetCursor(0x16, 0x14); + FaultDrawer_SetCursor(22, 20); - ctx = &t->context; - FaultDrawer_Printf("THREAD:%d (%d:%s)\n", t->id, causeStrIdx, sCpuExceptions[causeStrIdx]); + threadCtx = &thread->context; + FaultDrawer_Printf("THREAD:%d (%d:%s)\n", thread->id, causeStrIdx, sCpuExceptions[causeStrIdx]); FaultDrawer_SetCharPad(-1, 0); - FaultDrawer_Printf("PC:%08xH SR:%08xH VA:%08xH\n", (u32)ctx->pc, (u32)ctx->sr, (u32)ctx->badvaddr); - FaultDrawer_Printf("AT:%08xH V0:%08xH V1:%08xH\n", (u32)ctx->at, (u32)ctx->v0, (u32)ctx->v1); - FaultDrawer_Printf("A0:%08xH A1:%08xH A2:%08xH\n", (u32)ctx->a0, (u32)ctx->a1, (u32)ctx->a2); - FaultDrawer_Printf("A3:%08xH T0:%08xH T1:%08xH\n", (u32)ctx->a3, (u32)ctx->t0, (u32)ctx->t1); - FaultDrawer_Printf("T2:%08xH T3:%08xH T4:%08xH\n", (u32)ctx->t2, (u32)ctx->t3, (u32)ctx->t4); - FaultDrawer_Printf("T5:%08xH T6:%08xH T7:%08xH\n", (u32)ctx->t5, (u32)ctx->t6, (u32)ctx->t7); - FaultDrawer_Printf("S0:%08xH S1:%08xH S2:%08xH\n", (u32)ctx->s0, (u32)ctx->s1, (u32)ctx->s2); - FaultDrawer_Printf("S3:%08xH S4:%08xH S5:%08xH\n", (u32)ctx->s3, (u32)ctx->s4, (u32)ctx->s5); - FaultDrawer_Printf("S6:%08xH S7:%08xH T8:%08xH\n", (u32)ctx->s6, (u32)ctx->s7, (u32)ctx->t8); - FaultDrawer_Printf("T9:%08xH GP:%08xH SP:%08xH\n", (u32)ctx->t9, (u32)ctx->gp, (u32)ctx->sp); - FaultDrawer_Printf("S8:%08xH RA:%08xH LO:%08xH\n\n", (u32)ctx->s8, (u32)ctx->ra, (u32)ctx->lo); + FaultDrawer_Printf("PC:%08xH SR:%08xH VA:%08xH\n", (u32)threadCtx->pc, (u32)threadCtx->sr, + (u32)threadCtx->badvaddr); + FaultDrawer_Printf("AT:%08xH V0:%08xH V1:%08xH\n", (u32)threadCtx->at, (u32)threadCtx->v0, (u32)threadCtx->v1); + FaultDrawer_Printf("A0:%08xH A1:%08xH A2:%08xH\n", (u32)threadCtx->a0, (u32)threadCtx->a1, (u32)threadCtx->a2); + FaultDrawer_Printf("A3:%08xH T0:%08xH T1:%08xH\n", (u32)threadCtx->a3, (u32)threadCtx->t0, (u32)threadCtx->t1); + FaultDrawer_Printf("T2:%08xH T3:%08xH T4:%08xH\n", (u32)threadCtx->t2, (u32)threadCtx->t3, (u32)threadCtx->t4); + FaultDrawer_Printf("T5:%08xH T6:%08xH T7:%08xH\n", (u32)threadCtx->t5, (u32)threadCtx->t6, (u32)threadCtx->t7); + FaultDrawer_Printf("S0:%08xH S1:%08xH S2:%08xH\n", (u32)threadCtx->s0, (u32)threadCtx->s1, (u32)threadCtx->s2); + FaultDrawer_Printf("S3:%08xH S4:%08xH S5:%08xH\n", (u32)threadCtx->s3, (u32)threadCtx->s4, (u32)threadCtx->s5); + FaultDrawer_Printf("S6:%08xH S7:%08xH T8:%08xH\n", (u32)threadCtx->s6, (u32)threadCtx->s7, (u32)threadCtx->t8); + FaultDrawer_Printf("T9:%08xH GP:%08xH SP:%08xH\n", (u32)threadCtx->t9, (u32)threadCtx->gp, (u32)threadCtx->sp); + FaultDrawer_Printf("S8:%08xH RA:%08xH LO:%08xH\n\n", (u32)threadCtx->s8, (u32)threadCtx->ra, (u32)threadCtx->lo); - Fault_PrintFPCR(ctx->fpcsr); + Fault_PrintFPCR(threadCtx->fpcsr); FaultDrawer_Printf("\n"); - Fault_PrintFReg(0, &ctx->fp0.f.f_even); - Fault_PrintFReg(2, &ctx->fp2.f.f_even); + Fault_PrintFReg(0, &threadCtx->fp0.f.f_even); + Fault_PrintFReg(2, &threadCtx->fp2.f.f_even); FaultDrawer_Printf("\n"); - Fault_PrintFReg(4, &ctx->fp4.f.f_even); - Fault_PrintFReg(6, &ctx->fp6.f.f_even); + Fault_PrintFReg(4, &threadCtx->fp4.f.f_even); + Fault_PrintFReg(6, &threadCtx->fp6.f.f_even); FaultDrawer_Printf("\n"); - Fault_PrintFReg(8, &ctx->fp8.f.f_even); - Fault_PrintFReg(0xA, &ctx->fp10.f.f_even); + Fault_PrintFReg(8, &threadCtx->fp8.f.f_even); + Fault_PrintFReg(0xA, &threadCtx->fp10.f.f_even); FaultDrawer_Printf("\n"); - Fault_PrintFReg(0xC, &ctx->fp12.f.f_even); - Fault_PrintFReg(0xE, &ctx->fp14.f.f_even); + Fault_PrintFReg(0xC, &threadCtx->fp12.f.f_even); + Fault_PrintFReg(0xE, &threadCtx->fp14.f.f_even); FaultDrawer_Printf("\n"); - Fault_PrintFReg(0x10, &ctx->fp16.f.f_even); - Fault_PrintFReg(0x12, &ctx->fp18.f.f_even); + Fault_PrintFReg(0x10, &threadCtx->fp16.f.f_even); + Fault_PrintFReg(0x12, &threadCtx->fp18.f.f_even); FaultDrawer_Printf("\n"); - Fault_PrintFReg(0x14, &ctx->fp20.f.f_even); - Fault_PrintFReg(0x16, &ctx->fp22.f.f_even); + Fault_PrintFReg(0x14, &threadCtx->fp20.f.f_even); + Fault_PrintFReg(0x16, &threadCtx->fp22.f.f_even); FaultDrawer_Printf("\n"); - Fault_PrintFReg(0x18, &ctx->fp24.f.f_even); - Fault_PrintFReg(0x1A, &ctx->fp26.f.f_even); + Fault_PrintFReg(0x18, &threadCtx->fp24.f.f_even); + Fault_PrintFReg(0x1A, &threadCtx->fp26.f.f_even); FaultDrawer_Printf("\n"); - Fault_PrintFReg(0x1C, &ctx->fp28.f.f_even); - Fault_PrintFReg(0x1E, &ctx->fp30.f.f_even); + Fault_PrintFReg(0x1C, &threadCtx->fp28.f.f_even); + Fault_PrintFReg(0x1E, &threadCtx->fp30.f.f_even); FaultDrawer_Printf("\n"); FaultDrawer_SetCharPad(0, 0); - if (D_8009BE54 != 0) { - FaultDrawer_DrawText(0xA0, 0xD8, "%5.2f sec\n", D_8009BE54); + if (sFaultTimeTotal != 0.0f) { + FaultDrawer_DrawText(160, 216, "%5.2f sec\n", sFaultTimeTotal); } } -void osSyncPrintfThreadContext(OSThread* t) { - __OSThreadContext* ctx; - s32 causeStrIdx = (s32)((((u32)t->context.cause >> 2) & 0x1F) << 0x10) >> 0x10; - if (causeStrIdx == 0x17) { - causeStrIdx = 0x10; +void osSyncPrintfThreadContext(OSThread* thread) { + __OSThreadContext* threadCtx; + s16 causeStrIdx = _SHIFTR((u32)thread->context.cause, 2, 5); + + if (causeStrIdx == 23) { // Watchpoint + causeStrIdx = 16; } - if (causeStrIdx == 0x1F) { - causeStrIdx = 0x11; + if (causeStrIdx == 31) { // Virtual coherency on data + causeStrIdx = 17; } - ctx = &t->context; + threadCtx = &thread->context; osSyncPrintf("\n"); - osSyncPrintf("THREAD ID:%d (%d:%s)\n", t->id, causeStrIdx, sCpuExceptions[causeStrIdx]); + osSyncPrintf("THREAD ID:%d (%d:%s)\n", thread->id, causeStrIdx, sCpuExceptions[causeStrIdx]); - osSyncPrintf("PC:%08xH SR:%08xH VA:%08xH\n", (u32)ctx->pc, (u32)ctx->sr, (u32)ctx->badvaddr); - osSyncPrintf("AT:%08xH V0:%08xH V1:%08xH\n", (u32)ctx->at, (u32)ctx->v0, (u32)ctx->v1); - osSyncPrintf("A0:%08xH A1:%08xH A2:%08xH\n", (u32)ctx->a0, (u32)ctx->a1, (u32)ctx->a2); - osSyncPrintf("A3:%08xH T0:%08xH T1:%08xH\n", (u32)ctx->a3, (u32)ctx->t0, (u32)ctx->t1); - osSyncPrintf("T2:%08xH T3:%08xH T4:%08xH\n", (u32)ctx->t2, (u32)ctx->t3, (u32)ctx->t4); - osSyncPrintf("T5:%08xH T6:%08xH T7:%08xH\n", (u32)ctx->t5, (u32)ctx->t6, (u32)ctx->t7); - osSyncPrintf("S0:%08xH S1:%08xH S2:%08xH\n", (u32)ctx->s0, (u32)ctx->s1, (u32)ctx->s2); - osSyncPrintf("S3:%08xH S4:%08xH S5:%08xH\n", (u32)ctx->s3, (u32)ctx->s4, (u32)ctx->s5); - osSyncPrintf("S6:%08xH S7:%08xH T8:%08xH\n", (u32)ctx->s6, (u32)ctx->s7, (u32)ctx->t8); - osSyncPrintf("T9:%08xH GP:%08xH SP:%08xH\n", (u32)ctx->t9, (u32)ctx->gp, (u32)ctx->sp); - osSyncPrintf("S8:%08xH RA:%08xH LO:%08xH\n", (u32)ctx->s8, (u32)ctx->ra, (u32)ctx->lo); + osSyncPrintf("PC:%08xH SR:%08xH VA:%08xH\n", (u32)threadCtx->pc, (u32)threadCtx->sr, (u32)threadCtx->badvaddr); + osSyncPrintf("AT:%08xH V0:%08xH V1:%08xH\n", (u32)threadCtx->at, (u32)threadCtx->v0, (u32)threadCtx->v1); + osSyncPrintf("A0:%08xH A1:%08xH A2:%08xH\n", (u32)threadCtx->a0, (u32)threadCtx->a1, (u32)threadCtx->a2); + osSyncPrintf("A3:%08xH T0:%08xH T1:%08xH\n", (u32)threadCtx->a3, (u32)threadCtx->t0, (u32)threadCtx->t1); + osSyncPrintf("T2:%08xH T3:%08xH T4:%08xH\n", (u32)threadCtx->t2, (u32)threadCtx->t3, (u32)threadCtx->t4); + osSyncPrintf("T5:%08xH T6:%08xH T7:%08xH\n", (u32)threadCtx->t5, (u32)threadCtx->t6, (u32)threadCtx->t7); + osSyncPrintf("S0:%08xH S1:%08xH S2:%08xH\n", (u32)threadCtx->s0, (u32)threadCtx->s1, (u32)threadCtx->s2); + osSyncPrintf("S3:%08xH S4:%08xH S5:%08xH\n", (u32)threadCtx->s3, (u32)threadCtx->s4, (u32)threadCtx->s5); + osSyncPrintf("S6:%08xH S7:%08xH T8:%08xH\n", (u32)threadCtx->s6, (u32)threadCtx->s7, (u32)threadCtx->t8); + osSyncPrintf("T9:%08xH GP:%08xH SP:%08xH\n", (u32)threadCtx->t9, (u32)threadCtx->gp, (u32)threadCtx->sp); + osSyncPrintf("S8:%08xH RA:%08xH LO:%08xH\n", (u32)threadCtx->s8, (u32)threadCtx->ra, (u32)threadCtx->lo); osSyncPrintf("\n"); - osSyncPrintfFPCR(ctx->fpcsr); + Fault_LogFPCSR(threadCtx->fpcsr); osSyncPrintf("\n"); - osSyncPrintfFReg(0, &ctx->fp0.f.f_even); - osSyncPrintfFReg(2, &ctx->fp2.f.f_even); + Fault_LogFReg(0, &threadCtx->fp0.f.f_even); + Fault_LogFReg(2, &threadCtx->fp2.f.f_even); osSyncPrintf("\n"); - osSyncPrintfFReg(4, &ctx->fp4.f.f_even); - osSyncPrintfFReg(6, &ctx->fp6.f.f_even); + Fault_LogFReg(4, &threadCtx->fp4.f.f_even); + Fault_LogFReg(6, &threadCtx->fp6.f.f_even); osSyncPrintf("\n"); - osSyncPrintfFReg(8, &ctx->fp8.f.f_even); - osSyncPrintfFReg(10, &ctx->fp10.f.f_even); + Fault_LogFReg(8, &threadCtx->fp8.f.f_even); + Fault_LogFReg(10, &threadCtx->fp10.f.f_even); osSyncPrintf("\n"); - osSyncPrintfFReg(12, &ctx->fp12.f.f_even); - osSyncPrintfFReg(14, &ctx->fp14.f.f_even); + Fault_LogFReg(12, &threadCtx->fp12.f.f_even); + Fault_LogFReg(14, &threadCtx->fp14.f.f_even); osSyncPrintf("\n"); - osSyncPrintfFReg(16, &ctx->fp16.f.f_even); - osSyncPrintfFReg(18, &ctx->fp18.f.f_even); + Fault_LogFReg(16, &threadCtx->fp16.f.f_even); + Fault_LogFReg(18, &threadCtx->fp18.f.f_even); osSyncPrintf("\n"); - osSyncPrintfFReg(20, &ctx->fp20.f.f_even); - osSyncPrintfFReg(22, &ctx->fp22.f.f_even); + Fault_LogFReg(20, &threadCtx->fp20.f.f_even); + Fault_LogFReg(22, &threadCtx->fp22.f.f_even); osSyncPrintf("\n"); - osSyncPrintfFReg(24, &ctx->fp24.f.f_even); - osSyncPrintfFReg(26, &ctx->fp26.f.f_even); + Fault_LogFReg(24, &threadCtx->fp24.f.f_even); + Fault_LogFReg(26, &threadCtx->fp26.f.f_even); osSyncPrintf("\n"); - osSyncPrintfFReg(28, &ctx->fp28.f.f_even); - osSyncPrintfFReg(30, &ctx->fp30.f.f_even); + Fault_LogFReg(28, &threadCtx->fp28.f.f_even); + Fault_LogFReg(30, &threadCtx->fp30.f.f_even); osSyncPrintf("\n"); } -OSThread* Fault_FindFaultedThread() { +/** + * Iterates through the active thread queue for a user thread with either + * the CPU break or Fault flag set. + */ +OSThread* Fault_FindFaultedThread(void) { OSThread* iter = __osGetActiveQueue(); - while (iter->priority != -1) { - if ((iter->priority > 0) && (iter->priority < 0x7F) && (iter->flags & 3)) { + + while (iter->priority != OS_PRIORITY_THREADTAIL) { + if ((iter->priority > OS_PRIORITY_IDLE) && (iter->priority < OS_PRIORITY_APPMAX) && + (iter->flags & (OS_FLAG_CPU_BREAK | OS_FLAG_FAULT))) { return iter; } iter = iter->tlnext; } + return NULL; } void Fault_Wait5Seconds(void) { - u32 pad; - OSTime start; - start = osGetTime(); - do { - Fault_Sleep(0x10); - } while ((osGetTime() - start) <= OS_USEC_TO_CYCLES(5000000)); + s32 pad; + OSTime start = osGetTime(); - sFaultContext->faultActive = 1; + do { + Fault_Sleep(1000 / 60); + } while ((osGetTime() - start) <= OS_SEC_TO_CYCLES(5)); + + sFaultInstance->autoScroll = true; } +/** + * Waits for the following button combination to be entered before returning: + * + * (DPad-Left & L & R & C-Right) & Start + */ void Fault_WaitForButtonCombo(void) { - Input* input = &sFaultContext->padInput[0]; + Input* input = &sFaultInstance->inputs[0]; + + FaultDrawer_SetForeColor(GPACK_RGBA5551(255, 255, 255, 1)); + FaultDrawer_SetBackColor(GPACK_RGBA5551(0, 0, 0, 1)); - FaultDrawer_SetForeColor(0xFFFF); - FaultDrawer_SetBackColor(1); do { do { - Fault_Sleep(0x10); + Fault_Sleep(1000 / 60); Fault_UpdatePadImpl(); } while (!CHECK_BTN_ALL(input->press.button, BTN_RESET)); } while (!CHECK_BTN_ALL(input->cur.button, BTN_DLEFT | BTN_L | BTN_R | BTN_CRIGHT)); } -void Fault_DrawMemDumpPage(const char* title, u32* addr, u32 param_3) { - u32* alignedAddr; +void Fault_DrawMemDumpContents(const char* title, uintptr_t addr, u32 param_3) { + uintptr_t alignedAddr = addr; u32* writeAddr; s32 y; s32 x; - alignedAddr = addr; - - if (alignedAddr < (u32*)0x80000000) { - alignedAddr = (u32*)0x80000000; + // Ensure address is within the bounds of RDRAM (Fault_DrawMemDump has already done this) + if (alignedAddr < K0BASE) { + alignedAddr = K0BASE; } - if (alignedAddr > (u32*)0x807FFF00) { - alignedAddr = (u32*)0x807FFF00; + // 8MB RAM, leave room to display 0x100 bytes on the final page + //! @bug The loop below draws 22 * 4 * 4 = 0x160 bytes per page. Due to this, by scrolling further than + //! 0x807FFEA0 some invalid bytes are read from outside of 8MB RDRAM space. This does not cause a crash, + //! however the values it displays are meaningless. On N64 hardware these invalid addresses are read as 0. + if (alignedAddr > (K0BASE + 0x800000 - 0x100)) { + alignedAddr = K0BASE + 0x800000 - 0x100; } - alignedAddr = (u32*)((u32)alignedAddr & ~3); - writeAddr = alignedAddr; + // Ensure address is word-aligned + alignedAddr &= ~3; + writeAddr = (u32*)alignedAddr; + Fault_FillScreenBlack(); FaultDrawer_SetCharPad(-2, 0); - FaultDrawer_DrawText(0x24, 0x12, "%s %08x", title ? title : "PrintDump", alignedAddr); - if (alignedAddr >= (u32*)0x80000000 && alignedAddr < (u32*)0xC0000000) { - for (y = 0x1C; y != 0xE2; y += 9) { - FaultDrawer_DrawText(0x18, y, "%06x", writeAddr); - for (x = 0x52; x != 0x122; x += 0x34) { - FaultDrawer_DrawText(x, y, "%08x", *writeAddr++); + FaultDrawer_DrawText(36, 18, "%s %08x", title ? title : "PrintDump", alignedAddr); + + if (alignedAddr >= K0BASE && alignedAddr < K2BASE) { + for (y = 0; y < 22; y++) { + FaultDrawer_DrawText(24, 28 + y * 9, "%06x", writeAddr); + for (x = 0; x < 4; x++) { + FaultDrawer_DrawText(82 + x * 52, 28 + y * 9, "%08x", *writeAddr++); } } } @@ -511,54 +603,79 @@ void Fault_DrawMemDumpPage(const char* title, u32* addr, u32 param_3) { FaultDrawer_SetCharPad(0, 0); } -void Fault_DrawMemDump(u32 pc, u32 sp, u32 unk0, u32 unk1) { - s32 count; +/** + * Draws the memory dump page. + * + * DPad-Up scrolls up. + * DPad-Down scrolls down. + * Holding A while scrolling speeds up scrolling by a factor of 0x10. + * Holding B while scrolling speeds up scrolling by a factor of 0x100. + * + * L toggles auto-scrolling pages. + * START and A move on to the next page. + * + * @param pc Program counter, pressing C-Up jumps to this address + * @param sp Stack pointer, pressing C-Down jumps to this address + * @param cLeftJump Unused parameter, pressing C-Left jumps to this address + * @param cRightJump Unused parameter, pressing C-Right jumps to this address + */ +void Fault_DrawMemDump(uintptr_t pc, uintptr_t sp, uintptr_t cLeftJump, uintptr_t cRightJump) { + s32 scrollCountdown; s32 off; - Input* input = &sFaultContext->padInput[0]; - u32 addr = pc; + Input* input = &sFaultInstance->inputs[0]; + uintptr_t addr = pc; do { - count = 0; - if (addr < 0x80000000) { - addr = 0x80000000; + scrollCountdown = 0; + // Ensure address is within the bounds of RDRAM + if (addr < K0BASE) { + addr = K0BASE; } - if (addr > 0x807FFF00) { - addr = 0x807FFF00; + // 8MB RAM, leave room to display 0x100 bytes on the final page + if (addr > (K0BASE + 0x800000 - 0x100)) { + addr = K0BASE + 0x800000 - 0x100; } + // Align down the address to 0x10 bytes and draw the page contents addr &= ~0xF; - Fault_DrawMemDumpPage("Dump", (u32*)addr, 0); + Fault_DrawMemDumpContents("Dump", addr, 0); - count = 600; - while (sFaultContext->faultActive) { - if (count == 0) { + scrollCountdown = 600; + while (sFaultInstance->autoScroll) { + // Count down until it's time to move on to the next page + if (scrollCountdown == 0) { return; } - count--; + scrollCountdown--; - Fault_Sleep(0x10); + Fault_Sleep(1000 / 60); Fault_UpdatePadImpl(); if (CHECK_BTN_ALL(input->press.button, BTN_L)) { - sFaultContext->faultActive = 0; + sFaultInstance->autoScroll = false; } } + + // Wait for input do { - Fault_Sleep(0x10); + Fault_Sleep(1000 / 60); Fault_UpdatePadImpl(); } while (input->press.button == 0); + // Move to next page if (CHECK_BTN_ALL(input->press.button, BTN_START)) { return; } + // Memory dump controls + off = 0x10; if (CHECK_BTN_ALL(input->cur.button, BTN_A)) { - off = 0x100; + off *= 0x10; } if (CHECK_BTN_ALL(input->cur.button, BTN_B)) { - off <<= 8; + off *= 0x100; } if (CHECK_BTN_ALL(input->press.button, BTN_DUP)) { addr -= off; @@ -573,204 +690,249 @@ void Fault_DrawMemDump(u32 pc, u32 sp, u32 unk0, u32 unk1) { addr = sp; } if (CHECK_BTN_ALL(input->press.button, BTN_CLEFT)) { - addr = unk0; + addr = cLeftJump; } if (CHECK_BTN_ALL(input->press.button, BTN_CRIGHT)) { - addr = unk1; + addr = cRightJump; } } while (!CHECK_BTN_ALL(input->press.button, BTN_L)); - sFaultContext->faultActive = 1; + // Resume auto-scroll and move to next page + sFaultInstance->autoScroll = true; } -void Fault_FindNextStackCall(uintptr_t* spPtr, uintptr_t* pcPtr, uintptr_t* raPtr) { +/** + * Searches a single function's stack frame for the function it was called from. + * There are two cases that must be covered: Leaf and non-leaf functions. + * + * A leaf function is one that does not call any other function, in this case the + * return address need not be saved to the stack. Since a leaf function does not + * call other functions, only the function the stack trace begins in could possibly + * be a leaf function, in which case the return address is in the thread context's + * $ra already, as it never left. + * + * The procedure is therefore + * - Iterate instructions + * - Once jr $ra is found, set pc to $ra + * - Done after delay slot + * + * A non-leaf function calls other functions, it is necessary for the return address + * to be saved to the stack. In these functions, it is important to keep track of the + * stack frame size of each function. + * + * The procedure is therefore + * - Iterate instructions + * - If lw $ra ($sp) is found, fetch the saved $ra from stack memory + * - If addiu $sp, $sp, is found, modify $sp by the immediate value + * - If jr $ra is found, set pc to $ra + * - Done after delay slot + * + * Note that searching for one jr $ra is sufficient, as only leaf functions can have + * multiple jr $ra in the same function. + * + * There is also additional handling for eret and j. Neither of these instructions + * appear in IDO compiled C, however do show up in the exception handler. It is not + * possible to backtrace through an eret as an interrupt can occur at any time, so + * there is no choice but to give up here. For j instructions, they can be followed + * and the backtrace may continue as normal. + */ +void Fault_WalkStack(uintptr_t* spPtr, uintptr_t* pcPtr, uintptr_t* raPtr) { uintptr_t sp = *spPtr; uintptr_t pc = *pcPtr; uintptr_t ra = *raPtr; - u32 lastOpc; - u16 opcHi; - s16 opcLo; + u32 lastInsn; + u16 insnHi; + s16 insnLo; u32 imm; - if (sp & 3 || sp < 0x80000000 || sp >= 0xC0000000 || ra & 3 || ra < 0x80000000 || ra >= 0xC0000000) { + if ((sp % 4 != 0) || (sp < K0BASE) || (sp >= K2BASE) || (ra % 4 != 0) || (ra < K0BASE) || (ra >= K2BASE)) { *spPtr = 0; *pcPtr = 0; *raPtr = 0; return; } - if (pc & 3 || pc < 0x80000000 || pc >= 0xC0000000) { + if ((pc % 4 != 0) || (pc < K0BASE) || (pc >= K2BASE)) { *pcPtr = ra; return; } - lastOpc = 0; + lastInsn = 0; while (true) { - opcHi = *(uintptr_t*)pc >> 16; - opcLo = *(uintptr_t*)pc & 0xFFFF; - imm = opcLo; + insnHi = *(uintptr_t*)pc >> 16; + insnLo = *(uintptr_t*)pc & 0xFFFF; + imm = insnLo; - if (opcHi == 0x8FBF) { + if (insnHi == 0x8FBF) { + // lw $ra, ($sp) + // read return address saved on the stack ra = *(uintptr_t*)(sp + imm); - } else if (opcHi == 0x27BD) { + } else if (insnHi == 0x27BD) { + // addiu $sp, $sp, + // stack pointer increment or decrement sp += imm; } else if (*(uintptr_t*)pc == 0x42000018) { + // eret + // cannot backtrace through an eret, give up sp = 0; pc = 0; ra = 0; - goto end; + goto done; } - if (lastOpc == 0x3E00008) { + if (lastInsn == 0x3E00008) { + // jr $ra + // return to previous function pc = ra; - goto end; - } else if ((lastOpc >> 26) == 2) { - pc = pc >> 28 << 28 | lastOpc << 6 >> 4; - goto end; + goto done; + } else if (lastInsn >> 26 == 2) { + // j + // extract jump target + pc = (pc >> 28 << 28) | (lastInsn << 6 >> 4); + goto done; } - lastOpc = *(uintptr_t*)pc; - pc += 4; + + lastInsn = *(uintptr_t*)pc; + pc += sizeof(u32); } -end: +done: *spPtr = sp; *pcPtr = pc; *raPtr = ra; } -void Fault_DrawStackTrace(OSThread* t, u32 flags) { - s32 y; - u32 sp; - u32 ra; - u32 pc; - u32 pad; - u32 convertedPc; - - sp = t->context.sp; - ra = t->context.ra; - pc = t->context.pc; +/** + * Draws the stack trace page contents for the specified thread + */ +void Fault_DrawStackTrace(OSThread* thread, u32 flags) { + s32 line; + uintptr_t sp = thread->context.sp; + uintptr_t ra = thread->context.ra; + uintptr_t pc = thread->context.pc; + s32 pad; + uintptr_t addr; Fault_FillScreenBlack(); - FaultDrawer_DrawText(0x78, 0x10, "STACK TRACE"); - FaultDrawer_DrawText(0x24, 0x18, "SP PC (VPC)"); + FaultDrawer_DrawText(120, 16, "STACK TRACE"); + FaultDrawer_DrawText(36, 24, "SP PC (VPC)"); - for (y = 1; (y < 22) && (((ra != 0) || (sp != 0)) && (pc != (u32)__osCleanupThread)); y++) { - FaultDrawer_DrawText(0x24, y * 8 + 24, "%08x %08x", sp, pc); + for (line = 1; (line < 22) && (((ra != 0) || (sp != 0)) && (pc != (uintptr_t)__osCleanupThread)); line++) { + FaultDrawer_DrawText(0x24, line * 8 + 24, "%08x %08x", sp, pc); if (flags & 1) { - convertedPc = (u32)Fault_ConvertAddress((void*)pc); - if (convertedPc != 0) { - FaultDrawer_Printf(" -> %08x", convertedPc); + // Try to convert the relocated program counter to the corresponding unrelocated virtual address + addr = Fault_ConvertAddress(pc); + if (addr != 0) { + FaultDrawer_Printf(" -> %08x", addr); } } else { FaultDrawer_Printf(" -> ????????"); } - Fault_FindNextStackCall(&sp, &pc, &ra); + Fault_WalkStack(&sp, &pc, &ra); } } -void osSyncPrintfStackTrace(OSThread* t, u32 flags) { - s32 y; - u32 sp; - u32 ra; - u32 pc; - u32 convertedPc; - - sp = t->context.sp; - ra = t->context.ra; - pc = t->context.pc; +void Fault_LogStackTrace(OSThread* thread, u32 flags) { + s32 line; + uintptr_t sp = thread->context.sp; + uintptr_t ra = thread->context.ra; + uintptr_t pc = thread->context.pc; + uintptr_t addr; osSyncPrintf("STACK TRACE"); osSyncPrintf("SP PC (VPC)\n"); - for (y = 1; (y < 22) && (((ra != 0) || (sp != 0)) && (pc != (u32)__osCleanupThread)); y++) { + for (line = 1; (line < 22) && (((ra != 0) || (sp != 0)) && (pc != (uintptr_t)__osCleanupThread)); line++) { osSyncPrintf("%08x %08x", sp, pc); if (flags & 1) { - convertedPc = (u32)Fault_ConvertAddress((void*)pc); - if (convertedPc != 0) { - osSyncPrintf(" -> %08x", convertedPc); + // Try to convert the relocated program counter to the corresponding unrelocated virtual address + addr = Fault_ConvertAddress(pc); + if (addr != 0) { + osSyncPrintf(" -> %08x", addr); } } else { osSyncPrintf(" -> ????????"); } osSyncPrintf("\n"); - Fault_FindNextStackCall(&sp, &pc, &ra); + Fault_WalkStack(&sp, &pc, &ra); } } -void Fault_ResumeThread(OSThread* t) { - t->context.cause = 0; - t->context.fpcsr = 0; - t->context.pc += 4; - *(u32*)t->context.pc = 0x0000000D; // write in a break instruction - osWritebackDCache((void*)t->context.pc, 4); - osInvalICache((void*)t->context.pc, 4); - osStartThread(t); +void Fault_ResumeThread(OSThread* thread) { + thread->context.cause = 0; + thread->context.fpcsr = 0; + thread->context.pc += sizeof(u32); + *(u32*)thread->context.pc = 0x0000000D; // write in a break instruction + osWritebackDCache((void*)thread->context.pc, 4); + osInvalICache((void*)thread->context.pc, 4); + osStartThread(thread); } -void Fault_CommitFB(void) { - u16* fb; +void Fault_DisplayFrameBuffer(void) { + void* fb; osViSetYScale(1.0f); osViSetMode(&osViModeNtscLan1); - osViSetSpecialFeatures(0x42); // gama_disable|dither_fliter_enable_aa_mode3_disable + osViSetSpecialFeatures(OS_VI_GAMMA_OFF | OS_VI_DITHER_FILTER_ON); osViBlack(false); - if (sFaultContext->fb) { - fb = sFaultContext->fb; + if (sFaultInstance->fb) { + fb = sFaultInstance->fb; } else { - fb = (u16*)osViGetNextFramebuffer(); - if ((u32)fb == 0x80000000) { - fb = (u16*)((osMemSize | 0x80000000) - 0x25800); + fb = osViGetNextFramebuffer(); + if ((uintptr_t)fb == K0BASE) { + fb = (void*)(PHYS_TO_K0(osMemSize) - SCREEN_HEIGHT * SCREEN_WIDTH * sizeof(u16)); } } osViSwapBuffer(fb); - FaultDrawer_SetDrawerFB(fb, SCREEN_WIDTH, SCREEN_HEIGHT); + FaultDrawer_SetDrawerFrameBuffer(fb, SCREEN_WIDTH, SCREEN_HEIGHT); } +/** + * Runs all registered fault clients. Each fault client displays a page + * on the crash screen. + */ void Fault_ProcessClients(void) { - FaultClient* iter = sFaultContext->clients; + FaultClient* client = sFaultInstance->clients; s32 idx = 0; - while (iter != NULL) { - if (iter->callback) { + while (client != NULL) { + if (client->callback != NULL) { Fault_FillScreenBlack(); FaultDrawer_SetCharPad(-2, 0); - FaultDrawer_Printf("\x1A\x38" - "CallBack (%d) %08x %08x %08x\n" - "\x1A\x37", - idx++, iter, iter->param0, iter->param1); + FaultDrawer_Printf(FAULT_COLOR(DARK_GRAY) "CallBack (%d) %08x %08x %08x\n" FAULT_COLOR(WHITE), idx++, + client, client->arg0, client->arg1); FaultDrawer_SetCharPad(0, 0); - iter->callback(iter->param0, iter->param1); + client->callback(client->arg0, client->arg1); Fault_WaitForInput(); - Fault_CommitFB(); + Fault_DisplayFrameBuffer(); } - iter = iter->next; + client = client->next; } } -#ifdef NON_MATCHING -// needs in-function static bss void Fault_SetOptionsFromController3(void) { static u32 faultCustomOptions; - Input* input3 = &sFaultContext->padInput[3]; + Input* input3 = &sFaultInstance->inputs[3]; u32 pad; - u32 graphPC; - u32 graphRA; - u32 graphSP; + uintptr_t pc; + uintptr_t ra; + uintptr_t sp; + // BTN_RESET is the "neutral reset". Corresponds to holding L+R and pressing S if (CHECK_BTN_ALL(input3->press.button, BTN_RESET)) { faultCustomOptions = !faultCustomOptions; } if (faultCustomOptions) { - graphPC = gGraphThread.context.pc; - graphRA = gGraphThread.context.ra; - graphSP = gGraphThread.context.sp; + pc = gGraphThread.context.pc; + ra = gGraphThread.context.ra; + sp = gGraphThread.context.sp; if (CHECK_BTN_ALL(input3->cur.button, BTN_R)) { static u32 faultCopyToLog; @@ -778,149 +940,189 @@ void Fault_SetOptionsFromController3(void) { FaultDrawer_SetOsSyncPrintfEnabled(faultCopyToLog); } if (CHECK_BTN_ALL(input3->cur.button, BTN_A)) { - osSyncPrintf("GRAPH PC=%08x RA=%08x STACK=%08x\n", graphPC, graphRA, graphSP); + osSyncPrintf("GRAPH PC=%08x RA=%08x STACK=%08x\n", pc, ra, sp); } if (CHECK_BTN_ALL(input3->cur.button, BTN_B)) { - FaultDrawer_SetDrawerFB(osViGetNextFramebuffer(), 0x140, 0xF0); + FaultDrawer_SetDrawerFrameBuffer(osViGetNextFramebuffer(), 0x140, 0xF0); Fault_DrawRec(0, 0xD7, 0x140, 9, 1); FaultDrawer_SetCharPad(-2, 0); - FaultDrawer_DrawText(0x20, 0xD8, "GRAPH PC %08x RA %08x SP %08x", graphPC, graphRA, graphSP); + FaultDrawer_DrawText(0x20, 0xD8, "GRAPH PC %08x RA %08x SP %08x", pc, ra, sp); } } } -#else -#pragma GLOBAL_ASM("asm/non_matchings/boot/fault/Fault_SetOptionsFromController3.s") -#endif -void Fault_SetOptions(void) { +void Fault_UpdatePad(void) { Fault_UpdatePadImpl(); Fault_SetOptionsFromController3(); } +#define FAULT_MSG_CPU_BREAK ((OSMesg)1) +#define FAULT_MSG_FAULT ((OSMesg)2) +#define FAULT_MSG_UNK ((OSMesg)3) + void Fault_ThreadEntry(void* arg) { OSMesg msg; u32 pad; OSThread* faultedThread; - osSetEventMesg(OS_EVENT_CPU_BREAK, &sFaultContext->queue, (OSMesg)1); - osSetEventMesg(OS_EVENT_FAULT, &sFaultContext->queue, (OSMesg)2); - while (1) { - do { - osRecvMesg(&sFaultContext->queue, &msg, OS_MESG_BLOCK); + // Direct OS event messages to the fault event queue + osSetEventMesg(OS_EVENT_CPU_BREAK, &sFaultInstance->queue, FAULT_MSG_CPU_BREAK); + osSetEventMesg(OS_EVENT_FAULT, &sFaultInstance->queue, FAULT_MSG_FAULT); - if (msg == (OSMesg)1) { - sFaultContext->msgId = 1; + while (true) { + do { + // Wait for a thread to hit a fault + osRecvMesg(&sFaultInstance->queue, &msg, OS_MESG_BLOCK); + + if (msg == FAULT_MSG_CPU_BREAK) { + sFaultInstance->msgId = (u32)FAULT_MSG_CPU_BREAK; + // "Fault manager: OS_EVENT_CPU_BREAK received" osSyncPrintf("フォルトマネージャ:OS_EVENT_CPU_BREAKを受信しました\n"); - } else if (msg == (OSMesg)2) { - sFaultContext->msgId = 2; + } else if (msg == FAULT_MSG_FAULT) { + sFaultInstance->msgId = (u32)FAULT_MSG_FAULT; + // "Fault manager: OS_EVENT_FAULT received" osSyncPrintf("フォルトマネージャ:OS_EVENT_FAULTを受信しました\n"); - } else if (msg == (OSMesg)3) { - Fault_SetOptions(); + } else if (msg == FAULT_MSG_UNK) { + Fault_UpdatePad(); faultedThread = NULL; continue; } else { - sFaultContext->msgId = 3; + sFaultInstance->msgId = (u32)FAULT_MSG_UNK; + // "Fault manager: received an unknown message" osSyncPrintf("フォルトマネージャ:不明なメッセージを受信しました\n"); } faultedThread = __osGetCurrFaultedThread(); osSyncPrintf("__osGetCurrFaultedThread()=%08x\n", faultedThread); - if (!faultedThread) { + + if (faultedThread == NULL) { faultedThread = Fault_FindFaultedThread(); osSyncPrintf("FindFaultedThread()=%08x\n", faultedThread); } } while (faultedThread == NULL); - __osSetFpcCsr(__osGetFpcCsr() & 0xFFFFF07F); - sFaultContext->faultedThread = faultedThread; - while (!sFaultContext->faultHandlerEnabled) { + __osSetFpcCsr(__osGetFpcCsr() & ~(FPCSR_EV | FPCSR_EZ | FPCSR_EO | FPCSR_EU | FPCSR_EI)); + sFaultInstance->faultedThread = faultedThread; + + while (!sFaultInstance->faultHandlerEnabled) { Fault_Sleep(1000); } - Fault_Sleep(500); - Fault_CommitFB(); + Fault_Sleep(1000 / 2); - if (sFaultContext->faultActive) { + // Show fault framebuffer + Fault_DisplayFrameBuffer(); + + if (sFaultInstance->autoScroll) { Fault_Wait5Seconds(); } else { - Fault_DrawCornerRec(0xF801); + // Draw error bar signifying the crash screen is available + Fault_DrawCornerRec(GPACK_RGBA5551(255, 0, 0, 1)); Fault_WaitForButtonCombo(); } - sFaultContext->faultActive = 1; - FaultDrawer_SetForeColor(0xFFFF); - FaultDrawer_SetBackColor(0); + // Set auto-scrolling and default colors + sFaultInstance->autoScroll = true; + FaultDrawer_SetForeColor(GPACK_RGBA5551(255, 255, 255, 1)); + FaultDrawer_SetBackColor(GPACK_RGBA5551(0, 0, 0, 0)); + // Draw pages do { + // Thread context page Fault_PrintThreadContext(faultedThread); osSyncPrintfThreadContext(faultedThread); Fault_WaitForInput(); + + // Stack trace page Fault_DrawStackTrace(faultedThread, 0); - osSyncPrintfStackTrace(faultedThread, 0); + Fault_LogStackTrace(faultedThread, 0); Fault_WaitForInput(); + + // Client pages Fault_ProcessClients(); + + // Memory dump page Fault_DrawMemDump((u32)(faultedThread->context.pc - 0x100), (u32)faultedThread->context.sp, 0, 0); Fault_DrawStackTrace(faultedThread, 1); - osSyncPrintfStackTrace(faultedThread, 1); + Fault_LogStackTrace(faultedThread, 1); Fault_WaitForInput(); + + // End page Fault_FillScreenRed(); - FaultDrawer_DrawText(0x40, 0x50, " CONGRATURATIONS! "); - FaultDrawer_DrawText(0x40, 0x5A, "All Pages are displayed."); - FaultDrawer_DrawText(0x40, 0x64, " THANK YOU! "); - FaultDrawer_DrawText(0x40, 0x6E, " You are great debugger!"); + FaultDrawer_DrawText(64, 80, " CONGRATURATIONS! "); + FaultDrawer_DrawText(64, 90, "All Pages are displayed."); + FaultDrawer_DrawText(64, 100, " THANK YOU! "); + FaultDrawer_DrawText(64, 110, " You are great debugger!"); Fault_WaitForInput(); + } while (!sFaultInstance->exit); - } while (!sFaultContext->exitDebugger); - - while (!sFaultContext->exitDebugger) { - ; - } + while (!sFaultInstance->exit) {} Fault_ResumeThread(faultedThread); } } -void Fault_SetFB(void* fb, u16 w, u16 h) { - sFaultContext->fb = fb; - FaultDrawer_SetDrawerFB(fb, w, h); +void Fault_SetFrameBuffer(void* fb, u16 w, u16 h) { + sFaultInstance->fb = fb; + FaultDrawer_SetDrawerFrameBuffer(fb, w, h); } -void Fault_Start(void) { - sFaultContext = &gFaultStruct; - bzero(sFaultContext, sizeof(FaultThreadStruct)); +STACK(sFaultStack, 0x600); +StackEntry sFaultStackInfo; +FaultMgr gFaultMgr; + +void Fault_Init(void) { + sFaultInstance = &gFaultMgr; + bzero(sFaultInstance, sizeof(FaultMgr)); FaultDrawer_Init(); FaultDrawer_SetInputCallback(Fault_WaitForInput); - sFaultContext->exitDebugger = 0; - sFaultContext->msgId = 0; - sFaultContext->faultHandlerEnabled = 0; - sFaultContext->faultedThread = NULL; - sFaultContext->padCallback = &Fault_PadCallback; - sFaultContext->clients = NULL; - sFaultContext->faultActive = 0; - gFaultStruct.faultHandlerEnabled = 1; - osCreateMesgQueue(&sFaultContext->queue, sFaultContext->msg, ARRAY_COUNT(sFaultContext->msg)); + sFaultInstance->exit = false; + sFaultInstance->msgId = 0; + sFaultInstance->faultHandlerEnabled = false; + sFaultInstance->faultedThread = NULL; + sFaultInstance->padCallback = Fault_PadCallback; + sFaultInstance->clients = NULL; + sFaultInstance->autoScroll = false; + gFaultMgr.faultHandlerEnabled = true; + osCreateMesgQueue(&sFaultInstance->queue, sFaultInstance->msg, ARRAY_COUNT(sFaultInstance->msg)); StackCheck_Init(&sFaultStackInfo, sFaultStack, STACK_TOP(sFaultStack), 0, 0x100, "fault"); - osCreateThread(&sFaultContext->thread, 2, Fault_ThreadEntry, NULL, STACK_TOP(sFaultStack), 0x7F); - osStartThread(&sFaultContext->thread); + osCreateThread(&sFaultInstance->thread, Z_THREAD_ID_FAULT, Fault_ThreadEntry, NULL, STACK_TOP(sFaultStack), + Z_PRIORITY_FAULT); + osStartThread(&sFaultInstance->thread); } -void Fault_HangupFaultClient(const char* arg0, char* arg1) { +/** + * Fault page for Hungup crashes. Displays the thread id and two messages + * specified in arguments to `Fault_AddHungupAndCrashImpl`. + */ +void Fault_HangupFaultClient(const char* exp1, const char* exp2) { osSyncPrintf("HungUp on Thread %d\n", osGetThreadId(NULL)); - osSyncPrintf("%s\n", arg0 ? arg0 : "(NULL)"); - osSyncPrintf("%s\n", arg1 ? arg1 : "(NULL)"); + osSyncPrintf("%s\n", exp1 != NULL ? exp1 : "(NULL)"); + osSyncPrintf("%s\n", exp2 != NULL ? exp2 : "(NULL)"); FaultDrawer_Printf("HungUp on Thread %d\n", osGetThreadId(NULL)); - FaultDrawer_Printf("%s\n", arg0 ? arg0 : "(NULL)"); - FaultDrawer_Printf("%s\n", arg1 ? arg1 : "(NULL)"); + FaultDrawer_Printf("%s\n", exp1 != NULL ? exp1 : "(NULL)"); + FaultDrawer_Printf("%s\n", exp2 != NULL ? exp2 : "(NULL)"); } -void Fault_AddHungupAndCrashImpl(const char* arg0, char* arg1) { +/** + * Immediately crashes the current thread, for cases where an irrecoverable + * error occurs. The parameters specify two messages detailing the error, one + * or both may be NULL. + */ +void Fault_AddHungupAndCrashImpl(const char* exp1, const char* exp2) { FaultClient client; - u32 pad; - Fault_AddClient(&client, Fault_HangupFaultClient, (void*)arg0, arg1); - *(u32*)0x11111111 = 0; // trigger an exception + s32 pad; + + Fault_AddClient(&client, (void*)Fault_HangupFaultClient, (void*)exp1, (void*)exp2); + *(u32*)0x11111111 = 0; // trigger an exception via unaligned memory access } -void Fault_AddHungupAndCrash(const char* filename, u32 line) { - char msg[256]; - sprintf(msg, "HungUp %s:%d", filename, line); +/** + * Like `Fault_AddHungupAndCrashImpl`, however provides a fixed message containing + * file and line number + */ +void Fault_AddHungupAndCrash(const char* file, s32 line) { + char msg[0x100]; + + sprintf(msg, "HungUp %s:%d", file, line); Fault_AddHungupAndCrashImpl(msg, NULL); } diff --git a/src/boot_O2_g3/fault_drawer.c b/src/boot_O2_g3/fault_drawer.c index f694be8aad..6031ccaa61 100644 --- a/src/boot_O2_g3/fault_drawer.c +++ b/src/boot_O2_g3/fault_drawer.c @@ -1,28 +1,64 @@ +/** + * @file fault_drawer.c + * + * Implements routines for drawing text with a fixed font directly to a framebuffer, used in displaying + * the crash screen implemented by fault.c + */ + +#include "fault.h" +#include "fault_internal.h" #include "global.h" #include "vt.h" +typedef struct { + /* 0x00 */ u16* frameBuffer; + /* 0x04 */ u16 w; + /* 0x06 */ u16 h; + /* 0x08 */ u16 yStart; + /* 0x0A */ u16 yEnd; + /* 0x0C */ u16 xStart; + /* 0x0E */ u16 xEnd; + /* 0x10 */ u16 foreColor; + /* 0x12 */ u16 backColor; + /* 0x14 */ u16 cursorX; + /* 0x16 */ u16 cursorY; + /* 0x18 */ const u32* fontData; + /* 0x1C */ u8 charW; + /* 0x1D */ u8 charH; + /* 0x1E */ s8 charWPad; + /* 0x1F */ s8 charHPad; + /* 0x20 */ u16 printColors[10]; + /* 0x34 */ u8 escCode; // bool + /* 0x35 */ u8 osSyncPrintfEnabled; + /* 0x38 */ FaultDrawerCallback inputCallback; +} FaultDrawer; // size = 0x3C + extern const u32 sFaultDrawerFont[]; -FaultDrawer sFaultDrawerStruct; +FaultDrawer sFaultDrawer; + +FaultDrawer* sFaultDrawerInstance = &sFaultDrawer; + +#define FAULT_DRAWER_CURSOR_X 22 +#define FAULT_DRAWER_CURSOR_Y 16 -FaultDrawer* sFaultDrawContext = &sFaultDrawerStruct; FaultDrawer sFaultDrawerDefault = { - FAULT_FB_ADDRESS, // fb - SCREEN_WIDTH, // w - SCREEN_HEIGHT, // h - 16, // yStart - 223, // yEnd - 22, // xStart - 297, // xEnd - GPACK_RGBA5551(255, 255, 255, 255), // foreColor - GPACK_RGBA5551(0, 0, 0, 0), // backColor - 22, // cursorX - 16, // cursorY - sFaultDrawerFont, // font - 8, // charW - 8, // charH - 0, // charWPad - 0, // charHPad + FAULT_FB_ADDRESS, // frameBuffer + SCREEN_WIDTH, // w + SCREEN_HEIGHT, // h + FAULT_DRAWER_CURSOR_Y, // yStart + SCREEN_HEIGHT - FAULT_DRAWER_CURSOR_Y - 1, // yEnd + FAULT_DRAWER_CURSOR_X, // xStart + SCREEN_WIDTH - FAULT_DRAWER_CURSOR_X - 1, // xEnd + GPACK_RGBA5551(255, 255, 255, 255), // foreColor + GPACK_RGBA5551(0, 0, 0, 0), // backColor + FAULT_DRAWER_CURSOR_X, // cursorX + FAULT_DRAWER_CURSOR_Y, // cursorY + sFaultDrawerFont, // fontData + 8, // charW + 8, // charH + 0, // charWPad + 0, // charHPad { // printColors GPACK_RGBA5551(0, 0, 0, 1), // BLACK @@ -36,24 +72,24 @@ FaultDrawer sFaultDrawerDefault = { GPACK_RGBA5551(120, 120, 120, 1), // DARK GRAY GPACK_RGBA5551(176, 176, 176, 1), // LIGHT GRAY }, - 0, // escCode - 0, // osSyncPrintfEnabled - NULL, // inputCallback + false, // escCode + false, // osSyncPrintfEnabled + NULL, // inputCallback }; //! TODO: Needs to be extracted #pragma GLOBAL_ASM("asm/non_matchings/boot/fault_drawer/sFaultDrawerFont.s") void FaultDrawer_SetOsSyncPrintfEnabled(u32 enabled) { - sFaultDrawContext->osSyncPrintfEnabled = enabled; + sFaultDrawerInstance->osSyncPrintfEnabled = enabled; } void FaultDrawer_DrawRecImpl(s32 xStart, s32 yStart, s32 xEnd, s32 yEnd, u16 color) { - u16* fb; + u16* frameBuffer; s32 x; s32 y; - s32 xDiff = sFaultDrawContext->w - xStart; - s32 yDiff = sFaultDrawContext->h - yStart; + s32 xDiff = sFaultDrawerInstance->w - xStart; + s32 yDiff = sFaultDrawerInstance->h - yStart; s32 xSize = xEnd - xStart + 1; s32 ySize = yEnd - yStart + 1; @@ -66,12 +102,12 @@ void FaultDrawer_DrawRecImpl(s32 xStart, s32 yStart, s32 xEnd, s32 yEnd, u16 col ySize = yDiff; } - fb = sFaultDrawContext->fb + sFaultDrawContext->w * yStart + xStart; + frameBuffer = sFaultDrawerInstance->frameBuffer + sFaultDrawerInstance->w * yStart + xStart; for (y = 0; y < ySize; y++) { for (x = 0; x < xSize; x++) { - *fb++ = color; + *frameBuffer++ = color; } - fb += sFaultDrawContext->w - xSize; + frameBuffer += sFaultDrawerInstance->w - xSize; } osWritebackDCacheAll(); @@ -82,29 +118,29 @@ void FaultDrawer_DrawChar(char c) { s32 x; s32 y; u32 data; - s32 cursorX = sFaultDrawContext->cursorX; - s32 cursorY = sFaultDrawContext->cursorY; + s32 cursorX = sFaultDrawerInstance->cursorX; + s32 cursorY = sFaultDrawerInstance->cursorY; s32 shift = c % 4; - const u32* dataPtr = &sFaultDrawContext->font[(((c / 8) * 16) + ((c & 4) >> 2))]; - u16* fb = sFaultDrawContext->fb + (sFaultDrawContext->w * cursorY) + cursorX; + const u32* dataPtr = &sFaultDrawerInstance->fontData[(((c / 8) * 16) + ((c & 4) >> 2))]; + u16* frameBuffer = sFaultDrawerInstance->frameBuffer + (sFaultDrawerInstance->w * cursorY) + cursorX; - if ((sFaultDrawContext->xStart <= cursorX) && - ((sFaultDrawContext->charW + cursorX - 1) <= sFaultDrawContext->xEnd) && - (sFaultDrawContext->yStart <= cursorY) && - ((sFaultDrawContext->charH + cursorY - 1) <= sFaultDrawContext->yEnd)) { - for (y = 0; y < sFaultDrawContext->charH; y++) { + if ((sFaultDrawerInstance->xStart <= cursorX) && + ((sFaultDrawerInstance->charW + cursorX - 1) <= sFaultDrawerInstance->xEnd) && + (sFaultDrawerInstance->yStart <= cursorY) && + ((sFaultDrawerInstance->charH + cursorY - 1) <= sFaultDrawerInstance->yEnd)) { + for (y = 0; y < sFaultDrawerInstance->charH; y++) { u32 mask = 0x10000000 << shift; data = *dataPtr; - for (x = 0; x < sFaultDrawContext->charW; x++) { + for (x = 0; x < sFaultDrawerInstance->charW; x++) { if (mask & data) { - fb[x] = sFaultDrawContext->foreColor; - } else if (sFaultDrawContext->backColor & 1) { - fb[x] = sFaultDrawContext->backColor; + frameBuffer[x] = sFaultDrawerInstance->foreColor; + } else if (sFaultDrawerInstance->backColor & 1) { + frameBuffer[x] = sFaultDrawerInstance->backColor; } mask >>= 4; } - fb += sFaultDrawContext->w; + frameBuffer += sFaultDrawerInstance->w; dataPtr += 2; } } @@ -113,37 +149,39 @@ void FaultDrawer_DrawChar(char c) { s32 FaultDrawer_ColorToPrintColor(u16 color) { s32 i; - for (i = 0; i < 10; i++) { - if (color == sFaultDrawContext->printColors[i]) { + for (i = 0; i < ARRAY_COUNT(sFaultDrawerInstance->printColors); i++) { + if (color == sFaultDrawerInstance->printColors[i]) { return i; } } return -1; } -void FaultDrawer_UpdatePrintColor() { - s32 idx; +void FaultDrawer_UpdatePrintColor(void) { + s32 index; - if (sFaultDrawContext->osSyncPrintfEnabled) { + if (sFaultDrawerInstance->osSyncPrintfEnabled) { osSyncPrintf(VT_RST); - idx = FaultDrawer_ColorToPrintColor(sFaultDrawContext->foreColor); - if ((idx >= 0) && (idx < 8)) { - osSyncPrintf(VT_SGR("3%d"), idx); + + index = FaultDrawer_ColorToPrintColor(sFaultDrawerInstance->foreColor); + if ((index >= 0) && (index < 8)) { + osSyncPrintf(VT_SGR("3%d"), index); } - idx = FaultDrawer_ColorToPrintColor(sFaultDrawContext->backColor); - if ((idx >= 0) && (idx < 8)) { - osSyncPrintf(VT_SGR("4%d"), idx); + + index = FaultDrawer_ColorToPrintColor(sFaultDrawerInstance->backColor); + if ((index >= 0) && (index < 8)) { + osSyncPrintf(VT_SGR("4%d"), index); } } } void FaultDrawer_SetForeColor(u16 color) { - sFaultDrawContext->foreColor = color; + sFaultDrawerInstance->foreColor = color; FaultDrawer_UpdatePrintColor(); } void FaultDrawer_SetBackColor(u16 color) { - sFaultDrawContext->backColor = color; + sFaultDrawerInstance->backColor = color; FaultDrawer_UpdatePrintColor(); } @@ -152,70 +190,71 @@ void FaultDrawer_SetFontColor(u16 color) { } void FaultDrawer_SetCharPad(s8 padW, s8 padH) { - sFaultDrawContext->charWPad = padW; - sFaultDrawContext->charHPad = padH; + sFaultDrawerInstance->charWPad = padW; + sFaultDrawerInstance->charHPad = padH; } void FaultDrawer_SetCursor(s32 x, s32 y) { - if (sFaultDrawContext->osSyncPrintfEnabled) { - osSyncPrintf(VT_CUP("%d", "%d"), - (y - sFaultDrawContext->yStart) / (sFaultDrawContext->charH + sFaultDrawContext->charHPad), - (x - sFaultDrawContext->xStart) / (sFaultDrawContext->charW + sFaultDrawContext->charWPad)); + if (sFaultDrawerInstance->osSyncPrintfEnabled) { + osSyncPrintf( + VT_CUP("%d", "%d"), + (y - sFaultDrawerInstance->yStart) / (sFaultDrawerInstance->charH + sFaultDrawerInstance->charHPad), + (x - sFaultDrawerInstance->xStart) / (sFaultDrawerInstance->charW + sFaultDrawerInstance->charWPad)); } - sFaultDrawContext->cursorX = x; - sFaultDrawContext->cursorY = y; + sFaultDrawerInstance->cursorX = x; + sFaultDrawerInstance->cursorY = y; } void FaultDrawer_FillScreen() { - if (sFaultDrawContext->osSyncPrintfEnabled) { + if (sFaultDrawerInstance->osSyncPrintfEnabled) { osSyncPrintf(VT_CLS); } - FaultDrawer_DrawRecImpl(sFaultDrawContext->xStart, sFaultDrawContext->yStart, sFaultDrawContext->xEnd, - sFaultDrawContext->yEnd, sFaultDrawContext->backColor | 1); - FaultDrawer_SetCursor(sFaultDrawContext->xStart, sFaultDrawContext->yStart); + FaultDrawer_DrawRecImpl(sFaultDrawerInstance->xStart, sFaultDrawerInstance->yStart, sFaultDrawerInstance->xEnd, + sFaultDrawerInstance->yEnd, sFaultDrawerInstance->backColor | 1); + FaultDrawer_SetCursor(sFaultDrawerInstance->xStart, sFaultDrawerInstance->yStart); } void* FaultDrawer_FormatStringFunc(void* arg, const char* str, size_t count) { for (; count != 0; count--, str++) { - if (sFaultDrawContext->escCode) { - sFaultDrawContext->escCode = false; - if ((*str >= '1') && (*str <= '9')) { - FaultDrawer_SetForeColor(sFaultDrawContext->printColors[*str - '0']); + if (sFaultDrawerInstance->escCode) { + sFaultDrawerInstance->escCode = false; + if (*str >= '1' && *str <= '9') { + FaultDrawer_SetForeColor(sFaultDrawerInstance->printColors[*str - '0']); } } else { switch (*str) { case '\n': - if (sFaultDrawContext->osSyncPrintfEnabled) { + if (sFaultDrawerInstance->osSyncPrintfEnabled) { osSyncPrintf("\n"); } - sFaultDrawContext->cursorX = sFaultDrawContext->w; + sFaultDrawerInstance->cursorX = sFaultDrawerInstance->w; break; - case '\x1A': - sFaultDrawContext->escCode = true; + case FAULT_ESC: + sFaultDrawerInstance->escCode = true; break; default: - if (sFaultDrawContext->osSyncPrintfEnabled) { + if (sFaultDrawerInstance->osSyncPrintfEnabled) { osSyncPrintf("%c", *str); } FaultDrawer_DrawChar(*str); - sFaultDrawContext->cursorX += sFaultDrawContext->charW + sFaultDrawContext->charWPad; + sFaultDrawerInstance->cursorX += sFaultDrawerInstance->charW + sFaultDrawerInstance->charWPad; } } - if (sFaultDrawContext->cursorX >= (sFaultDrawContext->xEnd - sFaultDrawContext->charW)) { - sFaultDrawContext->cursorX = sFaultDrawContext->xStart; - sFaultDrawContext->cursorY += sFaultDrawContext->charH + sFaultDrawContext->charHPad; - if (sFaultDrawContext->yEnd - sFaultDrawContext->charH <= sFaultDrawContext->cursorY) { - if (sFaultDrawContext->inputCallback != NULL) { - sFaultDrawContext->inputCallback(); + if (sFaultDrawerInstance->cursorX >= (sFaultDrawerInstance->xEnd - sFaultDrawerInstance->charW)) { + sFaultDrawerInstance->cursorX = sFaultDrawerInstance->xStart; + sFaultDrawerInstance->cursorY += sFaultDrawerInstance->charH + sFaultDrawerInstance->charHPad; + if (sFaultDrawerInstance->yEnd - sFaultDrawerInstance->charH <= sFaultDrawerInstance->cursorY) { + if (sFaultDrawerInstance->inputCallback != NULL) { + sFaultDrawerInstance->inputCallback(); FaultDrawer_FillScreen(); } - sFaultDrawContext->cursorY = sFaultDrawContext->yStart; + sFaultDrawerInstance->cursorY = sFaultDrawerInstance->yStart; } } } @@ -227,17 +266,21 @@ void* FaultDrawer_FormatStringFunc(void* arg, const char* str, size_t count) { const char D_80099080[] = "(null)"; -void FaultDrawer_VPrintf(const char* fmt, va_list ap) { - _Printf(FaultDrawer_FormatStringFunc, sFaultDrawContext, fmt, ap); +s32 FaultDrawer_VPrintf(const char* fmt, va_list ap) { + return _Printf(FaultDrawer_FormatStringFunc, sFaultDrawerInstance, fmt, ap); } -void FaultDrawer_Printf(const char* fmt, ...) { +s32 FaultDrawer_Printf(const char* fmt, ...) { + s32 ret; va_list args; + va_start(args, fmt); - FaultDrawer_VPrintf(fmt, args); + ret = FaultDrawer_VPrintf(fmt, args); va_end(args); + + return ret; } void FaultDrawer_DrawText(s32 x, s32 y, const char* fmt, ...) { @@ -250,18 +293,18 @@ void FaultDrawer_DrawText(s32 x, s32 y, const char* fmt, ...) { va_end(args); } -void FaultDrawer_SetDrawerFB(void* fb, u16 w, u16 h) { - sFaultDrawContext->fb = (u16*)fb; - sFaultDrawContext->w = w; - sFaultDrawContext->h = h; +void FaultDrawer_SetDrawerFrameBuffer(void* frameBuffer, u16 w, u16 h) { + sFaultDrawerInstance->frameBuffer = frameBuffer; + sFaultDrawerInstance->w = w; + sFaultDrawerInstance->h = h; } void FaultDrawer_SetInputCallback(FaultDrawerCallback callback) { - sFaultDrawContext->inputCallback = callback; + sFaultDrawerInstance->inputCallback = callback; } void FaultDrawer_Init() { - sFaultDrawContext = &sFaultDrawerStruct; - bcopy(&sFaultDrawerDefault, sFaultDrawContext, sizeof(FaultDrawer)); - sFaultDrawContext->fb = (u16*)((osMemSize | 0x80000000) - 0x25800); + sFaultDrawerInstance = &sFaultDrawer; + bcopy(&sFaultDrawerDefault, sFaultDrawerInstance, sizeof(FaultDrawer)); + sFaultDrawerInstance->frameBuffer = (u16*)(PHYS_TO_K0(osMemSize) - SCREEN_HEIGHT * SCREEN_WIDTH * sizeof(u16)); } diff --git a/src/boot_O2_g3/yaz0.c b/src/boot_O2_g3/yaz0.c index 76f990659c..31166c3b5e 100644 --- a/src/boot_O2_g3/yaz0.c +++ b/src/boot_O2_g3/yaz0.c @@ -1,5 +1,6 @@ #include "prevent_bss_reordering.h" #include "global.h" +#include "fault.h" u8 sYaz0DataBuffer[0x400]; u8* sYaz0CurDataEnd; diff --git a/src/boot_O2_g3/z_std_dma.c b/src/boot_O2_g3/z_std_dma.c index 477fd3f688..ac49922645 100644 --- a/src/boot_O2_g3/z_std_dma.c +++ b/src/boot_O2_g3/z_std_dma.c @@ -1,5 +1,6 @@ #include "prevent_bss_reordering.h" #include "global.h" +#include "fault.h" #include "stack.h" #include "stackcheck.h" #include "z64thread.h" diff --git a/src/code/graph.c b/src/code/graph.c index 6d4665b39e..ce929e6c06 100644 --- a/src/code/graph.c +++ b/src/code/graph.c @@ -1,5 +1,17 @@ -#include "prevent_bss_reordering.h" -#include "global.h" +#include "z64.h" +#include "regs.h" +#include "functions.h" +#include "fault.h" + +// Variables are put before most headers as a hacky way to bypass bss reordering +FaultAddrConvClient sGraphFaultAddrConvClient; +FaultClient sGraphFaultClient; +GfxMasterList* gGfxMasterDL; +CfbInfo sGraphCfbInfos[3]; +OSTime sGraphTaskStartTime; + +#include "variables.h" +#include "macros.h" #include "buffers.h" #include "idle.h" #include "system_malloc.h" @@ -10,12 +22,6 @@ #include "overlays/gamestates/ovl_title/z_title.h" #include "z_title_setup.h" -FaultAddrConvClient sGraphFaultAddrConvClient; -FaultClient sGraphFaultClient; -GfxMasterList* gGfxMasterDL; -CfbInfo sGraphCfbInfos[3]; -OSTime sGraphTaskStartTime; - void Graph_FaultClient(void) { FaultDrawer_DrawText(30, 100, "ShowFrameBuffer PAGE 0/1"); osViSwapBuffer(SysCfb_GetFramebuffer(0)); @@ -93,7 +99,7 @@ GameStateOverlay* Graph_GetNextGameState(GameState* gameState) { return NULL; } -void* Graph_FaultAddrConv(void* address, void* param) { +uintptr_t Graph_FaultAddrConv(uintptr_t address, void* param) { uintptr_t addr = address; GameStateOverlay* gameStateOvl = &gGameStateOverlayTable[0]; size_t ramConv; @@ -112,7 +118,7 @@ void* Graph_FaultAddrConv(void* address, void* param) { } } } - return NULL; + return 0; } void Graph_Init(GraphicsContext* gfxCtx) { @@ -124,7 +130,7 @@ void Graph_Init(GraphicsContext* gfxCtx) { gfxCtx->xScale = gViConfigXScale; gfxCtx->yScale = gViConfigYScale; osCreateMesgQueue(&gfxCtx->queue, gfxCtx->msgBuff, ARRAY_COUNT(gfxCtx->msgBuff)); - Fault_AddClient(&sGraphFaultClient, Graph_FaultClient, NULL, NULL); + Fault_AddClient(&sGraphFaultClient, (void*)Graph_FaultClient, NULL, NULL); Fault_AddAddrConvClient(&sGraphFaultAddrConvClient, Graph_FaultAddrConv, NULL); } @@ -354,7 +360,7 @@ void Graph_ThreadEntry(void* arg) { gGfxSPTaskOutputBufferEndHiRes = (u8*)gGfxSPTaskOutputBufferHiRes + sizeof(*gGfxSPTaskOutputBufferHiRes); SysCfb_Init(); - Fault_SetFB(gWorkBuffer, SCREEN_WIDTH, SCREEN_HEIGHT); + Fault_SetFrameBuffer(gWorkBuffer, SCREEN_WIDTH, SCREEN_HEIGHT); Graph_Init(&gfxCtx); while (nextOvl) { diff --git a/src/code/main.c b/src/code/main.c index f969df70d9..7fbb7ccb8f 100644 --- a/src/code/main.c +++ b/src/code/main.c @@ -1,12 +1,11 @@ #include "audiomgr.h" +#include "fault.h" #include "idle.h" #include "irqmgr.h" #include "padmgr.h" #include "scheduler.h" #include "stack.h" #include "stackcheck.h" -#include "system_heap.h" -#include "z64thread.h" // Variables are put before most headers as a hacky way to bypass bss reordering OSMesgQueue sSerialEventQueue; @@ -32,6 +31,8 @@ PadMgr gPadMgr; #include "main.h" #include "buffers.h" #include "global.h" +#include "system_heap.h" +#include "z64thread.h" s32 gScreenWidth = SCREEN_WIDTH; s32 gScreenHeight = SCREEN_HEIGHT; @@ -47,7 +48,7 @@ void Main(void* arg) { gScreenHeight = SCREEN_HEIGHT; Nmi_Init(); - Fault_Start(); + Fault_Init(); Check_RegionIsSupported(); Check_ExpansionPak(); diff --git a/src/code/padmgr.c b/src/code/padmgr.c index a04a14f579..eb6a811b7c 100644 --- a/src/code/padmgr.c +++ b/src/code/padmgr.c @@ -30,9 +30,11 @@ * `osContStartReadData` to receiving the data. By running this on a separate thread to the game state, work can be * done while waiting for this operation to complete. */ + #include "global.h" #include "io/controller.h" #include "ultra64/motor.h" +#include "fault.h" #define PADMGR_RETRACE_MSG (1 << 0) #define PADMGR_PRE_NMI_MSG (1 << 1) @@ -641,7 +643,7 @@ void PadMgr_HandleRetrace(void) { } // Rumble Pak - if (gFaultStruct.msgId != 0) { + if (gFaultMgr.msgId != 0) { // If fault is active, no rumble PadMgr_RumbleStop(); } else if (sPadMgrInstance->rumbleOffTimer > 0) { diff --git a/src/code/sched.c b/src/code/sched.c index fb7a0073c7..19e6a6bc94 100644 --- a/src/code/sched.c +++ b/src/code/sched.c @@ -1,15 +1,8 @@ -#include "prevent_bss_reordering.h" -#include "global.h" +#include "fault.h" #include "idle.h" -#include "stackcheck.h" -#include "z64thread.h" - -#define RSP_DONE_MSG 667 -#define RDP_DONE_MSG 668 -#define ENTRY_MSG 670 -#define RDP_AUDIO_CANCEL_MSG 671 -#define RSP_GFX_CANCEL_MSG 672 +#include "z64.h" +// Variables are put before most headers as a hacky way to bypass bss reordering FaultClient sSchedFaultClient; OSTime sRSPGFXStartTime; @@ -20,6 +13,17 @@ OSTime sRDPStartTime; u64* gAudioSPDataPtr; u32 gAudioSPDataSize; +#include "functions.h" +#include "variables.h" +#include "stackcheck.h" +#include "z64thread.h" + +#define RSP_DONE_MSG 667 +#define RDP_DONE_MSG 668 +#define ENTRY_MSG 670 +#define RDP_AUDIO_CANCEL_MSG 671 +#define RSP_GFX_CANCEL_MSG 672 + void Sched_SwapFramebuffer(CfbInfo* cfbInfo) { if (cfbInfo->swapBuffer != NULL) { osViSwapBuffer(cfbInfo->swapBuffer); diff --git a/src/code/sys_flashrom.c b/src/code/sys_flashrom.c index f2424b8501..0f4e9f7480 100644 --- a/src/code/sys_flashrom.c +++ b/src/code/sys_flashrom.c @@ -1,5 +1,6 @@ #include "prevent_bss_reordering.h" #include "global.h" +#include "fault.h" #include "stack.h" #include "stackcheck.h" #include "system_malloc.h" diff --git a/src/code/sys_initial_check.c b/src/code/sys_initial_check.c index ec61d07b56..3f46a58183 100644 --- a/src/code/sys_initial_check.c +++ b/src/code/sys_initial_check.c @@ -7,6 +7,7 @@ * are DMA'd directly to fixed RAM addresses. */ #include "global.h" +#include "fault.h" #include "misc/locerrmsg/locerrmsg.h" #include "misc/memerrmsg/memerrmsg.h" @@ -23,7 +24,7 @@ // Address with enough room after to load either of the error message image files before the fault screen buffer at the // end of RDRAM -#define CHECK_ERRMSG_STATIC_SEGMENT (u8*)(FAULT_FB_ADDRESS - MAX(SIZEOF_LOCERRMSG, SIZEOF_MEMERRMSG)) +#define CHECK_ERRMSG_STATIC_SEGMENT (u8*)((uintptr_t)FAULT_FB_ADDRESS - MAX(SIZEOF_LOCERRMSG, SIZEOF_MEMERRMSG)) void Check_WriteRGBA16Pixel(u16* buffer, u32 x, u32 y, u32 value) { if (value & RGBA16_PIXEL_OPAQUE) { diff --git a/src/code/sys_matrix.c b/src/code/sys_matrix.c index de8da3defb..6269add5fa 100644 --- a/src/code/sys_matrix.c +++ b/src/code/sys_matrix.c @@ -39,6 +39,7 @@ * (APPLY), or to just overwrite it (NEW). */ +#include "prevent_bss_reordering.h" #include "global.h" /* data */ diff --git a/src/code/z_actor.c b/src/code/z_actor.c index 094b36787b..35dbc8361d 100644 --- a/src/code/z_actor.c +++ b/src/code/z_actor.c @@ -4,6 +4,7 @@ */ #include "global.h" +#include "fault.h" #include "loadfragment.h" #include "z64horse.h" #include "z64quake.h" diff --git a/src/code/z_actor_dlftbls.c b/src/code/z_actor_dlftbls.c index 2e2f80a50d..012c118fbd 100644 --- a/src/code/z_actor_dlftbls.c +++ b/src/code/z_actor_dlftbls.c @@ -1,4 +1,5 @@ #include "global.h" +#include "fault.h" // Init Vars declarations (also used in the table below) #define DEFINE_ACTOR(name, _enumValue, _allocType, _debugName) extern ActorInit name##_InitVars; @@ -60,7 +61,7 @@ void ActorOverlayTable_FaultClient(void* arg0, void* arg1) { } } -void* ActorOverlayTable_FaultAddrConv(void* address, void* param) { +uintptr_t ActorOverlayTable_FaultAddrConv(uintptr_t address, void* param) { uintptr_t addr = address; ActorOverlay* actorOvl = &gActorOverlayTable[0]; size_t ramConv; @@ -79,7 +80,8 @@ void* ActorOverlayTable_FaultAddrConv(void* address, void* param) { } } } - return NULL; + + return 0; } void ActorOverlayTable_Init(void) { diff --git a/src/code/z_bgcheck.c b/src/code/z_bgcheck.c index f63ff6b019..ab2eca48a9 100644 --- a/src/code/z_bgcheck.c +++ b/src/code/z_bgcheck.c @@ -1,4 +1,5 @@ #include "global.h" +#include "fault.h" #include "fixed_point.h" #include "vt.h" #include "overlays/kaleido_scope/ovl_kaleido_scope/z_kaleido_scope.h" @@ -80,9 +81,15 @@ BgSpecialSceneMaxObjects sCustomDynapolyMem[] = { // TODO: All these bss variables are localized to one function and can // likely be made into in-function static bss variables in the future +char D_801ED950[80]; +char D_801ED9A0[80]; + Vec3f D_801ED9F0[3]; // polyVerts Vec3f D_801EDA18[3]; // polyVerts +MtxF sModelToWorldMtxF; Vec3f D_801EDA80[3]; // polyVerts +char D_801EDAA8[80]; +char D_801EDAF8[80]; Vec3f D_801EDB48[3]; // polyVerts #ifndef NON_MATCHING @@ -92,13 +99,6 @@ Sphere16 D_801EDBA8; // sphere; TriNorm D_801EDBB0; // tri; #endif -char D_801ED950[80]; -char D_801ED9A0[80]; - -char D_801EDAA8[80]; -char D_801EDAF8[80]; -MtxF sModelToWorldMtxF; - void BgCheck_GetStaticLookupIndicesFromPos(CollisionContext* colCtx, Vec3f* pos, Vec3i* sector); f32 BgCheck_RaycastFloorDyna(DynaRaycast* dynaRaycast); s32 BgCheck_SphVsDynaWall(CollisionContext* colCtx, u16 xpFlags, f32* outX, f32* outZ, Vec3f* pos, f32 radius, diff --git a/src/code/z_collision_check.c b/src/code/z_collision_check.c index 39b57cbd03..af1f4ad6c9 100644 --- a/src/code/z_collision_check.c +++ b/src/code/z_collision_check.c @@ -1,3 +1,4 @@ +#include "prevent_bss_reordering.h" #include "global.h" #include "z64collision_check.h" diff --git a/src/code/z_demo.c b/src/code/z_demo.c index 262bc0ebcb..771e767225 100644 --- a/src/code/z_demo.c +++ b/src/code/z_demo.c @@ -1,3 +1,4 @@ +#include "prevent_bss_reordering.h" #include "global.h" #include "z64quake.h" #include "z64rumble.h" diff --git a/src/code/z_kaleido_manager.c b/src/code/z_kaleido_manager.c index a84e74776c..e09492d4ef 100644 --- a/src/code/z_kaleido_manager.c +++ b/src/code/z_kaleido_manager.c @@ -1,4 +1,5 @@ #include "global.h" +#include "fault.h" #include "loadfragment.h" #define KALEIDO_OVERLAY(name) \ @@ -16,7 +17,7 @@ void* sKaleidoAreaPtr = NULL; KaleidoMgrOverlay* gKaleidoMgrCurOvl = NULL; FaultAddrConvClient sKaleidoMgrFaultAddrConvClient; -void* KaleidoManager_FaultAddrConv(void* address, void* param) { +uintptr_t KaleidoManager_FaultAddrConv(uintptr_t address, void* param) { uintptr_t addr = address; KaleidoMgrOverlay* kaleidoMgrOvl = gKaleidoMgrCurOvl; size_t ramConv; @@ -34,7 +35,7 @@ void* KaleidoManager_FaultAddrConv(void* address, void* param) { } } } - return NULL; + return 0; } void KaleidoManager_LoadOvl(KaleidoMgrOverlay* ovl) { diff --git a/src/code/z_play.c b/src/code/z_play.c index a97cf9f8ed..2965e0e2e3 100644 --- a/src/code/z_play.c +++ b/src/code/z_play.c @@ -1,3 +1,4 @@ +#include "prevent_bss_reordering.h" #include "global.h" #include "buffers.h" #include "idle.h" diff --git a/src/code/z_player_lib.c b/src/code/z_player_lib.c index d879051177..bd8a5b94ea 100644 --- a/src/code/z_player_lib.c +++ b/src/code/z_player_lib.c @@ -3,7 +3,6 @@ * Description: Set of library functions to interact with the Player system */ -#include "prevent_bss_reordering.h" #include "global.h" #include "objects/gameplay_keep/gameplay_keep.h" diff --git a/src/libultra/os/createthread.c b/src/libultra/os/createthread.c index 0e61b05838..e67743eb31 100644 --- a/src/libultra/os/createthread.c +++ b/src/libultra/os/createthread.c @@ -1,28 +1,28 @@ #include "global.h" -void osCreateThread(OSThread* t, OSId id, void* entry, void* arg, void* sp, OSPri p) { +void osCreateThread(OSThread* thread, OSId id, void* entry, void* arg, void* sp, OSPri p) { register u32 saveMask; OSIntMask mask; - t->id = id; - t->priority = p; - t->next = NULL; - t->queue = NULL; - t->context.pc = (u32)entry; - t->context.a0 = arg; - t->context.sp = (u64)(s32)sp - 16; - t->context.ra = __osCleanupThread; + thread->id = id; + thread->priority = p; + thread->next = NULL; + thread->queue = NULL; + thread->context.pc = (u32)entry; + thread->context.a0 = arg; + thread->context.sp = (u64)(s32)sp - 16; + thread->context.ra = __osCleanupThread; mask = 0x3FFF01; - t->context.sr = 0xFF03; - t->context.rcp = (mask & 0x3F0000) >> 16; - t->context.fpcsr = 0x01000800; - t->fp = 0; - t->state = 1; - t->flags = 0; + thread->context.sr = 0xFF03; + thread->context.rcp = (mask & 0x3F0000) >> 16; + thread->context.fpcsr = 0x01000800; + thread->fp = 0; + thread->state = 1; + thread->flags = 0; saveMask = __osDisableInt(); - t->tlnext = __osActiveQueue; - __osActiveQueue = t; + thread->tlnext = __osActiveQueue; + __osActiveQueue = thread; __osRestoreInt(saveMask); } diff --git a/src/libultra/os/setthreadpri.c b/src/libultra/os/setthreadpri.c index fde6e78dd5..2da5bf2660 100644 --- a/src/libultra/os/setthreadpri.c +++ b/src/libultra/os/setthreadpri.c @@ -1,20 +1,20 @@ #include "global.h" -void osSetThreadPri(OSThread* t, OSPri p) { +void osSetThreadPri(OSThread* thread, OSPri p) { register u32 saveMask; saveMask = __osDisableInt(); - if (t == NULL) { - t = __osRunningThread; + if (thread == NULL) { + thread = __osRunningThread; } - if (t->priority != p) { - t->priority = p; + if (thread->priority != p) { + thread->priority = p; - if (t != __osRunningThread && t->state != 1) { - __osDequeueThread(t->queue, t); - __osEnqueueThread(t->queue, t); + if (thread != __osRunningThread && thread->state != 1) { + __osDequeueThread(thread->queue, thread); + __osEnqueueThread(thread->queue, thread); } if (__osRunningThread->priority < __osRunQueue->priority) { diff --git a/src/overlays/actors/ovl_Boss_02/z_boss_02.c b/src/overlays/actors/ovl_Boss_02/z_boss_02.c index bf40d44802..393c882328 100644 --- a/src/overlays/actors/ovl_Boss_02/z_boss_02.c +++ b/src/overlays/actors/ovl_Boss_02/z_boss_02.c @@ -4,6 +4,7 @@ * Description: Twinmold */ +#include "prevent_bss_reordering.h" #include "z_boss_02.h" #include "z64rumble.h" #include "z64shrink_window.h" diff --git a/src/overlays/actors/ovl_En_Ot/z_en_ot.c b/src/overlays/actors/ovl_En_Ot/z_en_ot.c index 137829cf50..8dd771bd81 100644 --- a/src/overlays/actors/ovl_En_Ot/z_en_ot.c +++ b/src/overlays/actors/ovl_En_Ot/z_en_ot.c @@ -4,7 +4,6 @@ * Description: Seahorse */ -#include "prevent_bss_reordering.h" #include "z_en_ot.h" #include "objects/object_ot/object_ot.h" #include "objects/gameplay_keep/gameplay_keep.h" diff --git a/src/overlays/actors/ovl_Obj_Hunsui/z_obj_hunsui.c b/src/overlays/actors/ovl_Obj_Hunsui/z_obj_hunsui.c index 2354f9f623..f440338619 100644 --- a/src/overlays/actors/ovl_Obj_Hunsui/z_obj_hunsui.c +++ b/src/overlays/actors/ovl_Obj_Hunsui/z_obj_hunsui.c @@ -3,6 +3,7 @@ * Overlay: ovl_Obj_Hunsui * Description: Switch-Activated Geyser */ + #include "z_obj_hunsui.h" #include "objects/object_hunsui/object_hunsui.h" diff --git a/src/overlays/actors/ovl_player_actor/z_player.c b/src/overlays/actors/ovl_player_actor/z_player.c index 09f870713e..d210bc91d1 100644 --- a/src/overlays/actors/ovl_player_actor/z_player.c +++ b/src/overlays/actors/ovl_player_actor/z_player.c @@ -3,7 +3,7 @@ * Overlay: ovl_player_actor * Description: Player */ -#include "prevent_bss_reordering.h" + #include "global.h" #include "z64horse.h" #include "z64quake.h" 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 c7f7379fe1..374535c3a9 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 @@ -3,6 +3,8 @@ * Overlay: ovl_kaleido_scope * Description: Pause Menu */ + +#include "prevent_bss_reordering.h" #include "z_kaleido_scope.h" #include "z64view.h" #include "overlays/gamestates/ovl_opening/z_opening.h" diff --git a/tools/disasm/functions.txt b/tools/disasm/functions.txt index b56253e417..557c278c7e 100644 --- a/tools/disasm/functions.txt +++ b/tools/disasm/functions.txt @@ -65,27 +65,27 @@ 0x8008212C:("Fault_FillScreenRed",), 0x80082180:("Fault_DrawCornerRec",), 0x800821D4:("Fault_PrintFReg",), - 0x80082280:("osSyncPrintfFReg",), + 0x80082280:("Fault_LogFReg",), 0x80082330:("Fault_PrintFPCR",), - 0x800823D4:("osSyncPrintfFPCR",), + 0x800823D4:("Fault_LogFPCSR",), 0x8008246C:("Fault_PrintThreadContext",), 0x800827BC:("osSyncPrintfThreadContext",), 0x80082AB8:("Fault_FindFaultedThread",), 0x80082B40:("Fault_Wait5Seconds",), 0x80082BD0:("Fault_WaitForButtonCombo",), - 0x80082C6C:("Fault_DrawMemDumpPage",), + 0x80082C6C:("Fault_DrawMemDumpContents",), 0x80082DD8:("Fault_DrawMemDump",), - 0x80082FD0:("Fault_FindNextStackCall",), + 0x80082FD0:("Fault_WalkStack",), 0x80083144:("Fault_DrawStackTrace",), - 0x800832D4:("osSyncPrintfStackTrace",), + 0x800832D4:("Fault_LogStackTrace",), 0x80083450:("Fault_ResumeThread",), - 0x800834CC:("Fault_CommitFB",), + 0x800834CC:("Fault_DisplayFrameBuffer",), 0x8008358C:("Fault_ProcessClients",), 0x80083670:("Fault_SetOptionsFromController3",), - 0x800837E4:("Fault_SetOptions",), + 0x800837E4:("Fault_UpdatePad",), 0x80083828:("Fault_ThreadEntry",), - 0x80083B70:("Fault_SetFB",), - 0x80083BC4:("Fault_Start",), + 0x80083B70:("Fault_SetFrameBuffer",), + 0x80083BC4:("Fault_Init",), 0x80083CF8:("Fault_HangupFaultClient",), 0x80083DF4:("Fault_AddHungupAndCrashImpl",), 0x80083E4C:("Fault_AddHungupAndCrash",), @@ -104,7 +104,7 @@ 0x8008477C:("FaultDrawer_VPrintf",), 0x800847CC:("FaultDrawer_Printf",), 0x8008481C:("FaultDrawer_DrawText",), - 0x8008486C:("FaultDrawer_SetDrawerFB",), + 0x8008486C:("FaultDrawer_SetDrawerFrameBuffer",), 0x800848A4:("FaultDrawer_SetInputCallback",), 0x800848B8:("FaultDrawer_Init",), 0x80084940:("func_80084940",), diff --git a/tools/disasm/variables.txt b/tools/disasm/variables.txt index 04d8933100..9b61c54c7a 100644 --- a/tools/disasm/variables.txt +++ b/tools/disasm/variables.txt @@ -25,7 +25,7 @@ 0x80096B78:("sIrqMgrRetraceCount","s32","",0x4), 0x80096B80:("sCpuExceptions","char*","[18]",0x48), 0x80096BC8:("sFpuExceptions","char*","[6]",0x18), - 0x80096BE0:("sFaultDrawContext","FaultDrawer*","",0x4), + 0x80096BE0:("sFaultDrawerInstance","FaultDrawer*","",0x4), 0x80096BE4:("sFaultDrawerDefault","FaultDrawer","",0x3c), 0x80096C20:("gLoadLogSeverity","UNK_TYPE4","",0x4), 0x80096C30:("gOverlayLogSeverity","UNK_TYPE4","",0x4), @@ -313,14 +313,14 @@ 0x8009BE30:("D_8009BE30","UNK_TYPE4","",0x4), 0x8009BE34:("D_8009BE34","UNK_TYPE4","",0x4), 0x8009BE38:("romInfoFaultClient","FaultClient","",0x10), - 0x8009BE50:("sFaultContext","FaultThreadStruct*","",0x4), - 0x8009BE54:("D_8009BE54","f32","",0x4), + 0x8009BE50:("sFaultInstance","FaultMgr*","",0x4), + 0x8009BE54:("sFaultTimeTotal","f32","",0x4), 0x8009BE58:("faultCustomOptions","u32","",0x4), 0x8009BE5C:("faultCopyToLog","u32","",0x4), 0x8009BE60:("sFaultStack","u8","[1536]",0x600), - 0x8009C460:("sFaultStackInfo","StackEntry","",0x1c), - 0x8009C480:("gFaultStruct","FaultThreadStruct","",0x848), - 0x8009CCD0:("sFaultDrawerStruct","FaultDrawer","",0x3c), + 0x8009C460:("sFaultStackInfo","StackEntry","",0x1C), + 0x8009C480:("gFaultMgr","FaultMgr","",0x848), + 0x8009CCD0:("sFaultDrawer","FaultDrawer","",0x3c), 0x8009CD10:("D_8009CD10","UNK_TYPE4","",0x4), 0x8009CD20:("gSystemArena","Arena","",0x24), 0x8009CD50:("sRandFloat","f32","",0x4), diff --git a/tools/namefixer.py b/tools/namefixer.py index 514d68d3c9..0a809f8042 100755 --- a/tools/namefixer.py +++ b/tools/namefixer.py @@ -1128,6 +1128,8 @@ wordReplace = { "D_801D1E20": "gIdentityMtxF", "sSetupDL": "gSetupDLs", + "gFaultStruct": "gFaultMgr", + "D_801BDA9C": "gHorseIsMounted", "D_801BDAA4": "gHorsePlayedEponasSong", diff --git a/tools/sizes/boot_functions.csv b/tools/sizes/boot_functions.csv index c94e9cc723..1c05ba562b 100644 --- a/tools/sizes/boot_functions.csv +++ b/tools/sizes/boot_functions.csv @@ -62,27 +62,27 @@ asm/non_matchings/boot/fault/Fault_FillScreenBlack.s,Fault_FillScreenBlack,0x800 asm/non_matchings/boot/fault/Fault_FillScreenRed.s,Fault_FillScreenRed,0x8008212C,0x15 asm/non_matchings/boot/fault/Fault_DrawCornerRec.s,Fault_DrawCornerRec,0x80082180,0x15 asm/non_matchings/boot/fault/Fault_PrintFReg.s,Fault_PrintFReg,0x800821D4,0x2B -asm/non_matchings/boot/fault/osSyncPrintfFReg.s,osSyncPrintfFReg,0x80082280,0x2C +asm/non_matchings/boot/fault/Fault_LogFReg.s,Fault_LogFReg,0x80082280,0x2C asm/non_matchings/boot/fault/Fault_PrintFPCR.s,Fault_PrintFPCR,0x80082330,0x29 -asm/non_matchings/boot/fault/osSyncPrintfFPCR.s,osSyncPrintfFPCR,0x800823D4,0x26 +asm/non_matchings/boot/fault/Fault_LogFPCSR.s,Fault_LogFPCSR,0x800823D4,0x26 asm/non_matchings/boot/fault/Fault_PrintThreadContext.s,Fault_PrintThreadContext,0x8008246C,0xD4 asm/non_matchings/boot/fault/osSyncPrintfThreadContext.s,osSyncPrintfThreadContext,0x800827BC,0xBF asm/non_matchings/boot/fault/Fault_FindFaultedThread.s,Fault_FindFaultedThread,0x80082AB8,0x22 asm/non_matchings/boot/fault/Fault_Wait5Seconds.s,Fault_Wait5Seconds,0x80082B40,0x24 asm/non_matchings/boot/fault/Fault_WaitForButtonCombo.s,Fault_WaitForButtonCombo,0x80082BD0,0x27 -asm/non_matchings/boot/fault/Fault_DrawMemDumpPage.s,Fault_DrawMemDumpPage,0x80082C6C,0x5B +asm/non_matchings/boot/fault/Fault_DrawMemDumpContents.s,Fault_DrawMemDumpContents,0x80082C6C,0x5B asm/non_matchings/boot/fault/Fault_DrawMemDump.s,Fault_DrawMemDump,0x80082DD8,0x7E -asm/non_matchings/boot/fault/Fault_FindNextStackCall.s,Fault_FindNextStackCall,0x80082FD0,0x5D +asm/non_matchings/boot/fault/Fault_WalkStack.s,Fault_WalkStack,0x80082FD0,0x5D asm/non_matchings/boot/fault/Fault_DrawStackTrace.s,Fault_DrawStackTrace,0x80083144,0x64 -asm/non_matchings/boot/fault/osSyncPrintfStackTrace.s,osSyncPrintfStackTrace,0x800832D4,0x5F +asm/non_matchings/boot/fault/Fault_LogStackTrace.s,Fault_LogStackTrace,0x800832D4,0x5F asm/non_matchings/boot/fault/Fault_ResumeThread.s,Fault_ResumeThread,0x80083450,0x1F -asm/non_matchings/boot/fault/Fault_CommitFB.s,Fault_CommitFB,0x800834CC,0x30 +asm/non_matchings/boot/fault/Fault_DisplayFrameBuffer.s,Fault_DisplayFrameBuffer,0x800834CC,0x30 asm/non_matchings/boot/fault/Fault_ProcessClients.s,Fault_ProcessClients,0x8008358C,0x39 asm/non_matchings/boot/fault/Fault_SetOptionsFromController3.s,Fault_SetOptionsFromController3,0x80083670,0x5D -asm/non_matchings/boot/fault/Fault_SetOptions.s,Fault_SetOptions,0x800837E4,0x11 +asm/non_matchings/boot/fault/Fault_UpdatePad.s,Fault_UpdatePad,0x800837E4,0x11 asm/non_matchings/boot/fault/Fault_ThreadEntry.s,Fault_ThreadEntry,0x80083828,0xD2 -asm/non_matchings/boot/fault/Fault_SetFB.s,Fault_SetFB,0x80083B70,0x15 -asm/non_matchings/boot/fault/Fault_Start.s,Fault_Start,0x80083BC4,0x4D +asm/non_matchings/boot/fault/Fault_SetFrameBuffer.s,Fault_SetFrameBuffer,0x80083B70,0x15 +asm/non_matchings/boot/fault/Fault_Init.s,Fault_Init,0x80083BC4,0x4D asm/non_matchings/boot/fault/Fault_HangupFaultClient.s,Fault_HangupFaultClient,0x80083CF8,0x3F asm/non_matchings/boot/fault/Fault_AddHungupAndCrashImpl.s,Fault_AddHungupAndCrashImpl,0x80083DF4,0x16 asm/non_matchings/boot/fault/Fault_AddHungupAndCrash.s,Fault_AddHungupAndCrash,0x80083E4C,0x19 @@ -101,7 +101,7 @@ asm/non_matchings/boot/fault_drawer/FaultDrawer_FormatStringFunc.s,FaultDrawer_F asm/non_matchings/boot/fault_drawer/FaultDrawer_VPrintf.s,FaultDrawer_VPrintf,0x8008477C,0x14 asm/non_matchings/boot/fault_drawer/FaultDrawer_Printf.s,FaultDrawer_Printf,0x800847CC,0x14 asm/non_matchings/boot/fault_drawer/FaultDrawer_DrawText.s,FaultDrawer_DrawText,0x8008481C,0x14 -asm/non_matchings/boot/fault_drawer/FaultDrawer_SetDrawerFB.s,FaultDrawer_SetDrawerFB,0x8008486C,0xE +asm/non_matchings/boot/fault_drawer/FaultDrawer_SetDrawerFrameBuffer.s,FaultDrawer_SetDrawerFrameBuffer,0x8008486C,0xE asm/non_matchings/boot/fault_drawer/FaultDrawer_SetInputCallback.s,FaultDrawer_SetInputCallback,0x800848A4,0x5 asm/non_matchings/boot/fault_drawer/FaultDrawer_Init.s,FaultDrawer_Init,0x800848B8,0x22 asm/non_matchings/boot/boot_80084940/func_80084940.s,func_80084940,0x80084940,0xA