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