From 13524cb62ae310e5ee9268fe7337dd07c1e769c1 Mon Sep 17 00:00:00 2001 From: Snesrev Date: Sat, 11 Mar 2023 17:56:55 +0100 Subject: [PATCH] Show a better message if the game crashes --- src/main.c | 8 ++++++-- src/snes/cart.c | 9 ++++++--- src/snes/cpu.c | 10 +++++++--- src/types.h | 1 + 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index c8a6dc2..25bbfcd 100644 --- a/src/main.c +++ b/src/main.c @@ -78,13 +78,17 @@ static uint16 g_gamepad_last_cmd[kGamepadBtn_Count]; extern Snes *g_snes; void NORETURN Die(const char *error) { -#if defined(NDEBUG) && defined(_WIN32) SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, kWindowTitle, error, NULL); -#endif fprintf(stderr, "Error: %s\n", error); exit(1); } +void Warning(const char *error) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, kWindowTitle, error, NULL); + fprintf(stderr, "Warning: %s\n", error); +} + + void ChangeWindowScale(int scale_step) { if ((SDL_GetWindowFlags(g_window) & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED)) != 0) return; diff --git a/src/snes/cart.c b/src/snes/cart.c index 623f713..3b96db5 100644 --- a/src/snes/cart.c +++ b/src/snes/cart.c @@ -3,8 +3,7 @@ #include #include #include -#include - +#include "../types.h" #include "cart.h" #include "snes.h" @@ -72,6 +71,8 @@ void cart_write(Cart* cart, uint8_t bank, uint16_t adr, uint8_t val) { } } +void DumpCpuHistory(); + static uint8_t cart_readLorom(Cart* cart, uint8_t bank, uint16_t adr) { if(((bank >= 0x70 && bank < 0x7e) || bank >= 0xf0) && adr < 0x8000 && cart->ramSize > 0) { // banks 70-7e and f0-ff, adr 0000-7fff @@ -82,7 +83,9 @@ static uint8_t cart_readLorom(Cart* cart, uint8_t bank, uint16_t adr) { // adr 8000-ffff in all banks or all addresses in banks 40-7f and c0-ff return cart->rom[((bank << 15) | (adr & 0x7fff)) & (cart->romSize - 1)]; } - assert(0); + printf("While trying to read from 0x%x\n", bank << 16 | adr); + DumpCpuHistory(); + Die("The game crashed in cart_readLorom"); return cart->snes->openBus; } diff --git a/src/snes/cpu.c b/src/snes/cpu.c index 6995ef7..ab205b9 100644 --- a/src/snes/cpu.c +++ b/src/snes/cpu.c @@ -727,6 +727,12 @@ uint32_t pc_hist[8], pc_hist_ctr; uint32_t pc_bp = 0; +void DumpCpuHistory() { + for (int i = 0; i < 8; i++) { + printf("PC history: 0x%x\n", pc_hist[(pc_hist_ctr + i) & 7]); + } +} + static void cpu_doOpcode(Cpu* cpu, uint8_t opcode) { uint32 cur_pc = ((cpu->k << 16) | cpu->pc - 1); pc_hist[pc_hist_ctr] = cur_pc; @@ -1344,9 +1350,7 @@ restart: uint8_t new_k = cpu_readOpcode(cpu); if (new_k == 0x80 && value == 0x8573) { printf("Current PC = 0x%x\n", cpu->k << 16 | cpu->pc); - for (int i = 0; i < 8; i++) { - printf("PC history: 0x%x\n", pc_hist[(pc_hist_ctr + i) & 7]); - } + DumpCpuHistory(); Die("The game has crashed!\n"); } cpu->k = new_k; diff --git a/src/types.h b/src/types.h index 72f37d7..dc05f79 100644 --- a/src/types.h +++ b/src/types.h @@ -70,6 +70,7 @@ static FORCEINLINE uint UintMax(uint a, uint b) { return a > b ? a : b; } static inline uint16 swap16(uint16 v) { return (v << 8) | (v >> 8); } void NORETURN Die(const char *error); +void Warning(const char *error); // compile time assertion #define __CASSERT_N0__(l) COMPILE_TIME_ASSERT_ ## l