| 1 |
#include "MSXDevice.hh" |
| 2 |
|
| 3 |
#include "CacheLine.hh" |
| 4 |
#include "CartridgeSlotManager.hh" |
| 5 |
#include "HardwareConfig.hh" |
| 6 |
#include "MSXCPUInterface.hh" |
| 7 |
#include "MSXException.hh" |
| 8 |
#include "MSXMotherBoard.hh" |
| 9 |
#include "TclObject.hh" |
| 10 |
#include "XMLElement.hh" |
| 11 |
|
| 12 |
#include "one_of.hh" |
| 13 |
#include "serialize.hh" |
| 14 |
#include "stl.hh" |
| 15 |
#include "unreachable.hh" |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <bit> |
| 19 |
#include <cassert> |
| 20 |
|
| 21 |
namespace openmsx { |
| 22 |
|
| 23 |
MSXDevice::MSXDevice(const DeviceConfig& config, std::string_view name) |
| 24 |
: deviceConfig(config) |
| 25 |
{ |
| 26 |
initName(name); |
| 27 |
} |
| 28 |
|
| 29 |
MSXDevice::MSXDevice(const DeviceConfig& config) |
| 30 |
: deviceConfig(config) |
| 31 |
{ |
| 32 |
initName(getDeviceConfig().getAttributeValue("id")); |
| 33 |
} |
| 34 |
|
| 35 |
void MSXDevice::initName(std::string_view name) |
| 36 |
{ |
| 37 |
deviceName = name; |
| 38 |
if (getMotherBoard().findDevice(deviceName)) { |
| 39 |
unsigned n = 0; |
| 40 |
do { |
| 41 |
deviceName = strCat(name, " (", ++n, ')'); |
| 42 |
} while (getMotherBoard().findDevice(deviceName)); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
void MSXDevice::init() |
| 47 |
{ |
| 48 |
staticInit(); |
| 49 |
|
| 50 |
lockDevices(); |
| 51 |
registerSlots(); |
| 52 |
registerPorts(); |
| 53 |
} |
| 54 |
|
| 55 |
MSXDevice::~MSXDevice() |
| 56 |
{ |
| 57 |
unregisterPorts(); |
| 58 |
unregisterSlots(); |
| 59 |
unlockDevices(); |
| 60 |
assert(referencedBy.empty()); |
| 61 |
} |
| 62 |
|
| 63 |
void MSXDevice::staticInit() |
| 64 |
{ |
| 65 |
static bool alreadyInit = false; |
| 66 |
if (alreadyInit) return; |
| 67 |
alreadyInit = true; |
| 68 |
|
| 69 |
std::ranges::fill(unmappedRead, 0xFF); |
| 70 |
} |
| 71 |
|
| 72 |
MSXMotherBoard& MSXDevice::getMotherBoard() const |
| 73 |
{ |
| 74 |
return getHardwareConfig().getMotherBoard(); |
| 75 |
} |
| 76 |
|
| 77 |
void MSXDevice::testRemove(std::span<const std::unique_ptr<MSXDevice>> removed) const |
| 78 |
{ |
| 79 |
|
| 80 |
|
| 81 |
std::string err; |
| 82 |
for (const auto* dev : referencedBy) { |
| 83 |
if (!contains(removed, dev, [](const auto& d) { return d.get(); })) { |
| 84 |
strAppend(err, ' ', dev->getName()); |
| 85 |
} |
| 86 |
} |
| 87 |
if (!err.empty()) { |
| 88 |
throw MSXException("Still in use by:", err); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
void MSXDevice::lockDevices() |
| 93 |
{ |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
for (const auto* c : getDeviceConfig().getChildren("device")) { |
| 101 |
auto name = c->getAttributeValue("idref"); |
| 102 |
auto* dev = getMotherBoard().findDevice(name); |
| 103 |
if (!dev) { |
| 104 |
throw MSXException( |
| 105 |
"Unsatisfied dependency: '", getName(), |
| 106 |
"' depends on unavailable device '", |
| 107 |
name, "'."); |
| 108 |
} |
| 109 |
references.push_back(dev); |
| 110 |
dev->referencedBy.push_back(this); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void MSXDevice::unlockDevices() |
| 115 |
{ |
| 116 |
for (auto& r : references) { |
| 117 |
move_pop_back(r->referencedBy, rfind_unguarded(r->referencedBy, this)); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
const MSXDevice::Devices& MSXDevice::getReferences() const |
| 122 |
{ |
| 123 |
|
| 124 |
return references; |
| 125 |
} |
| 126 |
|
| 127 |
EmuTime MSXDevice::getCurrentTime() const |
| 128 |
{ |
| 129 |
return getMotherBoard().getCurrentTime(); |
| 130 |
} |
| 131 |
MSXCPU& MSXDevice::getCPU() const |
| 132 |
{ |
| 133 |
return getMotherBoard().getCPU(); |
| 134 |
} |
| 135 |
MSXCPUInterface& MSXDevice::getCPUInterface() const |
| 136 |
{ |
| 137 |
return getMotherBoard().getCPUInterface(); |
| 138 |
} |
| 139 |
Scheduler& MSXDevice::getScheduler() const |
| 140 |
{ |
| 141 |
return getMotherBoard().getScheduler(); |
| 142 |
} |
| 143 |
MSXCliComm& MSXDevice::getCliComm() const |
| 144 |
{ |
| 145 |
return getMotherBoard().getMSXCliComm(); |
| 146 |
} |
| 147 |
Reactor& MSXDevice::getReactor() const |
| 148 |
{ |
| 149 |
return getMotherBoard().getReactor(); |
| 150 |
} |
| 151 |
CommandController& MSXDevice::getCommandController() const |
| 152 |
{ |
| 153 |
return getMotherBoard().getCommandController(); |
| 154 |
} |
| 155 |
LedStatus& MSXDevice::getLedStatus() const |
| 156 |
{ |
| 157 |
return getMotherBoard().getLedStatus(); |
| 158 |
} |
| 159 |
PluggingController& MSXDevice::getPluggingController() const |
| 160 |
{ |
| 161 |
return getMotherBoard().getPluggingController(); |
| 162 |
} |
| 163 |
|
| 164 |
void MSXDevice::registerSlots() |
| 165 |
{ |
| 166 |
MemRegions tmpMemRegions; |
| 167 |
unsigned align = getBaseSizeAlignment(); |
| 168 |
assert(std::has_single_bit(align)); |
| 169 |
for (const auto* m : getDeviceConfig().getChildren("mem")) { |
| 170 |
unsigned base = m->getAttributeValueAsInt("base", 0); |
| 171 |
unsigned size = m->getAttributeValueAsInt("size", 0); |
| 172 |
if ((base >= 0x10000) || (size > 0x10000) || ((base + size) > 0x10000)) { |
| 173 |
throw MSXException( |
| 174 |
"Invalid memory specification for device ", |
| 175 |
getName(), " should be in range " |
| 176 |
"[0x0000,0x10000)."); |
| 177 |
} |
| 178 |
if (((base & (align - 1)) || (size & (align - 1))) && |
| 179 |
!allowUnaligned()) { |
| 180 |
throw MSXException( |
| 181 |
"invalid memory specification for device ", |
| 182 |
getName(), " should be aligned on at least 0x", |
| 183 |
hex_string<4>(align), '.'); |
| 184 |
} |
| 185 |
tmpMemRegions.push_back({.base = base, .size = size}); |
| 186 |
} |
| 187 |
if (tmpMemRegions.empty()) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
auto& slotManager = getMotherBoard().getSlotManager(); |
| 193 |
auto* primaryConfig = getDeviceConfig2().getPrimary(); |
| 194 |
auto* secondaryConfig = getDeviceConfig2().getSecondary(); |
| 195 |
if (primaryConfig) { |
| 196 |
ps = slotManager.getSlotNum(primaryConfig->getAttributeValue("slot")); |
| 197 |
} else { |
| 198 |
throw MSXException("Invalid memory specification"); |
| 199 |
} |
| 200 |
if (secondaryConfig) { |
| 201 |
auto ss_str = secondaryConfig->getAttributeValue("slot"); |
| 202 |
ss = slotManager.getSlotNum(ss_str); |
| 203 |
if ((-16 <= ss) && (ss <= -1) && (ss != ps)) { |
| 204 |
throw MSXException( |
| 205 |
"Invalid secondary slot specification: \"", |
| 206 |
ss_str, "\"."); |
| 207 |
} |
| 208 |
} else { |
| 209 |
ss = 0; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
auto& mutableConfig = getDeviceConfig(); |
| 219 |
if (auto** primSlotPtr = mutableConfig.findAttributePointer("primary_slot")) { |
| 220 |
ps = slotManager.getSlotNum((*primSlotPtr)->getValue()); |
| 221 |
mutableConfig.removeAttribute(primSlotPtr); |
| 222 |
if (auto** secSlotPtr = mutableConfig.findAttributePointer("secondary_slot")) { |
| 223 |
ss = slotManager.getSlotNum((*secSlotPtr)->getValue()); |
| 224 |
mutableConfig.removeAttribute(secSlotPtr); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
if ((-128 <= ss) && (ss < 0)) { |
| 230 |
if ((0 <= ps) && (ps < 4) && |
| 231 |
getCPUInterface().isExpanded(ps)) { |
| 232 |
ss += 128; |
| 233 |
} else { |
| 234 |
ss = 0; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
if (ps == -256) { |
| 240 |
slotManager.getAnyFreeSlot(ps, ss); |
| 241 |
} else if (ps < 0) { |
| 242 |
|
| 243 |
slotManager.getSpecificSlot(-ps - 1, ps, ss); |
| 244 |
} else { |
| 245 |
|
| 246 |
} |
| 247 |
assert((0 <= ps) && (ps <= 3)); |
| 248 |
|
| 249 |
if (!getCPUInterface().isExpanded(ps)) { |
| 250 |
ss = -1; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
assert(primaryConfig); |
| 260 |
XMLDocument& doc = deviceConfig.getXMLDocument(); |
| 261 |
doc.setAttribute(*primaryConfig, "slot", doc.allocateString(tmpStrCat(ps))); |
| 262 |
if (secondaryConfig) { |
| 263 |
doc.setAttribute(*secondaryConfig, "slot", doc.allocateString( |
| 264 |
(ss == -1) ? std::string_view("X") : tmpStrCat(ss))); |
| 265 |
} else { |
| 266 |
if (ss != -1) { |
| 267 |
throw MSXException( |
| 268 |
"Missing <secondary> tag for device", getName()); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
int logicalSS = (ss == -1) ? 0 : ss; |
| 273 |
for (const auto& r : tmpMemRegions) { |
| 274 |
getCPUInterface().registerMemDevice( |
| 275 |
*this, ps, logicalSS, r.base, r.size); |
| 276 |
memRegions.push_back(r); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
slotManager.allocateSlot(ps, ss, getHardwareConfig()); |
| 285 |
} |
| 286 |
|
| 287 |
void MSXDevice::unregisterSlots() |
| 288 |
{ |
| 289 |
if (memRegions.empty()) return; |
| 290 |
|
| 291 |
int logicalSS = (ss == -1) ? 0 : ss; |
| 292 |
for (const auto& [base, size] : memRegions) { |
| 293 |
getCPUInterface().unregisterMemDevice( |
| 294 |
*this, ps, logicalSS, base, size); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
getMotherBoard().getSlotManager().freeSlot(ps, ss, getHardwareConfig()); |
| 301 |
} |
| 302 |
|
| 303 |
void MSXDevice::getVisibleMemRegion(unsigned& base, unsigned& size) const |
| 304 |
{ |
| 305 |
|
| 306 |
if (memRegions.empty()) { |
| 307 |
base = 0; |
| 308 |
size = 0; |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
auto lowest = min_value(memRegions, &BaseSize::base); |
| 313 |
auto highest = max_value(memRegions, &BaseSize::end); |
| 314 |
assert(lowest <= highest); |
| 315 |
base = lowest; |
| 316 |
size = highest - lowest; |
| 317 |
} |
| 318 |
|
| 319 |
void MSXDevice::registerPorts() |
| 320 |
{ |
| 321 |
|
| 322 |
for (const auto* i : getDeviceConfig().getChildren("io")) { |
| 323 |
unsigned base = i->getAttributeValueAsInt("base", 0); |
| 324 |
unsigned num = i->getAttributeValueAsInt("num", 0); |
| 325 |
auto type = i->getAttributeValue("type", "IO"); |
| 326 |
if (((base + num) > 256) || (num == 0) || |
| 327 |
(type != one_of("I", "O", "IO"))) { |
| 328 |
throw MSXException("Invalid IO port specification"); |
| 329 |
} |
| 330 |
if (type != "O") { |
| 331 |
inPorts.setPosN(base, num); |
| 332 |
} |
| 333 |
if (type != "I") { |
| 334 |
outPorts.setPosN(base, num); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
doRegisterPorts(); |
| 339 |
} |
| 340 |
|
| 341 |
void MSXDevice::doRegisterPorts() |
| 342 |
{ |
| 343 |
inPorts.foreachSetBit([&](auto port) { |
| 344 |
getCPUInterface().register_IO_In(narrow_cast<byte>(port), this); |
| 345 |
}); |
| 346 |
outPorts.foreachSetBit([&](auto port) { |
| 347 |
getCPUInterface().register_IO_Out(narrow_cast<byte>(port), this); |
| 348 |
}); |
| 349 |
} |
| 350 |
|
| 351 |
void MSXDevice::unregisterPorts() |
| 352 |
{ |
| 353 |
inPorts.foreachSetBit([&](auto port) { |
| 354 |
getCPUInterface().unregister_IO_In(narrow_cast<byte>(port), this); |
| 355 |
}); |
| 356 |
outPorts.foreachSetBit([&](auto port) { |
| 357 |
getCPUInterface().unregister_IO_Out(narrow_cast<byte>(port), this); |
| 358 |
}); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
void MSXDevice::reset(EmuTime ) |
| 363 |
{ |
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
byte MSXDevice::readIRQVector() |
| 368 |
{ |
| 369 |
return 0xFF; |
| 370 |
} |
| 371 |
|
| 372 |
void MSXDevice::powerDown(EmuTime ) |
| 373 |
{ |
| 374 |
|
| 375 |
} |
| 376 |
|
| 377 |
void MSXDevice::powerUp(EmuTime time) |
| 378 |
{ |
| 379 |
reset(time); |
| 380 |
} |
| 381 |
|
| 382 |
const std::string& MSXDevice::getName() const |
| 383 |
{ |
| 384 |
return deviceName; |
| 385 |
} |
| 386 |
|
| 387 |
void MSXDevice::getNameList(TclObject& result) const |
| 388 |
{ |
| 389 |
result.addListElement(getName()); |
| 390 |
} |
| 391 |
|
| 392 |
void MSXDevice::getDeviceInfo(TclObject& result) const |
| 393 |
{ |
| 394 |
result.addDictKeyValue("type", getDeviceConfig().getName()); |
| 395 |
getExtraDeviceInfo(result); |
| 396 |
} |
| 397 |
|
| 398 |
void MSXDevice::getExtraDeviceInfo(TclObject& ) const |
| 399 |
{ |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
unsigned MSXDevice::getBaseSizeAlignment() const |
| 404 |
{ |
| 405 |
return CacheLine::SIZE; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
byte MSXDevice::readIO(uint16_t , EmuTime ) |
| 410 |
{ |
| 411 |
|
| 412 |
return 0xFF; |
| 413 |
} |
| 414 |
|
| 415 |
void MSXDevice::writeIO(uint16_t , byte , EmuTime ) |
| 416 |
{ |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
byte MSXDevice::peekIO(uint16_t , EmuTime ) const |
| 421 |
{ |
| 422 |
return 0xFF; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
byte MSXDevice::readMem(uint16_t , EmuTime ) |
| 427 |
{ |
| 428 |
|
| 429 |
return 0xFF; |
| 430 |
} |
| 431 |
|
| 432 |
const byte* MSXDevice::getReadCacheLine(uint16_t ) const |
| 433 |
{ |
| 434 |
return nullptr; |
| 435 |
} |
| 436 |
|
| 437 |
void MSXDevice::writeMem(uint16_t , byte , |
| 438 |
EmuTime ) |
| 439 |
{ |
| 440 |
|
| 441 |
} |
| 442 |
|
| 443 |
byte MSXDevice::peekMem(uint16_t address, EmuTime ) const |
| 444 |
{ |
| 445 |
uint16_t base = address & CacheLine::HIGH; |
| 446 |
if (const byte* cache = getReadCacheLine(base)) { |
| 447 |
uint16_t offset = address & CacheLine::LOW; |
| 448 |
return cache[offset]; |
| 449 |
} else { |
| 450 |
|
| 451 |
return 0xFF; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
void MSXDevice::globalWrite(uint16_t , byte , |
| 456 |
EmuTime ) |
| 457 |
{ |
| 458 |
UNREACHABLE; |
| 459 |
} |
| 460 |
|
| 461 |
void MSXDevice::globalRead(uint16_t , EmuTime ) |
| 462 |
{ |
| 463 |
UNREACHABLE; |
| 464 |
} |
| 465 |
|
| 466 |
byte* MSXDevice::getWriteCacheLine(uint16_t ) |
| 467 |
{ |
| 468 |
return nullptr; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
template<typename Action, typename... Args> |
| 475 |
void MSXDevice::clip(unsigned start, unsigned size, Action action, Args... args) |
| 476 |
{ |
| 477 |
assert(start < 0x10000); |
| 478 |
int ss2 = (ss != -1) ? ss : 0; |
| 479 |
unsigned end = start + size; |
| 480 |
for (auto [base, fullBsize] : memRegions) { |
| 481 |
|
| 482 |
while (fullBsize > 0) { |
| 483 |
unsigned bsize = std::min(fullBsize, ((base + 0x4000) & ~0x3fff) - base); |
| 484 |
|
| 485 |
unsigned baseEnd = base + bsize; |
| 486 |
|
| 487 |
unsigned clipStart = std::max(start, base); |
| 488 |
unsigned clipEnd = std::min(end, baseEnd); |
| 489 |
if (clipStart < clipEnd) { |
| 490 |
unsigned clipSize = clipEnd - clipStart; |
| 491 |
action(narrow<uint16_t>(clipStart), clipSize, args..., ps, ss2); |
| 492 |
} |
| 493 |
|
| 494 |
base += bsize; |
| 495 |
fullBsize -= bsize; |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
void MSXDevice::invalidateDeviceRWCache(unsigned start, unsigned size) |
| 501 |
{ |
| 502 |
clip(start, size, [&](auto... args) { getCPUInterface().invalidateRWCache(args...); }); |
| 503 |
} |
| 504 |
void MSXDevice::invalidateDeviceRCache(unsigned start, unsigned size) |
| 505 |
{ |
| 506 |
clip(start, size, [&](auto... args) { getCPUInterface().invalidateRCache(args...); }); |
| 507 |
} |
| 508 |
void MSXDevice::invalidateDeviceWCache(unsigned start, unsigned size) |
| 509 |
{ |
| 510 |
clip(start, size, [&](auto... args) { getCPUInterface().invalidateWCache(args...); }); |
| 511 |
} |
| 512 |
|
| 513 |
void MSXDevice::fillDeviceRWCache(unsigned start, unsigned size, byte* rwData) |
| 514 |
{ |
| 515 |
fillDeviceRWCache(start, size, rwData, rwData); |
| 516 |
} |
| 517 |
void MSXDevice::fillDeviceRWCache(unsigned start, unsigned size, const byte* rData, byte* wData) |
| 518 |
{ |
| 519 |
assert(!allowUnaligned()); |
| 520 |
clip(start, size, [&](auto... args) { getCPUInterface().fillRWCache(args...); }, rData, wData); |
| 521 |
} |
| 522 |
void MSXDevice::fillDeviceRCache(unsigned start, unsigned size, const byte* rData) |
| 523 |
{ |
| 524 |
assert(!allowUnaligned()); |
| 525 |
clip(start, size, [&](auto... args) { getCPUInterface().fillRCache(args...); }, rData); |
| 526 |
} |
| 527 |
void MSXDevice::fillDeviceWCache(unsigned start, unsigned size, byte* wData) |
| 528 |
{ |
| 529 |
assert(!allowUnaligned()); |
| 530 |
clip(start, size, [&](auto... args) { getCPUInterface().fillWCache(args...); }, wData); |
| 531 |
} |
| 532 |
|
| 533 |
template<typename Archive> |
| 534 |
void MSXDevice::serialize(Archive& ar, unsigned ) |
| 535 |
{ |
| 536 |
|
| 537 |
|
| 538 |
ar.serialize("name", deviceName); |
| 539 |
} |
| 540 |
INSTANTIATE_SERIALIZE_METHODS(MSXDevice); |
| 541 |
|
| 542 |
} |