mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-08 23:25:40 +12:00
Merge pull request #101 from SimoneN64/master
Don't force users to load rom from terminal
This commit is contained in:
commit
148232c0b2
5 changed files with 96 additions and 51 deletions
|
@ -61,12 +61,18 @@ class Emulator {
|
||||||
std::optional<std::filesystem::path> romPath = std::nullopt;
|
std::optional<std::filesystem::path> romPath = std::nullopt;
|
||||||
|
|
||||||
public:
|
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();
|
||||||
~Emulator();
|
~Emulator();
|
||||||
|
|
||||||
void step();
|
void step();
|
||||||
void render();
|
void render();
|
||||||
void reset();
|
void reset(ReloadOption reload);
|
||||||
void run();
|
void run();
|
||||||
void runFrame();
|
void runFrame();
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,9 @@ class GPUService {
|
||||||
u32 privilegedProcess;
|
u32 privilegedProcess;
|
||||||
std::optional<Handle> interruptEvent;
|
std::optional<Handle> interruptEvent;
|
||||||
|
|
||||||
|
// Number of threads registered via RegisterInterruptRelayQueue
|
||||||
|
u32 gspThreadCount = 0;
|
||||||
|
|
||||||
MAKE_LOG_FUNCTION(log, gspGPULogger)
|
MAKE_LOG_FUNCTION(log, gspGPULogger)
|
||||||
void processCommandBuffer();
|
void processCommandBuffer();
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ namespace GXCommands {
|
||||||
void GPUService::reset() {
|
void GPUService::reset() {
|
||||||
privilegedProcess = 0xFFFFFFFF; // Set the privileged process to an invalid handle
|
privilegedProcess = 0xFFFFFFFF; // Set the privileged process to an invalid handle
|
||||||
interruptEvent = std::nullopt;
|
interruptEvent = std::nullopt;
|
||||||
|
gspThreadCount = 0;
|
||||||
sharedMem = nullptr;
|
sharedMem = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,9 +78,10 @@ void GPUService::acquireRight(u32 messagePointer) {
|
||||||
// How does the shared memory handle thing work?
|
// How does the shared memory handle thing work?
|
||||||
void GPUService::registerInterruptRelayQueue(u32 messagePointer) {
|
void GPUService::registerInterruptRelayQueue(u32 messagePointer) {
|
||||||
// Detect if this function is called a 2nd time because we'll likely need to impl threads properly for the GSP
|
// Detect if this function is called a 2nd time because we'll likely need to impl threads properly for the GSP
|
||||||
static bool beenHere = false;
|
if (gspThreadCount >= 1) {
|
||||||
if (beenHere) Helpers::panic("RegisterInterruptRelayQueue called a second time. Need to implement GSP threads properly");
|
Helpers::panic("RegisterInterruptRelayQueue called a second time. Need to implement GSP threads properly");
|
||||||
beenHere = true;
|
}
|
||||||
|
gspThreadCount += 1;
|
||||||
|
|
||||||
const u32 flags = mem.read32(messagePointer + 4);
|
const u32 flags = mem.read32(messagePointer + 4);
|
||||||
const u32 eventHandle = mem.read32(messagePointer + 12);
|
const u32 eventHandle = mem.read32(messagePointer + 12);
|
||||||
|
|
110
src/emulator.cpp
110
src/emulator.cpp
|
@ -53,13 +53,12 @@ Emulator::Emulator() : kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory
|
||||||
}
|
}
|
||||||
|
|
||||||
config.load(std::filesystem::current_path() / "config.toml");
|
config.load(std::filesystem::current_path() / "config.toml");
|
||||||
|
reset(ReloadOption::NoReload);
|
||||||
reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Emulator::~Emulator() { config.save(std::filesystem::current_path() / "config.toml"); }
|
Emulator::~Emulator() { config.save(std::filesystem::current_path() / "config.toml"); }
|
||||||
|
|
||||||
void Emulator::reset() {
|
void Emulator::reset(ReloadOption reload) {
|
||||||
cpu.reset();
|
cpu.reset();
|
||||||
gpu.reset();
|
gpu.reset();
|
||||||
memory.reset();
|
memory.reset();
|
||||||
|
@ -70,8 +69,9 @@ void Emulator::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 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 a ROM is active and we reset, with the reload option enabled then reload it.
|
||||||
if (romType != ROMType::None && romPath.has_value()) {
|
// 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());
|
bool success = loadROM(romPath.value());
|
||||||
if (!success) {
|
if (!success) {
|
||||||
romType = ROMType::None;
|
romType = ROMType::None;
|
||||||
|
@ -89,18 +89,21 @@ void Emulator::run() {
|
||||||
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
||||||
httpServer.startHttpServer();
|
httpServer.startHttpServer();
|
||||||
#endif
|
#endif
|
||||||
while (running) {
|
|
||||||
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
|
||||||
pollHttpServer();
|
|
||||||
#endif
|
|
||||||
runFrame(); // Run 1 frame of instructions
|
|
||||||
gpu.display(); // Display graphics
|
|
||||||
|
|
||||||
|
while (running) {
|
||||||
ServiceManager& srv = kernel.getServiceManager();
|
ServiceManager& srv = kernel.getServiceManager();
|
||||||
|
|
||||||
// Send VBlank interrupts
|
if (romType != ROMType::None) {
|
||||||
srv.sendGPUInterrupt(GPUInterrupt::VBlank0);
|
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
||||||
srv.sendGPUInterrupt(GPUInterrupt::VBlank1);
|
pollHttpServer();
|
||||||
|
#endif
|
||||||
|
runFrame(); // Run 1 frame of instructions
|
||||||
|
gpu.display(); // Display graphics
|
||||||
|
|
||||||
|
// Send VBlank interrupts
|
||||||
|
srv.sendGPUInterrupt(GPUInterrupt::VBlank0);
|
||||||
|
srv.sendGPUInterrupt(GPUInterrupt::VBlank1);
|
||||||
|
}
|
||||||
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
|
@ -113,6 +116,8 @@ void Emulator::run() {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
|
if (romType == ROMType::None) break;
|
||||||
|
|
||||||
switch (event.key.keysym.sym) {
|
switch (event.key.keysym.sym) {
|
||||||
case SDLK_l: srv.pressKey(Keys::A); break;
|
case SDLK_l: srv.pressKey(Keys::A); break;
|
||||||
case SDLK_k: srv.pressKey(Keys::B); break;
|
case SDLK_k: srv.pressKey(Keys::B); break;
|
||||||
|
@ -153,6 +158,8 @@ void Emulator::run() {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_KEYUP:
|
case SDL_KEYUP:
|
||||||
|
if (romType == ROMType::None) break;
|
||||||
|
|
||||||
switch (event.key.keysym.sym) {
|
switch (event.key.keysym.sym) {
|
||||||
case SDLK_l: srv.releaseKey(Keys::A); break;
|
case SDLK_l: srv.releaseKey(Keys::A); break;
|
||||||
case SDLK_k: srv.releaseKey(Keys::B); break;
|
case SDLK_k: srv.releaseKey(Keys::B); break;
|
||||||
|
@ -185,7 +192,9 @@ void Emulator::run() {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_MOUSEBUTTONDOWN: {
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
|
if (romType == ROMType::None) break;
|
||||||
|
|
||||||
if (event.button.button == SDL_BUTTON_LEFT) {
|
if (event.button.button == SDL_BUTTON_LEFT) {
|
||||||
const s32 x = event.button.x;
|
const s32 x = event.button.x;
|
||||||
const s32 y = event.button.y;
|
const s32 y = event.button.y;
|
||||||
|
@ -203,10 +212,12 @@ void Emulator::run() {
|
||||||
} else if (event.button.button == SDL_BUTTON_RIGHT) {
|
} else if (event.button.button == SDL_BUTTON_RIGHT) {
|
||||||
holdingRightClick = true;
|
holdingRightClick = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case SDL_MOUSEBUTTONUP:
|
case SDL_MOUSEBUTTONUP:
|
||||||
|
if (romType == ROMType::None) break;
|
||||||
|
|
||||||
if (event.button.button == SDL_BUTTON_LEFT) {
|
if (event.button.button == SDL_BUTTON_LEFT) {
|
||||||
srv.releaseTouchScreen();
|
srv.releaseTouchScreen();
|
||||||
} else if (event.button.button == SDL_BUTTON_RIGHT) {
|
} else if (event.button.button == SDL_BUTTON_RIGHT) {
|
||||||
|
@ -231,6 +242,7 @@ void Emulator::run() {
|
||||||
|
|
||||||
case SDL_CONTROLLERBUTTONUP:
|
case SDL_CONTROLLERBUTTONUP:
|
||||||
case SDL_CONTROLLERBUTTONDOWN: {
|
case SDL_CONTROLLERBUTTONDOWN: {
|
||||||
|
if (romType == ROMType::None) break;
|
||||||
u32 key = 0;
|
u32 key = 0;
|
||||||
|
|
||||||
switch (event.cbutton.button) {
|
switch (event.cbutton.button) {
|
||||||
|
@ -255,12 +267,13 @@ void Emulator::run() {
|
||||||
srv.releaseKey(key);
|
srv.releaseKey(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect mouse motion events for gyroscope emulation
|
// Detect mouse motion events for gyroscope emulation
|
||||||
case SDL_MOUSEMOTION: {
|
case SDL_MOUSEMOTION: {
|
||||||
// We use right click to indicate we want to rotate the console. If right click is not held, then this is not a gyroscope rotation
|
// We use right click to indicate we want to rotate the console. If right click is not held, then this is not a gyroscope rotation
|
||||||
if (!holdingRightClick) break;
|
if (romType == ROMType::None || !holdingRightClick) break;
|
||||||
|
|
||||||
// Relative motion since last mouse motion event
|
// Relative motion since last mouse motion event
|
||||||
const s32 motionX = event.motion.xrel;
|
const s32 motionX = event.motion.xrel;
|
||||||
|
@ -274,32 +287,46 @@ void Emulator::run() {
|
||||||
srv.setPitch(pitch);
|
srv.setPitch(pitch);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case SDL_DROPFILE: {
|
||||||
|
char* droppedDir = event.drop.file;
|
||||||
|
|
||||||
|
if (droppedDir) {
|
||||||
|
loadROM(droppedDir);
|
||||||
|
SDL_free(droppedDir);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gameController != nullptr) {
|
// Update controller analog sticks and HID service
|
||||||
const s16 stickX = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTX);
|
if (romType != ROMType::None) {
|
||||||
const s16 stickY = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTY);
|
if (gameController != nullptr) {
|
||||||
constexpr s16 deadzone = 3276;
|
const s16 stickX = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTX);
|
||||||
constexpr s16 maxValue = 0x9C;
|
const s16 stickY = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTY);
|
||||||
constexpr s16 div = 0x8000 / maxValue;
|
constexpr s16 deadzone = 3276;
|
||||||
|
constexpr s16 maxValue = 0x9C;
|
||||||
|
constexpr s16 div = 0x8000 / maxValue;
|
||||||
|
|
||||||
// Avoid overriding the keyboard's circlepad input
|
// Avoid overriding the keyboard's circlepad input
|
||||||
if (abs(stickX) < deadzone && !keyboardAnalogX) {
|
if (abs(stickX) < deadzone && !keyboardAnalogX) {
|
||||||
srv.setCirclepadX(0);
|
srv.setCirclepadX(0);
|
||||||
} else {
|
} else {
|
||||||
srv.setCirclepadX(stickX / div);
|
srv.setCirclepadX(stickX / div);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (abs(stickY) < deadzone && !keyboardAnalogY) {
|
||||||
|
srv.setCirclepadY(0);
|
||||||
|
} else {
|
||||||
|
srv.setCirclepadY(-(stickY / div));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (abs(stickY) < deadzone && !keyboardAnalogY) {
|
srv.updateInputs(cpu.getTicks());
|
||||||
srv.setCirclepadY(0);
|
|
||||||
} else {
|
|
||||||
srv.setCirclepadY(-(stickY / div));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update inputs in the HID module
|
// Update inputs in the HID module
|
||||||
srv.updateInputs(cpu.getTicks());
|
|
||||||
SDL_GL_SwapWindow(window);
|
SDL_GL_SwapWindow(window);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -307,6 +334,11 @@ void Emulator::run() {
|
||||||
void Emulator::runFrame() { cpu.runFrame(); }
|
void Emulator::runFrame() { cpu.runFrame(); }
|
||||||
|
|
||||||
bool Emulator::loadROM(const std::filesystem::path& path) {
|
bool Emulator::loadROM(const std::filesystem::path& path) {
|
||||||
|
// Reset the emulator if we've already loaded a ROM
|
||||||
|
if (romType != ROMType::None) {
|
||||||
|
reset(ReloadOption::NoReload);
|
||||||
|
}
|
||||||
|
|
||||||
// Get path for saving files (AppData on Windows, /home/user/.local/share/ApplcationName on Linux, etc)
|
// Get path for saving files (AppData on Windows, /home/user/.local/share/ApplcationName on Linux, etc)
|
||||||
// Inside that path, we be use a game-specific folder as well. Eg if we were loading a ROM called PenguinDemo.3ds, the savedata would be in
|
// Inside that path, we be use a game-specific folder as well. Eg if we were loading a ROM called PenguinDemo.3ds, the savedata would be in
|
||||||
// %APPDATA%/Alber/PenguinDemo/SaveData on Windows, and so on. We do this because games save data in their own filesystem on the cart
|
// %APPDATA%/Alber/PenguinDemo/SaveData on Windows, and so on. We do this because games save data in their own filesystem on the cart
|
||||||
|
@ -326,7 +358,7 @@ 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
|
bool success; // Tracks if we loaded the ROM successfully
|
||||||
|
|
||||||
if (extension == ".elf" || extension == ".axf")
|
if (extension == ".elf" || extension == ".axf")
|
||||||
success = loadELF(path);
|
success = loadELF(path);
|
||||||
|
@ -390,26 +422,24 @@ bool Emulator::loadELF(std::ifstream& file) {
|
||||||
if (entrypoint.value() & 1) {
|
if (entrypoint.value() & 1) {
|
||||||
Helpers::panic("Misaligned ELF entrypoint. TODO: Check if ELFs can boot in thumb mode");
|
Helpers::panic("Misaligned ELF entrypoint. TODO: Check if ELFs can boot in thumb mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset our graphics context and initialize the GPU's graphics context
|
// Reset our graphics context and initialize the GPU's graphics context
|
||||||
void Emulator::initGraphicsContext() {
|
void Emulator::initGraphicsContext() {
|
||||||
gl.reset(); // TODO (For when we have multiple backends): Only do this if we are using OpenGL
|
gl.reset(); // TODO (For when we have multiple backends): Only do this if we are using OpenGL
|
||||||
gpu.initGraphicsContext();
|
gpu.initGraphicsContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
||||||
void Emulator::pollHttpServer() {
|
void Emulator::pollHttpServer() {
|
||||||
std::scoped_lock lock(httpServer.actionMutex);
|
std::scoped_lock lock(httpServer.actionMutex);
|
||||||
|
|
||||||
ServiceManager& srv = kernel.getServiceManager();
|
ServiceManager& srv = kernel.getServiceManager();
|
||||||
|
|
||||||
if (httpServer.pendingAction) {
|
if (httpServer.pendingAction) {
|
||||||
switch (httpServer.action) {
|
switch (httpServer.action) {
|
||||||
case HttpAction::Screenshot:
|
case HttpAction::Screenshot: gpu.screenshot(HttpServer::httpServerScreenshotPath); break;
|
||||||
gpu.screenshot(HttpServer::httpServerScreenshotPath);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case HttpAction::PressKey:
|
case HttpAction::PressKey:
|
||||||
if (httpServer.pendingKey != 0) {
|
if (httpServer.pendingKey != 0) {
|
||||||
|
|
16
src/main.cpp
16
src/main.cpp
|
@ -5,11 +5,15 @@ int main (int argc, char *argv[]) {
|
||||||
|
|
||||||
emu.initGraphicsContext();
|
emu.initGraphicsContext();
|
||||||
|
|
||||||
auto romPath = std::filesystem::current_path() / (argc > 1 ? argv[1] : "teapot.elf");
|
if (argc > 1) {
|
||||||
if (!emu.loadROM(romPath)) {
|
auto romPath = std::filesystem::current_path() / argv[1];
|
||||||
// For some reason just .c_str() doesn't show the proper path
|
if (!emu.loadROM(romPath)) {
|
||||||
Helpers::panic("Failed to load ROM file: %s", romPath.string().c_str());
|
// For some reason just .c_str() doesn't show the proper path
|
||||||
}
|
Helpers::panic("Failed to load ROM file: %s", romPath.string().c_str());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("No ROM inserted! Load a ROM by dragging and dropping it into the emulator window!\n");
|
||||||
|
}
|
||||||
|
|
||||||
emu.run();
|
emu.run();
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue