| 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 <math.h> |
| 20 | #include "pcecd_drive.h" |
| 21 | #include "../cdrom/cdromif.h" |
| 22 | #include "../cdrom/SimpleFIFO.h" |
| 23 | #include "../msvc_compat.h" |
| 24 | |
| 25 | static inline void SCSIDBG(const char *format, ...) |
| 26 | { |
| 27 | //printf("SCSI: " format "\n", ## __VA_ARGS__); |
| 28 | } |
| 29 | //#define SCSIDBG(format, ...) { } |
| 30 | |
| 31 | using namespace CDUtility; |
| 32 | |
| 33 | static uint32 CD_DATA_TRANSFER_RATE; |
| 34 | static uint32 System_Clock; |
| 35 | static void (*CDIRQCallback)(int); |
| 36 | static void (*CDStuffSubchannels)(uint8, int); |
| 37 | static Blip_Buffer *sbuf[2]; |
| 38 | static CDIF *Cur_CDIF; |
| 39 | |
| 40 | // Internal operation to the SCSI CD unit. Only pass 1 or 0 to these macros! |
| 41 | #define SetIOP(mask, set) { cd_bus.signals &= ~mask; if(set) cd_bus.signals |= mask; } |
| 42 | |
| 43 | #define SetBSY(set) SetIOP(PCECD_Drive_BSY_mask, set) |
| 44 | #define SetIO(set) SetIOP(PCECD_Drive_IO_mask, set) |
| 45 | #define SetCD(set) SetIOP(PCECD_Drive_CD_mask, set) |
| 46 | #define SetMSG(set) SetIOP(PCECD_Drive_MSG_mask, set) |
| 47 | |
| 48 | static INLINE void SetREQ(bool set) |
| 49 | { |
| 50 | if(set && !REQ_signal) |
| 51 | CDIRQCallback(PCECD_Drive_IRQ_MAGICAL_REQ); |
| 52 | |
| 53 | SetIOP(PCECD_Drive_REQ_mask, set); |
| 54 | } |
| 55 | |
| 56 | #define SetkingACK(set) SetIOP(PCECD_Drive_kingACK_mask, set) |
| 57 | #define SetkingRST(set) SetIOP(PCECD_Drive_kingRST_mask, set) |
| 58 | #define SetkingSEL(set) SetIOP(PCECD_Drive_kingSEL_mask, set) |
| 59 | |
| 60 | |
| 61 | enum |
| 62 | { |
| 63 | QMode_Zero = 0, |
| 64 | QMode_Time = 1, |
| 65 | QMode_MCN = 2, // Media Catalog Number |
| 66 | QMode_ISRC = 3 // International Standard Recording Code |
| 67 | }; |
| 68 | |
| 69 | typedef struct |
| 70 | { |
| 71 | bool last_RST_signal; |
| 72 | |
| 73 | // The pending message to send(in the message phase) |
| 74 | uint8 message_pending; |
| 75 | |
| 76 | bool status_sent, message_sent; |
| 77 | |
| 78 | // Pending error codes |
| 79 | uint8 key_pending, asc_pending, ascq_pending, fru_pending; |
| 80 | |
| 81 | uint8 command_buffer[256]; |
| 82 | uint8 command_buffer_pos; |
| 83 | uint8 command_size_left; |
| 84 | |
| 85 | // FALSE if not all pending data is in the FIFO, TRUE if it is. |
| 86 | // Used for multiple sector CD reads. |
| 87 | bool data_transfer_done; |
| 88 | |
| 89 | bool TrayOpen; |
| 90 | bool DiscChanged; |
| 91 | |
| 92 | uint8 SubQBuf[4][0xC]; // One for each of the 4 most recent q-Modes. |
| 93 | uint8 SubQBuf_Last[0xC]; // The most recent q subchannel data, regardless of q-mode. |
| 94 | |
| 95 | uint8 SubPWBuf[96]; |
| 96 | |
| 97 | } pcecd_drive_t; |
| 98 | |
| 99 | typedef Blip_Synth < blip_good_quality, 1> CDSynth; |
| 100 | |
| 101 | enum |
| 102 | { |
| 103 | CDDASTATUS_PAUSED = -1, |
| 104 | CDDASTATUS_STOPPED = 0, |
| 105 | CDDASTATUS_PLAYING = 1, |
| 106 | }; |
| 107 | |
| 108 | enum |
| 109 | { |
| 110 | PLAYMODE_SILENT = 0x00, |
| 111 | PLAYMODE_NORMAL, |
| 112 | PLAYMODE_INTERRUPT, |
| 113 | PLAYMODE_LOOP, |
| 114 | }; |
| 115 | |
| 116 | typedef struct |
| 117 | { |
| 118 | int32 CDDADivAcc; |
| 119 | uint32 scan_sec_end; |
| 120 | |
| 121 | uint8 PlayMode; |
| 122 | CDSynth CDDASynth; |
| 123 | int32 CDDAVolume; |
| 124 | int16 last_sample[2]; |
| 125 | int16 CDDASectorBuffer[1176]; |
| 126 | uint32 CDDAReadPos; |
| 127 | |
| 128 | int8 CDDAStatus; |
| 129 | uint8 ScanMode; |
| 130 | int32 CDDADiv; |
| 131 | int CDDATimeDiv; |
| 132 | } cdda_t; |
| 133 | |
| 134 | static INLINE void MakeSense(uint8 target[18], uint8 key, uint8 asc, uint8 ascq, uint8 fru) |
| 135 | { |
| 136 | memset(target, 0, 18); |
| 137 | |
| 138 | target[0] = 0x70; // Current errors and sense data is not SCSI compliant |
| 139 | target[2] = key; |
| 140 | target[7] = 0x0A; |
| 141 | target[12] = asc; // Additional Sense Code |
| 142 | target[13] = ascq; // Additional Sense Code Qualifier |
| 143 | target[14] = fru; // Field Replaceable Unit code |
| 144 | } |
| 145 | |
| 146 | static void (*SCSILog)(const char *, const char *format, ...); |
| 147 | |
| 148 | static pcecd_drive_timestamp_t lastts; |
| 149 | static int64 monotonic_timestamp; |
| 150 | static int64 pce_lastsapsp_timestamp; |
| 151 | |
| 152 | static pcecd_drive_t cd; |
| 153 | pcecd_drive_bus_t cd_bus; |
| 154 | static cdda_t cdda; |
| 155 | |
| 156 | static SimpleFIFO<uint8> din(2048); |
| 157 | |
| 158 | static CDUtility::TOC toc; |
| 159 | |
| 160 | static uint32 read_sec_start; |
| 161 | static uint32 read_sec; |
| 162 | static uint32 read_sec_end; |
| 163 | |
| 164 | static int32 CDReadTimer; |
| 165 | static uint32 SectorAddr; |
| 166 | static uint32 SectorCount; |
| 167 | |
| 168 | |
| 169 | enum |
| 170 | { |
| 171 | PHASE_BUS_FREE = 0, |
| 172 | PHASE_COMMAND, |
| 173 | PHASE_DATA_IN, |
| 174 | PHASE_STATUS, |
| 175 | PHASE_MESSAGE_IN, |
| 176 | }; |
| 177 | |
| 178 | static unsigned int CurrentPhase; |
| 179 | static void ChangePhase(const unsigned int new_phase); |
| 180 | |
| 181 | static void VirtualReset(void) |
| 182 | { |
| 183 | din.Flush(); |
| 184 | |
| 185 | cdda.CDDADivAcc = (int64)System_Clock * 65536 / 44100; |
| 186 | CDReadTimer = 0; |
| 187 | |
| 188 | pce_lastsapsp_timestamp = monotonic_timestamp; |
| 189 | |
| 190 | SectorAddr = SectorCount = 0; |
| 191 | read_sec_start = read_sec = 0; |
| 192 | read_sec_end = ~0; |
| 193 | |
| 194 | cdda.PlayMode = PLAYMODE_SILENT; |
| 195 | cdda.CDDAReadPos = 0; |
| 196 | cdda.CDDAStatus = CDDASTATUS_STOPPED; |
| 197 | cdda.CDDADiv = 0; |
| 198 | |
| 199 | cdda.ScanMode = 0; |
| 200 | cdda.scan_sec_end = 0; |
| 201 | |
| 202 | ChangePhase(PHASE_BUS_FREE); |
| 203 | } |
| 204 | |
| 205 | void PCECD_Drive_Power(pcecd_drive_timestamp_t system_timestamp) |
| 206 | { |
| 207 | memset(&cd, 0, sizeof(pcecd_drive_t)); |
| 208 | memset(&cd_bus, 0, sizeof(pcecd_drive_bus_t)); |
| 209 | |
| 210 | monotonic_timestamp = system_timestamp; |
| 211 | |
| 212 | cd.DiscChanged = false; |
| 213 | |
| 214 | if(Cur_CDIF && !cd.TrayOpen) |
| 215 | Cur_CDIF->ReadTOC(&toc); |
| 216 | |
| 217 | CurrentPhase = PHASE_BUS_FREE; |
| 218 | |
| 219 | VirtualReset(); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | void PCECD_Drive_SetDB(uint8 data) |
| 224 | { |
| 225 | cd_bus.DB = data; |
| 226 | //printf("Set DB: %02x\n", data); |
| 227 | } |
| 228 | |
| 229 | void PCECD_Drive_SetACK(bool set) |
| 230 | { |
| 231 | SetkingACK(set); |
| 232 | //printf("Set ACK: %d\n", set); |
| 233 | } |
| 234 | |
| 235 | void PCECD_Drive_SetSEL(bool set) |
| 236 | { |
| 237 | SetkingSEL(set); |
| 238 | //printf("Set SEL: %d\n", set); |
| 239 | } |
| 240 | |
| 241 | void PCECD_Drive_SetRST(bool set) |
| 242 | { |
| 243 | SetkingRST(set); |
| 244 | //printf("Set RST: %d\n", set); |
| 245 | } |
| 246 | |
| 247 | static void GenSubQFromSubPW(void) |
| 248 | { |
| 249 | uint8 SubQBuf[0xC]; |
| 250 | |
| 251 | subq_deinterleave(cd.SubPWBuf, SubQBuf); |
| 252 | |
| 253 | //printf("Real %d/ SubQ %d - ", read_sec, BCD_to_U8(SubQBuf[7]) * 75 * 60 + BCD_to_U8(SubQBuf[8]) * 75 + BCD_to_U8(SubQBuf[9]) - 150); |
| 254 | // Debug code, remove me. |
| 255 | //for(int i = 0; i < 0xC; i++) |
| 256 | // printf("%02x ", SubQBuf[i]); |
| 257 | //printf("\n"); |
| 258 | |
| 259 | if(!subq_check_checksum(SubQBuf)) |
| 260 | { |
| 261 | SCSIDBG("SubQ checksum error!"); |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | memcpy(cd.SubQBuf_Last, SubQBuf, 0xC); |
| 266 | |
| 267 | uint8 adr = SubQBuf[0] & 0xF; |
| 268 | |
| 269 | if(adr <= 0x3) |
| 270 | memcpy(cd.SubQBuf[adr], SubQBuf, 0xC); |
| 271 | |
| 272 | //if(adr == 0x02) |
| 273 | //for(int i = 0; i < 12; i++) |
| 274 | // printf("%02x\n", cd.SubQBuf[0x2][i]); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
| 279 | #define STATUS_GOOD 0 |
| 280 | #define STATUS_CHECK_CONDITION 1 |
| 281 | |
| 282 | #define SENSEKEY_NO_SENSE 0x0 |
| 283 | #define SENSEKEY_NOT_READY 0x2 |
| 284 | #define SENSEKEY_MEDIUM_ERROR 0x3 |
| 285 | #define SENSEKEY_HARDWARE_ERROR 0x4 |
| 286 | #define SENSEKEY_ILLEGAL_REQUEST 0x5 |
| 287 | #define SENSEKEY_UNIT_ATTENTION 0x6 |
| 288 | #define SENSEKEY_ABORTED_COMMAND 0xB |
| 289 | |
| 290 | #define ASC_MEDIUM_NOT_PRESENT 0x3A |
| 291 | |
| 292 | |
| 293 | // NEC sub-errors(ASC), no ASCQ. |
| 294 | #define NSE_NO_DISC 0x0B // Used with SENSEKEY_NOT_READY - This condition occurs when tray is closed with no disc present. |
| 295 | #define NSE_TRAY_OPEN 0x0D // Used with SENSEKEY_NOT_READY |
| 296 | #define NSE_SEEK_ERROR 0x15 |
| 297 | #define NSE_HEADER_READ_ERROR 0x16 // Used with SENSEKEY_MEDIUM_ERROR |
| 298 | #define NSE_NOT_AUDIO_TRACK 0x1C // Used with SENSEKEY_MEDIUM_ERROR |
| 299 | #define NSE_NOT_DATA_TRACK 0x1D // Used with SENSEKEY_MEDIUM_ERROR |
| 300 | #define NSE_INVALID_COMMAND 0x20 |
| 301 | #define NSE_INVALID_ADDRESS 0x21 |
| 302 | #define NSE_INVALID_PARAMETER 0x22 |
| 303 | #define NSE_END_OF_VOLUME 0x25 |
| 304 | #define NSE_INVALID_REQUEST_IN_CDB 0x27 |
| 305 | #define NSE_DISC_CHANGED 0x28 // Used with SENSEKEY_UNIT_ATTENTION |
| 306 | #define NSE_AUDIO_NOT_PLAYING 0x2C |
| 307 | |
| 308 | // ASC, ASCQ pair |
| 309 | #define AP_UNRECOVERED_READ_ERROR 0x11, 0x00 |
| 310 | #define AP_LEC_UNCORRECTABLE_ERROR 0x11, 0x05 |
| 311 | #define AP_CIRC_UNRECOVERED_ERROR 0x11, 0x06 |
| 312 | |
| 313 | #define AP_UNKNOWN_MEDIUM_FORMAT 0x30, 0x01 |
| 314 | #define AP_INCOMPAT_MEDIUM_FORMAT 0x30, 0x02 |
| 315 | |
| 316 | static void ChangePhase(const unsigned int new_phase) |
| 317 | { |
| 318 | //printf("New phase: %d %lld\n", new_phase, monotonic_timestamp); |
| 319 | switch(new_phase) |
| 320 | { |
| 321 | case PHASE_BUS_FREE: |
| 322 | SetBSY(false); |
| 323 | SetMSG(false); |
| 324 | SetCD(false); |
| 325 | SetIO(false); |
| 326 | SetREQ(false); |
| 327 | |
| 328 | CDIRQCallback(0x8000 | PCECD_Drive_IRQ_DATA_TRANSFER_DONE); |
| 329 | break; |
| 330 | |
| 331 | case PHASE_DATA_IN: // Us to them |
| 332 | SetBSY(true); |
| 333 | SetMSG(false); |
| 334 | SetCD(false); |
| 335 | SetIO(true); |
| 336 | //SetREQ(true); |
| 337 | SetREQ(false); |
| 338 | break; |
| 339 | |
| 340 | case PHASE_STATUS: // Us to them |
| 341 | SetBSY(true); |
| 342 | SetMSG(false); |
| 343 | SetCD(true); |
| 344 | SetIO(true); |
| 345 | SetREQ(true); |
| 346 | break; |
| 347 | |
| 348 | case PHASE_MESSAGE_IN: // Us to them |
| 349 | SetBSY(true); |
| 350 | SetMSG(true); |
| 351 | SetCD(true); |
| 352 | SetIO(true); |
| 353 | SetREQ(true); |
| 354 | break; |
| 355 | |
| 356 | case PHASE_COMMAND: // Them to us |
| 357 | SetBSY(true); |
| 358 | SetMSG(false); |
| 359 | SetCD(true); |
| 360 | SetIO(false); |
| 361 | SetREQ(true); |
| 362 | break; |
| 363 | } |
| 364 | CurrentPhase = new_phase; |
| 365 | } |
| 366 | |
| 367 | static void SendStatusAndMessage(uint8 status, uint8 message) |
| 368 | { |
| 369 | // This should never ever happen, but that doesn't mean it won't. ;) |
| 370 | if(din.CanRead()) |
| 371 | { |
| 372 | printf("BUG: %d bytes still in SCSI CD FIFO\n", din.CanRead()); |
| 373 | din.Flush(); |
| 374 | } |
| 375 | |
| 376 | cd.message_pending = message; |
| 377 | |
| 378 | cd.status_sent = FALSE; |
| 379 | cd.message_sent = FALSE; |
| 380 | |
| 381 | |
| 382 | if(status == STATUS_GOOD) |
| 383 | cd_bus.DB = 0x00; |
| 384 | else |
| 385 | cd_bus.DB = 0x01; |
| 386 | |
| 387 | |
| 388 | ChangePhase(PHASE_STATUS); |
| 389 | } |
| 390 | |
| 391 | static void DoSimpleDataIn(const uint8 *data_in, uint32 len) |
| 392 | { |
| 393 | din.Write(data_in, len); |
| 394 | |
| 395 | cd.data_transfer_done = true; |
| 396 | |
| 397 | ChangePhase(PHASE_DATA_IN); |
| 398 | } |
| 399 | |
| 400 | void PCECD_Drive_SetDisc(bool tray_open, CDIF *cdif, bool no_emu_side_effects) |
| 401 | { |
| 402 | Cur_CDIF = cdif; |
| 403 | |
| 404 | // Closing the tray. |
| 405 | if(cd.TrayOpen && !tray_open) |
| 406 | { |
| 407 | cd.TrayOpen = false; |
| 408 | |
| 409 | if(cdif) |
| 410 | { |
| 411 | cdif->ReadTOC(&toc); |
| 412 | |
| 413 | if(!no_emu_side_effects) |
| 414 | { |
| 415 | memset(cd.SubQBuf, 0, sizeof(cd.SubQBuf)); |
| 416 | memset(cd.SubQBuf_Last, 0, sizeof(cd.SubQBuf_Last)); |
| 417 | cd.DiscChanged = true; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | else if(!cd.TrayOpen && tray_open) // Opening the tray |
| 422 | { |
| 423 | cd.TrayOpen = true; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | static void CommandCCError(int key, int asc = 0, int ascq = 0) |
| 428 | { |
| 429 | printf("CC Error: %02x %02x %02x\n", key, asc, ascq); |
| 430 | |
| 431 | cd.key_pending = key; |
| 432 | cd.asc_pending = asc; |
| 433 | cd.ascq_pending = ascq; |
| 434 | cd.fru_pending = 0x00; |
| 435 | |
| 436 | SendStatusAndMessage(STATUS_CHECK_CONDITION, 0x00); |
| 437 | } |
| 438 | |
| 439 | static bool ValidateRawDataSector(uint8 *data, const uint32 lba) |
| 440 | { |
| 441 | if(!edc_lec_check_and_correct(data, false)) |
| 442 | { |
| 443 | MDFN_DispMessage(_("Uncorrectable data at sector %u"), lba); |
| 444 | MDFN_PrintError(_("Uncorrectable data at sector %u"), lba); |
| 445 | |
| 446 | din.Flush(); |
| 447 | cd.data_transfer_done = false; |
| 448 | |
| 449 | CommandCCError(SENSEKEY_MEDIUM_ERROR, AP_LEC_UNCORRECTABLE_ERROR); |
| 450 | return(false); |
| 451 | } |
| 452 | |
| 453 | return(true); |
| 454 | } |
| 455 | |
| 456 | static void DoREADBase(uint32 sa, uint32 sc) |
| 457 | { |
| 458 | if(sa > toc.tracks[100].lba) // Another one of those off-by-one PC-FX CD bugs. |
| 459 | { |
| 460 | CommandCCError(SENSEKEY_ILLEGAL_REQUEST, NSE_END_OF_VOLUME); |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | // Case for READ(10) and READ(12) where sc == 0, and sa == toc.tracks[100].lba |
| 465 | if(!sc && sa == toc.tracks[100].lba) |
| 466 | { |
| 467 | CommandCCError(SENSEKEY_MEDIUM_ERROR, NSE_HEADER_READ_ERROR); |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | if(SCSILog) |
| 472 | { |
| 473 | int Track = toc.FindTrackByLBA(sa); |
| 474 | uint32 Offset = sa - toc.tracks[Track].lba; //Cur_CDIF->GetTrackStartPositionLBA(Track); |
| 475 | SCSILog("SCSI", "Read: start=0x%08x(track=%d, offs=0x%08x), cnt=0x%08x", sa, Track, Offset, sc); |
| 476 | } |
| 477 | |
| 478 | SectorAddr = sa; |
| 479 | SectorCount = sc; |
| 480 | if(SectorCount) |
| 481 | { |
| 482 | Cur_CDIF->HintReadSector(sa); //, sa + sc); |
| 483 | |
| 484 | CDReadTimer = (uint64)3 * 2048 * System_Clock / CD_DATA_TRANSFER_RATE; |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | CDReadTimer = 0; |
| 489 | SendStatusAndMessage(STATUS_GOOD, 0x00); |
| 490 | } |
| 491 | cdda.CDDAStatus = CDDASTATUS_STOPPED; |
| 492 | } |
| 493 | |
| 494 | // |
| 495 | // |
| 496 | // |
| 497 | static void DoTESTUNITREADY(const uint8 *cdb) |
| 498 | { |
| 499 | SendStatusAndMessage(STATUS_GOOD, 0x00); |
| 500 | } |
| 501 | |
| 502 | static void DoREQUESTSENSE(const uint8 *cdb) |
| 503 | { |
| 504 | uint8 data_in[18]; |
| 505 | |
| 506 | MakeSense(data_in, cd.key_pending, cd.asc_pending, cd.ascq_pending, cd.fru_pending); |
| 507 | |
| 508 | DoSimpleDataIn(data_in, 18); |
| 509 | |
| 510 | cd.key_pending = 0; |
| 511 | cd.asc_pending = 0; |
| 512 | cd.ascq_pending = 0; |
| 513 | cd.fru_pending = 0; |
| 514 | } |
| 515 | |
| 516 | /******************************************************** |
| 517 | * * |
| 518 | * SCSI Command 0x08 - READ(6) * |
| 519 | * * |
| 520 | ********************************************************/ |
| 521 | static void DoREAD6(const uint8 *cdb) |
| 522 | { |
| 523 | uint32 sa = ((cdb[1] & 0x1F) << 16) | (cdb[2] << 8) | (cdb[3] << 0); |
| 524 | uint32 sc = cdb[4]; |
| 525 | |
| 526 | // TODO: confirm real PCE does this(PC-FX does at least). |
| 527 | if(!sc) |
| 528 | { |
| 529 | SCSIDBG("READ(6) with count == 0.\n"); |
| 530 | sc = 256; |
| 531 | } |
| 532 | |
| 533 | DoREADBase(sa, sc); |
| 534 | } |
| 535 | |
| 536 | /******************************************************** |
| 537 | * * |
| 538 | * PC Engine CD Command 0xD8 - SAPSP * |
| 539 | * * |
| 540 | ********************************************************/ |
| 541 | static void DoNEC_PCE_SAPSP(const uint8 *cdb) |
| 542 | { |
| 543 | uint32 new_read_sec_start; |
| 544 | |
| 545 | //printf("Set audio start: %02x %02x %02x %02x %02x %02x %02x\n", cdb[9], cdb[1], cdb[2], cdb[3], cdb[4], cdb[5], cdb[6]); |
| 546 | switch (cdb[9] & 0xc0) |
| 547 | { |
| 548 | default: SCSIDBG("Unknown SAPSP 9: %02x\n", cdb[9]); |
| 549 | case 0x00: |
| 550 | new_read_sec_start = (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; |
| 551 | break; |
| 552 | |
| 553 | case 0x40: |
| 554 | new_read_sec_start = AMSF_to_LBA(BCD_to_U8(cdb[2]), BCD_to_U8(cdb[3]), BCD_to_U8(cdb[4])); |
| 555 | break; |
| 556 | |
| 557 | case 0x80: |
| 558 | { |
| 559 | int track = BCD_to_U8(cdb[2]); |
| 560 | |
| 561 | if(!track) |
| 562 | track = 1; |
| 563 | else if(track >= toc.last_track + 1) |
| 564 | track = 100; |
| 565 | new_read_sec_start = toc.tracks[track].lba; |
| 566 | } |
| 567 | break; |
| 568 | } |
| 569 | |
| 570 | //printf("%lld\n", (long long)(monotonic_timestamp - pce_lastsapsp_timestamp) * 1000 / System_Clock); |
| 571 | if(cdda.CDDAStatus == CDDASTATUS_PLAYING && new_read_sec_start == read_sec_start && ((int64)(monotonic_timestamp - pce_lastsapsp_timestamp) * 1000 / System_Clock) < 190) |
| 572 | { |
| 573 | pce_lastsapsp_timestamp = monotonic_timestamp; |
| 574 | |
| 575 | SendStatusAndMessage(STATUS_GOOD, 0x00); |
| 576 | CDIRQCallback(PCECD_Drive_IRQ_DATA_TRANSFER_DONE); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | pce_lastsapsp_timestamp = monotonic_timestamp; |
| 581 | |
| 582 | read_sec = read_sec_start = new_read_sec_start; |
| 583 | read_sec_end = toc.tracks[100].lba; |
| 584 | |
| 585 | |
| 586 | cdda.CDDAReadPos = 588; |
| 587 | |
| 588 | cdda.CDDAStatus = CDDASTATUS_PAUSED; |
| 589 | cdda.PlayMode = PLAYMODE_SILENT; |
| 590 | |
| 591 | if(cdb[1]) |
| 592 | { |
| 593 | cdda.PlayMode = PLAYMODE_NORMAL; |
| 594 | cdda.CDDAStatus = CDDASTATUS_PLAYING; |
| 595 | } |
| 596 | |
| 597 | if(read_sec < toc.tracks[100].lba) |
| 598 | Cur_CDIF->HintReadSector(read_sec); |
| 599 | |
| 600 | SendStatusAndMessage(STATUS_GOOD, 0x00); |
| 601 | CDIRQCallback(PCECD_Drive_IRQ_DATA_TRANSFER_DONE); |
| 602 | } |
| 603 | |
| 604 | |
| 605 | |
| 606 | /******************************************************** |
| 607 | * * |
| 608 | * PC Engine CD Command 0xD9 - SAPEP * |
| 609 | * * |
| 610 | ********************************************************/ |
| 611 | static void DoNEC_PCE_SAPEP(const uint8 *cdb) |
| 612 | { |
| 613 | uint32 new_read_sec_end; |
| 614 | |
| 615 | //printf("Set audio end: %02x %02x %02x %02x %02x %02x %02x\n", cdb[9], cdb[1], cdb[2], cdb[3], cdb[4], cdb[5], cdb[6]); |
| 616 | |
| 617 | switch (cdb[9] & 0xc0) |
| 618 | { |
| 619 | default: SCSIDBG("Unknown SAPEP 9: %02x\n", cdb[9]); |
| 620 | |
| 621 | case 0x00: |
| 622 | new_read_sec_end = (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; |
| 623 | break; |
| 624 | |
| 625 | case 0x40: |
| 626 | new_read_sec_end = BCD_to_U8(cdb[4]) + 75 * (BCD_to_U8(cdb[3]) + 60 * BCD_to_U8(cdb[2])); |
| 627 | new_read_sec_end -= 150; |
| 628 | break; |
| 629 | |
| 630 | case 0x80: |
| 631 | { |
| 632 | int track = BCD_to_U8(cdb[2]); |
| 633 | |
| 634 | if(!track) |
| 635 | track = 1; |
| 636 | else if(track >= toc.last_track + 1) |
| 637 | track = 100; |
| 638 | new_read_sec_end = toc.tracks[track].lba; |
| 639 | } |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | read_sec_end = new_read_sec_end; |
| 644 | |
| 645 | switch(cdb[1]) // PCE CD(TODO: Confirm these, and check the mode mask): |
| 646 | { |
| 647 | default: |
| 648 | case 0x03: cdda.PlayMode = PLAYMODE_NORMAL; |
| 649 | cdda.CDDAStatus = CDDASTATUS_PLAYING; |
| 650 | break; |
| 651 | |
| 652 | case 0x02: cdda.PlayMode = PLAYMODE_INTERRUPT; |
| 653 | cdda.CDDAStatus = CDDASTATUS_PLAYING; |
| 654 | break; |
| 655 | |
| 656 | case 0x01: cdda.PlayMode = PLAYMODE_LOOP; |
| 657 | cdda.CDDAStatus = CDDASTATUS_PLAYING; |
| 658 | break; |
| 659 | |
| 660 | case 0x00: cdda.PlayMode = PLAYMODE_SILENT; |
| 661 | cdda.CDDAStatus = CDDASTATUS_STOPPED; |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | SendStatusAndMessage(STATUS_GOOD, 0x00); |
| 666 | } |
| 667 | |
| 668 | |
| 669 | |
| 670 | /******************************************************** |
| 671 | * * |
| 672 | * PC Engine CD Command 0xDA - Pause * |
| 673 | * * |
| 674 | ********************************************************/ |
| 675 | static void DoNEC_PCE_PAUSE(const uint8 *cdb) |
| 676 | { |
| 677 | if(cdda.CDDAStatus != CDDASTATUS_STOPPED) // Hmm, should we give an error if it tries to pause and it's already paused? |
| 678 | { |
| 679 | cdda.CDDAStatus = CDDASTATUS_PAUSED; |
| 680 | SendStatusAndMessage(STATUS_GOOD, 0x00); |
| 681 | } |
| 682 | else // Definitely give an error if it tries to pause when no track is playing! |
| 683 | { |
| 684 | CommandCCError(SENSEKEY_ILLEGAL_REQUEST, NSE_AUDIO_NOT_PLAYING); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | |
| 689 | |
| 690 | /******************************************************** |
| 691 | * * |
| 692 | * PC Engine CD Command 0xDD - Read Subchannel Q * |
| 693 | * * |
| 694 | ********************************************************/ |
| 695 | static void DoNEC_PCE_READSUBQ(const uint8 *cdb) |
| 696 | { |
| 697 | uint8 *SubQBuf = cd.SubQBuf[QMode_Time]; |
| 698 | uint8 data_in[8192]; |
| 699 | |
| 700 | memset(data_in, 0x00, 10); |
| 701 | |
| 702 | data_in[2] = SubQBuf[1]; // Track |
| 703 | data_in[3] = SubQBuf[2]; // Index |
| 704 | data_in[4] = SubQBuf[3]; // M(rel) |
| 705 | data_in[5] = SubQBuf[4]; // S(rel) |
| 706 | data_in[6] = SubQBuf[5]; // F(rel) |
| 707 | data_in[7] = SubQBuf[7]; // M(abs) |
| 708 | data_in[8] = SubQBuf[8]; // S(abs) |
| 709 | data_in[9] = SubQBuf[9]; // F(abs) |
| 710 | |
| 711 | if(cdda.CDDAStatus == CDDASTATUS_PAUSED) |
| 712 | data_in[0] = 2; // Pause |
| 713 | else if(cdda.CDDAStatus == CDDASTATUS_PLAYING) |
| 714 | data_in[0] = 0; // Playing |
| 715 | else |
| 716 | data_in[0] = 3; // Stopped |
| 717 | |
| 718 | DoSimpleDataIn(data_in, 10); |
| 719 | } |
| 720 | |
| 721 | |
| 722 | |
| 723 | /******************************************************** |
| 724 | * * |
| 725 | * PC Engine CD Command 0xDE - Get Directory Info * |
| 726 | * * |
| 727 | ********************************************************/ |
| 728 | static void DoNEC_PCE_GETDIRINFO(const uint8 *cdb) |
| 729 | { |
| 730 | // Problems: |
| 731 | // Returned data lengths on real PCE are not confirmed. |
| 732 | // Mode 0x03 behavior not tested on real PCE |
| 733 | |
| 734 | uint8 data_in[2048]; |
| 735 | uint32 data_in_size = 0; |
| 736 | |
| 737 | memset(data_in, 0, sizeof(data_in)); |
| 738 | |
| 739 | switch(cdb[1]) |
| 740 | { |
| 741 | default: MDFN_DispMessage("Unknown GETDIRINFO Mode: %02x", cdb[1]); |
| 742 | printf("Unknown GETDIRINFO Mode: %02x", cdb[1]); |
| 743 | case 0x0: |
| 744 | data_in[0] = U8_to_BCD(toc.first_track); |
| 745 | data_in[1] = U8_to_BCD(toc.last_track); |
| 746 | |
| 747 | data_in_size = 2; |
| 748 | break; |
| 749 | |
| 750 | case 0x1: |
| 751 | { |
| 752 | uint8 m, s, f; |
| 753 | |
| 754 | LBA_to_AMSF(toc.tracks[100].lba, &m, &s, &f); |
| 755 | |
| 756 | data_in[0] = U8_to_BCD(m); |
| 757 | data_in[1] = U8_to_BCD(s); |
| 758 | data_in[2] = U8_to_BCD(f); |
| 759 | |
| 760 | data_in_size = 3; |
| 761 | } |
| 762 | break; |
| 763 | |
| 764 | case 0x2: |
| 765 | { |
| 766 | uint8 m, s, f; |
| 767 | int track = BCD_to_U8(cdb[2]); |
| 768 | |
| 769 | if(!track) |
| 770 | track = 1; |
| 771 | else if(cdb[2] == 0xAA) |
| 772 | { |
| 773 | track = 100; |
| 774 | } |
| 775 | else if(track > 99) |
| 776 | { |
| 777 | CommandCCError(SENSEKEY_ILLEGAL_REQUEST, NSE_INVALID_PARAMETER); |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | LBA_to_AMSF(toc.tracks[track].lba, &m, &s, &f); |
| 782 | |
| 783 | data_in[0] = U8_to_BCD(m); |
| 784 | data_in[1] = U8_to_BCD(s); |
| 785 | data_in[2] = U8_to_BCD(f); |
| 786 | data_in[3] = toc.tracks[track].control; |
| 787 | data_in_size = 4; |
| 788 | } |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | DoSimpleDataIn(data_in, data_in_size); |
| 793 | } |
| 794 | |
| 795 | #define SCF_REQUIRES_MEDIUM 0x01 |
| 796 | |
| 797 | typedef struct |
| 798 | { |
| 799 | uint8 cmd; |
| 800 | uint8 flags; |
| 801 | void (*func)(const uint8 *cdb); |
| 802 | const char *pretty_name; |
| 803 | const char *format_string; |
| 804 | } SCSICH; |
| 805 | |
| 806 | static const uint8 RequiredCDBLen[16] = |
| 807 | { |
| 808 | 6, // 0x0n |
| 809 | 6, // 0x1n |
| 810 | 10, // 0x2n |
| 811 | 10, // 0x3n |
| 812 | 10, // 0x4n |
| 813 | 10, // 0x5n |
| 814 | 10, // 0x6n |
| 815 | 10, // 0x7n |
| 816 | 10, // 0x8n |
| 817 | 10, // 0x9n |
| 818 | 12, // 0xAn |
| 819 | 12, // 0xBn |
| 820 | 10, // 0xCn |
| 821 | 10, // 0xDn |
| 822 | 10, // 0xEn |
| 823 | 10, // 0xFn |
| 824 | }; |
| 825 | |
| 826 | static SCSICH PCECommandDefs[] = |
| 827 | { |
| 828 | { 0x00, SCF_REQUIRES_MEDIUM, DoTESTUNITREADY, "Test Unit Ready" }, |
| 829 | { 0x03, 0, DoREQUESTSENSE, "Request Sense" }, |
| 830 | { 0x08, SCF_REQUIRES_MEDIUM, DoREAD6, "Read(6)" }, |
| 831 | |
| 832 | { 0xD8, SCF_REQUIRES_MEDIUM, DoNEC_PCE_SAPSP, "Set Audio Playback Start Position" }, |
| 833 | { 0xD9, SCF_REQUIRES_MEDIUM, DoNEC_PCE_SAPEP, "Set Audio Playback End Position" }, |
| 834 | { 0xDA, SCF_REQUIRES_MEDIUM, DoNEC_PCE_PAUSE, "Pause" }, |
| 835 | { 0xDD, SCF_REQUIRES_MEDIUM, DoNEC_PCE_READSUBQ, "Read Subchannel Q" }, |
| 836 | { 0xDE, SCF_REQUIRES_MEDIUM, DoNEC_PCE_GETDIRINFO, "Get Dir Info" }, |
| 837 | |
| 838 | { 0xFF, 0, 0, NULL, NULL }, |
| 839 | }; |
| 840 | |
| 841 | void PCECD_Drive_ResetTS(void) |
| 842 | { |
| 843 | lastts = 0; |
| 844 | } |
| 845 | |
| 846 | void PCECD_Drive_GetCDDAValues(int16 &left, int16 &right) |
| 847 | { |
| 848 | if(cdda.CDDAStatus) |
| 849 | { |
| 850 | left = cdda.CDDASectorBuffer[cdda.CDDAReadPos * 2]; |
| 851 | right = cdda.CDDASectorBuffer[cdda.CDDAReadPos * 2 + 1]; |
| 852 | } |
| 853 | else |
| 854 | left = right = 0; |
| 855 | } |
| 856 | |
| 857 | static INLINE void RunCDDA(uint32 system_timestamp, int32 run_time) |
| 858 | { |
| 859 | if(cdda.CDDAStatus == CDDASTATUS_PLAYING) |
| 860 | { |
| 861 | int32 sample[2]; |
| 862 | |
| 863 | cdda.CDDADiv -= run_time << 16; |
| 864 | |
| 865 | while(cdda.CDDADiv <= 0) |
| 866 | { |
| 867 | const uint32 synthtime = ((system_timestamp + (cdda.CDDADiv >> 16))) / cdda.CDDATimeDiv; |
| 868 | |
| 869 | cdda.CDDADiv += cdda.CDDADivAcc; |
| 870 | |
| 871 | //MDFN_DispMessage("%d %d %d\n", read_sec_start, read_sec, read_sec_end); |
| 872 | |
| 873 | if(cdda.CDDAReadPos == 588) |
| 874 | { |
| 875 | if(read_sec >= read_sec_end) |
| 876 | { |
| 877 | switch(cdda.PlayMode) |
| 878 | { |
| 879 | case PLAYMODE_SILENT: |
| 880 | case PLAYMODE_NORMAL: |
| 881 | cdda.CDDAStatus = CDDASTATUS_STOPPED; |
| 882 | break; |
| 883 | |
| 884 | case PLAYMODE_INTERRUPT: |
| 885 | cdda.CDDAStatus = CDDASTATUS_STOPPED; |
| 886 | CDIRQCallback(PCECD_Drive_IRQ_DATA_TRANSFER_DONE); |
| 887 | break; |
| 888 | |
| 889 | case PLAYMODE_LOOP: |
| 890 | read_sec = read_sec_start; |
| 891 | break; |
| 892 | } |
| 893 | |
| 894 | // If CDDA playback is stopped, break out of our while(CDDADiv ...) loop and don't play any more sound! |
| 895 | if(cdda.CDDAStatus == CDDASTATUS_STOPPED) |
| 896 | break; |
| 897 | } |
| 898 | |
| 899 | // Don't play past the user area of the disc. |
| 900 | if(read_sec >= toc.tracks[100].lba) |
| 901 | { |
| 902 | cdda.CDDAStatus = CDDASTATUS_STOPPED; |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | if(cd.TrayOpen) |
| 907 | { |
| 908 | cdda.CDDAStatus = CDDASTATUS_STOPPED; |
| 909 | |
| 910 | #if 0 |
| 911 | cd.data_transfer_done = FALSE; |
| 912 | cd.key_pending = SENSEKEY_NOT_READY; |
| 913 | cd.asc_pending = ASC_MEDIUM_NOT_PRESENT; |
| 914 | cd.ascq_pending = 0x00; |
| 915 | cd.fru_pending = 0x00; |
| 916 | SendStatusAndMessage(STATUS_CHECK_CONDITION, 0x00); |
| 917 | #endif |
| 918 | |
| 919 | break; |
| 920 | } |
| 921 | |
| 922 | |
| 923 | cdda.CDDAReadPos = 0; |
| 924 | |
| 925 | { |
| 926 | uint8 tmpbuf[2352 + 96]; |
| 927 | |
| 928 | Cur_CDIF->ReadRawSector(tmpbuf, read_sec); //, read_sec_end, read_sec_start); |
| 929 | |
| 930 | for(int i = 0; i < 588 * 2; i++) |
| 931 | cdda.CDDASectorBuffer[i] = MDFN_de16lsb(&tmpbuf[i * 2]); |
| 932 | |
| 933 | memcpy(cd.SubPWBuf, tmpbuf + 2352, 96); |
| 934 | } |
| 935 | GenSubQFromSubPW(); |
| 936 | read_sec++; |
| 937 | } // End if(CDDAReadPos == 588) |
| 938 | |
| 939 | // If the last valid sub-Q data decoded indicate that the corresponding sector is a data sector, don't output the |
| 940 | // current sector as audio. |
| 941 | sample[0] = sample[1] = 0; |
| 942 | |
| 943 | if(!(cd.SubQBuf_Last[0] & 0x40) && cdda.PlayMode != PLAYMODE_SILENT) |
| 944 | { |
| 945 | sample[0] = (cdda.CDDASectorBuffer[cdda.CDDAReadPos * 2 + 0] * cdda.CDDAVolume) >> 16; |
| 946 | sample[1] = (cdda.CDDASectorBuffer[cdda.CDDAReadPos * 2 + 1] * cdda.CDDAVolume) >> 16; |
| 947 | } |
| 948 | |
| 949 | if(!(cdda.CDDAReadPos % 6)) |
| 950 | { |
| 951 | int subindex = cdda.CDDAReadPos / 6 - 2; |
| 952 | |
| 953 | if(subindex >= 0) |
| 954 | CDStuffSubchannels(cd.SubPWBuf[subindex], subindex); |
| 955 | else // The system-specific emulation code should handle what value the sync bytes are. |
| 956 | CDStuffSubchannels(0x00, subindex); |
| 957 | } |
| 958 | |
| 959 | if(sbuf[0] && sbuf[1]) |
| 960 | { |
| 961 | cdda.CDDASynth.offset_inline(synthtime, sample[0] - cdda.last_sample[0], sbuf[0]); |
| 962 | cdda.CDDASynth.offset_inline(synthtime, sample[1] - cdda.last_sample[1], sbuf[1]); |
| 963 | } |
| 964 | |
| 965 | cdda.last_sample[0] = sample[0]; |
| 966 | cdda.last_sample[1] = sample[1]; |
| 967 | |
| 968 | cdda.CDDAReadPos++; |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | static INLINE void RunCDRead(uint32 system_timestamp, int32 run_time) |
| 974 | { |
| 975 | if(CDReadTimer > 0) |
| 976 | { |
| 977 | CDReadTimer -= run_time; |
| 978 | |
| 979 | if(CDReadTimer <= 0) |
| 980 | { |
| 981 | if(din.CanWrite() < 2048) |
| 982 | { |
| 983 | //printf("Carp: %d %d %d\n", din.CanWrite(), SectorCount, CDReadTimer); |
| 984 | //CDReadTimer = (cd.data_in_size - cd.data_in_pos) * 10; |
| 985 | |
| 986 | CDReadTimer += (uint64) 1 * 2048 * System_Clock / CD_DATA_TRANSFER_RATE; |
| 987 | |
| 988 | //CDReadTimer += (uint64) 1 * 128 * System_Clock / CD_DATA_TRANSFER_RATE; |
| 989 | } |
| 990 | else |
| 991 | { |
| 992 | uint8 tmp_read_buf[2352 + 96]; |
| 993 | |
| 994 | if(cd.TrayOpen) |
| 995 | { |
| 996 | din.Flush(); |
| 997 | cd.data_transfer_done = FALSE; |
| 998 | |
| 999 | CommandCCError(SENSEKEY_NOT_READY, NSE_TRAY_OPEN); |
| 1000 | } |
| 1001 | else if(SectorAddr >= toc.tracks[100].lba) |
| 1002 | { |
| 1003 | CommandCCError(SENSEKEY_ILLEGAL_REQUEST, NSE_END_OF_VOLUME); |
| 1004 | } |
| 1005 | else if(!Cur_CDIF->ReadRawSector(tmp_read_buf, SectorAddr)) //, SectorAddr + SectorCount)) |
| 1006 | { |
| 1007 | cd.data_transfer_done = FALSE; |
| 1008 | |
| 1009 | CommandCCError(SENSEKEY_ILLEGAL_REQUEST); |
| 1010 | } |
| 1011 | else if(ValidateRawDataSector(tmp_read_buf, SectorAddr)) |
| 1012 | { |
| 1013 | memcpy(cd.SubPWBuf, tmp_read_buf + 2352, 96); |
| 1014 | |
| 1015 | if(tmp_read_buf[12 + 3] == 0x2) |
| 1016 | din.Write(tmp_read_buf + 24, 2048); |
| 1017 | else |
| 1018 | din.Write(tmp_read_buf + 16, 2048); |
| 1019 | |
| 1020 | GenSubQFromSubPW(); |
| 1021 | |
| 1022 | CDIRQCallback(PCECD_Drive_IRQ_DATA_TRANSFER_READY); |
| 1023 | |
| 1024 | SectorAddr++; |
| 1025 | SectorCount--; |
| 1026 | |
| 1027 | if(CurrentPhase != PHASE_DATA_IN) |
| 1028 | ChangePhase(PHASE_DATA_IN); |
| 1029 | |
| 1030 | if(SectorCount) |
| 1031 | { |
| 1032 | cd.data_transfer_done = FALSE; |
| 1033 | CDReadTimer += (uint64) 1 * 2048 * System_Clock / CD_DATA_TRANSFER_RATE; |
| 1034 | } |
| 1035 | else |
| 1036 | { |
| 1037 | cd.data_transfer_done = TRUE; |
| 1038 | } |
| 1039 | } |
| 1040 | } // end else to if(!Cur_CDIF->ReadSector |
| 1041 | |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | uint32 PCECD_Drive_Run(pcecd_drive_timestamp_t system_timestamp) |
| 1048 | { |
| 1049 | int32 run_time = system_timestamp - lastts; |
| 1050 | |
| 1051 | if(system_timestamp < lastts) |
| 1052 | { |
| 1053 | fprintf(stderr, "Meow: %d %d\n", system_timestamp, lastts); |
| 1054 | assert(system_timestamp >= lastts); |
| 1055 | } |
| 1056 | |
| 1057 | monotonic_timestamp += run_time; |
| 1058 | |
| 1059 | lastts = system_timestamp; |
| 1060 | |
| 1061 | RunCDRead(system_timestamp, run_time); |
| 1062 | RunCDDA(system_timestamp, run_time); |
| 1063 | |
| 1064 | bool ResetNeeded = false; |
| 1065 | |
| 1066 | if(RST_signal && !cd.last_RST_signal) |
| 1067 | ResetNeeded = true; |
| 1068 | |
| 1069 | cd.last_RST_signal = RST_signal; |
| 1070 | |
| 1071 | if(ResetNeeded) |
| 1072 | { |
| 1073 | //puts("RST"); |
| 1074 | VirtualReset(); |
| 1075 | } |
| 1076 | else switch(CurrentPhase) |
| 1077 | { |
| 1078 | case PHASE_BUS_FREE: |
| 1079 | if(SEL_signal) |
| 1080 | { |
| 1081 | ChangePhase(PHASE_COMMAND); |
| 1082 | } |
| 1083 | break; |
| 1084 | |
| 1085 | case PHASE_COMMAND: |
| 1086 | if(REQ_signal && ACK_signal) // Data bus is valid nowww |
| 1087 | { |
| 1088 | //printf("Command Phase Byte I->T: %02x, %d\n", cd_bus.DB, cd.command_buffer_pos); |
| 1089 | cd.command_buffer[cd.command_buffer_pos++] = cd_bus.DB; |
| 1090 | SetREQ(FALSE); |
| 1091 | } |
| 1092 | |
| 1093 | if(!REQ_signal && !ACK_signal && cd.command_buffer_pos) // Received at least one byte, what should we do? |
| 1094 | { |
| 1095 | if(cd.command_buffer_pos == RequiredCDBLen[cd.command_buffer[0] >> 4]) |
| 1096 | { |
| 1097 | const SCSICH* cmd_info_ptr = PCECommandDefs; |
| 1098 | |
| 1099 | while(cmd_info_ptr->pretty_name && cmd_info_ptr->cmd != cd.command_buffer[0]) |
| 1100 | cmd_info_ptr++; |
| 1101 | |
| 1102 | if(SCSILog) |
| 1103 | { |
| 1104 | char log_buffer[1024]; |
| 1105 | int lb_pos; |
| 1106 | |
| 1107 | log_buffer[0] = 0; |
| 1108 | |
| 1109 | lb_pos = snprintf(log_buffer, 1024, "Command: %02x, %s ", cd.command_buffer[0], cmd_info_ptr->pretty_name ? cmd_info_ptr->pretty_name : "!!BAD COMMAND!!"); |
| 1110 | |
| 1111 | for(int i = 0; i < RequiredCDBLen[cd.command_buffer[0] >> 4]; i++) |
| 1112 | lb_pos += snprintf(log_buffer + lb_pos, 1024 - lb_pos, "%02x ", cd.command_buffer[i]); |
| 1113 | |
| 1114 | SCSILog("SCSI", "%s", log_buffer); |
| 1115 | //puts(log_buffer); |
| 1116 | } |
| 1117 | |
| 1118 | |
| 1119 | if(cmd_info_ptr->pretty_name == NULL) // Command not found! |
| 1120 | { |
| 1121 | CommandCCError(SENSEKEY_ILLEGAL_REQUEST, NSE_INVALID_COMMAND); |
| 1122 | |
| 1123 | SCSIDBG("Bad Command: %02x\n", cd.command_buffer[0]); |
| 1124 | |
| 1125 | if(SCSILog) |
| 1126 | SCSILog("SCSI", "Bad Command: %02x", cd.command_buffer[0]); |
| 1127 | |
| 1128 | cd.command_buffer_pos = 0; |
| 1129 | } |
| 1130 | else |
| 1131 | { |
| 1132 | if(cd.TrayOpen && (cmd_info_ptr->flags & SCF_REQUIRES_MEDIUM)) |
| 1133 | { |
| 1134 | CommandCCError(SENSEKEY_NOT_READY, NSE_TRAY_OPEN); |
| 1135 | } |
| 1136 | else if(!Cur_CDIF && (cmd_info_ptr->flags & SCF_REQUIRES_MEDIUM)) |
| 1137 | { |
| 1138 | CommandCCError(SENSEKEY_NOT_READY, NSE_NO_DISC); |
| 1139 | } |
| 1140 | else if(cd.DiscChanged && (cmd_info_ptr->flags & SCF_REQUIRES_MEDIUM)) |
| 1141 | { |
| 1142 | CommandCCError(SENSEKEY_UNIT_ATTENTION, NSE_DISC_CHANGED); |
| 1143 | cd.DiscChanged = false; |
| 1144 | } |
| 1145 | else |
| 1146 | { |
| 1147 | cmd_info_ptr->func(cd.command_buffer); |
| 1148 | } |
| 1149 | |
| 1150 | cd.command_buffer_pos = 0; |
| 1151 | } |
| 1152 | } // end if(cd.command_buffer_pos == RequiredCDBLen[cd.command_buffer[0] >> 4]) |
| 1153 | else // Otherwise, get more data for the command! |
| 1154 | SetREQ(TRUE); |
| 1155 | } |
| 1156 | break; |
| 1157 | |
| 1158 | case PHASE_STATUS: |
| 1159 | if(REQ_signal && ACK_signal) |
| 1160 | { |
| 1161 | SetREQ(FALSE); |
| 1162 | cd.status_sent = TRUE; |
| 1163 | } |
| 1164 | |
| 1165 | if(!REQ_signal && !ACK_signal && cd.status_sent) |
| 1166 | { |
| 1167 | // Status sent, so get ready to send the message! |
| 1168 | cd.status_sent = FALSE; |
| 1169 | cd_bus.DB = cd.message_pending; |
| 1170 | |
| 1171 | ChangePhase(PHASE_MESSAGE_IN); |
| 1172 | } |
| 1173 | break; |
| 1174 | |
| 1175 | case PHASE_DATA_IN: |
| 1176 | if(!REQ_signal && !ACK_signal) |
| 1177 | { |
| 1178 | //puts("REQ and ACK false"); |
| 1179 | if(din.CanRead() == 0) // aaand we're done! |
| 1180 | { |
| 1181 | CDIRQCallback(0x8000 | PCECD_Drive_IRQ_DATA_TRANSFER_READY); |
| 1182 | |
| 1183 | if(cd.data_transfer_done) |
| 1184 | { |
| 1185 | SendStatusAndMessage(STATUS_GOOD, 0x00); |
| 1186 | cd.data_transfer_done = FALSE; |
| 1187 | CDIRQCallback(PCECD_Drive_IRQ_DATA_TRANSFER_DONE); |
| 1188 | } |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | cd_bus.DB = din.ReadByte(); |
| 1193 | SetREQ(TRUE); |
| 1194 | } |
| 1195 | } |
| 1196 | if(REQ_signal && ACK_signal) |
| 1197 | { |
| 1198 | //puts("REQ and ACK true"); |
| 1199 | SetREQ(FALSE); |
| 1200 | } |
| 1201 | break; |
| 1202 | |
| 1203 | case PHASE_MESSAGE_IN: |
| 1204 | if(REQ_signal && ACK_signal) |
| 1205 | { |
| 1206 | SetREQ(FALSE); |
| 1207 | cd.message_sent = TRUE; |
| 1208 | } |
| 1209 | |
| 1210 | if(!REQ_signal && !ACK_signal && cd.message_sent) |
| 1211 | { |
| 1212 | cd.message_sent = FALSE; |
| 1213 | ChangePhase(PHASE_BUS_FREE); |
| 1214 | } |
| 1215 | break; |
| 1216 | } |
| 1217 | |
| 1218 | int32 next_time = 0x7fffffff; |
| 1219 | |
| 1220 | if(CDReadTimer > 0 && CDReadTimer < next_time) |
| 1221 | next_time = CDReadTimer; |
| 1222 | |
| 1223 | if(cdda.CDDAStatus == CDDASTATUS_PLAYING) |
| 1224 | { |
| 1225 | int32 cdda_div_sexytime = (cdda.CDDADiv + 0xFFFF) >> 16; |
| 1226 | if(cdda_div_sexytime > 0 && cdda_div_sexytime < next_time) |
| 1227 | next_time = cdda_div_sexytime; |
| 1228 | } |
| 1229 | |
| 1230 | assert(next_time >= 0); |
| 1231 | |
| 1232 | return(next_time); |
| 1233 | } |
| 1234 | |
| 1235 | void PCECD_Drive_SetLog(void (*logfunc)(const char *, const char *, ...)) |
| 1236 | { |
| 1237 | SCSILog = logfunc; |
| 1238 | } |
| 1239 | |
| 1240 | void PCECD_Drive_SetTransferRate(uint32 TransferRate) |
| 1241 | { |
| 1242 | CD_DATA_TRANSFER_RATE = TransferRate; |
| 1243 | } |
| 1244 | |
| 1245 | void PCECD_Drive_Close(void) |
| 1246 | { |
| 1247 | |
| 1248 | } |
| 1249 | |
| 1250 | void PCECD_Drive_Init(int cdda_time_div, Blip_Buffer *leftbuf, Blip_Buffer *rightbuf, uint32 TransferRate, uint32 SystemClock, void (*IRQFunc)(int), void (*SSCFunc)(uint8, int)) |
| 1251 | { |
| 1252 | Cur_CDIF = NULL; |
| 1253 | cd.TrayOpen = false; |
| 1254 | |
| 1255 | monotonic_timestamp = 0; |
| 1256 | lastts = 0; |
| 1257 | |
| 1258 | SCSILog = NULL; |
| 1259 | |
| 1260 | //din = new SimpleFIFO<uint8>(2048); |
| 1261 | |
| 1262 | cdda.CDDATimeDiv = cdda_time_div; |
| 1263 | |
| 1264 | cdda.CDDAVolume = 65536; |
| 1265 | cdda.CDDASynth.volume(1.0f / 65536); |
| 1266 | cdda.CDDASynth.treble_eq(0); |
| 1267 | sbuf[0] = leftbuf; |
| 1268 | sbuf[1] = rightbuf; |
| 1269 | |
| 1270 | CD_DATA_TRANSFER_RATE = TransferRate; |
| 1271 | System_Clock = SystemClock; |
| 1272 | CDIRQCallback = IRQFunc; |
| 1273 | CDStuffSubchannels = SSCFunc; |
| 1274 | } |
| 1275 | |
| 1276 | void PCECD_Drive_SetCDDAVolume(unsigned vol) |
| 1277 | { |
| 1278 | cdda.CDDAVolume = vol; |
| 1279 | } |
| 1280 | |
| 1281 | int PCECD_Drive_StateAction(StateMem * sm, int load, int data_only, const char *sname) |
| 1282 | { |
| 1283 | SFORMAT StateRegs[] = |
| 1284 | { |
| 1285 | SFVARN(cd_bus.DB, "DB"), |
| 1286 | SFVARN(cd_bus.signals, "Signals"), |
| 1287 | SFVAR(CurrentPhase), |
| 1288 | |
| 1289 | SFVARN(cd.last_RST_signal, "last_RST"), |
| 1290 | SFVARN(cd.message_pending, "message_pending"), |
| 1291 | SFVARN(cd.status_sent, "status_sent"), |
| 1292 | SFVARN(cd.message_sent, "message_sent"), |
| 1293 | SFVARN(cd.key_pending, "key_pending"), |
| 1294 | SFVARN(cd.asc_pending, "asc_pending"), |
| 1295 | SFVARN(cd.ascq_pending, "ascq_pending"), |
| 1296 | SFVARN(cd.fru_pending, "fru_pending"), |
| 1297 | |
| 1298 | SFARRAYN(cd.command_buffer, 256, "command_buffer"), |
| 1299 | SFVARN(cd.command_buffer_pos, "command_buffer_pos"), |
| 1300 | SFVARN(cd.command_size_left, "command_size_left"), |
| 1301 | |
| 1302 | // Don't save the FIFO's write position, it will be reconstructed from read_pos and in_count |
| 1303 | SFARRAYN(&din.data[0], din.data.size(), "din_fifo"), |
| 1304 | SFVARN(din.read_pos, "din_read_pos"), |
| 1305 | SFVARN(din.in_count, "din_in_count"), |
| 1306 | SFVARN(cd.data_transfer_done, "data_transfer_done"), |
| 1307 | |
| 1308 | SFVARN(cd.TrayOpen, "TrayOpen"), |
| 1309 | SFVARN(cd.DiscChanged, "DiscChanged"), |
| 1310 | |
| 1311 | SFVAR(cdda.PlayMode), |
| 1312 | SFARRAY16(cdda.CDDASectorBuffer, 1176), |
| 1313 | SFVAR(cdda.CDDAReadPos), |
| 1314 | SFVAR(cdda.CDDAStatus), |
| 1315 | SFVAR(cdda.CDDADiv), |
| 1316 | SFVAR(read_sec_start), |
| 1317 | SFVAR(read_sec), |
| 1318 | SFVAR(read_sec_end), |
| 1319 | |
| 1320 | SFVAR(CDReadTimer), |
| 1321 | SFVAR(SectorAddr), |
| 1322 | SFVAR(SectorCount), |
| 1323 | |
| 1324 | SFVAR(cdda.ScanMode), |
| 1325 | SFVAR(cdda.scan_sec_end), |
| 1326 | |
| 1327 | SFARRAYN(&cd.SubQBuf[0][0], sizeof(cd.SubQBuf), "SubQBufs"), |
| 1328 | SFARRAYN(cd.SubQBuf_Last, sizeof(cd.SubQBuf_Last), "SubQBufLast"), |
| 1329 | SFARRAYN(cd.SubPWBuf, sizeof(cd.SubPWBuf), "SubPWBuf"), |
| 1330 | |
| 1331 | SFVAR(monotonic_timestamp), |
| 1332 | SFVAR(pce_lastsapsp_timestamp), |
| 1333 | |
| 1334 | SFEND |
| 1335 | }; |
| 1336 | |
| 1337 | int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, sname); |
| 1338 | |
| 1339 | if(load) |
| 1340 | { |
| 1341 | din.in_count &= din.size - 1; |
| 1342 | din.read_pos &= din.size - 1; |
| 1343 | din.write_pos = (din.read_pos + din.in_count) & (din.size - 1); |
| 1344 | |
| 1345 | if(cdda.CDDADiv <= 0) |
| 1346 | cdda.CDDADiv = 1; |
| 1347 | //printf("%d %d %d\n", din.in_count, din.read_pos, din.write_pos); |
| 1348 | } |
| 1349 | |
| 1350 | return (ret); |
| 1351 | } |