1 /***********************************************************
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 *
8 ***********************************************************/
10 #include "../pico_int.h"
14 //#define cdprintf(f,...) printf(f "\n",##__VA_ARGS__) // tmp
16 static void to_upper(char *d, const char *s)
18 for (; *s != 0; d++, s++) {
19 if ('a' <= *s && *s <= 'z')
26 static int audio_track_mp3(const char *fname, int index)
28 _scd_track *Tracks = Pico_mcd->TOC.Tracks;
32 tmp_file = fopen(fname, "rb");
36 ret = fseek(tmp_file, 0, SEEK_END);
37 fs = ftell(tmp_file); // used to calculate length
38 fseek(tmp_file, 0, SEEK_SET);
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.
44 tmp_file = (void *) strdup(fname);
46 Tracks[index].KBtps = (short) mp3_get_bitrate(tmp_file, fs);
47 Tracks[index].KBtps >>= 3;
48 if (ret != 0 || Tracks[index].KBtps <= 0)
50 elprintf(EL_STATUS, "track %2i: mp3 bitrate %i", index+1, Tracks[index].KBtps);
51 #ifdef _PSP_FW_VERSION
59 Tracks[index].F = tmp_file;
62 Tracks[index].ftype = CT_MP3;
64 fs /= Tracks[index].KBtps * 1000;
65 Tracks[index].Length = fs;
66 Tracks[index].Offset = 0;
71 PICO_INTERNAL int Load_CD_Image(const char *cd_img_name, cd_img_type type)
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;
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",
84 if (PicoCDLoadProgressCB != NULL)
85 PicoCDLoadProgressCB(cd_img_name, 1);
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;
96 Tracks[0].ftype = type == CIT_BIN ? CT_BIN : CT_ISO;
98 Tracks[0].F = pmf = pm_open(cd_img_name);
99 if (Tracks[0].F == NULL)
102 Tracks[0].Length = 0;
103 if (cue_data != NULL)
104 cue_destroy(cue_data);
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;
113 Tracks[0].MSF.M = 0; // minutes
114 Tracks[0].MSF.S = 2; // seconds
115 Tracks[0].MSF.F = 0; // frames
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);
121 Cur_LBA = Tracks[0].Length = cd_img_sectors;
123 if (cue_data != NULL)
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;
128 i = 100 / cue_data->track_count+1;
129 for (num_track = 2; num_track <= cue_data->track_count; num_track++)
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);
141 Tracks[index].ftype = cue_data->tracks[num_track].type;
142 if (cue_data->tracks[num_track].fname != NULL)
144 pm_file *pmfn = pm_open(cue_data->tracks[num_track].fname);
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;
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;
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;
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;
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;
175 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
176 Cur_LBA += Tracks[index].Length;
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);
182 cue_destroy(cue_data);
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;
191 for (num_track = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
193 if (PicoCDLoadProgressCB != NULL && i > 1)
194 PicoCDLoadProgressCB(cd_img_name, i + (100-i)*missed/4);
196 for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
201 index = num_track - 1;
203 sprintf(tmp_ext, exts[j], i);
204 ext_len = strlen(tmp_ext);
205 to_upper(tmp_ext_u, tmp_ext);
207 memcpy(tmp_name, cd_img_name, iso_name_len + 1);
208 p = tmp_name + iso_name_len - 4;
211 ret = audio_track_mp3(tmp_name, index);
213 strcpy(p, tmp_ext_u);
214 ret = audio_track_mp3(tmp_name, index);
217 if (ret != 0 && i > 1 && iso_name_len > ext_len) {
218 p = tmp_name + iso_name_len - ext_len;
220 ret = audio_track_mp3(tmp_name, index);
222 strcpy(p, tmp_ext_u);
223 ret = audio_track_mp3(tmp_name, index);
229 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
230 Cur_LBA += Tracks[index].Length;
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);
240 if (ret != 0 && i > 1) missed++;
244 Pico_mcd->TOC.Last_Track = num_track - 1;
246 index = num_track - 1;
248 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
250 elprintf(EL_STATUS, "End CD - %02d:%02d:%02d\n", Tracks[index].MSF.M,
251 Tracks[index].MSF.S, Tracks[index].MSF.F);
253 if (PicoCDLoadProgressCB != NULL)
254 PicoCDLoadProgressCB(cd_img_name, 100);
260 PICO_INTERNAL void Unload_ISO(void)
264 if (Pico_mcd == NULL) return;
266 if (Pico_mcd->TOC.Tracks[0].F) pm_close(Pico_mcd->TOC.Tracks[0].F);
268 for(i = 1; i < 100; i++)
270 if (Pico_mcd->TOC.Tracks[i].F != NULL)
272 if (Pico_mcd->TOC.Tracks[i].ftype == CT_MP3)
273 #ifdef _PSP_FW_VERSION
274 free(Pico_mcd->TOC.Tracks[i].F);
276 fclose(Pico_mcd->TOC.Tracks[i].F);
279 pm_close(Pico_mcd->TOC.Tracks[i].F);
282 memset(Pico_mcd->TOC.Tracks, 0, sizeof(Pico_mcd->TOC.Tracks));
286 PICO_INTERNAL int FILE_Read_One_LBA_CDC(void)
288 if (Pico_mcd->s68k_regs[0x36] & 1) // DATA
290 if (Pico_mcd->TOC.Tracks[0].F == NULL) return -1;
293 //fseek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
294 //fread(cp_buf, 1, 2048, Pico_mcd->TOC.Tracks[0].F);
296 cdprintf("Read file CDC 1 data sector :\n");
300 cdprintf("Read file CDC 1 audio sector :\n");
307 if (Pico_mcd->s68k_regs[0x36] & 1) // DATA track
309 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80) // DECEN = decoding enable
311 if (Pico_mcd->cdc.CTRL.B.B0 & 0x04) // WRRQ : this bit enable write to buffer
315 // CAUTION : lookahead bit not implemented
317 if (Pico_mcd->scd.Cur_LBA < 0)
319 else if (Pico_mcd->scd.Cur_LBA >= Pico_mcd->TOC.Tracks[0].Length)
320 where_read = Pico_mcd->TOC.Tracks[0].Length - 1;
321 else where_read = Pico_mcd->scd.Cur_LBA;
323 Pico_mcd->scd.Cur_LBA++;
325 Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF; // add one sector to WA
326 Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF;
328 *(unsigned int *)(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N) = Pico_mcd->cdc.HEAD.N;
329 //memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N + 4], cp_buf, 2048);
331 //pm_seek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
332 //pm_read(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, 2048, Pico_mcd->TOC.Tracks[0].F);
333 PicoCDBufferRead(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, where_read);
335 cdprintf("Read -> WA = %d Buffer[%d] =", Pico_mcd->cdc.WA.N, Pico_mcd->cdc.PT.N & 0x3FFF);
336 cdprintf("Header 1 = %.2X %.2X %.2X %.2X", Pico_mcd->cdc.HEAD.B.B0,
337 Pico_mcd->cdc.HEAD.B.B1, Pico_mcd->cdc.HEAD.B.B2, Pico_mcd->cdc.HEAD.B.B3);
338 cdprintf("Header 2 = %.2X %.2X %.2X %.2X --- %.2X %.2X\n\n",
339 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 0) & 0x3FFF],
340 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 1) & 0x3FFF],
341 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 2) & 0x3FFF],
342 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 3) & 0x3FFF],
343 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 4) & 0x3FFF],
344 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 5) & 0x3FFF]);
351 Pico_mcd->scd.Cur_LBA++;
353 Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF; // add one sector to WA
354 Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF;
356 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80) // DECEN = decoding enable
358 if (Pico_mcd->cdc.CTRL.B.B0 & 0x04) // WRRQ : this bit enable write to buffer
360 // CAUTION : lookahead bit not implemented
362 // this is pretty rough, but oh well - not much depends on this anyway
363 memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N], cdda_out_buffer, 2352);
368 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80) // DECEN = decoding enable
370 Pico_mcd->cdc.STAT.B.B0 = 0x80;
372 if (Pico_mcd->cdc.CTRL.B.B0 & 0x10) // determine form bit form sub header ?
374 Pico_mcd->cdc.STAT.B.B2 = Pico_mcd->cdc.CTRL.B.B1 & 0x08;
378 Pico_mcd->cdc.STAT.B.B2 = Pico_mcd->cdc.CTRL.B.B1 & 0x0C;
381 if (Pico_mcd->cdc.CTRL.B.B0 & 0x02) Pico_mcd->cdc.STAT.B.B3 = 0x20; // ECC done
382 else Pico_mcd->cdc.STAT.B.B3 = 0x00; // ECC not done
384 if (Pico_mcd->cdc.IFCTRL & 0x20)
386 if (Pico_mcd->s68k_regs[0x33] & (1<<5))
388 elprintf(EL_INTS, "cdc dec irq 5");
392 Pico_mcd->cdc.IFSTAT &= ~0x20; // DEC interrupt happen
393 Pico_mcd->cdc.Decode_Reg_Read = 0; // Reset read after DEC int