| e8555c89 Andy McCall |
1 |
#include "IDECDROM.hh" |
| 2 |
|
| 3 |
#include "CommandException.hh" |
| 4 |
#include "DeviceConfig.hh" |
| 5 |
#include "FileContext.hh" |
| 6 |
#include "FileException.hh" |
| 7 |
#include "MSXCliComm.hh" |
| 8 |
#include "TclObject.hh" |
| 9 |
#include "serialize.hh" |
| 10 |
|
| 11 |
#include "endian.hh" |
| 12 |
#include "narrow.hh" |
| 13 |
#include "one_of.hh" |
| 14 |
#include "xrange.hh" |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
#include <array> |
| 18 |
#include <cassert> |
| 19 |
#include <cstdio> |
| 20 |
#include <memory> |
| 21 |
|
| 22 |
namespace openmsx { |
| 23 |
|
| 24 |
std::shared_ptr<IDECDROM::CDInUse> IDECDROM::getDrivesInUse(MSXMotherBoard& motherBoard) |
| 25 |
{ |
| 26 |
return motherBoard.getSharedStuff<CDInUse>("cdInUse"); |
| 27 |
} |
| 28 |
|
| 29 |
IDECDROM::IDECDROM(const DeviceConfig& config) |
| 30 |
: AbstractIDEDevice(config.getMotherBoard()) |
| 31 |
, name("cdX") |
| 32 |
, devName(config.getChildData("name", "openMSX CD-ROM")) |
| 33 |
{ |
| 34 |
cdInUse = getDrivesInUse(getMotherBoard()); |
| 35 |
|
| 36 |
unsigned id = 0; |
| 37 |
while ((*cdInUse)[id]) { |
| 38 |
++id; |
| 39 |
if (id == MAX_CD) { |
| 40 |
throw MSXException("Too many CDs"); |
| 41 |
} |
| 42 |
} |
| 43 |
name[2] = char('a' + id); |
| 44 |
(*cdInUse)[id] = true; |
| 45 |
cdxCommand.emplace( |
| 46 |
getMotherBoard().getCommandController(), |
| 47 |
getMotherBoard().getStateChangeDistributor(), |
| 48 |
getMotherBoard().getScheduler(), *this); |
| 49 |
|
| 50 |
senseKey = 0; |
| 51 |
|
| 52 |
remMedStatNotifEnabled = false; |
| 53 |
mediaChanged = false; |
| 54 |
|
| 55 |
byteCountLimit = 0; |
| 56 |
transferOffset = 0; |
| 57 |
readSectorData = false; |
| 58 |
|
| 59 |
getMotherBoard().registerMediaProvider(name, *this); |
| 60 |
getMotherBoard().getMSXCliComm().update(CliComm::UpdateType::HARDWARE, name, "add"); |
| 61 |
} |
| 62 |
|
| 63 |
IDECDROM::~IDECDROM() |
| 64 |
{ |
| 65 |
getMotherBoard().unregisterMediaProvider(*this); |
| 66 |
getMotherBoard().getMSXCliComm().update(CliComm::UpdateType::HARDWARE, name, "remove"); |
| 67 |
|
| 68 |
unsigned id = name[2] - 'a'; |
| 69 |
assert((*cdInUse)[id]); |
| 70 |
(*cdInUse)[id] = false; |
| 71 |
} |
| 72 |
|
| 73 |
void IDECDROM::getMediaInfo(TclObject& result) |
| 74 |
{ |
| 75 |
result.addDictKeyValue("target", filename); |
| 76 |
} |
| 77 |
|
| 78 |
void IDECDROM::setMedia(const TclObject& info, EmuTime ) |
| 79 |
{ |
| 80 |
auto target = info.getOptionalDictValue(TclObject("target")); |
| 81 |
if (!target) return; |
| 82 |
|
| 83 |
if (auto trgt = target->getString(); trgt.empty()) { |
| 84 |
eject(); |
| 85 |
} else { |
| 86 |
insert(trgt); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
bool IDECDROM::isPacketDevice() |
| 91 |
{ |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
std::string_view IDECDROM::getDeviceName() |
| 96 |
{ |
| 97 |
return devName; |
| 98 |
} |
| 99 |
|
| 100 |
void IDECDROM::fillIdentifyBlock(AlignedBuffer& buf) |
| 101 |
{ |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
buf[0 * 2 + 0] = 0xC4; |
| 108 |
|
| 109 |
|
| 110 |
buf[0 * 2 + 1] = 0x85; |
| 111 |
|
| 112 |
|
| 113 |
buf[ 83 * 2 + 0] = 0x10; |
| 114 |
|
| 115 |
buf[ 86 * 2 + 0] = remMedStatNotifEnabled * 0x10; |
| 116 |
|
| 117 |
buf[127 * 2 + 0] = 0x01; |
| 118 |
} |
| 119 |
|
| 120 |
unsigned IDECDROM::readBlockStart(AlignedBuffer& buf, unsigned count) |
| 121 |
{ |
| 122 |
assert(readSectorData); |
| 123 |
if (file.is_open()) { |
| 124 |
|
| 125 |
file.seek(transferOffset); |
| 126 |
file.read(std::span{buf.data(), count}); |
| 127 |
transferOffset += count; |
| 128 |
return count; |
| 129 |
} else { |
| 130 |
|
| 131 |
|
| 132 |
abortReadTransfer(0); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void IDECDROM::readEnd() |
| 138 |
{ |
| 139 |
setInterruptReason(I_O | C_D); |
| 140 |
} |
| 141 |
|
| 142 |
void IDECDROM::writeBlockComplete(AlignedBuffer& buf, unsigned count) |
| 143 |
{ |
| 144 |
|
| 145 |
assert(count == 12); |
| 146 |
(void)count; |
| 147 |
executePacketCommand(buf); |
| 148 |
} |
| 149 |
|
| 150 |
void IDECDROM::executeCommand(uint8_t cmd) |
| 151 |
{ |
| 152 |
switch (cmd) { |
| 153 |
case 0xA0: |
| 154 |
|
| 155 |
byteCountLimit = getByteCount(); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
startWriteTransfer(12); |
| 161 |
setInterruptReason(C_D); |
| 162 |
break; |
| 163 |
|
| 164 |
case 0xDA: |
| 165 |
if (remMedStatNotifEnabled) { |
| 166 |
setError(0); |
| 167 |
} else { |
| 168 |
|
| 169 |
uint8_t err = 0; |
| 170 |
if (file.is_open()) { |
| 171 |
err |= 0x40; |
| 172 |
} else { |
| 173 |
err |= 0x02; |
| 174 |
} |
| 175 |
|
| 176 |
if (mediaChanged) { |
| 177 |
err |= 0x20; |
| 178 |
mediaChanged = false; |
| 179 |
} |
| 180 |
|
| 181 |
setError(err); |
| 182 |
} |
| 183 |
break; |
| 184 |
|
| 185 |
case 0xEF: |
| 186 |
switch (getFeatureReg()) { |
| 187 |
case 0x31: |
| 188 |
remMedStatNotifEnabled = false; |
| 189 |
break; |
| 190 |
case 0x95: |
| 191 |
setLBAMid(0x00); |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
setLBAHigh(remMedStatNotifEnabled); |
| 196 |
remMedStatNotifEnabled = true; |
| 197 |
break; |
| 198 |
default: |
| 199 |
AbstractIDEDevice::executeCommand(cmd); |
| 200 |
} |
| 201 |
break; |
| 202 |
|
| 203 |
default: |
| 204 |
AbstractIDEDevice::executeCommand(cmd); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void IDECDROM::startPacketReadTransfer(unsigned count) |
| 209 |
{ |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
unsigned packetSize = 512; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
unsigned size = std::min(packetSize, count); |
| 220 |
setByteCount(size); |
| 221 |
setInterruptReason(I_O); |
| 222 |
} |
| 223 |
|
| 224 |
void IDECDROM::executePacketCommand(AlignedBuffer& packet) |
| 225 |
{ |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
readSectorData = false; |
| 235 |
|
| 236 |
switch (packet[0]) { |
| 237 |
case 0x03: { |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
const int byteCount = 18; |
| 245 |
startPacketReadTransfer(byteCount); |
| 246 |
auto& buf = startShortReadTransfer(byteCount); |
| 247 |
for (auto i : xrange(byteCount)) { |
| 248 |
buf[i] = 0x00; |
| 249 |
} |
| 250 |
buf[ 0] = 0xF0; |
| 251 |
buf[ 2] = narrow_cast<uint8_t>((senseKey >> 16) & 0xFF); |
| 252 |
buf[12] = narrow_cast<uint8_t>((senseKey >> 8) & 0xFF); |
| 253 |
buf[13] = narrow_cast<uint8_t>((senseKey >> 0) & 0xFF); |
| 254 |
buf[ 7] = byteCount - 7; |
| 255 |
senseKey = 0; |
| 256 |
break; |
| 257 |
} |
| 258 |
case 0x43: { |
| 259 |
|
| 260 |
int format = packet[2] & 0x0F; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
switch (format) { |
| 265 |
case 0: { |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
setError(ABORT); |
| 271 |
break; |
| 272 |
} |
| 273 |
case 1: |
| 274 |
case 2: |
| 275 |
case 3: |
| 276 |
case 4: |
| 277 |
default: |
| 278 |
fprintf(stderr, " read TOC: format %d not implemented\n", format); |
| 279 |
setError(ABORT); |
| 280 |
} |
| 281 |
break; |
| 282 |
} |
| 283 |
case 0xA8: { |
| 284 |
uint32_t sectorNumber = Endian::read_UA_B32(&packet[2]); |
| 285 |
uint32_t sectorCount = Endian::read_UA_B32(&packet[6]); |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
readSectorData = true; |
| 309 |
transferOffset = sectorNumber * 2048; |
| 310 |
unsigned count = sectorCount * 2048; |
| 311 |
startPacketReadTransfer(count); |
| 312 |
startLongReadTransfer(count); |
| 313 |
break; |
| 314 |
} |
| 315 |
default: |
| 316 |
fprintf(stderr, " unknown packet command 0x%02X\n", packet[0]); |
| 317 |
setError(ABORT); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
void IDECDROM::eject() |
| 322 |
{ |
| 323 |
file.close(); |
| 324 |
filename.clear(); |
| 325 |
mediaChanged = true; |
| 326 |
senseKey = 0x06 << 16; |
| 327 |
getMotherBoard().getMSXCliComm().update(CliComm::UpdateType::MEDIA, name, {}); |
| 328 |
} |
| 329 |
|
| 330 |
void IDECDROM::insert(zstring_view fname) |
| 331 |
{ |
| 332 |
file = File(fname); |
| 333 |
filename = fname; |
| 334 |
mediaChanged = true; |
| 335 |
senseKey = 0x06 << 16; |
| 336 |
getMotherBoard().getMSXCliComm().update(CliComm::UpdateType::MEDIA, name, filename); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
CDXCommand::CDXCommand(CommandController& commandController_, |
| 343 |
StateChangeDistributor& stateChangeDistributor_, |
| 344 |
Scheduler& scheduler_, IDECDROM& cd_) |
| 345 |
: RecordedCommand(commandController_, stateChangeDistributor_, |
| 346 |
scheduler_, cd_.name) |
| 347 |
, cd(cd_) |
| 348 |
{ |
| 349 |
} |
| 350 |
|
| 351 |
void CDXCommand::execute(std::span<const TclObject> tokens, TclObject& result, |
| 352 |
EmuTime ) |
| 353 |
{ |
| 354 |
if (tokens.size() == 1) { |
| 355 |
const auto& file = cd.file; |
| 356 |
result.addListElement(tmpStrCat(cd.name, ':'), cd.filename); |
| 357 |
if (!file.is_open()) result.addListElement("empty"); |
| 358 |
} else if ((tokens.size() == 2) && (tokens[1] == one_of("eject", "-eject"))) { |
| 359 |
cd.eject(); |
| 360 |
|
| 361 |
if (tokens[1] == "-eject") { |
| 362 |
result = "Warning: use of '-eject' is deprecated, " |
| 363 |
"instead use the 'eject' subcommand"; |
| 364 |
} |
| 365 |
} else if ((tokens.size() == 2) || |
| 366 |
((tokens.size() == 3) && (tokens[1] == "insert"))) { |
| 367 |
int fileToken = 1; |
| 368 |
if (tokens[1] == "insert") { |
| 369 |
if (tokens.size() > 2) { |
| 370 |
fileToken = 2; |
| 371 |
} else { |
| 372 |
throw CommandException( |
| 373 |
"Missing argument to insert subcommand"); |
| 374 |
} |
| 375 |
} |
| 376 |
try { |
| 377 |
auto filename = userFileContext().resolve( |
| 378 |
tokens[fileToken].getString()); |
| 379 |
cd.insert(filename); |
| 380 |
} catch (FileException& e) { |
| 381 |
throw CommandException("Can't change cd image: ", |
| 382 |
e.getMessage()); |
| 383 |
} |
| 384 |
} else { |
| 385 |
throw CommandException("Too many or wrong arguments."); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
std::string CDXCommand::help(std::span<const TclObject> ) const |
| 390 |
{ |
| 391 |
return strCat( |
| 392 |
cd.name, " : display the cd image for this CD-ROM drive\n", |
| 393 |
cd.name, " eject : eject the cd image from this CD-ROM drive\n", |
| 394 |
cd.name, " insert <filename> : change the cd image for this CD-ROM drive\n", |
| 395 |
cd.name, " <filename> : change the cd image for this CD-ROM drive\n"); |
| 396 |
} |
| 397 |
|
| 398 |
void CDXCommand::tabCompletion(std::vector<std::string>& tokens) const |
| 399 |
{ |
| 400 |
using namespace std::literals; |
| 401 |
static constexpr std::array extra = {"eject"sv, "insert"sv}; |
| 402 |
completeFileName(tokens, userFileContext(), extra); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
template<typename Archive> |
| 407 |
void IDECDROM::serialize(Archive& ar, unsigned ) |
| 408 |
{ |
| 409 |
ar.template serializeBase<AbstractIDEDevice>(*this); |
| 410 |
|
| 411 |
ar.serialize("filename", filename); |
| 412 |
if constexpr (Archive::IS_LOADER) { |
| 413 |
|
| 414 |
if (filename.empty()) { |
| 415 |
eject(); |
| 416 |
} else { |
| 417 |
insert(filename); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
ar.serialize("byteCountLimit", byteCountLimit, |
| 422 |
"transferOffset", transferOffset, |
| 423 |
"senseKey", senseKey, |
| 424 |
"readSectorData", readSectorData, |
| 425 |
"remMedStatNotifEnabled", remMedStatNotifEnabled, |
| 426 |
"mediaChanged", mediaChanged); |
| 427 |
} |
| 428 |
INSTANTIATE_SERIALIZE_METHODS(IDECDROM); |
| 429 |
REGISTER_POLYMORPHIC_INITIALIZER(IDEDevice, IDECDROM, "IDECDROM"); |
| 430 |
|
| 431 |
} |