[ntsc-1.2] Decompile fault.c (#2046)

* wip decomp ntsc-1.2 fault.c

* more ntsc-1.2 fault.c

* decompiled fault_v1.c

* name fault_v1 functions, wip

* try to clean up build system (two versions of the fault system coexisting)

* cleanup

* cleanup2

* fix build gc-eu-mq-dbg

* match 2 more, ty anon

* matched

* review

* more review

* fixup spec

* `(uintptr_t)ptr op int` instead of `ptr op (type*)int`

* move fault.h out of global headers, properly include fault.h and versions.h

* compile all fault_*.c files regardless of version by overriding FAULT_VERSION

* n64 FaultMgr.framebuffer s32 -> u16*

* FaultMgr.framebuffer -> FaultMgr.fb

* make gc FaultMgr.fb u16* (yes, thats everything)

* bss

* fix Fault_Printf return type

* noop FaultDrawer_SetFontColor, FaultDrawer_SetCharPad in fault_n64

* fault_color_ stuff is only for fault_gc

* rm empty line in makefile

* I guess `D_80105A90_unknown` is `[sg]TotalAllocFailures`

* bss

* `Fault_WaitInput` -> `Fault_WaitForInput`

* use named fault funcs in pointers array

* FAULT_OOT{N64,GC} -> FAULT_{N64,GC}

* Apply suggestions from code review

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>
Co-authored-by: Anghelo Carvajal <angheloalf95@gmail.com>

* review, sync fault_{gc,n64}

* Apply `FPCSR_CE` suggestions from code review

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

---------

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>
Co-authored-by: Anghelo Carvajal <angheloalf95@gmail.com>
This commit is contained in:
Dragorn421
2024-08-28 09:38:42 +02:00
committed by GitHub
parent aa97586659
commit 68818044db
34 changed files with 1031 additions and 80 deletions

View File

@@ -439,10 +439,12 @@ $(BUILD_DIR)/src/code/relocation.o: OPTFLAGS := -O2
$(BUILD_DIR)/src/code/sleep.o: OPTFLAGS := -O2
$(BUILD_DIR)/src/code/system_malloc.o: OPTFLAGS := -O2
$(BUILD_DIR)/src/code/fault.o: CFLAGS += -trapuv
$(BUILD_DIR)/src/code/fault.o: OPTFLAGS := -O2 -g3
$(BUILD_DIR)/src/code/fault_drawer.o: CFLAGS += -trapuv
$(BUILD_DIR)/src/code/fault_drawer.o: OPTFLAGS := -O2 -g3
$(BUILD_DIR)/src/code/fault_n64.o: CFLAGS += -trapuv
$(BUILD_DIR)/src/code/fault_gc.o: CFLAGS += -trapuv
$(BUILD_DIR)/src/code/fault_gc.o: OPTFLAGS := -O2 -g3
$(BUILD_DIR)/src/code/fault_gc_drawer.o: CFLAGS += -trapuv
$(BUILD_DIR)/src/code/fault_gc_drawer.o: OPTFLAGS := -O2 -g3
$(BUILD_DIR)/src/code/ucode_disas.o: OPTFLAGS := -O2 -g3
ifeq ($(PLATFORM),N64)

View File

@@ -4,7 +4,9 @@
#include "ultra64.h"
#include "attributes.h"
#include "padmgr.h"
#include "versions.h"
#if FAULT_VERSION == FAULT_GC
// These are the same as the 3-bit ansi color codes
#define FAULT_COLOR_BLACK 0
#define FAULT_COLOR_RED 1
@@ -23,6 +25,7 @@
#define FAULT_ESC '\x1A'
#define FAULT_COLOR(n) "\x1A" FAULT_COLOR_EXPAND_AND_STRINGIFY(FAULT_COLOR_ ## n)
#endif
typedef struct FaultClient {
/* 0x00 */ struct FaultClient* next;
@@ -31,11 +34,13 @@ typedef struct FaultClient {
/* 0x0C */ void* arg1;
} FaultClient; // size = 0x10
#if FAULT_VERSION == FAULT_GC
typedef struct FaultAddrConvClient {
/* 0x00 */ struct FaultAddrConvClient* next;
/* 0x04 */ void* callback;
/* 0x08 */ void* arg;
} FaultAddrConvClient; // size = 0xC
#endif
// Initialization
@@ -51,15 +56,32 @@ NORETURN void Fault_AddHungupAndCrash(const char* file, int line);
void Fault_AddClient(FaultClient* client, void* callback, void* arg0, void* arg1);
void Fault_RemoveClient(FaultClient* client);
#if FAULT_VERSION == FAULT_GC
void Fault_AddAddrConvClient(FaultAddrConvClient* client, void* callback, void* arg);
void Fault_RemoveAddrConvClient(FaultAddrConvClient* client);
#endif
// For use in Fault Client callbacks
void Fault_WaitForInput(void);
void Fault_FillScreenBlack(void);
void Fault_SetFrameBuffer(void* fb, u16 w, u16 h);
void Fault_WaitForInput(void);
#if FAULT_VERSION == FAULT_N64
// Not implemented. Silently noop-ing is fine, these are not essential for functionality.
#define FaultDrawer_SetFontColor(color) (void)0
#define FaultDrawer_SetCharPad(padW, padH) (void)0
void Fault_SetCursor(s32 x, s32 y);
s32 Fault_Printf(const char* fmt, ...);
void Fault_DrawText(s32 x, s32 y, const char* fmt, ...);
#define FaultDrawer_SetCursor Fault_SetCursor
#define FaultDrawer_Printf Fault_Printf
#define FaultDrawer_DrawText Fault_DrawText
#elif FAULT_VERSION == FAULT_GC
void FaultDrawer_SetForeColor(u16 color);
void FaultDrawer_SetBackColor(u16 color);
void FaultDrawer_SetFontColor(u16 color);
@@ -69,9 +91,19 @@ s32 FaultDrawer_VPrintf(const char* fmt, va_list args);
s32 FaultDrawer_Printf(const char* fmt, ...);
void FaultDrawer_DrawText(s32 x, s32 y, const char* fmt, ...);
#endif
#if FAULT_VERSION == FAULT_N64
extern vs32 gFaultMsgId;
#define FAULT_MSG_ID gFaultMsgId
#elif FAULT_VERSION == FAULT_GC
typedef struct FaultMgr {
/* 0x000 */ OSThread thread;
/* 0x1B0 */ char unk_1B0[0x600]; // probably an unused internal thread stack for `Fault_ClientRunTask`/`clientThreadSp`
/* 0x1B0 */ char unk_1B0[0x600];
/* 0x7B0 */ OSMesgQueue queue;
/* 0x7C8 */ OSMesg msg;
/* 0x7CC */ u8 exit;
@@ -84,10 +116,14 @@ typedef struct FaultMgr {
/* 0x7DC */ FaultAddrConvClient* addrConvClients;
/* 0x7E0 */ char unk_7E0[0x4];
/* 0x7E4 */ Input inputs[MAXCONTROLLERS];
/* 0x844 */ void* fb;
/* 0x844 */ u16* fb;
/* 0x848 */ void* clientThreadSp;
} FaultMgr; // size = 0x850
extern FaultMgr gFaultMgr;
#define FAULT_MSG_ID gFaultMgr.msgId
#endif
#endif

View File

@@ -3,6 +3,7 @@
#include "ultra64.h"
#include "irqmgr.h"
#include "versions.h"
typedef enum ControllerPakType {
CONT_PAK_NONE,
@@ -50,9 +51,9 @@ void PadMgr_Init(PadMgr* padMgr, OSMesgQueue* serialEventQueue, IrqMgr* irqMgr,
// Fetching inputs
// This function cannot be prototyped here without AVOID_UB because it is called incorrectly in fault.c (see bug in
// `Fault_PadCallback`)
#ifdef AVOID_UB
// This function cannot be prototyped here in all configurations because it is called incorrectly in fault_gc.c
// (see bug in `Fault_PadCallback`)
#if FAULT_VERSION == FAULT_N64 || defined(AVOID_UB)
void PadMgr_RequestPadData(PadMgr* padmgr, Input* inputs, s32 gameRequest);
#endif

View File

@@ -248,4 +248,6 @@ extern u64 gGfxSPTaskStack[SP_DRAM_STACK_SIZE64]; // 0x400 bytes
extern GfxPool gGfxPools[2]; // 0x24820 bytes
extern u8 gAudioHeap[0x38000]; // 0x38000 bytes
extern u32 gTotalAllocFailures;
#endif

View File

@@ -16,4 +16,12 @@
#define GC_EU_MQ 12
#define GC_JP_CE 13
#define FAULT_N64 1 // in OoT N64
#define FAULT_GC 2 // in OoT GC
#if PLATFORM_N64
#define FAULT_VERSION FAULT_N64
#else
#define FAULT_VERSION FAULT_GC
#endif
#endif

View File

@@ -52,7 +52,6 @@
#include "regs.h"
#include "irqmgr.h"
#include "padmgr.h"
#include "fault.h"
#include "sched.h"
#include "rumble.h"
#include "mempak.h"

10
spec
View File

@@ -2,6 +2,8 @@
* ROM spec file
*/
#include "include/versions.h"
beginseg
name "makerom"
include "$(BUILD_DIR)/src/makerom/rom_header.o"
@@ -530,8 +532,12 @@ beginseg
#if OOT_DEBUG
include "$(BUILD_DIR)/src/code/debug_malloc.o"
#endif
include "$(BUILD_DIR)/src/code/fault.o"
include "$(BUILD_DIR)/src/code/fault_drawer.o"
#if FAULT_VERSION == FAULT_N64
include "$(BUILD_DIR)/src/code/fault_n64.o"
#elif FAULT_VERSION == FAULT_GC
include "$(BUILD_DIR)/src/code/fault_gc.o"
include "$(BUILD_DIR)/src/code/fault_gc_drawer.o"
#endif
include "$(BUILD_DIR)/src/code/kanread.o"
#if OOT_DEBUG
include "$(BUILD_DIR)/src/code/ucode_disas.o"

View File

@@ -1,4 +1,5 @@
#include "global.h"
#include "fault.h"
NORETURN void __assert(const char* assertion, const char* file, int line) {
char msg[256];

View File

@@ -1,4 +1,5 @@
#include "global.h"
#include "fault.h"
#include "terminal.h"
#if PLATFORM_N64 || OOT_DEBUG

View File

@@ -19,6 +19,7 @@
* to be uncompressed and the request queue and address translation is skipped.
*/
#include "global.h"
#include "fault.h"
#include "terminal.h"
#if PLATFORM_N64
#include "n64dd.h"

View File

@@ -1,4 +1,5 @@
#include "global.h"
#include "fault.h"
#include "terminal.h"
#define FILL_ALLOC_BLOCK_FLAG (1 << 0)
@@ -53,12 +54,12 @@
#define CHECK_FREE_BLOCK(arena, node) (void)0
// Number of allocation failures across all arenas.
u32 sTotalAllocFailures = 0;
u32 gTotalAllocFailures = 0; // "Arena_failcnt"
#define CHECK_ALLOC_FAILURE(arena, ptr) \
do { \
if ((ptr) == NULL) { \
sTotalAllocFailures++; \
gTotalAllocFailures++; \
(arena)->allocFailures++; \
} \
} while (0)

View File

@@ -1,10 +1,9 @@
#include "global.h"
#include "fault.h"
#include "n64dd.h"
// TODO functions of unknown prototype
extern char func_801C8510_unknown[];
extern char func_800AE170_unknown[];
extern char func_800ADCD8_unknown[];
extern char osGetIntMask[];
extern char osSetTime[];
@@ -14,8 +13,8 @@ n64ddStruct_800FF4B0_pointers D_800FF4B0 = {
NULL,
Fault_RemoveClient,
Fault_AddClient,
func_800AE170_unknown,
func_800ADCD8_unknown,
FaultDrawer_DrawText,
Fault_WaitForInput,
Fault_AddHungupAndCrashImpl,
Fault_AddHungupAndCrash,
func_800ADC08,

View File

@@ -40,11 +40,19 @@
* 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 versions.h first and redefine FAULT_VERSION
// This allows this file to compile even when versions.h uses FAULT_N64
#include "versions.h"
#undef FAULT_VERSION
#define FAULT_VERSION FAULT_GC
#include "global.h"
#include "fault.h"
#include "terminal.h"
#include "alloca.h"
#pragma increment_block_number "gc-eu:64 gc-eu-mq:64 gc-eu-mq-dbg:0 gc-jp:64 gc-jp-ce:64 gc-jp-mq:64 gc-us:64" \
#pragma increment_block_number "gc-eu:64 gc-eu-mq:64 gc-eu-mq-dbg:222 gc-jp:64 gc-jp-ce:64 gc-jp-mq:64 gc-us:64" \
"gc-us-mq:64"
void FaultDrawer_Init(void);
@@ -453,9 +461,9 @@ void Fault_DrawCornerRec(u16 color) {
void Fault_PrintFReg(s32 idx, f32* value) {
u32 raw = *(u32*)value;
s32 exp = ((raw & 0x7F800000) >> 0x17) - 0x7F;
s32 exp = ((raw & 0x7F800000) >> 23) - 127;
if ((exp >= -0x7E && exp < 0x80) || raw == 0) {
if ((exp > -127 && exp <= 127) || raw == 0) {
FaultDrawer_Printf("F%02d:%14.7e ", idx, *value);
} else {
// Print subnormal floats as their ieee-754 hex representation
@@ -465,9 +473,9 @@ void Fault_PrintFReg(s32 idx, f32* value) {
void Fault_LogFReg(s32 idx, f32* value) {
u32 raw = *(u32*)value;
s32 exp = ((raw & 0x7F800000) >> 0x17) - 0x7F;
s32 exp = ((raw & 0x7F800000) >> 23) - 127;
if ((exp >= -0x7E && exp < 0x80) || raw == 0) {
if ((exp > -127 && exp <= 127) || raw == 0) {
osSyncPrintf("F%02d:%14.7e ", idx, *value);
} else {
osSyncPrintf("F%02d: %08x(16) ", idx, *(u32*)value);
@@ -510,10 +518,10 @@ void Fault_PrintThreadContext(OSThread* thread) {
__OSThreadContext* ctx;
s16 causeStrIdx = _SHIFTR((u32)thread->context.cause, 2, 5);
if (causeStrIdx == 23) { // Watchpoint
if (causeStrIdx == (EXC_WATCH >> CAUSE_EXCSHIFT)) {
causeStrIdx = 16;
}
if (causeStrIdx == 31) { // Virtual coherency on data
if (causeStrIdx == (EXC_VCED >> CAUSE_EXCSHIFT)) {
causeStrIdx = 17;
}
@@ -572,10 +580,10 @@ void Fault_LogThreadContext(OSThread* thread) {
__OSThreadContext* ctx;
s16 causeStrIdx = _SHIFTR((u32)thread->context.cause, 2, 5);
if (causeStrIdx == 23) { // Watchpoint
if (causeStrIdx == (EXC_WATCH >> CAUSE_EXCSHIFT)) {
causeStrIdx = 16;
}
if (causeStrIdx == 31) { // Virtual coherency on data
if (causeStrIdx == (EXC_VCED >> CAUSE_EXCSHIFT)) {
causeStrIdx = 17;
}
@@ -670,10 +678,12 @@ void Fault_WaitForButtonCombo(void) {
if (1) {}
if (1) {}
// KeyWaitB (LRZ Up Down Up Down Left Left Right Right B A START)
osSyncPrintf(
VT_FGCOL(WHITE) "KeyWaitB ( " VT_FGCOL(WHITE) "" VT_FGCOL(YELLOW) "" VT_FGCOL(YELLOW) "" VT_FGCOL(WHITE) "" VT_FGCOL(WHITE) "" VT_FGCOL(
YELLOW) "" VT_FGCOL(YELLOW) "" VT_FGCOL(WHITE) "" VT_FGCOL(GREEN) "" VT_FGCOL(BLUE) "" VT_FGCOL(RED) "START" VT_FGCOL(WHITE) ")" VT_RST
"\n");
// KeyWaitB'(LR Left Right START)
osSyncPrintf(VT_FGCOL(WHITE) "KeyWaitB'(LR左" VT_FGCOL(YELLOW) "右 +" VT_FGCOL(RED) "START" VT_FGCOL(
WHITE) ")" VT_RST "\n");
@@ -1104,7 +1114,7 @@ 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
*(u32*)thread->context.pc = 0x0000000D; // write in a break instruction
osWritebackDCache((void*)thread->context.pc, 4);
osInvalICache((void*)thread->context.pc, 4);
osStartThread(thread);
@@ -1178,9 +1188,11 @@ void Fault_ThreadEntry(void* arg) {
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 == FAULT_MSG_FAULT) {
sFaultInstance->msgId = (u32)FAULT_MSG_FAULT;
// Fault Manager: OS_EVENT_FAULT received
osSyncPrintf("フォルトマネージャ:OS_EVENT_FAULTを受信しました\n");
} else if (msg == FAULT_MSG_UNK) {
Fault_UpdatePad();
@@ -1188,6 +1200,7 @@ void Fault_ThreadEntry(void* arg) {
continue;
} else {
sFaultInstance->msgId = (u32)FAULT_MSG_UNK;
// Fault Manager: Unknown message received
osSyncPrintf("フォルトマネージャ:不明なメッセージを受信しました\n");
}

View File

@@ -4,7 +4,15 @@
* Implements routines for drawing text with a fixed font directly to a framebuffer, used in displaying
* the crash screen implemented by fault.c
*/
// Include versions.h first and redefine FAULT_VERSION
// This allows this file to compile even when versions.h uses FAULT_N64
#include "versions.h"
#undef FAULT_VERSION
#define FAULT_VERSION FAULT_GC
#include "global.h"
#include "fault.h"
#include "terminal.h"
typedef struct FaultDrawer {
@@ -99,6 +107,8 @@ FaultDrawer sFaultDrawerDefault = {
NULL,
};
#pragma increment_block_number "gc-eu:128 gc-eu-mq:128"
FaultDrawer sFaultDrawer;
char D_8016B6C0[0x20];

845
src/code/fault_n64.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,7 @@
#include "global.h"
#if OOT_DEBUG
#include "fault.h"
#endif
#include "terminal.h"
#pragma increment_block_number "gc-eu:128 gc-eu-mq:128 gc-jp:128 gc-jp-ce:128 gc-jp-mq:128 gc-us:128 gc-us-mq:128"

View File

@@ -1,4 +1,5 @@
#include "global.h"
#include "fault.h"
#include "terminal.h"
#define GFXPOOL_HEAD_MAGIC 0x1234

View File

@@ -1,4 +1,5 @@
#include "global.h"
#include "fault.h"
#include "terminal.h"
#include "versions.h"
#if PLATFORM_N64

View File

@@ -29,6 +29,7 @@
* done while waiting for this operation to complete.
*/
#include "global.h"
#include "fault.h"
#include "terminal.h"
#define PADMGR_LOG(controllerNum, msg) \
@@ -236,7 +237,7 @@ void PadMgr_RumbleStop(PadMgr* padMgr) {
if (osMotorInit(serialEventQueue, &padMgr->rumblePfs[i], i) == 0) {
// If there is a rumble pak attached to this controller, stop it
if (gFaultMgr.msgId == 0 && padMgr->rumbleOnTimer != 0) {
if (FAULT_MSG_ID == 0 && padMgr->rumbleOnTimer != 0) {
PADMGR_LOG(i, T("振動パック 停止", "Stop rumble pak"));
}
osMotorStop(&padMgr->rumblePfs[i]);
@@ -399,7 +400,7 @@ void PadMgr_HandleRetrace(PadMgr* padMgr) {
}
padMgr->validCtrlrsMask = mask;
if (gFaultMgr.msgId != 0) {
if (FAULT_MSG_ID != 0) {
// If fault is active, no rumble
PadMgr_RumbleStop(padMgr);
} else if (padMgr->rumbleOffTimer > 0) {

View File

@@ -40,6 +40,7 @@
* @see irqmgr.c
*/
#include "global.h"
#include "fault.h"
#define RSP_DONE_MSG 667
#define RDP_DONE_MSG 668

Some files were not shown because too many files have changed in this diff Show More