Slightly more robust ROM management

This commit is contained in:
wheremyfoodat 2023-07-07 13:39:35 +03:00
parent 0647fb5b85
commit 4a12e59c2f
2 changed files with 28 additions and 7 deletions

View file

@ -5,6 +5,7 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <optional>
#include "PICA/gpu.hpp" #include "PICA/gpu.hpp"
#include "cpu.hpp" #include "cpu.hpp"
@ -25,7 +26,7 @@ class Emulator {
GLStateManager gl; GLStateManager gl;
SDL_Window* window; SDL_Window* window;
SDL_GLContext glContext; SDL_GLContext glContext;
SDL_GameController* gameController; SDL_GameController* gameController = nullptr;
int gameControllerID; int gameControllerID;
// Variables to keep track of whether the user is controlling the 3DS analog stick with their keyboard // Variables to keep track of whether the user is controlling the 3DS analog stick with their keyboard
@ -44,6 +45,8 @@ class Emulator {
std::ifstream loadedELF; std::ifstream loadedELF;
NCSD loadedNCSD; NCSD loadedNCSD;
std::optional<std::filesystem::path> romPath = std::nullopt;
public: public:
Emulator(); Emulator();

View file

@ -63,8 +63,16 @@ void Emulator::reset() {
// Reloading r13 and r15 needs to happen after everything has been reset // Reloading r13 and r15 needs to happen after everything has been reset
// Otherwise resetting the kernel or cpu might nuke them // Otherwise resetting the kernel or cpu might nuke them
cpu.setReg(13, VirtualAddrs::StackTop); // Set initial SP cpu.setReg(13, VirtualAddrs::StackTop); // Set initial SP
if (romType == ROMType::ELF) { // Reload ELF if we're using one
loadELF(loadedELF); // 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()) {
bool success = loadROM(romPath.value());
if (!success) {
romType = ROMType::None;
romPath = std::nullopt;
Helpers::panic("Failed to reload ROM. This should pause the emulator in the future GUI");
}
} }
} }
@ -284,17 +292,27 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
kernel.initializeFS(); kernel.initializeFS();
auto extension = path.extension(); auto extension = path.extension();
bool success; // Tracks if we loaded the ROM successfully
if (extension == ".elf" || extension == ".axf") if (extension == ".elf" || extension == ".axf")
return loadELF(path); success = loadELF(path);
else if (extension == ".3ds") else if (extension == ".3ds")
return loadNCSD(path, ROMType::NCSD); success = loadNCSD(path, ROMType::NCSD);
else if (extension == ".cxi" || extension == ".app") else if (extension == ".cxi" || extension == ".app")
return loadNCSD(path, ROMType::CXI); success = loadNCSD(path, ROMType::CXI);
else { else {
printf("Unknown file type\n"); printf("Unknown file type\n");
return false; success = false;
} }
if (success) {
romPath = path;
} else {
romPath = std::nullopt;
romType = ROMType::None;
}
return success;
} }
// Used for loading both CXI and NCSD files since they are both so similar and use the same interface // Used for loading both CXI and NCSD files since they are both so similar and use the same interface