cd: switch to CD controller code from genplus
[picodrive.git] / pico / cd / cd_file.c
1 /***********************************************************
2  *                                                         *
3  * This source was taken from the Gens project             *
4  * Written by Stéphane Dallongeville                       *
5  * Copyright (c) 2002 by Stéphane Dallongeville            *
6  * Modified/adapted for PicoDrive by notaz, 2007           *
7  *                                                         *
8  ***********************************************************/
9
10 #include "../pico_int.h"
11 #include "cd_file.h"
12 #include "cue.h"
13
14 //#define cdprintf(f,...) printf(f "\n",##__VA_ARGS__) // tmp
15
16 static void to_upper(char *d, const char *s)
17 {
18         for (; *s != 0; d++, s++) {
19                 if ('a' <= *s && *s <= 'z')
20                         *d = *s - 'a' + 'A';
21                 else
22                         *d = *s;
23         }
24 }
25
26 static int audio_track_mp3(const char *fname, int index)
27 {
28         _scd_track *Tracks = Pico_mcd->TOC.Tracks;
29         FILE *tmp_file;
30         int fs, ret;
31
32         tmp_file = fopen(fname, "rb");
33         if (tmp_file == NULL)
34                 return -1;
35
36         ret = fseek(tmp_file, 0, SEEK_END);
37         fs = ftell(tmp_file);                           // used to calculate length
38         fseek(tmp_file, 0, SEEK_SET);
39
40 #ifdef _PSP_FW_VERSION
41         // some systems (like PSP) can't have many open files at a time,
42         // so we work with their names instead.
43         fclose(tmp_file);
44         tmp_file = (void *) strdup(fname);
45 #endif
46         Tracks[index].KBtps = (short) mp3_get_bitrate(tmp_file, fs);
47         Tracks[index].KBtps >>= 3;
48         if (ret != 0 || Tracks[index].KBtps <= 0)
49         {
50                 elprintf(EL_STATUS, "track %2i: mp3 bitrate %i", index+1, Tracks[index].KBtps);
51 #ifdef _PSP_FW_VERSION
52                 free(tmp_file);
53 #else
54                 fclose(tmp_file);
55 #endif
56                 return -1;
57         }
58
59         Tracks[index].F = tmp_file;
60
61         // MP3 File
62         Tracks[index].ftype = CT_MP3;
63         fs *= 75;
64         fs /= Tracks[index].KBtps * 1000;
65         Tracks[index].Length = fs;
66         Tracks[index].Offset = 0;
67
68         return 0;
69 }
70
71 PICO_INTERNAL int Load_CD_Image(const char *cd_img_name, cd_img_type type)
72 {
73         int i, j, num_track, Cur_LBA, index, ret;
74         int iso_name_len, missed, cd_img_sectors;
75         _scd_track *Tracks = Pico_mcd->TOC.Tracks;
76         char tmp_name[256], tmp_ext[10], tmp_ext_u[10];
77         cue_data_t *cue_data = NULL;
78         pm_file *pmf;
79         static const char *exts[] = {
80                 "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
81                 "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
82         };
83
84         if (PicoCDLoadProgressCB != NULL)
85                 PicoCDLoadProgressCB(cd_img_name, 1);
86
87         Unload_ISO();
88
89         /* is this a .cue? */
90         cue_data = cue_parse(cd_img_name);
91         if (cue_data != NULL) {
92                 cd_img_name = cue_data->tracks[1].fname;
93                 Tracks[0].ftype = cue_data->tracks[1].type;
94         }
95         else
96                 Tracks[0].ftype = type == CIT_BIN ? CT_BIN : CT_ISO;
97
98         Tracks[0].F = pmf = pm_open(cd_img_name);
99         if (Tracks[0].F == NULL)
100         {
101                 Tracks[0].ftype = 0;
102                 Tracks[0].Length = 0;
103                 if (cue_data != NULL)
104                         cue_destroy(cue_data);
105                 return -1;
106         }
107
108         if (Tracks[0].ftype == CT_ISO)
109              cd_img_sectors = pmf->size >>= 11; // size in sectors
110         else cd_img_sectors = pmf->size /= 2352;
111         Tracks[0].Offset = 0;
112
113         Tracks[0].MSF.M = 0; // minutes
114         Tracks[0].MSF.S = 2; // seconds
115         Tracks[0].MSF.F = 0; // frames
116
117         elprintf(EL_STATUS, "Track  1: %02d:%02d:%02d %9i DATA  %s",
118                 Tracks[0].MSF.M, Tracks[0].MSF.S, Tracks[0].MSF.F,
119                 Tracks[0].Length, cd_img_name);
120
121         Cur_LBA = Tracks[0].Length = cd_img_sectors;
122
123         if (cue_data != NULL)
124         {
125                 if (cue_data->tracks[2].fname == NULL) { // NULL means track2 is in same file as track1
126                         Cur_LBA = Tracks[0].Length = cue_data->tracks[2].sector_offset;
127                 }
128                 i = 100 / cue_data->track_count+1;
129                 for (num_track = 2; num_track <= cue_data->track_count; num_track++)
130                 {
131                         if (PicoCDLoadProgressCB != NULL)
132                                 PicoCDLoadProgressCB(cd_img_name, i * num_track);
133                         index = num_track - 1;
134                         Cur_LBA += cue_data->tracks[num_track].pregap;
135                         if (cue_data->tracks[num_track].type == CT_MP3) {
136                                 ret = audio_track_mp3(cue_data->tracks[num_track].fname, index);
137                                 if (ret != 0) break;
138                         }
139                         else
140                         {
141                                 Tracks[index].ftype = cue_data->tracks[num_track].type;
142                                 if (cue_data->tracks[num_track].fname != NULL)
143                                 {
144                                         pm_file *pmfn = pm_open(cue_data->tracks[num_track].fname);
145                                         if (pmfn != NULL)
146                                         {
147                                                 // addume raw, ignore header for wav..
148                                                 Tracks[index].F = pmfn;
149                                                 Tracks[index].Length = pmfn->size / 2352;
150                                                 Tracks[index].Offset = cue_data->tracks[num_track].sector_offset;
151                                         }
152                                         else
153                                         {
154                                                 elprintf(EL_STATUS, "track %2i (%s): can't determine length",
155                                                         num_track, cue_data->tracks[num_track].fname);
156                                                 Tracks[index].Length = 2*75;
157                                                 Tracks[index].Offset = 0;
158                                         }
159                                 }
160                                 else
161                                 {
162                                         if (num_track < cue_data->track_count)
163                                                 Tracks[index].Length = cue_data->tracks[num_track+1].sector_offset -
164                                                         cue_data->tracks[num_track].sector_offset;
165                                         else
166                                                 Tracks[index].Length = cd_img_sectors - cue_data->tracks[num_track].sector_offset;
167                                         Tracks[index].Offset = cue_data->tracks[num_track].sector_offset;
168                                 }
169                         }
170
171                         if (cue_data->tracks[num_track].sector_xlength != 0)
172                                 // overriden by custom cue command
173                                 Tracks[index].Length = cue_data->tracks[num_track].sector_xlength;
174
175                         LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
176                         Cur_LBA += Tracks[index].Length;
177
178                         elprintf(EL_STATUS, "Track %2i: %02d:%02d:%02d %9i AUDIO %s", num_track, Tracks[index].MSF.M,
179                                 Tracks[index].MSF.S, Tracks[index].MSF.F, Tracks[index].Length,
180                                 cue_data->tracks[num_track].fname);
181                 }
182                 cue_destroy(cue_data);
183                 goto finish;
184         }
185
186         /* mp3 track autosearch, Gens-like */
187         iso_name_len = strlen(cd_img_name);
188         if (iso_name_len >= sizeof(tmp_name))
189                 iso_name_len = sizeof(tmp_name) - 1;
190
191         for (num_track = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
192         {
193                 if (PicoCDLoadProgressCB != NULL && i > 1)
194                         PicoCDLoadProgressCB(cd_img_name, i + (100-i)*missed/4);
195
196                 for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
197                 {
198                         int ext_len;
199                         char *p;
200
201                         index = num_track - 1;
202
203                         sprintf(tmp_ext, exts[j], i);
204                         ext_len = strlen(tmp_ext);
205                         to_upper(tmp_ext_u, tmp_ext);
206
207                         memcpy(tmp_name, cd_img_name, iso_name_len + 1);
208                         p = tmp_name + iso_name_len - 4;
209
210                         strcpy(p, tmp_ext);
211                         ret = audio_track_mp3(tmp_name, index);
212                         if (ret != 0) {
213                                 strcpy(p, tmp_ext_u);
214                                 ret = audio_track_mp3(tmp_name, index);
215                         }
216
217                         if (ret != 0 && i > 1 && iso_name_len > ext_len) {
218                                 p = tmp_name + iso_name_len - ext_len;
219                                 strcpy(p, tmp_ext);
220                                 ret = audio_track_mp3(tmp_name, index);
221                                 if (ret != 0) {
222                                         strcpy(p, tmp_ext_u);
223                                         ret = audio_track_mp3(tmp_name, index);
224                                 }
225                         }
226
227                         if (ret == 0)
228                         {
229                                 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
230                                 Cur_LBA += Tracks[index].Length;
231
232                                 elprintf(EL_STATUS, "Track %2i: %02d:%02d:%02d %9i AUDIO - %s", num_track, Tracks[index].MSF.M,
233                                         Tracks[index].MSF.S, Tracks[index].MSF.F, Tracks[index].Length, tmp_name);
234
235                                 num_track++;
236                                 missed = 0;
237                                 break;
238                         }
239                 }
240                 if (ret != 0 && i > 1) missed++;
241         }
242
243 finish:
244         Pico_mcd->TOC.Last_Track = num_track - 1;
245
246         index = num_track - 1;
247
248         LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
249
250         elprintf(EL_STATUS, "End CD -  %02d:%02d:%02d\n", Tracks[index].MSF.M,
251                 Tracks[index].MSF.S, Tracks[index].MSF.F);
252
253         if (PicoCDLoadProgressCB != NULL)
254                 PicoCDLoadProgressCB(cd_img_name, 100);
255
256         return 0;
257 }
258
259
260 PICO_INTERNAL void Unload_ISO(void)
261 {
262         int i;
263
264         if (Pico_mcd == NULL) return;
265
266         if (Pico_mcd->TOC.Tracks[0].F) pm_close(Pico_mcd->TOC.Tracks[0].F);
267
268         for(i = 1; i < 100; i++)
269         {
270                 if (Pico_mcd->TOC.Tracks[i].F != NULL)
271                 {
272                         if (Pico_mcd->TOC.Tracks[i].ftype == CT_MP3)
273 #ifdef _PSP_FW_VERSION
274                                 free(Pico_mcd->TOC.Tracks[i].F);
275 #else
276                                 fclose(Pico_mcd->TOC.Tracks[i].F);
277 #endif
278                         else
279                                 pm_close(Pico_mcd->TOC.Tracks[i].F);
280                 }
281         }
282         memset(Pico_mcd->TOC.Tracks, 0, sizeof(Pico_mcd->TOC.Tracks));
283 }
284
285 #if 1*0
286
287 PICO_INTERNAL int FILE_Read_One_LBA_CDC(void)
288 {
289         if (Pico_mcd->s68k_regs[0x36] & 1)                                      // DATA
290         {
291                 if (Pico_mcd->TOC.Tracks[0].F == NULL) return -1;
292
293                 // moved below..
294                 //fseek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
295                 //fread(cp_buf, 1, 2048, Pico_mcd->TOC.Tracks[0].F);
296
297                 cdprintf("Read file CDC 1 data sector :\n");
298         }
299         else                                                                    // AUDIO
300         {
301                 cdprintf("Read file CDC 1 audio sector :\n");
302         }
303
304         // Update CDC stuff
305
306         CDC_Update_Header();
307
308         if (Pico_mcd->s68k_regs[0x36] & 1)              // DATA track
309         {
310                 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80)             // DECEN = decoding enable
311                 {
312                         if (Pico_mcd->cdc.CTRL.B.B0 & 0x04)     // WRRQ : this bit enable write to buffer
313                         {
314                                 int where_read = 0;
315
316                                 // CAUTION : lookahead bit not implemented
317
318                                 if (Pico_mcd->scd.Cur_LBA < 0)
319                                         where_read = 0;
320                                 else if (Pico_mcd->scd.Cur_LBA >= Pico_mcd->TOC.Tracks[0].Length)
321                                         where_read = Pico_mcd->TOC.Tracks[0].Length - 1;
322                                 else where_read = Pico_mcd->scd.Cur_LBA;
323
324                                 Pico_mcd->scd.Cur_LBA++;
325
326                                 Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF;              // add one sector to WA
327                                 Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF;
328
329                                 *(unsigned int *)(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N) = Pico_mcd->cdc.HEAD.N;
330                                 //memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N + 4], cp_buf, 2048);
331
332                                 //pm_seek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
333                                 //pm_read(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, 2048, Pico_mcd->TOC.Tracks[0].F);
334                                 PicoCDBufferRead(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, where_read);
335
336                                 cdprintf("Read -> WA = %d  Buffer[%d] =", Pico_mcd->cdc.WA.N, Pico_mcd->cdc.PT.N & 0x3FFF);
337                                 cdprintf("Header 1 = %.2X %.2X %.2X %.2X", Pico_mcd->cdc.HEAD.B.B0,
338                                         Pico_mcd->cdc.HEAD.B.B1, Pico_mcd->cdc.HEAD.B.B2, Pico_mcd->cdc.HEAD.B.B3);
339                                 cdprintf("Header 2 = %.2X %.2X %.2X %.2X --- %.2X %.2X\n\n",
340                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 0) & 0x3FFF],
341                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 1) & 0x3FFF],
342                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 2) & 0x3FFF],
343                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 3) & 0x3FFF],
344                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 4) & 0x3FFF],
345                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 5) & 0x3FFF]);
346                         }
347
348                 }
349         }
350         else            // music track
351         {
352                 Pico_mcd->scd.Cur_LBA++;
353
354                 Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF;              // add one sector to WA
355                 Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF;
356
357                 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80)             // DECEN = decoding enable
358                 {
359                         if (Pico_mcd->cdc.CTRL.B.B0 & 0x04)     // WRRQ : this bit enable write to buffer
360                         {
361                                 // CAUTION : lookahead bit not implemented
362
363                                 // this is pretty rough, but oh well - not much depends on this anyway
364                                 memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N], cdda_out_buffer, 2352);
365                         }
366                 }
367         }
368
369         if (Pico_mcd->cdc.CTRL.B.B0 & 0x80)             // DECEN = decoding enable
370         {
371                 Pico_mcd->cdc.STAT.B.B0 = 0x80;
372
373                 if (Pico_mcd->cdc.CTRL.B.B0 & 0x10)     // determine form bit form sub header ?
374                 {
375                         Pico_mcd->cdc.STAT.B.B2 = Pico_mcd->cdc.CTRL.B.B1 & 0x08;
376                 }
377                 else
378                 {
379                         Pico_mcd->cdc.STAT.B.B2 = Pico_mcd->cdc.CTRL.B.B1 & 0x0C;
380                 }
381
382                 if (Pico_mcd->cdc.CTRL.B.B0 & 0x02) Pico_mcd->cdc.STAT.B.B3 = 0x20;     // ECC done
383                 else Pico_mcd->cdc.STAT.B.B3 = 0x00;    // ECC not done
384
385                 if (Pico_mcd->cdc.IFCTRL & 0x20)
386                 {
387                         if (Pico_mcd->s68k_regs[0x33] & (1<<5))
388                         {
389                                 elprintf(EL_INTS, "cdc dec irq 5");
390                                 SekInterruptS68k(5);
391                         }
392
393                         Pico_mcd->cdc.IFSTAT &= ~0x20;          // DEC interrupt happen
394                         Pico_mcd->cdc.Decode_Reg_Read = 0;      // Reset read after DEC int
395                 }
396         }
397
398
399         return 0;
400 }
401
402 #endif