| 1 |
#include "MegaFlashRomSCCPlus.hh" |
| 2 |
|
| 3 |
#include "CacheLine.hh" |
| 4 |
#include "DummyAY8910Periphery.hh" |
| 5 |
#include "MSXCPUInterface.hh" |
| 6 |
#include "serialize.hh" |
| 7 |
|
| 8 |
#include "narrow.hh" |
| 9 |
#include "ranges.hh" |
| 10 |
#include "xrange.hh" |
| 11 |
|
| 12 |
#include <array> |
| 13 |
#include <cassert> |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
namespace openmsx { |
| 195 |
|
| 196 |
MegaFlashRomSCCPlus::MegaFlashRomSCCPlus( |
| 197 |
DeviceConfig& config, Rom&& rom_) |
| 198 |
: MSXRom(config, std::move(rom_)) |
| 199 |
, scc("MFR SCC+ SCC-I", config, getCurrentTime(), SCC::Mode::Compatible) |
| 200 |
, psg("MFR SCC+ PSG", DummyAY8910Periphery::instance(), config, |
| 201 |
getCurrentTime()) |
| 202 |
, flash(rom, AmdFlashChip::M29W800DB, {}, config) |
| 203 |
{ |
| 204 |
|
| 205 |
|
| 206 |
psg.setSoftwareVolume(21000.0f/9000.0f, getCurrentTime()); |
| 207 |
|
| 208 |
powerUp(getCurrentTime()); |
| 209 |
getCPUInterface().register_IO_Out_range(0x10, 2, this); |
| 210 |
} |
| 211 |
|
| 212 |
MegaFlashRomSCCPlus::~MegaFlashRomSCCPlus() |
| 213 |
{ |
| 214 |
getCPUInterface().unregister_IO_Out_range(0x10, 2, this); |
| 215 |
} |
| 216 |
|
| 217 |
void MegaFlashRomSCCPlus::powerUp(EmuTime time) |
| 218 |
{ |
| 219 |
scc.powerUp(time); |
| 220 |
reset(time); |
| 221 |
} |
| 222 |
|
| 223 |
void MegaFlashRomSCCPlus::reset(EmuTime time) |
| 224 |
{ |
| 225 |
configReg = 0; |
| 226 |
offsetReg = 0; |
| 227 |
subslotReg = 0; |
| 228 |
for (auto& regs : bankRegs) { |
| 229 |
ranges::iota(regs, byte(0)); |
| 230 |
} |
| 231 |
|
| 232 |
sccModeReg = 0; |
| 233 |
ranges::iota(sccBanks, byte(0)); |
| 234 |
scc.reset(time); |
| 235 |
|
| 236 |
psgLatch = 0; |
| 237 |
psg.reset(time); |
| 238 |
|
| 239 |
flash.reset(); |
| 240 |
|
| 241 |
invalidateDeviceRCache(); |
| 242 |
} |
| 243 |
|
| 244 |
MegaFlashRomSCCPlus::SCCMode MegaFlashRomSCCPlus::getSCCMode() const |
| 245 |
{ |
| 246 |
using enum SCCMode; |
| 247 |
if ((sccModeReg & 0x20) && (sccBanks[3] & 0x80)) { |
| 248 |
return SCCPLUS; |
| 249 |
} else if ((!(sccModeReg & 0x20)) && ((sccBanks[2] & 0x3F) == 0x3F)) { |
| 250 |
return SCC; |
| 251 |
} else { |
| 252 |
return NONE; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
unsigned MegaFlashRomSCCPlus::getSubslot(unsigned addr) const |
| 257 |
{ |
| 258 |
return (configReg & 0x10) |
| 259 |
? (subslotReg >> (2 * (addr >> 14))) & 0x03 |
| 260 |
: 0; |
| 261 |
} |
| 262 |
|
| 263 |
unsigned MegaFlashRomSCCPlus::getFlashAddr(unsigned addr) const |
| 264 |
{ |
| 265 |
|
| 266 |
addr = (addr & 0xE000) | ((addr & 0x1000) >> 12) | ((addr & 0x0FFF) << 1); |
| 267 |
|
| 268 |
unsigned subslot = getSubslot(addr); |
| 269 |
unsigned tmp = [&] { |
| 270 |
if ((configReg & 0xC0) == 0x40) { |
| 271 |
unsigned bank = bankRegs[subslot][addr >> 14] + offsetReg; |
| 272 |
return (bank * 0x4000) + (addr & 0x3FFF); |
| 273 |
} else { |
| 274 |
unsigned page = (addr >> 13) - 2; |
| 275 |
if (page >= 4) { |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
return unsigned(-1); |
| 280 |
} |
| 281 |
unsigned bank = bankRegs[subslot][page] + offsetReg; |
| 282 |
return (bank * 0x2000) + (addr & 0x1FFF); |
| 283 |
} |
| 284 |
}(); |
| 285 |
return ((0x40000 * subslot) + tmp) & 0xFFFFF; |
| 286 |
} |
| 287 |
|
| 288 |
bool MegaFlashRomSCCPlus::isSCCAccessEnabled() const |
| 289 |
{ |
| 290 |
|
| 291 |
|
| 292 |
return (configReg & 0xE0) == one_of(0x00, 0x20); |
| 293 |
} |
| 294 |
|
| 295 |
byte MegaFlashRomSCCPlus::peekMem(uint16_t addr, EmuTime time) const |
| 296 |
{ |
| 297 |
if ((configReg & 0x10) && (addr == 0xFFFF)) { |
| 298 |
|
| 299 |
return subslotReg ^ 0xFF; |
| 300 |
} |
| 301 |
|
| 302 |
if (isSCCAccessEnabled()) { |
| 303 |
auto mode = getSCCMode(); |
| 304 |
if (((mode == SCCMode::SCC) && (0x9800 <= addr) && (addr < 0xA000)) || |
| 305 |
((mode == SCCMode::SCCPLUS) && (0xB800 <= addr) && (addr < 0xC000))) { |
| 306 |
return scc.peekMem(narrow_cast<uint8_t>(addr & 0xFF), time); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if (((configReg & 0xC0) == 0x40) || |
| 311 |
((0x4000 <= addr) && (addr < 0xC000))) { |
| 312 |
|
| 313 |
unsigned flashAddr = getFlashAddr(addr); |
| 314 |
assert(flashAddr != unsigned(-1)); |
| 315 |
return flash.peek(flashAddr, time); |
| 316 |
} else { |
| 317 |
|
| 318 |
return 0xFF; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
byte MegaFlashRomSCCPlus::readMem(uint16_t addr, EmuTime time) |
| 323 |
{ |
| 324 |
if ((configReg & 0x10) && (addr == 0xFFFF)) { |
| 325 |
|
| 326 |
return subslotReg ^ 0xFF; |
| 327 |
} |
| 328 |
|
| 329 |
if (isSCCAccessEnabled()) { |
| 330 |
auto mode = getSCCMode(); |
| 331 |
if (((mode == SCCMode::SCC) && (0x9800 <= addr) && (addr < 0xA000)) || |
| 332 |
((mode == SCCMode::SCCPLUS) && (0xB800 <= addr) && (addr < 0xC000))) { |
| 333 |
return scc.readMem(narrow_cast<uint8_t>(addr & 0xFF), time); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
if (((configReg & 0xC0) == 0x40) || |
| 338 |
((0x4000 <= addr) && (addr < 0xC000))) { |
| 339 |
|
| 340 |
unsigned flashAddr = getFlashAddr(addr); |
| 341 |
assert(flashAddr != unsigned(-1)); |
| 342 |
return flash.read(flashAddr, time); |
| 343 |
} else { |
| 344 |
|
| 345 |
return 0xFF; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
const byte* MegaFlashRomSCCPlus::getReadCacheLine(uint16_t addr) const |
| 350 |
{ |
| 351 |
if ((configReg & 0x10) && |
| 352 |
((addr & CacheLine::HIGH) == (0xFFFF & CacheLine::HIGH))) { |
| 353 |
|
| 354 |
return nullptr; |
| 355 |
} |
| 356 |
|
| 357 |
if (isSCCAccessEnabled()) { |
| 358 |
auto mode = getSCCMode(); |
| 359 |
if (((mode == SCCMode::SCC) && (0x9800 <= addr) && (addr < 0xA000)) || |
| 360 |
((mode == SCCMode::SCCPLUS) && (0xB800 <= addr) && (addr < 0xC000))) { |
| 361 |
return nullptr; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if (((configReg & 0xC0) == 0x40) || |
| 366 |
((0x4000 <= addr) && (addr < 0xC000))) { |
| 367 |
return nullptr; |
| 368 |
} else { |
| 369 |
|
| 370 |
return unmappedRead.data(); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
void MegaFlashRomSCCPlus::writeMem(uint16_t addr, byte value, EmuTime time) |
| 375 |
{ |
| 376 |
|
| 377 |
unsigned flashAddr = getFlashAddr(addr); |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
if ((configReg & 0x10) && (addr == 0xFFFF)) { |
| 384 |
|
| 385 |
byte diff = value ^ subslotReg; |
| 386 |
subslotReg = value; |
| 387 |
for (auto i : xrange(4)) { |
| 388 |
if (diff & (3 << (2 * i))) { |
| 389 |
invalidateDeviceRCache(0x4000 * i, 0x4000); |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
if (((configReg & 0x04) == 0x00) && ((addr & 0xFFFE) == 0x7FFE)) { |
| 395 |
|
| 396 |
configReg = value; |
| 397 |
invalidateDeviceRCache(); |
| 398 |
} |
| 399 |
|
| 400 |
if (isSCCAccessEnabled()) { |
| 401 |
|
| 402 |
if ((addr & 0xFFFE) == 0xBFFE) { |
| 403 |
sccModeReg = value; |
| 404 |
scc.setMode((value & 0x20) ? SCC::Mode::Plus |
| 405 |
: SCC::Mode::Compatible); |
| 406 |
invalidateDeviceRCache(0x9800, 0x800); |
| 407 |
invalidateDeviceRCache(0xB800, 0x800); |
| 408 |
} |
| 409 |
auto mode = getSCCMode(); |
| 410 |
bool isRamSegment2 = ((sccModeReg & 0x24) == 0x24) || |
| 411 |
((sccModeReg & 0x10) == 0x10); |
| 412 |
bool isRamSegment3 = ((sccModeReg & 0x10) == 0x10); |
| 413 |
if (((mode == SCCMode::SCC) && !isRamSegment2 && |
| 414 |
(0x9800 <= addr) && (addr < 0xA000)) || |
| 415 |
((mode == SCCMode::SCCPLUS) && !isRamSegment3 && |
| 416 |
(0xB800 <= addr) && (addr < 0xC000))) { |
| 417 |
scc.writeMem(narrow_cast<uint8_t>(addr & 0xFF), value, time); |
| 418 |
return; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
unsigned subslot = getSubslot(addr); |
| 423 |
unsigned page8kB = (addr >> 13) - 2; |
| 424 |
if (((configReg & 0x02) == 0x00) && (page8kB < 4)) { |
| 425 |
|
| 426 |
switch (configReg & 0xE0) { |
| 427 |
case 0x00: |
| 428 |
|
| 429 |
if ((addr & 0x1800) == 0x1000) { |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
sccBanks[page8kB] = value; |
| 434 |
if ((value & 0x80) && (page8kB == 0)) { |
| 435 |
offsetReg = value & 0x7F; |
| 436 |
invalidateDeviceRCache(0x4000, 0x8000); |
| 437 |
} else { |
| 438 |
|
| 439 |
|
| 440 |
byte mask = (configReg & 0x01) ? 0x3F : 0x7F; |
| 441 |
bankRegs[subslot][page8kB] = value & mask; |
| 442 |
invalidateDeviceRCache(0x4000 + 0x2000 * page8kB, 0x2000); |
| 443 |
} |
| 444 |
} |
| 445 |
break; |
| 446 |
case 0x20: { |
| 447 |
|
| 448 |
if (((configReg & 0x08) == 0x08) && (addr < 0x6000)) { |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
break; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
if ((addr < 0x5000) || ((0x5800 <= addr) && (addr < 0x6000))) break; |
| 458 |
byte mask = (configReg & 0x01) ? 0x1F : 0x7F; |
| 459 |
bankRegs[subslot][page8kB] = value & mask; |
| 460 |
sccBanks[page8kB] = value; |
| 461 |
invalidateDeviceRCache(0x4000 + 0x2000 * page8kB, 0x2000); |
| 462 |
break; |
| 463 |
} |
| 464 |
case 0x40: |
| 465 |
case 0x60: |
| 466 |
|
| 467 |
bankRegs[subslot][page8kB] = value; |
| 468 |
invalidateDeviceRCache(0x0000 + 0x4000 * page8kB, 0x4000); |
| 469 |
break; |
| 470 |
case 0x80: |
| 471 |
case 0xA0: |
| 472 |
|
| 473 |
if ((0x6000 <= addr) && (addr < 0x8000)) { |
| 474 |
byte bank = (addr >> 11) & 0x03; |
| 475 |
bankRegs[subslot][bank] = value; |
| 476 |
invalidateDeviceRCache(0x4000 + 0x2000 * bank, 0x2000); |
| 477 |
} |
| 478 |
break; |
| 479 |
case 0xC0: |
| 480 |
case 0xE0: |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
if ((0x6000 <= addr) && (addr < 0x6800)) { |
| 489 |
bankRegs[subslot][0] = narrow_cast<uint8_t>(2 * value + 0); |
| 490 |
bankRegs[subslot][1] = narrow_cast<uint8_t>(2 * value + 1); |
| 491 |
invalidateDeviceRCache(0x4000, 0x4000); |
| 492 |
} |
| 493 |
if ((0x7000 <= addr) && (addr < 0x7800)) { |
| 494 |
bankRegs[subslot][2] = narrow_cast<uint8_t>(2 * value + 0); |
| 495 |
bankRegs[subslot][3] = narrow_cast<uint8_t>(2 * value + 1); |
| 496 |
invalidateDeviceRCache(0x8000, 0x4000); |
| 497 |
} |
| 498 |
break; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
if (((configReg & 0xC0) == 0x40) || |
| 504 |
((0x4000 <= addr) && (addr < 0xC000))) { |
| 505 |
assert(flashAddr != unsigned(-1)); |
| 506 |
flash.write(flashAddr, value, time); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
byte* MegaFlashRomSCCPlus::getWriteCacheLine(uint16_t ) |
| 511 |
{ |
| 512 |
return nullptr; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
void MegaFlashRomSCCPlus::writeIO(uint16_t port, byte value, EmuTime time) |
| 517 |
{ |
| 518 |
if ((port & 0xFF) == 0x10) { |
| 519 |
psgLatch = value & 0x0F; |
| 520 |
} else { |
| 521 |
assert((port & 0xFF) == 0x11); |
| 522 |
psg.writeRegister(psgLatch, value, time); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
template<typename Archive> |
| 528 |
void MegaFlashRomSCCPlus::serialize(Archive& ar, unsigned ) |
| 529 |
{ |
| 530 |
|
| 531 |
ar.template serializeBase<MSXDevice>(*this); |
| 532 |
|
| 533 |
ar.serialize("scc", scc, |
| 534 |
"psg", psg, |
| 535 |
"flash", flash, |
| 536 |
|
| 537 |
"configReg", configReg, |
| 538 |
"offsetReg", offsetReg, |
| 539 |
"subslotReg", subslotReg, |
| 540 |
"bankRegs", bankRegs, |
| 541 |
"psgLatch", psgLatch, |
| 542 |
"sccMode", sccModeReg, |
| 543 |
"sccBanks", sccBanks); |
| 544 |
} |
| 545 |
INSTANTIATE_SERIALIZE_METHODS(MegaFlashRomSCCPlus); |
| 546 |
REGISTER_MSXDEVICE(MegaFlashRomSCCPlus, "MegaFlashRomSCCPlus"); |
| 547 |
|
| 548 |
} |