mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-08 23:25:40 +12:00
Don't force users to load rom from terminal or by dragging and dropping onto executable file. Instead, open a blank window and use SDL's drag&drop feature
This commit is contained in:
parent
008f0bfb84
commit
6ce861624d
5 changed files with 179 additions and 149 deletions
|
@ -47,7 +47,7 @@ class Emulator {
|
||||||
static constexpr u32 width = 400;
|
static constexpr u32 width = 400;
|
||||||
static constexpr u32 height = 240 * 2; // * 2 because 2 screens
|
static constexpr u32 height = 240 * 2; // * 2 because 2 screens
|
||||||
ROMType romType = ROMType::None;
|
ROMType romType = ROMType::None;
|
||||||
bool running = true;
|
bool running = true, romLoaded = false;
|
||||||
|
|
||||||
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
||||||
HttpServer httpServer;
|
HttpServer httpServer;
|
||||||
|
@ -64,6 +64,7 @@ class Emulator {
|
||||||
Emulator();
|
Emulator();
|
||||||
~Emulator();
|
~Emulator();
|
||||||
|
|
||||||
|
void stop();
|
||||||
void step();
|
void step();
|
||||||
void render();
|
void render();
|
||||||
void reset();
|
void reset();
|
||||||
|
|
|
@ -22,6 +22,7 @@ enum class GPUInterrupt : u8 {
|
||||||
class Kernel;
|
class Kernel;
|
||||||
|
|
||||||
class GPUService {
|
class GPUService {
|
||||||
|
bool registerInterruptRelayQueueBeenHere = false;
|
||||||
Handle handle = KernelHandles::GPU;
|
Handle handle = KernelHandles::GPU;
|
||||||
Memory& mem;
|
Memory& mem;
|
||||||
GPU& gpu;
|
GPU& gpu;
|
||||||
|
|
|
@ -34,6 +34,7 @@ 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;
|
||||||
sharedMem = nullptr;
|
sharedMem = nullptr;
|
||||||
|
registerInterruptRelayQueueBeenHere = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPUService::handleSyncRequest(u32 messagePointer) {
|
void GPUService::handleSyncRequest(u32 messagePointer) {
|
||||||
|
@ -77,9 +78,8 @@ 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 (registerInterruptRelayQueueBeenHere) Helpers::panic("RegisterInterruptRelayQueue called a second time. Need to implement GSP threads properly");
|
||||||
if (beenHere) Helpers::panic("RegisterInterruptRelayQueue called a second time. Need to implement GSP threads properly");
|
registerInterruptRelayQueueBeenHere = true;
|
||||||
beenHere = true;
|
|
||||||
|
|
||||||
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);
|
||||||
|
|
|
@ -59,7 +59,7 @@ Emulator::Emulator() : kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory
|
||||||
|
|
||||||
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::stop() {
|
||||||
cpu.reset();
|
cpu.reset();
|
||||||
gpu.reset();
|
gpu.reset();
|
||||||
memory.reset();
|
memory.reset();
|
||||||
|
@ -69,7 +69,9 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
void Emulator::reset() {
|
||||||
// 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, 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 (romType != ROMType::None && romPath.has_value()) {
|
||||||
bool success = loadROM(romPath.value());
|
bool success = loadROM(romPath.value());
|
||||||
|
@ -90,18 +92,18 @@ void Emulator::run() {
|
||||||
httpServer.startHttpServer();
|
httpServer.startHttpServer();
|
||||||
#endif
|
#endif
|
||||||
while (running) {
|
while (running) {
|
||||||
|
ServiceManager& srv = kernel.getServiceManager();
|
||||||
|
if(romLoaded) {
|
||||||
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
||||||
pollHttpServer();
|
pollHttpServer();
|
||||||
#endif
|
#endif
|
||||||
runFrame(); // Run 1 frame of instructions
|
runFrame(); // Run 1 frame of instructions
|
||||||
gpu.display(); // Display graphics
|
gpu.display(); // Display graphics
|
||||||
|
|
||||||
ServiceManager& srv = kernel.getServiceManager();
|
|
||||||
|
|
||||||
// Send VBlank interrupts
|
// Send VBlank interrupts
|
||||||
srv.sendGPUInterrupt(GPUInterrupt::VBlank0);
|
srv.sendGPUInterrupt(GPUInterrupt::VBlank0);
|
||||||
srv.sendGPUInterrupt(GPUInterrupt::VBlank1);
|
srv.sendGPUInterrupt(GPUInterrupt::VBlank1);
|
||||||
|
}
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
namespace Keys = HID::Keys;
|
namespace Keys = HID::Keys;
|
||||||
|
@ -113,6 +115,7 @@ void Emulator::run() {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
|
if(romLoaded) {
|
||||||
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;
|
||||||
|
@ -150,9 +153,11 @@ void Emulator::run() {
|
||||||
case SDLK_RETURN: srv.pressKey(Keys::Start); break;
|
case SDLK_RETURN: srv.pressKey(Keys::Start); break;
|
||||||
case SDLK_BACKSPACE: srv.pressKey(Keys::Select); break;
|
case SDLK_BACKSPACE: srv.pressKey(Keys::Select); break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_KEYUP:
|
case SDL_KEYUP:
|
||||||
|
if(romLoaded) {
|
||||||
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;
|
||||||
|
@ -183,9 +188,11 @@ void Emulator::run() {
|
||||||
case SDLK_RETURN: srv.releaseKey(Keys::Start); break;
|
case SDLK_RETURN: srv.releaseKey(Keys::Start); break;
|
||||||
case SDLK_BACKSPACE: srv.releaseKey(Keys::Select); break;
|
case SDLK_BACKSPACE: srv.releaseKey(Keys::Select); break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_MOUSEBUTTONDOWN: {
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
|
if(romLoaded) {
|
||||||
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,15 +210,17 @@ 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(romLoaded) {
|
||||||
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) {
|
||||||
holdingRightClick = false;
|
holdingRightClick = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_CONTROLLERDEVICEADDED:
|
case SDL_CONTROLLERDEVICEADDED:
|
||||||
|
@ -230,7 +239,8 @@ void Emulator::run() {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_CONTROLLERBUTTONUP:
|
case SDL_CONTROLLERBUTTONUP:
|
||||||
case SDL_CONTROLLERBUTTONDOWN: {
|
case SDL_CONTROLLERBUTTONDOWN:
|
||||||
|
if(romLoaded) {
|
||||||
u32 key = 0;
|
u32 key = 0;
|
||||||
|
|
||||||
switch (event.cbutton.button) {
|
switch (event.cbutton.button) {
|
||||||
|
@ -256,9 +266,11 @@ void Emulator::run() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
// Detect mouse motion events for gyroscope emulation
|
// Detect mouse motion events for gyroscope emulation
|
||||||
case SDL_MOUSEMOTION: {
|
case SDL_MOUSEMOTION:
|
||||||
|
if(romLoaded) {
|
||||||
// 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 (!holdingRightClick) break;
|
||||||
|
|
||||||
|
@ -272,11 +284,21 @@ void Emulator::run() {
|
||||||
const s32 pitch = motionY > 0 ? 0x7f : -0x7f;
|
const s32 pitch = motionY > 0 ? 0x7f : -0x7f;
|
||||||
srv.setRoll(roll);
|
srv.setRoll(roll);
|
||||||
srv.setPitch(pitch);
|
srv.setPitch(pitch);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDL_DROPFILE: {
|
||||||
|
char *droppedDir = event.drop.file;
|
||||||
|
if(droppedDir) {
|
||||||
|
loadROM(droppedDir);
|
||||||
|
SDL_free(droppedDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if(romLoaded) {
|
||||||
if (gameController != nullptr) {
|
if (gameController != nullptr) {
|
||||||
const s16 stickX = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTX);
|
const s16 stickX = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTX);
|
||||||
const s16 stickY = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTY);
|
const s16 stickY = SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTY);
|
||||||
|
@ -297,9 +319,10 @@ void Emulator::run() {
|
||||||
srv.setCirclepadY(-(stickY / div));
|
srv.setCirclepadY(-(stickY / div));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
srv.updateInputs(cpu.getTicks());
|
||||||
|
}
|
||||||
|
|
||||||
// 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 +330,7 @@ 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) {
|
||||||
|
stop();
|
||||||
// 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
|
||||||
|
@ -346,6 +370,7 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
|
||||||
romType = ROMType::None;
|
romType = ROMType::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
romLoaded = success;
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -390,6 +415,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,11 +5,13 @@ 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) {
|
||||||
|
auto romPath = std::filesystem::current_path() / argv[1];
|
||||||
if (!emu.loadROM(romPath)) {
|
if (!emu.loadROM(romPath)) {
|
||||||
// For some reason just .c_str() doesn't show the proper path
|
// For some reason just .c_str() doesn't show the proper path
|
||||||
Helpers::panic("Failed to load ROM file: %s", romPath.string().c_str());
|
Helpers::panic("Failed to load ROM file: %s", romPath.string().c_str());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
emu.run();
|
emu.run();
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue