#include "emu.h"\r
#include "menu.h"\r
#include "asmutils.h"\r
+#include "mp3.h"\r
#include "../../Pico/PicoInt.h"\r
-\r
-// tmp\r
#include "../../Pico/sound/mix.h"\r
\r
/* we will need some gp2x internals here */\r
\r
static unsigned char *shared_mem = 0;\r
static _940_data_t *shared_data = 0;\r
-static _940_ctl_t *shared_ctl = 0;\r
-static unsigned char *mp3_mem = 0;\r
+_940_ctl_t *shared_ctl = 0;\r
+unsigned char *mp3_mem = 0;\r
\r
#define MP3_SIZE_MAX (0x1000000 - 4*640*480)\r
\r
return 0;\r
}\r
\r
-static int g_busy = 10;\r
\r
-static void wait_busy_940(void)\r
+#define CHECK_BUSY(job) \\r
+ (gp2x_memregs[0x3b46>>1] & (1<<(job-1)))\r
+\r
+static void wait_busy_940(int job)\r
{\r
int i;\r
-#if 0\r
- printf("940 busy, entering wait loop.. (cnt: %i, wc: %i, ve: ", shared_ctl->loopc, shared_ctl->waitc);\r
- for (i = 0; i < 8; i++)\r
- printf("%i ", shared_ctl->vstarts[i]);\r
- printf(")\n");\r
\r
- for (i = 0; shared_ctl->busy; i++)\r
- {\r
- spend_cycles(1024); /* needs tuning */\r
- }\r
- printf("wait iterations: %i\n", i);\r
-#else\r
- for (i = 0; /*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1] && i < 0x10000; i++)\r
+ job--;\r
+ for (i = 0; (gp2x_memregs[0x3b46>>1] & (1<<job)) && i < 0x10000; i++)\r
spend_cycles(8*1024); // tested to be best for mp3 dec\r
- //printf("i = 0x%x\n", i);\r
if (i < 0x10000) return;\r
\r
/* 940 crashed */\r
for (i = 0; i < 8; i++)\r
printf("%i ", shared_ctl->vstarts[i]);\r
printf(")\n");\r
- printf("irq pending flags: DUALCPU %04x (see 15?), SRCPND %08lx (see 26), INTPND %08lx\n",\r
+ printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",\r
gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);\r
- printf("last irq PC: %08x, lastjob: 0x%03x, busy: %x, lastbusy: %x, g_busy: %x\n", shared_ctl->last_irq_pc,\r
- shared_ctl->lastjob, gp2x_memregs[0x3B3E>>1]/*shared_ctl->busy*/, shared_ctl->lastbusy, g_busy);\r
+ printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r
printf("trying to interrupt..\n");\r
gp2x_memregs[0x3B3E>>1] = 0xffff;\r
- for (i = 0; /*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1] && i < 0x10000; i++)\r
+ for (i = 0; gp2x_memregs[0x3b46>>1] && i < 0x10000; i++)\r
spend_cycles(8*1024);\r
printf("i = 0x%x\n", i);\r
- printf("irq pending flags: DUALCPU %04x (see 15?), SRCPND %08lx (see 26), INTPND %08lx\n",\r
+ printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n",\r
gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]);\r
- printf("last irq PC: %08x, lastjob: 0x%03x, busy: %x\n", shared_ctl->last_irq_pc, shared_ctl->lastjob, gp2x_memregs[0x3B3E>>1]/*shared_ctl->busy*/);\r
+ printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob);\r
\r
strcpy(menuErrorMsg, "940 crashed.");\r
engineState = PGS_Menu;\r
crashed_940 = 1;\r
-#endif\r
}\r
\r
\r
-static void add_job_940(int job0, int job1)\r
+static void add_job_940(int job)\r
{\r
-/* if (gp2x_memregs[0x3b46>>1] || shared_ctl->busy)\r
- {\r
- printf("!!add_job_940: irq pending flags: DUALCPU %04x (see 15?), SRCPND %08lx (see 26), INTPND %08lx, busy: %x\n",\r
- gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2], shared_ctl->busy);\r
+ if (job <= 0 || job > 16) {\r
+ printf("add_job_940: bad job: %i\n", job);\r
+ return;\r
}\r
-*/\r
- shared_ctl->jobs[0] = job0;\r
- shared_ctl->jobs[1] = job1;\r
- //shared_ctl->busy = ++g_busy; // 1;\r
- gp2x_memregs[0x3B3E>>1] = ++g_busy; // set busy flag\r
-// gp2x_memregs[0x3B3E>>1] = 0xffff; // cause interrupt\r
+\r
+ // generate interrupt for this job\r
+ job--;\r
+ gp2x_memregs[(0x3B20+job*2)>>1] = 1;\r
+\r
+// printf("added %i, pending %04x\n", job+1, gp2x_memregs[0x3b46>>1]);\r
}\r
\r
\r
{\r
int i, old_A1 = addr_A1;\r
\r
- if (/*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1]) wait_busy_940();\r
-\r
// feed all the registers and update internal state\r
for(i = 0; i < 0x100; i++) {\r
YM2612Write_940(0, i);\r
\r
addr_A1 = old_A1;\r
\r
- add_job_940(JOB940_PICOSTATELOAD, 0);\r
+ add_job_940(JOB940_PICOSTATELOAD);\r
}\r
\r
\r
gp2x_memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920\r
gp2x_memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940\r
\r
+ gp2x_memregl[0x4504>>2] = 0; // make sure no FIQs will be generated\r
+ gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller\r
+\r
+\r
+\r
if (shared_mem == NULL)\r
{\r
shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);\r
\r
loaded_mp3 = 0;\r
\r
- /* now cause 940 to init it's ym2612 stuff */\r
- shared_ctl->baseclock = baseclock;\r
- shared_ctl->rate = rate;\r
- shared_ctl->jobs[0] = JOB940_INITALL;\r
- shared_ctl->jobs[1] = 0;\r
- //shared_ctl->busy = 1;\r
- gp2x_memregs[0x3B3E>>1] = 1; // set busy flag\r
-\r
gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940\r
- gp2x_memregl[0x4500>>2] = 0; // clear pending IRQs in SRCPND\r
- gp2x_memregl[0x4510>>2] = 0; // clear pending IRQs in INTPND\r
- gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller\r
+ gp2x_memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND\r
+ gp2x_memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND\r
\r
/* start the 940 */\r
Reset940(0, 2);\r
Pause940(0);\r
\r
// YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT\r
+\r
+ /* now cause 940 to init it's ym2612 stuff */\r
+ shared_ctl->baseclock = baseclock;\r
+ shared_ctl->rate = rate;\r
+ add_job_940(JOB940_INITALL);\r
}\r
\r
\r
return;\r
}\r
\r
- if (/*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1]) wait_busy_940();\r
-\r
internal_reset();\r
\r
- add_job_940(JOB940_YM2612RESETCHIP, 0);\r
+ add_job_940(JOB940_YM2612RESETCHIP);\r
}\r
\r
\r
-//extern int pcm_buffer[2*44100/50];\r
-/*\r
-static void mix_samples(int *dest_buf, short *mp3_buf, int len, int stereo)\r
+int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)\r
{\r
-// int *pcm = pcm_buffer + offset * 2;\r
+ int *ym_buf = shared_data->ym_buffer;\r
\r
- if (stereo)\r
- {\r
- for (; len > 0; len--)\r
- {\r
- int lm, rm;\r
- lm = *mp3_buf++; rm = *mp3_buf++;\r
- *dest_buf++ += lm - lm/2; *dest_buf++ += rm - rm/2;\r
- }\r
+ //printf("YM2612UpdateOne_940()\n");\r
+\r
+ if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE);\r
+\r
+ // mix in ym buffer\r
+ if (is_buf_empty) memcpy32(buffer, ym_buf, length<<stereo);\r
+ // else TODO\r
+\r
+// for (len = length << stereo; len > 0; len--)\r
+// {\r
+// *dest_buf++ += *ym_buf++;\r
+// }\r
+\r
+ if (shared_ctl->writebuffsel == 1) {\r
+ shared_ctl->writebuff0[writebuff_ptr] = 0xffff;\r
} else {\r
- for (; len > 0; len--)\r
- {\r
- int l = *mp3_buf++;\r
- *dest_buf++ = l - l/2;\r
- }\r
+ shared_ctl->writebuff1[writebuff_ptr] = 0xffff;\r
}\r
+ writebuff_ptr = 0;\r
+\r
+ /* predict sample counter for next frame */\r
+ if (PsndLen_exc_add) {\r
+ if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length = PsndLen + 1;\r
+ else length = PsndLen;\r
+ }\r
+\r
+ /* give 940 ym job */\r
+ shared_ctl->writebuffsel ^= 1;\r
+ shared_ctl->length = length;\r
+ shared_ctl->stereo = stereo;\r
+\r
+ add_job_940(JOB940_YM2612UPDATEONE);\r
+\r
+ return 1;\r
}\r
-*/\r
\r
-// here we assume that length is different between games, but constant in one game\r
\r
static int mp3_samples_ready = 0, mp3_buffer_offs = 0;\r
-static int mp3_play_bufsel = 0;\r
+static int mp3_play_bufsel = 0, mp3_job_started = 0;\r
\r
-int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)\r
+void mp3_update(int *buffer, int length, int stereo)\r
{\r
- int length_mp3 = Pico.m.pal ? 44100/50 : 44100/60; // mp3s are locked to 44100Hz stereo\r
- int *ym_buf = shared_data->mix_buffer;\r
-// int *dest_buf = buffer;\r
- int cdda_on, mp3_job = 0;\r
-// int len;\r
+ int length_mp3;\r
+ int cdda_on;\r
\r
- // emulating CD, enabled in opts, not data track, CDC is reading, playback was started, track not ended\r
- cdda_on = (PicoMCD & 1) && (PicoOpt & 0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&\r
- (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3;\r
+ // not data track, CDC is reading, playback was started, track not ended\r
+ cdda_on = !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1) &&\r
+ loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;\r
\r
- //printf("YM2612UpdateOne_940()\n");\r
- if (/*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1]) wait_busy_940();\r
+ if (!cdda_on) return;\r
\r
- // track ended?\r
- cdda_on = cdda_on && shared_ctl->mp3_offs < shared_ctl->mp3_len;\r
+ if (!(PicoOpt&0x200)) {\r
+ mp3_update_local(buffer, length, stereo);\r
+ return;\r
+ }\r
\r
- // mix in ym buffer\r
- if (is_buf_empty) memcpy32(buffer, ym_buf, length<<stereo);\r
- // else TODO\r
+ length_mp3 = length;\r
+ if (PsndRate == 22050) length_mp3 <<= 1; // mp3s are locked to 44100Hz stereo\r
+ else if (PsndRate == 11025) length_mp3 <<= 2; // so make length 44100ish\r
\r
-// for (len = length << stereo; len > 0; len--)\r
-// {\r
-// *dest_buf++ += *ym_buf++;\r
-// }\r
+ /* do we have to wait? */\r
+ if (mp3_job_started && mp3_samples_ready < length_mp3) {\r
+ if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);\r
+ mp3_job_started = 0;\r
+ mp3_samples_ready += 1152;\r
+ }\r
\r
/* mix mp3 data, only stereo */\r
- if (cdda_on && mp3_samples_ready >= length_mp3)\r
+ if (mp3_samples_ready >= length_mp3)\r
{\r
int shr = 0;\r
void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;\r
else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; shr = 2; }\r
\r
if (1152 - mp3_buffer_offs >= length_mp3) {\r
- mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, (length_mp3>>shr)<<1);\r
+ mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1);\r
\r
mp3_buffer_offs += length_mp3;\r
} else {\r
- // collect from both buffers..\r
+ // collect samples from both buffers..\r
int left = 1152 - mp3_buffer_offs;\r
if (mp3_play_bufsel == 0)\r
{\r
- mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, (length_mp3>>shr)<<1);\r
+ mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, length<<1);\r
mp3_buffer_offs = length_mp3 - left;\r
mp3_play_bufsel = 1;\r
} else {\r
mp3_samples_ready -= length_mp3;\r
}\r
\r
- if (shared_ctl->writebuffsel == 1) {\r
- shared_ctl->writebuff0[writebuff_ptr] = 0xffff;\r
- } else {\r
- shared_ctl->writebuff1[writebuff_ptr] = 0xffff;\r
- }\r
- writebuff_ptr = 0;\r
-\r
- /* predict sample counter for next frame */\r
- if (PsndLen_exc_add) {\r
- if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length = PsndLen + 1;\r
- else length = PsndLen;\r
- }\r
-\r
- /* give 940 ym job */\r
- shared_ctl->writebuffsel ^= 1;\r
- shared_ctl->length = length;\r
- shared_ctl->stereo = stereo;\r
-\r
- // make sure we will have enough mp3 samples next frame\r
- if (cdda_on && mp3_samples_ready < length_mp3)\r
+ // ask to decode more if we already can\r
+ if (!mp3_job_started)\r
{\r
+ mp3_job_started = 1;\r
shared_ctl->mp3_buffsel ^= 1;\r
- mp3_job = JOB940_MP3DECODE;\r
- mp3_samples_ready += 1152;\r
+ add_job_940(JOB940_MP3DECODE);\r
}\r
-\r
- add_job_940(JOB940_YM2612UPDATEONE, mp3_job);\r
- //spend_cycles(512);\r
- //printf("SRCPND: %08lx, INTMODE: %08lx, INTMASK: %08lx, INTPEND: %08lx\n",\r
- // gp2x_memregl[0x4500>>2], gp2x_memregl[0x4504>>2], gp2x_memregl[0x4508>>2], gp2x_memregl[0x4510>>2]);\r
-\r
- return 1;\r
}\r
\r
\r
// else printf("done. mp3 too large, not all data loaded.\n");\r
shared_ctl->mp3_len = ftell(f);\r
loaded_mp3 = f;\r
+\r
+ if (PicoOpt&0x200) {\r
+ // as we are going to change 940's cacheable area, we must invalidate it's cache..\r
+ if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);\r
+ add_job_940(JOB940_INVALIDATE_DCACHE);\r
+ }\r
}\r
\r
// seek..\r
\r
shared_ctl->mp3_offs = byte_offs;\r
\r
- // reset buffer pointers..\r
+ // reset buffer pointers and stuff..\r
mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0;\r
+ mp3_job_started = 0;\r
shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode\r
+\r
+ if (!(PicoOpt&0x200)) mp3_start_local();\r
}\r
\r
\r
int cdda_on;\r
\r
cdda_on = (PicoMCD & 1) && (PicoOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&\r
- (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;\r
+ (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3;\r
\r
if (cdda_on) {\r
offs1024 = shared_ctl->mp3_offs << 7;\r
--- /dev/null
+#include <stdio.h>
+#include <string.h>
+
+#include "../../Pico/sound/mix.h"
+#include "code940/940shared.h"
+#include "helix/pub/mp3dec.h"
+
+static short mp3_out_buffer[2*1152];
+static HMP3Decoder mp3dec = 0;
+static int mp3_buffer_offs = 0;
+
+extern _940_ctl_t *shared_ctl;
+extern unsigned char *mp3_mem;
+extern int PsndRate;
+
+
+static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
+{
+ int ret, offs1, offs = 0;
+
+ while (1)
+ {
+ offs1 = MP3FindSyncWord(buff + offs, 2048 - offs);
+ if (offs1 < 0) return -2;
+ offs += offs1;
+ if (2048 - offs < 4) return -3;
+
+ // printf("trying header %08x\n", *(int *)(buff + offs));
+
+ ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
+ if (ret == 0 && fi->bitrate != 0) break;
+ offs++;
+ }
+
+ return ret;
+}
+
+int mp3_get_bitrate(FILE *f, int len)
+{
+ unsigned char buff[2048];
+ MP3FrameInfo fi;
+ int ret;
+
+ memset(buff, 0, 2048);
+
+ if (!mp3dec) mp3dec = MP3InitDecoder();
+
+ fseek(f, 0, SEEK_SET);
+ ret = fread(buff, 1, 2048, f);
+ fseek(f, 0, SEEK_SET);
+ if (ret <= 0) return -1;
+
+ ret = try_get_header(buff, &fi);
+ if (ret != 0 || fi.bitrate == 0) {
+ // try to read somewhere around the middle
+ fseek(f, len>>1, SEEK_SET);
+ fread(buff, 1, 2048, f);
+ fseek(f, 0, SEEK_SET);
+ ret = try_get_header(buff, &fi);
+ }
+ if (ret != 0) return ret;
+
+ // printf("bitrate: %i\n", fi.bitrate / 1000);
+
+ return fi.bitrate / 1000;
+}
+
+
+static void mp3_decode(void)
+{
+ // tried copying this to cached mem, no improvement noticed
+ int mp3_offs = shared_ctl->mp3_offs;
+ unsigned char *readPtr = mp3_mem + mp3_offs;
+ int bytesLeft = shared_ctl->mp3_len - mp3_offs;
+ int offset; // frame offset from readPtr
+ int err;
+
+ if (bytesLeft <= 0) return; // EOF, nothing to do
+
+ offset = MP3FindSyncWord(readPtr, bytesLeft);
+ if (offset < 0) {
+ shared_ctl->mp3_offs = shared_ctl->mp3_len;
+ return; // EOF
+ }
+ readPtr += offset;
+ bytesLeft -= offset;
+
+ err = MP3Decode(mp3dec, &readPtr, &bytesLeft, mp3_out_buffer, 0);
+ if (err) {
+ if (err == ERR_MP3_INDATA_UNDERFLOW) {
+ shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
+ return;
+ } else if (err <= -6 && err >= -12) {
+ // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
+ // just try to skip the offending frame..
+ readPtr++;
+ }
+ shared_ctl->mp3_errors++;
+ shared_ctl->mp3_lasterr = err;
+ }
+ shared_ctl->mp3_offs = readPtr - mp3_mem;
+}
+
+
+void mp3_update_local(int *buffer, int length, int stereo)
+{
+ int length_mp3, shr = 0;
+ void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
+
+ length_mp3 = length;
+ if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; length_mp3 <<= 1; shr = 1; }
+ else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; length_mp3 <<= 2; shr = 2; }
+
+ if (1152 - mp3_buffer_offs >= length_mp3) {
+ mix_samples(buffer, mp3_out_buffer + mp3_buffer_offs*2, length<<1);
+
+ mp3_buffer_offs += length_mp3;
+ } else {
+ int left = 1152 - mp3_buffer_offs;
+
+ mix_samples(buffer, mp3_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
+ mp3_decode();
+ mp3_buffer_offs = length_mp3 - left;
+ mix_samples(buffer + ((left>>shr)<<1), mp3_out_buffer, (mp3_buffer_offs>>shr)<<1);
+ }
+}
+
+
+void mp3_start_local(void)
+{
+ mp3_buffer_offs = 0;
+ mp3_decode();
+}
+