From c7adf21a4c52bd78428a93cdbaf25c52b03783ba Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 16 Feb 2024 22:22:10 +0100 Subject: Allow the user to reset the emulator --- src/TODO | 1 - src/ahoy/config.h | 4 ++-- src/ahoy/emulator.c | 20 +++++++++++++------- src/ahoy/emulator.h | 1 + src/ahoy/gui.c | 8 ++++---- src/ahoy/main.c | 5 ++--- 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/TODO b/src/TODO index 87bc4da..bade6b3 100644 --- a/src/TODO +++ b/src/TODO @@ -12,7 +12,6 @@ ahoy: — Savestates — Scanlines — Other cool visual effects - — Machine reset c8c: — Implement the C8 compiler — Write assembly to stdout, allowing for such constructions as diff --git a/src/ahoy/config.h b/src/ahoy/config.h index 106382f..6fa60c2 100644 --- a/src/ahoy/config.h +++ b/src/ahoy/config.h @@ -4,8 +4,8 @@ #include struct config { - /* Valid ranges are 0–UINT16_MAX. >UINT16_MAX is for random seed. */ - uint32_t seed; + bool seeded; + uint16_t seed; unsigned cpu_hz; }; diff --git a/src/ahoy/emulator.c b/src/ahoy/emulator.c index f774b54..3b6544b 100644 --- a/src/ahoy/emulator.c +++ b/src/ahoy/emulator.c @@ -43,24 +43,30 @@ 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; + + memset(&c8, 0, sizeof(c8)); c8.PC = MEM_RESERVED; - memcpy(mem + c8.PC, prog.p, prog.len); - if (cfg.seed > UINT16_MAX) { + if (cfg.seeded) + srand(cfg.seed); + else { if (clock_gettime(CLOCK_REALTIME, &tp) == -1) die("clock_gettime"); srand((tp.tv_sec ^ tp.tv_nsec) & UINT16_MAX); - } else - srand(cfg.seed); + } } void diff --git a/src/ahoy/emulator.h b/src/ahoy/emulator.h index 24aa8ce..33618da 100644 --- a/src/ahoy/emulator.h +++ b/src/ahoy/emulator.h @@ -26,6 +26,7 @@ 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 a052a39..072d5b9 100644 --- a/src/ahoy/gui.c +++ b/src/ahoy/gui.c @@ -128,10 +128,10 @@ readkb(void) gs = gs == GUI_RUNNING ? GUI_PAUSED : GUI_RUNNING; break; - // case SDLK_EQUALS: - // init_chip8(chip8, *config, chip8->rom_name); - // break; - // + case SDLK_EQUALS: + emureset(); + break; + // case SDLK_j: // // 'j': Decrease color lerp rate // if (config->color_lerp_rate > 0.1) diff --git a/src/ahoy/main.c b/src/ahoy/main.c index fc737e6..857185b 100644 --- a/src/ahoy/main.c +++ b/src/ahoy/main.c @@ -52,9 +52,7 @@ STRTOX(uint16_t, u16) [[noreturn]] static void usage(void); static void run(int, const char *); -struct config cfg = { - .seed = UINT16_MAX + 1, -}; +struct config cfg; static const char *argv0; void @@ -114,6 +112,7 @@ main(int argc, char **argv) break; case 's': NUMERIC_ARG(uint16_t, UINT16_MAX, PRIu16, strtou16, seed, "seed"); + cfg.seeded = true; break; default: usage(); -- cgit v1.2.3