yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
apu.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EMU_APU_H_
2#define YAZE_APP_EMU_APU_H_
3
4#include <array>
5#include <cstdint>
6#include <vector>
7
8#include "app/emu/audio/dsp.h"
11
12namespace yaze {
13namespace emu {
14
15// Forward declaration
16namespace debug {
18}
19
20typedef struct Timer {
21 uint8_t cycles;
22 uint8_t divider;
23 uint8_t target;
24 uint8_t counter;
25 bool enabled;
27
53class Apu {
54 public:
55 Apu(MemoryImpl& memory) : memory_(memory) {}
56
57 void Init();
58 void Reset();
59
60 void RunCycles(uint64_t cycles);
61
62 void SaveState(std::ostream& stream);
63 void LoadState(std::istream& stream);
64
65 uint8_t SpcRead(uint16_t address);
66 void SpcWrite(uint16_t address, uint8_t data);
67 void SpcIdle(bool waiting);
68
69 void Cycle();
70
71 uint8_t Read(uint16_t address);
72 void Write(uint16_t address, uint8_t data);
73
74 auto dsp() -> Dsp& { return dsp_; }
75 auto dsp() const -> const Dsp& { return dsp_; }
76 auto spc700() -> Spc700& { return spc700_; }
77
78 uint64_t GetCycles() const { return cycles_; }
79
80 // Audio debugging
84 uint8_t GetStatus() const { return ram[0x00]; }
85 uint8_t GetControl() const { return ram[0x01]; }
86 void GetSamples(int16_t* buffer, int count, bool loop = false) {
87 dsp_.GetSamples(buffer, count, loop);
88 }
89 void WriteDma(uint16_t address, const uint8_t* data, int count) {
90 for (int i = 0; i < count; i++) {
91 ram[address + i] = data[i];
92 }
93 }
94
108 void BootstrapDirect(uint16_t entry_point);
109
114 bool IsDriverRunning() const { return !rom_readable_; }
115
120 const Timer& GetTimer(int timer_index) const {
121 if (timer_index < 0) timer_index = 0;
122 if (timer_index > 2) timer_index = 2;
123 return timer_[timer_index];
124 }
125
130 void WriteToDsp(uint8_t address, uint8_t value) {
131 if (address < 0x80) {
132 dsp_.Write(address, value);
133 }
134 }
135
136 // Port buffers (equivalent to $2140 to $2143 for the main CPU)
137 std::array<uint8_t, 6> in_ports_; // includes 2 bytes of ram
138 std::array<uint8_t, 4> out_ports_;
139 std::vector<uint8_t> ram = std::vector<uint8_t>(0x10000, 0);
140
141 private:
142 bool rom_readable_ = false;
143
144 uint8_t dsp_adr_ = 0;
145 uint64_t cycles_ = 0;
147
148 // IPL ROM transfer tracking for proper termination
149 uint8_t transfer_size_ = 0;
150 bool in_transfer_ = false;
151
153 std::array<Timer, 3> timer_;
154
155 // Audio debugging
157
159 [&](uint16_t adr, uint8_t val) { SpcWrite(adr, val); },
160 [&](uint16_t adr) { return SpcRead(adr); },
161 [&](bool waiting) { SpcIdle(waiting); },
162 };
165};
166
167} // namespace emu
168} // namespace yaze
169
170#endif
The Apu class represents the Audio Processing Unit (APU) of a system.
Definition apu.h:53
bool in_transfer_
Definition apu.h:150
std::array< Timer, 3 > timer_
Definition apu.h:153
uint8_t SpcRead(uint16_t address)
Definition apu.cc:412
uint8_t Read(uint16_t address)
Definition apu.cc:253
void WriteToDsp(uint8_t address, uint8_t value)
Write directly to DSP register Used for direct instrument/note preview without going through driver.
Definition apu.h:130
ApuCallbacks callbacks_
Definition apu.h:158
auto dsp() -> Dsp &
Definition apu.h:74
MemoryImpl & memory_
Definition apu.h:152
void LoadState(std::istream &stream)
Definition apu.cc:449
debug::ApuHandshakeTracker * handshake_tracker_
Definition apu.h:156
void Write(uint16_t address, uint8_t data)
Definition apu.cc:300
void SpcIdle(bool waiting)
Definition apu.cc:422
uint8_t dsp_adr_
Definition apu.h:144
auto spc700() -> Spc700 &
Definition apu.h:76
uint64_t last_master_cycles_
Definition apu.h:146
const Timer & GetTimer(int timer_index) const
Get timer state for debug UI.
Definition apu.h:120
void Init()
Definition apu.cc:47
void Reset()
Definition apu.cc:54
uint8_t transfer_size_
Definition apu.h:149
bool IsDriverRunning() const
Check if SPC has completed IPL ROM boot and is running driver code.
Definition apu.h:114
void set_handshake_tracker(debug::ApuHandshakeTracker *tracker)
Definition apu.h:81
auto dsp() const -> const Dsp &
Definition apu.h:75
void RunCycles(uint64_t cycles)
Definition apu.cc:88
Spc700 spc700_
Definition apu.h:164
void SpcWrite(uint16_t address, uint8_t data)
Definition apu.cc:417
uint64_t cycles_
Definition apu.h:145
void WriteDma(uint16_t address, const uint8_t *data, int count)
Definition apu.h:89
std::vector< uint8_t > ram
Definition apu.h:139
uint8_t GetControl() const
Definition apu.h:85
std::array< uint8_t, 4 > out_ports_
Definition apu.h:138
Apu(MemoryImpl &memory)
Definition apu.h:55
void BootstrapDirect(uint16_t entry_point)
Bootstrap SPC directly to driver code (bypasses IPL ROM handshake)
Definition apu.cc:478
void GetSamples(int16_t *buffer, int count, bool loop=false)
Definition apu.h:86
uint64_t GetCycles() const
Definition apu.h:78
uint8_t GetStatus() const
Definition apu.h:84
bool rom_readable_
Definition apu.h:142
std::array< uint8_t, 6 > in_ports_
Definition apu.h:137
void Cycle()
Definition apu.cc:226
void SaveState(std::ostream &stream)
Definition apu.cc:426
void GetSamples(int16_t *sample_data, int samples_per_frame, bool pal_timing)
Definition dsp.cc:722
void Write(uint8_t adr, uint8_t val)
Definition dsp.cc:487
Implementation of the Memory interface for emulating memory in a SNES system.
Definition memory.h:118
The Spc700 class represents the SPC700 processor.
Definition spc700.h:69
IPL ROM handshake tracker.
struct yaze::emu::Timer Timer
bool enabled
Definition apu.h:25
uint8_t counter
Definition apu.h:24
uint8_t divider
Definition apu.h:22
uint8_t target
Definition apu.h:23
uint8_t cycles
Definition apu.h:21