Refactor SNES class: rename to Snes for consistency, update method names, and clean up includes

This commit is contained in:
scawful
2024-12-30 07:50:30 -05:00
parent b904cfd8a4
commit d2a6169914
2 changed files with 43 additions and 52 deletions

View File

@@ -1,18 +1,11 @@
#include "app/emu/snes.h" #include "app/emu/snes.h"
#include <cstdint> #include <cstdint>
#include <memory>
#include <string>
#include <thread>
#include "app/emu/audio/apu.h" #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/dma.h"
#include "app/emu/memory/memory.h" #include "app/emu/memory/memory.h"
#include "app/emu/video/ppu.h" #include "app/emu/video/ppu.h"
#include "app/rom.h"
namespace yaze { namespace yaze {
namespace emu { namespace emu {
@@ -32,7 +25,7 @@ uint8_t input_read(Input* input) {
} }
} // namespace } // namespace
void SNES::Init(std::vector<uint8_t>& rom_data) { void Snes::Init(std::vector<uint8_t>& rom_data) {
// Initialize the CPU, PPU, and APU // Initialize the CPU, PPU, and APU
ppu_.Init(); ppu_.Init();
apu_.Init(); apu_.Init();
@@ -44,11 +37,11 @@ void SNES::Init(std::vector<uint8_t>& rom_data) {
running_ = true; running_ = true;
} }
void SNES::Reset(bool hard) { void Snes::Reset(bool hard) {
cpu_.Reset(hard); cpu_.Reset(hard);
apu_.Reset(); apu_.Reset();
ppu_.Reset(); ppu_.Reset();
memory::dma::Reset(&memory_); ResetDma(&memory_);
input1.latch_line_ = false; input1.latch_line_ = false;
input2.latch_line_ = false; input2.latch_line_ = false;
input1.latched_state_ = 0; input1.latched_state_ = 0;
@@ -84,7 +77,7 @@ void SNES::Reset(bool hard) {
InitAccessTime(false); InitAccessTime(false);
} }
void SNES::RunFrame() { void Snes::RunFrame() {
while (in_vblank_) { while (in_vblank_) {
cpu_.RunOpcode(); 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_)); memset(port_auto_read_, 0, sizeof(port_auto_read_));
// latch controllers // latch controllers
input_latch(&input1, true); input_latch(&input1, true);
@@ -113,7 +106,7 @@ void SNES::HandleInput() {
} }
} }
void SNES::RunCycle() { void Snes::RunCycle() {
cycles_ += 2; cycles_ += 2;
// check for h/v timer irq's // check for h/v timer irq's
@@ -228,7 +221,7 @@ void SNES::RunCycle() {
if (auto_joy_timer_ > 0) auto_joy_timer_ -= 2; 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 (memory_.h_pos() + cycles >= 536 && memory_.h_pos() < 536) {
// if we go past 536, add 40 cycles for dram refersh // if we go past 536, add 40 cycles for dram refersh
cycles += 40; 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; int count = 0;
if (start) { if (start) {
sync_cycle_ = cycles_; sync_cycle_ = cycles_;
@@ -249,7 +242,7 @@ void SNES::SyncCycles(bool start, int sync_cycles) {
RunCycles(count); RunCycles(count);
} }
uint8_t SNES::ReadBBus(uint8_t adr) { uint8_t Snes::ReadBBus(uint8_t adr) {
if (adr < 0x40) { if (adr < 0x40) {
return ppu_.Read(adr, ppu_latch_); return ppu_.Read(adr, ppu_latch_);
} }
@@ -265,7 +258,7 @@ uint8_t SNES::ReadBBus(uint8_t adr) {
return memory_.open_bus(); return memory_.open_bus();
} }
uint8_t SNES::ReadReg(uint16_t adr) { uint8_t Snes::ReadReg(uint16_t adr) {
switch (adr) { switch (adr) {
case 0x4210: { case 0x4210: {
uint8_t val = 0x2; // CPU version (4 bit) 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; uint8_t bank = adr >> 16;
adr &= 0xffff; adr &= 0xffff;
if (bank == 0x7e || bank == 0x7f) { if (bank == 0x7e || bank == 0x7f) {
@@ -341,20 +334,20 @@ uint8_t SNES::Rread(uint32_t adr) {
return ReadReg(adr); // internal registers return ReadReg(adr); // internal registers
} }
if (adr >= 0x4300 && adr < 0x4380) { if (adr >= 0x4300 && adr < 0x4380) {
return memory::dma::Read(&memory_, adr); // dma registers return ReadDma(&memory_, adr); // dma registers
} }
} }
// read from cart // read from cart
return memory_.cart_read(bank, adr); 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); uint8_t val = Rread(adr);
memory_.set_open_bus(val); memory_.set_open_bus(val);
return val; return val;
} }
void SNES::WriteBBus(uint8_t adr, uint8_t val) { void Snes::WriteBBus(uint8_t adr, uint8_t val) {
if (adr < 0x40) { if (adr < 0x40) {
ppu_.Write(adr, val); ppu_.Write(adr, val);
return; 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) { switch (adr) {
case 0x4200: { case 0x4200: {
auto_joy_read_ = val & 0x1; auto_joy_read_ = val & 0x1;
@@ -455,11 +448,11 @@ void SNES::WriteReg(uint16_t adr, uint8_t val) {
break; break;
} }
case 0x420b: { case 0x420b: {
memory::dma::StartDma(&memory_, val, false); StartDma(&memory_, val, false);
break; break;
} }
case 0x420c: { case 0x420c: {
memory::dma::StartDma(&memory_, val, true); StartDma(&memory_, val, true);
break; break;
} }
case 0x420d: { 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); memory_.set_open_bus(val);
uint8_t bank = adr >> 16; uint8_t bank = adr >> 16;
adr &= 0xffff; adr &= 0xffff;
@@ -494,7 +487,7 @@ void SNES::Write(uint32_t adr, uint8_t val) {
WriteReg(adr, val); // internal registers WriteReg(adr, val); // internal registers
} }
if (adr >= 0x4300 && adr < 0x4380) { 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); memory_.cart_write(bank, adr, val);
} }
int SNES::GetAccessTime(uint32_t adr) { int Snes::GetAccessTime(uint32_t adr) {
uint8_t bank = adr >> 16; uint8_t bank = adr >> 16;
adr &= 0xffff; adr &= 0xffff;
if ((bank < 0x40 || (bank >= 0x80 && bank < 0xc0)) && adr < 0x8000) { 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+ : 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); cpu_.set_int_delay(false);
const int cycles = access_time[adr] - 4; const int cycles = access_time[adr] - 4;
memory::dma::HandleDma(this, &memory_, cycles); HandleDma(this, &memory_, cycles);
RunCycles(cycles); RunCycles(cycles);
uint8_t rv = Read(adr); uint8_t rv = Read(adr);
memory::dma::HandleDma(this, &memory_, 4); HandleDma(this, &memory_, 4);
RunCycles(4); RunCycles(4);
return rv; 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); cpu_.set_int_delay(false);
const int cycles = access_time[adr]; const int cycles = access_time[adr];
memory::dma::HandleDma(this, &memory_, cycles); HandleDma(this, &memory_, cycles);
RunCycles(cycles); RunCycles(cycles);
Write(adr, val); Write(adr, val);
} }
void SNES::CpuIdle(bool waiting) { void Snes::CpuIdle(bool waiting) {
cpu_.set_int_delay(false); cpu_.set_int_delay(false);
memory::dma::HandleDma(this, &memory_, 6); HandleDma(this, &memory_, 6);
RunCycles(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()); 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 // set key in controller
if (player == 1) { if (player == 1) {
if (pressed) { 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 int start = (recalc) ? 0x800000 : 0; // recalc only updates fast rom
access_time.resize(0x1000000); access_time.resize(0x1000000);
for (int i = start; i < 0x1000000; i++) { for (int i = start; i < 0x1000000; i++) {
@@ -573,5 +566,4 @@ void SNES::InitAccessTime(bool recalc) {
} }
} // namespace emu } // namespace emu
} // namespace yaze
} // namespace yaze

View File

@@ -14,17 +14,16 @@ namespace emu {
struct Input { struct Input {
uint8_t type; uint8_t type;
// latchline
bool latch_line_; bool latch_line_;
// for controller // for controller
uint16_t current_state_; // actual state uint16_t current_state_; // actual state
uint16_t latched_state_; uint16_t latched_state_;
}; };
class SNES { class Snes {
public: public:
SNES() = default; Snes() = default;
~SNES() = default; ~Snes() = default;
// Initialization // Initialization
void Init(std::vector<uint8_t>& rom_data); void Init(std::vector<uint8_t>& rom_data);
@@ -61,9 +60,9 @@ class SNES {
void SetButtonState(int player, int button, bool pressed); void SetButtonState(int player, int button, bool pressed);
bool running() const { return running_; } bool running() const { return running_; }
auto cpu() -> Cpu& { return cpu_; } auto cpu() -> Cpu& { return cpu_; }
auto ppu() -> video::Ppu& { return ppu_; } auto ppu() -> Ppu& { return ppu_; }
auto apu() -> audio::Apu& { return apu_; } auto apu() -> Apu& { return apu_; }
auto Memory() -> memory::MemoryImpl& { return memory_; } auto Memory() -> MemoryImpl& { return memory_; }
auto get_ram() -> uint8_t* { return ram; } auto get_ram() -> uint8_t* { return ram; }
auto mutable_cycles() -> uint64_t& { return cycles_; } auto mutable_cycles() -> uint64_t& { return cycles_; }
void InitAccessTime(bool recalc); void InitAccessTime(bool recalc);
@@ -73,16 +72,16 @@ class SNES {
private: private:
// Components of the SNES // Components of the SNES
ClockImpl clock_; ClockImpl clock_;
memory::MemoryImpl memory_; MemoryImpl memory_;
memory::CpuCallbacks cpu_callbacks_ = { CpuCallbacks cpu_callbacks_ = {
[&](uint32_t adr) { return CpuRead(adr); }, [&](uint32_t adr) { return CpuRead(adr); },
[&](uint32_t adr, uint8_t val) { CpuWrite(adr, val); }, [&](uint32_t adr, uint8_t val) { CpuWrite(adr, val); },
[&](bool waiting) { CpuIdle(waiting); }, [&](bool waiting) { CpuIdle(waiting); },
}; };
Cpu cpu_{memory_, clock_, cpu_callbacks_}; Cpu cpu_{memory_, clock_, cpu_callbacks_};
video::Ppu ppu_{memory_, clock_}; Ppu ppu_{memory_, clock_};
audio::Apu apu_{memory_}; Apu apu_{memory_};
// Currently loaded ROM // Currently loaded ROM
std::vector<uint8_t> rom_data; std::vector<uint8_t> rom_data;