Commit | Line | Data |
---|---|---|
ef79bbde P |
1 | /*************************************************************************** |
2 | * Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team * | |
3 | * * | |
4 | * This program is free software; you can redistribute it and/or modify * | |
5 | * it under the terms of the GNU General Public License as published by * | |
6 | * the Free Software Foundation; either version 2 of the License, or * | |
7 | * (at your option) any later version. * | |
8 | * * | |
9 | * This program is distributed in the hope that it will be useful, * | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
12 | * GNU General Public License for more details. * | |
13 | * * | |
14 | * You should have received a copy of the GNU General Public License * | |
15 | * along with this program; if not, write to the * | |
16 | * Free Software Foundation, Inc., * | |
22bbabf6 | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
ef79bbde P |
18 | ***************************************************************************/ |
19 | ||
22bbabf6 | 20 | /* |
ef79bbde P |
21 | * Handles all CD-ROM registers and functions. |
22 | */ | |
23 | ||
24 | #include "cdrom.h" | |
25 | #include "ppf.h" | |
9f8b032d | 26 | #include "psxdma.h" |
8f2bb0cb | 27 | #include "arm_features.h" |
ef79bbde | 28 | |
8b380e97 | 29 | /* logging */ |
30 | #if 0 | |
31 | #define CDR_LOG SysPrintf | |
32 | #else | |
33 | #define CDR_LOG(...) | |
34 | #endif | |
35 | #if 0 | |
36 | #define CDR_LOG_I SysPrintf | |
37 | #else | |
38 | #define CDR_LOG_I(...) | |
39 | #endif | |
40 | #if 0 | |
41 | #define CDR_LOG_IO SysPrintf | |
42 | #else | |
43 | #define CDR_LOG_IO(...) | |
44 | #endif | |
45 | //#define CDR_LOG_CMD_IRQ | |
46 | ||
ef79bbde | 47 | cdrStruct cdr; |
8e1040b6 | 48 | static unsigned char *pTransfer; |
f4627eb3 | 49 | static s16 read_buf[CD_FRAMESIZE_RAW/2]; |
ef79bbde P |
50 | |
51 | /* CD-ROM magic numbers */ | |
1f035e27 | 52 | #define CdlSync 0 /* nocash documentation : "Uh, actually, returns error code 40h = Invalid Command...?" */ |
ef79bbde P |
53 | #define CdlNop 1 |
54 | #define CdlSetloc 2 | |
55 | #define CdlPlay 3 | |
56 | #define CdlForward 4 | |
57 | #define CdlBackward 5 | |
58 | #define CdlReadN 6 | |
59 | #define CdlStandby 7 | |
60 | #define CdlStop 8 | |
61 | #define CdlPause 9 | |
b0bd140d | 62 | #define CdlReset 10 |
ef79bbde P |
63 | #define CdlMute 11 |
64 | #define CdlDemute 12 | |
65 | #define CdlSetfilter 13 | |
66 | #define CdlSetmode 14 | |
dcd72441 | 67 | #define CdlGetparam 15 |
ef79bbde P |
68 | #define CdlGetlocL 16 |
69 | #define CdlGetlocP 17 | |
70 | #define CdlReadT 18 | |
71 | #define CdlGetTN 19 | |
72 | #define CdlGetTD 20 | |
73 | #define CdlSeekL 21 | |
74 | #define CdlSeekP 22 | |
75 | #define CdlSetclock 23 | |
76 | #define CdlGetclock 24 | |
77 | #define CdlTest 25 | |
78 | #define CdlID 26 | |
79 | #define CdlReadS 27 | |
b0bd140d | 80 | #define CdlInit 28 |
e4268a49 | 81 | #define CdlGetQ 29 |
ef79bbde P |
82 | #define CdlReadToc 30 |
83 | ||
ef79bbde P |
84 | char *CmdName[0x100]= { |
85 | "CdlSync", "CdlNop", "CdlSetloc", "CdlPlay", | |
86 | "CdlForward", "CdlBackward", "CdlReadN", "CdlStandby", | |
b0bd140d | 87 | "CdlStop", "CdlPause", "CdlReset", "CdlMute", |
dcd72441 | 88 | "CdlDemute", "CdlSetfilter", "CdlSetmode", "CdlGetparam", |
ef79bbde P |
89 | "CdlGetlocL", "CdlGetlocP", "CdlReadT", "CdlGetTN", |
90 | "CdlGetTD", "CdlSeekL", "CdlSeekP", "CdlSetclock", | |
91 | "CdlGetclock", "CdlTest", "CdlID", "CdlReadS", | |
b0bd140d | 92 | "CdlInit", NULL, "CDlReadToc", NULL |
ef79bbde P |
93 | }; |
94 | ||
95 | unsigned char Test04[] = { 0 }; | |
96 | unsigned char Test05[] = { 0 }; | |
97 | unsigned char Test20[] = { 0x98, 0x06, 0x10, 0xC3 }; | |
98 | unsigned char Test22[] = { 0x66, 0x6F, 0x72, 0x20, 0x45, 0x75, 0x72, 0x6F }; | |
99 | unsigned char Test23[] = { 0x43, 0x58, 0x44, 0x32, 0x39 ,0x34, 0x30, 0x51 }; | |
100 | ||
9f8b032d | 101 | // cdr.Stat: |
102 | #define NoIntr 0 | |
103 | #define DataReady 1 | |
104 | #define Complete 2 | |
105 | #define Acknowledge 3 | |
106 | #define DataEnd 4 | |
107 | #define DiskError 5 | |
108 | ||
109 | /* Modes flags */ | |
110 | #define MODE_SPEED (1<<7) // 0x80 | |
111 | #define MODE_STRSND (1<<6) // 0x40 ADPCM on/off | |
112 | #define MODE_SIZE_2340 (1<<5) // 0x20 | |
113 | #define MODE_SIZE_2328 (1<<4) // 0x10 | |
cd0c9f5b | 114 | #define MODE_SIZE_2048 (0<<4) // 0x00 |
9f8b032d | 115 | #define MODE_SF (1<<3) // 0x08 channel on/off |
116 | #define MODE_REPORT (1<<2) // 0x04 | |
117 | #define MODE_AUTOPAUSE (1<<1) // 0x02 | |
118 | #define MODE_CDDA (1<<0) // 0x01 | |
119 | ||
120 | /* Status flags */ | |
121 | #define STATUS_PLAY (1<<7) // 0x80 | |
122 | #define STATUS_SEEK (1<<6) // 0x40 | |
123 | #define STATUS_READ (1<<5) // 0x20 | |
124 | #define STATUS_SHELLOPEN (1<<4) // 0x10 | |
125 | #define STATUS_UNKNOWN3 (1<<3) // 0x08 | |
126 | #define STATUS_UNKNOWN2 (1<<2) // 0x04 | |
127 | #define STATUS_ROTATING (1<<1) // 0x02 | |
128 | #define STATUS_ERROR (1<<0) // 0x01 | |
129 | ||
e4268a49 | 130 | /* Errors */ |
003cfc63 | 131 | #define ERROR_NOTREADY (1<<7) // 0x80 |
e4268a49 | 132 | #define ERROR_INVALIDCMD (1<<6) // 0x40 |
133 | #define ERROR_INVALIDARG (1<<5) // 0x20 | |
9f8b032d | 134 | |
ef79bbde P |
135 | // 1x = 75 sectors per second |
136 | // PSXCLK = 1 sec in the ps | |
137 | // so (PSXCLK / 75) = cdr read time (linuzappz) | |
138 | #define cdReadTime (PSXCLK / 75) | |
139 | ||
80f91a20 | 140 | enum drive_state { |
141 | DRIVESTATE_STANDBY = 0, | |
142 | DRIVESTATE_LID_OPEN, | |
143 | DRIVESTATE_RESCAN_CD, | |
144 | DRIVESTATE_PREPARE_CD, | |
e4268a49 | 145 | DRIVESTATE_STOPPED, |
80f91a20 | 146 | }; |
147 | ||
39df67df | 148 | // for cdr.Seeked |
149 | enum seeked_state { | |
150 | SEEK_PENDING = 0, | |
151 | SEEK_DONE = 1, | |
39df67df | 152 | }; |
153 | ||
ef79bbde | 154 | static struct CdrStat stat; |
ef79bbde | 155 | |
fffad32e | 156 | static unsigned int msf2sec(const u8 *msf) { |
9f8b032d | 157 | return ((msf[0] * 60 + msf[1]) * 75) + msf[2]; |
158 | } | |
159 | ||
fffad32e | 160 | // for that weird psemu API.. |
161 | static unsigned int fsm2sec(const u8 *msf) { | |
162 | return ((msf[2] * 60 + msf[1]) * 75) + msf[0]; | |
163 | } | |
164 | ||
305c8c93 | 165 | static void sec2msf(unsigned int s, u8 *msf) { |
9f8b032d | 166 | msf[0] = s / 75 / 60; |
167 | s = s - msf[0] * 75 * 60; | |
168 | msf[1] = s / 75; | |
169 | s = s - msf[1] * 75; | |
170 | msf[2] = s; | |
171 | } | |
172 | ||
39df67df | 173 | // cdrInterrupt |
ef79bbde | 174 | #define CDR_INT(eCycle) { \ |
d28b54b1 | 175 | psxRegs.interrupt |= (1 << PSXINT_CDR); \ |
176 | psxRegs.intCycle[PSXINT_CDR].cycle = eCycle; \ | |
177 | psxRegs.intCycle[PSXINT_CDR].sCycle = psxRegs.cycle; \ | |
178 | new_dyna_set_event(PSXINT_CDR, eCycle); \ | |
ae602c19 | 179 | } |
ef79bbde | 180 | |
39df67df | 181 | // cdrReadInterrupt |
ef79bbde | 182 | #define CDREAD_INT(eCycle) { \ |
d28b54b1 | 183 | psxRegs.interrupt |= (1 << PSXINT_CDREAD); \ |
184 | psxRegs.intCycle[PSXINT_CDREAD].cycle = eCycle; \ | |
185 | psxRegs.intCycle[PSXINT_CDREAD].sCycle = psxRegs.cycle; \ | |
186 | new_dyna_set_event(PSXINT_CDREAD, eCycle); \ | |
ae602c19 | 187 | } |
ef79bbde | 188 | |
39df67df | 189 | // cdrLidSeekInterrupt |
9f8b032d | 190 | #define CDRLID_INT(eCycle) { \ |
191 | psxRegs.interrupt |= (1 << PSXINT_CDRLID); \ | |
192 | psxRegs.intCycle[PSXINT_CDRLID].cycle = eCycle; \ | |
193 | psxRegs.intCycle[PSXINT_CDRLID].sCycle = psxRegs.cycle; \ | |
194 | new_dyna_set_event(PSXINT_CDRLID, eCycle); \ | |
195 | } | |
196 | ||
39df67df | 197 | // cdrPlayInterrupt |
198 | #define CDRMISC_INT(eCycle) { \ | |
7f457614 | 199 | psxRegs.interrupt |= (1 << PSXINT_CDRPLAY); \ |
200 | psxRegs.intCycle[PSXINT_CDRPLAY].cycle = eCycle; \ | |
201 | psxRegs.intCycle[PSXINT_CDRPLAY].sCycle = psxRegs.cycle; \ | |
202 | new_dyna_set_event(PSXINT_CDRPLAY, eCycle); \ | |
203 | } | |
204 | ||
ef79bbde P |
205 | #define StopReading() { \ |
206 | if (cdr.Reading) { \ | |
207 | cdr.Reading = 0; \ | |
d28b54b1 | 208 | psxRegs.interrupt &= ~(1 << PSXINT_CDREAD); \ |
ef79bbde | 209 | } \ |
9ffe8d5c | 210 | cdr.StatP &= ~(STATUS_READ|STATUS_SEEK);\ |
ef79bbde P |
211 | } |
212 | ||
213 | #define StopCdda() { \ | |
214 | if (cdr.Play) { \ | |
215 | if (!Config.Cdda) CDR_stop(); \ | |
9f8b032d | 216 | cdr.StatP &= ~STATUS_PLAY; \ |
ef79bbde | 217 | cdr.Play = FALSE; \ |
9f8b032d | 218 | cdr.FastForward = 0; \ |
219 | cdr.FastBackward = 0; \ | |
220 | /*SPU_registerCallback( SPUirq );*/ \ | |
ef79bbde P |
221 | } \ |
222 | } | |
223 | ||
224 | #define SetResultSize(size) { \ | |
94c118c3 | 225 | cdr.ResultP = 0; \ |
ef79bbde P |
226 | cdr.ResultC = size; \ |
227 | cdr.ResultReady = 1; \ | |
228 | } | |
229 | ||
94c118c3 | 230 | static void setIrq(void) |
231 | { | |
232 | if (cdr.Stat & cdr.Reg2) | |
233 | psxHu32ref(0x1070) |= SWAP32((u32)0x4); | |
234 | } | |
9f8b032d | 235 | |
80f91a20 | 236 | // timing used in this function was taken from tests on real hardware |
237 | // (yes it's slow, but you probably don't want to modify it) | |
9f8b032d | 238 | void cdrLidSeekInterrupt() |
239 | { | |
80f91a20 | 240 | switch (cdr.DriveState) { |
241 | default: | |
242 | case DRIVESTATE_STANDBY: | |
9f8b032d | 243 | cdr.StatP &= ~STATUS_SEEK; |
244 | ||
80f91a20 | 245 | if (CDR_getStatus(&stat) == -1) |
246 | return; | |
9f8b032d | 247 | |
80f91a20 | 248 | if (stat.Status & STATUS_SHELLOPEN) |
9f8b032d | 249 | { |
80f91a20 | 250 | StopCdda(); |
251 | cdr.DriveState = DRIVESTATE_LID_OPEN; | |
252 | CDRLID_INT(0x800); | |
253 | } | |
254 | break; | |
9f8b032d | 255 | |
80f91a20 | 256 | case DRIVESTATE_LID_OPEN: |
257 | if (CDR_getStatus(&stat) == -1) | |
258 | stat.Status &= ~STATUS_SHELLOPEN; | |
9f8b032d | 259 | |
80f91a20 | 260 | // 02, 12, 10 |
261 | if (!(cdr.StatP & STATUS_SHELLOPEN)) { | |
262 | StopReading(); | |
263 | cdr.StatP |= STATUS_SHELLOPEN; | |
9f8b032d | 264 | |
80f91a20 | 265 | // could generate error irq here, but real hardware |
266 | // only sometimes does that | |
267 | // (not done when lots of commands are sent?) | |
9f8b032d | 268 | |
80f91a20 | 269 | CDRLID_INT(cdReadTime * 30); |
270 | break; | |
9f8b032d | 271 | } |
80f91a20 | 272 | else if (cdr.StatP & STATUS_ROTATING) { |
273 | cdr.StatP &= ~STATUS_ROTATING; | |
274 | } | |
275 | else if (!(stat.Status & STATUS_SHELLOPEN)) { | |
276 | // closed now | |
277 | CheckCdrom(); | |
9f8b032d | 278 | |
80f91a20 | 279 | // cdr.StatP STATUS_SHELLOPEN is "sticky" |
280 | // and is only cleared by CdlNop | |
9f8b032d | 281 | |
80f91a20 | 282 | cdr.DriveState = DRIVESTATE_RESCAN_CD; |
283 | CDRLID_INT(cdReadTime * 105); | |
284 | break; | |
285 | } | |
9f8b032d | 286 | |
80f91a20 | 287 | // recheck for close |
288 | CDRLID_INT(cdReadTime * 3); | |
289 | break; | |
9f8b032d | 290 | |
80f91a20 | 291 | case DRIVESTATE_RESCAN_CD: |
292 | cdr.StatP |= STATUS_ROTATING; | |
293 | cdr.DriveState = DRIVESTATE_PREPARE_CD; | |
9f8b032d | 294 | |
80f91a20 | 295 | // this is very long on real hardware, over 6 seconds |
296 | // make it a bit faster here... | |
297 | CDRLID_INT(cdReadTime * 150); | |
298 | break; | |
9f8b032d | 299 | |
80f91a20 | 300 | case DRIVESTATE_PREPARE_CD: |
301 | cdr.StatP |= STATUS_SEEK; | |
9f8b032d | 302 | |
80f91a20 | 303 | cdr.DriveState = DRIVESTATE_STANDBY; |
304 | CDRLID_INT(cdReadTime * 26); | |
305 | break; | |
9f8b032d | 306 | } |
307 | } | |
308 | ||
fffad32e | 309 | static void Find_CurTrack(const u8 *time) |
310 | { | |
311 | int current, sect; | |
9f8b032d | 312 | |
fffad32e | 313 | current = msf2sec(time); |
9f8b032d | 314 | |
fffad32e | 315 | for (cdr.CurTrack = 1; cdr.CurTrack < cdr.ResultTN[1]; cdr.CurTrack++) { |
316 | CDR_getTD(cdr.CurTrack + 1, cdr.ResultTD); | |
317 | sect = fsm2sec(cdr.ResultTD); | |
318 | if (sect - current >= 150) | |
9f8b032d | 319 | break; |
9f8b032d | 320 | } |
321 | } | |
322 | ||
73b29eeb | 323 | static void generate_subq(const u8 *time) |
324 | { | |
325 | unsigned char start[3], next[3]; | |
326 | unsigned int this_s, start_s, next_s, pregap; | |
327 | int relative_s; | |
328 | ||
329 | CDR_getTD(cdr.CurTrack, start); | |
330 | if (cdr.CurTrack + 1 <= cdr.ResultTN[1]) { | |
331 | pregap = 150; | |
332 | CDR_getTD(cdr.CurTrack + 1, next); | |
333 | } | |
334 | else { | |
335 | // last track - cd size | |
336 | pregap = 0; | |
337 | next[0] = cdr.SetSectorEnd[2]; | |
338 | next[1] = cdr.SetSectorEnd[1]; | |
339 | next[2] = cdr.SetSectorEnd[0]; | |
340 | } | |
341 | ||
342 | this_s = msf2sec(time); | |
343 | start_s = fsm2sec(start); | |
344 | next_s = fsm2sec(next); | |
345 | ||
346 | cdr.TrackChanged = FALSE; | |
347 | ||
348 | if (next_s - this_s < pregap) { | |
349 | cdr.TrackChanged = TRUE; | |
350 | cdr.CurTrack++; | |
351 | start_s = next_s; | |
352 | } | |
353 | ||
354 | cdr.subq.Index = 1; | |
355 | ||
356 | relative_s = this_s - start_s; | |
357 | if (relative_s < 0) { | |
358 | cdr.subq.Index = 0; | |
359 | relative_s = -relative_s; | |
360 | } | |
361 | sec2msf(relative_s, cdr.subq.Relative); | |
362 | ||
363 | cdr.subq.Track = itob(cdr.CurTrack); | |
364 | cdr.subq.Relative[0] = itob(cdr.subq.Relative[0]); | |
365 | cdr.subq.Relative[1] = itob(cdr.subq.Relative[1]); | |
366 | cdr.subq.Relative[2] = itob(cdr.subq.Relative[2]); | |
367 | cdr.subq.Absolute[0] = itob(time[0]); | |
368 | cdr.subq.Absolute[1] = itob(time[1]); | |
369 | cdr.subq.Absolute[2] = itob(time[2]); | |
370 | } | |
371 | ||
372 | static void ReadTrack(const u8 *time) { | |
fffad32e | 373 | unsigned char tmp[3]; |
374 | struct SubQ *subq; | |
375 | u16 crc; | |
ef79bbde | 376 | |
fffad32e | 377 | tmp[0] = itob(time[0]); |
378 | tmp[1] = itob(time[1]); | |
379 | tmp[2] = itob(time[2]); | |
ef79bbde | 380 | |
fffad32e | 381 | if (memcmp(cdr.Prev, tmp, 3) == 0) |
382 | return; | |
39df67df | 383 | |
fffad32e | 384 | CDR_LOG("ReadTrack *** %02x:%02x:%02x\n", tmp[0], tmp[1], tmp[2]); |
9f8b032d | 385 | |
c9c7a925 | 386 | cdr.NoErr = CDR_readTrack(tmp); |
fffad32e | 387 | memcpy(cdr.Prev, tmp, 3); |
9f8b032d | 388 | |
fffad32e | 389 | if (CheckSBI(time)) |
390 | return; | |
9f8b032d | 391 | |
fffad32e | 392 | subq = (struct SubQ *)CDR_getBufferSub(); |
73b29eeb | 393 | if (subq != NULL && cdr.CurTrack == 1) { |
fffad32e | 394 | crc = calcCrc((u8 *)subq + 12, 10); |
73b29eeb | 395 | if (crc == (((u16)subq->CRC[0] << 8) | subq->CRC[1])) { |
fffad32e | 396 | cdr.subq.Track = subq->TrackNumber; |
397 | cdr.subq.Index = subq->IndexNumber; | |
398 | memcpy(cdr.subq.Relative, subq->TrackRelativeAddress, 3); | |
399 | memcpy(cdr.subq.Absolute, subq->AbsoluteAddress, 3); | |
fffad32e | 400 | } |
92ca1ba6 | 401 | else { |
73b29eeb | 402 | CDR_LOG_I("subq bad crc @%02x:%02x:%02x\n", |
92ca1ba6 | 403 | tmp[0], tmp[1], tmp[2]); |
404 | } | |
9f8b032d | 405 | } |
fffad32e | 406 | else { |
73b29eeb | 407 | generate_subq(time); |
9f8b032d | 408 | } |
9f8b032d | 409 | |
fffad32e | 410 | CDR_LOG(" -> %02x,%02x %02x:%02x:%02x %02x:%02x:%02x\n", |
411 | cdr.subq.Track, cdr.subq.Index, | |
412 | cdr.subq.Relative[0], cdr.subq.Relative[1], cdr.subq.Relative[2], | |
413 | cdr.subq.Absolute[0], cdr.subq.Absolute[1], cdr.subq.Absolute[2]); | |
414 | } | |
9f8b032d | 415 | |
e4268a49 | 416 | static void AddIrqQueue(unsigned short irq, unsigned long ecycle) { |
417 | if (cdr.Irq != 0) { | |
418 | if (irq == cdr.Irq || irq + 0x100 == cdr.Irq) { | |
419 | cdr.IrqRepeated = 1; | |
420 | CDR_INT(ecycle); | |
421 | return; | |
422 | } | |
423 | ||
fffad32e | 424 | CDR_LOG_I("cdr: override cmd %02x -> %02x\n", cdr.Irq, irq); |
e4268a49 | 425 | } |
9f8b032d | 426 | |
fffad32e | 427 | cdr.Irq = irq; |
428 | cdr.eCycle = ecycle; | |
9f8b032d | 429 | |
fffad32e | 430 | CDR_INT(ecycle); |
ef79bbde P |
431 | } |
432 | ||
7f457614 | 433 | static void cdrPlayInterrupt_Autopause() |
434 | { | |
bf4df3ba | 435 | u32 abs_lev_max = 0; |
436 | boolean abs_lev_chselect; | |
437 | u32 i; | |
f4627eb3 | 438 | |
fffad32e | 439 | if ((cdr.Mode & MODE_AUTOPAUSE) && cdr.TrackChanged) { |
7f457614 | 440 | CDR_LOG( "CDDA STOP\n" ); |
7f457614 | 441 | |
442 | // Magic the Gathering | |
443 | // - looping territory cdda | |
444 | ||
445 | // ...? | |
446 | //cdr.ResultReady = 1; | |
447 | //cdr.Stat = DataReady; | |
448 | cdr.Stat = DataEnd; | |
94c118c3 | 449 | setIrq(); |
7f457614 | 450 | |
451 | StopCdda(); | |
452 | } | |
c9c7a925 | 453 | else if (((cdr.Mode & MODE_REPORT) || cdr.FastForward || cdr.FastBackward)) { |
fffad32e | 454 | cdr.Result[0] = cdr.StatP; |
455 | cdr.Result[1] = cdr.subq.Track; | |
456 | cdr.Result[2] = cdr.subq.Index; | |
bf4df3ba | 457 | |
458 | abs_lev_chselect = cdr.subq.Absolute[1] & 0x01; | |
459 | ||
460 | /* 8 is a hack. For accuracy, it should be 588. */ | |
461 | for (i = 0; i < 8; i++) | |
462 | { | |
463 | abs_lev_max = MAX_VALUE(abs_lev_max, abs(read_buf[i * 2 + abs_lev_chselect])); | |
464 | } | |
465 | abs_lev_max = MIN_VALUE(abs_lev_max, 32767); | |
466 | abs_lev_max |= abs_lev_chselect << 15; | |
9b470ab7 | 467 | |
fffad32e | 468 | if (cdr.subq.Absolute[2] & 0x10) { |
469 | cdr.Result[3] = cdr.subq.Relative[0]; | |
470 | cdr.Result[4] = cdr.subq.Relative[1] | 0x80; | |
471 | cdr.Result[5] = cdr.subq.Relative[2]; | |
7f457614 | 472 | } |
fffad32e | 473 | else { |
474 | cdr.Result[3] = cdr.subq.Absolute[0]; | |
475 | cdr.Result[4] = cdr.subq.Absolute[1]; | |
476 | cdr.Result[5] = cdr.subq.Absolute[2]; | |
477 | } | |
478 | ||
bf4df3ba | 479 | cdr.Result[6] = abs_lev_max >> 0; |
480 | cdr.Result[7] = abs_lev_max >> 8; | |
7f457614 | 481 | |
482 | // Rayman: Logo freeze (resultready + dataready) | |
483 | cdr.ResultReady = 1; | |
484 | cdr.Stat = DataReady; | |
485 | ||
486 | SetResultSize(8); | |
94c118c3 | 487 | setIrq(); |
7f457614 | 488 | } |
489 | } | |
490 | ||
39df67df | 491 | // also handles seek |
7f457614 | 492 | void cdrPlayInterrupt() |
493 | { | |
892bd7d4 | 494 | if (cdr.Seeked == SEEK_PENDING) { |
e7e33ef2 | 495 | if (cdr.Stat) { |
8b380e97 | 496 | CDR_LOG_I("cdrom: seek stat hack\n"); |
e7e33ef2 | 497 | CDRMISC_INT(0x1000); |
498 | return; | |
499 | } | |
39df67df | 500 | SetResultSize(1); |
501 | cdr.StatP |= STATUS_ROTATING; | |
502 | cdr.StatP &= ~STATUS_SEEK; | |
503 | cdr.Result[0] = cdr.StatP; | |
892bd7d4 | 504 | cdr.Seeked = SEEK_DONE; |
a34a2445 | 505 | if (cdr.Irq == 0) { |
39df67df | 506 | cdr.Stat = Complete; |
94c118c3 | 507 | setIrq(); |
39df67df | 508 | } |
509 | ||
55b8460a | 510 | if (cdr.SetlocPending) { |
511 | memcpy(cdr.SetSectorPlay, cdr.SetSector, 4); | |
512 | cdr.SetlocPending = 0; | |
dc0ee8d4 | 513 | cdr.m_locationChanged = TRUE; |
55b8460a | 514 | } |
fffad32e | 515 | Find_CurTrack(cdr.SetSectorPlay); |
73b29eeb | 516 | ReadTrack(cdr.SetSectorPlay); |
fffad32e | 517 | cdr.TrackChanged = FALSE; |
39df67df | 518 | } |
519 | ||
520 | if (!cdr.Play) return; | |
7f457614 | 521 | |
7f457614 | 522 | CDR_LOG( "CDDA - %d:%d:%d\n", |
523 | cdr.SetSectorPlay[0], cdr.SetSectorPlay[1], cdr.SetSectorPlay[2] ); | |
8b380e97 | 524 | |
fffad32e | 525 | if (memcmp(cdr.SetSectorPlay, cdr.SetSectorEnd, 3) == 0) { |
526 | StopCdda(); | |
527 | cdr.TrackChanged = TRUE; | |
528 | } | |
ffd2bcfa | 529 | else { |
530 | CDR_readCDDA(cdr.SetSectorPlay[0], cdr.SetSectorPlay[1], cdr.SetSectorPlay[2], (u8 *)read_buf); | |
531 | } | |
7f457614 | 532 | |
892bd7d4 | 533 | if (!cdr.Irq && !cdr.Stat && (cdr.Mode & (MODE_AUTOPAUSE|MODE_REPORT))) |
7f457614 | 534 | cdrPlayInterrupt_Autopause(); |
535 | ||
ffd2bcfa | 536 | if (CDR_readCDDA && !cdr.Muted && !Config.Cdda) { |
ecd502e1 | 537 | cdrAttenuate(read_buf, CD_FRAMESIZE_RAW / 4, 1); |
f4627eb3 | 538 | if (SPU_playCDDAchannel) |
ecd502e1 | 539 | SPU_playCDDAchannel(read_buf, CD_FRAMESIZE_RAW); |
f4627eb3 | 540 | } |
fffad32e | 541 | |
7f457614 | 542 | cdr.SetSectorPlay[2]++; |
543 | if (cdr.SetSectorPlay[2] == 75) { | |
544 | cdr.SetSectorPlay[2] = 0; | |
545 | cdr.SetSectorPlay[1]++; | |
546 | if (cdr.SetSectorPlay[1] == 60) { | |
547 | cdr.SetSectorPlay[1] = 0; | |
548 | cdr.SetSectorPlay[0]++; | |
549 | } | |
550 | } | |
551 | ||
dc0ee8d4 | 552 | if (cdr.m_locationChanged) |
553 | { | |
554 | CDRMISC_INT(cdReadTime * 30); | |
555 | cdr.m_locationChanged = FALSE; | |
556 | } | |
557 | else | |
558 | { | |
559 | CDRMISC_INT(cdReadTime); | |
560 | } | |
fffad32e | 561 | |
562 | // update for CdlGetlocP/autopause | |
73b29eeb | 563 | generate_subq(cdr.SetSectorPlay); |
7f457614 | 564 | } |
565 | ||
ef79bbde | 566 | void cdrInterrupt() { |
e4268a49 | 567 | u16 Irq = cdr.Irq; |
568 | int no_busy_error = 0; | |
569 | int start_rotating = 0; | |
570 | int error = 0; | |
571 | int delay; | |
fbf19ce6 | 572 | unsigned int seekTime = 0; |
2da09dae | 573 | u8 set_loc[3]; |
574 | int i; | |
ef79bbde | 575 | |
9f8b032d | 576 | // Reschedule IRQ |
ef79bbde | 577 | if (cdr.Stat) { |
8b380e97 | 578 | CDR_LOG_I("cdrom: stat hack: %02x %x\n", cdr.Irq, cdr.Stat); |
e7e33ef2 | 579 | CDR_INT(0x1000); |
ef79bbde P |
580 | return; |
581 | } | |
582 | ||
ef79bbde P |
583 | cdr.Ctrl &= ~0x80; |
584 | ||
e4268a49 | 585 | // default response |
586 | SetResultSize(1); | |
587 | cdr.Result[0] = cdr.StatP; | |
588 | cdr.Stat = Acknowledge; | |
589 | ||
590 | if (cdr.IrqRepeated) { | |
591 | cdr.IrqRepeated = 0; | |
592 | if (cdr.eCycle > psxRegs.cycle) { | |
593 | CDR_INT(cdr.eCycle); | |
594 | goto finish; | |
595 | } | |
596 | } | |
597 | ||
598 | cdr.Irq = 0; | |
599 | ||
ef79bbde | 600 | switch (Irq) { |
ef79bbde | 601 | case CdlNop: |
80f91a20 | 602 | if (cdr.DriveState != DRIVESTATE_LID_OPEN) |
603 | cdr.StatP &= ~STATUS_SHELLOPEN; | |
e4268a49 | 604 | no_busy_error = 1; |
ef79bbde P |
605 | break; |
606 | ||
607 | case CdlSetloc: | |
2da09dae | 608 | CDR_LOG("CDROM setloc command (%02X, %02X, %02X)\n", cdr.Param[0], cdr.Param[1], cdr.Param[2]); |
609 | ||
610 | // MM must be BCD, SS must be BCD and <0x60, FF must be BCD and <0x75 | |
611 | if (((cdr.Param[0] & 0x0F) > 0x09) || (cdr.Param[0] > 0x99) || ((cdr.Param[1] & 0x0F) > 0x09) || (cdr.Param[1] >= 0x60) || ((cdr.Param[2] & 0x0F) > 0x09) || (cdr.Param[2] >= 0x75)) | |
612 | { | |
613 | CDR_LOG("Invalid/out of range seek to %02X:%02X:%02X\n", cdr.Param[0], cdr.Param[1], cdr.Param[2]); | |
614 | error = ERROR_INVALIDARG; | |
615 | goto set_error; | |
616 | } | |
617 | else | |
618 | { | |
619 | for (i = 0; i < 3; i++) | |
620 | { | |
621 | set_loc[i] = btoi(cdr.Param[i]); | |
622 | } | |
623 | ||
624 | i = msf2sec(cdr.SetSectorPlay); | |
625 | i = abs(i - msf2sec(set_loc)); | |
626 | if (i > 16) | |
627 | cdr.Seeked = SEEK_PENDING; | |
628 | ||
629 | memcpy(cdr.SetSector, set_loc, 3); | |
630 | cdr.SetSector[3] = 0; | |
631 | cdr.SetlocPending = 1; | |
632 | } | |
ef79bbde P |
633 | break; |
634 | ||
55b8460a | 635 | do_CdlPlay: |
ef79bbde | 636 | case CdlPlay: |
030bd76a | 637 | StopCdda(); |
39df67df | 638 | if (cdr.Seeked == SEEK_PENDING) { |
639 | // XXX: wrong, should seek instead.. | |
39df67df | 640 | cdr.Seeked = SEEK_DONE; |
9f8b032d | 641 | } |
c9c7a925 | 642 | |
643 | cdr.FastBackward = 0; | |
644 | cdr.FastForward = 0; | |
645 | ||
55b8460a | 646 | if (cdr.SetlocPending) { |
647 | memcpy(cdr.SetSectorPlay, cdr.SetSector, 4); | |
648 | cdr.SetlocPending = 0; | |
dc0ee8d4 | 649 | cdr.m_locationChanged = TRUE; |
55b8460a | 650 | } |
9f8b032d | 651 | |
2d5ca6c0 | 652 | // BIOS CD Player |
653 | // - Pause player, hit Track 01/02/../xx (Setloc issued!!) | |
654 | ||
655 | if (cdr.ParamC == 0 || cdr.Param[0] == 0) { | |
656 | CDR_LOG("PLAY Resume @ %d:%d:%d\n", | |
657 | cdr.SetSectorPlay[0], cdr.SetSectorPlay[1], cdr.SetSectorPlay[2]); | |
658 | } | |
659 | else | |
660 | { | |
661 | int track = btoi( cdr.Param[0] ); | |
662 | ||
663 | if (track <= cdr.ResultTN[1]) | |
664 | cdr.CurTrack = track; | |
665 | ||
666 | CDR_LOG("PLAY track %d\n", cdr.CurTrack); | |
667 | ||
668 | if (CDR_getTD((u8)cdr.CurTrack, cdr.ResultTD) != -1) { | |
669 | cdr.SetSectorPlay[0] = cdr.ResultTD[2]; | |
670 | cdr.SetSectorPlay[1] = cdr.ResultTD[1]; | |
671 | cdr.SetSectorPlay[2] = cdr.ResultTD[0]; | |
672 | } | |
673 | } | |
674 | ||
9f8b032d | 675 | /* |
676 | Rayman: detect track changes | |
677 | - fixes logo freeze | |
678 | ||
679 | Twisted Metal 2: skip PREGAP + starting accurate SubQ | |
680 | - plays tracks without retry play | |
681 | ||
682 | Wild 9: skip PREGAP + starting accurate SubQ | |
683 | - plays tracks without retry play | |
684 | */ | |
fffad32e | 685 | Find_CurTrack(cdr.SetSectorPlay); |
73b29eeb | 686 | ReadTrack(cdr.SetSectorPlay); |
fffad32e | 687 | cdr.TrackChanged = FALSE; |
9f8b032d | 688 | |
48aa3a5a | 689 | StopReading(); |
2d5ca6c0 | 690 | if (!Config.Cdda) |
691 | CDR_play(cdr.SetSectorPlay); | |
9f8b032d | 692 | |
693 | // Vib Ribbon: gameplay checks flag | |
694 | cdr.StatP &= ~STATUS_SEEK; | |
ef79bbde | 695 | cdr.Result[0] = cdr.StatP; |
9f8b032d | 696 | |
697 | cdr.StatP |= STATUS_PLAY; | |
9f8b032d | 698 | |
699 | // BIOS player - set flag again | |
700 | cdr.Play = TRUE; | |
701 | ||
39df67df | 702 | CDRMISC_INT( cdReadTime ); |
e4268a49 | 703 | start_rotating = 1; |
ef79bbde P |
704 | break; |
705 | ||
9f8b032d | 706 | case CdlForward: |
e4268a49 | 707 | // TODO: error 80 if stopped |
9f8b032d | 708 | cdr.Stat = Complete; |
709 | ||
9f8b032d | 710 | // GameShark CD Player: Calls 2x + Play 2x |
c9c7a925 | 711 | cdr.FastForward = 1; |
9f8b032d | 712 | cdr.FastBackward = 0; |
ef79bbde P |
713 | break; |
714 | ||
9f8b032d | 715 | case CdlBackward: |
9f8b032d | 716 | cdr.Stat = Complete; |
717 | ||
9f8b032d | 718 | // GameShark CD Player: Calls 2x + Play 2x |
c9c7a925 | 719 | cdr.FastBackward = 1; |
9f8b032d | 720 | cdr.FastForward = 0; |
ef79bbde P |
721 | break; |
722 | ||
9f8b032d | 723 | case CdlStandby: |
e4268a49 | 724 | if (cdr.DriveState != DRIVESTATE_STOPPED) { |
725 | error = ERROR_INVALIDARG; | |
726 | goto set_error; | |
727 | } | |
728 | AddIrqQueue(CdlStandby + 0x100, cdReadTime * 125 / 2); | |
729 | start_rotating = 1; | |
730 | break; | |
731 | ||
732 | case CdlStandby + 0x100: | |
ef79bbde P |
733 | cdr.Stat = Complete; |
734 | break; | |
735 | ||
736 | case CdlStop: | |
030bd76a | 737 | if (cdr.Play) { |
738 | // grab time for current track | |
739 | CDR_getTD((u8)(cdr.CurTrack), cdr.ResultTD); | |
740 | ||
741 | cdr.SetSectorPlay[0] = cdr.ResultTD[2]; | |
742 | cdr.SetSectorPlay[1] = cdr.ResultTD[1]; | |
743 | cdr.SetSectorPlay[2] = cdr.ResultTD[0]; | |
744 | } | |
745 | ||
746 | StopCdda(); | |
747 | StopReading(); | |
748 | ||
e4268a49 | 749 | delay = 0x800; |
750 | if (cdr.DriveState == DRIVESTATE_STANDBY) | |
751 | delay = cdReadTime * 30 / 2; | |
752 | ||
753 | cdr.DriveState = DRIVESTATE_STOPPED; | |
754 | AddIrqQueue(CdlStop + 0x100, delay); | |
755 | break; | |
756 | ||
757 | case CdlStop + 0x100: | |
9f8b032d | 758 | cdr.StatP &= ~STATUS_ROTATING; |
ef79bbde P |
759 | cdr.Result[0] = cdr.StatP; |
760 | cdr.Stat = Complete; | |
ef79bbde P |
761 | break; |
762 | ||
763 | case CdlPause: | |
12228917 | 764 | /* |
765 | Gundam Battle Assault 2: much slower (*) | |
766 | - Fixes boot, gameplay | |
767 | ||
768 | Hokuto no Ken 2: slower | |
769 | - Fixes intro + subtitles | |
770 | ||
771 | InuYasha - Feudal Fairy Tale: slower | |
772 | - Fixes battles | |
773 | */ | |
4e70ea5a | 774 | /* Gameblabla - Tightening the timings (as taken from Duckstation). |
775 | * The timings from Duckstation are based upon hardware tests. | |
776 | * Mednafen's timing don't work for Gundam Battle Assault 2 in PAL/50hz mode, | |
777 | * seems to be timing sensitive as it can depend on the CPU's clock speed. | |
778 | * */ | |
6c192edd | 779 | if (cdr.DriveState == DRIVESTATE_STANDBY) |
e3d555e0 | 780 | { |
4e70ea5a | 781 | delay = 7000; |
e3d555e0 | 782 | } |
783 | else | |
784 | { | |
4e70ea5a | 785 | delay = (((cdr.Mode & MODE_SPEED) ? 2 : 1) * (1000000)); |
e3d555e0 | 786 | CDRMISC_INT((cdr.Mode & MODE_SPEED) ? cdReadTime / 2 : cdReadTime); |
787 | } | |
788 | AddIrqQueue(CdlPause + 0x100, delay); | |
ef79bbde P |
789 | cdr.Ctrl |= 0x80; |
790 | break; | |
791 | ||
e4268a49 | 792 | case CdlPause + 0x100: |
9f8b032d | 793 | cdr.StatP &= ~STATUS_READ; |
ef79bbde | 794 | cdr.Result[0] = cdr.StatP; |
9f8b032d | 795 | cdr.Stat = Complete; |
ef79bbde P |
796 | break; |
797 | ||
b0bd140d | 798 | case CdlReset: |
799 | cdr.Muted = FALSE; | |
800 | cdr.Mode = 0x20; /* This fixes This is Football 2, Pooh's Party lockups */ | |
801 | AddIrqQueue(CdlReset + 0x100, 4100000); | |
e4268a49 | 802 | no_busy_error = 1; |
803 | start_rotating = 1; | |
804 | break; | |
805 | ||
b0bd140d | 806 | case CdlReset + 0x100: |
9f8b032d | 807 | cdr.Stat = Complete; |
ef79bbde P |
808 | break; |
809 | ||
9f8b032d | 810 | case CdlMute: |
030bd76a | 811 | cdr.Muted = TRUE; |
ef79bbde P |
812 | break; |
813 | ||
9f8b032d | 814 | case CdlDemute: |
030bd76a | 815 | cdr.Muted = FALSE; |
ef79bbde P |
816 | break; |
817 | ||
9f8b032d | 818 | case CdlSetfilter: |
030bd76a | 819 | cdr.File = cdr.Param[0]; |
820 | cdr.Channel = cdr.Param[1]; | |
9f8b032d | 821 | break; |
ef79bbde P |
822 | |
823 | case CdlSetmode: | |
e4268a49 | 824 | no_busy_error = 1; |
9f8b032d | 825 | break; |
ef79bbde | 826 | |
dcd72441 | 827 | case CdlGetparam: |
828 | /* Gameblabla : According to mednafen, Result size should be 5 and done this way. */ | |
829 | SetResultSize(5); | |
9f8b032d | 830 | cdr.Result[1] = cdr.Mode; |
dcd72441 | 831 | cdr.Result[2] = 0; |
832 | cdr.Result[3] = cdr.File; | |
833 | cdr.Result[4] = cdr.Channel; | |
e4268a49 | 834 | no_busy_error = 1; |
9f8b032d | 835 | break; |
ef79bbde | 836 | |
9f8b032d | 837 | case CdlGetlocL: |
ef79bbde | 838 | SetResultSize(8); |
e4268a49 | 839 | memcpy(cdr.Result, cdr.Transfer, 8); |
9f8b032d | 840 | break; |
ef79bbde P |
841 | |
842 | case CdlGetlocP: | |
fffad32e | 843 | SetResultSize(8); |
844 | memcpy(&cdr.Result, &cdr.subq, 8); | |
e4268a49 | 845 | break; |
892bd7d4 | 846 | |
e4268a49 | 847 | case CdlReadT: // SetSession? |
848 | // really long | |
849 | AddIrqQueue(CdlReadT + 0x100, cdReadTime * 290 / 4); | |
850 | start_rotating = 1; | |
851 | break; | |
852 | ||
853 | case CdlReadT + 0x100: | |
854 | cdr.Stat = Complete; | |
ef79bbde P |
855 | break; |
856 | ||
9f8b032d | 857 | case CdlGetTN: |
ef79bbde | 858 | SetResultSize(3); |
9f8b032d | 859 | if (CDR_getTN(cdr.ResultTN) == -1) { |
ef79bbde | 860 | cdr.Stat = DiskError; |
9f8b032d | 861 | cdr.Result[0] |= STATUS_ERROR; |
862 | } else { | |
863 | cdr.Stat = Acknowledge; | |
864 | cdr.Result[1] = itob(cdr.ResultTN[0]); | |
865 | cdr.Result[2] = itob(cdr.ResultTN[1]); | |
866 | } | |
867 | break; | |
ef79bbde | 868 | |
9f8b032d | 869 | case CdlGetTD: |
ef79bbde P |
870 | cdr.Track = btoi(cdr.Param[0]); |
871 | SetResultSize(4); | |
ef79bbde P |
872 | if (CDR_getTD(cdr.Track, cdr.ResultTD) == -1) { |
873 | cdr.Stat = DiskError; | |
9f8b032d | 874 | cdr.Result[0] |= STATUS_ERROR; |
ef79bbde P |
875 | } else { |
876 | cdr.Stat = Acknowledge; | |
877 | cdr.Result[0] = cdr.StatP; | |
878 | cdr.Result[1] = itob(cdr.ResultTD[2]); | |
879 | cdr.Result[2] = itob(cdr.ResultTD[1]); | |
cfeb7cab | 880 | /* According to Nocash's documentation, the function doesn't care about ff. |
881 | * This can be seen also in Mednafen's implementation. */ | |
882 | //cdr.Result[3] = itob(cdr.ResultTD[0]); | |
ef79bbde P |
883 | } |
884 | break; | |
885 | ||
9f8b032d | 886 | case CdlSeekL: |
39df67df | 887 | case CdlSeekP: |
030bd76a | 888 | StopCdda(); |
889 | StopReading(); | |
9f8b032d | 890 | cdr.StatP |= STATUS_SEEK; |
9f8b032d | 891 | |
892 | /* | |
893 | Crusaders of Might and Magic = 0.5x-4x | |
894 | - fix cutscene speech start | |
895 | ||
896 | Eggs of Steel = 2x-? | |
897 | - fix new game | |
898 | ||
899 | Medievil = ?-4x | |
900 | - fix cutscene speech | |
901 | ||
902 | Rockman X5 = 0.5-4x | |
903 | - fix capcom logo | |
904 | */ | |
39df67df | 905 | CDRMISC_INT(cdr.Seeked == SEEK_DONE ? 0x800 : cdReadTime * 4); |
892bd7d4 | 906 | cdr.Seeked = SEEK_PENDING; |
e4268a49 | 907 | start_rotating = 1; |
ef79bbde P |
908 | break; |
909 | ||
910 | case CdlTest: | |
9f8b032d | 911 | switch (cdr.Param[0]) { |
912 | case 0x20: // System Controller ROM Version | |
ef79bbde P |
913 | SetResultSize(4); |
914 | memcpy(cdr.Result, Test20, 4); | |
915 | break; | |
916 | case 0x22: | |
917 | SetResultSize(8); | |
918 | memcpy(cdr.Result, Test22, 4); | |
919 | break; | |
920 | case 0x23: case 0x24: | |
921 | SetResultSize(8); | |
922 | memcpy(cdr.Result, Test23, 4); | |
923 | break; | |
9f8b032d | 924 | } |
e4268a49 | 925 | no_busy_error = 1; |
ef79bbde P |
926 | break; |
927 | ||
9f8b032d | 928 | case CdlID: |
e4268a49 | 929 | AddIrqQueue(CdlID + 0x100, 20480); |
ef79bbde P |
930 | break; |
931 | ||
e4268a49 | 932 | case CdlID + 0x100: |
ef79bbde | 933 | SetResultSize(8); |
89ec56af | 934 | cdr.Result[0] = cdr.StatP; |
935 | cdr.Result[1] = 0; | |
936 | cdr.Result[2] = 0; | |
937 | cdr.Result[3] = 0; | |
9f8b032d | 938 | |
d8432250 | 939 | // 0x10 - audio | 0x40 - disk missing | 0x80 - unlicensed |
940 | if (CDR_getStatus(&stat) == -1 || stat.Type == 0 || stat.Type == 0xff) { | |
941 | cdr.Result[1] = 0xc0; | |
9f8b032d | 942 | } |
943 | else { | |
89ec56af | 944 | if (stat.Type == 2) |
945 | cdr.Result[1] |= 0x10; | |
946 | if (CdromId[0] == '\0') | |
9f8b032d | 947 | cdr.Result[1] |= 0x80; |
9f8b032d | 948 | } |
89ec56af | 949 | cdr.Result[0] |= (cdr.Result[1] >> 4) & 0x08; |
9f8b032d | 950 | |
ef79bbde P |
951 | strncpy((char *)&cdr.Result[4], "PCSX", 4); |
952 | cdr.Stat = Complete; | |
953 | break; | |
954 | ||
b0bd140d | 955 | case CdlInit: |
e4268a49 | 956 | // yes, it really sets STATUS_SHELLOPEN |
957 | cdr.StatP |= STATUS_SHELLOPEN; | |
958 | cdr.DriveState = DRIVESTATE_RESCAN_CD; | |
959 | CDRLID_INT(20480); | |
960 | no_busy_error = 1; | |
961 | start_rotating = 1; | |
9f8b032d | 962 | break; |
963 | ||
e4268a49 | 964 | case CdlGetQ: |
6360a61b | 965 | no_busy_error = 1; |
9f8b032d | 966 | break; |
967 | ||
968 | case CdlReadToc: | |
e4268a49 | 969 | AddIrqQueue(CdlReadToc + 0x100, cdReadTime * 180 / 4); |
970 | no_busy_error = 1; | |
971 | start_rotating = 1; | |
ef79bbde P |
972 | break; |
973 | ||
e4268a49 | 974 | case CdlReadToc + 0x100: |
9f8b032d | 975 | cdr.Stat = Complete; |
e4268a49 | 976 | no_busy_error = 1; |
ef79bbde P |
977 | break; |
978 | ||
94c118c3 | 979 | case CdlReadN: |
980 | case CdlReadS: | |
55b8460a | 981 | if (cdr.SetlocPending) { |
fbf19ce6 | 982 | seekTime = abs(msf2sec(cdr.SetSectorPlay) - msf2sec(cdr.SetSector)) * (cdReadTime / 200); |
c9c7a925 | 983 | /* |
984 | * Gameblabla : | |
985 | * It was originally set to 1000000 for Driver, however it is not high enough for Worms Pinball | |
986 | * and was unreliable for that game. | |
987 | * I also tested it against Mednafen and Driver's titlescreen music starts 25 frames later, not immediatly. | |
988 | * | |
989 | * Obviously, this isn't perfect but right now, it should be a bit better. | |
990 | * Games to test this against if you change that setting : | |
991 | * - Driver (titlescreen music delay and retry mission) | |
992 | * - Worms Pinball (Will either not boot or crash in the memory card screen) | |
993 | * - Viewpoint (short pauses if the delay in the ingame music is too long) | |
994 | * | |
995 | * It seems that 3386880 * 5 is too much for Driver's titlescreen and it starts skipping. | |
996 | * However, 1000000 is not enough for Worms Pinball to reliably boot. | |
997 | */ | |
998 | if(seekTime > 3386880 * 2) seekTime = 3386880 * 2; | |
55b8460a | 999 | memcpy(cdr.SetSectorPlay, cdr.SetSector, 4); |
1000 | cdr.SetlocPending = 0; | |
dc0ee8d4 | 1001 | cdr.m_locationChanged = TRUE; |
55b8460a | 1002 | } |
1003 | Find_CurTrack(cdr.SetSectorPlay); | |
1004 | ||
1005 | if ((cdr.Mode & MODE_CDDA) && cdr.CurTrack > 1) | |
1006 | // Read* acts as play for cdda tracks in cdda mode | |
1007 | goto do_CdlPlay; | |
1008 | ||
030bd76a | 1009 | cdr.Reading = 1; |
1010 | cdr.FirstSector = 1; | |
ef79bbde | 1011 | |
9f8b032d | 1012 | // Fighting Force 2 - update subq time immediately |
1013 | // - fixes new game | |
55b8460a | 1014 | ReadTrack(cdr.SetSectorPlay); |
9f8b032d | 1015 | |
1016 | ||
1017 | // Crusaders of Might and Magic - update getlocl now | |
1018 | // - fixes cutscene speech | |
1019 | { | |
1020 | u8 *buf = CDR_getBuffer(); | |
a48ae3a7 | 1021 | if (buf != NULL) |
1022 | memcpy(cdr.Transfer, buf, 8); | |
9f8b032d | 1023 | } |
39df67df | 1024 | |
9f8b032d | 1025 | /* |
1026 | Duke Nukem: Land of the Babes - seek then delay read for one frame | |
1027 | - fixes cutscenes | |
914722b2 | 1028 | C-12 - Final Resistance - doesn't like seek |
9f8b032d | 1029 | */ |
c9c7a925 | 1030 | |
1031 | /* | |
1032 | By nicolasnoble from PCSX Redux : | |
1033 | "It LOOKS like this logic is wrong, therefore disabling it with `&& false` for now. | |
1034 | For "PoPoLoCrois Monogatari II", the game logic will soft lock and will never issue GetLocP to detect | |
1035 | the end of its XA streams, as it seems to assume ReadS will not return a status byte with the SEEK | |
1036 | flag set. I think the reasonning is that since it's invalid to call GetLocP while seeking, the game | |
1037 | tries to protect itself against errors by preventing from issuing a GetLocP while it knows the | |
1038 | last status was "seek". But this makes the logic just softlock as it'll never get a notification | |
1039 | about the fact the drive is done seeking and the read actually started. | |
1040 | ||
1041 | In other words, this state machine here is probably wrong in assuming the response to ReadS/ReadN is | |
1042 | done right away. It's rather when it's done seeking, and the read has actually started. This probably | |
1043 | requires a bit more work to make sure seek delays are processed properly. | |
1044 | Checked with a few games, this seems to work fine." | |
1045 | ||
1046 | Gameblabla additional notes : | |
1047 | This still needs the "+ seekTime" that PCSX Redux doesn't have for the Driver "retry" mission error. | |
1048 | */ | |
1049 | cdr.StatP |= STATUS_READ; | |
1050 | cdr.StatP &= ~STATUS_SEEK; | |
9f8b032d | 1051 | |
c9c7a925 | 1052 | CDREAD_INT(((cdr.Mode & 0x80) ? (cdReadTime) : cdReadTime * 2) + seekTime); |
ef79bbde | 1053 | |
9f8b032d | 1054 | cdr.Result[0] = cdr.StatP; |
e4268a49 | 1055 | start_rotating = 1; |
ef79bbde | 1056 | break; |
1f035e27 | 1057 | case CdlSync: |
ef79bbde | 1058 | default: |
e4268a49 | 1059 | CDR_LOG_I("Invalid command: %02x\n", Irq); |
1060 | error = ERROR_INVALIDCMD; | |
1061 | // FALLTHROUGH | |
1062 | ||
1063 | set_error: | |
1064 | SetResultSize(2); | |
1065 | cdr.Result[0] = cdr.StatP | STATUS_ERROR; | |
1066 | cdr.Result[1] = error; | |
1067 | cdr.Stat = DiskError; | |
ef79bbde P |
1068 | break; |
1069 | } | |
1070 | ||
e4268a49 | 1071 | if (cdr.DriveState == DRIVESTATE_STOPPED && start_rotating) { |
1072 | cdr.DriveState = DRIVESTATE_STANDBY; | |
1073 | cdr.StatP |= STATUS_ROTATING; | |
1074 | } | |
1075 | ||
1076 | if (!no_busy_error) { | |
80f91a20 | 1077 | switch (cdr.DriveState) { |
1078 | case DRIVESTATE_LID_OPEN: | |
1079 | case DRIVESTATE_RESCAN_CD: | |
1080 | case DRIVESTATE_PREPARE_CD: | |
1081 | SetResultSize(2); | |
1082 | cdr.Result[0] = cdr.StatP | STATUS_ERROR; | |
003cfc63 | 1083 | cdr.Result[1] = ERROR_NOTREADY; |
80f91a20 | 1084 | cdr.Stat = DiskError; |
1085 | break; | |
1086 | } | |
1087 | } | |
9f8b032d | 1088 | |
e4268a49 | 1089 | finish: |
94c118c3 | 1090 | setIrq(); |
e4268a49 | 1091 | cdr.ParamC = 0; |
ef79bbde | 1092 | |
8b380e97 | 1093 | #ifdef CDR_LOG_CMD_IRQ |
e4268a49 | 1094 | { |
1095 | int i; | |
1096 | SysPrintf("CDR IRQ %d cmd %02x stat %02x: ", | |
1097 | !!(cdr.Stat & cdr.Reg2), Irq, cdr.Stat); | |
1098 | for (i = 0; i < cdr.ResultC; i++) | |
1099 | SysPrintf("%02x ", cdr.Result[i]); | |
1100 | SysPrintf("\n"); | |
1101 | } | |
ef79bbde P |
1102 | #endif |
1103 | } | |
1104 | ||
8f2bb0cb | 1105 | #ifdef HAVE_ARMV7 |
53598a71 | 1106 | #define ssat32_to_16(v) \ |
1107 | asm("ssat %0,#16,%1" : "=r" (v) : "r" (v)) | |
1108 | #else | |
1109 | #define ssat32_to_16(v) do { \ | |
1110 | if (v < -32768) v = -32768; \ | |
1111 | else if (v > 32767) v = 32767; \ | |
1112 | } while (0) | |
1113 | #endif | |
1114 | ||
1115 | void cdrAttenuate(s16 *buf, int samples, int stereo) | |
1116 | { | |
1117 | int i, l, r; | |
1118 | int ll = cdr.AttenuatorLeftToLeft; | |
1119 | int lr = cdr.AttenuatorLeftToRight; | |
1120 | int rl = cdr.AttenuatorRightToLeft; | |
1121 | int rr = cdr.AttenuatorRightToRight; | |
1122 | ||
1123 | if (lr == 0 && rl == 0 && 0x78 <= ll && ll <= 0x88 && 0x78 <= rr && rr <= 0x88) | |
1124 | return; | |
1125 | ||
1126 | if (!stereo && ll == 0x40 && lr == 0x40 && rl == 0x40 && rr == 0x40) | |
1127 | return; | |
1128 | ||
1129 | if (stereo) { | |
1130 | for (i = 0; i < samples; i++) { | |
1131 | l = buf[i * 2]; | |
1132 | r = buf[i * 2 + 1]; | |
1133 | l = (l * ll + r * rl) >> 7; | |
1134 | r = (r * rr + l * lr) >> 7; | |
1135 | ssat32_to_16(l); | |
1136 | ssat32_to_16(r); | |
1137 | buf[i * 2] = l; | |
1138 | buf[i * 2 + 1] = r; | |
1139 | } | |
1140 | } | |
1141 | else { | |
1142 | for (i = 0; i < samples; i++) { | |
1143 | l = buf[i]; | |
1144 | l = l * (ll + rl) >> 7; | |
1145 | //r = r * (rr + lr) >> 7; | |
1146 | ssat32_to_16(l); | |
1147 | //ssat32_to_16(r); | |
1148 | buf[i] = l; | |
1149 | } | |
1150 | } | |
1151 | } | |
1152 | ||
ef79bbde P |
1153 | void cdrReadInterrupt() { |
1154 | u8 *buf; | |
1155 | ||
1156 | if (!cdr.Reading) | |
1157 | return; | |
1158 | ||
9f8b032d | 1159 | if (cdr.Irq || cdr.Stat) { |
8b380e97 | 1160 | CDR_LOG_I("cdrom: read stat hack %02x %x\n", cdr.Irq, cdr.Stat); |
e7e33ef2 | 1161 | CDREAD_INT(0x1000); |
ef79bbde P |
1162 | return; |
1163 | } | |
1164 | ||
9f8b032d | 1165 | cdr.OCUP = 1; |
ef79bbde | 1166 | SetResultSize(1); |
9f8b032d | 1167 | cdr.StatP |= STATUS_READ|STATUS_ROTATING; |
1168 | cdr.StatP &= ~STATUS_SEEK; | |
1169 | cdr.Result[0] = cdr.StatP; | |
39df67df | 1170 | cdr.Seeked = SEEK_DONE; |
ef79bbde | 1171 | |
55b8460a | 1172 | ReadTrack(cdr.SetSectorPlay); |
ef79bbde P |
1173 | |
1174 | buf = CDR_getBuffer(); | |
1175 | if (buf == NULL) | |
c9c7a925 | 1176 | cdr.NoErr = 0; |
ef79bbde | 1177 | |
c9c7a925 | 1178 | if (!cdr.NoErr) { |
8b380e97 | 1179 | CDR_LOG_I("cdrReadInterrupt() Log: err\n"); |
ef79bbde P |
1180 | memset(cdr.Transfer, 0, DATA_SIZE); |
1181 | cdr.Stat = DiskError; | |
9f8b032d | 1182 | cdr.Result[0] |= STATUS_ERROR; |
ef79bbde P |
1183 | CDREAD_INT((cdr.Mode & 0x80) ? (cdReadTime / 2) : cdReadTime); |
1184 | return; | |
1185 | } | |
1186 | ||
1187 | memcpy(cdr.Transfer, buf, DATA_SIZE); | |
1188 | CheckPPFCache(cdr.Transfer, cdr.Prev[0], cdr.Prev[1], cdr.Prev[2]); | |
1189 | ||
ef79bbde | 1190 | |
8b380e97 | 1191 | CDR_LOG("cdrReadInterrupt() Log: cdr.Transfer %x:%x:%x\n", cdr.Transfer[0], cdr.Transfer[1], cdr.Transfer[2]); |
ef79bbde | 1192 | |
9f8b032d | 1193 | if ((!cdr.Muted) && (cdr.Mode & MODE_STRSND) && (!Config.Xa) && (cdr.FirstSector != -1)) { // CD-XA |
1194 | // Firemen 2: Multi-XA files - briefings, cutscenes | |
1195 | if( cdr.FirstSector == 1 && (cdr.Mode & MODE_SF)==0 ) { | |
1196 | cdr.File = cdr.Transfer[4 + 0]; | |
1197 | cdr.Channel = cdr.Transfer[4 + 1]; | |
1198 | } | |
1199 | ||
e73e384c | 1200 | /* Gameblabla |
1201 | * Skips playing on channel 255. | |
1202 | * Fixes missing audio in Blue's Clues : Blue's Big Musical. (Should also fix Taxi 2) | |
1203 | * TODO : Check if this is the proper behaviour. | |
1204 | * */ | |
cd0c9f5b | 1205 | if((cdr.Transfer[4 + 2] & 0x4) && |
9f8b032d | 1206 | (cdr.Transfer[4 + 1] == cdr.Channel) && |
e73e384c | 1207 | (cdr.Transfer[4 + 0] == cdr.File) && cdr.Channel != 255) { |
ef79bbde | 1208 | int ret = xa_decode_sector(&cdr.Xa, cdr.Transfer+4, cdr.FirstSector); |
ef79bbde | 1209 | if (!ret) { |
53598a71 | 1210 | cdrAttenuate(cdr.Xa.pcm, cdr.Xa.nsamples, cdr.Xa.stereo); |
fcaa8d22 | 1211 | /* |
1212 | * Gameblabla - | |
1213 | * This is a hack for Megaman X4, Castlevania etc... | |
1214 | * that regressed from the new m_locationChanged and CDROM timings changes. | |
1215 | * It is mostly noticeable in Castevania however and the stuttering can be very jarring. | |
1216 | * | |
1217 | * According to PCSX redux authors, we shouldn't cause a location change if | |
1218 | * the sector difference is too small. | |
1219 | * I attempted to go with that approach but came empty handed. | |
1220 | * So for now, let's just set cdr.m_locationChanged to false when playing back any ADPCM samples. | |
1221 | * This does not regress Crash Team Racing's intro at least. | |
1222 | */ | |
1223 | cdr.m_locationChanged = FALSE; | |
ef79bbde P |
1224 | SPU_playADPCMchannel(&cdr.Xa); |
1225 | cdr.FirstSector = 0; | |
1226 | } | |
1227 | else cdr.FirstSector = -1; | |
1228 | } | |
1229 | } | |
1230 | ||
55b8460a | 1231 | cdr.SetSectorPlay[2]++; |
1232 | if (cdr.SetSectorPlay[2] == 75) { | |
1233 | cdr.SetSectorPlay[2] = 0; | |
1234 | cdr.SetSectorPlay[1]++; | |
1235 | if (cdr.SetSectorPlay[1] == 60) { | |
1236 | cdr.SetSectorPlay[1] = 0; | |
1237 | cdr.SetSectorPlay[0]++; | |
9f8b032d | 1238 | } |
1239 | } | |
1240 | ||
1241 | cdr.Readed = 0; | |
1242 | ||
dc0ee8d4 | 1243 | uint32_t delay = (cdr.Mode & MODE_SPEED) ? (cdReadTime / 2) : cdReadTime; |
1244 | if (cdr.m_locationChanged) { | |
1245 | CDREAD_INT(delay * 30); | |
1246 | cdr.m_locationChanged = FALSE; | |
1247 | } else { | |
1248 | CDREAD_INT(delay); | |
1249 | } | |
9f8b032d | 1250 | |
1251 | /* | |
1252 | Croc 2: $40 - only FORM1 (*) | |
1253 | Judge Dredd: $C8 - only FORM1 (*) | |
1254 | Sim Theme Park - no adpcm at all (zero) | |
1255 | */ | |
1256 | ||
a34a2445 | 1257 | if (!(cdr.Mode & MODE_STRSND) || !(cdr.Transfer[4+2] & 0x4)) { |
9f8b032d | 1258 | cdr.Stat = DataReady; |
a34a2445 | 1259 | setIrq(); |
ef79bbde | 1260 | } |
9f8b032d | 1261 | |
fffad32e | 1262 | // update for CdlGetlocP |
55b8460a | 1263 | ReadTrack(cdr.SetSectorPlay); |
ef79bbde P |
1264 | } |
1265 | ||
1266 | /* | |
1267 | cdrRead0: | |
94c118c3 | 1268 | bit 0,1 - mode |
ef79bbde P |
1269 | bit 2 - unknown |
1270 | bit 3 - unknown | |
1271 | bit 4 - unknown | |
1272 | bit 5 - 1 result ready | |
1273 | bit 6 - 1 dma ready | |
1274 | bit 7 - 1 command being processed | |
1275 | */ | |
1276 | ||
1277 | unsigned char cdrRead0(void) { | |
1278 | if (cdr.ResultReady) | |
1279 | cdr.Ctrl |= 0x20; | |
1280 | else | |
1281 | cdr.Ctrl &= ~0x20; | |
1282 | ||
1283 | if (cdr.OCUP) | |
1284 | cdr.Ctrl |= 0x40; | |
dda25fa4 | 1285 | // else |
1286 | // cdr.Ctrl &= ~0x40; | |
ef79bbde P |
1287 | |
1288 | // What means the 0x10 and the 0x08 bits? I only saw it used by the bios | |
1289 | cdr.Ctrl |= 0x18; | |
1290 | ||
8b380e97 | 1291 | CDR_LOG_IO("cdr r0: %02x\n", cdr.Ctrl); |
ef79bbde P |
1292 | |
1293 | return psxHu8(0x1800) = cdr.Ctrl; | |
1294 | } | |
1295 | ||
ef79bbde | 1296 | void cdrWrite0(unsigned char rt) { |
8b380e97 | 1297 | CDR_LOG_IO("cdr w0: %02x\n", rt); |
1298 | ||
50b13be9 | 1299 | cdr.Ctrl = (rt & 3) | (cdr.Ctrl & ~3); |
ef79bbde P |
1300 | } |
1301 | ||
1302 | unsigned char cdrRead1(void) { | |
94c118c3 | 1303 | if ((cdr.ResultP & 0xf) < cdr.ResultC) |
9f8b032d | 1304 | psxHu8(0x1801) = cdr.Result[cdr.ResultP & 0xf]; |
94c118c3 | 1305 | else |
ef79bbde | 1306 | psxHu8(0x1801) = 0; |
94c118c3 | 1307 | cdr.ResultP++; |
1308 | if (cdr.ResultP == cdr.ResultC) | |
1309 | cdr.ResultReady = 0; | |
1310 | ||
8b380e97 | 1311 | CDR_LOG_IO("cdr r1: %02x\n", psxHu8(0x1801)); |
1312 | ||
ef79bbde P |
1313 | return psxHu8(0x1801); |
1314 | } | |
1315 | ||
1316 | void cdrWrite1(unsigned char rt) { | |
8b380e97 | 1317 | CDR_LOG_IO("cdr w1: %02x\n", rt); |
9f8b032d | 1318 | |
94c118c3 | 1319 | switch (cdr.Ctrl & 3) { |
1320 | case 0: | |
1321 | break; | |
1322 | case 3: | |
53598a71 | 1323 | cdr.AttenuatorRightToRightT = rt; |
94c118c3 | 1324 | return; |
1325 | default: | |
1326 | return; | |
9f8b032d | 1327 | } |
1328 | ||
9f8b032d | 1329 | cdr.Cmd = rt; |
ef79bbde P |
1330 | cdr.OCUP = 0; |
1331 | ||
8b380e97 | 1332 | #ifdef CDR_LOG_CMD_IRQ |
89ec56af | 1333 | SysPrintf("CD1 write: %x (%s)", rt, CmdName[rt]); |
ef79bbde P |
1334 | if (cdr.ParamC) { |
1335 | SysPrintf(" Param[%d] = {", cdr.ParamC); | |
1336 | for (i = 0; i < cdr.ParamC; i++) | |
1337 | SysPrintf(" %x,", cdr.Param[i]); | |
1338 | SysPrintf("}\n"); | |
1339 | } else { | |
1340 | SysPrintf("\n"); | |
1341 | } | |
1342 | #endif | |
1343 | ||
2daaaae3 | 1344 | cdr.ResultReady = 0; |
94c118c3 | 1345 | cdr.Ctrl |= 0x80; |
1346 | // cdr.Stat = NoIntr; | |
1347 | AddIrqQueue(cdr.Cmd, 0x800); | |
2daaaae3 | 1348 | |
9f8b032d | 1349 | switch (cdr.Cmd) { |
ef79bbde | 1350 | |
94c118c3 | 1351 | case CdlReadN: |
030bd76a | 1352 | case CdlReadS: |
94c118c3 | 1353 | case CdlPause: |
9f8b032d | 1354 | StopCdda(); |
1355 | StopReading(); | |
9f8b032d | 1356 | break; |
1357 | ||
94c118c3 | 1358 | case CdlInit: |
b0bd140d | 1359 | case CdlReset: |
39df67df | 1360 | cdr.Seeked = SEEK_DONE; |
9f8b032d | 1361 | StopCdda(); |
1362 | StopReading(); | |
94c118c3 | 1363 | break; |
9f8b032d | 1364 | |
ef79bbde | 1365 | case CdlSetmode: |
9f8b032d | 1366 | CDR_LOG("cdrWrite1() Log: Setmode %x\n", cdr.Param[0]); |
8b380e97 | 1367 | |
ef79bbde | 1368 | cdr.Mode = cdr.Param[0]; |
9f8b032d | 1369 | |
1370 | // Squaresoft on PlayStation 1998 Collector's CD Vol. 1 | |
1371 | // - fixes choppy movie sound | |
1372 | if( cdr.Play && (cdr.Mode & MODE_CDDA) == 0 ) | |
1373 | StopCdda(); | |
ef79bbde | 1374 | break; |
9f8b032d | 1375 | } |
ef79bbde P |
1376 | } |
1377 | ||
1378 | unsigned char cdrRead2(void) { | |
1379 | unsigned char ret; | |
1380 | ||
1381 | if (cdr.Readed == 0) { | |
1382 | ret = 0; | |
1383 | } else { | |
8e1040b6 | 1384 | ret = *pTransfer++; |
ef79bbde P |
1385 | } |
1386 | ||
8b380e97 | 1387 | CDR_LOG_IO("cdr r2: %02x\n", ret); |
ef79bbde P |
1388 | return ret; |
1389 | } | |
1390 | ||
1391 | void cdrWrite2(unsigned char rt) { | |
8b380e97 | 1392 | CDR_LOG_IO("cdr w2: %02x\n", rt); |
9f8b032d | 1393 | |
94c118c3 | 1394 | switch (cdr.Ctrl & 3) { |
1395 | case 0: | |
1396 | if (cdr.ParamC < 8) // FIXME: size and wrapping | |
1397 | cdr.Param[cdr.ParamC++] = rt; | |
1398 | return; | |
1399 | case 1: | |
1400 | cdr.Reg2 = rt; | |
1401 | setIrq(); | |
1402 | return; | |
1403 | case 2: | |
53598a71 | 1404 | cdr.AttenuatorLeftToLeftT = rt; |
94c118c3 | 1405 | return; |
1406 | case 3: | |
53598a71 | 1407 | cdr.AttenuatorRightToLeftT = rt; |
94c118c3 | 1408 | return; |
ef79bbde P |
1409 | } |
1410 | } | |
1411 | ||
1412 | unsigned char cdrRead3(void) { | |
94c118c3 | 1413 | if (cdr.Ctrl & 0x1) |
1414 | psxHu8(0x1803) = cdr.Stat | 0xE0; | |
1415 | else | |
1416 | psxHu8(0x1803) = cdr.Reg2 | 0xE0; | |
8b380e97 | 1417 | |
1418 | CDR_LOG_IO("cdr r3: %02x\n", psxHu8(0x1803)); | |
ef79bbde P |
1419 | return psxHu8(0x1803); |
1420 | } | |
1421 | ||
1422 | void cdrWrite3(unsigned char rt) { | |
8b380e97 | 1423 | CDR_LOG_IO("cdr w3: %02x\n", rt); |
056d6759 | 1424 | |
94c118c3 | 1425 | switch (cdr.Ctrl & 3) { |
1426 | case 0: | |
a34a2445 | 1427 | break; // transfer |
94c118c3 | 1428 | case 1: |
a34a2445 | 1429 | cdr.Stat &= ~rt; |
1430 | ||
1431 | if (rt & 0x40) | |
1432 | cdr.ParamC = 0; | |
1433 | return; | |
94c118c3 | 1434 | case 2: |
53598a71 | 1435 | cdr.AttenuatorLeftToRightT = rt; |
94c118c3 | 1436 | return; |
1437 | case 3: | |
53598a71 | 1438 | if (rt & 0x20) { |
1439 | memcpy(&cdr.AttenuatorLeftToLeft, &cdr.AttenuatorLeftToLeftT, 4); | |
1440 | CDR_LOG_I("CD-XA Volume: %02x %02x | %02x %02x\n", | |
1441 | cdr.AttenuatorLeftToLeft, cdr.AttenuatorLeftToRight, | |
1442 | cdr.AttenuatorRightToLeft, cdr.AttenuatorRightToRight); | |
1443 | } | |
94c118c3 | 1444 | return; |
9f8b032d | 1445 | } |
056d6759 | 1446 | |
94c118c3 | 1447 | if ((rt & 0x80) && cdr.Readed == 0) { |
ef79bbde | 1448 | cdr.Readed = 1; |
8e1040b6 | 1449 | pTransfer = cdr.Transfer; |
ef79bbde P |
1450 | |
1451 | switch (cdr.Mode & 0x30) { | |
9f8b032d | 1452 | case MODE_SIZE_2328: |
ef79bbde | 1453 | case 0x00: |
8e1040b6 | 1454 | pTransfer += 12; |
ef79bbde | 1455 | break; |
9f8b032d | 1456 | |
1457 | case MODE_SIZE_2340: | |
8e1040b6 | 1458 | pTransfer += 0; |
9f8b032d | 1459 | break; |
1460 | ||
ef79bbde P |
1461 | default: |
1462 | break; | |
1463 | } | |
1464 | } | |
1465 | } | |
1466 | ||
1467 | void psxDma3(u32 madr, u32 bcr, u32 chcr) { | |
1468 | u32 cdsize; | |
c7071c43 | 1469 | int size; |
ef79bbde P |
1470 | u8 *ptr; |
1471 | ||
ef79bbde | 1472 | CDR_LOG("psxDma3() Log: *** DMA 3 *** %x addr = %x size = %x\n", chcr, madr, bcr); |
ef79bbde P |
1473 | |
1474 | switch (chcr) { | |
1475 | case 0x11000000: | |
1476 | case 0x11400100: | |
1477 | if (cdr.Readed == 0) { | |
ef79bbde | 1478 | CDR_LOG("psxDma3() Log: *** DMA 3 *** NOT READY\n"); |
ef79bbde P |
1479 | break; |
1480 | } | |
1481 | ||
1482 | cdsize = (bcr & 0xffff) * 4; | |
1483 | ||
9f8b032d | 1484 | // Ape Escape: bcr = 0001 / 0000 |
1485 | // - fix boot | |
1486 | if( cdsize == 0 ) | |
1487 | { | |
cd0c9f5b | 1488 | switch (cdr.Mode & (MODE_SIZE_2340|MODE_SIZE_2328)) { |
9f8b032d | 1489 | case MODE_SIZE_2340: cdsize = 2340; break; |
cd0c9f5b | 1490 | case MODE_SIZE_2328: cdsize = 2328; break; |
1491 | default: | |
1492 | case MODE_SIZE_2048: cdsize = 2048; break; | |
9f8b032d | 1493 | } |
1494 | } | |
1495 | ||
1496 | ||
ef79bbde P |
1497 | ptr = (u8 *)PSXM(madr); |
1498 | if (ptr == NULL) { | |
ef79bbde | 1499 | CDR_LOG("psxDma3() Log: *** DMA 3 *** NULL Pointer!\n"); |
ef79bbde P |
1500 | break; |
1501 | } | |
9f8b032d | 1502 | |
1503 | /* | |
1504 | GS CDX: Enhancement CD crash | |
1505 | - Setloc 0:0:0 | |
1506 | - CdlPlay | |
1507 | - Spams DMA3 and gets buffer overrun | |
1508 | */ | |
8e1040b6 | 1509 | size = CD_FRAMESIZE_RAW - (pTransfer - cdr.Transfer); |
c7071c43 | 1510 | if (size > cdsize) |
1511 | size = cdsize; | |
1512 | if (size > 0) | |
9f8b032d | 1513 | { |
8e1040b6 | 1514 | memcpy(ptr, pTransfer, size); |
9f8b032d | 1515 | } |
1516 | ||
ef79bbde | 1517 | psxCpu->Clear(madr, cdsize / 4); |
8e1040b6 | 1518 | pTransfer += cdsize; |
9f8b032d | 1519 | |
9f8b032d | 1520 | if( chcr == 0x11400100 ) { |
58ebb94c | 1521 | HW_DMA3_MADR = SWAPu32(madr + cdsize); |
9f8b032d | 1522 | CDRDMA_INT( (cdsize/4) / 4 ); |
1523 | } | |
1524 | else if( chcr == 0x11000000 ) { | |
fc4803bd | 1525 | // CDRDMA_INT( (cdsize/4) * 1 ); |
1526 | // halted | |
1527 | psxRegs.cycle += (cdsize/4) * 24/2; | |
1528 | CDRDMA_INT(16); | |
9f8b032d | 1529 | } |
1530 | return; | |
1531 | ||
ef79bbde | 1532 | default: |
ef79bbde | 1533 | CDR_LOG("psxDma3() Log: Unknown cddma %x\n", chcr); |
ef79bbde P |
1534 | break; |
1535 | } | |
1536 | ||
1537 | HW_DMA3_CHCR &= SWAP32(~0x01000000); | |
1538 | DMA_INTERRUPT(3); | |
1539 | } | |
1540 | ||
9f8b032d | 1541 | void cdrDmaInterrupt() |
1542 | { | |
ad418c19 | 1543 | if (HW_DMA3_CHCR & SWAP32(0x01000000)) |
1544 | { | |
1545 | HW_DMA3_CHCR &= SWAP32(~0x01000000); | |
1546 | DMA_INTERRUPT(3); | |
1547 | } | |
9f8b032d | 1548 | } |
1549 | ||
fffad32e | 1550 | static void getCdInfo(void) |
1551 | { | |
1552 | u8 tmp; | |
1553 | ||
1554 | CDR_getTN(cdr.ResultTN); | |
1555 | CDR_getTD(0, cdr.SetSectorEnd); | |
1556 | tmp = cdr.SetSectorEnd[0]; | |
1557 | cdr.SetSectorEnd[0] = cdr.SetSectorEnd[2]; | |
1558 | cdr.SetSectorEnd[2] = tmp; | |
1559 | } | |
1560 | ||
ef79bbde P |
1561 | void cdrReset() { |
1562 | memset(&cdr, 0, sizeof(cdr)); | |
1563 | cdr.CurTrack = 1; | |
1564 | cdr.File = 1; | |
1565 | cdr.Channel = 1; | |
a2d3ccb8 | 1566 | cdr.Reg2 = 0x1f; |
1567 | cdr.Stat = NoIntr; | |
80f91a20 | 1568 | cdr.DriveState = DRIVESTATE_STANDBY; |
22bbabf6 | 1569 | cdr.StatP = STATUS_ROTATING; |
8e1040b6 | 1570 | pTransfer = cdr.Transfer; |
c9c7a925 | 1571 | |
056d6759 | 1572 | // BIOS player - default values |
53598a71 | 1573 | cdr.AttenuatorLeftToLeft = 0x80; |
1574 | cdr.AttenuatorLeftToRight = 0x00; | |
1575 | cdr.AttenuatorRightToLeft = 0x00; | |
1576 | cdr.AttenuatorRightToRight = 0x80; | |
fffad32e | 1577 | |
1578 | getCdInfo(); | |
ef79bbde P |
1579 | } |
1580 | ||
496d88d4 | 1581 | int cdrFreeze(void *f, int Mode) { |
8e1040b6 | 1582 | u32 tmp; |
fffad32e | 1583 | u8 tmpp[3]; |
ef79bbde | 1584 | |
fffad32e | 1585 | if (Mode == 0 && !Config.Cdda) |
1586 | CDR_stop(); | |
9f8b032d | 1587 | |
55b8460a | 1588 | cdr.freeze_ver = 0x63647202; |
9f8b032d | 1589 | gzfreeze(&cdr, sizeof(cdr)); |
1590 | ||
94c118c3 | 1591 | if (Mode == 1) { |
1592 | cdr.ParamP = cdr.ParamC; | |
8e1040b6 | 1593 | tmp = pTransfer - cdr.Transfer; |
94c118c3 | 1594 | } |
ef79bbde P |
1595 | |
1596 | gzfreeze(&tmp, sizeof(tmp)); | |
1597 | ||
42f3c512 | 1598 | if (Mode == 0) { |
fffad32e | 1599 | getCdInfo(); |
1600 | ||
8e1040b6 | 1601 | pTransfer = cdr.Transfer + tmp; |
ef79bbde | 1602 | |
fffad32e | 1603 | // read right sub data |
f84d4864 DS |
1604 | tmpp[0] = btoi(cdr.Prev[0]); |
1605 | tmpp[1] = btoi(cdr.Prev[1]); | |
1606 | tmpp[2] = btoi(cdr.Prev[2]); | |
fffad32e | 1607 | cdr.Prev[0]++; |
73b29eeb | 1608 | ReadTrack(tmpp); |
fffad32e | 1609 | |
1610 | if (cdr.Play) { | |
55b8460a | 1611 | if (cdr.freeze_ver < 0x63647202) |
1612 | memcpy(cdr.SetSectorPlay, cdr.SetSector, 3); | |
1613 | ||
fffad32e | 1614 | Find_CurTrack(cdr.SetSectorPlay); |
1615 | if (!Config.Cdda) | |
1616 | CDR_play(cdr.SetSectorPlay); | |
1617 | } | |
c69642c8 | 1618 | |
1619 | if ((cdr.freeze_ver & 0xffffff00) != 0x63647200) { | |
1620 | // old versions did not latch Reg2, have to fixup.. | |
1621 | if (cdr.Reg2 == 0) { | |
1622 | SysPrintf("cdrom: fixing up old savestate\n"); | |
1623 | cdr.Reg2 = 7; | |
1624 | } | |
0f2dee0f | 1625 | // also did not save Attenuator.. |
1626 | if ((cdr.AttenuatorLeftToLeft | cdr.AttenuatorLeftToRight | |
1627 | | cdr.AttenuatorRightToLeft | cdr.AttenuatorRightToRight) == 0) | |
1628 | { | |
1629 | cdr.AttenuatorLeftToLeft = cdr.AttenuatorRightToRight = 0x80; | |
1630 | } | |
c69642c8 | 1631 | } |
42f3c512 | 1632 | } |
1633 | ||
ef79bbde P |
1634 | return 0; |
1635 | } | |
9f8b032d | 1636 | |
1637 | void LidInterrupt() { | |
fffad32e | 1638 | getCdInfo(); |
fffad32e | 1639 | StopCdda(); |
80f91a20 | 1640 | cdrLidSeekInterrupt(); |
9f8b032d | 1641 | } |