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