2 // Virtual AY-3-8910 Emulator
5 // (C) 2018 Underground Software
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. :-)
16 #include <string.h> // for memset()
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 };
26 // Class variable instantiation/initialization
27 float VAY_3_8910::maxVolume = 8192.0f;
28 float VAY_3_8910::normalizedVolume[16];// = {};
31 VAY_3_8910::VAY_3_8910()
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).
39 for(int i=15; i>=0; i--)
41 normalizedVolume[i] = level;
42 level /= 1.4125375446228; // 10.0 ^ (3.0 / 20.0) = 3 dB
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
50 float vol0 = normalizedVolume[0];
51 float vol15 = normalizedVolume[15] - vol0;
53 for(int i=0; i<16; i++)
54 normalizedVolume[i] = (normalizedVolume[i] - vol0) / vol15;
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));
65 void VAY_3_8910::Reset(void)
67 memset(this, 0, sizeof(struct VAY_3_8910));
68 prng = 1; // Set correct PRNG seed
72 void VAY_3_8910::WriteControl(uint8_t value)
74 if ((value & 0x04) == 0)
76 else if ((value & 0x03) == 0x03)
78 else if ((value & 0x03) == 0x02)
83 void VAY_3_8910::WriteData(uint8_t value)
89 void VAY_3_8910::SetRegister(void)
92 static char regname[16][32] = {
110 WriteLog("*** AY(%d) Reg: %s = $%02X\n", chipNum, regname[reg], value);
112 uint16_t value = (uint16_t)data;
117 // The square wave period is the passed in value times 16, so we handle
119 period[0] = (period[0] & 0xF000) | (value << 4);
122 period[0] = ((value & 0x0F) << 12) | (period[0] & 0xFF0);
125 period[1] = (period[1] & 0xF000) | (value << 4);
128 period[1] = ((value & 0x0F) << 12) | (period[1] & 0xFF0);
131 period[2] = (period[2] & 0xF000) | (value << 4);
134 period[2] = ((value & 0x0F) << 12) | (period[2] & 0xFF0);
137 // Like the square wave period, the value is the what's passed * 16.
138 noisePeriod = (value & 0x1F) << 4;
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);
149 volume[0] = value & 0x0F;
150 envEnable[0] = (value & 0x10 ? true : false);
155 volume[0] = (envAttack ? 0 : 15);
156 envDirection[0] = (envAttack ? 1 : -1);
160 volume[1] = value & 0x0F;
161 envEnable[1] = (value & 0x10 ? true : false);
166 volume[1] = (envAttack ? 0 : 15);
167 envDirection[1] = (envAttack ? 1 : -1);
171 volume[2] = value & 0x0F;
172 envEnable[2] = (value & 0x10 ? true : false);
177 volume[2] = (envAttack ? 0 : 15);
178 envDirection[2] = (envAttack ? 1 : -1);
182 // The envelope period is 256 times the passed in value
183 envPeriod = (envPeriod & 0xFF0000) | (value << 8);
186 envPeriod = (value << 16) | (envPeriod & 0xFF00);
189 envAttack = (value & 0x04 ? true : false);
190 envAlternate = (value & 0x02 ? true : false);
191 envHold = (value & 0x01 ? true : false);
193 // If the Continue bit is *not* set, the Alternate bit is forced to the
194 // Attack bit, and Hold is forced on.
197 envAlternate = envAttack;
201 // Reset all voice envelope counts...
202 for(int i=0; i<3; i++)
205 envDirection[i] = (envAttack ? 1 : -1);
207 // Only reset the volume if the envelope is enabled!
209 volume[i] = (envAttack ? 0 : 15);
217 // Generate one sample and quit
219 bool logAYInternal = false;
220 uint16_t VAY_3_8910::GetSample(void)
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;
229 int fullCycles = (int)exactCycles;
230 overflow += exactCycles - (double)fullCycles;
238 for(int i=0; i<fullCycles; i++)
240 for(int j=0; j<3; j++)
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))
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))
253 state[j] = !state[j];
257 // Envelope generator only runs if the corresponding voice flag is
263 // It's (EP / 16) because there are 16 volume steps in each EP.
264 if (envCount[j] > (envPeriod / 16))
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)
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];
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))
279 // Hold means we set the volume to (Alternate XOR
280 // Attack) and stay there after the Attack EP.
283 volume[j] = (envAttack != envAlternate ? 15: 0);
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.
293 envDirection[j] = -envDirection[j];
294 volume[j] += envDirection[j];
297 volume[j] = (envAttack ? 0 : 15);
304 // Noise generator (the PRNG) runs all the time:
307 if (noiseCount > noisePeriod)
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.
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.
323 // This version is called the "Galois configuration".
325 // The noise wave *toggles* when a one shows up in bit0...
326 noiseState = !noiseState;
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++)
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);
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])
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]);