]> Shamusworld >> Repos - virtualjaguar/blob - src/cdrom.cpp
Added FPS counter.
[virtualjaguar] / src / cdrom.cpp
1 //
2 // CD handler
3 //
4 // Originally by David Raingeard
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Extensive rewrites/cleanups/fixes by James Hammons
7 // (C) 2010 Underground Software
8 //
9 // JLH = James Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -------------------------------------------------------------
13 // JLH  01/16/2010  Created this log ;-)
14 //
15
16 #include "cdrom.h"
17
18 #include <string.h>                                                                     // For memset, etc.
19 //#include "jaguar.h"                                                                   // For GET32/SET32 macros
20 //#include "m68k.h"     //???
21 //#include "memory.h"
22 #include "cdintf.h"                                                                     // System agnostic CD interface functions
23 #include "log.h"
24 #include "dac.h"
25
26 //#define CDROM_LOG                                                                     // For CDROM logging, obviously
27
28 /*
29 BUTCH     equ  $DFFF00          ; base of Butch=interrupt control register, R/W
30 DSCNTRL   equ  BUTCH+4          ; DSA control register, R/W
31 DS_DATA   equ  BUTCH+$A         ; DSA TX/RX data, R/W
32 I2CNTRL   equ  BUTCH+$10        ; i2s bus control register, R/W
33 SBCNTRL   equ  BUTCH+$14        ; CD subcode control register, R/W
34 SUBDATA   equ  BUTCH+$18        ; Subcode data register A
35 SUBDATB   equ  BUTCH+$1C        ; Subcode data register B
36 SB_TIME   equ  BUTCH+$20        ; Subcode time and compare enable (D24)
37 FIFO_DATA equ  BUTCH+$24        ; i2s FIFO data
38 I2SDAT1   equ  BUTCH+$24        ; i2s FIFO data
39 I2SDAT2   equ  BUTCH+$28        ; i2s FIFO data
40           equ  BUTCH+$2C        ; CD EEPROM interface
41
42 ;
43 ; Butch's hardware registers
44 ;
45 ;BUTCH     equ  $DFFF00         ;base of Butch=interrupt control register, R/W
46 ;
47 ;  When written (Long):
48 ;
49 ;  bit0 - set to enable interrupts
50 ;  bit1 - enable CD data FIFO half full interrupt
51 ;  bit2 - enable CD subcode frame-time interrupt (@ 2x spped = 7ms.)
52 ;  bit3 - enable pre-set subcode time-match found interrupt
53 ;  bit4 - CD module command transmit buffer empty interrupt
54 ;  bit5 - CD module command receive buffer full
55 ;  bit6 - CIRC failure interrupt
56 ;
57 ;  bit7-31  reserved, set to 0
58 ;
59 ;  When read (Long):
60 ;
61 ;  bit0-8 reserved
62 ;
63 ;  bit9  - CD data FIFO half-full flag pending
64 ;  bit10 - Frame pending
65 ;  bit11 - Subcode data pending
66 ;  bit12 - Command to CD drive pending (trans buffer empty if 1)
67 ;  bit13 - Response from CD drive pending (rec buffer full if 1)
68 ;  bit14 - CD uncorrectable data error pending
69 ;
70 ;   Offsets from BUTCH
71 ;
72 O_DSCNTRL   equ  4              ; DSA control register, R/W
73 O_DS_DATA   equ  $A             ; DSA TX/RX data, R/W
74 ;
75 O_I2CNTRL   equ  $10            ; i2s bus control register, R/W
76 ;
77 ;  When read:
78 ;
79 ;  b0 - I2S data from drive is ON if 1
80 ;  b1 - I2S path to Jerry is ON if 1
81 ;  b2 - reserved
82 ;  b3 - host bus width is 16 if 1, else 32
83 ;  b4 - FIFO state is not empty if 1
84 ;
85 O_SBCNTRL   equ  $14            ; CD subcode control register, R/W
86 O_SUBDATA   equ  $18            ; Subcode data register A
87 O_SUBDATB   equ  $1C            ; Subcode data register B
88 O_SB_TIME   equ  $20            ; Subcode time and compare enable (D24)
89 O_FIFODAT   equ  $24            ; i2s FIFO data
90 O_I2SDAT2   equ  $28            ; i2s FIFO data (old)
91 */
92
93 /*
94 Commands sent through DS_DATA:
95
96 $01nn - ? Play track nn ? Seek to track nn ?
97 $0200 - Stop CD
98 $03nn - Read session nn TOC (short)
99 $0400 - Pause CD
100 $0500 - Unpause CD
101 $10nn - Goto (min?)
102 $11nn - Goto (sec?)
103 $12nn - Goto (frm?)
104 $14nn - Read session nn TOC (full)
105 $15nn - Set CD mode
106 $18nn - Spin up CD to session nn
107 $5000 - ?
108 $5100 - Mute CD (audio mode only)
109 $51FF - Unmute CD (audio mode only)
110 $5400 - Read # of sessions on CD
111 $70nn - Set oversampling mode
112
113 Commands send through serial bus:
114
115 $100 - ? Acknowledge ? (Erase/Write disable)
116 $130 - ? (Seems to always prefix the $14n commands) (Erase/Write enable)
117 $140 - Returns ACK (1) (Write to NVRAM?) (Write selected register)
118 $141 - Returns ACK (1)
119 $142 - Returns ACK (1)
120 $143 - Returns ACK (1)
121 $144 - Returns ACK (1)
122 $145 - Returns ACK (1)
123 $180 - Returns 16-bit value (NVRAM?) (read from EEPROM)
124 $181 - Returns 16-bit value
125 $182 - Returns 16-bit value
126 $183 - Returns 16-bit value
127 $184 - Returns 16-bit value
128 $185 - Returns 16-bit value
129
130 ;  The BUTCH interface for the CD-ROM module is a long-word register,
131 ;   where only the least signifigant 4 bits are used
132 ;
133 eeprom  equ     $DFFF2c                 ;interface to CD-eeprom
134 ;
135 ;  bit3 - busy if 0 after write cmd, or Data In after read cmd 
136 ;  bit2 - Data Out
137 ;  bit1 - clock
138 ;  bit0 - Chip Select (CS)
139 ;
140 ;
141 ;   Commands specific to the National Semiconductor NM93C14
142 ;
143 ;
144 ;  9-bit commands..
145 ;                        876543210
146 eREAD   equ     %110000000              ;read from EEPROM
147 eEWEN   equ     %100110000              ;Erase/write Enable
148 eERASE  equ     %111000000              ;Erase selected register
149 eWRITE  equ     %101000000              ;Write selected register
150 eERAL   equ     %100100000              ;Erase all registers
151 eWRAL   equ     %100010000              ;Writes all registers
152 eEWDS   equ     %100000000              ;Erase/Write disable (default)
153
154 So... are there $40 words of memory? 128 bytes?
155
156 */
157
158 // Private function prototypes
159
160 static void CDROMBusWrite(uint16_t);
161 static uint16_t CDROMBusRead(void);
162
163 #define BUTCH           0x00                            // base of Butch == interrupt control register, R/W
164 #define DSCNTRL         BUTCH + 0x04            // DSA control register, R/W
165 #define DS_DATA         BUTCH + 0x0A            // DSA TX/RX data, R/W
166 #define I2CNTRL         BUTCH + 0x10            // i2s bus control register, R/W
167 #define SBCNTRL         BUTCH + 0x14            // CD subcode control register, R/W
168 #define SUBDATA         BUTCH + 0x18            // Subcode data register A
169 #define SUBDATB         BUTCH + 0x1C            // Subcode data register B
170 #define SB_TIME         BUTCH + 0x20            // Subcode time and compare enable (D24)
171 #define FIFO_DATA       BUTCH + 0x24            // i2s FIFO data
172 #define I2SDAT2         BUTCH + 0x28            // i2s FIFO data (old)
173 #define UNKNOWN         BUTCH + 0x2C            // Seems to be some sort of I2S interface
174
175 const char * BReg[12] = { "BUTCH", "DSCNTRL", "DS_DATA", "???", "I2CNTRL", "SBCNTRL", "SUBDATA", "SUBDATB",
176         "SB_TIME", "FIFO_DATA", "I2SDAT2", "UNKNOWN" };
177 //extern const char * whoName[9];
178
179
180 static uint8_t cdRam[0x100];
181 static uint16_t cdCmd = 0, cdPtr = 0;
182 static bool haveCDGoodness;
183 static uint32_t min, sec, frm, block;
184 static uint8_t cdBuf[2352 + 96];
185 static uint32_t cdBufPtr = 2352;
186 //Also need to set up (save/restore) the CD's NVRAM
187
188
189 //extern bool GetRawTOC(void);
190 void CDROMInit(void)
191 {
192         haveCDGoodness = CDIntfInit();
193
194 //GetRawTOC();
195 /*uint8_t buf[2448];
196 uint32_t sec = 18667 - 150;
197 memset(buf, 0, 2448);
198 if (!CDIntfReadBlock(sec, buf))
199 {
200         WriteLog("CDROM: Attempt to read with subchannel data failed!\n");
201         return;
202 }
203
204 //24x98+96
205 //96=4x24=4x4x6
206 WriteLog("\nCDROM: Read sector %u...\n\n", sec);
207 for(int i=0; i<98; i++)
208 {
209         WriteLog("%04X: ", i*24);
210         for(int j=0; j<24; j++)
211         {
212                 WriteLog("%02X ", buf[j + (i*24)]);
213         }
214         WriteLog("\n");
215 }
216 WriteLog("\nRaw P-W subchannel data:\n\n");
217 for(int i=0; i<6; i++)
218 {
219         WriteLog("%02X: ", i*16);
220         for(int j=0; j<16; j++)
221         {
222                 WriteLog("%02X ", buf[2352 + j + (i*16)]);
223         }
224         WriteLog("\n");
225 }
226 WriteLog("\nP subchannel data: ");
227 for(int i=0; i<96; i+=8)
228 {
229         uint8_t b = 0;
230         for(int j=0; j<8; j++)
231                 b |= ((buf[2352 + i + j] & 0x80) >> 7) << (7 - j);
232
233         WriteLog("%02X ", b);
234 }
235 WriteLog("\nQ subchannel data: ");
236 for(int i=0; i<96; i+=8)
237 {
238         uint8_t b = 0;
239         for(int j=0; j<8; j++)
240                 b |= ((buf[2352 + i + j] & 0x40) >> 6) << (7 - j);
241
242         WriteLog("%02X ", b);
243 }
244 WriteLog("\n\n");//*/
245 }
246
247 void CDROMReset(void)
248 {
249         memset(cdRam, 0x00, 0x100);
250         cdCmd = 0;
251 }
252
253 void CDROMDone(void)
254 {
255         CDIntfDone();
256 }
257
258
259 //
260 // This approach is probably wrong, but let's do it for now.
261 // What's needed is a complete overhaul of the interrupt system so that
262 // interrupts are handled as they're generated--instead of the current
263 // scheme where they're handled on scanline boundaries.
264 //
265 void BUTCHExec(uint32_t cycles)
266 {
267 #if 1
268 // We're chickening out for now...
269 return;
270 #else
271 //      extern uint8_t * jerry_ram_8;                                   // Hmm.
272
273         // For now, we just do the FIFO interrupt. Timing is also likely to be WRONG as well.
274         uint32_t cdState = GET32(cdRam, BUTCH);
275
276         if (!(cdState & 0x01))                                          // No BUTCH interrupts enabled
277                 return;
278
279         if (!(cdState & 0x22))
280                 return;                                                                 // For now, we only handle FIFO/buffer full interrupts...
281
282         // From what I can make out, it seems that each FIFO is 32 bytes long
283
284 //      DSPSetIRQLine(DSPIRQ_EXT, ASSERT_LINE);
285 //I'm *sure* this is wrong--prolly need to generate DSP IRQs as well!
286         if (jerry_ram_8[0x23] & 0x3F)                           // Only generate an IRQ if enabled!
287                 GPUSetIRQLine(GPUIRQ_DSP, ASSERT_LINE);
288 #endif
289 }
290
291
292 //
293 // CD-ROM memory access functions
294 //
295
296 uint8_t CDROMReadByte(uint32_t offset, uint32_t who/*=UNKNOWN*/)
297 {
298 #ifdef CDROM_LOG
299         if ((offset & 0xFF) < 12 * 4)
300                 WriteLog("[%s] ", BReg[(offset & 0xFF) / 4]);
301         WriteLog("CDROM: %s reading byte $%02X from $%08X [68K PC=$%08X]\n", whoName[who], offset, cdRam[offset & 0xFF], m68k_get_reg(NULL, M68K_REG_PC));
302 #endif
303         return cdRam[offset & 0xFF];
304 }
305
306 static uint8_t trackNum = 1, minTrack, maxTrack;
307 //static uint8_t minutes[16] = {  0,  0,  2,  5,  7, 10, 12, 15, 17, 20, 22, 25, 27, 30, 32, 35 };
308 //static uint8_t seconds[16] = {  0,  0, 30,  0, 30,  0, 30,  0, 30,  0, 30,  0, 30,  0, 30,  0 };
309 //static uint8_t frames[16]  = {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 };
310 //static uint16_t sd = 0;
311 uint16_t CDROMReadWord(uint32_t offset, uint32_t who/*=UNKNOWN*/)
312 {
313         offset &= 0xFF;
314
315         uint16_t data = 0x0000;
316
317         if (offset == BUTCH)
318                 data = 0x0000;
319         else if (offset == BUTCH + 2)
320 // We need to fix this so it's not as brain-dead as it is now--i.e., make it so that when
321 // a command is sent to the CDROM, we control here whether or not it succeeded or whether
322 // the command is still being carried out, etc.
323
324 // bit12 - Command to CD drive pending (trans buffer empty if 1)
325 // bit13 - Response from CD drive pending (rec buffer full if 1)
326 //              data = (haveCDGoodness ? 0x3000 : 0x0000);      // DSA RX Interrupt pending bit (0 = pending)
327 //This only returns ACKs for interrupts that are set:
328 //This doesn't work for the initial code that writes $180000 to BUTCH. !!! FIX !!!
329                 data = (haveCDGoodness ? cdRam[BUTCH + 3] << 8 : 0x0000);
330 //      else if (offset == SUBDATA + 2)
331 //              data = sd++ | 0x0010;                                           // Have no idea what this is...
332         else if (offset == DS_DATA && haveCDGoodness)
333         {
334                 if ((cdCmd & 0xFF00) == 0x0100)                         // ???
335                 {
336 //Not sure how to acknowledge the ???...
337 //                      data = 0x0400;//?? 0x0200;
338                         cdPtr++;
339                         switch (cdPtr)
340                         {
341                         case 1:
342                                 data = 0x0000;
343                                 break;
344                         case 2:
345                                 data = 0x0100;
346                                 break;
347                         case 3:
348                                 data = 0x0200;
349                                 break;
350                         case 4:
351                                 data = 0x0300;
352                                 break;
353                         case 5:
354                                 data = 0x0400;
355                         }//*/
356                         WriteLog("CDROM: Reading DS_DATA (???), cdCmd=$%04X\n", cdCmd);
357                 }
358                 else if ((cdCmd & 0xFF00) == 0x0200)                    // Stop CD
359                 {
360 //Not sure how to acknowledge the stop...
361                         data = 0x0400;//?? 0x0200;
362 /*                      cdPtr++;
363                         switch (cdPtr)
364                         {
365                         case 1:
366                                 data = 0x00FF;
367                                 break;
368                         case 2:
369                                 data = 0x01FF;
370                                 break;
371                         case 3:
372                                 data = 0x02FF;
373                                 break;
374                         case 4:
375                                 data = 0x03FF;
376                                 break;
377                         case 5:
378                                 data = 0x0400;
379                         }//*/
380                         WriteLog("CDROM: Reading DS_DATA (stop), cdCmd=$%04X\n", cdCmd);
381                 }
382                 else if ((cdCmd & 0xFF00) == 0x0300)            // Read session TOC (overview?)
383                 {
384
385 /*
386 TOC: [Sess] [adrCtl] [?] [point] [?] [?] [?] [?] [pmin] [psec] [pframe]
387 TOC: 1 10 00 a0 00:00:00 00 01:00:00
388 TOC: 1 10 00 a1 00:00:00 00 01:00:00
389 TOC: 1 10 00 a2 00:00:00 00 03:42:42
390 TOC: 1 10 00  1 00:00:00 00 00:02:00   <-- Track #1
391 TOC: 1 50 00 b0 06:12:42 02 79:59:74
392 TOC: 1 50 00 c0 128:00:32 00 97:18:06
393 TOC: 2 10 00 a0 00:00:00 00 02:00:00
394 TOC: 2 10 00 a1 00:00:00 00 11:00:00
395 TOC: 2 10 00 a2 00:00:00 00 54:32:18
396 TOC: 2 10 00  2 00:00:00 00 06:14:42   <-- Track #2
397 TOC: 2 10 00  3 00:00:00 00 06:24:42   <-- Track #3
398 TOC: 2 10 00  4 00:00:00 00 17:42:00   <-- Track #4
399 TOC: 2 10 00  5 00:00:00 00 22:26:15   <-- Track #5
400 TOC: 2 10 00  6 00:00:00 00 29:50:16   <-- Track #6
401 TOC: 2 10 00  7 00:00:00 00 36:01:49   <-- Track #7
402 TOC: 2 10 00  8 00:00:00 00 40:37:59   <-- Track #8
403 TOC: 2 10 00  9 00:00:00 00 45:13:70   <-- Track #9
404 TOC: 2 10 00  a 00:00:00 00 49:50:06   <-- Track #10
405 TOC: 2 10 00  b 00:00:00 00 54:26:17   <-- Track #11
406 */
407
408 //Should do something like so:
409 //                      data = GetSessionInfo(cdCmd & 0xFF, cdPtr);
410                         data = CDIntfGetSessionInfo(cdCmd & 0xFF, cdPtr);
411                         if (data == 0xFF)       // Failed...
412                         {
413                                 data = 0x0400;
414                                 WriteLog("CDROM: Requested invalid session #%u (or failed to load TOC, or bad cdPtr value)\n", cdCmd & 0xFF);
415                         }
416                         else
417                         {
418                                 data |= (0x20 | cdPtr++) << 8;
419                                 WriteLog("CDROM: Reading DS_DATA (session #%u TOC byte #%u): $%04X\n", cdCmd & 0xFF, cdPtr, data);
420                         }
421
422 /*                      bool isValidSession = ((cdCmd & 0xFF) == 0 ? true : false);//Hardcoded... !!! FIX !!!
423 //NOTE: This should return error condition if the requested session doesn't exist! ($0400?)
424                         if (isValidSession)
425                         {
426                                 cdPtr++;
427                                 switch (cdPtr)
428                                 {
429                                 case 1:
430                                         data = 0x2001;  // Min track for this session?
431                                         break;
432                                 case 2:
433                                         data = 0x210A;  // Max track for this session?
434                                         break;
435                                 case 3:
436                                         data = 0x2219;  // Max lead-out time, absolute minutes
437                                         break;
438                                 case 4:
439                                         data = 0x2319;  // Max lead-out time, absolute seconds
440                                         break;
441                                 case 5:
442                                         data = 0x2419;  // Max lead-out time, absolute frames
443                                         break;
444                                 default:
445                                         data = 0xFFFF;
446
447 //;    +0 - unused, reserved (0)
448 //;    +1 - unused, reserved (0)
449 //;    +2 - minimum track number
450 //;    +3 - maximum track number
451 //;    +4 - total number of sessions
452 //;    +5 - start of last lead-out time, absolute minutes
453 //;    +6 - start of last lead-out time, absolute seconds
454 //;    +7 - start of last lead-out time, absolute frames
455
456                                 }
457                                 WriteLog("CDROM: Reading DS_DATA (session #%u TOC byte #%u): $%04X\n", cdCmd & 0xFF, cdPtr, data);
458                         }
459                         else
460                         {
461                                 data = 0x0400;
462                                 WriteLog("CDROM: Requested invalid session #%u\n", cdCmd & 0xFF);
463                         }*/
464                 }
465                 // Seek to m, s, or f position
466                 else if ((cdCmd & 0xFF00) == 0x1000 || (cdCmd & 0xFF00) == 0x1100 || (cdCmd & 0xFF00) == 0x1200)
467                         data = 0x0100;  // Success, though this doesn't take error handling into account.
468                         // Ideally, we would also set the bits in BUTCH to let the processor know that
469                         // this is ready to be read... !!! FIX !!!
470                 else if ((cdCmd & 0xFF00) == 0x1400)            // Read "full" session TOC
471                 {
472 //Need to be a bit more tricky here, since it's reading the "session" TOC instead of the
473 //full TOC--so we need to check for the min/max tracks for each session here... [DONE]
474
475                         if (trackNum > maxTrack)
476                         {
477                                 data = 0x400;
478 WriteLog("CDROM: Requested invalid track #%u for session #%u\n", trackNum, cdCmd & 0xFF);
479                         }
480                         else
481                         {
482                                 if (cdPtr < 0x62)
483                                         data = (cdPtr << 8) | trackNum;
484                                 else if (cdPtr < 0x65)
485                                         data = (cdPtr << 8) | CDIntfGetTrackInfo(trackNum, (cdPtr - 2) & 0x0F);
486
487 WriteLog("CDROM: Reading DS_DATA (session #%u, full TOC byte #%u): $%04X\n", cdCmd & 0xFF, (cdPtr+1) & 0x0F, data);
488
489                                 cdPtr++;
490                                 if (cdPtr == 0x65)
491                                         cdPtr = 0x60, trackNum++;
492                         }
493
494                         // Note that it seems to return track info in sets of 4 (or is it 5?)
495 /*
496 ;    +0 - track # (must be non-zero)
497 ;    +1 - absolute minutes (0..99), start of track
498 ;    +2 - absolute seconds (0..59), start of track
499 ;    +3 - absolute frames, (0..74), start of track
500 ;    +4 - session # (0..99)
501 ;    +5 - track duration minutes
502 ;    +6 - track duration seconds
503 ;    +7 - track duration frames
504 */
505                         // Seems to be the following format: $60xx -> Track #xx
506                         //                                   $61xx -> min?   (trk?)
507                         //                                   $62xx -> sec?   (min?)
508                         //                                   $63xx -> frame? (sec?)
509                         //                                   $64xx -> ?      (frame?)
510 /*                      cdPtr++;
511                         switch (cdPtr)
512                         {
513                         case 1:
514                                 data = 0x6000 | trackNum;       // Track #
515                                 break;
516                         case 2:
517                                 data = 0x6100 | trackNum;       // Track # (again?)
518                                 break;
519                         case 3:
520                                 data = 0x6200 | minutes[trackNum];      // Minutes
521                                 break;
522                         case 4:
523                                 data = 0x6300 | seconds[trackNum];      // Seconds
524                                 break;
525                         case 5:
526                                 data = 0x6400 | frames[trackNum];               // Frames
527                                 trackNum++;
528                                 cdPtr = 0;
529                         }//*/
530                 }
531                 else if ((cdCmd & 0xFF00) == 0x1500)            // Read CD mode
532                 {
533                         data = cdCmd | 0x0200;  // ?? not sure ?? [Seems OK]
534                         WriteLog("CDROM: Reading DS_DATA (mode), cdCmd=$%04X\n", cdCmd);
535                 }
536                 else if ((cdCmd & 0xFF00) == 0x1800)            // Spin up session #
537                 {
538                         data = cdCmd;
539                         WriteLog("CDROM: Reading DS_DATA (spin up session), cdCmd=$%04X\n", cdCmd);
540                 }
541                 else if ((cdCmd & 0xFF00) == 0x5400)            // Read # of sessions
542                 {
543                         data = cdCmd | 0x00;    // !!! Hardcoded !!! FIX !!!
544                         WriteLog("CDROM: Reading DS_DATA (# of sessions), cdCmd=$%04X\n", cdCmd);
545                 }
546                 else if ((cdCmd & 0xFF00) == 0x7000)            // Read oversampling
547                 {
548 //NOTE: This setting will probably affect the # of DSP interrupts that need to happen. !!! FIX !!!
549                         data = cdCmd;
550                         WriteLog("CDROM: Reading DS_DATA (oversampling), cdCmd=$%04X\n", cdCmd);
551                 }
552                 else
553                 {
554                         data = 0x0400;
555                         WriteLog("CDROM: Reading DS_DATA, unhandled cdCmd=$%04X\n", cdCmd);
556                 }
557         }
558         else if (offset == DS_DATA && !haveCDGoodness)
559                 data = 0x0400;                                                          // No CD interface present, so return error
560         else if (offset >= FIFO_DATA && offset <= FIFO_DATA + 3)
561         {
562         }
563         else if (offset >= FIFO_DATA + 4 && offset <= FIFO_DATA + 7)
564         {
565         }
566         else
567                 data = GET16(cdRam, offset);
568
569 //Returning $00000008 seems to cause it to use the starfield. Dunno why.
570 // It looks like it's getting the CD_mode this way...
571 //Temp, for testing...
572 //Very interesting...! Seems to control sumthin' or other...
573 /*if (offset == 0x2C || offset == 0x2E)
574         data = 0xFFFF;//*/
575 /*if (offset == 0x2C)
576         data = 0x0000;
577 if (offset == 0x2E)
578         data = 0;//0x0008;//*/
579         if (offset == UNKNOWN + 2)
580                 data = CDROMBusRead();
581
582 #ifdef CDROM_LOG
583         if ((offset & 0xFF) < 11 * 4)
584                 WriteLog("[%s] ", BReg[(offset & 0xFF) / 4]);
585         if (offset != UNKNOWN && offset != UNKNOWN + 2)
586                 WriteLog("CDROM: %s reading word $%04X from $%08X [68K PC=$%08X]\n", whoName[who], data, offset, m68k_get_reg(NULL, M68K_REG_PC));
587 #endif
588         return data;
589 }
590
591 void CDROMWriteByte(uint32_t offset, uint8_t data, uint32_t who/*=UNKNOWN*/)
592 {
593         offset &= 0xFF;
594         cdRam[offset] = data;
595
596 #ifdef CDROM_LOG
597         if ((offset & 0xFF) < 12 * 4)
598                 WriteLog("[%s] ", BReg[(offset & 0xFF) / 4]);
599         WriteLog("CDROM: %s writing byte $%02X at $%08X [68K PC=$%08X]\n", whoName[who], data, offset, m68k_get_reg(NULL, M68K_REG_PC));
600 #endif
601 }
602
603 void CDROMWriteWord(uint32_t offset, uint16_t data, uint32_t who/*=UNKNOWN*/)
604 {
605         offset &= 0xFF;
606         SET16(cdRam, offset, data);
607
608         // Command register
609 //Lesse what this does... Seems to work OK...!
610         if (offset == DS_DATA)
611         {
612                 cdCmd = data;
613                 if ((data & 0xFF00) == 0x0200)                          // Stop CD
614                 {
615                         cdPtr = 0;
616                         WriteLog("CDROM: Stopping CD\n", data & 0xFF);
617                 }
618                 else if ((data & 0xFF00) == 0x0300)                     // Read session TOC (short? overview?)
619                 {
620                         cdPtr = 0;
621                         WriteLog("CDROM: Reading TOC for session #%u\n", data & 0xFF);
622                 }
623 //Not sure how these three acknowledge...
624                 else if ((data & 0xFF00) == 0x1000)                     // Seek to minute position
625                 {
626                         min = data & 0x00FF;
627                 }
628                 else if ((data & 0xFF00) == 0x1100)                     // Seek to second position
629                 {
630                         sec = data & 0x00FF;
631                 }
632                 else if ((data & 0xFF00) == 0x1200)                     // Seek to frame position
633                 {
634                         frm = data & 0x00FF;
635                         block = (((min * 60) + sec) * 75) + frm;
636                         cdBufPtr = 2352;                                                // Ensure that SSI read will do so immediately
637                         WriteLog("CDROM: Seeking to %u:%02u:%02u [block #%u]\n", min, sec, frm, block);
638                 }
639                 else if ((data & 0xFF00) == 0x1400)                     // Read "full" TOC for session
640                 {
641                         cdPtr = 0x60,
642                         minTrack = CDIntfGetSessionInfo(data & 0xFF, 0),
643                         maxTrack = CDIntfGetSessionInfo(data & 0xFF, 1);
644                         trackNum = minTrack;
645                         WriteLog("CDROM: Reading \"full\" TOC for session #%u (min=%u, max=%u)\n", data & 0xFF, minTrack, maxTrack);
646                 }
647                 else if ((data & 0xFF00) == 0x1500)                     // Set CDROM mode
648                 {
649                         // Mode setting is as follows: bit 0 set -> single speed, bit 1 set -> double,
650                         // bit 3 set -> multisession CD, bit 3 unset -> audio CD
651                         WriteLog("CDROM: Setting mode $%02X\n", data & 0xFF);
652                 }
653                 else if ((data & 0xFF00) == 0x1800)                     // Spin up session #
654                 {
655                         WriteLog("CDROM: Spinning up session #%u\n", data & 0xFF);
656                 }
657                 else if ((data & 0xFF00) == 0x5400)                     // Read # of sessions
658                 {
659                         WriteLog("CDROM: Reading # of sessions\n", data & 0xFF);
660                 }
661                 else if ((data & 0xFF00) == 0x7000)                     // Set oversampling rate
662                 {
663                         // 1 = none, 2 = 2x, 3 = 4x, 4 = 8x
664                         uint32_t rates[5] = { 0, 1, 2, 4, 8 };
665                         WriteLog("CDROM: Setting oversample rate to %uX\n", rates[(data & 0xFF)]);
666                 }
667                 else
668                         WriteLog("CDROM: Unknown command $%04X\n", data);
669         }//*/
670
671         if (offset == UNKNOWN + 2)
672                 CDROMBusWrite(data);
673
674 #ifdef CDROM_LOG
675         if ((offset & 0xFF) < 11 * 4)
676                 WriteLog("[%s] ", BReg[(offset & 0xFF) / 4]);
677         if (offset != UNKNOWN && offset != UNKNOWN + 2)
678                 WriteLog("CDROM: %s writing word $%04X at $%08X [68K PC=$%08X]\n", whoName[who], data, offset, m68k_get_reg(NULL, M68K_REG_PC));
679 #endif
680 }
681
682 //
683 // State machine for sending/receiving data along a serial bus
684 //
685
686 enum ButchState { ST_INIT, ST_RISING, ST_FALLING };
687 static ButchState currentState = ST_INIT;
688 static uint16_t counter = 0;
689 static bool cmdTx = false;
690 static uint16_t busCmd;
691 static uint16_t rxData, txData;
692 static uint16_t rxDataBit;
693 static bool firstTime = false;
694
695 static void CDROMBusWrite(uint16_t data)
696 {
697 //This is kinda lame. What we should do is check for a 0->1 transition on either bits 0 or 1...
698 //!!! FIX !!!
699
700 #ifdef CDROM_LOG
701         if (data & 0xFFF0)
702                 WriteLog("CDROM: BusWrite write on unknown line: $%04X\n", data);
703 #endif
704
705         switch (currentState)
706         {
707         case ST_INIT:
708                 currentState = ST_RISING;
709                 break;
710         case ST_RISING:
711                 if (data & 0x0001)                                                      // Command coming
712                 {
713                         cmdTx = true;
714                         counter = 0;
715                         busCmd = 0;
716                 }
717                 else
718                 {
719                         if (cmdTx)
720                         {
721                                 busCmd <<= 1;                                           // Make room for next bit
722                                 busCmd |= (data & 0x04);                        // & put it in
723                                 counter++;
724
725                                 if (counter == 9)
726                                 {
727                                         busCmd >>= 2;                                   // Because we ORed bit 2, we need to shift right by 2
728                                         cmdTx = false;
729
730 //What it looks like:
731 //It seems that the $18x series reads from NVRAM while the
732 //$130, $14x, $100 series writes values to NVRAM...
733                                         if (busCmd == 0x180)
734                                                 rxData = 0x0024;//1234;
735                                         else if (busCmd == 0x181)
736                                                 rxData = 0x0004;//5678;
737                                         else if (busCmd == 0x182)
738                                                 rxData = 0x0071;//9ABC;
739                                         else if (busCmd == 0x183)
740                                                 rxData = 0xFF67;//DEF0;
741                                         else if (busCmd == 0x184)
742                                                 rxData = 0xFFFF;//892F;
743                                         else if (busCmd == 0x185)
744                                                 rxData = 0xFFFF;//8000;
745                                         else
746                                                 rxData = 0x0001;
747 //                                              rxData = 0x8349;//8000;//0F67;
748
749                                         counter = 0;
750                                         firstTime = true;
751                                         txData = 0;
752 #ifdef CDROM_LOG
753                                         WriteLog("CDROM: *** BusWrite got command $%04X\n", busCmd);
754 #endif
755                                 }
756                         }
757                         else
758                         {
759                                 txData = (txData << 1) | ((data & 0x04) >> 2);
760 //WriteLog("[%s]", data & 0x04 ? "1" : "0");
761
762                                 rxDataBit = (rxData & 0x8000) >> 12;
763                                 rxData <<= 1;
764                                 counter++;
765 #ifdef CDROM_LOG
766                                 if (counter == 16)
767                                         WriteLog("CDROM: *** BusWrite got extra command $%04X\n", txData);
768 #endif
769                         }
770                 }
771
772                 currentState = ST_FALLING;
773                 break;
774         case ST_FALLING:
775                 currentState = ST_INIT;
776                 break;
777         }
778 }
779
780 static uint16_t CDROMBusRead(void)
781 {
782 // It seems the counter == 0 simply waits for a single bit acknowledge-- !!! FIX !!!
783 // Or does it? Hmm. It still "pumps" 16 bits through above, so how is this special?
784 // Seems to be because it sits and looks at it as if it will change. Dunno!
785 #ifdef CDROM_LOG
786         if ((counter & 0x0F) == 0)
787         {
788                 if (counter == 0 && rxDataBit == 0)
789                 {
790                         if (firstTime)
791                         {
792                                 firstTime = false;
793                                 WriteLog("0...\n");
794                         }
795                 }
796                 else
797                         WriteLog("%s\n", rxDataBit ? "1" : "0");
798         }
799         else
800                 WriteLog("%s", rxDataBit ? "1" : "0");
801 #endif
802
803         return rxDataBit;
804 }
805
806 //
807 // This simulates a read from BUTCH over the SSI to JERRY. Uses real reading!
808 //
809 //temp, until I can fix my CD image... Argh!
810 static uint8_t cdBuf2[2532 + 96], cdBuf3[2532 + 96];
811 uint16_t GetWordFromButchSSI(uint32_t offset, uint32_t who/*= UNKNOWN*/)
812 {
813         bool go = ((offset & 0x0F) == 0x0A || (offset & 0x0F) == 0x0E ? true : false);
814
815         if (!go)
816                 return 0x000;
817
818 // The problem comes in here. Really, we should generate the IRQ once we've stuffed
819 // our values into the DAC L/RRXD ports...
820 // But then again, the whole IRQ system needs an overhaul in order to make it more
821 // cycle accurate WRT to the various CPUs. Right now, it's catch-as-catch-can, which
822 // means that IRQs get serviced on scanline boundaries instead of when they occur.
823         cdBufPtr += 2;
824
825         if (cdBufPtr >= 2352)
826         {
827 WriteLog("CDROM: %s reading block #%u...\n", whoName[who], block);
828                 //No error checking. !!! FIX !!!
829 //NOTE: We have to subtract out the 1st track start as well (in cdintf_foo.cpp)!
830 //              CDIntfReadBlock(block - 150, cdBuf);
831
832 //Crappy kludge for shitty shit. Lesse if it works!
833                 CDIntfReadBlock(block - 150, cdBuf2);
834                 CDIntfReadBlock(block - 149, cdBuf3);
835                 for(int i=0; i<2352-4; i+=4)
836                 {
837                         cdBuf[i+0] = cdBuf2[i+4];
838                         cdBuf[i+1] = cdBuf2[i+5];
839                         cdBuf[i+2] = cdBuf2[i+2];
840                         cdBuf[i+3] = cdBuf2[i+3];
841                 }
842                 cdBuf[2348] = cdBuf3[0];
843                 cdBuf[2349] = cdBuf3[1];
844                 cdBuf[2350] = cdBuf2[2350];
845                 cdBuf[2351] = cdBuf2[2351];//*/
846
847                 block++, cdBufPtr = 0;
848         }
849
850 /*extern bool doDSPDis;
851 if (block == 244968)
852         doDSPDis = true;//*/
853
854 WriteLog("[%04X:%01X]", GET16(cdBuf, cdBufPtr), offset & 0x0F);
855 if (cdBufPtr % 32 == 30)
856         WriteLog("\n");
857
858 //      return GET16(cdBuf, cdBufPtr);
859 //This probably isn't endian safe...
860 // But then again... It seems that even though the data on the CD is organized as
861 // LL LH RL RH the way it expects to see the data is RH RL LH LL.
862 // D'oh! It doesn't matter *how* the data comes in, since it puts each sample into
863 // its own left or right side queue, i.e. it reads them 32 bits at a time and puts
864 // them into their L/R channel queues. It does seem, though, that it expects the
865 // right channel to be the upper 16 bits and the left to be the lower 16.
866         return (cdBuf[cdBufPtr + 1] << 8) | cdBuf[cdBufPtr + 0];
867 }
868
869 bool ButchIsReadyToSend(void)
870 {
871 #ifdef LOG_CDROM_VERBOSE
872 WriteLog("Butch is%s ready to send...\n", cdRam[I2CNTRL + 3] & 0x02 ? "" : " not");
873 #endif
874         return (cdRam[I2CNTRL + 3] & 0x02 ? true : false);
875 }
876
877 //
878 // This simulates a read from BUTCH over the SSI to JERRY. Uses real reading!
879 //
880 void SetSSIWordsXmittedFromButch(void)
881 {
882
883 // The problem comes in here. Really, we should generate the IRQ once we've stuffed
884 // our values into the DAC L/RRXD ports...
885 // But then again, the whole IRQ system needs an overhaul in order to make it more
886 // cycle accurate WRT to the various CPUs. Right now, it's catch-as-catch-can, which
887 // means that IRQs get serviced on scanline boundaries instead of when they occur.
888
889 // NOTE: The CD BIOS uses the following SMODE:
890 //       DAC: M68K writing to SMODE. Bits: WSEN FALLING  [68K PC=00050D8C]
891         cdBufPtr += 4;
892
893         if (cdBufPtr >= 2352)
894         {
895 WriteLog("CDROM: Reading block #%u...\n", block);
896                 //No error checking. !!! FIX !!!
897 //NOTE: We have to subtract out the 1st track start as well (in cdintf_foo.cpp)!
898 //              CDIntfReadBlock(block - 150, cdBuf);
899
900 //Crappy kludge for shitty shit. Lesse if it works!
901 //It does! That means my CD is WRONG! FUCK!
902
903 // But, then again, according to Belboz at AA the two zeroes in front *ARE* necessary...
904 // So that means my CD is OK, just this method is wrong!
905 // It all depends on whether or not the interrupt occurs on the RISING or FALLING edge
906 // of the word strobe... !!! FIX !!!
907
908 // When WS rises, left channel was done transmitting. When WS falls, right channel is done.
909 //              CDIntfReadBlock(block - 150, cdBuf2);
910 //              CDIntfReadBlock(block - 149, cdBuf3);
911                 CDIntfReadBlock(block, cdBuf2);
912                 CDIntfReadBlock(block + 1, cdBuf3);
913                 memcpy(cdBuf, cdBuf2 + 2, 2350);
914                 cdBuf[2350] = cdBuf3[0];
915                 cdBuf[2351] = cdBuf3[1];//*/
916
917                 block++, cdBufPtr = 0;
918
919 /*extern bool doDSPDis;
920 static int foo = 0;
921 if (block == 244968)
922 {
923         foo++;
924 WriteLog("\n***** foo = %u, block = %u *****\n\n", foo, block);
925         if (foo == 2)
926                 doDSPDis = true;
927 }//*/
928         }
929
930
931 WriteLog("[%02X%02X %02X%02X]", cdBuf[cdBufPtr+1], cdBuf[cdBufPtr+0], cdBuf[cdBufPtr+3], cdBuf[cdBufPtr+2]);
932 if (cdBufPtr % 32 == 28)
933         WriteLog("\n");
934
935 //This probably isn't endian safe...
936 // But then again... It seems that even though the data on the CD is organized as
937 // LL LH RL RH the way it expects to see the data is RH RL LH LL.
938 // D'oh! It doesn't matter *how* the data comes in, since it puts each sample into
939 // its own left or right side queue, i.e. it reads them 32 bits at a time and puts
940 // them into their L/R channel queues. It does seem, though, that it expects the
941 // right channel to be the upper 16 bits and the left to be the lower 16.
942
943 // This behavior is strictly a function of *where* the WS creates an IRQ. If the data
944 // is shifted by two zeroes (00 00 in front of the data file) then this *is* the
945 // correct behavior, since the left channel will be xmitted followed by the right
946
947 // Now we have definitive proof: The MYST CD shows a word offset. So that means we have
948 // to figure out how to make that work here *without* having to load 2 sectors, offset, etc.
949 // !!! FIX !!!
950         lrxd = (cdBuf[cdBufPtr + 3] << 8) | cdBuf[cdBufPtr + 2],
951         rrxd = (cdBuf[cdBufPtr + 1] << 8) | cdBuf[cdBufPtr + 0];
952 }
953
954 /*
955 [18667]
956 TOC for MYST
957
958 CDINTF: Disc summary
959         # of sessions: 2, # of tracks: 10
960         Session info:
961         1: min track= 1, max track= 1, lead out= 1:36:67
962         2: min track= 2, max track=10, lead out=55:24:71
963         Track info:
964          1: start= 0:02:00
965          2: start= 4:08:67
966          3: start= 4:16:65
967          4: start= 4:29:19
968          5: start=29:31:03
969          6: start=33:38:50
970          7: start=41:38:60
971          8: start=44:52:18
972          9: start=51:51:22
973         10: start=55:18:73
974
975 CDROM: Read sector 18517 (18667 - 150)...
976
977 0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
978 0018: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
979 0030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
980 0048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
981 0060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
982 0078: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
983 0090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
984 00A8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
985 00C0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
986 00D8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
987 00F0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
988 0108: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
989 0120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
990 0138: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
991 0150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
992 0168: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
993 0180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
994 0198: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
995 01B0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
996 01C8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
997 01E0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
998 01F8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
999 0210: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1000 0228: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1001 0240: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1002 0258: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1003 0270: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1004 0288: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1005 02A0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1006 02B8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1007 02D0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1008 02E8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1009 0300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1010 0318: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1011 0330: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1012 0348: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1013 0360: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1014 0378: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1015 0390: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1016 03A8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1017 03C0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1018 03D8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1019 03F0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1020 0408: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1021 0420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1022 0438: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1023 0450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1024 0468: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1025 0480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1026 0498: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1027 04B0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1028 04C8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1029 04E0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1030 04F8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1031 0510: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1032 0528: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1033 0540: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1034 0558: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1035 0570: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1036 0588: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1037 05A0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1038 05B8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1039 05D0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1040 05E8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1041 0600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1042 0618: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1043 0630: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1044 0648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1045 0660: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1046 0678: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1047 0690: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1048 06A8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1049 06C0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1050 06D8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1051 06F0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1052 0708: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1053 0720: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1054 0738: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1055 0750: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1056 0768: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1057 0780: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1058 0798: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1059 07B0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1060 07C8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00[54 41 49 52]54 41
1061 07E0: 49 52 54 41 49 52 54 41 49 52 54 41 49 52 54 41 49 52 54 41 49 52 54 41
1062 07F8: 49 52 54 41 49 52 54 41 49 52 54 41 49 52 54 41 49 52 54 41 49 52 54 41
1063 0810: 49 52 54 41 49 52[54 41 49 52]54 41 52 41 20 49 50 41 52 50 56 4F 44 45
1064 0828: 44 20 54 41 20 41 45 48 44 41 52 45 41 20 52 54 20 49[00 00 00 50]01 00
1065 0840: 80 83 FC 23 07 00 07 00 F0 00 0C 21 FC 23 07 00 07 00 F1 00 0C A1 FC 33
1066 0858: FF FF F0 00 4E 00 7C 2E 1F 00 FC FF 00 61 08 00 F9 4E 00 00 00 51 E7 48
1067 0870: 00 FE 39 30 F1 00 02 40 40 02 10 00 00 67 1C 00 79 42 01 00 8C D3 3C 34
1068 0888: 37 03 3C 30 81 05 3C 3C 0A 01 3C 38 F1 00 00 60 1A 00 FC 33 01 00 01 00
1069 08A0: 8C D3 3C 34 4B 03 3C 30 65 05 3C 3C 42 01 3C 38 1F 01 C0 33 01 00 88 D3
1070 08B8: C4 33 01 00 8A D3 00 32 41 E2 41 94 7C D4 04 00 7C 92 01 00 41 00 00 04
1071 08D0: C1 33 01 00 82 D3 C1 33 F0 00 3C 00 C2 33 01 00 80 D3 C2 33 F0 00 38 00
1072 08E8: C2 33 F0 00 3A 00 06 3A 44 9A C5 33 01 00 84 D3 44 DC C6 33 01 00 86 D3
1073 0900: F9 33 01 00 84 D3 F0 00 46 00 FC 33 FF FF F0 00 48 00 FC 23 00 00 00 00
1074 0918: F0 00 2A 00 FC 33 00 00 F0 00 58 00 DF 4C 7F 00 75 4E 00 00 00 00 00 00
1075
1076 Raw P-W subchannel data:
1077
1078 00: 80 80 C0 80 80 80 80 C0 80 80 80 80 80 80 C0 80
1079 10: 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80
1080 20: 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 C0
1081 30: 80 80 80 80 80 80 80 80 80 80 80 80 80 C0 80 80
1082 40: 80 80 80 80 C0 80 80 80 80 C0 C0 80 80 C0 C0 80
1083 50: C0 80 80 C0 C0 C0 80 80 C0 80 80 80 C0 80 80 80
1084
1085 P subchannel data: FF FF FF FF FF FF FF FF FF FF FF FF
1086 Q subchannel data: 21 02 00 00 00 01 00 04 08 66 9C 88
1087
1088 Run address: $5000, Length: $18380
1089 */
1090
1091
1092 /*
1093 CD_read function from the CD BIOS: Note that it seems to direct the EXT1 interrupt
1094 to the GPU--so that would mean *any* interrupt that BUTCH generates would be routed
1095 to the GPU...
1096
1097 read:
1098                 btst.l  #31,d0
1099                 bne.w   .play
1100                 subq.l  #4,a0           ; Make up for ISR pre-increment
1101                 move.l  d0,-(sp)
1102                 move.l  BUTCH,d0
1103                 and.l   #$ffff0000,d0
1104                 move.l  d0,BUTCH        ; NO INTERRUPTS!!!!!!!!!!!
1105                 move.l  (sp)+,d0
1106 ;               move.l  #0,BUTCH
1107
1108                 move.w  #$101,J_INT
1109
1110                 move.l  d1,-(sp)
1111                 move.l  I2CNTRL,d1      ;Read I2S Control Register
1112                 bclr    #2,d1           ; Stop data
1113                 move.l  d1,I2CNTRL
1114                 move.l  (sp)+,d1
1115
1116                 move.l  PTRLOC,a2
1117                 move.l  a0,(a2)+
1118                 move.l  a1,(a2)+
1119                 move.l  #0,(a2)+
1120
1121                 btst.b  #7,INITTYPE
1122                 beq     .not_bad
1123                 move.l  PTRLOC,a0
1124                 asl.l   #5,d2
1125
1126                 move.l  d2,-(sp)
1127
1128                 or.l    #$089a3c1a,d2           ; These instructions include the bclr
1129                 move.l  d2,188(a0)
1130
1131                 move.l  (sp)+,d2
1132
1133                 swap    d2
1134                 or.l    #$3c1a1838,d2           ; These instructions include the bclr
1135                 move.l  d2,196(a0)
1136
1137                 move.l  #16,(a2)+
1138                 move.l  d1,(a2)
1139
1140 .not_bad:
1141
1142                 move.w  DS_DATA,d1                      ; Clear any pending DSARX states
1143                 move.l  I2CNTRL,d1                      ; Clear any pending errors
1144
1145 ; Drain the FIFO so that we don't get overloaded
1146
1147 .dump:
1148                 move.l  FIFO_DATA,d1
1149                 move.l  I2CNTRL,d1
1150                 btst    #4,d1
1151                 bne.b   .dump
1152
1153 .butch_go:
1154                 move.l  BUTCH,d1
1155                 and.l   #$FFFF0000,d1
1156                 or.l    #%000100001,d1                   ;Enable DSARX interrupt
1157                 move.l  d1,BUTCH
1158 ;               move.l  #%000100001,BUTCH                ;Enable DSARX interrupt
1159
1160 ; Do a play @
1161
1162 .play:  move.l  d0,d1           ; mess with copy in d1
1163                 lsr.l   #8,d1           ; shift the byte over
1164                 lsr.w   #8,d1
1165                 or.w    #$1000,d1       ; format it for goto
1166                 move.w  d1,DS_DATA      ; DSA tx
1167         bsr.b   DSA_tx
1168
1169                 move.l  d0,d1           ; mess with copy in d1
1170                 lsr.w   #8,d1
1171                 or.w    #$1100,d1       ; format it for goto
1172                 move.w  d1,DS_DATA      ; DSA tx
1173         bsr.b   DSA_tx
1174
1175                 move.l  d0,d1           ; mess with copy in d1
1176                 and.w   #$00FF,d1       ; mask for minutes
1177                 or.w    #$1200,d1       ; format it for goto
1178                 move.w  d1,DS_DATA      ; DSA tx
1179         bsr.b   DSA_tx
1180
1181                 rts
1182
1183
1184 ****************************
1185 * Here's the GPU interrupt *
1186 ****************************
1187
1188 JERRY_ISR:
1189         movei   #G_FLAGS,r30
1190         load    (r30),r29               ;read the flags
1191
1192         movei   #BUTCH,r24
1193
1194 make_ptr:
1195         move    pc,Ptrloc
1196         movei   #(make_ptr-PTRPOS),TEMP
1197         sub     TEMP,Ptrloc
1198
1199 HERE:
1200         move    pc,r25
1201         movei   #(EXIT_ISR-HERE),r27
1202         add     r27,r25
1203
1204 ; Is this a DSARX interrupt?
1205
1206         load    (r24),r27               ;check for DSARX int pending
1207         btst    #13,r27
1208         jr      z,fifo_read                     ; This should ALWAYS fall thru the first time
1209
1210 ; Set the match bit, to allow data
1211 ;       moveq   #3,r26                  ; enable FIFO only
1212 ; Don't just jam a value
1213 ; Clear the DSARX and set FIFO
1214         bclr    #5,r27
1215         bset    #1,r27
1216         store   r27,(r24)
1217         addq    #$10,r24
1218         load    (r24),r27
1219         bset    #2,r27
1220         store   r27,(r24)               ; Disable SUBCODE match
1221
1222 ; Now we clear the DSARX interrupt in Butch
1223
1224         subq    #12,r24                 ; does what the above says
1225         load    (r24),r26               ;Clears DSA pending interrupt
1226         addq    #6,r24
1227         loadw   (r24),r27               ; Read DSA response
1228         btst    #10,r27                 ; Check for error
1229         jr      nz,error
1230         or      r26,r26
1231         jump    (r25)
1232 ;       nop
1233
1234 fifo_read:
1235 ; Check for ERROR!!!!!!!!!!!!!!!!!!!!!
1236         btst    #14,r27
1237         jr      z,noerror
1238         bset    #31,r27
1239 error:
1240         addq    #$10,r24
1241         load    (r24),TEMP
1242         or      TEMP,TEMP
1243         subq    #$10,r24
1244         load    (Ptrloc),TEMP
1245         addq    #8,Ptrloc
1246         store   TEMP,(Ptrloc)
1247         subq    #8,Ptrloc
1248 noerror:
1249         load    (Ptrloc),Dataptr        ;get pointer
1250
1251 ; Check to see if we should stop
1252         addq    #4,Ptrloc
1253         load    (Ptrloc),TEMP
1254         subq    #4,Ptrloc
1255         cmp     Dataptr,TEMP
1256         jr      pl,notend
1257 ;       nop
1258         bclr    #0,r27
1259         store   r27,(r24)
1260
1261 notend:
1262         movei   #FIFO_DATA,CDdata
1263         move    CDdata,r25
1264         addq    #4,CDdata
1265 loptop:
1266         load    (CDdata),TEMP
1267         load    (r25),r30
1268         load    (CDdata),r21
1269         load    (r25),r22
1270         load    (CDdata),r24
1271         load    (r25),r20
1272         load    (CDdata),r19
1273         load    (r25),r18
1274         addq    #4,Dataptr
1275         store   TEMP,(Dataptr)
1276         addqt   #4,Dataptr
1277         store   r30,(Dataptr)
1278         addqt   #4,Dataptr
1279         store   r21,(Dataptr)
1280         addqt   #4,Dataptr
1281         store   r22,(Dataptr)
1282         addqt   #4,Dataptr
1283         store   r24,(Dataptr)
1284         addqt   #4,Dataptr
1285         store   r20,(Dataptr)
1286         addqt   #4,Dataptr
1287         store   r19,(Dataptr)
1288         addqt   #4,Dataptr
1289         store   r18,(Dataptr)
1290
1291         store   Dataptr,(Ptrloc)
1292
1293 exit_isr:
1294         movei   #J_INT,r24      ; Acknowledge in Jerry
1295         moveq   #1,TEMP
1296         bset    #8,TEMP
1297         storew  TEMP,(r24)
1298
1299 .if FLAG
1300 ; Stack r18
1301         load    (r31),r18
1302         addq    #4,r31
1303
1304 ; Stack r19
1305         load    (r31),r19
1306         addq    #4,r31
1307
1308 ; Stack r20
1309         load    (r31),r20
1310         addq    #4,r31
1311
1312 ; Stack r21
1313         load    (r31),r21
1314         addq    #4,r31
1315
1316 ; Stack r22
1317         load    (r31),r22
1318         addq    #4,r31
1319
1320 ; Stack r23
1321         load    (r31),r23
1322         addq    #4,r31
1323
1324 ; Stack r26
1325         load    (r31),r26
1326         addq    #4,r31
1327
1328 ; Stack r27
1329         load    (r31),r27
1330         addq    #4,r31
1331
1332 ; Stack r24
1333         load    (r31),r24
1334         addq    #4,r31
1335
1336 ; Stack r25
1337         load    (r31),r25
1338         addq    #4,r31
1339 .endif
1340
1341         movei   #G_FLAGS,r30
1342
1343 ;r29 already has flags
1344         bclr    #3,r29          ;IMASK
1345         bset    #10,r29         ;Clear DSP int bit in TOM
1346
1347         load    (r31),r28       ;Load return address
1348
1349
1350         addq    #2,r28          ;Fix it up
1351         addq    #4,r31
1352         jump    (r28)           ;Return
1353         store   r29,(r30)       ;Restore broken flags
1354
1355
1356         align long
1357
1358 stackbot:
1359         ds.l    20
1360 STACK:
1361
1362
1363 */
1364