From b352309290087a719a21547f847c7ea53dd8be23 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Tue, 11 Jul 2023 16:17:36 +0300 Subject: [PATCH] Introduce 2 methods of resetting the emulator; with and without reload --- include/emulator.hpp | 8 +++++++- src/emulator.cpp | 11 ++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/emulator.hpp b/include/emulator.hpp index 3985c613..83b832f6 100644 --- a/include/emulator.hpp +++ b/include/emulator.hpp @@ -61,12 +61,18 @@ class Emulator { std::optional romPath = std::nullopt; public: + // Decides whether to reload or not reload the ROM when resetting. We use enum class over a plain bool for clarity. + // If NoReload is selected, the emulator will not reload its selected ROM. This is useful for things like booting up the emulator, or resetting to + // change ROMs. If Reload is selected, the emulator will reload its selected ROM. This is useful for eg a "reset" button that keeps the current ROM + // and just resets the emu + enum class ReloadOption { NoReload, Reload }; + Emulator(); ~Emulator(); void step(); void render(); - void reset(); + void reset(ReloadOption reload); void run(); void runFrame(); diff --git a/src/emulator.cpp b/src/emulator.cpp index c7508681..502e7900 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -53,12 +53,12 @@ Emulator::Emulator() : kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory } config.load(std::filesystem::current_path() / "config.toml"); - reset(); + reset(ReloadOption::NoReload); } Emulator::~Emulator() { config.save(std::filesystem::current_path() / "config.toml"); } -void Emulator::reset() { +void Emulator::reset(ReloadOption reload) { cpu.reset(); gpu.reset(); memory.reset(); @@ -69,8 +69,9 @@ void Emulator::reset() { // Otherwise resetting the kernel or cpu might nuke them cpu.setReg(13, VirtualAddrs::StackTop); // Set initial SP - // If a ROM is active and we reset, reload it. This is necessary to set up stack, executable memory, .data/.rodata/.bss all over again - if (romType != ROMType::None && romPath.has_value()) { + // If a ROM is active and we reset, with the reload option enabled then reload it. + // This is necessary to set up stack, executable memory, .data/.rodata/.bss all over again + if (reload == ReloadOption::Reload && romType != ROMType::None && romPath.has_value()) { bool success = loadROM(romPath.value()); if (!success) { romType = ROMType::None; @@ -335,7 +336,7 @@ void Emulator::runFrame() { cpu.runFrame(); } bool Emulator::loadROM(const std::filesystem::path& path) { // Reset the emulator if we've already loaded a ROM if (romType != ROMType::None) { - reset(); + reset(ReloadOption::NoReload); } // Get path for saving files (AppData on Windows, /home/user/.local/share/ApplcationName on Linux, etc)