| 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 |
return (!IsOpnA ? chnum : ((chnum % 3) + 0x100 * (chnum / 3))); |
| 150 |
} |
| 151 |
|
| 152 |
// map operator number to register offset |
| 153 |
static constexpr uint32_t operator_offset(uint32_t opnum) |
| 154 |
{ |
| 155 |
/*assert(opnum < OPERATORS); |
| 156 |
if (!IsOpnA) |
| 157 |
return opnum + opnum / 3; |
| 158 |
else |
| 159 |
return (opnum % 12) + ((opnum % 12) / 3) + 0x100 * (opnum / 12);*/ |
| 160 |
return (!IsOpnA ? (opnum + opnum / 3) : ((opnum % 12) + ((opnum % 12) / 3) + 0x100 * (opnum / 12))); |
| 161 |
} |
| 162 |
|
| 163 |
// return an array of operator indices for each channel |
| 164 |
struct operator_mapping { uint32_t chan[CHANNELS]; }; |
| 165 |
void operator_map(operator_mapping &dest) const; |
| 166 |
|
| 167 |
// read a register value |
| 168 |
uint8_t read(uint16_t index) const { return m_regdata[index]; } |
| 169 |
|
| 170 |
// handle writes to the register array |
| 171 |
bool write(uint16_t index, uint8_t data, uint32_t &chan, uint32_t &opmask); |
| 172 |
|
| 173 |
// clock the noise and LFO, if present, returning LFO PM value |
| 174 |
int32_t clock_noise_and_lfo(); |
| 175 |
|
| 176 |
// reset the LFO |
| 177 |
void reset_lfo() { m_lfo_counter = 0; } |
| 178 |
|
| 179 |
// return the AM offset from LFO for the given channel |
| 180 |
uint32_t lfo_am_offset(uint32_t choffs) const; |
| 181 |
|
| 182 |
// return LFO/noise states |
| 183 |
uint32_t noise_state() const { return 0; } |
| 184 |
|
| 185 |
// caching helpers |
| 186 |
void cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache); |
| 187 |
|
| 188 |
// compute the phase step, given a PM value |
| 189 |
uint32_t compute_phase_step(uint32_t choffs, uint32_t opoffs, opdata_cache const &cache, int32_t lfo_raw_pm); |
| 190 |
|
| 191 |
// log a key-on event |
| 192 |
std::string log_keyon(uint32_t choffs, uint32_t opoffs); |
| 193 |
|
| 194 |
// system-wide registers |
| 195 |
uint32_t test() const { return byte(0x21, 0, 8); } |
| 196 |
uint32_t lfo_enable() const { return IsOpnA ? byte(0x22, 3, 1) : 0; } |
| 197 |
uint32_t lfo_rate() const { return IsOpnA ? byte(0x22, 0, 3) : 0; } |
| 198 |
uint32_t timer_a_value() const { return word(0x24, 0, 8, 0x25, 0, 2); } |
| 199 |
uint32_t timer_b_value() const { return byte(0x26, 0, 8); } |
| 200 |
uint32_t csm() const { return (byte(0x27, 6, 2) == 2); } |
| 201 |
uint32_t multi_freq() const { return (byte(0x27, 6, 2) != 0); } |
| 202 |
uint32_t reset_timer_b() const { return byte(0x27, 5, 1); } |
| 203 |
uint32_t reset_timer_a() const { return byte(0x27, 4, 1); } |
| 204 |
uint32_t enable_timer_b() const { return byte(0x27, 3, 1); } |
| 205 |
uint32_t enable_timer_a() const { return byte(0x27, 2, 1); } |
| 206 |
uint32_t load_timer_b() const { return byte(0x27, 1, 1); } |
| 207 |
uint32_t load_timer_a() const { return byte(0x27, 0, 1); } |
| 208 |
uint32_t multi_block_freq(uint32_t num) const { return word(0xac, 0, 6, 0xa8, 0, 8, num); } |
| 209 |
|
| 210 |
// per-channel registers |
| 211 |
uint32_t ch_block_freq(uint32_t choffs) const { return word(0xa4, 0, 6, 0xa0, 0, 8, choffs); } |
| 212 |
uint32_t ch_feedback(uint32_t choffs) const { return byte(0xb0, 3, 3, choffs); } |
| 213 |
uint32_t ch_algorithm(uint32_t choffs) const { return byte(0xb0, 0, 3, choffs); } |
| 214 |
uint32_t ch_output_any(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 6, 2, choffs) : 1; } |
| 215 |
uint32_t ch_output_0(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 7, 1, choffs) : 1; } |
| 216 |
uint32_t ch_output_1(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 6, 1, choffs) : 0; } |
| 217 |
uint32_t ch_output_2(uint32_t /*choffs*/) const { return 0; } |
| 218 |
uint32_t ch_output_3(uint32_t /*choffs*/) const { return 0; } |
| 219 |
uint32_t ch_lfo_am_sens(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 4, 2, choffs) : 0; } |
| 220 |
uint32_t ch_lfo_pm_sens(uint32_t choffs) const { return IsOpnA ? byte(0xb4, 0, 3, choffs) : 0; } |
| 221 |
|
| 222 |
// per-operator registers |
| 223 |
uint32_t op_detune(uint32_t opoffs) const { return byte(0x30, 4, 3, opoffs); } |
| 224 |
uint32_t op_multiple(uint32_t opoffs) const { return byte(0x30, 0, 4, opoffs); } |
| 225 |
uint32_t op_total_level(uint32_t opoffs) const { return byte(0x40, 0, 7, opoffs); } |
| 226 |
uint32_t op_ksr(uint32_t opoffs) const { return byte(0x50, 6, 2, opoffs); } |
| 227 |
uint32_t op_attack_rate(uint32_t opoffs) const { return byte(0x50, 0, 5, opoffs); } |
| 228 |
uint32_t op_decay_rate(uint32_t opoffs) const { return byte(0x60, 0, 5, opoffs); } |
| 229 |
uint32_t op_lfo_am_enable(uint32_t opoffs) const { return IsOpnA ? byte(0x60, 7, 1, opoffs) : 0; } |
| 230 |
uint32_t op_sustain_rate(uint32_t opoffs) const { return byte(0x70, 0, 5, opoffs); } |
| 231 |
uint32_t op_sustain_level(uint32_t opoffs) const { return byte(0x80, 4, 4, opoffs); } |
| 232 |
uint32_t op_release_rate(uint32_t opoffs) const { return byte(0x80, 0, 4, opoffs); } |
| 233 |
uint32_t op_ssg_eg_enable(uint32_t opoffs) const { return byte(0x90, 3, 1, opoffs); } |
| 234 |
uint32_t op_ssg_eg_mode(uint32_t opoffs) const { return byte(0x90, 0, 3, opoffs); } |
| 235 |
|
| 236 |
protected: |
| 237 |
// return a bitfield extracted from a byte |
| 238 |
uint32_t byte(uint32_t offset, uint32_t start, uint32_t count, uint32_t extra_offset = 0) const |
| 239 |
{ |
| 240 |
return bitfield(m_regdata[offset + extra_offset], start, count); |
| 241 |
} |
| 242 |
|
| 243 |
// return a bitfield extracted from a pair of bytes, MSBs listed first |
| 244 |
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 |
| 245 |
{ |
| 246 |
return (byte(offset1, start1, count1, extra_offset) << count2) | byte(offset2, start2, count2, extra_offset); |
| 247 |
} |
| 248 |
|
| 249 |
// internal state |
| 250 |
uint32_t m_lfo_counter; // LFO counter |
| 251 |
uint8_t m_lfo_am; // current LFO AM value |
| 252 |
uint8_t m_regdata[REGISTERS]; // register data |
| 253 |
uint16_t m_waveform[WAVEFORMS][WAVEFORM_LENGTH]; // waveforms |
| 254 |
}; |
| 255 |
|
| 256 |
using opn_registers = opn_registers_base<false>; |
| 257 |
using opna_registers = opn_registers_base<true>; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
//********************************************************* |
| 262 |
// OPN IMPLEMENTATION CLASSES |
| 263 |
//********************************************************* |
| 264 |
|
| 265 |
// A note about prescaling and sample rates. |
| 266 |
// |
| 267 |
// YM2203, YM2608, and YM2610 contain an onboard SSG (basically, a YM2149). |
| 268 |
// In order to properly generate sound at fully fidelity, the output sample |
| 269 |
// rate of the YM2149 must be input_clock / 8. This is much higher than the |
| 270 |
// FM needs, but in the interest of keeping things simple, the OPN generate |
| 271 |
// functions will output at the higher rate and just replicate the last FM |
| 272 |
// sample as many times as needed. |
| 273 |
// |
| 274 |
// To make things even more complicated, the YM2203 and YM2608 allow for |
| 275 |
// software-controlled prescaling, which affects the FM and SSG clocks in |
| 276 |
// different ways. There are three settings: divide by 6/4 (FM/SSG); divide |
| 277 |
// by 3/2; and divide by 2/1. |
| 278 |
// |
| 279 |
// Thus, the minimum output sample rate needed by each part of the chip |
| 280 |
// varies with the prescale as follows: |
| 281 |
// |
| 282 |
// ---- YM2203 ----- ---- YM2608 ----- ---- YM2610 ----- |
| 283 |
// Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate |
| 284 |
// 6 /72 /16 /144 /32 /144 /32 |
| 285 |
// 3 /36 /8 /72 /16 |
| 286 |
// 2 /24 /4 /48 /8 |
| 287 |
// |
| 288 |
// If we standardized on the fastest SSG rate, we'd end up with the following |
| 289 |
// (ratios are output_samples:source_samples): |
| 290 |
// |
| 291 |
// ---- YM2203 ----- ---- YM2608 ----- ---- YM2610 ----- |
| 292 |
// rate = clock/4 rate = clock/8 rate = clock/16 |
| 293 |
// Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate |
| 294 |
// 6 18:1 4:1 18:1 4:1 9:1 2:1 |
| 295 |
// 3 9:1 2:1 9:1 2:1 |
| 296 |
// 2 6:1 1:1 6:1 1:1 |
| 297 |
// |
| 298 |
// However, that's a pretty big performance hit for minimal gain. Going to |
| 299 |
// the other extreme, we could standardize on the fastest FM rate, but then |
| 300 |
// at least one prescale case (3) requires the FM to be smeared across two |
| 301 |
// output samples: |
| 302 |
// |
| 303 |
// ---- YM2203 ----- ---- YM2608 ----- ---- YM2610 ----- |
| 304 |
// rate = clock/24 rate = clock/48 rate = clock/144 |
| 305 |
// Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate |
| 306 |
// 6 3:1 2:3 3:1 2:3 1:1 2:9 |
| 307 |
// 3 1.5:1 1:3 1.5:1 1:3 |
| 308 |
// 2 1:1 1:6 1:1 1:6 |
| 309 |
// |
| 310 |
// Stepping back one factor of 2 addresses that issue: |
| 311 |
// |
| 312 |
// ---- YM2203 ----- ---- YM2608 ----- ---- YM2610 ----- |
| 313 |
// rate = clock/12 rate = clock/24 rate = clock/144 |
| 314 |
// Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate |
| 315 |
// 6 6:1 4:3 6:1 4:3 1:1 2:9 |
| 316 |
// 3 3:1 2:3 3:1 2:3 |
| 317 |
// 2 2:1 1:3 2:1 1:3 |
| 318 |
// |
| 319 |
// This gives us three levels of output fidelity: |
| 320 |
// OPN_FIDELITY_MAX -- highest sample rate, using fastest SSG rate |
| 321 |
// OPN_FIDELITY_MIN -- lowest sample rate, using fastest FM rate |
| 322 |
// OPN_FIDELITY_MED -- medium sample rate such that FM is never smeared |
| 323 |
// |
| 324 |
// At the maximum clocks for YM2203/YM2608 (4Mhz/8MHz), these rates will |
| 325 |
// end up as: |
| 326 |
// OPN_FIDELITY_MAX = 1000kHz |
| 327 |
// OPN_FIDELITY_MIN = 166kHz |
| 328 |
// OPN_FIEDLITY_MED = 333kHz |
| 329 |
|
| 330 |
|
| 331 |
// ======================> opn_fidelity |
| 332 |
|
| 333 |
enum opn_fidelity : uint8_t |
| 334 |
{ |
| 335 |
OPN_FIDELITY_MAX, |
| 336 |
OPN_FIDELITY_MIN, |
| 337 |
OPN_FIDELITY_MED, |
| 338 |
|
| 339 |
OPN_FIDELITY_DEFAULT = OPN_FIDELITY_MAX |
| 340 |
}; |
| 341 |
|
| 342 |
|
| 343 |
// ======================> ssg_resampler |
| 344 |
|
| 345 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 346 |
class ssg_resampler |
| 347 |
{ |
| 348 |
private: |
| 349 |
// helper to add the last computed value to the sums, applying the given scale |
| 350 |
void add_last(int32_t &sum0, int32_t &sum1, int32_t &sum2, int32_t scale = 1); |
| 351 |
|
| 352 |
// helper to clock a new value and then add it to the sums, applying the given scale |
| 353 |
void clock_and_add(int32_t &sum0, int32_t &sum1, int32_t &sum2, int32_t scale = 1); |
| 354 |
|
| 355 |
// helper to write the sums to the appropriate outputs, applying the given |
| 356 |
// divisor to the final result |
| 357 |
void write_to_output(OutputType *output, int32_t sum0, int32_t sum1, int32_t sum2, int32_t divisor = 1); |
| 358 |
|
| 359 |
public: |
| 360 |
// constructor |
| 361 |
ssg_resampler(ssg_engine &ssg); |
| 362 |
|
| 363 |
// save/restore |
| 364 |
void save_restore(ymfm_saved_state &state); |
| 365 |
|
| 366 |
// get the current sample index |
| 367 |
uint32_t sampindex() const { return m_sampindex; } |
| 368 |
|
| 369 |
// configure the ratio |
| 370 |
void configure(uint8_t outsamples, uint8_t srcsamples); |
| 371 |
|
| 372 |
// resample |
| 373 |
void resample(OutputType *output, uint32_t numsamples) |
| 374 |
{ |
| 375 |
(this->*m_resampler)(output, numsamples); |
| 376 |
} |
| 377 |
|
| 378 |
private: |
| 379 |
// resample SSG output to the target at a rate of 1 SSG sample |
| 380 |
// to every n output samples |
| 381 |
template<int Multiplier> |
| 382 |
void resample_n_1(OutputType *output, uint32_t numsamples); |
| 383 |
|
| 384 |
// resample SSG output to the target at a rate of n SSG samples |
| 385 |
// to every 1 output sample |
| 386 |
template<int Divisor> |
| 387 |
void resample_1_n(OutputType *output, uint32_t numsamples); |
| 388 |
|
| 389 |
// resample SSG output to the target at a rate of 9 SSG samples |
| 390 |
// to every 2 output samples |
| 391 |
void resample_2_9(OutputType *output, uint32_t numsamples); |
| 392 |
|
| 393 |
// resample SSG output to the target at a rate of 3 SSG samples |
| 394 |
// to every 1 output sample |
| 395 |
void resample_1_3(OutputType *output, uint32_t numsamples); |
| 396 |
|
| 397 |
// resample SSG output to the target at a rate of 3 SSG samples |
| 398 |
// to every 2 output samples |
| 399 |
void resample_2_3(OutputType *output, uint32_t numsamples); |
| 400 |
|
| 401 |
// resample SSG output to the target at a rate of 3 SSG samples |
| 402 |
// to every 4 output samples |
| 403 |
void resample_4_3(OutputType *output, uint32_t numsamples); |
| 404 |
|
| 405 |
// no-op resampler |
| 406 |
void resample_nop(OutputType *output, uint32_t numsamples); |
| 407 |
|
| 408 |
// define a pointer type |
| 409 |
using resample_func = void (ssg_resampler::*)(OutputType *output, uint32_t numsamples); |
| 410 |
|
| 411 |
// internal state |
| 412 |
ssg_engine &m_ssg; |
| 413 |
uint32_t m_sampindex; |
| 414 |
resample_func m_resampler; |
| 415 |
ssg_engine::output_data m_last; |
| 416 |
}; |
| 417 |
|
| 418 |
#if 0 |
| 419 |
// ======================> ym2203 |
| 420 |
|
| 421 |
class ym2203 |
| 422 |
{ |
| 423 |
public: |
| 424 |
using fm_engine = fm_engine_base<opn_registers>; |
| 425 |
static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS; |
| 426 |
static constexpr uint32_t SSG_OUTPUTS = ssg_engine::OUTPUTS; |
| 427 |
static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS; |
| 428 |
using output_data = ymfm_output<OUTPUTS>; |
| 429 |
|
| 430 |
// constructor |
| 431 |
ym2203(ymfm_interface &intf); |
| 432 |
|
| 433 |
// configuration |
| 434 |
void ssg_override(ssg_override &intf) { m_ssg.override(intf); } |
| 435 |
void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(m_fm.clock_prescale()); } |
| 436 |
|
| 437 |
// reset |
| 438 |
void reset(); |
| 439 |
|
| 440 |
// save/restore |
| 441 |
void save_restore(ymfm_saved_state &state); |
| 442 |
|
| 443 |
// pass-through helpers |
| 444 |
uint32_t sample_rate(uint32_t input_clock) const |
| 445 |
{ |
| 446 |
switch (m_fidelity) |
| 447 |
{ |
| 448 |
case OPN_FIDELITY_MIN: return input_clock / 24; |
| 449 |
case OPN_FIDELITY_MED: return input_clock / 12; |
| 450 |
default: |
| 451 |
case OPN_FIDELITY_MAX: return input_clock / 4; |
| 452 |
} |
| 453 |
} |
| 454 |
uint32_t ssg_effective_clock(uint32_t input_clock) const { uint32_t scale = m_fm.clock_prescale() * 2 / 3; return input_clock * 2 / scale; } |
| 455 |
void invalidate_caches() { m_fm.invalidate_caches(); } |
| 456 |
|
| 457 |
// read access |
| 458 |
uint8_t read_status(); |
| 459 |
uint8_t read_data(); |
| 460 |
uint8_t read(uint32_t offset); |
| 461 |
|
| 462 |
// write access |
| 463 |
void write_address(uint8_t data); |
| 464 |
void write_data(uint8_t data); |
| 465 |
void write(uint32_t offset, uint8_t data); |
| 466 |
|
| 467 |
// generate one sample of sound |
| 468 |
void generate(output_data *output, uint32_t numsamples = 1); |
| 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 |
#endif |
| 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 |
/* [BambooTracker] Separate sample generate with FM and SSG */ |
| 550 |
/*void generate(output_data *output, uint32_t numsamples = 1);*/ |
| 551 |
void generate_fm_adpcm(output_data *output, uint32_t numsamples = 1); |
| 552 |
void generate_ssg(output_data *output, uint32_t numsamples = 1); |
| 553 |
|
| 554 |
protected: |
| 555 |
// internal helpers |
| 556 |
void update_prescale(uint8_t prescale); |
| 557 |
void clock_fm_and_adpcm(); |
| 558 |
|
| 559 |
// internal state |
| 560 |
opn_fidelity m_fidelity; // configured fidelity |
| 561 |
uint16_t m_address; // address register |
| 562 |
uint8_t m_fm_samples_per_output; // how many samples to repeat |
| 563 |
uint8_t m_irq_enable; // IRQ enable register |
| 564 |
uint8_t m_flag_control; // flag control register |
| 565 |
fm_engine::output_data m_last_fm; // last FM output |
| 566 |
fm_engine m_fm; // core FM engine |
| 567 |
ssg_engine m_ssg; // SSG engine |
| 568 |
ssg_resampler<output_data, 2, true> m_ssg_resampler; // SSG resampler helper |
| 569 |
adpcm_a_engine m_adpcm_a; // ADPCM-A engine |
| 570 |
adpcm_b_engine m_adpcm_b; // ADPCM-B engine |
| 571 |
}; |
| 572 |
|
| 573 |
#if 0 |
| 574 |
// ======================> ymf288 |
| 575 |
|
| 576 |
class ymf288 |
| 577 |
{ |
| 578 |
public: |
| 579 |
using fm_engine = fm_engine_base<opna_registers>; |
| 580 |
static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS; |
| 581 |
static constexpr uint32_t SSG_OUTPUTS = 1; |
| 582 |
static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS; |
| 583 |
using output_data = ymfm_output<OUTPUTS>; |
| 584 |
|
| 585 |
// constructor |
| 586 |
ymf288(ymfm_interface &intf); |
| 587 |
|
| 588 |
// configuration |
| 589 |
void ssg_override(ssg_override &intf) { m_ssg.override(intf); } |
| 590 |
void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(); } |
| 591 |
|
| 592 |
// reset |
| 593 |
void reset(); |
| 594 |
|
| 595 |
// save/restore |
| 596 |
void save_restore(ymfm_saved_state &state); |
| 597 |
|
| 598 |
// pass-through helpers |
| 599 |
uint32_t sample_rate(uint32_t input_clock) const |
| 600 |
{ |
| 601 |
switch (m_fidelity) |
| 602 |
{ |
| 603 |
case OPN_FIDELITY_MIN: return input_clock / 144; |
| 604 |
case OPN_FIDELITY_MED: return input_clock / 144; |
| 605 |
default: |
| 606 |
case OPN_FIDELITY_MAX: return input_clock / 16; |
| 607 |
} |
| 608 |
} |
| 609 |
uint32_t ssg_effective_clock(uint32_t input_clock) const { return input_clock / 4; } |
| 610 |
void invalidate_caches() { m_fm.invalidate_caches(); } |
| 611 |
|
| 612 |
// read access |
| 613 |
uint8_t read_status(); |
| 614 |
uint8_t read_data(); |
| 615 |
uint8_t read_status_hi(); |
| 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_address_hi(uint8_t data); |
| 622 |
void write_data_hi(uint8_t data); |
| 623 |
void write(uint32_t offset, uint8_t data); |
| 624 |
|
| 625 |
// generate one sample of sound |
| 626 |
void generate(output_data *output, uint32_t numsamples = 1); |
| 627 |
|
| 628 |
protected: |
| 629 |
// internal helpers |
| 630 |
bool ymf288_mode() { return ((m_fm.regs().read(0x20) & 0x02) != 0); } |
| 631 |
void update_prescale(); |
| 632 |
void clock_fm_and_adpcm(); |
| 633 |
|
| 634 |
// internal state |
| 635 |
opn_fidelity m_fidelity; // configured fidelity |
| 636 |
uint16_t m_address; // address register |
| 637 |
uint8_t m_fm_samples_per_output; // how many samples to repeat |
| 638 |
uint8_t m_irq_enable; // IRQ enable register |
| 639 |
uint8_t m_flag_control; // flag control register |
| 640 |
fm_engine::output_data m_last_fm; // last FM output |
| 641 |
fm_engine m_fm; // core FM engine |
| 642 |
ssg_engine m_ssg; // SSG engine |
| 643 |
ssg_resampler<output_data, 2, true> m_ssg_resampler; // SSG resampler helper |
| 644 |
adpcm_a_engine m_adpcm_a; // ADPCM-A engine |
| 645 |
}; |
| 646 |
|
| 647 |
|
| 648 |
// ======================> ym2610/ym2610b |
| 649 |
|
| 650 |
class ym2610 |
| 651 |
{ |
| 652 |
static constexpr uint8_t EOS_FLAGS_MASK = 0xbf; |
| 653 |
|
| 654 |
public: |
| 655 |
using fm_engine = fm_engine_base<opna_registers>; |
| 656 |
static constexpr uint32_t FM_OUTPUTS = fm_engine::OUTPUTS; |
| 657 |
static constexpr uint32_t SSG_OUTPUTS = 1; |
| 658 |
static constexpr uint32_t OUTPUTS = FM_OUTPUTS + SSG_OUTPUTS; |
| 659 |
using output_data = ymfm_output<OUTPUTS>; |
| 660 |
|
| 661 |
// constructor |
| 662 |
ym2610(ymfm_interface &intf, uint8_t channel_mask = 0x36); |
| 663 |
|
| 664 |
// configuration |
| 665 |
void ssg_override(ssg_override &intf) { m_ssg.override(intf); } |
| 666 |
void set_fidelity(opn_fidelity fidelity) { m_fidelity = fidelity; update_prescale(); } |
| 667 |
|
| 668 |
// reset |
| 669 |
void reset(); |
| 670 |
|
| 671 |
// save/restore |
| 672 |
void save_restore(ymfm_saved_state &state); |
| 673 |
|
| 674 |
// pass-through helpers |
| 675 |
uint32_t sample_rate(uint32_t input_clock) const |
| 676 |
{ |
| 677 |
switch (m_fidelity) |
| 678 |
{ |
| 679 |
case OPN_FIDELITY_MIN: return input_clock / 144; |
| 680 |
case OPN_FIDELITY_MED: return input_clock / 144; |
| 681 |
default: |
| 682 |
case OPN_FIDELITY_MAX: return input_clock / 16; |
| 683 |
} |
| 684 |
} |
| 685 |
uint32_t ssg_effective_clock(uint32_t input_clock) const { return input_clock / 4; } |
| 686 |
void invalidate_caches() { m_fm.invalidate_caches(); } |
| 687 |
|
| 688 |
// read access |
| 689 |
uint8_t read_status(); |
| 690 |
uint8_t read_data(); |
| 691 |
uint8_t read_status_hi(); |
| 692 |
uint8_t read_data_hi(); |
| 693 |
uint8_t read(uint32_t offset); |
| 694 |
|
| 695 |
// write access |
| 696 |
void write_address(uint8_t data); |
| 697 |
void write_data(uint8_t data); |
| 698 |
void write_address_hi(uint8_t data); |
| 699 |
void write_data_hi(uint8_t data); |
| 700 |
void write(uint32_t offset, uint8_t data); |
| 701 |
|
| 702 |
// generate one sample of sound |
| 703 |
void generate(output_data *output, uint32_t numsamples = 1); |
| 704 |
|
| 705 |
protected: |
| 706 |
// internal helpers |
| 707 |
void update_prescale(); |
| 708 |
void clock_fm_and_adpcm(); |
| 709 |
|
| 710 |
// internal state |
| 711 |
opn_fidelity m_fidelity; // configured fidelity |
| 712 |
uint16_t m_address; // address register |
| 713 |
uint8_t const m_fm_mask; // FM channel mask |
| 714 |
uint8_t m_fm_samples_per_output; // how many samples to repeat |
| 715 |
uint8_t m_eos_status; // end-of-sample signals |
| 716 |
uint8_t m_flag_mask; // flag mask control |
| 717 |
fm_engine::output_data m_last_fm; // last FM output |
| 718 |
fm_engine m_fm; // core FM engine |
| 719 |
ssg_engine m_ssg; // core FM engine |
| 720 |
ssg_resampler<output_data, 2, true> m_ssg_resampler; // SSG resampler helper |
| 721 |
adpcm_a_engine m_adpcm_a; // ADPCM-A engine |
| 722 |
adpcm_b_engine m_adpcm_b; // ADPCM-B engine |
| 723 |
}; |
| 724 |
|
| 725 |
class ym2610b : public ym2610 |
| 726 |
{ |
| 727 |
public: |
| 728 |
// constructor |
| 729 |
ym2610b(ymfm_interface &intf) : ym2610(intf, 0x3f) { } |
| 730 |
}; |
| 731 |
|
| 732 |
|
| 733 |
// ======================> ym2612 |
| 734 |
|
| 735 |
class ym2612 |
| 736 |
{ |
| 737 |
public: |
| 738 |
using fm_engine = fm_engine_base<opna_registers>; |
| 739 |
static constexpr uint32_t OUTPUTS = fm_engine::OUTPUTS; |
| 740 |
using output_data = fm_engine::output_data; |
| 741 |
|
| 742 |
// constructor |
| 743 |
ym2612(ymfm_interface &intf); |
| 744 |
|
| 745 |
// reset |
| 746 |
void reset(); |
| 747 |
|
| 748 |
// save/restore |
| 749 |
void save_restore(ymfm_saved_state &state); |
| 750 |
|
| 751 |
// pass-through helpers |
| 752 |
uint32_t sample_rate(uint32_t input_clock) const { return m_fm.sample_rate(input_clock); } |
| 753 |
void invalidate_caches() { m_fm.invalidate_caches(); } |
| 754 |
|
| 755 |
// read access |
| 756 |
uint8_t read_status(); |
| 757 |
uint8_t read(uint32_t offset); |
| 758 |
|
| 759 |
// write access |
| 760 |
void write_address(uint8_t data); |
| 761 |
void write_data(uint8_t data); |
| 762 |
void write_address_hi(uint8_t data); |
| 763 |
void write_data_hi(uint8_t data); |
| 764 |
void write(uint32_t offset, uint8_t data); |
| 765 |
|
| 766 |
// generate one sample of sound |
| 767 |
void generate(output_data *output, uint32_t numsamples = 1); |
| 768 |
|
| 769 |
protected: |
| 770 |
// simulate the DAC discontinuity |
| 771 |
constexpr int32_t dac_discontinuity(int32_t value) const { return (value < 0) ? (value - 3) : (value + 4); } |
| 772 |
|
| 773 |
// internal state |
| 774 |
uint16_t m_address; // address register |
| 775 |
uint16_t m_dac_data; // 9-bit DAC data |
| 776 |
uint8_t m_dac_enable; // DAC enabled? |
| 777 |
fm_engine m_fm; // core FM engine |
| 778 |
}; |
| 779 |
|
| 780 |
|
| 781 |
// ======================> ym3438 |
| 782 |
|
| 783 |
class ym3438 : public ym2612 |
| 784 |
{ |
| 785 |
public: |
| 786 |
ym3438(ymfm_interface &intf) : ym2612(intf) { } |
| 787 |
|
| 788 |
// generate one sample of sound |
| 789 |
void generate(output_data *output, uint32_t numsamples = 1); |
| 790 |
}; |
| 791 |
|
| 792 |
|
| 793 |
// ======================> ymf276 |
| 794 |
|
| 795 |
class ymf276 : public ym2612 |
| 796 |
{ |
| 797 |
public: |
| 798 |
ymf276(ymfm_interface &intf) : ym2612(intf) { } |
| 799 |
|
| 800 |
// generate one sample of sound |
| 801 |
void generate(output_data *output, uint32_t numsamples); |
| 802 |
}; |
| 803 |
#endif |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
#endif // YMFM_OPN_H |