-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapu.cpp
More file actions
309 lines (268 loc) · 8.47 KB
/
apu.cpp
File metadata and controls
309 lines (268 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// apu.cpp
// rnes
//
//
#include <cmath>
#include "apu.h"
#include "apuunit.h"
#include "ringbuffer.h"
#include "save.pb.h"
#include "sdl.h"
namespace Rnes {
void apuSdlCallback(void *data, uint8_t *stream, int len) {
RingBuffer<int16_t> *rb = (RingBuffer<int16_t> *)data;
uint32_t items = len / sizeof(int16_t);
int16_t *outData = (int16_t *)stream;
rb->getData(outData, items);
}
void Apu::clockLengthAndSweep() {
pulseA->clockLengthAndSweep();
pulseB->clockLengthAndSweep();
triangle->clockLength();
noise->clockLength();
}
void Apu::clockEnvAndTriangle() {
pulseA->clockEnvelope();
pulseB->clockEnvelope();
triangle->clockLinearCounter();
}
void Apu::resetFrameCounter() {
frameDivider = 0;
step = 0;
if (!isFourStepFrame()) {
stepAdvance();
}
}
void Apu::stepAdvance() {
uint32_t frameSteps = isFourStepFrame() ? 4 : 5;
if (isFourStepFrame()) {
if (step % 2) {
clockLengthAndSweep();
}
clockEnvAndTriangle();
if ((step == 3) and isFrameIntEnabled()) {
setRequestFrameIrq();
}
} else {
if (step < 4) {
clockEnvAndTriangle();
}
if (step == 0 or step == 2) {
clockLengthAndSweep();
}
}
step += 1;
if (step == frameSteps) {
step = 0;
}
}
void Apu::stepFastTimers() { triangle->clockTimer(); }
void Apu::stepSlowTimers() {
pulseA->clockTimer();
pulseB->clockTimer();
noise->clockTimer();
}
void Apu::generateSample() {
// float sample = (samples[sampleOffset % sampleBufferSize] + samples[(sampleOffset - 32) %
// sampleBufferSize]) / 2;
float sample = 0.0f;
sample += 0.02051777 * (samples[sampleOffset % sampleBufferSize]);
sample += 0.06532911 * (samples[(sampleOffset - 1) % sampleBufferSize]);
sample += 0.16640572 * (samples[(sampleOffset - 2) % sampleBufferSize]);
sample += 0.2477474 * (samples[(sampleOffset - 3) % sampleBufferSize]);
sample += 0.2477474 * (samples[(sampleOffset - 4) % sampleBufferSize]);
sample += 0.16640572 * (samples[(sampleOffset - 5) % sampleBufferSize]);
sample += 0.06532911 * (samples[(sampleOffset - 6) % sampleBufferSize]);
sample += 0.02051777 * (samples[(sampleOffset - 7) % sampleBufferSize]);
int16_t truncSample = (int16_t)((sample) * (1 << 14));
const unsigned int bufferedSamples = 32;
sampleBuffer.push_back(truncSample);
if (sampleBuffer.size() == bufferedSamples) {
rb->putData(sampleBuffer.data(), bufferedSamples);
sampleBuffer.clear();
}
}
void Apu::tick() {
// Divider logic for frame
if (frameDivider == 0) {
stepAdvance();
}
frameDivider = (frameDivider + 1) % frameCycles;
// Divider logic for timers
stepFastTimers();
if (halfTimerDivider == 0) {
stepSlowTimers();
}
halfTimerDivider = !halfTimerDivider;
// Compute output each cycle
sampleOffset += 1;
float sample =
95.88f /
((8128.0f / (float(pulseA->getCurrentSample() + pulseB->getCurrentSample()))) + 100.0f);
sample += 159.79f / (100.0f + (1.0f / ((float(triangle->getCurrentSample()) / 8227.0f) +
float(noise->getCurrentSample()) / 12241.0f)));
samples[sampleOffset % sampleBufferSize] = sample;
// Determine when to sample, and sample if needed.
nextSampleCountdown--;
if (!nextSampleCountdown) {
float prevSampleClk = currentSampleClk;
currentSampleClk += clksPerSample;
nextSampleCountdown = (uint32_t)currentSampleClk - (uint32_t)prevSampleClk;
if (currentSampleClk >= float(cpuClk)) {
currentSampleClk = 0.0f;
}
generateSample();
}
/*
if (samplerDivider == 0) {
}
samplerDivider = (samplerDivider + 1) % uint32_t(cpuClk/ sampleRate);
*/
}
void Apu::run(int cycles) {
for (int i = 0; i < cycles; i++) {
tick();
}
}
void Apu::writeReg(uint32_t reg, uint8_t val) {
regs[reg] = val;
if (reg == SOFTCLOCK) {
resetFrameCounter();
} else if (reg == CONTROL_STATUS) {
clearRequestDmcIrq();
// xx what to do with the upper bits here
if (~val & STATUS_CHANNEL1_LENGTH) {
pulseA->zeroLength();
}
if (~val & STATUS_CHANNEL2_LENGTH) {
pulseB->zeroLength();
}
if (~val & STATUS_CHANNEL3_LENGTH) {
triangle->zeroLength();
}
if (~val & STATUS_CHANNEL4_LENGTH) {
noise->zeroLength();
}
} else if (reg == CHANNEL1_LENGTH) {
pulseA->resetLength();
pulseA->resetSequencer();
pulseA->resetEnvelope();
} else if (reg == CHANNEL2_LENGTH) {
pulseB->resetLength();
pulseB->resetSequencer();
pulseB->resetEnvelope();
} else if (reg == CHANNEL3_LENGTH) {
triangle->resetLength();
triangle->setHaltFlag();
} else if (reg == CHANNEL4_LENGTH) {
noise->resetLength();
noise->resetEnvelope();
}
}
uint8_t Apu::readReg(uint32_t reg) {
uint8_t result = regs[reg];
if (reg == CONTROL_STATUS) {
clearRequestFrameIrq();
result = result & (STATUS_FRAME_IRQ_REQUESTED | STATUS_DMC_IRQ_REQUESTED);
result |= pulseA->isNonZeroLength() ? STATUS_CHANNEL1_LENGTH : 0;
result |= pulseB->isNonZeroLength() ? STATUS_CHANNEL2_LENGTH : 0;
result |= triangle->isNonZeroLength() ? STATUS_CHANNEL3_LENGTH : 0;
result |= noise->isNonZeroLength() ? STATUS_CHANNEL4_LENGTH : 0;
}
return result;
}
void Apu::save(ApuState &pb) {
// Save sub-units.
pulseA->save(*pb.mutable_pulsea());
pulseB->save(*pb.mutable_pulseb());
triangle->save(*pb.mutable_triangle());
noise->save(*pb.mutable_noise());
/*
// Apu register file.
repeated uint32 reg = 1;
optional uint32 sampleRate = 2;
optional uint32 fourFrameCount = 3;
optional uint32 fiveFrameCount = 4;
optional uint32 frameDivider = 5;
optional uint32 step = 6;
optional uint32 halfTimerDivider = 7;
optional uint32 samplerDivider = 8;
optional float clksPerSample = 9;
optional float currentSampleClk = 10;
optional uint32 nextSampleCountdown = 11;
*/
for (unsigned i = 0; i < REG_COUNT; i++) {
pb.add_reg(regs[i]);
}
pb.set_samplerate(sampleRate);
pb.set_fourframecount(fourFrameCount);
pb.set_fiveframecount(fiveFrameCount);
pb.set_framedivider(frameDivider);
pb.set_step(step);
pb.set_halftimerdivider(halfTimerDivider);
pb.set_samplerdivider(samplerDivider);
pb.set_clkspersample(clksPerSample);
pb.set_currentsampleclk(currentSampleClk);
pb.set_nextsamplecountdown(nextSampleCountdown);
}
void Apu::restore(const ApuState &pb) {
// Restore sub-units.
pulseA->restore(pb.pulsea());
pulseB->restore(pb.pulseb());
triangle->restore(pb.triangle());
noise->restore(pb.noise());
/*
// Apu register file.
repeated uint32 reg = 1;
optional uint32 sampleRate = 2;
optional uint32 fourFrameCount = 3;
optional uint32 fiveFrameCount = 4;
optional uint32 frameDivider = 5;
optional uint32 step = 6;
optional uint32 halfTimerDivider = 7;
optional uint32 samplerDivider = 8;
optional float clksPerSample = 9;
optional float currentSampleClk = 10;
optional uint32 nextSampleCountdown = 11;
*/
for (int i = 0; i < pb.reg_size(); i++) {
regs[i] = pb.reg(i);
}
sampleRate = pb.samplerate();
fourFrameCount = pb.fourframecount();
fiveFrameCount = pb.fiveframecount();
frameDivider = pb.framedivider();
step = pb.step();
halfTimerDivider = pb.halftimerdivider();
samplerDivider = pb.samplerdivider();
clksPerSample = pb.clkspersample();
currentSampleClk = pb.currentsampleclk();
nextSampleCountdown = pb.nextsamplecountdown();
}
Apu::Apu(Nes *parent, Sdl *audio)
: nes{parent}, audio{audio}, frameDivider{0}, step{0}, halfTimerDivider{0},
samplerDivider{0}, regs{0}, fourFrameCount{0}, fiveFrameCount{0}, sampleBuffer{} {
// Create audio ringbuffer.
std::unique_ptr<RingBuffer<uint16_t>> rbLocal(new RingBuffer<uint16_t>(1 << 12));
rb = std::move(rbLocal);
// Create apu units.
std::unique_ptr<Pulse> pulseALocal(new Pulse{®s[CHANNEL1_VOLUME_DECAY], true});
pulseA = std::move(pulseALocal);
std::unique_ptr<Pulse> pulseBLocal(new Pulse{®s[CHANNEL2_VOLUME_DECAY], false});
pulseB = std::move(pulseBLocal);
std::unique_ptr<Triangle> triangleLocal(new Triangle{®s[CHANNEL3_LINEAR_COUNTER]});
triangle = std::move(triangleLocal);
std::unique_ptr<Noise> noiseLocal(new Noise{®s[CHANNEL4_VOLUME_DECAY]});
noise = std::move(noiseLocal);
// Get Current sample rate
sampleRate = audio->getSampleRate();
// Start audio callbacks.
audio->registerAudioCallback(apuSdlCallback, rb.get());
// compute clocks per sample
clksPerSample = float(cpuClk) / float(sampleRate);
nextSampleCountdown = 1;
currentSampleClk = 0.0f;
}
Apu::~Apu() { audio->unregisterAudioCallback(); }
}; // namespace Rnes