diff --git a/libpcsxcore/cdrom.c b/libpcsxcore/cdrom.c index 950b6484b..9519d8c53 100644 --- a/libpcsxcore/cdrom.c +++ b/libpcsxcore/cdrom.c @@ -52,10 +52,10 @@ static struct { // unused members maintain savesate compatibility unsigned char unused0; unsigned char unused1; - unsigned char Reg2; + unsigned char IrqMask; unsigned char unused2; unsigned char Ctrl; - unsigned char Stat; + unsigned char IrqStat; unsigned char StatP; @@ -177,7 +177,7 @@ unsigned char Test20[] = { 0x98, 0x06, 0x10, 0xC3 }; unsigned char Test22[] = { 0x66, 0x6F, 0x72, 0x20, 0x45, 0x75, 0x72, 0x6F }; unsigned char Test23[] = { 0x43, 0x58, 0x44, 0x32, 0x39 ,0x34, 0x30, 0x51 }; -// cdr.Stat: +// cdr.IrqStat: #define NoIntr 0 #define DataReady 1 #define Complete 2 @@ -209,7 +209,8 @@ unsigned char Test23[] = { 0x43, 0x58, 0x44, 0x32, 0x39 ,0x34, 0x30, 0x51 }; /* Errors */ #define ERROR_NOTREADY (1<<7) // 0x80 #define ERROR_INVALIDCMD (1<<6) // 0x40 -#define ERROR_INVALIDARG (1<<5) // 0x20 +#define ERROR_BAD_ARGNUM (1<<5) // 0x20 +#define ERROR_BAD_ARGVAL (1<<4) // 0x10 #define ERROR_SHELLOPEN (1<<3) // 0x08 // 1x = 75 sectors per second @@ -276,23 +277,33 @@ static void sec2msf(unsigned int s, u8 *msf) { x |= f; \ } -#define SetResultSize(size) { \ +#define SetResultSize_(size) { \ cdr.ResultP = 0; \ cdr.ResultC = size; \ cdr.ResultReady = 1; \ } -static void setIrq(int log_cmd) +#define SetResultSize(size) { \ + if (cdr.ResultP < cdr.ResultC) \ + CDR_LOG_I("overwriting result, len=%u\n", cdr.ResultC); \ + SetResultSize_(size); \ +} + +static void setIrq(u8 irq, int log_cmd) { - if (cdr.Stat & cdr.Reg2) + u8 old = cdr.IrqStat & cdr.IrqMask ? 1 : 0; + u8 new_ = irq & cdr.IrqMask ? 1 : 0; + + cdr.IrqStat = irq; + if ((old ^ new_) & new_) psxHu32ref(0x1070) |= SWAP32((u32)0x4); #ifdef CDR_LOG_CMD_IRQ - if (cdr.Stat) + if (cdr.IrqStat) { int i; - CDR_LOG_I("CDR IRQ=%d cmd %02x stat %02x: ", - !!(cdr.Stat & cdr.Reg2), log_cmd, cdr.Stat); + CDR_LOG_I("CDR IRQ=%d cmd %02x irqstat %02x: ", + !!(cdr.IrqStat & cdr.IrqMask), log_cmd, cdr.IrqStat); for (i = 0; i < cdr.ResultC; i++) SysPrintf("%02x ", cdr.Result[i]); SysPrintf("\n"); @@ -340,8 +351,7 @@ void cdrLidSeekInterrupt(void) SetResultSize(2); cdr.Result[0] = cdr.StatP | STATUS_SEEKERROR; cdr.Result[1] = ERROR_SHELLOPEN; - cdr.Stat = DiskError; - setIrq(0x1006); + setIrq(DiskError, 0x1006); } if (cdr.CmdInProgress) { psxRegs.interrupt &= ~(1 << PSXINT_CDR); @@ -349,8 +359,7 @@ void cdrLidSeekInterrupt(void) SetResultSize(2); cdr.Result[0] = cdr.StatP | STATUS_ERROR; cdr.Result[1] = ERROR_NOTREADY; - cdr.Stat = DiskError; - setIrq(0x1007); + setIrq(DiskError, 0x1007); } set_event(PSXINT_CDRLID, cdReadTime * 30); @@ -524,8 +533,7 @@ static void cdrPlayInterrupt_Autopause() SetResultSize(1); cdr.Result[0] = cdr.StatP; - cdr.Stat = DataEnd; - setIrq(0x1000); // 0x1000 just for logging purposes + setIrq(DataEnd, 0x1000); // 0x1000 just for logging purposes StopCdda(); SetPlaySeekRead(cdr.StatP, 0); @@ -561,8 +569,7 @@ static void cdrPlayInterrupt_Autopause() cdr.Result[6] = abs_lev_max >> 0; cdr.Result[7] = abs_lev_max >> 8; - cdr.Stat = DataReady; - setIrq(0x1001); + setIrq(DataReady, 0x1001); } if (cdr.ReportDelay) @@ -676,7 +683,7 @@ void cdrPlayReadInterrupt(void) CDR_readCDDA(cdr.SetSectorPlay[0], cdr.SetSectorPlay[1], cdr.SetSectorPlay[2], (u8 *)read_buf); } - if (!cdr.Stat && (cdr.Mode & (MODE_AUTOPAUSE|MODE_REPORT))) + if (!cdr.IrqStat && (cdr.Mode & (MODE_AUTOPAUSE|MODE_REPORT))) cdrPlayInterrupt_Autopause(); if (!cdr.Muted && cdr.Play && !Config.Cdda) { @@ -706,12 +713,13 @@ void cdrInterrupt(void) { u8 set_loc[3]; int read_ok; u16 not_ready = 0; + u8 IrqStat = Acknowledge; u16 Cmd; int i; - if (cdr.Stat) { + if (cdr.IrqStat) { CDR_LOG_I("cmd %02x with irqstat %x\n", - cdr.CmdInProgress, cdr.Stat); + cdr.CmdInProgress, cdr.IrqStat); return; } if (cdr.Irq1Pending) { @@ -722,16 +730,14 @@ void cdrInterrupt(void) { cdr.CmdInProgress, cdr.Irq1Pending); SetResultSize(1); cdr.Result[0] = cdr.Irq1Pending; - cdr.Stat = (cdr.Irq1Pending & STATUS_ERROR) ? DiskError : DataReady; cdr.Irq1Pending = 0; - setIrq(0x1003); + setIrq((cdr.Irq1Pending & STATUS_ERROR) ? DiskError : DataReady, 0x1003); return; } // default response SetResultSize(1); cdr.Result[0] = cdr.StatP; - cdr.Stat = Acknowledge; Cmd = cdr.CmdInProgress; cdr.CmdInProgress = 0; @@ -778,7 +784,7 @@ void cdrInterrupt(void) { CDR_LOG_I("Invalid/out of range seek to %02X:%02X:%02X\n", cdr.Param[0], cdr.Param[1], cdr.Param[2]); if (++cdr.errorRetryhack > 100) break; - error = ERROR_INVALIDARG; + error = ERROR_BAD_ARGNUM; goto set_error; } else @@ -862,7 +868,7 @@ void cdrInterrupt(void) { case CdlForward: // TODO: error 80 if stopped - cdr.Stat = Complete; + IrqStat = Complete; // GameShark CD Player: Calls 2x + Play 2x cdr.FastForward = 1; @@ -870,7 +876,7 @@ void cdrInterrupt(void) { break; case CdlBackward: - cdr.Stat = Complete; + IrqStat = Complete; // GameShark CD Player: Calls 2x + Play 2x cdr.FastBackward = 1; @@ -879,7 +885,7 @@ void cdrInterrupt(void) { case CdlStandby: if (cdr.DriveState != DRIVESTATE_STOPPED) { - error = ERROR_INVALIDARG; + error = ERROR_BAD_ARGNUM; goto set_error; } second_resp_time = cdReadTime * 125 / 2; @@ -887,7 +893,7 @@ void cdrInterrupt(void) { break; case CdlStandby + CMD_PART2: - cdr.Stat = Complete; + IrqStat = Complete; break; case CdlStop: @@ -914,7 +920,7 @@ void cdrInterrupt(void) { break; case CdlStop + CMD_PART2: - cdr.Stat = Complete; + IrqStat = Complete; break; case CdlPause: @@ -953,7 +959,7 @@ void cdrInterrupt(void) { break; case CdlPause + CMD_PART2: - cdr.Stat = Complete; + IrqStat = Complete; break; case CdlReset: @@ -970,7 +976,7 @@ void cdrInterrupt(void) { case CdlReset + CMD_PART2: case CdlReset + CMD_PART2 + CMD_WHILE_NOT_READY: - cdr.Stat = Complete; + IrqStat = Complete; break; case CdlMute: @@ -995,7 +1001,7 @@ void cdrInterrupt(void) { case CdlGetparam: case CdlGetparam + CMD_WHILE_NOT_READY: /* Gameblabla : According to mednafen, Result size should be 5 and done this way. */ - SetResultSize(5); + SetResultSize_(5); cdr.Result[1] = cdr.Mode; cdr.Result[2] = 0; cdr.Result[3] = cdr.FilterFile; @@ -1007,12 +1013,12 @@ void cdrInterrupt(void) { error = 0x80; goto set_error; } - SetResultSize(8); + SetResultSize_(8); memcpy(cdr.Result, cdr.LocL, 8); break; case CdlGetlocP: - SetResultSize(8); + SetResultSize_(8); memcpy(&cdr.Result, &cdr.subq, 8); break; @@ -1023,36 +1029,29 @@ void cdrInterrupt(void) { break; case CdlReadT + CMD_PART2: - cdr.Stat = Complete; + IrqStat = Complete; break; case CdlGetTN: - SetResultSize(3); if (CDR_getTN(cdr.ResultTN) == -1) { - cdr.Stat = DiskError; - cdr.Result[0] |= STATUS_ERROR; - } else { - cdr.Stat = Acknowledge; - cdr.Result[1] = itob(cdr.ResultTN[0]); - cdr.Result[2] = itob(cdr.ResultTN[1]); + assert(0); } + SetResultSize_(3); + cdr.Result[1] = itob(cdr.ResultTN[0]); + cdr.Result[2] = itob(cdr.ResultTN[1]); break; case CdlGetTD: cdr.Track = btoi(cdr.Param[0]); - SetResultSize(4); if (CDR_getTD(cdr.Track, cdr.ResultTD) == -1) { - cdr.Stat = DiskError; - cdr.Result[0] |= STATUS_ERROR; - } else { - cdr.Stat = Acknowledge; - cdr.Result[0] = cdr.StatP; - cdr.Result[1] = itob(cdr.ResultTD[2]); - cdr.Result[2] = itob(cdr.ResultTD[1]); - /* According to Nocash's documentation, the function doesn't care about ff. - * This can be seen also in Mednafen's implementation. */ - //cdr.Result[3] = itob(cdr.ResultTD[0]); + error = ERROR_BAD_ARGVAL; + goto set_error; } + SetResultSize_(3); + cdr.Result[1] = itob(cdr.ResultTD[2]); + cdr.Result[2] = itob(cdr.ResultTD[1]); + // no sector number + //cdr.Result[3] = itob(cdr.ResultTD[0]); break; case CdlSeekL: @@ -1084,7 +1083,7 @@ void cdrInterrupt(void) { case CdlSeekP + CMD_PART2: SetPlaySeekRead(cdr.StatP, 0); cdr.Result[0] = cdr.StatP; - cdr.Stat = Complete; + IrqStat = Complete; Find_CurTrack(cdr.SetSectorPlay); read_ok = ReadTrack(cdr.SetSectorPlay); @@ -1099,15 +1098,15 @@ void cdrInterrupt(void) { case CdlTest + CMD_WHILE_NOT_READY: switch (cdr.Param[0]) { case 0x20: // System Controller ROM Version - SetResultSize(4); + SetResultSize_(4); memcpy(cdr.Result, Test20, 4); break; case 0x22: - SetResultSize(8); + SetResultSize_(8); memcpy(cdr.Result, Test22, 4); break; case 0x23: case 0x24: - SetResultSize(8); + SetResultSize_(8); memcpy(cdr.Result, Test23, 4); break; } @@ -1118,7 +1117,7 @@ void cdrInterrupt(void) { break; case CdlID + CMD_PART2: - SetResultSize(8); + SetResultSize_(8); cdr.Result[0] = cdr.StatP; cdr.Result[1] = 0; cdr.Result[2] = 0; @@ -1140,7 +1139,7 @@ void cdrInterrupt(void) { /* This adds the string "PCSX" in Playstation bios boot screen */ memcpy((char *)&cdr.Result[4], "PCSX", 4); - cdr.Stat = Complete; + IrqStat = Complete; break; case CdlInit: @@ -1168,7 +1167,7 @@ void cdrInterrupt(void) { case CdlReadToc + CMD_PART2: case CdlReadToc + CMD_PART2 + CMD_WHILE_NOT_READY: - cdr.Stat = Complete; + IrqStat = Complete; break; case CdlReadN: @@ -1215,10 +1214,10 @@ void cdrInterrupt(void) { // FALLTHROUGH set_error: - SetResultSize(2); + SetResultSize_(2); cdr.Result[0] = cdr.StatP | STATUS_ERROR; cdr.Result[1] = not_ready ? ERROR_NOTREADY : error; - cdr.Stat = DiskError; + IrqStat = DiskError; CDR_LOG_I("cmd %02x error %02x\n", Cmd, cdr.Result[1]); break; } @@ -1237,7 +1236,7 @@ void cdrInterrupt(void) { CDR_LOG_I("cmd %02x came before %02x finished\n", cdr.Cmd, Cmd); } - setIrq(Cmd); + setIrq(IrqStat, Cmd); } #ifdef HAVE_ARMV7 @@ -1301,17 +1300,16 @@ static void cdrAttenuate(s16 *buf, int samples, int stereo) static void cdrReadInterruptSetResult(unsigned char result) { - if (cdr.Stat) { + if (cdr.IrqStat) { CDR_LOG_I("%d:%02d:%02d irq miss, cmd=%02x irqstat=%02x\n", cdr.SetSectorPlay[0], cdr.SetSectorPlay[1], cdr.SetSectorPlay[2], - cdr.CmdInProgress, cdr.Stat); + cdr.CmdInProgress, cdr.IrqStat); cdr.Irq1Pending = result; return; } SetResultSize(1); cdr.Result[0] = result; - cdr.Stat = (result & STATUS_ERROR) ? DiskError : DataReady; - setIrq(0x1004); + setIrq((result & STATUS_ERROR) ? DiskError : DataReady, 0x1004); } static void cdrUpdateTransferBuf(const u8 *buf) @@ -1360,7 +1358,7 @@ static void cdrReadInterrupt(void) } memcpy(cdr.LocL, buf, 8); - if (!cdr.Stat && !cdr.Irq1Pending) + if (!cdr.IrqStat && !cdr.Irq1Pending) cdrUpdateTransferBuf(buf); subhdr = (void *)(buf + 4); @@ -1528,8 +1526,8 @@ void cdrWrite2(unsigned char rt) { cdr.Param[cdr.ParamC++] = rt; return; case 1: - cdr.Reg2 = rt; - setIrq(0x1005); + cdr.IrqMask = rt; + setIrq(cdr.IrqStat, 0x1005); return; case 2: cdr.AttenuatorLeftToLeftT = rt; @@ -1542,9 +1540,9 @@ void cdrWrite2(unsigned char rt) { unsigned char cdrRead3(void) { if (cdr.Ctrl & 0x1) - psxHu8(0x1803) = cdr.Stat | 0xE0; + psxHu8(0x1803) = cdr.IrqStat | 0xE0; else - psxHu8(0x1803) = cdr.Reg2 | 0xE0; + psxHu8(0x1803) = cdr.IrqMask | 0xE0; CDR_LOG_IO("cdr r3.%s: %02x\n", (cdr.Ctrl & 1) ? "ifl" : "ien", psxHu8(0x1803)); return psxHu8(0x1803); @@ -1558,13 +1556,13 @@ void cdrWrite3(unsigned char rt) { case 0: break; // transfer case 1: - if (cdr.Stat & rt) { + if (cdr.IrqStat & rt) { u32 nextCycle = psxRegs.intCycle[PSXINT_CDR].sCycle + psxRegs.intCycle[PSXINT_CDR].cycle; int pending = psxRegs.interrupt & (1 << PSXINT_CDR); #ifdef CDR_LOG_CMD_IRQ - CDR_LOG_I("ack %02x (w=%02x p=%d,%x,%x,%d)\n", cdr.Stat & rt, rt, - !!pending, cdr.CmdInProgress, + CDR_LOG_I("ack %02x (w=%02x p=%d,%x,%x,%d)\n", + cdr.IrqStat & rt, rt, !!pending, cdr.CmdInProgress, cdr.Irq1Pending, nextCycle - psxRegs.cycle); #endif // note: Croc, Shadow Tower (more) vs Discworld Noir (<993) @@ -1578,7 +1576,7 @@ void cdrWrite3(unsigned char rt) { set_event(PSXINT_CDR, c); } } - cdr.Stat &= ~rt; + cdr.IrqStat &= ~rt; if (rt & 0x40) cdr.ParamC = 0; @@ -1712,8 +1710,8 @@ void cdrReset() { cdr.CurTrack = 1; cdr.FilterFile = 0; cdr.FilterChannel = 0; - cdr.Reg2 = 0x1f; - cdr.Stat = NoIntr; + cdr.IrqMask = 0x1f; + cdr.IrqStat = NoIntr; cdr.FifoOffset = DATA_SIZE; // fifo empty CDR_getStatus(&stat); @@ -1779,20 +1777,6 @@ int cdrFreeze(void *f, int Mode) { if (!Config.Cdda) CDR_play(cdr.SetSectorPlay); } - - if ((cdr.freeze_ver & 0xffffff00) != 0x63647200) { - // old versions did not latch Reg2, have to fixup.. - if (cdr.Reg2 == 0) { - SysPrintf("cdrom: fixing up old savestate\n"); - cdr.Reg2 = 7; - } - // also did not save Attenuator.. - if ((cdr.AttenuatorLeftToLeft | cdr.AttenuatorLeftToRight - | cdr.AttenuatorRightToLeft | cdr.AttenuatorRightToRight) == 0) - { - cdr.AttenuatorLeftToLeft = cdr.AttenuatorRightToRight = 0x80; - } - } } return 0;