Debian Codegraph @

thirdparty/ymfm/src/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 protected:
469 // internal helpers
470 void update_prescale(uint8_t prescale);
471 void clock_fm();
472
473 // internal state
474 opn_fidelity m_fidelity; // configured fidelity
475 uint8_t m_address; // address register
476 uint8_t m_fm_samples_per_output; // how many samples to repeat
477 fm_engine::output_data m_last_fm; // last FM output
478 fm_engine m_fm; // core FM engine
479 ssg_engine m_ssg; // SSG engine
480 ssg_resampler<output_data, 1, false> m_ssg_resampler; // SSG resampler helper
481 };
482
483
484
485 //*********************************************************
486 // OPNA IMPLEMENTATION CLASSES
487 //*********************************************************
488
489 // ======================> ym2608
490
491 class ym2608
492 {
493 static constexpr uint8_t STATUS_ADPCM_B_EOS = 0x04;
494 static constexpr uint8_t STATUS_ADPCM_B_BRDY = 0x08;
495 static constexpr uint8_t STATUS_ADPCM_B_ZERO = 0x10;
496 static constexpr uint8_t STATUS_ADPCM_B_PLAYING = 0x20;
497
498 public:
499 using fm_engine = fm_engine_base<opna_registers>;
500 static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS;
501 static constexpr uint32_t SSG_OUTPUTS = 1;
502 static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS;
503 using output_data = ymfm_output<OUTPUTS>;
504
505 // constructor
506 ym2608(ymfm_interface &intf);
507
508 // configuration
509 void ssg_override(ssg_override &intf) { m_ssg.override(intf); }
510 void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(m_fm.clock_prescale()); }
511
512 // reset
513 void reset();
514
515 // save/restore
516 void save_restore(ymfm_saved_state &state);
517
518 // pass-through helpers
519 uint32_t sample_rate(uint32_t input_clock) const
520 {
521 switch (m_fidelity)
522 {
523 case OPN_FIDELITY_MIN: return input_clock / 48;
524 case OPN_FIDELITY_MED: return input_clock / 24;
525 default:
526 case OPN_FIDELITY_MAX: return input_clock / 8;
527 }
528 }
529 uint32_t ssg_effective_clock(uint32_t input_clock) const { uint32_t scale = m_fm.clock_prescale() * 2 / 3; return input_clock / scale; }
530 void invalidate_caches() { m_fm.invalidate_caches(); }
531
532 // read access
533 uint8_t read_status();
534 uint8_t read_data();
535 uint8_t read_status_hi();
536 uint8_t read_data_hi();
537 uint8_t read(uint32_t offset);
538
539 // write access
540 void write_address(uint8_t data);
541 void write_data(uint8_t data);
542 void write_address_hi(uint8_t data);
543 void write_data_hi(uint8_t data);
544 void write(uint32_t offset, uint8_t data);
545
546 // generate one sample of sound
547 void generate(output_data *output, uint32_t numsamples = 1);
548
549 protected:
550 // internal helpers
551 void update_prescale(uint8_t prescale);
552 void clock_fm_and_adpcm();
553
554 // internal state
555 opn_fidelity m_fidelity; // configured fidelity
556 uint16_t m_address; // address register
557 uint8_t m_fm_samples_per_output; // how many samples to repeat
558 uint8_t m_irq_enable; // IRQ enable register
559 uint8_t m_flag_control; // flag control register
560 fm_engine::output_data m_last_fm; // last FM output
561 fm_engine m_fm; // core FM engine
562 ssg_engine m_ssg; // SSG engine
563 ssg_resampler<output_data, 2, true> m_ssg_resampler; // SSG resampler helper
564 adpcm_a_engine m_adpcm_a; // ADPCM-A engine
565 adpcm_b_engine m_adpcm_b; // ADPCM-B engine
566 };
567
568
569 // ======================> ymf288
570
571 class ymf288
572 {
573 public:
574 using fm_engine = fm_engine_base<opna_registers>;
575 static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS;
576 static constexpr uint32_t SSG_OUTPUTS = 1;
577 static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS;
578 using output_data = ymfm_output<OUTPUTS>;
579
580 // constructor
581 ymf288(ymfm_interface &intf);
582
583 // configuration
584 void ssg_override(ssg_override &intf) { m_ssg.override(intf); }
585 void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(); }
586
587 // reset
588 void reset();
589
590 // save/restore
591 void save_restore(ymfm_saved_state &state);
592
593 // pass-through helpers
594 uint32_t sample_rate(uint32_t input_clock) const
595 {
596 switch (m_fidelity)
597 {
598 case OPN_FIDELITY_MIN: return input_clock / 144;
599 case OPN_FIDELITY_MED: return input_clock / 144;
600 default:
601 case OPN_FIDELITY_MAX: return input_clock / 16;
602 }
603 }
604 uint32_t ssg_effective_clock(uint32_t input_clock) const { return input_clock / 4; }
605 void invalidate_caches() { m_fm.invalidate_caches(); }
606
607 // read access
608 uint8_t read_status();
609 uint8_t read_data();
610 uint8_t read_status_hi();
611 uint8_t read(uint32_t offset);
612
613 // write access
614 void write_address(uint8_t data);
615 void write_data(uint8_t data);
616 void write_address_hi(uint8_t data);
617 void write_data_hi(uint8_t data);
618 void write(uint32_t offset, uint8_t data);
619
620 // generate one sample of sound
621 void generate(output_data *output, uint32_t numsamples = 1);
622
623 protected:
624 // internal helpers
625 bool ymf288_mode() { return ((m_fm.regs().read(0x20) & 0x02) != 0); }
626 void update_prescale();
627 void clock_fm_and_adpcm();
628
629 // internal state
630 opn_fidelity m_fidelity; // configured fidelity
631 uint16_t m_address; // address register
632 uint8_t m_fm_samples_per_output; // how many samples to repeat
633 uint8_t m_irq_enable; // IRQ enable register
634 uint8_t m_flag_control; // flag control register
635 fm_engine::output_data m_last_fm; // last FM output
636 fm_engine m_fm; // core FM engine
637 ssg_engine m_ssg; // SSG engine
638 ssg_resampler<output_data, 2, true> m_ssg_resampler; // SSG resampler helper
639 adpcm_a_engine m_adpcm_a; // ADPCM-A engine
640 };
641
642
643 // ======================> ym2610/ym2610b
644
645 class ym2610
646 {
647 static constexpr uint8_t EOS_FLAGS_MASK = 0xbf;
648
649 public:
650 using fm_engine = fm_engine_base<opna_registers>;
651 static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS;
652 static constexpr uint32_t SSG_OUTPUTS = 1;
653 static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS;
654 using output_data = ymfm_output<OUTPUTS>;
655
656 // constructor
657 ym2610(ymfm_interface &intf, uint8_t channel_mask = 0x36);
658
659 // configuration
660 void ssg_override(ssg_override &intf) { m_ssg.override(intf); }
661 void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(); }
662
663 // reset
664 void reset();
665
666 // save/restore
667 void save_restore(ymfm_saved_state &state);
668
669 // pass-through helpers
670 uint32_t sample_rate(uint32_t input_clock) const
671 {
672 switch (m_fidelity)
673 {
674 case OPN_FIDELITY_MIN: return input_clock / 144;
675 case OPN_FIDELITY_MED: return input_clock / 144;
676 default:
677 case OPN_FIDELITY_MAX: return input_clock / 16;
678 }
679 }
680 uint32_t ssg_effective_clock(uint32_t input_clock) const { return input_clock / 4; }
681 void invalidate_caches() { m_fm.invalidate_caches(); }
682
683 // read access
684 uint8_t read_status();
685 uint8_t read_data();
686 uint8_t read_status_hi();
687 uint8_t read_data_hi();
688 uint8_t read(uint32_t offset);
689
690 // write access
691 void write_address(uint8_t data);
692 void write_data(uint8_t data);
693 void write_address_hi(uint8_t data);
694 void write_data_hi(uint8_t data);
695 void write(uint32_t offset, uint8_t data);
696
697 // generate one sample of sound
698 void generate(output_data *output, uint32_t numsamples = 1);
699
700 protected:
701 // internal helpers
702 void update_prescale();
703 void clock_fm_and_adpcm();
704
705 // internal state
706 opn_fidelity m_fidelity; // configured fidelity
707 uint16_t m_address; // address register
708 uint8_t const m_fm_mask; // FM channel mask
709 uint8_t m_fm_samples_per_output; // how many samples to repeat
710 uint8_t m_eos_status; // end-of-sample signals
711 uint8_t m_flag_mask; // flag mask control
712 fm_engine::output_data m_last_fm; // last FM output
713 fm_engine m_fm; // core FM engine
714 ssg_engine m_ssg; // core FM engine
715 ssg_resampler<output_data, 2, true> m_ssg_resampler; // SSG resampler helper
716 adpcm_a_engine m_adpcm_a; // ADPCM-A engine
717 adpcm_b_engine m_adpcm_b; // ADPCM-B engine
718 };
719
720 class ym2610b : public ym2610
721 {
722 public:
723 // constructor
724 ym2610b(ymfm_interface &intf) : ym2610(intf, 0x3f) { }
725 };
726
727
728 // ======================> ym2612
729
730 class ym2612
731 {
732 public:
733 using fm_engine = fm_engine_base<opna_registers>;
734 static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS;
735 using output_data = fm_engine::output_data;
736
737 // constructor
738 ym2612(ymfm_interface &intf);
739
740 // reset
741 void reset();
742
743 // save/restore
744 void save_restore(ymfm_saved_state &state);
745
746 // pass-through helpers
747 uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); }
748 void invalidate_caches() { m_fm.invalidate_caches(); }
749
750 // read access
751 uint8_t read_status();
752 uint8_t read(uint32_t offset);
753
754 // write access
755 void write_address(uint8_t data);
756 void write_data(uint8_t data);
757 void write_address_hi(uint8_t data);
758 void write_data_hi(uint8_t data);
759 void write(uint32_t offset, uint8_t data);
760
761 // generate one sample of sound
762 void generate(output_data *output, uint32_t numsamples = 1);
763
764 protected:
765 // simulate the DAC discontinuity
766 constexpr int32_t dac_discontinuity(int32_t value) const { return (value < 0) ? (value - 3) : (value + 4); }
767
768 // internal state
769 uint16_t m_address; // address register
770 uint16_t m_dac_data; // 9-bit DAC data
771 uint8_t m_dac_enable; // DAC enabled?
772 fm_engine m_fm; // core FM engine
773 };
774
775
776 // ======================> ym3438
777
778 class ym3438 : public ym2612
779 {
780 public:
781 ym3438(ymfm_interface &intf) : ym2612(intf) { }
782
783 // generate one sample of sound
784 void generate(output_data *output, uint32_t numsamples = 1);
785 };
786
787
788 // ======================> ymf276
789
790 class ymf276 : public ym2612
791 {
792 public:
793 ymf276(ymfm_interface &intf) : ym2612(intf) { }
794
795 // generate one sample of sound
796 void generate(output_data *output, uint32_t numsamples);
797 };
798
799 }
800
801
802 #endif // YMFM_OPN_H