| 1 |
#include "MegaFlashRomSCCPlusSD.hh" |
| 2 |
|
| 3 |
#include "CheckedRam.hh" |
| 4 |
#include "SdCard.hh" |
| 5 |
|
| 6 |
#include "CacheLine.hh" |
| 7 |
#include "DummyAY8910Periphery.hh" |
| 8 |
#include "MSXCPUInterface.hh" |
| 9 |
#include "serialize.hh" |
| 10 |
|
| 11 |
#include "enumerate.hh" |
| 12 |
#include "narrow.hh" |
| 13 |
#include "ranges.hh" |
| 14 |
#include "xrange.hh" |
| 15 |
|
| 16 |
#include <array> |
| 17 |
#include <memory> |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
static constexpr unsigned MEMORY_MAPPER_SIZE = 512; |
| 268 |
static constexpr uint8_t MEMORY_MAPPER_MASK = (MEMORY_MAPPER_SIZE / 16) - 1; |
| 269 |
|
| 270 |
namespace openmsx { |
| 271 |
|
| 272 |
MegaFlashRomSCCPlusSD::MegaFlashRomSCCPlusSD(DeviceConfig& config) |
| 273 |
: MSXDevice(config) |
| 274 |
, flash("MFR SCC+ SD flash", AmdFlashChip::M29W640GB, {}, config) |
| 275 |
, scc("MFR SCC+ SD SCC-I", config, getCurrentTime(), SCC::Mode::Compatible) |
| 276 |
, psg("MFR SCC+ SD PSG", DummyAY8910Periphery::instance(), config, |
| 277 |
getCurrentTime()) |
| 278 |
, checkedRam(config.getChildDataAsBool("hasmemorymapper", true) ? |
| 279 |
std::make_unique<CheckedRam>(config, getName() + " memory mapper", "memory mapper", MEMORY_MAPPER_SIZE * 1024) |
| 280 |
: nullptr) |
| 281 |
, mapperIO(checkedRam ? std::make_unique<MapperIO>(*this) : nullptr) |
| 282 |
{ |
| 283 |
|
| 284 |
|
| 285 |
psg.setSoftwareVolume(21000.0f/9000.0f, getCurrentTime()); |
| 286 |
|
| 287 |
powerUp(getCurrentTime()); |
| 288 |
getCPUInterface().register_IO_Out_range(0x10, 2, this); |
| 289 |
|
| 290 |
sdCard[0] = std::make_unique<SdCard>(DeviceConfig(config, config.findChild("sdcard1"))); |
| 291 |
sdCard[1] = std::make_unique<SdCard>(DeviceConfig(config, config.findChild("sdcard2"))); |
| 292 |
} |
| 293 |
|
| 294 |
MegaFlashRomSCCPlusSD::~MegaFlashRomSCCPlusSD() |
| 295 |
{ |
| 296 |
|
| 297 |
updateConfigReg(3); |
| 298 |
getCPUInterface().unregister_IO_Out_range(0x10, 2, this); |
| 299 |
} |
| 300 |
|
| 301 |
void MegaFlashRomSCCPlusSD::powerUp(EmuTime time) |
| 302 |
{ |
| 303 |
scc.powerUp(time); |
| 304 |
reset(time); |
| 305 |
} |
| 306 |
|
| 307 |
void MegaFlashRomSCCPlusSD::reset(EmuTime time) |
| 308 |
{ |
| 309 |
mapperReg = 0; |
| 310 |
offsetReg = 0; |
| 311 |
updateConfigReg(3); |
| 312 |
subslotReg = 0; |
| 313 |
ranges::iota(bankRegsSubSlot1, uint16_t(0)); |
| 314 |
|
| 315 |
sccMode = 0; |
| 316 |
ranges::iota(sccBanks, byte(0)); |
| 317 |
scc.reset(time); |
| 318 |
|
| 319 |
psgLatch = 0; |
| 320 |
psg.reset(time); |
| 321 |
|
| 322 |
flash.reset(); |
| 323 |
|
| 324 |
|
| 325 |
for (auto [i, mr] : enumerate(memMapperRegs)) { |
| 326 |
mr = byte(3 - i); |
| 327 |
} |
| 328 |
|
| 329 |
for (auto [bank, reg] : enumerate(bankRegsSubSlot3)) { |
| 330 |
reg = (bank == 1) ? 1 : 0; |
| 331 |
} |
| 332 |
|
| 333 |
selectedCard = 0; |
| 334 |
|
| 335 |
invalidateDeviceRWCache(); |
| 336 |
} |
| 337 |
|
| 338 |
byte MegaFlashRomSCCPlusSD::getSubSlot(unsigned addr) const |
| 339 |
{ |
| 340 |
return isSlotExpanderEnabled() ? |
| 341 |
(subslotReg >> (2 * (addr >> 14))) & 3 : 1; |
| 342 |
} |
| 343 |
|
| 344 |
void MegaFlashRomSCCPlusSD::writeToFlash(unsigned addr, byte value, EmuTime time) |
| 345 |
{ |
| 346 |
if (isFlashRomWriteEnabled()) { |
| 347 |
flash.write(addr, value, time); |
| 348 |
} else { |
| 349 |
|
| 350 |
|
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
byte MegaFlashRomSCCPlusSD::peekMem(uint16_t addr, EmuTime time) const |
| 355 |
{ |
| 356 |
if (isSlotExpanderEnabled() && (addr == 0xFFFF)) { |
| 357 |
|
| 358 |
return subslotReg ^ 0xFF; |
| 359 |
} |
| 360 |
|
| 361 |
switch (getSubSlot(addr)) { |
| 362 |
case 0: return peekMemSubSlot0(addr, time); |
| 363 |
case 1: return peekMemSubSlot1(addr, time); |
| 364 |
case 2: return isMemoryMapperEnabled() ? |
| 365 |
peekMemSubSlot2(addr) : 0xFF; |
| 366 |
case 3: return peekMemSubSlot3(addr, time); |
| 367 |
default: UNREACHABLE; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
byte MegaFlashRomSCCPlusSD::readMem(uint16_t addr, EmuTime time) |
| 372 |
{ |
| 373 |
if (isSlotExpanderEnabled() && (addr == 0xFFFF)) { |
| 374 |
|
| 375 |
return subslotReg ^ 0xFF; |
| 376 |
} |
| 377 |
|
| 378 |
switch (getSubSlot(addr)) { |
| 379 |
case 0: return readMemSubSlot0(addr, time); |
| 380 |
case 1: return readMemSubSlot1(addr, time); |
| 381 |
case 2: return isMemoryMapperEnabled() ? |
| 382 |
readMemSubSlot2(addr) : 0xFF; |
| 383 |
case 3: return readMemSubSlot3(addr, time); |
| 384 |
default: UNREACHABLE; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
const byte* MegaFlashRomSCCPlusSD::getReadCacheLine(uint16_t addr) const |
| 389 |
{ |
| 390 |
if (isSlotExpanderEnabled() && |
| 391 |
((addr & CacheLine::HIGH) == (0xFFFF & CacheLine::HIGH))) { |
| 392 |
|
| 393 |
return nullptr; |
| 394 |
} |
| 395 |
|
| 396 |
switch (getSubSlot(addr)) { |
| 397 |
case 0: return getReadCacheLineSubSlot0(addr); |
| 398 |
case 1: return getReadCacheLineSubSlot1(addr); |
| 399 |
case 2: return isMemoryMapperEnabled() ? |
| 400 |
getReadCacheLineSubSlot2(addr) : unmappedRead.data(); |
| 401 |
case 3: return getReadCacheLineSubSlot3(addr); |
| 402 |
default: UNREACHABLE; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
void MegaFlashRomSCCPlusSD::writeMem(uint16_t addr, byte value, EmuTime time) |
| 407 |
{ |
| 408 |
if (isSlotExpanderEnabled() && (addr == 0xFFFF)) { |
| 409 |
|
| 410 |
byte diff = value ^ subslotReg; |
| 411 |
subslotReg = value; |
| 412 |
for (auto i : xrange(4)) { |
| 413 |
if (diff & (3 << (2 * i))) { |
| 414 |
invalidateDeviceRWCache(0x4000 * i, 0x4000); |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
switch (getSubSlot(addr)) { |
| 420 |
case 0: writeMemSubSlot0(addr, value, time); break; |
| 421 |
case 1: writeMemSubSlot1(addr, value, time); break; |
| 422 |
case 2: if (isMemoryMapperEnabled()) { |
| 423 |
writeMemSubSlot2(addr, value); |
| 424 |
} |
| 425 |
break; |
| 426 |
case 3: writeMemSubSlot3(addr, value, time); break; |
| 427 |
default: UNREACHABLE; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
byte* MegaFlashRomSCCPlusSD::getWriteCacheLine(uint16_t addr) |
| 432 |
{ |
| 433 |
if (isSlotExpanderEnabled() && |
| 434 |
((addr & CacheLine::HIGH) == (0xFFFF & CacheLine::HIGH))) { |
| 435 |
|
| 436 |
return nullptr; |
| 437 |
} |
| 438 |
|
| 439 |
switch (getSubSlot(addr)) { |
| 440 |
case 0: return getWriteCacheLineSubSlot0(addr); |
| 441 |
case 1: return getWriteCacheLineSubSlot1(addr); |
| 442 |
case 2: return isMemoryMapperEnabled() ? |
| 443 |
getWriteCacheLineSubSlot2(addr) : unmappedWrite.data(); |
| 444 |
case 3: return getWriteCacheLineSubSlot3(addr); |
| 445 |
default: UNREACHABLE; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
byte MegaFlashRomSCCPlusSD::readMemSubSlot0(uint16_t addr, EmuTime time) |
| 452 |
{ |
| 453 |
|
| 454 |
|
| 455 |
return flash.read(addr & 0x3FFF, time); |
| 456 |
} |
| 457 |
|
| 458 |
byte MegaFlashRomSCCPlusSD::peekMemSubSlot0(uint16_t addr, EmuTime time) const |
| 459 |
{ |
| 460 |
|
| 461 |
|
| 462 |
return flash.peek(addr & 0x3FFF, time); |
| 463 |
} |
| 464 |
|
| 465 |
const byte* MegaFlashRomSCCPlusSD::getReadCacheLineSubSlot0(uint16_t addr) const |
| 466 |
{ |
| 467 |
return flash.getReadCacheLine(addr & 0x3FFF); |
| 468 |
} |
| 469 |
|
| 470 |
void MegaFlashRomSCCPlusSD::writeMemSubSlot0(uint16_t addr, byte value, EmuTime time) |
| 471 |
{ |
| 472 |
|
| 473 |
writeToFlash(addr & 0x3FFF, value, time); |
| 474 |
} |
| 475 |
|
| 476 |
byte* MegaFlashRomSCCPlusSD::getWriteCacheLineSubSlot0(uint16_t ) |
| 477 |
{ |
| 478 |
return nullptr; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
void MegaFlashRomSCCPlusSD::updateConfigReg(byte value) |
| 484 |
{ |
| 485 |
if ((value ^ configReg) & 0x08) { |
| 486 |
if (value & 0x08) { |
| 487 |
for (auto port : {0xa0, 0xa1}) { |
| 488 |
getCPUInterface().register_IO_Out(narrow_cast<byte>(port), this); |
| 489 |
} |
| 490 |
} else { |
| 491 |
for (auto port : {0xa0, 0xa1}) { |
| 492 |
getCPUInterface().unregister_IO_Out(narrow_cast<byte>(port), this); |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
configReg = value; |
| 497 |
flash.setVppWpPinLow(isFlashRomBlockProtectEnabled()); |
| 498 |
invalidateDeviceRWCache(); |
| 499 |
} |
| 500 |
|
| 501 |
MegaFlashRomSCCPlusSD::SCCEnable MegaFlashRomSCCPlusSD::getSCCEnable() const |
| 502 |
{ |
| 503 |
if ((sccMode & 0x20) && (sccBanks[3] & 0x80)) { |
| 504 |
return EN_SCCPLUS; |
| 505 |
} else if ((!(sccMode & 0x20)) && ((sccBanks[2] & 0x3F) == 0x3F)) { |
| 506 |
return EN_SCC; |
| 507 |
} else { |
| 508 |
return EN_NONE; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
unsigned MegaFlashRomSCCPlusSD::getFlashAddrSubSlot1(unsigned addr) const |
| 513 |
{ |
| 514 |
unsigned page = is64KmapperConfigured() ? (addr >> 14) : ((addr >> 13) - 2); |
| 515 |
unsigned size = is64KmapperConfigured() ? 0x4000 : 0x2000; |
| 516 |
|
| 517 |
if (page >= 4) return unsigned(-1); |
| 518 |
|
| 519 |
unsigned bank = bankRegsSubSlot1[page]; |
| 520 |
if (isDSKmodeEnabled() && (page == 0) && (bank == 0)) { |
| 521 |
bank = 0x3FA; |
| 522 |
} else if (isDSKmodeEnabled() && (page == 1) && (bank == 1)) { |
| 523 |
bank = 0x3FB; |
| 524 |
} else { |
| 525 |
bank += offsetReg; |
| 526 |
} |
| 527 |
|
| 528 |
unsigned tmp = (bank * size) + (addr & (size - 1)); |
| 529 |
return (tmp + 0x010000) & 0x7FFFFF; |
| 530 |
} |
| 531 |
|
| 532 |
byte MegaFlashRomSCCPlusSD::readMemSubSlot1(uint16_t addr, EmuTime time) |
| 533 |
{ |
| 534 |
if (isKonamiSCCmapperConfigured()) { |
| 535 |
SCCEnable enable = getSCCEnable(); |
| 536 |
if (((enable == EN_SCC) && (0x9800 <= addr) && (addr < 0xA000)) || |
| 537 |
((enable == EN_SCCPLUS) && (0xB800 <= addr) && (addr < 0xC000))) { |
| 538 |
byte val = scc.readMem(narrow_cast<uint8_t>(addr & 0xFF), time); |
| 539 |
return val; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
unsigned flashAddr = getFlashAddrSubSlot1(addr); |
| 544 |
return (flashAddr != unsigned(-1)) |
| 545 |
? flash.read(flashAddr, time) |
| 546 |
: 0xFF; |
| 547 |
} |
| 548 |
|
| 549 |
byte MegaFlashRomSCCPlusSD::peekMemSubSlot1(uint16_t addr, EmuTime time) const |
| 550 |
{ |
| 551 |
if (isKonamiSCCmapperConfigured()) { |
| 552 |
SCCEnable enable = getSCCEnable(); |
| 553 |
if (((enable == EN_SCC) && (0x9800 <= addr) && (addr < 0xA000)) || |
| 554 |
((enable == EN_SCCPLUS) && (0xB800 <= addr) && (addr < 0xC000))) { |
| 555 |
byte val = scc.peekMem(narrow_cast<uint8_t>(addr & 0xFF), time); |
| 556 |
return val; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
unsigned flashAddr = getFlashAddrSubSlot1(addr); |
| 561 |
return (flashAddr != unsigned(-1)) |
| 562 |
? flash.peek(flashAddr, time) |
| 563 |
: 0xFF; |
| 564 |
} |
| 565 |
|
| 566 |
const byte* MegaFlashRomSCCPlusSD::getReadCacheLineSubSlot1(uint16_t addr) const |
| 567 |
{ |
| 568 |
if (isKonamiSCCmapperConfigured()) { |
| 569 |
SCCEnable enable = getSCCEnable(); |
| 570 |
if (((enable == EN_SCC) && (0x9800 <= addr) && (addr < 0xA000)) || |
| 571 |
((enable == EN_SCCPLUS) && (0xB800 <= addr) && (addr < 0xC000))) { |
| 572 |
return nullptr; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
unsigned flashAddr = getFlashAddrSubSlot1(addr); |
| 577 |
return (flashAddr != unsigned(-1)) |
| 578 |
? flash.getReadCacheLine(flashAddr) |
| 579 |
: unmappedRead.data(); |
| 580 |
} |
| 581 |
|
| 582 |
void MegaFlashRomSCCPlusSD::writeMemSubSlot1(uint16_t addr, byte value, EmuTime time) |
| 583 |
{ |
| 584 |
|
| 585 |
unsigned flashAddr = getFlashAddrSubSlot1(addr); |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
if (!isConfigRegDisabled() && (addr == 0x7FFC)) { |
| 595 |
|
| 596 |
updateConfigReg(value); |
| 597 |
} |
| 598 |
|
| 599 |
if (!isMapperRegisterDisabled() && (addr == 0x7FFF)) { |
| 600 |
|
| 601 |
mapperReg = value; |
| 602 |
invalidateDeviceRWCache(); |
| 603 |
} |
| 604 |
|
| 605 |
if (!areBankRegsAndOffsetRegsDisabled() && (addr == 0x7FFD)) { |
| 606 |
|
| 607 |
offsetReg = (offsetReg & 0x300) | value; |
| 608 |
invalidateDeviceRWCache(); |
| 609 |
} |
| 610 |
|
| 611 |
if (!areBankRegsAndOffsetRegsDisabled() && (addr == 0x7FFE)) { |
| 612 |
|
| 613 |
offsetReg = (offsetReg & 0xFF) + ((value & 0x3) << 8); |
| 614 |
invalidateDeviceRWCache(); |
| 615 |
} |
| 616 |
|
| 617 |
if (isKonamiSCCmapperConfigured()) { |
| 618 |
|
| 619 |
if ((addr & 0xFFFE) == 0xBFFE) { |
| 620 |
sccMode = value; |
| 621 |
scc.setMode((value & 0x20) ? SCC::Mode::Plus |
| 622 |
: SCC::Mode::Compatible); |
| 623 |
invalidateDeviceRWCache(0x9800, 0x800); |
| 624 |
invalidateDeviceRWCache(0xB800, 0x800); |
| 625 |
} |
| 626 |
SCCEnable enable = getSCCEnable(); |
| 627 |
bool isRamSegment2 = ((sccMode & 0x24) == 0x24) || |
| 628 |
((sccMode & 0x10) == 0x10); |
| 629 |
bool isRamSegment3 = ((sccMode & 0x10) == 0x10); |
| 630 |
if (((enable == EN_SCC) && !isRamSegment2 && |
| 631 |
(0x9800 <= addr) && (addr < 0xA000)) || |
| 632 |
((enable == EN_SCCPLUS) && !isRamSegment3 && |
| 633 |
(0xB800 <= addr) && (addr < 0xC000))) { |
| 634 |
scc.writeMem(narrow_cast<uint8_t>(addr & 0xFF), value, time); |
| 635 |
return; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
unsigned page8kB = (addr >> 13) - 2; |
| 640 |
if (!areBankRegsAndOffsetRegsDisabled() && (page8kB < 4)) { |
| 641 |
|
| 642 |
switch (mapperReg & 0xE0) { |
| 643 |
case 0x00: |
| 644 |
|
| 645 |
if ((addr & 0x1800) == 0x1000) { |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
sccBanks[page8kB] = value; |
| 650 |
|
| 651 |
|
| 652 |
byte mask = areKonamiMapperLimitsEnabled() ? 0x3F : 0xFF; |
| 653 |
bankRegsSubSlot1[page8kB] = value & mask; |
| 654 |
invalidateDeviceRWCache(0x4000 + 0x2000 * page8kB, 0x2000); |
| 655 |
} |
| 656 |
break; |
| 657 |
case 0x20: { |
| 658 |
|
| 659 |
if (isWritingKonamiBankRegisterDisabled() && (addr < 0x6000)) { |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
break; |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
if ((addr < 0x5000) || ((0x5800 <= addr) && (addr < 0x6000))) break; |
| 669 |
byte mask = areKonamiMapperLimitsEnabled() ? 0x1F : 0xFF; |
| 670 |
bankRegsSubSlot1[page8kB] = value & mask; |
| 671 |
invalidateDeviceRWCache(0x4000 + 0x2000 * page8kB, 0x2000); |
| 672 |
break; |
| 673 |
} |
| 674 |
case 0x40: |
| 675 |
case 0x60: |
| 676 |
|
| 677 |
bankRegsSubSlot1[page8kB] = value; |
| 678 |
invalidateDeviceRWCache(0x0000 + 0x4000 * page8kB, 0x4000); |
| 679 |
break; |
| 680 |
case 0x80: |
| 681 |
case 0xA0: |
| 682 |
|
| 683 |
if ((0x6000 <= addr) && (addr < 0x8000)) { |
| 684 |
byte bank = (addr >> 11) & 0x03; |
| 685 |
bankRegsSubSlot1[bank] = value; |
| 686 |
invalidateDeviceRWCache(0x4000 + 0x2000 * bank, 0x2000); |
| 687 |
} |
| 688 |
break; |
| 689 |
case 0xC0: |
| 690 |
case 0xE0: |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
const uint16_t mask = (1 << 9) - 1; |
| 706 |
if ((0x6000 <= addr) && (addr < 0x6800)) { |
| 707 |
bankRegsSubSlot1[0] = (2 * value + 0) & mask; |
| 708 |
bankRegsSubSlot1[1] = (2 * value + 1) & mask; |
| 709 |
invalidateDeviceRWCache(0x4000, 0x4000); |
| 710 |
} |
| 711 |
if ((0x7000 <= addr) && (addr < 0x7800)) { |
| 712 |
bankRegsSubSlot1[2] = (2 * value + 0) & mask; |
| 713 |
bankRegsSubSlot1[3] = (2 * value + 1) & mask; |
| 714 |
invalidateDeviceRWCache(0x8000, 0x4000); |
| 715 |
} |
| 716 |
break; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
if (flashAddr != unsigned(-1)) { |
| 721 |
writeToFlash(flashAddr, value, time); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
byte* MegaFlashRomSCCPlusSD::getWriteCacheLineSubSlot1(uint16_t ) |
| 726 |
{ |
| 727 |
return nullptr; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
unsigned MegaFlashRomSCCPlusSD::calcMemMapperAddress(uint16_t address) const |
| 733 |
{ |
| 734 |
auto bank = memMapperRegs[address >> 14]; |
| 735 |
return ((bank & MEMORY_MAPPER_MASK) << 14) | (address & 0x3FFF); |
| 736 |
} |
| 737 |
|
| 738 |
byte MegaFlashRomSCCPlusSD::readMemSubSlot2(uint16_t addr) |
| 739 |
{ |
| 740 |
|
| 741 |
return checkedRam->read(calcMemMapperAddress(addr)); |
| 742 |
} |
| 743 |
|
| 744 |
byte MegaFlashRomSCCPlusSD::peekMemSubSlot2(uint16_t addr) const |
| 745 |
{ |
| 746 |
return checkedRam->peek(calcMemMapperAddress(addr)); |
| 747 |
} |
| 748 |
|
| 749 |
const byte* MegaFlashRomSCCPlusSD::getReadCacheLineSubSlot2(uint16_t addr) const |
| 750 |
{ |
| 751 |
return checkedRam->getReadCacheLine(calcMemMapperAddress(addr)); |
| 752 |
} |
| 753 |
|
| 754 |
void MegaFlashRomSCCPlusSD::writeMemSubSlot2(uint16_t addr, byte value) |
| 755 |
{ |
| 756 |
|
| 757 |
checkedRam->write(calcMemMapperAddress(addr), value); |
| 758 |
} |
| 759 |
|
| 760 |
byte* MegaFlashRomSCCPlusSD::getWriteCacheLineSubSlot2(uint16_t addr) |
| 761 |
{ |
| 762 |
return checkedRam->getWriteCacheLine(calcMemMapperAddress(addr)); |
| 763 |
} |
| 764 |
|
| 765 |
byte MegaFlashRomSCCPlusSD::MapperIO::readIO(uint16_t port, EmuTime time) |
| 766 |
{ |
| 767 |
return peekIO(port, time); |
| 768 |
} |
| 769 |
|
| 770 |
byte MegaFlashRomSCCPlusSD::MapperIO::peekIO(uint16_t port, EmuTime ) const |
| 771 |
{ |
| 772 |
return getSelectedSegment(port & 3) | byte(~MEMORY_MAPPER_MASK); |
| 773 |
} |
| 774 |
|
| 775 |
void MegaFlashRomSCCPlusSD::MapperIO::writeIO(uint16_t port, byte value, EmuTime ) |
| 776 |
{ |
| 777 |
mega.memMapperRegs[port & 3] = value & MEMORY_MAPPER_MASK; |
| 778 |
mega.invalidateDeviceRWCache(0x4000 * (port & 0x03), 0x4000); |
| 779 |
} |
| 780 |
|
| 781 |
byte MegaFlashRomSCCPlusSD::MapperIO::getSelectedSegment(byte page) const |
| 782 |
{ |
| 783 |
return mega.memMapperRegs[page]; |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
unsigned MegaFlashRomSCCPlusSD::getFlashAddrSubSlot3(unsigned addr) const |
| 789 |
{ |
| 790 |
unsigned page8kB = (addr >> 13) - 2; |
| 791 |
return (bankRegsSubSlot3[page8kB] & 0x7f) * 0x2000 + (addr & 0x1fff) + 0x700000; |
| 792 |
} |
| 793 |
|
| 794 |
byte MegaFlashRomSCCPlusSD::readMemSubSlot3(uint16_t addr, EmuTime time) |
| 795 |
{ |
| 796 |
if (((bankRegsSubSlot3[0] & 0xC0) == 0x40) && ((0x4000 <= addr) && (addr < 0x6000))) { |
| 797 |
|
| 798 |
return sdCard[selectedCard]->transfer(0xFF, (addr & 0x1000) != 0); |
| 799 |
} |
| 800 |
|
| 801 |
if ((0x4000 <= addr) && (addr < 0xC000)) { |
| 802 |
|
| 803 |
unsigned flashAddr = getFlashAddrSubSlot3(addr); |
| 804 |
return flash.read(flashAddr, time); |
| 805 |
} else { |
| 806 |
|
| 807 |
return 0xFF; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
byte MegaFlashRomSCCPlusSD::peekMemSubSlot3(uint16_t addr, EmuTime time) const |
| 812 |
{ |
| 813 |
if ((0x4000 <= addr) && (addr < 0xC000)) { |
| 814 |
|
| 815 |
unsigned flashAddr = getFlashAddrSubSlot3(addr); |
| 816 |
return flash.peek(flashAddr, time); |
| 817 |
} else { |
| 818 |
|
| 819 |
return 0xFF; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
const byte* MegaFlashRomSCCPlusSD::getReadCacheLineSubSlot3(uint16_t addr) const |
| 824 |
{ |
| 825 |
if (((bankRegsSubSlot3[0] & 0xC0) == 0x40) && ((0x4000 <= addr) && (addr < 0x6000))) { |
| 826 |
return nullptr; |
| 827 |
} |
| 828 |
|
| 829 |
if ((0x4000 <= addr) && (addr < 0xC000)) { |
| 830 |
|
| 831 |
unsigned flashAddr = getFlashAddrSubSlot3(addr); |
| 832 |
return flash.getReadCacheLine(flashAddr); |
| 833 |
} else { |
| 834 |
return unmappedRead.data(); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
void MegaFlashRomSCCPlusSD::writeMemSubSlot3(uint16_t addr, byte value, EmuTime time) |
| 839 |
{ |
| 840 |
|
| 841 |
if (((bankRegsSubSlot3[0] & 0xC0) == 0x40) && ((0x4000 <= addr) && (addr < 0x6000))) { |
| 842 |
if (addr >= 0x5800) { |
| 843 |
selectedCard = value & 1; |
| 844 |
} else { |
| 845 |
|
| 846 |
sdCard[selectedCard]->transfer(value, (addr & 0x1000) != 0); |
| 847 |
} |
| 848 |
} else { |
| 849 |
|
| 850 |
if ((0x4000 <= addr) && (addr < 0xC000)) { |
| 851 |
unsigned flashAddr = getFlashAddrSubSlot3(addr); |
| 852 |
writeToFlash(flashAddr, value, time); |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
if ((0x6000 <= addr) && (addr < 0x8000)) { |
| 857 |
byte page8kB = (addr >> 11) & 0x03; |
| 858 |
bankRegsSubSlot3[page8kB] = value; |
| 859 |
invalidateDeviceRWCache(0x4000 + 0x2000 * page8kB, 0x2000); |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
byte* MegaFlashRomSCCPlusSD::getWriteCacheLineSubSlot3(uint16_t ) |
| 865 |
{ |
| 866 |
return nullptr; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
void MegaFlashRomSCCPlusSD::writeIO(uint16_t port, byte value, EmuTime time) |
| 872 |
{ |
| 873 |
switch (port & 0xFF) { |
| 874 |
case 0xA0: |
| 875 |
if (!isPSGalsoMappedToNormalPorts()) return; |
| 876 |
[[fallthrough]]; |
| 877 |
case 0x10: |
| 878 |
psgLatch = value & 0x0F; |
| 879 |
break; |
| 880 |
|
| 881 |
case 0xA1: |
| 882 |
if (!isPSGalsoMappedToNormalPorts()) return; |
| 883 |
[[fallthrough]]; |
| 884 |
case 0x11: |
| 885 |
psg.writeRegister(psgLatch, value, time); |
| 886 |
break; |
| 887 |
|
| 888 |
default: |
| 889 |
UNREACHABLE; |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
template<typename Archive> |
| 894 |
void MegaFlashRomSCCPlusSD::serialize(Archive& ar, unsigned ) |
| 895 |
{ |
| 896 |
|
| 897 |
ar.template serializeBase<MSXDevice>(*this); |
| 898 |
|
| 899 |
|
| 900 |
ar.serialize("flash", flash, |
| 901 |
"subslotReg", subslotReg); |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
ar.serialize("scc", scc, |
| 908 |
"sccMode", sccMode, |
| 909 |
"sccBanks", sccBanks, |
| 910 |
"psg", psg, |
| 911 |
"psgLatch", psgLatch, |
| 912 |
"configReg", configReg, |
| 913 |
"mapperReg", mapperReg, |
| 914 |
"offsetReg", offsetReg, |
| 915 |
"bankRegsSubSlot1", bankRegsSubSlot1); |
| 916 |
if constexpr (Archive::IS_LOADER) { |
| 917 |
|
| 918 |
byte tmp = configReg; |
| 919 |
configReg = 3; |
| 920 |
updateConfigReg(tmp); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
if (checkedRam) ar.serialize("ram", checkedRam->getUncheckedRam()); |
| 926 |
ar.serialize("memMapperRegs", memMapperRegs); |
| 927 |
|
| 928 |
|
| 929 |
ar.serialize("bankRegsSubSlot3", bankRegsSubSlot3, |
| 930 |
"selectedCard", selectedCard, |
| 931 |
"sdCard0", *sdCard[0], |
| 932 |
"sdCard1", *sdCard[1]); |
| 933 |
} |
| 934 |
INSTANTIATE_SERIALIZE_METHODS(MegaFlashRomSCCPlusSD); |
| 935 |
REGISTER_MSXDEVICE(MegaFlashRomSCCPlusSD, "MegaFlashRomSCCPlusSD"); |
| 936 |
|
| 937 |
} |