NDS: Try to add a crash handler

This commit is contained in:
UnknownShadow200 2024-10-20 22:32:09 +11:00
parent 2e4d92f526
commit 384892972c
2 changed files with 66 additions and 14 deletions

View File

@ -17,6 +17,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <nds/arm9/background.h>
#include <nds/bios.h> #include <nds/bios.h>
#include <nds/cothread.h> #include <nds/cothread.h>
#include <nds/interrupts.h> #include <nds/interrupts.h>
@ -25,6 +26,7 @@
#include <nds/system.h> #include <nds/system.h>
#include <nds/arm9/dldi.h> #include <nds/arm9/dldi.h>
#include <nds/arm9/sdmmc.h> #include <nds/arm9/sdmmc.h>
#include <nds/arm9/exceptions.h>
#include <fat.h> #include <fat.h>
#ifdef BUILD_DSI #ifdef BUILD_DSI
#include "../third_party/dsiwifi/include/dsiwifi9.h" #include "../third_party/dsiwifi/include/dsiwifi9.h"
@ -83,10 +85,11 @@ static void LogNocash(const char* msg, int len) {
nocashWrite(buffer, len + 2); 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) { void Platform_Log(const char* msg, int len) {
LogNocash(msg, len); LogNocash(msg, len);
consolePrintString(msg, len); Console_PrintString(msg, len);
} }
TimeMS DateTime_CurrentUTC(void) { 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------------------------------------------------------* *-----------------------------------------------------Directory/File------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
@ -524,6 +575,7 @@ void Platform_Init(void) {
#else #else
Platform_Log1("Running in %c mode with NDS wifi", dsiMode ? "DSi" : "DS"); Platform_Log1("Running in %c mode with NDS wifi", dsiMode ? "DSi" : "DS");
#endif #endif
InstallCrashHandler();
InitFilesystem(); InitFilesystem();
InitNetworking(); InitNetworking();

View File

@ -37,7 +37,7 @@ static u16* conFontBgMap;
static int conFontCurPal; static int conFontCurPal;
static int conCursorX, conCurrentRow; static int conCursorX, conCurrentRow;
static void consoleClear(void) { void Console_Clear(void) {
for (int i = 0; i < CON_WIDTH * CON_HEIGHT; i++) for (int i = 0; i < CON_WIDTH * CON_HEIGHT; i++)
{ {
conFontBgMap[i] = ' ' - FONT_ASCII_OFFSET; conFontBgMap[i] = ' ' - FONT_ASCII_OFFSET;
@ -47,7 +47,7 @@ static void consoleClear(void) {
conCurrentRow = 0; conCurrentRow = 0;
} }
static void consoleNewLine(void) { static void Console_NewLine(void) {
conCursorX = 0; conCursorX = 0;
conCurrentRow++; conCurrentRow++;
if (conCurrentRow < CON_HEIGHT) return; 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 (c < ' ') return; // only ASCII supported
if (conCursorX >= CON_WIDTH) if (conCursorX >= CON_WIDTH)
consoleNewLine(); Console_NewLine();
u16 value = conFontCurPal | (c - FONT_ASCII_OFFSET); u16 value = conFontCurPal | (c - FONT_ASCII_OFFSET);
conFontBgMap[conCursorX + conCurrentRow * CON_WIDTH] = value; conFontBgMap[conCursorX + conCurrentRow * CON_WIDTH] = value;
conCursorX++; conCursorX++;
} }
void consolePrintString(const char* ptr, int len) { void Console_PrintString(const char* ptr, int len) {
if (!conFontBgMap) return; if (!conFontBgMap) return;
for (int i = 0; i < len; i++) 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); conFontBgMap = (u16*)bgGetMapPtr(bgId);
u16* fontBgGfx = (u16*)bgGetGfxPtr(bgId); u16* fontBgGfx = (u16*)bgGetGfxPtr(bgId);
conFontCurPal = 15 << 12; conFontCurPal = 15 << 12;
@ -117,7 +117,7 @@ static void consoleLoadFont(int bgId, u16* palette) {
palette[0] = RGB15( 0, 0, 0); palette[0] = RGB15( 0, 0, 0);
} }
static void consoleInit(cc_bool onSub) { static void Console_Init(cc_bool onSub) {
int bgId; int bgId;
if (onSub) { if (onSub) {
bgId = bgInitSub(LAYER_CON, BgType_Text4bpp, BgSize_T_256x256, 22, 2); 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); bgId = bgInit( LAYER_CON, BgType_Text4bpp, BgSize_T_256x256, 22, 2);
} }
consoleLoadFont(bgId, onSub ? BG_PALETTE_SUB : BG_PALETTE); Console_LoadFont(bgId, onSub ? BG_PALETTE_SUB : BG_PALETTE);
consoleClear(); Console_Clear();
} }
@ -165,7 +165,7 @@ static void SetupVideo(cc_bool mode) {
videoSetMode(MODE_0_3D); videoSetMode(MODE_0_3D);
} }
consoleInit(!launcherMode); Console_Init(!launcherMode);
} }
void Window_PreInit(void) { void Window_PreInit(void) {