fix GP2X build
[libpicofe.git] / gp2x / 940ctl.c
CommitLineData
6ab2f79c 1// Code for communication with ARM940 and control of it.\r
2// (c) Copyright 2007, Grazvydas "notaz" Ignotas\r
3\r
720ee7f6 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
b30a8e67 13#include "code940/940shared.h"\r
d572cbad 14#include "soc_mmsp2.h"\r
15#include "soc.h"\r
11f4e722 16#include "../common/mp3.h"\r
c7a4ff64 17#include "../common/arm_utils.h"\r
18#include "../common/menu.h"\r
f013066e 19#include "../common/emu.h"\r
b7911801 20#include "../common/input.h"\r
f11bad75 21#include "../../pico/pico_int.h"\r
22#include "../../pico/sound/ym2612.h"\r
23#include "../../pico/sound/mix.h"\r
49fe50f0 24\r
720ee7f6 25static unsigned char *shared_mem = 0;\r
26static _940_data_t *shared_data = 0;\r
edbb82f8 27_940_ctl_t *shared_ctl = 0;\r
28unsigned char *mp3_mem = 0;\r
598e7c06 29\r
60b4b403 30#define MP3_SIZE_MAX (0x400000 + 0x800000) // 12M\r
389aa76e 31#define CODE940_FILE "pico940_v3.bin"\r
720ee7f6 32\r
33int crashed_940 = 0;\r
34\r
979ba09f 35static FILE *loaded_mp3 = 0;\r
720ee7f6 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
720ee7f6 49static UINT8 ST_address; /* address register */\r
7481dd3a 50static INT32 addr_A1; /* address line A1 */\r
720ee7f6 51\r
52static int writebuff_ptr = 0;\r
53\r
54\r
720ee7f6 55/* YM2612 write */\r
56/* a = address */\r
57/* v = value */\r
58/* returns 1 if sample affecting state changed */\r
7481dd3a 59int YM2612Write_940(unsigned int a, unsigned int v, int scanline)\r
720ee7f6 60{\r
79cad122 61 int upd = 1; /* the write affects sample generation */\r
720ee7f6 62\r
720ee7f6 63 a &= 3;\r
64\r
79cad122 65 //printf("%05i:%03i: ym w ([%i] %02x)\n", Pico.m.frame_count, Pico.m.scanline, a, v);\r
66\r
389aa76e 67 switch (a)\r
68 {\r
7481dd3a 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
d032c15a 76 return 0;\r
7481dd3a 77 upd = 0;\r
78 break;\r
720ee7f6 79\r
7481dd3a 80 case 2: /* address port 1 */\r
81 if (addr_A1 == 1 && ST_address == v)\r
720ee7f6 82 return 0;\r
7481dd3a 83 ST_address = v;\r
84 addr_A1 = 1;\r
85 upd = 0;\r
720ee7f6 86 break;\r
720ee7f6 87 }\r
88\r
79cad122 89 //printf("ym pass\n");\r
90\r
7481dd3a 91 if (currentConfig.EmuOpt & 4)\r
92 {\r
79cad122 93 UINT16 *writebuff = shared_ctl->writebuffsel ? shared_ctl->writebuff0 : shared_ctl->writebuff1;\r
94\r
95 /* detect rapid ym updates */\r
7481dd3a 96 if (upd && !(writebuff_ptr & 0x80000000) && scanline < 224)\r
97 {\r
79cad122 98 int mid = Pico.m.pal ? 68 : 93;\r
7481dd3a 99 if (scanline > mid) {\r
100 //printf("%05i:%03i: rapid ym\n", Pico.m.frame_count, scanline);\r
79cad122 101 writebuff[writebuff_ptr++ & 0xffff] = 0xfffe;\r
102 writebuff_ptr |= 0x80000000;\r
7481dd3a 103 //printf("%05i:%03i: ym w ([%02x] %02x, upd=%i)\n", Pico.m.frame_count, scanline, addr, v, upd);\r
720ee7f6 104 }\r
79cad122 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
720ee7f6 110 } else {\r
d032c15a 111 printf("warning: writebuff_ptr > 2047 ([%i] %02x)\n", a, v);\r
720ee7f6 112 }\r
113 }\r
114\r
115 return 0; // cause the engine to do updates once per frame only\r
116}\r
117\r
720ee7f6 118\r
edbb82f8 119#define CHECK_BUSY(job) \\r
120 (gp2x_memregs[0x3b46>>1] & (1<<(job-1)))\r
121\r
122static void wait_busy_940(int job)\r
720ee7f6 123{\r
124 int i;\r
720ee7f6 125\r
edbb82f8 126 job--;\r
127 for (i = 0; (gp2x_memregs[0x3b46>>1] & (1<<job)) && i < 0x10000; i++)\r
b30a8e67 128 spend_cycles(8*1024); // tested to be best for mp3 dec\r
720ee7f6 129 if (i < 0x10000) return;\r
130\r
131 /* 940 crashed */\r
b30a8e67 132 printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);\r
720ee7f6 133 for (i = 0; i < 8; i++)\r
134 printf("%i ", shared_ctl->vstarts[i]);\r
135 printf(")\n");\r
edbb82f8 136 printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",\r
49fe50f0 137 gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);\r
edbb82f8 138 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r
49fe50f0 139 printf("trying to interrupt..\n");\r
140 gp2x_memregs[0x3B3E>>1] = 0xffff;\r
edbb82f8 141 for (i = 0; gp2x_memregs[0x3b46>>1] && i < 0x10000; i++)\r
49fe50f0 142 spend_cycles(8*1024);\r
143 printf("i = 0x%x\n", i);\r
edbb82f8 144 printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",\r
49fe50f0 145 gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);\r
edbb82f8 146 printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r
49fe50f0 147\r
3ffee69c 148 me_update_msg("940 crashed, too much overclock?");\r
720ee7f6 149 engineState = PGS_Menu;\r
150 crashed_940 = 1;\r
720ee7f6 151}\r
152\r
153\r
edbb82f8 154static void add_job_940(int job)\r
720ee7f6 155{\r
edbb82f8 156 if (job <= 0 || job > 16) {\r
157 printf("add_job_940: bad job: %i\n", job);\r
158 return;\r
49fe50f0 159 }\r
edbb82f8 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
720ee7f6 166}\r
167\r
168\r
169void YM2612PicoStateLoad_940(void)\r
170{\r
7481dd3a 171 UINT8 *REGS = YM2612GetRegs();\r
172\r
fb88e614 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
7481dd3a 178 addr_A1 = *(INT32 *) (REGS + 0x200);\r
720ee7f6 179}\r
180\r
389aa76e 181void 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
203int 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
720ee7f6 236\r
237static void internal_reset(void)\r
238{\r
239 writebuff_ptr = 0;\r
7481dd3a 240 ST_address = addr_A1 = -1;\r
720ee7f6 241}\r
242\r
243\r
52d3dd5f 244/* this must be called after mmu hack, the allocated regions must not get cached */\r
fa5e045b 245void sharedmem940_init(void)\r
52d3dd5f 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
389aa76e 250 if (shared_mem == MAP_FAILED)\r
52d3dd5f 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
60b4b403 258 mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2400000);\r
52d3dd5f 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
fa5e045b 268void sharedmem940_finish(void)\r
4dfd9f42 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
720ee7f6 278extern char **g_argv;\r
279\r
720ee7f6 280void YM2612Init_940(int baseclock, int rate)\r
281{\r
282 printf("YM2612Init_940()\n");\r
8dfb9fd5 283 printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));\r
720ee7f6 284\r
d572cbad 285 reset940(1, 2);\r
286 pause940(1);\r
720ee7f6 287\r
49fe50f0 288 gp2x_memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920\r
289 gp2x_memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940\r
720ee7f6 290\r
edbb82f8 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
720ee7f6 295 if (crashed_940)\r
296 {\r
297 unsigned char ucData[1024];\r
b7911801 298 int nRead, nLen = 0;\r
45deb409 299 char binpath[512];\r
720ee7f6 300 FILE *fp;\r
301\r
45deb409 302 emu_make_path(binpath, CODE940_FILE, sizeof(binpath));\r
720ee7f6 303 fp = fopen(binpath, "rb");\r
304 if(!fp)\r
305 {\r
e31266dd 306 memset(g_screen_ptr, 0, 320*240*2);\r
13059a60 307 text_out16(10, 100, "failed to open required file:");\r
308 text_out16(10, 110, CODE940_FILE);\r
309 gp2x_video_flip2();\r
b7911801 310 in_menu_wait(PBTN_MOK|PBTN_MBACK, 100);\r
720ee7f6 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
31a77bde 330 /* cause local ym2612 to init REGS */\r
331 YM2612Init_(baseclock, rate);\r
332\r
720ee7f6 333 internal_reset();\r
334\r
979ba09f 335 loaded_mp3 = 0;\r
336\r
49fe50f0 337 gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940\r
edbb82f8 338 gp2x_memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND\r
339 gp2x_memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND\r
720ee7f6 340\r
341 /* start the 940 */\r
d572cbad 342 reset940(0, 2);\r
343 pause940(0);\r
720ee7f6 344\r
345 // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT\r
edbb82f8 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
720ee7f6 351}\r
352\r
353\r
354void YM2612ResetChip_940(void)\r
355{\r
f76e88b5 356 //printf("YM2612ResetChip_940()\n");\r
720ee7f6 357 if (shared_data == NULL) {\r
358 printf("YM2612ResetChip_940: reset before init?\n");\r
359 return;\r
360 }\r
361\r
7481dd3a 362 YM2612ResetChip_();\r
720ee7f6 363 internal_reset();\r
364\r
edbb82f8 365 add_job_940(JOB940_YM2612RESETCHIP);\r
720ee7f6 366}\r
367\r
368\r
edbb82f8 369int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)\r
598e7c06 370{\r
edbb82f8 371 int *ym_buf = shared_data->ym_buffer;\r
4dfd9f42 372 int ym_active_chs;\r
49fe50f0 373\r
edbb82f8 374 //printf("YM2612UpdateOne_940()\n");\r
375\r
376 if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);\r
377\r
4dfd9f42 378 ym_active_chs = shared_ctl->ym_active_chs;\r
edbb82f8 379\r
4dfd9f42 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
edbb82f8 383\r
384 if (shared_ctl->writebuffsel == 1) {\r
79cad122 385 shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;\r
49fe50f0 386 } else {\r
79cad122 387 shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;\r
598e7c06 388 }\r
edbb82f8 389 writebuff_ptr = 0;\r
390\r
391 /* predict sample counter for next frame */\r
392 if (PsndLen_exc_add) {\r
2dce60fd 393 length = PsndLen;\r
394 if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length++;\r
edbb82f8 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
4dfd9f42 404 return ym_active_chs;\r
598e7c06 405}\r
598e7c06 406\r
598e7c06 407\r
9615b3df 408/***********************************************************/\r
409\r
979ba09f 410static int mp3_samples_ready = 0, mp3_buffer_offs = 0;\r
edbb82f8 411static int mp3_play_bufsel = 0, mp3_job_started = 0;\r
598e7c06 412\r
edbb82f8 413void mp3_update(int *buffer, int length, int stereo)\r
720ee7f6 414{\r
edbb82f8 415 int length_mp3;\r
720ee7f6 416\r
9615b3df 417 if (!(PicoOpt & POPT_EXT_FM)) {\r
edbb82f8 418 mp3_update_local(buffer, length, stereo);\r
419 return;\r
420 }\r
720ee7f6 421\r
9615b3df 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
edbb82f8 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
49fe50f0 429\r
edbb82f8 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
598e7c06 436\r
49fe50f0 437 /* mix mp3 data, only stereo */\r
edbb82f8 438 if (mp3_samples_ready >= length_mp3)\r
598e7c06 439 {\r
49fe50f0 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
598e7c06 444\r
49fe50f0 445 if (1152 - mp3_buffer_offs >= length_mp3) {\r
edbb82f8 446 mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1);\r
49fe50f0 447\r
448 mp3_buffer_offs += length_mp3;\r
598e7c06 449 } else {\r
edbb82f8 450 // collect samples from both buffers..\r
598e7c06 451 int left = 1152 - mp3_buffer_offs;\r
49fe50f0 452 if (mp3_play_bufsel == 0)\r
453 {\r
edbb82f8 454 mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, length<<1);\r
49fe50f0 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
720ee7f6 464 }\r
49fe50f0 465 mp3_samples_ready -= length_mp3;\r
720ee7f6 466 }\r
467\r
edbb82f8 468 // ask to decode more if we already can\r
469 if (!mp3_job_started)\r
598e7c06 470 {\r
edbb82f8 471 mp3_job_started = 1;\r
598e7c06 472 shared_ctl->mp3_buffsel ^= 1;\r
edbb82f8 473 add_job_940(JOB940_MP3DECODE);\r
598e7c06 474 }\r
720ee7f6 475}\r
598e7c06 476\r
477\r
598e7c06 478void mp3_start_play(FILE *f, int pos) // pos is 0-1023\r
479{\r
480 int byte_offs = 0;\r
481\r
9615b3df 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
598e7c06 487 return;\r
488 }\r
489\r
490 if (loaded_mp3 != f)\r
491 {\r
dfa4c846 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
598e7c06 498 fseek(f, 0, SEEK_SET);\r
499 fread(mp3_mem, 1, MP3_SIZE_MAX, f);\r
60b4b403 500 if (!feof(f)) printf("Warning: mp3 was too large, not all data loaded.\n");\r
598e7c06 501 shared_ctl->mp3_len = ftell(f);\r
502 loaded_mp3 = f;\r
edbb82f8 503\r
6e507f76 504 if (PicoOpt & POPT_EXT_FM) {\r
edbb82f8 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
8f1b51ef 509 reset_timing = 1;\r
598e7c06 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
6e507f76 518 printf(" mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);\r
598e7c06 519\r
520 shared_ctl->mp3_offs = byte_offs;\r
979ba09f 521\r
edbb82f8 522 // reset buffer pointers and stuff..\r
979ba09f 523 mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0;\r
edbb82f8 524 mp3_job_started = 0;\r
979ba09f 525 shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode\r
edbb82f8 526\r
9615b3df 527 add_job_940(JOB940_MP3RESET);\r
528 if (CHECK_BUSY(JOB940_MP3RESET)) wait_busy_940(JOB940_MP3RESET);\r
98c9d8d9 529}\r
530\r