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