Debian Codegraph @

thirdparty/ymfm/src/ymfm_opl.h

raw
1 // BSD 3-Clause License
2 //
3 // Copyright (c) 2021, Aaron Giles
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this
10 // list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice,
13 // this list of conditions and the following disclaimer in the documentation
14 // and/or other materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #ifndef YMFM_OPL_H
32 #define YMFM_OPL_H
33
34 #pragma once
35
36 #include "ymfm.h"
37 #include "ymfm_adpcm.h"
38 #include "ymfm_fm.h"
39 #include "ymfm_pcm.h"
40
41 namespace ymfm
42 {
43
44 //*********************************************************
45 // REGISTER CLASSES
46 //*********************************************************
47
48 // ======================> opl_registers_base
49
50 //
51 // OPL/OPL2/OPL3/OPL4 register map:
52 //
53 // System-wide registers:
54 // 01 xxxxxxxx Test register
55 // --x----- Enable OPL compatibility mode [OPL2 only] (1 = enable)
56 // 02 xxxxxxxx Timer A value (4 * OPN)
57 // 03 xxxxxxxx Timer B value
58 // 04 x------- RST
59 // -x------ Mask timer A
60 // --x----- Mask timer B
61 // ------x- Load timer B
62 // -------x Load timer A
63 // 08 x------- CSM mode [OPL/OPL2 only]
64 // -x------ Note select
65 // BD x------- AM depth
66 // -x------ PM depth
67 // --x----- Rhythm enable
68 // ---x---- Bass drum key on
69 // ----x--- Snare drum key on
70 // -----x-- Tom key on
71 // ------x- Top cymbal key on
72 // -------x High hat key on
73 // 101 --xxxxxx Test register 2 [OPL3 only]
74 // 104 --x----- Channel 6 4-operator mode [OPL3 only]
75 // ---x---- Channel 5 4-operator mode [OPL3 only]
76 // ----x--- Channel 4 4-operator mode [OPL3 only]
77 // -----x-- Channel 3 4-operator mode [OPL3 only]
78 // ------x- Channel 2 4-operator mode [OPL3 only]
79 // -------x Channel 1 4-operator mode [OPL3 only]
80 // 105 -------x New [OPL3 only]
81 // ------x- New2 [OPL4 only]
82 //
83 // Per-channel registers (channel in address bits 0-3)
84 // Note that all these apply to address+100 as well on OPL3+
85 // A0-A8 xxxxxxxx F-number (low 8 bits)
86 // B0-B8 --x----- Key on
87 // ---xxx-- Block (octvate, 0-7)
88 // ------xx F-number (high two bits)
89 // C0-C8 x------- CHD output (to DO0 pin) [OPL3+ only]
90 // -x------ CHC output (to DO0 pin) [OPL3+ only]
91 // --x----- CHB output (mixed right, to DO2 pin) [OPL3+ only]
92 // ---x---- CHA output (mixed left, to DO2 pin) [OPL3+ only]
93 // ----xxx- Feedback level for operator 1 (0-7)
94 // -------x Operator connection algorithm
95 //
96 // Per-operator registers (operator in bits 0-5)
97 // Note that all these apply to address+100 as well on OPL3+
98 // 20-35 x------- AM enable
99 // -x------ PM enable (VIB)
100 // --x----- EG type
101 // ---x---- Key scale rate
102 // ----xxxx Multiple value (0-15)
103 // 40-55 xx------ Key scale level (0-3)
104 // --xxxxxx Total level (0-63)
105 // 60-75 xxxx---- Attack rate (0-15)
106 // ----xxxx Decay rate (0-15)
107 // 80-95 xxxx---- Sustain level (0-15)
108 // ----xxxx Release rate (0-15)
109 // E0-F5 ------xx Wave select (0-3) [OPL2 only]
110 // -----xxx Wave select (0-7) [OPL3+ only]
111 //
112
113 template<int Revision>
114 class opl_registers_base : public fm_registers_base
115 {
116 static constexpr bool IsOpl2 = (Revision == 2);
117 static constexpr bool IsOpl2Plus = (Revision >= 2);
118 static constexpr bool IsOpl3Plus = (Revision >= 3);
119 static constexpr bool IsOpl4Plus = (Revision >= 4);
120
121 public:
122 // constants
123 static constexpr uint32_t OUTPUTS = IsOpl3Plus ? 4 : 1;
124 static constexpr uint32_t CHANNELS = IsOpl3Plus ? 18 : 9;
125 static constexpr uint32_t ALL_CHANNELS = (1 << CHANNELS) - 1;
126 static constexpr uint32_t OPERATORS = CHANNELS * 2;
127 static constexpr uint32_t WAVEFORMS = IsOpl3Plus ? 8 : (IsOpl2Plus ? 4 : 1);
128 static constexpr uint32_t REGISTERS = IsOpl3Plus ? 0x200 : 0x100;
129 static constexpr uint32_t REG_MODE = 0x04;
130 static constexpr uint32_t DEFAULT_PRESCALE = IsOpl4Plus ? 19 : (IsOpl3Plus ? 8 : 4);
131 static constexpr uint32_t EG_CLOCK_DIVIDER = 1;
132 static constexpr uint32_t CSM_TRIGGER_MASK = ALL_CHANNELS;
133 static constexpr bool DYNAMIC_OPS = IsOpl3Plus;
134 static constexpr bool MODULATOR_DELAY = !IsOpl3Plus;
135 static constexpr uint8_t STATUS_TIMERA = 0x40;
136 static constexpr uint8_t STATUS_TIMERB = 0x20;
137 static constexpr uint8_t STATUS_BUSY = 0;
138 static constexpr uint8_t STATUS_IRQ = 0x80;
139
140 // constructor
141 opl_registers_base();
142
143 // reset to initial state
144 void reset();
145
146 // save/restore
147 void save_restore(ymfm_saved_state &state);
148
149 // map channel number to register offset
150 static constexpr uint32_t channel_offset(uint32_t chnum)
151 {
152 assert(chnum < CHANNELS);
153 if (!IsOpl3Plus)
154 return chnum;
155 else
156 return (chnum % 9) + 0x100 * (chnum / 9);
157 }
158
159 // map operator number to register offset
160 static constexpr uint32_t operator_offset(uint32_t opnum)
161 {
162 assert(opnum < OPERATORS);
163 if (!IsOpl3Plus)
164 return opnum + 2 * (opnum / 6);
165 else
166 return (opnum % 18) + 2 * ((opnum % 18) / 6) + 0x100 * (opnum / 18);
167 }
168
169 // return an array of operator indices for each channel
170 struct operator_mapping { uint32_t chan[CHANNELS]; };
171 void operator_map(operator_mapping &dest) const;
172
173 // OPL4 apparently can read back FM registers?
174 uint8_t read(uint16_t index) const { return m_regdata[index]; }
175
176 // handle writes to the register array
177 bool write(uint16_t index, uint8_t data, uint32_t &chan, uint32_t &opmask);
178
179 // clock the noise and LFO, if present, returning LFO PM value
180 int32_t clock_noise_and_lfo();
181
182 // reset the LFO
183 void reset_lfo() { m_lfo_am_counter = m_lfo_pm_counter = 0; }
184
185 // return the AM offset from LFO for the given channel
186 // on OPL this is just a fixed value
187 uint32_t lfo_am_offset(uint32_t choffs) const { return m_lfo_am; }
188
189 // return LFO/noise states
190 uint32_t noise_state() const { return m_noise_lfsr >> 23; }
191
192 // caching helpers
193 void cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache);
194
195 // compute the phase step, given a PM value
196 uint32_t compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm);
197
198 // log a key-on event
199 std::string log_keyon(uint32_t choffs, uint32_t opoffs);
200
201 // system-wide registers
202 uint32_t test() const { return byte(0x01, 0, 8); }
203 uint32_t waveform_enable() const { return IsOpl2 ? byte(0x01, 5, 1) : (IsOpl3Plus ? 1 : 0); }
204 uint32_t timer_a_value() const { return byte(0x02, 0, 8) * 4; } // 8->10 bits
205 uint32_t timer_b_value() const { return byte(0x03, 0, 8); }
206 uint32_t status_mask() const { return byte(0x04, 0, 8) & 0x78; }
207 uint32_t irq_reset() const { return byte(0x04, 7, 1); }
208 uint32_t reset_timer_b() const { return byte(0x04, 7, 1) | byte(0x04, 5, 1); }
209 uint32_t reset_timer_a() const { return byte(0x04, 7, 1) | byte(0x04, 6, 1); }
210 uint32_t enable_timer_b() const { return 1; }
211 uint32_t enable_timer_a() const { return 1; }
212 uint32_t load_timer_b() const { return byte(0x04, 1, 1); }
213 uint32_t load_timer_a() const { return byte(0x04, 0, 1); }
214 uint32_t csm() const { return IsOpl3Plus ? 0 : byte(0x08, 7, 1); }
215 uint32_t note_select() const { return byte(0x08, 6, 1); }
216 uint32_t lfo_am_depth() const { return byte(0xbd, 7, 1); }
217 uint32_t lfo_pm_depth() const { return byte(0xbd, 6, 1); }
218 uint32_t rhythm_enable() const { return byte(0xbd, 5, 1); }
219 uint32_t rhythm_keyon() const { return byte(0xbd, 4, 0); }
220 uint32_t newflag() const { return IsOpl3Plus ? byte(0x105, 0, 1) : 0; }
221 uint32_t new2flag() const { return IsOpl4Plus ? byte(0x105, 1, 1) : 0; }
222 uint32_t fourop_enable() const { return IsOpl3Plus ? byte(0x104, 0, 6) : 0; }
223
224 // per-channel registers
225 uint32_t ch_block_freq(uint32_t choffs) const { return word(0xb0, 0, 5, 0xa0, 0, 8, choffs); }
226 uint32_t ch_feedback(uint32_t choffs) const { return byte(0xc0, 1, 3, choffs); }
227 uint32_t ch_algorithm(uint32_t choffs) const { return byte(0xc0, 0, 1, choffs) | (IsOpl3Plus ? (8 | (byte(0xc3, 0, 1, choffs) << 1)) : 0); }
228 uint32_t ch_output_any(uint32_t choffs) const { return newflag() ? byte(0xc0 + choffs, 4, 4) : 1; }
229 uint32_t ch_output_0(uint32_t choffs) const { return newflag() ? byte(0xc0 + choffs, 4, 1) : 1; }
230 uint32_t ch_output_1(uint32_t choffs) const { return newflag() ? byte(0xc0 + choffs, 5, 1) : (IsOpl3Plus ? 1 : 0); }
231 uint32_t ch_output_2(uint32_t choffs) const { return newflag() ? byte(0xc0 + choffs, 6, 1) : 0; }
232 uint32_t ch_output_3(uint32_t choffs) const { return newflag() ? byte(0xc0 + choffs, 7, 1) : 0; }
233
234 // per-operator registers
235 uint32_t op_lfo_am_enable(uint32_t opoffs) const { return byte(0x20, 7, 1, opoffs); }
236 uint32_t op_lfo_pm_enable(uint32_t opoffs) const { return byte(0x20, 6, 1, opoffs); }
237 uint32_t op_eg_sustain(uint32_t opoffs) const { return byte(0x20, 5, 1, opoffs); }
238 uint32_t op_ksr(uint32_t opoffs) const { return byte(0x20, 4, 1, opoffs); }
239 uint32_t op_multiple(uint32_t opoffs) const { return byte(0x20, 0, 4, opoffs); }
240 uint32_t op_ksl(uint32_t opoffs) const { uint32_t temp = byte(0x40, 6, 2, opoffs); return bitfield(temp, 1) | (bitfield(temp, 0) << 1); }
241 uint32_t op_total_level(uint32_t opoffs) const { return byte(0x40, 0, 6, opoffs); }
242 uint32_t op_attack_rate(uint32_t opoffs) const { return byte(0x60, 4, 4, opoffs); }
243 uint32_t op_decay_rate(uint32_t opoffs) const { return byte(0x60, 0, 4, opoffs); }
244 uint32_t op_sustain_level(uint32_t opoffs) const { return byte(0x80, 4, 4, opoffs); }
245 uint32_t op_release_rate(uint32_t opoffs) const { return byte(0x80, 0, 4, opoffs); }
246 uint32_t op_waveform(uint32_t opoffs) const { return IsOpl2Plus ? byte(0xe0, 0, newflag() ? 3 : 2, opoffs) : 0; }
247
248 protected:
249 // return a bitfield extracted from a byte
250 uint32_t byte(uint32_t offset, uint32_t start, uint32_t count, uint32_t extra_offset = 0) const
251 {
252 return bitfield(m_regdata[offset + extra_offset], start, count);
253 }
254
255 // return a bitfield extracted from a pair of bytes, MSBs listed first
256 uint32_t word(uint32_t offset1, uint32_t start1, uint32_t count1, uint32_t offset2, uint32_t start2, uint32_t count2, uint32_t extra_offset = 0) const
257 {
258 return (byte(offset1, start1, count1, extra_offset) << count2) | byte(offset2, start2, count2, extra_offset);
259 }
260
261 // helper to determine if the this channel is an active rhythm channel
262 bool is_rhythm(uint32_t choffs) const
263 {
264 return rhythm_enable() && (choffs >= 6 && choffs <= 8);
265 }
266
267 // internal state
268 uint16_t m_lfo_am_counter; // LFO AM counter
269 uint16_t m_lfo_pm_counter; // LFO PM counter
270 uint32_t m_noise_lfsr; // noise LFSR state
271 uint8_t m_lfo_am; // current LFO AM value
272 uint8_t m_regdata[REGISTERS]; // register data
273 uint16_t m_waveform[WAVEFORMS][WAVEFORM_LENGTH]; // waveforms
274 };
275
276 using opl_registers = opl_registers_base<1>;
277 using opl2_registers = opl_registers_base<2>;
278 using opl3_registers = opl_registers_base<3>;
279 using opl4_registers = opl_registers_base<4>;
280
281
282
283 // ======================> opll_registers
284
285 //
286 // OPLL register map:
287 //
288 // System-wide registers:
289 // 0E --x----- Rhythm enable
290 // ---x---- Bass drum key on
291 // ----x--- Snare drum key on
292 // -----x-- Tom key on
293 // ------x- Top cymbal key on
294 // -------x High hat key on
295 // 0F xxxxxxxx Test register
296 //
297 // Per-channel registers (channel in address bits 0-3)
298 // 10-18 xxxxxxxx F-number (low 8 bits)
299 // 20-28 --x----- Sustain on
300 // ---x---- Key on
301 // --- xxx- Block (octvate, 0-7)
302 // -------x F-number (high bit)
303 // 30-38 xxxx---- Instrument selection
304 // ----xxxx Volume
305 //
306 // User instrument registers (for carrier, modulator operators)
307 // 00-01 x------- AM enable
308 // -x------ PM enable (VIB)
309 // --x----- EG type
310 // ---x---- Key scale rate
311 // ----xxxx Multiple value (0-15)
312 // 02 xx------ Key scale level (carrier, 0-3)
313 // --xxxxxx Total level (modulator, 0-63)
314 // 03 xx------ Key scale level (modulator, 0-3)
315 // ---x---- Rectified wave (carrier)
316 // ----x--- Rectified wave (modulator)
317 // -----xxx Feedback level for operator 1 (0-7)
318 // 04-05 xxxx---- Attack rate (0-15)
319 // ----xxxx Decay rate (0-15)
320 // 06-07 xxxx---- Sustain level (0-15)
321 // ----xxxx Release rate (0-15)
322 //
323 // Internal (fake) registers:
324 // 40-48 xxxxxxxx Current instrument base address
325 // 4E-5F xxxxxxxx Current instrument base address + operator slot (0/1)
326 // 70-FF xxxxxxxx Data for instruments (1-16 plus 3 drums)
327 //
328
329 class opll_registers : public fm_registers_base
330 {
331 public:
332 static constexpr uint32_t OUTPUTS = 2;
333 static constexpr uint32_t CHANNELS = 9;
334 static constexpr uint32_t ALL_CHANNELS = (1 << CHANNELS) - 1;
335 static constexpr uint32_t OPERATORS = CHANNELS * 2;
336 static constexpr uint32_t WAVEFORMS = 2;
337 static constexpr uint32_t REGISTERS = 0x40;
338 static constexpr uint32_t REG_MODE = 0x3f;
339 static constexpr uint32_t DEFAULT_PRESCALE = 4;
340 static constexpr uint32_t EG_CLOCK_DIVIDER = 1;
341 static constexpr uint32_t CSM_TRIGGER_MASK = 0;
342 static constexpr bool EG_HAS_DEPRESS = true;
343 static constexpr bool MODULATOR_DELAY = true;
344 static constexpr uint8_t STATUS_TIMERA = 0;
345 static constexpr uint8_t STATUS_TIMERB = 0;
346 static constexpr uint8_t STATUS_BUSY = 0;
347 static constexpr uint8_t STATUS_IRQ = 0;
348
349 // OPLL-specific constants
350 static constexpr uint32_t INSTDATA_SIZE = 0x90;
351
352 // constructor
353 opll_registers();
354
355 // reset to initial state
356 void reset();
357
358 // save/restore
359 void save_restore(ymfm_saved_state &state);
360
361 // map channel number to register offset
362 static constexpr uint32_t channel_offset(uint32_t chnum)
363 {
364 assert(chnum < CHANNELS);
365 return chnum;
366 }
367
368 // map operator number to register offset
369 static constexpr uint32_t operator_offset(uint32_t opnum)
370 {
371 assert(opnum < OPERATORS);
372 return opnum;
373 }
374
375 // return an array of operator indices for each channel
376 struct operator_mapping { uint32_t chan[CHANNELS]; };
377 void operator_map(operator_mapping &dest) const;
378
379 // read a register value
380 uint8_t read(uint16_t index) const { return m_regdata[index]; }
381
382 // handle writes to the register array
383 bool write(uint16_t index, uint8_t data, uint32_t &chan, uint32_t &opmask);
384
385 // clock the noise and LFO, if present, returning LFO PM value
386 int32_t clock_noise_and_lfo();
387
388 // reset the LFO
389 void reset_lfo() { m_lfo_am_counter = m_lfo_pm_counter = 0; }
390
391 // return the AM offset from LFO for the given channel
392 // on OPL this is just a fixed value
393 uint32_t lfo_am_offset(uint32_t choffs) const { return m_lfo_am; }
394
395 // return LFO/noise states
396 uint32_t noise_state() const { return m_noise_lfsr >> 23; }
397
398 // caching helpers
399 void cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache);
400
401 // compute the phase step, given a PM value
402 uint32_t compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm);
403
404 // log a key-on event
405 std::string log_keyon(uint32_t choffs, uint32_t opoffs);
406
407 // set the instrument data
408 void set_instrument_data(uint8_t const *data)
409 {
410 std::copy_n(data, INSTDATA_SIZE, &m_instdata[0]);
411 }
412
413 // system-wide registers
414 uint32_t rhythm_enable() const { return byte(0x0e, 5, 1); }
415 uint32_t rhythm_keyon() const { return byte(0x0e, 4, 0); }
416 uint32_t test() const { return byte(0x0f, 0, 8); }
417 uint32_t waveform_enable() const { return 1; }
418 uint32_t timer_a_value() const { return 0; }
419 uint32_t timer_b_value() const { return 0; }
420 uint32_t status_mask() const { return 0; }
421 uint32_t irq_reset() const { return 0; }
422 uint32_t reset_timer_b() const { return 0; }
423 uint32_t reset_timer_a() const { return 0; }
424 uint32_t enable_timer_b() const { return 0; }
425 uint32_t enable_timer_a() const { return 0; }
426 uint32_t load_timer_b() const { return 0; }
427 uint32_t load_timer_a() const { return 0; }
428 uint32_t csm() const { return 0; }
429
430 // per-channel registers
431 uint32_t ch_block_freq(uint32_t choffs) const { return word(0x20, 0, 4, 0x10, 0, 8, choffs); }
432 uint32_t ch_sustain(uint32_t choffs) const { return byte(0x20, 5, 1, choffs); }
433 uint32_t ch_total_level(uint32_t choffs) const { return instchbyte(0x02, 0, 6, choffs); }
434 uint32_t ch_feedback(uint32_t choffs) const { return instchbyte(0x03, 0, 3, choffs); }
435 uint32_t ch_algorithm(uint32_t choffs) const { return 0; }
436 uint32_t ch_instrument(uint32_t choffs) const { return byte(0x30, 4, 4, choffs); }
437 uint32_t ch_output_any(uint32_t choffs) const { return 1; }
438 uint32_t ch_output_0(uint32_t choffs) const { return !is_rhythm(choffs); }
439 uint32_t ch_output_1(uint32_t choffs) const { return is_rhythm(choffs); }
440 uint32_t ch_output_2(uint32_t choffs) const { return 0; }
441 uint32_t ch_output_3(uint32_t choffs) const { return 0; }
442
443 // per-operator registers
444 uint32_t op_lfo_am_enable(uint32_t opoffs) const { return instopbyte(0x00, 7, 1, opoffs); }
445 uint32_t op_lfo_pm_enable(uint32_t opoffs) const { return instopbyte(0x00, 6, 1, opoffs); }
446 uint32_t op_eg_sustain(uint32_t opoffs) const { return instopbyte(0x00, 5, 1, opoffs); }
447 uint32_t op_ksr(uint32_t opoffs) const { return instopbyte(0x00, 4, 1, opoffs); }
448 uint32_t op_multiple(uint32_t opoffs) const { return instopbyte(0x00, 0, 4, opoffs); }
449 uint32_t op_ksl(uint32_t opoffs) const { return instopbyte(0x02, 6, 2, opoffs); }
450 uint32_t op_waveform(uint32_t opoffs) const { return instchbyte(0x03, 3 + bitfield(opoffs, 0), 1, opoffs >> 1); }
451 uint32_t op_attack_rate(uint32_t opoffs) const { return instopbyte(0x04, 4, 4, opoffs); }
452 uint32_t op_decay_rate(uint32_t opoffs) const { return instopbyte(0x04, 0, 4, opoffs); }
453 uint32_t op_sustain_level(uint32_t opoffs) const { return instopbyte(0x06, 4, 4, opoffs); }
454 uint32_t op_release_rate(uint32_t opoffs) const { return instopbyte(0x06, 0, 4, opoffs); }
455 uint32_t op_volume(uint32_t opoffs) const { return byte(0x30, 4 * bitfield(~opoffs, 0), 4, opoffs >> 1); }
456
457 private:
458 // return a bitfield extracted from a byte
459 uint32_t byte(uint32_t offset, uint32_t start, uint32_t count, uint32_t extra_offset = 0) const
460 {
461 return bitfield(m_regdata[offset + extra_offset], start, count);
462 }
463
464 // return a bitfield extracted from a pair of bytes, MSBs listed first
465 uint32_t word(uint32_t offset1, uint32_t start1, uint32_t count1, uint32_t offset2, uint32_t start2, uint32_t count2, uint32_t extra_offset = 0) const
466 {
467 return (byte(offset1, start1, count1, extra_offset) << count2) | byte(offset2, start2, count2, extra_offset);
468 }
469
470 // helpers to read from instrument channel/operator data
471 uint32_t instchbyte(uint32_t offset, uint32_t start, uint32_t count, uint32_t choffs) const { return bitfield(m_chinst[choffs][offset], start, count); }
472 uint32_t instopbyte(uint32_t offset, uint32_t start, uint32_t count, uint32_t opoffs) const { return bitfield(m_opinst[opoffs][offset], start, count); }
473
474 // helper to determine if the this channel is an active rhythm channel
475 bool is_rhythm(uint32_t choffs) const
476 {
477 return rhythm_enable() && choffs >= 6;
478 }
479
480 // internal state
481 uint16_t m_lfo_am_counter; // LFO AM counter
482 uint16_t m_lfo_pm_counter; // LFO PM counter
483 uint32_t m_noise_lfsr; // noise LFSR state
484 uint8_t m_lfo_am; // current LFO AM value
485 uint8_t const *m_chinst[CHANNELS]; // pointer to instrument data for each channel
486 uint8_t const *m_opinst[OPERATORS]; // pointer to instrument data for each operator
487 uint8_t m_regdata[REGISTERS]; // register data
488 uint8_t m_instdata[INSTDATA_SIZE]; // instrument data
489 uint16_t m_waveform[WAVEFORMS][WAVEFORM_LENGTH]; // waveforms
490 };
491
492
493
494 //*********************************************************
495 // OPL IMPLEMENTATION CLASSES
496 //*********************************************************
497
498 // ======================> ym3526
499
500 class ym3526
501 {
502 public:
503 using fm_engine = fm_engine_base<opl_registers>;
504 using output_data = fm_engine::output_data;
505 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
506
507 // constructor
508 ym3526(ymfm_interface &intf);
509
510 // reset
511 void reset();
512
513 // save/restore
514 void save_restore(ymfm_saved_state &state);
515
516 // pass-through helpers
517 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
518 void invalidate_caches() { m_fm.invalidate_caches(); }
519
520 // read access
521 uint8_t read_status();
522 uint8_t read(uint32_t offset);
523
524 // write access
525 void write_address(uint8_t data);
526 void write_data(uint8_t data);
527 void write(uint32_t offset, uint8_t data);
528
529 // generate samples of sound
530 void generate(output_data *output, uint32_t numsamples = 1);
531 protected:
532 // internal state
533 uint8_t m_address; // address register
534 fm_engine m_fm; // core FM engine
535 };
536
537
538 // ======================> y8950
539
540 class y8950
541 {
542 public:
543 using fm_engine = fm_engine_base<opl_registers>;
544 using output_data = fm_engine::output_data;
545 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
546
547 static constexpr uint8_t STATUS_ADPCM_B_PLAYING = 0x01;
548 static constexpr uint8_t STATUS_ADPCM_B_BRDY = 0x08;
549 static constexpr uint8_t STATUS_ADPCM_B_EOS = 0x10;
550 static constexpr uint8_t ALL_IRQS = STATUS_ADPCM_B_BRDY | STATUS_ADPCM_B_EOS | fm_engine::STATUS_TIMERA | fm_engine::STATUS_TIMERB;
551
552 // constructor
553 y8950(ymfm_interface &intf);
554
555 // reset
556 void reset();
557
558 // save/restore
559 void save_restore(ymfm_saved_state &state);
560
561 // pass-through helpers
562 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
563 void invalidate_caches() { m_fm.invalidate_caches(); }
564
565 // read access
566 uint8_t read_status();
567 uint8_t read_data();
568 uint8_t read(uint32_t offset);
569
570 // write access
571 void write_address(uint8_t data);
572 void write_data(uint8_t data);
573 void write(uint32_t offset, uint8_t data);
574
575 // generate samples of sound
576 void generate(output_data *output, uint32_t numsamples = 1);
577
578 protected:
579 // internal state
580 uint8_t m_address; // address register
581 uint8_t m_io_ddr; // data direction register for I/O
582 fm_engine m_fm; // core FM engine
583 adpcm_b_engine m_adpcm_b; // ADPCM-B engine
584 };
585
586
587
588 //*********************************************************
589 // OPL2 IMPLEMENTATION CLASSES
590 //*********************************************************
591
592 // ======================> ym3812
593
594 class ym3812
595 {
596 public:
597 using fm_engine = fm_engine_base<opl2_registers>;
598 using output_data = fm_engine::output_data;
599 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
600
601 // constructor
602 ym3812(ymfm_interface &intf);
603
604 // reset
605 void reset();
606
607 // save/restore
608 void save_restore(ymfm_saved_state &state);
609
610 // pass-through helpers
611 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
612 void invalidate_caches() { m_fm.invalidate_caches(); }
613
614 // read access
615 uint8_t read_status();
616 uint8_t read(uint32_t offset);
617
618 // write access
619 void write_address(uint8_t data);
620 void write_data(uint8_t data);
621 void write(uint32_t offset, uint8_t data);
622
623 // generate samples of sound
624 void generate(output_data *output, uint32_t numsamples = 1);
625
626 protected:
627 // internal state
628 uint8_t m_address; // address register
629 fm_engine m_fm; // core FM engine
630 };
631
632
633
634 //*********************************************************
635 // OPL3 IMPLEMENTATION CLASSES
636 //*********************************************************
637
638 // ======================> ymf262
639
640 class ymf262
641 {
642 public:
643 using fm_engine = fm_engine_base<opl3_registers>;
644 using output_data = fm_engine::output_data;
645 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
646
647 // constructor
648 ymf262(ymfm_interface &intf);
649
650 // reset
651 void reset();
652
653 // save/restore
654 void save_restore(ymfm_saved_state &state);
655
656 // pass-through helpers
657 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
658 void invalidate_caches() { m_fm.invalidate_caches(); }
659
660 // read access
661 uint8_t read_status();
662 uint8_t read(uint32_t offset);
663
664 // write access
665 void write_address(uint8_t data);
666 void write_data(uint8_t data);
667 void write_address_hi(uint8_t data);
668 void write(uint32_t offset, uint8_t data);
669
670 // generate samples of sound
671 void generate(output_data *output, uint32_t numsamples = 1);
672
673 protected:
674 // internal state
675 uint16_t m_address; // address register
676 fm_engine m_fm; // core FM engine
677 };
678
679
680 // ======================> ymf289b
681
682 class ymf289b
683 {
684 static constexpr uint8_t STATUS_BUSY_FLAGS = 0x05;
685
686 public:
687 using fm_engine = fm_engine_base<opl3_registers>;
688 using output_data = fm_engine::output_data;
689 static constexpr uint32_t OUTPUTS = 2;
690
691 // constructor
692 ymf289b(ymfm_interface &intf);
693
694 // reset
695 void reset();
696
697 // save/restore
698 void save_restore(ymfm_saved_state &state);
699
700 // pass-through helpers
701 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
702 void invalidate_caches() { m_fm.invalidate_caches(); }
703
704 // read access
705 uint8_t read_status();
706 uint8_t read_data();
707 uint8_t read(uint32_t offset);
708
709 // write access
710 void write_address(uint8_t data);
711 void write_data(uint8_t data);
712 void write_address_hi(uint8_t data);
713 void write(uint32_t offset, uint8_t data);
714
715 // generate samples of sound
716 void generate(output_data *output, uint32_t numsamples = 1);
717
718 protected:
719 // internal helpers
720 bool ymf289b_mode() { return ((m_fm.regs().read(0x105) & 0x04) != 0); }
721
722 // internal state
723 uint16_t m_address; // address register
724 fm_engine m_fm; // core FM engine
725 };
726
727
728
729 //*********************************************************
730 // OPL4 IMPLEMENTATION CLASSES
731 //*********************************************************
732
733 // ======================> ymf278b
734
735 class ymf278b
736 {
737 // Using the nominal datasheet frequency of 33.868MHz, the output of the
738 // chip will be clock/768 = 44.1kHz. However, the FM engine is clocked
739 // internally at clock/(19*36), or 49.515kHz, so the FM output needs to
740 // be downsampled. We treat this as needing to clock the FM engine an
741 // extra tick every few samples. The exact ratio is 768/(19*36) or
742 // 768/684 = 192/171. So if we always clock the FM once, we'll have
743 // 192/171 - 1 = 21/171 left. Thus we count 21 for each sample and when
744 // it gets above 171, we tick an extra time.
745 static constexpr uint32_t FM_EXTRA_SAMPLE_THRESH = 171;
746 static constexpr uint32_t FM_EXTRA_SAMPLE_STEP = 192 - FM_EXTRA_SAMPLE_THRESH;
747
748 public:
749 using fm_engine = fm_engine_base<opl4_registers>;
750 static constexpr uint32_t OUTPUTS = 6;
751 using output_data = ymfm_output<OUTPUTS>;
752
753 static constexpr uint8_t STATUS_BUSY = 0x01;
754 static constexpr uint8_t STATUS_LD = 0x02;
755
756 // constructor
757 ymf278b(ymfm_interface &intf);
758
759 // reset
760 void reset();
761
762 // save/restore
763 void save_restore(ymfm_saved_state &state);
764
765 // pass-through helpers
766 uint32_t sample_rate(uint32_t input_clock) const { return input_clock / 768; }
767 void invalidate_caches() { m_fm.invalidate_caches(); }
768
769 // read access
770 uint8_t read_status();
771 uint8_t read_data_pcm();
772 uint8_t read(uint32_t offset);
773
774 // write access
775 void write_address(uint8_t data);
776 void write_data(uint8_t data);
777 void write_address_hi(uint8_t data);
778 void write_address_pcm(uint8_t data);
779 void write_data_pcm(uint8_t data);
780 void write(uint32_t offset, uint8_t data);
781
782 // generate samples of sound
783 void generate(output_data *output, uint32_t numsamples = 1);
784
785 protected:
786 // internal state
787 uint16_t m_address; // address register
788 uint32_t m_fm_pos; // FM resampling position
789 uint32_t m_load_remaining; // how many more samples until LD flag clears
790 bool m_next_status_id; // flag to track which status ID to return
791 fm_engine m_fm; // core FM engine
792 pcm_engine m_pcm; // core PCM engine
793 };
794
795
796
797 //*********************************************************
798 // OPLL IMPLEMENTATION CLASSES
799 //*********************************************************
800
801 // ======================> opll_base
802
803 class opll_base
804 {
805 public:
806 using fm_engine = fm_engine_base<opll_registers>;
807 using output_data = fm_engine::output_data;
808 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
809
810 // constructor
811 opll_base(ymfm_interface &intf, uint8_t const *data);
812
813 // configuration
814 void set_instrument_data(uint8_t const *data) { m_fm.regs().set_instrument_data(data); }
815
816 // reset
817 void reset();
818
819 // save/restore
820 void save_restore(ymfm_saved_state &state);
821
822 // pass-through helpers
823 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
824 void invalidate_caches() { m_fm.invalidate_caches(); }
825
826 // read access -- doesn't really have any, but provide these for consistency
827 uint8_t read_status() { return 0x00; }
828 uint8_t read(uint32_t offset) { return 0x00; }
829
830 // write access
831 void write_address(uint8_t data);
832 void write_data(uint8_t data);
833 void write(uint32_t offset, uint8_t data);
834
835 // generate samples of sound
836 void generate(output_data *output, uint32_t numsamples = 1);
837
838 protected:
839 // internal state
840 uint8_t m_address; // address register
841 fm_engine m_fm; // core FM engine
842 };
843
844
845 // ======================> ym2413
846
847 class ym2413 : public opll_base
848 {
849 public:
850 // constructor
851 ym2413(ymfm_interface &intf, uint8_t const *instrument_data = nullptr);
852
853 private:
854 // internal state
855 static uint8_t const s_default_instruments[];
856 };
857
858
859 // ======================> ym2413
860
861 class ym2423 : public opll_base
862 {
863 public:
864 // constructor
865 ym2423(ymfm_interface &intf, uint8_t const *instrument_data = nullptr);
866
867 private:
868 // internal state
869 static uint8_t const s_default_instruments[];
870 };
871
872
873 // ======================> ymf281
874
875 class ymf281 : public opll_base
876 {
877 public:
878 // constructor
879 ymf281(ymfm_interface &intf, uint8_t const *instrument_data = nullptr);
880
881 private:
882 // internal state
883 static uint8_t const s_default_instruments[];
884 };
885
886
887 // ======================> ds1001
888
889 class ds1001 : public opll_base
890 {
891 public:
892 // constructor
893 ds1001(ymfm_interface &intf, uint8_t const *instrument_data = nullptr);
894
895 private:
896 // internal state
897 static uint8_t const s_default_instruments[];
898 };
899
900 }
901
902 #endif // YMFM_OPL_H