| 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_adpcm.h" |
| 32 |
|
| 33 |
namespace ymfm |
| 34 |
{ |
| 35 |
|
| 36 |
//********************************************************* |
| 37 |
// ADPCM "A" REGISTERS |
| 38 |
//********************************************************* |
| 39 |
|
| 40 |
//------------------------------------------------- |
| 41 |
// reset - reset the register state |
| 42 |
//------------------------------------------------- |
| 43 |
|
| 44 |
void adpcm_a_registers::reset() |
| 45 |
{ |
| 46 |
std::fill_n(&m_regdata[0], REGISTERS, 0); |
| 47 |
|
| 48 |
// initialize the pans to on by default, and max instrument volume; |
| 49 |
// some neogeo homebrews (for example ffeast) rely on this |
| 50 |
m_regdata[0x08] = m_regdata[0x09] = m_regdata[0x0a] = |
| 51 |
m_regdata[0x0b] = m_regdata[0x0c] = m_regdata[0x0d] = 0xdf; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
//------------------------------------------------- |
| 56 |
// save_restore - save or restore the data |
| 57 |
//------------------------------------------------- |
| 58 |
|
| 59 |
void adpcm_a_registers::save_restore(ymfm_saved_state &state) |
| 60 |
{ |
| 61 |
state.save_restore(m_regdata); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
//********************************************************* |
| 66 |
// ADPCM "A" CHANNEL |
| 67 |
//********************************************************* |
| 68 |
|
| 69 |
//------------------------------------------------- |
| 70 |
// adpcm_a_channel - constructor |
| 71 |
//------------------------------------------------- |
| 72 |
|
| 73 |
adpcm_a_channel::adpcm_a_channel(adpcm_a_engine &owner, uint32_t choffs, uint32_t addrshift) : |
| 74 |
m_choffs(choffs), |
| 75 |
m_address_shift(addrshift), |
| 76 |
m_playing(0), |
| 77 |
m_curnibble(0), |
| 78 |
m_curbyte(0), |
| 79 |
m_curaddress(0), |
| 80 |
m_accumulator(0), |
| 81 |
m_step_index(0), |
| 82 |
m_regs(owner.regs()), |
| 83 |
m_owner(owner) |
| 84 |
{ |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
//------------------------------------------------- |
| 89 |
// reset - reset the channel state |
| 90 |
//------------------------------------------------- |
| 91 |
|
| 92 |
void adpcm_a_channel::reset() |
| 93 |
{ |
| 94 |
m_playing = 0; |
| 95 |
m_curnibble = 0; |
| 96 |
m_curbyte = 0; |
| 97 |
m_curaddress = 0; |
| 98 |
m_accumulator = 0; |
| 99 |
m_step_index = 0; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
//------------------------------------------------- |
| 104 |
// save_restore - save or restore the data |
| 105 |
//------------------------------------------------- |
| 106 |
|
| 107 |
void adpcm_a_channel::save_restore(ymfm_saved_state &state) |
| 108 |
{ |
| 109 |
state.save_restore(m_playing); |
| 110 |
state.save_restore(m_curnibble); |
| 111 |
state.save_restore(m_curbyte); |
| 112 |
state.save_restore(m_curaddress); |
| 113 |
state.save_restore(m_accumulator); |
| 114 |
state.save_restore(m_step_index); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
//------------------------------------------------- |
| 119 |
// keyonoff - signal key on/off |
| 120 |
//------------------------------------------------- |
| 121 |
|
| 122 |
void adpcm_a_channel::keyonoff(bool on) |
| 123 |
{ |
| 124 |
// QUESTION: repeated key ons restart the sample? |
| 125 |
m_playing = on; |
| 126 |
if (m_playing) |
| 127 |
{ |
| 128 |
m_curaddress = m_regs.ch_start(m_choffs) << m_address_shift; |
| 129 |
m_curnibble = 0; |
| 130 |
m_curbyte = 0; |
| 131 |
m_accumulator = 0; |
| 132 |
m_step_index = 0; |
| 133 |
|
| 134 |
// don't log masked channels |
| 135 |
if (((debug::GLOBAL_ADPCM_A_CHANNEL_MASK >> m_choffs) & 1) != 0) |
| 136 |
debug::log_keyon("KeyOn ADPCM-A%d: pan=%d%d start=%04X end=%04X level=%02X\n", |
| 137 |
m_choffs, |
| 138 |
m_regs.ch_pan_left(m_choffs), |
| 139 |
m_regs.ch_pan_right(m_choffs), |
| 140 |
m_regs.ch_start(m_choffs), |
| 141 |
m_regs.ch_end(m_choffs), |
| 142 |
m_regs.ch_instrument_level(m_choffs)); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
//------------------------------------------------- |
| 148 |
// clock - master clocking function |
| 149 |
//------------------------------------------------- |
| 150 |
|
| 151 |
bool adpcm_a_channel::clock() |
| 152 |
{ |
| 153 |
// if not playing, just output 0 |
| 154 |
if (m_playing == 0) |
| 155 |
{ |
| 156 |
m_accumulator = 0; |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
// if we're about to read nibble 0, fetch the data |
| 161 |
uint8_t data; |
| 162 |
if (m_curnibble == 0) |
| 163 |
{ |
| 164 |
// stop when we hit the end address; apparently only low 20 bits are used for |
| 165 |
// comparison on the YM2610: this affects sample playback in some games, for |
| 166 |
// example twinspri character select screen music will skip some samples if |
| 167 |
// this is not correct |
| 168 |
// |
| 169 |
// note also: end address is inclusive, so wait until we are about to fetch |
| 170 |
// the sample just after the end before stopping; this is needed for nitd's |
| 171 |
// jump sound, for example |
| 172 |
uint32_t end = (m_regs.ch_end(m_choffs) + 1) << m_address_shift; |
| 173 |
if (((m_curaddress ^ end) & 0xfffff) == 0) |
| 174 |
{ |
| 175 |
m_playing = m_accumulator = 0; |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
m_curbyte = m_owner.intf().ymfm_external_read(ACCESS_ADPCM_A, m_curaddress++); |
| 180 |
data = m_curbyte >> 4; |
| 181 |
m_curnibble = 1; |
| 182 |
} |
| 183 |
|
| 184 |
// otherwise just extract from the previosuly-fetched byte |
| 185 |
else |
| 186 |
{ |
| 187 |
data = m_curbyte & 0xf; |
| 188 |
m_curnibble = 0; |
| 189 |
} |
| 190 |
|
| 191 |
// compute the ADPCM delta |
| 192 |
static uint16_t const s_steps[49] = |
| 193 |
{ |
| 194 |
16, 17, 19, 21, 23, 25, 28, |
| 195 |
31, 34, 37, 41, 45, 50, 55, |
| 196 |
60, 66, 73, 80, 88, 97, 107, |
| 197 |
118, 130, 143, 157, 173, 190, 209, |
| 198 |
230, 253, 279, 307, 337, 371, 408, |
| 199 |
449, 494, 544, 598, 658, 724, 796, |
| 200 |
876, 963, 1060, 1166, 1282, 1411, 1552 |
| 201 |
}; |
| 202 |
int32_t delta = (2 * bitfield(data, 0, 3) + 1) * s_steps[m_step_index] / 8; |
| 203 |
if (bitfield(data, 3)) |
| 204 |
delta = -delta; |
| 205 |
|
| 206 |
// the 12-bit accumulator wraps on the ym2610 and ym2608 (like the msm5205) |
| 207 |
m_accumulator = (m_accumulator + delta) & 0xfff; |
| 208 |
|
| 209 |
// adjust ADPCM step |
| 210 |
static int8_t const s_step_inc[8] = { -1, -1, -1, -1, 2, 5, 7, 9 }; |
| 211 |
m_step_index = clamp(m_step_index + s_step_inc[bitfield(data, 0, 3)], 0, 48); |
| 212 |
|
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
//------------------------------------------------- |
| 218 |
// output - return the computed output value, with |
| 219 |
// panning applied |
| 220 |
//------------------------------------------------- |
| 221 |
|
| 222 |
template<int NumOutputs> |
| 223 |
void adpcm_a_channel::output(ymfm_output<NumOutputs> &output) const |
| 224 |
{ |
| 225 |
// volume combines instrument and total levels |
| 226 |
int vol = (m_regs.ch_instrument_level(m_choffs) ^ 0x1f) + (m_regs.total_level() ^ 0x3f); |
| 227 |
|
| 228 |
// if combined is maximum, don't add to outputs |
| 229 |
if (vol >= 63) |
| 230 |
return; |
| 231 |
|
| 232 |
// convert into a shift and a multiplier |
| 233 |
// QUESTION: verify this from other sources |
| 234 |
int8_t mul = 15 - (vol & 7); |
| 235 |
uint8_t shift = 4 + 1 + (vol >> 3); |
| 236 |
|
| 237 |
// m_accumulator is a 12-bit value; shift up to sign-extend; |
| 238 |
// the downshift is incorporated into 'shift' |
| 239 |
int16_t value = ((int16_t(m_accumulator << 4) * mul) >> shift) & ~3; |
| 240 |
|
| 241 |
// apply to left/right as appropriate |
| 242 |
if (NumOutputs == 1 || m_regs.ch_pan_left(m_choffs)) |
| 243 |
output.data[0] += value; |
| 244 |
if (NumOutputs > 1 && m_regs.ch_pan_right(m_choffs)) |
| 245 |
output.data[1] += value; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
//********************************************************* |
| 251 |
// ADPCM "A" ENGINE |
| 252 |
//********************************************************* |
| 253 |
|
| 254 |
//------------------------------------------------- |
| 255 |
// adpcm_a_engine - constructor |
| 256 |
//------------------------------------------------- |
| 257 |
|
| 258 |
adpcm_a_engine::adpcm_a_engine(ymfm_interface &intf, uint32_t addrshift) : |
| 259 |
m_intf(intf) |
| 260 |
{ |
| 261 |
// create the channels |
| 262 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 263 |
m_channel[chnum] = std::make_unique<adpcm_a_channel>(*this, chnum, addrshift); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
//------------------------------------------------- |
| 268 |
// reset - reset the engine state |
| 269 |
//------------------------------------------------- |
| 270 |
|
| 271 |
void adpcm_a_engine::reset() |
| 272 |
{ |
| 273 |
// reset register state |
| 274 |
m_regs.reset(); |
| 275 |
|
| 276 |
// reset each channel |
| 277 |
for (auto &chan : m_channel) |
| 278 |
chan->reset(); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
//------------------------------------------------- |
| 283 |
// save_restore - save or restore the data |
| 284 |
//------------------------------------------------- |
| 285 |
|
| 286 |
void adpcm_a_engine::save_restore(ymfm_saved_state &state) |
| 287 |
{ |
| 288 |
// save register state |
| 289 |
m_regs.save_restore(state); |
| 290 |
|
| 291 |
// save channel state |
| 292 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 293 |
m_channel[chnum]->save_restore(state); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
//------------------------------------------------- |
| 298 |
// clock - master clocking function |
| 299 |
//------------------------------------------------- |
| 300 |
|
| 301 |
uint32_t adpcm_a_engine::clock(uint32_t chanmask) |
| 302 |
{ |
| 303 |
// clock each channel, setting a bit in result if it finished |
| 304 |
uint32_t result = 0; |
| 305 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 306 |
if (bitfield(chanmask, chnum)) |
| 307 |
if (m_channel[chnum]->clock()) |
| 308 |
result |= 1 << chnum; |
| 309 |
|
| 310 |
// return the bitmask of completed samples |
| 311 |
return result; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
//------------------------------------------------- |
| 316 |
// update - master update function |
| 317 |
//------------------------------------------------- |
| 318 |
|
| 319 |
template<int NumOutputs> |
| 320 |
void adpcm_a_engine::output(ymfm_output<NumOutputs> &output, uint32_t chanmask) |
| 321 |
{ |
| 322 |
// mask out some channels for debug purposes |
| 323 |
chanmask &= debug::GLOBAL_ADPCM_A_CHANNEL_MASK; |
| 324 |
|
| 325 |
// compute the output of each channel |
| 326 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 327 |
if (bitfield(chanmask, chnum)) |
| 328 |
m_channel[chnum]->output(output); |
| 329 |
} |
| 330 |
|
| 331 |
template void adpcm_a_engine::output<1>(ymfm_output<1> &output, uint32_t chanmask); |
| 332 |
template void adpcm_a_engine::output<2>(ymfm_output<2> &output, uint32_t chanmask); |
| 333 |
|
| 334 |
|
| 335 |
//------------------------------------------------- |
| 336 |
// write - handle writes to the ADPCM-A registers |
| 337 |
//------------------------------------------------- |
| 338 |
|
| 339 |
void adpcm_a_engine::write(uint32_t regnum, uint8_t data) |
| 340 |
{ |
| 341 |
// store the raw value to the register array; |
| 342 |
// most writes are passive, consumed only when needed |
| 343 |
m_regs.write(regnum, data); |
| 344 |
|
| 345 |
// actively handle writes to the control register |
| 346 |
if (regnum == 0x00) |
| 347 |
for (int chnum = 0; chnum < CHANNELS; chnum++) |
| 348 |
if (bitfield(data, chnum)) |
| 349 |
m_channel[chnum]->keyonoff(bitfield(~data, 7)); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
//********************************************************* |
| 355 |
// ADPCM "B" REGISTERS |
| 356 |
//********************************************************* |
| 357 |
|
| 358 |
//------------------------------------------------- |
| 359 |
// reset - reset the register state |
| 360 |
//------------------------------------------------- |
| 361 |
|
| 362 |
void adpcm_b_registers::reset() |
| 363 |
{ |
| 364 |
std::fill_n(&m_regdata[0], REGISTERS, 0); |
| 365 |
|
| 366 |
// default limit to wide open |
| 367 |
m_regdata[0x0c] = m_regdata[0x0d] = 0xff; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
//------------------------------------------------- |
| 372 |
// save_restore - save or restore the data |
| 373 |
//------------------------------------------------- |
| 374 |
|
| 375 |
void adpcm_b_registers::save_restore(ymfm_saved_state &state) |
| 376 |
{ |
| 377 |
state.save_restore(m_regdata); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
//********************************************************* |
| 383 |
// ADPCM "B" CHANNEL |
| 384 |
//********************************************************* |
| 385 |
|
| 386 |
//------------------------------------------------- |
| 387 |
// adpcm_b_channel - constructor |
| 388 |
//------------------------------------------------- |
| 389 |
|
| 390 |
adpcm_b_channel::adpcm_b_channel(adpcm_b_engine &owner, uint32_t addrshift) : |
| 391 |
m_address_shift(addrshift), |
| 392 |
m_status(STATUS_BRDY), |
| 393 |
m_curnibble(0), |
| 394 |
m_curbyte(0), |
| 395 |
m_dummy_read(0), |
| 396 |
m_position(0), |
| 397 |
m_curaddress(0), |
| 398 |
m_accumulator(0), |
| 399 |
m_prev_accum(0), |
| 400 |
m_adpcm_step(STEP_MIN), |
| 401 |
m_regs(owner.regs()), |
| 402 |
m_owner(owner) |
| 403 |
{ |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
//------------------------------------------------- |
| 408 |
// reset - reset the channel state |
| 409 |
//------------------------------------------------- |
| 410 |
|
| 411 |
void adpcm_b_channel::reset() |
| 412 |
{ |
| 413 |
m_status = STATUS_BRDY; |
| 414 |
m_curnibble = 0; |
| 415 |
m_curbyte = 0; |
| 416 |
m_dummy_read = 0; |
| 417 |
m_position = 0; |
| 418 |
m_curaddress = 0; |
| 419 |
m_accumulator = 0; |
| 420 |
m_prev_accum = 0; |
| 421 |
m_adpcm_step = STEP_MIN; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
//------------------------------------------------- |
| 426 |
// save_restore - save or restore the data |
| 427 |
//------------------------------------------------- |
| 428 |
|
| 429 |
void adpcm_b_channel::save_restore(ymfm_saved_state &state) |
| 430 |
{ |
| 431 |
state.save_restore(m_status); |
| 432 |
state.save_restore(m_curnibble); |
| 433 |
state.save_restore(m_curbyte); |
| 434 |
state.save_restore(m_dummy_read); |
| 435 |
state.save_restore(m_position); |
| 436 |
state.save_restore(m_curaddress); |
| 437 |
state.save_restore(m_accumulator); |
| 438 |
state.save_restore(m_prev_accum); |
| 439 |
state.save_restore(m_adpcm_step); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
//------------------------------------------------- |
| 444 |
// clock - master clocking function |
| 445 |
//------------------------------------------------- |
| 446 |
|
| 447 |
void adpcm_b_channel::clock() |
| 448 |
{ |
| 449 |
// only process if active and not recording (which we don't support) |
| 450 |
if (!m_regs.execute() || m_regs.record() || (m_status & STATUS_PLAYING) == 0) |
| 451 |
{ |
| 452 |
m_status &= ~STATUS_PLAYING; |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
// otherwise, advance the step |
| 457 |
uint32_t position = m_position + m_regs.delta_n(); |
| 458 |
m_position = uint16_t(position); |
| 459 |
if (position < 0x10000) |
| 460 |
return; |
| 461 |
|
| 462 |
// if we're about to process nibble 0, fetch sample |
| 463 |
if (m_curnibble == 0) |
| 464 |
{ |
| 465 |
// playing from RAM/ROM |
| 466 |
if (m_regs.external()) |
| 467 |
m_curbyte = m_owner.intf().ymfm_external_read(ACCESS_ADPCM_B, m_curaddress); |
| 468 |
} |
| 469 |
|
| 470 |
// extract the nibble from our current byte |
| 471 |
uint8_t data = uint8_t(m_curbyte << (4 * m_curnibble)) >> 4; |
| 472 |
m_curnibble ^= 1; |
| 473 |
|
| 474 |
// we just processed the last nibble |
| 475 |
if (m_curnibble == 0) |
| 476 |
{ |
| 477 |
// if playing from RAM/ROM, check the end/limit address or advance |
| 478 |
if (m_regs.external()) |
| 479 |
{ |
| 480 |
// handle the sample end, either repeating or stopping |
| 481 |
if (at_end()) |
| 482 |
{ |
| 483 |
// if repeating, go back to the start |
| 484 |
if (m_regs.repeat()) |
| 485 |
load_start(); |
| 486 |
|
| 487 |
// otherwise, done; set the EOS bit |
| 488 |
else |
| 489 |
{ |
| 490 |
m_accumulator = 0; |
| 491 |
m_prev_accum = 0; |
| 492 |
m_status = (m_status & ~STATUS_PLAYING) | STATUS_EOS; |
| 493 |
debug::log_keyon("%s\n", "ADPCM EOS"); |
| 494 |
return; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
// wrap at the limit address |
| 499 |
else if (at_limit()) |
| 500 |
m_curaddress = 0; |
| 501 |
|
| 502 |
// otherwise, advance the current address |
| 503 |
else |
| 504 |
{ |
| 505 |
m_curaddress++; |
| 506 |
m_curaddress &= 0xffffff; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
// if CPU-driven, copy the next byte and request more |
| 511 |
else |
| 512 |
{ |
| 513 |
m_curbyte = m_regs.cpudata(); |
| 514 |
m_status |= STATUS_BRDY; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
// remember previous value for interpolation |
| 519 |
m_prev_accum = m_accumulator; |
| 520 |
|
| 521 |
// forecast to next forecast: 1/8, 3/8, 5/8, 7/8, 9/8, 11/8, 13/8, 15/8 |
| 522 |
int32_t delta = (2 * bitfield(data, 0, 3) + 1) * m_adpcm_step / 8; |
| 523 |
if (bitfield(data, 3)) |
| 524 |
delta = -delta; |
| 525 |
|
| 526 |
// add and clamp to 16 bits |
| 527 |
m_accumulator = clamp(m_accumulator + delta, -32768, 32767); |
| 528 |
|
| 529 |
// scale the ADPCM step: 0.9, 0.9, 0.9, 0.9, 1.2, 1.6, 2.0, 2.4 |
| 530 |
static uint8_t const s_step_scale[8] = { 57, 57, 57, 57, 77, 102, 128, 153 }; |
| 531 |
m_adpcm_step = clamp((m_adpcm_step * s_step_scale[bitfield(data, 0, 3)]) / 64, STEP_MIN, STEP_MAX); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
//------------------------------------------------- |
| 536 |
// output - return the computed output value, with |
| 537 |
// panning applied |
| 538 |
//------------------------------------------------- |
| 539 |
|
| 540 |
template<int NumOutputs> |
| 541 |
void adpcm_b_channel::output(ymfm_output<NumOutputs> &output, uint32_t rshift) const |
| 542 |
{ |
| 543 |
// mask out some channels for debug purposes |
| 544 |
if ((debug::GLOBAL_ADPCM_B_CHANNEL_MASK & 1) == 0) |
| 545 |
return; |
| 546 |
|
| 547 |
// do a linear interpolation between samples |
| 548 |
int32_t result = (m_prev_accum * int32_t((m_position ^ 0xffff) + 1) + m_accumulator * int32_t(m_position)) >> 16; |
| 549 |
|
| 550 |
// apply volume (level) in a linear fashion and reduce |
| 551 |
result = (result * int32_t(m_regs.level())) >> (8 + rshift); |
| 552 |
|
| 553 |
// apply to left/right |
| 554 |
if (NumOutputs == 1 || m_regs.pan_left()) |
| 555 |
output.data[0] += result; |
| 556 |
if (NumOutputs > 1 && m_regs.pan_right()) |
| 557 |
output.data[1] += result; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
//------------------------------------------------- |
| 562 |
// read - handle special register reads |
| 563 |
//------------------------------------------------- |
| 564 |
|
| 565 |
uint8_t adpcm_b_channel::read(uint32_t regnum) |
| 566 |
{ |
| 567 |
uint8_t result = 0; |
| 568 |
|
| 569 |
// register 8 reads over the bus under some conditions |
| 570 |
if (regnum == 0x08 && !m_regs.execute() && !m_regs.record() && m_regs.external()) |
| 571 |
{ |
| 572 |
// two dummy reads are consumed first |
| 573 |
if (m_dummy_read != 0) |
| 574 |
{ |
| 575 |
load_start(); |
| 576 |
m_dummy_read--; |
| 577 |
} |
| 578 |
|
| 579 |
// read the data |
| 580 |
else |
| 581 |
{ |
| 582 |
// read from outside of the chip |
| 583 |
result = m_owner.intf().ymfm_external_read(ACCESS_ADPCM_B, m_curaddress++); |
| 584 |
|
| 585 |
// did we hit the end? if so, signal EOS |
| 586 |
if (at_end()) |
| 587 |
{ |
| 588 |
m_status = STATUS_EOS | STATUS_BRDY; |
| 589 |
debug::log_keyon("%s\n", "ADPCM EOS"); |
| 590 |
} |
| 591 |
else |
| 592 |
{ |
| 593 |
// signal ready |
| 594 |
m_status = STATUS_BRDY; |
| 595 |
} |
| 596 |
|
| 597 |
// wrap at the limit address |
| 598 |
if (at_limit()) |
| 599 |
m_curaddress = 0; |
| 600 |
} |
| 601 |
} |
| 602 |
return result; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
//------------------------------------------------- |
| 607 |
// write - handle special register writes |
| 608 |
//------------------------------------------------- |
| 609 |
|
| 610 |
void adpcm_b_channel::write(uint32_t regnum, uint8_t value) |
| 611 |
{ |
| 612 |
// register 0 can do a reset; also use writes here to reset the |
| 613 |
// dummy read counter |
| 614 |
if (regnum == 0x00) |
| 615 |
{ |
| 616 |
if (m_regs.execute()) |
| 617 |
{ |
| 618 |
load_start(); |
| 619 |
|
| 620 |
// don't log masked channels |
| 621 |
if ((debug::GLOBAL_ADPCM_B_CHANNEL_MASK & 1) != 0) |
| 622 |
debug::log_keyon("KeyOn ADPCM-B: rep=%d spk=%d pan=%d%d dac=%d 8b=%d rom=%d ext=%d rec=%d start=%04X end=%04X pre=%04X dn=%04X lvl=%02X lim=%04X\n", |
| 623 |
m_regs.repeat(), |
| 624 |
m_regs.speaker(), |
| 625 |
m_regs.pan_left(), |
| 626 |
m_regs.pan_right(), |
| 627 |
m_regs.dac_enable(), |
| 628 |
m_regs.dram_8bit(), |
| 629 |
m_regs.rom_ram(), |
| 630 |
m_regs.external(), |
| 631 |
m_regs.record(), |
| 632 |
m_regs.start(), |
| 633 |
m_regs.end(), |
| 634 |
m_regs.prescale(), |
| 635 |
m_regs.delta_n(), |
| 636 |
m_regs.level(), |
| 637 |
m_regs.limit()); |
| 638 |
} |
| 639 |
else |
| 640 |
m_status &= ~STATUS_EOS; |
| 641 |
if (m_regs.resetflag()) |
| 642 |
reset(); |
| 643 |
if (m_regs.external()) |
| 644 |
m_dummy_read = 2; |
| 645 |
} |
| 646 |
|
| 647 |
// register 8 writes over the bus under some conditions |
| 648 |
else if (regnum == 0x08) |
| 649 |
{ |
| 650 |
// if writing from the CPU during execute, clear the ready flag |
| 651 |
if (m_regs.execute() && !m_regs.record() && !m_regs.external()) |
| 652 |
m_status &= ~STATUS_BRDY; |
| 653 |
|
| 654 |
// if writing during "record", pass through as data |
| 655 |
else if (!m_regs.execute() && m_regs.record() && m_regs.external()) |
| 656 |
{ |
| 657 |
// clear out dummy reads and set start address |
| 658 |
if (m_dummy_read != 0) |
| 659 |
{ |
| 660 |
load_start(); |
| 661 |
m_dummy_read = 0; |
| 662 |
} |
| 663 |
|
| 664 |
// did we hit the end? if so, signal EOS |
| 665 |
if (at_end()) |
| 666 |
{ |
| 667 |
debug::log_keyon("%s\n", "ADPCM EOS"); |
| 668 |
m_status = STATUS_EOS | STATUS_BRDY; |
| 669 |
} |
| 670 |
|
| 671 |
// otherwise, write the data and signal ready |
| 672 |
else |
| 673 |
{ |
| 674 |
m_owner.intf().ymfm_external_write(ACCESS_ADPCM_B, m_curaddress++, value); |
| 675 |
m_status = STATUS_BRDY; |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
//------------------------------------------------- |
| 683 |
// address_shift - compute the current address |
| 684 |
// shift amount based on register settings |
| 685 |
//------------------------------------------------- |
| 686 |
|
| 687 |
uint32_t adpcm_b_channel::address_shift() const |
| 688 |
{ |
| 689 |
// if a constant address shift, just provide that |
| 690 |
if (m_address_shift != 0) |
| 691 |
return m_address_shift; |
| 692 |
|
| 693 |
// if ROM or 8-bit DRAM, shift is 5 bits |
| 694 |
if (m_regs.rom_ram()) |
| 695 |
return 5; |
| 696 |
if (m_regs.dram_8bit()) |
| 697 |
return 5; |
| 698 |
|
| 699 |
// otherwise, shift is 2 bits |
| 700 |
return 2; |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
//------------------------------------------------- |
| 705 |
// load_start - load the start address and |
| 706 |
// initialize the state |
| 707 |
//------------------------------------------------- |
| 708 |
|
| 709 |
void adpcm_b_channel::load_start() |
| 710 |
{ |
| 711 |
m_status = (m_status & ~STATUS_EOS) | STATUS_PLAYING; |
| 712 |
m_curaddress = m_regs.external() ? (m_regs.start() << address_shift()) : 0; |
| 713 |
m_curnibble = 0; |
| 714 |
m_curbyte = 0; |
| 715 |
m_position = 0; |
| 716 |
m_accumulator = 0; |
| 717 |
m_prev_accum = 0; |
| 718 |
m_adpcm_step = STEP_MIN; |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
//********************************************************* |
| 724 |
// ADPCM "B" ENGINE |
| 725 |
//********************************************************* |
| 726 |
|
| 727 |
//------------------------------------------------- |
| 728 |
// adpcm_b_engine - constructor |
| 729 |
//------------------------------------------------- |
| 730 |
|
| 731 |
adpcm_b_engine::adpcm_b_engine(ymfm_interface &intf, uint32_t addrshift) : |
| 732 |
m_intf(intf) |
| 733 |
{ |
| 734 |
// create the channel (only one supported for now, but leaving possibilities open) |
| 735 |
m_channel = std::make_unique<adpcm_b_channel>(*this, addrshift); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
//------------------------------------------------- |
| 740 |
// reset - reset the engine state |
| 741 |
//------------------------------------------------- |
| 742 |
|
| 743 |
void adpcm_b_engine::reset() |
| 744 |
{ |
| 745 |
// reset registers |
| 746 |
m_regs.reset(); |
| 747 |
|
| 748 |
// reset each channel |
| 749 |
m_channel->reset(); |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
//------------------------------------------------- |
| 754 |
// save_restore - save or restore the data |
| 755 |
//------------------------------------------------- |
| 756 |
|
| 757 |
void adpcm_b_engine::save_restore(ymfm_saved_state &state) |
| 758 |
{ |
| 759 |
// save our state |
| 760 |
m_regs.save_restore(state); |
| 761 |
|
| 762 |
// save channel state |
| 763 |
m_channel->save_restore(state); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
//------------------------------------------------- |
| 768 |
// clock - master clocking function |
| 769 |
//------------------------------------------------- |
| 770 |
|
| 771 |
void adpcm_b_engine::clock() |
| 772 |
{ |
| 773 |
// clock each channel, setting a bit in result if it finished |
| 774 |
m_channel->clock(); |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
//------------------------------------------------- |
| 779 |
// output - master output function |
| 780 |
//------------------------------------------------- |
| 781 |
|
| 782 |
template<int NumOutputs> |
| 783 |
void adpcm_b_engine::output(ymfm_output<NumOutputs> &output, uint32_t rshift) |
| 784 |
{ |
| 785 |
// compute the output of each channel |
| 786 |
m_channel->output(output, rshift); |
| 787 |
} |
| 788 |
|
| 789 |
template void adpcm_b_engine::output<1>(ymfm_output<1> &output, uint32_t rshift); |
| 790 |
template void adpcm_b_engine::output<2>(ymfm_output<2> &output, uint32_t rshift); |
| 791 |
|
| 792 |
|
| 793 |
//------------------------------------------------- |
| 794 |
// write - handle writes to the ADPCM-B registers |
| 795 |
//------------------------------------------------- |
| 796 |
|
| 797 |
void adpcm_b_engine::write(uint32_t regnum, uint8_t data) |
| 798 |
{ |
| 799 |
// store the raw value to the register array; |
| 800 |
// most writes are passive, consumed only when needed |
| 801 |
m_regs.write(regnum, data); |
| 802 |
|
| 803 |
// let the channel handle any special writes |
| 804 |
m_channel->write(regnum, data); |
| 805 |
} |
| 806 |
|
| 807 |
} |