From 953c9a5c7f7e989692c0d054337d445cb48534b0 Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 24 Apr 2024 23:38:00 -0400 Subject: [PATCH] Add load rom file to emulator ui --- src/app/emu/cpu/cpu.h | 1 - src/app/emu/emulator.cc | 37 +++++++++++++++++++++++++++---------- src/app/emu/emulator.h | 2 ++ src/cli/CMakeLists.txt | 1 + 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/app/emu/cpu/cpu.h b/src/app/emu/cpu/cpu.h index 40738dfc..69c62e24 100644 --- a/src/app/emu/cpu/cpu.h +++ b/src/app/emu/cpu/cpu.h @@ -727,7 +727,6 @@ class Cpu : public Loggable, public core::ExperimentFlags { // ========================================================================== uint16_t SP() const { return memory.SP(); } void SetSP(uint16_t value) { memory.SetSP(value); } - void set_next_pc(uint16_t value) { next_pc_ = value; } bool IsBreakpoint(uint32_t address) { return std::find(breakpoints_.begin(), breakpoints_.end(), address) != diff --git a/src/app/emu/emulator.cc b/src/app/emu/emulator.cc index 4d73f772..b7ca61f5 100644 --- a/src/app/emu/emulator.cc +++ b/src/app/emu/emulator.cc @@ -7,6 +7,7 @@ #include #include "app/core/constants.h" +#include "app/core/platform/file_dialog.h" #include "app/emu/snes.h" #include "app/gui/icons.h" #include "app/gui/input.h" @@ -58,9 +59,10 @@ void Emulator::Run() { printf("Failed to create texture: %s\n", SDL_GetError()); return; } - snes_.Init(*rom()); - wanted_frames_ = 1.0 / 60.0; - wanted_samples_ = 48000 /60; + rom_data_ = rom()->vector(); + snes_.Init(rom_data_); + wanted_frames_ = 1.0 / (snes_.Memory().pal_timing() ? 50.0 : 60.0); + wanted_samples_ = 48000 / (snes_.Memory().pal_timing() ? 50 : 60); loaded = true; countFreq = SDL_GetPerformanceFrequency(); @@ -110,9 +112,9 @@ void Emulator::Run() { } void Emulator::RenderSnesPpu() { - ImVec2 size = ImVec2(320, 480); + ImVec2 size = ImVec2(512, 480); if (snes_.running()) { - ImGui::BeginChild("EmulatorOutput", ImVec2(0, 240), true, + ImGui::BeginChild("EmulatorOutput", ImVec2(0, 480), true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar); ImGui::SetCursorPosX((ImGui::GetWindowSize().x - size.x) * 0.5f); ImGui::SetCursorPosY((ImGui::GetWindowSize().y - size.y) * 0.5f); @@ -121,7 +123,7 @@ void Emulator::RenderSnesPpu() { } else { ImGui::Text("Emulator output not available."); - ImGui::BeginChild("EmulatorOutput", ImVec2(0, 240), true, + ImGui::BeginChild("EmulatorOutput", ImVec2(0, 480), true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar); ImGui::SetCursorPosX(((ImGui::GetWindowSize().x * 0.5f) - size.x) * 0.5f); ImGui::SetCursorPosY(((ImGui::GetWindowSize().y * 0.5f) - size.y) * 0.5f); @@ -141,7 +143,8 @@ void Emulator::RenderNavBar() { } } )"; - auto navbar_node = gui::zeml::Parse(navbar_layout); + + static auto navbar_node = gui::zeml::Parse(navbar_layout); gui::zeml::Render(navbar_node); if (ImGui::Button(ICON_MD_PLAY_ARROW)) { @@ -210,14 +213,16 @@ void Emulator::RenderNavBar() { ImGui::SetTooltip("Settings"); } + static bool open_file = false; SameLine(); if (ImGui::Button(ICON_MD_INFO)) { + open_file = true; + + // About Debugger logic + } if (ImGui::IsItemHovered()) { ImGui::SetTooltip("About Debugger"); } - // About Debugger logic - } - SameLine(); ImGui::Checkbox("Logging", snes_.cpu().mutable_log_instructions()); @@ -239,6 +244,18 @@ void Emulator::RenderNavBar() { RenderMemoryViewer(); ImGui::End(); } + + if (open_file) { + auto file_name = FileDialogWrapper::ShowOpenFileDialog(); + if (!file_name.empty()) { + std::ifstream file(file_name, std::ios::binary); + // Load the data directly into rom_data + rom_data_.assign(std::istreambuf_iterator(file), + std::istreambuf_iterator()); + snes_.Init(rom_data_); + open_file = false; + } + } } void Emulator::HandleEvents() { diff --git a/src/app/emu/emulator.h b/src/app/emu/emulator.h index 324e15e5..da9775ef 100644 --- a/src/app/emu/emulator.h +++ b/src/app/emu/emulator.h @@ -143,6 +143,8 @@ class Emulator : public SharedRom { SNES snes_; SDL_Texture* ppu_texture_; + std::vector rom_data_; + gui::zeml::Node emulator_node_; }; diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index 404bc855..5bafd437 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -8,6 +8,7 @@ add_executable( app/core/labeling.cc app/gui/pipeline.cc app/editor/context/gfx_context.cc + app/core/platform/file_dialog.mm ${YAZE_APP_EMU_SRC} ${YAZE_APP_GFX_SRC} ${YAZE_APP_ZELDA3_SRC}