src/Core/apu.c

filter: Statementstmt 30.7% 407 / 1324 Branchbran 22.1% 245 / 1104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 26.8% 652 / 2428 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/Core/save_state.c ↑ Summary src/Core/mbc.c »
1 #include <stdint.h>
2 #include <math.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include "gb.h"
8  
9 /* Band limited synthesis loosely based on: http://www.slack.net/~ant/bl-synth/ */
10 static int32_t band_limited_steps[GB_BAND_LIMITED_PHASES][GB_BAND_LIMITED_WIDTH];
11  
12 8 static void __attribute__((constructor)) band_limited_init(void)
13 {
14 8 const unsigned master_size = GB_BAND_LIMITED_WIDTH * GB_BAND_LIMITED_PHASES;
15 8 double *master = malloc(master_size * sizeof(*master));
16 8 memset(master, 0, master_size * sizeof(*master));
17
18 8 const double lowpass = 15.0 / 16.0; // 1.0 means using Nyquist as the exact cutoff
19 8 const double to_angle = M_PI / GB_BAND_LIMITED_PHASES * lowpass;
20 8 double sum = 0;
21 131080 ▶ nounroll for (signed i = 0; i < master_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (signed i = 0; i < master_size; i++) { 131072 8 131080
22 // Exact Blackman window
23 131072 const double a0 = 7938 / 18608.0;
24 131072 const double a1 = 9240 / 18608.0;
25 131072 const double a2 = 1430 / 18608.0;
26 131072 double window_angle = (2.0 * M_PI * i) / (master_size);
27 131072 double window = a0 - a1 * cos(window_angle) + a2 * cos(2 * window_angle);
28
29 131072 double angle = (i - (signed)master_size / 2) * to_angle;
30 131072 ▶ sum += master[i] = (angle == 0? 1 : sin(angle) / angle) * window;
Branch: 1 branch
BranchTrueFalseTotal
sum += master[i] = (angle == 0? 1 : sin(angle) / angle) * window; 131064 8 131072
31 }
32
33 131080 ▶ nounroll for (signed i = 0; i < master_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (signed i = 0; i < master_size; i++) { 131072 8 131080
34 131072 master[i] /= sum;
35 }
36
37 2056 ▶ nounroll for (signed phase = 0; phase < GB_BAND_LIMITED_PHASES; phase++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (signed phase = 0; phase < GB_BAND_LIMITED_PHASES; phase++) { 2048 8 2056
38 2048 int32_t error = GB_BAND_LIMITED_ONE;
39 133120 ▶ nounroll for (signed i = 0; i < GB_BAND_LIMITED_WIDTH; i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (signed i = 0; i < GB_BAND_LIMITED_WIDTH; i++) { 131072 2048 133120
40 131072 double sum = 0;
41 33685504 ▶ nounroll for (signed j = 0; j < GB_BAND_LIMITED_PHASES; j++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (signed j = 0; j < GB_BAND_LIMITED_PHASES; j++) { 33554432 131072 33685504
42 33554432 signed index = i * GB_BAND_LIMITED_PHASES - phase + j;
43 33554432 ▶ if (index >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index >= 0) { 33293312 261120 33554432
44 33293312 sum += master[index];
45 }
46 }
47 131072 int32_t cur = sum * GB_BAND_LIMITED_ONE;
48 131072 error -= cur;
49 131072 band_limited_steps[phase][i] = cur;
50 }
51
52 // Make sure the deltas sum to 1.0
53 2048 band_limited_steps[phase][GB_BAND_LIMITED_WIDTH / 2] += error;
54 }
55 8 free(master);
56 8 }
57  
58 9269 static void band_limited_update(GB_band_limited_t *band_limited, const GB_sample_t *input, unsigned phase)
59 {
60 9269 ▶ if (input->packed == band_limited->input.packed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (input->packed == band_limited->input.packed) return; 5516 3753 9269
61 3753 unsigned delay = phase / GB_BAND_LIMITED_PHASES;
62 3753 phase = phase & (GB_BAND_LIMITED_PHASES - 1);
63
64 struct {
65 signed left, right;
66 3753 } delta = {
67 3753 .left = input->left - band_limited->input.left,
68 3753 .right = input->right - band_limited->input.right,
69 };
70 3753 band_limited->input.packed = input->packed;
71
72 243945 ▶ for (unsigned i = 0; i < GB_BAND_LIMITED_WIDTH; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < GB_BAND_LIMITED_WIDTH; i++) { 240192 3753 243945
73 240192 unsigned offset = (i + band_limited->pos + delay) & (sizeof(band_limited->buffer) / sizeof(band_limited->buffer[0]) - 1);
74 240192 band_limited->buffer[offset].left += delta.left * band_limited_steps[phase][i];
75 240192 band_limited->buffer[offset].right += delta.right * band_limited_steps[phase][i];
76 }
77 }
78  
79 0 static void band_limited_update_unfiltered(GB_band_limited_t *band_limited, const GB_sample_t *input, unsigned delay)
80 {
81 0 ▶ if (input->packed == band_limited->input.packed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (input->packed == band_limited->input.packed) return; 0 0 0
82
83 struct {
84 signed left, right;
85 0 } delta = {
86 0 .left = input->left - band_limited->input.left,
87 0 .right = input->right - band_limited->input.right,
88 };
89 0 band_limited->input.packed = input->packed;
90
91 0 unsigned offset = (band_limited->pos + delay) & (sizeof(band_limited->buffer) / sizeof(band_limited->buffer[0]) - 1);
92 0 band_limited->buffer[offset].left += delta.left * GB_BAND_LIMITED_ONE;
93 0 band_limited->buffer[offset].right += delta.right * GB_BAND_LIMITED_ONE;
94 }
95  
96 848732 static void band_limited_read(GB_band_limited_t *band_limited, GB_sample_t *output, uint32_t multiplier)
97 {
98 848732 band_limited->output.left += band_limited->buffer[band_limited->pos].left;
99 848732 band_limited->output.right += band_limited->buffer[band_limited->pos].right;
100
101 848732 band_limited->buffer[band_limited->pos].left = band_limited->buffer[band_limited->pos].right = 0;
102 848732 band_limited->pos = (band_limited->pos + 1) & (sizeof(band_limited->buffer) / sizeof(band_limited->buffer[0]) - 1);
103
104 848732 output->left = band_limited->output.left * multiplier / GB_BAND_LIMITED_ONE;
105 848732 output->right = band_limited->output.right * multiplier / GB_BAND_LIMITED_ONE;
106
107 /* This hueristic will mute the channel if it's only playing an amplitude of 1 or 2 units, usually
108 caused by rounding errors when the channel is playing a single frequency above Nyquist. */
109
110 848732 unsigned diff = abs(output->left - band_limited->last_output.left);
111 848732 ▶ if (diff > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff > 4) { 42517 806215 848732
112 42517 band_limited->silence_detection = 0;
113 42517 band_limited->last_output.packed = output->packed;
114 42517 return;
115 }
116
117 806215 diff = abs(output->right - band_limited->last_output.right);
118 806215 ▶ if (diff > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff > 4) { 0 806215 806215
119 0 band_limited->silence_detection = 0;
120 0 band_limited->last_output.packed = output->packed;
121 0 return;
122 }
123
124 806215 ▶ if (band_limited->silence_detection == 4000) {
Branch: 1 branch
BranchTrueFalseTotal
if (band_limited->silence_detection == 4000) { 635300 170915 806215
125 635300 output->packed = band_limited->last_output.packed;
126 }
127 else {
128 170915 band_limited->silence_detection++;
129 }
130 }
131  
132 226333 static inline uint32_t sample_fraction_multiply(GB_gameboy_t *gb, unsigned multiplier)
133 {
134 226333 ▶ if (unlikely(multiplier == 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(multiplier == 0)) return 0; 75 226258 226333
135 226258 ▶ if (likely(multiplier < GB_QUICK_MULTIPLY_COUNT + 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(multiplier < GB_QUICK_MULTIPLY_COUNT + 1)) { 226258 0 226258
136 226258 return gb->apu_output.quick_fraction_multiply_cache[multiplier - 1];
137 }
138 0 return gb->apu_output.quick_fraction_multiply_cache[0] * multiplier;
139 }
140  
141 static const uint8_t duties[] = {
142 0, 0, 0, 0, 0, 0, 0, 1,
143 1, 0, 0, 0, 0, 0, 0, 1,
144 1, 0, 0, 0, 0, 1, 1, 1,
145 0, 1, 1, 1, 1, 1, 1, 0,
146 };
147  
148 858001 bool GB_apu_is_DAC_enabled(GB_gameboy_t *gb, GB_channel_t index)
149 {
150 858001 ▶ if (gb->model > GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model > GB_MODEL_CGB_E) { 0 858001 858001
151 /* On the AGB, mixing is done digitally, so there are no per-channel
152 DACs. Instead, all channels are summed digital regardless of
153 whatever the DAC state would be on a CGB or earlier model. */
154 0 return true;
155 }
156
157 858001 switch (index) {
158 221404 case GB_SQUARE_1:
159 221404 return gb->io_registers[GB_IO_NR12] & 0xF8;
160  
161 212199 case GB_SQUARE_2:
162 212199 return gb->io_registers[GB_IO_NR22] & 0xF8;
163  
164 212199 case GB_WAVE:
165 212199 return gb->apu.wave_channel.enable;
166  
167 212199 case GB_NOISE:
168 212199 return gb->io_registers[GB_IO_NR42] & 0xF8;
169
170 0 nodefault;
171 }
172  
173 return false;
174 }
175  
176 0 static uint8_t agb_bias_for_channel(GB_gameboy_t *gb, GB_channel_t index)
177 {
178 0 ▶ if (!gb->apu.is_active[index]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.is_active[index]) return 0; 0 0 0
179
180 0 switch (index) {
181 0 case GB_SQUARE_1:
182 0 return gb->apu.square_channels[GB_SQUARE_1].current_volume;
183 0 case GB_SQUARE_2:
184 0 return gb->apu.square_channels[GB_SQUARE_2].current_volume;
185 0 case GB_WAVE:
186 0 return 0;
187 0 case GB_NOISE:
188 0 return gb->apu.noise_channel.current_volume;
189
190 0 nodefault;
191 }
192 }
193  
194 16815 static void update_sample(GB_gameboy_t *gb, GB_channel_t index, int8_t value, unsigned cycles_offset)
195 {
196 16815 ▶ if (gb->model > GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model > GB_MODEL_CGB_E) { 0 16815 16815
197 /* On the AGB, because no analog mixing is done, the behavior of NR51 is a bit different.
198 A channel that is not connected to a terminal is idenitcal to a connected channel
199 playing PCM sample 0. */
200 0 gb->apu.samples[index] = value;
201
202 0 ▶ if (gb->apu_output.sample_rate) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.sample_rate) { 0 0 0
203 0 unsigned right_volume = (gb->io_registers[GB_IO_NR50] & 7) + 1;
204 0 unsigned left_volume = ((gb->io_registers[GB_IO_NR50] >> 4) & 7) + 1;
205 0 int8_t silence = 0;
206 0 ▶ if (index == GB_WAVE) {
Branch: 1 branch
BranchTrueFalseTotal
if (index == GB_WAVE) { 0 0 0
207 /* For some reason, channel 3 is inverted on the AGB, and has a different "silence" value */
208 0 value ^= 0xF;
209 0 silence = 7 * 2;
210 }
211
212 0 uint8_t bias = agb_bias_for_channel(gb, index);
213
214 0 bool left = gb->io_registers[GB_IO_NR51] & (0x10 << index);
215 0 bool right = gb->io_registers[GB_IO_NR51] & (1 << index);
216
217 0 ▶ GB_sample_t output = {
Branch: 2 branches
BranchTrueFalseTotal
GB_sample_t output = { 0 0 0
GB_sample_t output = { 0 0 0
218 0 .left = (0xF - (left? value * 2 + bias : silence)) * left_volume,
219 0 .right = (0xF - (right? value * 2 + bias : silence)) * right_volume
220 };
221
222 0 ▶ if (unlikely(gb->apu_output.channel_muted[index])) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->apu_output.channel_muted[index])) { 0 0 0
223 0 output.left = output.right = 0;
224 }
225
226 0 ▶ if (unlikely(gb->apu_output.max_cycles_per_sample == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->apu_output.max_cycles_per_sample == 1)) { 0 0 0
227 0 band_limited_update_unfiltered(&gb->apu_output.band_limited[index], &output, cycles_offset);
228 }
229 else {
230 0 band_limited_update(&gb->apu_output.band_limited[index],
231 &output,
232 0 (((gb->apu_output.sample_fraction + sample_fraction_multiply(gb, cycles_offset)) >> 8) * GB_BAND_LIMITED_PHASES) >> 20);
233 }
234 }
235
236 0 return;
237 }
238
239 16815 ▶ if (value == 0 && gb->apu.samples[index] == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (value == 0 && gb->apu.samples[index] == 0) return; 9425 7390 16815
if (value == 0 && gb->apu.samples[index] == 0) return; 7546 1879 9425
240
241 9269 ▶ if (!GB_apu_is_DAC_enabled(gb, index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_apu_is_DAC_enabled(gb, index)) { 48 9221 9269
242 48 value = gb->apu.samples[index];
243 }
244 else {
245 9221 gb->apu.samples[index] = value;
246 }
247  
248 9269 ▶ if (gb->apu_output.sample_rate) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.sample_rate) { 9269 0 9269
249 9269 unsigned right_volume = 0;
250 9269 ▶ if (gb->io_registers[GB_IO_NR51] & (1 << index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_NR51] & (1 << index)) { 9237 32 9269
251 9237 right_volume = (gb->io_registers[GB_IO_NR50] & 7) + 1;
252 }
253 9269 unsigned left_volume = 0;
254 9269 ▶ if (gb->io_registers[GB_IO_NR51] & (0x10 << index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_NR51] & (0x10 << index)) { 9269 0 9269
255 9269 left_volume = ((gb->io_registers[GB_IO_NR50] >> 4) & 7) + 1;
256 }
257 9269 GB_sample_t output = {0, 0};
258 9269 ▶ if (likely(!gb->apu_output.channel_muted[index])) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!gb->apu_output.channel_muted[index])) { 9269 0 9269
259 9269 output = (GB_sample_t){(0xF - value * 2) * left_volume, (0xF - value * 2) * right_volume};
260 }
261 9269 ▶ if (unlikely(gb->apu_output.max_cycles_per_sample == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->apu_output.max_cycles_per_sample == 1)) { 0 9269 9269
262 0 band_limited_update_unfiltered(&gb->apu_output.band_limited[index], &output, cycles_offset);
263 }
264 else {
265 9269 band_limited_update(&gb->apu_output.band_limited[index],
266 &output,
267 9269 (((gb->apu_output.sample_fraction + sample_fraction_multiply(gb, cycles_offset)) >> 8) * GB_BAND_LIMITED_PHASES) >> 20);
268 }
269 }
270 }
271  
272 18 static double smooth(double x)
273 {
274 18 return 3*x*x - 2*x*x*x;
275 }
276  
277 0 static signed interference(GB_gameboy_t *gb)
278 {
279 /* These aren't scientifically measured, but based on ear based on several recordings */
280 0 signed ret = 0;
281 0 ▶ if (gb->halted) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->halted) { 0 0 0
282 0 ▶ if (gb->model <= GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_E) { 0 0 0
283 0 ret -= MAX_CH_AMP / 5;
284 }
285 else {
286 0 ret -= MAX_CH_AMP / 12;
287 }
288 }
289 0 ▶ if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) { 0 0 0
290 0 ret += MAX_CH_AMP / 7;
291 0 ▶ if ((gb->io_registers[GB_IO_STAT] & 3) == 3 && gb->model <= GB_MODEL_CGB_E) {
Branch: 2 branches
BranchTrueFalseTotal
if ((gb->io_registers[GB_IO_STAT] & 3) == 3 && gb->model <= GB_MODEL_CGB_E) { 0 0 0
if ((gb->io_registers[GB_IO_STAT] & 3) == 3 && gb->model <= GB_MODEL_CGB_E) { 0 0 0
292 0 ret += MAX_CH_AMP / 14;
293 }
294 0 ▶ else if ((gb->io_registers[GB_IO_STAT] & 3) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((gb->io_registers[GB_IO_STAT] & 3) == 1) { 0 0 0
295 0 ret -= MAX_CH_AMP / 7;
296 }
297 }
298
299 0 ▶ if (gb->apu.global_enable) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.global_enable) { 0 0 0
300 0 ret += MAX_CH_AMP / 10;
301 }
302
303 0 ▶ if (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_E && (gb->io_registers[GB_IO_RP] & 1)) {
Branch: 3 branches
BranchTrueFalseTotal
if (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_E && (gb->io_registers[GB_IO_RP] & 1)) { 0 0 0
if (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_E && (gb->io_registers[GB_IO_RP] & 1)) { 0 0 0
if (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_E && (gb->io_registers[GB_IO_RP] & 1)) { 0 0 0
304 0 ret += MAX_CH_AMP / 10;
305 }
306
307 0 ▶ if (!GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_cgb(gb)) { 0 0 0
308 0 ret /= 4;
309 }
310
311 0 ret += rand() % (MAX_CH_AMP / 12);
312
313 0 return ret;
314 }
315  
316 212183 static void render(GB_gameboy_t *gb)
317 {
318 212183 GB_sample_t output = {0, 0};
319  
320 1060915 ▶ unrolled for (unsigned i = 0; i < GB_N_CHANNELS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = 0; i < GB_N_CHANNELS; i++) { 848732 212183 1060915
321 848732 double multiplier = CH_STEP;
322
323 848732 ▶ if (gb->model <= GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_E) { 848732 0 848732
324 848732 ▶ if (!GB_apu_is_DAC_enabled(gb, i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_apu_is_DAC_enabled(gb, i)) { 660058 188674 848732
325 660058 gb->apu_output.dac_discharge[i] -= ((double) DAC_DECAY_SPEED) / gb->apu_output.sample_rate;
326 660058 ▶ if (gb->apu_output.dac_discharge[i] < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.dac_discharge[i] < 0) { 660058 0 660058
327 660058 multiplier = 0;
328 660058 gb->apu_output.dac_discharge[i] = 0;
329 }
330 else {
331 0 multiplier *= smooth(gb->apu_output.dac_discharge[i]);
332 }
333 }
334 else {
335 188674 gb->apu_output.dac_discharge[i] += ((double) DAC_ATTACK_SPEED) / gb->apu_output.sample_rate;
336 188674 ▶ if (gb->apu_output.dac_discharge[i] > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.dac_discharge[i] > 1) { 188656 18 188674
337 188656 gb->apu_output.dac_discharge[i] = 1;
338 }
339 else {
340 18 multiplier *= smooth(gb->apu_output.dac_discharge[i]);
341 }
342 }
343 }
344
345 GB_sample_t channel_output;
346 848732 band_limited_read(&gb->apu_output.band_limited[i], &channel_output, multiplier);
347  
348 848732 output.left += channel_output.left;
349 848732 output.right += channel_output.right;
350 }
351 212183 gb->apu_output.cycles_since_render = 0;
352 212183 ▶ if (unlikely(gb->apu_output.sample_fraction < (1 << 28))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->apu_output.sample_fraction < (1 << 28))) { 4 212179 212183
353 4 gb->apu_output.sample_fraction = 0;
354 }
355 else {
356 212179 gb->apu_output.sample_fraction -= 1 << 28;
357 }
358
359 212183 ▶ if (gb->sgb && gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) return;
Branch: 2 branches
BranchTrueFalseTotal
if (gb->sgb && gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) return; 0 212183 212183
if (gb->sgb && gb->sgb->intro_animation < GB_SGB_INTRO_ANIMATION_LENGTH) return; 0 0 0
360  
361 206276 GB_sample_t filtered_output = gb->apu_output.highpass_mode?
362 5907 (GB_sample_t) {output.left - (int16_t)gb->apu_output.highpass_diff.left,
363 212183 ▶ output.right - (int16_t)gb->apu_output.highpass_diff.right} :
Branch: 1 branch
BranchTrueFalseTotal
output.right - (int16_t)gb->apu_output.highpass_diff.right} : 5907 206276 212183
364 output;
365  
366 212183 switch (gb->apu_output.highpass_mode) {
367 206276 case GB_HIGHPASS_OFF:
368 206276 gb->apu_output.highpass_diff = (GB_double_sample_t) {0, 0};
369 212183 break;
370 5907 case GB_HIGHPASS_ACCURATE:
371 5907 gb->apu_output.highpass_diff = (GB_double_sample_t) {
372 5907 output.left - (output.left - gb->apu_output.highpass_diff.left) * gb->apu_output.highpass_rate,
373 5907 output.right - (output.right - gb->apu_output.highpass_diff.right) * gb->apu_output.highpass_rate
374 };
375 5907 break;
376 0 case GB_HIGHPASS_REMOVE_DC_OFFSET: {
377 0 unsigned mask = gb->io_registers[GB_IO_NR51];
378 0 unsigned left_volume = 0;
379 0 unsigned right_volume = 0;
380 0 ▶ unrolled for (unsigned i = GB_N_CHANNELS; i--;) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = GB_N_CHANNELS; i--;) { 0 0 0
381 0 ▶ if (GB_apu_is_DAC_enabled(gb, i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_apu_is_DAC_enabled(gb, i)) { 0 0 0
382 0 ▶ if (mask & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (mask & 1) { 0 0 0
383 0 left_volume += ((gb->io_registers[GB_IO_NR50] & 7) + 1) * CH_STEP * 0xF;
384 }
385 0 ▶ if (mask & 0x10) {
Branch: 1 branch
BranchTrueFalseTotal
if (mask & 0x10) { 0 0 0
386 0 right_volume += (((gb->io_registers[GB_IO_NR50] >> 4) & 7) + 1) * CH_STEP * 0xF;
387 }
388 }
389 0 mask >>= 1;
390 }
391 0 gb->apu_output.highpass_diff = (GB_double_sample_t) {
392 0 left_volume * (1 - gb->apu_output.highpass_rate) + gb->apu_output.highpass_diff.left * gb->apu_output.highpass_rate,
393 0 right_volume * (1 - gb->apu_output.highpass_rate) + gb->apu_output.highpass_diff.right * gb->apu_output.highpass_rate
394 };
395  
396 0 case GB_HIGHPASS_MAX:;
397 }
398  
399 }
400
401
402 212183 ▶ if (gb->apu_output.interference_volume) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.interference_volume) { 0 212183 212183
403 0 signed interference_bias = interference(gb);
404 0 int16_t interference_sample = (interference_bias - gb->apu_output.interference_highpass);
405 0 gb->apu_output.interference_highpass = gb->apu_output.interference_highpass * gb->apu_output.highpass_rate +
406 0 (1 - gb->apu_output.highpass_rate) * interference_sample;
407 0 interference_bias *= gb->apu_output.interference_volume;
408
409 0 filtered_output.left = MAX(MIN(filtered_output.left + interference_bias, 0x7FFF), -0x8000);
410 0 filtered_output.right = MAX(MIN(filtered_output.right + interference_bias, 0x7FFF), -0x8000);
411 }
412 assert(gb->apu_output.sample_callback);
413 212183 gb->apu_output.sample_callback(gb, &filtered_output);
414 212183 ▶ if (unlikely(gb->apu_output.output_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->apu_output.output_file)) { 0 212183 212183
415 #ifdef GB_BIG_ENDIAN
416 if (gb->apu_output.output_format == GB_AUDIO_FORMAT_WAV) {
417 filtered_output.left = LE16(filtered_output.left);
418 filtered_output.right = LE16(filtered_output.right);
419 }
420 #endif
421 0 ▶ if (fwrite(&filtered_output, sizeof(filtered_output), 1, gb->apu_output.output_file) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fwrite(&filtered_output, sizeof(filtered_output), 1, gb->apu_output.output_file) != 1) { 0 0 0
422 0 fclose(gb->apu_output.output_file);
423 0 gb->apu_output.output_file = NULL;
424 0 gb->apu_output.output_error = errno;
425 }
426 }
427 }
428  
429 16749 static void update_square_sample(GB_gameboy_t *gb, GB_channel_t index, unsigned cycles)
430 {
431 16749 ▶ if (gb->apu.square_channels[index].sample_surpressed) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[index].sample_surpressed) { 0 16749 16749
432 0 ▶ if (gb->model > GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model > GB_MODEL_CGB_E) { 0 0 0
433 0 update_sample(gb, index, gb->apu.samples[index], 0);
434 }
435 0 return;
436 }
437  
438 16749 ▶ uint8_t duty = gb->io_registers[index == GB_SQUARE_1? GB_IO_NR11 :GB_IO_NR21] >> 6;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t duty = gb->io_registers[index == GB_SQUARE_1? GB_IO_NR11 :GB_IO_NR21] >> 6; 16749 0 16749
439 16749 update_sample(gb, index,
440 16749 ▶ duties[gb->apu.square_channels[index].current_sample_index + duty * 8]?
Branch: 1 branch
BranchTrueFalseTotal
duties[gb->apu.square_channels[index].current_sample_index + duty * 8]? 8372 8377 16749
441 8372 gb->apu.square_channels[index].current_volume : 0,
442 cycles);
443 }
444  
445 0 static inline void update_wave_sample(GB_gameboy_t *gb, unsigned cycles)
446 {
447 0 ▶ if (gb->apu.wave_channel.current_sample_index & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.current_sample_index & 1) { 0 0 0
448 0 update_sample(gb, GB_WAVE,
449 0 (gb->apu.wave_channel.current_sample_byte & 0xF) >> gb->apu.wave_channel.shift,
450 cycles);
451 }
452 else {
453 0 update_sample(gb, GB_WAVE,
454 0 (gb->apu.wave_channel.current_sample_byte >> 4) >> gb->apu.wave_channel.shift,
455 cycles);
456 }
457 0 }
458  
459 62 static inline void set_envelope_clock(GB_envelope_clock_t *clock, bool value, bool direction, uint8_t volume)
460 {
461 62 ▶ if (clock->clock == value) return;
Branch: 1 branch
BranchTrueFalseTotal
if (clock->clock == value) return; 19 43 62
462 43 ▶ if (value) {
Branch: 1 branch
BranchTrueFalseTotal
if (value) { 22 21 43
463 22 clock->clock = true;
464 22 ▶ clock->should_lock = (volume == 0xF && direction) || (volume == 0x0 && !direction);
Branch: 4 branches
BranchTrueFalseTotal
clock->should_lock = (volume == 0xF && direction) || (volume == 0x0 && !direction); 4 18 22
clock->should_lock = (volume == 0xF && direction) || (volume == 0x0 && !direction); 4 0 4
clock->should_lock = (volume == 0xF && direction) || (volume == 0x0 && !direction); 2 20 22
clock->should_lock = (volume == 0xF && direction) || (volume == 0x0 && !direction); 2 0 2
465 }
466 else {
467 21 clock->clock = false;
468 21 clock->locked |= clock->should_lock;
469 }
470 }
471  
472 0 static void _nrx2_glitch(uint8_t *volume, uint8_t value, uint8_t old_value, uint8_t *countdown, GB_envelope_clock_t *lock)
473 {
474 0 ▶ if (lock->clock) {
Branch: 1 branch
BranchTrueFalseTotal
if (lock->clock) { 0 0 0
475 0 *countdown = value & 7;
476 }
477 0 ▶ bool should_tick = (value & 7) && !(old_value & 7) && !lock->locked;
Branch: 3 branches
BranchTrueFalseTotal
bool should_tick = (value & 7) && !(old_value & 7) && !lock->locked; 0 0 0
bool should_tick = (value & 7) && !(old_value & 7) && !lock->locked; 0 0 0
bool should_tick = (value & 7) && !(old_value & 7) && !lock->locked; 0 0 0
478 0 bool should_invert = (value & 8) ^ (old_value & 8);
479
480 0 ▶ if ((value & 0xF) == 8 && (old_value & 0xF) == 8 && !lock->locked) {
Branch: 3 branches
BranchTrueFalseTotal
if ((value & 0xF) == 8 && (old_value & 0xF) == 8 && !lock->locked) { 0 0 0
if ((value & 0xF) == 8 && (old_value & 0xF) == 8 && !lock->locked) { 0 0 0
if ((value & 0xF) == 8 && (old_value & 0xF) == 8 && !lock->locked) { 0 0 0
481 0 should_tick = true;
482 }
483
484 0 ▶ if (should_invert) {
Branch: 1 branch
BranchTrueFalseTotal
if (should_invert) { 0 0 0
485 // The weird and over-the-top way clocks for this counter are connected cause
486 // some weird ways for it to invert
487 0 ▶ if (value & 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (value & 8) { 0 0 0
488 0 ▶ if (!(old_value & 7) && !lock->locked) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(old_value & 7) && !lock->locked) { 0 0 0
if (!(old_value & 7) && !lock->locked) { 0 0 0
489 0 *volume ^= 0xF;
490 }
491 else {
492 0 *volume = 0xE - *volume;
493 0 *volume &= 0xF;
494 }
495 0 should_tick = false; // Somehow prevents ticking?
496 }
497 else {
498 0 *volume = 0x10 - *volume;
499 0 *volume &= 0xF;
500 }
501 }
502 0 ▶ if (should_tick) {
Branch: 1 branch
BranchTrueFalseTotal
if (should_tick) { 0 0 0
503 0 ▶ if (value & 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (value & 8) { 0 0 0
504 0 (*volume)++;
505 }
506 else {
507 0 (*volume)--;
508 }
509 0 *volume &= 0xF;
510 }
511 0 ▶ else if (!(value & 7) && lock->clock) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!(value & 7) && lock->clock) { 0 0 0
else if (!(value & 7) && lock->clock) { 0 0 0
512 0 set_envelope_clock(lock, false, 0, 0);
513 }
514 0 }
515  
516 0 static void nrx2_glitch(GB_gameboy_t *gb, uint8_t *volume, uint8_t value, uint8_t old_value, uint8_t *countdown, GB_envelope_clock_t *lock)
517 {
518 /* Note: on pre-CGB models *some* of these are non-deterministic. Specifically,
519 $x0 writes seem to be non-deterministic while $x8 always work as expected.
520 TODO: Might be useful to find which cases are non-deterministic, and allow
521 the debugger to issue warnings when they're used. I suspect writes to/from
522 $xF are guaranteed to be deterministic. */
523 0 ▶ if (gb->model <= GB_MODEL_CGB_C) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_C) { 0 0 0
524 0 _nrx2_glitch(volume, 0xFF, old_value, countdown, lock);
525 0 _nrx2_glitch(volume, value, 0xFF, countdown, lock);
526 }
527 else {
528 0 _nrx2_glitch(volume, value, old_value, countdown, lock);
529 }
530 0 }
531  
532 21 static void tick_square_envelope(GB_gameboy_t *gb, GB_channel_t index)
533 {
534 21 set_envelope_clock(&gb->apu.square_channels[index].envelope_clock, false, 0, 0);
535 21 ▶ if (gb->apu.square_channels[index].envelope_clock.locked) return;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[index].envelope_clock.locked) return; 2 19 21
536 19 ▶ uint8_t nrx2 = gb->io_registers[index == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22];
Branch: 1 branch
BranchTrueFalseTotal
uint8_t nrx2 = gb->io_registers[index == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22]; 19 0 19
537
538 19 ▶ if (!(nrx2 & 7)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!(nrx2 & 7)) return; 0 19 19
539 19 ▶ if (gb->cgb_double_speed) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_double_speed) { 0 19 19
540 0 ▶ if (index == GB_SQUARE_1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index == GB_SQUARE_1) { 0 0 0
541 0 gb->apu.pcm_mask[0] &= gb->apu.square_channels[GB_SQUARE_1].current_volume | 0xF1;
542 }
543 else {
544 /* Note: CGB-0 behavior is instance specific and non-deterministic. Emulated behavior follows my CGB-0,
545 except that my CGB-0 sometimes yields "1" for 8->7 and 4->3 transitions. */
546 uint8_t mask;
547 0 ▶ if (unlikely(gb->model == GB_MODEL_CGB_0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->model == GB_MODEL_CGB_0)) { 0 0 0
548 0 ▶ if (gb->apu.square_channels[GB_SQUARE_2].current_volume == 1 && (gb->io_registers[GB_IO_NR22] & 8)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.square_channels[GB_SQUARE_2].current_volume == 1 && (gb->io_registers[GB_IO_NR22] & 8)) { 0 0 0
if (gb->apu.square_channels[GB_SQUARE_2].current_volume == 1 && (gb->io_registers[GB_IO_NR22] & 8)) { 0 0 0
549 0 mask = 0x1F;
550 }
551 else {
552 0 mask = 0x3F;
553 }
554 }
555 else {
556 0 mask = 0x3F;
557 }
558 0 gb->apu.pcm_mask[0] &= (gb->apu.square_channels[GB_SQUARE_2].current_volume << 4) | mask;
559 }
560 }
561
562 19 set_envelope_clock(&gb->apu.square_channels[index].envelope_clock, false, 0, 0);
563
564 19 ▶ if (nrx2 & 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrx2 & 8) { 0 19 19
565 0 gb->apu.square_channels[index].current_volume++;
566 }
567 else {
568 19 gb->apu.square_channels[index].current_volume--;
569 }
570  
571 19 ▶ if (gb->apu.is_active[index]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[index]) { 19 0 19
572 19 update_square_sample(gb, index, 0);
573 }
574 }
575  
576 0 static void tick_noise_envelope(GB_gameboy_t *gb)
577 {
578 0 set_envelope_clock(&gb->apu.noise_channel.envelope_clock, false, 0, 0);
579 0 ▶ if (gb->apu.noise_channel.envelope_clock.locked) return;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.envelope_clock.locked) return; 0 0 0
580
581 0 uint8_t nr42 = gb->io_registers[GB_IO_NR42];
582 0 ▶ if (!(nr42 & 7)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!(nr42 & 7)) return; 0 0 0
583  
584 0 ▶ if (gb->cgb_double_speed) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_double_speed) { 0 0 0
585 0 gb->apu.pcm_mask[1] &= (gb->apu.noise_channel.current_volume << 4) | 0x1F;
586 }
587
588 0 ▶ if (nr42 & 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (nr42 & 8) { 0 0 0
589 0 gb->apu.noise_channel.current_volume++;
590 }
591 else {
592 0 gb->apu.noise_channel.current_volume--;
593 }
594  
595 0 ▶ if (gb->apu.is_active[GB_NOISE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[GB_NOISE]) { 0 0 0
596 0 update_sample(gb, GB_NOISE,
597 0 ▶ (gb->apu.noise_channel.lfsr & 1) ?
Branch: 1 branch
BranchTrueFalseTotal
(gb->apu.noise_channel.lfsr & 1) ? 0 0 0
598 0 gb->apu.noise_channel.current_volume : 0,
599 0);
600 }
601 }
602  
603 0 static void sweep_calculation_done(GB_gameboy_t *gb, unsigned cycles)
604 {
605 /* APU bug: sweep frequency is checked after adding the sweep delta twice */
606 0 ▶ if (gb->apu.channel_1_restart_hold == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.channel_1_restart_hold == 0) { 0 0 0
607 0 gb->apu.shadow_sweep_sample_length = gb->apu.square_channels[GB_SQUARE_1].sample_length;
608 }
609 0 ▶ if (gb->io_registers[GB_IO_NR10] & 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_NR10] & 8) { 0 0 0
610 0 gb->apu.sweep_length_addend ^= 0x7FF;
611 }
612 0 ▶ if (gb->apu.shadow_sweep_sample_length + gb->apu.sweep_length_addend > 0x7FF && !(gb->io_registers[GB_IO_NR10] & 8)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.shadow_sweep_sample_length + gb->apu.sweep_length_addend > 0x7FF && !(gb->io_registers[GB_IO_NR10] & 8)) { 0 0 0
if (gb->apu.shadow_sweep_sample_length + gb->apu.sweep_length_addend > 0x7FF && !(gb->io_registers[GB_IO_NR10] & 8)) { 0 0 0
613 0 gb->apu.is_active[GB_SQUARE_1] = false;
614 0 update_sample(gb, GB_SQUARE_1, 0, gb->apu.square_sweep_calculate_countdown * 2 - cycles);
615 }
616 0 gb->apu.channel1_completed_addend = gb->apu.sweep_length_addend;
617 0 }
618  
619 543 static void trigger_sweep_calculation(GB_gameboy_t *gb)
620 {
621 543 ▶ if ((gb->io_registers[GB_IO_NR10] & 0x70) && gb->apu.square_sweep_countdown == 7) {
Branch: 2 branches
BranchTrueFalseTotal
if ((gb->io_registers[GB_IO_NR10] & 0x70) && gb->apu.square_sweep_countdown == 7) { 0 543 543
if ((gb->io_registers[GB_IO_NR10] & 0x70) && gb->apu.square_sweep_countdown == 7) { 0 0 0
622 0 ▶ if (gb->io_registers[GB_IO_NR10] & 0x07) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_NR10] & 0x07) { 0 0 0
623 0 gb->apu.square_channels[GB_SQUARE_1].sample_length =
624 0 gb->apu.sweep_length_addend + gb->apu.shadow_sweep_sample_length + !!(gb->io_registers[GB_IO_NR10] & 0x8);
625 0 gb->apu.square_channels[GB_SQUARE_1].sample_length &= 0x7FF;
626 }
627 0 ▶ if (gb->apu.channel_1_restart_hold == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.channel_1_restart_hold == 0) { 0 0 0
628 0 gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length;
629 0 gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7);
630 }
631
632 /* Recalculation and overflow check only occurs after a delay */
633 0 gb->apu.square_sweep_calculate_countdown = gb->io_registers[GB_IO_NR10] & 0x7;
634 // TODO: this is a hack because DIV write timing is inaccurate. Will probably break on odd mode.
635 0 gb->apu.square_sweep_calculate_countdown_reload_timer = 1 + gb->apu.lf_div;
636 0 ▶ if (!gb->cgb_double_speed && gb->during_div_write) {
Branch: 2 branches
BranchTrueFalseTotal
if (!gb->cgb_double_speed && gb->during_div_write) { 0 0 0
if (!gb->cgb_double_speed && gb->during_div_write) { 0 0 0
637 0 gb->apu.square_sweep_calculate_countdown_reload_timer = 1;
638 }
639 0 gb->apu.unshifted_sweep = !(gb->io_registers[GB_IO_NR10] & 0x7);
640 0 gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7;
641 0 ▶ if (gb->apu.square_sweep_calculate_countdown == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_sweep_calculate_countdown == 0) { 0 0 0
642 0 gb->apu.square_sweep_instant_calculation_done = true;
643 }
644 }
645 543 }
646  
647 0 noinline void GB_apu_delayed_envelope_tick(GB_gameboy_t *gb)
648 {
649 0 gb->apu.pending_envelope_tick = false;
650 0 ▶ if (!gb->apu.global_enable) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.global_enable) return; 0 0 0
651
652 0 GB_apu_run(gb, true);
653 0 gb->apu.pcm_mask[0] = gb->apu.pcm_mask[1] = 0xFF;
654  
655  
656 0 ▶ unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { 0 0 0
657 0 ▶ if (gb->apu.square_channels[i].envelope_clock.clock) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[i].envelope_clock.clock) { 0 0 0
658 0 tick_square_envelope(gb, i);
659 }
660 }
661
662 0 ▶ if (gb->apu.noise_channel.envelope_clock.clock) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.envelope_clock.clock) { 0 0 0
663 0 tick_noise_envelope(gb);
664 }
665 }
666  
667 2459 noinline void GB_apu_div_event(GB_gameboy_t *gb)
668 {
669 2459 GB_apu_run(gb, true);
670 2459 gb->apu.pcm_mask[0] = gb->apu.pcm_mask[1] = 0xFF;
671  
672 2459 ▶ if (!gb->apu.global_enable) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.global_enable) return; 269 2190 2459
673 2190 ▶ if (gb->apu.skip_div_event == GB_SKIP_DIV_EVENT_SKIP) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.skip_div_event == GB_SKIP_DIV_EVENT_SKIP) { 7 2183 2190
674 7 gb->apu.skip_div_event = GB_SKIP_DIV_EVENT_SKIPPED;
675 7 return;
676 }
677 2183 ▶ if (gb->apu.skip_div_event == GB_SKIP_DIV_EVENT_SKIPPED) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.skip_div_event == GB_SKIP_DIV_EVENT_SKIPPED) { 7 2176 2183
678 7 gb->apu.skip_div_event = GB_SKIP_DIV_EVENT_INACTIVE;
679 }
680 else {
681 2176 gb->apu.div_divider++;
682 }
683  
684 2183 ▶ if ((gb->apu.div_divider & 7) == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.div_divider & 7) == 7) { 270 1913 2183
685 810 ▶ unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { 540 270 810
686 540 ▶ if (!gb->apu.square_channels[i].envelope_clock.clock) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.square_channels[i].envelope_clock.clock) { 540 0 540
687 540 gb->apu.square_channels[i].volume_countdown--;
688 540 gb->apu.square_channels[i].volume_countdown &= 7;
689 }
690 }
691 270 ▶ if (!gb->apu.noise_channel.envelope_clock.clock) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.noise_channel.envelope_clock.clock) { 270 0 270
692 270 gb->apu.noise_channel.volume_countdown--;
693 270 gb->apu.noise_channel.volume_countdown &= 7;
694 }
695 }
696  
697 2183 ▶ if (gb->cgb_double_speed && (gb->model == GB_MODEL_CGB_D || gb->model == GB_MODEL_CGB_E)) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->cgb_double_speed && (gb->model == GB_MODEL_CGB_D || gb->model == GB_MODEL_CGB_E)) { 0 2183 2183
if (gb->cgb_double_speed && (gb->model == GB_MODEL_CGB_D || gb->model == GB_MODEL_CGB_E)) { 0 0 0
if (gb->cgb_double_speed && (gb->model == GB_MODEL_CGB_D || gb->model == GB_MODEL_CGB_E)) { 0 0 0
698 0 gb->apu.pending_envelope_tick = true;
699 }
700 else {
701 6549 ▶ unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { 4366 2183 6549
702 4366 ▶ if (gb->apu.square_channels[i].envelope_clock.clock) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[i].envelope_clock.clock) { 21 4345 4366
703 21 tick_square_envelope(gb, i);
704 }
705 }
706
707 2183 ▶ if (gb->apu.noise_channel.envelope_clock.clock) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.envelope_clock.clock) { 0 2183 2183
708 0 tick_noise_envelope(gb);
709 }
710 }
711
712 2183 ▶ if ((gb->apu.div_divider & 1) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.div_divider & 1) == 1) { 1092 1091 2183
713 3276 ▶ unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { 2184 1092 3276
714 2184 ▶ if (gb->apu.square_channels[i].length_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[i].length_enabled) { 0 2184 2184
715 0 ▶ if (gb->apu.square_channels[i].pulse_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[i].pulse_length) { 0 0 0
716 0 ▶ if (!--gb->apu.square_channels[i].pulse_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (!--gb->apu.square_channels[i].pulse_length) { 0 0 0
717 0 gb->apu.is_active[i] = false;
718 0 update_sample(gb, i, 0, 0);
719 }
720 }
721 }
722 }
723  
724 1092 ▶ if (gb->apu.wave_channel.length_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.length_enabled) { 0 1092 1092
725 0 ▶ if (gb->apu.wave_channel.pulse_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.pulse_length) { 0 0 0
726 0 ▶ if (!--gb->apu.wave_channel.pulse_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (!--gb->apu.wave_channel.pulse_length) { 0 0 0
727 0 ▶ if (gb->apu.is_active[GB_WAVE] && gb->model > GB_MODEL_CGB_E) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.is_active[GB_WAVE] && gb->model > GB_MODEL_CGB_E) { 0 0 0
if (gb->apu.is_active[GB_WAVE] && gb->model > GB_MODEL_CGB_E) { 0 0 0
728 0 ▶ if (gb->apu.wave_channel.sample_countdown == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.sample_countdown == 0) { 0 0 0
729 0 gb->apu.wave_channel.current_sample_byte =
730 0 gb->io_registers[GB_IO_WAV_START + (((gb->apu.wave_channel.current_sample_index + 1) & 0xF) >> 1)];
731 }
732 0 ▶ else if (gb->apu.wave_channel.sample_countdown == 9) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->apu.wave_channel.sample_countdown == 9) { 0 0 0
733 // TODO: wtf?
734 0 gb->apu.wave_channel.current_sample_byte = gb->io_registers[GB_IO_WAV_START];
735 }
736 }
737 0 gb->apu.is_active[GB_WAVE] = false;
738 0 update_sample(gb, GB_WAVE, 0, 0);
739 }
740 }
741 }
742  
743 1092 ▶ if (gb->apu.noise_channel.length_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.length_enabled) { 0 1092 1092
744 0 ▶ if (gb->apu.noise_channel.pulse_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.pulse_length) { 0 0 0
745 0 ▶ if (!--gb->apu.noise_channel.pulse_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (!--gb->apu.noise_channel.pulse_length) { 0 0 0
746 0 gb->apu.is_active[GB_NOISE] = false;
747 0 update_sample(gb, GB_NOISE, 0, 0);
748 }
749 }
750 }
751 }
752  
753 2183 ▶ if ((gb->apu.div_divider & 3) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.div_divider & 3) == 3) { 543 1640 2183
754 543 gb->apu.square_sweep_countdown++;
755 543 gb->apu.square_sweep_countdown &= 7;
756 543 trigger_sweep_calculation(gb);
757 }
758 }
759  
760 2462 noinline void GB_apu_div_secondary_event(GB_gameboy_t *gb)
761 {
762 2462 GB_apu_run(gb, true);
763 2462 gb->apu.pcm_mask[0] = gb->apu.pcm_mask[1] = 0xFF;
764  
765 2462 ▶ if (!gb->apu.global_enable) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.global_enable) return; 276 2186 2462
766 6558 ▶ unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { 4372 2186 6558
767 4372 ▶ uint8_t nrx2 = gb->io_registers[i == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22];
Branch: 1 branch
BranchTrueFalseTotal
uint8_t nrx2 = gb->io_registers[i == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22]; 2186 2186 4372
768 4372 ▶ if (gb->apu.is_active[i] && gb->apu.square_channels[i].volume_countdown == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.is_active[i] && gb->apu.square_channels[i].volume_countdown == 0) { 557 3815 4372
if (gb->apu.is_active[i] && gb->apu.square_channels[i].volume_countdown == 0) { 22 535 557
769 22 set_envelope_clock(&gb->apu.square_channels[i].envelope_clock,
770 22 (gb->apu.square_channels[i].volume_countdown = nrx2 & 7),
771 22 nrx2 & 8,
772 22 gb->apu.square_channels[i].current_volume);
773  
774 }
775 }
776
777 2186 ▶ if (gb->apu.is_active[GB_NOISE] && gb->apu.noise_channel.volume_countdown == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.is_active[GB_NOISE] && gb->apu.noise_channel.volume_countdown == 0) { 0 2186 2186
if (gb->apu.is_active[GB_NOISE] && gb->apu.noise_channel.volume_countdown == 0) { 0 0 0
778 0 set_envelope_clock(&gb->apu.noise_channel.envelope_clock,
779 0 (gb->apu.noise_channel.volume_countdown = gb->io_registers[GB_IO_NR42] & 7),
780 0 gb->io_registers[GB_IO_NR42] & 8,
781 0 gb->apu.noise_channel.current_volume);
782 }
783 }
784  
785 0 static void update_lfsr(GB_gameboy_t *gb, unsigned cycles_offset)
786 {
787 0 gb->apu.noise_channel.current_lfsr_sample = gb->apu.noise_channel.lfsr & 1;
788 0 ▶ if (gb->apu.is_active[GB_NOISE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[GB_NOISE]) { 0 0 0
789 0 update_sample(gb, GB_NOISE,
790 0 ▶ gb->apu.noise_channel.current_lfsr_sample ?
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.noise_channel.current_lfsr_sample ? 0 0 0
791 0 gb->apu.noise_channel.current_volume : 0,
792 cycles_offset);
793 }
794 0 }
795  
796 0 static void step_lfsr(GB_gameboy_t *gb, unsigned cycles_offset)
797 {
798 0 gb->apu.lfsr_bit_7_before_step = gb->apu.noise_channel.lfsr & 0x80;
799 0 ▶ unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000;
Branch: 1 branch
BranchTrueFalseTotal
unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; 0 0 0
800 0 bool new_high_bit = (gb->apu.noise_channel.lfsr ^ (gb->apu.noise_channel.lfsr >> 1) ^ 1) & 1;
801 0 gb->apu.noise_channel.lfsr >>= 1;
802
803 0 ▶ if (new_high_bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_high_bit) { 0 0 0
804 0 gb->apu.noise_channel.lfsr |= high_bit_mask;
805 }
806 else {
807 /* This code is not redundent, it's relevant when switching LFSR widths */
808 0 gb->apu.noise_channel.lfsr &= ~high_bit_mask;
809 }
810
811 0 update_lfsr(gb, cycles_offset);
812 0 gb->apu.lfsr_stepped_in_narrow = gb->apu.noise_channel.narrow;
813 0 }
814  
815 5048319 void GB_apu_run(GB_gameboy_t *gb, bool force)
816 {
817 5048319 uint32_t clock_rate = GB_get_clock_rate(gb);
818 5048319 bool orig_force = force;
819
820 5048402 restart:;
821 5048402 uint16_t cycles = gb->apu.apu_cycles;
822  
823 5048402 ▶ if (force ||
Branch: 1 branch
BranchTrueFalseTotal
if (force || 5043238 5164 5048402
824 5043238 ▶ (cycles + gb->apu_output.cycles_since_render >= gb->apu_output.max_cycles_per_sample) ||
Branch: 1 branch
BranchTrueFalseTotal
(cycles + gb->apu_output.cycles_since_render >= gb->apu_output.max_cycles_per_sample) || 4878305 164933 5043238
825 4878305 ▶ (gb->apu_output.sample_cycles >= clock_rate) ||
Branch: 1 branch
BranchTrueFalseTotal
(gb->apu_output.sample_cycles >= clock_rate) || 4831042 47263 4878305
826 4831042 ▶ (gb->apu.square_sweep_calculate_countdown || gb->apu.channel_1_restart_hold || gb->apu.square_sweep_calculate_countdown_reload_timer) ||
Branch: 3 branches
BranchTrueFalseTotal
(gb->apu.square_sweep_calculate_countdown || gb->apu.channel_1_restart_hold || gb->apu.square_sweep_calculate_countdown_reload_timer) || 4831042 0 4831042
(gb->apu.square_sweep_calculate_countdown || gb->apu.channel_1_restart_hold || gb->apu.square_sweep_calculate_countdown_reload_timer) || 4831037 5 4831042
(gb->apu.square_sweep_calculate_countdown || gb->apu.channel_1_restart_hold || gb->apu.square_sweep_calculate_countdown_reload_timer) || 4831037 0 4831037
827 4831037 ▶ (gb->model <= GB_MODEL_CGB_E && (gb->apu.wave_channel.bugged_read_countdown || (gb->apu.wave_channel.enable && gb->apu.wave_channel.pulsed)))) {
Branch: 4 branches
BranchTrueFalseTotal
(gb->model <= GB_MODEL_CGB_E && (gb->apu.wave_channel.bugged_read_countdown || (gb->apu.wave_channel.enable && gb->apu.wave_channel.pulsed)))) { 4831037 0 4831037
(gb->model <= GB_MODEL_CGB_E && (gb->apu.wave_channel.bugged_read_countdown || (gb->apu.wave_channel.enable && gb->apu.wave_channel.pulsed)))) { 4831037 0 4831037
(gb->model <= GB_MODEL_CGB_E && (gb->apu.wave_channel.bugged_read_countdown || (gb->apu.wave_channel.enable && gb->apu.wave_channel.pulsed)))) { 0 4831037 4831037
(gb->model <= GB_MODEL_CGB_E && (gb->apu.wave_channel.bugged_read_countdown || (gb->apu.wave_channel.enable && gb->apu.wave_channel.pulsed)))) { 0 0 0
828 217365 force = true;
829 }
830 5048402 ▶ if (!force) {
Branch: 1 branch
BranchTrueFalseTotal
if (!force) { 4831037 217365 5048402
831 4831037 return;
832 }
833
834 /* Force renders to never be more than max_cycles_per_sample apart by spliting runs. */
835 217365 ▶ while (cycles + gb->apu_output.cycles_since_render > gb->apu_output.max_cycles_per_sample) {
Branch: 1 branch
BranchTrueFalseTotal
while (cycles + gb->apu_output.cycles_since_render > gb->apu_output.max_cycles_per_sample) { 96 217269 217365
836 /* We're already past max_cycles_per_sample. This can happen when changing clock rates, etc.
837 Let this sample render normally. */
838 96 ▶ if (unlikely(gb->apu_output.cycles_since_render > gb->apu_output.max_cycles_per_sample)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->apu_output.cycles_since_render > gb->apu_output.max_cycles_per_sample)) break; 12 84 96
839
840 84 gb->apu.apu_cycles = gb->apu_output.max_cycles_per_sample - gb->apu_output.cycles_since_render;
841
842 84 ▶ if (gb->apu.apu_cycles) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.apu_cycles) { 83 1 84
843 // Run for just enough cycles to reach max_cycles_per_sample
844 83 cycles -= gb->apu.apu_cycles;
845 83 GB_apu_run(gb, true);
846 // Re-evaluate force if needed
847 83 ▶ if (!orig_force) {
Branch: 1 branch
BranchTrueFalseTotal
if (!orig_force) { 83 0 83
848 83 force = false;
849 83 gb->apu.apu_cycles = cycles;
850 83 goto restart;
851 }
852 // Check if we need another batch
853 0 continue;
854 }
855
856 // Render if needed
857 1 ▶ if (gb->apu_output.sample_cycles >= clock_rate) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.sample_cycles >= clock_rate) { 0 1 1
858 0 gb->apu_output.sample_cycles -= clock_rate;
859 0 render(gb);
860 }
861 1 break;
862 }
863  
864 217282 gb->apu.apu_cycles = 0;
865 217282 ▶ if (!cycles) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cycles) { 218 217064 217282
866 /* This can happen in pre-CGB stop mode */
867 218 ▶ while (unlikely(gb->apu_output.sample_cycles >= clock_rate)) {
Branch: 1 branch
BranchTrueFalseTotal
while (unlikely(gb->apu_output.sample_cycles >= clock_rate)) { 0 218 218
868 0 gb->apu_output.sample_cycles -= clock_rate;
869 0 render(gb);
870 }
871 218 return;
872 }
873
874 217064 ▶ if (unlikely(gb->apu.wave_channel.bugged_read_countdown)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->apu.wave_channel.bugged_read_countdown)) { 0 217064 217064
875 0 uint16_t cycles_left = cycles;
876 0 ▶ while (cycles_left) {
Branch: 1 branch
BranchTrueFalseTotal
while (cycles_left) { 0 0 0
877 0 cycles_left--;
878 0 ▶ if (--gb->apu.wave_channel.bugged_read_countdown == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--gb->apu.wave_channel.bugged_read_countdown == 0) { 0 0 0
879 0 gb->apu.wave_channel.current_sample_byte =
880 0 gb->io_registers[GB_IO_WAV_START + (gb->address_bus & 0xF)];
881 0 ▶ if (gb->apu.is_active[GB_WAVE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[GB_WAVE]) { 0 0 0
882 0 update_wave_sample(gb, 0);
883 }
884 0 break;
885 }
886 }
887 }
888
889 217064 bool start_ch4 = false;
890 217064 ▶ if (likely(!gb->stopped || GB_is_cgb(gb))) {
Branch: 2 branches
BranchTrueFalseTotal
if (likely(!gb->stopped || GB_is_cgb(gb))) { 0 217064 217064
if (likely(!gb->stopped || GB_is_cgb(gb))) { 0 0 0
891 217064 ▶ if (gb->apu.noise_channel.dmg_delayed_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.dmg_delayed_start) { 0 217064 217064
892 0 ▶ if (gb->apu.noise_channel.dmg_delayed_start == cycles) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.dmg_delayed_start == cycles) { 0 0 0
893 0 gb->apu.noise_channel.dmg_delayed_start = 0;
894 0 start_ch4 = true;
895 }
896 0 ▶ else if (gb->apu.noise_channel.dmg_delayed_start > cycles) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->apu.noise_channel.dmg_delayed_start > cycles) { 0 0 0
897 0 gb->apu.noise_channel.dmg_delayed_start -= cycles;
898 }
899 else {
900 /* Split it into two */
901 0 cycles -= gb->apu.noise_channel.dmg_delayed_start;
902 0 gb->apu.apu_cycles = gb->apu.noise_channel.dmg_delayed_start;
903 0 GB_apu_run(gb, true);
904 }
905 }
906 /* To align the square signal to 1MHz */
907 217064 gb->apu.lf_div ^= cycles & 1;
908 217064 gb->apu.noise_channel.alignment += cycles;
909
910 217064 unsigned sweep_cycles = cycles / 2;
911 217064 ▶ if ((cycles & 1) && !gb->apu.lf_div) {
Branch: 2 branches
BranchTrueFalseTotal
if ((cycles & 1) && !gb->apu.lf_div) { 0 217064 217064
if ((cycles & 1) && !gb->apu.lf_div) { 0 0 0
912 0 sweep_cycles++;
913 }
914  
915 217064 ▶ if (gb->apu.square_sweep_calculate_countdown_reload_timer > sweep_cycles) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_sweep_calculate_countdown_reload_timer > sweep_cycles) { 0 217064 217064
916 0 gb->apu.square_sweep_calculate_countdown_reload_timer -= sweep_cycles;
917 0 sweep_cycles = 0;
918 }
919 else {
920 217064 ▶ if (gb->apu.square_sweep_calculate_countdown_reload_timer && !gb->apu.square_sweep_calculate_countdown && gb->apu.square_sweep_instant_calculation_done) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->apu.square_sweep_calculate_countdown_reload_timer && !gb->apu.square_sweep_calculate_countdown && gb->apu.square_sweep_instant_calculation_done) { 0 217064 217064
if (gb->apu.square_sweep_calculate_countdown_reload_timer && !gb->apu.square_sweep_calculate_countdown && gb->apu.square_sweep_instant_calculation_done) { 0 0 0
if (gb->apu.square_sweep_calculate_countdown_reload_timer && !gb->apu.square_sweep_calculate_countdown && gb->apu.square_sweep_instant_calculation_done) { 0 0 0
921 0 sweep_calculation_done(gb, cycles);
922 }
923 217064 gb->apu.square_sweep_instant_calculation_done = false;
924 217064 sweep_cycles -= gb->apu.square_sweep_calculate_countdown_reload_timer;
925 217064 gb->apu.square_sweep_calculate_countdown_reload_timer = 0;
926 }
927
928 217064 ▶ if (gb->apu.square_sweep_calculate_countdown &&
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_sweep_calculate_countdown && 0 217064 217064
929 0 ▶ (((gb->io_registers[GB_IO_NR10] & 7) || gb->apu.unshifted_sweep))) { // Calculation is paused if the lower bits are 0
Branch: 2 branches
BranchTrueFalseTotal
(((gb->io_registers[GB_IO_NR10] & 7) || gb->apu.unshifted_sweep))) { // Calculation is paused if the lower bits are 0 0 0 0
(((gb->io_registers[GB_IO_NR10] & 7) || gb->apu.unshifted_sweep))) { // Calculation is paused if the lower bits are 0 0 0 0
930 0 ▶ if (gb->apu.square_sweep_calculate_countdown > sweep_cycles) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_sweep_calculate_countdown > sweep_cycles) { 0 0 0
931 0 gb->apu.square_sweep_calculate_countdown -= sweep_cycles;
932 }
933 else {
934 0 gb->apu.square_sweep_calculate_countdown = 0;
935 0 sweep_calculation_done(gb, cycles);
936 }
937 }
938
939 217064 ▶ if (gb->apu.channel_1_restart_hold) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.channel_1_restart_hold) { 6 217058 217064
940 6 ▶ if (gb->apu.channel_1_restart_hold > cycles) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.channel_1_restart_hold > cycles) { 2 4 6
941 2 gb->apu.channel_1_restart_hold -= cycles;
942 }
943 else {
944 4 gb->apu.channel_1_restart_hold = 0;
945 }
946 }
947  
948 651192 ▶ unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { 434128 217064 651192
949 434128 ▶ if (gb->apu.is_active[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[i]) { 49061 385067 434128
950 49061 uint16_t cycles_left = cycles;
951 49061 ▶ if (unlikely(gb->apu.square_channels[i].delay)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->apu.square_channels[i].delay)) { 9 49052 49061
952 9 ▶ if (gb->apu.square_channels[i].delay < cycles_left) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[i].delay < cycles_left) { 4 5 9
953 4 gb->apu.square_channels[i].delay = 0;
954 }
955 else {
956 5 gb->apu.square_channels[i].delay -= cycles_left;
957 }
958 }
959 65789 ▶ while (unlikely(cycles_left > gb->apu.square_channels[i].sample_countdown)) {
Branch: 1 branch
BranchTrueFalseTotal
while (unlikely(cycles_left > gb->apu.square_channels[i].sample_countdown)) { 16728 49061 65789
960 16728 cycles_left -= gb->apu.square_channels[i].sample_countdown + 1;
961 16728 gb->apu.square_channels[i].sample_countdown = (gb->apu.square_channels[i].sample_length ^ 0x7FF) * 2 + 1;
962 16728 gb->apu.square_channels[i].current_sample_index++;
963 16728 gb->apu.square_channels[i].current_sample_index &= 0x7;
964 16728 gb->apu.square_channels[i].sample_surpressed = false;
965 16728 ▶ if (cycles_left == 0 && gb->apu.samples[i] == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (cycles_left == 0 && gb->apu.samples[i] == 0) { 714 16014 16728
if (cycles_left == 0 && gb->apu.samples[i] == 0) { 410 304 714
966 410 ▶ gb->apu.pcm_mask[0] &= i == GB_SQUARE_1? 0xF0 : 0x0F;
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.pcm_mask[0] &= i == GB_SQUARE_1? 0xF0 : 0x0F; 410 0 410
967 }
968 16728 gb->apu.square_channels[i].did_tick = true;
969 16728 update_square_sample(gb, i, cycles - cycles_left);
970  
971 16728 ▶ uint8_t duty = gb->io_registers[i == GB_SQUARE_1? GB_IO_NR11 :GB_IO_NR21] >> 6;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t duty = gb->io_registers[i == GB_SQUARE_1? GB_IO_NR11 :GB_IO_NR21] >> 6; 16728 0 16728
972 16728 uint8_t edge_sample_index = inline_const(uint8_t[], {7, 7, 5, 1})[duty];
973 16728 ▶ if (gb->apu.square_channels[i].current_sample_index == edge_sample_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[i].current_sample_index == edge_sample_index) { 2091 14637 16728
974 2091 gb->apu_output.edge_triggered[i] = true;
975 }
976 }
977 49061 gb->apu.square_channels[i].just_reloaded = cycles_left == 0;
978 49061 ▶ if (cycles_left) {
Branch: 1 branch
BranchTrueFalseTotal
if (cycles_left) { 48347 714 49061
979 48347 gb->apu.square_channels[i].sample_countdown -= cycles_left;
980 }
981 }
982 }
983  
984 217064 gb->apu.wave_channel.wave_form_just_read = false;
985 217064 ▶ if (gb->apu.is_active[GB_WAVE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[GB_WAVE]) { 0 217064 217064
986 0 uint16_t cycles_left = cycles;
987 0 ▶ while (unlikely(cycles_left > gb->apu.wave_channel.sample_countdown)) {
Branch: 1 branch
BranchTrueFalseTotal
while (unlikely(cycles_left > gb->apu.wave_channel.sample_countdown)) { 0 0 0
988 0 cycles_left -= gb->apu.wave_channel.sample_countdown + 1;
989 0 gb->apu.wave_channel.sample_countdown = gb->apu.wave_channel.sample_length ^ 0x7FF;
990 0 gb->apu.wave_channel.current_sample_index++;
991 0 gb->apu.wave_channel.current_sample_index &= 0x1F;
992 0 gb->apu.wave_channel.current_sample_byte =
993 0 gb->io_registers[GB_IO_WAV_START + (gb->apu.wave_channel.current_sample_index >> 1)];
994 0 update_wave_sample(gb, cycles - cycles_left);
995 0 gb->apu.wave_channel.wave_form_just_read = true;
996 0 ▶ if (gb->apu.wave_channel.current_sample_index == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.current_sample_index == 0) { 0 0 0
997 0 gb->apu_output.edge_triggered[GB_WAVE] = true;
998 }
999 }
1000 0 ▶ if (cycles_left) {
Branch: 1 branch
BranchTrueFalseTotal
if (cycles_left) { 0 0 0
1001 0 gb->apu.wave_channel.sample_countdown -= cycles_left;
1002 0 gb->apu.wave_channel.wave_form_just_read = false;
1003 }
1004 }
1005 217064 ▶ else if (gb->apu.wave_channel.enable && gb->apu.wave_channel.pulsed && gb->model <= GB_MODEL_CGB_E) {
Branch: 3 branches
BranchTrueFalseTotal
else if (gb->apu.wave_channel.enable && gb->apu.wave_channel.pulsed && gb->model <= GB_MODEL_CGB_E) { 0 217064 217064
else if (gb->apu.wave_channel.enable && gb->apu.wave_channel.pulsed && gb->model <= GB_MODEL_CGB_E) { 0 0 0
else if (gb->apu.wave_channel.enable && gb->apu.wave_channel.pulsed && gb->model <= GB_MODEL_CGB_E) { 0 0 0
1006 0 uint16_t cycles_left = cycles;
1007 0 ▶ while (unlikely(cycles_left > gb->apu.wave_channel.sample_countdown)) {
Branch: 1 branch
BranchTrueFalseTotal
while (unlikely(cycles_left > gb->apu.wave_channel.sample_countdown)) { 0 0 0
1008 0 cycles_left -= gb->apu.wave_channel.sample_countdown + 1;
1009 0 gb->apu.wave_channel.sample_countdown = gb->apu.wave_channel.sample_length ^ 0x7FF;
1010 0 ▶ if (cycles_left) {
Branch: 1 branch
BranchTrueFalseTotal
if (cycles_left) { 0 0 0
1011 0 gb->apu.wave_channel.current_sample_byte =
1012 0 gb->io_registers[GB_IO_WAV_START + (gb->address_bus & 0xF)];
1013 }
1014 else {
1015 0 gb->apu.wave_channel.bugged_read_countdown = 1;
1016 }
1017 }
1018 0 ▶ if (cycles_left) {
Branch: 1 branch
BranchTrueFalseTotal
if (cycles_left) { 0 0 0
1019 0 gb->apu.wave_channel.sample_countdown -= cycles_left;
1020 }
1021 0 ▶ if (gb->apu.wave_channel.sample_countdown == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.sample_countdown == 0) { 0 0 0
1022 0 gb->apu.wave_channel.bugged_read_countdown = 2;
1023 }
1024 }
1025
1026 // TODO: verify these conditions one a DMG somehow
1027 217064 ▶ if (gb->apu.noise_counter_active || gb->apu.noise_background_counter_active) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.noise_counter_active || gb->apu.noise_background_counter_active) { 217064 0 217064
if (gb->apu.noise_counter_active || gb->apu.noise_background_counter_active) { 0 217064 217064
1028 0 uint16_t cycles_left = cycles;
1029 0 unsigned divisor = (gb->io_registers[GB_IO_NR43] & 0x07) << 2;
1030 0 ▶ if (!divisor) divisor = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (!divisor) divisor = 2; 0 0 0
1031 0 ▶ if (gb->apu.noise_channel.counter_countdown == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.counter_countdown == 0) { 0 0 0
1032 0 gb->apu.noise_channel.counter_countdown = divisor;
1033 }
1034 // This while doesn't get an unlikely because the noise channel steps frequently enough
1035 0 ▶ while (cycles_left >= gb->apu.noise_channel.counter_countdown) {
Branch: 1 branch
BranchTrueFalseTotal
while (cycles_left >= gb->apu.noise_channel.counter_countdown) { 0 0 0
1036 0 cycles_left -= gb->apu.noise_channel.counter_countdown;
1037 0 gb->apu.noise_channel.counter_countdown = divisor;
1038 0 uint16_t mask = 1 << (gb->io_registers[GB_IO_NR43] >> 4);
1039 0 bool old_bit = gb->apu.noise_channel.counter & mask;
1040 0 gb->apu.noise_channel.counter++;
1041 0 gb->apu.noise_channel.counter &= 0x3FFF;
1042 0 gb->apu.noise_channel.did_step_counter = true;
1043 0 bool new_bit = gb->apu.noise_channel.counter & mask;
1044  
1045 /* Step LFSR */
1046 0 ▶ if (new_bit && !old_bit && gb->apu.is_active[GB_NOISE]) {
Branch: 3 branches
BranchTrueFalseTotal
if (new_bit && !old_bit && gb->apu.is_active[GB_NOISE]) { 0 0 0
if (new_bit && !old_bit && gb->apu.is_active[GB_NOISE]) { 0 0 0
if (new_bit && !old_bit && gb->apu.is_active[GB_NOISE]) { 0 0 0
1047 0 ▶ if (cycles_left == 0 && gb->apu.samples[GB_NOISE] == 0 && !gb->cgb_double_speed) {
Branch: 3 branches
BranchTrueFalseTotal
if (cycles_left == 0 && gb->apu.samples[GB_NOISE] == 0 && !gb->cgb_double_speed) { 0 0 0
if (cycles_left == 0 && gb->apu.samples[GB_NOISE] == 0 && !gb->cgb_double_speed) { 0 0 0
if (cycles_left == 0 && gb->apu.samples[GB_NOISE] == 0 && !gb->cgb_double_speed) { 0 0 0
1048 0 gb->apu.pcm_mask[1] &= 0x0F;
1049 }
1050 0 step_lfsr(gb, cycles - cycles_left);
1051 }
1052 }
1053 0 ▶ if (cycles_left) {
Branch: 1 branch
BranchTrueFalseTotal
if (cycles_left) { 0 0 0
1054 0 ▶ if (likely(gb->apu.noise_counter_active || gb->apu.noise_background_counter_active)) {
Branch: 2 branches
BranchTrueFalseTotal
if (likely(gb->apu.noise_counter_active || gb->apu.noise_background_counter_active)) { 0 0 0
if (likely(gb->apu.noise_counter_active || gb->apu.noise_background_counter_active)) { 0 0 0
1055 0 gb->apu.noise_channel.counter_countdown -= cycles_left;
1056 0 gb->apu.noise_channel.countdown_reloaded = false;
1057 }
1058 }
1059 else {
1060 0 gb->apu.noise_channel.countdown_reloaded = true;
1061 0 gb->apu_output.edge_triggered[GB_NOISE] = true;
1062 }
1063 }
1064 }
1065  
1066 217064 ▶ if (gb->apu_output.sample_rate) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.sample_rate) { 217064 0 217064
1067 217064 gb->apu_output.cycles_since_render += cycles;
1068 217064 gb->apu_output.sample_fraction += sample_fraction_multiply(gb, cycles);
1069 assert(gb->apu_output.sample_fraction < (4 << 28));
1070  
1071 217064 ▶ if (gb->apu_output.sample_cycles >= clock_rate) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.sample_cycles >= clock_rate) { 212183 4881 217064
1072 212183 gb->apu_output.sample_cycles -= clock_rate;
1073 212183 render(gb);
1074 }
1075 }
1076 217064 ▶ if (start_ch4) {
Branch: 1 branch
BranchTrueFalseTotal
if (start_ch4) { 0 217064 217064
1077 0 GB_apu_write(gb, GB_IO_NR44, gb->io_registers[GB_IO_NR44] | 0x80);
1078 }
1079 }
1080  
1081 8 void GB_apu_init(GB_gameboy_t *gb)
1082 {
1083 8 memset(&gb->apu, 0, sizeof(gb->apu));
1084 8 gb->apu.apu_cycles_in_2mhz = true;
1085 8 gb->apu.lf_div = 1;
1086 8 gb->apu.wave_channel.shift = 4;
1087 /* APU glitch: When turning the APU on while DIV's bit 4 (or 5 in double speed mode) is on,
1088 the first DIV/APU event is skipped. */
1089 8 ▶ if (gb->div_counter & (gb->cgb_double_speed? 0x2000 : 0x1000)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->div_counter & (gb->cgb_double_speed? 0x2000 : 0x1000)) { 0 8 8
if (gb->div_counter & (gb->cgb_double_speed? 0x2000 : 0x1000)) { 7 1 8
1090 7 gb->apu.skip_div_event = GB_SKIP_DIV_EVENT_SKIP;
1091 7 gb->apu.div_divider = 1;
1092 }
1093 8 gb->apu.square_channels[GB_SQUARE_1].sample_countdown = -1;
1094 8 gb->apu.square_channels[GB_SQUARE_2].sample_countdown = -1;
1095 8 }
1096  
1097 0 uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg)
1098 {
1099 0 GB_apu_run(gb, true);
1100 0 ▶ if (reg == GB_IO_NR52) {
Branch: 1 branch
BranchTrueFalseTotal
if (reg == GB_IO_NR52) { 0 0 0
1101 0 uint8_t value = 0;
1102 0 ▶ for (unsigned i = 0; i < GB_N_CHANNELS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < GB_N_CHANNELS; i++) { 0 0 0
1103 0 value >>= 1;
1104 0 ▶ if (gb->apu.is_active[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[i]) { 0 0 0
1105 0 value |= 0x8;
1106 }
1107 }
1108 0 ▶ if (gb->apu.global_enable) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.global_enable) { 0 0 0
1109 0 value |= 0x80;
1110 }
1111 0 value |= 0x70;
1112 0 return value;
1113 }
1114  
1115 static const char read_mask[GB_IO_WAV_END - GB_IO_NR10 + 1] = {
1116 /* NRX0 NRX1 NRX2 NRX3 NRX4 */
1117 0x80, 0x3F, 0x00, 0xFF, 0xBF, // NR1X
1118 0xFF, 0x3F, 0x00, 0xFF, 0xBF, // NR2X
1119 0x7F, 0xFF, 0x9F, 0xFF, 0xBF, // NR3X
1120 0xFF, 0xFF, 0x00, 0x00, 0xBF, // NR4X
1121 0x00, 0x00, 0x70, 0xFF, 0xFF, // NR5X
1122  
1123 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // Unused
1124 // Wave RAM
1125 0, /* ... */
1126 };
1127  
1128 0 ▶ if (reg >= GB_IO_WAV_START && reg <= GB_IO_WAV_END && gb->apu.is_active[GB_WAVE]) {
Branch: 3 branches
BranchTrueFalseTotal
if (reg >= GB_IO_WAV_START && reg <= GB_IO_WAV_END && gb->apu.is_active[GB_WAVE]) { 0 0 0
if (reg >= GB_IO_WAV_START && reg <= GB_IO_WAV_END && gb->apu.is_active[GB_WAVE]) { 0 0 0
if (reg >= GB_IO_WAV_START && reg <= GB_IO_WAV_END && gb->apu.is_active[GB_WAVE]) { 0 0 0
1129 0 ▶ if (!GB_is_cgb(gb) && !gb->apu.wave_channel.wave_form_just_read) {
Branch: 2 branches
BranchTrueFalseTotal
if (!GB_is_cgb(gb) && !gb->apu.wave_channel.wave_form_just_read) { 0 0 0
if (!GB_is_cgb(gb) && !gb->apu.wave_channel.wave_form_just_read) { 0 0 0
1130 0 return 0xFF;
1131 }
1132 0 ▶ if (gb->model > GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model > GB_MODEL_CGB_E) { 0 0 0
1133 0 return 0xFF;
1134 }
1135 0 reg = GB_IO_WAV_START + gb->apu.wave_channel.current_sample_index / 2;
1136 }
1137  
1138 0 return gb->io_registers[reg] | read_mask[reg - GB_IO_NR10];
1139 }
1140  
1141 0 static noinline void nr10_write_glitch(GB_gameboy_t *gb, uint8_t value)
1142 {
1143 // TODO: Check all of these in APU odd mode
1144 0 ▶ if (gb->model <= GB_MODEL_CGB_C) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_C) { 0 0 0
1145 0 ▶ if (gb->apu.square_sweep_calculate_countdown_reload_timer == 1 && !gb->apu.lf_div) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.square_sweep_calculate_countdown_reload_timer == 1 && !gb->apu.lf_div) { 0 0 0
if (gb->apu.square_sweep_calculate_countdown_reload_timer == 1 && !gb->apu.lf_div) { 0 0 0
1146 0 ▶ if (gb->cgb_double_speed) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_double_speed) { 0 0 0
1147 /* This is some instance-specific data corruption. It might also be affect by revision.
1148 At least for my CGB-0 (haven't tested any other CGB-0s), the '3' case is non-deterministic. */
1149 static const uint8_t corruption[8] = {7, 7, 5, 7, 3, 3, 5, 7}; // Two of my CGB-Cs, CGB-A
1150 // static const uint8_t corruption[8] = {7, 7, 1, 3, 3, 3, 5, 7}; // My other CGB-C, Coffee Bat's CGB-C
1151 // static const uint8_t corruption[8] = {7, 1, 1, 3, 3, 5, 5, 7}; // My CGB-B
1152 // static const uint8_t corruption[8] = {7, 7, 1, *, 3, 3, 5, 7}; // My CGB-0
1153
1154 // static const uint8_t corruption[8] = {7, 5, 1, 3, 3, 1, 5, 7}; // PinoBatch's CGB-B
1155 // static const uint8_t corruption[8] = {7, 5, 1, 3, 3, *, 5, 7}; // GenericHeroGuy CGB-C
1156
1157  
1158 // TODO: How does this affect actual frequency calculation?
1159
1160 0 gb->apu.square_sweep_calculate_countdown = corruption[gb->apu.square_sweep_calculate_countdown & 7];
1161 /* TODO: the value of 1 needs special handling, but it doesn't occur with the instance I'm emulating here */
1162 }
1163 }
1164 0 ▶ else if (gb->apu.square_sweep_calculate_countdown_reload_timer > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->apu.square_sweep_calculate_countdown_reload_timer > 1) { 0 0 0
1165 0 ▶ if (gb->cgb_double_speed) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_double_speed) { 0 0 0
1166 // TODO: How does this affect actual frequency calculation?
1167 0 gb->apu.square_sweep_calculate_countdown = value & 7;
1168 }
1169 }
1170 0 ▶ else if (gb->apu.square_sweep_calculate_countdown) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->apu.square_sweep_calculate_countdown) { 0 0 0
1171 // No clue why 1 is a special case here
1172 0 bool should_zombie_step = false;
1173 0 ▶ if (!(gb->io_registers[GB_IO_NR10] & 7)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(gb->io_registers[GB_IO_NR10] & 7)) { 0 0 0
1174 0 should_zombie_step = gb->apu.lf_div ^ gb->cgb_double_speed;
1175 }
1176 0 ▶ else if (gb->cgb_double_speed && gb->apu.square_sweep_calculate_countdown == 1) {
Branch: 2 branches
BranchTrueFalseTotal
else if (gb->cgb_double_speed && gb->apu.square_sweep_calculate_countdown == 1) { 0 0 0
else if (gb->cgb_double_speed && gb->apu.square_sweep_calculate_countdown == 1) { 0 0 0
1177 0 should_zombie_step = true;
1178 }
1179
1180 0 ▶ if (should_zombie_step) {
Branch: 1 branch
BranchTrueFalseTotal
if (should_zombie_step) { 0 0 0
1181 0 gb->apu.square_sweep_calculate_countdown--;
1182 0 ▶ if (gb->apu.square_sweep_calculate_countdown <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_sweep_calculate_countdown <= 1) { 0 0 0
1183 0 gb->apu.square_sweep_calculate_countdown = 0;
1184 0 sweep_calculation_done(gb, 0);
1185 }
1186 }
1187 }
1188 }
1189 else {
1190 0 ▶ if (gb->apu.square_sweep_calculate_countdown_reload_timer == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_sweep_calculate_countdown_reload_timer == 2) { 0 0 0
1191 // Countdown just reloaded, re-reload it
1192 0 gb->apu.square_sweep_calculate_countdown = value & 0x7;
1193 0 ▶ if (!gb->apu.square_sweep_calculate_countdown) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.square_sweep_calculate_countdown) { 0 0 0
1194 0 gb->apu.square_sweep_calculate_countdown_reload_timer = 0;
1195 }
1196 else {
1197 // TODO: How does this affect actual frequency calculation?
1198 }
1199 }
1200 0 ▶ if ((value & 7) && !(gb->io_registers[GB_IO_NR10] & 7) && !gb->apu.lf_div && gb->apu.square_sweep_calculate_countdown > 1) {
Branch: 4 branches
BranchTrueFalseTotal
if ((value & 7) && !(gb->io_registers[GB_IO_NR10] & 7) && !gb->apu.lf_div && gb->apu.square_sweep_calculate_countdown > 1) { 0 0 0
if ((value & 7) && !(gb->io_registers[GB_IO_NR10] & 7) && !gb->apu.lf_div && gb->apu.square_sweep_calculate_countdown > 1) { 0 0 0
if ((value & 7) && !(gb->io_registers[GB_IO_NR10] & 7) && !gb->apu.lf_div && gb->apu.square_sweep_calculate_countdown > 1) { 0 0 0
if ((value & 7) && !(gb->io_registers[GB_IO_NR10] & 7) && !gb->apu.lf_div && gb->apu.square_sweep_calculate_countdown > 1) { 0 0 0
1201 // TODO: Another odd glitch? Ditto
1202 0 gb->apu.square_sweep_calculate_countdown--;
1203 0 ▶ if (!gb->apu.square_sweep_calculate_countdown) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.square_sweep_calculate_countdown) { 0 0 0
1204 0 sweep_calculation_done(gb, 0);
1205 }
1206 }
1207 }
1208  
1209 0 }
1210  
1211 0 static void prepare_noise_start(GB_gameboy_t *gb)
1212 {
1213 /*
1214 TODO: When restarting a channel right after starting it, before it has the chance to tick the counter, things
1215 behave differently. Only certain behaviors of this edge case are emulated.
1216 */
1217
1218 /*
1219 TODO: Restarting a channel in double speed mode under CGB-C and older is not accurate if the divisor is 0 or 1.
1220 Specifically in the 0 case, the initial LFSR value seems to be deterministic, but dependant on various
1221 parameters. It is neither 0 or the equaly unexplained 0x0055.
1222 */
1223 0 gb->apu.noise_counter_active = gb->io_registers[GB_IO_NR42] & 0xF8; // Resets on APU off and DAC disable
1224 0 bool was_started_with_dac_disabled = gb->apu.noise_started_with_dac_disabled;
1225 0 gb->apu.noise_started_with_dac_disabled = !gb->apu.noise_counter_active;
1226 0 unsigned divisor = (gb->io_registers[GB_IO_NR43] & 0x07);
1227 0 bool was_background_counting = gb->apu.noise_background_counter_active;
1228 0 gb->apu.noise_background_counter_active = true;
1229 0 bool instant_step = false;
1230 0 bool div_1_glitch = false;
1231
1232 0 ▶ if (divisor > 1 && gb->apu.noise_channel.counter_countdown == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (divisor > 1 && gb->apu.noise_channel.counter_countdown == 1) { 0 0 0
if (divisor > 1 && gb->apu.noise_channel.counter_countdown == 1) { 0 0 0
1233 0 gb->apu.noise_channel.counter++;
1234 0 gb->apu.noise_channel.counter &= 0x3FFF;
1235 }
1236 0 ▶ else if (divisor > 1 && gb->apu.noise_channel.counter_countdown == 2 && gb->apu.is_active[GB_NOISE] && gb->model <= GB_MODEL_CGB_C && gb->cgb_double_speed) {
Branch: 5 branches
BranchTrueFalseTotal
else if (divisor > 1 && gb->apu.noise_channel.counter_countdown == 2 && gb->apu.is_active[GB_NOISE] && gb->model <= GB_MODEL_CGB_C && gb->cgb_double_speed) { 0 0 0
else if (divisor > 1 && gb->apu.noise_channel.counter_countdown == 2 && gb->apu.is_active[GB_NOISE] && gb->model <= GB_MODEL_CGB_C && gb->cgb_double_speed) { 0 0 0
else if (divisor > 1 && gb->apu.noise_channel.counter_countdown == 2 && gb->apu.is_active[GB_NOISE] && gb->model <= GB_MODEL_CGB_C && gb->cgb_double_speed) { 0 0 0
else if (divisor > 1 && gb->apu.noise_channel.counter_countdown == 2 && gb->apu.is_active[GB_NOISE] && gb->model <= GB_MODEL_CGB_C && gb->cgb_double_speed) { 0 0 0
else if (divisor > 1 && gb->apu.noise_channel.counter_countdown == 2 && gb->apu.is_active[GB_NOISE] && gb->model <= GB_MODEL_CGB_C && gb->cgb_double_speed) { 0 0 0
1237 0 gb->apu.noise_channel.counter++;
1238 0 gb->apu.noise_channel.counter &= 0x3FFF;
1239 }
1240 0 ▶ else if (gb->apu.noise_channel.counter_countdown == 2 &&
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->apu.noise_channel.counter_countdown == 2 && 0 0 0
1241 0 ▶ (gb->apu.noise_channel.alignment & 3) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
(gb->apu.noise_channel.alignment & 3) == 0 && 0 0 0
1242 0 ▶ gb->apu.is_active[GB_NOISE]) {
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.is_active[GB_NOISE]) { 0 0 0
1243 0 ▶ if (divisor == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (divisor == 0) { 0 0 0
1244 0 divisor = 8;
1245 }
1246 0 ▶ else if (divisor == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (divisor == 1) { 0 0 0
1247 0 ▶ if (!gb->apu.noise_channel.did_step_counter) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.noise_channel.did_step_counter) { 0 0 0
1248 0 div_1_glitch = true;
1249 }
1250
1251 0 uint16_t mask = 1 << (gb->io_registers[GB_IO_NR43] >> 4);
1252 0 bool old_bit = gb->apu.noise_channel.counter & mask;
1253 0 gb->apu.noise_channel.counter++;
1254 0 gb->apu.noise_channel.counter &= 0x3FFF;
1255 0 bool new_bit = gb->apu.noise_channel.counter & mask;
1256
1257 0 ▶ if ((new_bit && !old_bit)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((new_bit && !old_bit)) { 0 0 0
if ((new_bit && !old_bit)) { 0 0 0
1258 0 instant_step = true;
1259 }
1260 }
1261 }
1262 0 ▶ gb->apu.noise_channel.counter_countdown = divisor == 0? 6 : divisor * 4 + 6;
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.noise_channel.counter_countdown = divisor == 0? 6 : divisor * 4 + 6; 0 0 0
1263 0 ▶ if (gb->apu.noise_channel.alignment & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.alignment & 1) { 0 0 0
1264 0 ▶ if (!divisor) {
Branch: 1 branch
BranchTrueFalseTotal
if (!divisor) { 0 0 0
1265 0 ▶ if (gb->model <= GB_MODEL_CGB_C) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_C) { 0 0 0
1266 0 gb->apu.noise_channel.counter_countdown++;
1267 }
1268 0 ▶ else if (was_background_counting) {
Branch: 1 branch
BranchTrueFalseTotal
else if (was_background_counting) { 0 0 0
1269 0 gb->apu.noise_channel.counter_countdown--;
1270 }
1271 else {
1272 0 gb->apu.noise_channel.counter_countdown++;
1273 }
1274 }
1275 else {
1276 0 ▶ if (gb->apu.noise_channel.alignment & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.alignment & 2) { 0 0 0
1277 0 ▶ if (divisor == 1 && !gb->apu.is_active[GB_NOISE]) {
Branch: 2 branches
BranchTrueFalseTotal
if (divisor == 1 && !gb->apu.is_active[GB_NOISE]) { 0 0 0
if (divisor == 1 && !gb->apu.is_active[GB_NOISE]) { 0 0 0
1278 0 gb->apu.noise_channel.counter_countdown++;
1279 }
1280 else {
1281 0 gb->apu.noise_channel.counter_countdown -= 3;
1282 }
1283 }
1284 else {
1285 0 gb->apu.noise_channel.counter_countdown--;
1286 0 ▶ if (divisor == 1 && gb->apu.is_active[GB_NOISE]) {
Branch: 2 branches
BranchTrueFalseTotal
if (divisor == 1 && gb->apu.is_active[GB_NOISE]) { 0 0 0
if (divisor == 1 && gb->apu.is_active[GB_NOISE]) { 0 0 0
1287 0 gb->apu.noise_channel.counter_countdown -= 4;
1288 }
1289 }
1290 }
1291 }
1292 else {
1293 0 ▶ if (divisor) {
Branch: 1 branch
BranchTrueFalseTotal
if (divisor) { 0 0 0
1294 0 ▶ if (gb->apu.noise_channel.alignment & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.alignment & 2) { 0 0 0
1295 0 ▶ if (gb->cgb_double_speed && gb->model <= GB_MODEL_CGB_C && divisor == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->cgb_double_speed && gb->model <= GB_MODEL_CGB_C && divisor == 1) { 0 0 0
if (gb->cgb_double_speed && gb->model <= GB_MODEL_CGB_C && divisor == 1) { 0 0 0
if (gb->cgb_double_speed && gb->model <= GB_MODEL_CGB_C && divisor == 1) { 0 0 0
1296 0 gb->apu.noise_channel.counter_countdown += 2;
1297 }
1298 else {
1299 0 gb->apu.noise_channel.counter_countdown -= 2;
1300 }
1301 }
1302 0 ▶ else if (divisor > 1 && (!gb->cgb_double_speed || gb->model > GB_MODEL_CGB_C)) {
Branch: 3 branches
BranchTrueFalseTotal
else if (divisor > 1 && (!gb->cgb_double_speed || gb->model > GB_MODEL_CGB_C)) { 0 0 0
else if (divisor > 1 && (!gb->cgb_double_speed || gb->model > GB_MODEL_CGB_C)) { 0 0 0
else if (divisor > 1 && (!gb->cgb_double_speed || gb->model > GB_MODEL_CGB_C)) { 0 0 0
1303 0 gb->apu.noise_channel.counter_countdown -= 4;
1304 }
1305 /* TODO: This quirk seems way too specific */
1306 0 ▶ else if (divisor == 1 && gb->apu.is_active[GB_NOISE] && !(gb->io_registers[GB_IO_NR43] & 0xf0)) {
Branch: 3 branches
BranchTrueFalseTotal
else if (divisor == 1 && gb->apu.is_active[GB_NOISE] && !(gb->io_registers[GB_IO_NR43] & 0xf0)) { 0 0 0
else if (divisor == 1 && gb->apu.is_active[GB_NOISE] && !(gb->io_registers[GB_IO_NR43] & 0xf0)) { 0 0 0
else if (divisor == 1 && gb->apu.is_active[GB_NOISE] && !(gb->io_registers[GB_IO_NR43] & 0xf0)) { 0 0 0
1307 0 gb->apu.noise_channel.counter_countdown -= 4;
1308 }
1309 }
1310 0 ▶ else if (gb->cgb_double_speed && gb->model <= GB_MODEL_CGB_C) {
Branch: 2 branches
BranchTrueFalseTotal
else if (gb->cgb_double_speed && gb->model <= GB_MODEL_CGB_C) { 0 0 0
else if (gb->cgb_double_speed && gb->model <= GB_MODEL_CGB_C) { 0 0 0
1311 0 gb->apu.noise_channel.counter_countdown += 2;
1312 }
1313 }
1314
1315 /* Background counting glitches */
1316 /* TODO: Double speed mode not tested */
1317 0 ▶ if (divisor > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (divisor > 1) { 0 0 0
1318 0 ▶ if (!gb->apu.noise_counter_active && !(gb->apu.noise_channel.alignment & 3)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!gb->apu.noise_counter_active && !(gb->apu.noise_channel.alignment & 3)) { 0 0 0
if (!gb->apu.noise_counter_active && !(gb->apu.noise_channel.alignment & 3)) { 0 0 0
1319 0 gb->apu.noise_channel.counter_countdown += 4;
1320 }
1321 }
1322 else {
1323 0 ▶ if (was_background_counting && !gb->apu.is_active[GB_NOISE] && !(gb->apu.noise_channel.alignment & 3)) {
Branch: 3 branches
BranchTrueFalseTotal
if (was_background_counting && !gb->apu.is_active[GB_NOISE] && !(gb->apu.noise_channel.alignment & 3)) { 0 0 0
if (was_background_counting && !gb->apu.is_active[GB_NOISE] && !(gb->apu.noise_channel.alignment & 3)) { 0 0 0
if (was_background_counting && !gb->apu.is_active[GB_NOISE] && !(gb->apu.noise_channel.alignment & 3)) { 0 0 0
1324 0 ▶ if (divisor == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (divisor == 0) { 0 0 0
1325 0 ▶ if (was_started_with_dac_disabled) { // TODO: Why is it different?
Branch: 1 branch
BranchTrueFalseTotal
if (was_started_with_dac_disabled) { // TODO: Why is it different? 0 0 0
1326 0 gb->apu.noise_channel.counter_countdown += 28;
1327 }
1328 }
1329 else {
1330 0 gb->apu.noise_channel.counter_countdown -= 4;
1331 }
1332 }
1333 }
1334
1335 /* TODO: This is weird, is the clock going out of sync? */
1336 0 ▶ if (!divisor && gb->model <= GB_MODEL_CGB_C && was_background_counting && !gb->apu.is_active[GB_NOISE] && gb->cgb_double_speed) {
Branch: 5 branches
BranchTrueFalseTotal
if (!divisor && gb->model <= GB_MODEL_CGB_C && was_background_counting && !gb->apu.is_active[GB_NOISE] && gb->cgb_double_speed) { 0 0 0
if (!divisor && gb->model <= GB_MODEL_CGB_C && was_background_counting && !gb->apu.is_active[GB_NOISE] && gb->cgb_double_speed) { 0 0 0
if (!divisor && gb->model <= GB_MODEL_CGB_C && was_background_counting && !gb->apu.is_active[GB_NOISE] && gb->cgb_double_speed) { 0 0 0
if (!divisor && gb->model <= GB_MODEL_CGB_C && was_background_counting && !gb->apu.is_active[GB_NOISE] && gb->cgb_double_speed) { 0 0 0
if (!divisor && gb->model <= GB_MODEL_CGB_C && was_background_counting && !gb->apu.is_active[GB_NOISE] && gb->cgb_double_speed) { 0 0 0
1337 0 gb->apu.noise_channel.counter_countdown--;
1338 }
1339 0 ▶ if (div_1_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
if (div_1_glitch) { 0 0 0
1340 0 gb->apu.noise_channel.counter_countdown -= 4;
1341 }
1342
1343 0 ▶ if (!divisor && gb->apu.is_active[GB_NOISE] && (gb->apu.noise_channel.alignment & 3) == 3) {
Branch: 3 branches
BranchTrueFalseTotal
if (!divisor && gb->apu.is_active[GB_NOISE] && (gb->apu.noise_channel.alignment & 3) == 3) { 0 0 0
if (!divisor && gb->apu.is_active[GB_NOISE] && (gb->apu.noise_channel.alignment & 3) == 3) { 0 0 0
if (!divisor && gb->apu.is_active[GB_NOISE] && (gb->apu.noise_channel.alignment & 3) == 3) { 0 0 0
1344 /* TODO: I have no clue where this number comes from, but this number is confirmed for this edge case even for
1345 side LFSR, despite being seemingly arbitrary. */
1346 0 gb->apu.noise_channel.lfsr = 0x0055;
1347 }
1348 else {
1349 0 gb->apu.noise_channel.lfsr = 0;
1350 }
1351 0 ▶ if (instant_step) {
Branch: 1 branch
BranchTrueFalseTotal
if (instant_step) { 0 0 0
1352 0 step_lfsr(gb, 0);
1353 }
1354 0 }
1355  
1356 0 static void nr43_write(GB_gameboy_t *gb, uint8_t new)
1357 {
1358 /*
1359 NR43 writes cause glitch signals to the LFSR. They are often non-deterministic, and
1360 they're revision and instance specific. This implementation is trying to emulate a
1361 simplified and deterministic "variant" of specific instances of revisions I own.
1362
1363 For more details:
1364 https://github.com/LIJI32/SameBoy/issues/397#issuecomment-3733625631
1365 */
1366
1367 /*
1368 TODO: Non-determinism aside, this is currently only 100% accurate in CGB-E mode, where
1369 my specific CGB-E is currently emulated. My CGB-D, under rare cases, samples a second
1370 intermediate value, and this is not currently emulated. AGB revisions are extremely
1371 glitchy, and are hard to research.
1372
1373 Due to FF-write glitches in pre-CGB-D revisions, all writes (even no-change writes) go
1374 through 3 intermediate values by definition. Also, the effective counter value used is
1375 ORed with the next (or previous, timing needs to be verified) value.
1376 */
1377 0 bool old_narrow = gb->apu.noise_channel.narrow;
1378 0 gb->apu.noise_channel.narrow = new & 8;
1379 0 uint8_t old = gb->io_registers[GB_IO_NR43];
1380 0 gb->io_registers[GB_IO_NR43] = new;
1381
1382 0 ▶ if ((old & 0xF0) == (new & 0xF0)) return;
Branch: 1 branch
BranchTrueFalseTotal
if ((old & 0xF0) == (new & 0xF0)) return; 0 0 0
1383
1384 0 uint16_t effective_counter = gb->apu.noise_channel.counter;
1385 0 ▶ if (gb->model <= GB_MODEL_CGB_C && gb->apu.noise_channel.countdown_reloaded) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_C && gb->apu.noise_channel.countdown_reloaded) { 0 0 0
if (gb->model <= GB_MODEL_CGB_C && gb->apu.noise_channel.countdown_reloaded) { 0 0 0
1386 0 effective_counter |= (effective_counter - 1) & 0x3FFF;
1387 }
1388 0 bool old_bit = (effective_counter >> (old >> 4)) & 1;
1389  
1390 0 uint8_t glitch_value = (old & 0x7F) | (new & 0x80);
1391 0 bool glitch_bit = (effective_counter >> (glitch_value >> 4)) & 1;
1392 0 bool new_bit = (effective_counter >> (new >> 4)) & 1;
1393 0 bool force_glitch = false;
1394  
1395 0 ▶ if (gb->model == GB_MODEL_CGB_D) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model == GB_MODEL_CGB_D) { 0 0 0
1396 0 ▶ if (new_bit && glitch_bit && old_bit) {
Branch: 3 branches
BranchTrueFalseTotal
if (new_bit && glitch_bit && old_bit) { 0 0 0
if (new_bit && glitch_bit && old_bit) { 0 0 0
if (new_bit && glitch_bit && old_bit) { 0 0 0
1397 0 ▶ if ((old ^ new) & 0x70) {
Branch: 1 branch
BranchTrueFalseTotal
if ((old ^ new) & 0x70) { 0 0 0
1398 0 force_glitch = true;
1399 }
1400 }
1401 }
1402
1403 0 ▶ if (gb->model > GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model > GB_MODEL_CGB_E) { 0 0 0
1404 /* AGB behavior is very glitchy and incosistent. It can have 2 intermediate values for
1405 NR43, and sometimes even 3, and the pattern isn't very consistent. This is a *very*
1406 rough approximation of the behavior.
1407
1408 Due to having so up to 3 intermediate value, glitch behavior is complicated to look
1409 into, so currently CGB-E behavior is arbitrarily used if a glitch occurs. */
1410
1411 0 uint8_t glitch_value2 = 0;
1412 0 ▶ if (new >= 0x80 && old >= 0x80) {
Branch: 2 branches
BranchTrueFalseTotal
if (new >= 0x80 && old >= 0x80) { 0 0 0
if (new >= 0x80 && old >= 0x80) { 0 0 0
1413 0 glitch_value = (old & 0xCF) | (new & 0x30);
1414 0 glitch_value2 = (old & 0x8F) | (new & 0x70);
1415 }
1416 else {
1417 0 glitch_value = (old & 0xDF) | (new & 0x20);
1418 0 glitch_value2 = (old & 0xCF) | (new & 0x30);
1419 }
1420 0 glitch_bit = (gb->apu.noise_channel.counter >> (glitch_value >> 4)) & 1;
1421 0 uint8_t glitch_bit2 = (gb->apu.noise_channel.counter >> (glitch_value2 >> 4)) & 1;
1422 0 ▶ if (glitch_bit != glitch_bit2) {
Branch: 1 branch
BranchTrueFalseTotal
if (glitch_bit != glitch_bit2) { 0 0 0
1423 0 ▶ if (new_bit == old_bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_bit == old_bit) { 0 0 0
1424 0 glitch_bit = !new_bit;
1425 }
1426 0 ▶ else if (!glitch_bit && old_bit) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!glitch_bit && old_bit) { 0 0 0
else if (!glitch_bit && old_bit) { 0 0 0
1427 0 force_glitch = true;
1428 }
1429 }
1430 }
1431
1432 /* Step LFSR */
1433
1434 0 ▶ if ((old_bit == new_bit && new_bit != glitch_bit) || force_glitch) {
Branch: 3 branches
BranchTrueFalseTotal
if ((old_bit == new_bit && new_bit != glitch_bit) || force_glitch) { 0 0 0
if ((old_bit == new_bit && new_bit != glitch_bit) || force_glitch) { 0 0 0
if ((old_bit == new_bit && new_bit != glitch_bit) || force_glitch) { 0 0 0
1435 /* Glitching write. Has two categories, both have non-deterministic
1436 variants. These are the most common variants of the two categories,
1437 which are deterministic. */
1438 0 ▶ if (new_bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_bit) { 0 0 0
1439 /* Category 1 */
1440 0 ▶ if (gb->model >= GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model >= GB_MODEL_CGB_E) { 0 0 0
1441 0 ▶ if (!(new & 0x80)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(new & 0x80)) { 0 0 0
1442 0 step_lfsr(gb, 0);
1443 }
1444 else {
1445 /* Only happens under this odd condition */
1446 0 uint8_t t1 = (old >> 4) & 7;
1447 0 uint8_t t2 = (new >> 4) & 7;
1448
1449 0 ▶ if ((t1 ^ 7) + t2 > 7 || ((t1 ^ 7) & t2)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((t1 ^ 7) + t2 > 7 || ((t1 ^ 7) & t2)) { 0 0 0
if ((t1 ^ 7) + t2 > 7 || ((t1 ^ 7) & t2)) { 0 0 0
1450 /* Copy bit 8 to bit 7 */
1451 0 gb->apu.noise_channel.lfsr &= ~0x80;
1452 0 gb->apu.noise_channel.lfsr |= (gb->apu.noise_channel.lfsr >> 1) & 0x80;
1453
1454 /* All specific cases have non-deterministic behaviors involved */
1455 0 ▶ if ((t1 == 0 || t1 == 4) && t2 == 3) {
Branch: 3 branches
BranchTrueFalseTotal
if ((t1 == 0 || t1 == 4) && t2 == 3) { 0 0 0
if ((t1 == 0 || t1 == 4) && t2 == 3) { 0 0 0
if ((t1 == 0 || t1 == 4) && t2 == 3) { 0 0 0
1456 0 gb->apu.noise_channel.lfsr &= (gb->apu.noise_channel.lfsr >> 1) | 0x545;
1457 0 update_lfsr(gb, 0);
1458 }
1459 0 ▶ else if (t1 == 2 && t2 == 3) {
Branch: 2 branches
BranchTrueFalseTotal
else if (t1 == 2 && t2 == 3) { 0 0 0
else if (t1 == 2 && t2 == 3) { 0 0 0
1460 0 uint16_t mask = 0x555;
1461 0 ▶ if ((gb->apu.noise_channel.lfsr & 0xC) == 0xC) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.noise_channel.lfsr & 0xC) == 0xC) { 0 0 0
1462 0 mask |= 8;
1463 }
1464 0 ▶ if ((gb->apu.noise_channel.lfsr & 0xC00) == 0xC00) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.noise_channel.lfsr & 0xC00) == 0xC00) { 0 0 0
1465 0 mask |= 0x800;
1466 }
1467
1468 0 gb->apu.noise_channel.lfsr &= (gb->apu.noise_channel.lfsr >> 1) | mask;
1469 0 update_lfsr(gb, 0);
1470 }
1471 0 ▶ if (!gb->apu.noise_channel.narrow && old_narrow && gb->apu.lfsr_stepped_in_narrow) {
Branch: 3 branches
BranchTrueFalseTotal
if (!gb->apu.noise_channel.narrow && old_narrow && gb->apu.lfsr_stepped_in_narrow) { 0 0 0
if (!gb->apu.noise_channel.narrow && old_narrow && gb->apu.lfsr_stepped_in_narrow) { 0 0 0
if (!gb->apu.noise_channel.narrow && old_narrow && gb->apu.lfsr_stepped_in_narrow) { 0 0 0
1472 /* TODO: Behaves weirder in non-deterministic t1 == 0/4 scenarios? */
1473 0 ▶ if (gb->apu.lfsr_bit_7_before_step) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.lfsr_bit_7_before_step) { 0 0 0
1474 0 gb->apu.noise_channel.lfsr |= 0x40;
1475 }
1476 else {
1477 0 gb->apu.noise_channel.lfsr &= ~0x40;
1478 }
1479 }
1480 0 ▶ gb->apu.noise_channel.lfsr |= gb->apu.noise_channel.narrow ? 0x4040 : 0x4000;
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.noise_channel.lfsr |= gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; 0 0 0
1481 /* TODO: verify */
1482 0 gb->apu.lfsr_stepped_in_narrow = gb->apu.noise_channel.narrow;
1483 }
1484 }
1485 }
1486 0 ▶ else if (gb->model == GB_MODEL_CGB_D) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->model == GB_MODEL_CGB_D) { 0 0 0
1487 static const uint8_t glitch_map_l2h[8 * 8] = {
1488 [000] = 0x00, 0x01, 0x01, 0x21, 0x02, 0x21,
1489 [010] = 0x03, 0x00, 0x21, 0x01, 0x04, 0x04,
1490 [020] = 0x05, 0x01, 0x00, 0x01, 0x04, 0x21,
1491 [030] = 0x03, 0x05, 0x05, 0x00, 0x01, 0x01,
1492 [040] = 0x05, 0x01, 0x01, 0x21, 0x00, 0x01,
1493 [050] = 0x05, 0x05, 0x21, 0x01, 0x05, 0x00,
1494 [060] = 0x05, 0x01, 0x05, 0x01, 0x05, 0x01,
1495 [070] = 0x03, 0x05, 0x05, 0x05, 0x05, 0x05,
1496 };
1497
1498 /* The following transitions are a bit non-deterministic (except under forced glitched):
1499 1 -> c, 2 -> c, 3 -> c, 3 -> d */
1500
1501 static const uint8_t glitch_map_h2l[8 * 8] = {
1502 [000] = 0x00, 0x27, 0x26, 0x37, 0x21, 0x38, 0x01, 0x01,
1503 [010] = 0x01, 0x00, 0x38, 0x21, 0x21, 0x21, 0x01, 0x01,
1504 [020] = 0x01, 0x27, 0x00, 0x28, 0x21, 0x38, 0x01, 0x01,
1505 [030] = 0x01, 0x02, 0x01, 0x00, 0x31, 0x21, 0x01, 0x01,
1506 [040] = 0x06, 0x28, 0x28, 0x38, 0x00, 0x27, 0x01, 0x01,
1507 [050] = 0x01, 0x03, 0x38, 0x21, 0x01, 0x00, 0x01, 0x01,
1508 };
1509 /* The following transitions are a bit non-deterministic (except under forced glitched):
1510 8 -> 5, 2 -> 9, a -> 3, c -> 3 */
1511
1512 0 ▶ const uint8_t *glitch_map = old & 0x80? &glitch_map_h2l[0] : &glitch_map_l2h[0];
Branch: 1 branch
BranchTrueFalseTotal
const uint8_t *glitch_map = old & 0x80? &glitch_map_h2l[0] : &glitch_map_l2h[0]; 0 0 0
1513
1514
1515 0 unsigned glitch = glitch_map[((old & 0x70) >> 1) | ((new & 0x70) >> 4)];
1516 0 ▶ if (force_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
if (force_glitch) { 0 0 0
1517 0 ▶ if (!((new ^ old) & 0x80)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!((new ^ old) & 0x80)) { 0 0 0
1518 0 ▶ glitch = glitch & 0x20? 5 : 0;
Branch: 1 branch
BranchTrueFalseTotal
glitch = glitch & 0x20? 5 : 0; 0 0 0
1519 }
1520 0 ▶ else if (!(new & 0x80)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!(new & 0x80)) { 0 0 0
1521 0 ▶ glitch = glitch & 0x10? 5 : 0;
Branch: 1 branch
BranchTrueFalseTotal
glitch = glitch & 0x10? 5 : 0; 0 0 0
1522 }
1523 0 ▶ else if ((glitch & 0xF) == 1 || (glitch & 0xF) == 4) {
Branch: 2 branches
BranchTrueFalseTotal
else if ((glitch & 0xF) == 1 || (glitch & 0xF) == 4) { 0 0 0
else if ((glitch & 0xF) == 1 || (glitch & 0xF) == 4) { 0 0 0
1524 0 glitch = 5;
1525 }
1526 else {
1527 0 glitch = 0;
1528 }
1529 }
1530 else {
1531 0 glitch &= 0xF;
1532 }
1533 0 uint16_t old_lfsr = gb->apu.noise_channel.lfsr;
1534 0 ▶ uint16_t lfsr_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000;
Branch: 1 branch
BranchTrueFalseTotal
uint16_t lfsr_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; 0 0 0
1535 0 switch (glitch) {
1536 0 case 6: // Like 2, but conditional
1537 case 4: // Like 2, but conditional
1538 0 ▶ if ((gb->apu.noise_channel.lfsr & (glitch == 4? 0x60 : 0x40)) != 0x40) { // Todo check wide mode
Branch: 2 branches
BranchTrueFalseTotal
if ((gb->apu.noise_channel.lfsr & (glitch == 4? 0x60 : 0x40)) != 0x40) { // Todo check wide mode 0 0 0
if ((gb->apu.noise_channel.lfsr & (glitch == 4? 0x60 : 0x40)) != 0x40) { // Todo check wide mode 0 0 0
1539 case 2: // And bit 1 with bit 0 before doing glitch 1
1540 0 ▶ if (!(gb->apu.noise_channel.lfsr & 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(gb->apu.noise_channel.lfsr & 1)) { 0 0 0
1541 0 gb->apu.noise_channel.lfsr &= ~2;
1542 }
1543 }
1544 case 1: // Step and set the LFSR bit
1545 case 8: // Step and set the LFSR bit conditionally
1546 0 step_lfsr(gb, 0);
1547 0 case 5: // Just set LFSR
1548 0 ▶ if ((glitch != 8) || (old_lfsr & 3) != 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ((glitch != 8) || (old_lfsr & 3) != 2) { 0 0 0
if ((glitch != 8) || (old_lfsr & 3) != 2) { 0 0 0
1549 0 gb->apu.noise_channel.lfsr |= lfsr_mask;
1550 }
1551 else {
1552 0 gb->apu.noise_channel.lfsr |= old_lfsr & lfsr_mask;
1553 }
1554 0 break;
1555
1556 0 case 7: // Step and OR the LFSR bit with its old value
1557 0 step_lfsr(gb, 0);
1558 0 gb->apu.noise_channel.lfsr |= old_lfsr & lfsr_mask;
1559 0 break;
1560
1561 0 case 3: // A bit of a mess
1562 0 step_lfsr(gb, 0);
1563 0 gb->apu.noise_channel.lfsr &= old_lfsr;
1564 0 gb->apu.noise_channel.lfsr |= old_lfsr & 1;
1565 0 gb->apu.noise_channel.lfsr |= lfsr_mask;
1566 0 update_lfsr(gb, 0);
1567 0 break;
1568
1569
1570 0 default: break;
1571 }
1572 }
1573 }
1574 else {
1575 /* Category 2 */
1576 0 ▶ if (gb->model >= GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model >= GB_MODEL_CGB_E) { 0 0 0
1577 static const uint8_t glitch_map[8 * 8] = {
1578 /* 8 9 A B C D */
1579 [002] = 4, [003] = 2, [004] = 2, [005] = 2, // 0
1580 [012] = 2, [013] = 4, [014] = 2, [015] = 2, // 1
1581 [020] = 1, [021] = 2, [023] = 1, [024] = 5, [025] = 3, // 2
1582 [034] = 2, [035] = 2, // 3
1583 [041] = 2, [042] = 2, [043] = 2, // 4
1584 [050] = 6, [052] = 2, [053] = 2, // 5
1585 };
1586
1587 /* The following transitions are a bit non-deterministic:
1588 2 -> 8, 0 -> A, 2 -> C */
1589
1590 0 ▶ unsigned glitch = new & 0x80? glitch_map[((old & 0x70) >> 1) | ((new & 0x70) >> 4)] : 0;
Branch: 1 branch
BranchTrueFalseTotal
unsigned glitch = new & 0x80? glitch_map[((old & 0x70) >> 1) | ((new & 0x70) >> 4)] : 0; 0 0 0
1591 0 switch (glitch) {
1592 0 case 1: /* Step, followed by bit 1 &= bit 0 */
1593 case 6: /* Variant of type 1: LFSR bit - 1 glitched by LFSR bit, LFSR bit - 2, and bit 0 */
1594 0 step_lfsr(gb, 0);
1595 0 ▶ if (glitch == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (glitch == 6) { 0 0 0
1596 /* TODO: Verify wide mode */
1597 0 ▶ if ((gb->apu.noise_channel.narrow &&
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.noise_channel.narrow && 0 0 0
1598 0 ▶ ((gb->apu.noise_channel.lfsr & 0x71) == 0x20)) ||
Branch: 1 branch
BranchTrueFalseTotal
((gb->apu.noise_channel.lfsr & 0x71) == 0x20)) || 0 0 0
1599 0 ▶ (gb->apu.noise_channel.lfsr & 0x71) == 0x61) {
Branch: 1 branch
BranchTrueFalseTotal
(gb->apu.noise_channel.lfsr & 0x71) == 0x61) { 0 0 0
1600 0 gb->apu.noise_channel.lfsr &= ~0x20;
1601 }
1602 0 ▶ if ((gb->apu.noise_channel.lfsr & 0x7001) == 0x2000 ||
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.noise_channel.lfsr & 0x7001) == 0x2000 || 0 0 0
1603 0 ▶ (gb->apu.noise_channel.lfsr & 0x7001) == 0x6001) {
Branch: 1 branch
BranchTrueFalseTotal
(gb->apu.noise_channel.lfsr & 0x7001) == 0x6001) { 0 0 0
1604 0 gb->apu.noise_channel.lfsr &= ~0x2000;
1605 }
1606 }
1607 0 ▶ if ((gb->apu.noise_channel.lfsr & 0x3) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.noise_channel.lfsr & 0x3) == 2) { 0 0 0
1608 0 gb->apu.noise_channel.lfsr &= ~2;
1609 }
1610 0 break;
1611 0 case 2: { /* Step, bitwise AND with previous, except for bit 0 */
1612 0 uint16_t prev = gb->apu.noise_channel.lfsr;
1613 0 step_lfsr(gb, 0);
1614 0 gb->apu.noise_channel.lfsr &= prev | 1;
1615 0 break;
1616 }
1617
1618 0 case 5:; /* Non deterministic variant of type 3:
1619 The LFSR is unset if bit 0 & 1 are 0b10.
1620 Bit 3 is complex and non-deterministic (TODO: wide mode) */
1621 0 ▶ if ((gb->apu.noise_channel.lfsr & 0x3) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.noise_channel.lfsr & 0x3) == 2) { 0 0 0
1622 0 ▶ gb->apu.noise_channel.lfsr &= gb->apu.noise_channel.narrow? ~0x4040 : ~0x4000;
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.noise_channel.lfsr &= gb->apu.noise_channel.narrow? ~0x4040 : ~0x4000; 0 0 0
1623 }
1624
1625 0 ▶ if ((gb->apu.noise_channel.lfsr & 0x19) == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->apu.noise_channel.lfsr & 0x19) == 8) { 0 0 0
1626 0 gb->apu.noise_channel.lfsr &= ~8;
1627 }
1628
1629 case 3: /* No step, bit 0 = bit 1, some other bits have AND glitches with next*/
1630 0 gb->apu.noise_channel.lfsr &= ~1;
1631 0 gb->apu.noise_channel.lfsr |= (gb->apu.noise_channel.lfsr >> 1) & 1;
1632
1633 0 update_lfsr(gb, 0);
1634 /* TODO: verify */
1635 0 gb->apu.lfsr_stepped_in_narrow = gb->apu.noise_channel.narrow;
1636 0 break;
1637
1638 0 case 4: { /* Step, bit 1 &= bit 0, LFSR bit -1 &= LFSR bit */
1639 0 uint16_t prev = gb->apu.noise_channel.lfsr;
1640 0 step_lfsr(gb, 0);
1641 0 ▶ gb->apu.noise_channel.lfsr &= prev | (gb->apu.noise_channel.narrow? ~0x2022 : ~0x2002);
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.noise_channel.lfsr &= prev | (gb->apu.noise_channel.narrow? ~0x2022 : ~0x2002); 0 0 0
1642 0 break;
1643 }
1644
1645 0 default: /* No glitch, plain step*/
1646 0 step_lfsr(gb, 0);
1647 0 break;
1648
1649 }
1650 }
1651 else {
1652 0 step_lfsr(gb, 0);
1653 }
1654 }
1655 }
1656 0 ▶ else if (!old_bit && new_bit) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!old_bit && new_bit) { 0 0 0
else if (!old_bit && new_bit) { 0 0 0
1657 0 ▶ if (gb->model <= GB_MODEL_CGB_C) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_C) { 0 0 0
1658 0 bool previous_narrow = gb->apu.noise_channel.narrow;
1659 0 gb->apu.noise_channel.narrow = true;
1660 0 step_lfsr(gb, 0);
1661 0 gb->apu.noise_channel.narrow = previous_narrow;
1662 0 ▶ if ((new & 0xf0) <= 0x20 && glitch_bit && !(effective_counter & 8)) { // No clue why that specific bit is tested
Branch: 3 branches
BranchTrueFalseTotal
if ((new & 0xf0) <= 0x20 && glitch_bit && !(effective_counter & 8)) { // No clue why that specific bit is tested 0 0 0
if ((new & 0xf0) <= 0x20 && glitch_bit && !(effective_counter & 8)) { // No clue why that specific bit is tested 0 0 0
if ((new & 0xf0) <= 0x20 && glitch_bit && !(effective_counter & 8)) { // No clue why that specific bit is tested 0 0 0
1663 // Non-deterministic, not fully tested for revision differences and wide mode
1664 // Step twice?
1665 0 step_lfsr(gb, 0);
1666 0 ▶ gb->apu.noise_channel.lfsr &= ~(gb->apu.noise_channel.narrow? 0x4040 : 0x4000);
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.noise_channel.lfsr &= ~(gb->apu.noise_channel.narrow? 0x4040 : 0x4000); 0 0 0
1667 0 ▶ gb->apu.noise_channel.lfsr |= (gb->apu.noise_channel.lfsr & (gb->apu.noise_channel.narrow? 0x2020 : 0x2000)) << 1;
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.noise_channel.lfsr |= (gb->apu.noise_channel.lfsr & (gb->apu.noise_channel.narrow? 0x2020 : 0x2000)) << 1; 0 0 0
1668 }
1669 }
1670 else {
1671 0 step_lfsr(gb, 0);
1672 }
1673 }
1674 0 ▶ else if (gb->model <= GB_MODEL_CGB_C) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->model <= GB_MODEL_CGB_C) { 0 0 0
1675 0 ▶ if ((new & 0xf0) <= 0x20 && !glitch_bit && !new_bit && !old_bit && (effective_counter & 8)) { // No clue why that specific bit is tested
Branch: 5 branches
BranchTrueFalseTotal
if ((new & 0xf0) <= 0x20 && !glitch_bit && !new_bit && !old_bit && (effective_counter & 8)) { // No clue why that specific bit is tested 0 0 0
if ((new & 0xf0) <= 0x20 && !glitch_bit && !new_bit && !old_bit && (effective_counter & 8)) { // No clue why that specific bit is tested 0 0 0
if ((new & 0xf0) <= 0x20 && !glitch_bit && !new_bit && !old_bit && (effective_counter & 8)) { // No clue why that specific bit is tested 0 0 0
if ((new & 0xf0) <= 0x20 && !glitch_bit && !new_bit && !old_bit && (effective_counter & 8)) { // No clue why that specific bit is tested 0 0 0
if ((new & 0xf0) <= 0x20 && !glitch_bit && !new_bit && !old_bit && (effective_counter & 8)) { // No clue why that specific bit is tested 0 0 0
1676 // Step twice?
1677 0 step_lfsr(gb, 0);
1678 }
1679 }
1680 }
1681  
1682 160 void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value)
1683 {
1684 160 GB_apu_run(gb, true);
1685 160 ▶ if (!gb->apu.global_enable && reg != GB_IO_NR52 && reg < GB_IO_WAV_START && (GB_is_cgb(gb) ||
Branch: 5 branches
BranchTrueFalseTotal
if (!gb->apu.global_enable && reg != GB_IO_NR52 && reg < GB_IO_WAV_START && (GB_is_cgb(gb) || 120 40 160
if (!gb->apu.global_enable && reg != GB_IO_NR52 && reg < GB_IO_WAV_START && (GB_is_cgb(gb) || 112 8 120
if (!gb->apu.global_enable && reg != GB_IO_NR52 && reg < GB_IO_WAV_START && (GB_is_cgb(gb) || 0 112 112
if (!gb->apu.global_enable && reg != GB_IO_NR52 && reg < GB_IO_WAV_START && (GB_is_cgb(gb) || 0 0 0
if (!gb->apu.global_enable && reg != GB_IO_NR52 && reg < GB_IO_WAV_START && (GB_is_cgb(gb) || 0 0 0
1686 (
1687 0 ▶ reg != GB_IO_NR11 &&
Branch: 1 branch
BranchTrueFalseTotal
reg != GB_IO_NR11 && 0 0 0
1688 0 ▶ reg != GB_IO_NR21 &&
Branch: 1 branch
BranchTrueFalseTotal
reg != GB_IO_NR21 && 0 0 0
1689 0 ▶ reg != GB_IO_NR31 &&
Branch: 1 branch
BranchTrueFalseTotal
reg != GB_IO_NR31 && 0 0 0
1690 reg != GB_IO_NR41
1691 )
1692 )) {
1693 0 return;
1694 }
1695  
1696 160 ▶ if (reg >= GB_IO_WAV_START && reg <= GB_IO_WAV_END && gb->apu.is_active[GB_WAVE]) {
Branch: 3 branches
BranchTrueFalseTotal
if (reg >= GB_IO_WAV_START && reg <= GB_IO_WAV_END && gb->apu.is_active[GB_WAVE]) { 112 48 160
if (reg >= GB_IO_WAV_START && reg <= GB_IO_WAV_END && gb->apu.is_active[GB_WAVE]) { 112 0 112
if (reg >= GB_IO_WAV_START && reg <= GB_IO_WAV_END && gb->apu.is_active[GB_WAVE]) { 0 112 112
1697 0 ▶ if ((!GB_is_cgb(gb) && !gb->apu.wave_channel.wave_form_just_read) || gb->model > GB_MODEL_CGB_E) {
Branch: 3 branches
BranchTrueFalseTotal
if ((!GB_is_cgb(gb) && !gb->apu.wave_channel.wave_form_just_read) || gb->model > GB_MODEL_CGB_E) { 0 0 0
if ((!GB_is_cgb(gb) && !gb->apu.wave_channel.wave_form_just_read) || gb->model > GB_MODEL_CGB_E) { 0 0 0
if ((!GB_is_cgb(gb) && !gb->apu.wave_channel.wave_form_just_read) || gb->model > GB_MODEL_CGB_E) { 0 0 0
1698 0 return;
1699 }
1700 0 reg = GB_IO_WAV_START + gb->apu.wave_channel.current_sample_index / 2;
1701 }
1702  
1703 /* Todo: this can and should be rewritten with a function table. */
1704 160 switch (reg) {
1705 /* Globals */
1706 16 case GB_IO_NR50:
1707 case GB_IO_NR51:
1708 16 gb->io_registers[reg] = value;
1709 /* These registers affect the output of all 4 channels (but not the output of the PCM registers).*/
1710 /* We call update_samples with the current value so the APU output is updated with the new outputs */
1711 80 ▶ for (unsigned i = GB_N_CHANNELS; i--;) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = GB_N_CHANNELS; i--;) { 64 16 80
1712 64 int8_t sample = gb->apu.samples[i];
1713 64 gb->apu.samples[i] = 0x10; // Invalidate to force update
1714 64 update_sample(gb, i, sample, 0);
1715 }
1716 16 break;
1717 8 case GB_IO_NR52: {
1718  
1719 8 uint8_t old_pulse_lengths[] = {
1720 8 gb->apu.square_channels[0].pulse_length,
1721 8 gb->apu.square_channels[1].pulse_length,
1722 8 gb->apu.wave_channel.pulse_length,
1723 8 gb->apu.noise_channel.pulse_length
1724 };
1725 8 ▶ if ((value & 0x80) && !gb->apu.global_enable) {
Branch: 2 branches
BranchTrueFalseTotal
if ((value & 0x80) && !gb->apu.global_enable) { 8 0 8
if ((value & 0x80) && !gb->apu.global_enable) { 8 0 8
1726 8 GB_apu_init(gb);
1727 8 gb->apu.global_enable = true;
1728 }
1729 0 ▶ else if (!(value & 0x80) && gb->apu.global_enable) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!(value & 0x80) && gb->apu.global_enable) { 0 0 0
else if (!(value & 0x80) && gb->apu.global_enable) { 0 0 0
1730 0 ▶ for (unsigned i = GB_N_CHANNELS; i--;) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = GB_N_CHANNELS; i--;) { 0 0 0
1731 0 update_sample(gb, i, 0, 0);
1732 }
1733 0 memset(&gb->apu, 0, sizeof(gb->apu));
1734 0 memset(gb->io_registers + GB_IO_NR10, 0, GB_IO_WAV_START - GB_IO_NR10);
1735 0 gb->apu.global_enable = false;
1736 0 gb->apu.apu_cycles_in_2mhz = true;
1737 }
1738  
1739 8 ▶ if (!GB_is_cgb(gb) && (value & 0x80)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!GB_is_cgb(gb) && (value & 0x80)) { 1 7 8
if (!GB_is_cgb(gb) && (value & 0x80)) { 1 0 1
1740 1 gb->apu.square_channels[0].pulse_length = old_pulse_lengths[0];
1741 1 gb->apu.square_channels[1].pulse_length = old_pulse_lengths[1];
1742 1 gb->apu.wave_channel.pulse_length = old_pulse_lengths[2];
1743 1 gb->apu.noise_channel.pulse_length = old_pulse_lengths[3];
1744 }
1745 }
1746 8 break;
1747  
1748 /* Square channels */
1749 0 case GB_IO_NR10: {
1750 0 ▶ if (unlikely(gb->apu.square_sweep_calculate_countdown || gb->apu.square_sweep_calculate_countdown_reload_timer)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(gb->apu.square_sweep_calculate_countdown || gb->apu.square_sweep_calculate_countdown_reload_timer)) { 0 0 0
if (unlikely(gb->apu.square_sweep_calculate_countdown || gb->apu.square_sweep_calculate_countdown_reload_timer)) { 0 0 0
1751 0 nr10_write_glitch(gb, value);
1752 }
1753 0 bool old_negate = gb->io_registers[GB_IO_NR10] & 8;
1754 0 gb->io_registers[GB_IO_NR10] = value;
1755 0 ▶ if (gb->model <= GB_MODEL_CGB_C) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_C) { 0 0 0
1756 0 old_negate = true;
1757 }
1758 0 ▶ if (gb->apu.shadow_sweep_sample_length + gb->apu.channel1_completed_addend + old_negate > 0x7FF &&
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.shadow_sweep_sample_length + gb->apu.channel1_completed_addend + old_negate > 0x7FF && 0 0 0
1759 0 ▶ !(value & 8)) {
Branch: 1 branch
BranchTrueFalseTotal
!(value & 8)) { 0 0 0
1760 0 gb->apu.is_active[GB_SQUARE_1] = false;
1761 0 update_sample(gb, GB_SQUARE_1, 0, 0);
1762 }
1763 0 trigger_sweep_calculation(gb);
1764 0 break;
1765 }
1766  
1767 8 case GB_IO_NR11:
1768 case GB_IO_NR21: {
1769 8 GB_channel_t index = reg == GB_IO_NR21? GB_SQUARE_2: GB_SQUARE_1;
1770 8 gb->apu.square_channels[index].pulse_length = (0x40 - (value & 0x3F));
1771 8 ▶ if (!gb->apu.global_enable) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.global_enable) { 0 8 8
1772 0 value &= 0x3F;
1773 }
1774 8 break;
1775 }
1776  
1777 8 case GB_IO_NR12:
1778 case GB_IO_NR22: {
1779 8 GB_channel_t index = reg == GB_IO_NR22? GB_SQUARE_2: GB_SQUARE_1;
1780 8 ▶ if ((value & 0xF8) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value & 0xF8) == 0) { 0 8 8
1781 /* This disables the DAC */
1782 0 gb->io_registers[reg] = value;
1783 0 gb->apu.is_active[index] = false;
1784 0 update_sample(gb, index, 0, 0);
1785 }
1786 8 ▶ else if (gb->apu.is_active[index]) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->apu.is_active[index]) { 0 8 8
1787 0 nrx2_glitch(gb, &gb->apu.square_channels[index].current_volume,
1788 0 value, gb->io_registers[reg], &gb->apu.square_channels[index].volume_countdown,
1789 &gb->apu.square_channels[index].envelope_clock);
1790 0 update_square_sample(gb, index, 0);
1791 }
1792  
1793 8 break;
1794 }
1795  
1796 4 case GB_IO_NR13:
1797 case GB_IO_NR23: {
1798 4 GB_channel_t index = reg == GB_IO_NR23? GB_SQUARE_2: GB_SQUARE_1;
1799 4 gb->apu.square_channels[index].sample_length &= ~0xFF;
1800 4 gb->apu.square_channels[index].sample_length |= value & 0xFF;
1801 4 ▶ if (gb->apu.square_channels[index].just_reloaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[index].just_reloaded) { 0 4 4
1802 0 gb->apu.square_channels[index].sample_countdown = (gb->apu.square_channels[index].sample_length ^ 0x7FF) * 2 + 1;
1803 }
1804 4 break;
1805 }
1806  
1807 4 case GB_IO_NR14:
1808 case GB_IO_NR24: {
1809 4 GB_channel_t index = reg == GB_IO_NR24? GB_SQUARE_2: GB_SQUARE_1;
1810 4 bool was_active = gb->apu.is_active[index];
1811 /* TODO: When the sample length changes right before being updated from ≥$700 to <$700, the countdown
1812 should change to the old length, but the current sample should not change. Because our write
1813 timing isn't accurate to the T-cycle, we hack around it by stepping the sample index backwards. */
1814 4 ▶ if ((value & 0x80) == 0 && gb->apu.is_active[index] && (gb->io_registers[reg] & 0x7) == 7 && (value & 7) != 7) {
Branch: 4 branches
BranchTrueFalseTotal
if ((value & 0x80) == 0 && gb->apu.is_active[index] && (gb->io_registers[reg] & 0x7) == 7 && (value & 7) != 7) { 0 4 4
if ((value & 0x80) == 0 && gb->apu.is_active[index] && (gb->io_registers[reg] & 0x7) == 7 && (value & 7) != 7) { 0 0 0
if ((value & 0x80) == 0 && gb->apu.is_active[index] && (gb->io_registers[reg] & 0x7) == 7 && (value & 7) != 7) { 0 0 0
if ((value & 0x80) == 0 && gb->apu.is_active[index] && (gb->io_registers[reg] & 0x7) == 7 && (value & 7) != 7) { 0 0 0
1815 /* On an AGB, as well as on CGB C and earlier (TODO: Tested: 0, B and C), it behaves slightly different on
1816 double speed. */
1817 0 ▶ if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D || gb->apu.square_channels[index].sample_countdown & 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D || gb->apu.square_channels[index].sample_countdown & 1) { 0 0 0
if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D || gb->apu.square_channels[index].sample_countdown & 1) { 0 0 0
if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D || gb->apu.square_channels[index].sample_countdown & 1) { 0 0 0
1818 0 ▶ if (gb->apu.square_channels[index].did_tick &&
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[index].did_tick && 0 0 0
1819 0 ▶ gb->apu.square_channels[index].sample_countdown >> 1 == (gb->apu.square_channels[index].sample_length ^ 0x7FF)) {
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.square_channels[index].sample_countdown >> 1 == (gb->apu.square_channels[index].sample_length ^ 0x7FF)) { 0 0 0
1820 0 gb->apu.square_channels[index].current_sample_index--;
1821 0 gb->apu.square_channels[index].current_sample_index &= 7;
1822 0 gb->apu.square_channels[index].sample_surpressed = false;
1823 }
1824 }
1825 }
1826  
1827 4 uint16_t old_sample_length = gb->apu.square_channels[index].sample_length;
1828 4 gb->apu.square_channels[index].sample_length &= 0xFF;
1829 4 gb->apu.square_channels[index].sample_length |= (value & 7) << 8;
1830 4 ▶ if (gb->apu.square_channels[index].just_reloaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[index].just_reloaded) { 0 4 4
1831 0 gb->apu.square_channels[index].sample_countdown = (gb->apu.square_channels[index].sample_length ^ 0x7FF) * 2 + 1;
1832 }
1833 4 ▶ if (value & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (value & 0x80) { 4 0 4
1834 /* Current sample index remains unchanged when restarting channels 1 or 2. It is only reset by
1835 turning the APU off. */
1836 4 gb->apu.square_channels[index].envelope_clock.locked = false;
1837 4 gb->apu.square_channels[index].envelope_clock.clock = false;
1838 4 gb->apu.square_channels[index].did_tick = false;
1839 4 bool force_unsurpressed = false;
1840 4 ▶ if (!gb->apu.is_active[index]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.is_active[index]) { 2 2 4
1841 2 ▶ if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D) { 1 1 2
if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D) { 0 1 1
1842 1 ▶ if (!(value & 4) && !(((gb->apu.square_channels[index].sample_countdown - gb->apu.square_channels[index].delay) / 2) & 0x400)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(value & 4) && !(((gb->apu.square_channels[index].sample_countdown - gb->apu.square_channels[index].delay) / 2) & 0x400)) { 0 1 1
if (!(value & 4) && !(((gb->apu.square_channels[index].sample_countdown - gb->apu.square_channels[index].delay) / 2) & 0x400)) { 0 0 0
1843 0 gb->apu.square_channels[index].current_sample_index++;
1844 0 gb->apu.square_channels[index].current_sample_index &= 0x7;
1845 0 force_unsurpressed = true;
1846 }
1847 }
1848 2 ▶ gb->apu.square_channels[index].delay = 6 + gb->apu.lf_div * (gb->model < GB_MODEL_CGB_D && gb->cgb_double_speed? 1 : -1);
Branch: 2 branches
BranchTrueFalseTotal
gb->apu.square_channels[index].delay = 6 + gb->apu.lf_div * (gb->model < GB_MODEL_CGB_D && gb->cgb_double_speed? 1 : -1); 1 1 2
gb->apu.square_channels[index].delay = 6 + gb->apu.lf_div * (gb->model < GB_MODEL_CGB_D && gb->cgb_double_speed? 1 : -1); 0 1 1
1849 2 gb->apu.square_channels[index].sample_countdown = (gb->apu.square_channels[index].sample_length ^ 0x7FF) * 2 + gb->apu.square_channels[index].delay;
1850 }
1851 else {
1852 2 unsigned extra_delay = 0;
1853 2 ▶ if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D) { 1 1 2
if (gb->model == GB_MODEL_CGB_E || gb->model == GB_MODEL_CGB_D) { 0 1 1
1854 1 ▶ if (!gb->apu.square_channels[index].just_reloaded && !(value & 4) && !(((gb->apu.square_channels[index].sample_countdown - 1 - gb->apu.square_channels[index].delay) / 2) & 0x400)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!gb->apu.square_channels[index].just_reloaded && !(value & 4) && !(((gb->apu.square_channels[index].sample_countdown - 1 - gb->apu.square_channels[index].delay) / 2) & 0x400)) { 1 0 1
if (!gb->apu.square_channels[index].just_reloaded && !(value & 4) && !(((gb->apu.square_channels[index].sample_countdown - 1 - gb->apu.square_channels[index].delay) / 2) & 0x400)) { 0 1 1
if (!gb->apu.square_channels[index].just_reloaded && !(value & 4) && !(((gb->apu.square_channels[index].sample_countdown - 1 - gb->apu.square_channels[index].delay) / 2) & 0x400)) { 0 0 0
1855 0 gb->apu.square_channels[index].current_sample_index++;
1856 0 gb->apu.square_channels[index].current_sample_index &= 0x7;
1857 0 gb->apu.square_channels[index].sample_surpressed = false;
1858 }
1859 /* Todo: verify with the schematics what's going on in here */
1860 1 ▶ else if (gb->apu.square_channels[index].sample_length == 0x7FF &&
Branch: 2 branches
BranchTrueFalseTotal
else if (gb->apu.square_channels[index].sample_length == 0x7FF && 0 1 1
else if (gb->apu.square_channels[index].sample_length == 0x7FF && 0 0 0
1861 0 old_sample_length != 0x7FF &&
1862 0 ▶ (gb->apu.square_channels[index].sample_surpressed)) {
Branch: 1 branch
BranchTrueFalseTotal
(gb->apu.square_channels[index].sample_surpressed)) { 0 0 0
1863 0 extra_delay += 2;
1864 }
1865 }
1866 /* Timing quirk: if already active, sound starts 2 (2MHz) ticks earlier.*/
1867 2 gb->apu.square_channels[index].delay = 4 - gb->apu.lf_div + extra_delay;
1868 2 gb->apu.square_channels[index].sample_countdown = (gb->apu.square_channels[index].sample_length ^ 0x7FF) * 2 + gb->apu.square_channels[index].delay;
1869 }
1870 4 ▶ gb->apu.square_channels[index].current_volume = gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] >> 4;
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.square_channels[index].current_volume = gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] >> 4; 4 0 4
1871 /* The volume changes caused by NRx4 sound start takes effect instantly (i.e. the effect the previously
1872 started sound). The playback itself is not instant which is why we don't update the sample for other
1873 cases. */
1874 4 ▶ if (gb->apu.is_active[index]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[index]) { 2 2 4
1875 2 update_square_sample(gb, index, 0);
1876 }
1877  
1878 4 ▶ gb->apu.square_channels[index].volume_countdown = gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] & 7;
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.square_channels[index].volume_countdown = gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] & 7; 4 0 4
1879  
1880 4 ▶ if ((gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] & 0xF8) != 0 && !gb->apu.is_active[index]) {
Branch: 3 branches
BranchTrueFalseTotal
if ((gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] & 0xF8) != 0 && !gb->apu.is_active[index]) { 4 0 4
if ((gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] & 0xF8) != 0 && !gb->apu.is_active[index]) { 4 0 4
if ((gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] & 0xF8) != 0 && !gb->apu.is_active[index]) { 2 2 4
1881 2 gb->apu.is_active[index] = true;
1882 2 update_sample(gb, index, 0, 0);
1883 2 gb->apu.square_channels[index].sample_surpressed = true && !force_unsurpressed;
1884 }
1885 4 ▶ if (gb->apu.square_channels[index].pulse_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[index].pulse_length == 0) { 0 4 4
1886 0 gb->apu.square_channels[index].pulse_length = 0x40;
1887 0 gb->apu.square_channels[index].length_enabled = false;
1888 }
1889  
1890 4 ▶ if (index == GB_SQUARE_1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index == GB_SQUARE_1) { 4 0 4
1891 4 gb->apu.square_sweep_instant_calculation_done = false;
1892 4 gb->apu.shadow_sweep_sample_length = 0;
1893 4 gb->apu.channel1_completed_addend = 0;
1894 4 ▶ if (gb->io_registers[GB_IO_NR10] & 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_NR10] & 7) { 0 4 4
1895 /* APU bug: if shift is nonzero, overflow check also occurs on trigger */
1896 0 gb->apu.square_sweep_calculate_countdown = gb->io_registers[GB_IO_NR10] & 0x7;
1897 0 ▶ if ((gb->apu.lf_div ^ !gb->cgb_double_speed) && gb->model <= GB_MODEL_CGB_C) {
Branch: 2 branches
BranchTrueFalseTotal
if ((gb->apu.lf_div ^ !gb->cgb_double_speed) && gb->model <= GB_MODEL_CGB_C) { 0 0 0
if ((gb->apu.lf_div ^ !gb->cgb_double_speed) && gb->model <= GB_MODEL_CGB_C) { 0 0 0
1898 0 gb->apu.square_sweep_calculate_countdown_reload_timer = 3;
1899 }
1900 else {
1901 0 gb->apu.square_sweep_calculate_countdown_reload_timer = 2;
1902 }
1903 0 gb->apu.unshifted_sweep = false;
1904 0 ▶ if (!was_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (!was_active) { 0 0 0
1905 0 gb->apu.square_sweep_calculate_countdown_reload_timer++;
1906 }
1907 0 gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length;
1908 0 gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7);
1909 }
1910 else {
1911 4 gb->apu.sweep_length_addend = 0;
1912 }
1913 4 ▶ gb->apu.channel_1_restart_hold = 2 - gb->apu.lf_div + (GB_is_cgb(gb) && gb->model != GB_MODEL_CGB_D) * 2;
Branch: 2 branches
BranchTrueFalseTotal
gb->apu.channel_1_restart_hold = 2 - gb->apu.lf_div + (GB_is_cgb(gb) && gb->model != GB_MODEL_CGB_D) * 2; 2 2 4
gb->apu.channel_1_restart_hold = 2 - gb->apu.lf_div + (GB_is_cgb(gb) && gb->model != GB_MODEL_CGB_D) * 2; 2 0 2
1914 4 gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7;
1915 }
1916 }
1917  
1918 /* APU glitch - if length is enabled while the DIV-divider's LSB is 1, tick the length once. */
1919 4 ▶ if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older
Branch: 3 branches
BranchTrueFalseTotal
if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older 4 0 4
if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older 2 2 4
if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older 0 2 2
1920 0 ▶ !gb->apu.square_channels[index].length_enabled &&
Branch: 1 branch
BranchTrueFalseTotal
!gb->apu.square_channels[index].length_enabled && 0 0 0
1921 0 ▶ (gb->apu.div_divider & 1) &&
Branch: 1 branch
BranchTrueFalseTotal
(gb->apu.div_divider & 1) && 0 0 0
1922 0 ▶ gb->apu.square_channels[index].pulse_length) {
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.square_channels[index].pulse_length) { 0 0 0
1923 0 gb->apu.square_channels[index].pulse_length--;
1924 0 ▶ if (gb->apu.square_channels[index].pulse_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.square_channels[index].pulse_length == 0) { 0 0 0
1925 0 ▶ if (value & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (value & 0x80) { 0 0 0
1926 0 gb->apu.square_channels[index].pulse_length = 0x3F;
1927 }
1928 else {
1929 0 gb->apu.is_active[index] = false;
1930 0 update_sample(gb, index, 0, 0);
1931 }
1932 }
1933 }
1934 4 gb->apu.square_channels[index].length_enabled = value & 0x40;
1935 4 break;
1936 }
1937  
1938 /* Wave channel */
1939 0 case GB_IO_NR30:
1940 0 gb->apu.wave_channel.enable = value & 0x80;
1941 0 ▶ if (!gb->apu.wave_channel.enable) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu.wave_channel.enable) { 0 0 0
1942 0 gb->apu.wave_channel.pulsed = false;
1943 0 ▶ if (gb->apu.is_active[GB_WAVE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[GB_WAVE]) { 0 0 0
1944 // Todo: I assume this happens on pre-CGB models; test this with an audible test
1945 0 ▶ if (gb->apu.wave_channel.sample_countdown == 0 && gb->model <= GB_MODEL_CGB_E) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.wave_channel.sample_countdown == 0 && gb->model <= GB_MODEL_CGB_E) { 0 0 0
if (gb->apu.wave_channel.sample_countdown == 0 && gb->model <= GB_MODEL_CGB_E) { 0 0 0
1946 0 gb->apu.wave_channel.current_sample_byte = gb->io_registers[GB_IO_WAV_START + (gb->pc & 0xF)];
1947 }
1948 0 ▶ else if (gb->apu.wave_channel.wave_form_just_read && gb->model <= GB_MODEL_CGB_C) {
Branch: 2 branches
BranchTrueFalseTotal
else if (gb->apu.wave_channel.wave_form_just_read && gb->model <= GB_MODEL_CGB_C) { 0 0 0
else if (gb->apu.wave_channel.wave_form_just_read && gb->model <= GB_MODEL_CGB_C) { 0 0 0
1949 0 gb->apu.wave_channel.current_sample_byte = gb->io_registers[GB_IO_WAV_START + (GB_IO_NR30 & 0xF)];
1950 }
1951 }
1952 0 gb->apu.is_active[GB_WAVE] = false;
1953 0 update_sample(gb, GB_WAVE, 0, 0);
1954 }
1955 0 break;
1956 0 case GB_IO_NR31:
1957 0 gb->apu.wave_channel.pulse_length = (0x100 - value);
1958 0 break;
1959 0 case GB_IO_NR32:
1960 0 gb->apu.wave_channel.shift = inline_const(uint8_t[], {4, 0, 1, 2})[(value >> 5) & 3];
1961 0 ▶ if (gb->apu.is_active[GB_WAVE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.is_active[GB_WAVE]) { 0 0 0
1962 0 update_wave_sample(gb, 0);
1963 }
1964 0 break;
1965 0 case GB_IO_NR33:
1966 0 gb->apu.wave_channel.sample_length &= ~0xFF;
1967 0 gb->apu.wave_channel.sample_length |= value & 0xFF;
1968 0 ▶ if (gb->apu.wave_channel.bugged_read_countdown == 1) { // Just reloaded countdown
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.bugged_read_countdown == 1) { // Just reloaded countdown 0 0 0
1969 /* TODO: not verified with a test ROM yet */
1970 0 gb->apu.wave_channel.sample_countdown = gb->apu.wave_channel.sample_length ^ 0x7FF;
1971 }
1972 0 break;
1973 0 case GB_IO_NR34:
1974 0 gb->apu.wave_channel.sample_length &= 0xFF;
1975 0 gb->apu.wave_channel.sample_length |= (value & 7) << 8;
1976 0 ▶ if (value & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (value & 0x80) { 0 0 0
1977 0 gb->apu.wave_channel.pulsed = true;
1978 /* DMG bug: wave RAM gets corrupted if the channel is retriggerred 1 cycle before the APU
1979 reads from it. */
1980 0 ▶ if (!GB_is_cgb(gb) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_cgb(gb) && 0 0 0
1981 0 ▶ gb->apu.is_active[GB_WAVE] &&
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.is_active[GB_WAVE] && 0 0 0
1982 0 ▶ gb->apu.wave_channel.sample_countdown == 0) {
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.wave_channel.sample_countdown == 0) { 0 0 0
1983 0 unsigned offset = ((gb->apu.wave_channel.current_sample_index + 1) >> 1) & 0xF;
1984  
1985 /* This glitch varies between models and even specific instances:
1986 DMG-B: Most of them behave as emulated. A few behave differently.
1987 SGB: As far as I know, all tested instances behave as emulated.
1988 MGB, SGB2: Most instances behave non-deterministically, a few behave as emulated.
1989
1990 For DMG-B emulation I emulate the most common behavior, which blargg's tests expect (not my own DMG-B, which fails it)
1991 For MGB emulation, I emulate my Game Boy Light, which happens to be deterministic.
1992  
1993 Additionally, I believe DMGs, including those we behave differently than emulated,
1994 are all deterministic. */
1995 0 ▶ if (offset < 4 && gb->model != GB_MODEL_MGB) {
Branch: 2 branches
BranchTrueFalseTotal
if (offset < 4 && gb->model != GB_MODEL_MGB) { 0 0 0
if (offset < 4 && gb->model != GB_MODEL_MGB) { 0 0 0
1996 0 gb->io_registers[GB_IO_WAV_START] = gb->io_registers[GB_IO_WAV_START + offset];
1997 }
1998 else {
1999 0 memcpy(gb->io_registers + GB_IO_WAV_START,
2000 0 gb->io_registers + GB_IO_WAV_START + (offset & ~3),
2001 4);
2002 }
2003 }
2004 0 gb->apu.wave_channel.current_sample_index = 0;
2005 0 ▶ if (gb->apu.is_active[GB_WAVE] && gb->apu.wave_channel.sample_countdown == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.is_active[GB_WAVE] && gb->apu.wave_channel.sample_countdown == 0) { 0 0 0
if (gb->apu.is_active[GB_WAVE] && gb->apu.wave_channel.sample_countdown == 0) { 0 0 0
2006 0 gb->apu.wave_channel.current_sample_byte = gb->io_registers[GB_IO_WAV_START];
2007 }
2008 0 ▶ if (gb->apu.wave_channel.enable) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.enable) { 0 0 0
2009 0 gb->apu.is_active[GB_WAVE] = true;
2010 0 update_sample(gb, GB_WAVE,
2011 0 (gb->apu.wave_channel.current_sample_byte >> 4) >> gb->apu.wave_channel.shift,
2012 0);
2013 }
2014 0 gb->apu.wave_channel.sample_countdown = (gb->apu.wave_channel.sample_length ^ 0x7FF) + 3;
2015 0 ▶ if (gb->apu.wave_channel.pulse_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.pulse_length == 0) { 0 0 0
2016 0 gb->apu.wave_channel.pulse_length = 0x100;
2017 0 gb->apu.wave_channel.length_enabled = false;
2018 }
2019 /* Note that we don't change the sample just yet! This was verified on hardware. */
2020 }
2021  
2022 /* APU glitch - if length is enabled while the DIV-divider's LSB is 1, tick the length once. */
2023 0 ▶ if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older
Branch: 3 branches
BranchTrueFalseTotal
if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older 0 0 0
if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older 0 0 0
if (((value & 0x40) || (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_B)) && // Current value is irrelevant on CGB-B and older 0 0 0
2024 0 ▶ !gb->apu.wave_channel.length_enabled &&
Branch: 1 branch
BranchTrueFalseTotal
!gb->apu.wave_channel.length_enabled && 0 0 0
2025 0 ▶ (gb->apu.div_divider & 1) &&
Branch: 1 branch
BranchTrueFalseTotal
(gb->apu.div_divider & 1) && 0 0 0
2026 0 ▶ gb->apu.wave_channel.pulse_length) {
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.wave_channel.pulse_length) { 0 0 0
2027 0 gb->apu.wave_channel.pulse_length--;
2028 0 ▶ if (gb->apu.wave_channel.pulse_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.wave_channel.pulse_length == 0) { 0 0 0
2029 0 ▶ if (value & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (value & 0x80) { 0 0 0
2030 0 gb->apu.wave_channel.pulse_length = 0xFF;
2031 }
2032 else {
2033 0 gb->apu.is_active[GB_WAVE] = false;
2034 0 update_sample(gb, GB_WAVE, 0, 0);
2035 }
2036 }
2037 }
2038 0 gb->apu.wave_channel.length_enabled = value & 0x40;
2039  
2040 0 break;
2041  
2042 /* Noise Channel */
2043  
2044 0 case GB_IO_NR41: {
2045 0 gb->apu.noise_channel.pulse_length = (0x40 - (value & 0x3F));
2046 0 break;
2047 }
2048  
2049 0 case GB_IO_NR42: {
2050 0 ▶ if ((value & 0xF8) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value & 0xF8) == 0) { 0 0 0
2051 /* This disables the DAC */
2052 0 ▶ if (gb->apu.is_active[GB_NOISE] && gb->io_registers[GB_IO_NR43] & 7) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->apu.is_active[GB_NOISE] && gb->io_registers[GB_IO_NR43] & 7) { 0 0 0
if (gb->apu.is_active[GB_NOISE] && gb->io_registers[GB_IO_NR43] & 7) { 0 0 0
2053 0 ▶ if (gb->apu.noise_channel.counter_countdown <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.counter_countdown <= 2) { 0 0 0
2054 0 gb->apu.noise_channel.counter++;
2055 }
2056 0 gb->apu.noise_background_counter_active = false;
2057 }
2058  
2059 0 gb->io_registers[reg] = value;
2060 0 gb->apu.is_active[GB_NOISE] = false;
2061 0 update_sample(gb, GB_NOISE, 0, 0);
2062 0 gb->apu.noise_counter_active = false;
2063 }
2064 0 ▶ else if (gb->apu.is_active[GB_NOISE]) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->apu.is_active[GB_NOISE]) { 0 0 0
2065 0 nrx2_glitch(gb, &gb->apu.noise_channel.current_volume,
2066 0 value, gb->io_registers[reg], &gb->apu.noise_channel.volume_countdown,
2067 &gb->apu.noise_channel.envelope_clock);
2068 0 update_sample(gb, GB_NOISE,
2069 0 ▶ gb->apu.noise_channel.current_lfsr_sample ?
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.noise_channel.current_lfsr_sample ? 0 0 0
2070 0 gb->apu.noise_channel.current_volume : 0,
2071 0);
2072 }
2073 0 break;
2074 }
2075  
2076 0 case GB_IO_NR43: {
2077 0 ▶ if (gb->apu.noise_channel.countdown_reloaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.countdown_reloaded) { 0 0 0
2078 0 unsigned divisor = (value & 0x07) << 2;
2079 0 ▶ if (!divisor) divisor = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (!divisor) divisor = 2; 0 0 0
2080 0 ▶ if (gb->model > GB_MODEL_CGB_C) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model > GB_MODEL_CGB_C) { 0 0 0
2081 0 gb->apu.noise_channel.counter_countdown =
2082 0 ▶ divisor + (divisor == 2? 0 : inline_const(uint8_t[], {2, 1, 0, 3})[(gb->apu.noise_channel.alignment) & 3]);
Branch: 1 branch
BranchTrueFalseTotal
divisor + (divisor == 2? 0 : inline_const(uint8_t[], {2, 1, 0, 3})[(gb->apu.noise_channel.alignment) & 3]); 0 0 0
2083 }
2084 else {
2085 0 gb->apu.noise_channel.counter_countdown =
2086 0 ▶ divisor + (divisor == 2? 0 : inline_const(uint8_t[], {2, 1, 4, 3})[(gb->apu.noise_channel.alignment) & 3]);
Branch: 1 branch
BranchTrueFalseTotal
divisor + (divisor == 2? 0 : inline_const(uint8_t[], {2, 1, 4, 3})[(gb->apu.noise_channel.alignment) & 3]); 0 0 0
2087 }
2088 }
2089 0 ▶ if (gb->model <= GB_MODEL_CGB_C) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_C) { 0 0 0
2090 /* TODO: CGB≤C (and DMG) have various unemulated quirks when you write to NR43 just as the counter reloads */
2091 0 ▶ if (gb->apu.noise_channel.countdown_reloaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.countdown_reloaded) { 0 0 0
2092 0 bool old_bit = (gb->apu.noise_channel.counter >> (gb->io_registers[GB_IO_NR43] >> 4)) & 1;
2093 0 bool glitch_bit = (gb->apu.noise_channel.counter >> 7) & 1;
2094 0 bool new_bit = (gb->apu.noise_channel.counter >> (value >> 4)) & 1;
2095
2096 0 ▶ if (!old_bit && new_bit && glitch_bit) {
Branch: 3 branches
BranchTrueFalseTotal
if (!old_bit && new_bit && glitch_bit) { 0 0 0
if (!old_bit && new_bit && glitch_bit) { 0 0 0
if (!old_bit && new_bit && glitch_bit) { 0 0 0
2097 0 uint16_t previous_counter = (gb->apu.noise_channel.counter - 1) & 0x3FFF;
2098 0 bool old_bit = (previous_counter >> (gb->io_registers[GB_IO_NR43] >> 4)) & 1;
2099 0 bool glitch_bit = (previous_counter >> 7) & 1;
2100 0 bool new_bit = (previous_counter >> (value >> 4)) & 1;
2101 0 ▶ if (old_bit && !new_bit && glitch_bit) {
Branch: 3 branches
BranchTrueFalseTotal
if (old_bit && !new_bit && glitch_bit) { 0 0 0
if (old_bit && !new_bit && glitch_bit) { 0 0 0
if (old_bit && !new_bit && glitch_bit) { 0 0 0
2102 0 step_lfsr(gb, 0);
2103 }
2104 }
2105 }
2106 0 nr43_write(gb, 0xff);
2107 }
2108 0 nr43_write(gb, value);
2109
2110 0 break;
2111 }
2112  
2113 0 case GB_IO_NR44: {
2114 0 ▶ if (value & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (value & 0x80) { 0 0 0
2115 0 gb->apu.noise_channel.envelope_clock.locked = false;
2116 0 gb->apu.noise_channel.envelope_clock.clock = false;
2117 0 ▶ if (!GB_is_cgb(gb) && (gb->apu.noise_channel.alignment & 3) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!GB_is_cgb(gb) && (gb->apu.noise_channel.alignment & 3) != 0) { 0 0 0
if (!GB_is_cgb(gb) && (gb->apu.noise_channel.alignment & 3) != 0) { 0 0 0
2118 0 gb->apu.noise_channel.dmg_delayed_start = 6;
2119 }
2120 else {
2121 0 gb->apu.noise_channel.lfsr = 0;
2122 0 prepare_noise_start(gb);
2123
2124 0 gb->apu.noise_channel.current_volume = gb->io_registers[GB_IO_NR42] >> 4;
2125 0 gb->apu.noise_channel.current_lfsr_sample = false;
2126 0 gb->apu.noise_channel.volume_countdown = gb->io_registers[GB_IO_NR42] & 7;
2127 0 gb->apu.noise_channel.did_step_counter = (gb->apu.noise_channel.alignment & 3) == 2;
2128  
2129 0 ▶ if (gb->io_registers[GB_IO_NR42] & 0xF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_NR42] & 0xF8) { 0 0 0
2130 0 gb->apu.is_active[GB_NOISE] = true;
2131 0 update_sample(gb, GB_NOISE, 0, 0);
2132 }
2133  
2134 0 ▶ if (gb->apu.noise_channel.pulse_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.pulse_length == 0) { 0 0 0
2135 0 gb->apu.noise_channel.pulse_length = 0x40;
2136 0 gb->apu.noise_channel.length_enabled = false;
2137 }
2138 }
2139 }
2140  
2141 /* APU glitch - if length is enabled while the DIV-divider's LSB is 1, tick the length once. */
2142 0 ▶ if ((value & 0x40) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((value & 0x40) && 0 0 0
2143 0 ▶ !gb->apu.noise_channel.length_enabled &&
Branch: 1 branch
BranchTrueFalseTotal
!gb->apu.noise_channel.length_enabled && 0 0 0
2144 0 ▶ (gb->apu.div_divider & 1) &&
Branch: 1 branch
BranchTrueFalseTotal
(gb->apu.div_divider & 1) && 0 0 0
2145 0 ▶ gb->apu.noise_channel.pulse_length) {
Branch: 1 branch
BranchTrueFalseTotal
gb->apu.noise_channel.pulse_length) { 0 0 0
2146 0 gb->apu.noise_channel.pulse_length--;
2147 0 ▶ if (gb->apu.noise_channel.pulse_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu.noise_channel.pulse_length == 0) { 0 0 0
2148 0 ▶ if (value & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (value & 0x80) { 0 0 0
2149 0 gb->apu.noise_channel.pulse_length = 0x3F;
2150 }
2151 else {
2152 0 gb->apu.is_active[GB_NOISE] = false;
2153 0 update_sample(gb, GB_NOISE, 0, 0);
2154 }
2155 }
2156 }
2157 0 gb->apu.noise_channel.length_enabled = value & 0x40;
2158 0 break;
2159 }
2160 }
2161 160 gb->io_registers[reg] = value;
2162 }
2163  
2164 37 void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate)
2165 {
2166 37 if (gb->apu_output.sample_rate != sample_rate) {
2167 GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
2168 }
2169 37 gb->apu_output.sample_rate = sample_rate;
2170 37 ▶ if (sample_rate) {
Branch: 1 branch
BranchTrueFalseTotal
if (sample_rate) { 21 16 37
2171 21 gb->apu_output.highpass_rate = pow(0.999958, GB_get_clock_rate(gb) / (double)sample_rate);
2172 21 gb->apu_output.max_cycles_per_sample = ceil(GB_get_clock_rate(gb) / 2.0 / sample_rate);
2173 21 gb->apu_output.quick_fraction_multiply_cache[0] = round(sample_rate * 2.0 / GB_get_clock_rate(gb) * (1 << 28));
2174 1344 ▶ for (unsigned i = 1; i < GB_QUICK_MULTIPLY_COUNT; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 1; i < GB_QUICK_MULTIPLY_COUNT; i++) { 1323 21 1344
2175 1323 gb->apu_output.quick_fraction_multiply_cache[i] = gb->apu_output.quick_fraction_multiply_cache[0] * (i + 1);
2176 }
2177 }
2178 else {
2179 16 gb->apu_output.max_cycles_per_sample = 0x400;
2180 }
2181 37 }
2182  
2183 0 void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, double cycles_per_sample)
2184 {
2185 GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
2186 0 ▶ if (cycles_per_sample == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cycles_per_sample == 0) { 0 0 0
2187 0 GB_set_sample_rate(gb, 0);
2188 0 return;
2189 }
2190 0 gb->apu_output.sample_rate = GB_get_clock_rate(gb) / cycles_per_sample * 2;
2191 0 gb->apu_output.highpass_rate = pow(0.999958, cycles_per_sample);
2192 0 gb->apu_output.max_cycles_per_sample = ceil(cycles_per_sample / 4);
2193
2194 0 gb->apu_output.quick_fraction_multiply_cache[0] = round(gb->apu_output.sample_rate * 2.0 / GB_get_clock_rate(gb) * (1 << 28));
2195 0 ▶ for (unsigned i = 1; i < GB_QUICK_MULTIPLY_COUNT; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 1; i < GB_QUICK_MULTIPLY_COUNT; i++) { 0 0 0
2196 0 gb->apu_output.quick_fraction_multiply_cache[i] = gb->apu_output.quick_fraction_multiply_cache[0] * (i + 1);
2197 }
2198 }
2199  
2200 0 unsigned GB_get_sample_rate(GB_gameboy_t *gb)
2201 {
2202 0 return gb->apu_output.sample_rate;
2203 }
2204  
2205 16 void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback)
2206 {
2207 16 gb->apu_output.sample_callback = callback;
2208 16 }
2209  
2210 1 void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode)
2211 {
2212 1 gb->apu_output.highpass_mode = mode;
2213 1 }
2214  
2215 0 void GB_set_interference_volume(GB_gameboy_t *gb, double volume)
2216 {
2217 0 gb->apu_output.interference_volume = volume;
2218 0 }
2219  
2220 typedef struct __attribute__((packed)) {
2221 uint32_t format_chunk; // = BE32('FORM')
2222 uint32_t size; // = BE32(file size - 8)
2223 uint32_t format; // = BE32('AIFC')
2224
2225 uint32_t fver_chunk; // = BE32('FVER')
2226 uint32_t fver_size; // = BE32(4)
2227 uint32_t fver;
2228
2229 uint32_t comm_chunk; // = BE32('COMM')
2230 uint32_t comm_size; // = BE32(0x18)
2231
2232 uint16_t channels; // = BE16(2)
2233 uint32_t samples_per_channel; // = BE32(total number of samples / 2)
2234 uint16_t bit_depth; // = BE16(16)
2235 uint16_t frequency_exponent;
2236 uint64_t frequency_significand;
2237 uint32_t compression_type; // = 'NONE' (BE) or 'twos' (LE)
2238 uint16_t compression_name; // = 0
2239
2240 uint32_t ssnd_chunk; // = BE32('SSND')
2241 uint32_t ssnd_size; // = BE32(length of samples - 8)
2242 uint32_t ssnd_offset; // = 0
2243 uint32_t ssnd_block; // = 0
2244 } aiff_header_t;
2245  
2246 typedef struct __attribute__((packed)) {
2247 uint32_t marker; // = BE32('RIFF')
2248 uint32_t size; // = LE32(file size - 8)
2249 uint32_t type; // = BE32('WAVE')
2250
2251 uint32_t fmt_chunk; // = BE32('fmt ')
2252 uint32_t fmt_size; // = LE16(16)
2253 uint16_t format; // = LE16(1)
2254 uint16_t channels; // = LE16(2)
2255 uint32_t sample_rate; // = LE32(sample_rate)
2256 uint32_t byte_rate; // = LE32(sample_rate * 4)
2257 uint16_t frame_size; // = LE32(4)
2258 uint16_t bit_depth; // = LE16(16)
2259
2260 uint32_t data_chunk; // = BE32('data')
2261 uint32_t data_size; // = LE32(length of samples)
2262 } wav_header_t;
2263  
2264  
2265 0 int GB_start_audio_recording(GB_gameboy_t *gb, const char *path, GB_audio_format_t format)
2266 {
2267 0 ▶ if (gb->apu_output.sample_rate == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.sample_rate == 0) { 0 0 0
2268 0 return EINVAL;
2269 }
2270
2271 0 ▶ if (gb->apu_output.output_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->apu_output.output_file) { 0 0 0
2272 0 GB_stop_audio_recording(gb);
2273 }
2274 0 gb->apu_output.output_file = fopen(path, "wb");
2275 0 ▶ if (!gb->apu_output.output_file) return errno;
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu_output.output_file) return errno; 0 0 0
2276
2277 0 gb->apu_output.output_format = format;
2278 0 switch (format) {
2279 0 case GB_AUDIO_FORMAT_RAW:
2280 0 return 0;
2281 0 case GB_AUDIO_FORMAT_AIFF: {
2282 0 aiff_header_t header = {0,};
2283 0 ▶ if (fwrite(&header, sizeof(header), 1, gb->apu_output.output_file) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fwrite(&header, sizeof(header), 1, gb->apu_output.output_file) != 1) { 0 0 0
2284 0 ▶ int ret = errno ?: EIO;
Branch: 1 branch
BranchTrueFalseTotal
int ret = errno ?: EIO; 0 0 0
2285 0 fclose(gb->apu_output.output_file);
2286 0 gb->apu_output.output_file = NULL;
2287 0 return ret;
2288 }
2289 0 return 0;
2290 }
2291 0 case GB_AUDIO_FORMAT_WAV: {
2292 0 wav_header_t header = {0,};
2293 0 ▶ if (fwrite(&header, sizeof(header), 1, gb->apu_output.output_file) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fwrite(&header, sizeof(header), 1, gb->apu_output.output_file) != 1) { 0 0 0
2294 0 ▶ int ret = errno ?: EIO;
Branch: 1 branch
BranchTrueFalseTotal
int ret = errno ?: EIO; 0 0 0
2295 0 fclose(gb->apu_output.output_file);
2296 0 gb->apu_output.output_file = NULL;
2297 0 return ret;
2298 }
2299 0 return 0;
2300 }
2301 0 default:
2302 0 fclose(gb->apu_output.output_file);
2303 0 gb->apu_output.output_file = NULL;
2304 0 return EINVAL;
2305 }
2306 }
2307 16 int GB_stop_audio_recording(GB_gameboy_t *gb)
2308 {
2309 16 ▶ if (!gb->apu_output.output_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->apu_output.output_file) { 16 0 16
2310 16 ▶ int ret = gb->apu_output.output_error ?: -1;
Branch: 1 branch
BranchTrueFalseTotal
int ret = gb->apu_output.output_error ?: -1; 0 16 16
2311 16 gb->apu_output.output_error = 0;
2312 16 return ret;
2313 }
2314 0 gb->apu_output.output_error = 0;
2315 0 switch (gb->apu_output.output_format) {
2316 0 case GB_AUDIO_FORMAT_RAW:
2317 0 break;
2318 0 case GB_AUDIO_FORMAT_AIFF: {
2319 0 size_t file_size = ftell(gb->apu_output.output_file);
2320 0 size_t frames = (file_size - sizeof(aiff_header_t)) / sizeof(GB_sample_t);
2321 0 aiff_header_t header = {
2322 .format_chunk = BE32('FORM'),
2323 0 .size = BE32(file_size - 8),
2324 .format = BE32('AIFC'),
2325
2326 .fver_chunk = BE32('FVER'),
2327 .fver_size = BE32(4),
2328 .fver = BE32(0xA2805140),
2329
2330 .comm_chunk = BE32('COMM'),
2331 .comm_size = BE32(0x18),
2332 .channels = BE16(2),
2333 0 .samples_per_channel = BE32(frames),
2334 .bit_depth = BE16(16),
2335 #ifdef GB_BIG_ENDIAN
2336 .compression_type = 'NONE',
2337 #else
2338 .compression_type = 'twos',
2339 #endif
2340 .compression_name = 0,
2341 .ssnd_chunk = BE32('SSND'),
2342 0 .ssnd_size = BE32(frames * sizeof(GB_sample_t) - 8),
2343 .ssnd_offset = 0,
2344 .ssnd_block = 0,
2345 };
2346
2347 0 uint64_t significand = gb->apu_output.sample_rate;
2348 0 uint16_t exponent = 0x403E;
2349 0 ▶ while ((int64_t)significand > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((int64_t)significand > 0) { 0 0 0
2350 0 significand <<= 1;
2351 0 exponent--;
2352 }
2353 0 header.frequency_exponent = BE16(exponent);
2354 0 header.frequency_significand = BE64(significand);
2355
2356 0 fseek(gb->apu_output.output_file, 0, SEEK_SET);
2357 0 ▶ if (fwrite(&header, sizeof(header), 1, gb->apu_output.output_file) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fwrite(&header, sizeof(header), 1, gb->apu_output.output_file) != 1) { 0 0 0
2358 0 gb->apu_output.output_error = errno;
2359 }
2360 0 break;
2361 }
2362 0 case GB_AUDIO_FORMAT_WAV: {
2363 0 size_t file_size = ftell(gb->apu_output.output_file);
2364 0 size_t frames = (file_size - sizeof(wav_header_t)) / sizeof(GB_sample_t);
2365 0 wav_header_t header = {
2366 .marker = BE32('RIFF'),
2367 0 .size = LE32(file_size - 8),
2368 .type = BE32('WAVE'),
2369
2370 .fmt_chunk = BE32('fmt '),
2371 .fmt_size = LE16(16),
2372 .format = LE16(1),
2373 .channels = LE16(2),
2374 0 .sample_rate = LE32(gb->apu_output.sample_rate),
2375 0 .byte_rate = LE32(gb->apu_output.sample_rate * 4),
2376 .frame_size = LE32(4),
2377 .bit_depth = LE16(16),
2378
2379 .data_chunk = BE32('data'),
2380 .data_size = LE32(frames * sizeof(GB_sample_t)),
2381 };
2382
2383 0 fseek(gb->apu_output.output_file, 0, SEEK_SET);
2384 0 ▶ if (fwrite(&header, sizeof(header), 1, gb->apu_output.output_file) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fwrite(&header, sizeof(header), 1, gb->apu_output.output_file) != 1) { 0 0 0
2385 0 gb->apu_output.output_error = errno;
2386 }
2387 0 break;
2388 }
2389 }
2390 0 fclose(gb->apu_output.output_file);
2391 0 gb->apu_output.output_file = NULL;
2392
2393 0 int ret = gb->apu_output.output_error;
2394 0 gb->apu_output.output_error = 0;
2395 0 return ret;
2396 }
2397  
2398  
2399 0 void GB_set_channel_muted(GB_gameboy_t *gb, GB_channel_t channel, bool muted)
2400 {
2401 assert(channel < GB_N_CHANNELS);
2402 0 gb->apu_output.channel_muted[channel] = muted;
2403 0 }
2404  
2405 0 bool GB_is_channel_muted(GB_gameboy_t *gb, GB_channel_t channel)
2406 {
2407 0 return gb->apu_output.channel_muted[channel];
2408 }
2409  
2410 // Note: this intentionally does not check to see if the channel is muted.
2411 0 uint8_t GB_get_channel_volume(GB_gameboy_t *gb, GB_channel_t channel)
2412 {
2413 0 switch (channel) {
2414 0 case GB_SQUARE_1:
2415 case GB_SQUARE_2:
2416 0 return gb->apu.square_channels[channel].current_volume;
2417  
2418 0 case GB_WAVE:
2419 0 return inline_const(uint8_t[], {0xF, 8, 4, 0, 0})[gb->apu.wave_channel.shift];
2420  
2421 0 case GB_NOISE:
2422 0 return gb->apu.noise_channel.current_volume;
2423  
2424 0 default:
2425 0 return 0;
2426 }
2427 }
2428  
2429 0 uint8_t GB_get_channel_amplitude(GB_gameboy_t *gb, GB_channel_t channel)
2430 {
2431 0 ▶ return gb->apu.is_active[channel] ? gb->apu.samples[channel] : 0;
Branch: 1 branch
BranchTrueFalseTotal
return gb->apu.is_active[channel] ? gb->apu.samples[channel] : 0; 0 0 0
2432 }
2433  
2434 0 uint16_t GB_get_channel_period(GB_gameboy_t *gb, GB_channel_t channel)
2435 {
2436 0 switch (channel) {
2437 0 case GB_SQUARE_1:
2438 case GB_SQUARE_2:
2439 0 return gb->apu.square_channels[channel].sample_length;
2440  
2441 0 case GB_WAVE:
2442 0 return gb->apu.wave_channel.sample_length;
2443  
2444 0 case GB_NOISE:
2445 0 return (gb->io_registers[GB_IO_NR43] & 7) << (gb->io_registers[GB_IO_NR43] >> 4);
2446  
2447 0 default:
2448 0 return 0;
2449 }
2450 }
2451  
2452 // wave_table is a user allocated uint8_t[32] array
2453 0 void GB_get_apu_wave_table(GB_gameboy_t *gb, uint8_t *wave_table)
2454 {
2455 0 ▶ for (unsigned i = GB_IO_WAV_START; i <= GB_IO_WAV_END; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = GB_IO_WAV_START; i <= GB_IO_WAV_END; i++) { 0 0 0
2456 0 wave_table[2 * (i - GB_IO_WAV_START)] = gb->io_registers[i] >> 4;
2457 0 wave_table[2 * (i - GB_IO_WAV_START) + 1] = gb->io_registers[i] & 0xF;
2458 }
2459 0 }
2460  
2461 0 bool GB_get_channel_edge_triggered(GB_gameboy_t *gb, GB_channel_t channel)
2462 {
2463 0 bool edge_triggered = gb->apu_output.edge_triggered[channel];
2464 0 gb->apu_output.edge_triggered[channel] = false;
2465 0 return edge_triggered;
2466 }
« src/Core/save_state.c ↑ Summary src/Core/mbc.c »