| 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_v2.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 *REGS = 0; /* we will also keep local copy of regs for savestates and such */\r |
| 55 | static INT32 *addr_A1; /* address line A1 */\r |
| 56 | \r |
| 57 | static int dacen;\r |
| 58 | static INT32 dacout;\r |
| 59 | static UINT8 ST_address; /* address register */\r |
| 60 | \r |
| 61 | static int writebuff_ptr = 0;\r |
| 62 | \r |
| 63 | \r |
| 64 | /* OPN Mode Register Write */\r |
| 65 | static int set_timers( int v )\r |
| 66 | {\r |
| 67 | int change;\r |
| 68 | \r |
| 69 | /* b7 = CSM MODE */\r |
| 70 | /* b6 = 3 slot mode */\r |
| 71 | /* b5 = reset b */\r |
| 72 | /* b4 = reset a */\r |
| 73 | /* b3 = timer enable b */\r |
| 74 | /* b2 = timer enable a */\r |
| 75 | /* b1 = load b */\r |
| 76 | /* b0 = load a */\r |
| 77 | change = (ym2612_st->mode ^ v) & 0xc0;\r |
| 78 | ym2612_st->mode = v;\r |
| 79 | \r |
| 80 | /* reset Timer b flag */\r |
| 81 | if( v & 0x20 )\r |
| 82 | ym2612_st->status &= ~2;\r |
| 83 | \r |
| 84 | /* reset Timer a flag */\r |
| 85 | if( v & 0x10 )\r |
| 86 | ym2612_st->status &= ~1;\r |
| 87 | \r |
| 88 | return change;\r |
| 89 | }\r |
| 90 | \r |
| 91 | /* YM2612 write */\r |
| 92 | /* a = address */\r |
| 93 | /* v = value */\r |
| 94 | /* returns 1 if sample affecting state changed */\r |
| 95 | int YM2612Write_940(unsigned int a, unsigned int v)\r |
| 96 | {\r |
| 97 | int addr;\r |
| 98 | int upd = 1; /* the write affects sample generation */\r |
| 99 | \r |
| 100 | v &= 0xff; /* adjust to 8 bit bus */\r |
| 101 | a &= 3;\r |
| 102 | \r |
| 103 | //printf("%05i:%03i: ym w ([%i] %02x)\n", Pico.m.frame_count, Pico.m.scanline, a, v);\r |
| 104 | \r |
| 105 | switch( a ) {\r |
| 106 | case 0: /* address port 0 */\r |
| 107 | if (!*addr_A1 && ST_address == v)\r |
| 108 | return 0; /* address already selected, don't send this command to 940 */\r |
| 109 | ST_address = v;\r |
| 110 | /* don't send DAC or timer related address changes to 940 */\r |
| 111 | if (!*addr_A1 && (v & 0xf0) == 0x20 &&\r |
| 112 | (v == 0x24 || v == 0x25 || v == 0x26 || v == 0x2a))\r |
| 113 | return 0;\r |
| 114 | *addr_A1 = 0;\r |
| 115 | upd = 0;\r |
| 116 | break;\r |
| 117 | \r |
| 118 | case 1: /* data port 0 */\r |
| 119 | if (*addr_A1 != 0) {\r |
| 120 | return 0; /* verified on real YM2608 */\r |
| 121 | }\r |
| 122 | \r |
| 123 | addr = ST_address;\r |
| 124 | REGS[addr] = v;\r |
| 125 | \r |
| 126 | switch( addr & 0xf0 )\r |
| 127 | {\r |
| 128 | case 0x20: /* 0x20-0x2f Mode */\r |
| 129 | switch( addr )\r |
| 130 | {\r |
| 131 | case 0x24: { // timer A High 8\r |
| 132 | int TAnew = (ym2612_st->TA & 0x03)|(((int)v)<<2);\r |
| 133 | if (ym2612_st->TA != TAnew) {\r |
| 134 | // we should reset ticker only if new value is written. Outrun requires this.\r |
| 135 | ym2612_st->TA = TAnew;\r |
| 136 | ym2612_st->TAC = (1024-TAnew)*18;\r |
| 137 | ym2612_st->TAT = 0;\r |
| 138 | }\r |
| 139 | return 0;\r |
| 140 | }\r |
| 141 | case 0x25: { // timer A Low 2\r |
| 142 | int TAnew = (ym2612_st->TA & 0x3fc)|(v&3);\r |
| 143 | if (ym2612_st->TA != TAnew) {\r |
| 144 | ym2612_st->TA = TAnew;\r |
| 145 | ym2612_st->TAC = (1024-TAnew)*18;\r |
| 146 | ym2612_st->TAT = 0;\r |
| 147 | }\r |
| 148 | return 0;\r |
| 149 | }\r |
| 150 | case 0x26: // timer B\r |
| 151 | if (ym2612_st->TB != v) {\r |
| 152 | ym2612_st->TB = v;\r |
| 153 | ym2612_st->TBC = (256-v)<<4;\r |
| 154 | ym2612_st->TBC *= 18;\r |
| 155 | ym2612_st->TBT = 0;\r |
| 156 | }\r |
| 157 | return 0;\r |
| 158 | case 0x27: /* mode, timer control */\r |
| 159 | if (set_timers( v ))\r |
| 160 | break; // other side needs ST.mode for 3slot mode\r |
| 161 | return 0;\r |
| 162 | case 0x2a: /* DAC data (YM2612) */\r |
| 163 | dacout = ((int)v - 0x80) << 6; /* level unknown (notaz: 8 seems to be too much) */\r |
| 164 | return 0;\r |
| 165 | case 0x2b: /* DAC Sel (YM2612) */\r |
| 166 | /* b7 = dac enable */\r |
| 167 | dacen = v & 0x80;\r |
| 168 | upd = 0;\r |
| 169 | break; // other side has to know this\r |
| 170 | default:\r |
| 171 | break;\r |
| 172 | }\r |
| 173 | break;\r |
| 174 | }\r |
| 175 | break;\r |
| 176 | \r |
| 177 | case 2: /* address port 1 */\r |
| 178 | if (*addr_A1 && ST_address == v)\r |
| 179 | return 0;\r |
| 180 | ST_address = v;\r |
| 181 | *addr_A1 = 1;\r |
| 182 | upd = 0;\r |
| 183 | break;\r |
| 184 | \r |
| 185 | case 3: /* data port 1 */\r |
| 186 | if (*addr_A1 != 1) {\r |
| 187 | return 0; /* verified on real YM2608 */\r |
| 188 | }\r |
| 189 | \r |
| 190 | addr = ST_address | 0x100;\r |
| 191 | REGS[addr] = v;\r |
| 192 | break;\r |
| 193 | }\r |
| 194 | \r |
| 195 | //printf("ym pass\n");\r |
| 196 | \r |
| 197 | if(currentConfig.EmuOpt & 4) {\r |
| 198 | UINT16 *writebuff = shared_ctl->writebuffsel ? shared_ctl->writebuff0 : shared_ctl->writebuff1;\r |
| 199 | \r |
| 200 | /* detect rapid ym updates */\r |
| 201 | if (upd && !(writebuff_ptr & 0x80000000) && Pico.m.scanline < 224) {\r |
| 202 | int mid = Pico.m.pal ? 68 : 93;\r |
| 203 | if (Pico.m.scanline > mid) {\r |
| 204 | //printf("%05i:%03i: rapid ym\n", Pico.m.frame_count, Pico.m.scanline);\r |
| 205 | writebuff[writebuff_ptr++ & 0xffff] = 0xfffe;\r |
| 206 | writebuff_ptr |= 0x80000000;\r |
| 207 | //printf("%05i:%03i: ym w ([%02x] %02x, upd=%i)\n", Pico.m.frame_count, Pico.m.scanline, addr, v, upd);\r |
| 208 | }\r |
| 209 | }\r |
| 210 | \r |
| 211 | /* queue this write for 940 */\r |
| 212 | if ((writebuff_ptr&0xffff) < 2047) {\r |
| 213 | writebuff[writebuff_ptr++ & 0xffff] = (a<<8)|v;\r |
| 214 | } else {\r |
| 215 | printf("warning: writebuff_ptr > 2047 ([%i] %02x)\n", a, v);\r |
| 216 | }\r |
| 217 | }\r |
| 218 | \r |
| 219 | return 0; // cause the engine to do updates once per frame only\r |
| 220 | }\r |
| 221 | \r |
| 222 | \r |
| 223 | #define CHECK_BUSY(job) \\r |
| 224 | (gp2x_memregs[0x3b46>>1] & (1<<(job-1)))\r |
| 225 | \r |
| 226 | static void wait_busy_940(int job)\r |
| 227 | {\r |
| 228 | int i;\r |
| 229 | \r |
| 230 | job--;\r |
| 231 | for (i = 0; (gp2x_memregs[0x3b46>>1] & (1<<job)) && i < 0x10000; i++)\r |
| 232 | spend_cycles(8*1024); // tested to be best for mp3 dec\r |
| 233 | if (i < 0x10000) return;\r |
| 234 | \r |
| 235 | /* 940 crashed */\r |
| 236 | printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);\r |
| 237 | for (i = 0; i < 8; i++)\r |
| 238 | printf("%i ", shared_ctl->vstarts[i]);\r |
| 239 | printf(")\n");\r |
| 240 | printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",\r |
| 241 | gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);\r |
| 242 | printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r |
| 243 | printf("trying to interrupt..\n");\r |
| 244 | gp2x_memregs[0x3B3E>>1] = 0xffff;\r |
| 245 | for (i = 0; gp2x_memregs[0x3b46>>1] && i < 0x10000; i++)\r |
| 246 | spend_cycles(8*1024);\r |
| 247 | printf("i = 0x%x\n", i);\r |
| 248 | printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",\r |
| 249 | gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);\r |
| 250 | printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r |
| 251 | \r |
| 252 | strcpy(menuErrorMsg, "940 crashed, too much overclock?");\r |
| 253 | engineState = PGS_Menu;\r |
| 254 | crashed_940 = 1;\r |
| 255 | }\r |
| 256 | \r |
| 257 | \r |
| 258 | static void add_job_940(int job)\r |
| 259 | {\r |
| 260 | if (job <= 0 || job > 16) {\r |
| 261 | printf("add_job_940: bad job: %i\n", job);\r |
| 262 | return;\r |
| 263 | }\r |
| 264 | \r |
| 265 | // generate interrupt for this job\r |
| 266 | job--;\r |
| 267 | gp2x_memregs[(0x3B20+job*2)>>1] = 1;\r |
| 268 | \r |
| 269 | // printf("added %i, pending %04x\n", job+1, gp2x_memregs[0x3b46>>1]);\r |
| 270 | }\r |
| 271 | \r |
| 272 | \r |
| 273 | void YM2612PicoStateLoad_940(void)\r |
| 274 | {\r |
| 275 | int i, old_A1 = *addr_A1;\r |
| 276 | \r |
| 277 | /* make sure JOB940_PICOSTATELOAD gets done before next JOB940_YM2612UPDATEONE */\r |
| 278 | add_job_940(JOB940_PICOSTATELOAD);\r |
| 279 | if (CHECK_BUSY(JOB940_PICOSTATELOAD)) wait_busy_940(JOB940_PICOSTATELOAD);\r |
| 280 | \r |
| 281 | writebuff_ptr = 0;\r |
| 282 | \r |
| 283 | // feed all the registers and update internal state\r |
| 284 | for(i = 0; i < 0x100; i++) {\r |
| 285 | YM2612Write_940(0, i);\r |
| 286 | YM2612Write_940(1, REGS[i]);\r |
| 287 | }\r |
| 288 | for(i = 0; i < 0x100; i++) {\r |
| 289 | YM2612Write_940(2, i);\r |
| 290 | YM2612Write_940(3, REGS[i|0x100]);\r |
| 291 | }\r |
| 292 | \r |
| 293 | *addr_A1 = old_A1;\r |
| 294 | }\r |
| 295 | \r |
| 296 | \r |
| 297 | static void internal_reset(void)\r |
| 298 | {\r |
| 299 | writebuff_ptr = 0;\r |
| 300 | ym2612_st->mode = 0;\r |
| 301 | ym2612_st->status = 0; /* normal mode */\r |
| 302 | ym2612_st->TA = 0;\r |
| 303 | ym2612_st->TAC = 0;\r |
| 304 | ym2612_st->TAT = 0;\r |
| 305 | ym2612_st->TB = 0;\r |
| 306 | ym2612_st->TBC = 0;\r |
| 307 | ym2612_st->TBT = 0;\r |
| 308 | dacen = 0;\r |
| 309 | dacout = 0;\r |
| 310 | ST_address= 0;\r |
| 311 | }\r |
| 312 | \r |
| 313 | \r |
| 314 | /* this must be called after mmu hack, the allocated regions must not get cached */\r |
| 315 | void sharedmem_init(void)\r |
| 316 | {\r |
| 317 | if (shared_mem != NULL) return;\r |
| 318 | \r |
| 319 | shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);\r |
| 320 | if(shared_mem == MAP_FAILED)\r |
| 321 | {\r |
| 322 | printf("mmap(shared_data) failed with %i\n", errno);\r |
| 323 | exit(1);\r |
| 324 | }\r |
| 325 | shared_data = (_940_data_t *) (shared_mem+0x100000);\r |
| 326 | /* this area must not get buffered on either side */\r |
| 327 | shared_ctl = (_940_ctl_t *) (shared_mem+0x200000);\r |
| 328 | mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2400000);\r |
| 329 | if (mp3_mem == MAP_FAILED)\r |
| 330 | {\r |
| 331 | printf("mmap(mp3_mem) failed with %i\n", errno);\r |
| 332 | exit(1);\r |
| 333 | }\r |
| 334 | crashed_940 = 1;\r |
| 335 | }\r |
| 336 | \r |
| 337 | \r |
| 338 | void sharedmem_deinit(void)\r |
| 339 | {\r |
| 340 | munmap(shared_mem, 0x210000);\r |
| 341 | munmap(mp3_mem, MP3_SIZE_MAX);\r |
| 342 | shared_mem = mp3_mem = NULL;\r |
| 343 | shared_data = NULL;\r |
| 344 | shared_ctl = NULL;\r |
| 345 | }\r |
| 346 | \r |
| 347 | \r |
| 348 | extern char **g_argv;\r |
| 349 | \r |
| 350 | /* none of the functions in this file should be called before this one */\r |
| 351 | void YM2612Init_940(int baseclock, int rate)\r |
| 352 | {\r |
| 353 | printf("YM2612Init_940()\n");\r |
| 354 | printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));\r |
| 355 | \r |
| 356 | Reset940(1, 2);\r |
| 357 | Pause940(1);\r |
| 358 | \r |
| 359 | gp2x_memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920\r |
| 360 | gp2x_memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940\r |
| 361 | \r |
| 362 | gp2x_memregl[0x4504>>2] = 0; // make sure no FIQs will be generated\r |
| 363 | gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller\r |
| 364 | \r |
| 365 | \r |
| 366 | if (crashed_940)\r |
| 367 | {\r |
| 368 | unsigned char ucData[1024];\r |
| 369 | int nRead, i, nLen = 0;\r |
| 370 | char binpath[1024];\r |
| 371 | FILE *fp;\r |
| 372 | \r |
| 373 | strncpy(binpath, g_argv[0], 1023);\r |
| 374 | binpath[1023] = 0;\r |
| 375 | for (i = strlen(binpath); i > 0; i--)\r |
| 376 | if (binpath[i] == '/') { binpath[i] = 0; break; }\r |
| 377 | strcat(binpath, "/" CODE940_FILE);\r |
| 378 | \r |
| 379 | fp = fopen(binpath, "rb");\r |
| 380 | if(!fp)\r |
| 381 | {\r |
| 382 | memset(gp2x_screen, 0, 320*240*2);\r |
| 383 | text_out16(10, 100, "failed to open required file:");\r |
| 384 | text_out16(10, 110, CODE940_FILE);\r |
| 385 | gp2x_video_flip2();\r |
| 386 | printf("failed to open %s\n", binpath);\r |
| 387 | exit(1);\r |
| 388 | }\r |
| 389 | \r |
| 390 | while(1)\r |
| 391 | {\r |
| 392 | nRead = fread(ucData, 1, 1024, fp);\r |
| 393 | if(nRead <= 0)\r |
| 394 | break;\r |
| 395 | memcpy(shared_mem + nLen, ucData, nRead);\r |
| 396 | nLen += nRead;\r |
| 397 | }\r |
| 398 | fclose(fp);\r |
| 399 | crashed_940 = 0;\r |
| 400 | }\r |
| 401 | \r |
| 402 | memset(shared_data, 0, sizeof(*shared_data));\r |
| 403 | memset(shared_ctl, 0, sizeof(*shared_ctl));\r |
| 404 | \r |
| 405 | /* cause local ym2612 to init REGS */\r |
| 406 | YM2612Init_(baseclock, rate);\r |
| 407 | \r |
| 408 | REGS = YM2612GetRegs();\r |
| 409 | addr_A1 = (INT32 *) (REGS + 0x200);\r |
| 410 | \r |
| 411 | ym2612_dacen = &dacen;\r |
| 412 | ym2612_dacout = &dacout;\r |
| 413 | \r |
| 414 | internal_reset();\r |
| 415 | \r |
| 416 | loaded_mp3 = 0;\r |
| 417 | \r |
| 418 | gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940\r |
| 419 | gp2x_memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND\r |
| 420 | gp2x_memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND\r |
| 421 | \r |
| 422 | /* start the 940 */\r |
| 423 | Reset940(0, 2);\r |
| 424 | Pause940(0);\r |
| 425 | \r |
| 426 | // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT\r |
| 427 | \r |
| 428 | /* now cause 940 to init it's ym2612 stuff */\r |
| 429 | shared_ctl->baseclock = baseclock;\r |
| 430 | shared_ctl->rate = rate;\r |
| 431 | add_job_940(JOB940_INITALL);\r |
| 432 | }\r |
| 433 | \r |
| 434 | \r |
| 435 | void YM2612ResetChip_940(void)\r |
| 436 | {\r |
| 437 | printf("YM2612ResetChip_940()\n");\r |
| 438 | if (shared_data == NULL) {\r |
| 439 | printf("YM2612ResetChip_940: reset before init?\n");\r |
| 440 | return;\r |
| 441 | }\r |
| 442 | \r |
| 443 | internal_reset();\r |
| 444 | \r |
| 445 | add_job_940(JOB940_YM2612RESETCHIP);\r |
| 446 | }\r |
| 447 | \r |
| 448 | \r |
| 449 | int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)\r |
| 450 | {\r |
| 451 | int *ym_buf = shared_data->ym_buffer;\r |
| 452 | int ym_active_chs;\r |
| 453 | \r |
| 454 | //printf("YM2612UpdateOne_940()\n");\r |
| 455 | \r |
| 456 | if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);\r |
| 457 | \r |
| 458 | ym_active_chs = shared_ctl->ym_active_chs;\r |
| 459 | \r |
| 460 | // mix in ym buffer. is_buf_empty means nobody mixed there anything yet and it may contain trash\r |
| 461 | if (is_buf_empty && ym_active_chs) memcpy32(buffer, ym_buf, length<<stereo);\r |
| 462 | else memset32(buffer, 0, length<<stereo);\r |
| 463 | \r |
| 464 | if (shared_ctl->writebuffsel == 1) {\r |
| 465 | shared_ctl->writebuff0[writebuff_ptr & 0xffff] = 0xffff;\r |
| 466 | } else {\r |
| 467 | shared_ctl->writebuff1[writebuff_ptr & 0xffff] = 0xffff;\r |
| 468 | }\r |
| 469 | writebuff_ptr = 0;\r |
| 470 | \r |
| 471 | /* predict sample counter for next frame */\r |
| 472 | if (PsndLen_exc_add) {\r |
| 473 | if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length = PsndLen + 1;\r |
| 474 | else length = PsndLen;\r |
| 475 | }\r |
| 476 | \r |
| 477 | /* give 940 ym job */\r |
| 478 | shared_ctl->writebuffsel ^= 1;\r |
| 479 | shared_ctl->length = length;\r |
| 480 | shared_ctl->stereo = stereo;\r |
| 481 | \r |
| 482 | add_job_940(JOB940_YM2612UPDATEONE);\r |
| 483 | \r |
| 484 | return ym_active_chs;\r |
| 485 | }\r |
| 486 | \r |
| 487 | \r |
| 488 | static int mp3_samples_ready = 0, mp3_buffer_offs = 0;\r |
| 489 | static int mp3_play_bufsel = 0, mp3_job_started = 0;\r |
| 490 | \r |
| 491 | void mp3_update(int *buffer, int length, int stereo)\r |
| 492 | {\r |
| 493 | int length_mp3;\r |
| 494 | int cdda_on;\r |
| 495 | \r |
| 496 | // playback was started, track not ended\r |
| 497 | cdda_on = loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;\r |
| 498 | \r |
| 499 | if (!cdda_on) return;\r |
| 500 | \r |
| 501 | if (!(PicoOpt&0x200)) {\r |
| 502 | mp3_update_local(buffer, length, stereo);\r |
| 503 | return;\r |
| 504 | }\r |
| 505 | \r |
| 506 | length_mp3 = length;\r |
| 507 | if (PsndRate == 22050) length_mp3 <<= 1; // mp3s are locked to 44100Hz stereo\r |
| 508 | else if (PsndRate == 11025) length_mp3 <<= 2; // so make length 44100ish\r |
| 509 | \r |
| 510 | /* do we have to wait? */\r |
| 511 | if (mp3_job_started && mp3_samples_ready < length_mp3) {\r |
| 512 | if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);\r |
| 513 | mp3_job_started = 0;\r |
| 514 | mp3_samples_ready += 1152;\r |
| 515 | }\r |
| 516 | \r |
| 517 | /* mix mp3 data, only stereo */\r |
| 518 | if (mp3_samples_ready >= length_mp3)\r |
| 519 | {\r |
| 520 | int shr = 0;\r |
| 521 | void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;\r |
| 522 | if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; shr = 1; }\r |
| 523 | else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; shr = 2; }\r |
| 524 | \r |
| 525 | if (1152 - mp3_buffer_offs >= length_mp3) {\r |
| 526 | mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1);\r |
| 527 | \r |
| 528 | mp3_buffer_offs += length_mp3;\r |
| 529 | } else {\r |
| 530 | // collect samples from both buffers..\r |
| 531 | int left = 1152 - mp3_buffer_offs;\r |
| 532 | if (mp3_play_bufsel == 0)\r |
| 533 | {\r |
| 534 | mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, length<<1);\r |
| 535 | mp3_buffer_offs = length_mp3 - left;\r |
| 536 | mp3_play_bufsel = 1;\r |
| 537 | } else {\r |
| 538 | mix_samples(buffer, shared_data->mp3_buffer[1] + mp3_buffer_offs*2, (left>>shr)<<1);\r |
| 539 | mp3_buffer_offs = length_mp3 - left;\r |
| 540 | mix_samples(buffer + ((left>>shr)<<1),\r |
| 541 | shared_data->mp3_buffer[0], (mp3_buffer_offs>>shr)<<1);\r |
| 542 | mp3_play_bufsel = 0;\r |
| 543 | }\r |
| 544 | }\r |
| 545 | mp3_samples_ready -= length_mp3;\r |
| 546 | }\r |
| 547 | \r |
| 548 | // ask to decode more if we already can\r |
| 549 | if (!mp3_job_started)\r |
| 550 | {\r |
| 551 | mp3_job_started = 1;\r |
| 552 | shared_ctl->mp3_buffsel ^= 1;\r |
| 553 | add_job_940(JOB940_MP3DECODE);\r |
| 554 | }\r |
| 555 | }\r |
| 556 | \r |
| 557 | \r |
| 558 | /***********************************************************/\r |
| 559 | \r |
| 560 | void mp3_start_play(FILE *f, int pos) // pos is 0-1023\r |
| 561 | {\r |
| 562 | int byte_offs = 0;\r |
| 563 | \r |
| 564 | if (!(PicoOpt&0x800)) { // cdda disabled?\r |
| 565 | return;\r |
| 566 | }\r |
| 567 | \r |
| 568 | if (loaded_mp3 != f)\r |
| 569 | {\r |
| 570 | if (PicoMessage != NULL)\r |
| 571 | {\r |
| 572 | fseek(f, 0, SEEK_END);\r |
| 573 | if (ftell(f) > 2*1024*1024)\r |
| 574 | PicoMessage("Loading MP3...");\r |
| 575 | }\r |
| 576 | fseek(f, 0, SEEK_SET);\r |
| 577 | fread(mp3_mem, 1, MP3_SIZE_MAX, f);\r |
| 578 | if (!feof(f)) printf("Warning: mp3 was too large, not all data loaded.\n");\r |
| 579 | shared_ctl->mp3_len = ftell(f);\r |
| 580 | loaded_mp3 = f;\r |
| 581 | \r |
| 582 | if (PicoOpt&0x200) {\r |
| 583 | // as we are going to change 940's cacheable area, we must invalidate it's cache..\r |
| 584 | if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);\r |
| 585 | add_job_940(JOB940_INVALIDATE_DCACHE);\r |
| 586 | }\r |
| 587 | reset_timing = 1;\r |
| 588 | }\r |
| 589 | \r |
| 590 | // seek..\r |
| 591 | if (pos) {\r |
| 592 | byte_offs = (shared_ctl->mp3_len << 6) >> 10;\r |
| 593 | byte_offs *= pos;\r |
| 594 | byte_offs >>= 6;\r |
| 595 | }\r |
| 596 | // printf("mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);\r |
| 597 | \r |
| 598 | shared_ctl->mp3_offs = byte_offs;\r |
| 599 | \r |
| 600 | // reset buffer pointers and stuff..\r |
| 601 | mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0;\r |
| 602 | mp3_job_started = 0;\r |
| 603 | shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode\r |
| 604 | \r |
| 605 | if (!(PicoOpt&0x200)) mp3_start_local();\r |
| 606 | }\r |
| 607 | \r |
| 608 | \r |
| 609 | int mp3_get_offset(void)\r |
| 610 | {\r |
| 611 | unsigned int offs1024 = 0;\r |
| 612 | int cdda_on;\r |
| 613 | \r |
| 614 | cdda_on = (PicoAHW & PAHW_MCD) && (PicoOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&\r |
| 615 | (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3;\r |
| 616 | \r |
| 617 | if (cdda_on) {\r |
| 618 | offs1024 = shared_ctl->mp3_offs << 7;\r |
| 619 | offs1024 /= shared_ctl->mp3_len >> 3;\r |
| 620 | }\r |
| 621 | printf("offs1024=%u (%i/%i)\n", offs1024, shared_ctl->mp3_offs, shared_ctl->mp3_len);\r |
| 622 | \r |
| 623 | return offs1024;\r |
| 624 | }\r |
| 625 | \r |
| 626 | \r |