Debian Codegraph @

mednafen/pce_fast/psg.cpp

raw symbols SCIP
1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "../mednafen.h"
19 #include "psg.h"
20
21 #include <math.h>
22 #include <string.h>
23
24 void PCEFast_PSG::SetVolume(double new_volume)
25 {
26 OutputVolume = new_volume;
27
28 Synth.volume(OutputVolume / 6);
29 }
30
31 void PCEFast_PSG::UpdateOutput_Norm(const int32 timestamp, psg_channel *ch)
32 {
33 int32 samp[2];
34 int sv = ch->dda;
35
36 samp[0] = dbtable[ch->vl[0]][sv];
37 samp[1] = dbtable[ch->vl[1]][sv];
38
39 Synth.offset_inline(timestamp, samp[0] - ch->blip_prev_samp[0], sbuf[0]);
40 Synth.offset_inline(timestamp, samp[1] - ch->blip_prev_samp[1], sbuf[1]);
41
42 ch->blip_prev_samp[0] = samp[0];
43 ch->blip_prev_samp[1] = samp[1];
44 }
45
46 void PCEFast_PSG::UpdateOutput_Noise(const int32 timestamp, psg_channel *ch)
47 {
48 int32 samp[2];
49 int sv = ((ch->lfsr & 1) << 5) - (ch->lfsr & 1); //(ch->lfsr & 0x1) ? 0x1F : 0;
50
51 samp[0] = dbtable[ch->vl[0]][sv];
52 samp[1] = dbtable[ch->vl[1]][sv];
53
54 Synth.offset_inline(timestamp, samp[0] - ch->blip_prev_samp[0], sbuf[0]);
55 Synth.offset_inline(timestamp, samp[1] - ch->blip_prev_samp[1], sbuf[1]);
56
57 ch->blip_prev_samp[0] = samp[0];
58 ch->blip_prev_samp[1] = samp[1];
59 }
60
61 void PCEFast_PSG::UpdateOutput_Off(const int32 timestamp, psg_channel *ch)
62 {
63 int32 samp[2];
64
65 samp[0] = samp[1] = 0;
66
67 Synth.offset_inline(timestamp, samp[0] - ch->blip_prev_samp[0], sbuf[0]);
68 Synth.offset_inline(timestamp, samp[1] - ch->blip_prev_samp[1], sbuf[1]);
69
70 ch->blip_prev_samp[0] = samp[0];
71 ch->blip_prev_samp[1] = samp[1];
72 }
73
74
75 void PCEFast_PSG::UpdateOutput_Accum(const int32 timestamp, psg_channel *ch)
76 {
77 int32 samp[2];
78
79 samp[0] = ((int32)dbtable_volonly[ch->vl[0]] * ((int32)ch->samp_accum - 496)) >> (8 + 5);
80 samp[1] = ((int32)dbtable_volonly[ch->vl[1]] * ((int32)ch->samp_accum - 496)) >> (8 + 5);
81
82 Synth.offset_inline(timestamp, samp[0] - ch->blip_prev_samp[0], sbuf[0]);
83 Synth.offset_inline(timestamp, samp[1] - ch->blip_prev_samp[1], sbuf[1]);
84
85 ch->blip_prev_samp[0] = samp[0];
86 ch->blip_prev_samp[1] = samp[1];
87 }
88
89 // This function should always be called after RecalcFreqCache() (it's not called from RecalcFreqCache to avoid redundant code)
90 void PCEFast_PSG::RecalcUOFunc(int chnum)
91 {
92 psg_channel *ch = &channel[chnum];
93
94 //printf("UO Update: %d, %02x\n", chnum, ch->control);
95
96 if(!(ch->control & 0xC0))
97 ch->UpdateOutput = &PCEFast_PSG::UpdateOutput_Off;
98 else if(ch->noisectrl & ch->control & 0x80)
99 ch->UpdateOutput = &PCEFast_PSG::UpdateOutput_Noise;
100 // If the control for the channel is in waveform play mode, and the (real) playback frequency is too high, and the channel is either not the LFO modulator channel or
101 // if the LFO trigger bit(which halts the LFO modulator channel's waveform incrementing when set) is clear
102 else if((ch->control & 0xC0) == 0x80 && ch->freq_cache <= 0xA && (chnum != 1 || !(lfoctrl & 0x80)) )
103 ch->UpdateOutput = &PCEFast_PSG::UpdateOutput_Accum;
104 else
105 ch->UpdateOutput = &PCEFast_PSG::UpdateOutput_Norm;
106 }
107
108
109 void PCEFast_PSG::RecalcFreqCache(int chnum)
110 {
111 psg_channel *ch = &channel[chnum];
112
113 if(chnum == 0 && (lfoctrl & 0x03))
114 {
115 const uint32 shift = (((lfoctrl & 0x3) - 1) << 1);
116 uint8 la = channel[1].dda;
117 int32 tmp_freq = ((int32)ch->frequency + ((la - 0x10) << shift)) & 0xFFF;
118
119 ch->freq_cache = (tmp_freq ? tmp_freq : 4096) << 1;
120 }
121 else
122 {
123 ch->freq_cache = (ch->frequency ? ch->frequency : 4096) << 1;
124
125 if(chnum == 1 && (lfoctrl & 0x03))
126 ch->freq_cache *= lfofreq ? lfofreq : 256;
127 }
128 }
129
130 void PCEFast_PSG::RecalcNoiseFreqCache(int chnum)
131 {
132 psg_channel *ch = &channel[chnum];
133 int32 freq = 0x1F - (ch->noisectrl & 0x1F);
134
135 if(!freq)
136 freq = 0x20;
137 else
138 freq <<= 6;
139
140 freq <<= 1;
141
142 ch->noise_freq_cache = freq;
143 }
144
145 PCEFast_PSG::PCEFast_PSG(Blip_Buffer *bb_l, Blip_Buffer *bb_r)
146 {
147 //printf("Test: %u, %u\n", sizeof(psg_channel), (uint8*)&channel[0].balance - (uint8*)&channel[0].waveform[0]);
148 Synth.treble_eq(-2.0);
149
150 sbuf[0] = bb_l;
151 sbuf[1] = bb_r;
152
153 lastts = 0;
154 for(int ch = 0; ch < 6; ch++)
155 {
156 channel[ch].blip_prev_samp[0] = 0;
157 channel[ch].blip_prev_samp[1] = 0;
158 channel[ch].lastts = 0;
159 }
160
161 SetVolume(1.0);
162
163 for(int vl = 0; vl < 32; vl++)
164 {
165 double flub = 1;
166
167 if(vl)
168 flub /= powf(2, (double)1 / 4 * vl); // ~1.5dB reduction per increment of vl
169
170 if(vl == 0x1F)
171 flub = 0;
172
173 for(int samp = 0; samp < 32; samp++)
174 {
175 int eff_samp = samp * 2 - 0x1F;
176
177 dbtable[vl][samp] = (int32)(flub * eff_samp * 128);
178 dbtable_volonly[vl] = (int32)(flub * 65536);
179 }
180 }
181
182 Power(0);
183 }
184
185 PCEFast_PSG::~PCEFast_PSG()
186 {
187
188
189 }
190
191 int32 PCEFast_PSG::GetVL(const int chnum, const int lr)
192 {
193 // Note: Changing the 0x1F(not that there should be) would require changing the channel pseudo-off volume check logic later on.
194 static const uint8 scale_tab[] =
195 {
196 0x00, 0x03, 0x05, 0x07, 0x09, 0x0B, 0x0D, 0x0F,
197 0x10, 0x13, 0x15, 0x17, 0x19, 0x1B, 0x1D, 0x1F
198 };
199
200 psg_channel *ch = &channel[chnum];
201
202 const int gbal = 0x1F - scale_tab[(globalbalance >> (lr ? 0 : 4)) & 0xF];
203 const int bal = 0x1F - scale_tab[(ch->balance >> (lr ? 0 : 4)) & 0xF];
204 const int al = 0x1F - (ch->control & 0x1F);
205 int vol_reduction;
206
207 vol_reduction = gbal + bal + al;
208
209 if(vol_reduction > 0x1F)
210 vol_reduction = 0x1F;
211
212 return(vol_reduction);
213 }
214
215 void PCEFast_PSG::Write(int32 timestamp, uint8 A, uint8 V)
216 {
217 A &= 0x0F;
218
219 if(A == 0x00)
220 {
221 select = (V & 0x07);
222 return;
223 }
224
225 Update(timestamp);
226
227 psg_channel *ch = &channel[select];
228
229 //if(A == 0x01 || select == 5)
230 // printf("Write Ch: %d %04x %02x, %d\n", select, A, V, timestamp);
231
232 switch(A)
233 {
234 default: break;
235
236 case 0x01: /* Global sound balance */
237 globalbalance = V;
238 vol_pending = true;
239 break;
240
241 case 0x02: /* Channel frequency (LSB) */
242 if(select > 5) return; // no more than 6 channels, silly game.
243
244 ch->frequency = (ch->frequency & 0x0F00) | V;
245 RecalcFreqCache(select);
246 RecalcUOFunc(select);
247 break;
248
249 case 0x03: /* Channel frequency (MSB) */
250 if(select > 5) return; // no more than 6 channels, silly game.
251
252 ch->frequency = (ch->frequency & 0x00FF) | ((V & 0x0F) << 8);
253 RecalcFreqCache(select);
254 RecalcUOFunc(select);
255 break;
256
257 case 0x04: /* Channel enable, DDA, volume */
258 if(select > 5) return; // no more than 6 channels, silly game.
259
260 if((ch->control & 0x40) && !(V & 0x40))
261 {
262 ch->waveform_index = 0;
263 ch->dda = ch->waveform[ch->waveform_index];
264 ch->counter = ch->freq_cache;
265 }
266
267 if(!(ch->control & 0x80) && (V & 0x80))
268 {
269 if(!(V & 0x40))
270 {
271 ch->waveform_index = (ch->waveform_index + 1) & 0x1F;
272 ch->dda = ch->waveform[ch->waveform_index];
273 }
274 }
275
276 ch->control = V;
277 RecalcFreqCache(select);
278 RecalcUOFunc(select);
279
280 vol_pending = true;
281 break;
282
283 case 0x05: /* Channel balance */
284 if(select > 5) return; // no more than 6 channels, silly game.
285 ch->balance = V;
286
287 vol_pending = true;
288 break;
289
290 case 0x06: /* Channel waveform data */
291 if(select > 5) return; // no more than 6 channels, silly game.
292 V &= 0x1F;
293
294 if(!(ch->control & 0x40))
295 {
296 ch->samp_accum -= ch->waveform[ch->waveform_index];
297 ch->waveform[ch->waveform_index] = V;
298 ch->samp_accum += ch->waveform[ch->waveform_index];
299 }
300
301 if((ch->control & 0xC0) == 0x00)
302 ch->waveform_index = ((ch->waveform_index + 1) & 0x1F);
303
304 if(ch->control & 0x80)
305 {
306 // According to my tests(on SuperGrafx), writing to this channel
307 // will update the waveform value cache/latch regardless of DDA mode being enabled.
308 ch->dda = V;
309 }
310 break;
311
312 case 0x07: /* Noise enable and frequency */
313 if(select > 5) return; // no more than 6 channels, silly game.
314 if(select >= 4)
315 {
316 ch->noisectrl = V;
317 RecalcNoiseFreqCache(select);
318 RecalcUOFunc(select);
319 }
320 break;
321
322 case 0x08: /* LFO frequency */
323 lfofreq = V & 0xFF;
324 //printf("LFO Freq: %02x\n", V);
325 break;
326
327 case 0x09: /* LFO trigger and control */
328 //printf("LFO Ctrl: %02x\n", V);
329 if(V & 0x80)
330 {
331 channel[1].waveform_index = 0;
332 channel[1].dda = channel[1].waveform[channel[1].waveform_index];
333 channel[1].counter = channel[1].freq_cache;
334 }
335 lfoctrl = V;
336 RecalcFreqCache(0);
337 RecalcUOFunc(0);
338 RecalcFreqCache(1);
339 RecalcUOFunc(1);
340 break;
341 }
342 }
343
344 // Don't use INLINE, which has always_inline in it, due to gcc's inability to cope with the type of recursion
345 // used in this function.
346 template<bool LFO_On>
347 void PCEFast_PSG::RunChannel(int chc, int32 timestamp)
348 {
349 psg_channel *ch = &channel[chc];
350 int32 running_timestamp = ch->lastts;
351 int32 run_time = timestamp - ch->lastts;
352
353 ch->lastts = timestamp;
354
355 if(!run_time)
356 return;
357
358 (this->*ch->UpdateOutput)(running_timestamp, ch);
359
360 if(chc >= 4)
361 {
362 int32 freq = ch->noise_freq_cache;
363
364 ch->noisecount -= run_time;
365
366 #define CLOCK_LFSR(lfsr) { unsigned int newbit = ((lfsr >> 0) ^ (lfsr >> 1) ^ (lfsr >> 11) ^ (lfsr >> 12) ^ (lfsr >> 17)) & 1; lfsr = (lfsr >> 1) | (newbit << 17); }
367 if(&PCEFast_PSG::UpdateOutput_Noise == ch->UpdateOutput)
368 {
369 while(ch->noisecount <= 0)
370 {
371 CLOCK_LFSR(ch->lfsr);
372 UpdateOutput_Noise(timestamp + ch->noisecount, ch);
373 ch->noisecount += freq;
374 }
375 }
376 else
377 {
378 while(ch->noisecount <= 0)
379 {
380 CLOCK_LFSR(ch->lfsr);
381 ch->noisecount += freq;
382 }
383 }
384 #undef CLOCK_LFSR
385 }
386
387 // D7 of control is 0, don't clock the counter at all.
388 // D7 of lfocontrol is 1(and chc == 1), don't clock the counter at all(not sure about this)
389 // In DDA mode, don't clock the counter.
390 // (Noise being enabled isn't handled here since AFAIK it doesn't disable clocking of the waveform portion, its sound just overrides the sound from
391 // the waveform portion when the noise enable bit is set, which is handled in our RecalcUOFunc).
392 if(!(ch->control & 0x80) || (chc == 1 && (lfoctrl & 0x80)) || (ch->control & 0x40))
393 return;
394
395 ch->counter -= run_time;
396
397 if(!LFO_On && ch->freq_cache <= 0xA)
398 {
399 if(ch->counter <= 0)
400 {
401 const int32 inc_count = ((0 - ch->counter) / ch->freq_cache) + 1;
402
403 ch->counter += inc_count * ch->freq_cache;
404
405 ch->waveform_index = (ch->waveform_index + inc_count) & 0x1F;
406 ch->dda = ch->waveform[ch->waveform_index];
407 }
408 }
409
410 while(ch->counter <= 0)
411 {
412 ch->waveform_index = (ch->waveform_index + 1) & 0x1F;
413 ch->dda = ch->waveform[ch->waveform_index];
414
415 (this->*ch->UpdateOutput)(timestamp + ch->counter, ch);
416
417 if(LFO_On)
418 {
419 RunChannel<false>(1, timestamp + ch->counter);
420 RecalcFreqCache(0);
421 RecalcUOFunc(0);
422
423 ch->counter += (ch->freq_cache <= 0xA) ? 0xA : ch->freq_cache; // Not particularly accurate, but faster.
424 }
425 else
426 ch->counter += ch->freq_cache;
427 }
428 }
429
430 INLINE void PCEFast_PSG::UpdateSubLFO(int32 timestamp)
431 {
432 RunChannel<true>(0, timestamp);
433
434 for(int chc = 1; chc < 6; chc++)
435 RunChannel<false>(chc, timestamp);
436 }
437
438 INLINE void PCEFast_PSG::UpdateSubNonLFO(int32 timestamp)
439 {
440 for(int chc = 0; chc < 6; chc++)
441 RunChannel<false>(chc, timestamp);
442 }
443
444 //static int32 last_read;
445 //static int32 last_apply;
446
447 void PCEFast_PSG::Update(int32 timestamp)
448 {
449 int32 run_time = timestamp - lastts;
450
451 if(vol_pending && !vol_update_counter && !vol_update_which)
452 {
453 vol_update_counter = 1;
454 vol_pending = false;
455 }
456
457 bool lfo_on = (bool)(lfoctrl & 0x03);
458
459 if(lfo_on)
460 {
461 if(!(channel[1].control & 0x80) || (lfoctrl & 0x80))
462 {
463 lfo_on = 0;
464 RecalcFreqCache(0);
465 RecalcUOFunc(0);
466 }
467 }
468
469 int32 clocks = run_time;
470 int32 running_timestamp = lastts;
471
472 while(clocks > 0)
473 {
474 int32 chunk_clocks = clocks;
475
476 if(vol_update_counter > 0 && chunk_clocks > vol_update_counter)
477 chunk_clocks = vol_update_counter;
478
479 running_timestamp += chunk_clocks;
480 clocks -= chunk_clocks;
481
482 if(lfo_on)
483 UpdateSubLFO(running_timestamp);
484 else
485 UpdateSubNonLFO(running_timestamp);
486
487 if(vol_update_counter > 0)
488 {
489 vol_update_counter -= chunk_clocks;
490 if(!vol_update_counter)
491 {
492 const int phase = vol_update_which & 1;
493 const int lr = ((vol_update_which >> 1) & 1) ^ 1;
494 const int chnum = vol_update_which >> 2;
495
496 if(!phase)
497 {
498 //printf("Volume update(Read, %d since last): ch=%d, lr=%d, ts=%d\n", running_timestamp - last_read, chnum, lr, running_timestamp);
499
500 if(chnum < 6)
501 {
502 vol_update_vllatch = GetVL(chnum, lr);
503 }
504 //last_read = running_timestamp;
505 }
506 else
507 {
508 // printf("Volume update(Apply): ch=%d, lr=%d, ts=%d\n", chnum, lr, running_timestamp);
509 if(chnum < 6)
510 {
511 channel[chnum].vl[lr] = vol_update_vllatch;
512 }
513 //last_apply = running_timestamp;
514 }
515 vol_update_which = (vol_update_which + 1) & 0x1F;
516
517 if(vol_update_which)
518 vol_update_counter = phase ? 1 : 255;
519 else if(vol_pending)
520 {
521 vol_update_counter = phase ? 1 : 255;
522 vol_pending = false;
523 }
524 }
525 }
526
527 lastts = running_timestamp;
528 }
529 }
530
531 void PCEFast_PSG::EndFrame(int32 timestamp)
532 {
533 Update(timestamp);
534 lastts = 0;
535 for(int chc = 0; chc < 6; chc++)
536 channel[chc].lastts = 0;
537 }
538
539 void PCEFast_PSG::Power(const int32 timestamp)
540 {
541 // Not sure about power-on values, these are mostly just intuitive guesses(with some laziness thrown in).
542 if(timestamp != lastts)
543 Update(timestamp);
544
545 memset(&channel, 0, sizeof(channel));
546
547 select = 0;
548 globalbalance = 0;
549 lfofreq = 0;
550 lfoctrl = 0;
551
552 for(int ch = 0; ch < 6; ch++)
553 {
554 channel[ch].frequency = 0;
555 channel[ch].control = 0x00;
556 channel[ch].balance = 0;
557 memset(channel[ch].waveform, 0, 32);
558 channel[ch].samp_accum = 0;
559
560 channel[ch].waveform_index = 0;
561 channel[ch].dda = 0x00;
562 channel[ch].noisectrl = 0x00;
563
564 channel[ch].vl[0] = 0x1F;
565 channel[ch].vl[1] = 0x1F;
566
567 channel[ch].samp_accum = 0;
568
569 RecalcFreqCache(ch);
570 RecalcUOFunc(ch);
571
572 channel[ch].counter = channel[ch].freq_cache;
573
574 if(ch >= 4)
575 {
576 RecalcNoiseFreqCache(ch);
577 channel[ch].noisecount = 1;
578 channel[ch].lfsr = 1;
579 }
580 }
581
582 vol_pending = false;
583 vol_update_counter = 0;
584 vol_update_which = 0;
585 }
586
587 int PCEFast_PSG::StateAction(StateMem *sm, int load, int data_only)
588 {
589 int ret = 1;
590
591 for(int ch = 0; ch < 6; ch++)
592 {
593 char tmpstr[5] = "SCHx";
594 psg_channel *pt = &channel[ch];
595
596 SFORMAT CH_StateRegs[] =
597 {
598 SFVARN(pt->counter, "counter"),
599 SFVARN(pt->frequency, "frequency"),
600 SFVARN(pt->control, "control"),
601 SFVARN(pt->balance, "balance"),
602 SFARRAYN(pt->waveform, 32, "waveform"),
603 SFVARN(pt->waveform_index, "waveform_index"),
604 SFVARN(pt->dda, "dda"),
605 SFVARN(pt->noisectrl, "noisectrl"),
606 SFVARN(pt->noisecount, "noisecount"),
607 SFVARN(pt->lfsr, "lfsr"),
608 SFARRAY32N(pt->vl, 2, "vl"), // TODO
609 SFEND
610 };
611 tmpstr[3] = '0' + ch;
612 ret &= MDFNSS_StateAction(sm, load, data_only, CH_StateRegs, tmpstr);
613 }
614
615 SFORMAT PSG_StateRegs[] =
616 {
617 SFVAR(select),
618 SFVAR(globalbalance),
619 SFVAR(lfofreq),
620 SFVAR(lfoctrl),
621
622 SFVAR(vol_update_counter),
623 SFVAR(vol_update_which),
624 SFVAR(vol_pending),
625 SFEND
626 };
627
628 ret &= MDFNSS_StateAction(sm, load, data_only, PSG_StateRegs, "PSG");
629
630 if(load)
631 {
632 vol_update_which &= 0x1F;
633
634 if(!channel[4].lfsr)
635 channel[4].lfsr = 1;
636
637 if(!channel[5].lfsr)
638 channel[5].lfsr = 1;
639
640 for(int ch = 0; ch < 6; ch++)
641 {
642 channel[ch].samp_accum = 0;
643 for(int wi = 0; wi < 32; wi++)
644 {
645 channel[ch].waveform[wi] &= 0x1F;
646 channel[ch].samp_accum += channel[ch].waveform[wi];
647 }
648
649 for(int lr = 0; lr < 2; lr++)
650 channel[ch].vl[lr] &= 0x1F;
651
652 if(!channel[ch].noisecount && ch >= 4)
653 {
654 printf("ch=%d, noisecount == 0\n", ch);
655 channel[ch].noisecount = 1;
656 }
657
658 if(channel[ch].counter <= 0)
659 {
660 printf("ch=%d, counter <= 0\n", ch);
661 channel[ch].counter = 1;
662 }
663
664 if(ch >= 4)
665 RecalcNoiseFreqCache(ch);
666 RecalcFreqCache(ch);
667 RecalcUOFunc(ch);
668 }
669 }
670 return(ret);
671 }