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