+static void cdrPlayInterrupt_Autopause()
+{
+ u32 abs_lev_max = 0;
+ boolean abs_lev_chselect;
+ u32 i;
+
+ if ((cdr.Mode & MODE_AUTOPAUSE) && cdr.TrackChanged) {
+ CDR_LOG( "CDDA STOP\n" );
+
+ // Magic the Gathering
+ // - looping territory cdda
+
+ // ...?
+ //cdr.ResultReady = 1;
+ //cdr.Stat = DataReady;
+ cdr.Stat = DataEnd;
+ setIrq(0x1000); // 0x1000 just for logging purposes
+
+ StopCdda();
+ SetPlaySeekRead(cdr.StatP, 0);
+ }
+ else if (((cdr.Mode & MODE_REPORT) || cdr.FastForward || cdr.FastBackward)) {
+ cdr.Result[0] = cdr.StatP;
+ cdr.Result[1] = cdr.subq.Track;
+ cdr.Result[2] = cdr.subq.Index;
+
+ abs_lev_chselect = cdr.subq.Absolute[1] & 0x01;
+
+ /* 8 is a hack. For accuracy, it should be 588. */
+ for (i = 0; i < 8; i++)
+ {
+ abs_lev_max = MAX_VALUE(abs_lev_max, abs(read_buf[i * 2 + abs_lev_chselect]));
+ }
+ abs_lev_max = MIN_VALUE(abs_lev_max, 32767);
+ abs_lev_max |= abs_lev_chselect << 15;
+
+ if (cdr.subq.Absolute[2] & 0x10) {
+ cdr.Result[3] = cdr.subq.Relative[0];
+ cdr.Result[4] = cdr.subq.Relative[1] | 0x80;
+ cdr.Result[5] = cdr.subq.Relative[2];
+ }
+ else {
+ cdr.Result[3] = cdr.subq.Absolute[0];
+ cdr.Result[4] = cdr.subq.Absolute[1];
+ cdr.Result[5] = cdr.subq.Absolute[2];
+ }
+
+ cdr.Result[6] = abs_lev_max >> 0;
+ cdr.Result[7] = abs_lev_max >> 8;
+
+ // Rayman: Logo freeze (resultready + dataready)
+ cdr.ResultReady = 1;
+ cdr.Stat = DataReady;
+
+ SetResultSize(8);
+ setIrq(0x1001);
+ }
+}
+
+static int cdrSeekTime(unsigned char *target)
+{
+ int diff = msf2sec(cdr.SetSectorPlay) - msf2sec(target);
+ int seekTime = abs(diff) * (cdReadTime / 200);
+ /*
+ * Gameblabla :
+ * It was originally set to 1000000 for Driver, however it is not high enough for Worms Pinball
+ * and was unreliable for that game.
+ * I also tested it against Mednafen and Driver's titlescreen music starts 25 frames later, not immediatly.
+ *
+ * Obviously, this isn't perfect but right now, it should be a bit better.
+ * Games to test this against if you change that setting :
+ * - Driver (titlescreen music delay and retry mission)
+ * - Worms Pinball (Will either not boot or crash in the memory card screen)
+ * - Viewpoint (short pauses if the delay in the ingame music is too long)
+ *
+ * It seems that 3386880 * 5 is too much for Driver's titlescreen and it starts skipping.
+ * However, 1000000 is not enough for Worms Pinball to reliably boot.
+ */
+ if(seekTime > 3386880 * 2) seekTime = 3386880 * 2;
+ CDR_LOG("seek: %.2f %.2f\n", (float)seekTime / PSXCLK, (float)seekTime / cdReadTime);
+ return seekTime;
+}
+
+static u32 cdrAlignTimingHack(u32 cycles)
+{
+ /*
+ * timing hack for T'ai Fu - Wrath of the Tiger:
+ * The game has a bug where it issues some cdc commands from a low priority
+ * vint handler, however there is a higher priority default bios handler
+ * that acks the vint irq and returns, so game's handler is not reached
+ * (see bios irq handler chains at e004 and the game's irq handling func
+ * at 80036810). For the game to work, vint has to arrive after the bios
+ * vint handler rejects some other irq (of which only cd and rcnt2 are
+ * active), but before the game's handler loop reads I_STAT. The time
+ * window for this is quite small (~1k cycles of so). Apparently this
+ * somehow happens naturally on the real hardware.
+ *
+ * Note: always enforcing this breaks other games like Crash PAL version
+ * (inputs get dropped because bios handler doesn't see interrupts).
+ */
+ u32 vint_rel;
+ if (psxRegs.cycle - rcnts[3].cycleStart > 250000)
+ return cycles;
+ vint_rel = rcnts[3].cycleStart + 63000 - psxRegs.cycle;
+ vint_rel += PSXCLK / 60;
+ while ((s32)(vint_rel - cycles) < 0)
+ vint_rel += PSXCLK / 60;
+ return vint_rel;
+}
+
+static void cdrUpdateTransferBuf(const u8 *buf);
+static void cdrReadInterrupt(void);
+static void cdrPrepCdda(s16 *buf, int samples);
+static void cdrAttenuate(s16 *buf, int samples, int stereo);
+
+static void msfiAdd(u8 *msfi, u32 count)
+{
+ assert(count < 75);
+ msfi[2] += count;
+ if (msfi[2] >= 75) {
+ msfi[2] -= 75;
+ msfi[1]++;
+ if (msfi[1] == 60) {
+ msfi[1] = 0;
+ msfi[0]++;
+ }
+ }
+}
+
+void cdrPlayReadInterrupt(void)
+{
+ if (cdr.Reading) {
+ cdrReadInterrupt();
+ return;
+ }
+
+ if (!cdr.Play) return;
+
+ CDR_LOG( "CDDA - %d:%d:%d\n",
+ cdr.SetSectorPlay[0], cdr.SetSectorPlay[1], cdr.SetSectorPlay[2] );
+
+ SetPlaySeekRead(cdr.StatP, STATUS_PLAY);
+ if (memcmp(cdr.SetSectorPlay, cdr.SetSectorEnd, 3) == 0) {
+ StopCdda();
+ SetPlaySeekRead(cdr.StatP, 0);
+ cdr.TrackChanged = TRUE;
+ }
+ else {
+ CDR_readCDDA(cdr.SetSectorPlay[0], cdr.SetSectorPlay[1], cdr.SetSectorPlay[2], (u8 *)read_buf);
+ }
+
+ if (!cdr.Stat && (cdr.Mode & (MODE_AUTOPAUSE|MODE_REPORT)))
+ cdrPlayInterrupt_Autopause();
+
+ if (!cdr.Muted && !Config.Cdda) {
+ cdrPrepCdda(read_buf, CD_FRAMESIZE_RAW / 4);
+ cdrAttenuate(read_buf, CD_FRAMESIZE_RAW / 4, 1);
+ SPU_playCDDAchannel(read_buf, CD_FRAMESIZE_RAW, psxRegs.cycle, cdr.FirstSector);
+ cdr.FirstSector = 0;
+ }
+
+ msfiAdd(cdr.SetSectorPlay, 1);
+
+ // update for CdlGetlocP/autopause
+ generate_subq(cdr.SetSectorPlay);
+
+ CDRPLAYREAD_INT(cdReadTime, 0);
+}
+
+#define CMD_PART2 0x100
+#define CMD_WHILE_NOT_READY 0x200
+
+void cdrInterrupt(void) {
+ int start_rotating = 0;
+ int error = 0;
+ u32 cycles, seekTime = 0;
+ u32 second_resp_time = 0;
+ const void *buf;
+ u8 ParamC;
+ u8 set_loc[3];
+ int read_ok;
+ u16 not_ready = 0;
+ u16 Cmd;