]> Shamusworld >> Repos - apple2/blob - src/vay8910.cpp
Add documentation of the process for emulating an A2 hard drive.
[apple2] / src / vay8910.cpp
1 //
2 // Virtual AY-3-8910 Emulator
3 //
4 // by James Hammons
5 // (C) 2018 Underground Software
6 //
7 // This was written mainly from the General Instruments datasheet for the 8910
8 // part.  I would have used the one from MAME, but it was so poorly written and
9 // so utterly incomprehensible that I decided to start from scratch to see if I
10 // could do any better; and so here we are.  I *did* use a bit of code from
11 // MAME's AY-3-8910 RNG, as it was just too neat not to use.  :-)
12 //
13
14 #include "vay8910.h"
15
16 #include <string.h>                     // for memset()
17 #include "log.h"
18 #include "sound.h"
19
20
21 // AY-3-8910 register IDs
22 enum { AY_AFINE = 0, AY_ACOARSE, AY_BFINE, AY_BCOARSE, AY_CFINE, AY_CCOARSE,
23         AY_NOISEPER, AY_ENABLE, AY_AVOL, AY_BVOL, AY_CVOL, AY_EFINE, AY_ECOARSE,
24         AY_ESHAPE, AY_PORTA, AY_PORTB };
25
26 // Class variable instantiation/initialization
27 float VAY_3_8910::maxVolume = 8192.0f;
28 float VAY_3_8910::normalizedVolume[16];// = {};
29
30
31 VAY_3_8910::VAY_3_8910()
32 {
33         // Our normalized volume levels are from 0 to -48 dB, in 3 dB steps.
34         // N.B.: It's 3dB steps because those sound the best.  Dunno what it really
35         //       is, as nothing in the documentation tells you (it only says that
36         //       each channel's volume is normalized from 0 to 1.0V).
37         float level = 1.0f;
38
39         for(int i=15; i>=0; i--)
40         {
41                 normalizedVolume[i] = level;
42                 level /= 1.4125375446228;       // 10.0 ^ (3.0 / 20.0) = 3 dB
43         }
44
45         // In order to get a scale that goes from 0 to 1 smoothly, we renormalize
46         // our volumes so that volume[0] is actually 0, and volume[15] is 1.
47         // Basically, we're sliding the curve down the Y-axis so that volume[0]
48         // touches the X-axis, then stretching the result so that it fits into the
49         // interval (0, 1).
50         float vol0 = normalizedVolume[0];
51         float vol15 = normalizedVolume[15] - vol0;
52
53         for(int i=0; i<16; i++)
54                 normalizedVolume[i] = (normalizedVolume[i] - vol0) / vol15;
55
56 #if 0
57         WriteLog("\nRenormalized volume, level (max=%d):\n", (int)maxVolume);
58         for(int i=0; i<16; i++)
59                 WriteLog("%lf, %d\n", normalizedVolume[i], (int)(normalizedVolume[i] * maxVolume));
60         WriteLog("\n");
61 #endif
62 }
63
64
65 void VAY_3_8910::Reset(void)
66 {
67         memset(this, 0, sizeof(struct VAY_3_8910));
68         prng = 1;       // Set correct PRNG seed
69 }
70
71
72 void VAY_3_8910::WriteControl(uint8_t value)
73 {
74         if ((value & 0x04) == 0)
75                 Reset();
76         else if ((value & 0x03) == 0x03)
77                 regLatch = data;
78         else if ((value & 0x03) == 0x02)
79                 SetRegister();
80 }
81
82
83 void VAY_3_8910::WriteData(uint8_t value)
84 {
85         data = value;
86 }
87
88
89 void VAY_3_8910::SetRegister(void)
90 {
91 #if 0
92 static char regname[16][32] = {
93         "AY_AFINE   ",
94         "AY_ACOARSE ",
95         "AY_BFINE   ",
96         "AY_BCOARSE ",
97         "AY_CFINE   ",
98         "AY_CCOARSE ",
99         "AY_NOISEPER",
100         "AY_ENABLE  ",
101         "AY_AVOL    ",
102         "AY_BVOL    ",
103         "AY_CVOL    ",
104         "AY_EFINE   ",
105         "AY_ECOARSE ",
106         "AY_ESHAPE  ",
107         "AY_PORTA   ",
108         "AY_PORTB   "
109 };
110 WriteLog("*** AY(%d) Reg: %s = $%02X\n", chipNum, regname[reg], value);
111 #endif
112         uint16_t value = (uint16_t)data;
113
114         switch (regLatch)
115         {
116         case AY_AFINE:
117                 // The square wave period is the passed in value times 16, so we handle
118                 // that here.
119                 period[0] = (period[0] & 0xF000) | (value << 4);
120                 break;
121         case AY_ACOARSE:
122                 period[0] = ((value & 0x0F) << 12) | (period[0] & 0xFF0);
123                 break;
124         case AY_BFINE:
125                 period[1] = (period[1] & 0xF000) | (value << 4);
126                 break;
127         case AY_BCOARSE:
128                 period[1] = ((value & 0x0F) << 12) | (period[1] & 0xFF0);
129                 break;
130         case AY_CFINE:
131                 period[2] = (period[2] & 0xF000) | (value << 4);
132                 break;
133         case AY_CCOARSE:
134                 period[2] = ((value & 0x0F) << 12) | (period[2] & 0xFF0);
135                 break;
136         case AY_NOISEPER:
137                 // Like the square wave period, the value is the what's passed * 16.
138                 noisePeriod = (value & 0x1F) << 4;
139                 break;
140         case AY_ENABLE:
141                 toneEnable[0] = (value & 0x01 ? false : true);
142                 toneEnable[1] = (value & 0x02 ? false : true);
143                 toneEnable[2] = (value & 0x04 ? false : true);
144                 noiseEnable[0] = (value & 0x08 ? false : true);
145                 noiseEnable[1] = (value & 0x10 ? false : true);
146                 noiseEnable[2] = (value & 0x20 ? false : true);
147                 break;
148         case AY_AVOL:
149                 volume[0]    = value & 0x0F;
150                 envEnable[0] = (value & 0x10 ? true : false);
151
152                 if (envEnable[0])
153                 {
154                         envCount[0]     = 0;
155                         volume[0]       = (envAttack ? 0 : 15);
156                         envDirection[0] = (envAttack ? 1 : -1);
157                 }
158                 break;
159         case AY_BVOL:
160                 volume[1]    = value & 0x0F;
161                 envEnable[1] = (value & 0x10 ? true : false);
162
163                 if (envEnable[1])
164                 {
165                         envCount[1]     = 0;
166                         volume[1]       = (envAttack ? 0 : 15);
167                         envDirection[1] = (envAttack ? 1 : -1);
168                 }
169                 break;
170         case AY_CVOL:
171                 volume[2]    = value & 0x0F;
172                 envEnable[2] = (value & 0x10 ? true : false);
173
174                 if (envEnable[2])
175                 {
176                         envCount[2]     = 0;
177                         volume[2]       = (envAttack ? 0 : 15);
178                         envDirection[2] = (envAttack ? 1 : -1);
179                 }
180                 break;
181         case AY_EFINE:
182                 // The envelope period is 256 times the passed in value
183                 envPeriod = (envPeriod & 0xFF0000) | (value << 8);
184                 break;
185         case AY_ECOARSE:
186                 envPeriod = (value << 16) | (envPeriod & 0xFF00);
187                 break;
188         case AY_ESHAPE:
189                 envAttack    = (value & 0x04 ? true : false);
190                 envAlternate = (value & 0x02 ? true : false);
191                 envHold      = (value & 0x01 ? true : false);
192
193                 // If the Continue bit is *not* set, the Alternate bit is forced to the
194                 // Attack bit, and Hold is forced on.
195                 if (!(value & 0x08))
196                 {
197                         envAlternate = envAttack;
198                         envHold = true;
199                 }
200
201                 // Reset all voice envelope counts...
202                 for(int i=0; i<3; i++)
203                 {
204                         envCount[i]     = 0;
205                         envDirection[i] = (envAttack ? 1 : -1);
206
207                         // Only reset the volume if the envelope is enabled!
208                         if (envEnable[i])
209                                 volume[i] = (envAttack ? 0 : 15);
210                 }
211                 break;
212         }
213 }
214
215
216 //
217 // Generate one sample and quit
218 //
219 bool logAYInternal = false;
220 uint16_t VAY_3_8910::GetSample(void)
221 {
222         uint16_t sample = 0;
223
224         // Number of cycles per second to run the PSG is the 6502 clock rate
225         // divided by the host sample rate
226         const static double exactCycles = 1020484.32 / (double)SAMPLE_RATE;
227         static double overflow = 0;
228
229         int fullCycles = (int)exactCycles;
230         overflow += exactCycles - (double)fullCycles;
231
232         if (overflow >= 1.0)
233         {
234                 fullCycles++;
235                 overflow -= 1.0;
236         }
237
238         for(int i=0; i<fullCycles; i++)
239         {
240                 for(int j=0; j<3; j++)
241                 {
242                         // Tone generators only run if the corresponding voice is enabled.
243                         // N.B.: We also reject any period set that is less than 2.
244                         if (toneEnable[j] && (period[j] > 16))
245                         {
246                                 count[j]++;
247
248                                 // It's (period / 2) because one full period of a square wave
249                                 // is zero for half of its period and one for the other half!
250                                 if (count[j] > (period[j] / 2))
251                                 {
252                                         count[j] = 0;
253                                         state[j] = !state[j];
254                                 }
255                         }
256
257                         // Envelope generator only runs if the corresponding voice flag is
258                         // enabled.
259                         if (envEnable[j])
260                         {
261                                 envCount[j]++;
262
263                                 // It's (EP / 16) because there are 16 volume steps in each EP.
264                                 if (envCount[j] > (envPeriod / 16))
265                                 {
266                                         // Attack 0 = \, 1 = / (attack lasts one EP)
267                                         // Alternate = mirror envelope's last attack
268                                         // Hold = run 1 EP, hold at level (Alternate XOR Attack)
269                                         envCount[j] = 0;
270
271                                         // We've hit a point where we need to make a change to the
272                                         // envelope's volume, so do it:
273                                         volume[j] += envDirection[j];
274
275                                         // If we hit the end of the EP, change the state of the
276                                         // envelope according to the envelope's variables.
277                                         if ((volume[j] > 15) || (volume[j] < 0))
278                                         {
279                                                 // Hold means we set the volume to (Alternate XOR
280                                                 // Attack) and stay there after the Attack EP.
281                                                 if (envHold)
282                                                 {
283                                                         volume[j] = (envAttack != envAlternate ? 15: 0);
284                                                         envDirection[j] = 0;
285                                                 }
286                                                 else
287                                                 {
288                                                         // If the Alternate bit is set, we mirror the
289                                                         // Attack pattern; otherwise we reset it to the
290                                                         // whatever level was set by the Attack bit.
291                                                         if (envAlternate)
292                                                         {
293                                                                 envDirection[j] = -envDirection[j];
294                                                                 volume[j] += envDirection[j];
295                                                         }
296                                                         else
297                                                                 volume[j] = (envAttack ? 0 : 15);
298                                                 }
299                                         }
300                                 }
301                         }
302                 }
303
304                 // Noise generator (the PRNG) runs all the time:
305                 noiseCount++;
306
307                 if (noiseCount > noisePeriod)
308                 {
309                         noiseCount = 0;
310
311                         // The following is from MAME's AY-3-8910 code:
312                         // The Pseudo Random Number Generator of the 8910 is a 17-bit shift
313                         // register. The input to the shift register is bit0 XOR bit3 (bit0
314                         // is the output). This was verified on AY-3-8910 and YM2149 chips.
315
316                         // The following is a fast way to compute bit17 = bit0 ^ bit3.
317                         // Instead of doing all the logic operations, we only check bit0,
318                         // relying on the fact that after three shifts of the register,
319                         // what now is bit3 will become bit0, and will invert, if
320                         // necessary, bit14, which previously was bit17.
321                         if (prng & 0x00001)
322                         {
323                                 // This version is called the "Galois configuration".
324                                 prng ^= 0x24000;
325                                 // The noise wave *toggles* when a one shows up in bit0...
326                                 noiseState = !noiseState;
327                         }
328
329                         prng >>= 1;
330                 }
331         }
332
333         // We mix channels A-C here into one sample, because the Mockingboard just
334         // sums the output of the AY-3-8910 by tying their lines together.
335         // We also handle the various cases (of which there are four) of mixing
336         // pure tones and "noise" tones together.
337         for(int i=0; i<3; i++)
338         {
339                 // Set the volume level scaled by the maximum volume (which can be
340                 // altered outside of this module).
341                 int level = (int)(normalizedVolume[volume[i]] * maxVolume);
342
343                 if (toneEnable[i] && !noiseEnable[i])
344                         sample += (state[i] ? level : 0);
345                 else if (!toneEnable[i] && noiseEnable[i])
346                         sample += (noiseState ? level : 0);
347                 else if (toneEnable[i] && noiseEnable[i])
348                         sample += (state[i] & noiseState ? level : 0);
349                 else if (!toneEnable[i] && !noiseEnable[i])
350                         sample += level;
351         }
352
353         if (logAYInternal)
354         {
355                 WriteLog("    (%d) State A,B,C: %s %s %s, Sample: $%04X, P: $%X, $%X, $%X\n", id, (state[0] ? "1" : "0"), (state[1] ? "1" : "0"), (state[2] ? "1" : "0"), sample, period[0], period[1], period[2]);
356         }
357
358         return sample;
359 }
360