]> Shamusworld >> Repos - apple2/blob - src/ay8910.cpp
Set eol-style to native to keep things sane.
[apple2] / src / ay8910.cpp
1 /***************************************************************************
2
3   ay8910.cpp
4
5   Emulation of the AY-3-8910 / YM2149 sound chip.
6
7   Based on various code snippets by Ville Hallik, Michael Cuddy,
8   Tatsuyuki Satoh, Fabrice Frances, Nicola Salmoria.
9
10 ***************************************************************************/
11
12 // 
13 // From mame.txt (http://www.mame.net/readme.html)
14 // 
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
23 //    well.
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.
28 // 
29
30 // JLH: Removed MAME specific crap
31
32 #include <string.h>                                                             // for memset()
33 #include "ay8910.h"
34
35 ///////////////////////////////////////////////////////////
36 // typedefs & dummy funcs to allow MAME code to compile:
37 //
38 //typedef UINT8 (*mem_read_handler)(UINT32);
39 //typedef void (*mem_write_handler)(UINT32, UINT8);
40 //
41 //static void logerror(char* psz, ...)
42 //{
43 //}
44 //
45 //static unsigned short activecpu_get_pc()
46 //{
47 //      return 0;
48 //}
49 //
50 //
51 ///////////////////////////////////////////////////////////
52
53 #define MAX_OUTPUT 0x7fff
54
55 // See AY8910_set_clock() for definition of STEP
56 #define STEP 0x8000
57
58 //This is not used at all...
59 //static int num = 0, ym_num = 0;
60
61 struct AY8910
62 {
63         int Channel;
64         int SampleRate;
65 //      mem_read_handler PortAread;
66 //      mem_read_handler PortBread;
67 //      mem_write_handler PortAwrite;
68 //      mem_write_handler PortBwrite;
69         int register_latch;
70         unsigned char Regs[16];
71         int lastEnable;
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;
78         signed char CountEnv;
79         unsigned char Hold,Alternate,Attack,Holding;
80         int RNG;
81         unsigned int VolTable[32];
82 };
83
84 /* register id's */
85 #define AY_AFINE        (0)
86 #define AY_ACOARSE      (1)
87 #define AY_BFINE        (2)
88 #define AY_BCOARSE      (3)
89 #define AY_CFINE        (4)
90 #define AY_CCOARSE      (5)
91 #define AY_NOISEPER     (6)
92 #define AY_ENABLE       (7)
93 #define AY_AVOL         (8)
94 #define AY_BVOL         (9)
95 #define AY_CVOL         (10)
96 #define AY_EFINE        (11)
97 #define AY_ECOARSE      (12)
98 #define AY_ESHAPE       (13)
99
100 #define AY_PORTA        (14)
101 #define AY_PORTB        (15)
102
103
104 static struct AY8910 AYPSG[MAX_8910];           /* array of PSG's */
105
106
107
108 void _AYWriteReg(int n, int r, int v)
109 {
110         struct AY8910 *PSG = &AYPSG[n];
111         int old;
112
113
114         PSG->Regs[r] = v;
115
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. */
126         switch( r )
127         {
128         case AY_AFINE:
129         case AY_ACOARSE:
130                 PSG->Regs[AY_ACOARSE] &= 0x0f;
131                 old = PSG->PeriodA;
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;
136                 break;
137         case AY_BFINE:
138         case AY_BCOARSE:
139                 PSG->Regs[AY_BCOARSE] &= 0x0f;
140                 old = PSG->PeriodB;
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;
145                 break;
146         case AY_CFINE:
147         case AY_CCOARSE:
148                 PSG->Regs[AY_CCOARSE] &= 0x0f;
149                 old = PSG->PeriodC;
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;
154                 break;
155         case AY_NOISEPER:
156                 PSG->Regs[AY_NOISEPER] &= 0x1f;
157                 old = PSG->PeriodN;
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;
162                 break;
163         case AY_ENABLE:
164                 if ((PSG->lastEnable == -1) ||
165                     ((PSG->lastEnable & 0x40) != (PSG->Regs[AY_ENABLE] & 0x40)))
166                 {
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]
170                 }
171
172                 if ((PSG->lastEnable == -1) ||
173                     ((PSG->lastEnable & 0x80) != (PSG->Regs[AY_ENABLE] & 0x80)))
174                 {
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]
178                 }
179
180                 PSG->lastEnable = PSG->Regs[AY_ENABLE];
181                 break;
182         case AY_AVOL:
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];
186                 break;
187         case AY_BVOL:
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];
191                 break;
192         case AY_CVOL:
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];
196                 break;
197         case AY_EFINE:
198         case AY_ECOARSE:
199                 old = PSG->PeriodE;
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;
204                 break;
205         case AY_ESHAPE:
206                 /* envelope shapes:
207                 C AtAlH
208                 0 0 x x  \___
209
210                 0 1 x x  /___
211
212                 1 0 0 0  \\\\
213
214                 1 0 0 1  \___
215
216                 1 0 1 0  \/\/
217                           ___
218                 1 0 1 1  \
219
220                 1 1 0 0  ////
221                           ___
222                 1 1 0 1  /
223
224                 1 1 1 0  /\/\
225
226                 1 1 1 1  /___
227
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.
231                 */
232                 PSG->Regs[AY_ESHAPE] &= 0x0f;
233                 PSG->Attack = (PSG->Regs[AY_ESHAPE] & 0x04) ? 0x1f : 0x00;
234                 if ((PSG->Regs[AY_ESHAPE] & 0x08) == 0)
235                 {
236                         /* if Continue = 0, map the shape to the equivalent one which has Continue = 1 */
237                         PSG->Hold = 1;
238                         PSG->Alternate = PSG->Attack;
239                 }
240                 else
241                 {
242                         PSG->Hold = PSG->Regs[AY_ESHAPE] & 0x01;
243                         PSG->Alternate = PSG->Regs[AY_ESHAPE] & 0x02;
244                 }
245                 PSG->CountE = PSG->PeriodE;
246                 PSG->CountEnv = 0x1f;
247                 PSG->Holding = 0;
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;
252                 break;
253         case AY_PORTA:
254                 if (PSG->Regs[AY_ENABLE] & 0x40)
255                 {
256 //                      if (PSG->PortAwrite)
257 //                              (*PSG->PortAwrite)(0, PSG->Regs[AY_PORTA]);
258 //                      else
259 //                              logerror("PC %04x: warning - write %02x to 8910 #%d Port A\n",activecpu_get_pc(),PSG->Regs[AY_PORTA],n);
260                 }
261                 else
262                 {
263 //                      logerror("warning: write to 8910 #%d Port A set as input - ignored\n",n);
264                 }
265                 break;
266         case AY_PORTB:
267                 if (PSG->Regs[AY_ENABLE] & 0x80)
268                 {
269 //                      if (PSG->PortBwrite)
270 //                              (*PSG->PortBwrite)(0, PSG->Regs[AY_PORTB]);
271 //                      else
272 //                              logerror("PC %04x: warning - write %02x to 8910 #%d Port B\n",activecpu_get_pc(),PSG->Regs[AY_PORTB],n);
273                 }
274                 else
275                 {
276 //                      logerror("warning: write to 8910 #%d Port B set as input - ignored\n",n);
277                 }
278                 break;
279         }
280 }
281
282
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]
286 {
287         struct AY8910 *PSG = &AYPSG[chip];
288         INT16 *buf1,*buf2,*buf3;
289         int outn;
290
291         buf1 = buffer[0];
292         buf2 = buffer[1];
293         buf3 = buffer[2];
294
295
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. */
302
303
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)
310         {
311                 if (PSG->CountA <= length*STEP) PSG->CountA += length*STEP;
312                 PSG->OutputA = 1;
313         }
314         else if (PSG->Regs[AY_AVOL] == 0)
315         {
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;
320         }
321         if (PSG->Regs[AY_ENABLE] & 0x02)
322         {
323                 if (PSG->CountB <= length*STEP) PSG->CountB += length*STEP;
324                 PSG->OutputB = 1;
325         }
326         else if (PSG->Regs[AY_BVOL] == 0)
327         {
328                 if (PSG->CountB <= length*STEP) PSG->CountB += length*STEP;
329         }
330         if (PSG->Regs[AY_ENABLE] & 0x04)
331         {
332                 if (PSG->CountC <= length*STEP) PSG->CountC += length*STEP;
333                 PSG->OutputC = 1;
334         }
335         else if (PSG->Regs[AY_CVOL] == 0)
336         {
337                 if (PSG->CountC <= length*STEP) PSG->CountC += length*STEP;
338         }
339
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;
344
345         outn = (PSG->OutputN | PSG->Regs[AY_ENABLE]);
346
347
348         /* buffering loop */
349         while (length)
350         {
351                 int vola,volb,volc;
352                 int left;
353
354
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;
358
359                 left = STEP;
360                 do
361                 {
362                         int nextevent;
363
364
365                         if (PSG->CountN < left) nextevent = PSG->CountN;
366                         else nextevent = left;
367
368                         if (outn & 0x08)
369                         {
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 */
379                                 /* wave is 1. */
380                                 while (PSG->CountA <= 0)
381                                 {
382                                         PSG->CountA += PSG->PeriodA;
383                                         if (PSG->CountA > 0)
384                                         {
385                                                 PSG->OutputA ^= 1;
386                                                 if (PSG->OutputA) vola += PSG->PeriodA;
387                                                 break;
388                                         }
389                                         PSG->CountA += PSG->PeriodA;
390                                         vola += PSG->PeriodA;
391                                 }
392                                 if (PSG->OutputA) vola -= PSG->CountA;
393                         }
394                         else
395                         {
396                                 PSG->CountA -= nextevent;
397                                 while (PSG->CountA <= 0)
398                                 {
399                                         PSG->CountA += PSG->PeriodA;
400                                         if (PSG->CountA > 0)
401                                         {
402                                                 PSG->OutputA ^= 1;
403                                                 break;
404                                         }
405                                         PSG->CountA += PSG->PeriodA;
406                                 }
407                         }
408
409                         if (outn & 0x10)
410                         {
411                                 if (PSG->OutputB) volb += PSG->CountB;
412                                 PSG->CountB -= nextevent;
413                                 while (PSG->CountB <= 0)
414                                 {
415                                         PSG->CountB += PSG->PeriodB;
416                                         if (PSG->CountB > 0)
417                                         {
418                                                 PSG->OutputB ^= 1;
419                                                 if (PSG->OutputB) volb += PSG->PeriodB;
420                                                 break;
421                                         }
422                                         PSG->CountB += PSG->PeriodB;
423                                         volb += PSG->PeriodB;
424                                 }
425                                 if (PSG->OutputB) volb -= PSG->CountB;
426                         }
427                         else
428                         {
429                                 PSG->CountB -= nextevent;
430                                 while (PSG->CountB <= 0)
431                                 {
432                                         PSG->CountB += PSG->PeriodB;
433                                         if (PSG->CountB > 0)
434                                         {
435                                                 PSG->OutputB ^= 1;
436                                                 break;
437                                         }
438                                         PSG->CountB += PSG->PeriodB;
439                                 }
440                         }
441
442                         if (outn & 0x20)
443                         {
444                                 if (PSG->OutputC) volc += PSG->CountC;
445                                 PSG->CountC -= nextevent;
446                                 while (PSG->CountC <= 0)
447                                 {
448                                         PSG->CountC += PSG->PeriodC;
449                                         if (PSG->CountC > 0)
450                                         {
451                                                 PSG->OutputC ^= 1;
452                                                 if (PSG->OutputC) volc += PSG->PeriodC;
453                                                 break;
454                                         }
455                                         PSG->CountC += PSG->PeriodC;
456                                         volc += PSG->PeriodC;
457                                 }
458                                 if (PSG->OutputC) volc -= PSG->CountC;
459                         }
460                         else
461                         {
462                                 PSG->CountC -= nextevent;
463                                 while (PSG->CountC <= 0)
464                                 {
465                                         PSG->CountC += PSG->PeriodC;
466                                         if (PSG->CountC > 0)
467                                         {
468                                                 PSG->OutputC ^= 1;
469                                                 break;
470                                         }
471                                         PSG->CountC += PSG->PeriodC;
472                                 }
473                         }
474
475                         PSG->CountN -= nextevent;
476                         if (PSG->CountN <= 0)
477                         {
478                                 /* Is noise output going to change? */
479                                 if ((PSG->RNG + 1) & 2) /* (bit0^bit1)? */
480                                 {
481                                         PSG->OutputN = ~PSG->OutputN;
482                                         outn = (PSG->OutputN | PSG->Regs[AY_ENABLE]);
483                                 }
484
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. */
488
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". */
495                                 PSG->RNG >>= 1;
496                                 PSG->CountN += PSG->PeriodN;
497                         }
498
499                         left -= nextevent;
500                 } while (left > 0);
501
502                 /* update envelope */
503                 if (PSG->Holding == 0)
504                 {
505                         PSG->CountE -= STEP;
506                         if (PSG->CountE <= 0)
507                         {
508                                 do
509                                 {
510                                         PSG->CountEnv--;
511                                         PSG->CountE += PSG->PeriodE;
512                                 } while (PSG->CountE <= 0);
513
514                                 /* check envelope current position */
515                                 if (PSG->CountEnv < 0)
516                                 {
517                                         if (PSG->Hold)
518                                         {
519                                                 if (PSG->Alternate)
520                                                         PSG->Attack ^= 0x1f;
521                                                 PSG->Holding = 1;
522                                                 PSG->CountEnv = 0;
523                                         }
524                                         else
525                                         {
526                                                 /* if CountEnv has looped an odd number of times (usually 1), */
527                                                 /* invert the output. */
528                                                 if (PSG->Alternate && (PSG->CountEnv & 0x20))
529                                                         PSG->Attack ^= 0x1f;
530
531                                                 PSG->CountEnv &= 0x1f;
532                                         }
533                                 }
534
535                                 PSG->VolE = PSG->VolTable[PSG->CountEnv ^ PSG->Attack];
536                                 /* reload volume */
537                                 if (PSG->EnvelopeA) PSG->VolA = PSG->VolE;
538                                 if (PSG->EnvelopeB) PSG->VolB = PSG->VolE;
539                                 if (PSG->EnvelopeC) PSG->VolC = PSG->VolE;
540                         }
541                 }
542
543 #if 0
544                 *(buf1++) = (vola * PSG->VolA) / STEP;
545                 *(buf2++) = (volb * PSG->VolB) / STEP;
546                 *(buf3++) = (volc * PSG->VolC) / STEP;
547 #else
548                 // Output PCM wave [-32768...32767] instead of MAME's voltage level [0...32767]
549                 // - This allows for better s/w mixing
550
551                 if(PSG->VolA)
552                 {
553                         if(vola)
554                                 *(buf1++) = (vola * PSG->VolA) / STEP;
555                         else
556                                 *(buf1++) = - (int) PSG->VolA;
557                 }
558                 else
559                 {
560                         *(buf1++) = 0;
561                 }
562
563                 //
564
565                 if(PSG->VolB)
566                 {
567                         if(volb)
568                                 *(buf2++) = (volb * PSG->VolB) / STEP;
569                         else
570                                 *(buf2++) = - (int) PSG->VolB;
571                 }
572                 else
573                 {
574                         *(buf2++) = 0;
575                 }
576
577                 //
578
579                 if(PSG->VolC)
580                 {
581                         if(volc)
582                                 *(buf3++) = (volc * PSG->VolC) / STEP;
583                         else
584                                 *(buf3++) = - (int) PSG->VolC;
585                 }
586                 else
587                 {
588                         *(buf3++) = 0;
589                 }
590 #endif
591
592                 length--;
593         }
594 }
595
596
597 static void AY8910_set_clock(int chip,int clock)
598 {
599         struct AY8910 *PSG = &AYPSG[chip];
600
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     */
608         /* number.                                                               */
609         PSG->UpdateStep = (unsigned int) (((double)STEP * PSG->SampleRate * 8 + clock/2) / clock);      // [TC: unsigned int cast]
610 }
611
612
613 static void build_mixer_table(int chip)
614 {
615         struct AY8910 *PSG = &AYPSG[chip];
616         int i;
617         double out;
618
619
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). */
624         out = MAX_OUTPUT;
625         for (i = 31;i > 0;i--)
626         {
627                 PSG->VolTable[i] = (unsigned int) (out + 0.5);  /* round to nearest */  // [TC: unsigned int cast]
628
629                 out /= 1.188502227;     /* = 10 ^ (1.5/20) = 1.5dB */
630         }
631         PSG->VolTable[0] = 0;
632 }
633
634
635 void AY8910_reset(int chip)
636 {
637         int i;
638         struct AY8910 *PSG = &AYPSG[chip];
639
640         PSG->register_latch = 0;
641         PSG->RNG = 1;
642         PSG->OutputA = 0;
643         PSG->OutputB = 0;
644         PSG->OutputC = 0;
645         PSG->OutputN = 0xff;
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. */
651 }
652
653 //-------------------------------------
654
655 void AY8910_InitAll(int nClock, int nSampleRate)
656 {
657         for(int nChip=0; nChip<MAX_8910; nChip++)
658         {
659                 struct AY8910 *PSG = &AYPSG[nChip];
660
661                 memset(PSG,0,sizeof(struct AY8910));
662                 PSG->SampleRate = nSampleRate;
663
664 //              PSG->PortAread = NULL;
665 //              PSG->PortBread = NULL;
666 //              PSG->PortAwrite = NULL;
667 //              PSG->PortBwrite = NULL;
668
669                 AY8910_set_clock(nChip, nClock);
670
671                 build_mixer_table(nChip);
672         }
673 }
674
675 //-------------------------------------
676
677 void AY8910_InitClock(int nClock)
678 {
679         for(int nChip=0; nChip<MAX_8910; nChip++)
680         {
681                 AY8910_set_clock(nChip, nClock);
682         }
683 }
684
685 //-------------------------------------
686
687 uint8 * AY8910_GetRegsPtr(uint16 nAyNum)
688 {
689         if(nAyNum >= MAX_8910)
690                 return NULL;
691
692         return &AYPSG[nAyNum].Regs[0];
693 }