-
Notifications
You must be signed in to change notification settings - Fork 0
/
apuunit.cpp
314 lines (266 loc) · 6.69 KB
/
apuunit.cpp
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
310
311
312
313
314
//
// apu.cpp
// rnes
//
//
#include <cmath>
#include "apuunit.h"
#include "save.pb.h"
namespace Rnes {
uint16_t Pulse::computeSweepTarget() const {
uint16_t period = getTimerPeriod();
uint16_t shiftedPeriod = period >> getSweepShift();
if (isSweepNegative()) {
shiftedPeriod = isFirstPulse() ? -shiftedPeriod : -shiftedPeriod + 1;
}
uint16_t targetPeriod = period + shiftedPeriod;
return targetPeriod;
}
void Pulse::sweepPeriod() {
uint16_t targetPeriod = computeSweepTarget();
uint16_t period = getTimerPeriod();
if (targetPeriod > 0x7ff or period < 8) {
return;
}
setTimerPeriod(targetPeriod);
}
uint8_t Pulse::getVolume() const {
if (isDisabled()) {
return getEnvelopeN();
} else {
return envelope;
}
}
void Pulse::envelopeDividerClock() {
if (envelope) {
envelope--;
} else if (isEnvelopeLoopSet()) {
envelope = 15;
}
}
void Pulse::save(PulseState &pb) {
// length logic
pb.set_lengthcounter(lengthCounter);
// envelope logic
pb.set_envelope(envelope);
pb.set_envelopedivider(envelopeDivider);
pb.set_resetenvelopeanddivider(resetEnvelopeAndDivider);
// sweep logic
pb.set_resetsweepdivider(resetSweepDivider);
pb.set_sweepdivider(sweepDivider);
// current sample
pb.set_currentsample(currentSample);
// timer
pb.set_timerdivider(timerDivider);
// sequencer offset
pb.set_sequenceroffset(sequencerOffset);
}
void Pulse::restore(const PulseState &pb) {
lengthCounter = pb.lengthcounter();
envelope = pb.envelope();
envelopeDivider = pb.envelopedivider();
resetEnvelopeAndDivider = pb.resetenvelopeanddivider();
resetSweepDivider = pb.resetsweepdivider();
sweepDivider = pb.sweepdivider();
currentSample = pb.currentsample();
timerDivider = pb.timerdivider();
sequencerOffset = pb.sequenceroffset();
}
uint8_t Pulse::getCurrentSample() const {
// channel is silenced when period is < 8
const uint16_t timerPeriod = getTimerPeriod();
if (timerPeriod < 8) {
return 0;
}
// channel silenced when target is above 0x7ff
const uint16_t targetPeriod = computeSweepTarget();
if (targetPeriod > 0x7ff) {
return 0;
}
if (!isNonZeroLength()) {
return 0;
}
if (!currentSample) {
return 0;
}
return getVolume();
}
void Pulse::clockEnvelope() {
if (resetEnvelopeAndDivider) {
envelope = 15;
envelopeDivider = getEnvelopeN() + 1;
resetEnvelopeAndDivider = false;
} else {
if (envelopeDivider) {
envelopeDivider--;
} else {
envelopeDividerClock();
envelopeDivider = getEnvelopeN() + 1;
}
}
}
void Pulse::clockLengthAndSweep() {
// Length
if (!isHalted() and lengthCounter) {
lengthCounter--;
}
// Sweep
if (resetSweepDivider) {
sweepDivider = getSweepP() + 1;
resetSweepDivider = false;
} else {
if (sweepDivider) {
sweepDivider--;
} else {
if (isSweepEnabled()) {
sweepDivider = getSweepP() + 1;
sweepPeriod();
}
}
}
}
void Pulse::updateSample() {
const DutyCycle cycle = getDutyCycle();
currentSample = (sequences[cycle] & (1 << sequencerOffset)) != 0;
sequencerOffset = (sequencerOffset + 1) % 8;
}
void Pulse::clockTimer() {
if (timerDivider == 0) {
updateSample();
}
timerDivider = (timerDivider + 1) % getTimerPeriod();
}
void Triangle::save(TriangleState &pb) {
// length logic
pb.set_lengthcounter(lengthCounter);
// linear counter logic
pb.set_linearcounterhalt(linearCounterHalt);
pb.set_linearcounter(linearCounter);
// current sample
pb.set_currentsample(currentSample);
// timer
pb.set_timerdivider(timerDivider);
// sequencer offset
pb.set_sequenceroffset(sequencerOffset);
}
void Triangle::restore(const TriangleState &pb) {
lengthCounter = pb.lengthcounter();
linearCounterHalt = pb.linearcounterhalt();
linearCounter = pb.linearcounter();
currentSample = pb.currentsample();
timerDivider = pb.timerdivider();
sequencerOffset = pb.sequenceroffset();
}
void Triangle::clockLength() {
// Length
if (!isHalted() and lengthCounter) {
lengthCounter--;
}
}
void Triangle::clockLinearCounter() {
if (linearCounterHalt) {
linearCounter = getLinearCounterReload();
} else if (linearCounter) {
linearCounter--;
}
if (!getControlFlag()) {
linearCounterHalt = false;
}
}
void Triangle::updateSample() {
// length has run out or linear counter out, channel is silenced
currentSample = sequence[sequencerOffset];
sequencerOffset = (sequencerOffset + 1) % 32;
}
void Triangle::clockTimer() {
if (timerDivider == 0 and isNonZeroLength() && isNonZeroLinearCounter()) {
updateSample();
}
timerDivider = (timerDivider + 1) % (getTimerPeriod());
}
uint16_t Noise::getNextShiftReg(uint16_t reg) const {
uint16_t newBit = 0;
if (isShortMode()) {
newBit = (0x1 & ((reg) ^ (reg >> 6))) << 14;
} else {
newBit = (0x1 & ((reg) ^ (reg >> 1))) << 14;
}
return newBit | (reg >> 1);
}
uint8_t Noise::getVolume() const {
if (isDisabled()) {
return getEnvelopeN();
} else {
return envelope;
}
}
void Noise::envelopeDividerClock() {
if (envelope) {
envelope--;
} else if (isEnvelopeLoopSet()) {
envelope = 15;
}
}
uint8_t Noise::getCurrentSample() const {
if (!currentSample) {
return 0;
}
if (!isNonZeroLength()) {
return 0;
}
return getVolume();
}
void Noise::save(NoiseState &pb) {
// length logic
pb.set_lengthcounter(lengthCounter);
// noise shift register
pb.set_shiftregister(shiftRegister);
// envelope logic
pb.set_envelope(envelope);
pb.set_envelopedivider(envelopeDivider);
pb.set_resetenvelopeanddivider(resetEnvelopeAndDivider);
// current sample
pb.set_currentsample(currentSample);
// timer
pb.set_timerdivider(timerDivider);
}
void Noise::restore(const NoiseState &pb) {
lengthCounter = pb.lengthcounter();
shiftRegister = pb.shiftregister();
envelope = pb.envelope();
envelopeDivider = pb.envelopedivider();
resetEnvelopeAndDivider = pb.resetenvelopeanddivider();
currentSample = pb.currentsample();
timerDivider = pb.timerdivider();
}
void Noise::clockEnvelope() {
if (resetEnvelopeAndDivider) {
envelope = 15;
envelopeDivider = getEnvelopeN() + 1;
resetEnvelopeAndDivider = false;
} else {
if (envelopeDivider) {
envelopeDivider--;
} else {
envelopeDividerClock();
envelopeDivider = getEnvelopeN() + 1;
}
}
}
void Noise::clockLength() {
// Length
if (!isHalted() and lengthCounter) {
lengthCounter--;
}
}
void Noise::updateSample() {
shiftRegister = getNextShiftReg(shiftRegister);
currentSample = (shiftRegister & 1) ? 1 : 0;
}
void Noise::clockTimer() {
if (timerDivider == 0) {
updateSample();
}
timerDivider = (timerDivider + 1) % getTimerPeriod();
}
}; // namespace Rnes