From 1633955177e17b039ff40fcbb3d0e2c5ee5aaae6 Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 26 Nov 2023 16:48:49 -0500 Subject: [PATCH] Emulator housekeeping --- src/app/emu/emulator.cc | 30 +++++++++++++++++------ src/app/emu/snes.cc | 54 ++++++++++++++++++++--------------------- src/app/emu/snes.h | 14 +++++------ 3 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/app/emu/emulator.cc b/src/app/emu/emulator.cc index 464d5b12..ed865d0b 100644 --- a/src/app/emu/emulator.cc +++ b/src/app/emu/emulator.cc @@ -155,9 +155,9 @@ void Emulator::Run() { if (running_) { HandleEvents(); UpdateEmulator(); - RenderEmulator(); } + RenderEmulator(); if (debugger_) { RenderDebugger(); } @@ -166,11 +166,25 @@ void Emulator::Run() { void Emulator::RenderEmulator() { ImVec2 size = ImVec2(320, 240); if (snes_.running()) { - ImGui::Image((void*)snes_.Ppu().GetScreen()->texture(), size, ImVec2(0, 0), + ImGui::BeginChild( + "EmulatorOutput", ImVec2(0, 0), true, + ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | + ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse); + ImGui::SetCursorPosX((ImGui::GetWindowSize().x - size.x) * 0.5f); + ImGui::SetCursorPosY((ImGui::GetWindowSize().y - size.y) * 0.5f); + ImGui::Image((void*)snes_.ppu().GetScreen()->texture(), size, ImVec2(0, 0), ImVec2(1, 1)); + ImGui::EndChild(); ImGui::Separator(); } else { + ImGui::BeginChild( + "EmulatorOutput", ImVec2(0, 0), true, + ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | + ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse); + ImGui::SetCursorPosX((ImGui::GetWindowSize().x - size.x) * 0.5f); + ImGui::SetCursorPosY((ImGui::GetWindowSize().y - size.y) * 0.5f); ImGui::Dummy(size); + ImGui::EndChild(); ImGui::Separator(); ImGui::Text("Emulator output not available."); } @@ -242,10 +256,10 @@ void Emulator::RenderDebugger() { "DebugTable", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_ScrollY)) { TableNextColumn(); - RenderCpuState(snes_.Cpu()); + RenderCpuState(snes_.cpu()); TableNextColumn(); - RenderCPUInstructionLog(snes_.Cpu().instruction_log_); + RenderCPUInstructionLog(snes_.cpu().instruction_log_); TableNextColumn(); RenderBreakpointList(); @@ -286,21 +300,21 @@ void Emulator::RenderBreakpointList() { if (ImGui::InputText("##BreakpointInput", breakpoint_input, 10, ImGuiInputTextFlags_EnterReturnsTrue)) { int breakpoint = std::stoi(breakpoint_input, nullptr, 16); - snes_.Cpu().SetBreakpoint(breakpoint); + snes_.cpu().SetBreakpoint(breakpoint); memset(breakpoint_input, 0, sizeof(breakpoint_input)); } SameLine(); if (ImGui::Button("Add")) { int breakpoint = std::stoi(breakpoint_input, nullptr, 16); - snes_.Cpu().SetBreakpoint(breakpoint); + snes_.cpu().SetBreakpoint(breakpoint); memset(breakpoint_input, 0, sizeof(breakpoint_input)); } SameLine(); if (ImGui::Button("Clear")) { - snes_.Cpu().ClearBreakpoints(); + snes_.cpu().ClearBreakpoints(); } Separator(); - auto breakpoints = snes_.Cpu().GetBreakpoints(); + auto breakpoints = snes_.cpu().GetBreakpoints(); if (!breakpoints.empty()) { Text("Breakpoints:"); ImGui::BeginChild("BreakpointsList", ImVec2(0, 100), true); diff --git a/src/app/emu/snes.cc b/src/app/emu/snes.cc index af26dc06..3b07221b 100644 --- a/src/app/emu/snes.cc +++ b/src/app/emu/snes.cc @@ -64,45 +64,45 @@ ROMInfo SNES::ReadRomHeader(uint32_t offset) { // Read cartridge title char title[22]; for (int i = 0; i < 21; ++i) { - title[i] = cpu.ReadByte(offset + i); + title[i] = cpu_.ReadByte(offset + i); } title[21] = '\0'; // Null-terminate the string romInfo.title = std::string(title); // Read ROM speed and memory map mode - uint8_t romSpeedAndMapMode = cpu.ReadByte(offset + 0x15); + uint8_t romSpeedAndMapMode = cpu_.ReadByte(offset + 0x15); romInfo.romSpeed = (ROMSpeed)(romSpeedAndMapMode & 0x07); romInfo.bankSize = (BankSize)((romSpeedAndMapMode >> 5) & 0x01); // Read ROM type - romInfo.romType = (ROMType)cpu.ReadByte(offset + 0x16); + romInfo.romType = (ROMType)cpu_.ReadByte(offset + 0x16); // Read ROM size - romInfo.romSize = (ROMSize)cpu.ReadByte(offset + 0x17); + romInfo.romSize = (ROMSize)cpu_.ReadByte(offset + 0x17); // Read RAM size - romInfo.sramSize = (SRAMSize)cpu.ReadByte(offset + 0x18); + romInfo.sramSize = (SRAMSize)cpu_.ReadByte(offset + 0x18); // Read country code - romInfo.countryCode = (CountryCode)cpu.ReadByte(offset + 0x19); + romInfo.countryCode = (CountryCode)cpu_.ReadByte(offset + 0x19); // Read license - romInfo.license = (License)cpu.ReadByte(offset + 0x1A); + romInfo.license = (License)cpu_.ReadByte(offset + 0x1A); // Read ROM version - romInfo.version = cpu.ReadByte(offset + 0x1B); + romInfo.version = cpu_.ReadByte(offset + 0x1B); // Read checksum complement - romInfo.checksumComplement = cpu.ReadWord(offset + 0x1E); + romInfo.checksumComplement = cpu_.ReadWord(offset + 0x1E); // Read checksum - romInfo.checksum = cpu.ReadWord(offset + 0x1C); + romInfo.checksum = cpu_.ReadWord(offset + 0x1C); // Read NMI VBL vector - romInfo.nmiVblVector = cpu.ReadWord(offset + 0x3E); + romInfo.nmiVblVector = cpu_.ReadWord(offset + 0x3E); // Read reset vector - romInfo.resetVector = cpu.ReadWord(offset + 0x3C); + romInfo.resetVector = cpu_.ReadWord(offset + 0x3C); return romInfo; } @@ -112,18 +112,18 @@ void SNES::Init(ROM& rom) { // Disable the emulation flag (switch to 65816 native mode) // Initialize CPU - cpu.Init(); + cpu_.Init(); // Read the ROM header auto header_offset = GetHeaderOffset(memory_); rom_info_ = ReadRomHeader(header_offset); - cpu.PC = rom_info_.resetVector; + cpu_.PC = rom_info_.resetVector; // Initialize PPU - ppu.Init(); + ppu_.Init(); // Initialize APU - apu.Init(); + apu_.Init(); // Initialize SDL_Mixer to play the audio samples // Mix_HookMusic(audio_callback, &apu); @@ -232,16 +232,16 @@ void SNES::Run() { frame_accumulated_time += delta_time; // Update the CPU - cpu.UpdateClock(delta_time); - cpu.Update(GetCpuMode()); + cpu_.UpdateClock(delta_time); + cpu_.Update(GetCpuMode()); // Update the PPU - ppu.UpdateClock(delta_time); - ppu.Update(); + ppu_.UpdateClock(delta_time); + ppu_.Update(); // Update the APU - apu.UpdateClock(delta_time); - apu.Update(); + apu_.UpdateClock(delta_time); + apu_.Update(); if (frame_accumulated_time >= frame_time) { // renderer.Render(); @@ -279,11 +279,11 @@ void SNES::NmiIsr() { // ... // Push CPU registers to stack - cpu.PHP(); + cpu_.PHP(); // Reset DB and DP registers - cpu.DB = 0x80; // Assuming bank 0x80, can be changed to 0x00 - cpu.D = 0; + cpu_.DB = 0x80; // Assuming bank 0x80, can be changed to 0x00 + cpu_.D = 0; if (v_blank_flag_) { VBlankRoutine(); @@ -296,7 +296,7 @@ void SNES::NmiIsr() { frame_counter_++; // Restore CPU registers - cpu.PHB(); + cpu_.PHB(); } // VBlank routine @@ -307,7 +307,7 @@ void SNES::VBlankRoutine() { void SNES::BootAPUWithIPL() { // 1. Waiting for the SPC700 to be ready - while (!apu.IsReadySignalReceived()) { + while (!apu_.IsReadySignalReceived()) { // Active waiting (this can be optimized) } diff --git a/src/app/emu/snes.h b/src/app/emu/snes.h index a218e0a3..03fd5248 100644 --- a/src/app/emu/snes.h +++ b/src/app/emu/snes.h @@ -63,8 +63,8 @@ class SNES : public DMA { bool running() const { return running_; } - auto Cpu() -> CPU& { return cpu; } - auto Ppu() -> PPU& { return ppu; } + auto cpu() -> CPU& { return cpu_; } + auto ppu() -> Ppu& { return ppu_; } auto Memory() -> MemoryImpl* { return &memory_; } void SetCpuMode(int mode) { cpu_mode_ = mode; } @@ -74,8 +74,8 @@ class SNES : public DMA { void SetupMemory(ROM& rom) { // Setup observers for the memory space - memory_.AddObserver(&apu); - memory_.AddObserver(&ppu); + memory_.AddObserver(&apu_); + memory_.AddObserver(&ppu_); // Load the ROM into memory and set up the memory mapping memory_.Initialize(rom.vector()); @@ -91,9 +91,9 @@ class SNES : public DMA { ClockImpl clock_; AudioRamImpl audio_ram_; - CPU cpu{memory_, clock_}; - PPU ppu{memory_, clock_}; - APU apu{memory_, audio_ram_, clock_}; + CPU cpu_{memory_, clock_}; + Ppu ppu_{memory_, clock_}; + APU apu_{memory_, audio_ram_, clock_}; // Helper classes ROMInfo rom_info_;