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