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