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 | void YM2612Init_940(int baseclock, int rate)\r |
279 | {\r |
280 | printf("YM2612Init_940()\n");\r |
b837b69b |
281 | printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));\r |
cc68a136 |
282 | \r |
42171343 |
283 | reset940(1, 2);\r |
284 | pause940(1);\r |
cc68a136 |
285 | \r |
4f265db7 |
286 | gp2x_memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920\r |
287 | gp2x_memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940\r |
cc68a136 |
288 | \r |
abe0ea43 |
289 | gp2x_memregl[0x4504>>2] = 0; // make sure no FIQs will be generated\r |
290 | gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller\r |
291 | \r |
292 | \r |
cc68a136 |
293 | if (crashed_940)\r |
294 | {\r |
295 | unsigned char ucData[1024];\r |
cc41eb4f |
296 | int nRead, nLen = 0;\r |
27701801 |
297 | char binpath[512];\r |
cc68a136 |
298 | FILE *fp;\r |
299 | \r |
27701801 |
300 | emu_make_path(binpath, CODE940_FILE, sizeof(binpath));\r |
cc68a136 |
301 | fp = fopen(binpath, "rb");\r |
302 | if(!fp)\r |
303 | {\r |
e2de9939 |
304 | memset(g_screen_ptr, 0, 320*240*2);\r |
a12e0116 |
305 | text_out16(10, 100, "failed to open required file:");\r |
306 | text_out16(10, 110, CODE940_FILE);\r |
307 | gp2x_video_flip2();\r |
cc41eb4f |
308 | in_menu_wait(PBTN_MOK|PBTN_MBACK, 100);\r |
cc68a136 |
309 | printf("failed to open %s\n", binpath);\r |
310 | exit(1);\r |
311 | }\r |
312 | \r |
313 | while(1)\r |
314 | {\r |
315 | nRead = fread(ucData, 1, 1024, fp);\r |
316 | if(nRead <= 0)\r |
317 | break;\r |
318 | memcpy(shared_mem + nLen, ucData, nRead);\r |
319 | nLen += nRead;\r |
320 | }\r |
321 | fclose(fp);\r |
322 | crashed_940 = 0;\r |
323 | }\r |
324 | \r |
325 | memset(shared_data, 0, sizeof(*shared_data));\r |
326 | memset(shared_ctl, 0, sizeof(*shared_ctl));\r |
327 | \r |
5f8c85be |
328 | /* cause local ym2612 to init REGS */\r |
329 | YM2612Init_(baseclock, rate);\r |
330 | \r |
cc68a136 |
331 | internal_reset();\r |
332 | \r |
51a902ae |
333 | loaded_mp3 = 0;\r |
334 | \r |
4f265db7 |
335 | gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940\r |
abe0ea43 |
336 | gp2x_memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND\r |
337 | gp2x_memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND\r |
cc68a136 |
338 | \r |
339 | /* start the 940 */\r |
42171343 |
340 | reset940(0, 2);\r |
341 | pause940(0);\r |
cc68a136 |
342 | \r |
343 | // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT\r |
abe0ea43 |
344 | \r |
345 | /* now cause 940 to init it's ym2612 stuff */\r |
346 | shared_ctl->baseclock = baseclock;\r |
347 | shared_ctl->rate = rate;\r |
348 | add_job_940(JOB940_INITALL);\r |
cc68a136 |
349 | }\r |
350 | \r |
351 | \r |
352 | void YM2612ResetChip_940(void)\r |
353 | {\r |
daec7253 |
354 | //printf("YM2612ResetChip_940()\n");\r |
cc68a136 |
355 | if (shared_data == NULL) {\r |
356 | printf("YM2612ResetChip_940: reset before init?\n");\r |
357 | return;\r |
358 | }\r |
359 | \r |
4b9c5888 |
360 | YM2612ResetChip_();\r |
cc68a136 |
361 | internal_reset();\r |
362 | \r |
abe0ea43 |
363 | add_job_940(JOB940_YM2612RESETCHIP);\r |
cc68a136 |
364 | }\r |
365 | \r |
366 | \r |
abe0ea43 |
367 | int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)\r |
cb0316e4 |
368 | {\r |
abe0ea43 |
369 | int *ym_buf = shared_data->ym_buffer;\r |
85f8e929 |
370 | int ym_active_chs;\r |
4f265db7 |
371 | \r |
abe0ea43 |
372 | //printf("YM2612UpdateOne_940()\n");\r |
373 | \r |
374 | if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);\r |
375 | \r |
85f8e929 |
376 | ym_active_chs = shared_ctl->ym_active_chs;\r |
abe0ea43 |
377 | \r |
85f8e929 |
378 | // mix in ym buffer. is_buf_empty means nobody mixed there anything yet and it may contain trash\r |
379 | if (is_buf_empty && ym_active_chs) memcpy32(buffer, ym_buf, length<<stereo);\r |
380 | else memset32(buffer, 0, length<<stereo);\r |
abe0ea43 |
381 | \r |
382 | if (shared_ctl->writebuffsel == 1) {\r |
2433f409 |
383 | shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;\r |
4f265db7 |
384 | } else {\r |
2433f409 |
385 | shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;\r |
cb0316e4 |
386 | }\r |
abe0ea43 |
387 | writebuff_ptr = 0;\r |
388 | \r |
389 | /* predict sample counter for next frame */\r |
390 | if (PsndLen_exc_add) {\r |
52250671 |
391 | length = PsndLen;\r |
392 | if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length++;\r |
abe0ea43 |
393 | }\r |
394 | \r |
395 | /* give 940 ym job */\r |
396 | shared_ctl->writebuffsel ^= 1;\r |
397 | shared_ctl->length = length;\r |
398 | shared_ctl->stereo = stereo;\r |
399 | \r |
400 | add_job_940(JOB940_YM2612UPDATEONE);\r |
401 | \r |
85f8e929 |
402 | return ym_active_chs;\r |
cb0316e4 |
403 | }\r |
cb0316e4 |
404 | \r |
cb0316e4 |
405 | \r |
7c34867a |
406 | /***********************************************************/\r |
407 | \r |
51a902ae |
408 | static int mp3_samples_ready = 0, mp3_buffer_offs = 0;\r |
abe0ea43 |
409 | static int mp3_play_bufsel = 0, mp3_job_started = 0;\r |
cb0316e4 |
410 | \r |
abe0ea43 |
411 | void mp3_update(int *buffer, int length, int stereo)\r |
cc68a136 |
412 | {\r |
abe0ea43 |
413 | int length_mp3;\r |
cc68a136 |
414 | \r |
7c34867a |
415 | if (!(PicoOpt & POPT_EXT_FM)) {\r |
abe0ea43 |
416 | mp3_update_local(buffer, length, stereo);\r |
417 | return;\r |
418 | }\r |
cc68a136 |
419 | \r |
7c34867a |
420 | // check if playback was started, track not ended\r |
421 | if (loaded_mp3 == NULL || shared_ctl->mp3_offs >= shared_ctl->mp3_len)\r |
422 | return;\r |
423 | \r |
abe0ea43 |
424 | length_mp3 = length;\r |
425 | if (PsndRate == 22050) length_mp3 <<= 1; // mp3s are locked to 44100Hz stereo\r |
426 | else if (PsndRate == 11025) length_mp3 <<= 2; // so make length 44100ish\r |
4f265db7 |
427 | \r |
abe0ea43 |
428 | /* do we have to wait? */\r |
429 | if (mp3_job_started && mp3_samples_ready < length_mp3) {\r |
430 | if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);\r |
431 | mp3_job_started = 0;\r |
432 | mp3_samples_ready += 1152;\r |
433 | }\r |
cb0316e4 |
434 | \r |
4f265db7 |
435 | /* mix mp3 data, only stereo */\r |
abe0ea43 |
436 | if (mp3_samples_ready >= length_mp3)\r |
cb0316e4 |
437 | {\r |
4f265db7 |
438 | int shr = 0;\r |
439 | void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;\r |
440 | if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; shr = 1; }\r |
441 | else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; shr = 2; }\r |
cb0316e4 |
442 | \r |
4f265db7 |
443 | if (1152 - mp3_buffer_offs >= length_mp3) {\r |
abe0ea43 |
444 | mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1);\r |
4f265db7 |
445 | \r |
446 | mp3_buffer_offs += length_mp3;\r |
cb0316e4 |
447 | } else {\r |
abe0ea43 |
448 | // collect samples from both buffers..\r |
cb0316e4 |
449 | int left = 1152 - mp3_buffer_offs;\r |
4f265db7 |
450 | if (mp3_play_bufsel == 0)\r |
451 | {\r |
abe0ea43 |
452 | mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, length<<1);\r |
4f265db7 |
453 | mp3_buffer_offs = length_mp3 - left;\r |
454 | mp3_play_bufsel = 1;\r |
455 | } else {\r |
456 | mix_samples(buffer, shared_data->mp3_buffer[1] + mp3_buffer_offs*2, (left>>shr)<<1);\r |
457 | mp3_buffer_offs = length_mp3 - left;\r |
458 | mix_samples(buffer + ((left>>shr)<<1),\r |
459 | shared_data->mp3_buffer[0], (mp3_buffer_offs>>shr)<<1);\r |
460 | mp3_play_bufsel = 0;\r |
461 | }\r |
cc68a136 |
462 | }\r |
4f265db7 |
463 | mp3_samples_ready -= length_mp3;\r |
cc68a136 |
464 | }\r |
465 | \r |
abe0ea43 |
466 | // ask to decode more if we already can\r |
467 | if (!mp3_job_started)\r |
cb0316e4 |
468 | {\r |
abe0ea43 |
469 | mp3_job_started = 1;\r |
cb0316e4 |
470 | shared_ctl->mp3_buffsel ^= 1;\r |
abe0ea43 |
471 | add_job_940(JOB940_MP3DECODE);\r |
cb0316e4 |
472 | }\r |
cc68a136 |
473 | }\r |
cb0316e4 |
474 | \r |
475 | \r |
7c18e34a |
476 | void mp3_start_play(void *f_, int pos) // pos is 0-1023\r |
cb0316e4 |
477 | {\r |
478 | int byte_offs = 0;\r |
7c18e34a |
479 | FILE *f = f_;\r |
cb0316e4 |
480 | \r |
7c34867a |
481 | if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL)\r |
482 | return;\r |
483 | \r |
484 | if (!(PicoOpt & POPT_EXT_FM)) {\r |
485 | mp3_start_play_local(f, pos);\r |
cb0316e4 |
486 | return;\r |
487 | }\r |
488 | \r |
489 | if (loaded_mp3 != f)\r |
490 | {\r |
66fdc0f0 |
491 | if (PicoMessage != NULL)\r |
492 | {\r |
493 | fseek(f, 0, SEEK_END);\r |
494 | if (ftell(f) > 2*1024*1024)\r |
495 | PicoMessage("Loading MP3...");\r |
496 | }\r |
cb0316e4 |
497 | fseek(f, 0, SEEK_SET);\r |
498 | fread(mp3_mem, 1, MP3_SIZE_MAX, f);\r |
e362c573 |
499 | if (!feof(f)) printf("Warning: mp3 was too large, not all data loaded.\n");\r |
cb0316e4 |
500 | shared_ctl->mp3_len = ftell(f);\r |
501 | loaded_mp3 = f;\r |
abe0ea43 |
502 | \r |
091facb8 |
503 | if (PicoOpt & POPT_EXT_FM) {\r |
abe0ea43 |
504 | // as we are going to change 940's cacheable area, we must invalidate it's cache..\r |
505 | if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);\r |
506 | add_job_940(JOB940_INVALIDATE_DCACHE);\r |
507 | }\r |
00bd648e |
508 | reset_timing = 1;\r |
cb0316e4 |
509 | }\r |
510 | \r |
511 | // seek..\r |
512 | if (pos) {\r |
513 | byte_offs = (shared_ctl->mp3_len << 6) >> 10;\r |
514 | byte_offs *= pos;\r |
515 | byte_offs >>= 6;\r |
516 | }\r |
091facb8 |
517 | printf(" mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);\r |
cb0316e4 |
518 | \r |
519 | shared_ctl->mp3_offs = byte_offs;\r |
51a902ae |
520 | \r |
abe0ea43 |
521 | // reset buffer pointers and stuff..\r |
51a902ae |
522 | mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0;\r |
abe0ea43 |
523 | mp3_job_started = 0;\r |
51a902ae |
524 | shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode\r |
abe0ea43 |
525 | \r |
7c34867a |
526 | add_job_940(JOB940_MP3RESET);\r |
527 | if (CHECK_BUSY(JOB940_MP3RESET)) wait_busy_940(JOB940_MP3RESET);\r |
75736070 |
528 | }\r |
529 | \r |