From 384892972cbfb4f0c13e742df49bfa1564eaa06b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 20 Oct 2024 22:32:09 +1100 Subject: [PATCH] NDS: Try to add a crash handler --- src/Platform_NDS.c | 56 ++++++++++++++++++++++++++++++++++++++++++++-- src/Window_NDS.c | 24 ++++++++++---------- 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/Platform_NDS.c b/src/Platform_NDS.c index cac3fd1d9..c0029e9f0 100644 --- a/src/Platform_NDS.c +++ b/src/Platform_NDS.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -25,6 +26,7 @@ #include #include #include +#include #include #ifdef BUILD_DSI #include "../third_party/dsiwifi/include/dsiwifi9.h" @@ -83,10 +85,11 @@ static void LogNocash(const char* msg, int len) { nocashWrite(buffer, len + 2); } -extern void consolePrintString(const char* ptr, int len); +extern void Console_Clear(void); +extern void Console_PrintString(const char* ptr, int len); void Platform_Log(const char* msg, int len) { LogNocash(msg, len); - consolePrintString(msg, len); + Console_PrintString(msg, len); } TimeMS DateTime_CurrentUTC(void) { @@ -110,6 +113,54 @@ void DateTime_CurrentLocal(struct cc_datetime* t) { } +/*########################################################################################################################* +*-----------------------------------------------------Directory/File------------------------------------------------------* +*#########################################################################################################################*/ +static __attribute__((noreturn)) void CrashHandler(void) { + Console_Clear(); + Platform_LogConst(""); + Platform_LogConst(""); + Platform_LogConst("** CLASSICUBE FATALLY CRASHED **"); + Platform_LogConst(""); + + cc_uint32 mode = getCPSR() & CPSR_MODE_MASK; + if (mode == CPSR_MODE_ABORT) { + Platform_LogConst("Tried to read/write invalid memory"); + } else if (mode == CPSR_MODE_UNDEFINED) { + Platform_LogConst("Tried to execute invalid instruction"); + } else { + Platform_Log1("Unknown error: %h", &mode); + } + Platform_LogConst(""); + + static const char* const regNames[] = { + "R0 ", "R1 ", "R2 ", "R3 ", "R4 ", "R5 ", "R6 ", "R7 ", + "R8 ", "R9 ", "R10", "R11", "R12", "SP ", "LR ", "PC " + }; + + for (int r = 0; r < 8; r++) { + Platform_Log4("%c: %h %c: %h", + regNames[r], &exceptionRegisters[r], + regNames[r + 8], &exceptionRegisters[r + 8]); + } + + Platform_LogConst(""); + Platform_LogConst("Please report this on the ClassiCube Discord or forums"); + Platform_LogConst(""); + Platform_LogConst("You will need to restart your DS"); + + // Make the background red since game over anyways + BG_PALETTE_SUB[16 * 16 - 1] = RGB15(31, 0, 0); + BG_PALETTE [16 * 16 - 1] = RGB15(31, 0, 0); + + for (;;) { } +} + +static void InstallCrashHandler(void) { + setExceptionHandler(CrashHandler); +} + + /*########################################################################################################################* *-----------------------------------------------------Directory/File------------------------------------------------------* *#########################################################################################################################*/ @@ -524,6 +575,7 @@ void Platform_Init(void) { #else Platform_Log1("Running in %c mode with NDS wifi", dsiMode ? "DSi" : "DS"); #endif + InstallCrashHandler(); InitFilesystem(); InitNetworking(); diff --git a/src/Window_NDS.c b/src/Window_NDS.c index 396e2fa3a..111e62087 100644 --- a/src/Window_NDS.c +++ b/src/Window_NDS.c @@ -37,7 +37,7 @@ static u16* conFontBgMap; static int conFontCurPal; static int conCursorX, conCurrentRow; -static void consoleClear(void) { +void Console_Clear(void) { for (int i = 0; i < CON_WIDTH * CON_HEIGHT; i++) { conFontBgMap[i] = ' ' - FONT_ASCII_OFFSET; @@ -47,7 +47,7 @@ static void consoleClear(void) { conCurrentRow = 0; } -static void consoleNewLine(void) { +static void Console_NewLine(void) { conCursorX = 0; conCurrentRow++; if (conCurrentRow < CON_HEIGHT) return; @@ -72,28 +72,28 @@ static void consoleNewLine(void) { } } -static void consolePrintChar(char c) { +static void Console_PrintChar(char c) { if (c < ' ') return; // only ASCII supported if (conCursorX >= CON_WIDTH) - consoleNewLine(); + Console_NewLine(); u16 value = conFontCurPal | (c - FONT_ASCII_OFFSET); conFontBgMap[conCursorX + conCurrentRow * CON_WIDTH] = value; conCursorX++; } -void consolePrintString(const char* ptr, int len) { +void Console_PrintString(const char* ptr, int len) { if (!conFontBgMap) return; for (int i = 0; i < len; i++) { - consolePrintChar(ptr[i]); + Console_PrintChar(ptr[i]); } - consoleNewLine(); + Console_NewLine(); } -static void consoleLoadFont(int bgId, u16* palette) { +static void Console_LoadFont(int bgId, u16* palette) { conFontBgMap = (u16*)bgGetMapPtr(bgId); u16* fontBgGfx = (u16*)bgGetGfxPtr(bgId); conFontCurPal = 15 << 12; @@ -117,7 +117,7 @@ static void consoleLoadFont(int bgId, u16* palette) { palette[0] = RGB15( 0, 0, 0); } -static void consoleInit(cc_bool onSub) { +static void Console_Init(cc_bool onSub) { int bgId; if (onSub) { bgId = bgInitSub(LAYER_CON, BgType_Text4bpp, BgSize_T_256x256, 22, 2); @@ -125,8 +125,8 @@ static void consoleInit(cc_bool onSub) { bgId = bgInit( LAYER_CON, BgType_Text4bpp, BgSize_T_256x256, 22, 2); } - consoleLoadFont(bgId, onSub ? BG_PALETTE_SUB : BG_PALETTE); - consoleClear(); + Console_LoadFont(bgId, onSub ? BG_PALETTE_SUB : BG_PALETTE); + Console_Clear(); } @@ -165,7 +165,7 @@ static void SetupVideo(cc_bool mode) { videoSetMode(MODE_0_3D); } - consoleInit(!launcherMode); + Console_Init(!launcherMode); } void Window_PreInit(void) {