Debian Codegraph @

src/engine/platform/sound/ymfm/ymfm_opn.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_OPN_H
32 #define YMFM_OPN_H
33
34 #pragma once
35
36 #include "ymfm.h"
37 #include "ymfm_adpcm.h"
38 #include "ymfm_fm.h"
39 #include "ymfm_ssg.h"
40
41 namespace ymfm
42 {
43
44 //*********************************************************
45 // REGISTER CLASSES
46 //*********************************************************
47
48 // ======================> opn_registers_base
49
50 //
51 // OPN register map:
52 //
53 // System-wide registers:
54 // 21 xxxxxxxx Test register
55 // 22 ----x--- LFO enable [OPNA+ only]
56 // -----xxx LFO rate [OPNA+ only]
57 // 24 xxxxxxxx Timer A value (upper 8 bits)
58 // 25 ------xx Timer A value (lower 2 bits)
59 // 26 xxxxxxxx Timer B value
60 // 27 xx------ CSM/Multi-frequency mode for channel #2
61 // --x----- Reset timer B
62 // ---x---- Reset timer A
63 // ----x--- Enable timer B
64 // -----x-- Enable timer A
65 // ------x- Load timer B
66 // -------x Load timer A
67 // 28 x------- Key on/off operator 4
68 // -x------ Key on/off operator 3
69 // --x----- Key on/off operator 2
70 // ---x---- Key on/off operator 1
71 // ------xx Channel select
72 //
73 // Per-channel registers (channel in address bits 0-1)
74 // Note that all these apply to address+100 as well on OPNA+
75 // A0-A3 xxxxxxxx Frequency number lower 8 bits
76 // A4-A7 --xxx--- Block (0-7)
77 // -----xxx Frequency number upper 3 bits
78 // B0-B3 --xxx--- Feedback level for operator 1 (0-7)
79 // -----xxx Operator connection algorithm (0-7)
80 // B4-B7 x------- Pan left [OPNA]
81 // -x------ Pan right [OPNA]
82 // --xx---- LFO AM shift (0-3) [OPNA+ only]
83 // -----xxx LFO PM depth (0-7) [OPNA+ only]
84 //
85 // Per-operator registers (channel in address bits 0-1, operator in bits 2-3)
86 // Note that all these apply to address+100 as well on OPNA+
87 // 30-3F -xxx---- Detune value (0-7)
88 // ----xxxx Multiple value (0-15)
89 // 40-4F -xxxxxxx Total level (0-127)
90 // 50-5F xx------ Key scale rate (0-3)
91 // ---xxxxx Attack rate (0-31)
92 // 60-6F x------- LFO AM enable [OPNA]
93 // ---xxxxx Decay rate (0-31)
94 // 70-7F ---xxxxx Sustain rate (0-31)
95 // 80-8F xxxx---- Sustain level (0-15)
96 // ----xxxx Release rate (0-15)
97 // 90-9F ----x--- SSG-EG enable
98 // -----xxx SSG-EG envelope (0-7)
99 //
100 // Special multi-frequency registers (channel implicitly #2; operator in address bits 0-1)
101 // A8-AB xxxxxxxx Frequency number lower 8 bits
102 // AC-AF --xxx--- Block (0-7)
103 // -----xxx Frequency number upper 3 bits
104 //
105 // Internal (fake) registers:
106 // B8-BB --xxxxxx Latched frequency number upper bits (from A4-A7)
107 // BC-BF --xxxxxx Latched frequency number upper bits (from AC-AF)
108 //
109
110 template<bool IsOpnA>
111 class opn_registers_base : public fm_registers_base
112 {
113 public:
114 // constants
115 static constexpr uint32_t OUTPUTS = IsOpnA ? 2 : 1;
116 static constexpr uint32_t CHANNELS = IsOpnA ? 6 : 3;
117 static constexpr uint32_t ALL_CHANNELS = (1 << CHANNELS) - 1;
118 static constexpr uint32_t OPERATORS = CHANNELS * 4;
119 static constexpr uint32_t WAVEFORMS = 1;
120 static constexpr uint32_t REGISTERS = IsOpnA ? 0x200 : 0x100;
121 static constexpr uint32_t REG_MODE = 0x27;
122 static constexpr uint32_t DEFAULT_PRESCALE = 6;
123 static constexpr uint32_t EG_CLOCK_DIVIDER = 3;
124 static constexpr bool EG_HAS_SSG = true;
125 static constexpr bool MODULATOR_DELAY = false;
126 static constexpr uint32_t CSM_TRIGGER_MASK = 1 << 2;
127 static constexpr uint8_t STATUS_TIMERA = 0x01;
128 static constexpr uint8_t STATUS_TIMERB = 0x02;
129 static constexpr uint8_t STATUS_BUSY = 0x80;
130 static constexpr uint8_t STATUS_IRQ = 0;
131
132 // constructor
133 opn_registers_base();
134
135 // reset to initial state
136 void reset();
137
138 // save/restore
139 void save_restore(ymfm_saved_state &state);
140
141 // map channel number to register offset
142 static constexpr uint32_t channel_offset(uint32_t chnum)
143 {
144 assert(chnum < CHANNELS);
145 if (!IsOpnA)
146 return chnum;
147 else
148 return (chnum % 3) + 0x100 * (chnum / 3);
149 }
150
151 // map operator number to register offset
152 static constexpr uint32_t operator_offset(uint32_t opnum)
153 {
154 assert(opnum < OPERATORS);
155 if (!IsOpnA)
156 return opnum + opnum / 3;
157 else
158 return (opnum % 12) + ((opnum % 12) / 3) + 0x100 * (opnum / 12);
159 }
160
161 // return an array of operator indices for each channel
162 struct operator_mapping { uint32_t chan[CHANNELS]; };
163 void operator_map(operator_mapping &dest) const;
164
165 // read a register value
166 uint8_t read(uint16_t index) const { return m_regdata[index]; }
167
168 // handle writes to the register array
169 bool write(uint16_t index, uint8_t data, uint32_t &chan, uint32_t &opmask);
170
171 // clock the noise and LFO, if present, returning LFO PM value
172 int32_t clock_noise_and_lfo();
173
174 // reset the LFO
175 void reset_lfo() { m_lfo_counter = 0; }
176
177 // return the AM offset from LFO for the given channel
178 uint32_t lfo_am_offset(uint32_t choffs) const;
179
180 // return LFO/noise states
181 uint32_t noise_state() const { return 0; }
182
183 // caching helpers
184 void cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache);
185
186 // compute the phase step, given a PM value
187 uint32_t compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm);
188
189 // log a key-on event
190 std::string log_keyon(uint32_t choffs, uint32_t opoffs);
191
192 // system-wide registers
193 uint32_t test() const { return byte(0x21, 0, 8); }
194 uint32_t lfo_enable() const { return IsOpnA ? byte(0x22, 3, 1) : 0; }
195 uint32_t lfo_rate() const { return IsOpnA ? byte(0x22, 0, 3) : 0; }
196 uint32_t timer_a_value() const { return word(0x24, 0, 8, 0x25, 0, 2); }
197 uint32_t timer_b_value() const { return byte(0x26, 0, 8); }
198 uint32_t csm() const { return (byte(0x27, 6, 2) == 2); }
199 uint32_t multi_freq() const { return (byte(0x27, 6, 2) != 0); }
200 uint32_t reset_timer_b() const { return byte(0x27, 5, 1); }
201 uint32_t reset_timer_a() const { return byte(0x27, 4, 1); }
202 uint32_t enable_timer_b() const { return byte(0x27, 3, 1); }
203 uint32_t enable_timer_a() const { return byte(0x27, 2, 1); }
204 uint32_t load_timer_b() const { return byte(0x27, 1, 1); }
205 uint32_t load_timer_a() const { return byte(0x27, 0, 1); }
206 uint32_t multi_block_freq(uint32_t num) const { return word(0xac, 0, 6, 0xa8, 0, 8, num); }
207
208 // per-channel registers
209 uint32_t ch_block_freq(uint32_t choffs) const { return word(0xa4, 0, 6, 0xa0, 0, 8, choffs); }
210 uint32_t ch_feedback(uint32_t choffs) const { return byte(0xb0, 3, 3, choffs); }
211 uint32_t ch_algorithm(uint32_t choffs) const { return byte(0xb0, 0, 3, choffs); }
212 uint32_t ch_output_any(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 6, 2, choffs) : 1; }
213 uint32_t ch_output_0(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 7, 1, choffs) : 1; }
214 uint32_t ch_output_1(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 6, 1, choffs) : 0; }
215 uint32_t ch_output_2(uint32_t choffs) const { return 0; }
216 uint32_t ch_output_3(uint32_t choffs) const { return 0; }
217 uint32_t ch_lfo_am_sens(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 4, 2, choffs) : 0; }
218 uint32_t ch_lfo_pm_sens(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 0, 3, choffs) : 0; }
219
220 // per-operator registers
221 uint32_t op_detune(uint32_t opoffs) const { return byte(0x30, 4, 3, opoffs); }
222 uint32_t op_multiple(uint32_t opoffs) const { return byte(0x30, 0, 4, opoffs); }
223 uint32_t op_total_level(uint32_t opoffs) const { return byte(0x40, 0, 7, opoffs); }
224 uint32_t op_ksr(uint32_t opoffs) const { return byte(0x50, 6, 2, opoffs); }
225 uint32_t op_attack_rate(uint32_t opoffs) const { return byte(0x50, 0, 5, opoffs); }
226 uint32_t op_decay_rate(uint32_t opoffs) const { return byte(0x60, 0, 5, opoffs); }
227 uint32_t op_lfo_am_enable(uint32_t opoffs) const { return IsOpnA ? byte(0x60, 7, 1, opoffs) : 0; }
228 uint32_t op_sustain_rate(uint32_t opoffs) const { return byte(0x70, 0, 5, opoffs); }
229 uint32_t op_sustain_level(uint32_t opoffs) const { return byte(0x80, 4, 4, opoffs); }
230 uint32_t op_release_rate(uint32_t opoffs) const { return byte(0x80, 0, 4, opoffs); }
231 uint32_t op_ssg_eg_enable(uint32_t opoffs) const { return byte(0x90, 3, 1, opoffs); }
232 uint32_t op_ssg_eg_mode(uint32_t opoffs) const { return byte(0x90, 0, 3, opoffs); }
233
234 protected:
235 // return a bitfield extracted from a byte
236 uint32_t byte(uint32_t offset, uint32_t start, uint32_t count, uint32_t extra_offset = 0) const
237 {
238 return bitfield(m_regdata[offset + extra_offset], start, count);
239 }
240
241 // return a bitfield extracted from a pair of bytes, MSBs listed first
242 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
243 {
244 return (byte(offset1, start1, count1, extra_offset) << count2) | byte(offset2, start2, count2, extra_offset);
245 }
246
247 // internal state
248 uint32_t m_lfo_counter; // LFO counter
249 uint8_t m_lfo_am; // current LFO AM value
250 uint8_t m_regdata[REGISTERS]; // register data
251 uint16_t m_waveform[WAVEFORMS][WAVEFORM_LENGTH]; // waveforms
252 };
253
254 using opn_registers = opn_registers_base<false>;
255 using opna_registers = opn_registers_base<true>;
256
257
258
259 //*********************************************************
260 // OPN IMPLEMENTATION CLASSES
261 //*********************************************************
262
263 // A note about prescaling and sample rates.
264 //
265 // YM2203, YM2608, and YM2610 contain an onboard SSG (basically, a YM2149).
266 // In order to properly generate sound at fully fidelity, the output sample
267 // rate of the YM2149 must be input_clock / 8. This is much higher than the
268 // FM needs, but in the interest of keeping things simple, the OPN generate
269 // functions will output at the higher rate and just replicate the last FM
270 // sample as many times as needed.
271 //
272 // To make things even more complicated, the YM2203 and YM2608 allow for
273 // software-controlled prescaling, which affects the FM and SSG clocks in
274 // different ways. There are three settings: divide by 6/4 (FM/SSG); divide
275 // by 3/2; and divide by 2/1.
276 //
277 // Thus, the minimum output sample rate needed by each part of the chip
278 // varies with the prescale as follows:
279 //
280 // ---- YM2203 ----- ---- YM2608 ----- ---- YM2610 -----
281 // Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate
282 // 6 /72 /16 /144 /32 /144 /32
283 // 3 /36 /8 /72 /16
284 // 2 /24 /4 /48 /8
285 //
286 // If we standardized on the fastest SSG rate, we'd end up with the following
287 // (ratios are output_samples:source_samples):
288 //
289 // ---- YM2203 ----- ---- YM2608 ----- ---- YM2610 -----
290 // rate = clock/4 rate = clock/8 rate = clock/16
291 // Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate
292 // 6 18:1 4:1 18:1 4:1 9:1 2:1
293 // 3 9:1 2:1 9:1 2:1
294 // 2 6:1 1:1 6:1 1:1
295 //
296 // However, that's a pretty big performance hit for minimal gain. Going to
297 // the other extreme, we could standardize on the fastest FM rate, but then
298 // at least one prescale case (3) requires the FM to be smeared across two
299 // output samples:
300 //
301 // ---- YM2203 ----- ---- YM2608 ----- ---- YM2610 -----
302 // rate = clock/24 rate = clock/48 rate = clock/144
303 // Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate
304 // 6 3:1 2:3 3:1 2:3 1:1 2:9
305 // 3 1.5:1 1:3 1.5:1 1:3
306 // 2 1:1 1:6 1:1 1:6
307 //
308 // Stepping back one factor of 2 addresses that issue:
309 //
310 // ---- YM2203 ----- ---- YM2608 ----- ---- YM2610 -----
311 // rate = clock/12 rate = clock/24 rate = clock/144
312 // Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate
313 // 6 6:1 4:3 6:1 4:3 1:1 2:9
314 // 3 3:1 2:3 3:1 2:3
315 // 2 2:1 1:3 2:1 1:3
316 //
317 // This gives us three levels of output fidelity:
318 // OPN_FIDELITY_MAX -- highest sample rate, using fastest SSG rate
319 // OPN_FIDELITY_MIN -- lowest sample rate, using fastest FM rate
320 // OPN_FIDELITY_MED -- medium sample rate such that FM is never smeared
321 //
322 // At the maximum clocks for YM2203/YM2608 (4Mhz/8MHz), these rates will
323 // end up as:
324 // OPN_FIDELITY_MAX = 1000kHz
325 // OPN_FIDELITY_MIN = 166kHz
326 // OPN_FIEDLITY_MED = 333kHz
327
328
329 // ======================> opn_fidelity
330
331 enum opn_fidelity : uint8_t
332 {
333 OPN_FIDELITY_MAX,
334 OPN_FIDELITY_MIN,
335 OPN_FIDELITY_MED,
336
337 OPN_FIDELITY_DEFAULT = OPN_FIDELITY_MAX
338 };
339
340
341 // ======================> ssg_resampler
342
343 template<typename OutputType, int FirstOutput, bool MixTo1>
344 class ssg_resampler
345 {
346 private:
347 // helper to add the last computed value to the sums, applying the given scale
348 void add_last(int32_t &sum0, int32_t &sum1, int32_t &sum2, int32_t scale = 1);
349
350 // helper to clock a new value and then add it to the sums, applying the given scale
351 void clock_and_add(int32_t &sum0, int32_t &sum1, int32_t &sum2, int32_t scale = 1);
352
353 // helper to write the sums to the appropriate outputs, applying the given
354 // divisor to the final result
355 void write_to_output(OutputType *output, int32_t sum0, int32_t sum1, int32_t sum2, int32_t divisor = 1);
356
357 public:
358 // constructor
359 ssg_resampler(ssg_engine &ssg);
360
361 // save/restore
362 void save_restore(ymfm_saved_state &state);
363
364 // get the current sample index
365 uint32_t sampindex() const { return m_sampindex; }
366
367 // configure the ratio
368 void configure(uint8_t outsamples, uint8_t srcsamples);
369
370 // resample
371 void resample(OutputType *output, uint32_t numsamples)
372 {
373 (this->*m_resampler)(output, numsamples);
374 }
375
376 private:
377 // resample SSG output to the target at a rate of 1 SSG sample
378 // to every n output samples
379 template<int Multiplier>
380 void resample_n_1(OutputType *output, uint32_t numsamples);
381
382 // resample SSG output to the target at a rate of n SSG samples
383 // to every 1 output sample
384 template<int Divisor>
385 void resample_1_n(OutputType *output, uint32_t numsamples);
386
387 // resample SSG output to the target at a rate of 9 SSG samples
388 // to every 2 output samples
389 void resample_2_9(OutputType *output, uint32_t numsamples);
390
391 // resample SSG output to the target at a rate of 3 SSG samples
392 // to every 1 output sample
393 void resample_1_3(OutputType *output, uint32_t numsamples);
394
395 // resample SSG output to the target at a rate of 3 SSG samples
396 // to every 2 output samples
397 void resample_2_3(OutputType *output, uint32_t numsamples);
398
399 // resample SSG output to the target at a rate of 3 SSG samples
400 // to every 4 output samples
401 void resample_4_3(OutputType *output, uint32_t numsamples);
402
403 // no-op resampler
404 void resample_nop(OutputType *output, uint32_t numsamples);
405
406 // define a pointer type
407 using resample_func = void (ssg_resampler::*)(OutputType *output, uint32_t numsamples);
408
409 // internal state
410 ssg_engine &m_ssg;
411 uint32_t m_sampindex;
412 resample_func m_resampler;
413 ssg_engine::output_data m_last;
414 };
415
416
417 // ======================> ym2203
418
419 class ym2203
420 {
421 public:
422 using fm_engine = fm_engine_base<opn_registers>;
423 static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS;
424 static constexpr uint32_t SSG_OUTPUTS = ssg_engine::OUTPUTS;
425 static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS;
426 using output_data = ymfm_output<OUTPUTS>;
427
428 // constructor
429 ym2203(ymfm_interface &intf);
430
431 // configuration
432 void ssg_override(ssg_override &intf) { m_ssg.override(intf); }
433 void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(m_fm.clock_prescale()); }
434
435 // reset
436 void reset();
437
438 // save/restore
439 void save_restore(ymfm_saved_state &state);
440
441 // pass-through helpers
442 uint32_t sample_rate(uint32_t input_clock) const
443 {
444 switch (m_fidelity)
445 {
446 case OPN_FIDELITY_MIN: return input_clock / 24;
447 case OPN_FIDELITY_MED: return input_clock / 12;
448 default:
449 case OPN_FIDELITY_MAX: return input_clock / 4;
450 }
451 }
452 uint32_t ssg_effective_clock(uint32_t input_clock) const { uint32_t scale = m_fm.clock_prescale() * 2 / 3; return input_clock * 2 / scale; }
453 void invalidate_caches() { m_fm.invalidate_caches(); }
454
455 // read access
456 uint8_t read_status();
457 uint8_t read_data();
458 uint8_t read(uint32_t offset);
459
460 // write access
461 void write_address(uint8_t data);
462 void write_data(uint8_t data);
463 void write(uint32_t offset, uint8_t data);
464
465 // generate one sample of sound
466 void generate(output_data *output, uint32_t numsamples = 1);
467
468 fm_engine* debug_fm_engine() { return &m_fm; }
469
470 protected:
471 // internal helpers
472 void update_prescale(uint8_t prescale);
473 void clock_fm();
474
475 // internal state
476 opn_fidelity m_fidelity; // configured fidelity
477 uint8_t m_address; // address register
478 uint8_t m_fm_samples_per_output; // how many samples to repeat
479 fm_engine::output_data m_last_fm; // last FM output
480 fm_engine m_fm; // core FM engine
481 ssg_engine m_ssg; // SSG engine
482 ssg_resampler<output_data, 1, false> m_ssg_resampler; // SSG resampler helper
483 };
484
485
486
487 //*********************************************************
488 // OPNA IMPLEMENTATION CLASSES
489 //*********************************************************
490
491 // ======================> ym2608
492
493 class ym2608
494 {
495 static constexpr uint8_t STATUS_ADPCM_B_EOS = 0x04;
496 static constexpr uint8_t STATUS_ADPCM_B_BRDY = 0x08;
497 static constexpr uint8_t STATUS_ADPCM_B_ZERO = 0x10;
498 static constexpr uint8_t STATUS_ADPCM_B_PLAYING = 0x20;
499
500 public:
501 using fm_engine = fm_engine_base<opna_registers>;
502 static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS;
503 static constexpr uint32_t SSG_OUTPUTS = 1;
504 static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS;
505 using output_data = ymfm_output<OUTPUTS>;
506
507 // constructor
508 ym2608(ymfm_interface &intf);
509
510 // configuration
511 void ssg_override(ssg_override &intf) { m_ssg.override(intf); }
512 void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(m_fm.clock_prescale()); }
513
514 // reset
515 void reset();
516
517 // save/restore
518 void save_restore(ymfm_saved_state &state);
519
520 // pass-through helpers
521 uint32_t sample_rate(uint32_t input_clock) const
522 {
523 switch (m_fidelity)
524 {
525 case OPN_FIDELITY_MIN: return input_clock / 48;
526 case OPN_FIDELITY_MED: return input_clock / 24;
527 default:
528 case OPN_FIDELITY_MAX: return input_clock / 8;
529 }
530 }
531 uint32_t ssg_effective_clock(uint32_t input_clock) const { uint32_t scale = m_fm.clock_prescale() * 2 / 3; return input_clock / scale; }
532 void invalidate_caches() { m_fm.invalidate_caches(); }
533
534 // read access
535 uint8_t read_status();
536 uint8_t read_data();
537 uint8_t read_status_hi();
538 uint8_t read_data_hi();
539 uint8_t read(uint32_t offset);
540
541 // write access
542 void write_address(uint8_t data);
543 void write_data(uint8_t data);
544 void write_address_hi(uint8_t data);
545 void write_data_hi(uint8_t data);
546 void write(uint32_t offset, uint8_t data);
547
548 // generate one sample of sound
549 void generate(output_data *output, uint32_t numsamples = 1);
550
551 // get the engine
552 fm_engine* debug_fm_engine() { return &m_fm; }
553 ssg_engine* debug_ssg_engine() { return &m_ssg; }
554 adpcm_a_engine* debug_adpcm_a_engine() { return &m_adpcm_a; }
555 adpcm_b_engine* debug_adpcm_b_engine() { return &m_adpcm_b; }
556
557 protected:
558 // internal helpers
559 void update_prescale(uint8_t prescale);
560 void clock_fm_and_adpcm();
561
562 // internal state
563 opn_fidelity m_fidelity; // configured fidelity
564 uint16_t m_address; // address register
565 uint8_t m_fm_samples_per_output; // how many samples to repeat
566 uint8_t m_irq_enable; // IRQ enable register
567 uint8_t m_flag_control; // flag control register
568 fm_engine::output_data m_last_fm; // last FM output
569 fm_engine m_fm; // core FM engine
570 ssg_engine m_ssg; // SSG engine
571 ssg_resampler<output_data, 2, true> m_ssg_resampler; // SSG resampler helper
572 adpcm_a_engine m_adpcm_a; // ADPCM-A engine
573 adpcm_b_engine m_adpcm_b; // ADPCM-B engine
574 };
575
576
577 // ======================> ymf288
578
579 class ymf288
580 {
581 public:
582 using fm_engine = fm_engine_base<opna_registers>;
583 static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS;
584 static constexpr uint32_t SSG_OUTPUTS = 1;
585 static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS;
586 using output_data = ymfm_output<OUTPUTS>;
587
588 // constructor
589 ymf288(ymfm_interface &intf);
590
591 // configuration
592 void ssg_override(ssg_override &intf) { m_ssg.override(intf); }
593 void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(); }
594
595 // reset
596 void reset();
597
598 // save/restore
599 void save_restore(ymfm_saved_state &state);
600
601 // pass-through helpers
602 uint32_t sample_rate(uint32_t input_clock) const
603 {
604 switch (m_fidelity)
605 {
606 case OPN_FIDELITY_MIN: return input_clock / 144;
607 case OPN_FIDELITY_MED: return input_clock / 144;
608 default:
609 case OPN_FIDELITY_MAX: return input_clock / 16;
610 }
611 }
612 uint32_t ssg_effective_clock(uint32_t input_clock) const { return input_clock / 4; }
613 void invalidate_caches() { m_fm.invalidate_caches(); }
614
615 // read access
616 uint8_t read_status();
617 uint8_t read_data();
618 uint8_t read_status_hi();
619 uint8_t read(uint32_t offset);
620
621 // write access
622 void write_address(uint8_t data);
623 void write_data(uint8_t data);
624 void write_address_hi(uint8_t data);
625 void write_data_hi(uint8_t data);
626 void write(uint32_t offset, uint8_t data);
627
628 // generate one sample of sound
629 void generate(output_data *output, uint32_t numsamples = 1);
630
631 protected:
632 // internal helpers
633 bool ymf288_mode() { return ((m_fm.regs().read(0x20) & 0x02) != 0); }
634 void update_prescale();
635 void clock_fm_and_adpcm();
636
637 // internal state
638 opn_fidelity m_fidelity; // configured fidelity
639 uint16_t m_address; // address register
640 uint8_t m_fm_samples_per_output; // how many samples to repeat
641 uint8_t m_irq_enable; // IRQ enable register
642 uint8_t m_flag_control; // flag control register
643 fm_engine::output_data m_last_fm; // last FM output
644 fm_engine m_fm; // core FM engine
645 ssg_engine m_ssg; // SSG engine
646 ssg_resampler<output_data, 2, true> m_ssg_resampler; // SSG resampler helper
647 adpcm_a_engine m_adpcm_a; // ADPCM-A engine
648 };
649
650
651 // ======================> ym2610/ym2610b
652
653 class ym2610
654 {
655 static constexpr uint8_t EOS_FLAGS_MASK = 0xbf;
656
657 public:
658 using fm_engine = fm_engine_base<opna_registers>;
659 static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS;
660 static constexpr uint32_t SSG_OUTPUTS = 1;
661 static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS;
662 using output_data = ymfm_output<OUTPUTS>;
663
664 // constructor
665 ym2610(ymfm_interface &intf, uint8_t channel_mask = 0x36);
666
667 // configuration
668 void ssg_override(ssg_override &intf) { m_ssg.override(intf); }
669 void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(); }
670
671 // reset
672 void reset();
673
674 // save/restore
675 void save_restore(ymfm_saved_state &state);
676
677 // pass-through helpers
678 uint32_t sample_rate(uint32_t input_clock) const
679 {
680 switch (m_fidelity)
681 {
682 case OPN_FIDELITY_MIN: return input_clock / 144;
683 case OPN_FIDELITY_MED: return input_clock / 144;
684 default:
685 case OPN_FIDELITY_MAX: return input_clock / 16;
686 }
687 }
688 uint32_t ssg_effective_clock(uint32_t input_clock) const { return input_clock / 4; }
689 void invalidate_caches() { m_fm.invalidate_caches(); }
690
691 // read access
692 uint8_t read_status();
693 uint8_t read_data();
694 uint8_t read_status_hi();
695 uint8_t read_data_hi();
696 uint8_t read(uint32_t offset);
697
698 // write access
699 void write_address(uint8_t data);
700 void write_data(uint8_t data);
701 void write_address_hi(uint8_t data);
702 void write_data_hi(uint8_t data);
703 void write(uint32_t offset, uint8_t data);
704
705 // generate one sample of sound
706 void generate(output_data *output, uint32_t numsamples = 1);
707
708 // get the engine
709 fm_engine* debug_fm_engine() { return &m_fm; }
710 ssg_engine* debug_ssg_engine() { return &m_ssg; }
711 adpcm_a_engine* debug_adpcm_a_engine() { return &m_adpcm_a; }
712 adpcm_b_engine* debug_adpcm_b_engine() { return &m_adpcm_b; }
713
714 protected:
715 // internal helpers
716 void update_prescale();
717 void clock_fm_and_adpcm();
718
719 // internal state
720 opn_fidelity m_fidelity; // configured fidelity
721 uint16_t m_address; // address register
722 uint8_t const m_fm_mask; // FM channel mask
723 uint8_t m_fm_samples_per_output; // how many samples to repeat
724 uint8_t m_eos_status; // end-of-sample signals
725 uint8_t m_flag_mask; // flag mask control
726 fm_engine::output_data m_last_fm; // last FM output
727 fm_engine m_fm; // core FM engine
728 ssg_engine m_ssg; // core FM engine
729 ssg_resampler<output_data, 2, true> m_ssg_resampler; // SSG resampler helper
730 adpcm_a_engine m_adpcm_a; // ADPCM-A engine
731 adpcm_b_engine m_adpcm_b; // ADPCM-B engine
732 };
733
734 class ym2610b : public ym2610
735 {
736 public:
737 // constructor
738 ym2610b(ymfm_interface &intf) : ym2610(intf, 0x3f) { }
739 };
740
741
742 // ======================> ym2612
743
744 class ym2612
745 {
746 public:
747 using fm_engine = fm_engine_base<opna_registers>;
748 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
749 using output_data = fm_engine::output_data;
750
751 // constructor
752 ym2612(ymfm_interface &intf);
753
754 // reset
755 void reset();
756
757 // save/restore
758 void save_restore(ymfm_saved_state &state);
759
760 // pass-through helpers
761 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
762 void invalidate_caches() { m_fm.invalidate_caches(); }
763
764 // read access
765 uint8_t read_status();
766 uint8_t read(uint32_t offset);
767
768 // write access
769 void write_address(uint8_t data);
770 void write_data(uint8_t data);
771 void write_address_hi(uint8_t data);
772 void write_data_hi(uint8_t data);
773 void write(uint32_t offset, uint8_t data);
774
775 // generate one sample of sound
776 void generate(output_data *output, uint32_t numsamples = 1);
777
778 // get the engine
779 fm_engine* debug_engine() { return &m_fm; }
780
781 // get DAC state
782 uint16_t debug_dac_data() { return m_dac_data; }
783 uint8_t debug_dac_enable() { return m_dac_enable; }
784
785 protected:
786 // simulate the DAC discontinuity
787 int32_t dac_discontinuity(int32_t value) const { return (value < 0) ? (value - 3) : (value + 4); }
788
789 // internal state
790 uint16_t m_address; // address register
791 uint16_t m_dac_data; // 9-bit DAC data
792 uint8_t m_dac_enable; // DAC enabled?
793 fm_engine m_fm; // core FM engine
794 };
795
796
797 // ======================> ym3438
798
799 class ym3438 : public ym2612
800 {
801 public:
802 ym3438(ymfm_interface &intf) : ym2612(intf) { }
803
804 // generate one sample of sound
805 void generate(output_data *output, uint32_t numsamples = 1);
806 };
807
808
809 // ======================> ymf276
810
811 class ymf276 : public ym2612
812 {
813 public:
814 ymf276(ymfm_interface &intf) : ym2612(intf) { }
815
816 // generate one sample of sound
817 void generate(output_data *output, uint32_t numsamples);
818 };
819
820 }
821
822
823 #endif // YMFM_OPN_H