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