From e4841a3886f488cc76489a0f10c167829db07254 Mon Sep 17 00:00:00 2001 From: Adrian Siekierka Date: Fri, 4 Feb 2022 08:41:51 +0100 Subject: [PATCH] Add reset on START+SELECT, with confirmation Co-authored-by: DeltaF1 --- arm9/source/emulator.c | 47 +++++++++++++++++++++++++++++++++++++----- arm9/source/uxn.c | 22 ++++++++++++++++++-- include/uxn.h | 1 + 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/arm9/source/emulator.c b/arm9/source/emulator.c index 8397adf..a22d56a 100644 --- a/arm9/source/emulator.c +++ b/arm9/source/emulator.c @@ -28,13 +28,16 @@ static Device *devscreen, *devctrl, *devmouse, *devaudio0; Uint8 dispswap = 0, debug = 0; -#ifdef DEBUG static PrintConsole *mainConsole; +#ifdef DEBUG #ifdef DEBUG_PROFILE static PrintConsole profileConsole; #endif #endif +#define RESET_KEYS (KEY_START | KEY_SELECT) +#define TICK_RESET_KEYS (KEY_X | KEY_Y) + int clamp(int val, int min, int max) { @@ -356,6 +359,37 @@ profiler_ticks(Uint32 tticks, int pos, const char *name) } #endif +void +prompt_reset(Uxn *u) +{ + consoleClear(); + iprintf("Would you like to reset uxnds?\n\n [A] - Yes\n [B] - No\n"); + while(1) { + swiWaitForVBlank(); + scanKeys(); + int allHeld = keysDown() | keysHeld(); + if (allHeld & KEY_A) { + consoleClear(); + break; + } else if (allHeld & KEY_B) { + consoleClear(); + return; + } + } + + iprintf("Resetting...\n"); + + if(!resetuxn(u)) + return error("Resetting", "Failed"); + if(!loaduxn(u, "boot.rom")) + return error("Load", "Failed"); + if(!initppu(&ppu)) + return error("PPU", "Init failure"); + evaluxn(u, 0x0100); + + consoleClear(); +} + int start(Uxn *u) { @@ -366,12 +400,16 @@ start(Uxn *u) evaluxn(u, 0x0100); while(1) { scanKeys(); + int allHeld = keysDown() | keysHeld(); #ifdef DEBUG_PROFILE // X+Y in debugger mode resets tticks_peak - if ((keysHeld() & (KEY_X | KEY_Y)) == (KEY_X | KEY_Y)) + if ((keysDown() & TICK_RESET_KEYS) && ((allHeld & TICK_RESET_KEYS) == TICK_RESET_KEYS)) memset(tticks_peak, 0, sizeof(tticks_peak)); tticks = timer_ticks(0); #endif + // On the first frame that L+R are held + if ((keysDown() & RESET_KEYS) && ((allHeld & RESET_KEYS) == RESET_KEYS)) + prompt_reset(u); doctrl(u); domouse(u); #ifdef DEBUG_PROFILE @@ -406,9 +444,8 @@ main(int argc, char **argv) videoSetModeSub(MODE_0_2D); vramSetBankC(VRAM_C_SUB_BG); -#ifdef DEBUG mainConsole = consoleDemoInit(); - +#ifdef DEBUG #ifdef DEBUG_PROFILE // Timers 0-1 - profiling timers TIMER0_DATA = 0; @@ -423,8 +460,8 @@ main(int argc, char **argv) #else consoleSetWindow(mainConsole, 0, 0, 32, 14); #endif - consoleSelect(mainConsole); #endif + consoleSelect(mainConsole); keyboardDemoInit(); diff --git a/arm9/source/uxn.c b/arm9/source/uxn.c index 72d5d6b..bb980be 100644 --- a/arm9/source/uxn.c +++ b/arm9/source/uxn.c @@ -4034,13 +4034,31 @@ evaluxn(Uxn *u, Uint16 vec) #endif } +int +resetuxn(Uxn *u) +{ + // Reset the stacks + memset(&(u->wst), 0, sizeof(Stack)); + memset(&(u->rst), 0, sizeof(Stack)); + + Device *device; + for (int i = 0; i < 16; i++) { + device = &(u->dev[i]); + memset(device->dat, 0, 16); + device->vector = (Uint16) 0; + } + + // Reset RAM + memset(u->ram.dat, 0, 65536); + return 1; +} + int bootuxn(Uxn *u) { memset(u, 0, sizeof(*u)); u->ram.dat = malloc(65536); - memset(u->ram.dat, 0, 65536); - return 1; + return resetuxn(u); } int diff --git a/include/uxn.h b/include/uxn.h index 32a6692..4cdbe8a 100644 --- a/include/uxn.h +++ b/include/uxn.h @@ -60,6 +60,7 @@ static inline void poke16(Uint8 *m, Uint16 a, Uint16 b) { poke8(m, a, b >> 8); static inline Uint16 peek16(Uint8 *m, Uint16 a) { return (peek8(m, a) << 8) + peek8(m, a + 1); } int loaduxn(Uxn *c, char *filepath); +int resetuxn(Uxn *c); int bootuxn(Uxn *c); int evaluxn(Uxn *u, Uint16 vec); Device *portuxn(Uxn *u, Uint8 id, char *name, int (*talkfn)(Device *, Uint8, Uint8));