Debian Codegraph @

src/engine/platform/sound/ay8910.cpp

raw
1 // license:BSD-3-Clause
2 // copyright-holders:Couriersud
3 /*
4 * Couriersud, July 2014:
5 *
6 * This documents recent work on the AY8910. A YM2149 is now on it's way from
7 * Hong Kong as well.
8 *
9 * TODO:
10 *
11 * - Create a true sound device nAY8910 driver.
12 * - implement approach outlined below in this driver.
13 *
14 * For years I had a AY8910 in my drawer. Arduinos were around as well.
15 * Using the approach documented in this blog post
16 * http://www.986-studio.com/2014/05/18/another-ay-entry/#more-476
17 * I measured the output voltages using a Extech 520.
18 *
19 * Measurement Setup
20 *
21 * Laptop <--> Arduino <---> AY8910
22 *
23 * AY8910 Registers:
24 * 0x07: 3f
25 * 0x08: RV
26 * 0x09: RV
27 * 0x0A: RV
28 *
29 * Output was measured on Analog Output B with a resistor RD to
30 * ground.
31 *
32 * Measurement results:
33 *
34 * RD 983 9.830k 99.5k 1.001M open
35 *
36 * RV B B B B B
37 * 0 0.0000 0.0000 0.0001 0.0011 0.0616
38 * 1 0.0106 0.0998 0.6680 1.8150 2.7260
39 * 2 0.0150 0.1377 0.8320 1.9890 2.8120
40 * 3 0.0222 0.1960 1.0260 2.1740 2.9000
41 * 4 0.0320 0.2708 1.2320 2.3360 2.9760
42 * 5 0.0466 0.3719 1.4530 2.4880 3.0440
43 * 6 0.0665 0.4938 1.6680 2.6280 3.1130
44 * 7 0.1039 0.6910 1.9500 2.7900 3.1860
45 * 8 0.1237 0.7790 2.0500 2.8590 3.2340
46 * 9 0.1986 1.0660 2.3320 3.0090 3.3090
47 * 10 0.2803 1.3010 2.5050 3.0850 3.3380
48 * 11 0.3548 1.4740 2.6170 3.1340 3.3590
49 * 12 0.4702 1.6870 2.7340 3.1800 3.3730
50 * 13 0.6030 1.8870 2.8410 3.2300 3.4050
51 * 14 0.7530 2.0740 2.9280 3.2580 3.4170
52 * 15 0.9250 2.2510 3.0040 3.2940 3.4380
53 *
54 * Using an equivalent model approach with two resistors
55 *
56 * 5V
57 * |
58 * Z
59 * Z Resistor Value for RV
60 * Z
61 * |
62 * +---> Output signal
63 * |
64 * Z
65 * Z External RD
66 * Z
67 * |
68 * GND
69 *
70 * will NOT work out of the box since RV = RV(RD).
71 *
72 * The following approach will be used going forward based on die pictures
73 * of the AY8910 done by Dr. Stack van Hay:
74 *
75 *
76 * 5V
77 * _| D
78 * G | NMOS
79 * Vg ---|| Kn depends on volume selected
80 * |_ S Vs
81 * |
82 * |
83 * +---> VO Output signal
84 * |
85 * Z
86 * Z External RD
87 * Z
88 * |
89 * GND
90 *
91 * Whilst conducting, the FET operates in saturation mode:
92 *
93 * Id = Kn * (Vgs - Vth)^2
94 *
95 * Using Id = Vs / RD
96 *
97 * Vs = Kn * RD * (Vg - Vs - Vth)^2
98 *
99 * finally using Vg' = Vg - Vth
100 *
101 * Vs = Vg' + 1 / (2 * Kn * RD) - sqrt((Vg' + 1 / (2 * Kn * RD))^2 - Vg'^2)
102 *
103 * and finally
104 *
105 * VO = Vs
106 *
107 * and this can be used to re-Thenevin to 5V
108 *
109 * RVequiv = RD * ( 5V / VO - 1)
110 *
111 * The RV and Kn parameter are derived using least squares to match
112 * calculation results with measurements.
113 *
114 * FIXME:
115 * There is voltage of 60 mV measured with the EX520 (Ri ~ 10M). This may
116 * be induced by cutoff currents from the 15 FETs.
117 *
118 */
119
120
121 /***************************************************************************
122
123 ay8910.cpp
124
125 Emulation of the AY-3-8910 / YM2149 sound chip.
126
127 Based on various code snippets by Ville Hallik, Michael Cuddy,
128 Tatsuyuki Satoh, Fabrice Frances, Nicola Salmoria.
129
130 Mostly rewritten by couriersud in 2008
131
132 Public documentation:
133
134 - http://privatfrickler.de/blick-auf-den-chip-soundchip-general-instruments-ay-3-8910/
135 Die pictures of the AY8910
136
137 - US Patent 4933980
138
139 Games using ADSR: gyruss
140
141 A list with more games using ADSR can be found here:
142 http://mametesters.org/view.php?id=3043
143
144 TODO:
145 * Measure volume / envelope parameters for AY8930 expanded mode
146 * YM2610 & YM2608 will need a separate flag in their config structures
147 to distinguish between legacy and discrete mode.
148
149 The rewrite also introduces a generic model for the DAC. This model is
150 not perfect, but allows channel mixing based on a parametrized approach.
151 This model also allows to factor in different loads on individual channels.
152 If a better model is developped in the future or better measurements are
153 available, the driver should be easy to change. The model is described
154 later.
155
156 In order to not break hundreds of existing drivers by default the flag
157 AY8910_LEGACY_OUTPUT is used by drivers not changed to take into account the
158 new model. All outputs are normalized to the old output range (i.e. 0 .. 7ffff).
159 In the case of channel mixing, output range is 0...3 * 7fff.
160
161 The main difference between the AY-3-8910 and the YM2149 is, that the
162 AY-3-8910 datasheet mentions, that fixed volume level 0, which is set by
163 registers 8 to 10 is "channel off". The YM2149 mentions, that the generated
164 signal has a 2V DC component. This is confirmed by measurements. The approach
165 taken here is to assume the 2V DC offset for all outputs for the YM2149.
166 For the AY-3-8910, an offset is used if envelope is active for a channel.
167 This is backed by oscilloscope pictures from the datasheet. If a fixed volume
168 is set, i.e. envelope is disabled, the output voltage is set to 0V. Recordings
169 I found on the web for gyruss indicate, that the AY-3-8910 offset should
170 be around 0.2V. This will also make sound levels more compatible with
171 user observations for scramble.
172
173 The Model:
174 5V 5V
175 | |
176 / |
177 Volume Level x >---| Z
178 > Z Pullup Resistor RU
179 | Z
180 Z |
181 Rx Z |
182 Z |
183 | |
184 '-----+--------> >---+----> Output signal
185 | |
186 Z Z
187 Pulldown RD Z Z Load RL
188 Z Z
189 | |
190 GND GND
191
192 Each Volume level x will select a different resistor Rx. Measurements from fpgaarcade.com
193 where used to calibrate channel mixing for the YM2149. This was done using
194 a least square approach using a fixed RL of 1K Ohm.
195
196 For the AY measurements cited in e.g. openmsx as "Hacker Kay" for a single
197 channel were taken. These were normalized to 0 ... 65535 and consequently
198 adapted to an offset of 0.2V and a VPP of 1.3V. These measurements are in
199 line e.g. with the formula used by pcmenc for the volume: vol(i) = exp(i/2-7.5).
200
201 The following is documentation from the code moved here and amended to reflect
202 the changes done:
203
204 Careful studies of the chip output prove that the chip counts up from 0
205 until the counter becomes greater or equal to the period. This is an
206 important difference when the program is rapidly changing the period to
207 modulate the sound. This is worthwhile noting, since the datasheets
208 say, that the chip counts down.
209 Also, note that period = 0 is the same as period = 1. This is mentioned
210 in the YM2203 data sheets. However, this does NOT apply to the Envelope
211 period. In that case, period = 0 is half as period = 1.
212
213 Envelope shapes:
214 C AtAlH
215 0 0 x x \___
216 0 1 x x /___
217 1 0 0 0 \\\\
218 1 0 0 1 \___
219 1 0 1 0 \/\/
220 1 0 1 1 \```
221 1 1 0 0 ////
222 1 1 0 1 /```
223 1 1 1 0 /\/\
224 1 1 1 1 /___
225
226 The envelope counter on the AY-3-8910 has 16 steps. On the YM2149 it
227 has twice the steps, happening twice as fast.
228
229 ****************************************************************************
230
231 The bus control and chip selection signals of the AY PSGs and their
232 pin-compatible clones such as YM2149 are somewhat unconventional and
233 redundant, having been designed for compatibility with GI's CP1610
234 series of microprocessors. Much of the redundancy can be finessed by
235 tying BC2 to Vcc; AY-3-8913 and AY8930 do this internally.
236
237 /A9 A8 /CS BDIR BC2 BC1
238 AY-3-8910 24 25 n/a 27 28 29
239 AY-3-8912 n/a 17 n/a 18 19 20
240 AY-3-8913 22 23 24 2 n/a 3
241 ------------------------------------
242 Inactive NACT 0 0 0
243 Latch address ADAR 0 0 1
244 Inactive IAB 0 1 0
245 Read from PSG DTB 0 1 1
246 Latch address BAR 1 0 0
247 Inactive DW 1 0 1
248 Write to PSG DWS 1 1 0
249 Latch address INTAK 1 1 1
250
251 ***************************************************************************/
252
253 /**
254 AY-3-8910(A)/8914/8916/8917/8930/YM2149 (others?):
255 _______ _______
256 _| \__/ |_
257 [4] VSS (GND) -- |_|1 * 40|_| -- VCC (+5v)
258 _| |_
259 [5] NC |_|2 39|_| <- TEST 1 [1]
260 _| |_
261 ANALOG CHANNEL B <- |_|3 38|_| -> ANALOG CHANNEL C
262 _| |_
263 ANALOG CHANNEL A <- |_|4 37|_| <> DA0
264 _| |_
265 [5] NC |_|5 36|_| <> DA1
266 _| |_
267 IOB7 <> |_|6 35|_| <> DA2
268 _| |_
269 IOB6 <> |_|7 34|_| <> DA3
270 _| /---\ |_
271 IOB5 <> |_|8 \-/ | A 33|_| <> DA4
272 _| . . Y |_
273 IOB4 <> |_|9 |---| - S 32|_| <> DA5
274 _| ' ' 3 O |_
275 IOB3 <> |_|10 8 - U 31|_| <> DA6
276 _| 3 8 N |_
277 IOB2 <> |_|11 0 9 D 30|_| <> DA7
278 _| 8 1 |_
279 IOB1 <> |_|12 0 29|_| <- BC1
280 _| P |_
281 IOB0 <> |_|13 28|_| <- BC2
282 _| |_
283 IOA7 <> |_|14 27|_| <- BDIR
284 _| |_ Prelim. DS: YM2149/8930:
285 IOA6 <> |_|15 26|_| <- TEST 2 [2,3] CS2 /SEL
286 _| |_
287 IOA5 <> |_|16 25|_| <- A8 [3] CS1
288 _| |_
289 IOA4 <> |_|17 24|_| <- /A9 [3] /CS0
290 _| |_
291 IOA3 <> |_|18 23|_| <- /RESET
292 _| |_
293 IOA2 <> |_|19 22|_| == CLOCK
294 _| |_
295 IOA1 <> |_|20 21|_| <> IOA0
296 |__________________|
297
298 [1] Based on the decap, TEST 1 connects to the Envelope Generator and/or the
299 frequency divider somehow. Is this an input or an output?
300 [2] The TEST 2 input connects to the same selector as A8 and /A9 do on the 8910
301 and acts as just another active high enable like A8(pin 25).
302 The preliminary datasheet calls this pin CS2.
303 On the 8914, it performs the same above function but additionally ?disables?
304 the DA0-7 bus if pulled low/active. This additional function was removed
305 on the 8910.
306 This pin has an internal pullup.
307 On the AY8930 and YM2149, this pin is /SEL; if low, clock input is halved.
308 [3] These 3 pins are technically enables, and have pullups/pulldowns such that
309 if the pins are left floating, the chip remains enabled.
310 [4] On the AY-3-8910 the bond wire for the VSS pin goes to the substrate frame,
311 and then a separate bond wire connects it to a pad between pins 21 and 22.
312 [5] These pins lack internal bond wires entirely.
313
314
315 AY-3-8912(A):
316 _______ _______
317 _| \__/ |_
318 ANALOG CHANNEL C <- |_|1 * 28|_| <> DA0
319 _| |_
320 TEST 1 -> |_|2 27|_| <> DA1
321 _| |_
322 VCC (+5V) -- |_|3 26|_| <> DA2
323 _| |_
324 ANALOG CHANNEL B <- |_|4 25|_| <> DA3
325 _| /---\ |_
326 ANALOG CHANNEL A <- |_|5 \-/ | A 24|_| <> DA4
327 _| . . Y |_
328 VSS (GND) -- |_|6 |---| - S 23|_| <> DA5
329 _| ' ' 3 O |_
330 IOA7 <> |_|7 T 8 - U 22|_| <> DA6
331 _| A 3 8 N |_
332 IOA6 <> |_|8 I 1 9 D 21|_| <> DA7
333 _| W 1 1 |_
334 IOA5 <> |_|9 A C 2 20|_| <- BC1
335 _| N D |_
336 IOA4 <> |_|10 A 19|_| <- BC2
337 _| |_
338 IOA3 <> |_|11 18|_| <- BDIR
339 _| |_
340 IOA2 <> |_|12 17|_| <- A8
341 _| |_
342 IOA1 <> |_|13 16|_| <- /RESET
343 _| |_
344 IOA0 <> |_|14 15|_| == CLOCK
345 |__________________|
346
347
348 AY-3-8913:
349 _______ _______
350 _| \__/ |_
351 [1] VSS (GND) -- |_|1 * 24|_| <- /CHIP SELECT [2]
352 _| |_
353 BDIR -> |_|2 23|_| <- A8
354 _| |_
355 BC1 -> |_|3 22|_| <- /A9
356 _| /---\ |_
357 DA7 <> |_|4 \-/ | A 21|_| <- /RESET
358 _| . . Y |_
359 DA6 <> |_|5 |---| - 20|_| == CLOCK
360 _| ' ' 3 |_
361 DA5 <> |_|6 T 8 - 19|_| -- VSS (GND) [1]
362 _| A 3 8 |_
363 DA4 <> |_|7 I 3 9 18|_| -> ANALOG CHANNEL C
364 _| W 2 1 |_
365 DA3 <> |_|8 A 3 17|_| -> ANALOG CHANNEL A
366 _| N C |_
367 DA2 <> |_|9 - 16|_| NC(?)
368 _| A |_
369 DA1 <> |_|10 15|_| -> ANALOG CHANNEL B
370 _| |_
371 DA0 <> |_|11 14|_| ?? TEST IN [3]
372 _| |_
373 [4] TEST OUT ?? |_|12 13|_| -- VCC (+5V)
374 |__________________|
375
376 [1] Both of these are ground, they are probably connected together internally. Grounding either one should work.
377 [2] This is effectively another enable, much like TEST 2 is on the AY-3-8910 and 8914, but active low
378 [3] This is claimed to be equivalent to TEST 1 on the datasheet
379 [4] This is claimed to be equivalent to TEST 2 on the datasheet
380
381
382 GI AY-3-8910/A Programmable Sound Generator (PSG): 2 I/O ports
383 A7 thru A4 enable state for selecting a register can be changed with a
384 factory mask adjustment but default was 0000 for the "common" part shipped
385 (probably die "-100").
386 Pins 24, 25, and 26 are /A9, A8, and TEST2, which are an active low, high
387 and high chip enable, respectively.
388 AY-3-8910: Unused bits in registers have unknown behavior.
389 AY-3-8910A: Unused bits in registers have unknown behavior.
390 I/O current source/sink behavior is unknown.
391 AY-3-8910 die is labeled "90-32033" with a 1979 copyright and a "-100" die
392 code.
393 AY-3-8910A die is labeled "90-32128" with a 1983 copyright.
394 GI AY-3-8912/A: 1 I/O port
395 /A9 pin doesn't exist and is considered pulled low.
396 TEST2 pin doesn't exist and is considered pulled high.
397 IOB pins do not exist and have unknown behavior if driven high/low and read
398 back.
399 A7 thru A4 enable state for selecting a register can be changed with a
400 factory mask adjustment but default was 0000 for the "common" part shipped
401 AY-3-8912: Unused bits in registers have unknown behavior.
402 AY-3-8912A: Unused bits in registers have unknown behavior.
403 I/O current source/sink behavior is unknown.
404 AY-3-8912 die is unknown.
405 AY-3-8912A or A/P die is unknown.
406 AY-3-8913: 0 I/O ports
407 BC2 pin doesn't exist and is considered pulled high.
408 IOA/B pins do not exist and have unknown behavior if driven high/low and read back.
409 A7 thru A4 enable state for selecting a register can be changed with a
410 factory mask adjustment but default was 0000 for the "common" part shipped
411 AY-3-8913: Unused bits in registers have unknown behavior.
412 AY-3-8913 die is unknown.
413 GI AY-3-8914/A: 2 I/O ports
414 A7 thru A4 enable state for selecting a register can be changed with a
415 factory mask adjustment but was 0000 for the part shipped with the
416 Intellivision.
417 Pins 24, 25, and 26 are /A9, A8, and TEST2, which are an active low, high
418 and high chip enable, respectively.
419 TEST2 additionally ?disables? the data bus if pulled low.
420 The register mapping is different from the AY-3-8910, the AY-3-8914 register
421 mapping matches the "preliminary" 1978 AY-3-8910 datasheet.
422 The Envelope/Volume control register is 6 bits wide instead of 5 bits, and
423 the additional bit combines with the M bit to form a bit pair C0 and C1,
424 which shift the volume output of the Envelope generator right by 0, 1 or 2
425 bits on a given channel, or allow the low 4 bits to drive the channel
426 volume.
427 AY-3-8914: Unused bits in registers have unknown behavior.
428 AY-3-8914A: Unused bits in registers have unknown behavior.
429 I/O current source/sink behavior is unknown.
430 AY-3-8914 die is labeled "90-32022" with a 1978 copyright.
431 AY-3-8914A die is unknown.
432 GI AY-3-8916: 2 I/O ports
433 A7 thru A4 enable state for selecting a register can be changed with a
434 factory mask adjustment; its mask is unknown. This chip was shipped
435 with certain later Intellivision II systems.
436 Pins 24, 25, and 26 are /A9, /A8(!), and TEST2, which are an active low,
437 low(!) and high chip enable, respectively.
438 NOTE: the /A8 enable polarity may be mixed up with AY-3-8917 below.
439 AY-3-8916: Unused bits in registers have unknown behavior.
440 I/O current source/sink behavior is unknown.
441 AY-3-8916 die is unknown.
442 GI AY-3-8917: 2 I/O ports
443 A7 thru A4 enable state for selecting a register can be changed with a
444 factory mask adjustment but was 1111 for the part shipped with the
445 Intellivision ECS module.
446 Pins 24, 25, and 26 are /A9, A8, and TEST2, which are an active low, high
447 and high chip enable, respectively.
448 NOTE: the A8 enable polarity may be mixed up with AY-3-8916 above.
449 AY-3-8917: Unused bits in registers have unknown behavior.
450 I/O current source/sink behavior is unknown.
451 AY-3-8917 die is unknown.
452 Microchip AY8930 Enhanced Programmable Sound Generator (EPSG): 2 I/O ports
453 BC2 pin exists but is always considered pulled high. The pin might have no
454 bond wire at all.
455 Pins 2 and 5 might be additional test pins rather than being NC.
456 A7 thru A4 enable state for selecting a register are 0000 for all? parts
457 shipped.
458 Pins 24 and 25 are /A9, A8 which are an active low and high chip enable.
459 Pin 26 is /SELECT which if driven low divides the input clock by 2.
460 Writing 0xAn or 0xBn to register 0x0D turns on extended mode, which enables
461 an additional 16 registers (banked using 0x0D bit 0), and clears the
462 contents of all of the registers except the high 3 bits of register 0x0D
463 (according to the datasheet).
464 If the AY8930's extended mode is enabled, it gains higher resolution
465 frequency and volume control, separate volume per-channel, and the duty
466 cycle can be adjusted for the 3 channels.
467 If the mode is not enabled, it behaves almost exactly like an AY-3-8910(A?),
468 barring the BC2 and /SELECT differences.
469 AY8930: Unused bits in registers have unknown behavior, but the datasheet
470 explicitly states that unused bits always read as 0.
471 I/O current source/sink behavior is unknown.
472 AY8930 die is unknown.
473 Yamaha YM2149 Software-Controlled Sound Generator (SSG): 2 I/O ports
474 A7 thru A4 enable state for selecting a register are 0000 for all? parts
475 shipped.
476 Pins 24 and 25 are /A9, A8 which are an active low and high chip enable.
477 Pin 26 is /SEL which if driven low divides the input clock by 2.
478 The YM2149 envelope register has 5 bits of resolution internally, allowing
479 for smoother volume ramping, though the register for setting its direct
480 value remains 4 bits wide.
481 YM2149: Unused bits in registers have unknown behavior.
482 I/O current source/sink behavior is unknown.
483 YM2149 die is unknown; only one die revision, 'G', has been observed
484 from Yamaha chip/datecode silkscreen surface markings.
485 Yamaha YM2203: 2 I/O ports
486 The pinout of this chip is completely different from the AY-3-8910.
487 The entire way this chip is accessed is completely different from the usual
488 AY-3-8910 selection of chips, so there is a /CS and a /RD and a /WR and
489 an A0 pin; The chip status can be read back by reading the register
490 select address.
491 The chip has a 3-channel, 4-op FM synthesis sound core in it, not discussed
492 in this source file.
493 The first 16 registers are the same(?) as the YM2149.
494 YM2203: Unused bits in registers have unknown behavior.
495 I/O current source/sink behavior is unknown.
496 YM2203 die is unknown; three die revisions, 'D', 'F' and 'H', have been
497 observed from Yamaha chip/datecode silkscreen surface markings. It is
498 unknown what behavioral differences exist between these revisions.
499 The 'D' revision only appears during the first year of production, 1984, on chips marked 'YM2203B'
500 The 'F' revision exists from 1984?-1991, chips are marked 'YM2203C'
501 The 'H' revision exists from 1991 onward, chips are marked 'YM2203C'
502 Yamaha YM3439: limited info: CMOS version of YM2149?
503 Yamaha YMZ284: limited info: 0 I/O port, different clock divider
504 The chip selection logic is again simplified here: pin 1 is /WR, pin 2 is
505 /CS and pin 3 is A0.
506 D0-D7 are conveniently all on one side of the 16-pin package.
507 Pin 8 is /IC (initial clear), with an internal pullup.
508 Yamaha YMZ294: limited info: 0 I/O port
509 Pinout is identical to YMZ284 except for two additions: pin 8 selects
510 between 4MHz (H) and 6MHz (L), while pin 10 is /TEST.
511 OKI M5255, Winbond WF19054, JFC 95101, File KC89C72, Toshiba T7766A : differences to be listed
512
513 AY8930 Expanded mode registers :
514 Bank Register Bits
515 A 0 xxxx xxxx Channel A Tone period fine tune
516 A 1 xxxx xxxx Channel A Tone period coarse tune
517 A 2 xxxx xxxx Channel B Tone period fine tune
518 A 3 xxxx xxxx Channel B Tone period coarse tune
519 A 4 xxxx xxxx Channel C Tone period fine tune
520 A 5 xxxx xxxx Channel C Tone period coarse tune
521 A 6 xxxx xxxx Noise period
522 A 7 x--- ---- I/O Port B input(0) / output(1)
523 -x-- ---- I/O Port A input(0) / output(1)
524 --x- ---- Channel C Noise enable(0) / disable(1)
525 ---x ---- Channel B Noise enable(0) / disable(1)
526 ---- x--- Channel A Noise enable(0) / disable(1)
527 ---- -x-- Channel C Tone enable(0) / disable(1)
528 ---- --x- Channel B Tone enable(0) / disable(1)
529 ---- ---x Channel A Tone enable(0) / disable(1)
530 A 8 --x- ---- Channel A Envelope mode
531 ---x xxxx Channel A Tone volume
532 A 9 --x- ---- Channel B Envelope mode
533 ---x xxxx Channel B Tone volume
534 A A --x- ---- Channel C Envelope mode
535 ---x xxxx Channel C Tone volume
536 A B xxxx xxxx Channel A Envelope period fine tune
537 A C xxxx xxxx Channel A Envelope period coarse tune
538 A D 101- ---- 101 = Expanded mode enable, other AY-3-8910A Compatiblity mode
539 ---0 ---- 0 for Register Bank A
540 ---- xxxx Channel A Envelope Shape/Cycle
541 A E xxxx xxxx 8 bit Parallel I/O on Port A
542 A F xxxx xxxx 8 bit Parallel I/O on Port B
543
544 B 0 xxxx xxxx Channel B Envelope period fine tune
545 B 1 xxxx xxxx Channel B Envelope period coarse tune
546 B 2 xxxx xxxx Channel C Envelope period fine tune
547 B 3 xxxx xxxx Channel C Envelope period coarse tune
548 B 4 ---- xxxx Channel B Envelope Shape/Cycle
549 B 5 ---- xxxx Channel C Envelope Shape/Cycle
550 B 6 ---- xxxx Channel A Duty Cycle
551 B 7 ---- xxxx Channel B Duty Cycle
552 B 8 ---- xxxx Channel C Duty Cycle
553 B 9 xxxx xxxx Noise "And" Mask
554 B A xxxx xxxx Noise "Or" Mask
555 B B Reserved (Read as 0)
556 B C Reserved (Read as 0)
557 B D 101- ---- 101 = Expanded mode enable, other AY-3-8910A Compatiblity mode
558 ---1 ---- 1 for Register Bank B
559 ---- xxxx Channel A Envelope Shape
560 B E Reserved (Read as 0)
561 B F Test (Function unknown)
562
563 Decaps:
564 AY-3-8914 - http://siliconpr0n.org/map/gi/ay-3-8914/mz_mit20x/
565 AY-3-8910 - http://privatfrickler.de/blick-auf-den-chip-soundchip-general-instruments-ay-3-8910/
566 AY-3-8910A - https://seanriddledecap.blogspot.com/2017/01/gi-ay-3-8910-ay-3-8910a-gi-8705-cba.html (TODO: update this link when it has its own page at seanriddle.com)
567
568 Links:
569 AY-3-8910 'preliminary' datasheet (which actually describes the AY-3-8914) from 1978:
570 http://spatula-city.org/~im14u2c/intv/gi_micro_programmable_tv_games/page_7_100.png
571 http://spatula-city.org/~im14u2c/intv/gi_micro_programmable_tv_games/page_7_101.png
572 http://spatula-city.org/~im14u2c/intv/gi_micro_programmable_tv_games/page_7_102.png
573 http://spatula-city.org/~im14u2c/intv/gi_micro_programmable_tv_games/page_7_103.png
574 http://spatula-city.org/~im14u2c/intv/gi_micro_programmable_tv_games/page_7_104.png
575 http://spatula-city.org/~im14u2c/intv/gi_micro_programmable_tv_games/page_7_105.png
576 AY-3-8910/8912 Feb 1979 manual: https://web.archive.org/web/20140217224114/http://dev-docs.atariforge.org/files/GI_AY-3-8910_Feb-1979.pdf
577 AY-3-8910/8912/8913 post-1983 manual: http://map.grauw.nl/resources/sound/generalinstrument_ay-3-8910.pdf or http://www.ym2149.com/ay8910.pdf
578 AY-8930 datasheet: http://www.ym2149.com/ay8930.pdf
579 YM2149 datasheet: http://www.ym2149.com/ym2149.pdf
580 YM2203 English datasheet: http://www.appleii-box.de/APPLE2/JonasCard/YM2203%20datasheet.pdf
581 YM2203 Japanese datasheet contents, translated: http://www.larwe.com/technical/chip_ym2203.html
582 */
583
584 // additional modifications by tildearrow, Eulous, cam900 and Grauw for furnace (particularly AY8930 emulation)
585
586 #include "ay8910.h"
587 #include <stdio.h>
588 #include <math.h>
589 #include <string.h>
590 #include <vector>
591
592 /*************************************
593 *
594 * constants
595 *
596 *************************************/
597
598 #define ENABLE_REGISTER_TEST (0) /* Enable preprogrammed registers */
599 #define LOG_IGNORED_WRITES (0)
600
601 static constexpr float MAX_OUTPUT = 1.0;
602
603 /*************************************
604 *
605 * Type definitions
606 *
607 *************************************/
608
609
610 /*************************************
611 *
612 * Static
613 *
614 *************************************/
615
616 // duty cycle used for AY8930 expanded mode
617 static const unsigned int duty_cycle[9] =
618 {
619 0x80000000, // 3.125 %
620 0xc0000000, // 6.25 %
621 0xf0000000, // 12.50 %
622 0xff000000, // 25.00 %
623 0xffff0000, // 50.00 %
624 0xffffff00, // 75.00 %
625 0xfffffff0, // 87.50 %
626 0xfffffffc, // 93.75 %
627 0xfffffffe // 96.875 %
628 };
629
630 static const ay8910_device::ay_ym_param ym2149_param =
631 {
632 630, 801,
633 16,
634 { 73770, 37586, 27458, 21451, 15864, 12371, 8922, 6796,
635 4763, 3521, 2403, 1737, 1123, 762, 438, 251 },
636 };
637
638 static const ay8910_device::ay_ym_param ym2149_param_env =
639 {
640 630, 801,
641 32,
642 { 103350, 73770, 52657, 37586, 32125, 27458, 24269, 21451,
643 18447, 15864, 14009, 12371, 10506, 8922, 7787, 6796,
644 5689, 4763, 4095, 3521, 2909, 2403, 2043, 1737,
645 1397, 1123, 925, 762, 578, 438, 332, 251 },
646 };
647
648 #if 0
649 /* RL = 1000, Hacker Kay normalized, 2.1V to 3.2V */
650 static const ay8910_device::ay_ym_param ay8910_param =
651 {
652 664, 913,
653 16,
654 { 85785, 34227, 26986, 20398, 14886, 10588, 7810, 4856,
655 4120, 2512, 1737, 1335, 1005, 747, 586, 451 },
656 };
657
658 /*
659 * RL = 3000, Hacker Kay normalized pattern, 1.5V to 2.8V
660 * These values correspond with guesses based on Gyruss schematics
661 * They work well with scramble as well.
662 */
663 static const ay8910_device::ay_ym_param ay8910_param =
664 {
665 930, 454,
666 16,
667 { 85066, 34179, 27027, 20603, 15046, 10724, 7922, 4935,
668 4189, 2557, 1772, 1363, 1028, 766, 602, 464 },
669 };
670
671 /*
672 * RL = 1000, Hacker Kay normalized pattern, 0.75V to 2.05V
673 * These values correspond with guesses based on Gyruss schematics
674 * They work well with scramble as well.
675 */
676 static const ay8910_device::ay_ym_param ay8910_param =
677 {
678 1371, 313,
679 16,
680 { 93399, 33289, 25808, 19285, 13940, 9846, 7237, 4493,
681 3814, 2337, 1629, 1263, 962, 727, 580, 458 },
682 };
683
684 /*
685 * RL = 1000, Hacker Kay normalized pattern, 0.2V to 1.5V
686 */
687 static const ay8910_device::ay_ym_param ay8910_param =
688 {
689 5806, 300,
690 16,
691 { 118996, 42698, 33105, 24770, 17925, 12678, 9331, 5807,
692 4936, 3038, 2129, 1658, 1271, 969, 781, 623 }
693 };
694 #endif
695
696 /*
697 * RL = 2000, Based on Matthew Westcott's measurements from Dec 2001.
698 * -------------------------------------------------------------------
699 *
700 * http://groups.google.com/group/comp.sys.sinclair/browse_thread/thread/fb3091da4c4caf26/d5959a800cda0b5e?lnk=gst&q=Matthew+Westcott#d5959a800cda0b5e
701 * After what Russell mentioned a couple of weeks back about the lack of
702 * publicised measurements of AY chip volumes - I've finally got round to
703 * making these readings, and I'm placing them in the public domain - so
704 * anyone's welcome to use them in emulators or anything else.
705
706 * To make the readings, I set up the chip to produce a constant voltage on
707 * channel C (setting bits 2 and 5 of register 6), and varied the amplitude
708 * (the low 4 bits of register 10). The voltages were measured between the
709 * channel C output (pin 1) and ground (pin 6).
710 *
711 * Level Voltage
712 * 0 1.147
713 * 1 1.162
714 * 2 1.169
715 * 3 1.178
716 * 4 1.192
717 * 5 1.213
718 * 6 1.238
719 * 7 1.299
720 * 8 1.336
721 * 9 1.457
722 * 10 1.573
723 * 11 1.707
724 * 12 1.882
725 * 13 2.06
726 * 14 2.32
727 * 15 2.58
728 * -------------------------------------------------------------------
729 *
730 * The ZX spectrum output circuit was modelled in SwitcherCAD and
731 * the resistor values below create the voltage levels above.
732 * RD was measured on a real chip to be 8m Ohm, RU was 0.8m Ohm.
733 */
734
735 static const ay8910_device::ay_ym_param ay8910_param =
736 {
737 800000, 8000000,
738 16,
739 { 15950, 15350, 15090, 14760, 14275, 13620, 12890, 11370,
740 10600, 8590, 7190, 5985, 4820, 3945, 3017, 2345 }
741 };
742
743 static const ay8910_device::mosfet_param ay8910_mosfet_param =
744 {
745 1.465385778,
746 4.9,
747 16,
748 {
749 0.00076,
750 0.80536,
751 1.13106,
752 1.65952,
753 2.42261,
754 3.60536,
755 5.34893,
756 8.96871,
757 10.97202,
758 19.32370,
759 29.01935,
760 38.82026,
761 55.50539,
762 78.44395,
763 109.49257,
764 153.72985,
765 }
766 };
767
768
769
770
771 /*************************************
772 *
773 * Inline
774 *
775 *************************************/
776
777 static inline void build_3D_table(double rl, const ay8910_device::ay_ym_param *par, const ay8910_device::ay_ym_param *par_env, int normalize, double factor, int zero_is_off, short *tab)
778 {
779 double min = 10.0, max = 0.0;
780
781 std::vector<double> temp(8*32*32*32, 0);
782
783 for (int e = 0; e < 8; e++)
784 {
785 const ay8910_device::ay_ym_param *par_ch1 = (e & 0x01) ? par_env : par;
786 const ay8910_device::ay_ym_param *par_ch2 = (e & 0x02) ? par_env : par;
787 const ay8910_device::ay_ym_param *par_ch3 = (e & 0x04) ? par_env : par;
788
789 for (int j1 = 0; j1 < par_ch1->res_count; j1++)
790 for (int j2 = 0; j2 < par_ch2->res_count; j2++)
791 for (int j3 = 0; j3 < par_ch3->res_count; j3++)
792 {
793 double n;
794 if (zero_is_off)
795 {
796 n = (j1 != 0 || (e & 0x01)) ? 1 : 0;
797 n += (j2 != 0 || (e & 0x02)) ? 1 : 0;
798 n += (j3 != 0 || (e & 0x04)) ? 1 : 0;
799 }
800 else
801 n = 3.0;
802
803 double rt = n / par->r_up + 3.0 / par->r_down + 1.0 / rl;
804 double rw = n / par->r_up;
805
806 rw += 1.0 / par_ch1->res[j1];
807 rt += 1.0 / par_ch1->res[j1];
808 rw += 1.0 / par_ch2->res[j2];
809 rt += 1.0 / par_ch2->res[j2];
810 rw += 1.0 / par_ch3->res[j3];
811 rt += 1.0 / par_ch3->res[j3];
812
813 int indx = (e << 15) | (j3 << 10) | (j2 << 5) | j1;
814 temp[indx] = rw / rt;
815 if (temp[indx] < min)
816 min = temp[indx];
817 if (temp[indx] > max)
818 max = temp[indx];
819 }
820 }
821
822 if (normalize)
823 {
824 for (int j = 0; j < 32*32*32*8; j++)
825 tab[j] = 8000 * MAX_OUTPUT * (((temp[j] - min)/(max-min))) * factor;
826 }
827 else
828 {
829 for (int j = 0; j < 32*32*32*8; j++)
830 tab[j] = 8000 * MAX_OUTPUT * temp[j];
831 }
832
833 /* for (e = 0;e<16;e++) printf("%d %d\n",e << 10, tab[e << 10]); */
834 }
835
836 static inline void build_single_table(double rl, const ay8910_device::ay_ym_param *par, int normalize, short *tab, int zero_is_off)
837 {
838 double rt;
839 double rw;
840 double temp[32], min = 10.0, max = 0.0;
841
842 for (int j = 0; j < par->res_count; j++)
843 {
844 rt = 1.0 / par->r_down + 1.0 / rl;
845
846 rw = 1.0 / par->res[j];
847 rt += 1.0 / par->res[j];
848
849 if (!(zero_is_off && j == 0))
850 {
851 rw += 1.0 / par->r_up;
852 rt += 1.0 / par->r_up;
853 }
854
855 temp[j] = rw / rt;
856 if (temp[j] < min)
857 min = temp[j];
858 if (temp[j] > max)
859 max = temp[j];
860 }
861 if (normalize)
862 {
863 for (int j = 0; j < par->res_count; j++)
864 tab[j] = 16384 * MAX_OUTPUT * (((temp[j] - min)/(max-min)) - 0.25) * 0.5;
865 }
866 else
867 {
868 for (int j = 0; j < par->res_count; j++)
869 tab[j] = 16384 * MAX_OUTPUT * temp[j];
870 }
871
872 }
873
874 static inline void build_mosfet_resistor_table(const ay8910_device::mosfet_param &par, const double rd, short *tab)
875 {
876 for (int j = 0; j < par.m_count; j++)
877 {
878 const double Vd = 5.0;
879 const double Vg = par.m_Vg - par.m_Vth;
880 const double kn = par.m_Kn[j] / 1.0e6;
881 const double p2 = 1.0 / (2.0 * kn * rd) + Vg;
882 const double Vs = p2 - sqrt(p2 * p2 - Vg * Vg);
883
884 const double res = rd * (Vd / Vs - 1.0);
885
886 tab[j] = res * 16384;
887 //printf("%d %f %10d\n", j, rd / (res + rd) * 5.0, tab[j]);
888 }
889 }
890
891
892 float ay8910_device::mix_3D()
893 {
894 int indx = 0;
895
896 for (int chan = 0; chan < NUM_CHANNELS; chan++)
897 {
898 tone_t *tone = &m_tone[chan];
899 if (tone_envelope(tone) != 0)
900 {
901 envelope_t *envelope = &m_envelope[get_envelope_chan(chan)];
902 unsigned int env_volume = envelope->volume;
903 unsigned int env_mask = (1 << (chan + 15));
904 if (m_feature & PSG_HAS_EXPANDED_MODE)
905 {
906 if (!is_expanded_mode())
907 {
908 env_volume >>= 1;
909 env_mask = 0;
910 }
911 }
912 if (m_feature & PSG_EXTENDED_ENVELOPE) // AY8914 Has a two bit tone_envelope field
913 {
914 indx |= env_mask | (m_vol_enabled[chan] ? ((env_volume >> (3-tone_envelope(tone))) << (chan*5)) : 0);
915 }
916 else
917 {
918 indx |= env_mask | (m_vol_enabled[chan] ? env_volume << (chan*5) : 0);
919 }
920 }
921 else
922 {
923 const unsigned int tone_mask = is_expanded_mode() ? (1 << (chan + 15)) : 0;
924 indx |= tone_mask | (m_vol_enabled[chan] ? tone_volume(tone) << (chan*5) : 0);
925 }
926 }
927 lastIndx=indx;
928 return m_vol3d_table[indx];
929 }
930
931 /*************************************
932 *
933 * Static functions
934 *
935 *************************************/
936
937 void ay8910_device::ay8910_write_reg(int r, int v)
938 {
939 if ((r & 0xf) == AY_EASHAPE) // shared register
940 r &= 0xf;
941
942 //if (r >= 11 && r <= 13) printf("%d %x %02x\n", PSG->index, r, v);
943 m_regs[r] = v;
944 unsigned char coarse;
945
946 switch(r)
947 {
948 case AY_AFINE:
949 case AY_ACOARSE:
950 coarse = m_regs[AY_ACOARSE] & (is_expanded_mode() ? 0xff : 0xf);
951 m_tone[0].set_period(m_regs[AY_AFINE], coarse);
952 break;
953 case AY_BFINE:
954 case AY_BCOARSE:
955 coarse = m_regs[AY_BCOARSE] & (is_expanded_mode() ? 0xff : 0xf);
956 m_tone[1].set_period(m_regs[AY_BFINE], coarse);
957 break;
958 case AY_CFINE:
959 case AY_CCOARSE:
960 coarse = m_regs[AY_CCOARSE] & (is_expanded_mode() ? 0xff : 0xf);
961 m_tone[2].set_period(m_regs[AY_CFINE], coarse);
962 break;
963 case AY_NOISEPER:
964 /* No action required */
965 break;
966 case AY_AVOL:
967 m_tone[0].set_volume(m_regs[AY_AVOL]);
968 break;
969 case AY_BVOL:
970 m_tone[1].set_volume(m_regs[AY_BVOL]);
971 break;
972 case AY_CVOL:
973 m_tone[2].set_volume(m_regs[AY_CVOL]);
974 break;
975 case AY_EACOARSE:
976 case AY_EAFINE:
977 m_envelope[0].set_period(m_regs[AY_EAFINE], m_regs[AY_EACOARSE]);
978 break;
979 case AY_ENABLE:
980 m_last_enable = m_regs[AY_ENABLE];
981 break;
982 case AY_EASHAPE:
983 if (m_feature & PSG_HAS_EXPANDED_MODE)
984 {
985 const unsigned char old_mode = m_mode;
986 m_mode = (v >> 4) & 0xf;
987 if (old_mode != m_mode)
988 {
989 if (((old_mode & 0xe) == 0xa) ^ ((m_mode & 0xe) == 0xa)) // AY8930 expanded mode
990 {
991 for (int i = 0; i < AY_EASHAPE; i++)
992 {
993 ay8910_write_reg(i, 0);
994 ay8910_write_reg(i + 0x10, 0);
995 }
996 }
997 }
998 }
999 m_envelope[0].set_shape(m_regs[AY_EASHAPE], m_env_step_mask);
1000 break;
1001 case AY_EBFINE:
1002 case AY_EBCOARSE:
1003 m_envelope[1].set_period(m_regs[AY_EBFINE], m_regs[AY_EBCOARSE]);
1004 break;
1005 case AY_ECFINE:
1006 case AY_ECCOARSE:
1007 m_envelope[2].set_period(m_regs[AY_ECFINE], m_regs[AY_ECCOARSE]);
1008 break;
1009 case AY_EBSHAPE:
1010 m_envelope[1].set_shape(m_regs[AY_EBSHAPE], m_env_step_mask);
1011 break;
1012 case AY_ECSHAPE:
1013 m_envelope[2].set_shape(m_regs[AY_ECSHAPE], m_env_step_mask);
1014 break;
1015 case AY_ADUTY:
1016 m_tone[0].set_duty(m_regs[AY_ADUTY]);
1017 break;
1018 case AY_BDUTY:
1019 m_tone[1].set_duty(m_regs[AY_BDUTY]);
1020 break;
1021 case AY_CDUTY:
1022 m_tone[2].set_duty(m_regs[AY_CDUTY]);
1023 break;
1024 case AY_NOISEAND:
1025 case AY_NOISEOR:
1026 // No action required
1027 break;
1028 default:
1029 m_regs[r] = 0; // reserved, set as 0
1030 break;
1031 }
1032 }
1033
1034 //-------------------------------------------------
1035 // sound_stream_update - handle a stream update
1036 //-------------------------------------------------
1037
1038 void ay8910_device::sound_stream_update(short* outputs, int advance)
1039 {
1040 tone_t *tone;
1041 envelope_t *envelope;
1042
1043 /* hack to prevent us from hanging when starting filtered outputs */
1044 if (!m_ready)
1045 {
1046 for (int chan = 0; chan < m_streams; chan++)
1047 outputs[chan]=0;
1048 }
1049
1050 /* The 8910 has three outputs, each output is the mix of one of the three */
1051 /* tone generators and of the (single) noise generator. The two are mixed */
1052 /* BEFORE going into the DAC. The formula to mix each channel is: */
1053 /* (ToneOn | ToneDisable) & (NoiseOn | NoiseDisable). */
1054 /* Note that this means that if both tone and noise are disabled, the output */
1055 /* is 1, not 0, and can be modulated changing the volume. */
1056
1057 /* loop? kill the loop and optimize! */
1058 for (int chan = 0; chan < NUM_CHANNELS; chan++)
1059 {
1060 tone = &m_tone[chan];
1061 const int period = std::max<int>(1, tone->period) * (m_step_mul << 1);
1062 tone->count += advance << (is_expanded_mode() ? 5 : ((m_feature & PSG_HAS_EXPANDED_MODE) ? 0 : 1));
1063 if (tone->count>=period) {
1064 tone->duty_cycle = (tone->duty_cycle - (tone->count/period)) & 0x1f;
1065 tone->output = is_expanded_mode() ? BIT(duty_cycle[tone_duty(tone)], tone->duty_cycle) : BIT(tone->duty_cycle, 0);
1066 tone->count = tone->count % period;
1067 }
1068 }
1069
1070 const int period_noise = (int)(noise_period()) * m_step_mul;
1071 m_count_noise+=advance;
1072 if (m_count_noise >= period_noise)
1073 {
1074 /* toggle the prescaler output. Noise is no different to
1075 * channels.
1076 */
1077 m_count_noise = 0;
1078 m_prescale_noise = (m_prescale_noise + 1) & ((m_feature & PSG_HAS_EXPANDED_MODE) ? 3 : 1);
1079
1080 if (is_expanded_mode()) // AY8930 noise generator rate is twice? compares as compatibility mode
1081 {
1082 // This is called "Noise value" on the docs, but is a counter whose period is determined by the LFSR.
1083 // Using AND/OR gates, specific periods can be "filtered" out.
1084 // A square wave can be generated through this behavior, which can be used for crude AM pulse width modulation.
1085
1086 // The period of the noise is determined by this value.
1087 // The least significant byte of the LFSR is bitwise ANDed with the AND mask, and then bitwise ORed with the OR mask.
1088 if ((++m_noise_value) >= (((unsigned char)(m_rng) & noise_and()) | noise_or())) // Clock the noise value.
1089 {
1090 m_noise_value = 0;
1091
1092 // When everything is finally said and done, a 1bit latch is flipped.
1093 // This is the final output of the noise, to be multiplied by the tone and envelope generators of the channel.
1094 m_noise_out ^= 1;
1095
1096 noise_rng_tick();
1097 }
1098 }
1099 else if (!m_prescale_noise)
1100 noise_rng_tick();
1101 }
1102
1103 for (int chan = 0; chan < NUM_CHANNELS; chan++)
1104 {
1105 tone = &m_tone[chan];
1106 m_vol_enabled[chan] = (tone->output | (unsigned char)tone_enable(chan)) & (noise_output() | (unsigned char)noise_enable(chan));
1107 }
1108
1109 /* update envelope */
1110 // who cares about env 1/2 on 8910
1111 for (int chan = 0; chan < (is_expanded_mode() ? NUM_CHANNELS : 1); chan++)
1112 {
1113 envelope = &m_envelope[chan];
1114 if (envelope->holding == 0)
1115 {
1116 const int period = std::max<int>(1, envelope->period) * m_env_step_mul;
1117 envelope->count += advance;
1118 if (envelope->count >= period)
1119 {
1120 envelope->count %= period;
1121 envelope->step--;
1122
1123 /* check envelope current position */
1124 if (envelope->step < 0)
1125 {
1126 if (envelope->hold)
1127 {
1128 if (envelope->alternate)
1129 envelope->attack ^= m_env_step_mask;
1130 envelope->holding = 1;
1131 envelope->step = 0;
1132 }
1133 else
1134 {
1135 /* if CountEnv has looped an odd number of times (usually 1), */
1136 /* invert the output. */
1137 if (envelope->alternate && (envelope->step & (m_env_step_mask + 1)))
1138 envelope->attack ^= m_env_step_mask;
1139
1140 envelope->step &= m_env_step_mask;
1141 }
1142 }
1143
1144 }
1145 }
1146 envelope->volume = (envelope->step ^ envelope->attack);
1147 }
1148
1149 if (m_streams == 3)
1150 {
1151 for (int chan = 0; chan < NUM_CHANNELS; chan++)
1152 {
1153 tone = &m_tone[chan];
1154 if (tone_envelope(tone) != 0)
1155 {
1156 envelope = &m_envelope[get_envelope_chan(chan)];
1157 unsigned int env_volume = envelope->volume;
1158 if (m_feature & PSG_HAS_EXPANDED_MODE)
1159 {
1160 if (!is_expanded_mode())
1161 {
1162 env_volume >>= 1;
1163 if (m_feature & PSG_EXTENDED_ENVELOPE) // AY8914 Has a two bit tone_envelope field
1164 outputs[chan]=m_vol_table[chan][m_vol_enabled[chan] ? env_volume >> (3-tone_envelope(tone)) : 0];
1165 else
1166 outputs[chan]=m_vol_table[chan][m_vol_enabled[chan] ? env_volume : 0];
1167 }
1168 else
1169 {
1170 if (m_feature & PSG_EXTENDED_ENVELOPE) // AY8914 Has a two bit tone_envelope field
1171 outputs[chan]=m_env_table[chan][m_vol_enabled[chan] ? env_volume >> (3-tone_envelope(tone)) : 0];
1172 else
1173 outputs[chan]=m_env_table[chan][m_vol_enabled[chan] ? env_volume : 0];
1174 }
1175 }
1176 else
1177 {
1178 if (m_feature & PSG_EXTENDED_ENVELOPE) // AY8914 Has a two bit tone_envelope field
1179 outputs[chan]=m_env_table[chan][m_vol_enabled[chan] ? env_volume >> (3-tone_envelope(tone)) : 0];
1180 else
1181 outputs[chan]=m_env_table[chan][m_vol_enabled[chan] ? env_volume : 0];
1182 }
1183 }
1184 else
1185 {
1186 if (is_expanded_mode())
1187 outputs[chan]=m_env_table[chan][m_vol_enabled[chan] ? tone_volume(tone) : 0];
1188 else
1189 outputs[chan]=m_vol_table[chan][m_vol_enabled[chan] ? tone_volume(tone) : 0];
1190 }
1191 }
1192 }
1193 else
1194 {
1195 outputs[0]=mix_3D();
1196 }
1197 }
1198
1199 void ay8910_device::build_mixer_table()
1200 {
1201 int normalize = 0;
1202
1203 if ((m_flags & AY8910_LEGACY_OUTPUT) != 0)
1204 {
1205 normalize = 1;
1206 }
1207
1208 if ((m_flags & AY8910_RESISTOR_OUTPUT) != 0)
1209 {
1210 for (int chan = 0; chan < NUM_CHANNELS; chan++)
1211 {
1212 build_mosfet_resistor_table(ay8910_mosfet_param, m_res_load[chan], m_vol_table[chan]);
1213 build_mosfet_resistor_table(ay8910_mosfet_param, m_res_load[chan], m_env_table[chan]);
1214 }
1215 }
1216 else if (m_streams == NUM_CHANNELS)
1217 {
1218 for (int chan = 0; chan < NUM_CHANNELS; chan++)
1219 {
1220 build_single_table(m_res_load[chan], m_par, normalize, m_vol_table[chan], m_zero_is_off);
1221 build_single_table(m_res_load[chan], m_par_env, normalize, m_env_table[chan], 0);
1222 }
1223 }
1224 /*
1225 * The previous implementation added all three channels up instead of averaging them.
1226 * The factor of 3 will force the same levels if normalizing is used.
1227 */
1228 else
1229 {
1230 build_3D_table(m_res_load[0], m_par, m_par_env, normalize, 3, m_zero_is_off, m_vol3d_table);
1231 }
1232 }
1233
1234 //-------------------------------------------------
1235 // device_start - device-specific startup
1236 //-------------------------------------------------
1237
1238 void ay8910_device::device_start()
1239 {
1240 if ((m_flags & AY8910_SINGLE_OUTPUT) != 0)
1241 {
1242 m_streams = 1;
1243 }
1244
1245 build_mixer_table();
1246 }
1247
1248
1249 void ay8910_device::ay8910_reset_ym()
1250 {
1251 m_active = false;
1252 m_register_latch = 0;
1253 m_rng = (m_feature & PSG_HAS_EXPANDED_MODE) ? 0x1ffff : 1;
1254 m_mode = 0; // ay-3-8910 compatible mode
1255 m_noise_value = 0;
1256 m_noise_out = 0;
1257 for (int chan = 0; chan < NUM_CHANNELS; chan++)
1258 {
1259 m_tone[chan].reset();
1260 m_envelope[chan].reset();
1261 }
1262 m_count_noise = 0;
1263 m_prescale_noise = 0;
1264 m_last_enable = -1; /* force a write */
1265 for (int i = 0; i < AY_PORTA; i++)
1266 ay8910_write_reg(i,0);
1267 m_ready = 1;
1268 #if ENABLE_REGISTER_TEST
1269 ay8910_write_reg(AY_AFINE, 0);
1270 ay8910_write_reg(AY_ACOARSE, 1);
1271 ay8910_write_reg(AY_BFINE, 0);
1272 ay8910_write_reg(AY_BCOARSE, 2);
1273 ay8910_write_reg(AY_CFINE, 0);
1274 ay8910_write_reg(AY_CCOARSE, 4);
1275 //#define AY_NOISEPER (6)
1276 ay8910_write_reg(AY_ENABLE, ~7);
1277 ay8910_write_reg(AY_AVOL, 10);
1278 ay8910_write_reg(AY_BVOL, 10);
1279 ay8910_write_reg(AY_CVOL, 10);
1280 //#define AY_EAFINE (11)
1281 //#define AY_EACOARSE (12)
1282 //#define AY_EASHAPE (13)
1283 #endif
1284 }
1285
1286 void ay8910_device::ay8910_write_ym(int addr, unsigned char data)
1287 {
1288 if (addr & 1)
1289 {
1290 if (m_active)
1291 {
1292 const unsigned char register_latch = m_register_latch + get_register_bank();
1293 /* Data port */
1294 if (m_register_latch == AY_EASHAPE || m_regs[register_latch] != data)
1295 {
1296 /* update the output buffer before changing the register */
1297 }
1298
1299 ay8910_write_reg(register_latch, data);
1300 }
1301 }
1302 else
1303 {
1304 m_active = (data >> 4) == 0; // mask programmed 4-bit code
1305 if (m_active)
1306 {
1307 /* Register port */
1308 m_register_latch = data & 0x0f;
1309 }
1310 }
1311 }
1312
1313 unsigned char ay8910_device::ay8910_read_ym()
1314 {
1315 unsigned char r = m_register_latch + get_register_bank();
1316
1317 if (!m_active) return 0xff; // high impedance
1318
1319 if ((r & 0xf) == AY_EASHAPE) // shared register
1320 r &= 0xf;
1321
1322 /* There are no state dependent register in the AY8910! */
1323
1324 /* Depending on chip type, unused bits in registers may or may not be accessible.
1325 Untested chips are assumed to regard them as 'ram'
1326 Tested and confirmed on hardware:
1327 - AY-3-8910: inaccessible bits (see masks below) read back as 0
1328 - AY-3-8914: same as 8910 except regs B,C,D (8,9,A below due to 8910->8914 remapping) are 0x3f
1329 - AY-3-8916/8917 (used on ECS INTV expansion): inaccessible bits mirror one of the i/o ports, needs further testing
1330 - YM2149: no anomaly
1331 */
1332 if (chip_type == AY8910) {
1333 const unsigned char mask[0x10]={
1334 0xff,0x0f,0xff,0x0f,0xff,0x0f,0x1f,0xff,0x1f,0x1f,0x1f,0xff,0xff,0x0f,0xff,0xff
1335 };
1336 return m_regs[r] & mask[r];
1337 }
1338 else if (chip_type == AY8914) {
1339 const unsigned char mask[0x10]={
1340 0xff,0x0f,0xff,0x0f,0xff,0x0f,0x1f,0xff,0x3f,0x3f,0x3f,0xff,0xff,0x0f,0xff,0xff
1341 };
1342 return m_regs[r] & mask[r];
1343 }
1344 else return m_regs[r];
1345 }
1346
1347 /*************************************
1348 *
1349 * Sound Interface
1350 *
1351 *************************************/
1352
1353
1354 //-------------------------------------------------
1355 // device_reset - device-specific reset
1356 //-------------------------------------------------
1357
1358 void ay8910_device::device_reset()
1359 {
1360 lastIndx=0;
1361 ay8910_reset_ym();
1362 }
1363
1364 /*************************************
1365 *
1366 * Read/Write Handlers
1367 *
1368 *************************************/
1369
1370 void ay8910_device::address_w(unsigned char data)
1371 {
1372 #if ENABLE_REGISTER_TEST
1373 return;
1374 #else
1375 ay8910_write_ym(0, data);
1376 #endif
1377 }
1378
1379 void ay8910_device::data_w(unsigned char data)
1380 {
1381 #if ENABLE_REGISTER_TEST
1382 return;
1383 #else
1384 ay8910_write_ym(1, data);
1385 #endif
1386 }
1387
1388 // here, BC1 is hooked up to A0 on the host and BC2 is hooked up to A1
1389 void ay8910_device::write_bc1_bc2(int offset, unsigned char data)
1390 {
1391 switch (offset & 3)
1392 {
1393 case 0: // latch address
1394 address_w(data);
1395 break;
1396 case 1: // inactive
1397 break;
1398 case 2: // write to psg
1399 data_w(data);
1400 break;
1401 case 3: // latch address
1402 address_w(data);
1403 break;
1404 }
1405 }
1406
1407 static const unsigned char mapping8914to8910[16] = { 0, 2, 4, 11, 1, 3, 5, 12, 7, 6, 13, 8, 9, 10, 14, 15 };
1408
1409 unsigned char ay8914_device::read(int offset)
1410 {
1411 unsigned char rv;
1412 address_w(mapping8914to8910[offset & 0xf]);
1413 rv = data_r();
1414 return rv;
1415 }
1416
1417 void ay8914_device::write(int offset, unsigned char data)
1418 {
1419 address_w(mapping8914to8910[offset & 0xf]);
1420 data_w(data & 0xff);
1421 }
1422
1423
1424
1425
1426
1427 ay8910_device::ay8910_device(unsigned int clock)
1428 : ay8910_device(AY8910, clock, PSG_TYPE_AY, 3, 2)
1429 {
1430 }
1431
1432 ay8910_device::ay8910_device(device_type type, unsigned int clock,
1433 psg_type_t psg_type, int streams, int ioports, int feature, bool clk_sel)
1434 : chip_type(type),
1435 m_type(psg_type),
1436 m_streams(streams),
1437 m_ready(0),
1438 m_active(false),
1439 m_register_latch(0),
1440 m_last_enable(0),
1441 m_prescale_noise(0),
1442 m_noise_value(0),
1443 m_count_noise(0),
1444 m_rng(0),
1445 m_noise_out(0),
1446 m_mode(0),
1447 m_env_step_mask((!(feature & PSG_HAS_EXPANDED_MODE)) && (psg_type == PSG_TYPE_AY) ? 0x0f : 0x1f),
1448 m_step_mul( ((feature & PSG_HAS_INTERNAL_DIVIDER) || ((feature & PSG_PIN26_IS_CLKSEL) && clk_sel)) ? 2 : 1),
1449 m_env_step_mul( ((feature & PSG_HAS_EXPANDED_MODE) || (psg_type == PSG_TYPE_AY)) ? (m_step_mul << 1) : m_step_mul),
1450 m_zero_is_off( (!(feature & PSG_HAS_EXPANDED_MODE)) && (psg_type == PSG_TYPE_AY) ? 1 : 0),
1451 m_par( (!(feature & PSG_HAS_EXPANDED_MODE)) && (psg_type == PSG_TYPE_AY) ? &ay8910_param : &ym2149_param),
1452 m_par_env( (!(feature & PSG_HAS_EXPANDED_MODE)) && (psg_type == PSG_TYPE_AY) ? &ay8910_param : &ym2149_param_env),
1453 m_flags(AY8910_LEGACY_OUTPUT | (((feature & PSG_PIN26_IS_CLKSEL) && clk_sel) ? YM2149_PIN26_LOW : 0)),
1454 m_feature(feature)
1455 {
1456 memset(&m_regs,0,sizeof(m_regs));
1457 memset(&m_tone,0,sizeof(m_tone));
1458 memset(&m_envelope,0,sizeof(m_envelope));
1459 memset(&m_vol_enabled,0,sizeof(m_vol_enabled));
1460 memset(&m_vol_table,0,sizeof(m_vol_table));
1461 memset(&m_env_table,0,sizeof(m_env_table));
1462 m_res_load[0] = m_res_load[1] = m_res_load[2] = 1000; //Default values for resistor loads
1463
1464 // TODO : measure ay8930 volume parameters (PSG_TYPE_YM for temporary 5 bit handling)
1465 set_type((m_feature & PSG_HAS_EXPANDED_MODE) ? PSG_TYPE_YM : psg_type, clk_sel);
1466 }
1467
1468 void ay8910_device::set_type(psg_type_t psg_type, bool clk_sel)
1469 {
1470 m_type = psg_type;
1471 if (psg_type == PSG_TYPE_AY)
1472 {
1473 m_env_step_mask = 0x0f;
1474 m_env_step_mul = is_clock_divided() ? 4 : 2;
1475 m_zero_is_off = 1;
1476 m_par = &ay8910_param;
1477 m_par_env = &ay8910_param;
1478 }
1479 else
1480 {
1481 m_env_step_mask = 0x1f;
1482 m_env_step_mul = is_clock_divided() ? 2 : 1;
1483 m_zero_is_off = 0;
1484 m_par = &ym2149_param;
1485 m_par_env = &ym2149_param_env;
1486 }
1487 if (m_feature & PSG_HAS_EXPANDED_MODE)
1488 m_env_step_mul <<= 1;
1489
1490 set_clock_sel(clk_sel);
1491 }
1492
1493
1494
1495 ay8912_device::ay8912_device(unsigned int clock)
1496 : ay8910_device(AY8912, clock, PSG_TYPE_AY, 3, 1)
1497 {
1498 }
1499
1500
1501
1502
1503 ay8913_device::ay8913_device(unsigned int clock)
1504 : ay8910_device(AY8913, clock, PSG_TYPE_AY, 3, 0)
1505 {
1506 }
1507
1508
1509
1510
1511 ay8914_device::ay8914_device(unsigned int clock)
1512 : ay8910_device(AY8914, clock, PSG_TYPE_AY, 3, 2, PSG_EXTENDED_ENVELOPE)
1513 {
1514 }
1515
1516
1517
1518
1519 ay8930_device::ay8930_device(unsigned int clock, bool clk_sel)
1520 : ay8910_device(AY8930, clock, PSG_TYPE_YM, 3, 2, PSG_PIN26_IS_CLKSEL | PSG_HAS_EXPANDED_MODE, clk_sel)
1521 {
1522 }
1523
1524
1525
1526
1527 ym2149_device::ym2149_device(unsigned int clock, bool clk_sel)
1528 : ay8910_device(YM2149, clock, PSG_TYPE_YM, 3, 2, PSG_PIN26_IS_CLKSEL, clk_sel)
1529 {
1530 }
1531
1532
1533
1534
1535 ym3439_device::ym3439_device(unsigned int clock, bool clk_sel)
1536 : ay8910_device(YM3439, clock, PSG_TYPE_YM, 3, 2, PSG_PIN26_IS_CLKSEL, clk_sel)
1537 {
1538 }
1539
1540
1541
1542
1543 ymz284_device::ymz284_device(unsigned int clock)
1544 : ay8910_device(YMZ284, clock, PSG_TYPE_YM, 1, 0)
1545 {
1546 }
1547
1548
1549
1550
1551 ymz294_device::ymz294_device(unsigned int clock)
1552 : ay8910_device(YMZ294, clock, PSG_TYPE_YM, 1, 0)
1553 {
1554 }
1555
1556
1557
1558
1559 sunsoft_5b_sound_device::sunsoft_5b_sound_device(unsigned int clock)
1560 : ay8910_device(SUNSOFT_5B_SOUND, clock, PSG_TYPE_YM, 1, 0, PSG_HAS_INTERNAL_DIVIDER)
1561 {
1562 }