From d2a616991468a9de68ea56919d8fc81bae84f870 Mon Sep 17 00:00:00 2001 From: scawful Date: Mon, 30 Dec 2024 07:50:30 -0500 Subject: [PATCH] Refactor SNES class: rename to Snes for consistency, update method names, and clean up includes --- src/app/emu/snes.cc | 74 ++++++++++++++++++++------------------------- src/app/emu/snes.h | 21 ++++++------- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/src/app/emu/snes.cc b/src/app/emu/snes.cc index 19ea3c20..dd43f2eb 100644 --- a/src/app/emu/snes.cc +++ b/src/app/emu/snes.cc @@ -1,18 +1,11 @@ #include "app/emu/snes.h" #include -#include -#include -#include #include "app/emu/audio/apu.h" -#include "app/emu/audio/spc700.h" -#include "app/emu/cpu/clock.h" -#include "app/emu/cpu/cpu.h" #include "app/emu/memory/dma.h" #include "app/emu/memory/memory.h" #include "app/emu/video/ppu.h" -#include "app/rom.h" namespace yaze { namespace emu { @@ -32,7 +25,7 @@ uint8_t input_read(Input* input) { } } // namespace -void SNES::Init(std::vector& rom_data) { +void Snes::Init(std::vector& rom_data) { // Initialize the CPU, PPU, and APU ppu_.Init(); apu_.Init(); @@ -44,11 +37,11 @@ void SNES::Init(std::vector& rom_data) { running_ = true; } -void SNES::Reset(bool hard) { +void Snes::Reset(bool hard) { cpu_.Reset(hard); apu_.Reset(); ppu_.Reset(); - memory::dma::Reset(&memory_); + ResetDma(&memory_); input1.latch_line_ = false; input2.latch_line_ = false; input1.latched_state_ = 0; @@ -84,7 +77,7 @@ void SNES::Reset(bool hard) { InitAccessTime(false); } -void SNES::RunFrame() { +void Snes::RunFrame() { while (in_vblank_) { cpu_.RunOpcode(); } @@ -94,9 +87,9 @@ void SNES::RunFrame() { } } -void SNES::CatchUpApu() { apu_.RunCycles(cycles_); } +void Snes::CatchUpApu() { apu_.RunCycles(cycles_); } -void SNES::HandleInput() { +void Snes::HandleInput() { memset(port_auto_read_, 0, sizeof(port_auto_read_)); // latch controllers input_latch(&input1, true); @@ -113,7 +106,7 @@ void SNES::HandleInput() { } } -void SNES::RunCycle() { +void Snes::RunCycle() { cycles_ += 2; // check for h/v timer irq's @@ -228,7 +221,7 @@ void SNES::RunCycle() { if (auto_joy_timer_ > 0) auto_joy_timer_ -= 2; } -void SNES::RunCycles(int cycles) { +void Snes::RunCycles(int cycles) { if (memory_.h_pos() + cycles >= 536 && memory_.h_pos() < 536) { // if we go past 536, add 40 cycles for dram refersh cycles += 40; @@ -238,7 +231,7 @@ void SNES::RunCycles(int cycles) { } } -void SNES::SyncCycles(bool start, int sync_cycles) { +void Snes::SyncCycles(bool start, int sync_cycles) { int count = 0; if (start) { sync_cycle_ = cycles_; @@ -249,7 +242,7 @@ void SNES::SyncCycles(bool start, int sync_cycles) { RunCycles(count); } -uint8_t SNES::ReadBBus(uint8_t adr) { +uint8_t Snes::ReadBBus(uint8_t adr) { if (adr < 0x40) { return ppu_.Read(adr, ppu_latch_); } @@ -265,7 +258,7 @@ uint8_t SNES::ReadBBus(uint8_t adr) { return memory_.open_bus(); } -uint8_t SNES::ReadReg(uint16_t adr) { +uint8_t Snes::ReadReg(uint16_t adr) { switch (adr) { case 0x4210: { uint8_t val = 0x2; // CPU version (4 bit) @@ -318,7 +311,7 @@ uint8_t SNES::ReadReg(uint16_t adr) { } } -uint8_t SNES::Rread(uint32_t adr) { +uint8_t Snes::Rread(uint32_t adr) { uint8_t bank = adr >> 16; adr &= 0xffff; if (bank == 0x7e || bank == 0x7f) { @@ -341,20 +334,20 @@ uint8_t SNES::Rread(uint32_t adr) { return ReadReg(adr); // internal registers } if (adr >= 0x4300 && adr < 0x4380) { - return memory::dma::Read(&memory_, adr); // dma registers + return ReadDma(&memory_, adr); // dma registers } } // read from cart return memory_.cart_read(bank, adr); } -uint8_t SNES::Read(uint32_t adr) { +uint8_t Snes::Read(uint32_t adr) { uint8_t val = Rread(adr); memory_.set_open_bus(val); return val; } -void SNES::WriteBBus(uint8_t adr, uint8_t val) { +void Snes::WriteBBus(uint8_t adr, uint8_t val) { if (adr < 0x40) { ppu_.Write(adr, val); return; @@ -385,7 +378,7 @@ void SNES::WriteBBus(uint8_t adr, uint8_t val) { } } -void SNES::WriteReg(uint16_t adr, uint8_t val) { +void Snes::WriteReg(uint16_t adr, uint8_t val) { switch (adr) { case 0x4200: { auto_joy_read_ = val & 0x1; @@ -455,11 +448,11 @@ void SNES::WriteReg(uint16_t adr, uint8_t val) { break; } case 0x420b: { - memory::dma::StartDma(&memory_, val, false); + StartDma(&memory_, val, false); break; } case 0x420c: { - memory::dma::StartDma(&memory_, val, true); + StartDma(&memory_, val, true); break; } case 0x420d: { @@ -472,7 +465,7 @@ void SNES::WriteReg(uint16_t adr, uint8_t val) { } } -void SNES::Write(uint32_t adr, uint8_t val) { +void Snes::Write(uint32_t adr, uint8_t val) { memory_.set_open_bus(val); uint8_t bank = adr >> 16; adr &= 0xffff; @@ -494,7 +487,7 @@ void SNES::Write(uint32_t adr, uint8_t val) { WriteReg(adr, val); // internal registers } if (adr >= 0x4300 && adr < 0x4380) { - memory::dma::Write(&memory_, adr, val); // dma registers + WriteDma(&memory_, adr, val); // dma registers } } @@ -502,7 +495,7 @@ void SNES::Write(uint32_t adr, uint8_t val) { memory_.cart_write(bank, adr, val); } -int SNES::GetAccessTime(uint32_t adr) { +int Snes::GetAccessTime(uint32_t adr) { uint8_t bank = adr >> 16; adr &= 0xffff; if ((bank < 0x40 || (bank >= 0x80 && bank < 0xc0)) && adr < 0x8000) { @@ -516,38 +509,38 @@ int SNES::GetAccessTime(uint32_t adr) { : 8; // depends on setting in banks 80+ } -uint8_t SNES::CpuRead(uint32_t adr) { +uint8_t Snes::CpuRead(uint32_t adr) { cpu_.set_int_delay(false); const int cycles = access_time[adr] - 4; - memory::dma::HandleDma(this, &memory_, cycles); + HandleDma(this, &memory_, cycles); RunCycles(cycles); uint8_t rv = Read(adr); - memory::dma::HandleDma(this, &memory_, 4); + HandleDma(this, &memory_, 4); RunCycles(4); return rv; } -void SNES::CpuWrite(uint32_t adr, uint8_t val) { +void Snes::CpuWrite(uint32_t adr, uint8_t val) { cpu_.set_int_delay(false); const int cycles = access_time[adr]; - memory::dma::HandleDma(this, &memory_, cycles); + HandleDma(this, &memory_, cycles); RunCycles(cycles); Write(adr, val); } -void SNES::CpuIdle(bool waiting) { +void Snes::CpuIdle(bool waiting) { cpu_.set_int_delay(false); - memory::dma::HandleDma(this, &memory_, 6); + HandleDma(this, &memory_, 6); RunCycles(6); } -void SNES::SetSamples(int16_t* sample_data, int wanted_samples) { +void Snes::SetSamples(int16_t* sample_data, int wanted_samples) { apu_.dsp().GetSamples(sample_data, wanted_samples, memory_.pal_timing()); } -void SNES::SetPixels(uint8_t* pixel_data) { ppu_.PutPixels(pixel_data); } +void Snes::SetPixels(uint8_t* pixel_data) { ppu_.PutPixels(pixel_data); } -void SNES::SetButtonState(int player, int button, bool pressed) { +void Snes::SetButtonState(int player, int button, bool pressed) { // set key in controller if (player == 1) { if (pressed) { @@ -564,7 +557,7 @@ void SNES::SetButtonState(int player, int button, bool pressed) { } } -void SNES::InitAccessTime(bool recalc) { +void Snes::InitAccessTime(bool recalc) { int start = (recalc) ? 0x800000 : 0; // recalc only updates fast rom access_time.resize(0x1000000); for (int i = start; i < 0x1000000; i++) { @@ -573,5 +566,4 @@ void SNES::InitAccessTime(bool recalc) { } } // namespace emu - -} // namespace yaze \ No newline at end of file +} // namespace yaze diff --git a/src/app/emu/snes.h b/src/app/emu/snes.h index 2ca4690f..993c5338 100644 --- a/src/app/emu/snes.h +++ b/src/app/emu/snes.h @@ -14,17 +14,16 @@ namespace emu { struct Input { uint8_t type; - // latchline bool latch_line_; // for controller uint16_t current_state_; // actual state uint16_t latched_state_; }; -class SNES { +class Snes { public: - SNES() = default; - ~SNES() = default; + Snes() = default; + ~Snes() = default; // Initialization void Init(std::vector& rom_data); @@ -61,9 +60,9 @@ class SNES { void SetButtonState(int player, int button, bool pressed); bool running() const { return running_; } auto cpu() -> Cpu& { return cpu_; } - auto ppu() -> video::Ppu& { return ppu_; } - auto apu() -> audio::Apu& { return apu_; } - auto Memory() -> memory::MemoryImpl& { return memory_; } + auto ppu() -> Ppu& { return ppu_; } + auto apu() -> Apu& { return apu_; } + auto Memory() -> MemoryImpl& { return memory_; } auto get_ram() -> uint8_t* { return ram; } auto mutable_cycles() -> uint64_t& { return cycles_; } void InitAccessTime(bool recalc); @@ -73,16 +72,16 @@ class SNES { private: // Components of the SNES ClockImpl clock_; - memory::MemoryImpl memory_; + MemoryImpl memory_; - memory::CpuCallbacks cpu_callbacks_ = { + CpuCallbacks cpu_callbacks_ = { [&](uint32_t adr) { return CpuRead(adr); }, [&](uint32_t adr, uint8_t val) { CpuWrite(adr, val); }, [&](bool waiting) { CpuIdle(waiting); }, }; Cpu cpu_{memory_, clock_, cpu_callbacks_}; - video::Ppu ppu_{memory_, clock_}; - audio::Apu apu_{memory_}; + Ppu ppu_{memory_, clock_}; + Apu apu_{memory_}; // Currently loaded ROM std::vector rom_data;