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: Removed MAME specific crap
32 #include <string.h> // for memset()
35 ///////////////////////////////////////////////////////////
36 // typedefs & dummy funcs to allow MAME code to compile:
38 //typedef UINT8 (*mem_read_handler)(UINT32);
39 //typedef void (*mem_write_handler)(UINT32, UINT8);
41 //static void logerror(char* psz, ...)
45 //static unsigned short activecpu_get_pc()
51 ///////////////////////////////////////////////////////////
53 #define MAX_OUTPUT 0x7fff
55 // See AY8910_set_clock() for definition of STEP
58 //This is not used at all...
59 //static int num = 0, ym_num = 0;
65 // mem_read_handler PortAread;
66 // mem_read_handler PortBread;
67 // mem_write_handler PortAwrite;
68 // mem_write_handler PortBwrite;
70 unsigned char Regs[16];
72 unsigned int UpdateStep;
73 int PeriodA,PeriodB,PeriodC,PeriodN,PeriodE;
74 int CountA,CountB,CountC,CountN,CountE;
75 unsigned int VolA,VolB,VolC,VolE;
76 unsigned char EnvelopeA,EnvelopeB,EnvelopeC;
77 unsigned char OutputA,OutputB,OutputC,OutputN;
79 unsigned char Hold,Alternate,Attack,Holding;
81 unsigned int VolTable[32];
86 #define AY_ACOARSE (1)
88 #define AY_BCOARSE (3)
90 #define AY_CCOARSE (5)
91 #define AY_NOISEPER (6)
97 #define AY_ECOARSE (12)
98 #define AY_ESHAPE (13)
100 #define AY_PORTA (14)
101 #define AY_PORTB (15)
104 static struct AY8910 AYPSG[MAX_8910]; /* array of PSG's */
108 void _AYWriteReg(int n, int r, int v)
110 struct AY8910 *PSG = &AYPSG[n];
116 /* A note about the period of tones, noise and envelope: for speed reasons,*/
117 /* we count down from the period to 0, but careful studies of the chip */
118 /* output prove that it instead counts up from 0 until the counter becomes */
119 /* greater or equal to the period. This is an important difference when the*/
120 /* program is rapidly changing the period to modulate the sound. */
121 /* To compensate for the difference, when the period is changed we adjust */
122 /* our internal counter. */
123 /* Also, note that period = 0 is the same as period = 1. This is mentioned */
124 /* in the YM2203 data sheets. However, this does NOT apply to the Envelope */
125 /* period. In that case, period = 0 is half as period = 1. */
130 PSG->Regs[AY_ACOARSE] &= 0x0f;
132 PSG->PeriodA = (PSG->Regs[AY_AFINE] + 256 * PSG->Regs[AY_ACOARSE]) * PSG->UpdateStep;
133 if (PSG->PeriodA == 0) PSG->PeriodA = PSG->UpdateStep;
134 PSG->CountA += PSG->PeriodA - old;
135 if (PSG->CountA <= 0) PSG->CountA = 1;
139 PSG->Regs[AY_BCOARSE] &= 0x0f;
141 PSG->PeriodB = (PSG->Regs[AY_BFINE] + 256 * PSG->Regs[AY_BCOARSE]) * PSG->UpdateStep;
142 if (PSG->PeriodB == 0) PSG->PeriodB = PSG->UpdateStep;
143 PSG->CountB += PSG->PeriodB - old;
144 if (PSG->CountB <= 0) PSG->CountB = 1;
148 PSG->Regs[AY_CCOARSE] &= 0x0f;
150 PSG->PeriodC = (PSG->Regs[AY_CFINE] + 256 * PSG->Regs[AY_CCOARSE]) * PSG->UpdateStep;
151 if (PSG->PeriodC == 0) PSG->PeriodC = PSG->UpdateStep;
152 PSG->CountC += PSG->PeriodC - old;
153 if (PSG->CountC <= 0) PSG->CountC = 1;
156 PSG->Regs[AY_NOISEPER] &= 0x1f;
158 PSG->PeriodN = PSG->Regs[AY_NOISEPER] * PSG->UpdateStep;
159 if (PSG->PeriodN == 0) PSG->PeriodN = PSG->UpdateStep;
160 PSG->CountN += PSG->PeriodN - old;
161 if (PSG->CountN <= 0) PSG->CountN = 1;
164 if ((PSG->lastEnable == -1) ||
165 ((PSG->lastEnable & 0x40) != (PSG->Regs[AY_ENABLE] & 0x40)))
167 /* write out 0xff if port set to input */
168 // if (PSG->PortAwrite)
169 // (*PSG->PortAwrite)(0, (UINT8) ((PSG->Regs[AY_ENABLE] & 0x40) ? PSG->Regs[AY_PORTA] : 0xff)); // [TC: UINT8 cast]
172 if ((PSG->lastEnable == -1) ||
173 ((PSG->lastEnable & 0x80) != (PSG->Regs[AY_ENABLE] & 0x80)))
175 /* write out 0xff if port set to input */
176 // if (PSG->PortBwrite)
177 // (*PSG->PortBwrite)(0, (UINT8) ((PSG->Regs[AY_ENABLE] & 0x80) ? PSG->Regs[AY_PORTB] : 0xff)); // [TC: UINT8 cast]
180 PSG->lastEnable = PSG->Regs[AY_ENABLE];
183 PSG->Regs[AY_AVOL] &= 0x1f;
184 PSG->EnvelopeA = PSG->Regs[AY_AVOL] & 0x10;
185 PSG->VolA = PSG->EnvelopeA ? PSG->VolE : PSG->VolTable[PSG->Regs[AY_AVOL] ? PSG->Regs[AY_AVOL]*2+1 : 0];
188 PSG->Regs[AY_BVOL] &= 0x1f;
189 PSG->EnvelopeB = PSG->Regs[AY_BVOL] & 0x10;
190 PSG->VolB = PSG->EnvelopeB ? PSG->VolE : PSG->VolTable[PSG->Regs[AY_BVOL] ? PSG->Regs[AY_BVOL]*2+1 : 0];
193 PSG->Regs[AY_CVOL] &= 0x1f;
194 PSG->EnvelopeC = PSG->Regs[AY_CVOL] & 0x10;
195 PSG->VolC = PSG->EnvelopeC ? PSG->VolE : PSG->VolTable[PSG->Regs[AY_CVOL] ? PSG->Regs[AY_CVOL]*2+1 : 0];
200 PSG->PeriodE = ((PSG->Regs[AY_EFINE] + 256 * PSG->Regs[AY_ECOARSE])) * PSG->UpdateStep;
201 if (PSG->PeriodE == 0) PSG->PeriodE = PSG->UpdateStep / 2;
202 PSG->CountE += PSG->PeriodE - old;
203 if (PSG->CountE <= 0) PSG->CountE = 1;
228 The envelope counter on the AY-3-8910 has 16 steps. On the YM2149 it
229 has twice the steps, happening twice as fast. Since the end result is
230 just a smoother curve, we always use the YM2149 behaviour.
232 PSG->Regs[AY_ESHAPE] &= 0x0f;
233 PSG->Attack = (PSG->Regs[AY_ESHAPE] & 0x04) ? 0x1f : 0x00;
234 if ((PSG->Regs[AY_ESHAPE] & 0x08) == 0)
236 /* if Continue = 0, map the shape to the equivalent one which has Continue = 1 */
238 PSG->Alternate = PSG->Attack;
242 PSG->Hold = PSG->Regs[AY_ESHAPE] & 0x01;
243 PSG->Alternate = PSG->Regs[AY_ESHAPE] & 0x02;
245 PSG->CountE = PSG->PeriodE;
246 PSG->CountEnv = 0x1f;
248 PSG->VolE = PSG->VolTable[PSG->CountEnv ^ PSG->Attack];
249 if (PSG->EnvelopeA) PSG->VolA = PSG->VolE;
250 if (PSG->EnvelopeB) PSG->VolB = PSG->VolE;
251 if (PSG->EnvelopeC) PSG->VolC = PSG->VolE;
254 if (PSG->Regs[AY_ENABLE] & 0x40)
256 // if (PSG->PortAwrite)
257 // (*PSG->PortAwrite)(0, PSG->Regs[AY_PORTA]);
259 // logerror("PC %04x: warning - write %02x to 8910 #%d Port A\n",activecpu_get_pc(),PSG->Regs[AY_PORTA],n);
263 // logerror("warning: write to 8910 #%d Port A set as input - ignored\n",n);
267 if (PSG->Regs[AY_ENABLE] & 0x80)
269 // if (PSG->PortBwrite)
270 // (*PSG->PortBwrite)(0, PSG->Regs[AY_PORTB]);
272 // logerror("PC %04x: warning - write %02x to 8910 #%d Port B\n",activecpu_get_pc(),PSG->Regs[AY_PORTB],n);
276 // logerror("warning: write to 8910 #%d Port B set as input - ignored\n",n);
283 // /length/ is the number of samples we require
284 // NB. This should be called at twice the 6522 IRQ rate or (eg) 60Hz if no IRQ.
285 void AY8910Update(int chip, int16 ** buffer, int length) // [TC: Removed static]
287 struct AY8910 *PSG = &AYPSG[chip];
288 INT16 *buf1,*buf2,*buf3;
296 /* The 8910 has three outputs, each output is the mix of one of the three */
297 /* tone generators and of the (single) noise generator. The two are mixed */
298 /* BEFORE going into the DAC. The formula to mix each channel is: */
299 /* (ToneOn | ToneDisable) & (NoiseOn | NoiseDisable). */
300 /* Note that this means that if both tone and noise are disabled, the output */
301 /* is 1, not 0, and can be modulated changing the volume. */
304 /* If the channels are disabled, set their output to 1, and increase the */
305 /* counter, if necessary, so they will not be inverted during this update. */
306 /* Setting the output to 1 is necessary because a disabled channel is locked */
307 /* into the ON state (see above); and it has no effect if the volume is 0. */
308 /* If the volume is 0, increase the counter, but don't touch the output. */
309 if (PSG->Regs[AY_ENABLE] & 0x01)
311 if (PSG->CountA <= length*STEP) PSG->CountA += length*STEP;
314 else if (PSG->Regs[AY_AVOL] == 0)
316 /* note that I do count += length, NOT count = length + 1. You might think */
317 /* it's the same since the volume is 0, but doing the latter could cause */
318 /* interferencies when the program is rapidly modulating the volume. */
319 if (PSG->CountA <= length*STEP) PSG->CountA += length*STEP;
321 if (PSG->Regs[AY_ENABLE] & 0x02)
323 if (PSG->CountB <= length*STEP) PSG->CountB += length*STEP;
326 else if (PSG->Regs[AY_BVOL] == 0)
328 if (PSG->CountB <= length*STEP) PSG->CountB += length*STEP;
330 if (PSG->Regs[AY_ENABLE] & 0x04)
332 if (PSG->CountC <= length*STEP) PSG->CountC += length*STEP;
335 else if (PSG->Regs[AY_CVOL] == 0)
337 if (PSG->CountC <= length*STEP) PSG->CountC += length*STEP;
340 /* for the noise channel we must not touch OutputN - it's also not necessary */
341 /* since we use outn. */
342 if ((PSG->Regs[AY_ENABLE] & 0x38) == 0x38) /* all off */
343 if (PSG->CountN <= length*STEP) PSG->CountN += length*STEP;
345 outn = (PSG->OutputN | PSG->Regs[AY_ENABLE]);
355 /* vola, volb and volc keep track of how long each square wave stays */
356 /* in the 1 position during the sample period. */
357 vola = volb = volc = 0;
365 if (PSG->CountN < left) nextevent = PSG->CountN;
366 else nextevent = left;
370 if (PSG->OutputA) vola += PSG->CountA;
371 PSG->CountA -= nextevent;
372 /* PeriodA is the half period of the square wave. Here, in each */
373 /* loop I add PeriodA twice, so that at the end of the loop the */
374 /* square wave is in the same status (0 or 1) it was at the start. */
375 /* vola is also incremented by PeriodA, since the wave has been 1 */
376 /* exactly half of the time, regardless of the initial position. */
377 /* If we exit the loop in the middle, OutputA has to be inverted */
378 /* and vola incremented only if the exit status of the square */
380 while (PSG->CountA <= 0)
382 PSG->CountA += PSG->PeriodA;
386 if (PSG->OutputA) vola += PSG->PeriodA;
389 PSG->CountA += PSG->PeriodA;
390 vola += PSG->PeriodA;
392 if (PSG->OutputA) vola -= PSG->CountA;
396 PSG->CountA -= nextevent;
397 while (PSG->CountA <= 0)
399 PSG->CountA += PSG->PeriodA;
405 PSG->CountA += PSG->PeriodA;
411 if (PSG->OutputB) volb += PSG->CountB;
412 PSG->CountB -= nextevent;
413 while (PSG->CountB <= 0)
415 PSG->CountB += PSG->PeriodB;
419 if (PSG->OutputB) volb += PSG->PeriodB;
422 PSG->CountB += PSG->PeriodB;
423 volb += PSG->PeriodB;
425 if (PSG->OutputB) volb -= PSG->CountB;
429 PSG->CountB -= nextevent;
430 while (PSG->CountB <= 0)
432 PSG->CountB += PSG->PeriodB;
438 PSG->CountB += PSG->PeriodB;
444 if (PSG->OutputC) volc += PSG->CountC;
445 PSG->CountC -= nextevent;
446 while (PSG->CountC <= 0)
448 PSG->CountC += PSG->PeriodC;
452 if (PSG->OutputC) volc += PSG->PeriodC;
455 PSG->CountC += PSG->PeriodC;
456 volc += PSG->PeriodC;
458 if (PSG->OutputC) volc -= PSG->CountC;
462 PSG->CountC -= nextevent;
463 while (PSG->CountC <= 0)
465 PSG->CountC += PSG->PeriodC;
471 PSG->CountC += PSG->PeriodC;
475 PSG->CountN -= nextevent;
476 if (PSG->CountN <= 0)
478 /* Is noise output going to change? */
479 if ((PSG->RNG + 1) & 2) /* (bit0^bit1)? */
481 PSG->OutputN = ~PSG->OutputN;
482 outn = (PSG->OutputN | PSG->Regs[AY_ENABLE]);
485 /* The Random Number Generator of the 8910 is a 17-bit shift */
486 /* register. The input to the shift register is bit0 XOR bit3 */
487 /* (bit0 is the output). This was verified on AY-3-8910 and YM2149 chips. */
489 /* The following is a fast way to compute bit17 = bit0^bit3. */
490 /* Instead of doing all the logic operations, we only check */
491 /* bit0, relying on the fact that after three shifts of the */
492 /* register, what now is bit3 will become bit0, and will */
493 /* invert, if necessary, bit14, which previously was bit17. */
494 if (PSG->RNG & 1) PSG->RNG ^= 0x24000; /* This version is called the "Galois configuration". */
496 PSG->CountN += PSG->PeriodN;
502 /* update envelope */
503 if (PSG->Holding == 0)
506 if (PSG->CountE <= 0)
511 PSG->CountE += PSG->PeriodE;
512 } while (PSG->CountE <= 0);
514 /* check envelope current position */
515 if (PSG->CountEnv < 0)
526 /* if CountEnv has looped an odd number of times (usually 1), */
527 /* invert the output. */
528 if (PSG->Alternate && (PSG->CountEnv & 0x20))
531 PSG->CountEnv &= 0x1f;
535 PSG->VolE = PSG->VolTable[PSG->CountEnv ^ PSG->Attack];
537 if (PSG->EnvelopeA) PSG->VolA = PSG->VolE;
538 if (PSG->EnvelopeB) PSG->VolB = PSG->VolE;
539 if (PSG->EnvelopeC) PSG->VolC = PSG->VolE;
544 *(buf1++) = (vola * PSG->VolA) / STEP;
545 *(buf2++) = (volb * PSG->VolB) / STEP;
546 *(buf3++) = (volc * PSG->VolC) / STEP;
548 // Output PCM wave [-32768...32767] instead of MAME's voltage level [0...32767]
549 // - This allows for better s/w mixing
554 *(buf1++) = (vola * PSG->VolA) / STEP;
556 *(buf1++) = - (int) PSG->VolA;
568 *(buf2++) = (volb * PSG->VolB) / STEP;
570 *(buf2++) = - (int) PSG->VolB;
582 *(buf3++) = (volc * PSG->VolC) / STEP;
584 *(buf3++) = - (int) PSG->VolC;
597 static void AY8910_set_clock(int chip,int clock)
599 struct AY8910 *PSG = &AYPSG[chip];
601 /* the step clock for the tone and noise generators is the chip clock */
602 /* divided by 8; for the envelope generator of the AY-3-8910, it is half */
603 /* that much (clock/16), but the envelope of the YM2149 goes twice as */
604 /* fast, therefore again clock/8. */
605 /* Here we calculate the number of steps which happen during one sample */
606 /* at the given sample rate. No. of events = sample rate / (clock/8). */
607 /* STEP is a multiplier used to turn the fraction into a fixed point */
609 PSG->UpdateStep = (unsigned int) (((double)STEP * PSG->SampleRate * 8 + clock/2) / clock); // [TC: unsigned int cast]
613 static void build_mixer_table(int chip)
615 struct AY8910 *PSG = &AYPSG[chip];
620 /* calculate the volume->voltage conversion table */
621 /* The AY-3-8910 has 16 levels, in a logarithmic scale (3dB per step) */
622 /* The YM2149 still has 16 levels for the tone generators, but 32 for */
623 /* the envelope generator (1.5dB per step). */
625 for (i = 31;i > 0;i--)
627 PSG->VolTable[i] = (unsigned int) (out + 0.5); /* round to nearest */ // [TC: unsigned int cast]
629 out /= 1.188502227; /* = 10 ^ (1.5/20) = 1.5dB */
631 PSG->VolTable[0] = 0;
635 void AY8910_reset(int chip)
638 struct AY8910 *PSG = &AYPSG[chip];
640 PSG->register_latch = 0;
646 PSG->lastEnable = -1; /* force a write */
647 for (i = 0;i < AY_PORTA;i++)
648 _AYWriteReg(chip,i,0); /* AYWriteReg() uses the timer system; we cannot */
649 /* call it at this time because the timer system */
650 /* has not been initialized. */
653 //-------------------------------------
655 void AY8910_InitAll(int nClock, int nSampleRate)
657 for(int nChip=0; nChip<MAX_8910; nChip++)
659 struct AY8910 *PSG = &AYPSG[nChip];
661 memset(PSG,0,sizeof(struct AY8910));
662 PSG->SampleRate = nSampleRate;
664 // PSG->PortAread = NULL;
665 // PSG->PortBread = NULL;
666 // PSG->PortAwrite = NULL;
667 // PSG->PortBwrite = NULL;
669 AY8910_set_clock(nChip, nClock);
671 build_mixer_table(nChip);
675 //-------------------------------------
677 void AY8910_InitClock(int nClock)
679 for(int nChip=0; nChip<MAX_8910; nChip++)
681 AY8910_set_clock(nChip, nClock);
685 //-------------------------------------
687 uint8 * AY8910_GetRegsPtr(uint16 nAyNum)
689 if(nAyNum >= MAX_8910)
692 return &AYPSG[nAyNum].Regs[0];