Bonk frontend changes

This commit is contained in:
wheremyfoodat 2023-07-11 02:47:09 +03:00
parent 6ce861624d
commit de2751fb5c
5 changed files with 149 additions and 140 deletions

View file

@ -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, romLoaded = false; bool running = true;
#ifdef PANDA3DS_ENABLE_HTTP_SERVER #ifdef PANDA3DS_ENABLE_HTTP_SERVER
HttpServer httpServer; HttpServer httpServer;
@ -64,7 +64,6 @@ class Emulator {
Emulator(); Emulator();
~Emulator(); ~Emulator();
void stop();
void step(); void step();
void render(); void render();
void reset(); void reset();

View file

@ -22,7 +22,6 @@ 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;
@ -35,6 +34,9 @@ class GPUService {
u32 privilegedProcess; u32 privilegedProcess;
std::optional<Handle> interruptEvent; std::optional<Handle> interruptEvent;
// Number of threads registers via RegisterInterruptRelayQueue
u32 gspThreadCount = 0;
MAKE_LOG_FUNCTION(log, gspGPULogger) MAKE_LOG_FUNCTION(log, gspGPULogger)
void processCommandBuffer(); void processCommandBuffer();

View file

@ -33,8 +33,8 @@ 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;
registerInterruptRelayQueueBeenHere = false;
} }
void GPUService::handleSyncRequest(u32 messagePointer) { void GPUService::handleSyncRequest(u32 messagePointer) {
@ -78,8 +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
if (registerInterruptRelayQueueBeenHere) Helpers::panic("RegisterInterruptRelayQueue called a second time. Need to implement GSP threads properly"); if (gspThreadCount >= 1) {
registerInterruptRelayQueueBeenHere = true; Helpers::panic("RegisterInterruptRelayQueue called a second time. Need to implement GSP threads properly");
}
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);

View file

@ -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(); reset();
} }
Emulator::~Emulator() { config.save(std::filesystem::current_path() / "config.toml"); } Emulator::~Emulator() { config.save(std::filesystem::current_path() / "config.toml"); }
void Emulator::stop() { void Emulator::reset() {
cpu.reset(); cpu.reset();
gpu.reset(); gpu.reset();
memory.reset(); memory.reset();
@ -69,9 +68,7 @@ void Emulator::stop() {
// 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());
@ -91,9 +88,11 @@ void Emulator::run() {
#ifdef PANDA3DS_ENABLE_HTTP_SERVER #ifdef PANDA3DS_ENABLE_HTTP_SERVER
httpServer.startHttpServer(); httpServer.startHttpServer();
#endif #endif
while (running) { while (running) {
ServiceManager& srv = kernel.getServiceManager(); ServiceManager& srv = kernel.getServiceManager();
if(romLoaded) {
if (romType != ROMType::None) {
#ifdef PANDA3DS_ENABLE_HTTP_SERVER #ifdef PANDA3DS_ENABLE_HTTP_SERVER
pollHttpServer(); pollHttpServer();
#endif #endif
@ -104,6 +103,7 @@ void Emulator::run() {
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;
@ -115,111 +115,112 @@ void Emulator::run() {
return; return;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if(romLoaded) { if (romType == ROMType::None) break;
switch (event.key.keysym.sym) {
case SDLK_l: srv.pressKey(Keys::A); break;
case SDLK_k: srv.pressKey(Keys::B); break;
case SDLK_o: srv.pressKey(Keys::X); break;
case SDLK_i: srv.pressKey(Keys::Y); break;
case SDLK_q: srv.pressKey(Keys::L); break; switch (event.key.keysym.sym) {
case SDLK_p: srv.pressKey(Keys::R); break; case SDLK_l: srv.pressKey(Keys::A); break;
case SDLK_k: srv.pressKey(Keys::B); break;
case SDLK_o: srv.pressKey(Keys::X); break;
case SDLK_i: srv.pressKey(Keys::Y); break;
case SDLK_RIGHT: srv.pressKey(Keys::Right); break; case SDLK_q: srv.pressKey(Keys::L); break;
case SDLK_LEFT: srv.pressKey(Keys::Left); break; case SDLK_p: srv.pressKey(Keys::R); break;
case SDLK_UP: srv.pressKey(Keys::Up); break;
case SDLK_DOWN: srv.pressKey(Keys::Down); break;
case SDLK_w: case SDLK_RIGHT: srv.pressKey(Keys::Right); break;
srv.setCirclepadY(0x9C); case SDLK_LEFT: srv.pressKey(Keys::Left); break;
keyboardAnalogY = true; case SDLK_UP: srv.pressKey(Keys::Up); break;
break; case SDLK_DOWN: srv.pressKey(Keys::Down); break;
case SDLK_a: case SDLK_w:
srv.setCirclepadX(-0x9C); srv.setCirclepadY(0x9C);
keyboardAnalogX = true; keyboardAnalogY = true;
break; break;
case SDLK_s: case SDLK_a:
srv.setCirclepadY(-0x9C); srv.setCirclepadX(-0x9C);
keyboardAnalogY = true; keyboardAnalogX = true;
break; break;
case SDLK_d: case SDLK_s:
srv.setCirclepadX(0x9C); srv.setCirclepadY(-0x9C);
keyboardAnalogX = true; keyboardAnalogY = true;
break; break;
case SDLK_RETURN: srv.pressKey(Keys::Start); break; case SDLK_d:
case SDLK_BACKSPACE: srv.pressKey(Keys::Select); break; srv.setCirclepadX(0x9C);
} keyboardAnalogX = true;
break;
case SDLK_RETURN: srv.pressKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.pressKey(Keys::Select); break;
} }
break; break;
case SDL_KEYUP: case SDL_KEYUP:
if(romLoaded) { if (romType == ROMType::None) break;
switch (event.key.keysym.sym) {
case SDLK_l: srv.releaseKey(Keys::A); break;
case SDLK_k: srv.releaseKey(Keys::B); break;
case SDLK_o: srv.releaseKey(Keys::X); break;
case SDLK_i: srv.releaseKey(Keys::Y); break;
case SDLK_q: srv.releaseKey(Keys::L); break; switch (event.key.keysym.sym) {
case SDLK_p: srv.releaseKey(Keys::R); break; case SDLK_l: srv.releaseKey(Keys::A); break;
case SDLK_k: srv.releaseKey(Keys::B); break;
case SDLK_o: srv.releaseKey(Keys::X); break;
case SDLK_i: srv.releaseKey(Keys::Y); break;
case SDLK_RIGHT: srv.releaseKey(Keys::Right); break; case SDLK_q: srv.releaseKey(Keys::L); break;
case SDLK_LEFT: srv.releaseKey(Keys::Left); break; case SDLK_p: srv.releaseKey(Keys::R); break;
case SDLK_UP: srv.releaseKey(Keys::Up); break;
case SDLK_DOWN: srv.releaseKey(Keys::Down); break;
// Err this is probably not ideal case SDLK_RIGHT: srv.releaseKey(Keys::Right); break;
case SDLK_w: case SDLK_LEFT: srv.releaseKey(Keys::Left); break;
case SDLK_s: case SDLK_UP: srv.releaseKey(Keys::Up); break;
srv.setCirclepadY(0); case SDLK_DOWN: srv.releaseKey(Keys::Down); break;
keyboardAnalogY = false;
break;
case SDLK_a: // Err this is probably not ideal
case SDLK_d: case SDLK_w:
srv.setCirclepadX(0); case SDLK_s:
keyboardAnalogX = false; srv.setCirclepadY(0);
break; keyboardAnalogY = false;
break;
case SDLK_RETURN: srv.releaseKey(Keys::Start); break; case SDLK_a:
case SDLK_BACKSPACE: srv.releaseKey(Keys::Select); break; case SDLK_d:
} srv.setCirclepadX(0);
keyboardAnalogX = false;
break;
case SDLK_RETURN: srv.releaseKey(Keys::Start); break;
case SDLK_BACKSPACE: srv.releaseKey(Keys::Select); break;
} }
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
if(romLoaded) { if (romType == ROMType::None) break;
if (event.button.button == SDL_BUTTON_LEFT) {
const s32 x = event.button.x;
const s32 y = event.button.y;
// Check if touch falls in the touch screen area if (event.button.button == SDL_BUTTON_LEFT) {
if (y >= 240 && y <= 480 && x >= 40 && x < 40 + 320) { const s32 x = event.button.x;
// Convert to 3DS coordinates const s32 y = event.button.y;
u16 x_converted = static_cast<u16>(x) - 40;
u16 y_converted = static_cast<u16>(y) - 240;
srv.setTouchScreenPress(x_converted, y_converted); // Check if touch falls in the touch screen area
} else { if (y >= 240 && y <= 480 && x >= 40 && x < 40 + 320) {
srv.releaseTouchScreen(); // Convert to 3DS coordinates
} u16 x_converted = static_cast<u16>(x) - 40;
} else if (event.button.button == SDL_BUTTON_RIGHT) { u16 y_converted = static_cast<u16>(y) - 240;
holdingRightClick = true;
srv.setTouchScreenPress(x_converted, y_converted);
} else {
srv.releaseTouchScreen();
} }
} else if (event.button.button == SDL_BUTTON_RIGHT) {
holdingRightClick = true;
} }
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if(romLoaded) { if (romType == ROMType::None) break;
if (event.button.button == SDL_BUTTON_LEFT) {
srv.releaseTouchScreen(); if (event.button.button == SDL_BUTTON_LEFT) {
} else if (event.button.button == SDL_BUTTON_RIGHT) { srv.releaseTouchScreen();
holdingRightClick = false; } else if (event.button.button == SDL_BUTTON_RIGHT) {
} holdingRightClick = false;
} }
break; break;
@ -239,66 +240,67 @@ void Emulator::run() {
break; break;
case SDL_CONTROLLERBUTTONUP: case SDL_CONTROLLERBUTTONUP:
case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONDOWN: {
if(romLoaded) { if (romType == ROMType::None) break;
u32 key = 0; u32 key = 0;
switch (event.cbutton.button) { switch (event.cbutton.button) {
case SDL_CONTROLLER_BUTTON_A: key = Keys::B; break; case SDL_CONTROLLER_BUTTON_A: key = Keys::B; break;
case SDL_CONTROLLER_BUTTON_B: key = Keys::A; break; case SDL_CONTROLLER_BUTTON_B: key = Keys::A; break;
case SDL_CONTROLLER_BUTTON_X: key = Keys::Y; break; case SDL_CONTROLLER_BUTTON_X: key = Keys::Y; break;
case SDL_CONTROLLER_BUTTON_Y: key = Keys::X; break; case SDL_CONTROLLER_BUTTON_Y: key = Keys::X; break;
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: key = Keys::L; break; case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: key = Keys::L; break;
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: key = Keys::R; break; case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: key = Keys::R; break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT: key = Keys::Left; break; case SDL_CONTROLLER_BUTTON_DPAD_LEFT: key = Keys::Left; break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: key = Keys::Right; break; case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: key = Keys::Right; break;
case SDL_CONTROLLER_BUTTON_DPAD_UP: key = Keys::Up; break; case SDL_CONTROLLER_BUTTON_DPAD_UP: key = Keys::Up; break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN: key = Keys::Down; break; case SDL_CONTROLLER_BUTTON_DPAD_DOWN: key = Keys::Down; break;
case SDL_CONTROLLER_BUTTON_BACK: key = Keys::Select; break; case SDL_CONTROLLER_BUTTON_BACK: key = Keys::Select; break;
case SDL_CONTROLLER_BUTTON_START: key = Keys::Start; break; case SDL_CONTROLLER_BUTTON_START: key = Keys::Start; break;
} }
if (key != 0) { if (key != 0) {
if (event.cbutton.state == SDL_PRESSED) { if (event.cbutton.state == SDL_PRESSED) {
srv.pressKey(key); srv.pressKey(key);
} else { } else {
srv.releaseKey(key); srv.releaseKey(key);
}
} }
} }
break; 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 (romType == ROMType::None || !holdingRightClick) break;
if (!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;
const s32 motionY = event.motion.yrel; const s32 motionY = event.motion.yrel;
// The gyroscope involves lots of weird math I don't want to bother with atm // The gyroscope involves lots of weird math I don't want to bother with atm
// So up until then, we will set the gyroscope euler angles to fixed values based on the direction of the relative motion // So up until then, we will set the gyroscope euler angles to fixed values based on the direction of the relative motion
const s32 roll = motionX > 0 ? 0x7f : -0x7f; const s32 roll = motionX > 0 ? 0x7f : -0x7f;
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; break;
}
case SDL_DROPFILE: { case SDL_DROPFILE: {
char *droppedDir = event.drop.file; char* droppedDir = event.drop.file;
if(droppedDir) {
if (droppedDir) {
loadROM(droppedDir); loadROM(droppedDir);
SDL_free(droppedDir); SDL_free(droppedDir);
} }
break;
} }
break;
} }
} }
if(romLoaded) { // Update controller analog sticks and HID service
if (romType != ROMType::None) {
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);
@ -319,6 +321,7 @@ void Emulator::run() {
srv.setCirclepadY(-(stickY / div)); srv.setCirclepadY(-(stickY / div));
} }
} }
srv.updateInputs(cpu.getTicks()); srv.updateInputs(cpu.getTicks());
} }
@ -330,7 +333,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) {
stop(); // Reset the emulator if we've already loaded a ROM
if (romType != ROMType::None) {
reset();
}
// 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
@ -350,7 +357,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);
@ -370,7 +377,6 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
romType = ROMType::None; romType = ROMType::None;
} }
romLoaded = success;
return success; return success;
} }
@ -421,21 +427,19 @@ bool Emulator::loadELF(std::ifstream& file) {
// 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) {

View file

@ -5,12 +5,14 @@ int main (int argc, char *argv[]) {
emu.initGraphicsContext(); emu.initGraphicsContext();
if(argc > 1) { if (argc > 1) {
auto romPath = std::filesystem::current_path() / argv[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());
} }
} else {
printf("No ROM inserted! Load a ROM by dragging and dropping it into the emulator window!\n");
} }
emu.run(); emu.run();