Debian Codegraph @

src/engine/platform/sound/nes_nsfplay/nes_dmc.cpp

raw
1 #include "nes_dmc.h"
2 #include "nes_apu.h"
3 #include "common.h"
4 #include <assert.h>
5 #include <cstdlib>
6
7 namespace xgm
8 {
9 const unsigned int NES_DMC::wavlen_table[2][16] = {
10 { // NTSC
11 4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068
12 },
13 { // PAL
14 4, 8, 14, 30, 60, 88, 118, 148, 188, 236, 354, 472, 708, 944, 1890, 3778
15 }};
16
17 const unsigned int NES_DMC::freq_table[2][16] = {
18 { // NTSC
19 428, 380, 340, 320, 286, 254, 226, 214, 190, 160, 142, 128, 106, 84, 72, 54
20 },
21 { // PAL
22 398, 354, 316, 298, 276, 236, 210, 198, 176, 148, 132, 118, 98, 78, 66, 50
23 }};
24
25 const unsigned int BITREVERSE[256] = {
26 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
27 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
28 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
29 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
30 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
31 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
32 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
33 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
34 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
35 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
36 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
37 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
38 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
39 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
40 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
41 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF,
42 };
43
44 NES_DMC::NES_DMC () : GETA_BITS (20)
45 {
46 SetClock (DEFAULT_CLOCK);
47 SetRate (DEFAULT_RATE);
48 SetPal (false);
49 option[OPT_ENABLE_4011] = 1;
50 option[OPT_ENABLE_PNOISE] = 1;
51 option[OPT_UNMUTE_ON_RESET] = 1;
52 option[OPT_DPCM_ANTI_CLICK] = 0;
53 option[OPT_NONLINEAR_MIXER] = 1;
54 option[OPT_RANDOMIZE_NOISE] = 1;
55 option[OPT_RANDOMIZE_TRI] = 1;
56 option[OPT_TRI_MUTE] = 1;
57 option[OPT_DPCM_REVERSE] = 0;
58 tnd_table[0][0][0][0] = 0;
59 tnd_table[1][0][0][0] = 0;
60
61 apu = NULL;
62 frame_sequence_count = 0;
63 frame_sequence_length = 7458;
64 frame_sequence_steps = 4;
65
66 for(int c=0;c<2;++c)
67 for(int t=0;t<3;++t)
68 sm[c][t] = 128;
69 }
70
71
72 NES_DMC::~NES_DMC ()
73 {
74 }
75
76 void NES_DMC::SetStereoMix(int trk, short mixl, short mixr)
77 {
78 if (trk < 0) return;
79 if (trk > 2) return;
80 sm[0][trk] = mixl;
81 sm[1][trk] = mixr;
82 }
83
84 void NES_DMC::FrameSequence(int s)
85 {
86 //DEBUG_OUT("FrameSequence: %d¥n",s);
87
88 if (s > 3) return; // no operation in step 4
89
90 if (apu)
91 {
92 apu->FrameSequence(s);
93 }
94
95 if (s == 0 && (frame_sequence_steps == 4))
96 {
97 if (frame_irq_enable) frame_irq = true;
98 }
99
100 // 240hz clock
101 {
102 // triangle linear counter
103 if (linear_counter_halt)
104 {
105 linear_counter = linear_counter_reload;
106 }
107 else
108 {
109 if (linear_counter > 0) --linear_counter;
110 }
111 if (!linear_counter_control)
112 {
113 linear_counter_halt = false;
114 }
115
116 // noise envelope
117 bool divider = false;
118 if (envelope_write)
119 {
120 envelope_write = false;
121 envelope_counter = 15;
122 envelope_div = 0;
123 }
124 else
125 {
126 ++envelope_div;
127 if (envelope_div > envelope_div_period)
128 {
129 divider = true;
130 envelope_div = 0;
131 }
132 }
133 if (divider)
134 {
135 if (envelope_loop && envelope_counter == 0)
136 envelope_counter = 15;
137 else if (envelope_counter > 0)
138 --envelope_counter;
139 }
140 }
141
142 // 120hz clock
143 if ((s&1) == 0)
144 {
145 // triangle length counter
146 if (!linear_counter_control && (length_counter[0] > 0))
147 --length_counter[0];
148
149 // noise length counter
150 if (!envelope_loop && (length_counter[1] > 0))
151 --length_counter[1];
152 }
153
154 }
155
156 // 三角波チャンネルの計算 戻り値は0-15
157 unsigned int NES_DMC::calc_tri (unsigned int clocks)
158 {
159 static unsigned int tritbl[32] =
160 {
161 15,14,13,12,11,10, 9, 8,
162 7, 6, 5, 4, 3, 2, 1, 0,
163 0, 1, 2, 3, 4, 5, 6, 7,
164 8, 9,10,11,12,13,14,15,
165 };
166
167 if (linear_counter > 0 && length_counter[0] > 0
168 && (!option[OPT_TRI_MUTE] || tri_freq > 0))
169 {
170 counter[0] -= clocks;
171 while (counter[0] < 0)
172 {
173 tphase = (tphase + 1) & 31;
174 counter[0] += (tri_freq + 1);
175 }
176 }
177
178 unsigned int ret = tritbl[tphase];
179 return ret;
180 }
181
182 // ノイズチャンネルの計算 戻り値は0-127
183 // 低サンプリングレートで合成するとエイリアスノイズが激しいので
184 // ノイズだけはこの関数内で高クロック合成し、簡易なサンプリングレート
185 // 変換を行っている。
186 unsigned int NES_DMC::calc_noise(unsigned int clocks)
187 {
188 unsigned int env = envelope_disable ? noise_volume : envelope_counter;
189 if (length_counter[1] < 1) env = 0;
190
191 unsigned int last = (noise & 0x4000) ? 0 : env;
192 if (clocks < 1) return last;
193
194 // simple anti-aliasing (noise requires it, even when oversampling is off)
195 unsigned int count = 0;
196 unsigned int accum = counter[1] * last; // samples pending from previous calc
197 unsigned int accum_clocks = counter[1];
198 #ifdef _DEBUG
199 int start_clocks = counter[1];
200 #endif
201 if (counter[1] < 0) // only happens on startup when using the randomize noise option
202 {
203 accum = 0;
204 accum_clocks = 0;
205 }
206
207 counter[1] -= clocks;
208 assert (nfreq > 0); // prevent infinite loop
209 while (counter[1] < 0)
210 {
211 // tick the noise generator
212 unsigned int feedback = (noise&1) ^ ((noise&noise_tap)?1:0);
213 noise = (noise>>1) | (feedback<<14);
214
215 last = (noise & 0x4000) ? 0 : env;
216 accum += (last * nfreq);
217 counter[1] += nfreq;
218 ++count;
219 accum_clocks += nfreq;
220 }
221
222 if (count < 1) // no change over interval, don't anti-alias
223 {
224 return last;
225 }
226
227 accum -= (last * counter[1]); // remove these samples which belong in the next calc
228 accum_clocks -= counter[1];
229 #ifdef _DEBUG
230 if (start_clocks >= 0) assert(accum_clocks == clocks); // these should be equal
231 #endif
232
233 unsigned int average = accum / accum_clocks;
234 assert(average <= 15); // above this would indicate overflow
235 return average;
236 }
237
238 // Tick the DMC for the number of clocks, and return output counter;
239 unsigned int NES_DMC::calc_dmc (unsigned int clocks)
240 {
241 counter[2] -= clocks;
242 assert (dfreq > 0); // prevent infinite loop
243 while (counter[2] < 0)
244 {
245 counter[2] += dfreq;
246
247 if ( data > 0x100 ) // data = 0x100 when shift register is empty
248 {
249 if (!empty)
250 {
251 if ((data & 1) && (damp < 63))
252 damp++;
253 else if (!(data & 1) && (0 < damp))
254 damp--;
255 }
256 data >>=1;
257 }
258
259 if ( data <= 0x100 ) // shift register is empty
260 {
261 if (dlength > 0)
262 {
263 memory (daddress, data);
264 // (checking for the 3-cycle case would require sub-instruction emulation)
265 data &= 0xFF; // read 8 bits
266 if (option[OPT_DPCM_REVERSE]) data = BITREVERSE[data];
267 data |= 0x10000; // use an extra bit to signal end of data
268 empty = false;
269 daddress = ((daddress+1)&0xFFFF)|0x8000 ;
270 --dlength;
271 if (dlength == 0)
272 {
273 if (mode & 1) // looped DPCM = auto-reload
274 {
275 daddress = ((adr_reg<<6)|0xC000);
276 dlength = (len_reg<<4)+1;
277 }
278 else if (mode & 2) // IRQ and not looped
279 {
280 irq = true;
281 }
282 }
283 }
284 else
285 {
286 data = 0x10000; // DMC will do nothing
287 empty = true;
288 }
289 }
290 }
291
292 return (damp<<1) + dac_lsb;
293 }
294
295 void NES_DMC::TickFrameSequence (unsigned int clocks)
296 {
297 frame_sequence_count += clocks;
298 while (frame_sequence_count > frame_sequence_length)
299 {
300 FrameSequence(frame_sequence_step);
301 frame_sequence_count -= frame_sequence_length;
302 ++frame_sequence_step;
303 if(frame_sequence_step >= frame_sequence_steps)
304 frame_sequence_step = 0;
305 }
306 }
307
308 void NES_DMC::Tick (unsigned int clocks)
309 {
310 out[0] = calc_tri(clocks);
311 out[1] = calc_noise(clocks);
312 out[2] = calc_dmc(clocks);
313 }
314
315 unsigned int NES_DMC::Render (int b[2])
316 {
317 out[0] = (mask & 1) ? 0 : out[0];
318 out[1] = (mask & 2) ? 0 : out[1];
319 out[2] = (mask & 4) ? 0 : out[2];
320
321 int m[3];
322 m[0] = tnd_table[0][out[0]][0][0];
323 m[1] = tnd_table[0][0][out[1]][0];
324 m[2] = tnd_table[0][0][0][out[2]];
325
326 if (option[OPT_NONLINEAR_MIXER])
327 {
328 int ref = m[0] + m[1] + m[2];
329 int voltage = tnd_table[1][out[0]][out[1]][out[2]];
330 if (ref)
331 {
332 for (int i=0; i < 3; ++i)
333 m[i] = (m[i] * voltage) / ref;
334 }
335 else
336 {
337 for (int i=0; i < 3; ++i)
338 m[i] = voltage;
339 }
340 }
341
342 // anti-click nullifies any 4011 write but preserves nonlinearity
343 if (option[OPT_DPCM_ANTI_CLICK])
344 {
345 if (dmc_pop) // $4011 will cause pop this frame
346 {
347 // adjust offset to counteract pop
348 dmc_pop_offset += dmc_pop_follow - m[2];
349 dmc_pop = false;
350
351 // prevent overflow, keep headspace at edges
352 const int OFFSET_MAX = (1 << 30) - (4 << 16);
353 if (dmc_pop_offset > OFFSET_MAX) dmc_pop_offset = OFFSET_MAX;
354 if (dmc_pop_offset < -OFFSET_MAX) dmc_pop_offset = -OFFSET_MAX;
355 }
356 dmc_pop_follow = m[2]; // remember previous position
357
358 m[2] += dmc_pop_offset; // apply offset
359
360 // TODO implement this in a better way
361 // roll off offset (not ideal, but prevents overflow)
362 if (dmc_pop_offset > 0) --dmc_pop_offset;
363 else if (dmc_pop_offset < 0) ++dmc_pop_offset;
364 }
365
366 b[0] = m[0] * sm[0][0];
367 b[0] += m[1] * sm[0][1];
368 b[0] += m[2] * sm[0][2];
369 b[0] >>= 7;
370
371 b[1] = m[0] * sm[1][0];
372 b[1] += m[1] * sm[1][1];
373 b[1] += m[2] * sm[1][2];
374 b[1] >>= 7;
375
376 return 2;
377 }
378
379 void NES_DMC::SetClock (double c)
380 {
381 clock = c;
382 }
383
384 void NES_DMC::SetRate (double r)
385 {
386 rate = (unsigned int)(r?r:DEFAULT_RATE);
387 }
388
389 void NES_DMC::SetPal (bool is_pal)
390 {
391 pal = (is_pal ? 1 : 0);
392 // set CPU cycles in frame_sequence
393 frame_sequence_length = is_pal ? 8314 : 7458;
394 }
395
396 void NES_DMC::SetAPU (NES_APU* apu_)
397 {
398 apu = apu_;
399 }
400
401 // Initializing TRI, NOISE, DPCM mixing table
402 void NES_DMC::InitializeTNDTable(double wt, double wn, double wd) {
403
404 // volume adjusted by 0.95 based on empirical measurements
405 const double MASTER = 8192.0 * 0.95;
406 // truthfully, the nonlinear curve does not appear to match well
407 // with my tests. Do more testing of the APU/DMC DAC later.
408 // this value keeps the triangle consistent with measured levels,
409 // but not necessarily the rest of this APU channel,
410 // because of the lack of a good DAC model, currently.
411
412 { // Linear Mixer
413 for(int t=0; t<16 ; t++) {
414 for(int n=0; n<16; n++) {
415 for(int d=0; d<128; d++) {
416 tnd_table[0][t][n][d] = (unsigned int)(MASTER*(3.0*t+2.0*n+d)/208.0);
417 }
418 }
419 }
420 }
421 { // Non-Linear Mixer
422 tnd_table[1][0][0][0] = 0;
423 for(int t=0; t<16 ; t++) {
424 for(int n=0; n<16; n++) {
425 for(int d=0; d<128; d++) {
426 if(t!=0||n!=0||d!=0)
427 tnd_table[1][t][n][d] = (unsigned int)((MASTER*159.79)/(100.0+1.0/((double)t/wt+(double)n/wn+(double)d/wd)));
428 }
429 }
430 }
431 }
432
433 }
434
435 void NES_DMC::Reset ()
436 {
437 int i;
438 mask = 0;
439
440 InitializeTNDTable(8227,12241,22638);
441
442 counter[0] = 0;
443 counter[1] = 0;
444 counter[2] = 0;
445 tphase = 0;
446 nfreq = wavlen_table[0][0];
447 dfreq = freq_table[0][0];
448 tri_freq = 0;
449 linear_counter = 0;
450 linear_counter_reload = 0;
451 linear_counter_halt = 0;
452 linear_counter_control = 0;
453 noise_volume = 0;
454 noise = 0;
455 noise_tap = 0;
456 envelope_loop = 0;
457 envelope_disable = 0;
458 envelope_write = 0;
459 envelope_div_period = 0;
460 envelope_div = 0;
461 envelope_counter = 0;
462 enable[0] = 0;
463 enable[1] = 0;
464 length_counter[0] = 0;
465 length_counter[1] = 0;
466 frame_irq = false;
467 frame_irq_enable = false;
468 frame_sequence_count = 0;
469 frame_sequence_steps = 4;
470 frame_sequence_step = 0;
471
472 for (i = 0; i < 0x0F; i++)
473 Write (0x4008 + i, 0);
474 Write (0x4017, 0x40);
475
476 irq = false;
477 Write (0x4015, 0x00);
478 if (option[OPT_UNMUTE_ON_RESET])
479 Write (0x4015, 0x0f);
480
481 out[0] = out[1] = out[2] = 0;
482 damp = 0;
483 dmc_pop = false;
484 dmc_pop_offset = 0;
485 dmc_pop_follow = 0;
486 dac_lsb = 0;
487 data = 0x100;
488 empty = true;
489 adr_reg = 0;
490 dlength = 0;
491 len_reg = 0;
492 daddress = 0;
493 noise = 1;
494 noise_tap = (1<<1);
495
496
497 SetRate(rate);
498 }
499
500 void NES_DMC::SetMemory (std::function<void(unsigned short, unsigned int&)> r)
501 {
502 memory = r;
503 }
504
505 void NES_DMC::SetOption (int id, int val)
506 {
507 if(id<OPT_END)
508 {
509 option[id] = val;
510 if(id==OPT_NONLINEAR_MIXER)
511 InitializeTNDTable(8227,12241,22638);
512 }
513 }
514
515 bool NES_DMC::Write (unsigned int adr, unsigned int val, unsigned int id)
516 {
517 static const unsigned char length_table[32] = {
518 0x0A, 0xFE,
519 0x14, 0x02,
520 0x28, 0x04,
521 0x50, 0x06,
522 0xA0, 0x08,
523 0x3C, 0x0A,
524 0x0E, 0x0C,
525 0x1A, 0x0E,
526 0x0C, 0x10,
527 0x18, 0x12,
528 0x30, 0x14,
529 0x60, 0x16,
530 0xC0, 0x18,
531 0x48, 0x1A,
532 0x10, 0x1C,
533 0x20, 0x1E
534 };
535
536 if (adr == 0x4015)
537 {
538 enable[0] = (val & 4) ? true : false;
539 enable[1] = (val & 8) ? true : false;
540
541 if (!enable[0])
542 {
543 length_counter[0] = 0;
544 }
545 if (!enable[1])
546 {
547 length_counter[1] = 0;
548 }
549
550 if ((val & 16) && dlength == 0)
551 {
552 daddress = (0xC000 | (adr_reg << 6));
553 dlength = (len_reg << 4) + 1;
554 }
555 else if (!(val & 16))
556 {
557 dlength = 0;
558 }
559
560 irq = false;
561
562 reg[adr-0x4008] = val;
563 return true;
564 }
565
566 if (adr == 0x4017)
567 {
568 //DEBUG_OUT("4017 = %02X¥n", val);
569 frame_irq_enable = ((val & 0x40) != 0x40);
570 if (frame_irq_enable) frame_irq = false;
571
572 frame_sequence_count = 0;
573 if (val & 0x80)
574 {
575 frame_sequence_steps = 5;
576 frame_sequence_step = 0;
577 FrameSequence(frame_sequence_step);
578 ++frame_sequence_step;
579 }
580 else
581 {
582 frame_sequence_steps = 4;
583 frame_sequence_step = 1;
584 }
585 }
586
587 if (adr<0x4008||0x4013<adr)
588 return false;
589
590 reg[adr-0x4008] = val&0xff;
591
592 //DEBUG_OUT("$%04X %02X¥n", adr, val);
593
594 switch (adr)
595 {
596
597 // tri
598
599 case 0x4008:
600 linear_counter_control = (val >> 7) & 1;
601 linear_counter_reload = val & 0x7F;
602 break;
603
604 case 0x4009:
605 break;
606
607 case 0x400a:
608 tri_freq = val | (tri_freq & 0x700) ;
609 break;
610
611 case 0x400b:
612 tri_freq = (tri_freq & 0xff) | ((val & 0x7) << 8) ;
613 linear_counter_halt = true;
614 if (enable[0])
615 {
616 length_counter[0] = length_table[(val >> 3) & 0x1f];
617 }
618 break;
619
620 // noise
621
622 case 0x400c:
623 noise_volume = val & 15;
624 envelope_div_period = val & 15;
625 envelope_disable = (val >> 4) & 1;
626 envelope_loop = (val >> 5) & 1;
627 break;
628
629 case 0x400d:
630 break;
631
632 case 0x400e:
633 if (option[OPT_ENABLE_PNOISE])
634 noise_tap = (val & 0x80) ? (1<<6) : (1<<1);
635 else
636 noise_tap = (1<<1);
637 nfreq = wavlen_table[pal][val&15];
638 break;
639
640 case 0x400f:
641 if (enable[1])
642 {
643 length_counter[1] = length_table[(val >> 3) & 0x1f];
644 }
645 envelope_write = true;
646 break;
647
648 // dmc
649
650 case 0x4010:
651 mode = (val >> 6) & 3;
652 if (!(mode & 2))
653 {
654 irq = false;
655 }
656 dfreq = freq_table[pal][val&15];
657 break;
658
659 case 0x4011:
660 if (option[OPT_ENABLE_4011])
661 {
662 damp = (val >> 1) & 0x3f;
663 dac_lsb = val & 1;
664 dmc_pop = true;
665 }
666 break;
667
668 case 0x4012:
669 adr_reg = val&0xff;
670 // ここでdaddressは更新されない
671 break;
672
673 case 0x4013:
674 len_reg = val&0xff;
675 // ここでlengthは更新されない
676 break;
677
678 default:
679 return false;
680 }
681
682 return true;
683 }
684
685 bool NES_DMC::Read (unsigned int adr, unsigned int & val, unsigned int id)
686 {
687 if (adr == 0x4015)
688 {
689 val |=(irq ? 0x80 : 0)
690 | (frame_irq ? 0x40 : 0)
691 | ((dlength>0) ? 0x10 : 0)
692 | (length_counter[1] ? 0x08 : 0)
693 | (length_counter[0] ? 0x04 : 0)
694 ;
695
696 frame_irq = false;
697 return true;
698 }
699 else if (0x4008<=adr&&adr<=0x4014)
700 {
701 val |= reg[adr-0x4008];
702 return true;
703 }
704 else
705 return false;
706 }
707 } // namespace