Debian Codegraph @

src/engine/platform/sound/pce_psg.cpp

raw
1 /* Mednafen - Multi-system Emulator
2 *
3 * Original skeleton write handler and PSG structure definition:
4 * Copyright (C) 2001 Charles MacDonald
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 // additional modifications by tildearrow for furnace
22
23 #include "pce_psg.h"
24 #include <stdio.h>
25 #include <math.h>
26 #include <string.h>
27 #include <assert.h>
28
29 // Frequency cache cutoff optimization threshold (<= FREQC7M_COT)
30 #define FREQC7M_COT 0x7 //0xA
31
32 void PCE_PSG::SetVolume(double new_volume)
33 {
34 for(int vl = 0; vl < 32; vl++)
35 {
36 double flub = 1.0 * new_volume * 8 / 6;
37
38 if(vl)
39 flub /= pow(2, (double)1 / 4 * vl); // ~1.5dB reduction per increment of vl
40
41 if(vl == 0x1F)
42 flub = 0;
43
44 for(int samp = 0; samp < 32; samp++)
45 {
46 int eff_samp;
47
48 if(revision == REVISION_HUC6280)
49 eff_samp = samp * 2;
50 else
51 eff_samp = samp * 2 - 0x1F;
52
53 dbtable[vl][samp] = (int32_t)(flub * eff_samp * 128); // * 256);
54 dbtable_volonly[vl] = (int32_t)(flub * 65536);
55
56 // dbtable[vl][samp] = (int32_t)(flub * eff_samp * 128);
57 // dbtable_volonly[vl] = (int32_t)(flub * 65536);
58 }
59 }
60 }
61
62 // Note: Changing the 0x1F(not that there should be) would require changing the channel pseudo-off volume check logic later on.
63 static const int scale_tab[] =
64 {
65 0x00, 0x03, 0x05, 0x07, 0x09, 0x0B, 0x0D, 0x0F,
66 0x10, 0x13, 0x15, 0x17, 0x19, 0x1B, 0x1D, 0x1F
67 };
68
69 #define CLOCK_LFSR(lfsr) { unsigned int newbit = ((lfsr >> 0) ^ (lfsr >> 1) ^ (lfsr >> 11) ^ (lfsr >> 12) ^ (lfsr >> 17)) & 1; lfsr = (lfsr >> 1) | (newbit << 17); }
70
71 inline void PCE_PSG::UpdateOutputSub(const int32_t timestamp, psg_channel *ch, const int32_t samp0, const int32_t samp1)
72 {
73 int32_t delta[2];
74
75 delta[0] = samp0 - ch->blip_prev_samp[0];
76 delta[1] = samp1 - ch->blip_prev_samp[1];
77
78 if (delta[0]) blip_add_delta(bb[0],timestamp,delta[0]);
79 if (delta[1]) blip_add_delta(bb[1],timestamp,delta[1]);
80
81 ch->oscBuf->putSample(timestamp,CLAMP(samp0+samp1,-32768,32767));
82
83 ch->blip_prev_samp[0] = samp0;
84 ch->blip_prev_samp[1] = samp1;
85
86 }
87
88 void PCE_PSG::UpdateOutput_Norm(const int32_t timestamp, psg_channel *ch)
89 {
90 int sv = ch->dda;
91
92 UpdateOutputSub(timestamp, ch, dbtable[ch->vl[0]][sv],
93 dbtable[ch->vl[1]][sv]);
94 }
95
96 void PCE_PSG::UpdateOutput_Noise(const int32_t timestamp, psg_channel *ch)
97 {
98 int sv = ((ch->lfsr & 1) << 5) - (ch->lfsr & 1); //(ch->lfsr & 0x1) ? 0x1F : 0;
99
100 UpdateOutputSub(timestamp, ch, dbtable[ch->vl[0]][sv],
101 dbtable[ch->vl[1]][sv]);
102 }
103
104 void PCE_PSG::UpdateOutput_Off(const int32_t timestamp, psg_channel *ch)
105 {
106 UpdateOutputSub(timestamp, ch, 0, 0);
107 }
108
109 void PCE_PSG::UpdateOutput_Accum_HuC6280A(const int32_t timestamp, psg_channel *ch)
110 {
111 int32_t samp[2];
112
113 // 31(5-bit max) * 32 samples = 992
114 // 992 / 2 = 496
115 //
116 // 8 + 5 = 13
117 // 13 - 12 = 1
118
119 samp[0] = ((int32_t)dbtable_volonly[ch->vl[0]] * ((int32_t)ch->samp_accum - 496)) >> (8 + 5);
120 samp[1] = ((int32_t)dbtable_volonly[ch->vl[1]] * ((int32_t)ch->samp_accum - 496)) >> (8 + 5);
121
122 UpdateOutputSub(timestamp, ch, samp[0], samp[1]);
123 }
124
125 void PCE_PSG::UpdateOutput_Accum_HuC6280(const int32_t timestamp, psg_channel *ch)
126 {
127 int32_t samp[2];
128
129 samp[0] = ((int32_t)dbtable_volonly[ch->vl[0]] * (int32_t)ch->samp_accum) >> (8 + 5);
130 samp[1] = ((int32_t)dbtable_volonly[ch->vl[1]] * (int32_t)ch->samp_accum) >> (8 + 5);
131
132 UpdateOutputSub(timestamp, ch, samp[0], samp[1]);
133 }
134
135
136 // This function should always be called after RecalcFreqCache() (it's not called from RecalcFreqCache to avoid redundant code)
137 void PCE_PSG::RecalcUOFunc(int chnum)
138 {
139 psg_channel *ch = &channel[chnum];
140
141 //printf("UO Update: %d, %02x\n", chnum, ch->control);
142
143 if((revision != REVISION_HUC6280 && !(ch->control & 0xC0)) || (revision == REVISION_HUC6280 && !(ch->control & 0x80)))
144 ch->UpdateOutput = &PCE_PSG::UpdateOutput_Off;
145 else if(ch->noisectrl & ch->control & 0x80)
146 ch->UpdateOutput = &PCE_PSG::UpdateOutput_Noise;
147 // 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
148 // if the LFO trigger bit(which halts the LFO modulator channel's waveform incrementing when set) is clear
149 else if((ch->control & 0xC0) == 0x80 && ch->freq_cache <= FREQC7M_COT && (chnum != 1 || !(lfoctrl & 0x80)) )
150 ch->UpdateOutput = UpdateOutput_Accum;
151 else
152 ch->UpdateOutput = &PCE_PSG::UpdateOutput_Norm;
153 }
154
155
156 void PCE_PSG::RecalcFreqCache(int chnum)
157 {
158 psg_channel *ch = &channel[chnum];
159
160 if(chnum == 0 && (lfoctrl & 0x03))
161 {
162 const uint32_t shift = (((lfoctrl & 0x3) - 1) << 1);
163 uint8_t la = channel[1].dda;
164 uint32_t tmp_freq = (ch->frequency + ((uint32_t)(la - 0x10) << shift)) & 0xFFF;
165
166 ch->freq_cache = (tmp_freq ? tmp_freq : 4096) << 1;
167 }
168 else
169 {
170 ch->freq_cache = (ch->frequency ? ch->frequency : 4096) << 1;
171
172 if(chnum == 1 && (lfoctrl & 0x03))
173 ch->freq_cache *= lfofreq ? lfofreq : 256;
174 }
175 }
176
177 void PCE_PSG::RecalcNoiseFreqCache(int chnum)
178 {
179 psg_channel *ch = &channel[chnum];
180 int32_t freq = 0x1F - (ch->noisectrl & 0x1F);
181
182 if(!freq)
183 freq = 0x20;
184 else
185 freq <<= 6;
186
187 freq <<= 1;
188
189 ch->noise_freq_cache = freq;
190 }
191
192 void PCE_PSG::PeekWave(const unsigned int ch, uint32_t Address, uint32_t Length, uint8_t *Buffer)
193 {
194 assert(ch <= 5);
195
196 while(Length--)
197 {
198 Address &= 0x1F;
199 *Buffer = channel[ch].waveform[Address];
200 Address++;
201 Buffer++;
202 }
203 }
204
205 void PCE_PSG::PokeWave(const unsigned int ch, uint32_t Address, uint32_t Length, const uint8_t *Buffer)
206 {
207 assert(ch <= 5);
208
209 while(Length--)
210 {
211 Address &= 0x1F;
212 channel[ch].samp_accum -= channel[ch].waveform[Address];
213 channel[ch].waveform[Address] = *Buffer & 0x1F;
214 channel[ch].samp_accum += channel[ch].waveform[Address];
215 Address++;
216 Buffer++;
217 }
218 }
219
220 uint32_t PCE_PSG::GetRegister(const unsigned int id, char *special, const uint32_t special_len)
221 {
222 uint32_t value = 0xDEADBEEF;
223 const int ch = (id >> 8) & 0xF;
224
225 switch(id & 0xF0FF)
226 {
227 default: break;
228
229 case PSG_GSREG_SELECT:
230 value = select;
231 break;
232
233 case PSG_GSREG_GBALANCE:
234 value = globalbalance;
235 break;
236
237 case PSG_GSREG_LFOFREQ:
238 value = lfofreq;
239 break;
240
241 case PSG_GSREG_LFOCTRL:
242 value = lfoctrl;
243 break;
244
245 case PSG_GSREG_CH0_FREQ:
246 value = channel[ch].frequency;
247 break;
248
249 case PSG_GSREG_CH0_CTRL:
250 value = channel[ch].control;
251 break;
252
253 case PSG_GSREG_CH0_BALANCE:
254 value = channel[ch].balance;
255 break;
256
257 case PSG_GSREG_CH0_WINDEX:
258 value = channel[ch].waveform_index;
259 break;
260
261 case PSG_GSREG_CH0_SCACHE:
262 value = channel[ch].dda;
263 break;
264
265 case PSG_GSREG_CH0_NCTRL:
266 value = channel[ch].noisectrl;
267 break;
268
269 case PSG_GSREG_CH0_LFSR:
270 value = channel[ch].lfsr & 0x3FFFF;
271 break;
272 }
273 return(value);
274 }
275
276
277 void PCE_PSG::SetRegister(const unsigned int id, const uint32_t value)
278 {
279 const int ch = (id >> 8) & 0xF;
280
281 switch(id & 0xF0FF)
282 {
283 default: break;
284
285 case PSG_GSREG_SELECT:
286 select = value & 0x07;
287 break;
288
289 case PSG_GSREG_GBALANCE:
290 globalbalance = value & 0xFF;
291 break;
292
293 case PSG_GSREG_LFOFREQ:
294 lfofreq = value & 0xFF;
295 break;
296
297 case PSG_GSREG_LFOCTRL:
298 lfoctrl = value & 0x83;
299 RecalcFreqCache(0);
300 RecalcUOFunc(0);
301 RecalcFreqCache(1);
302 RecalcUOFunc(1);
303 break;
304
305 case PSG_GSREG_CH0_FREQ:
306 channel[ch].frequency = value & 0xFFF;
307 RecalcFreqCache(ch);
308 RecalcUOFunc(ch);
309 break;
310
311 case PSG_GSREG_CH0_CTRL:
312 channel[ch].control = value & 0xFF;
313 RecalcFreqCache(ch);
314 RecalcUOFunc(ch);
315 break;
316
317 case PSG_GSREG_CH0_BALANCE:
318 channel[ch].balance = value & 0xFF;
319 break;
320
321 case PSG_GSREG_CH0_WINDEX:
322 channel[ch].waveform_index = value & 0x1F;
323 break;
324
325 case PSG_GSREG_CH0_SCACHE:
326 channel[ch].dda = value & 0x1F;
327 break;
328
329 case PSG_GSREG_CH0_NCTRL:
330 channel[ch].noisectrl = value & 0xFF;
331 RecalcNoiseFreqCache(ch);
332 RecalcUOFunc(ch);
333 break;
334
335 case PSG_GSREG_CH0_LFSR:
336 channel[ch].lfsr = value & 0x3FFFF;
337 break;
338 }
339 }
340
341
342 #if 0
343 void PSG_SetRegister(const unsigned int id, const uint32_t value)
344 {
345
346
347 if(name == "Select")
348 PSG_Write(0x00, V);
349 else if(name == "GBalance")
350 PSG_Write(0x01, V);
351 else if(name == "LFOFreq")
352 {
353 PSG_Write(0x08, V);
354 }
355 else if(name == "LFOCtrl")
356 PSG_Write(0x09, V);
357 else if(!strncmp(name.c_str(), "CH", 2))
358 {
359 unsigned int psg_sel_save = select;
360 int ch = name[2] - '0';
361 char moomoo[64];
362 strncpy(moomoo, name.c_str() + 3, 63);
363
364 PSG_Write(0x00, ch);
365
366 if(!strcmp(moomoo, "Freq"))
367 {
368 PSG_Write(0x02, V);
369 PSG_Write(0x03, V >> 8);
370 }
371 else if(!strcmp(moomoo, "Ctrl"))
372 PSG_Write(0x04, V);
373 else if(!strcmp(moomoo, "Balance"))
374 PSG_Write(0x05, V);
375 else if(!strcmp(moomoo, "WIndex"))
376 psg.channel[ch].waveform_index = V & 0x1F;
377 else if(!strcmp(moomoo, "SCache"))
378 psg.channel[ch].dda = V & 0x1F;
379 else if(!strcmp(moomoo, "NCtrl") && ch < 4)
380 psg.channel[ch].noisectrl = V;
381 else if(!strcmp(moomoo, "LFSR") && ch < 4)
382 psg.channel[ch].lfsr = V & 0x3FFFF;
383
384 PSG_Write(0x00, psg_sel_save);
385 }
386 }
387 #endif
388
389 PCE_PSG::PCE_PSG(int want_revision)
390 {
391 //printf("Test: %u, %u\n", sizeof(psg_channel), (uint8_t*)&channel[0].balance - (uint8_t*)&channel[0].waveform[0]);
392
393 revision = want_revision;
394 switch(revision)
395 {
396 default:
397 //abort();
398 break;
399
400 case REVISION_HUC6280:
401 UpdateOutput_Accum = &PCE_PSG::UpdateOutput_Accum_HuC6280;
402 break;
403
404 case REVISION_HUC6280A:
405 UpdateOutput_Accum = &PCE_PSG::UpdateOutput_Accum_HuC6280A;
406 break;
407 }
408 bb[0]=NULL;
409 bb[1]=NULL;
410
411 lastts = 0;
412 for(int ch = 0; ch < 6; ch++)
413 {
414 channel[ch].blip_prev_samp[0] = 0;
415 channel[ch].blip_prev_samp[1] = 0;
416 channel[ch].lastts = 0;
417 }
418
419 SetVolume(1.0); // Will build dbtable in the process.
420 Power(0);
421 }
422
423 PCE_PSG::~PCE_PSG()
424 {
425
426
427 }
428
429 int32_t PCE_PSG::GetVL(const int chnum, const int lr)
430 {
431 psg_channel *ch = &channel[chnum];
432
433 const int gbal = 0x1F - scale_tab[(globalbalance >> (lr ? 0 : 4)) & 0xF];
434 const int bal = 0x1F - scale_tab[(ch->balance >> (lr ? 0 : 4)) & 0xF];
435 const int al = 0x1F - (ch->control & 0x1F);
436 int vol_reduction;
437
438 vol_reduction = gbal + bal + al;
439
440 if(vol_reduction > 0x1F)
441 vol_reduction = 0x1F;
442
443 return(vol_reduction);
444 }
445
446 void PCE_PSG::Write(int32_t timestamp, uint8_t A, uint8_t V)
447 {
448 A &= 0x0F;
449
450 if(A == 0x00)
451 {
452 select = (V & 0x07);
453 return;
454 }
455
456 Update(timestamp);
457
458 psg_channel *ch = &channel[select];
459
460 //if(A == 0x01 || select == 5)
461 //printf("Write Ch: %d %04x %02x, %d\n", select, A, V, timestamp);
462
463 switch(A)
464 {
465 default: break;
466
467 case 0x01: /* Global sound balance */
468 globalbalance = V;
469 vol_pending = true;
470 break;
471
472 case 0x02: /* Channel frequency (LSB) */
473 if(select > 5) return; // no more than 6 channels, silly game.
474
475 ch->frequency = (ch->frequency & 0x0F00) | V;
476 RecalcFreqCache(select);
477 RecalcUOFunc(select);
478 break;
479
480 case 0x03: /* Channel frequency (MSB) */
481 if(select > 5) return; // no more than 6 channels, silly game.
482
483 ch->frequency = (ch->frequency & 0x00FF) | ((V & 0x0F) << 8);
484 RecalcFreqCache(select);
485 RecalcUOFunc(select);
486 break;
487
488 case 0x04: /* Channel enable, DDA, volume */
489 if(select > 5) return; // no more than 6 channels, silly game.
490
491 if((ch->control & 0x40) && !(V & 0x40))
492 {
493 ch->waveform_index = 0;
494 ch->dda = ch->waveform[ch->waveform_index];
495 ch->counter = ch->freq_cache;
496 }
497
498 if(!(ch->control & 0x80) && (V & 0x80))
499 {
500 if(!(V & 0x40))
501 {
502 ch->waveform_index = (ch->waveform_index + 1) & 0x1F;
503 ch->dda = ch->waveform[ch->waveform_index];
504 }
505 }
506
507 ch->control = V;
508 RecalcFreqCache(select);
509 RecalcUOFunc(select);
510
511 vol_pending = true;
512 break;
513
514 case 0x05: /* Channel balance */
515 if(select > 5) return; // no more than 6 channels, silly game.
516 ch->balance = V;
517
518 vol_pending = true;
519 break;
520
521 case 0x06: /* Channel waveform data */
522 if(select > 5) return; // no more than 6 channels, silly game.
523 V &= 0x1F;
524
525 if(!(ch->control & 0x40))
526 {
527 ch->samp_accum -= ch->waveform[ch->waveform_index];
528 ch->waveform[ch->waveform_index] = V;
529 ch->samp_accum += ch->waveform[ch->waveform_index];
530 }
531
532 if((ch->control & 0xC0) == 0x00)
533 ch->waveform_index = ((ch->waveform_index + 1) & 0x1F);
534
535 if(ch->control & 0x80)
536 {
537 // According to my tests(on SuperGrafx), writing to this channel
538 // will update the waveform value cache/latch regardless of DDA mode being enabled.
539 ch->dda = V;
540 }
541 break;
542
543 case 0x07: /* Noise enable and frequency */
544 if(select > 5) return; // no more than 6 channels, silly game.
545 if(select >= 4)
546 {
547 ch->noisectrl = V;
548 RecalcNoiseFreqCache(select);
549 RecalcUOFunc(select);
550 }
551 break;
552
553 case 0x08: /* LFO frequency */
554 lfofreq = V & 0xFF;
555 //printf("LFO Freq: %02x\n", V);
556 break;
557
558 case 0x09: /* LFO trigger and control */
559 //printf("LFO Ctrl: %02x\n", V);
560 if(V & 0x80)
561 {
562 channel[1].waveform_index = 0;
563 channel[1].dda = channel[1].waveform[channel[1].waveform_index];
564 channel[1].counter = channel[1].freq_cache;
565 }
566 lfoctrl = V;
567 RecalcFreqCache(0);
568 RecalcUOFunc(0);
569 RecalcFreqCache(1);
570 RecalcUOFunc(1);
571 break;
572 }
573 }
574
575 // Don't use inline, which has always_inline in it, due to gcc's inability to cope with the type of recursion
576 // used in this function.
577 void PCE_PSG::RunChannel(int chc, int32_t timestamp, const bool LFO_On)
578 {
579 psg_channel *ch = &channel[chc];
580 int32_t running_timestamp = ch->lastts;
581 int32_t run_time = timestamp - ch->lastts;
582
583 ch->lastts = timestamp;
584
585 if(!run_time) {
586 return;
587 }
588
589 (this->*ch->UpdateOutput)(running_timestamp, ch);
590
591 if(chc >= 4)
592 {
593 int32_t freq = ch->noise_freq_cache;
594
595 ch->noisecount -= run_time;
596
597 if(&PCE_PSG::UpdateOutput_Noise == ch->UpdateOutput)
598 while(ch->noisecount <= 0)
599 {
600 CLOCK_LFSR(ch->lfsr);
601 UpdateOutput_Noise(timestamp + ch->noisecount, ch);
602 ch->noisecount += freq;
603 }
604 else
605 while(ch->noisecount <= 0)
606 {
607 CLOCK_LFSR(ch->lfsr);
608 ch->noisecount += freq;
609 }
610 }
611
612 // D7 of control is 0, don't clock the counter at all.
613 // D7 of lfocontrol is 1(and chc == 1), don't clock the counter at all(not sure about this)
614 // In DDA mode, don't clock the counter.
615 // (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
616 // the waveform portion when the noise enable bit is set, which is handled in our RecalcUOFunc).
617 if(!(ch->control & 0x80) || (chc == 1 && (lfoctrl & 0x80)) || (ch->control & 0x40))
618 return;
619
620 ch->counter -= run_time;
621
622 if(!LFO_On && ch->freq_cache <= FREQC7M_COT)
623 {
624 if(ch->counter <= 0)
625 {
626 const int32_t inc_count = ((0 - ch->counter) / ch->freq_cache) + 1;
627
628 ch->counter += inc_count * ch->freq_cache;
629
630 ch->waveform_index = (ch->waveform_index + inc_count) & 0x1F;
631 ch->dda = ch->waveform[ch->waveform_index];
632 }
633 }
634
635 while(ch->counter <= 0)
636 {
637 ch->waveform_index = (ch->waveform_index + 1) & 0x1F;
638 ch->dda = ch->waveform[ch->waveform_index];
639
640 if(&PCE_PSG::UpdateOutput_Noise != ch->UpdateOutput) {
641 (this->*ch->UpdateOutput)(timestamp + ch->counter, ch);
642 }
643
644 if(LFO_On)
645 {
646 RunChannel(1, timestamp + ch->counter, false);
647 RecalcFreqCache(0);
648 RecalcUOFunc(0);
649
650 ch->counter += (ch->freq_cache <= FREQC7M_COT) ? FREQC7M_COT : ch->freq_cache; // Not particularly accurate, but faster.
651 }
652 else
653 ch->counter += ch->freq_cache;
654 }
655 }
656
657 void PCE_PSG::UpdateSubLFO(int32_t timestamp)
658 {
659 for(int chc = 0; chc < 6; chc++)
660 RunChannel(chc, timestamp, chc == 0);
661 }
662
663 void PCE_PSG::UpdateSubNonLFO(int32_t timestamp)
664 {
665 for(int chc = 0; chc < 6; chc++)
666 RunChannel(chc, timestamp, false);
667 }
668
669 void PCE_PSG::Update(int32_t timestamp)
670 {
671 int32_t run_time = timestamp - lastts;
672
673 if(vol_pending && !vol_update_counter && !vol_update_which)
674 {
675 vol_update_counter = 1;
676 vol_pending = false;
677 }
678
679 bool lfo_on = (bool)(lfoctrl & 0x03);
680
681 if(lfo_on)
682 {
683 if(!(channel[1].control & 0x80) || (lfoctrl & 0x80))
684 {
685 lfo_on = 0;
686 RecalcFreqCache(0);
687 RecalcUOFunc(0);
688 }
689 }
690
691 int32_t clocks = run_time;
692 int32_t running_timestamp = lastts;
693
694 while(clocks > 0)
695 {
696 int32_t chunk_clocks = clocks;
697
698 if(vol_update_counter > 0 && chunk_clocks > vol_update_counter)
699 chunk_clocks = vol_update_counter;
700
701 running_timestamp += chunk_clocks;
702 clocks -= chunk_clocks;
703
704 if(lfo_on)
705 UpdateSubLFO(running_timestamp);
706 else
707 UpdateSubNonLFO(running_timestamp);
708
709 if(vol_update_counter > 0)
710 {
711 vol_update_counter -= chunk_clocks;
712 if(!vol_update_counter)
713 {
714 const int phase = vol_update_which & 1;
715 const int lr = ((vol_update_which >> 1) & 1) ^ 1;
716 const int chnum = vol_update_which >> 2;
717
718 if(!phase)
719 {
720 //printf("Volume update(Read, %d since last): ch=%d, lr=%d, ts=%d\n", running_timestamp - last_read, chnum, lr, running_timestamp);
721
722 if(chnum < 6)
723 {
724 vol_update_vllatch = GetVL(chnum, lr);
725 }
726 //last_read = running_timestamp;
727 }
728 else
729 {
730 // printf("Volume update(Apply): ch=%d, lr=%d, ts=%d\n", chnum, lr, running_timestamp);
731 if(chnum < 6)
732 {
733 channel[chnum].vl[lr] = vol_update_vllatch;
734 }
735 //last_apply = running_timestamp;
736 }
737 vol_update_which = (vol_update_which + 1) & 0x1F;
738
739 if(vol_update_which)
740 vol_update_counter = phase ? 1 : 255;
741 else if(vol_pending)
742 {
743 vol_update_counter = phase ? 1 : 255;
744 vol_pending = false;
745 }
746 }
747 }
748
749 lastts = running_timestamp;
750 }
751 }
752
753 void PCE_PSG::ResetTS(int32_t ts_base)
754 {
755 lastts = ts_base;
756
757 for(int chc = 0; chc < 6; chc++) {
758 channel[chc].lastts = ts_base;
759 }
760 }
761
762 void PCE_PSG::Power(const int32_t timestamp)
763 {
764 // Not sure about power-on values, these are mostly just intuitive guesses(with some laziness thrown in).
765 if(timestamp != lastts)
766 Update(timestamp);
767
768 // Don't memset channel to 0, there's stuff like lastts and blip_prev_samp that shouldn't be altered on Power().
769
770 select = 0;
771 globalbalance = 0;
772 lfofreq = 0;
773 lfoctrl = 0;
774
775 for(int ch = 0; ch < 6; ch++)
776 {
777 channel[ch].frequency = 0;
778 channel[ch].control = 0x00;
779 channel[ch].balance = 0;
780 memset(channel[ch].waveform, 0, 32);
781 channel[ch].samp_accum = 0;
782
783 channel[ch].waveform_index = 0;
784 channel[ch].dda = 0x00;
785 channel[ch].noisectrl = 0x00;
786
787 channel[ch].vl[0] = 0x1F;
788 channel[ch].vl[1] = 0x1F;
789
790 channel[ch].samp_accum = 0;
791
792 RecalcFreqCache(ch);
793 RecalcUOFunc(ch);
794
795 channel[ch].counter = channel[ch].freq_cache;
796
797 if(ch >= 4)
798 {
799 RecalcNoiseFreqCache(ch);
800 }
801 channel[ch].noisecount = 1;
802 channel[ch].lfsr = 1;
803 }
804
805 vol_pending = false;
806 vol_update_counter = 0;
807 vol_update_which = 0;
808 }