get rid of port_config.h
[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 = TYPE_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, iso_name_len, missed, cd_img_sectors;
74         _scd_track *Tracks = Pico_mcd->TOC.Tracks;
75         char tmp_name[1024], tmp_ext[10], tmp_ext_u[10];
76         cue_data_t *cue_data = NULL;
77         pm_file *pmf;
78         static const char *exts[] = {
79                 "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
80                 "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
81         };
82
83         if (PicoCDLoadProgressCB != NULL)
84                 PicoCDLoadProgressCB(cd_img_name, 1);
85
86         Unload_ISO();
87
88         /* is this .cue? */
89         ret = strlen(cd_img_name);
90         if (ret >= 3 && strcasecmp(cd_img_name + ret - 3, "cue") == 0)
91                 cue_data = cue_parse(cd_img_name);
92         if (cue_data != NULL)
93                 cd_img_name = cue_data->tracks[1].fname;
94
95         Tracks[0].ftype = type == CIT_BIN ? TYPE_BIN : TYPE_ISO;
96
97         Tracks[0].F = pmf = pm_open(cd_img_name);
98         if (Tracks[0].F == NULL)
99         {
100                 Tracks[0].ftype = 0;
101                 Tracks[0].Length = 0;
102                 if (cue_data != NULL)
103                         cue_destroy(cue_data);
104                 return -1;
105         }
106
107         if (Tracks[0].ftype == TYPE_ISO)
108              cd_img_sectors = pmf->size >>= 11; // size in sectors
109         else cd_img_sectors = pmf->size /= 2352;
110         Tracks[0].Offset = 0;
111
112         Tracks[0].MSF.M = 0; // minutes
113         Tracks[0].MSF.S = 2; // seconds
114         Tracks[0].MSF.F = 0; // frames
115
116         elprintf(EL_STATUS, "Track  1: %02d:%02d:%02d %9i DATA",
117                 Tracks[0].MSF.M, Tracks[0].MSF.S, Tracks[0].MSF.F, Tracks[0].Length);
118
119         Cur_LBA = Tracks[0].Length = cd_img_sectors;
120
121         if (cue_data != NULL)
122         {
123                 if (cue_data->tracks[2].fname == NULL) { // NULL means track2 is in same file as track1
124                         Cur_LBA = Tracks[0].Length = cue_data->tracks[2].sector_offset;
125                 }
126                 i = 100 / cue_data->track_count+1;
127                 for (num_track = 2; num_track <= cue_data->track_count; num_track++)
128                 {
129                         if (PicoCDLoadProgressCB != NULL)
130                                 PicoCDLoadProgressCB(cd_img_name, i * num_track);
131                         index = num_track - 1;
132                         Cur_LBA += cue_data->tracks[num_track].pregap;
133                         if (cue_data->tracks[num_track].type == CT_MP3) {
134                                 ret = audio_track_mp3(cue_data->tracks[num_track].fname, index);
135                                 if (ret != 0) break;
136                         }
137                         else
138                         {
139                                 Tracks[index].ftype = cue_data->tracks[num_track].type;
140                                 if (cue_data->tracks[num_track].fname != NULL)
141                                 {
142                                         pm_file *pmfn = pm_open(cue_data->tracks[num_track].fname);
143                                         if (pmfn != NULL)
144                                         {
145                                                 // addume raw, ignore header for wav..
146                                                 Tracks[index].F = pmfn;
147                                                 Tracks[index].Length = pmfn->size / 2352;
148                                                 Tracks[index].Offset = cue_data->tracks[num_track].sector_offset;
149                                         }
150                                         else
151                                         {
152                                                 elprintf(EL_STATUS, "track %2i (%s): can't determine length",
153                                                         num_track, cue_data->tracks[num_track].fname);
154                                                 Tracks[index].Length = 2*75;
155                                                 Tracks[index].Offset = 0;
156                                         }
157                                 }
158                                 else
159                                 {
160                                         if (num_track < cue_data->track_count)
161                                                 Tracks[index].Length = cue_data->tracks[num_track+1].sector_offset -
162                                                         cue_data->tracks[num_track].sector_offset;
163                                         else
164                                                 Tracks[index].Length = cd_img_sectors - cue_data->tracks[num_track].sector_offset;
165                                         Tracks[index].Offset = cue_data->tracks[num_track].sector_offset;
166                                 }
167                         }
168
169                         if (cue_data->tracks[num_track].sector_xlength != 0)
170                                 // overriden by custom cue command
171                                 Tracks[index].Length = cue_data->tracks[num_track].sector_xlength;
172
173                         LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
174                         Cur_LBA += Tracks[index].Length;
175
176                         elprintf(EL_STATUS, "Track %2i: %02d:%02d:%02d %9i AUDIO - %s", num_track, Tracks[index].MSF.M,
177                                 Tracks[index].MSF.S, Tracks[index].MSF.F, Tracks[index].Length,
178                                 cue_data->tracks[num_track].fname);
179                 }
180                 cue_destroy(cue_data);
181                 goto finish;
182         }
183
184         /* mp3 track autosearch, Gens-like */
185         iso_name_len = strlen(cd_img_name);
186         if (iso_name_len >= sizeof(tmp_name))
187                 iso_name_len = sizeof(tmp_name) - 1;
188
189         for (num_track = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
190         {
191                 if (PicoCDLoadProgressCB != NULL && i > 1)
192                         PicoCDLoadProgressCB(cd_img_name, i + (100-i)*missed/4);
193
194                 for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
195                 {
196                         int ext_len;
197                         char *p;
198
199                         index = num_track - 1;
200
201                         sprintf(tmp_ext, exts[j], i);
202                         ext_len = strlen(tmp_ext);
203                         to_upper(tmp_ext_u, tmp_ext);
204
205                         memcpy(tmp_name, cd_img_name, iso_name_len + 1);
206                         p = tmp_name + iso_name_len - 4;
207
208                         strcpy(p, tmp_ext);
209                         ret = audio_track_mp3(tmp_name, index);
210                         if (ret != 0) {
211                                 strcpy(p, tmp_ext_u);
212                                 ret = audio_track_mp3(tmp_name, index);
213                         }
214
215                         if (ret != 0 && i > 1 && iso_name_len > ext_len) {
216                                 p = tmp_name + iso_name_len - ext_len;
217                                 strcpy(p, tmp_ext);
218                                 ret = audio_track_mp3(tmp_name, index);
219                                 if (ret != 0) {
220                                         strcpy(p, tmp_ext_u);
221                                         ret = audio_track_mp3(tmp_name, index);
222                                 }
223                         }
224
225                         if (ret == 0)
226                         {
227                                 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
228                                 Cur_LBA += Tracks[index].Length;
229
230                                 elprintf(EL_STATUS, "Track %2i: %02d:%02d:%02d %9i AUDIO - %s", num_track, Tracks[index].MSF.M,
231                                         Tracks[index].MSF.S, Tracks[index].MSF.F, Tracks[index].Length, tmp_name);
232
233                                 num_track++;
234                                 missed = 0;
235                                 break;
236                         }
237                 }
238                 if (ret != 0 && i > 1) missed++;
239         }
240
241 finish:
242         Pico_mcd->TOC.Last_Track = num_track - 1;
243
244         index = num_track - 1;
245
246         LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
247
248         elprintf(EL_STATUS, "End CD -  %02d:%02d:%02d\n", Tracks[index].MSF.M,
249                 Tracks[index].MSF.S, Tracks[index].MSF.F);
250
251         if (PicoCDLoadProgressCB != NULL)
252                 PicoCDLoadProgressCB(cd_img_name, 100);
253
254         return 0;
255 }
256
257
258 PICO_INTERNAL void Unload_ISO(void)
259 {
260         int i;
261
262         if (Pico_mcd == NULL) return;
263
264         if (Pico_mcd->TOC.Tracks[0].F) pm_close(Pico_mcd->TOC.Tracks[0].F);
265
266         for(i = 1; i < 100; i++)
267         {
268                 if (Pico_mcd->TOC.Tracks[i].F != NULL)
269                 {
270                         if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_MP3)
271 #ifdef _PSP_FW_VERSION
272                                 free(Pico_mcd->TOC.Tracks[i].F);
273 #else
274                                 fclose(Pico_mcd->TOC.Tracks[i].F);
275 #endif
276                         else
277                                 pm_close(Pico_mcd->TOC.Tracks[i].F);
278                 }
279         }
280         memset(Pico_mcd->TOC.Tracks, 0, sizeof(Pico_mcd->TOC.Tracks));
281 }
282
283
284 PICO_INTERNAL int FILE_Read_One_LBA_CDC(void)
285 {
286         if (Pico_mcd->s68k_regs[0x36] & 1)                                      // DATA
287         {
288                 if (Pico_mcd->TOC.Tracks[0].F == NULL) return -1;
289
290                 // moved below..
291                 //fseek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
292                 //fread(cp_buf, 1, 2048, Pico_mcd->TOC.Tracks[0].F);
293
294                 cdprintf("Read file CDC 1 data sector :\n");
295         }
296         else                                                                    // AUDIO
297         {
298                 cdprintf("Read file CDC 1 audio sector :\n");
299         }
300
301         // Update CDC stuff
302
303         CDC_Update_Header();
304
305         if (Pico_mcd->s68k_regs[0x36] & 1)              // DATA track
306         {
307                 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80)             // DECEN = decoding enable
308                 {
309                         if (Pico_mcd->cdc.CTRL.B.B0 & 0x04)     // WRRQ : this bit enable write to buffer
310                         {
311                                 int where_read = 0;
312
313                                 // CAUTION : lookahead bit not implemented
314
315                                 if (Pico_mcd->scd.Cur_LBA < 0)
316                                         where_read = 0;
317                                 else if (Pico_mcd->scd.Cur_LBA >= Pico_mcd->TOC.Tracks[0].Length)
318                                         where_read = Pico_mcd->TOC.Tracks[0].Length - 1;
319                                 else where_read = Pico_mcd->scd.Cur_LBA;
320
321                                 Pico_mcd->scd.Cur_LBA++;
322
323                                 Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF;              // add one sector to WA
324                                 Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF;
325
326                                 *(unsigned int *)(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N) = Pico_mcd->cdc.HEAD.N;
327                                 //memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N + 4], cp_buf, 2048);
328
329                                 //pm_seek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
330                                 //pm_read(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, 2048, Pico_mcd->TOC.Tracks[0].F);
331                                 PicoCDBufferRead(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, where_read);
332
333                                 cdprintf("Read -> WA = %d  Buffer[%d] =", Pico_mcd->cdc.WA.N, Pico_mcd->cdc.PT.N & 0x3FFF);
334                                 cdprintf("Header 1 = %.2X %.2X %.2X %.2X", Pico_mcd->cdc.HEAD.B.B0,
335                                         Pico_mcd->cdc.HEAD.B.B1, Pico_mcd->cdc.HEAD.B.B2, Pico_mcd->cdc.HEAD.B.B3);
336                                 cdprintf("Header 2 = %.2X %.2X %.2X %.2X --- %.2X %.2X\n\n",
337                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 0) & 0x3FFF],
338                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 1) & 0x3FFF],
339                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 2) & 0x3FFF],
340                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 3) & 0x3FFF],
341                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 4) & 0x3FFF],
342                                         Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 5) & 0x3FFF]);
343                         }
344
345                 }
346         }
347         else            // music track
348         {
349                 Pico_mcd->scd.Cur_LBA++;
350
351                 Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF;              // add one sector to WA
352                 Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF;
353
354                 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80)             // DECEN = decoding enable
355                 {
356                         if (Pico_mcd->cdc.CTRL.B.B0 & 0x04)     // WRRQ : this bit enable write to buffer
357                         {
358                                 // CAUTION : lookahead bit not implemented
359
360                                 // this is pretty rough, but oh well - not much depends on this anyway
361                                 memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N], cdda_out_buffer, 2352);
362                         }
363                 }
364         }
365
366         if (Pico_mcd->cdc.CTRL.B.B0 & 0x80)             // DECEN = decoding enable
367         {
368                 Pico_mcd->cdc.STAT.B.B0 = 0x80;
369
370                 if (Pico_mcd->cdc.CTRL.B.B0 & 0x10)     // determine form bit form sub header ?
371                 {
372                         Pico_mcd->cdc.STAT.B.B2 = Pico_mcd->cdc.CTRL.B.B1 & 0x08;
373                 }
374                 else
375                 {
376                         Pico_mcd->cdc.STAT.B.B2 = Pico_mcd->cdc.CTRL.B.B1 & 0x0C;
377                 }
378
379                 if (Pico_mcd->cdc.CTRL.B.B0 & 0x02) Pico_mcd->cdc.STAT.B.B3 = 0x20;     // ECC done
380                 else Pico_mcd->cdc.STAT.B.B3 = 0x00;    // ECC not done
381
382                 if (Pico_mcd->cdc.IFCTRL & 0x20)
383                 {
384                         if (Pico_mcd->s68k_regs[0x33] & (1<<5))
385                         {
386                                 elprintf(EL_INTS, "cdc dec irq 5");
387                                 SekInterruptS68k(5);
388                         }
389
390                         Pico_mcd->cdc.IFSTAT &= ~0x20;          // DEC interrupt happen
391                         Pico_mcd->cdc.Decode_Reg_Read = 0;      // Reset read after DEC int
392                 }
393         }
394
395
396         return 0;
397 }
398