From 20fe0f35b58cd0be20102b82c4f3fff883911017 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 16 Feb 2024 22:40:21 +0100 Subject: Improve reset code --- src/ahoy/emulator.c | 11 +++-------- src/ahoy/emulator.h | 1 - src/ahoy/gui.c | 8 ++++---- src/ahoy/gui.h | 11 ++++++----- src/ahoy/main.c | 11 +++++++++-- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/ahoy/emulator.c b/src/ahoy/emulator.c index 3b6544b..79ff4dc 100644 --- a/src/ahoy/emulator.c +++ b/src/ahoy/emulator.c @@ -43,20 +43,15 @@ static uint8_t mem[MEM_TOTAL] = { void emuinit(struct u8view prog, const char *fn) { + struct timespec tp; + filename = fn; if (prog.len > MEM_FREE) { diex("%s: binary of size %.1f KiB exceeds %d B maximum", filename, (double)prog.len / 1024, MEM_FREE); } - memcpy(mem + MEM_RESERVED, prog.p, prog.len); - emureset(); -} - -void -emureset(void) -{ - struct timespec tp; + memcpy(mem + MEM_RESERVED, prog.p, prog.len); memset(&c8, 0, sizeof(c8)); c8.PC = MEM_RESERVED; diff --git a/src/ahoy/emulator.h b/src/ahoy/emulator.h index 33618da..24aa8ce 100644 --- a/src/ahoy/emulator.h +++ b/src/ahoy/emulator.h @@ -26,7 +26,6 @@ struct chip8 { void emuinit(struct u8view, const char *); void emutick(void); -void emureset(void); extern struct chip8 c8; diff --git a/src/ahoy/gui.c b/src/ahoy/gui.c index 083d4e2..0997bf9 100644 --- a/src/ahoy/gui.c +++ b/src/ahoy/gui.c @@ -17,7 +17,7 @@ static void audio_callback(void *, uint8_t *, int); -guistate gs; +emustate estate = ES_RUNNING; static SDL_Window *win; static SDL_Renderer *rndr; static SDL_AudioDeviceID adev; @@ -119,17 +119,17 @@ readkb(void) while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: - gs = GUI_STOP; + estate = ES_STOP; break; case SDL_KEYDOWN: switch (e.key.keysym.sym) { case SDLK_SPACE: - gs = gs == GUI_RUNNING ? GUI_PAUSED : GUI_RUNNING; + estate = estate == ES_RUNNING ? ES_PAUSED : ES_RUNNING; break; case SDLK_EQUALS: - emureset(); + estate = ES_RESET; break; // case SDLK_j: diff --git a/src/ahoy/gui.h b/src/ahoy/gui.h index d3953b5..0760437 100644 --- a/src/ahoy/gui.h +++ b/src/ahoy/gui.h @@ -5,10 +5,11 @@ #include typedef enum { - GUI_RUNNING, - GUI_PAUSED, - GUI_STOP, -} guistate; + ES_PAUSED, + ES_RESET, + ES_RUNNING, + ES_STOP, +} emustate; void wininit(void); void winfree(void); @@ -16,6 +17,6 @@ void windrw(void); void auplay(bool); void readkb(void); -extern guistate gs; +extern emustate estate; #endif /* !AHOY_AHOY_GUI_H */ diff --git a/src/ahoy/main.c b/src/ahoy/main.c index 857185b..b107fa2 100644 --- a/src/ahoy/main.c +++ b/src/ahoy/main.c @@ -176,15 +176,22 @@ run(int fd, const char *fn) free(buf); wininit(); +reset: emuinit(u8strtou8(sb), fn); + windrw(); + auplay(true); - while (gs != GUI_STOP) { + while (estate != ES_STOP) { double dt; uint64_t st, et; readkb(); - if (gs == GUI_PAUSED) + if (estate == ES_PAUSED) continue; + if (estate == ES_RESET) { + estate = ES_RUNNING; + goto reset; + } st = SDL_GetPerformanceCounter(); for (unsigned i = 0; i < cfg.cpu_hz / FPS; i++) -- cgit v1.2.3