| 1 |
#include "RealDrive.hh" |
| 2 |
|
| 3 |
#include "DMKDiskImage.hh" |
| 4 |
#include "DirAsDSK.hh" |
| 5 |
#include "Disk.hh" |
| 6 |
#include "DummyDisk.hh" |
| 7 |
#include "RamDSKDiskImage.hh" |
| 8 |
|
| 9 |
#include "GlobalSettings.hh" |
| 10 |
#include "LedStatus.hh" |
| 11 |
#include "MSXCliComm.hh" |
| 12 |
#include "MSXCommandController.hh" |
| 13 |
#include "MSXException.hh" |
| 14 |
#include "MSXMotherBoard.hh" |
| 15 |
#include "Reactor.hh" |
| 16 |
|
| 17 |
#include "narrow.hh" |
| 18 |
#include "serialize.hh" |
| 19 |
#include "unreachable.hh" |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
#include <ranges> |
| 23 |
|
| 24 |
namespace openmsx { |
| 25 |
|
| 26 |
std::shared_ptr<RealDrive::DrivesInUse> RealDrive::getDrivesInUse(MSXMotherBoard& motherBoard) |
| 27 |
{ |
| 28 |
return motherBoard.getSharedStuff<DrivesInUse>("drivesInUse"); |
| 29 |
} |
| 30 |
|
| 31 |
RealDrive::RealDrive(MSXMotherBoard& motherBoard_, EmuDuration motorTimeout_, |
| 32 |
bool signalsNeedMotorOn_, bool doubleSided, unsigned nominalTracks, |
| 33 |
DiskDrive::TrackMode trackMode_) |
| 34 |
: syncLoadingTimeout(motherBoard_.getScheduler()) |
| 35 |
, syncMotorTimeout (motherBoard_.getScheduler()) |
| 36 |
, motherBoard(motherBoard_) |
| 37 |
, loadingIndicator( |
| 38 |
motherBoard.getReactor().getGlobalSettings().getThrottleManager()) |
| 39 |
, motorTimeout(motorTimeout_) |
| 40 |
, motorTimer(getCurrentTime()) |
| 41 |
, doubleSizedDrive(doubleSided) |
| 42 |
, signalsNeedMotorOn(signalsNeedMotorOn_) |
| 43 |
, trackMode(trackMode_) |
| 44 |
{ |
| 45 |
drivesInUse = getDrivesInUse(motherBoard); |
| 46 |
|
| 47 |
unsigned i = 0; |
| 48 |
while ((*drivesInUse)[i]) { |
| 49 |
if (++i == MAX_DRIVES) { |
| 50 |
throw MSXException("Too many disk drives."); |
| 51 |
} |
| 52 |
} |
| 53 |
(*drivesInUse)[i] = true; |
| 54 |
std::string driveName = "diskX"; driveName[4] = char('a' + i); |
| 55 |
|
| 56 |
if (motherBoard.getMSXCommandController().hasCommand(driveName)) { |
| 57 |
throw MSXException("Duplicated drive name: ", driveName); |
| 58 |
} |
| 59 |
motherBoard.getMSXCliComm().update(CliComm::UpdateType::HARDWARE, driveName, "add"); |
| 60 |
changer.emplace(motherBoard, driveName, true, doubleSizedDrive, nominalTracks, |
| 61 |
[this]() { invalidateTrack(); }); |
| 62 |
motherBoard.registerMediaProvider(changer->getDriveName(), *this); |
| 63 |
} |
| 64 |
|
| 65 |
RealDrive::~RealDrive() |
| 66 |
{ |
| 67 |
try { |
| 68 |
flushTrack(); |
| 69 |
} catch (MSXException&) { |
| 70 |
|
| 71 |
} |
| 72 |
doSetMotor(false, getCurrentTime()); |
| 73 |
|
| 74 |
motherBoard.unregisterMediaProvider(*this); |
| 75 |
|
| 76 |
const auto& driveName = changer->getDriveName(); |
| 77 |
motherBoard.getMSXCliComm().update(CliComm::UpdateType::HARDWARE, driveName, "remove"); |
| 78 |
|
| 79 |
unsigned driveNum = driveName[4] - 'a'; |
| 80 |
assert((*drivesInUse)[driveNum]); |
| 81 |
(*drivesInUse)[driveNum] = false; |
| 82 |
} |
| 83 |
|
| 84 |
void RealDrive::getMediaInfo(TclObject& result) |
| 85 |
{ |
| 86 |
auto typeStr = [&] -> std::string_view { |
| 87 |
if (dynamic_cast<DummyDisk*>(&(changer->getDisk()))) { |
| 88 |
return "empty"; |
| 89 |
} else if (dynamic_cast<DirAsDSK*>(&(changer->getDisk()))) { |
| 90 |
return "dirasdisk"; |
| 91 |
} else if (dynamic_cast<RamDSKDiskImage*>(&(changer->getDisk()))) { |
| 92 |
return "ramdsk"; |
| 93 |
} else { |
| 94 |
return "file"; |
| 95 |
} |
| 96 |
}(); |
| 97 |
result.addDictKeyValues("target", changer->getDiskName().getResolved(), |
| 98 |
"type", typeStr, |
| 99 |
"readonly", changer->getDisk().isWriteProtected(), |
| 100 |
"doublesided", changer->getDisk().isDoubleSided()); |
| 101 |
if (!dynamic_cast<DMKDiskImage*>(&(changer->getDisk()))) { |
| 102 |
result.addDictKeyValues("size", int(changer->getDisk().getNbSectors() * SectorAccessibleDisk::SECTOR_SIZE)); |
| 103 |
} |
| 104 |
if (const auto* disk = changer->getSectorAccessibleDisk()) { |
| 105 |
TclObject patches; |
| 106 |
patches.addListElements(std::views::transform(disk->getPatches(), [](auto& p) { |
| 107 |
return p.getResolved(); |
| 108 |
})); |
| 109 |
result.addDictKeyValue("patches", patches); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void RealDrive::setMedia(const TclObject& info, EmuTime ) |
| 114 |
{ |
| 115 |
std::vector<TclObject> args; |
| 116 |
args.emplace_back(changer->getDriveName()); |
| 117 |
|
| 118 |
auto target = info.getOptionalDictValue(TclObject("target")); |
| 119 |
if (!target) return; |
| 120 |
if (auto trgt = target->getString(); trgt.empty()) { |
| 121 |
args.emplace_back("eject"); |
| 122 |
} else { |
| 123 |
args.emplace_back(*target); |
| 124 |
} |
| 125 |
|
| 126 |
if (auto patches = info.getOptionalDictValue(TclObject("patches"))) { |
| 127 |
for (auto i : xrange(patches->size())) { |
| 128 |
args.push_back(patches->getListIndexUnchecked(i)); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
changer->sendChangeDiskEvent(args); |
| 133 |
} |
| 134 |
|
| 135 |
bool RealDrive::isDiskInserted() const |
| 136 |
{ |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if (!doubleSizedDrive && (side != 0)) return false; |
| 150 |
|
| 151 |
return !changer->getDisk().isDummyDisk(); |
| 152 |
} |
| 153 |
|
| 154 |
bool RealDrive::isWriteProtected() const |
| 155 |
{ |
| 156 |
|
| 157 |
|
| 158 |
if (signalsNeedMotorOn && !motorStatus) return false; |
| 159 |
return changer->getDisk().isWriteProtected(); |
| 160 |
} |
| 161 |
|
| 162 |
bool RealDrive::isDoubleSided() |
| 163 |
{ |
| 164 |
return doubleSizedDrive ? changer->getDisk().isDoubleSided() |
| 165 |
: false; |
| 166 |
} |
| 167 |
|
| 168 |
void RealDrive::setSide(bool side_) |
| 169 |
{ |
| 170 |
invalidateTrack(); |
| 171 |
side = side_ ? 1 : 0; |
| 172 |
} |
| 173 |
|
| 174 |
bool RealDrive::getSide() const |
| 175 |
{ |
| 176 |
return side; |
| 177 |
} |
| 178 |
|
| 179 |
unsigned RealDrive::getMaxTrack() const |
| 180 |
{ |
| 181 |
constexpr unsigned MAX_TRACK = 85; |
| 182 |
switch (trackMode) { |
| 183 |
case DiskDrive::TrackMode::NORMAL: |
| 184 |
return MAX_TRACK; |
| 185 |
case DiskDrive::TrackMode::YAMAHA_FD_03: |
| 186 |
|
| 187 |
return MAX_TRACK * 4 + 3; |
| 188 |
default: |
| 189 |
UNREACHABLE; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
std::optional<unsigned> RealDrive::getDiskReadTrack() const |
| 194 |
{ |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
switch (trackMode) { |
| 200 |
case DiskDrive::TrackMode::NORMAL: |
| 201 |
return headPos; |
| 202 |
case DiskDrive::TrackMode::YAMAHA_FD_03: |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
if ((headPos >= 2) && ((headPos % 4) != 2)) { |
| 207 |
return (headPos - 2) / 4; |
| 208 |
} else { |
| 209 |
return {}; |
| 210 |
} |
| 211 |
default: |
| 212 |
UNREACHABLE; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
std::optional<unsigned> RealDrive::getDiskWriteTrack() const |
| 217 |
{ |
| 218 |
switch (trackMode) { |
| 219 |
case DiskDrive::TrackMode::NORMAL: |
| 220 |
return headPos; |
| 221 |
case DiskDrive::TrackMode::YAMAHA_FD_03: |
| 222 |
|
| 223 |
if ((headPos >= 4) && ((headPos % 4) == 0)) { |
| 224 |
return (headPos - 4) / 4; |
| 225 |
} else { |
| 226 |
return {}; |
| 227 |
} |
| 228 |
default: |
| 229 |
UNREACHABLE; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
void RealDrive::step(bool direction, EmuTime time) |
| 234 |
{ |
| 235 |
invalidateTrack(); |
| 236 |
|
| 237 |
if (direction) { |
| 238 |
|
| 239 |
if (headPos < getMaxTrack()) { |
| 240 |
headPos++; |
| 241 |
} |
| 242 |
} else { |
| 243 |
|
| 244 |
if (headPos > 0) { |
| 245 |
headPos--; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if (motorStatus) setLoading(time); |
| 252 |
} |
| 253 |
|
| 254 |
bool RealDrive::isTrack00() const |
| 255 |
{ |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
if (signalsNeedMotorOn && !motorStatus) return false; |
| 261 |
return headPos == 0; |
| 262 |
} |
| 263 |
|
| 264 |
void RealDrive::setMotor(bool status, EmuTime time) |
| 265 |
{ |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
if (status) { |
| 281 |
|
| 282 |
if (syncMotorTimeout.removeSyncPoint()) { |
| 283 |
|
| 284 |
|
| 285 |
assert(motorStatus); |
| 286 |
setLoading(time); |
| 287 |
return; |
| 288 |
} |
| 289 |
if (motorStatus) { |
| 290 |
|
| 291 |
|
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
doSetMotor(true, time); |
| 296 |
setLoading(time); |
| 297 |
} else { |
| 298 |
if (!motorStatus) { |
| 299 |
|
| 300 |
return; |
| 301 |
} |
| 302 |
if (syncMotorTimeout.isPending()) { |
| 303 |
|
| 304 |
|
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
syncLoadingTimeout.removeSyncPoint(); |
| 312 |
loadingIndicator.update(false); |
| 313 |
|
| 314 |
|
| 315 |
syncMotorTimeout.setSyncPoint(time + motorTimeout); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
bool RealDrive::getMotor() const |
| 320 |
{ |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
return motorStatus; |
| 325 |
} |
| 326 |
|
| 327 |
unsigned RealDrive::getCurrentAngle(EmuTime time) const |
| 328 |
{ |
| 329 |
if (motorStatus) { |
| 330 |
|
| 331 |
auto deltaAngle = motorTimer.getTicksTillUp(time); |
| 332 |
return narrow_cast<unsigned>((startAngle + deltaAngle) % TICKS_PER_ROTATION); |
| 333 |
} else { |
| 334 |
|
| 335 |
return startAngle; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
void RealDrive::doSetMotor(bool status, EmuTime time) |
| 340 |
{ |
| 341 |
if (!status) { |
| 342 |
invalidateTrack(); |
| 343 |
} |
| 344 |
|
| 345 |
startAngle = getCurrentAngle(time); |
| 346 |
motorStatus = status; |
| 347 |
motorTimer.advance(time); |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
motherBoard.getLedStatus().setLed(LedStatus::FDD, status); |
| 353 |
} |
| 354 |
|
| 355 |
void RealDrive::setLoading(EmuTime time) |
| 356 |
{ |
| 357 |
assert(motorStatus); |
| 358 |
loadingIndicator.update(true); |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
syncLoadingTimeout.removeSyncPoint(); |
| 364 |
syncLoadingTimeout.setSyncPoint(time + EmuDuration::sec(1)); |
| 365 |
} |
| 366 |
|
| 367 |
void RealDrive::execLoadingTimeout() |
| 368 |
{ |
| 369 |
loadingIndicator.update(false); |
| 370 |
} |
| 371 |
|
| 372 |
void RealDrive::execMotorTimeout(EmuTime time) |
| 373 |
{ |
| 374 |
doSetMotor(false, time); |
| 375 |
} |
| 376 |
|
| 377 |
bool RealDrive::indexPulse(EmuTime time) |
| 378 |
{ |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
if (!(motorStatus && isDiskInserted())) { |
| 383 |
return false; |
| 384 |
} |
| 385 |
return getCurrentAngle(time) < INDEX_DURATION; |
| 386 |
} |
| 387 |
|
| 388 |
EmuTime RealDrive::getTimeTillIndexPulse(EmuTime time, int count) |
| 389 |
{ |
| 390 |
if (!motorStatus || !isDiskInserted()) { |
| 391 |
return EmuTime::infinity(); |
| 392 |
} |
| 393 |
unsigned delta = TICKS_PER_ROTATION - getCurrentAngle(time); |
| 394 |
auto dur1 = MotorClock::duration(delta); |
| 395 |
auto dur2 = MotorClock::duration(TICKS_PER_ROTATION) * (count - 1); |
| 396 |
return time + dur1 + dur2; |
| 397 |
} |
| 398 |
|
| 399 |
void RealDrive::invalidateTrack() |
| 400 |
{ |
| 401 |
try { |
| 402 |
flushTrack(); |
| 403 |
} catch (MSXException&) { |
| 404 |
|
| 405 |
} |
| 406 |
trackValid = false; |
| 407 |
} |
| 408 |
|
| 409 |
void RealDrive::getTrack() |
| 410 |
{ |
| 411 |
if (!motorStatus) { |
| 412 |
|
| 413 |
assert(!trackValid); |
| 414 |
return; |
| 415 |
} |
| 416 |
if (!trackValid) { |
| 417 |
if (auto rdTrack = getDiskReadTrack()) { |
| 418 |
changer->getDisk().readTrack(narrow<uint8_t>(*rdTrack), |
| 419 |
narrow<uint8_t>(side), |
| 420 |
track); |
| 421 |
} else { |
| 422 |
track.clear(track.getLength()); |
| 423 |
} |
| 424 |
trackValid = true; |
| 425 |
trackDirty = false; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
unsigned RealDrive::getTrackLength() |
| 430 |
{ |
| 431 |
getTrack(); |
| 432 |
return track.getLength(); |
| 433 |
} |
| 434 |
|
| 435 |
void RealDrive::writeTrackByte(int idx, uint8_t val, bool addIdam) |
| 436 |
{ |
| 437 |
getTrack(); |
| 438 |
|
| 439 |
|
| 440 |
track.write(idx, val, addIdam); |
| 441 |
trackDirty = true; |
| 442 |
} |
| 443 |
|
| 444 |
uint8_t RealDrive::readTrackByte(int idx) |
| 445 |
{ |
| 446 |
getTrack(); |
| 447 |
return trackValid ? track.read(idx) : 0; |
| 448 |
} |
| 449 |
|
| 450 |
static constexpr unsigned divUp(unsigned a, unsigned b) |
| 451 |
{ |
| 452 |
return (a + b - 1) / b; |
| 453 |
} |
| 454 |
EmuTime RealDrive::getNextSector(EmuTime time, RawTrack::Sector& sector) |
| 455 |
{ |
| 456 |
getTrack(); |
| 457 |
unsigned currentAngle = getCurrentAngle(time); |
| 458 |
unsigned trackLen = track.getLength(); |
| 459 |
unsigned idx = divUp(currentAngle * trackLen, TICKS_PER_ROTATION); |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
if (auto s = track.decodeNextSector(idx + 4)) { |
| 469 |
sector = *s; |
| 470 |
} else { |
| 471 |
return EmuTime::infinity(); |
| 472 |
} |
| 473 |
unsigned sectorAngle = divUp(sector.addrIdx * TICKS_PER_ROTATION, trackLen); |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
auto delta = narrow<int>(sectorAngle) - narrow<int>(currentAngle); |
| 478 |
if (delta < 4) delta += TICKS_PER_ROTATION; |
| 479 |
assert(4 <= delta); assert(unsigned(delta) < (TICKS_PER_ROTATION + 4)); |
| 480 |
|
| 481 |
return time + MotorClock::duration(delta); |
| 482 |
} |
| 483 |
|
| 484 |
void RealDrive::flushTrack() |
| 485 |
{ |
| 486 |
if (trackValid && trackDirty) { |
| 487 |
if (auto wrTrack = getDiskWriteTrack()) { |
| 488 |
changer->getDisk().writeTrack(narrow<uint8_t>(*wrTrack), |
| 489 |
narrow<uint8_t>(side), |
| 490 |
track); |
| 491 |
} |
| 492 |
trackDirty = false; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
bool RealDrive::diskChanged() |
| 497 |
{ |
| 498 |
return changer->diskChanged(); |
| 499 |
} |
| 500 |
|
| 501 |
bool RealDrive::peekDiskChanged() const |
| 502 |
{ |
| 503 |
return changer->peekDiskChanged(); |
| 504 |
} |
| 505 |
|
| 506 |
bool RealDrive::isDummyDrive() const |
| 507 |
{ |
| 508 |
return false; |
| 509 |
} |
| 510 |
|
| 511 |
void RealDrive::applyWd2793ReadTrackQuirk() |
| 512 |
{ |
| 513 |
track.applyWd2793ReadTrackQuirk(); |
| 514 |
} |
| 515 |
|
| 516 |
void RealDrive::invalidateWd2793ReadTrackQuirk() |
| 517 |
{ |
| 518 |
trackValid = false; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
template<typename Archive> |
| 529 |
void RealDrive::serialize(Archive& ar, unsigned version) |
| 530 |
{ |
| 531 |
if (ar.versionAtLeast(version, 4)) { |
| 532 |
ar.serialize("syncLoadingTimeout", syncLoadingTimeout, |
| 533 |
"syncMotorTimeout", syncMotorTimeout); |
| 534 |
} else { |
| 535 |
Schedulable::restoreOld(ar, {&syncLoadingTimeout, &syncMotorTimeout}); |
| 536 |
} |
| 537 |
ar.serialize("motorTimer", motorTimer, |
| 538 |
"changer", *changer, |
| 539 |
"headPos", headPos, |
| 540 |
"side", side, |
| 541 |
"motorStatus", motorStatus); |
| 542 |
if (ar.versionAtLeast(version, 3)) { |
| 543 |
ar.serialize("startAngle", startAngle); |
| 544 |
} else { |
| 545 |
assert(Archive::IS_LOADER); |
| 546 |
startAngle = 0; |
| 547 |
} |
| 548 |
if (ar.versionAtLeast(version, 5)) { |
| 549 |
ar.serialize("track", track); |
| 550 |
ar.serialize("trackValid", trackValid); |
| 551 |
ar.serialize("trackDirty", trackDirty); |
| 552 |
} |
| 553 |
if constexpr (Archive::IS_LOADER) { |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
if (motorStatus) { |
| 564 |
motherBoard.getLedStatus().setLed(LedStatus::FDD, true); |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
INSTANTIATE_SERIALIZE_METHODS(RealDrive); |
| 569 |
|
| 570 |
} |