| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "Y8950Adpcm.hh" |
| 12 |
|
| 13 |
#include "Y8950.hh" |
| 14 |
|
| 15 |
#include "Clock.hh" |
| 16 |
#include "DeviceConfig.hh" |
| 17 |
#include "MSXMotherBoard.hh" |
| 18 |
#include "serialize.hh" |
| 19 |
|
| 20 |
#include "Math.hh" |
| 21 |
#include "narrow.hh" |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <array> |
| 25 |
|
| 26 |
namespace openmsx { |
| 27 |
|
| 28 |
|
| 29 |
static constexpr int R07_RESET = 0x01; |
| 30 |
static constexpr int R07_SP_OFF = 0x08; |
| 31 |
static constexpr int R07_REPEAT = 0x10; |
| 32 |
static constexpr int R07_MEMORY_DATA = 0x20; |
| 33 |
static constexpr int R07_REC = 0x40; |
| 34 |
static constexpr int R07_START = 0x80; |
| 35 |
static constexpr int R07_MODE = 0xE0; |
| 36 |
|
| 37 |
|
| 38 |
static constexpr int R08_ROM = 0x01; |
| 39 |
static constexpr int R08_64K = 0x02; |
| 40 |
static constexpr int R08_DA_AD = 0x04; |
| 41 |
static constexpr int R08_SAMPL = 0x08; |
| 42 |
static constexpr int R08_NOTE_SET = 0x40; |
| 43 |
static constexpr int R08_CSM = 0x80; |
| 44 |
|
| 45 |
static constexpr int DIFF_MAX = 0x6000; |
| 46 |
static constexpr int DIFF_MIN = 0x7F; |
| 47 |
static constexpr int DIFF_DEFAULT = 0x7F; |
| 48 |
|
| 49 |
static constexpr int STEP_BITS = 16; |
| 50 |
static constexpr int STEP_MASK = (1 << STEP_BITS) -1; |
| 51 |
|
| 52 |
|
| 53 |
Y8950Adpcm::Y8950Adpcm(Y8950& y8950_, const DeviceConfig& config, |
| 54 |
const std::string& name, unsigned sampleRam) |
| 55 |
: Schedulable(config.getScheduler()) |
| 56 |
, y8950(y8950_) |
| 57 |
, ram(config, name + " RAM", "Y8950 sample RAM", sampleRam) |
| 58 |
, clock(config.getMotherBoard().getCurrentTime()) |
| 59 |
{ |
| 60 |
clearRam(); |
| 61 |
} |
| 62 |
|
| 63 |
void Y8950Adpcm::clearRam() |
| 64 |
{ |
| 65 |
ram.clear(0xFF); |
| 66 |
} |
| 67 |
|
| 68 |
void Y8950Adpcm::reset(EmuTime time) |
| 69 |
{ |
| 70 |
removeSyncPoint(); |
| 71 |
|
| 72 |
clock.reset(time); |
| 73 |
|
| 74 |
startAddr = 0; |
| 75 |
stopAddr = 7; |
| 76 |
delta = 0; |
| 77 |
addrMask = (1 << 18) - 1; |
| 78 |
reg7 = 0; |
| 79 |
reg15 = 0; |
| 80 |
readDelay = 0; |
| 81 |
romBank = false; |
| 82 |
writeReg(0x12, 255, time); |
| 83 |
|
| 84 |
restart(emu); |
| 85 |
restart(aud); |
| 86 |
|
| 87 |
y8950.setStatus(Y8950::STATUS_BUF_RDY); |
| 88 |
} |
| 89 |
|
| 90 |
bool Y8950Adpcm::isPlaying() const |
| 91 |
{ |
| 92 |
return (reg7 & 0xC0) == 0x80; |
| 93 |
} |
| 94 |
bool Y8950Adpcm::isMuted() const |
| 95 |
{ |
| 96 |
return !isPlaying() || (reg7 & R07_SP_OFF); |
| 97 |
} |
| 98 |
|
| 99 |
void Y8950Adpcm::restart(PlayData& pd) const |
| 100 |
{ |
| 101 |
pd.memPtr = startAddr; |
| 102 |
pd.nowStep = (1 << STEP_BITS) - delta; |
| 103 |
pd.out = 0; |
| 104 |
pd.output = 0; |
| 105 |
pd.diff = DIFF_DEFAULT; |
| 106 |
pd.nextLeveling = 0; |
| 107 |
pd.sampleStep = 0; |
| 108 |
pd.adpcm_data = 0; |
| 109 |
} |
| 110 |
|
| 111 |
void Y8950Adpcm::sync(EmuTime time) |
| 112 |
{ |
| 113 |
if (isPlaying()) { |
| 114 |
unsigned ticks = clock.getTicksTill(time); |
| 115 |
for (unsigned i = 0; isPlaying() && (i < ticks); ++i) { |
| 116 |
(void)calcSample(true); |
| 117 |
} |
| 118 |
} |
| 119 |
clock.advance(time); |
| 120 |
} |
| 121 |
|
| 122 |
void Y8950Adpcm::schedule() |
| 123 |
{ |
| 124 |
assert(isPlaying()); |
| 125 |
if ((stopAddr > startAddr) && (delta != 0)) { |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
if (reg7 & R07_MEMORY_DATA) { |
| 130 |
|
| 131 |
Clock<Y8950::CLOCK_FREQ, Y8950::CLOCK_FREQ_DIV> stop(clock); |
| 132 |
uint64_t samples = stopAddr - emu.memPtr + 1; |
| 133 |
uint64_t length = (samples << STEP_BITS) + |
| 134 |
((1 << STEP_BITS) - emu.nowStep) + |
| 135 |
(delta - 1); |
| 136 |
stop += unsigned(length / delta); |
| 137 |
setSyncPoint(stop.getTime()); |
| 138 |
} else { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
void Y8950Adpcm::executeUntil(EmuTime time) |
| 147 |
{ |
| 148 |
assert(isPlaying()); |
| 149 |
sync(time); |
| 150 |
assert(y8950.peekRawStatus() & Y8950::STATUS_EOS); |
| 151 |
if (isPlaying() && (reg7 & R07_REPEAT)) { |
| 152 |
schedule(); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
void Y8950Adpcm::writeReg(uint8_t rg, uint8_t data, EmuTime time) |
| 157 |
{ |
| 158 |
sync(time); |
| 159 |
switch (rg) { |
| 160 |
case 0x07: |
| 161 |
reg7 = data; |
| 162 |
if (reg7 & R07_START) { |
| 163 |
y8950.setStatus(Y8950::STATUS_PCM_BSY); |
| 164 |
} else { |
| 165 |
y8950.resetStatus(Y8950::STATUS_PCM_BSY); |
| 166 |
} |
| 167 |
if (reg7 & R07_RESET) { |
| 168 |
reg7 = 0; |
| 169 |
} |
| 170 |
if (reg7 & R07_START) { |
| 171 |
|
| 172 |
restart(emu); |
| 173 |
restart(aud); |
| 174 |
} |
| 175 |
if (reg7 & R07_MEMORY_DATA) { |
| 176 |
|
| 177 |
emu.memPtr = startAddr; |
| 178 |
aud.memPtr = startAddr; |
| 179 |
readDelay = 2; |
| 180 |
if ((reg7 & 0xA0) == 0x20) { |
| 181 |
|
| 182 |
y8950.setStatus(Y8950::STATUS_BUF_RDY); |
| 183 |
} |
| 184 |
} else { |
| 185 |
|
| 186 |
emu.memPtr = 0; |
| 187 |
aud.memPtr = 0; |
| 188 |
} |
| 189 |
removeSyncPoint(); |
| 190 |
if (isPlaying()) { |
| 191 |
schedule(); |
| 192 |
} |
| 193 |
break; |
| 194 |
|
| 195 |
case 0x08: |
| 196 |
romBank = data & R08_ROM; |
| 197 |
addrMask = data & R08_64K ? (1 << 16) - 1 : (1 << 18) - 1; |
| 198 |
break; |
| 199 |
|
| 200 |
case 0x09: |
| 201 |
startAddr = (startAddr & 0x7F807) | (data << 3); |
| 202 |
break; |
| 203 |
case 0x0A: |
| 204 |
startAddr = (startAddr & 0x007FF) | (data << 11); |
| 205 |
break; |
| 206 |
|
| 207 |
case 0x0B: |
| 208 |
stopAddr = (stopAddr & 0x7F807) | (data << 3); |
| 209 |
if (isPlaying()) { |
| 210 |
removeSyncPoint(); |
| 211 |
schedule(); |
| 212 |
} |
| 213 |
break; |
| 214 |
case 0x0C: |
| 215 |
stopAddr = (stopAddr & 0x007FF) | (data << 11); |
| 216 |
if (isPlaying()) { |
| 217 |
removeSyncPoint(); |
| 218 |
schedule(); |
| 219 |
} |
| 220 |
break; |
| 221 |
|
| 222 |
case 0x0F: |
| 223 |
writeData(data); |
| 224 |
break; |
| 225 |
|
| 226 |
case 0x10: |
| 227 |
delta = (delta & 0xFF00) | data; |
| 228 |
volumeWStep = (volume * delta) >> STEP_BITS; |
| 229 |
if (isPlaying()) { |
| 230 |
removeSyncPoint(); |
| 231 |
schedule(); |
| 232 |
} |
| 233 |
break; |
| 234 |
case 0x11: |
| 235 |
delta = (delta & 0x00FF) | (data << 8); |
| 236 |
volumeWStep = (volume * delta) >> STEP_BITS; |
| 237 |
if (isPlaying()) { |
| 238 |
removeSyncPoint(); |
| 239 |
schedule(); |
| 240 |
} |
| 241 |
break; |
| 242 |
|
| 243 |
case 0x12: { |
| 244 |
volume = data; |
| 245 |
volumeWStep = (volume * delta) >> STEP_BITS; |
| 246 |
break; |
| 247 |
} |
| 248 |
case 0x0D: |
| 249 |
case 0x0E: |
| 250 |
case 0x15: |
| 251 |
case 0x16: |
| 252 |
case 0x17: |
| 253 |
case 0x1A: |
| 254 |
|
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
void Y8950Adpcm::writeData(uint8_t data) |
| 260 |
{ |
| 261 |
reg15 = data; |
| 262 |
if ((reg7 & R07_MODE) == 0x60) { |
| 263 |
|
| 264 |
assert(!isPlaying()); |
| 265 |
if (readDelay) { |
| 266 |
emu.memPtr = startAddr; |
| 267 |
readDelay = 0; |
| 268 |
} |
| 269 |
if (emu.memPtr <= stopAddr) { |
| 270 |
writeMemory(emu.memPtr, data); |
| 271 |
emu.memPtr += 2; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
y8950.resetStatus(Y8950::STATUS_BUF_RDY); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
y8950.setStatus(Y8950::STATUS_BUF_RDY); |
| 285 |
|
| 286 |
if (emu.memPtr > stopAddr) { |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
y8950.setStatus(Y8950::STATUS_EOS); |
| 291 |
|
| 292 |
|
| 293 |
emu.memPtr = startAddr; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
} else if ((reg7 & R07_MODE) == 0x80) { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
y8950.resetStatus(Y8950::STATUS_BUF_RDY); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
uint8_t Y8950Adpcm::readReg(uint8_t rg, EmuTime time) |
| 307 |
{ |
| 308 |
sync(time); |
| 309 |
uint8_t result = (rg == 0x0F) |
| 310 |
? readData() |
| 311 |
: peekReg(rg); |
| 312 |
return result; |
| 313 |
} |
| 314 |
|
| 315 |
uint8_t Y8950Adpcm::peekReg(uint8_t rg, EmuTime time) const |
| 316 |
{ |
| 317 |
const_cast<Y8950Adpcm*>(this)->sync(time); |
| 318 |
return peekReg(rg); |
| 319 |
} |
| 320 |
|
| 321 |
uint8_t Y8950Adpcm::peekReg(uint8_t rg) const |
| 322 |
{ |
| 323 |
switch (rg) { |
| 324 |
case 0x0F: |
| 325 |
return peekData(); |
| 326 |
case 0x13: |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
return narrow_cast<uint8_t>((emu.output >> 8) & 0xFF); |
| 331 |
case 0x14: |
| 332 |
return narrow_cast<uint8_t>((emu.output >> 16) & 0xFF); |
| 333 |
default: |
| 334 |
return 255; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
void Y8950Adpcm::resetStatus() |
| 339 |
{ |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
if (((reg7 & R07_MODE & ~R07_REC) == R07_MEMORY_DATA) || |
| 357 |
((reg7 & R07_MODE) == 0)){ |
| 358 |
|
| 359 |
y8950.setStatus(Y8950::STATUS_BUF_RDY); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
uint8_t Y8950Adpcm::readData() |
| 364 |
{ |
| 365 |
if ((reg7 & R07_MODE) == R07_MEMORY_DATA) { |
| 366 |
|
| 367 |
assert(!isPlaying()); |
| 368 |
if (readDelay) { |
| 369 |
emu.memPtr = startAddr; |
| 370 |
} |
| 371 |
} |
| 372 |
uint8_t result = peekData(); |
| 373 |
if ((reg7 & R07_MODE) == R07_MEMORY_DATA) { |
| 374 |
assert(!isPlaying()); |
| 375 |
if (readDelay) { |
| 376 |
|
| 377 |
--readDelay; |
| 378 |
y8950.setStatus(Y8950::STATUS_BUF_RDY); |
| 379 |
} else if (emu.memPtr > stopAddr) { |
| 380 |
|
| 381 |
y8950.setStatus(Y8950::STATUS_EOS); |
| 382 |
} else { |
| 383 |
emu.memPtr += 2; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
y8950.resetStatus(Y8950::STATUS_BUF_RDY); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
y8950.setStatus(Y8950::STATUS_BUF_RDY); |
| 397 |
} |
| 398 |
} |
| 399 |
return result; |
| 400 |
} |
| 401 |
|
| 402 |
uint8_t Y8950Adpcm::peekData() const |
| 403 |
{ |
| 404 |
if ((reg7 & R07_MODE) == R07_MEMORY_DATA) { |
| 405 |
|
| 406 |
assert(!isPlaying()); |
| 407 |
if (readDelay) { |
| 408 |
return reg15; |
| 409 |
} else if (emu.memPtr > stopAddr) { |
| 410 |
return 0; |
| 411 |
} else { |
| 412 |
return readMemory(emu.memPtr); |
| 413 |
} |
| 414 |
} else { |
| 415 |
return 0; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
void Y8950Adpcm::writeMemory(unsigned memPtr, uint8_t value) |
| 420 |
{ |
| 421 |
unsigned addr = (memPtr / 2) & addrMask; |
| 422 |
if ((addr < ram.size()) && !romBank) { |
| 423 |
ram.write(addr, value); |
| 424 |
} |
| 425 |
} |
| 426 |
uint8_t Y8950Adpcm::readMemory(unsigned memPtr) const |
| 427 |
{ |
| 428 |
unsigned addr = (memPtr / 2) & addrMask; |
| 429 |
if (romBank || (addr >= ram.size())) { |
| 430 |
return 0; |
| 431 |
} else { |
| 432 |
return ram[addr]; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
int Y8950Adpcm::calcSample() |
| 437 |
{ |
| 438 |
|
| 439 |
if (!isPlaying()) return 0; |
| 440 |
int output = calcSample(false); |
| 441 |
return (reg7 & R07_SP_OFF) ? 0 : output; |
| 442 |
} |
| 443 |
|
| 444 |
int Y8950Adpcm::calcSample(bool doEmu) |
| 445 |
{ |
| 446 |
|
| 447 |
static constexpr std::array<int, 16> F1 = { |
| 448 |
1, 3, 5, 7, 9, 11, 13, 15, |
| 449 |
-1, -3, -5, -7, -9, -11, -13, -15 |
| 450 |
}; |
| 451 |
static constexpr std::array<int, 16> F2 = { |
| 452 |
57, 57, 57, 57, 77, 102, 128, 153, |
| 453 |
57, 57, 57, 57, 77, 102, 128, 153 |
| 454 |
}; |
| 455 |
|
| 456 |
assert(isPlaying()); |
| 457 |
|
| 458 |
PlayData& pd = doEmu ? emu : aud; |
| 459 |
pd.nowStep += delta; |
| 460 |
if (pd.nowStep & ~STEP_MASK) { |
| 461 |
pd.nowStep &= STEP_MASK; |
| 462 |
uint8_t val = [&] { |
| 463 |
if (!(pd.memPtr & 1)) { |
| 464 |
|
| 465 |
if (reg7 & R07_MEMORY_DATA) { |
| 466 |
pd.adpcm_data = readMemory(pd.memPtr); |
| 467 |
} else { |
| 468 |
pd.adpcm_data = reg15; |
| 469 |
|
| 470 |
if (doEmu) { |
| 471 |
y8950.setStatus(Y8950::STATUS_BUF_RDY); |
| 472 |
} |
| 473 |
} |
| 474 |
return uint8_t(pd.adpcm_data >> 4); |
| 475 |
} else { |
| 476 |
|
| 477 |
return uint8_t(pd.adpcm_data & 0x0F); |
| 478 |
} |
| 479 |
}(); |
| 480 |
int prevOut = pd.out; |
| 481 |
pd.out = Math::clipToInt16(pd.out + (pd.diff * F1[val]) / 8); |
| 482 |
pd.diff = std::clamp((pd.diff * F2[val]) / 64, DIFF_MIN, DIFF_MAX); |
| 483 |
|
| 484 |
int prevLeveling = pd.nextLeveling; |
| 485 |
pd.nextLeveling = (prevOut + pd.out) / 2; |
| 486 |
int deltaLeveling = pd.nextLeveling - prevLeveling; |
| 487 |
pd.sampleStep = deltaLeveling * volumeWStep; |
| 488 |
int tmp = deltaLeveling * ((volume * narrow<int>(pd.nowStep)) >> STEP_BITS); |
| 489 |
pd.output = prevLeveling * volume + tmp; |
| 490 |
|
| 491 |
++pd.memPtr; |
| 492 |
if ((reg7 & R07_MEMORY_DATA) && |
| 493 |
(pd.memPtr > stopAddr)) { |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
if (doEmu) { |
| 501 |
y8950.setStatus(Y8950::STATUS_EOS); |
| 502 |
} |
| 503 |
if (reg7 & R07_REPEAT) { |
| 504 |
restart(pd); |
| 505 |
} else { |
| 506 |
if (doEmu) { |
| 507 |
removeSyncPoint(); |
| 508 |
reg7 = 0; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
} else { |
| 513 |
pd.output += pd.sampleStep; |
| 514 |
} |
| 515 |
return pd.output >> 12; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
template<typename Archive> |
| 526 |
void Y8950Adpcm::serialize(Archive& ar, unsigned version) |
| 527 |
{ |
| 528 |
ar.template serializeBase<Schedulable>(*this); |
| 529 |
ar.serialize("ram", ram, |
| 530 |
"startAddr", startAddr, |
| 531 |
"stopAddr", stopAddr, |
| 532 |
"addrMask", addrMask, |
| 533 |
"volume", volume, |
| 534 |
"volumeWStep", volumeWStep, |
| 535 |
"readDelay", readDelay, |
| 536 |
"delta", delta, |
| 537 |
"reg7", reg7, |
| 538 |
"reg15", reg15, |
| 539 |
"romBank", romBank, |
| 540 |
|
| 541 |
"memPntr", emu.memPtr, |
| 542 |
"nowStep", emu.nowStep, |
| 543 |
"out", emu.out, |
| 544 |
"output", emu.output, |
| 545 |
"diff", emu.diff, |
| 546 |
"nextLeveling", emu.nextLeveling, |
| 547 |
"sampleStep", emu.sampleStep, |
| 548 |
"adpcm_data", emu.adpcm_data); |
| 549 |
if constexpr (Archive::IS_LOADER) { |
| 550 |
|
| 551 |
|
| 552 |
aud = emu; |
| 553 |
} |
| 554 |
|
| 555 |
if (ar.versionBelow(version, 2)) { |
| 556 |
clock.reset(getCurrentTime()); |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
removeSyncPoint(); |
| 561 |
if (isPlaying()) { |
| 562 |
schedule(); |
| 563 |
} |
| 564 |
} else { |
| 565 |
ar.serialize("clock", clock); |
| 566 |
} |
| 567 |
} |
| 568 |
INSTANTIATE_SERIALIZE_METHODS(Y8950Adpcm); |
| 569 |
|
| 570 |
} |