6cadc2da |
1 | // Code for communication with ARM940 and control of it.\r |
2 | // (c) Copyright 2007, Grazvydas "notaz" Ignotas\r |
3 | \r |
cc68a136 |
4 | #include <stdio.h>\r |
5 | #include <stdlib.h>\r |
6 | #include <string.h>\r |
7 | #include <unistd.h>\r |
8 | #include <sys/mman.h>\r |
9 | #include <sys/ioctl.h>\r |
10 | #include <fcntl.h>\r |
11 | #include <errno.h>\r |
12 | \r |
42c7b147 |
13 | #include "code940/940shared.h"\r |
42171343 |
14 | #include "soc_mmsp2.h"\r |
15 | #include "soc.h"\r |
da42200b |
16 | #include "../common/mp3.h"\r |
e5f426aa |
17 | #include "../common/arm_utils.h"\r |
18 | #include "../common/menu.h"\r |
ea8c405f |
19 | #include "../common/emu.h"\r |
cc41eb4f |
20 | #include "../common/input.h"\r |
efcba75f |
21 | #include "../../pico/pico_int.h"\r |
22 | #include "../../pico/sound/ym2612.h"\r |
23 | #include "../../pico/sound/mix.h"\r |
4f265db7 |
24 | \r |
cc68a136 |
25 | static unsigned char *shared_mem = 0;\r |
26 | static _940_data_t *shared_data = 0;\r |
abe0ea43 |
27 | _940_ctl_t *shared_ctl = 0;\r |
28 | unsigned char *mp3_mem = 0;\r |
cb0316e4 |
29 | \r |
e362c573 |
30 | #define MP3_SIZE_MAX (0x400000 + 0x800000) // 12M\r |
e4fb433c |
31 | #define CODE940_FILE "pico940_v3.bin"\r |
cc68a136 |
32 | \r |
33 | int crashed_940 = 0;\r |
34 | \r |
51a902ae |
35 | static FILE *loaded_mp3 = 0;\r |
cc68a136 |
36 | \r |
37 | /***********************************************************/\r |
38 | \r |
39 | #define MAXOUT (+32767)\r |
40 | #define MINOUT (-32768)\r |
41 | \r |
42 | /* limitter */\r |
43 | #define Limit(val, max,min) { \\r |
44 | if ( val > max ) val = max; \\r |
45 | else if ( val < min ) val = min; \\r |
46 | }\r |
47 | \r |
48 | /* these will be managed locally on our side */\r |
cc68a136 |
49 | static UINT8 ST_address; /* address register */\r |
4b9c5888 |
50 | static INT32 addr_A1; /* address line A1 */\r |
cc68a136 |
51 | \r |
52 | static int writebuff_ptr = 0;\r |
53 | \r |
54 | \r |
cc68a136 |
55 | /* YM2612 write */\r |
56 | /* a = address */\r |
57 | /* v = value */\r |
58 | /* returns 1 if sample affecting state changed */\r |
4b9c5888 |
59 | int YM2612Write_940(unsigned int a, unsigned int v, int scanline)\r |
cc68a136 |
60 | {\r |
2433f409 |
61 | int upd = 1; /* the write affects sample generation */\r |
cc68a136 |
62 | \r |
cc68a136 |
63 | a &= 3;\r |
64 | \r |
2433f409 |
65 | //printf("%05i:%03i: ym w ([%i] %02x)\n", Pico.m.frame_count, Pico.m.scanline, a, v);\r |
66 | \r |
e4fb433c |
67 | switch (a)\r |
68 | {\r |
4b9c5888 |
69 | case 0: /* address port 0 */\r |
70 | if (addr_A1 == 0 && ST_address == v)\r |
71 | return 0; /* address already selected, don't send this command to 940 */\r |
72 | ST_address = v;\r |
73 | addr_A1 = 0;\r |
74 | /* don't send DAC or timer related address changes to 940 */\r |
75 | if (v == 0x24 || v == 0x25 || v == 0x26 || v == 0x2a)\r |
4ff2d527 |
76 | return 0;\r |
4b9c5888 |
77 | upd = 0;\r |
78 | break;\r |
cc68a136 |
79 | \r |
4b9c5888 |
80 | case 2: /* address port 1 */\r |
81 | if (addr_A1 == 1 && ST_address == v)\r |
cc68a136 |
82 | return 0;\r |
4b9c5888 |
83 | ST_address = v;\r |
84 | addr_A1 = 1;\r |
85 | upd = 0;\r |
cc68a136 |
86 | break;\r |
cc68a136 |
87 | }\r |
88 | \r |
2433f409 |
89 | //printf("ym pass\n");\r |
90 | \r |
4b9c5888 |
91 | if (currentConfig.EmuOpt & 4)\r |
92 | {\r |
2433f409 |
93 | UINT16 *writebuff = shared_ctl->writebuffsel ? shared_ctl->writebuff0 : shared_ctl->writebuff1;\r |
94 | \r |
95 | /* detect rapid ym updates */\r |
4b9c5888 |
96 | if (upd && !(writebuff_ptr & 0x80000000) && scanline < 224)\r |
97 | {\r |
2433f409 |
98 | int mid = Pico.m.pal ? 68 : 93;\r |
4b9c5888 |
99 | if (scanline > mid) {\r |
100 | //printf("%05i:%03i: rapid ym\n", Pico.m.frame_count, scanline);\r |
2433f409 |
101 | writebuff[writebuff_ptr++ & 0xffff] = 0xfffe;\r |
102 | writebuff_ptr |= 0x80000000;\r |
4b9c5888 |
103 | //printf("%05i:%03i: ym w ([%02x] %02x, upd=%i)\n", Pico.m.frame_count, scanline, addr, v, upd);\r |
cc68a136 |
104 | }\r |
2433f409 |
105 | }\r |
106 | \r |
107 | /* queue this write for 940 */\r |
108 | if ((writebuff_ptr&0xffff) < 2047) {\r |
109 | writebuff[writebuff_ptr++ & 0xffff] = (a<<8)|v;\r |
cc68a136 |
110 | } else {\r |
4ff2d527 |
111 | printf("warning: writebuff_ptr > 2047 ([%i] %02x)\n", a, v);\r |
cc68a136 |
112 | }\r |
113 | }\r |
114 | \r |
115 | return 0; // cause the engine to do updates once per frame only\r |
116 | }\r |
117 | \r |
cc68a136 |
118 | \r |
abe0ea43 |
119 | #define CHECK_BUSY(job) \\r |
120 | (gp2x_memregs[0x3b46>>1] & (1<<(job-1)))\r |
121 | \r |
122 | static void wait_busy_940(int job)\r |
cc68a136 |
123 | {\r |
124 | int i;\r |
cc68a136 |
125 | \r |
abe0ea43 |
126 | job--;\r |
127 | for (i = 0; (gp2x_memregs[0x3b46>>1] & (1<<job)) && i < 0x10000; i++)\r |
42c7b147 |
128 | spend_cycles(8*1024); // tested to be best for mp3 dec\r |
cc68a136 |
129 | if (i < 0x10000) return;\r |
130 | \r |
131 | /* 940 crashed */\r |
42c7b147 |
132 | printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);\r |
cc68a136 |
133 | for (i = 0; i < 8; i++)\r |
134 | printf("%i ", shared_ctl->vstarts[i]);\r |
135 | printf(")\n");\r |
abe0ea43 |
136 | printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",\r |
4f265db7 |
137 | gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);\r |
abe0ea43 |
138 | printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r |
4f265db7 |
139 | printf("trying to interrupt..\n");\r |
140 | gp2x_memregs[0x3B3E>>1] = 0xffff;\r |
abe0ea43 |
141 | for (i = 0; gp2x_memregs[0x3b46>>1] && i < 0x10000; i++)\r |
4f265db7 |
142 | spend_cycles(8*1024);\r |
143 | printf("i = 0x%x\n", i);\r |
abe0ea43 |
144 | printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",\r |
4f265db7 |
145 | gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);\r |
abe0ea43 |
146 | printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r |
4f265db7 |
147 | \r |
c6c6c9cd |
148 | me_update_msg("940 crashed, too much overclock?");\r |
cc68a136 |
149 | engineState = PGS_Menu;\r |
150 | crashed_940 = 1;\r |
cc68a136 |
151 | }\r |
152 | \r |
153 | \r |
abe0ea43 |
154 | static void add_job_940(int job)\r |
cc68a136 |
155 | {\r |
abe0ea43 |
156 | if (job <= 0 || job > 16) {\r |
157 | printf("add_job_940: bad job: %i\n", job);\r |
158 | return;\r |
4f265db7 |
159 | }\r |
abe0ea43 |
160 | \r |
161 | // generate interrupt for this job\r |
162 | job--;\r |
163 | gp2x_memregs[(0x3B20+job*2)>>1] = 1;\r |
164 | \r |
165 | // printf("added %i, pending %04x\n", job+1, gp2x_memregs[0x3b46>>1]);\r |
cc68a136 |
166 | }\r |
167 | \r |
168 | \r |
169 | void YM2612PicoStateLoad_940(void)\r |
170 | {\r |
4b9c5888 |
171 | UINT8 *REGS = YM2612GetRegs();\r |
172 | \r |
d5d11924 |
173 | /* make sure JOB940_PICOSTATELOAD gets done before next JOB940_YM2612UPDATEONE */\r |
174 | add_job_940(JOB940_PICOSTATELOAD);\r |
175 | if (CHECK_BUSY(JOB940_PICOSTATELOAD)) wait_busy_940(JOB940_PICOSTATELOAD);\r |
176 | \r |
177 | writebuff_ptr = 0;\r |
4b9c5888 |
178 | addr_A1 = *(INT32 *) (REGS + 0x200);\r |
cc68a136 |
179 | }\r |
180 | \r |
e4fb433c |
181 | void YM2612PicoStateSave2_940(int tat, int tbt)\r |
182 | {\r |
183 | UINT8 *ym_remote_regs, *ym_local_regs;\r |
184 | add_job_940(JOB940_PICOSTATESAVE2);\r |
185 | if (CHECK_BUSY(JOB940_PICOSTATESAVE2)) wait_busy_940(JOB940_PICOSTATESAVE2);\r |
186 | \r |
187 | ym_remote_regs = (UINT8 *) shared_ctl->writebuff0;\r |
188 | ym_local_regs = YM2612GetRegs();\r |
189 | if (*(UINT32 *)(ym_remote_regs + 0x100) != 0x41534d59) {\r |
190 | printf("code940 didn't return valid save data\n");\r |
191 | return;\r |
192 | }\r |
193 | \r |
194 | /* copy addin data only */\r |
195 | memcpy(ym_local_regs, ym_remote_regs, 0x20);\r |
196 | memcpy(ym_local_regs + 0x100, ym_remote_regs + 0x100, 0x30);\r |
197 | memcpy(ym_local_regs + 0x0b8, ym_remote_regs + 0x0b8, 0x48);\r |
198 | memcpy(ym_local_regs + 0x1b8, ym_remote_regs + 0x1b8, 0x48);\r |
199 | *(INT32 *)(ym_local_regs + 0x108) = tat;\r |
200 | *(INT32 *)(ym_local_regs + 0x10c) = tbt;\r |
201 | }\r |
202 | \r |
203 | int YM2612PicoStateLoad2_940(int *tat, int *tbt)\r |
204 | {\r |
205 | UINT8 *ym_remote_regs, *ym_local_regs;\r |
206 | ym_local_regs = YM2612GetRegs();\r |
207 | ym_remote_regs = (UINT8 *) shared_ctl->writebuff0;\r |
208 | \r |
209 | if (*(UINT32 *)(ym_local_regs + 0x100) != 0x41534d59)\r |
210 | return -1;\r |
211 | \r |
212 | *tat = *(INT32 *)(ym_local_regs + 0x108);\r |
213 | *tbt = *(INT32 *)(ym_local_regs + 0x10c);\r |
214 | \r |
215 | if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);\r |
216 | \r |
217 | /* flush writes */\r |
218 | if (shared_ctl->writebuffsel == 1) {\r |
219 | shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;\r |
220 | } else {\r |
221 | shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;\r |
222 | }\r |
223 | shared_ctl->writebuffsel ^= 1;\r |
224 | writebuff_ptr = 0;\r |
225 | add_job_940(JOB940_PICOSTATELOAD2_PREP);\r |
226 | if (CHECK_BUSY(JOB940_PICOSTATELOAD2_PREP)) wait_busy_940(JOB940_PICOSTATELOAD2_PREP);\r |
227 | \r |
228 | memcpy(ym_remote_regs, ym_local_regs, 0x200);\r |
229 | \r |
230 | add_job_940(JOB940_PICOSTATELOAD2);\r |
231 | if (CHECK_BUSY(JOB940_PICOSTATELOAD2)) wait_busy_940(JOB940_PICOSTATELOAD2);\r |
232 | \r |
233 | return 0;\r |
234 | }\r |
235 | \r |
cc68a136 |
236 | \r |
237 | static void internal_reset(void)\r |
238 | {\r |
239 | writebuff_ptr = 0;\r |
4b9c5888 |
240 | ST_address = addr_A1 = -1;\r |
cc68a136 |
241 | }\r |
242 | \r |
243 | \r |
f80d67e1 |
244 | /* this must be called after mmu hack, the allocated regions must not get cached */\r |
0d9bf4fc |
245 | void sharedmem940_init(void)\r |
f80d67e1 |
246 | {\r |
247 | if (shared_mem != NULL) return;\r |
248 | \r |
249 | shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);\r |
e4fb433c |
250 | if (shared_mem == MAP_FAILED)\r |
f80d67e1 |
251 | {\r |
252 | printf("mmap(shared_data) failed with %i\n", errno);\r |
253 | exit(1);\r |
254 | }\r |
255 | shared_data = (_940_data_t *) (shared_mem+0x100000);\r |
256 | /* this area must not get buffered on either side */\r |
257 | shared_ctl = (_940_ctl_t *) (shared_mem+0x200000);\r |
e362c573 |
258 | mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2400000);\r |
f80d67e1 |
259 | if (mp3_mem == MAP_FAILED)\r |
260 | {\r |
261 | printf("mmap(mp3_mem) failed with %i\n", errno);\r |
262 | exit(1);\r |
263 | }\r |
264 | crashed_940 = 1;\r |
265 | }\r |
266 | \r |
267 | \r |
0d9bf4fc |
268 | void sharedmem940_finish(void)\r |
85f8e929 |
269 | {\r |
270 | munmap(shared_mem, 0x210000);\r |
271 | munmap(mp3_mem, MP3_SIZE_MAX);\r |
272 | shared_mem = mp3_mem = NULL;\r |
273 | shared_data = NULL;\r |
274 | shared_ctl = NULL;\r |
275 | }\r |
276 | \r |
277 | \r |
cc68a136 |
278 | extern char **g_argv;\r |
279 | \r |
cc68a136 |
280 | void YM2612Init_940(int baseclock, int rate)\r |
281 | {\r |
282 | printf("YM2612Init_940()\n");\r |
b837b69b |
283 | printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));\r |
cc68a136 |
284 | \r |
42171343 |
285 | reset940(1, 2);\r |
286 | pause940(1);\r |
cc68a136 |
287 | \r |
4f265db7 |
288 | gp2x_memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920\r |
289 | gp2x_memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940\r |
cc68a136 |
290 | \r |
abe0ea43 |
291 | gp2x_memregl[0x4504>>2] = 0; // make sure no FIQs will be generated\r |
292 | gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller\r |
293 | \r |
294 | \r |
cc68a136 |
295 | if (crashed_940)\r |
296 | {\r |
297 | unsigned char ucData[1024];\r |
cc41eb4f |
298 | int nRead, nLen = 0;\r |
27701801 |
299 | char binpath[512];\r |
cc68a136 |
300 | FILE *fp;\r |
301 | \r |
27701801 |
302 | emu_make_path(binpath, CODE940_FILE, sizeof(binpath));\r |
cc68a136 |
303 | fp = fopen(binpath, "rb");\r |
304 | if(!fp)\r |
305 | {\r |
e2de9939 |
306 | memset(g_screen_ptr, 0, 320*240*2);\r |
a12e0116 |
307 | text_out16(10, 100, "failed to open required file:");\r |
308 | text_out16(10, 110, CODE940_FILE);\r |
309 | gp2x_video_flip2();\r |
cc41eb4f |
310 | in_menu_wait(PBTN_MOK|PBTN_MBACK, 100);\r |
cc68a136 |
311 | printf("failed to open %s\n", binpath);\r |
312 | exit(1);\r |
313 | }\r |
314 | \r |
315 | while(1)\r |
316 | {\r |
317 | nRead = fread(ucData, 1, 1024, fp);\r |
318 | if(nRead <= 0)\r |
319 | break;\r |
320 | memcpy(shared_mem + nLen, ucData, nRead);\r |
321 | nLen += nRead;\r |
322 | }\r |
323 | fclose(fp);\r |
324 | crashed_940 = 0;\r |
325 | }\r |
326 | \r |
327 | memset(shared_data, 0, sizeof(*shared_data));\r |
328 | memset(shared_ctl, 0, sizeof(*shared_ctl));\r |
329 | \r |
5f8c85be |
330 | /* cause local ym2612 to init REGS */\r |
331 | YM2612Init_(baseclock, rate);\r |
332 | \r |
cc68a136 |
333 | internal_reset();\r |
334 | \r |
51a902ae |
335 | loaded_mp3 = 0;\r |
336 | \r |
4f265db7 |
337 | gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940\r |
abe0ea43 |
338 | gp2x_memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND\r |
339 | gp2x_memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND\r |
cc68a136 |
340 | \r |
341 | /* start the 940 */\r |
42171343 |
342 | reset940(0, 2);\r |
343 | pause940(0);\r |
cc68a136 |
344 | \r |
345 | // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT\r |
abe0ea43 |
346 | \r |
347 | /* now cause 940 to init it's ym2612 stuff */\r |
348 | shared_ctl->baseclock = baseclock;\r |
349 | shared_ctl->rate = rate;\r |
350 | add_job_940(JOB940_INITALL);\r |
cc68a136 |
351 | }\r |
352 | \r |
353 | \r |
354 | void YM2612ResetChip_940(void)\r |
355 | {\r |
daec7253 |
356 | //printf("YM2612ResetChip_940()\n");\r |
cc68a136 |
357 | if (shared_data == NULL) {\r |
358 | printf("YM2612ResetChip_940: reset before init?\n");\r |
359 | return;\r |
360 | }\r |
361 | \r |
4b9c5888 |
362 | YM2612ResetChip_();\r |
cc68a136 |
363 | internal_reset();\r |
364 | \r |
abe0ea43 |
365 | add_job_940(JOB940_YM2612RESETCHIP);\r |
cc68a136 |
366 | }\r |
367 | \r |
368 | \r |
abe0ea43 |
369 | int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)\r |
cb0316e4 |
370 | {\r |
abe0ea43 |
371 | int *ym_buf = shared_data->ym_buffer;\r |
85f8e929 |
372 | int ym_active_chs;\r |
4f265db7 |
373 | \r |
abe0ea43 |
374 | //printf("YM2612UpdateOne_940()\n");\r |
375 | \r |
376 | if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);\r |
377 | \r |
85f8e929 |
378 | ym_active_chs = shared_ctl->ym_active_chs;\r |
abe0ea43 |
379 | \r |
85f8e929 |
380 | // mix in ym buffer. is_buf_empty means nobody mixed there anything yet and it may contain trash\r |
381 | if (is_buf_empty && ym_active_chs) memcpy32(buffer, ym_buf, length<<stereo);\r |
382 | else memset32(buffer, 0, length<<stereo);\r |
abe0ea43 |
383 | \r |
384 | if (shared_ctl->writebuffsel == 1) {\r |
2433f409 |
385 | shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;\r |
4f265db7 |
386 | } else {\r |
2433f409 |
387 | shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;\r |
cb0316e4 |
388 | }\r |
abe0ea43 |
389 | writebuff_ptr = 0;\r |
390 | \r |
391 | /* predict sample counter for next frame */\r |
392 | if (PsndLen_exc_add) {\r |
52250671 |
393 | length = PsndLen;\r |
394 | if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length++;\r |
abe0ea43 |
395 | }\r |
396 | \r |
397 | /* give 940 ym job */\r |
398 | shared_ctl->writebuffsel ^= 1;\r |
399 | shared_ctl->length = length;\r |
400 | shared_ctl->stereo = stereo;\r |
401 | \r |
402 | add_job_940(JOB940_YM2612UPDATEONE);\r |
403 | \r |
85f8e929 |
404 | return ym_active_chs;\r |
cb0316e4 |
405 | }\r |
cb0316e4 |
406 | \r |
cb0316e4 |
407 | \r |
7c34867a |
408 | /***********************************************************/\r |
409 | \r |
51a902ae |
410 | static int mp3_samples_ready = 0, mp3_buffer_offs = 0;\r |
abe0ea43 |
411 | static int mp3_play_bufsel = 0, mp3_job_started = 0;\r |
cb0316e4 |
412 | \r |
abe0ea43 |
413 | void mp3_update(int *buffer, int length, int stereo)\r |
cc68a136 |
414 | {\r |
abe0ea43 |
415 | int length_mp3;\r |
cc68a136 |
416 | \r |
7c34867a |
417 | if (!(PicoOpt & POPT_EXT_FM)) {\r |
abe0ea43 |
418 | mp3_update_local(buffer, length, stereo);\r |
419 | return;\r |
420 | }\r |
cc68a136 |
421 | \r |
7c34867a |
422 | // check if playback was started, track not ended\r |
423 | if (loaded_mp3 == NULL || shared_ctl->mp3_offs >= shared_ctl->mp3_len)\r |
424 | return;\r |
425 | \r |
abe0ea43 |
426 | length_mp3 = length;\r |
427 | if (PsndRate == 22050) length_mp3 <<= 1; // mp3s are locked to 44100Hz stereo\r |
428 | else if (PsndRate == 11025) length_mp3 <<= 2; // so make length 44100ish\r |
4f265db7 |
429 | \r |
abe0ea43 |
430 | /* do we have to wait? */\r |
431 | if (mp3_job_started && mp3_samples_ready < length_mp3) {\r |
432 | if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);\r |
433 | mp3_job_started = 0;\r |
434 | mp3_samples_ready += 1152;\r |
435 | }\r |
cb0316e4 |
436 | \r |
4f265db7 |
437 | /* mix mp3 data, only stereo */\r |
abe0ea43 |
438 | if (mp3_samples_ready >= length_mp3)\r |
cb0316e4 |
439 | {\r |
4f265db7 |
440 | int shr = 0;\r |
441 | void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;\r |
442 | if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; shr = 1; }\r |
443 | else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; shr = 2; }\r |
cb0316e4 |
444 | \r |
4f265db7 |
445 | if (1152 - mp3_buffer_offs >= length_mp3) {\r |
abe0ea43 |
446 | mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1);\r |
4f265db7 |
447 | \r |
448 | mp3_buffer_offs += length_mp3;\r |
cb0316e4 |
449 | } else {\r |
abe0ea43 |
450 | // collect samples from both buffers..\r |
cb0316e4 |
451 | int left = 1152 - mp3_buffer_offs;\r |
4f265db7 |
452 | if (mp3_play_bufsel == 0)\r |
453 | {\r |
abe0ea43 |
454 | mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, length<<1);\r |
4f265db7 |
455 | mp3_buffer_offs = length_mp3 - left;\r |
456 | mp3_play_bufsel = 1;\r |
457 | } else {\r |
458 | mix_samples(buffer, shared_data->mp3_buffer[1] + mp3_buffer_offs*2, (left>>shr)<<1);\r |
459 | mp3_buffer_offs = length_mp3 - left;\r |
460 | mix_samples(buffer + ((left>>shr)<<1),\r |
461 | shared_data->mp3_buffer[0], (mp3_buffer_offs>>shr)<<1);\r |
462 | mp3_play_bufsel = 0;\r |
463 | }\r |
cc68a136 |
464 | }\r |
4f265db7 |
465 | mp3_samples_ready -= length_mp3;\r |
cc68a136 |
466 | }\r |
467 | \r |
abe0ea43 |
468 | // ask to decode more if we already can\r |
469 | if (!mp3_job_started)\r |
cb0316e4 |
470 | {\r |
abe0ea43 |
471 | mp3_job_started = 1;\r |
cb0316e4 |
472 | shared_ctl->mp3_buffsel ^= 1;\r |
abe0ea43 |
473 | add_job_940(JOB940_MP3DECODE);\r |
cb0316e4 |
474 | }\r |
cc68a136 |
475 | }\r |
cb0316e4 |
476 | \r |
477 | \r |
cb0316e4 |
478 | void mp3_start_play(FILE *f, int pos) // pos is 0-1023\r |
479 | {\r |
480 | int byte_offs = 0;\r |
481 | \r |
7c34867a |
482 | if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL)\r |
483 | return;\r |
484 | \r |
485 | if (!(PicoOpt & POPT_EXT_FM)) {\r |
486 | mp3_start_play_local(f, pos);\r |
cb0316e4 |
487 | return;\r |
488 | }\r |
489 | \r |
490 | if (loaded_mp3 != f)\r |
491 | {\r |
66fdc0f0 |
492 | if (PicoMessage != NULL)\r |
493 | {\r |
494 | fseek(f, 0, SEEK_END);\r |
495 | if (ftell(f) > 2*1024*1024)\r |
496 | PicoMessage("Loading MP3...");\r |
497 | }\r |
cb0316e4 |
498 | fseek(f, 0, SEEK_SET);\r |
499 | fread(mp3_mem, 1, MP3_SIZE_MAX, f);\r |
e362c573 |
500 | if (!feof(f)) printf("Warning: mp3 was too large, not all data loaded.\n");\r |
cb0316e4 |
501 | shared_ctl->mp3_len = ftell(f);\r |
502 | loaded_mp3 = f;\r |
abe0ea43 |
503 | \r |
091facb8 |
504 | if (PicoOpt & POPT_EXT_FM) {\r |
abe0ea43 |
505 | // as we are going to change 940's cacheable area, we must invalidate it's cache..\r |
506 | if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);\r |
507 | add_job_940(JOB940_INVALIDATE_DCACHE);\r |
508 | }\r |
00bd648e |
509 | reset_timing = 1;\r |
cb0316e4 |
510 | }\r |
511 | \r |
512 | // seek..\r |
513 | if (pos) {\r |
514 | byte_offs = (shared_ctl->mp3_len << 6) >> 10;\r |
515 | byte_offs *= pos;\r |
516 | byte_offs >>= 6;\r |
517 | }\r |
091facb8 |
518 | printf(" mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);\r |
cb0316e4 |
519 | \r |
520 | shared_ctl->mp3_offs = byte_offs;\r |
51a902ae |
521 | \r |
abe0ea43 |
522 | // reset buffer pointers and stuff..\r |
51a902ae |
523 | mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0;\r |
abe0ea43 |
524 | mp3_job_started = 0;\r |
51a902ae |
525 | shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode\r |
abe0ea43 |
526 | \r |
7c34867a |
527 | add_job_940(JOB940_MP3RESET);\r |
528 | if (CHECK_BUSY(JOB940_MP3RESET)) wait_busy_940(JOB940_MP3RESET);\r |
75736070 |
529 | }\r |
530 | \r |