Skip to content

Commit

Permalink
Add reset on START+SELECT, with confirmation
Browse files Browse the repository at this point in the history
Co-authored-by: DeltaF1 <[email protected]>
  • Loading branch information
asiekierka and DeltaF1 committed Feb 4, 2022
1 parent 649db83 commit e4841a3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
47 changes: 42 additions & 5 deletions arm9/source/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -423,8 +460,8 @@ main(int argc, char **argv)
#else
consoleSetWindow(mainConsole, 0, 0, 32, 14);
#endif
consoleSelect(mainConsole);
#endif
consoleSelect(mainConsole);

keyboardDemoInit();

Expand Down
22 changes: 20 additions & 2 deletions arm9/source/uxn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/uxn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));

0 comments on commit e4841a3

Please sign in to comment.