bugfixes, refactoring
[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
bf098bc5 10#include "../PicoInt.h"
eff55556 11#include "cd_file.h"
bf098bc5 12
4b167c12 13//#define cdprintf(f,...) printf(f "\n",##__VA_ARGS__) // tmp
c459aefd 14#define DEBUG_CD
bf098bc5 15
eff55556 16PICO_INTERNAL int Load_ISO(const char *iso_name, int is_bin)
bf098bc5 17{
cb0316e4 18 int i, j, num_track, Cur_LBA, index, ret, iso_name_len;
75736070 19 _scd_track *Tracks = Pico_mcd->TOC.Tracks;
bf098bc5 20 char tmp_name[1024], tmp_ext[10];
83bd0b76 21 pm_file *pmf;
bf098bc5 22 static char *exts[] = {
23 "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
24 "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
4b167c12 25#if CASE_SENSITIVE_FS
51a902ae 26 "%02d.MP3", " %02d.MP3", "-%02d.MP3", "_%02d.MP3", " - %02d.MP3",
4b167c12 27#endif
bf098bc5 28 };
29
4b167c12 30 if (PicoCDLoadProgressCB != NULL) PicoCDLoadProgressCB(1);
31
bf098bc5 32 Unload_ISO();
33
75736070 34 Tracks[0].ftype = is_bin ? TYPE_BIN : TYPE_ISO;
bf098bc5 35
83bd0b76 36 Tracks[0].F = pmf = pm_open(iso_name);
bf098bc5 37 if (Tracks[0].F == NULL)
38 {
75736070 39 Tracks[0].ftype = 0;
cb0316e4 40 Tracks[0].Length = 0;
bf098bc5 41 return -1;
42 }
43
83bd0b76 44 if (Tracks[0].ftype == TYPE_ISO)
45 Tracks[0].Length = pmf->size >>= 11; // size in sectors
46 else Tracks[0].Length = pmf->size /= 2352;
bf098bc5 47
75736070 48 Tracks[0].MSF.M = 0; // minutes
49 Tracks[0].MSF.S = 2; // seconds
50 Tracks[0].MSF.F = 0; // frames
bf098bc5 51
75736070 52 cdprintf("Track 0 - %02d:%02d:%02d DATA", Tracks[0].MSF.M, Tracks[0].MSF.S, Tracks[0].MSF.F);
bf098bc5 53
cb0316e4 54 Cur_LBA = Tracks[0].Length; // Size in sectors
bf098bc5 55
cb0316e4 56 iso_name_len = strlen(iso_name);
4b167c12 57 if (iso_name_len >= sizeof(tmp_name))
58 iso_name_len = sizeof(tmp_name) - 1;
bf098bc5 59
cb0316e4 60 for (num_track = 2, i = 0; i < 100; i++)
bf098bc5 61 {
4b167c12 62 if (PicoCDLoadProgressCB != NULL && i > 1) PicoCDLoadProgressCB(i);
63
64 for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
bf098bc5 65 {
cb0316e4 66 int ext_len;
83bd0b76 67 FILE *tmp_file;
bf098bc5 68 sprintf(tmp_ext, exts[j], i);
cb0316e4 69 ext_len = strlen(tmp_ext);
70
71 memcpy(tmp_name, iso_name, iso_name_len + 1);
72 tmp_name[iso_name_len - 4] = 0;
bf098bc5 73 strcat(tmp_name, tmp_ext);
74
75 tmp_file = fopen(tmp_name, "rb");
cb0316e4 76 if (!tmp_file && i > 1 && iso_name_len > ext_len) {
77 tmp_name[iso_name_len - ext_len] = 0;
78 strcat(tmp_name, tmp_ext);
79 tmp_file = fopen(tmp_name, "rb");
80 }
bf098bc5 81
82 if (tmp_file)
83 {
cb0316e4 84 int fs;
75736070 85 index = num_track - 1;
bf098bc5 86
4b167c12 87 ret = fseek(tmp_file, 0, SEEK_END);
88 fs = ftell(tmp_file); // used to calculate lenght
89 fseek(tmp_file, 0, SEEK_SET);
bf098bc5 90
4b167c12 91#if DONT_OPEN_MANY_FILES
92 // some systems (like PSP) can't have many open files at a time,
93 // so we work with their names instead.
94 fclose(tmp_file);
95 tmp_file = (void *) strdup(tmp_name);
96#endif
cb0316e4 97 Tracks[index].KBtps = (short) mp3_get_bitrate(tmp_file, fs);
98 Tracks[index].KBtps >>= 3;
99 if (ret != 0 || Tracks[index].KBtps <= 0)
100 {
4b167c12 101 cdprintf("Error track %i: rate %i", index, Tracks[index].KBtps);
102#if !DONT_OPEN_MANY_FILES
cb0316e4 103 fclose(tmp_file);
4b167c12 104#else
105 free(tmp_file);
106#endif
cb0316e4 107 continue;
108 }
bf098bc5 109
110 Tracks[index].F = tmp_file;
111
75736070 112 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
bf098bc5 113
bf098bc5 114 // MP3 File
75736070 115 Tracks[index].ftype = TYPE_MP3;
bf098bc5 116 fs *= 75;
cb0316e4 117 fs /= Tracks[index].KBtps * 1000;
118 Tracks[index].Length = fs;
119 Cur_LBA += Tracks[index].Length;
120
75736070 121 cdprintf("Track %i: %s - %02d:%02d:%02d len=%i AUDIO", index, tmp_name, Tracks[index].MSF.M,
122 Tracks[index].MSF.S, Tracks[index].MSF.F, fs);
bf098bc5 123
124 num_track++;
125 break;
126 }
127 }
128 }
129
75736070 130 Pico_mcd->TOC.Last_Track = num_track - 1;
bf098bc5 131
75736070 132 index = num_track - 1;
bf098bc5 133
75736070 134 LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
bf098bc5 135
75736070 136 cdprintf("End CD - %02d:%02d:%02d\n\n", Tracks[index].MSF.M,
137 Tracks[index].MSF.S, Tracks[index].MSF.F);
bf098bc5 138
4b167c12 139 if (PicoCDLoadProgressCB != NULL) PicoCDLoadProgressCB(100);
140
bf098bc5 141 return 0;
142}
143
144
eff55556 145PICO_INTERNAL void Unload_ISO(void)
bf098bc5 146{
147 int i;
148
75736070 149 if (Pico_mcd == NULL) return;
bf098bc5 150
83bd0b76 151 if (Pico_mcd->TOC.Tracks[0].F) pm_close(Pico_mcd->TOC.Tracks[0].F);
152
153 for(i = 1; i < 100; i++)
bf098bc5 154 {
4b167c12 155 if (Pico_mcd->TOC.Tracks[i].F != NULL)
156#if !DONT_OPEN_MANY_FILES
157 fclose(Pico_mcd->TOC.Tracks[i].F);
158#else
159 free(Pico_mcd->TOC.Tracks[i].F);
160#endif
bf098bc5 161 }
83bd0b76 162 memset(Pico_mcd->TOC.Tracks, 0, sizeof(Pico_mcd->TOC.Tracks));
bf098bc5 163}
164
165
eff55556 166PICO_INTERNAL int FILE_Read_One_LBA_CDC(void)
bf098bc5 167{
75736070 168// static char cp_buf[2560];
bf098bc5 169
170 if (Pico_mcd->s68k_regs[0x36] & 1) // DATA
171 {
75736070 172 if (Pico_mcd->TOC.Tracks[0].F == NULL) return -1;
bf098bc5 173
75736070 174 // moved below..
175 //fseek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
176 //fread(cp_buf, 1, 2048, Pico_mcd->TOC.Tracks[0].F);
bf098bc5 177
c459aefd 178 cdprintf("Read file CDC 1 data sector :\n");
bf098bc5 179 }
180 else // AUDIO
181 {
182 // int rate, channel;
183
0a051f55 184 // if (Pico_mcd->TOC.Tracks[Pico_mcd->scd.Cur_Track - 1].ftype == TYPE_MP3)
bf098bc5 185 {
186 // TODO
187 // MP3_Update(cp_buf, &rate, &channel, 0);
188 // Write_CD_Audio((short *) cp_buf, rate, channel, 588);
189 }
190
c459aefd 191 cdprintf("Read file CDC 1 audio sector :\n");
bf098bc5 192 }
193
194 // Update CDC stuff
195
196 CDC_Update_Header();
197
198 if (Pico_mcd->s68k_regs[0x36] & 1) // DATA track
199 {
200 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80) // DECEN = decoding enable
201 {
202 if (Pico_mcd->cdc.CTRL.B.B0 & 0x04) // WRRQ : this bit enable write to buffer
203 {
0a051f55 204 int where_read = 0;
205
bf098bc5 206 // CAUTION : lookahead bit not implemented
207
0a051f55 208 if (Pico_mcd->scd.Cur_LBA < 0)
209 where_read = 0;
210 else if (Pico_mcd->scd.Cur_LBA >= Pico_mcd->TOC.Tracks[0].Length)
211 where_read = Pico_mcd->TOC.Tracks[0].Length - 1;
212 else where_read = Pico_mcd->scd.Cur_LBA;
213
bf098bc5 214 Pico_mcd->scd.Cur_LBA++;
215
216 Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF; // add one sector to WA
217 Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF;
218
83bd0b76 219 *(unsigned int *)(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N) = Pico_mcd->cdc.HEAD.N;
75736070 220 //memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N + 4], cp_buf, 2048);
221
0a051f55 222 //pm_seek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
223 //pm_read(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, 2048, Pico_mcd->TOC.Tracks[0].F);
224 PicoCDBufferRead(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, where_read);
bf098bc5 225
226#ifdef DEBUG_CD
c459aefd 227 cdprintf("Read -> WA = %d Buffer[%d] =", Pico_mcd->cdc.WA.N, Pico_mcd->cdc.PT.N & 0x3FFF);
228 cdprintf("Header 1 = %.2X %.2X %.2X %.2X", Pico_mcd->cdc.HEAD.B.B0,
bf098bc5 229 Pico_mcd->cdc.HEAD.B.B1, Pico_mcd->cdc.HEAD.B.B2, Pico_mcd->cdc.HEAD.B.B3);
230 cdprintf("Header 2 = %.2X %.2X %.2X %.2X --- %.2X %.2X\n\n",
231 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 0) & 0x3FFF],
232 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 1) & 0x3FFF],
233 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 2) & 0x3FFF],
234 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 3) & 0x3FFF],
235 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 4) & 0x3FFF],
236 Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 5) & 0x3FFF]);
237#endif
238 }
239
240 }
241 }
242 else // music track
243 {
244 Pico_mcd->scd.Cur_LBA++;
245
246 Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF; // add one sector to WA
247 Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF;
248
249 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80) // DECEN = decoding enable
250 {
251 if (Pico_mcd->cdc.CTRL.B.B0 & 0x04) // WRRQ : this bit enable write to buffer
252 {
253 // CAUTION : lookahead bit not implemented
254
75736070 255 //memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N], cp_buf, 2352);
bf098bc5 256 }
257 }
258 }
259
260 if (Pico_mcd->cdc.CTRL.B.B0 & 0x80) // DECEN = decoding enable
261 {
262 Pico_mcd->cdc.STAT.B.B0 = 0x80;
263
264 if (Pico_mcd->cdc.CTRL.B.B0 & 0x10) // determine form bit form sub header ?
265 {
266 Pico_mcd->cdc.STAT.B.B2 = Pico_mcd->cdc.CTRL.B.B1 & 0x08;
267 }
268 else
269 {
270 Pico_mcd->cdc.STAT.B.B2 = Pico_mcd->cdc.CTRL.B.B1 & 0x0C;
271 }
272
273 if (Pico_mcd->cdc.CTRL.B.B0 & 0x02) Pico_mcd->cdc.STAT.B.B3 = 0x20; // ECC done
274 else Pico_mcd->cdc.STAT.B.B3 = 0x00; // ECC not done
275
276 if (Pico_mcd->cdc.IFCTRL & 0x20)
277 {
278 if (Pico_mcd->s68k_regs[0x33] & (1<<5))
279 {
ca61ee42 280 elprintf(EL_INTS, "cdc dec irq 5");
bf098bc5 281 SekInterruptS68k(5);
282 }
283
75736070 284 Pico_mcd->cdc.IFSTAT &= ~0x20; // DEC interrupt happen
285 Pico_mcd->cdc.Decode_Reg_Read = 0; // Reset read after DEC int
bf098bc5 286 }
287 }
288
289
290 return 0;
291}
292
293
eff55556 294PICO_INTERNAL int FILE_Play_CD_LBA(void)
bf098bc5 295{
75736070 296 int index = Pico_mcd->scd.Cur_Track - 1;
297 Pico_mcd->m.audio_track = index;
bf098bc5 298
75736070 299 cdprintf("Play track #%i", Pico_mcd->scd.Cur_Track);
bf098bc5 300
75736070 301 if (Pico_mcd->TOC.Tracks[index].F == NULL)
bf098bc5 302 {
303 return 1;
304 }
305
75736070 306 if (Pico_mcd->TOC.Tracks[index].ftype == TYPE_MP3)
bf098bc5 307 {
75736070 308 int pos1024 = 0;
bf098bc5 309 int Track_LBA_Pos = Pico_mcd->scd.Cur_LBA - Track_to_LBA(Pico_mcd->scd.Cur_Track);
310 if (Track_LBA_Pos < 0) Track_LBA_Pos = 0;
cb0316e4 311 if (Track_LBA_Pos)
75736070 312 pos1024 = Track_LBA_Pos * 1024 / Pico_mcd->TOC.Tracks[index].Length;
bf098bc5 313
75736070 314 mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024);
bf098bc5 315 }
316 else
317 {
318 return 3;
319 }
320
321 return 0;
322}
323