| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include "../mednafen.h" |
| 19 | #include <string.h> |
| 20 | #include <sys/types.h> |
| 21 | #include "cdromif.h" |
| 22 | #include "CDAccess.h" |
| 23 | #include "../general.h" |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include "../../libretro.h" |
| 27 | |
| 28 | extern retro_log_printf_t log_cb; |
| 29 | |
| 30 | using namespace CDUtility; |
| 31 | |
| 32 | enum |
| 33 | { |
| 34 | // Status/Error messages |
| 35 | CDIF_MSG_DONE = 0, // Read -> emu. args: No args. |
| 36 | CDIF_MSG_INFO, // Read -> emu. args: str_message |
| 37 | CDIF_MSG_FATAL_ERROR, // Read -> emu. args: *TODO ARGS* |
| 38 | |
| 39 | // |
| 40 | // Command messages. |
| 41 | // |
| 42 | CDIF_MSG_DIEDIEDIE, // Emu -> read |
| 43 | |
| 44 | CDIF_MSG_READ_SECTOR, /* Emu -> read |
| 45 | args[0] = lba |
| 46 | */ |
| 47 | |
| 48 | CDIF_MSG_EJECT, // Emu -> read, args[0]; 0=insert, 1=eject |
| 49 | }; |
| 50 | |
| 51 | class CDIF_Message |
| 52 | { |
| 53 | public: |
| 54 | |
| 55 | CDIF_Message(); |
| 56 | CDIF_Message(unsigned int message_, uint32 arg0 = 0, uint32 arg1 = 0, uint32 arg2 = 0, uint32 arg3 = 0); |
| 57 | CDIF_Message(unsigned int message_, const std::string &str); |
| 58 | ~CDIF_Message(); |
| 59 | |
| 60 | unsigned int message; |
| 61 | uint32 args[4]; |
| 62 | void *parg; |
| 63 | std::string str_message; |
| 64 | }; |
| 65 | |
| 66 | class CDIF_Queue |
| 67 | { |
| 68 | public: |
| 69 | |
| 70 | CDIF_Queue(); |
| 71 | ~CDIF_Queue(); |
| 72 | |
| 73 | bool Read(CDIF_Message *message, bool blocking = TRUE); |
| 74 | |
| 75 | void Write(const CDIF_Message &message); |
| 76 | |
| 77 | private: |
| 78 | std::queue<CDIF_Message> ze_queue; |
| 79 | MDFN_Mutex *ze_mutex; |
| 80 | MDFN_Cond *ze_cond; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | typedef struct |
| 85 | { |
| 86 | bool valid; |
| 87 | bool error; |
| 88 | uint32 lba; |
| 89 | uint8 data[2352 + 96]; |
| 90 | } CDIF_Sector_Buffer; |
| 91 | |
| 92 | // TODO: prohibit copy constructor |
| 93 | class CDIF_MT : public CDIF |
| 94 | { |
| 95 | public: |
| 96 | |
| 97 | CDIF_MT(CDAccess *cda); |
| 98 | virtual ~CDIF_MT(); |
| 99 | |
| 100 | virtual void HintReadSector(uint32 lba); |
| 101 | virtual bool ReadRawSector(uint8 *buf, uint32 lba); |
| 102 | |
| 103 | // Return true if operation succeeded or it was a NOP(either due to not being implemented, or the current status matches eject_status). |
| 104 | // Returns false on failure(usually drive error of some kind; not completely fatal, can try again). |
| 105 | virtual bool Eject(bool eject_status); |
| 106 | |
| 107 | // FIXME: Semi-private: |
| 108 | int ReadThreadStart(void); |
| 109 | |
| 110 | private: |
| 111 | |
| 112 | CDAccess *disc_cdaccess; |
| 113 | |
| 114 | MDFN_Thread *CDReadThread; |
| 115 | |
| 116 | // Queue for messages to the read thread. |
| 117 | CDIF_Queue ReadThreadQueue; |
| 118 | |
| 119 | // Queue for messages to the emu thread. |
| 120 | CDIF_Queue EmuThreadQueue; |
| 121 | |
| 122 | |
| 123 | enum { SBSize = 256 }; |
| 124 | CDIF_Sector_Buffer SectorBuffers[SBSize]; |
| 125 | |
| 126 | uint32 SBWritePos; |
| 127 | |
| 128 | MDFN_Mutex *SBMutex; |
| 129 | MDFN_Cond *SBCond; |
| 130 | |
| 131 | |
| 132 | // |
| 133 | // Read-thread-only: |
| 134 | // |
| 135 | void RT_EjectDisc(bool eject_status, bool skip_actual_eject = false); |
| 136 | |
| 137 | uint32 ra_lba; |
| 138 | int ra_count; |
| 139 | uint32 last_read_lba; |
| 140 | }; |
| 141 | |
| 142 | |
| 143 | // TODO: prohibit copy constructor |
| 144 | class CDIF_ST : public CDIF |
| 145 | { |
| 146 | public: |
| 147 | |
| 148 | CDIF_ST(CDAccess *cda); |
| 149 | virtual ~CDIF_ST(); |
| 150 | |
| 151 | virtual void HintReadSector(uint32 lba); |
| 152 | virtual bool ReadRawSector(uint8 *buf, uint32 lba); |
| 153 | virtual bool Eject(bool eject_status); |
| 154 | |
| 155 | private: |
| 156 | CDAccess *disc_cdaccess; |
| 157 | }; |
| 158 | |
| 159 | CDIF::CDIF() : UnrecoverableError(false), is_phys_cache(false), DiscEjected(false) |
| 160 | { |
| 161 | |
| 162 | } |
| 163 | |
| 164 | CDIF::~CDIF() |
| 165 | { |
| 166 | |
| 167 | } |
| 168 | |
| 169 | |
| 170 | CDIF_Message::CDIF_Message() |
| 171 | { |
| 172 | message = 0; |
| 173 | |
| 174 | memset(args, 0, sizeof(args)); |
| 175 | } |
| 176 | |
| 177 | CDIF_Message::CDIF_Message(unsigned int message_, uint32 arg0, uint32 arg1, uint32 arg2, uint32 arg3) |
| 178 | { |
| 179 | message = message_; |
| 180 | args[0] = arg0; |
| 181 | args[1] = arg1; |
| 182 | args[2] = arg2; |
| 183 | args[3] = arg3; |
| 184 | } |
| 185 | |
| 186 | CDIF_Message::CDIF_Message(unsigned int message_, const std::string &str) |
| 187 | { |
| 188 | message = message_; |
| 189 | str_message = str; |
| 190 | } |
| 191 | |
| 192 | CDIF_Message::~CDIF_Message() |
| 193 | { |
| 194 | |
| 195 | } |
| 196 | |
| 197 | CDIF_Queue::CDIF_Queue() |
| 198 | { |
| 199 | ze_mutex = MDFND_CreateMutex(); |
| 200 | ze_cond = MDFND_CreateCond(); |
| 201 | } |
| 202 | |
| 203 | CDIF_Queue::~CDIF_Queue() |
| 204 | { |
| 205 | MDFND_DestroyMutex(ze_mutex); |
| 206 | MDFND_DestroyCond(ze_cond); |
| 207 | } |
| 208 | |
| 209 | // Returns FALSE if message not read, TRUE if it was read. Will always return TRUE if "blocking" is set. |
| 210 | // Will throw MDFN_Error if the read message code is CDIF_MSG_FATAL_ERROR |
| 211 | bool CDIF_Queue::Read(CDIF_Message *message, bool blocking) |
| 212 | { |
| 213 | bool ret = true; |
| 214 | |
| 215 | // |
| 216 | // |
| 217 | // |
| 218 | MDFND_LockMutex(ze_mutex); |
| 219 | |
| 220 | if(blocking) |
| 221 | { |
| 222 | while(ze_queue.size() == 0) // while, not just if. |
| 223 | MDFND_WaitCond(ze_cond, ze_mutex); |
| 224 | } |
| 225 | |
| 226 | if(ze_queue.size() == 0) |
| 227 | ret = false; |
| 228 | else |
| 229 | { |
| 230 | *message = ze_queue.front(); |
| 231 | ze_queue.pop(); |
| 232 | } |
| 233 | |
| 234 | MDFND_UnlockMutex(ze_mutex); |
| 235 | // |
| 236 | // |
| 237 | // |
| 238 | |
| 239 | if(ret && message->message == CDIF_MSG_FATAL_ERROR) |
| 240 | throw MDFN_Error(0, "%s", message->str_message.c_str()); |
| 241 | |
| 242 | return(ret); |
| 243 | } |
| 244 | |
| 245 | void CDIF_Queue::Write(const CDIF_Message &message) |
| 246 | { |
| 247 | MDFND_LockMutex(ze_mutex); |
| 248 | |
| 249 | ze_queue.push(message); |
| 250 | |
| 251 | MDFND_SignalCond(ze_cond); // Signal while the mutex is held to prevent icky race conditions |
| 252 | |
| 253 | MDFND_UnlockMutex(ze_mutex); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void CDIF_MT::RT_EjectDisc(bool eject_status, bool skip_actual_eject) |
| 258 | { |
| 259 | int32 old_de = DiscEjected; |
| 260 | |
| 261 | DiscEjected = eject_status; |
| 262 | |
| 263 | if(old_de != DiscEjected) |
| 264 | { |
| 265 | if(!skip_actual_eject) |
| 266 | disc_cdaccess->Eject(eject_status); |
| 267 | |
| 268 | if(!eject_status) // Re-read the TOC |
| 269 | { |
| 270 | disc_cdaccess->Read_TOC(&disc_toc); |
| 271 | |
| 272 | if(disc_toc.first_track < 1 || disc_toc.last_track > 99 || disc_toc.first_track > disc_toc.last_track) |
| 273 | { |
| 274 | throw(MDFN_Error(0, _("TOC first(%d)/last(%d) track numbers bad."), disc_toc.first_track, disc_toc.last_track)); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | SBWritePos = 0; |
| 279 | ra_lba = 0; |
| 280 | ra_count = 0; |
| 281 | last_read_lba = ~0U; |
| 282 | memset(SectorBuffers, 0, SBSize * sizeof(CDIF_Sector_Buffer)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | struct RTS_Args |
| 287 | { |
| 288 | CDIF_MT *cdif_ptr; |
| 289 | }; |
| 290 | |
| 291 | static int ReadThreadStart_C(void *v_arg) |
| 292 | { |
| 293 | RTS_Args *args = (RTS_Args *)v_arg; |
| 294 | |
| 295 | return args->cdif_ptr->ReadThreadStart(); |
| 296 | } |
| 297 | |
| 298 | int CDIF_MT::ReadThreadStart() |
| 299 | { |
| 300 | bool Running = TRUE; |
| 301 | |
| 302 | DiscEjected = true; |
| 303 | SBWritePos = 0; |
| 304 | ra_lba = 0; |
| 305 | ra_count = 0; |
| 306 | last_read_lba = ~0U; |
| 307 | |
| 308 | try |
| 309 | { |
| 310 | RT_EjectDisc(false, true); |
| 311 | } |
| 312 | catch(std::exception &e) |
| 313 | { |
| 314 | EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_FATAL_ERROR, std::string(e.what()))); |
| 315 | return(0); |
| 316 | } |
| 317 | |
| 318 | is_phys_cache = false; |
| 319 | |
| 320 | EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_DONE)); |
| 321 | |
| 322 | while(Running) |
| 323 | { |
| 324 | CDIF_Message msg; |
| 325 | |
| 326 | // Only do a blocking-wait for a message if we don't have any sectors to read-ahead. |
| 327 | // MDFN_DispMessage("%d %d %d\n", last_read_lba, ra_lba, ra_count); |
| 328 | if(ReadThreadQueue.Read(&msg, ra_count ? FALSE : TRUE)) |
| 329 | { |
| 330 | switch(msg.message) |
| 331 | { |
| 332 | case CDIF_MSG_DIEDIEDIE: |
| 333 | Running = FALSE; |
| 334 | break; |
| 335 | |
| 336 | case CDIF_MSG_EJECT: |
| 337 | try |
| 338 | { |
| 339 | RT_EjectDisc(msg.args[0]); |
| 340 | EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_DONE)); |
| 341 | } |
| 342 | catch(std::exception &e) |
| 343 | { |
| 344 | EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_FATAL_ERROR, std::string(e.what()))); |
| 345 | } |
| 346 | break; |
| 347 | |
| 348 | case CDIF_MSG_READ_SECTOR: |
| 349 | { |
| 350 | static const int max_ra = 16; |
| 351 | static const int initial_ra = 1; |
| 352 | static const int speedmult_ra = 2; |
| 353 | uint32 new_lba = msg.args[0]; |
| 354 | |
| 355 | assert((unsigned int)max_ra < (SBSize / 4)); |
| 356 | |
| 357 | if(last_read_lba != ~0U && new_lba == (last_read_lba + 1)) |
| 358 | { |
| 359 | int how_far_ahead = ra_lba - new_lba; |
| 360 | |
| 361 | if(how_far_ahead <= max_ra) |
| 362 | ra_count = std::min(speedmult_ra, 1 + max_ra - how_far_ahead); |
| 363 | else |
| 364 | ra_count++; |
| 365 | } |
| 366 | else if(new_lba != last_read_lba) |
| 367 | { |
| 368 | ra_lba = new_lba; |
| 369 | ra_count = initial_ra; |
| 370 | } |
| 371 | |
| 372 | last_read_lba = new_lba; |
| 373 | } |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // Don't read >= the "end" of the disc, silly snake. Slither. |
| 379 | if(ra_count && ra_lba == disc_toc.tracks[100].lba) |
| 380 | { |
| 381 | ra_count = 0; |
| 382 | //printf("Ephemeral scarabs: %d!\n", ra_lba); |
| 383 | } |
| 384 | |
| 385 | if(ra_count) |
| 386 | { |
| 387 | uint8 tmpbuf[2352 + 96]; |
| 388 | bool error_condition = false; |
| 389 | |
| 390 | try |
| 391 | { |
| 392 | disc_cdaccess->Read_Raw_Sector(tmpbuf, ra_lba); |
| 393 | } |
| 394 | catch(std::exception &e) |
| 395 | { |
| 396 | if (log_cb) |
| 397 | log_cb(RETRO_LOG_ERROR, "Sector %u read error: %s\n", ra_lba, e.what()); |
| 398 | memset(tmpbuf, 0, sizeof(tmpbuf)); |
| 399 | error_condition = true; |
| 400 | } |
| 401 | |
| 402 | MDFND_LockMutex(SBMutex); |
| 403 | |
| 404 | SectorBuffers[SBWritePos].lba = ra_lba; |
| 405 | memcpy(SectorBuffers[SBWritePos].data, tmpbuf, 2352 + 96); |
| 406 | SectorBuffers[SBWritePos].valid = TRUE; |
| 407 | SectorBuffers[SBWritePos].error = error_condition; |
| 408 | SBWritePos = (SBWritePos + 1) % SBSize; |
| 409 | |
| 410 | MDFND_SignalCond(SBCond); |
| 411 | |
| 412 | MDFND_UnlockMutex(SBMutex); |
| 413 | |
| 414 | ra_lba++; |
| 415 | ra_count--; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return(1); |
| 420 | } |
| 421 | |
| 422 | CDIF_MT::CDIF_MT(CDAccess *cda) : disc_cdaccess(cda), CDReadThread(NULL), SBMutex(NULL), SBCond(NULL) |
| 423 | { |
| 424 | try |
| 425 | { |
| 426 | CDIF_Message msg; |
| 427 | RTS_Args s; |
| 428 | |
| 429 | SBMutex = MDFND_CreateMutex(); |
| 430 | SBCond = MDFND_CreateCond(); |
| 431 | UnrecoverableError = false; |
| 432 | |
| 433 | s.cdif_ptr = this; |
| 434 | |
| 435 | CDReadThread = MDFND_CreateThread(ReadThreadStart_C, &s); |
| 436 | EmuThreadQueue.Read(&msg); |
| 437 | } |
| 438 | catch(...) |
| 439 | { |
| 440 | if(CDReadThread) |
| 441 | { |
| 442 | MDFND_WaitThread(CDReadThread, NULL); |
| 443 | CDReadThread = NULL; |
| 444 | } |
| 445 | |
| 446 | if(SBMutex) |
| 447 | { |
| 448 | MDFND_DestroyMutex(SBMutex); |
| 449 | SBMutex = NULL; |
| 450 | } |
| 451 | |
| 452 | if(SBCond) |
| 453 | { |
| 454 | MDFND_DestroyCond(SBCond); |
| 455 | SBCond = NULL; |
| 456 | } |
| 457 | |
| 458 | if(disc_cdaccess) |
| 459 | { |
| 460 | delete disc_cdaccess; |
| 461 | disc_cdaccess = NULL; |
| 462 | } |
| 463 | |
| 464 | throw; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | |
| 469 | CDIF_MT::~CDIF_MT() |
| 470 | { |
| 471 | bool thread_deaded_failed = false; |
| 472 | |
| 473 | try |
| 474 | { |
| 475 | ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_DIEDIEDIE)); |
| 476 | } |
| 477 | catch(std::exception &e) |
| 478 | { |
| 479 | if (log_cb) |
| 480 | log_cb(RETRO_LOG_ERROR, "%s.\n", e.what()); |
| 481 | thread_deaded_failed = true; |
| 482 | } |
| 483 | |
| 484 | if(!thread_deaded_failed) |
| 485 | MDFND_WaitThread(CDReadThread, NULL); |
| 486 | |
| 487 | if(SBMutex) |
| 488 | { |
| 489 | MDFND_DestroyMutex(SBMutex); |
| 490 | SBMutex = NULL; |
| 491 | } |
| 492 | |
| 493 | if(disc_cdaccess) |
| 494 | { |
| 495 | delete disc_cdaccess; |
| 496 | disc_cdaccess = NULL; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | bool CDIF::ValidateRawSector(uint8 *buf) |
| 501 | { |
| 502 | int mode = buf[12 + 3]; |
| 503 | |
| 504 | if(mode != 0x1 && mode != 0x2) |
| 505 | return(false); |
| 506 | |
| 507 | if(!edc_lec_check_and_correct(buf, mode == 2)) |
| 508 | return(false); |
| 509 | |
| 510 | return(true); |
| 511 | } |
| 512 | |
| 513 | bool CDIF_MT::ReadRawSector(uint8 *buf, uint32 lba) |
| 514 | { |
| 515 | bool found = FALSE; |
| 516 | bool error_condition = false; |
| 517 | |
| 518 | if(UnrecoverableError) |
| 519 | { |
| 520 | memset(buf, 0, 2352 + 96); |
| 521 | return(false); |
| 522 | } |
| 523 | |
| 524 | // This shouldn't happen, the emulated-system-specific CDROM emulation code should make sure the emulated program doesn't try |
| 525 | // to read past the last "real" sector of the disc. |
| 526 | if(lba >= disc_toc.tracks[100].lba) |
| 527 | { |
| 528 | printf("Attempt to read LBA %d, >= LBA %d\n", lba, disc_toc.tracks[100].lba); |
| 529 | return(FALSE); |
| 530 | } |
| 531 | |
| 532 | ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_READ_SECTOR, lba)); |
| 533 | |
| 534 | MDFND_LockMutex(SBMutex); |
| 535 | |
| 536 | do |
| 537 | { |
| 538 | |
| 539 | for(int i = 0; i < SBSize; i++) |
| 540 | { |
| 541 | if(SectorBuffers[i].valid && SectorBuffers[i].lba == lba) |
| 542 | { |
| 543 | error_condition = SectorBuffers[i].error; |
| 544 | memcpy(buf, SectorBuffers[i].data, 2352 + 96); |
| 545 | found = TRUE; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if(!found) |
| 550 | { |
| 551 | //int32 swt = MDFND_GetTime(); |
| 552 | MDFND_WaitCond(SBCond, SBMutex); |
| 553 | //printf("SB Waited: %d\n", MDFND_GetTime() - swt); |
| 554 | } |
| 555 | } while(!found); |
| 556 | |
| 557 | MDFND_UnlockMutex(SBMutex); |
| 558 | |
| 559 | return(!error_condition); |
| 560 | } |
| 561 | |
| 562 | void CDIF_MT::HintReadSector(uint32 lba) |
| 563 | { |
| 564 | if(UnrecoverableError) |
| 565 | return; |
| 566 | |
| 567 | ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_READ_SECTOR, lba)); |
| 568 | } |
| 569 | |
| 570 | int CDIF::ReadSector(uint8* pBuf, uint32 lba, uint32 nSectors) |
| 571 | { |
| 572 | int ret = 0; |
| 573 | |
| 574 | if(UnrecoverableError) |
| 575 | return(false); |
| 576 | |
| 577 | while(nSectors--) |
| 578 | { |
| 579 | uint8 tmpbuf[2352 + 96]; |
| 580 | |
| 581 | if(!ReadRawSector(tmpbuf, lba)) |
| 582 | { |
| 583 | puts("CDIF Raw Read error"); |
| 584 | return(FALSE); |
| 585 | } |
| 586 | |
| 587 | if(!ValidateRawSector(tmpbuf)) |
| 588 | { |
| 589 | if (log_cb) |
| 590 | { |
| 591 | log_cb(RETRO_LOG_ERROR, "Uncorrectable data at sector %d\n", lba); |
| 592 | log_cb(RETRO_LOG_ERROR, "Uncorrectable data at sector %d\n", lba); |
| 593 | } |
| 594 | return(false); |
| 595 | } |
| 596 | |
| 597 | const int mode = tmpbuf[12 + 3]; |
| 598 | |
| 599 | if(!ret) |
| 600 | ret = mode; |
| 601 | |
| 602 | if(mode == 1) |
| 603 | { |
| 604 | memcpy(pBuf, &tmpbuf[12 + 4], 2048); |
| 605 | } |
| 606 | else if(mode == 2) |
| 607 | { |
| 608 | memcpy(pBuf, &tmpbuf[12 + 4 + 8], 2048); |
| 609 | } |
| 610 | else |
| 611 | { |
| 612 | printf("CDIF_ReadSector() invalid sector type at LBA=%u\n", (unsigned int)lba); |
| 613 | return(false); |
| 614 | } |
| 615 | |
| 616 | pBuf += 2048; |
| 617 | lba++; |
| 618 | } |
| 619 | |
| 620 | return(ret); |
| 621 | } |
| 622 | |
| 623 | bool CDIF_MT::Eject(bool eject_status) |
| 624 | { |
| 625 | if(UnrecoverableError) |
| 626 | return(false); |
| 627 | |
| 628 | try |
| 629 | { |
| 630 | CDIF_Message msg; |
| 631 | |
| 632 | ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_EJECT, eject_status)); |
| 633 | EmuThreadQueue.Read(&msg); |
| 634 | } |
| 635 | catch(std::exception &e) |
| 636 | { |
| 637 | log_cb(RETRO_LOG_ERROR, "Error on eject/insert attempt: %s\n", e.what()); |
| 638 | return(false); |
| 639 | } |
| 640 | |
| 641 | return(true); |
| 642 | } |
| 643 | |
| 644 | // |
| 645 | // |
| 646 | // Single-threaded implementation follows. |
| 647 | // |
| 648 | // |
| 649 | |
| 650 | CDIF_ST::CDIF_ST(CDAccess *cda) : disc_cdaccess(cda) |
| 651 | { |
| 652 | //puts("***WARNING USING SINGLE-THREADED CD READER***"); |
| 653 | |
| 654 | is_phys_cache = false; |
| 655 | UnrecoverableError = false; |
| 656 | DiscEjected = false; |
| 657 | |
| 658 | disc_cdaccess->Read_TOC(&disc_toc); |
| 659 | |
| 660 | if(disc_toc.first_track < 1 || disc_toc.last_track > 99 || disc_toc.first_track > disc_toc.last_track) |
| 661 | { |
| 662 | throw(MDFN_Error(0, _("TOC first(%d)/last(%d) track numbers bad."), disc_toc.first_track, disc_toc.last_track)); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | CDIF_ST::~CDIF_ST() |
| 667 | { |
| 668 | if(disc_cdaccess) |
| 669 | { |
| 670 | delete disc_cdaccess; |
| 671 | disc_cdaccess = NULL; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | void CDIF_ST::HintReadSector(uint32 lba) |
| 676 | { |
| 677 | // TODO: disc_cdaccess seek hint? (probably not, would require asynchronousitycamel) |
| 678 | } |
| 679 | |
| 680 | bool CDIF_ST::ReadRawSector(uint8 *buf, uint32 lba) |
| 681 | { |
| 682 | if(UnrecoverableError) |
| 683 | { |
| 684 | memset(buf, 0, 2352 + 96); |
| 685 | return(false); |
| 686 | } |
| 687 | |
| 688 | try |
| 689 | { |
| 690 | disc_cdaccess->Read_Raw_Sector(buf, lba); |
| 691 | } |
| 692 | catch(std::exception &e) |
| 693 | { |
| 694 | if (log_cb) |
| 695 | log_cb(RETRO_LOG_ERROR, "Sector %u read error: %s\n", lba, e.what()); |
| 696 | memset(buf, 0, 2352 + 96); |
| 697 | return(false); |
| 698 | } |
| 699 | |
| 700 | return(true); |
| 701 | } |
| 702 | |
| 703 | bool CDIF_ST::Eject(bool eject_status) |
| 704 | { |
| 705 | if(UnrecoverableError) |
| 706 | return(false); |
| 707 | |
| 708 | try |
| 709 | { |
| 710 | int32 old_de = DiscEjected; |
| 711 | |
| 712 | DiscEjected = eject_status; |
| 713 | |
| 714 | if(old_de != DiscEjected) |
| 715 | { |
| 716 | disc_cdaccess->Eject(eject_status); |
| 717 | |
| 718 | if(!eject_status) // Re-read the TOC |
| 719 | { |
| 720 | disc_cdaccess->Read_TOC(&disc_toc); |
| 721 | |
| 722 | if(disc_toc.first_track < 1 || disc_toc.last_track > 99 || disc_toc.first_track > disc_toc.last_track) |
| 723 | { |
| 724 | throw(MDFN_Error(0, _("TOC first(%d)/last(%d) track numbers bad."), disc_toc.first_track, disc_toc.last_track)); |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | catch(std::exception &e) |
| 730 | { |
| 731 | if (log_cb) |
| 732 | log_cb(RETRO_LOG_ERROR, "%s\n", e.what()); |
| 733 | return(false); |
| 734 | } |
| 735 | |
| 736 | return(true); |
| 737 | } |
| 738 | |
| 739 | |
| 740 | class CDIF_Stream_Thing : public Stream |
| 741 | { |
| 742 | public: |
| 743 | |
| 744 | CDIF_Stream_Thing(CDIF *cdintf_arg, uint32 lba_arg, uint32 sector_count_arg); |
| 745 | ~CDIF_Stream_Thing(); |
| 746 | |
| 747 | virtual uint64 attributes(void); |
| 748 | virtual uint8 *map(void); |
| 749 | virtual void unmap(void); |
| 750 | |
| 751 | virtual uint64 read(void *data, uint64 count, bool error_on_eos = true); |
| 752 | virtual void write(const void *data, uint64 count); |
| 753 | |
| 754 | virtual void seek(int64 offset, int whence); |
| 755 | virtual int64 tell(void); |
| 756 | virtual int64 size(void); |
| 757 | virtual void close(void); |
| 758 | |
| 759 | private: |
| 760 | CDIF *cdintf; |
| 761 | const uint32 start_lba; |
| 762 | const uint32 sector_count; |
| 763 | int64 position; |
| 764 | }; |
| 765 | |
| 766 | CDIF_Stream_Thing::CDIF_Stream_Thing(CDIF *cdintf_arg, uint32 start_lba_arg, uint32 sector_count_arg) : cdintf(cdintf_arg), start_lba(start_lba_arg), sector_count(sector_count_arg) |
| 767 | { |
| 768 | |
| 769 | } |
| 770 | |
| 771 | CDIF_Stream_Thing::~CDIF_Stream_Thing() |
| 772 | { |
| 773 | |
| 774 | } |
| 775 | |
| 776 | uint64 CDIF_Stream_Thing::attributes(void) |
| 777 | { |
| 778 | return(ATTRIBUTE_READABLE | ATTRIBUTE_SEEKABLE); |
| 779 | } |
| 780 | |
| 781 | uint8 *CDIF_Stream_Thing::map(void) |
| 782 | { |
| 783 | return NULL; |
| 784 | } |
| 785 | |
| 786 | void CDIF_Stream_Thing::unmap(void) |
| 787 | { |
| 788 | |
| 789 | } |
| 790 | |
| 791 | uint64 CDIF_Stream_Thing::read(void *data, uint64 count, bool error_on_eos) |
| 792 | { |
| 793 | if(count > (((uint64)sector_count * 2048) - position)) |
| 794 | { |
| 795 | if(error_on_eos) |
| 796 | { |
| 797 | throw MDFN_Error(0, "EOF"); |
| 798 | } |
| 799 | |
| 800 | count = ((uint64)sector_count * 2048) - position; |
| 801 | } |
| 802 | |
| 803 | if(!count) |
| 804 | return(0); |
| 805 | |
| 806 | for(uint64 rp = position; rp < (position + count); rp = (rp &~ 2047) + 2048) |
| 807 | { |
| 808 | uint8 buf[2048]; |
| 809 | |
| 810 | if(!cdintf->ReadSector(buf, start_lba + (rp / 2048), 1)) |
| 811 | { |
| 812 | throw MDFN_Error(ErrnoHolder(EIO)); |
| 813 | } |
| 814 | |
| 815 | //::printf("Meow: %08llx -- %08llx\n", count, (rp - position) + std::min<uint64>(2048 - (rp & 2047), count - (rp - position))); |
| 816 | memcpy((uint8*)data + (rp - position), buf + (rp & 2047), std::min<uint64>(2048 - (rp & 2047), count - (rp - position))); |
| 817 | } |
| 818 | |
| 819 | position += count; |
| 820 | |
| 821 | return count; |
| 822 | } |
| 823 | |
| 824 | void CDIF_Stream_Thing::write(const void *data, uint64 count) |
| 825 | { |
| 826 | throw MDFN_Error(ErrnoHolder(EBADF)); |
| 827 | } |
| 828 | |
| 829 | void CDIF_Stream_Thing::seek(int64 offset, int whence) |
| 830 | { |
| 831 | int64 new_position; |
| 832 | |
| 833 | switch(whence) |
| 834 | { |
| 835 | default: |
| 836 | throw MDFN_Error(ErrnoHolder(EINVAL)); |
| 837 | break; |
| 838 | |
| 839 | case SEEK_SET: |
| 840 | new_position = offset; |
| 841 | break; |
| 842 | |
| 843 | case SEEK_CUR: |
| 844 | new_position = position + offset; |
| 845 | break; |
| 846 | |
| 847 | case SEEK_END: |
| 848 | new_position = ((int64)sector_count * 2048) + offset; |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | if(new_position < 0 || new_position > ((int64)sector_count * 2048)) |
| 853 | throw MDFN_Error(ErrnoHolder(EINVAL)); |
| 854 | |
| 855 | position = new_position; |
| 856 | } |
| 857 | |
| 858 | int64 CDIF_Stream_Thing::tell(void) |
| 859 | { |
| 860 | return position; |
| 861 | } |
| 862 | |
| 863 | int64 CDIF_Stream_Thing::size(void) |
| 864 | { |
| 865 | return(sector_count * 2048); |
| 866 | } |
| 867 | |
| 868 | void CDIF_Stream_Thing::close(void) |
| 869 | { |
| 870 | |
| 871 | } |
| 872 | |
| 873 | |
| 874 | Stream *CDIF::MakeStream(uint32 lba, uint32 sector_count) |
| 875 | { |
| 876 | return new CDIF_Stream_Thing(this, lba, sector_count); |
| 877 | } |
| 878 | |
| 879 | |
| 880 | CDIF *CDIF_Open(const char *path, const bool is_device, bool image_memcache) |
| 881 | { |
| 882 | CDAccess *cda = cdaccess_open_image(path, image_memcache); |
| 883 | |
| 884 | if(!image_memcache) |
| 885 | return new CDIF_MT(cda); |
| 886 | else |
| 887 | return new CDIF_ST(cda); |
| 888 | } |