1 // ---------------------------------------------------------------------------
2 // PSG Sound Implementation
3 // Copyright (C) cisc 1997, 1999.
4 // ---------------------------------------------------------------------------
5 // $Id: psg.cpp,v 1.10 2002/05/15 21:38:01 cisc Exp $
13 #include "../../fileio.h"
15 // ---------------------------------------------------------------------------
16 //
\83R
\83\93\83X
\83g
\83\89\83N
\83^
\81E
\83f
\83X
\83g
\83\89\83N
\83^
24 envelop_l = enveloptable_l[0]; // temporary fix
25 envelop_r = enveloptable_r[0]; // temporary fix
33 // ---------------------------------------------------------------------------
34 // PSG
\82ð
\8f\89\8aú
\89»
\82·
\82é(RESET)
38 for (int i=0; i<14; i++)
45 // ---------------------------------------------------------------------------
46 //
\83N
\83\8d\83b
\83N
\8eü
\94g
\90\94\82Ì
\90Ý
\92è
48 void PSG::SetClock(int clock, int rate)
50 tperiodbase = int((1 << toneshift ) / 4.0 * clock / rate);
51 eperiodbase = int((1 << envshift ) / 4.0 * clock / rate);
52 nperiodbase = int((1 << noiseshift) / 4.0 * clock / rate);
54 //
\8ae
\83f
\81[
\83^
\82Ì
\8dX
\90V
56 tmp = ((reg[0] + reg[1] * 256) & 0xfff);
57 speriod[0] = tmp ? tperiodbase / tmp : tperiodbase;
58 tmp = ((reg[2] + reg[3] * 256) & 0xfff);
59 speriod[1] = tmp ? tperiodbase / tmp : tperiodbase;
60 tmp = ((reg[4] + reg[5] * 256) & 0xfff);
61 speriod[2] = tmp ? tperiodbase / tmp : tperiodbase;
63 nperiod = tmp ? nperiodbase / tmp / 2 : nperiodbase / 2;
64 tmp = ((reg[11] + reg[12] * 256) & 0xffff);
65 eperiod = tmp ? eperiodbase / tmp : eperiodbase * 2;
68 // ---------------------------------------------------------------------------
69 //
\83m
\83C
\83Y
\83e
\81[
\83u
\83\8b\82ð
\8dì
\90¬
\82·
\82é
71 void PSG::MakeNoiseTable()
76 for (int i=0; i<noisetablesize; i++)
79 for (int j=0; j<32; j++)
81 n = n * 2 + (noise & 1);
82 noise = (noise >> 1) | (((noise << 14) ^ (noise << 16)) & 0x10000);
89 // ---------------------------------------------------------------------------
90 //
\8fo
\97Í
\83e
\81[
\83u
\83\8b\82ð
\8dì
\90¬
91 //
\91f
\92¼
\82É
\83e
\81[
\83u
\83\8b\82Å
\8e\9d\82Á
\82½
\82Ù
\82¤
\82ª
\8fÈ
\83X
\83y
\81[
\83X
\81B
93 void PSG::SetVolume(int volume_l, int volume_r)
95 double base_l = 0x4000 / 3.0 * pow(10.0, volume_l / 40.0);
96 double base_r = 0x4000 / 3.0 * pow(10.0, volume_r / 40.0);
97 #if defined(HAS_AY_3_8910) || defined(HAS_AY_3_8912)
98 // AY-3-8190/8192 (PSG): 16step
99 for (int i=31; i>=3; i-=2)
101 EmitTableL[i] = EmitTableL[i-1] = int(base_l);
102 EmitTableR[i] = EmitTableR[i-1] = int(base_r);
103 base_l /= 1.189207115;
104 base_l /= 1.189207115;
105 base_r /= 1.189207115;
106 base_r /= 1.189207115;
109 // YM2203 (SSG): 32step
110 for (int i=31; i>=2; i--)
112 EmitTableL[i] = int(base_l);
113 EmitTableR[i] = int(base_r);
114 base_l /= 1.189207115;
115 base_r /= 1.189207115;
124 SetChannelMask(~mask);
127 void PSG::SetChannelMask(int c)
130 for (int i=0; i<3; i++)
132 olevel_l[i] = mask & (1 << i) ? EmitTableL[(reg[8+i] & 15) * 2 + 1] : 0;
133 olevel_r[i] = mask & (1 << i) ? EmitTableR[(reg[8+i] & 15) * 2 + 1] : 0;
137 // ---------------------------------------------------------------------------
138 //
\83G
\83\93\83x
\83\8d\81[
\83v
\94g
\8c`
\83e
\81[
\83u
\83\8b
140 void PSG::MakeEnvelopTable()
142 // 0 lo 1 up 2 down 3 hi
143 static uint8 table1[16*2] =
145 2,0, 2,0, 2,0, 2,0, 1,0, 1,0, 1,0, 1,0,
146 2,2, 2,0, 2,1, 2,3, 1,1, 1,3, 1,2, 1,0,
148 static uint8 table2[4] = { 0, 0, 31, 31 };
149 //static uint8 table3[4] = { 0, 1, 255, 0 }; // -1 will embed bugs except some compilers.
150 static uint8 table3[4] = { 0, 1, -1, 0 }; // -1 will embed bugs except some compilers.
152 uint* ptr_l = enveloptable_l[0];
153 uint* ptr_r = enveloptable_r[0];
155 for (int i=0; i<16*2; i++)
157 uint8 v = table2[table1[i]];
159 for (int j=0; j<32; j++)
161 *ptr_l++ = EmitTableL[v];
162 *ptr_r++ = EmitTableR[v];
163 v += table3[table1[i]];
168 // ---------------------------------------------------------------------------
169 // PSG
\82Ì
\83\8c\83W
\83X
\83^
\82É
\92l
\82ð
\83Z
\83b
\83g
\82·
\82é
170 // regnum
\83\8c\83W
\83X
\83^
\82Ì
\94Ô
\8d\86 (0 - 15)
171 // data
\83Z
\83b
\83g
\82·
\82é
\92l
173 void PSG::SetReg(uint regnum, uint8 data)
182 case 0: // ChA Fine Tune
183 case 1: // ChA Coarse Tune
184 tmp = ((reg[0] + reg[1] * 256) & 0xfff);
185 speriod[0] = tmp ? tperiodbase / tmp : tperiodbase;
188 case 2: // ChB Fine Tune
189 case 3: // ChB Coarse Tune
190 tmp = ((reg[2] + reg[3] * 256) & 0xfff);
191 speriod[1] = tmp ? tperiodbase / tmp : tperiodbase;
194 case 4: // ChC Fine Tune
195 case 5: // ChC Coarse Tune
196 tmp = ((reg[4] + reg[5] * 256) & 0xfff);
197 speriod[2] = tmp ? tperiodbase / tmp : tperiodbase;
200 case 6: // Noise generator control
202 nperiod = data ? nperiodbase / data : nperiodbase;
206 olevel_l[0] = mask & 1 ? EmitTableL[(data & 15) * 2 + 1] : 0;
207 olevel_r[0] = mask & 1 ? EmitTableR[(data & 15) * 2 + 1] : 0;
211 olevel_l[1] = mask & 2 ? EmitTableL[(data & 15) * 2 + 1] : 0;
212 olevel_r[1] = mask & 2 ? EmitTableR[(data & 15) * 2 + 1] : 0;
216 olevel_l[2] = mask & 4 ? EmitTableL[(data & 15) * 2 + 1] : 0;
217 olevel_r[2] = mask & 4 ? EmitTableR[(data & 15) * 2 + 1] : 0;
220 case 11: // Envelop period
222 tmp = ((reg[11] + reg[12] * 256) & 0xffff);
223 eperiod = tmp ? eperiodbase / tmp : eperiodbase * 2;
226 case 13: // Envelop shape
228 envelop_l = enveloptable_l[data & 15];
229 envelop_r = enveloptable_r[data & 15];
235 // ---------------------------------------------------------------------------
238 inline void PSG::StoreSample(Sample& dest, int32 data)
240 if (sizeof(Sample) == 2)
241 dest = (Sample) Limit(dest + data, 0x7fff, -0x8000);
246 // ---------------------------------------------------------------------------
247 // PCM
\83f
\81[
\83^
\82ð
\93f
\82«
\8fo
\82·(2ch)
248 // dest PCM
\83f
\81[
\83^
\82ð
\93W
\8aJ
\82·
\82é
\83|
\83C
\83\93\83^
249 // nsamples
\93W
\8aJ
\82·
\82é PCM
\82Ì
\83T
\83\93\83v
\83\8b\90\94
251 void PSG::Mix(Sample* dest, int nsamples)
253 uint8 chenable[3], nenable[3];
256 if ((r7 & 0x3f) | ((reg[8] | reg[9] | reg[10]) & 0x1f))
258 chenable[0] = (r7 & 0x01) && (speriod[0] <= (1 << toneshift));
259 chenable[1] = (r7 & 0x02) && (speriod[1] <= (1 << toneshift));
260 chenable[2] = (r7 & 0x04) && (speriod[2] <= (1 << toneshift));
261 nenable[0] = (r7 >> 3) & 1;
262 nenable[1] = (r7 >> 4) & 1;
263 nenable[2] = (r7 >> 5) & 1;
265 int noise, sample_l, sample_r;
268 uint* p1_l = ((mask & 1) && (reg[ 8] & 0x10)) ? &env_l : &olevel_l[0];
269 uint* p1_r = ((mask & 1) && (reg[ 8] & 0x10)) ? &env_r : &olevel_r[0];
270 uint* p2_l = ((mask & 2) && (reg[ 9] & 0x10)) ? &env_l : &olevel_l[1];
271 uint* p2_r = ((mask & 2) && (reg[ 9] & 0x10)) ? &env_r : &olevel_r[1];
272 uint* p3_l = ((mask & 4) && (reg[10] & 0x10)) ? &env_l : &olevel_l[2];
273 uint* p3_r = ((mask & 4) && (reg[10] & 0x10)) ? &env_r : &olevel_r[2];
275 #define SCOUNT(ch) (scount[ch] >> (toneshift+oversampling))
277 if (p1_l != &env_l && p2_l != &env_l && p3_l != &env_l)
279 //
\83G
\83\93\83x
\83\8d\81[
\83v
\96³
\82µ
280 if ((r7 & 0x38) == 0)
282 //
\83m
\83C
\83Y
\96³
\82µ
283 for (int i=0; i<nsamples; i++)
287 for (int j=0; j < (1 << oversampling); j++)
290 x = (SCOUNT(0) & chenable[0]) - 1;
291 sample_l += (olevel_l[0] + x) ^ x;
292 sample_r += (olevel_r[0] + x) ^ x;
293 scount[0] += speriod[0];
294 y = (SCOUNT(1) & chenable[1]) - 1;
295 sample_l += (olevel_l[1] + y) ^ y;
296 sample_r += (olevel_r[1] + y) ^ y;
297 scount[1] += speriod[1];
298 z = (SCOUNT(2) & chenable[2]) - 1;
299 sample_l += (olevel_l[2] + z) ^ z;
300 sample_r += (olevel_r[2] + z) ^ z;
301 scount[2] += speriod[2];
303 sample_l /= (1 << oversampling);
304 sample_r /= (1 << oversampling);
305 StoreSample(dest[0], sample_l);
306 StoreSample(dest[1], sample_r);
312 //
\83m
\83C
\83Y
\97L
\82è
313 for (int i=0; i<nsamples; i++)
317 for (int j=0; j < (1 << oversampling); j++)
320 noise = noisetable[(ncount >> (noiseshift+oversampling+6)) & (noisetablesize-1)]
321 >> (ncount >> (noiseshift+oversampling+1));
323 noise = noisetable[(ncount >> (noiseshift+oversampling+6)) & (noisetablesize-1)]
324 >> (ncount >> (noiseshift+oversampling+1) & 31);
329 x = ((SCOUNT(0) & chenable[0]) | (nenable[0] & noise)) - 1; // 0 or -1
330 sample_l += (olevel_l[0] + x) ^ x;
331 sample_r += (olevel_r[0] + x) ^ x;
332 scount[0] += speriod[0];
333 y = ((SCOUNT(1) & chenable[1]) | (nenable[1] & noise)) - 1;
334 sample_l += (olevel_l[1] + y) ^ y;
335 sample_r += (olevel_r[1] + y) ^ y;
336 scount[1] += speriod[1];
337 z = ((SCOUNT(2) & chenable[2]) | (nenable[2] & noise)) - 1;
338 sample_l += (olevel_l[2] + z) ^ z;
339 sample_r += (olevel_r[2] + z) ^ z;
340 scount[2] += speriod[2];
342 sample_l /= (1 << oversampling);
343 sample_r /= (1 << oversampling);
344 StoreSample(dest[0], sample_l);
345 StoreSample(dest[1], sample_r);
350 //
\83G
\83\93\83x
\83\8d\81[
\83v
\82Ì
\8cv
\8eZ
\82ð
\82³
\82Ú
\82Á
\82½
\92 \90K
\82 \82í
\82¹
351 ecount = (ecount >> 8) + (eperiod >> (8-oversampling)) * nsamples;
352 if (ecount >= (1 << (envshift+6+oversampling-8)))
354 if ((reg[0x0d] & 0x0b) != 0x0a)
355 ecount |= (1 << (envshift+5+oversampling-8));
356 ecount &= (1 << (envshift+6+oversampling-8)) - 1;
362 //
\83G
\83\93\83x
\83\8d\81[
\83v
\82 \82è
363 for (int i=0; i<nsamples; i++)
367 for (int j=0; j < (1 << oversampling); j++)
369 env_l = envelop_l[ecount >> (envshift+oversampling)];
370 env_r = envelop_r[ecount >> (envshift+oversampling)];
372 if (ecount >= (1 << (envshift+6+oversampling)))
374 if ((reg[0x0d] & 0x0b) != 0x0a)
375 ecount |= (1 << (envshift+5+oversampling));
376 ecount &= (1 << (envshift+6+oversampling)) - 1;
379 noise = noisetable[(ncount >> (noiseshift+oversampling+6)) & (noisetablesize-1)]
380 >> (ncount >> (noiseshift+oversampling+1));
382 noise = noisetable[(ncount >> (noiseshift+oversampling+6)) & (noisetablesize-1)]
383 >> (ncount >> (noiseshift+oversampling+1) & 31);
388 x = ((SCOUNT(0) & chenable[0]) | (nenable[0] & noise)) - 1; // 0 or -1
389 sample_l += (*p1_l + x) ^ x;
390 sample_r += (*p1_r + x) ^ x;
391 scount[0] += speriod[0];
392 y = ((SCOUNT(1) & chenable[1]) | (nenable[1] & noise)) - 1;
393 sample_l += (*p2_l + y) ^ y;
394 sample_r += (*p2_r + y) ^ y;
395 scount[1] += speriod[1];
396 z = ((SCOUNT(2) & chenable[2]) | (nenable[2] & noise)) - 1;
397 sample_l += (*p3_l + z) ^ z;
398 sample_r += (*p3_r + z) ^ z;
399 scount[2] += speriod[2];
401 sample_l /= (1 << oversampling);
402 sample_r /= (1 << oversampling);
403 StoreSample(dest[0], sample_l);
404 StoreSample(dest[1], sample_r);
411 // ---------------------------------------------------------------------------
412 //
\83e
\81[
\83u
\83\8b
414 uint PSG::noisetable[noisetablesize] = { 0, };
415 int PSG::EmitTableL[0x20] = { -1, };
416 int PSG::EmitTableR[0x20] = { -1, };
417 uint PSG::enveloptable_l[16][64] = { 0, };
418 uint PSG::enveloptable_r[16][64] = { 0, };
420 // ---------------------------------------------------------------------------
421 //
\83X
\83e
\81[
\83g
\83Z
\81[
\83u
423 #define PSG_STATE_VERSION 2
425 void PSG::SaveState(void *f)
427 FILEIO *state_fio = (FILEIO *)f;
429 state_fio->FputUint32(PSG_STATE_VERSION);
431 state_fio->Fwrite(reg, sizeof(reg), 1);
432 state_fio->FputInt32((int)(envelop_l - &enveloptable_l[0][0]));
433 state_fio->Fwrite(olevel_l, sizeof(olevel_l), 1);
434 state_fio->Fwrite(olevel_r, sizeof(olevel_r), 1);
435 state_fio->Fwrite(scount, sizeof(scount), 1);
436 state_fio->Fwrite(speriod, sizeof(speriod), 1);
437 state_fio->FputUint32(ecount);
438 state_fio->FputUint32(eperiod);
439 state_fio->FputUint32(ncount);
440 state_fio->FputUint32(nperiod);
441 state_fio->FputUint32(tperiodbase);
442 state_fio->FputUint32(eperiodbase);
443 state_fio->FputUint32(nperiodbase);
444 state_fio->FputInt32(mask);
447 bool PSG::LoadState(void *f)
449 FILEIO *state_fio = (FILEIO *)f;
451 if(state_fio->FgetUint32() != PSG_STATE_VERSION) {
454 state_fio->Fread(reg, sizeof(reg), 1);
455 int offset = state_fio->FgetInt32();
456 envelop_l = &enveloptable_l[0][0] + offset;
457 envelop_r = &enveloptable_r[0][0] + offset;
458 state_fio->Fread(olevel_l, sizeof(olevel_l), 1);
459 state_fio->Fread(olevel_r, sizeof(olevel_r), 1);
460 state_fio->Fread(scount, sizeof(scount), 1);
461 state_fio->Fread(speriod, sizeof(speriod), 1);
462 ecount = state_fio->FgetUint32();
463 eperiod = state_fio->FgetUint32();
464 ncount = state_fio->FgetUint32();
465 nperiod = state_fio->FgetUint32();
466 tperiodbase = state_fio->FgetUint32();
467 eperiodbase = state_fio->FgetUint32();
468 nperiodbase = state_fio->FgetUint32();
469 mask = state_fio->FgetInt32();