Debian Codegraph @

src/engine/platform/sound/ay8910.h

raw
1 // license:BSD-3-Clause
2 // copyright-holders:Couriersud
3 #ifndef MAME_SOUND_AY8910_H
4 #define MAME_SOUND_AY8910_H
5
6 #pragma once
7
8 #include <algorithm>
9
10 #define ALL_8910_CHANNELS -1
11
12 /* Internal resistance at Volume level 7. */
13
14 #define AY8910_INTERNAL_RESISTANCE (356)
15 #define YM2149_INTERNAL_RESISTANCE (353)
16
17 /*
18 * The following is used by all drivers not reviewed yet.
19 * This will like the old behavior, output between
20 * 0 and 7FFF
21 */
22 #define AY8910_LEGACY_OUTPUT (0x01)
23
24 /*
25 * Specifying the next define will simulate the special
26 * cross channel mixing if outputs are tied together.
27 * The driver will only provide one stream in this case.
28 */
29 #define AY8910_SINGLE_OUTPUT (0x02)
30
31 /*
32 * The following define is the default behavior.
33 * Output level 0 is 0V and 7ffff corresponds to 5V.
34 * Use this to specify that a discrete mixing stage
35 * follows.
36 */
37 #define AY8910_DISCRETE_OUTPUT (0x04)
38
39 /*
40 * The following define causes the driver to output
41 * resistor values. Intended to be used for
42 * netlist interfacing.
43 */
44
45 #define AY8910_RESISTOR_OUTPUT (0x08)
46
47 /*
48 * This define specifies the initial state of
49 * YM2149, YM3439, AY8930 pin 26 (SEL pin).
50 * By default it is set to high,
51 * compatible with AY8910.
52 */
53 /* TODO: make it controllable while it's running (used by any hw???) */
54 #define YM2149_PIN26_HIGH (0x00) /* or N/C */
55 #define YM2149_PIN26_LOW (0x10)
56
57 #define BIT(x,n) (((x)>>(n))&1)
58
59 enum device_type {
60 AY8910,
61 AY8912,
62 AY8913,
63 AY8914,
64 AY8930,
65 YM2149,
66 YM3439,
67 YMZ284,
68 YMZ294,
69 SUNSOFT_5B_SOUND
70 };
71
72
73 class ay8910_device
74 {
75 public:
76 enum psg_type_t
77 {
78 PSG_TYPE_AY,
79 PSG_TYPE_YM
80 };
81
82 enum config_t
83 {
84 PSG_DEFAULT = 0x0,
85 PSG_PIN26_IS_CLKSEL = 0x1,
86 PSG_HAS_INTERNAL_DIVIDER = 0x2,
87 PSG_EXTENDED_ENVELOPE = 0x4,
88 PSG_HAS_EXPANDED_MODE = 0x8
89 };
90
91 // construction/destruction
92 ay8910_device(unsigned int clock);
93
94 // configuration helpers
95 void set_flags(int flags) { m_flags = flags; }
96 void set_psg_type(psg_type_t psg_type) { set_type(psg_type, m_flags & YM2149_PIN26_LOW); }
97 void set_psg_type(psg_type_t psg_type, bool clk_sel) { set_type(psg_type, clk_sel); }
98 void set_resistors_load(int res_load0, int res_load1, int res_load2) { m_res_load[0] = res_load0; m_res_load[1] = res_load1; m_res_load[2] = res_load2; }
99
100 unsigned char data_r() { return ay8910_read_ym(); }
101 void address_w(unsigned char data);
102 void data_w(unsigned char data);
103
104 // /RES
105 void reset_w(unsigned char data = 0) { ay8910_reset_ym(); }
106
107 // Clock select pin
108 void set_clock_sel(bool clk_sel)
109 {
110 if (m_feature & PSG_PIN26_IS_CLKSEL)
111 {
112 if (clk_sel)
113 m_flags |= YM2149_PIN26_LOW;
114 else
115 m_flags &= ~YM2149_PIN26_LOW;
116
117 m_step_mul = is_clock_divided() ? 2 : 1;
118 m_env_step_mul = (!(m_feature & PSG_HAS_EXPANDED_MODE)) && (m_type == PSG_TYPE_AY) ? (m_step_mul << 1) : m_step_mul;
119 if (m_feature & PSG_HAS_EXPANDED_MODE)
120 m_env_step_mul <<= 1;
121 }
122 }
123
124 // use this when BC1 == A0; here, BC1=0 selects 'data' and BC1=1 selects 'latch address'
125 void data_address_w(int offset, unsigned char data) { ay8910_write_ym(~offset & 1, data); } // note that directly connecting BC1 to A0 puts data on 0 and address on 1
126
127 // use this when BC1 == !A0; here, BC1=0 selects 'latch address' and BC1=1 selects 'data'
128 void address_data_w(int offset, unsigned char data) { ay8910_write_ym(offset & 1, data); }
129
130 // bc1=a0, bc2=a1
131 void write_bc1_bc2(int offset, unsigned char data);
132
133 struct ay_ym_param
134 {
135 double r_up;
136 double r_down;
137 int res_count;
138 double res[32];
139 };
140
141 struct mosfet_param
142 {
143 double m_Vth;
144 double m_Vg;
145 int m_count;
146 double m_Kn[32];
147 };
148
149 int lastIndx;
150
151 // internal interface for PSG component of YM device
152 // FIXME: these should be private, but vector06 accesses them directly
153
154 ay8910_device(device_type type, unsigned int clock, psg_type_t psg_type, int streams, int ioports, int feature = PSG_DEFAULT, bool clk_sel = false);
155
156 // device-level overrides
157 void device_start();
158 void device_reset();
159
160 // sound stream update overrides
161 void sound_stream_update(short* outputs, int outLen);
162
163 void ay8910_write_ym(int addr, unsigned char data);
164 unsigned char ay8910_read_ym();
165 void ay8910_reset_ym();
166
167 public:
168 static constexpr int NUM_CHANNELS = 3;
169 device_type chip_type;
170
171 /* register id's */
172 enum
173 {
174 AY_AFINE = 0x00,
175 AY_ACOARSE = 0x01,
176 AY_BFINE = 0x02,
177 AY_BCOARSE = 0x03,
178 AY_CFINE = 0x04,
179 AY_CCOARSE = 0x05,
180 AY_NOISEPER = 0x06,
181 AY_ENABLE = 0x07,
182 AY_AVOL = 0x08,
183 AY_BVOL = 0x09,
184 AY_CVOL = 0x0a,
185 AY_EAFINE = 0x0b,
186 AY_EACOARSE = 0x0c,
187 AY_EASHAPE = 0x0d,
188 AY_PORTA = 0x0e,
189 AY_PORTB = 0x0f,
190 AY_EBFINE = 0x10,
191 AY_EBCOARSE = 0x11,
192 AY_ECFINE = 0x12,
193 AY_ECCOARSE = 0x13,
194 AY_EBSHAPE = 0x14,
195 AY_ECSHAPE = 0x15,
196 AY_ADUTY = 0x16,
197 AY_BDUTY = 0x17,
198 AY_CDUTY = 0x18,
199 AY_NOISEAND = 0x19,
200 AY_NOISEOR = 0x1a,
201 AY_TEST = 0x1f
202 };
203
204 // structs
205 struct tone_t
206 {
207 unsigned int period;
208 unsigned char volume;
209 unsigned char duty;
210 int count;
211 unsigned char duty_cycle;
212 unsigned char output;
213
214 void reset()
215 {
216 period = 1;
217 volume = 0;
218 duty = 0;
219 count = 0;
220 duty_cycle = 0;
221 output = 0;
222 }
223
224 void set_period(unsigned char fine, unsigned char coarse)
225 {
226 period = std::max<unsigned int>(1, fine | (coarse << 8));
227 }
228
229 void set_volume(unsigned char val)
230 {
231 volume = val;
232 }
233
234 void set_duty(unsigned char val)
235 {
236 duty = val;
237 }
238 };
239
240 struct envelope_t
241 {
242 unsigned int period;
243 int count;
244 signed char step;
245 unsigned int volume;
246 unsigned char hold, alternate, attack, holding;
247
248 void reset()
249 {
250 period = 1;
251 count = 0;
252 step = 0;
253 volume = 0;
254 hold = 0;
255 alternate = 0;
256 attack = 0;
257 holding = 0;
258 }
259
260 void set_period(unsigned char fine, unsigned char coarse)
261 {
262 period = std::max<unsigned int>(1, fine | (coarse << 8));
263 }
264
265 void set_shape(unsigned char shape, unsigned char mask)
266 {
267 attack = (shape & 0x04) ? mask : 0x00;
268 if ((shape & 0x08) == 0)
269 {
270 /* if Continue = 0, map the shape to the equivalent one which has Continue = 1 */
271 hold = 1;
272 alternate = attack;
273 }
274 else
275 {
276 hold = shape & 0x01;
277 alternate = shape & 0x02;
278 }
279 step = mask;
280 holding = 0;
281 volume = (step ^ attack);
282 }
283 };
284
285 inline void noise_rng_tick()
286 {
287 // The Random Number Generator of the 8910 is a 17-bit shift
288 // register. The input to the shift register is bit0 XOR bit3
289 // (bit0 is the output). This was verified on AY-3-8910 and YM2149 chips.
290
291 if (m_feature & PSG_HAS_EXPANDED_MODE) // AY8930 LFSR algorithm is slightly different, verified from manual
292 m_rng = (m_rng >> 1) | ((BIT(m_rng, 0) ^ BIT(m_rng, 2)) << 16);
293 else
294 m_rng = (m_rng >> 1) | ((BIT(m_rng, 0) ^ BIT(m_rng, 3)) << 16);
295 }
296
297 // inlines
298 inline bool tone_enable(int chan) { return BIT(m_regs[AY_ENABLE], chan); }
299 inline unsigned char tone_volume(tone_t *tone) { return tone->volume & (is_expanded_mode() ? 0x1f : 0x0f); }
300 inline unsigned char tone_envelope(tone_t *tone) { return (tone->volume >> (is_expanded_mode() ? 5 : 4)) & ((m_feature & PSG_EXTENDED_ENVELOPE) ? 3 : 1); }
301 inline unsigned char tone_duty(tone_t *tone) { return is_expanded_mode() ? (tone->duty & 0x8 ? 0x8 : (tone->duty & 0xf)) : 0x4; }
302 inline unsigned char get_envelope_chan(int chan) { return is_expanded_mode() ? chan : 0; }
303
304 inline bool noise_enable(int chan) { return BIT(m_regs[AY_ENABLE], 3 + chan); }
305 inline unsigned char noise_period() { return std::max<unsigned char>(1, is_expanded_mode() ? (m_regs[AY_NOISEPER] & 0xff) : (m_regs[AY_NOISEPER] & 0x1f)); }
306 inline unsigned char noise_output() { return is_expanded_mode() ? m_noise_out & 1 : m_rng & 1; }
307
308 inline bool is_expanded_mode() { return ((m_feature & PSG_HAS_EXPANDED_MODE) && ((m_mode & 0xe) == 0xa)); }
309 inline unsigned char get_register_bank() { return is_expanded_mode() ? (m_mode & 0x1) << 4 : 0; }
310
311 inline unsigned char noise_and() { return m_regs[AY_NOISEAND] & 0xff; }
312 inline unsigned char noise_or() { return m_regs[AY_NOISEOR] & 0xff; }
313
314 inline bool is_clock_divided() { return ((m_feature & PSG_HAS_INTERNAL_DIVIDER) || ((m_feature & PSG_PIN26_IS_CLKSEL) && (m_flags & YM2149_PIN26_LOW))); }
315
316 // internal helpers
317 void set_type(psg_type_t psg_type, bool clk_sel);
318 inline float mix_3D();
319 void ay8910_write_reg(int r, int v);
320 void build_mixer_table();
321
322 // internal state
323 psg_type_t m_type;
324 int m_streams;
325 int m_ready;
326 //sound_stream *m_channel;
327 bool m_active;
328 unsigned char m_register_latch;
329 unsigned char m_regs[16 * 2];
330 int m_last_enable;
331 tone_t m_tone[NUM_CHANNELS];
332 envelope_t m_envelope[NUM_CHANNELS];
333 unsigned char m_prescale_noise;
334 signed short m_noise_value;
335 signed short m_count_noise;
336 unsigned int m_rng;
337 unsigned char m_noise_out;
338 unsigned char m_mode;
339 unsigned char m_env_step_mask;
340 /* init parameters ... */
341 int m_step_mul;
342 int m_env_step_mul;
343 int m_zero_is_off;
344 unsigned char m_vol_enabled[NUM_CHANNELS];
345 const ay_ym_param *m_par;
346 const ay_ym_param *m_par_env;
347 short m_vol_table[NUM_CHANNELS][16];
348 short m_env_table[NUM_CHANNELS][32];
349 short m_vol3d_table[32*32*32*8];
350 int m_flags; /* Flags */
351 int m_feature; /* Chip specific features */
352 int m_res_load[3]; /* Load on channel in ohms */
353 };
354
355 class ay8912_device : public ay8910_device
356 {
357 public:
358 ay8912_device(unsigned int clock);
359 };
360
361 class ay8913_device : public ay8910_device
362 {
363 public:
364 ay8913_device(unsigned int clock);
365 };
366
367 class ay8914_device : public ay8910_device
368 {
369 public:
370 ay8914_device(unsigned int clock);
371
372 /* AY8914 handlers needed due to different register map */
373 unsigned char read(int offset);
374 void write(int offset, unsigned char data);
375 };
376
377 class ay8930_device : public ay8910_device
378 {
379 public:
380 ay8930_device(unsigned int clock, bool clk_sel = false);
381 };
382
383 class ym2149_device : public ay8910_device
384 {
385 public:
386 ym2149_device(unsigned int clock, bool clk_sel = false);
387 };
388
389 class ym3439_device : public ay8910_device
390 {
391 public:
392 ym3439_device(unsigned int clock, bool clk_sel = false);
393 };
394
395 class ymz284_device : public ay8910_device
396 {
397 public:
398 ymz284_device(unsigned int clock);
399 };
400
401 class ymz294_device : public ay8910_device
402 {
403 public:
404 ymz294_device(unsigned int clock);
405 };
406
407 class sunsoft_5b_sound_device : public ay8910_device
408 {
409 public:
410 sunsoft_5b_sound_device(unsigned int clock);
411 };
412
413
414 #endif // MAME_DEVICES_SOUND_AY8910_H