From aaf972453113aa78931b104537096fafcc32241c Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 24 Apr 2024 12:32:09 -0400 Subject: [PATCH] Move Apu cycling from SNES to Apu class --- src/app/emu/audio/apu.cc | 16 ++++++++-------- src/app/emu/audio/apu.h | 6 +++--- src/app/emu/snes.cc | 9 +-------- src/app/emu/snes.h | 1 + 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/app/emu/audio/apu.cc b/src/app/emu/audio/apu.cc index 79704e35..1be425aa 100644 --- a/src/app/emu/audio/apu.cc +++ b/src/app/emu/audio/apu.cc @@ -17,6 +17,9 @@ namespace app { namespace emu { namespace audio { +static const double apuCyclesPerMaster = (32040 * 32) / (1364 * 262 * 60.0); +static const double apuCyclesPerMasterPal = (32040 * 32) / (1364 * 312 * 50.0); + static const uint8_t bootRom[0x40] = { 0xcd, 0xef, 0xbd, 0xe8, 0x00, 0xc6, 0x1d, 0xd0, 0xfc, 0x8f, 0xaa, 0xf4, 0x8f, 0xbb, 0xf5, 0x78, 0xcc, 0xf4, 0xd0, 0xfb, 0x2f, 0x19, @@ -59,17 +62,14 @@ void Apu::Update() { } } -int Apu::RunCycles(uint32_t wanted_cycles) { - int run_cycles = 0; - uint32_t start_cycles = cycles_; +void Apu::RunCycles(uint64_t cycles) { + uint64_t sync_to = + (uint64_t)cycles * + (memory_.pal_timing() ? apuCyclesPerMasterPal : apuCyclesPerMaster); - while (run_cycles < wanted_cycles) { + while (cycles_ < sync_to) { spc700_.RunOpcode(); - - run_cycles += (uint32_t)cycles_ - start_cycles; - start_cycles = cycles_; } - return run_cycles; } void Apu::Cycle() { diff --git a/src/app/emu/audio/apu.h b/src/app/emu/audio/apu.h index 0f37bf49..d3bb7857 100644 --- a/src/app/emu/audio/apu.h +++ b/src/app/emu/audio/apu.h @@ -56,14 +56,13 @@ typedef struct Timer { */ class Apu { public: - Apu(MemoryImpl &memory, Clock &clock) - : clock_(clock), memory_(memory) {} + Apu(MemoryImpl &memory, Clock &clock) : clock_(clock), memory_(memory) {} void Init(); void Reset(); void Update(); - int RunCycles(uint32_t wanted_cycles); + void RunCycles(uint64_t cycles); uint8_t SpcRead(uint16_t address); void SpcWrite(uint16_t address, uint8_t data); void SpcIdle(bool waiting); @@ -76,6 +75,7 @@ class Apu { void UpdateClock(int delta_time) { clock_.UpdateClock(delta_time); } auto dsp() -> Dsp & { return dsp_; } + auto spc700() -> Spc700 & { return spc700_; } // Port buffers (equivalent to $2140 to $2143 for the main CPU) uint8_t in_ports_[6]; // includes 2 bytes of ram diff --git a/src/app/emu/snes.cc b/src/app/emu/snes.cc index 507447d8..90dbe245 100644 --- a/src/app/emu/snes.cc +++ b/src/app/emu/snes.cc @@ -87,18 +87,13 @@ void SNES::RunFrame() { while (!in_vblank_ && frame == frames_) { cpu_.RunOpcode(); } - CatchUpApu(); } void SNES::CatchUpApu() { - int catchup_cycles = (int)apu_catchup_cycles_; - int ran_cycles = apu_.RunCycles(catchup_cycles); - apu_catchup_cycles_ -= ran_cycles; + apu_.RunCycles(cycles_); } namespace { -static const double apuCyclesPerMaster = (32040 * 32) / (1364 * 262 * 60.0); -static const double apuCyclesPerMasterPal = (32040 * 32) / (1364 * 312 * 50.0); void input_latch(Input* input, bool value) { input->latchLine = value; @@ -132,8 +127,6 @@ void SNES::HandleInput() { } void SNES::RunCycle() { - apu_catchup_cycles_ += - (memory_.pal_timing() ? apuCyclesPerMasterPal : apuCyclesPerMaster) * 2.0; cycles_ += 2; // check for h/v timer irq's diff --git a/src/app/emu/snes.h b/src/app/emu/snes.h index dd84b318..c0728ded 100644 --- a/src/app/emu/snes.h +++ b/src/app/emu/snes.h @@ -69,6 +69,7 @@ class SNES { 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_; } private: