| 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 |
#include "ymfm_opn.h" |
| 32 |
#include "ymfm_fm.ipp" |
| 33 |
|
| 34 |
namespace ymfm |
| 35 |
{ |
| 36 |
|
| 37 |
//********************************************************* |
| 38 |
// OPN/OPNA REGISTERS |
| 39 |
//********************************************************* |
| 40 |
|
| 41 |
//------------------------------------------------- |
| 42 |
// opn_registers_base - constructor |
| 43 |
//------------------------------------------------- |
| 44 |
|
| 45 |
template<bool IsOpnA> |
| 46 |
opn_registers_base<IsOpnA>::opn_registers_base() : |
| 47 |
m_lfo_counter(0), |
| 48 |
m_lfo_am(0) |
| 49 |
{ |
| 50 |
// create the waveforms |
| 51 |
for (uint32_t index = 0; index < WAVEFORM_LENGTH; index++) |
| 52 |
m_waveform[0][index] = abs_sin_attenuation(index) | (bitfield(index, 9) << 15); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
//------------------------------------------------- |
| 57 |
// reset - reset to initial state |
| 58 |
//------------------------------------------------- |
| 59 |
|
| 60 |
template<bool IsOpnA> |
| 61 |
void opn_registers_base<IsOpnA>::reset() |
| 62 |
{ |
| 63 |
std::fill_n(&m_regdata[0], REGISTERS, 0); |
| 64 |
if (IsOpnA) |
| 65 |
{ |
| 66 |
// enable output on both channels by default |
| 67 |
m_regdata[0xb4] = m_regdata[0xb5] = m_regdata[0xb6] = 0xc0; |
| 68 |
m_regdata[0x1b4] = m_regdata[0x1b5] = m_regdata[0x1b6] = 0xc0; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
//------------------------------------------------- |
| 74 |
// save_restore - save or restore the data |
| 75 |
//------------------------------------------------- |
| 76 |
|
| 77 |
template<bool IsOpnA> |
| 78 |
void opn_registers_base<IsOpnA>::save_restore(ymfm_saved_state &state) |
| 79 |
{ |
| 80 |
if (IsOpnA) |
| 81 |
{ |
| 82 |
state.save_restore(m_lfo_counter); |
| 83 |
state.save_restore(m_lfo_am); |
| 84 |
} |
| 85 |
state.save_restore(m_regdata); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
//------------------------------------------------- |
| 90 |
// operator_map - return an array of operator |
| 91 |
// indices for each channel; for OPN this is fixed |
| 92 |
//------------------------------------------------- |
| 93 |
|
| 94 |
template<> |
| 95 |
void opn_registers_base<false>::operator_map(operator_mapping &dest) const |
| 96 |
{ |
| 97 |
// Note that the channel index order is 0,2,1,3, so we bitswap the index. |
| 98 |
// |
| 99 |
// This is because the order in the map is: |
| 100 |
// carrier 1, carrier 2, modulator 1, modulator 2 |
| 101 |
// |
| 102 |
// But when wiring up the connections, the more natural order is: |
| 103 |
// carrier 1, modulator 1, carrier 2, modulator 2 |
| 104 |
static const operator_mapping s_fixed_map = |
| 105 |
{ { |
| 106 |
operator_list( 0, 6, 3, 9 ), // Channel 0 operators |
| 107 |
operator_list( 1, 7, 4, 10 ), // Channel 1 operators |
| 108 |
operator_list( 2, 8, 5, 11 ), // Channel 2 operators |
| 109 |
} }; |
| 110 |
dest = s_fixed_map; |
| 111 |
} |
| 112 |
|
| 113 |
template<> |
| 114 |
void opn_registers_base<true>::operator_map(operator_mapping &dest) const |
| 115 |
{ |
| 116 |
// Note that the channel index order is 0,2,1,3, so we bitswap the index. |
| 117 |
// |
| 118 |
// This is because the order in the map is: |
| 119 |
// carrier 1, carrier 2, modulator 1, modulator 2 |
| 120 |
// |
| 121 |
// But when wiring up the connections, the more natural order is: |
| 122 |
// carrier 1, modulator 1, carrier 2, modulator 2 |
| 123 |
static const operator_mapping s_fixed_map = |
| 124 |
{ { |
| 125 |
operator_list( 0, 6, 3, 9 ), // Channel 0 operators |
| 126 |
operator_list( 1, 7, 4, 10 ), // Channel 1 operators |
| 127 |
operator_list( 2, 8, 5, 11 ), // Channel 2 operators |
| 128 |
operator_list( 12, 18, 15, 21 ), // Channel 3 operators |
| 129 |
operator_list( 13, 19, 16, 22 ), // Channel 4 operators |
| 130 |
operator_list( 14, 20, 17, 23 ), // Channel 5 operators |
| 131 |
} }; |
| 132 |
dest = s_fixed_map; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
//------------------------------------------------- |
| 137 |
// write - handle writes to the register array |
| 138 |
//------------------------------------------------- |
| 139 |
|
| 140 |
template<bool IsOpnA> |
| 141 |
bool opn_registers_base<IsOpnA>::write(uint16_t index, uint8_t data, uint32_t &channel, uint32_t &opmask) |
| 142 |
{ |
| 143 |
assert(index < REGISTERS); |
| 144 |
|
| 145 |
// writes in the 0xa0-af/0x1a0-af region are handled as latched pairs |
| 146 |
// borrow unused registers 0xb8-bf/0x1b8-bf as temporary holding locations |
| 147 |
if ((index & 0xf0) == 0xa0) |
| 148 |
{ |
| 149 |
if (bitfield(index, 0, 2) == 3) |
| 150 |
return false; |
| 151 |
|
| 152 |
uint32_t latchindex = 0xb8 | bitfield(index, 3); |
| 153 |
if (IsOpnA) |
| 154 |
latchindex |= index & 0x100; |
| 155 |
|
| 156 |
// writes to the upper half just latch (only low 6 bits matter) |
| 157 |
if (bitfield(index, 2)) |
| 158 |
m_regdata[latchindex] = data | 0x80; |
| 159 |
|
| 160 |
// writes to the lower half only commit if the latch is there |
| 161 |
else if (bitfield(m_regdata[latchindex], 7)) |
| 162 |
{ |
| 163 |
m_regdata[index] = data; |
| 164 |
m_regdata[index | 4] = m_regdata[latchindex] & 0x3f; |
| 165 |
m_regdata[latchindex] = 0; |
| 166 |
} |
| 167 |
return false; |
| 168 |
} |
| 169 |
else if ((index & 0xf8) == 0xb8) |
| 170 |
{ |
| 171 |
// registers 0xb8-0xbf are used internally |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
// everything else is normal |
| 176 |
m_regdata[index] = data; |
| 177 |
|
| 178 |
// handle writes to the key on index |
| 179 |
if (index == 0x28) |
| 180 |
{ |
| 181 |
channel = bitfield(data, 0, 2); |
| 182 |
if (channel == 3) |
| 183 |
return false; |
| 184 |
if (IsOpnA) |
| 185 |
channel += bitfield(data, 2, 1) * 3; |
| 186 |
opmask = bitfield(data, 4, 4); |
| 187 |
return true; |
| 188 |
} |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
//------------------------------------------------- |
| 194 |
// clock_noise_and_lfo - clock the noise and LFO, |
| 195 |
// handling clock division, depth, and waveform |
| 196 |
// computations |
| 197 |
//------------------------------------------------- |
| 198 |
|
| 199 |
template<bool IsOpnA> |
| 200 |
int32_t opn_registers_base<IsOpnA>::clock_noise_and_lfo() |
| 201 |
{ |
| 202 |
// OPN has no noise generation |
| 203 |
|
| 204 |
// if LFO not enabled (not present on OPN), quick exit with 0s |
| 205 |
if (!IsOpnA || !lfo_enable()) |
| 206 |
{ |
| 207 |
m_lfo_counter = 0; |
| 208 |
|
| 209 |
// special case: if LFO is disabled on OPNA, it basically just keeps the counter |
| 210 |
// at 0; since position 0 gives an AM value of 0x3f, it is important to reflect |
| 211 |
// that here; for example, MegaDrive Venom plays some notes with LFO globally |
| 212 |
// disabled but enabling LFO on the operators, and it expects this added attenutation |
| 213 |
m_lfo_am = IsOpnA ? 0x3f : 0x00; |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
// this table is based on converting the frequencies in the applications |
| 218 |
// manual to clock dividers, based on the assumption of a 7-bit LFO value |
| 219 |
static uint8_t const lfo_max_count[8] = { 109, 78, 72, 68, 63, 45, 9, 6 }; |
| 220 |
uint32_t subcount = uint8_t(m_lfo_counter++); |
| 221 |
|
| 222 |
// when we cross the divider count, add enough to zero it and cause an |
| 223 |
// increment at bit 8; the 7-bit value lives from bits 8-14 |
| 224 |
if (subcount >= lfo_max_count[lfo_rate()]) |
| 225 |
{ |
| 226 |
// note: to match the published values this should be 0x100 - subcount; |
| 227 |
// however, tests on the hardware and nuked bear out an off-by-one |
| 228 |
// error exists that causes the max LFO rate to be faster than published |
| 229 |
m_lfo_counter += 0x101 - subcount; |
| 230 |
} |
| 231 |
|
| 232 |
// AM value is 7 bits, staring at bit 8; grab the low 6 directly |
| 233 |
m_lfo_am = bitfield(m_lfo_counter, 8, 6); |
| 234 |
|
| 235 |
// first half of the AM period (bit 6 == 0) is inverted |
| 236 |
if (bitfield(m_lfo_counter, 8+6) == 0) |
| 237 |
m_lfo_am ^= 0x3f; |
| 238 |
|
| 239 |
// PM value is 5 bits, starting at bit 10; grab the low 3 directly |
| 240 |
int32_t pm = bitfield(m_lfo_counter, 10, 3); |
| 241 |
|
| 242 |
// PM is reflected based on bit 3 |
| 243 |
if (bitfield(m_lfo_counter, 10+3)) |
| 244 |
pm ^= 7; |
| 245 |
|
| 246 |
// PM is negated based on bit 4 |
| 247 |
return bitfield(m_lfo_counter, 10+4) ? -pm : pm; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
//------------------------------------------------- |
| 252 |
// lfo_am_offset - return the AM offset from LFO |
| 253 |
// for the given channel |
| 254 |
//------------------------------------------------- |
| 255 |
|
| 256 |
template<bool IsOpnA> |
| 257 |
uint32_t opn_registers_base<IsOpnA>::lfo_am_offset(uint32_t choffs) const |
| 258 |
{ |
| 259 |
// shift value for AM sensitivity is [7, 3, 1, 0], |
| 260 |
// mapping to values of [0, 1.4, 5.9, and 11.8dB] |
| 261 |
uint32_t am_shift = (1 << (ch_lfo_am_sens(choffs) ^ 3)) - 1; |
| 262 |
|
| 263 |
// QUESTION: max sensitivity should give 11.8dB range, but this value |
| 264 |
// is directly added to an x.8 attenuation value, which will only give |
| 265 |
// 126/256 or ~4.9dB range -- what am I missing? The calculation below |
| 266 |
// matches several other emulators, including the Nuked implemenation. |
| 267 |
|
| 268 |
// raw LFO AM value on OPN is 0-3F, scale that up by a factor of 2 |
| 269 |
// (giving 7 bits) before applying the final shift |
| 270 |
return (m_lfo_am << 1) >> am_shift; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
//------------------------------------------------- |
| 275 |
// cache_operator_data - fill the operator cache |
| 276 |
// with prefetched data |
| 277 |
//------------------------------------------------- |
| 278 |
|
| 279 |
template<bool IsOpnA> |
| 280 |
void opn_registers_base<IsOpnA>::cache_operator_data(uint32_t choffs, uint32_t opoffs, opdata_cache &cache) |
| 281 |
{ |
| 282 |
// set up the easy stuff |
| 283 |
cache.waveform = &m_waveform[0][0]; |
| 284 |
|
| 285 |
// get frequency from the channel |
| 286 |
uint32_t block_freq = cache.block_freq = ch_block_freq(choffs); |
| 287 |
|
| 288 |
// if multi-frequency mode is enabled and this is channel 2, |
| 289 |
// fetch one of the special frequencies |
| 290 |
if (multi_freq() && choffs == 2) |
| 291 |
{ |
| 292 |
if (opoffs == 2) |
| 293 |
block_freq = cache.block_freq = multi_block_freq(1); |
| 294 |
else if (opoffs == 10) |
| 295 |
block_freq = cache.block_freq = multi_block_freq(2); |
| 296 |
else if (opoffs == 6) |
| 297 |
block_freq = cache.block_freq = multi_block_freq(0); |
| 298 |
} |
| 299 |
|
| 300 |
// compute the keycode: block_freq is: |
| 301 |
// |
| 302 |
// BBBFFFFFFFFFFF |
| 303 |
// ^^^^??? |
| 304 |
// |
| 305 |
// the 5-bit keycode uses the top 4 bits plus a magic formula |
| 306 |
// for the final bit |
| 307 |
uint32_t keycode = bitfield(block_freq, 10, 4) << 1; |
| 308 |
|
| 309 |
// lowest bit is determined by a mix of next lower FNUM bits |
| 310 |
// according to this equation from the YM2608 manual: |
| 311 |
// |
| 312 |
// (F11 & (F10 | F9 | F8)) | (!F11 & F10 & F9 & F8) |
| 313 |
// |
| 314 |
// for speed, we just look it up in a 16-bit constant |
| 315 |
keycode |= bitfield(0xfe80, bitfield(block_freq, 7, 4)); |
| 316 |
|
| 317 |
// detune adjustment |
| 318 |
cache.detune = detune_adjustment(op_detune(opoffs), keycode); |
| 319 |
|
| 320 |
// multiple value, as an x.1 value (0 means 0.5) |
| 321 |
cache.multiple = op_multiple(opoffs) * 2; |
| 322 |
if (cache.multiple == 0) |
| 323 |
cache.multiple = 1; |
| 324 |
|
| 325 |
// phase step, or PHASE_STEP_DYNAMIC if PM is active; this depends on |
| 326 |
// block_freq, detune, and multiple, so compute it after we've done those |
| 327 |
if (!IsOpnA || lfo_enable() == 0 || ch_lfo_pm_sens(choffs) == 0) |
| 328 |
cache.phase_step = compute_phase_step(choffs, opoffs, cache, 0); |
| 329 |
else |
| 330 |
cache.phase_step = opdata_cache::PHASE_STEP_DYNAMIC; |
| 331 |
|
| 332 |
// total level, scaled by 8 |
| 333 |
cache.total_level = op_total_level(opoffs) << 3; |
| 334 |
|
| 335 |
// 4-bit sustain level, but 15 means 31 so effectively 5 bits |
| 336 |
cache.eg_sustain = op_sustain_level(opoffs); |
| 337 |
cache.eg_sustain |= (cache.eg_sustain + 1) & 0x10; |
| 338 |
cache.eg_sustain <<= 5; |
| 339 |
|
| 340 |
// determine KSR adjustment for enevlope rates |
| 341 |
uint32_t ksrval = keycode >> (op_ksr(opoffs) ^ 3); |
| 342 |
cache.eg_rate[EG_ATTACK] = effective_rate(op_attack_rate(opoffs) * 2, ksrval); |
| 343 |
cache.eg_rate[EG_DECAY] = effective_rate(op_decay_rate(opoffs) * 2, ksrval); |
| 344 |
cache.eg_rate[EG_SUSTAIN] = effective_rate(op_sustain_rate(opoffs) * 2, ksrval); |
| 345 |
cache.eg_rate[EG_RELEASE] = effective_rate(op_release_rate(opoffs) * 4 + 2, ksrval); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
//------------------------------------------------- |
| 350 |
// compute_phase_step - compute the phase step |
| 351 |
//------------------------------------------------- |
| 352 |
|
| 353 |
template<bool IsOpnA> |
| 354 |
uint32_t opn_registers_base<IsOpnA>::compute_phase_step(uint32_t choffs, uint32_t /*opoffs*/, opdata_cache const &cache, int32_t lfo_raw_pm) |
| 355 |
{ |
| 356 |
// OPN phase calculation has only a single detune parameter |
| 357 |
// and uses FNUMs instead of keycodes |
| 358 |
|
| 359 |
// extract frequency number (low 11 bits of block_freq) |
| 360 |
uint32_t fnum = bitfield(cache.block_freq, 0, 11) << 1; |
| 361 |
|
| 362 |
// if there's a non-zero PM sensitivity, compute the adjustment |
| 363 |
uint32_t pm_sensitivity = ch_lfo_pm_sens(choffs); |
| 364 |
if (pm_sensitivity != 0) |
| 365 |
{ |
| 366 |
// apply the phase adjustment based on the upper 7 bits |
| 367 |
// of FNUM and the PM depth parameters |
| 368 |
fnum += opn_lfo_pm_phase_adjustment(bitfield(cache.block_freq, 4, 7), pm_sensitivity, lfo_raw_pm); |
| 369 |
|
| 370 |
// keep fnum to 12 bits |
| 371 |
fnum &= 0xfff; |
| 372 |
} |
| 373 |
|
| 374 |
// apply block shift to compute phase step |
| 375 |
uint32_t block = bitfield(cache.block_freq, 11, 3); |
| 376 |
uint32_t phase_step = (fnum << block) >> 2; |
| 377 |
|
| 378 |
// apply detune based on the keycode |
| 379 |
phase_step += cache.detune; |
| 380 |
|
| 381 |
// clamp to 17 bits in case detune overflows |
| 382 |
// QUESTION: is this specific to the YM2612/3438? |
| 383 |
phase_step &= 0x1ffff; |
| 384 |
|
| 385 |
// apply frequency multiplier (which is cached as an x.1 value) |
| 386 |
return (phase_step * cache.multiple) >> 1; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
//------------------------------------------------- |
| 391 |
// log_keyon - log a key-on event |
| 392 |
//------------------------------------------------- |
| 393 |
|
| 394 |
template<bool IsOpnA> |
| 395 |
std::string opn_registers_base<IsOpnA>::log_keyon(uint32_t choffs, uint32_t opoffs) |
| 396 |
{ |
| 397 |
uint32_t chnum = (choffs & 3) + 3 * bitfield(choffs, 8); |
| 398 |
uint32_t opnum = (opoffs & 15) - ((opoffs & 15) / 4) + 12 * bitfield(opoffs, 8); |
| 399 |
|
| 400 |
uint32_t block_freq = ch_block_freq(choffs); |
| 401 |
if (multi_freq() && choffs == 2) |
| 402 |
{ |
| 403 |
if (opoffs == 2) |
| 404 |
block_freq = multi_block_freq(1); |
| 405 |
else if (opoffs == 10) |
| 406 |
block_freq = multi_block_freq(2); |
| 407 |
else if (opoffs == 6) |
| 408 |
block_freq = multi_block_freq(0); |
| 409 |
} |
| 410 |
|
| 411 |
char buffer[256]; |
| 412 |
char *end = &buffer[0]; |
| 413 |
|
| 414 |
end += sprintf(end, "%u.%02u freq=%04X dt=%u fb=%u alg=%X mul=%X tl=%02X ksr=%u adsr=%02X/%02X/%02X/%X sl=%X", |
| 415 |
chnum, opnum, |
| 416 |
block_freq, |
| 417 |
op_detune(opoffs), |
| 418 |
ch_feedback(choffs), |
| 419 |
ch_algorithm(choffs), |
| 420 |
op_multiple(opoffs), |
| 421 |
op_total_level(opoffs), |
| 422 |
op_ksr(opoffs), |
| 423 |
op_attack_rate(opoffs), |
| 424 |
op_decay_rate(opoffs), |
| 425 |
op_sustain_rate(opoffs), |
| 426 |
op_release_rate(opoffs), |
| 427 |
op_sustain_level(opoffs)); |
| 428 |
|
| 429 |
if (OUTPUTS > 1) |
| 430 |
end += sprintf(end, " out=%c%c", |
| 431 |
ch_output_0(choffs) ? 'L' : '-', |
| 432 |
ch_output_1(choffs) ? 'R' : '-'); |
| 433 |
if (op_ssg_eg_enable(opoffs)) |
| 434 |
end += sprintf(end, " ssg=%X", op_ssg_eg_mode(opoffs)); |
| 435 |
bool am = (op_lfo_am_enable(opoffs) && ch_lfo_am_sens(choffs) != 0); |
| 436 |
if (am) |
| 437 |
end += sprintf(end, " am=%u", ch_lfo_am_sens(choffs)); |
| 438 |
bool pm = (ch_lfo_pm_sens(choffs) != 0); |
| 439 |
if (pm) |
| 440 |
end += sprintf(end, " pm=%u", ch_lfo_pm_sens(choffs)); |
| 441 |
if (am || pm) |
| 442 |
end += sprintf(end, " lfo=%02X", lfo_rate()); |
| 443 |
if (multi_freq() && choffs == 2) |
| 444 |
end += sprintf(end, " multi=1"); |
| 445 |
|
| 446 |
return buffer; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
//********************************************************* |
| 452 |
// SSG RESAMPLER |
| 453 |
//********************************************************* |
| 454 |
|
| 455 |
//------------------------------------------------- |
| 456 |
// add_last - helper to add the last computed |
| 457 |
// value to the sums, applying the given scale |
| 458 |
//------------------------------------------------- |
| 459 |
|
| 460 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 461 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::add_last(int32_t &sum0, int32_t &sum1, int32_t &sum2, int32_t scale) |
| 462 |
{ |
| 463 |
sum0 += m_last.data[0] * scale; |
| 464 |
sum1 += m_last.data[1] * scale; |
| 465 |
sum2 += m_last.data[2] * scale; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
//------------------------------------------------- |
| 470 |
// clock_and_add - helper to clock a new value |
| 471 |
// and then add it to the sums, applying the |
| 472 |
// given scale |
| 473 |
//------------------------------------------------- |
| 474 |
|
| 475 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 476 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::clock_and_add(int32_t &sum0, int32_t &sum1, int32_t &sum2, int32_t scale) |
| 477 |
{ |
| 478 |
m_ssg.clock(); |
| 479 |
m_ssg.output(m_last); |
| 480 |
add_last(sum0, sum1, sum2, scale); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
//------------------------------------------------- |
| 485 |
// write_to_output - helper to write the sums to |
| 486 |
// the appropriate outputs, applying the given |
| 487 |
// divisor to the final result |
| 488 |
//------------------------------------------------- |
| 489 |
|
| 490 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 491 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::write_to_output(OutputType *output, int32_t sum0, int32_t sum1, int32_t sum2, int32_t divisor) |
| 492 |
{ |
| 493 |
if (MixTo1) |
| 494 |
{ |
| 495 |
// mixing to one, apply a 2/3 factor to prevent overflow |
| 496 |
output->data[FirstOutput] = (sum0 + sum1 + sum2) * 2 / (3 * divisor); |
| 497 |
} |
| 498 |
else |
| 499 |
{ |
| 500 |
// write three outputs in a row |
| 501 |
output->data[FirstOutput + 0] = sum0 / divisor; |
| 502 |
output->data[FirstOutput + 1] = sum1 / divisor; |
| 503 |
output->data[FirstOutput + 2] = sum2 / divisor; |
| 504 |
} |
| 505 |
|
| 506 |
// track the sample index here |
| 507 |
m_sampindex++; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
//------------------------------------------------- |
| 512 |
// ssg_resampler - constructor |
| 513 |
//------------------------------------------------- |
| 514 |
|
| 515 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 516 |
ssg_resampler<OutputType, FirstOutput, MixTo1>::ssg_resampler(ssg_engine &ssg) : |
| 517 |
m_ssg(ssg), |
| 518 |
m_sampindex(0), |
| 519 |
m_resampler(&ssg_resampler::resample_nop) |
| 520 |
{ |
| 521 |
m_last.clear(); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
//------------------------------------------------- |
| 526 |
// save_restore - save or restore the data |
| 527 |
//------------------------------------------------- |
| 528 |
|
| 529 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 530 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::save_restore(ymfm_saved_state &state) |
| 531 |
{ |
| 532 |
state.save_restore(m_sampindex); |
| 533 |
state.save_restore(m_last.data); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
//------------------------------------------------- |
| 538 |
// configure - configure a new ratio |
| 539 |
//------------------------------------------------- |
| 540 |
|
| 541 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 542 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::configure(uint8_t outsamples, uint8_t srcsamples) |
| 543 |
{ |
| 544 |
switch (outsamples * 10 + srcsamples) |
| 545 |
{ |
| 546 |
case 4*10 + 1: /* 4:1 */ m_resampler = &ssg_resampler::resample_n_1<4>; break; |
| 547 |
case 2*10 + 1: /* 2:1 */ m_resampler = &ssg_resampler::resample_n_1<2>; break; |
| 548 |
case 4*10 + 3: /* 4:3 */ m_resampler = &ssg_resampler::resample_4_3; break; |
| 549 |
case 1*10 + 1: /* 1:1 */ m_resampler = &ssg_resampler::resample_n_1<1>; break; |
| 550 |
case 2*10 + 3: /* 2:3 */ m_resampler = &ssg_resampler::resample_2_3; break; |
| 551 |
case 1*10 + 3: /* 1:3 */ m_resampler = &ssg_resampler::resample_1_n<3>; break; |
| 552 |
case 2*10 + 9: /* 2:9 */ m_resampler = &ssg_resampler::resample_2_9; break; |
| 553 |
case 1*10 + 6: /* 1:6 */ m_resampler = &ssg_resampler::resample_1_n<6>; break; |
| 554 |
case 0*10 + 0: /* 0:0 */ m_resampler = &ssg_resampler::resample_nop; break; |
| 555 |
default: assert(false); break; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
//------------------------------------------------- |
| 561 |
// resample_n_1 - resample SSG output to the |
| 562 |
// target at a rate of 1 SSG sample to every |
| 563 |
// n output sample |
| 564 |
//------------------------------------------------- |
| 565 |
|
| 566 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 567 |
template<int Multiplier> |
| 568 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_n_1(OutputType *output, uint32_t numsamples) |
| 569 |
{ |
| 570 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 571 |
{ |
| 572 |
if (m_sampindex % Multiplier == 0) |
| 573 |
{ |
| 574 |
m_ssg.clock(); |
| 575 |
m_ssg.output(m_last); |
| 576 |
} |
| 577 |
write_to_output(output, m_last.data[0], m_last.data[1], m_last.data[2]); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
//------------------------------------------------- |
| 583 |
// resample_1_n - resample SSG output to the |
| 584 |
// target at a rate of n SSG samples to every |
| 585 |
// 1 output sample |
| 586 |
//------------------------------------------------- |
| 587 |
|
| 588 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 589 |
template<int Divisor> |
| 590 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_1_n(OutputType *output, uint32_t numsamples) |
| 591 |
{ |
| 592 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 593 |
{ |
| 594 |
int32_t sum0 = 0, sum1 = 0, sum2 = 0; |
| 595 |
for (int rep = 0; rep < Divisor; rep++) |
| 596 |
clock_and_add(sum0, sum1, sum2); |
| 597 |
write_to_output(output, sum0, sum1, sum2, Divisor); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
//------------------------------------------------- |
| 603 |
// resample_2_9 - resample SSG output to the |
| 604 |
// target at a rate of 9 SSG samples to every |
| 605 |
// 2 output samples |
| 606 |
//------------------------------------------------- |
| 607 |
|
| 608 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 609 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_2_9(OutputType *output, uint32_t numsamples) |
| 610 |
{ |
| 611 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 612 |
{ |
| 613 |
int32_t sum0 = 0, sum1 = 0, sum2 = 0; |
| 614 |
if (bitfield(m_sampindex, 0) != 0) |
| 615 |
add_last(sum0, sum1, sum2, 1); |
| 616 |
clock_and_add(sum0, sum1, sum2, 2); |
| 617 |
clock_and_add(sum0, sum1, sum2, 2); |
| 618 |
clock_and_add(sum0, sum1, sum2, 2); |
| 619 |
clock_and_add(sum0, sum1, sum2, 2); |
| 620 |
if (bitfield(m_sampindex, 0) == 0) |
| 621 |
clock_and_add(sum0, sum1, sum2, 1); |
| 622 |
write_to_output(output, sum0, sum1, sum2, 9); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
//------------------------------------------------- |
| 628 |
// resample_2_3 - resample SSG output to the |
| 629 |
// target at a rate of 3 SSG samples to every |
| 630 |
// 2 output samples |
| 631 |
//------------------------------------------------- |
| 632 |
|
| 633 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 634 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_2_3(OutputType *output, uint32_t numsamples) |
| 635 |
{ |
| 636 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 637 |
{ |
| 638 |
int32_t sum0 = 0, sum1 = 0, sum2 = 0; |
| 639 |
if (bitfield(m_sampindex, 0) == 0) |
| 640 |
{ |
| 641 |
clock_and_add(sum0, sum1, sum2, 2); |
| 642 |
clock_and_add(sum0, sum1, sum2, 1); |
| 643 |
} |
| 644 |
else |
| 645 |
{ |
| 646 |
add_last(sum0, sum1, sum2, 1); |
| 647 |
clock_and_add(sum0, sum1, sum2, 2); |
| 648 |
} |
| 649 |
write_to_output(output, sum0, sum1, sum2, 3); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
//------------------------------------------------- |
| 655 |
// resample_4_3 - resample SSG output to the |
| 656 |
// target at a rate of 3 SSG samples to every |
| 657 |
// 4 output samples |
| 658 |
//------------------------------------------------- |
| 659 |
|
| 660 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 661 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_4_3(OutputType *output, uint32_t numsamples) |
| 662 |
{ |
| 663 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 664 |
{ |
| 665 |
int32_t sum0 = 0, sum1 = 0, sum2 = 0; |
| 666 |
int32_t step = bitfield(m_sampindex, 0, 2); |
| 667 |
add_last(sum0, sum1, sum2, step); |
| 668 |
if (step != 3) |
| 669 |
clock_and_add(sum0, sum1, sum2, 3 - step); |
| 670 |
write_to_output(output, sum0, sum1, sum2, 3); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
//------------------------------------------------- |
| 676 |
// resample_nop - no-op resampler |
| 677 |
//------------------------------------------------- |
| 678 |
|
| 679 |
template<typename OutputType, int FirstOutput, bool MixTo1> |
| 680 |
void ssg_resampler<OutputType, FirstOutput, MixTo1>::resample_nop(OutputType */*output*/, uint32_t numsamples) |
| 681 |
{ |
| 682 |
// nothing to do except increment the sample index |
| 683 |
m_sampindex += numsamples; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
#if 0 |
| 688 |
//********************************************************* |
| 689 |
// YM2203 |
| 690 |
//********************************************************* |
| 691 |
|
| 692 |
//------------------------------------------------- |
| 693 |
// ym2203 - constructor |
| 694 |
//------------------------------------------------- |
| 695 |
|
| 696 |
ym2203::ym2203(ymfm_interface &intf) : |
| 697 |
m_fidelity(OPN_FIDELITY_MAX), |
| 698 |
m_address(0), |
| 699 |
m_fm(intf), |
| 700 |
m_ssg(intf), |
| 701 |
m_ssg_resampler(m_ssg) |
| 702 |
{ |
| 703 |
m_last_fm.clear(); |
| 704 |
update_prescale(m_fm.clock_prescale()); |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
//------------------------------------------------- |
| 709 |
// reset - reset the system |
| 710 |
//------------------------------------------------- |
| 711 |
|
| 712 |
void ym2203::reset() |
| 713 |
{ |
| 714 |
// reset the engines |
| 715 |
m_fm.reset(); |
| 716 |
m_ssg.reset(); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
//------------------------------------------------- |
| 721 |
// save_restore - save or restore the data |
| 722 |
//------------------------------------------------- |
| 723 |
|
| 724 |
void ym2203::save_restore(ymfm_saved_state &state) |
| 725 |
{ |
| 726 |
state.save_restore(m_address); |
| 727 |
state.save_restore(m_last_fm.data); |
| 728 |
|
| 729 |
m_fm.save_restore(state); |
| 730 |
m_ssg.save_restore(state); |
| 731 |
m_ssg_resampler.save_restore(state); |
| 732 |
|
| 733 |
update_prescale(m_fm.clock_prescale()); |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
//------------------------------------------------- |
| 738 |
// read_status - read the status register |
| 739 |
//------------------------------------------------- |
| 740 |
|
| 741 |
uint8_t ym2203::read_status() |
| 742 |
{ |
| 743 |
uint8_t result = m_fm.status(); |
| 744 |
if (m_fm.intf().ymfm_is_busy()) |
| 745 |
result |= fm_engine::STATUS_BUSY; |
| 746 |
return result; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
//------------------------------------------------- |
| 751 |
// read_data - read the data register |
| 752 |
//------------------------------------------------- |
| 753 |
|
| 754 |
uint8_t ym2203::read_data() |
| 755 |
{ |
| 756 |
uint8_t result = 0; |
| 757 |
if (m_address < 0x10) |
| 758 |
{ |
| 759 |
// 00-0F: Read from SSG |
| 760 |
result = m_ssg.read(m_address & 0x0f); |
| 761 |
} |
| 762 |
return result; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
//------------------------------------------------- |
| 767 |
// read - handle a read from the device |
| 768 |
//------------------------------------------------- |
| 769 |
|
| 770 |
uint8_t ym2203::read(uint32_t offset) |
| 771 |
{ |
| 772 |
uint8_t result = 0xff; |
| 773 |
switch (offset & 1) |
| 774 |
{ |
| 775 |
case 0: // status port |
| 776 |
result = read_status(); |
| 777 |
break; |
| 778 |
|
| 779 |
case 1: // data port (only SSG) |
| 780 |
result = read_data(); |
| 781 |
break; |
| 782 |
} |
| 783 |
return result; |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
//------------------------------------------------- |
| 788 |
// write_address - handle a write to the address |
| 789 |
// register |
| 790 |
//------------------------------------------------- |
| 791 |
|
| 792 |
void ym2203::write_address(uint8_t data) |
| 793 |
{ |
| 794 |
// just set the address |
| 795 |
m_address = data; |
| 796 |
|
| 797 |
// special case: update the prescale |
| 798 |
if (m_address >= 0x2d && m_address <= 0x2f) |
| 799 |
{ |
| 800 |
// 2D-2F: prescaler select |
| 801 |
if (m_address == 0x2d) |
| 802 |
update_prescale(6); |
| 803 |
else if (m_address == 0x2e && m_fm.clock_prescale() == 6) |
| 804 |
update_prescale(3); |
| 805 |
else if (m_address == 0x2f) |
| 806 |
update_prescale(2); |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
//------------------------------------------------- |
| 812 |
// write - handle a write to the register |
| 813 |
// interface |
| 814 |
//------------------------------------------------- |
| 815 |
|
| 816 |
void ym2203::write_data(uint8_t data) |
| 817 |
{ |
| 818 |
if (m_address < 0x10) |
| 819 |
{ |
| 820 |
// 00-0F: write to SSG |
| 821 |
m_ssg.write(m_address & 0x0f, data); |
| 822 |
} |
| 823 |
else |
| 824 |
{ |
| 825 |
// 10-FF: write to FM |
| 826 |
m_fm.write(m_address, data); |
| 827 |
} |
| 828 |
|
| 829 |
// mark busy for a bit |
| 830 |
m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale()); |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
//------------------------------------------------- |
| 835 |
// write - handle a write to the register |
| 836 |
// interface |
| 837 |
//------------------------------------------------- |
| 838 |
|
| 839 |
void ym2203::write(uint32_t offset, uint8_t data) |
| 840 |
{ |
| 841 |
switch (offset & 1) |
| 842 |
{ |
| 843 |
case 0: // address port |
| 844 |
write_address(data); |
| 845 |
break; |
| 846 |
|
| 847 |
case 1: // data port |
| 848 |
write_data(data); |
| 849 |
break; |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
//------------------------------------------------- |
| 855 |
// generate - generate one sample of sound |
| 856 |
//------------------------------------------------- |
| 857 |
|
| 858 |
void ym2203::generate(output_data *output, uint32_t numsamples) |
| 859 |
{ |
| 860 |
// FM output is just repeated the prescale number of times; note that |
| 861 |
// 0 is a special 1.5 case |
| 862 |
if (m_fm_samples_per_output != 0) |
| 863 |
{ |
| 864 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 865 |
{ |
| 866 |
if ((m_ssg_resampler.sampindex() + samp) % m_fm_samples_per_output == 0) |
| 867 |
clock_fm(); |
| 868 |
output->data[0] = m_last_fm.data[0]; |
| 869 |
} |
| 870 |
} |
| 871 |
else |
| 872 |
{ |
| 873 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 874 |
{ |
| 875 |
uint32_t step = (m_ssg_resampler.sampindex() + samp) % 3; |
| 876 |
if (step == 0) |
| 877 |
clock_fm(); |
| 878 |
output->data[0] = m_last_fm.data[0]; |
| 879 |
if (step == 1) |
| 880 |
{ |
| 881 |
clock_fm(); |
| 882 |
output->data[0] = (output->data[0] + m_last_fm.data[0]) / 2; |
| 883 |
} |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
// resample the SSG as configured |
| 888 |
m_ssg_resampler.resample(output - numsamples, numsamples); |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
//------------------------------------------------- |
| 893 |
// update_prescale - update the prescale value, |
| 894 |
// recomputing derived values |
| 895 |
//------------------------------------------------- |
| 896 |
|
| 897 |
void ym2203::update_prescale(uint8_t prescale) |
| 898 |
{ |
| 899 |
// tell the FM engine |
| 900 |
m_fm.set_clock_prescale(prescale); |
| 901 |
m_ssg.prescale_changed(); |
| 902 |
|
| 903 |
// Fidelity: ---- minimum ---- ---- medium ----- ---- maximum----- |
| 904 |
// rate = clock/24 rate = clock/12 rate = clock/4 |
| 905 |
// Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate |
| 906 |
// 6 3:1 2:3 6:1 4:3 18:1 4:1 |
| 907 |
// 3 1.5:1 1:3 3:1 2:3 9:1 2:1 |
| 908 |
// 2 1:1 1:6 2:1 1:3 6:1 1:1 |
| 909 |
|
| 910 |
// compute the number of FM samples per output sample, and select the |
| 911 |
// resampler function |
| 912 |
if (m_fidelity == OPN_FIDELITY_MIN) |
| 913 |
{ |
| 914 |
switch (prescale) |
| 915 |
{ |
| 916 |
default: |
| 917 |
case 6: m_fm_samples_per_output = 3; m_ssg_resampler.configure(2, 3); break; |
| 918 |
case 3: m_fm_samples_per_output = 0; m_ssg_resampler.configure(1, 3); break; |
| 919 |
case 2: m_fm_samples_per_output = 1; m_ssg_resampler.configure(1, 6); break; |
| 920 |
} |
| 921 |
} |
| 922 |
else if (m_fidelity == OPN_FIDELITY_MED) |
| 923 |
{ |
| 924 |
switch (prescale) |
| 925 |
{ |
| 926 |
default: |
| 927 |
case 6: m_fm_samples_per_output = 6; m_ssg_resampler.configure(4, 3); break; |
| 928 |
case 3: m_fm_samples_per_output = 3; m_ssg_resampler.configure(2, 3); break; |
| 929 |
case 2: m_fm_samples_per_output = 2; m_ssg_resampler.configure(1, 3); break; |
| 930 |
} |
| 931 |
} |
| 932 |
else |
| 933 |
{ |
| 934 |
switch (prescale) |
| 935 |
{ |
| 936 |
default: |
| 937 |
case 6: m_fm_samples_per_output = 18; m_ssg_resampler.configure(4, 1); break; |
| 938 |
case 3: m_fm_samples_per_output = 9; m_ssg_resampler.configure(2, 1); break; |
| 939 |
case 2: m_fm_samples_per_output = 6; m_ssg_resampler.configure(1, 1); break; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
// if overriding the SSG, override the configuration with the nop |
| 944 |
// resampler to at least keep the sample index moving forward |
| 945 |
if (m_ssg.overridden()) |
| 946 |
m_ssg_resampler.configure(0, 0); |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
//------------------------------------------------- |
| 951 |
// clock_fm - clock FM state |
| 952 |
//------------------------------------------------- |
| 953 |
|
| 954 |
void ym2203::clock_fm() |
| 955 |
{ |
| 956 |
// clock the system |
| 957 |
m_fm.clock(fm_engine::ALL_CHANNELS); |
| 958 |
|
| 959 |
// update the FM content; OPN is full 14-bit with no intermediate clipping |
| 960 |
m_fm.output(m_last_fm.clear(), 0, 32767, fm_engine::ALL_CHANNELS); |
| 961 |
|
| 962 |
// convert to 10.3 floating point value for the DAC and back |
| 963 |
m_last_fm.roundtrip_fp(); |
| 964 |
} |
| 965 |
|
| 966 |
#endif |
| 967 |
|
| 968 |
//********************************************************* |
| 969 |
// YM2608 |
| 970 |
//********************************************************* |
| 971 |
|
| 972 |
//------------------------------------------------- |
| 973 |
// ym2608 - constructor |
| 974 |
//------------------------------------------------- |
| 975 |
|
| 976 |
ym2608::ym2608(ymfm_interface &intf) : |
| 977 |
m_fidelity(OPN_FIDELITY_MAX), |
| 978 |
m_address(0), |
| 979 |
m_irq_enable(0x1f), |
| 980 |
m_flag_control(0x1c), |
| 981 |
m_fm(intf), |
| 982 |
m_ssg(intf), |
| 983 |
m_ssg_resampler(m_ssg), |
| 984 |
m_adpcm_a(intf, 0), |
| 985 |
m_adpcm_b(intf) |
| 986 |
{ |
| 987 |
m_last_fm.clear(); |
| 988 |
update_prescale(m_fm.clock_prescale()); |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
//------------------------------------------------- |
| 993 |
// reset - reset the system |
| 994 |
//------------------------------------------------- |
| 995 |
|
| 996 |
void ym2608::reset() |
| 997 |
{ |
| 998 |
// reset the engines |
| 999 |
m_fm.reset(); |
| 1000 |
m_ssg.reset(); |
| 1001 |
m_adpcm_a.reset(); |
| 1002 |
m_adpcm_b.reset(); |
| 1003 |
|
| 1004 |
// configure ADPCM percussion sounds; these are present in an embedded ROM |
| 1005 |
m_adpcm_a.set_start_end(0, 0x0000, 0x01bf); // bass drum |
| 1006 |
m_adpcm_a.set_start_end(1, 0x01c0, 0x043f); // snare drum |
| 1007 |
m_adpcm_a.set_start_end(2, 0x0440, 0x1b7f); // top cymbal |
| 1008 |
m_adpcm_a.set_start_end(3, 0x1b80, 0x1cff); // high hat |
| 1009 |
m_adpcm_a.set_start_end(4, 0x1d00, 0x1f7f); // tom tom |
| 1010 |
m_adpcm_a.set_start_end(5, 0x1f80, 0x1fff); // rim shot |
| 1011 |
|
| 1012 |
// initialize our special interrupt states, then read the upper status |
| 1013 |
// register, which updates the IRQs |
| 1014 |
m_irq_enable = 0x1f; |
| 1015 |
m_flag_control = 0x1c; |
| 1016 |
read_status_hi(); |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
//------------------------------------------------- |
| 1021 |
// save_restore - save or restore the data |
| 1022 |
//------------------------------------------------- |
| 1023 |
|
| 1024 |
void ym2608::save_restore(ymfm_saved_state &state) |
| 1025 |
{ |
| 1026 |
state.save_restore(m_address); |
| 1027 |
state.save_restore(m_irq_enable); |
| 1028 |
state.save_restore(m_flag_control); |
| 1029 |
state.save_restore(m_last_fm.data); |
| 1030 |
|
| 1031 |
m_fm.save_restore(state); |
| 1032 |
m_ssg.save_restore(state); |
| 1033 |
m_ssg_resampler.save_restore(state); |
| 1034 |
m_adpcm_a.save_restore(state); |
| 1035 |
m_adpcm_b.save_restore(state); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
//------------------------------------------------- |
| 1040 |
// read_status - read the status register |
| 1041 |
//------------------------------------------------- |
| 1042 |
|
| 1043 |
uint8_t ym2608::read_status() |
| 1044 |
{ |
| 1045 |
uint8_t result = m_fm.status() & (fm_engine::STATUS_TIMERA | fm_engine::STATUS_TIMERB); |
| 1046 |
if (m_fm.intf().ymfm_is_busy()) |
| 1047 |
result |= fm_engine::STATUS_BUSY; |
| 1048 |
return result; |
| 1049 |
} |
| 1050 |
|
| 1051 |
|
| 1052 |
//------------------------------------------------- |
| 1053 |
// read_data - read the data register |
| 1054 |
//------------------------------------------------- |
| 1055 |
|
| 1056 |
uint8_t ym2608::read_data() |
| 1057 |
{ |
| 1058 |
uint8_t result = 0; |
| 1059 |
if (m_address < 0x10) |
| 1060 |
{ |
| 1061 |
// 00-0F: Read from SSG |
| 1062 |
result = m_ssg.read(m_address & 0x0f); |
| 1063 |
} |
| 1064 |
else if (m_address == 0xff) |
| 1065 |
{ |
| 1066 |
// FF: ID code |
| 1067 |
result = 1; |
| 1068 |
} |
| 1069 |
return result; |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
//------------------------------------------------- |
| 1074 |
// read_status_hi - read the extended status |
| 1075 |
// register |
| 1076 |
//------------------------------------------------- |
| 1077 |
|
| 1078 |
uint8_t ym2608::read_status_hi() |
| 1079 |
{ |
| 1080 |
// fetch regular status |
| 1081 |
uint8_t status = m_fm.status() & ~(STATUS_ADPCM_B_EOS | STATUS_ADPCM_B_BRDY | STATUS_ADPCM_B_PLAYING); |
| 1082 |
|
| 1083 |
// fetch ADPCM-B status, and merge in the bits |
| 1084 |
uint8_t adpcm_status = m_adpcm_b.status(); |
| 1085 |
if ((adpcm_status & adpcm_b_channel::STATUS_EOS) != 0) |
| 1086 |
status |= STATUS_ADPCM_B_EOS; |
| 1087 |
if ((adpcm_status & adpcm_b_channel::STATUS_BRDY) != 0) |
| 1088 |
status |= STATUS_ADPCM_B_BRDY; |
| 1089 |
if ((adpcm_status & adpcm_b_channel::STATUS_PLAYING) != 0) |
| 1090 |
status |= STATUS_ADPCM_B_PLAYING; |
| 1091 |
|
| 1092 |
// turn off any bits that have been requested to be masked |
| 1093 |
status &= ~(m_flag_control & 0x1f); |
| 1094 |
|
| 1095 |
// update the status so that IRQs are propagated |
| 1096 |
m_fm.set_reset_status(status, ~status); |
| 1097 |
|
| 1098 |
// merge in the busy flag |
| 1099 |
if (m_fm.intf().ymfm_is_busy()) |
| 1100 |
status |= fm_engine::STATUS_BUSY; |
| 1101 |
return status; |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
//------------------------------------------------- |
| 1106 |
// read_data_hi - read the upper data register |
| 1107 |
//------------------------------------------------- |
| 1108 |
|
| 1109 |
uint8_t ym2608::read_data_hi() |
| 1110 |
{ |
| 1111 |
uint8_t result = 0; |
| 1112 |
if ((m_address & 0xff) < 0x10) |
| 1113 |
{ |
| 1114 |
// 00-0F: Read from ADPCM-B |
| 1115 |
result = m_adpcm_b.read(m_address & 0x0f); |
| 1116 |
} |
| 1117 |
return result; |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
//------------------------------------------------- |
| 1122 |
// read - handle a read from the device |
| 1123 |
//------------------------------------------------- |
| 1124 |
|
| 1125 |
uint8_t ym2608::read(uint32_t offset) |
| 1126 |
{ |
| 1127 |
uint8_t result = 0; |
| 1128 |
switch (offset & 3) |
| 1129 |
{ |
| 1130 |
case 0: // status port, YM2203 compatible |
| 1131 |
result = read_status(); |
| 1132 |
break; |
| 1133 |
|
| 1134 |
case 1: // data port (only SSG) |
| 1135 |
result = read_data(); |
| 1136 |
break; |
| 1137 |
|
| 1138 |
case 2: // status port, extended |
| 1139 |
result = read_status_hi(); |
| 1140 |
break; |
| 1141 |
|
| 1142 |
case 3: // ADPCM-B data |
| 1143 |
result = read_data_hi(); |
| 1144 |
break; |
| 1145 |
} |
| 1146 |
return result; |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
//------------------------------------------------- |
| 1151 |
// write_address - handle a write to the address |
| 1152 |
// register |
| 1153 |
//------------------------------------------------- |
| 1154 |
|
| 1155 |
void ym2608::write_address(uint8_t data) |
| 1156 |
{ |
| 1157 |
// just set the address |
| 1158 |
m_address = data; |
| 1159 |
|
| 1160 |
// special case: update the prescale |
| 1161 |
if (m_address >= 0x2d && m_address <= 0x2f) |
| 1162 |
{ |
| 1163 |
// 2D-2F: prescaler select |
| 1164 |
if (m_address == 0x2d) |
| 1165 |
update_prescale(6); |
| 1166 |
else if (m_address == 0x2e && m_fm.clock_prescale() == 6) |
| 1167 |
update_prescale(3); |
| 1168 |
else if (m_address == 0x2f) |
| 1169 |
update_prescale(2); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
//------------------------------------------------- |
| 1175 |
// write - handle a write to the data register |
| 1176 |
//------------------------------------------------- |
| 1177 |
|
| 1178 |
void ym2608::write_data(uint8_t data) |
| 1179 |
{ |
| 1180 |
// ignore if paired with upper address |
| 1181 |
if (bitfield(m_address, 8)) |
| 1182 |
return; |
| 1183 |
|
| 1184 |
if (m_address < 0x10) |
| 1185 |
{ |
| 1186 |
// 00-0F: write to SSG |
| 1187 |
m_ssg.write(m_address & 0x0f, data); |
| 1188 |
} |
| 1189 |
else if (m_address < 0x20) |
| 1190 |
{ |
| 1191 |
// 10-1F: write to ADPCM-A |
| 1192 |
m_adpcm_a.write(m_address & 0x0f, data); |
| 1193 |
} |
| 1194 |
else if (m_address == 0x29) |
| 1195 |
{ |
| 1196 |
// 29: special IRQ mask register |
| 1197 |
m_irq_enable = data; |
| 1198 |
m_fm.set_irq_mask(m_irq_enable & ~m_flag_control & 0x1f); |
| 1199 |
} |
| 1200 |
else |
| 1201 |
{ |
| 1202 |
// 20-28, 2A-FF: write to FM |
| 1203 |
m_fm.write(m_address, data); |
| 1204 |
} |
| 1205 |
|
| 1206 |
// mark busy for a bit |
| 1207 |
m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale()); |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
//------------------------------------------------- |
| 1212 |
// write_address_hi - handle a write to the upper |
| 1213 |
// address register |
| 1214 |
//------------------------------------------------- |
| 1215 |
|
| 1216 |
void ym2608::write_address_hi(uint8_t data) |
| 1217 |
{ |
| 1218 |
// just set the address |
| 1219 |
m_address = 0x100 | data; |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
//------------------------------------------------- |
| 1224 |
// write_data_hi - handle a write to the upper |
| 1225 |
// data register |
| 1226 |
//------------------------------------------------- |
| 1227 |
|
| 1228 |
void ym2608::write_data_hi(uint8_t data) |
| 1229 |
{ |
| 1230 |
// ignore if paired with upper address |
| 1231 |
if (!bitfield(m_address, 8)) |
| 1232 |
return; |
| 1233 |
|
| 1234 |
if (m_address < 0x110) |
| 1235 |
{ |
| 1236 |
// 100-10F: write to ADPCM-B |
| 1237 |
m_adpcm_b.write(m_address & 0x0f, data); |
| 1238 |
} |
| 1239 |
else if (m_address == 0x110) |
| 1240 |
{ |
| 1241 |
// 110: IRQ flag control |
| 1242 |
if (bitfield(data, 7)) |
| 1243 |
m_fm.set_reset_status(0, 0xff); |
| 1244 |
else |
| 1245 |
{ |
| 1246 |
m_flag_control = data; |
| 1247 |
m_fm.set_irq_mask(m_irq_enable & ~m_flag_control & 0x1f); |
| 1248 |
} |
| 1249 |
} |
| 1250 |
else |
| 1251 |
{ |
| 1252 |
// 111-1FF: write to FM |
| 1253 |
m_fm.write(m_address, data); |
| 1254 |
} |
| 1255 |
|
| 1256 |
// mark busy for a bit |
| 1257 |
m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale()); |
| 1258 |
} |
| 1259 |
|
| 1260 |
|
| 1261 |
//------------------------------------------------- |
| 1262 |
// write - handle a write to the register |
| 1263 |
// interface |
| 1264 |
//------------------------------------------------- |
| 1265 |
|
| 1266 |
void ym2608::write(uint32_t offset, uint8_t data) |
| 1267 |
{ |
| 1268 |
switch (offset & 3) |
| 1269 |
{ |
| 1270 |
case 0: // address port |
| 1271 |
write_address(data); |
| 1272 |
break; |
| 1273 |
|
| 1274 |
case 1: // data port |
| 1275 |
write_data(data); |
| 1276 |
break; |
| 1277 |
|
| 1278 |
case 2: // upper address port |
| 1279 |
write_address_hi(data); |
| 1280 |
break; |
| 1281 |
|
| 1282 |
case 3: // upper data port |
| 1283 |
write_data_hi(data); |
| 1284 |
break; |
| 1285 |
} |
| 1286 |
} |
| 1287 |
|
| 1288 |
|
| 1289 |
//------------------------------------------------- |
| 1290 |
// generate - generate one sample of sound |
| 1291 |
//------------------------------------------------- |
| 1292 |
|
| 1293 |
/* [BambooTracker] Separate sample generate with FM and SSG */ |
| 1294 |
//void ym2608::generate(output_data *output, uint32_t numsamples) |
| 1295 |
//{ |
| 1296 |
// // FM output is just repeated the prescale number of times; note that |
| 1297 |
// // 0 is a special 1.5 case |
| 1298 |
// if (m_fm_samples_per_output != 0) |
| 1299 |
// { |
| 1300 |
// for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 1301 |
// { |
| 1302 |
// if ((m_ssg_resampler.sampindex() + samp) % m_fm_samples_per_output == 0) |
| 1303 |
// clock_fm_and_adpcm(); |
| 1304 |
// output->data[0] = m_last_fm.data[0]; |
| 1305 |
// output->data[1] = m_last_fm.data[1]; |
| 1306 |
// } |
| 1307 |
// } |
| 1308 |
// else |
| 1309 |
// { |
| 1310 |
// for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 1311 |
// { |
| 1312 |
// uint32_t step = (m_ssg_resampler.sampindex() + samp) % 3; |
| 1313 |
// if (step == 0) |
| 1314 |
// clock_fm_and_adpcm(); |
| 1315 |
// output->data[0] = m_last_fm.data[0]; |
| 1316 |
// output->data[1] = m_last_fm.data[1]; |
| 1317 |
// if (step == 1) |
| 1318 |
// { |
| 1319 |
// clock_fm_and_adpcm(); |
| 1320 |
// output->data[0] = (output->data[0] + m_last_fm.data[0]) / 2; |
| 1321 |
// output->data[1] = (output->data[1] + m_last_fm.data[1]) / 2; |
| 1322 |
// } |
| 1323 |
// } |
| 1324 |
// } |
| 1325 |
|
| 1326 |
// // resample the SSG as configured |
| 1327 |
// m_ssg_resampler.resample(output - numsamples, numsamples); |
| 1328 |
//} |
| 1329 |
|
| 1330 |
/* [BambooTracker] |
| 1331 |
* We use only prescale = 6, match output rate to FM synthesis rate */ |
| 1332 |
void ym2608::generate_fm_adpcm(output_data *output, uint32_t numsamples) |
| 1333 |
{ |
| 1334 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 1335 |
{ |
| 1336 |
clock_fm_and_adpcm(); |
| 1337 |
output->data[0] = m_last_fm.data[0]; |
| 1338 |
output->data[1] = m_last_fm.data[1]; |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
/* [BambooTracker] |
| 1343 |
* We use only prescale = 6 and do not resample SSG */ |
| 1344 |
void ym2608::generate_ssg(output_data *output, uint32_t numsamples) |
| 1345 |
{ |
| 1346 |
// TODO |
| 1347 |
// resample the SSG as configured |
| 1348 |
m_ssg_resampler.resample(output, numsamples); |
| 1349 |
} |
| 1350 |
|
| 1351 |
//------------------------------------------------- |
| 1352 |
// update_prescale - update the prescale value, |
| 1353 |
// recomputing derived values |
| 1354 |
//------------------------------------------------- |
| 1355 |
|
| 1356 |
void ym2608::update_prescale(uint8_t prescale) |
| 1357 |
{ |
| 1358 |
// tell the FM engine |
| 1359 |
m_fm.set_clock_prescale(prescale); |
| 1360 |
m_ssg.prescale_changed(); |
| 1361 |
|
| 1362 |
// Fidelity: ---- minimum ---- ---- medium ----- ---- maximum----- |
| 1363 |
// rate = clock/48 rate = clock/24 rate = clock/8 |
| 1364 |
// Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate |
| 1365 |
// 6 3:1 2:3 6:1 4:3 18:1 4:1 |
| 1366 |
// 3 1.5:1 1:3 3:1 2:3 9:1 2:1 |
| 1367 |
// 2 1:1 1:6 2:1 1:3 6:1 1:1 |
| 1368 |
|
| 1369 |
// compute the number of FM samples per output sample, and select the |
| 1370 |
// resampler function |
| 1371 |
if (m_fidelity == OPN_FIDELITY_MIN) |
| 1372 |
{ |
| 1373 |
switch (prescale) |
| 1374 |
{ |
| 1375 |
default: |
| 1376 |
/* [BambooTracker] |
| 1377 |
* We use only prescale = 6, match output rate to FM synthesis rate |
| 1378 |
* and do not resample SSG */ |
| 1379 |
case 6: m_fm_samples_per_output = 1; m_ssg_resampler.configure(1, 1); break; |
| 1380 |
case 3: m_fm_samples_per_output = 0; m_ssg_resampler.configure(1, 3); break; |
| 1381 |
case 2: m_fm_samples_per_output = 1; m_ssg_resampler.configure(1, 6); break; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
else if (m_fidelity == OPN_FIDELITY_MED) |
| 1385 |
{ |
| 1386 |
switch (prescale) |
| 1387 |
{ |
| 1388 |
default: |
| 1389 |
case 6: m_fm_samples_per_output = 6; m_ssg_resampler.configure(4, 3); break; |
| 1390 |
case 3: m_fm_samples_per_output = 3; m_ssg_resampler.configure(2, 3); break; |
| 1391 |
case 2: m_fm_samples_per_output = 2; m_ssg_resampler.configure(1, 3); break; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
else |
| 1395 |
{ |
| 1396 |
switch (prescale) |
| 1397 |
{ |
| 1398 |
default: |
| 1399 |
case 6: m_fm_samples_per_output = 18; m_ssg_resampler.configure(4, 1); break; |
| 1400 |
case 3: m_fm_samples_per_output = 9; m_ssg_resampler.configure(2, 1); break; |
| 1401 |
case 2: m_fm_samples_per_output = 6; m_ssg_resampler.configure(1, 1); break; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
// if overriding the SSG, override the configuration with the nop |
| 1406 |
// resampler to at least keep the sample index moving forward |
| 1407 |
if (m_ssg.overridden()) |
| 1408 |
m_ssg_resampler.configure(0, 0); |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
//------------------------------------------------- |
| 1413 |
// clock_fm_and_adpcm - clock FM and ADPCM state |
| 1414 |
//------------------------------------------------- |
| 1415 |
|
| 1416 |
void ym2608::clock_fm_and_adpcm() |
| 1417 |
{ |
| 1418 |
// top bit of the IRQ enable flags controls 3-channel vs 6-channel mode |
| 1419 |
uint32_t fmmask = bitfield(m_irq_enable, 7) ? 0x3f : 0x07; |
| 1420 |
|
| 1421 |
// clock the system |
| 1422 |
uint32_t env_counter = m_fm.clock(fm_engine::ALL_CHANNELS); |
| 1423 |
|
| 1424 |
// clock the ADPCM-A engine on every envelope cycle |
| 1425 |
// (channels 4 and 5 clock every 2 envelope clocks) |
| 1426 |
if (bitfield(env_counter, 0, 2) == 0) |
| 1427 |
m_adpcm_a.clock(bitfield(env_counter, 2) ? 0x0f : 0x3f); |
| 1428 |
|
| 1429 |
// clock the ADPCM-B engine every cycle |
| 1430 |
m_adpcm_b.clock(); |
| 1431 |
|
| 1432 |
// update the FM content; OPNA is 13-bit with no intermediate clipping |
| 1433 |
m_fm.output(m_last_fm.clear(), 1, 32767, fmmask); |
| 1434 |
|
| 1435 |
// mix in the ADPCM and clamp |
| 1436 |
m_adpcm_a.output(m_last_fm, 0x3f); |
| 1437 |
m_adpcm_b.output(m_last_fm, 1); |
| 1438 |
m_last_fm.clamp16(); |
| 1439 |
} |
| 1440 |
|
| 1441 |
#if 0 |
| 1442 |
//********************************************************* |
| 1443 |
// YMF288 |
| 1444 |
//********************************************************* |
| 1445 |
|
| 1446 |
// YMF288 is a YM2608 with the following changes: |
| 1447 |
// * ADPCM-B part removed |
| 1448 |
// * prescaler removed (fixed at 6) |
| 1449 |
// * CSM removed |
| 1450 |
// * Low power mode added |
| 1451 |
// * SSG tone frequency is altered in some way? (explicitly DC for Tp 0-7, also double volume in some cases) |
| 1452 |
// * I/O ports removed |
| 1453 |
// * Shorter busy times |
| 1454 |
// * All registers can be read |
| 1455 |
|
| 1456 |
//------------------------------------------------- |
| 1457 |
// ymf288 - constructor |
| 1458 |
//------------------------------------------------- |
| 1459 |
|
| 1460 |
ymf288::ymf288(ymfm_interface &intf) : |
| 1461 |
m_fidelity(OPN_FIDELITY_MAX), |
| 1462 |
m_address(0), |
| 1463 |
m_irq_enable(0x03), |
| 1464 |
m_flag_control(0x03), |
| 1465 |
m_fm(intf), |
| 1466 |
m_ssg(intf), |
| 1467 |
m_ssg_resampler(m_ssg), |
| 1468 |
m_adpcm_a(intf, 0) |
| 1469 |
{ |
| 1470 |
m_last_fm.clear(); |
| 1471 |
update_prescale(); |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
//------------------------------------------------- |
| 1476 |
// reset - reset the system |
| 1477 |
//------------------------------------------------- |
| 1478 |
|
| 1479 |
void ymf288::reset() |
| 1480 |
{ |
| 1481 |
// reset the engines |
| 1482 |
m_fm.reset(); |
| 1483 |
m_ssg.reset(); |
| 1484 |
m_adpcm_a.reset(); |
| 1485 |
|
| 1486 |
// configure ADPCM percussion sounds; these are present in an embedded ROM |
| 1487 |
m_adpcm_a.set_start_end(0, 0x0000, 0x01bf); // bass drum |
| 1488 |
m_adpcm_a.set_start_end(1, 0x01c0, 0x043f); // snare drum |
| 1489 |
m_adpcm_a.set_start_end(2, 0x0440, 0x1b7f); // top cymbal |
| 1490 |
m_adpcm_a.set_start_end(3, 0x1b80, 0x1cff); // high hat |
| 1491 |
m_adpcm_a.set_start_end(4, 0x1d00, 0x1f7f); // tom tom |
| 1492 |
m_adpcm_a.set_start_end(5, 0x1f80, 0x1fff); // rim shot |
| 1493 |
|
| 1494 |
// initialize our special interrupt states, then read the upper status |
| 1495 |
// register, which updates the IRQs |
| 1496 |
m_irq_enable = 0x03; |
| 1497 |
m_flag_control = 0x00; |
| 1498 |
read_status_hi(); |
| 1499 |
} |
| 1500 |
|
| 1501 |
|
| 1502 |
//------------------------------------------------- |
| 1503 |
// save_restore - save or restore the data |
| 1504 |
//------------------------------------------------- |
| 1505 |
|
| 1506 |
void ymf288::save_restore(ymfm_saved_state &state) |
| 1507 |
{ |
| 1508 |
state.save_restore(m_address); |
| 1509 |
state.save_restore(m_irq_enable); |
| 1510 |
state.save_restore(m_flag_control); |
| 1511 |
state.save_restore(m_last_fm.data); |
| 1512 |
|
| 1513 |
m_fm.save_restore(state); |
| 1514 |
m_ssg.save_restore(state); |
| 1515 |
m_ssg_resampler.save_restore(state); |
| 1516 |
m_adpcm_a.save_restore(state); |
| 1517 |
} |
| 1518 |
|
| 1519 |
|
| 1520 |
//------------------------------------------------- |
| 1521 |
// read_status - read the status register |
| 1522 |
//------------------------------------------------- |
| 1523 |
|
| 1524 |
uint8_t ymf288::read_status() |
| 1525 |
{ |
| 1526 |
uint8_t result = m_fm.status() & (fm_engine::STATUS_TIMERA | fm_engine::STATUS_TIMERB); |
| 1527 |
if (m_fm.intf().ymfm_is_busy()) |
| 1528 |
result |= fm_engine::STATUS_BUSY; |
| 1529 |
return result; |
| 1530 |
} |
| 1531 |
|
| 1532 |
|
| 1533 |
//------------------------------------------------- |
| 1534 |
// read_data - read the data register |
| 1535 |
//------------------------------------------------- |
| 1536 |
|
| 1537 |
uint8_t ymf288::read_data() |
| 1538 |
{ |
| 1539 |
uint8_t result = 0; |
| 1540 |
if (m_address < 0x0e) |
| 1541 |
{ |
| 1542 |
// 00-0D: Read from SSG |
| 1543 |
result = m_ssg.read(m_address & 0x0f); |
| 1544 |
} |
| 1545 |
else if (m_address < 0x10) |
| 1546 |
{ |
| 1547 |
// 0E-0F: I/O ports not supported |
| 1548 |
result = 0xff; |
| 1549 |
} |
| 1550 |
else if (m_address == 0xff) |
| 1551 |
{ |
| 1552 |
// FF: ID code |
| 1553 |
result = 2; |
| 1554 |
} |
| 1555 |
else if (ymf288_mode()) |
| 1556 |
{ |
| 1557 |
// registers are readable in YMF288 mode |
| 1558 |
result = m_fm.regs().read(m_address); |
| 1559 |
} |
| 1560 |
return result; |
| 1561 |
} |
| 1562 |
|
| 1563 |
|
| 1564 |
//------------------------------------------------- |
| 1565 |
// read_status_hi - read the extended status |
| 1566 |
// register |
| 1567 |
//------------------------------------------------- |
| 1568 |
|
| 1569 |
uint8_t ymf288::read_status_hi() |
| 1570 |
{ |
| 1571 |
// fetch regular status |
| 1572 |
uint8_t status = m_fm.status() & (fm_engine::STATUS_TIMERA | fm_engine::STATUS_TIMERB); |
| 1573 |
|
| 1574 |
// turn off any bits that have been requested to be masked |
| 1575 |
status &= ~(m_flag_control & 0x03); |
| 1576 |
|
| 1577 |
// update the status so that IRQs are propagated |
| 1578 |
m_fm.set_reset_status(status, ~status); |
| 1579 |
|
| 1580 |
// merge in the busy flag |
| 1581 |
if (m_fm.intf().ymfm_is_busy()) |
| 1582 |
status |= fm_engine::STATUS_BUSY; |
| 1583 |
return status; |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
//------------------------------------------------- |
| 1588 |
// read - handle a read from the device |
| 1589 |
//------------------------------------------------- |
| 1590 |
|
| 1591 |
uint8_t ymf288::read(uint32_t offset) |
| 1592 |
{ |
| 1593 |
uint8_t result = 0; |
| 1594 |
switch (offset & 3) |
| 1595 |
{ |
| 1596 |
case 0: // status port, YM2203 compatible |
| 1597 |
result = read_status(); |
| 1598 |
break; |
| 1599 |
|
| 1600 |
case 1: // data port |
| 1601 |
result = read_data(); |
| 1602 |
break; |
| 1603 |
|
| 1604 |
case 2: // status port, extended |
| 1605 |
result = read_status_hi(); |
| 1606 |
break; |
| 1607 |
|
| 1608 |
case 3: // unmapped |
| 1609 |
debug::log_unexpected_read_write("Unexpected read from YMF288 offset %d\n", offset & 3); |
| 1610 |
break; |
| 1611 |
} |
| 1612 |
return result; |
| 1613 |
} |
| 1614 |
|
| 1615 |
|
| 1616 |
//------------------------------------------------- |
| 1617 |
// write_address - handle a write to the address |
| 1618 |
// register |
| 1619 |
//------------------------------------------------- |
| 1620 |
|
| 1621 |
void ymf288::write_address(uint8_t data) |
| 1622 |
{ |
| 1623 |
// just set the address |
| 1624 |
m_address = data; |
| 1625 |
|
| 1626 |
// in YMF288 mode, busy is signaled after address writes too |
| 1627 |
if (ymf288_mode()) |
| 1628 |
m_fm.intf().ymfm_set_busy_end(16); |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
//------------------------------------------------- |
| 1633 |
// write - handle a write to the data register |
| 1634 |
//------------------------------------------------- |
| 1635 |
|
| 1636 |
void ymf288::write_data(uint8_t data) |
| 1637 |
{ |
| 1638 |
// ignore if paired with upper address |
| 1639 |
if (bitfield(m_address, 8)) |
| 1640 |
return; |
| 1641 |
|
| 1642 |
// wait times are shorter in YMF288 mode |
| 1643 |
int busy_cycles = ymf288_mode() ? 16 : 32 * m_fm.clock_prescale(); |
| 1644 |
if (m_address < 0x0e) |
| 1645 |
{ |
| 1646 |
// 00-0D: write to SSG |
| 1647 |
m_ssg.write(m_address & 0x0f, data); |
| 1648 |
} |
| 1649 |
else if (m_address < 0x10) |
| 1650 |
{ |
| 1651 |
// 0E-0F: I/O ports not supported |
| 1652 |
} |
| 1653 |
else if (m_address < 0x20) |
| 1654 |
{ |
| 1655 |
// 10-1F: write to ADPCM-A |
| 1656 |
m_adpcm_a.write(m_address & 0x0f, data); |
| 1657 |
busy_cycles = 32 * m_fm.clock_prescale(); |
| 1658 |
} |
| 1659 |
else if (m_address == 0x27) |
| 1660 |
{ |
| 1661 |
// 27: mode register; CSM isn't supported so disable it |
| 1662 |
data &= 0x7f; |
| 1663 |
m_fm.write(m_address, data); |
| 1664 |
} |
| 1665 |
else if (m_address == 0x29) |
| 1666 |
{ |
| 1667 |
// 29: special IRQ mask register |
| 1668 |
m_irq_enable = data; |
| 1669 |
m_fm.set_irq_mask(m_irq_enable & ~m_flag_control & 0x03); |
| 1670 |
} |
| 1671 |
else |
| 1672 |
{ |
| 1673 |
// 20-27, 2A-FF: write to FM |
| 1674 |
m_fm.write(m_address, data); |
| 1675 |
} |
| 1676 |
|
| 1677 |
// mark busy for a bit |
| 1678 |
m_fm.intf().ymfm_set_busy_end(busy_cycles); |
| 1679 |
} |
| 1680 |
|
| 1681 |
|
| 1682 |
//------------------------------------------------- |
| 1683 |
// write_address_hi - handle a write to the upper |
| 1684 |
// address register |
| 1685 |
//------------------------------------------------- |
| 1686 |
|
| 1687 |
void ymf288::write_address_hi(uint8_t data) |
| 1688 |
{ |
| 1689 |
// just set the address |
| 1690 |
m_address = 0x100 | data; |
| 1691 |
|
| 1692 |
// in YMF288 mode, busy is signaled after address writes too |
| 1693 |
if (ymf288_mode()) |
| 1694 |
m_fm.intf().ymfm_set_busy_end(16); |
| 1695 |
} |
| 1696 |
|
| 1697 |
|
| 1698 |
//------------------------------------------------- |
| 1699 |
// write_data_hi - handle a write to the upper |
| 1700 |
// data register |
| 1701 |
//------------------------------------------------- |
| 1702 |
|
| 1703 |
void ymf288::write_data_hi(uint8_t data) |
| 1704 |
{ |
| 1705 |
// ignore if paired with upper address |
| 1706 |
if (!bitfield(m_address, 8)) |
| 1707 |
return; |
| 1708 |
|
| 1709 |
// wait times are shorter in YMF288 mode |
| 1710 |
int busy_cycles = ymf288_mode() ? 16 : 32 * m_fm.clock_prescale(); |
| 1711 |
if (m_address == 0x110) |
| 1712 |
{ |
| 1713 |
// 110: IRQ flag control |
| 1714 |
if (bitfield(data, 7)) |
| 1715 |
m_fm.set_reset_status(0, 0xff); |
| 1716 |
else |
| 1717 |
{ |
| 1718 |
m_flag_control = data; |
| 1719 |
m_fm.set_irq_mask(m_irq_enable & ~m_flag_control & 0x03); |
| 1720 |
} |
| 1721 |
} |
| 1722 |
else |
| 1723 |
{ |
| 1724 |
// 100-10F,111-1FF: write to FM |
| 1725 |
m_fm.write(m_address, data); |
| 1726 |
} |
| 1727 |
|
| 1728 |
// mark busy for a bit |
| 1729 |
m_fm.intf().ymfm_set_busy_end(busy_cycles); |
| 1730 |
} |
| 1731 |
|
| 1732 |
|
| 1733 |
//------------------------------------------------- |
| 1734 |
// write - handle a write to the register |
| 1735 |
// interface |
| 1736 |
//------------------------------------------------- |
| 1737 |
|
| 1738 |
void ymf288::write(uint32_t offset, uint8_t data) |
| 1739 |
{ |
| 1740 |
switch (offset & 3) |
| 1741 |
{ |
| 1742 |
case 0: // address port |
| 1743 |
write_address(data); |
| 1744 |
break; |
| 1745 |
|
| 1746 |
case 1: // data port |
| 1747 |
write_data(data); |
| 1748 |
break; |
| 1749 |
|
| 1750 |
case 2: // upper address port |
| 1751 |
write_address_hi(data); |
| 1752 |
break; |
| 1753 |
|
| 1754 |
case 3: // upper data port |
| 1755 |
write_data_hi(data); |
| 1756 |
break; |
| 1757 |
} |
| 1758 |
} |
| 1759 |
|
| 1760 |
|
| 1761 |
//------------------------------------------------- |
| 1762 |
// generate - generate one sample of sound |
| 1763 |
//------------------------------------------------- |
| 1764 |
|
| 1765 |
void ymf288::generate(output_data *output, uint32_t numsamples) |
| 1766 |
{ |
| 1767 |
// FM output is just repeated the prescale number of times; note that |
| 1768 |
// 0 is a special 1.5 case |
| 1769 |
if (m_fm_samples_per_output != 0) |
| 1770 |
{ |
| 1771 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 1772 |
{ |
| 1773 |
if ((m_ssg_resampler.sampindex() + samp) % m_fm_samples_per_output == 0) |
| 1774 |
clock_fm_and_adpcm(); |
| 1775 |
output->data[0] = m_last_fm.data[0]; |
| 1776 |
output->data[1] = m_last_fm.data[1]; |
| 1777 |
} |
| 1778 |
} |
| 1779 |
else |
| 1780 |
{ |
| 1781 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 1782 |
{ |
| 1783 |
uint32_t step = (m_ssg_resampler.sampindex() + samp) % 3; |
| 1784 |
if (step == 0) |
| 1785 |
clock_fm_and_adpcm(); |
| 1786 |
output->data[0] = m_last_fm.data[0]; |
| 1787 |
output->data[1] = m_last_fm.data[1]; |
| 1788 |
if (step == 1) |
| 1789 |
{ |
| 1790 |
clock_fm_and_adpcm(); |
| 1791 |
output->data[0] = (output->data[0] + m_last_fm.data[0]) / 2; |
| 1792 |
output->data[1] = (output->data[1] + m_last_fm.data[1]) / 2; |
| 1793 |
} |
| 1794 |
} |
| 1795 |
} |
| 1796 |
|
| 1797 |
// resample the SSG as configured |
| 1798 |
m_ssg_resampler.resample(output - numsamples, numsamples); |
| 1799 |
} |
| 1800 |
|
| 1801 |
|
| 1802 |
//------------------------------------------------- |
| 1803 |
// update_prescale - update the prescale value, |
| 1804 |
// recomputing derived values |
| 1805 |
//------------------------------------------------- |
| 1806 |
|
| 1807 |
void ymf288::update_prescale() |
| 1808 |
{ |
| 1809 |
// Fidelity: ---- minimum ---- ---- medium ----- ---- maximum----- |
| 1810 |
// rate = clock/144 rate = clock/144 rate = clock/16 |
| 1811 |
// Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate |
| 1812 |
// 6 1:1 2:9 1:1 2:9 9:1 2:1 |
| 1813 |
|
| 1814 |
// compute the number of FM samples per output sample, and select the |
| 1815 |
// resampler function |
| 1816 |
if (m_fidelity == OPN_FIDELITY_MIN || m_fidelity == OPN_FIDELITY_MED) |
| 1817 |
{ |
| 1818 |
m_fm_samples_per_output = 1; |
| 1819 |
m_ssg_resampler.configure(2, 9); |
| 1820 |
} |
| 1821 |
else |
| 1822 |
{ |
| 1823 |
m_fm_samples_per_output = 9; |
| 1824 |
m_ssg_resampler.configure(2, 1); |
| 1825 |
} |
| 1826 |
|
| 1827 |
// if overriding the SSG, override the configuration with the nop |
| 1828 |
// resampler to at least keep the sample index moving forward |
| 1829 |
if (m_ssg.overridden()) |
| 1830 |
m_ssg_resampler.configure(0, 0); |
| 1831 |
} |
| 1832 |
|
| 1833 |
|
| 1834 |
//------------------------------------------------- |
| 1835 |
// clock_fm_and_adpcm - clock FM and ADPCM state |
| 1836 |
//------------------------------------------------- |
| 1837 |
|
| 1838 |
void ymf288::clock_fm_and_adpcm() |
| 1839 |
{ |
| 1840 |
// top bit of the IRQ enable flags controls 3-channel vs 6-channel mode |
| 1841 |
uint32_t fmmask = bitfield(m_irq_enable, 7) ? 0x3f : 0x07; |
| 1842 |
|
| 1843 |
// clock the system |
| 1844 |
uint32_t env_counter = m_fm.clock(fm_engine::ALL_CHANNELS); |
| 1845 |
|
| 1846 |
// clock the ADPCM-A engine on every envelope cycle |
| 1847 |
// (channels 4 and 5 clock every 2 envelope clocks) |
| 1848 |
if (bitfield(env_counter, 0, 2) == 0) |
| 1849 |
m_adpcm_a.clock(bitfield(env_counter, 2) ? 0x0f : 0x3f); |
| 1850 |
|
| 1851 |
// update the FM content; OPNA is 13-bit with no intermediate clipping |
| 1852 |
m_fm.output(m_last_fm.clear(), 1, 32767, fmmask); |
| 1853 |
|
| 1854 |
// mix in the ADPCM |
| 1855 |
m_adpcm_a.output(m_last_fm, 0x3f); |
| 1856 |
} |
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
//********************************************************* |
| 1861 |
// YM2610 |
| 1862 |
//********************************************************* |
| 1863 |
|
| 1864 |
//------------------------------------------------- |
| 1865 |
// ym2610 - constructor |
| 1866 |
//------------------------------------------------- |
| 1867 |
|
| 1868 |
ym2610::ym2610(ymfm_interface &intf, uint8_t channel_mask) : |
| 1869 |
m_fidelity(OPN_FIDELITY_MAX), |
| 1870 |
m_address(0), |
| 1871 |
m_fm_mask(channel_mask), |
| 1872 |
m_eos_status(0x00), |
| 1873 |
m_flag_mask(EOS_FLAGS_MASK), |
| 1874 |
m_fm(intf), |
| 1875 |
m_ssg(intf), |
| 1876 |
m_ssg_resampler(m_ssg), |
| 1877 |
m_adpcm_a(intf, 8), |
| 1878 |
m_adpcm_b(intf, 8) |
| 1879 |
{ |
| 1880 |
update_prescale(); |
| 1881 |
} |
| 1882 |
|
| 1883 |
|
| 1884 |
//------------------------------------------------- |
| 1885 |
// reset - reset the system |
| 1886 |
//------------------------------------------------- |
| 1887 |
|
| 1888 |
void ym2610::reset() |
| 1889 |
{ |
| 1890 |
// reset the engines |
| 1891 |
m_fm.reset(); |
| 1892 |
m_ssg.reset(); |
| 1893 |
m_adpcm_a.reset(); |
| 1894 |
m_adpcm_b.reset(); |
| 1895 |
|
| 1896 |
// initialize our special interrupt states |
| 1897 |
m_eos_status = 0x00; |
| 1898 |
m_flag_mask = EOS_FLAGS_MASK; |
| 1899 |
} |
| 1900 |
|
| 1901 |
|
| 1902 |
//------------------------------------------------- |
| 1903 |
// save_restore - save or restore the data |
| 1904 |
//------------------------------------------------- |
| 1905 |
|
| 1906 |
void ym2610::save_restore(ymfm_saved_state &state) |
| 1907 |
{ |
| 1908 |
state.save_restore(m_address); |
| 1909 |
state.save_restore(m_eos_status); |
| 1910 |
state.save_restore(m_flag_mask); |
| 1911 |
|
| 1912 |
m_fm.save_restore(state); |
| 1913 |
m_ssg.save_restore(state); |
| 1914 |
m_ssg_resampler.save_restore(state); |
| 1915 |
m_adpcm_a.save_restore(state); |
| 1916 |
m_adpcm_b.save_restore(state); |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
//------------------------------------------------- |
| 1921 |
// read_status - read the status register |
| 1922 |
//------------------------------------------------- |
| 1923 |
|
| 1924 |
uint8_t ym2610::read_status() |
| 1925 |
{ |
| 1926 |
uint8_t result = m_fm.status() & (fm_engine::STATUS_TIMERA | fm_engine::STATUS_TIMERB); |
| 1927 |
if (m_fm.intf().ymfm_is_busy()) |
| 1928 |
result |= fm_engine::STATUS_BUSY; |
| 1929 |
return result; |
| 1930 |
} |
| 1931 |
|
| 1932 |
|
| 1933 |
//------------------------------------------------- |
| 1934 |
// read_data - read the data register |
| 1935 |
//------------------------------------------------- |
| 1936 |
|
| 1937 |
uint8_t ym2610::read_data() |
| 1938 |
{ |
| 1939 |
uint8_t result = 0; |
| 1940 |
if (m_address < 0x0e) |
| 1941 |
{ |
| 1942 |
// 00-0D: Read from SSG |
| 1943 |
result = m_ssg.read(m_address & 0x0f); |
| 1944 |
} |
| 1945 |
else if (m_address < 0x10) |
| 1946 |
{ |
| 1947 |
// 0E-0F: I/O ports not supported |
| 1948 |
result = 0xff; |
| 1949 |
} |
| 1950 |
else if (m_address == 0xff) |
| 1951 |
{ |
| 1952 |
// FF: ID code |
| 1953 |
result = 1; |
| 1954 |
} |
| 1955 |
return result; |
| 1956 |
} |
| 1957 |
|
| 1958 |
|
| 1959 |
//------------------------------------------------- |
| 1960 |
// read_status_hi - read the extended status |
| 1961 |
// register |
| 1962 |
//------------------------------------------------- |
| 1963 |
|
| 1964 |
uint8_t ym2610::read_status_hi() |
| 1965 |
{ |
| 1966 |
return m_eos_status & m_flag_mask; |
| 1967 |
} |
| 1968 |
|
| 1969 |
|
| 1970 |
//------------------------------------------------- |
| 1971 |
// read_data_hi - read the upper data register |
| 1972 |
//------------------------------------------------- |
| 1973 |
|
| 1974 |
uint8_t ym2610::read_data_hi() |
| 1975 |
{ |
| 1976 |
uint8_t result = 0; |
| 1977 |
return result; |
| 1978 |
} |
| 1979 |
|
| 1980 |
|
| 1981 |
//------------------------------------------------- |
| 1982 |
// read - handle a read from the device |
| 1983 |
//------------------------------------------------- |
| 1984 |
|
| 1985 |
uint8_t ym2610::read(uint32_t offset) |
| 1986 |
{ |
| 1987 |
uint8_t result = 0; |
| 1988 |
switch (offset & 3) |
| 1989 |
{ |
| 1990 |
case 0: // status port, YM2203 compatible |
| 1991 |
result = read_status(); |
| 1992 |
break; |
| 1993 |
|
| 1994 |
case 1: // data port (only SSG) |
| 1995 |
result = read_data(); |
| 1996 |
break; |
| 1997 |
|
| 1998 |
case 2: // status port, extended |
| 1999 |
result = read_status_hi(); |
| 2000 |
break; |
| 2001 |
|
| 2002 |
case 3: // ADPCM-B data |
| 2003 |
result = read_data_hi(); |
| 2004 |
break; |
| 2005 |
} |
| 2006 |
return result; |
| 2007 |
} |
| 2008 |
|
| 2009 |
|
| 2010 |
//------------------------------------------------- |
| 2011 |
// write_address - handle a write to the address |
| 2012 |
// register |
| 2013 |
//------------------------------------------------- |
| 2014 |
|
| 2015 |
void ym2610::write_address(uint8_t data) |
| 2016 |
{ |
| 2017 |
// just set the address |
| 2018 |
m_address = data; |
| 2019 |
} |
| 2020 |
|
| 2021 |
|
| 2022 |
//------------------------------------------------- |
| 2023 |
// write - handle a write to the data register |
| 2024 |
//------------------------------------------------- |
| 2025 |
|
| 2026 |
void ym2610::write_data(uint8_t data) |
| 2027 |
{ |
| 2028 |
// ignore if paired with upper address |
| 2029 |
if (bitfield(m_address, 8)) |
| 2030 |
return; |
| 2031 |
|
| 2032 |
if (m_address < 0x0e) |
| 2033 |
{ |
| 2034 |
// 00-0D: write to SSG |
| 2035 |
m_ssg.write(m_address & 0x0f, data); |
| 2036 |
} |
| 2037 |
else if (m_address < 0x10) |
| 2038 |
{ |
| 2039 |
// 0E-0F: I/O ports not supported |
| 2040 |
} |
| 2041 |
else if (m_address < 0x1c) |
| 2042 |
{ |
| 2043 |
// 10-1B: write to ADPCM-B |
| 2044 |
// YM2610 effectively forces external mode on, and disables recording |
| 2045 |
if (m_address == 0x10) |
| 2046 |
data = (data | 0x20) & ~0x40; |
| 2047 |
m_adpcm_b.write(m_address & 0x0f, data); |
| 2048 |
} |
| 2049 |
else if (m_address == 0x1c) |
| 2050 |
{ |
| 2051 |
// 1C: EOS flag reset |
| 2052 |
m_flag_mask = ~data & EOS_FLAGS_MASK; |
| 2053 |
m_eos_status &= ~(data & EOS_FLAGS_MASK); |
| 2054 |
} |
| 2055 |
else |
| 2056 |
{ |
| 2057 |
// 1D-FF: write to FM |
| 2058 |
m_fm.write(m_address, data); |
| 2059 |
} |
| 2060 |
|
| 2061 |
// mark busy for a bit |
| 2062 |
m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale()); |
| 2063 |
} |
| 2064 |
|
| 2065 |
|
| 2066 |
//------------------------------------------------- |
| 2067 |
// write_address_hi - handle a write to the upper |
| 2068 |
// address register |
| 2069 |
//------------------------------------------------- |
| 2070 |
|
| 2071 |
void ym2610::write_address_hi(uint8_t data) |
| 2072 |
{ |
| 2073 |
// just set the address |
| 2074 |
m_address = 0x100 | data; |
| 2075 |
} |
| 2076 |
|
| 2077 |
|
| 2078 |
//------------------------------------------------- |
| 2079 |
// write_data_hi - handle a write to the upper |
| 2080 |
// data register |
| 2081 |
//------------------------------------------------- |
| 2082 |
|
| 2083 |
void ym2610::write_data_hi(uint8_t data) |
| 2084 |
{ |
| 2085 |
// ignore if paired with upper address |
| 2086 |
if (!bitfield(m_address, 8)) |
| 2087 |
return; |
| 2088 |
|
| 2089 |
if (m_address < 0x130) |
| 2090 |
{ |
| 2091 |
// 100-12F: write to ADPCM-A |
| 2092 |
m_adpcm_a.write(m_address & 0x3f, data); |
| 2093 |
} |
| 2094 |
else |
| 2095 |
{ |
| 2096 |
// 130-1FF: write to FM |
| 2097 |
m_fm.write(m_address, data); |
| 2098 |
} |
| 2099 |
|
| 2100 |
// mark busy for a bit |
| 2101 |
m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale()); |
| 2102 |
} |
| 2103 |
|
| 2104 |
|
| 2105 |
//------------------------------------------------- |
| 2106 |
// write - handle a write to the register |
| 2107 |
// interface |
| 2108 |
//------------------------------------------------- |
| 2109 |
|
| 2110 |
void ym2610::write(uint32_t offset, uint8_t data) |
| 2111 |
{ |
| 2112 |
switch (offset & 3) |
| 2113 |
{ |
| 2114 |
case 0: // address port |
| 2115 |
write_address(data); |
| 2116 |
break; |
| 2117 |
|
| 2118 |
case 1: // data port |
| 2119 |
write_data(data); |
| 2120 |
break; |
| 2121 |
|
| 2122 |
case 2: // upper address port |
| 2123 |
write_address_hi(data); |
| 2124 |
break; |
| 2125 |
|
| 2126 |
case 3: // upper data port |
| 2127 |
write_data_hi(data); |
| 2128 |
break; |
| 2129 |
} |
| 2130 |
} |
| 2131 |
|
| 2132 |
|
| 2133 |
//------------------------------------------------- |
| 2134 |
// generate - generate one sample of sound |
| 2135 |
//------------------------------------------------- |
| 2136 |
|
| 2137 |
void ym2610::generate(output_data *output, uint32_t numsamples) |
| 2138 |
{ |
| 2139 |
// FM output is just repeated the prescale number of times |
| 2140 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 2141 |
{ |
| 2142 |
if ((m_ssg_resampler.sampindex() + samp) % m_fm_samples_per_output == 0) |
| 2143 |
clock_fm_and_adpcm(); |
| 2144 |
output->data[0] = m_last_fm.data[0]; |
| 2145 |
output->data[1] = m_last_fm.data[1]; |
| 2146 |
} |
| 2147 |
|
| 2148 |
// resample the SSG as configured |
| 2149 |
m_ssg_resampler.resample(output - numsamples, numsamples); |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
//------------------------------------------------- |
| 2154 |
// update_prescale - update the prescale value, |
| 2155 |
// recomputing derived values |
| 2156 |
//------------------------------------------------- |
| 2157 |
|
| 2158 |
void ym2610::update_prescale() |
| 2159 |
{ |
| 2160 |
// Fidelity: ---- minimum ---- ---- medium ----- ---- maximum----- |
| 2161 |
// rate = clock/144 rate = clock/144 rate = clock/16 |
| 2162 |
// Prescale FM rate SSG rate FM rate SSG rate FM rate SSG rate |
| 2163 |
// 6 1:1 2:9 1:1 2:9 9:1 2:1 |
| 2164 |
|
| 2165 |
// compute the number of FM samples per output sample, and select the |
| 2166 |
// resampler function |
| 2167 |
if (m_fidelity == OPN_FIDELITY_MIN || m_fidelity == OPN_FIDELITY_MED) |
| 2168 |
{ |
| 2169 |
m_fm_samples_per_output = 1; |
| 2170 |
m_ssg_resampler.configure(2, 9); |
| 2171 |
} |
| 2172 |
else |
| 2173 |
{ |
| 2174 |
m_fm_samples_per_output = 9; |
| 2175 |
m_ssg_resampler.configure(2, 1); |
| 2176 |
} |
| 2177 |
|
| 2178 |
// if overriding the SSG, override the configuration with the nop |
| 2179 |
// resampler to at least keep the sample index moving forward |
| 2180 |
if (m_ssg.overridden()) |
| 2181 |
m_ssg_resampler.configure(0, 0); |
| 2182 |
} |
| 2183 |
|
| 2184 |
|
| 2185 |
//------------------------------------------------- |
| 2186 |
// clock_fm_and_adpcm - clock FM and ADPCM state |
| 2187 |
//------------------------------------------------- |
| 2188 |
|
| 2189 |
void ym2610::clock_fm_and_adpcm() |
| 2190 |
{ |
| 2191 |
// clock the system |
| 2192 |
uint32_t env_counter = m_fm.clock(m_fm_mask); |
| 2193 |
|
| 2194 |
// clock the ADPCM-A engine on every envelope cycle |
| 2195 |
if (bitfield(env_counter, 0, 2) == 0) |
| 2196 |
m_eos_status |= m_adpcm_a.clock(0x3f); |
| 2197 |
|
| 2198 |
// clock the ADPCM-B engine every cycle |
| 2199 |
m_adpcm_b.clock(); |
| 2200 |
|
| 2201 |
// we track the last ADPCM-B EOS value in bit 6 (which is hidden from callers); |
| 2202 |
// if it changed since the last sample, update the visible EOS state in bit 7 |
| 2203 |
uint8_t live_eos = ((m_adpcm_b.status() & adpcm_b_channel::STATUS_EOS) != 0) ? 0x40 : 0x00; |
| 2204 |
if (((live_eos ^ m_eos_status) & 0x40) != 0) |
| 2205 |
m_eos_status = (m_eos_status & ~0xc0) | live_eos | (live_eos << 1); |
| 2206 |
|
| 2207 |
// update the FM content; OPNB is 13-bit with no intermediate clipping |
| 2208 |
m_fm.output(m_last_fm.clear(), 1, 32767, m_fm_mask); |
| 2209 |
|
| 2210 |
// mix in the ADPCM and clamp |
| 2211 |
m_adpcm_a.output(m_last_fm, 0x3f); |
| 2212 |
m_adpcm_b.output(m_last_fm, 1); |
| 2213 |
m_last_fm.clamp16(); |
| 2214 |
} |
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
//********************************************************* |
| 2219 |
// YM2612 |
| 2220 |
//********************************************************* |
| 2221 |
|
| 2222 |
//------------------------------------------------- |
| 2223 |
// ym2612 - constructor |
| 2224 |
//------------------------------------------------- |
| 2225 |
|
| 2226 |
ym2612::ym2612(ymfm_interface &intf) : |
| 2227 |
m_address(0), |
| 2228 |
m_dac_data(0), |
| 2229 |
m_dac_enable(0), |
| 2230 |
m_fm(intf) |
| 2231 |
{ |
| 2232 |
} |
| 2233 |
|
| 2234 |
|
| 2235 |
//------------------------------------------------- |
| 2236 |
// reset - reset the system |
| 2237 |
//------------------------------------------------- |
| 2238 |
|
| 2239 |
void ym2612::reset() |
| 2240 |
{ |
| 2241 |
// reset the engines |
| 2242 |
m_fm.reset(); |
| 2243 |
} |
| 2244 |
|
| 2245 |
|
| 2246 |
//------------------------------------------------- |
| 2247 |
// save_restore - save or restore the data |
| 2248 |
//------------------------------------------------- |
| 2249 |
|
| 2250 |
void ym2612::save_restore(ymfm_saved_state &state) |
| 2251 |
{ |
| 2252 |
state.save_restore(m_address); |
| 2253 |
state.save_restore(m_dac_data); |
| 2254 |
state.save_restore(m_dac_enable); |
| 2255 |
m_fm.save_restore(state); |
| 2256 |
} |
| 2257 |
|
| 2258 |
|
| 2259 |
//------------------------------------------------- |
| 2260 |
// read_status - read the status register |
| 2261 |
//------------------------------------------------- |
| 2262 |
|
| 2263 |
uint8_t ym2612::read_status() |
| 2264 |
{ |
| 2265 |
uint8_t result = m_fm.status(); |
| 2266 |
if (m_fm.intf().ymfm_is_busy()) |
| 2267 |
result |= fm_engine::STATUS_BUSY; |
| 2268 |
return result; |
| 2269 |
} |
| 2270 |
|
| 2271 |
|
| 2272 |
//------------------------------------------------- |
| 2273 |
// read - handle a read from the device |
| 2274 |
//------------------------------------------------- |
| 2275 |
|
| 2276 |
uint8_t ym2612::read(uint32_t offset) |
| 2277 |
{ |
| 2278 |
uint8_t result = 0; |
| 2279 |
switch (offset & 3) |
| 2280 |
{ |
| 2281 |
case 0: // status port, YM2203 compatible |
| 2282 |
result = read_status(); |
| 2283 |
break; |
| 2284 |
|
| 2285 |
case 1: // data port (unused) |
| 2286 |
case 2: // status port, extended |
| 2287 |
case 3: // data port (unused) |
| 2288 |
debug::log_unexpected_read_write("Unexpected read from YM2612 offset %d\n", offset & 3); |
| 2289 |
break; |
| 2290 |
} |
| 2291 |
return result; |
| 2292 |
} |
| 2293 |
|
| 2294 |
|
| 2295 |
//------------------------------------------------- |
| 2296 |
// write_address - handle a write to the address |
| 2297 |
// register |
| 2298 |
//------------------------------------------------- |
| 2299 |
|
| 2300 |
void ym2612::write_address(uint8_t data) |
| 2301 |
{ |
| 2302 |
// just set the address |
| 2303 |
m_address = data; |
| 2304 |
} |
| 2305 |
|
| 2306 |
|
| 2307 |
//------------------------------------------------- |
| 2308 |
// write_data - handle a write to the data |
| 2309 |
// register |
| 2310 |
//------------------------------------------------- |
| 2311 |
|
| 2312 |
void ym2612::write_data(uint8_t data) |
| 2313 |
{ |
| 2314 |
// ignore if paired with upper address |
| 2315 |
if (bitfield(m_address, 8)) |
| 2316 |
return; |
| 2317 |
|
| 2318 |
if (m_address == 0x2a) |
| 2319 |
{ |
| 2320 |
// 2A: DAC data (most significant 8 bits) |
| 2321 |
m_dac_data = (m_dac_data & ~0x1fe) | ((data ^ 0x80) << 1); |
| 2322 |
} |
| 2323 |
else if (m_address == 0x2b) |
| 2324 |
{ |
| 2325 |
// 2B: DAC enable (bit 7) |
| 2326 |
m_dac_enable = bitfield(data, 7); |
| 2327 |
} |
| 2328 |
else if (m_address == 0x2c) |
| 2329 |
{ |
| 2330 |
// 2C: test/low DAC bit |
| 2331 |
m_dac_data = (m_dac_data & ~1) | bitfield(data, 3); |
| 2332 |
} |
| 2333 |
else |
| 2334 |
{ |
| 2335 |
// 00-29, 2D-FF: write to FM |
| 2336 |
m_fm.write(m_address, data); |
| 2337 |
} |
| 2338 |
|
| 2339 |
// mark busy for a bit |
| 2340 |
m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale()); |
| 2341 |
} |
| 2342 |
|
| 2343 |
|
| 2344 |
//------------------------------------------------- |
| 2345 |
// write_address_hi - handle a write to the upper |
| 2346 |
// address register |
| 2347 |
//------------------------------------------------- |
| 2348 |
|
| 2349 |
void ym2612::write_address_hi(uint8_t data) |
| 2350 |
{ |
| 2351 |
// just set the address |
| 2352 |
m_address = 0x100 | data; |
| 2353 |
} |
| 2354 |
|
| 2355 |
|
| 2356 |
//------------------------------------------------- |
| 2357 |
// write_data_hi - handle a write to the upper |
| 2358 |
// data register |
| 2359 |
//------------------------------------------------- |
| 2360 |
|
| 2361 |
void ym2612::write_data_hi(uint8_t data) |
| 2362 |
{ |
| 2363 |
// ignore if paired with upper address |
| 2364 |
if (!bitfield(m_address, 8)) |
| 2365 |
return; |
| 2366 |
|
| 2367 |
// 100-1FF: write to FM |
| 2368 |
m_fm.write(m_address, data); |
| 2369 |
|
| 2370 |
// mark busy for a bit |
| 2371 |
m_fm.intf().ymfm_set_busy_end(32 * m_fm.clock_prescale()); |
| 2372 |
} |
| 2373 |
|
| 2374 |
|
| 2375 |
//------------------------------------------------- |
| 2376 |
// write - handle a write to the register |
| 2377 |
// interface |
| 2378 |
//------------------------------------------------- |
| 2379 |
|
| 2380 |
void ym2612::write(uint32_t offset, uint8_t data) |
| 2381 |
{ |
| 2382 |
switch (offset & 3) |
| 2383 |
{ |
| 2384 |
case 0: // address port |
| 2385 |
write_address(data); |
| 2386 |
break; |
| 2387 |
|
| 2388 |
case 1: // data port |
| 2389 |
write_data(data); |
| 2390 |
break; |
| 2391 |
|
| 2392 |
case 2: // upper address port |
| 2393 |
write_address_hi(data); |
| 2394 |
break; |
| 2395 |
|
| 2396 |
case 3: // upper data port |
| 2397 |
write_data_hi(data); |
| 2398 |
break; |
| 2399 |
} |
| 2400 |
} |
| 2401 |
|
| 2402 |
|
| 2403 |
//------------------------------------------------- |
| 2404 |
// generate - generate one sample of sound |
| 2405 |
//------------------------------------------------- |
| 2406 |
|
| 2407 |
void ym2612::generate(output_data *output, uint32_t numsamples) |
| 2408 |
{ |
| 2409 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 2410 |
{ |
| 2411 |
// clock the system |
| 2412 |
m_fm.clock(fm_engine::ALL_CHANNELS); |
| 2413 |
|
| 2414 |
// sum individual channels to apply DAC discontinuity on each |
| 2415 |
output->clear(); |
| 2416 |
output_data temp; |
| 2417 |
|
| 2418 |
// first do FM-only channels; OPN2 is 9-bit with intermediate clipping |
| 2419 |
int const last_fm_channel = m_dac_enable ? 5 : 6; |
| 2420 |
for (int chan = 0; chan < last_fm_channel; chan++) |
| 2421 |
{ |
| 2422 |
m_fm.output(temp.clear(), 5, 256, 1 << chan); |
| 2423 |
output->data[0] += dac_discontinuity(temp.data[0]); |
| 2424 |
output->data[1] += dac_discontinuity(temp.data[1]); |
| 2425 |
} |
| 2426 |
|
| 2427 |
// add in DAC |
| 2428 |
if (m_dac_enable) |
| 2429 |
{ |
| 2430 |
// DAC enabled: start with DAC value then add the first 5 channels only |
| 2431 |
int32_t dacval = dac_discontinuity(int16_t(m_dac_data << 7) >> 7); |
| 2432 |
output->data[0] += m_fm.regs().ch_output_0(0x102) ? dacval : dac_discontinuity(0); |
| 2433 |
output->data[1] += m_fm.regs().ch_output_1(0x102) ? dacval : dac_discontinuity(0); |
| 2434 |
} |
| 2435 |
|
| 2436 |
// output is technically multiplexed rather than mixed, but that requires |
| 2437 |
// a better sound mixer than we usually have, so just average over the six |
| 2438 |
// channels; also apply a 64/65 factor to account for the discontinuity |
| 2439 |
// adjustment above |
| 2440 |
output->data[0] = (output->data[0] * 128) * 64 / (6 * 65); |
| 2441 |
output->data[1] = (output->data[1] * 128) * 64 / (6 * 65); |
| 2442 |
} |
| 2443 |
} |
| 2444 |
|
| 2445 |
|
| 2446 |
//------------------------------------------------- |
| 2447 |
// generate - generate one sample of sound |
| 2448 |
//------------------------------------------------- |
| 2449 |
|
| 2450 |
void ym3438::generate(output_data *output, uint32_t numsamples) |
| 2451 |
{ |
| 2452 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 2453 |
{ |
| 2454 |
// clock the system |
| 2455 |
m_fm.clock(fm_engine::ALL_CHANNELS); |
| 2456 |
|
| 2457 |
// first do FM-only channels; OPN2C is 9-bit with intermediate clipping |
| 2458 |
if (!m_dac_enable) |
| 2459 |
{ |
| 2460 |
// DAC disabled: all 6 channels sum together |
| 2461 |
m_fm.output(output->clear(), 5, 256, fm_engine::ALL_CHANNELS); |
| 2462 |
} |
| 2463 |
else |
| 2464 |
{ |
| 2465 |
// DAC enabled: start with DAC value then add the first 5 channels only |
| 2466 |
int32_t dacval = int16_t(m_dac_data << 7) >> 7; |
| 2467 |
output->data[0] = m_fm.regs().ch_output_0(0x102) ? dacval : 0; |
| 2468 |
output->data[1] = m_fm.regs().ch_output_1(0x102) ? dacval : 0; |
| 2469 |
m_fm.output(*output, 5, 256, fm_engine::ALL_CHANNELS ^ (1 << 5)); |
| 2470 |
} |
| 2471 |
|
| 2472 |
// YM3438 doesn't have the same DAC discontinuity, though its output is |
| 2473 |
// multiplexed like the YM2612 |
| 2474 |
output->data[0] = (output->data[0] * 128) / 6; |
| 2475 |
output->data[1] = (output->data[1] * 128) / 6; |
| 2476 |
} |
| 2477 |
} |
| 2478 |
|
| 2479 |
|
| 2480 |
//------------------------------------------------- |
| 2481 |
// generate - generate one sample of sound |
| 2482 |
//------------------------------------------------- |
| 2483 |
|
| 2484 |
void ymf276::generate(output_data *output, uint32_t numsamples) |
| 2485 |
{ |
| 2486 |
for (uint32_t samp = 0; samp < numsamples; samp++, output++) |
| 2487 |
{ |
| 2488 |
// clock the system |
| 2489 |
m_fm.clock(fm_engine::ALL_CHANNELS); |
| 2490 |
|
| 2491 |
// first do FM-only channels; OPN2L is 14-bit with intermediate clipping |
| 2492 |
if (!m_dac_enable) |
| 2493 |
{ |
| 2494 |
// DAC disabled: all 6 channels sum together |
| 2495 |
m_fm.output(output->clear(), 0, 8191, fm_engine::ALL_CHANNELS); |
| 2496 |
} |
| 2497 |
else |
| 2498 |
{ |
| 2499 |
// DAC enabled: start with DAC value then add the first 5 channels only |
| 2500 |
int32_t dacval = int16_t(m_dac_data << 7) >> 7; |
| 2501 |
output->data[0] = m_fm.regs().ch_output_0(0x102) ? dacval : 0; |
| 2502 |
output->data[1] = m_fm.regs().ch_output_1(0x102) ? dacval : 0; |
| 2503 |
m_fm.output(*output, 0, 8191, fm_engine::ALL_CHANNELS ^ (1 << 5)); |
| 2504 |
} |
| 2505 |
|
| 2506 |
// YMF276 is properly mixed; it shifts down 1 bit before clamping |
| 2507 |
output->data[0] = clamp(output->data[0] >> 1, -32768, 32767); |
| 2508 |
output->data[1] = clamp(output->data[1] >> 1, -32768, 32767); |
| 2509 |
} |
| 2510 |
} |
| 2511 |
#endif |
| 2512 |
} |