1 /***************************************************************************
5 Emulation of the AY-3-8910 / YM2149 sound chip.
7 Based on various code snippets by Ville Hallik, Michael Cuddy,
8 Tatsuyuki Satoh, Fabrice Frances, Nicola Salmoria.
10 ***************************************************************************/
13 // From mame.txt (http://www.mame.net/readme.html)
15 // VI. Reuse of Source Code
16 // --------------------------
17 // This chapter might not apply to specific portions of MAME (e.g. CPU
18 // emulators) which bear different copyright notices.
19 // The source code cannot be used in a commercial product without the written
20 // authorization of the authors. Use in non-commercial products is allowed, and
21 // indeed encouraged. If you use portions of the MAME source code in your
22 // program, however, you must make the full source code freely available as
24 // Usage of the _information_ contained in the source code is free for any use.
25 // However, given the amount of time and energy it took to collect this
26 // information, if you find new information we would appreciate if you made it
27 // freely available as well.
30 // JLH: Commented out MAME specific crap
33 #include <string.h> // for memset()
35 #define MAX_OUTPUT 0x7FFF
37 // See AY8910_set_clock() for definition of STEP
44 // mem_read_handler PortAread;
45 // mem_read_handler PortBread;
46 // mem_write_handler PortAwrite;
47 // mem_write_handler PortBwrite;
49 unsigned char Regs[16];
51 unsigned int UpdateStep;
52 int PeriodA, PeriodB, PeriodC, PeriodN, PeriodE;
53 int CountA, CountB, CountC, CountN, CountE;
54 unsigned int VolA, VolB, VolC, VolE;
55 unsigned char EnvelopeA, EnvelopeB, EnvelopeC;
56 unsigned char OutputA, OutputB, OutputC, OutputN;
58 unsigned char Hold, Alternate, Attack, Holding;
60 unsigned int VolTable[32];
65 #define AY_ACOARSE (1)
67 #define AY_BCOARSE (3)
69 #define AY_CCOARSE (5)
70 #define AY_NOISEPER (6)
76 #define AY_ECOARSE (12)
77 #define AY_ESHAPE (13)
83 static struct AY8910 AYPSG[MAX_8910]; /* array of PSG's */
86 void _AYWriteReg(int n, int r, int v)
88 struct AY8910 *PSG = &AYPSG[n];
93 /* A note about the period of tones, noise and envelope: for speed reasons, *
94 * we count down from the period to 0, but careful studies of the chip *
95 * output prove that it instead counts up from 0 until the counter becomes *
96 * greater or equal to the period. This is an important difference when the *
97 * program is rapidly changing the period to modulate the sound. *
98 * To compensate for the difference, when the period is changed we adjust *
99 * our internal counter. *
100 * Also, note that period = 0 is the same as period = 1. This is mentioned *
101 * in the YM2203 data sheets. However, this does NOT apply to the Envelope *
102 * period. In that case, period = 0 is half as period = 1. */
107 PSG->Regs[AY_ACOARSE] &= 0x0F;
109 PSG->PeriodA = (PSG->Regs[AY_AFINE] + 256 * PSG->Regs[AY_ACOARSE]) * PSG->UpdateStep;
111 if (PSG->PeriodA == 0)
112 PSG->PeriodA = PSG->UpdateStep;
114 PSG->CountA += PSG->PeriodA - old;
116 if (PSG->CountA <= 0)
121 PSG->Regs[AY_BCOARSE] &= 0x0F;
123 PSG->PeriodB = (PSG->Regs[AY_BFINE] + 256 * PSG->Regs[AY_BCOARSE]) * PSG->UpdateStep;
125 if (PSG->PeriodB == 0)
126 PSG->PeriodB = PSG->UpdateStep;
128 PSG->CountB += PSG->PeriodB - old;
130 if (PSG->CountB <= 0)
135 PSG->Regs[AY_CCOARSE] &= 0x0F;
137 PSG->PeriodC = (PSG->Regs[AY_CFINE] + 256 * PSG->Regs[AY_CCOARSE]) * PSG->UpdateStep;
139 if (PSG->PeriodC == 0)
140 PSG->PeriodC = PSG->UpdateStep;
142 PSG->CountC += PSG->PeriodC - old;
144 if (PSG->CountC <= 0)
148 PSG->Regs[AY_NOISEPER] &= 0x1F;
150 PSG->PeriodN = PSG->Regs[AY_NOISEPER] * PSG->UpdateStep;
152 if (PSG->PeriodN == 0)
153 PSG->PeriodN = PSG->UpdateStep;
155 PSG->CountN += PSG->PeriodN - old;
157 if (PSG->CountN <= 0)
161 if ((PSG->lastEnable == -1) ||
162 ((PSG->lastEnable & 0x40) != (PSG->Regs[AY_ENABLE] & 0x40)))
164 /* write out 0xff if port set to input */
165 // if (PSG->PortAwrite)
166 // (*PSG->PortAwrite)(0, (UINT8) ((PSG->Regs[AY_ENABLE] & 0x40) ? PSG->Regs[AY_PORTA] : 0xff)); // [TC: UINT8 cast]
169 if ((PSG->lastEnable == -1) ||
170 ((PSG->lastEnable & 0x80) != (PSG->Regs[AY_ENABLE] & 0x80)))
172 /* write out 0xff if port set to input */
173 // if (PSG->PortBwrite)
174 // (*PSG->PortBwrite)(0, (UINT8) ((PSG->Regs[AY_ENABLE] & 0x80) ? PSG->Regs[AY_PORTB] : 0xff)); // [TC: UINT8 cast]
177 PSG->lastEnable = PSG->Regs[AY_ENABLE];
180 PSG->Regs[AY_AVOL] &= 0x1F;
181 PSG->EnvelopeA = PSG->Regs[AY_AVOL] & 0x10;
182 PSG->VolA = PSG->EnvelopeA ? PSG->VolE : PSG->VolTable[PSG->Regs[AY_AVOL] ? PSG->Regs[AY_AVOL]*2+1 : 0];
185 PSG->Regs[AY_BVOL] &= 0x1F;
186 PSG->EnvelopeB = PSG->Regs[AY_BVOL] & 0x10;
187 PSG->VolB = PSG->EnvelopeB ? PSG->VolE : PSG->VolTable[PSG->Regs[AY_BVOL] ? PSG->Regs[AY_BVOL]*2+1 : 0];
190 PSG->Regs[AY_CVOL] &= 0x1F;
191 PSG->EnvelopeC = PSG->Regs[AY_CVOL] & 0x10;
192 PSG->VolC = PSG->EnvelopeC ? PSG->VolE : PSG->VolTable[PSG->Regs[AY_CVOL] ? PSG->Regs[AY_CVOL]*2+1 : 0];
197 PSG->PeriodE = ((PSG->Regs[AY_EFINE] + 256 * PSG->Regs[AY_ECOARSE])) * PSG->UpdateStep;
199 if (PSG->PeriodE == 0)
200 PSG->PeriodE = PSG->UpdateStep / 2;
202 PSG->CountE += PSG->PeriodE - old;
204 if (PSG->CountE <= 0)
230 The envelope counter on the AY-3-8910 has 16 steps. On the YM2149 it
231 has twice the steps, happening twice as fast. Since the end result is
232 just a smoother curve, we always use the YM2149 behaviour.
234 PSG->Regs[AY_ESHAPE] &= 0x0F;
235 PSG->Attack = (PSG->Regs[AY_ESHAPE] & 0x04) ? 0x1F : 0x00;
237 if ((PSG->Regs[AY_ESHAPE] & 0x08) == 0)
239 /* if Continue = 0, map the shape to the equivalent one which has Continue = 1 */
241 PSG->Alternate = PSG->Attack;
245 PSG->Hold = PSG->Regs[AY_ESHAPE] & 0x01;
246 PSG->Alternate = PSG->Regs[AY_ESHAPE] & 0x02;
249 PSG->CountE = PSG->PeriodE;
250 PSG->CountEnv = 0x1F;
252 PSG->VolE = PSG->VolTable[PSG->CountEnv ^ PSG->Attack];
255 PSG->VolA = PSG->VolE;
258 PSG->VolB = PSG->VolE;
261 PSG->VolC = PSG->VolE;
264 if (PSG->Regs[AY_ENABLE] & 0x40)
266 // if (PSG->PortAwrite)
267 // (*PSG->PortAwrite)(0, PSG->Regs[AY_PORTA]);
269 // logerror("PC %04x: warning - write %02x to 8910 #%d Port A\n",activecpu_get_pc(),PSG->Regs[AY_PORTA],n);
273 // logerror("warning: write to 8910 #%d Port A set as input - ignored\n",n);
277 if (PSG->Regs[AY_ENABLE] & 0x80)
279 // if (PSG->PortBwrite)
280 // (*PSG->PortBwrite)(0, PSG->Regs[AY_PORTB]);
282 // logerror("PC %04x: warning - write %02x to 8910 #%d Port B\n",activecpu_get_pc(),PSG->Regs[AY_PORTB],n);
286 // logerror("warning: write to 8910 #%d Port B set as input - ignored\n",n);
293 // /length/ is the number of samples we require
294 // NB. This should be called at twice the 6522 IRQ rate or (eg) 60Hz if no IRQ.
295 void AY8910Update(int chip, int16 ** buffer, int length) // [TC: Removed static]
297 struct AY8910 * PSG = &AYPSG[chip];
298 int16 * buf1, * buf2, * buf3;
305 /* The 8910 has three outputs, each output is the mix of one of the three *
306 * tone generators and of the (single) noise generator. The two are mixed *
307 * BEFORE going into the DAC. The formula to mix each channel is: *
308 * (ToneOn | ToneDisable) & (NoiseOn | NoiseDisable). *
309 * Note that this means that if both tone and noise are disabled, the output *
310 * is 1, not 0, and can be modulated changing the volume. *
312 * If the channels are disabled, set their output to 1, and increase the *
313 * counter, if necessary, so they will not be inverted during this update. *
314 * Setting the output to 1 is necessary because a disabled channel is locked *
315 * into the ON state (see above); and it has no effect if the volume is 0. *
316 * If the volume is 0, increase the counter, but don't touch the output. */
317 if (PSG->Regs[AY_ENABLE] & 0x01)
319 if (PSG->CountA <= length * STEP)
320 PSG->CountA += length * STEP;
324 else if (PSG->Regs[AY_AVOL] == 0)
326 /* note that I do count += length, NOT count = length + 1. You might think *
327 * it's the same since the volume is 0, but doing the latter could cause *
328 * interferencies when the program is rapidly modulating the volume. */
329 if (PSG->CountA <= length * STEP)
330 PSG->CountA += length * STEP;
333 if (PSG->Regs[AY_ENABLE] & 0x02)
335 if (PSG->CountB <= length * STEP)
336 PSG->CountB += length * STEP;
340 else if (PSG->Regs[AY_BVOL] == 0)
342 if (PSG->CountB <= length * STEP)
343 PSG->CountB += length * STEP;
346 if (PSG->Regs[AY_ENABLE] & 0x04)
348 if (PSG->CountC <= length * STEP)
349 PSG->CountC += length * STEP;
353 else if (PSG->Regs[AY_CVOL] == 0)
355 if (PSG->CountC <= length * STEP)
356 PSG->CountC += length * STEP;
359 /* for the noise channel we must not touch OutputN - it's also not necessary *
360 * since we use outn. */
361 if ((PSG->Regs[AY_ENABLE] & 0x38) == 0x38) /* all off */
362 if (PSG->CountN <= length * STEP)
363 PSG->CountN += length * STEP;
365 outn = (PSG->OutputN | PSG->Regs[AY_ENABLE]);
370 int vola, volb, volc;
373 /* vola, volb and volc keep track of how long each square wave stays *
374 * in the 1 position during the sample period. */
375 vola = volb = volc = 0;
382 if (PSG->CountN < left)
383 nextevent = PSG->CountN;
392 PSG->CountA -= nextevent;
393 /* PeriodA is the half period of the square wave. Here, in each *
394 * loop I add PeriodA twice, so that at the end of the loop the *
395 * square wave is in the same status (0 or 1) it was at the start. *
396 * vola is also incremented by PeriodA, since the wave has been 1 *
397 * exactly half of the time, regardless of the initial position. *
398 * If we exit the loop in the middle, OutputA has to be inverted *
399 * and vola incremented only if the exit status of the square *
401 while (PSG->CountA <= 0)
403 PSG->CountA += PSG->PeriodA;
410 vola += PSG->PeriodA;
414 PSG->CountA += PSG->PeriodA;
415 vola += PSG->PeriodA;
423 PSG->CountA -= nextevent;
424 while (PSG->CountA <= 0)
426 PSG->CountA += PSG->PeriodA;
434 PSG->CountA += PSG->PeriodA;
443 PSG->CountB -= nextevent;
445 while (PSG->CountB <= 0)
447 PSG->CountB += PSG->PeriodB;
454 volb += PSG->PeriodB;
458 PSG->CountB += PSG->PeriodB;
459 volb += PSG->PeriodB;
467 PSG->CountB -= nextevent;
469 while (PSG->CountB <= 0)
471 PSG->CountB += PSG->PeriodB;
479 PSG->CountB += PSG->PeriodB;
488 PSG->CountC -= nextevent;
490 while (PSG->CountC <= 0)
492 PSG->CountC += PSG->PeriodC;
499 volc += PSG->PeriodC;
503 PSG->CountC += PSG->PeriodC;
504 volc += PSG->PeriodC;
512 PSG->CountC -= nextevent;
514 while (PSG->CountC <= 0)
516 PSG->CountC += PSG->PeriodC;
524 PSG->CountC += PSG->PeriodC;
528 PSG->CountN -= nextevent;
530 if (PSG->CountN <= 0)
532 /* Is noise output going to change? */
533 if ((PSG->RNG + 1) & 0x00002) /* (bit0^bit1)? */
535 PSG->OutputN = ~PSG->OutputN;
536 outn = (PSG->OutputN | PSG->Regs[AY_ENABLE]);
539 /* The Random Number Generator of the 8910 is a 17-bit shift *
540 * register. The input to the shift register is bit0 XOR bit3 *
541 * (bit0 is the output). This was verified on AY-3-8910 and *
544 * The following is a fast way to compute bit17 = bit0^bit3. *
545 * Instead of doing all the logic operations, we only check *
546 * bit0, relying on the fact that after three shifts of the *
547 * register, what now is bit3 will become bit0, and will *
548 * invert, if necessary, bit14, which previously was bit17. */
549 if (PSG->RNG & 0x00001)
550 PSG->RNG ^= 0x24000; /* This version is called the "Galois configuration". */
553 PSG->CountN += PSG->PeriodN;
560 /* update envelope */
561 if (PSG->Holding == 0)
565 if (PSG->CountE <= 0)
570 PSG->CountE += PSG->PeriodE;
572 while (PSG->CountE <= 0);
574 /* check envelope current position */
575 if (PSG->CountEnv < 0)
587 /* if CountEnv has looped an odd number of times (usually 1), *
588 * invert the output. */
589 if (PSG->Alternate && (PSG->CountEnv & 0x20))
592 PSG->CountEnv &= 0x1F;
596 PSG->VolE = PSG->VolTable[PSG->CountEnv ^ PSG->Attack];
599 PSG->VolA = PSG->VolE;
602 PSG->VolB = PSG->VolE;
605 PSG->VolC = PSG->VolE;
610 *(buf1++) = (vola * PSG->VolA) / STEP;
611 *(buf2++) = (volb * PSG->VolB) / STEP;
612 *(buf3++) = (volc * PSG->VolC) / STEP;
613 #else // [Tom's code...]
614 // Output PCM wave [-32768...32767] instead of MAME's voltage level [0...32767]
615 // - This allows for better s/w mixing
620 *(buf1++) = (vola * PSG->VolA) / STEP;
622 *(buf1++) = -(int)PSG->VolA;
630 *(buf2++) = (volb * PSG->VolB) / STEP;
632 *(buf2++) = -(int)PSG->VolB;
640 *(buf3++) = (volc * PSG->VolC) / STEP;
642 *(buf3++) = -(int)PSG->VolC;
652 static void AY8910_set_clock(int chip, int clock)
654 struct AY8910 * PSG = &AYPSG[chip];
656 /* The step clock for the tone and noise generators is the chip clock *
657 * divided by 8; for the envelope generator of the AY-3-8910, it is half *
658 * that much (clock/16), but the envelope of the YM2149 goes twice as *
659 * fast, therefore again clock/8. *
660 * Here we calculate the number of steps which happen during one sample *
661 * at the given sample rate. No. of events = sample rate / (clock/8). *
662 * STEP is a multiplier used to turn the fraction into a fixed point *
664 PSG->UpdateStep = (unsigned int)(((double)STEP * PSG->SampleRate * 8 + clock / 2) / clock); // [TC: unsigned int cast]
668 static void build_mixer_table(int chip)
670 struct AY8910 * PSG = &AYPSG[chip];
672 /* calculate the volume->voltage conversion table */
673 /* The AY-3-8910 has 16 levels, in a logarithmic scale (3dB per step) */
674 /* The YM2149 still has 16 levels for the tone generators, but 32 for */
675 /* the envelope generator (1.5dB per step). */
676 double out = MAX_OUTPUT;
678 for(int i=31; i>0; i--)
680 PSG->VolTable[i] = (unsigned int)(out + 0.5); /* round to nearest */ // [TC: unsigned int cast]
681 out /= 1.188502227; /* = 10 ^ (1.5/20) = 1.5dB */
684 PSG->VolTable[0] = 0;
688 void AY8910_reset(int chip)
691 struct AY8910 * PSG = &AYPSG[chip];
693 PSG->register_latch = 0;
699 PSG->lastEnable = -1; /* force a write */
701 for(i=0; i<AY_PORTA; i++)
702 _AYWriteReg(chip, i, 0); /* AYWriteReg() uses the timer system; we cannot */
703 /* call it at this time because the timer system */
704 /* has not been initialized. */
707 // This stuff looks like Tom's code, so let's streamline and un-MSHungarianize this shit:
710 void AY8910_InitAll(int clock, int sampleRate)
712 for(int chip=0; chip<MAX_8910; chip++)
714 struct AY8910 * PSG = &AYPSG[chip];
716 memset(PSG, 0, sizeof(struct AY8910));
717 PSG->SampleRate = sampleRate;
718 AY8910_set_clock(chip, clock);
719 build_mixer_table(chip);
723 void AY8910_InitClock(int clock)
725 for(int chip=0; chip<MAX_8910; chip++)
726 AY8910_set_clock(chip, clock);
729 uint8 * AY8910_GetRegsPtr(uint16 chipNum)
731 if (chipNum >= MAX_8910)
734 return &AYPSG[chipNum].Regs[0];