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