Debian Codegraph @

src/engine/platform/sound/nes/apu.h

raw
1 /*
2 * Copyright (C) 2010-2019 Fabio Cavallo (aka FHorse)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 // additional modifications by tildearrow for furnace
20
21 #ifndef APU_H_
22 #define APU_H_
23
24 #include "common.h"
25 #include "blip_buf.h"
26 #include "../../../dispatch.h"
27
28 enum dmc_types_of_dma { DMC_NORMAL, DMC_CPU_WRITE, DMC_R4014, DMC_NNL_DMA };
29 enum apu_channels { APU_S1, APU_S2, APU_TR, APU_NS, APU_DMC, APU_EXTRA, APU_MASTER };
30 enum apu_mode { APU_60HZ, APU_48HZ };
31
32 #define apu_pre_amp 1.4f
33
34 /* length counter */
35 #define length_run(channel)\
36 /*\
37 * se non e' settato il flag halt e il length\
38 * counter non e' 0 allora devo decrementarlo.\
39 */\
40 if (!channel.length.halt && channel.length.value) {\
41 channel.length.value--;\
42 }
43 #define length_clock()\
44 a->apu.length_clocked = TRUE;\
45 length_run(a->S1)\
46 length_run(a->S2)\
47 length_run(a->TR)\
48 length_run(a->NS)
49 /* envelope */
50 #define envelope_run(channel)\
51 if (channel.envelope.enabled) {\
52 channel.envelope.enabled = FALSE;\
53 channel.envelope.counter = 15;\
54 channel.envelope.delay = (channel.envelope.divider + 1);\
55 } else if (!(--channel.envelope.delay)) {\
56 channel.envelope.delay = (channel.envelope.divider + 1);\
57 if (channel.envelope.counter | channel.length.halt) {\
58 channel.envelope.counter = (channel.envelope.counter - 1) & 0x0F;\
59 }\
60 }
61 #define envelope_volume(channel)\
62 /* setto il volume */\
63 if (!channel.length.value) {\
64 channel.volume = 0;\
65 } else if (channel.envelope.constant_volume) {\
66 channel.volume = channel.envelope.divider;\
67 } else {\
68 channel.volume = channel.envelope.counter;\
69 }
70 #define envelope_clock()\
71 envelope_run(a->S1)\
72 envelope_run(a->S2)\
73 envelope_run(a->NS)
74 /* sweep */
75 #define sweep_run(channel, negative_adjust)\
76 if (!(--channel.sweep.delay)) {\
77 channel.sweep.delay = (channel.sweep.divider + 1);\
78 if (channel.sweep.enabled && channel.sweep.shift && (channel.timer >= 8)) {\
79 SWORD offset = channel.timer >> channel.sweep.shift;\
80 if (channel.sweep.negate) {\
81 channel.timer += ((SWORD) negative_adjust - offset);\
82 } else if ((channel.timer + offset) <= 0x800) {\
83 channel.timer += offset;\
84 }\
85 }\
86 sweep_silence(channel)\
87 }\
88 if (channel.sweep.reload) {\
89 channel.sweep.reload = FALSE;\
90 channel.sweep.delay = (channel.sweep.divider + 1);\
91 }
92 #define sweep_silence(channel)\
93 {\
94 WORD offset = channel.timer >> channel.sweep.shift;\
95 channel.sweep.silence = FALSE;\
96 if ((channel.timer <= 8) || (!channel.sweep.negate && ((channel.timer + offset) >= 0x800))) {\
97 channel.sweep.silence = TRUE;\
98 }\
99 }
100 #define sweep_clock()\
101 sweep_run(a->S1, -1)\
102 sweep_run(a->S2, 0)
103 /* linear counter */
104 #define linear_clock()\
105 if (a->TR.linear.halt) {\
106 a->TR.linear.value = a->TR.linear.reload;\
107 } else if (a->TR.linear.value) {\
108 a->TR.linear.value--;\
109 }\
110 if (!a->TR.length.halt) {\
111 a->TR.linear.halt = FALSE;\
112 }
113 /* output */
114 #define square_output(square, swap)\
115 {\
116 envelope_volume(square)\
117 if (square.sweep.silence) {\
118 square.output = 0;\
119 } else {\
120 square.output = square_duty[swap][square.duty][square.sequencer] * square.volume;\
121 }\
122 }
123 #define triangle_output()\
124 /*\
125 * ai 2 cicli piu' bassi del timer, la frequenza\
126 * risultante e' troppo alta (oltre i 20 kHz,\
127 * quindi non udibile), percio' la taglio.\
128 */\
129 a->TR.output = triangle_duty[a->TR.sequencer];\
130 if (a->TR.timer < 2) {\
131 a->TR.output = triangle_duty[8];\
132 }
133 #define noise_output()\
134 envelope_volume(a->NS)\
135 a->NS.output = 0;\
136 if (a->NS.length.value && !(a->NS.shift & 0x0001)) {\
137 a->NS.output = a->NS.volume;\
138 }
139 #define dmc_output()\
140 a->DMC.output = a->DMC.counter & 0x7F
141 /* tick */
142
143 #define apu_change_step(index)\
144 a->apu.cycles += apuPeriod[a->apu.mode][a->apu.type][index]
145 #if defined (VECCHIA_GESTIONE_JITTER)
146 #define r4017_jitter()\
147 a->r4017.value = (a->r4017.jitter.value & 0xC0);\
148 /*\
149 * se il bit 7 e' a zero, devo attivare la\
150 * modalita' NTSC, se a uno quella PAL.\
151 */\
152 if (a->r4017.value & 0x80) {\
153 a->apu.mode = APU_48HZ;\
154 } else {\
155 a->apu.mode = APU_60HZ;\
156 }\
157 if (a->r4017.value & 0x40) {\
158 /* azzero il bit 6 del $4015 */\
159 a->r4015.value &= 0xBF;\
160 /* questo non e' affatto necessario sul forno */\
161 }\
162 /* riavvio il frame audio */\
163 a->apu.step = a->apu.cycles = 0;\
164 apu_change_step(a->apu.step)
165 #else
166 #define r4017_jitter(apc)\
167 a->r4017.value = (a->r4017.jitter.value & 0xC0);\
168 a->r4017.reset_frame_delay = 1;\
169 if (a->apu.cycles == apc) {\
170 if (a->apu.mode == APU_48HZ) {\
171 a->r4017.reset_frame_delay += 1;\
172 } else {\
173 a->r4017.reset_frame_delay += 2;\
174 }\
175 }\
176 /*\
177 * se il bit 7 e' a zero, devo attivare la\
178 * modalita' NTSC, se a uno quella PAL.\
179 */\
180 if (a->r4017.value & 0x80) {\
181 a->apu.mode = APU_48HZ;\
182 } else {\
183 a->apu.mode = APU_60HZ;\
184 }\
185 if (a->r4017.value & 0x40) {\
186 /* azzero il bit 6 del $4015 */\
187 a->r4015.value &= 0xBF;\
188 /* questo non e' affatto necessario sul forno */\
189 }
190 #define r4017_reset_frame()\
191 if (a->r4017.reset_frame_delay && (--a->r4017.reset_frame_delay == 0)) {\
192 /* riavvio il frame audio */\
193 a->apu.step = a->apu.cycles = 0;\
194 apu_change_step(a->apu.step);\
195 }
196 #endif
197 #define square_reg0(square)\
198 /* duty */\
199 square.duty = value >> 6;\
200 /* length counter */\
201 square.length.halt = value & 0x20;\
202 /* envelope */\
203 square.envelope.constant_volume = value & 0x10;\
204 square.envelope.divider = value & 0x0F
205 #define square_reg1(square)\
206 /* sweep */\
207 square.sweep.reload = TRUE;\
208 square.sweep.divider = (value >> 4) & 0x07;\
209 square.sweep.shift = value & 0x07;\
210 square.sweep.enabled = value & 0x80;\
211 square.sweep.negate = value & 0x08
212 #define square_reg2(square)\
213 /* timer (low 8 bits) */\
214 square.timer = (square.timer & 0x0700) | value;\
215 if (!square.firstTime) {\
216 square.frequency=1;\
217 square.firstTime=1;\
218 }
219 #define square_reg3(square,length_clocked)\
220 /* length counter */\
221 /*\
222 * se non disabilitato, una scrittura in\
223 * questo registro, carica immediatamente il\
224 * length counter del canale, tranne nel caso\
225 * in cui la scrittura avvenga nello stesso\
226 * momento del clock di un length counter e\
227 * con il length diverso da zero.\
228 */\
229 if (square.length.enabled && !(length_clocked && square.length.value)) {\
230 square.length.value = length_table[value >> 3];\
231 }\
232 /* envelope */\
233 square.envelope.enabled = TRUE;\
234 /* timer (high 3 bits) */\
235 square.timer = (square.timer & 0x00FF) | ((value & 0x07) << 8);\
236 /*The correct behaviour is to reset the duty cycle sequencers but not the clock dividers*/\
237 /*square.frequency = 1;*/\
238 /* sequencer */\
239 square.sequencer = 0;\
240 if (!square.firstTime) {\
241 square.frequency=1;\
242 square.firstTime=1;\
243 }
244 #define init_nla_table(p, t)\
245 {\
246 WORD i;\
247 for (i = 0; i < LENGTH(nla_table.pulse); i++) {\
248 double vl = 95.52 / (8128.0 / (double) i + 100.0);\
249 nla_table.pulse[i] = (vl * p);\
250 }\
251 for (i = 0; i < LENGTH(nla_table.tnd); i++) {\
252 double vl = 163.67 / (24329.0 / (double) i + 100.0);\
253 nla_table.tnd[i] = (vl * t);\
254 }\
255 }
256 #define _apu_channel_volume_adjust(ch, index)\
257 ((ch))
258 #define s1_out(a)\
259 (a->muted[0] ? 0 : _apu_channel_volume_adjust(a->S1.output, APU_S1))
260 #define s2_out(a)\
261 (a->muted[1] ? 0 : _apu_channel_volume_adjust(a->S2.output, APU_S2))
262 #define tr_out(a)\
263 (a->muted[2] ? 0 : _apu_channel_volume_adjust(a->TR.output, APU_TR))
264 #define ns_out(a)\
265 (a->muted[3] ? 0 : _apu_channel_volume_adjust(a->NS.output, APU_NS))
266 #define dmc_out(a)\
267 (a->muted[4] ? 0 : _apu_channel_volume_adjust(a->DMC.output, APU_DMC))
268 #define extra_out(ch)\
269 (ch * cfg->apu.channel[APU_EXTRA])
270 #define pulse_output(a)\
271 nla_table.pulse[(int) (s1_out(a) + s2_out(a))]
272 #define tnd_output(a)\
273 nla_table.tnd[(int) ((tr_out(a) * 3) + (ns_out(a) * 2) + dmc_out(a))]
274
275 typedef struct _config_apu {
276 BYTE channel[APU_MASTER + 1];
277 double volume[APU_MASTER + 1];
278 } _config_apu;
279 typedef struct _apu {
280 BYTE mode;
281 BYTE type;
282 BYTE step;
283 BYTE length_clocked;
284 BYTE DMC;
285 SWORD cycles;
286
287 int cpu_cycles;
288 int cpu_opcode_cycle;
289 BYTE odd_cycle;
290 } _apu;
291 typedef struct _r4011 {
292 BYTE value;
293 DBWORD frames;
294 DBWORD cycles;
295 SWORD output;
296 } _r4011;
297 typedef struct _r4015 {
298 BYTE value;
299 } _r4015;
300 typedef struct _r4017 {
301 BYTE value;
302 struct _r4017_litter {
303 BYTE value;
304 BYTE delay;
305 } jitter;
306 BYTE reset_frame_delay;
307 } _r4017;
308 typedef struct _envelope {
309 BYTE enabled;
310 BYTE divider;
311 BYTE counter;
312 BYTE constant_volume;
313 SBYTE delay;
314 } _envelope;
315 typedef struct _sweep {
316 BYTE enabled;
317 BYTE negate;
318 BYTE divider;
319 BYTE shift;
320 BYTE reload;
321 BYTE silence;
322 SBYTE delay;
323 } _sweep;
324 typedef struct _length_counter {
325 BYTE value;
326 BYTE enabled;
327 BYTE halt;
328 } _length_counter;
329 typedef struct _linear_counter {
330 BYTE value;
331 BYTE reload;
332 BYTE halt;
333 } _linear_counter;
334 typedef struct _apuSquare {
335 /* timer */
336 DBWORD timer;
337 /* ogni quanti cicli devo generare un output */
338 WORD frequency;
339 /* optimization */
340 BYTE firstTime;
341 /* duty */
342 BYTE duty;
343 /* envelope */
344 _envelope envelope;
345 /* volume */
346 BYTE volume;
347 /* sequencer */
348 BYTE sequencer;
349 /* sweep */
350 _sweep sweep;
351 /* length counter */
352 _length_counter length;
353 /* output */
354 SWORD output;
355 } _apuSquare;
356 typedef struct _apuTriangle {
357 /* timer */
358 DBWORD timer;
359 /* ogni quanti cicli devo generare un output */
360 WORD frequency;
361 /* linear counter */
362 _linear_counter linear;
363 /* length counter */
364 _length_counter length;
365 /* sequencer */
366 BYTE sequencer;
367 /* optimization */
368 BYTE firstTime;
369 /* output */
370 SWORD output;
371 } _apuTriangle;
372 typedef struct _apuNoise {
373 /* timer */
374 DBWORD timer;
375 /* ogni quanti cicli devo generare un output */
376 WORD frequency;
377 /* envelope */
378 _envelope envelope;
379 /* specifico del noise */
380 BYTE mode;
381 /* volume */
382 BYTE volume;
383 /* shift register */
384 WORD shift;
385 /* length counter */
386 _length_counter length;
387 /* sequencer */
388 BYTE sequencer;
389 /* optimization */
390 BYTE firstTime;
391 /* output */
392 SWORD output;
393 } _apuNoise;
394 typedef struct _apuDMC {
395 /* ogni quanti cicli devo generare un output */
396 WORD frequency;
397
398 WORD remain;
399 BYTE irq_enabled;
400 BYTE loop;
401 BYTE rate_index;
402 WORD address_start;
403 DBWORD address;
404 WORD length;
405 BYTE counter;
406 BYTE empty;
407 BYTE buffer;
408
409 /* DMA */
410 BYTE dma_cycle;
411
412 /* output unit */
413 BYTE silence;
414 BYTE shift;
415 BYTE counter_out;
416
417 /* output */
418 SWORD output;
419
420 /* misc */
421 BYTE tick_type;
422 } _apuDMC;
423
424 struct _nla_table {
425 SWORD pulse[32];
426 SWORD tnd[203];
427 };
428
429 extern struct _nla_table nla_table;
430
431 struct NESAPU {
432 _apu apu;
433 _r4011 r4011;
434 _r4015 r4015;
435 _r4017 r4017;
436 _apuSquare S1, S2;
437 _apuTriangle TR;
438 _apuNoise NS;
439 _apuDMC DMC;
440 blip_buffer_t* bb;
441 void* readDMCUser;
442 unsigned char (*readDMC)(void*,unsigned short);
443 int timestamp;
444 int lastSample;
445 unsigned char muted[5];
446 DivDispatchOscBuffer* oscBuf[5];
447 };
448
449 /* apuPeriod[mode][type][cycles] */
450 static const WORD apuPeriod[2][3][7] = {
451 /*
452 * Mode 0: 4-step sequence
453 * Action Envelopes & Length Counter& Interrupt Delay to next
454 * Linear Counter Sweep Units Flag NTSC PAL Dendy
455 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
456 * $4017=$00 - - - 7459 8315 7459
457 * Step 1 Clock - - 7456 8314 7456
458 * Step 2 Clock Clock - 7458 8312 7458
459 * Step 3 Clock - - 7458 8314 7458
460 * Step 4 Clock Clock Set if enabled 7458 8314 7458
461 */
462 {
463 {7459, 7456, 7458, 7457, 1, 1, 7457},
464 {8315, 8314, 8312, 8313, 1, 1, 8313},
465 {7459, 7456, 7458, 7457, 1, 1, 7457}
466 },
467 /*
468 * Mode 1: 5-step sequence
469 * Action Envelopes & Length Counter& Interrupt Delay to next
470 * Linear Counter Sweep Units Flag NTSC PAL Dendy
471 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
472 * $4017=$80 - - - 1 1 1
473 * Step 1 Clock Clock - 7458 8314 7458
474 * Step 2 Clock - - 7456 8314 7456
475 * Step 3 Clock Clock - 7458 8312 7458
476 * Step 4 Clock - - 7458 8314 7458
477 * Step 5 - - - 7452 8312 7452
478 *
479 * Note:
480 * il 7452 e il 8312 dello step 5 diventano 7451 e 8311
481 * nella mia tabella perche' il ciclo mancante lo eseguo
482 * all'inizio del ciclo successivo.
483 */
484 {
485 {1, 7458, 7456, 7458, 7458, 7451, 0},
486 {1, 8314, 8314, 8312, 8314, 8311, 0},
487 {1, 7458, 7456, 7458, 7458, 7451, 0}
488 }
489 };
490
491 /* la tabella con i valori da caricare nel length counter del canale */
492 static const BYTE length_table[32] = {
493 0x0A, 0xFE, 0x14, 0x02, 0x28, 0x04, 0x50, 0x06,
494 0xA0, 0x08, 0x3C, 0x0A, 0x0E, 0x0C, 0x1A, 0x0E,
495 0x0C, 0x10, 0x18, 0x12, 0x30, 0x14, 0x60, 0x16,
496 0xC0, 0x18, 0x48, 0x1A, 0x10, 0x1C, 0x20, 0x1E
497 };
498
499 static const BYTE square_duty[2][4][8] = {
500 {
501 { 1, 0, 0, 0, 0, 0, 0, 0},
502 { 1, 1, 0, 0, 0, 0, 0, 0},
503 { 1, 1, 1, 1, 0, 0, 0, 0},
504 { 0, 0, 1, 1, 1, 1, 1, 1}
505 },
506 {
507 { 1, 0, 0, 0, 0, 0, 0, 0},
508 { 1, 1, 1, 1, 0, 0, 0, 0},
509 { 1, 1, 0, 0, 0, 0, 0, 0},
510 { 0, 0, 1, 1, 1, 1, 1, 1}
511 },
512 };
513
514 static const BYTE triangle_duty[32] = {
515 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
516 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
517 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
518 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
519 };
520
521 static const WORD noise_timer[3][16] = {
522 {
523 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0060, 0x0080, 0x00A0,
524 0x00CA, 0x00FE, 0x017C, 0x01FC, 0x02FA, 0x03F8, 0x07F2, 0x0FE4
525 },
526 {
527 0x0004, 0x0007, 0x000E, 0x001E, 0x003C, 0x0058, 0x0076, 0x0094,
528 0x00BC, 0x00EC, 0x0162, 0x01D8, 0x02C4, 0x03B0, 0x0762, 0x0EC2
529 },
530 {
531 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0060, 0x0080, 0x00A0,
532 0x00CA, 0x00FE, 0x017C, 0x01FC, 0x02FA, 0x03F8, 0x07F2, 0x0FE4
533 }
534 };
535
536 static const WORD dmc_rate[3][16] = {
537 {
538 0x01AC, 0x017C, 0x0154, 0x0140, 0x011E, 0x00FE, 0x00E2, 0x00D6,
539 0x00BE, 0x00A0, 0x008E, 0x0080, 0x006A, 0x0054, 0x0048, 0x0036
540 },
541 {
542 0x018E, 0x0162, 0x013C, 0x012A, 0x0114, 0x00EC, 0x00D2, 0x00C6,
543 0x00B0, 0x0094, 0x0084, 0x0076, 0x0062, 0x004E, 0x0042, 0x0032
544 },
545 {
546 0x01AC, 0x017C, 0x0154, 0x0140, 0x011E, 0x00FE, 0x00E2, 0x00D6,
547 0x00BE, 0x00A0, 0x008E, 0x0080, 0x006A, 0x0054, 0x0048, 0x0036
548 }
549 };
550
551 void apu_tick(struct NESAPU* a, int len);
552 void apu_turn_on(struct NESAPU* a, BYTE apu_type);
553
554 #endif /* APU_H_ */