drc: dumb detection of writes to current block
[picodrive.git] / pico / cd / cd_file.c
CommitLineData
6cadc2da 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
efcba75f 10#include "../pico_int.h"
eff55556 11#include "cd_file.h"
9037e45d 12#include "cue.h"
bf098bc5 13
4b167c12 14//#define cdprintf(f,...) printf(f "\n",##__VA_ARGS__) // tmp
bf098bc5 15
9c9cda8c 16static 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
9037e45d 26static int audio_track_mp3(const char *fname, int index)
bf098bc5 27{
9037e45d 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
9c9cda8c 40#ifdef _PSP_FW_VERSION
9037e45d 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);
9c9cda8c 51#ifdef _PSP_FW_VERSION
9037e45d 52 free(tmp_file);
9c9cda8c 53#else
54 fclose(tmp_file);
9037e45d 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
71PICO_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;
75736070 74 _scd_track *Tracks = Pico_mcd->TOC.Tracks;
9c9cda8c 75 char tmp_name[1024], tmp_ext[10], tmp_ext_u[10];
9037e45d 76 cue_data_t *cue_data = NULL;
83bd0b76 77 pm_file *pmf;
9c9cda8c 78 static const char *exts[] = {
bf098bc5 79 "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
80 "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
bf098bc5 81 };
82
b2451ac6 83 if (PicoCDLoadProgressCB != NULL)
84 PicoCDLoadProgressCB(cd_img_name, 1);
4b167c12 85
bf098bc5 86 Unload_ISO();
87
9037e45d 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
b923ecbe 95 Tracks[0].ftype = type == CIT_BIN ? TYPE_BIN : TYPE_ISO;
bf098bc5 96
9037e45d 97 Tracks[0].F = pmf = pm_open(cd_img_name);
bf098bc5 98 if (Tracks[0].F == NULL)
99 {
75736070 100 Tracks[0].ftype = 0;
cb0316e4 101 Tracks[0].Length = 0;
9037e45d 102 if (cue_data != NULL)
103 cue_destroy(cue_data);
bf098bc5 104 return -1;
105 }
106
83bd0b76 107 if (Tracks[0].ftype == TYPE_ISO)
9037e45d 108 cd_img_sectors = pmf->size >>= 11; // size in sectors
109 else cd_img_sectors = pmf->size /= 2352;
110 Tracks[0].Offset = 0;
bf098bc5 111
75736070 112 Tracks[0].MSF.M = 0; // minutes
113 Tracks[0].MSF.S = 2; // seconds
114 Tracks[0].MSF.F = 0; // frames
bf098bc5 115
0bccafeb 116 elprintf(EL_STATUS, "Track 1: %02d:%02d:%02d %9i DATA",
9037e45d 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 }
c9e1affc 126 i = 100 / cue_data->track_count+1;
9037e45d 127 for (num_track = 2; num_track <= cue_data->track_count; num_track++)
128 {
b2451ac6 129 if (PicoCDLoadProgressCB != NULL)
130 PicoCDLoadProgressCB(cd_img_name, i * num_track);
9037e45d 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 {
0bccafeb 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 {
9037e45d 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
0bccafeb 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
9037e45d 173 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
174 Cur_LBA += Tracks[index].Length;
bf098bc5 175
0bccafeb 176 elprintf(EL_STATUS, "Track %2i: %02d:%02d:%02d %9i AUDIO - %s", num_track, Tracks[index].MSF.M,
9037e45d 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 }
bf098bc5 183
c9e1affc 184 /* mp3 track autosearch, Gens-like */
9037e45d 185 iso_name_len = strlen(cd_img_name);
4b167c12 186 if (iso_name_len >= sizeof(tmp_name))
187 iso_name_len = sizeof(tmp_name) - 1;
bf098bc5 188
9037e45d 189 for (num_track = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
bf098bc5 190 {
b2451ac6 191 if (PicoCDLoadProgressCB != NULL && i > 1)
192 PicoCDLoadProgressCB(cd_img_name, i + (100-i)*missed/4);
4b167c12 193
194 for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
bf098bc5 195 {
cb0316e4 196 int ext_len;
9c9cda8c 197 char *p;
198
199 index = num_track - 1;
200
bf098bc5 201 sprintf(tmp_ext, exts[j], i);
cb0316e4 202 ext_len = strlen(tmp_ext);
9c9cda8c 203 to_upper(tmp_ext_u, tmp_ext);
cb0316e4 204
9037e45d 205 memcpy(tmp_name, cd_img_name, iso_name_len + 1);
9c9cda8c 206 p = tmp_name + iso_name_len - 4;
bf098bc5 207
9c9cda8c 208 strcpy(p, tmp_ext);
9037e45d 209 ret = audio_track_mp3(tmp_name, index);
9c9cda8c 210 if (ret != 0) {
211 strcpy(p, tmp_ext_u);
212 ret = audio_track_mp3(tmp_name, index);
213 }
214
9037e45d 215 if (ret != 0 && i > 1 && iso_name_len > ext_len) {
9c9cda8c 216 p = tmp_name + iso_name_len - ext_len;
217 strcpy(p, tmp_ext);
9037e45d 218 ret = audio_track_mp3(tmp_name, index);
9c9cda8c 219 if (ret != 0) {
220 strcpy(p, tmp_ext_u);
221 ret = audio_track_mp3(tmp_name, index);
222 }
cb0316e4 223 }
bf098bc5 224
9037e45d 225 if (ret == 0)
bf098bc5 226 {
75736070 227 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
cb0316e4 228 Cur_LBA += Tracks[index].Length;
229
0bccafeb 230 elprintf(EL_STATUS, "Track %2i: %02d:%02d:%02d %9i AUDIO - %s", num_track, Tracks[index].MSF.M,
9037e45d 231 Tracks[index].MSF.S, Tracks[index].MSF.F, Tracks[index].Length, tmp_name);
bf098bc5 232
233 num_track++;
9037e45d 234 missed = 0;
bf098bc5 235 break;
236 }
237 }
c9e1affc 238 if (ret != 0 && i > 1) missed++;
bf098bc5 239 }
240
9037e45d 241finish:
75736070 242 Pico_mcd->TOC.Last_Track = num_track - 1;
bf098bc5 243
75736070 244 index = num_track - 1;
bf098bc5 245
75736070 246 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
bf098bc5 247
9037e45d 248 elprintf(EL_STATUS, "End CD - %02d:%02d:%02d\n", Tracks[index].MSF.M,
75736070 249 Tracks[index].MSF.S, Tracks[index].MSF.F);
bf098bc5 250
b2451ac6 251 if (PicoCDLoadProgressCB != NULL)
252 PicoCDLoadProgressCB(cd_img_name, 100);
4b167c12 253
bf098bc5 254 return 0;
255}
256
257
eff55556 258PICO_INTERNAL void Unload_ISO(void)
bf098bc5 259{
260 int i;
261
75736070 262 if (Pico_mcd == NULL) return;
bf098bc5 263
83bd0b76 264 if (Pico_mcd->TOC.Tracks[0].F) pm_close(Pico_mcd->TOC.Tracks[0].F);
265
266 for(i = 1; i < 100; i++)
bf098bc5 267 {
4b167c12 268 if (Pico_mcd->TOC.Tracks[i].F != NULL)
9037e45d 269 {
c9e1affc 270 if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_MP3)
9c9cda8c 271#ifdef _PSP_FW_VERSION
9037e45d 272 free(Pico_mcd->TOC.Tracks[i].F);
c9e1affc 273#else
9037e45d 274 fclose(Pico_mcd->TOC.Tracks[i].F);
c9e1affc 275#endif
276 else
277 pm_close(Pico_mcd->TOC.Tracks[i].F);
9037e45d 278 }
bf098bc5 279 }
83bd0b76 280 memset(Pico_mcd->TOC.Tracks, 0, sizeof(Pico_mcd->TOC.Tracks));
bf098bc5 281}
282
283
eff55556 284PICO_INTERNAL int FILE_Read_One_LBA_CDC(void)
bf098bc5 285{
bf098bc5 286 if (Pico_mcd->s68k_regs[0x36] & 1) // DATA
287 {
75736070 288 if (Pico_mcd->TOC.Tracks[0].F == NULL) return -1;
bf098bc5 289
75736070 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);
bf098bc5 293
c459aefd 294 cdprintf("Read file CDC 1 data sector :\n");
bf098bc5 295 }
296 else // AUDIO
297 {
c459aefd 298 cdprintf("Read file CDC 1 audio sector :\n");
bf098bc5 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 {
0a051f55 311 int where_read = 0;
312
bf098bc5 313 // CAUTION : lookahead bit not implemented
314
0a051f55 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
bf098bc5 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
83bd0b76 326 *(unsigned int *)(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N) = Pico_mcd->cdc.HEAD.N;
75736070 327 //memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N + 4], cp_buf, 2048);
328
0a051f55 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);
bf098bc5 332
c459aefd 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,
bf098bc5 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]);
bf098bc5 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
c9e1affc 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);
bf098bc5 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 {
ca61ee42 386 elprintf(EL_INTS, "cdc dec irq 5");
bf098bc5 387 SekInterruptS68k(5);
388 }
389
75736070 390 Pico_mcd->cdc.IFSTAT &= ~0x20; // DEC interrupt happen
391 Pico_mcd->cdc.Decode_Reg_Read = 0; // Reset read after DEC int
bf098bc5 392 }
393 }
394
395
396 return 0;
397}
398