some audio problems fixed, missing files added
authornotaz <notasas@gmail.com>
Thu, 8 Feb 2007 20:12:45 +0000 (20:12 +0000)
committernotaz <notasas@gmail.com>
Thu, 8 Feb 2007 20:12:45 +0000 (20:12 +0000)
git-svn-id: file:///home/notaz/opt/svn/PicoDrive@31 be3aeb3a-fb24-0410-a615-afba39da0efa

Pico/cd/pcm.c [new file with mode: 0644]
Pico/cd/pcm.h [new file with mode: 0644]
Pico/sound/sound.c
platform/gp2x/940ctl_ym2612.c
platform/gp2x/code940/940.c
platform/gp2x/code940/940init.s
platform/gp2x/code940/940shared.h
platform/gp2x/mp3.c [new file with mode: 0644]
platform/gp2x/mp3.h [new file with mode: 0644]

diff --git a/Pico/cd/pcm.c b/Pico/cd/pcm.c
new file mode 100644 (file)
index 0000000..1de04d6
--- /dev/null
@@ -0,0 +1,130 @@
+#include "../PicoInt.h"
+#include "pcm.h"
+
+static unsigned int g_rate = 0; // 18.14 fixed point
+
+void pcm_write(unsigned int a, unsigned int d)
+{
+//printf("pcm_write(%i, %02x)\n", a, d);
+
+       if (a < 7)
+       {
+               Pico_mcd->pcm.ch[Pico_mcd->pcm.cur_ch].regs[a] = d;
+       }
+       else if (a == 7) // control register
+       {
+               if (d & 0x40)   Pico_mcd->pcm.cur_ch = d & 7;
+               else            Pico_mcd->pcm.bank = d & 0xf;
+               Pico_mcd->pcm.control = d;
+               // dprintf("pcm control=%02x", Pico_mcd->pcm.control);
+       }
+       else if (a == 8) // sound on/off
+       {
+               if (!(Pico_mcd->pcm.enabled & 0x01)) Pico_mcd->pcm.ch[0].addr =
+                       Pico_mcd->pcm.ch[0].regs[6] << (PCM_STEP_SHIFT + 8);
+               if (!(Pico_mcd->pcm.enabled & 0x02)) Pico_mcd->pcm.ch[1].addr =
+                       Pico_mcd->pcm.ch[1].regs[6] << (PCM_STEP_SHIFT + 8);
+               if (!(Pico_mcd->pcm.enabled & 0x04)) Pico_mcd->pcm.ch[2].addr =
+                       Pico_mcd->pcm.ch[2].regs[6] << (PCM_STEP_SHIFT + 8);
+               if (!(Pico_mcd->pcm.enabled & 0x08)) Pico_mcd->pcm.ch[3].addr =
+                       Pico_mcd->pcm.ch[3].regs[6] << (PCM_STEP_SHIFT + 8);
+               if (!(Pico_mcd->pcm.enabled & 0x10)) Pico_mcd->pcm.ch[4].addr =
+                       Pico_mcd->pcm.ch[4].regs[6] << (PCM_STEP_SHIFT + 8);
+               if (!(Pico_mcd->pcm.enabled & 0x20)) Pico_mcd->pcm.ch[5].addr =
+                       Pico_mcd->pcm.ch[5].regs[6] << (PCM_STEP_SHIFT + 8);
+               if (!(Pico_mcd->pcm.enabled & 0x40)) Pico_mcd->pcm.ch[6].addr =
+                       Pico_mcd->pcm.ch[6].regs[6] << (PCM_STEP_SHIFT + 8);
+               if (!(Pico_mcd->pcm.enabled & 0x80)) Pico_mcd->pcm.ch[7].addr =
+                       Pico_mcd->pcm.ch[7].regs[6] << (PCM_STEP_SHIFT + 8);
+//             printf("addr %x %x %x %x %x %x %x %x\n", Pico_mcd->pcm.ch[0].addr, Pico_mcd->pcm.ch[1].addr
+//             , Pico_mcd->pcm.ch[2].addr, Pico_mcd->pcm.ch[3].addr, Pico_mcd->pcm.ch[4].addr, Pico_mcd->pcm.ch[5].addr
+//             , Pico_mcd->pcm.ch[6].addr, Pico_mcd->pcm.ch[7].addr);
+
+               Pico_mcd->pcm.enabled = ~d;
+//printf("enabled=%02x\n", Pico_mcd->pcm.enabled);
+       }
+}
+
+
+void pcm_set_rate(int rate)
+{
+       double step = 31.8 * 1024.0 / (double) rate; // max <4 @ 8000Hz
+       step *= 256*256/4;
+       g_rate = (unsigned int) step;
+       printf("g_rate: %08x\n", g_rate);
+}
+
+
+// TODO: make use of the fact that max_length == 3
+
+void pcm_update(int *buffer, int length, int stereo)
+{
+       struct pcm_chan *ch;
+       unsigned int step, addr;
+       int mul_l, mul_r, smp;
+       int i, j, k;
+       int *out;
+
+
+       // PCM disabled or all channels off (to be checked by caller)
+       //if (!(Pico_mcd->pcm.control & 0x80) || !Pico_mcd->pcm.enabled) return;
+
+//printf("-- upd %i\n", length);
+
+       for (i = 0; i < 8; i++)
+       {
+               if (!(Pico_mcd->pcm.enabled & (1 << i))) continue; // channel disabled
+
+               out = buffer;
+               ch = &Pico_mcd->pcm.ch[i];
+
+               addr = ch->addr; // >> PCM_STEP_SHIFT;
+               mul_l = ((int)ch->regs[0] * (ch->regs[1] & 0xf)) >> (5+1); // (env * pan) >> 5
+               mul_r = ((int)ch->regs[0] * (ch->regs[1] >>  4)) >> (5+1);
+               step  = ((unsigned int)(*(unsigned short *)&ch->regs[2]) * g_rate) >> 14; // freq step
+//             printf("step=%i, cstep=%i, mul_l=%i, mul_r=%i, ch=%i, addr=%x, en=%02x\n",
+//                     *(unsigned short *)&ch->regs[2], step, mul_l, mul_r, i, addr, Pico_mcd->pcm.enabled);
+
+               if (!stereo && mul_l < mul_r) mul_l = mul_r;
+
+               for (j = 0; j < length; j++)
+               {
+//                     printf("addr=%08x\n", addr);
+                       smp = Pico_mcd->pcm_ram[addr >> PCM_STEP_SHIFT];
+
+                       // test for loop signal
+                       if (smp == 0xff)
+                       {
+                               addr = *(unsigned short *)&ch->regs[4]; // loop_addr
+                               smp = Pico_mcd->pcm_ram[addr];
+                               addr <<= PCM_STEP_SHIFT;
+                               if (smp == 0xff) break;
+                       }
+
+                       if (smp & 0x80) smp = -(smp & 0x7f);
+
+                       *out++ += smp * mul_l; // max 128 * 119 = 15232
+                       if(stereo)
+                               *out++ += smp * mul_r;
+
+                       // update address register
+                       k = (addr >> PCM_STEP_SHIFT) + 1;
+                       addr = (addr + step) & 0x7FFFFFF;
+
+                       for(; k < (addr >> PCM_STEP_SHIFT); k++)
+                       {
+                               if (Pico_mcd->pcm_ram[k] == 0xff)
+                               {
+                                       addr = (unsigned int)(*(unsigned short *)&ch->regs[4]) << PCM_STEP_SHIFT; // loop_addr
+                                       break;
+                               }
+                       }
+               }
+
+               if (Pico_mcd->pcm_ram[addr >> PCM_STEP_SHIFT] == 0xff)
+                       addr = (unsigned int)(*(unsigned short *)&ch->regs[4]) << PCM_STEP_SHIFT; // loop_addr
+
+               ch->addr = addr;
+       }
+}
+
diff --git a/Pico/cd/pcm.h b/Pico/cd/pcm.h
new file mode 100644 (file)
index 0000000..8756bb0
--- /dev/null
@@ -0,0 +1,7 @@
+
+#define PCM_STEP_SHIFT 11
+
+void pcm_write(unsigned int a, unsigned int d);
+void pcm_set_rate(int rate);
+void pcm_update(int *buffer, int length, int stereo);
+
index 18cc46e..a9f2f68 100644 (file)
@@ -255,8 +255,8 @@ int sound_render(int offset, int length)
     pcm_update(buf32, length, stereo);\r
 \r
   // CD: CDDA audio\r
-//  if ((PicoMCD & 1) && (PicoOpt & 0x800))\r
-//    mp3_update(PsndBuffer+offset, length, stereo);\r
+  if ((PicoMCD & 1) && (PicoOpt & 0x800))\r
+    mp3_update(buf32, length, stereo);\r
 \r
   // convert + limit to normal 16bit output\r
   if (stereo)\r
index 29fd8bf..1059b5d 100644 (file)
@@ -12,9 +12,8 @@
 #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
@@ -23,8 +22,8 @@ extern volatile unsigned long  *gp2x_memregl;
 \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
@@ -226,26 +225,17 @@ int YM2612PicoTick_940(int n)
        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
@@ -253,39 +243,36 @@ static void wait_busy_940(void)
        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
@@ -293,8 +280,6 @@ void YM2612PicoStateLoad_940(void)
 {\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
@@ -307,7 +292,7 @@ void YM2612PicoStateLoad_940(void)
 \r
        addr_A1 = old_A1;\r
 \r
-       add_job_940(JOB940_PICOSTATELOAD, 0);\r
+       add_job_940(JOB940_PICOSTATELOAD);\r
 }\r
 \r
 \r
@@ -338,6 +323,11 @@ void YM2612Init_940(int baseclock, int rate)
        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
@@ -406,24 +396,20 @@ void YM2612Init_940(int baseclock, int rate)
 \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
@@ -435,72 +421,85 @@ void YM2612ResetChip_940(void)
                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
@@ -508,15 +507,15 @@ int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)
                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
@@ -530,38 +529,13 @@ int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty)
                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
@@ -584,6 +558,12 @@ void mp3_start_play(FILE *f, int pos) // pos is 0-1023
                // 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
@@ -596,9 +576,12 @@ void mp3_start_play(FILE *f, int pos) // pos is 0-1023
 \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
@@ -608,7 +591,7 @@ int mp3_get_offset(void)
        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
index 18d282c..b242603 100644 (file)
@@ -6,10 +6,11 @@ static unsigned char *mp3_data  = (unsigned char *) 0x01000000;
 YM2612 *ym2612_940;\r
 \r
 // from init.s\r
-void wait_irq(void);\r
+int  wait_get_job(int oldjob);\r
 void spend_cycles(int c);\r
-void cache_clean(void);\r
-void cache_clean_flush(void);\r
+void dcache_clean(void);\r
+void dcache_clean_flush(void);\r
+void drain_wb(void);\r
 // this should help to resolve race confition where shared var\r
 // is changed by other core just before we update it\r
 void set_if_not_changed(int *val, int oldval, int newval);\r
@@ -41,7 +42,6 @@ static void mp3_decode(void)
                        shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);\r
        if (err) {\r
                if (err == ERR_MP3_INDATA_UNDERFLOW) {\r
-                       shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF\r
                        set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len);\r
                        return;\r
                } else if (err <= -6 && err >= -12) {\r
@@ -56,97 +56,73 @@ static void mp3_decode(void)
 }\r
 \r
 \r
-void Main940(int startvector, int pc_at_irq)\r
+void Main940(void)\r
 {\r
-       int mix_buffer = shared_data->mix_buffer;\r
+       int *ym_buffer = shared_data->ym_buffer;\r
+       int job = 0;\r
        ym2612_940 = &shared_data->ym2612;\r
 \r
-       // debug\r
-       shared_ctl->vstarts[startvector]++;\r
-       shared_ctl->last_irq_pc = pc_at_irq;\r
-       // asm volatile ("mcr p15, 0, r0, c7, c10, 4" ::: "r0");\r
 \r
-\r
-//     for (;;)\r
+       for (;;)\r
        {\r
-               int job_num = 0;\r
-/*\r
-               while (!shared_ctl->busy)\r
-               {\r
-                       //shared_ctl->waitc++;\r
-                       spend_cycles(8*1024);\r
-               }\r
-*/\r
-/*\r
-               if (!shared_ctl->busy)\r
-               {\r
-                       wait_irq();\r
-               }\r
-               shared_ctl->lastbusy = shared_ctl->busy;\r
-*/\r
+               job = wait_get_job(job);\r
+\r
+               shared_ctl->lastjob = job;\r
 \r
-               for (job_num = 0; job_num < MAX_940JOBS; job_num++)\r
+               switch (job)\r
                {\r
-                       shared_ctl->lastjob = (job_num << 8) | shared_ctl->jobs[job_num];\r
-\r
-                       switch (shared_ctl->jobs[job_num])\r
-                       {\r
-                               case JOB940_INITALL:\r
-                                       /* ym2612 */\r
-                                       shared_ctl->writebuff0[0] = shared_ctl->writebuff1[0] = 0xffff;\r
-                                       YM2612Init_(shared_ctl->baseclock, shared_ctl->rate);\r
-                                       /* Helix mp3 decoder */\r
-                                       shared_data->mp3dec = MP3InitDecoder();\r
-                                       break;\r
-\r
-                               case JOB940_YM2612RESETCHIP:\r
-                                       YM2612ResetChip_();\r
-                                       break;\r
-\r
-                               case JOB940_PICOSTATELOAD:\r
-                                       YM2612PicoStateLoad_();\r
-                                       break;\r
-\r
-                               case JOB940_YM2612UPDATEONE: {\r
-                                       int i, dw, *wbuff;\r
-                                       if (shared_ctl->writebuffsel == 1) {\r
-                                               wbuff = (int *) shared_ctl->writebuff1;\r
-                                       } else {\r
-                                               wbuff = (int *) shared_ctl->writebuff0;\r
-                                       }\r
-\r
-                                       /* playback all writes */\r
-                                       for (i = 2048/2; i > 0; i--) {\r
-                                               UINT16 d;\r
-                                               dw = *wbuff++;\r
-                                               d = dw;\r
-                                               if (d == 0xffff) break;\r
-                                               YM2612Write_(d >> 8, d);\r
-                                               d = (dw>>16);\r
-                                               if (d == 0xffff) break;\r
-                                               YM2612Write_(d >> 8, d);\r
-                                       }\r
-\r
-                                       YM2612UpdateOne_(mix_buffer, shared_ctl->length, shared_ctl->stereo, 1);\r
-                                       break;\r
+                       case JOB940_INITALL:\r
+                               /* ym2612 */\r
+                               shared_ctl->writebuff0[0] = shared_ctl->writebuff1[0] = 0xffff;\r
+                               YM2612Init_(shared_ctl->baseclock, shared_ctl->rate);\r
+                               /* Helix mp3 decoder */\r
+                               shared_data->mp3dec = MP3InitDecoder();\r
+                               break;\r
+\r
+                       case JOB940_INVALIDATE_DCACHE:\r
+                               drain_wb();\r
+                               dcache_clean_flush();\r
+                               break;\r
+\r
+                       case JOB940_YM2612RESETCHIP:\r
+                               YM2612ResetChip_();\r
+                               break;\r
+\r
+                       case JOB940_PICOSTATELOAD:\r
+                               YM2612PicoStateLoad_();\r
+                               break;\r
+\r
+                       case JOB940_YM2612UPDATEONE: {\r
+                               int i, dw, *wbuff;\r
+                               if (shared_ctl->writebuffsel == 1) {\r
+                                       wbuff = (int *) shared_ctl->writebuff1;\r
+                               } else {\r
+                                       wbuff = (int *) shared_ctl->writebuff0;\r
                                }\r
 \r
-                               case JOB940_MP3DECODE:\r
-                                       mp3_decode();\r
-                                       break;\r
+                               /* playback all writes */\r
+                               for (i = 2048/2; i > 0; i--) {\r
+                                       UINT16 d;\r
+                                       dw = *wbuff++;\r
+                                       d = dw;\r
+                                       if (d == 0xffff) break;\r
+                                       YM2612Write_(d >> 8, d);\r
+                                       d = (dw>>16);\r
+                                       if (d == 0xffff) break;\r
+                                       YM2612Write_(d >> 8, d);\r
+                               }\r
+\r
+                               YM2612UpdateOne_(ym_buffer, shared_ctl->length, shared_ctl->stereo, 1);\r
+                               break;\r
                        }\r
-               }\r
 \r
-               cache_clean();\r
-//             asm volatile ("mov r0, #0" ::: "r0");\r
-//             asm volatile ("mcr p15, 0, r0, c7, c10, 4" ::: "r0"); /* drain write buffer, should be done on nonbuffered write */\r
-//             cache_clean_flush();\r
+                       case JOB940_MP3DECODE:\r
+                               mp3_decode();\r
+                               break;\r
+               }\r
 \r
                shared_ctl->loopc++;\r
-\r
-//             // shared_ctl->busy = 0; // shared mem is not reliable?\r
-\r
-               wait_irq();\r
+               dcache_clean();\r
        }\r
 }\r
 \r
index 750e0ae..2a17a9c 100644 (file)
@@ -1,8 +1,13 @@
-.global code940\r
+@ vim:filetype=armasm:\r
+\r
 \r
 .equ mmsp2_regs, (0xc0000000-0x02000000) @ assume we live @ 0x2000000 bank\r
+.equ shared_ctl,  0x00200000             @ this is where shared_ctl struncture is located\r
+\r
 \r
-code940:                          @ interrupt table:\r
+@ exception table:\r
+.global code940\r
+code940:\r
     b .b_reset                    @ reset\r
     b .b_undef                    @ undefined instructions\r
     b .b_swi                      @ software interrupt\r
@@ -32,19 +37,23 @@ code940:                          @ interrupt table:
     mov     r12, #5\r
     b       .Begin\r
 .b_irq:\r
-    mov     r12, #6\r
     mov     sp, #0x100000       @ reset stack\r
     sub     sp, sp, #4\r
-    mov     r1, #mmsp2_regs\r
-    orr     r2, r1, #0x3B00\r
-    orr     r2, r2, #0x0046\r
-    mvn     r3, #0\r
-    strh    r3, [r2]            @ clear any pending interrupts from the DUALCPU unit\r
-    orr     r2, r1, #0x4500\r
-    str     r3, [r2]            @ clear all pending interrupts in irq controller's SRCPND register\r
-    orr     r2, r2, #0x0010\r
-    str     r3, [r2]            @ clear all pending interrupts in irq controller's INTPND register\r
-    b       .Enter\r
+    mov     r0, #shared_ctl     @ remember where we were when interrupt happened\r
+    add     r0, r0, #0x20\r
+    str     lr, [r0]\r
+    mov     r0, #shared_ctl     @ increment exception counter (for debug)\r
+    add     r0, r0, #(6*4)\r
+    ldr     r1, [r0]\r
+    add     r1, r1, #1\r
+    str     r1, [r0]\r
+\r
+    bl Main940\r
+\r
+    @ we should never get here\r
+    b .b_reserved\r
+\r
+\r
 .b_fiq:\r
     mov     r12, #7\r
     b       .Begin\r
@@ -81,8 +90,13 @@ code940:                          @ interrupt table:
     mcr p15, 0, r0, c6, c4, 0\r
     mcr p15, 0, r0, c6, c4, 1\r
 \r
-    @ set regions 1 and 4 to be cacheable (so the first 2M and mp3 area will be cacheable)\r
-    mov r0, #(1<<1)|(1<<4)\r
+    @ region 5: 4K 0x00000000-0x00001000 (boot code protection region)\r
+    mov r0, #(0x0b<<1)|1\r
+    mcr p15, 0, r0, c6, c5, 0\r
+    mcr p15, 0, r0, c6, c5, 1\r
+\r
+    @ set regions 1, 4 and 5 to be cacheable (so the first 2M and mp3 area will be cacheable)\r
+    mov r0, #(1<<1)|(1<<4)|(1<<5)\r
     mcr p15, 0, r0, c2, c0, 0\r
     mcr p15, 0, r0, c2, c0, 1\r
 \r
@@ -90,10 +104,13 @@ code940:                          @ interrupt table:
     mov r0, #(1<<1)\r
     mcr p15, 0, r0, c3, c0, 0\r
 \r
-    @ set protection, allow access only to regions 1 and 2\r
-    mov r0, #(3<<8)|(3<<6)|(3<<4)|(3<<2)|(0)  @ data: [full, full, full, full, no access] for regions [4 3 2 1 0]\r
+    @ set access protection\r
+    @ data: [no, full, full, full, full, no access] for regions [5 4 3 2 1 0]\r
+    mov r0, #(0<<10)|(3<<8)|(3<<6)|(3<<4)|(3<<2)|(0)\r
     mcr p15, 0, r0, c5, c0, 0\r
-    mov r0, #(0<<8)|(0<<6)|(0<<4)|(3<<2)|(0)  @ instructions: [no access, no, no, full, no]\r
+    @ instructions: [full, no access, no, no, full, no]\r
+    mov r0,         #(0<< 6)|(0<<4)|(3<<2)|(0)\r
+    orr r0, r0,     #(3<<10)|(0<<8)\r
     mcr p15, 0, r0, c5, c0, 1\r
 \r
     mrc p15, 0, r0, c1, c0, 0   @ fetch current control reg\r
@@ -109,19 +126,74 @@ code940:                          @ interrupt table:
     mov r0, #0\r
     mcr p15, 0, r0, c7, c6, 0\r
 \r
-.Enter:\r
-    mov r0, r12\r
-    mov r1, lr\r
-    bl Main940\r
+    @ remember which exception vector we came from (increment counter for debug)\r
+    mov     r0, #shared_ctl\r
+    add     r0, r0, r12, lsl #2\r
+    ldr     r1, [r0]\r
+    add     r1, r1, #1\r
+    str     r1, [r0]\r
+    \r
+    @ remember last lr (for debug)\r
+    mov     r0, #shared_ctl\r
+    add     r0, r0, #0x20\r
+    str     lr, [r0]\r
+\r
+    @ ready to take first job-interrupt\r
+wait_for_irq:\r
+    mrs     r0, cpsr\r
+    bic     r0, r0, #0x80\r
+    msr     cpsr_c, r0              @ enable interrupts\r
+\r
+    mov     r0, #0\r
+    mcr     p15, 0, r0, c7, c0, 4   @ wait for IRQ\r
+@    mcr     p15, 0, r0, c15, c8, 2\r
+    nop\r
+    nop\r
+    b       .b_reserved\r
+\r
 \r
-    @ we should never get here\r
-@.b_deadloop:\r
-@    b .b_deadloop\r
-    b .b_reserved\r
 \r
+@ next job getter\r
+.global wait_get_job @ int oldjob\r
 \r
+wait_get_job:\r
+    mov     r3, #mmsp2_regs\r
+    orr     r2, r3, #0x3B00\r
+    orr     r2, r2, #0x0046         @ DUALPEND940 register\r
+    ldrh    r12,[r2]\r
 \r
-@ so asm utils are also defined here:\r
+    tst     r0, r0\r
+    beq     wgj_no_old\r
+    sub     r0, r0, #1\r
+    mov     r1, #1\r
+    mov     r1, r1, lsl r0\r
+    strh    r1, [r2]                @ clear finished job's pending bit\r
+    bic     r12,r12,r1\r
+\r
+wgj_no_old:\r
+    tst     r12,r12\r
+    beq     wgj_no_jobs\r
+    mov     r0, #0\r
+wgj_loop:\r
+    add     r0, r0, #1\r
+    movs    r12,r12,lsr #1\r
+    bxcs    lr\r
+    b       wgj_loop\r
+\r
+wgj_no_jobs:\r
+    mvn     r0, #0\r
+    orr     r2, r3, #0x4500\r
+    str     r0, [r2]            @ clear all pending interrupts in irq controller's SRCPND register\r
+    orr     r2, r2, #0x0010\r
+    str     r0, [r2]            @ clear all pending interrupts in irq controller's INTPND register\r
+    b       wait_for_irq\r
+\r
+.pool\r
+\r
+\r
+\r
+\r
+@ some asm utils are also defined here:\r
 .global spend_cycles @ c\r
 \r
 spend_cycles:\r
@@ -135,16 +207,16 @@ spend_cycles:
 \r
 \r
 @ clean-flush function from ARM940T technical reference manual\r
-.global cache_clean_flush\r
+.global dcache_clean_flush\r
 \r
-cache_clean_flush:\r
+dcache_clean_flush:\r
     mov     r1, #0                  @ init line counter\r
 ccf_outer_loop:\r
     mov     r0, #0                  @ segment counter\r
 ccf_inner_loop:\r
     orr     r2, r1, r0              @ make segment and line address\r
     mcr     p15, 0, r2, c7, c14, 2  @ clean and flush that line\r
-    add     r0, r0, #0x10           @ incremet secment counter\r
+    add     r0, r0, #0x10           @ incremet segment counter\r
     cmp     r0, #0x40               @ complete all 4 segments?\r
     bne     ccf_inner_loop\r
     add     r1, r1, #0x04000000     @ increment line counter\r
@@ -153,17 +225,18 @@ ccf_inner_loop:
     bx      lr\r
 \r
 \r
+\r
 @ clean-only version\r
-.global cache_clean\r
+.global dcache_clean\r
 \r
-cache_clean:\r
+dcache_clean:\r
     mov     r1, #0                  @ init line counter\r
 cf_outer_loop:\r
     mov     r0, #0                  @ segment counter\r
 cf_inner_loop:\r
     orr     r2, r1, r0              @ make segment and line address\r
     mcr     p15, 0, r2, c7, c10, 2  @ clean that line\r
-    add     r0, r0, #0x10           @ incremet secment counter\r
+    add     r0, r0, #0x10           @ incremet segment counter\r
     cmp     r0, #0x40               @ complete all 4 segments?\r
     bne     cf_inner_loop\r
     add     r1, r1, #0x04000000     @ increment line counter\r
@@ -172,31 +245,13 @@ cf_inner_loop:
     bx      lr\r
 \r
 \r
-.global wait_irq\r
-\r
-wait_irq:\r
-    mov     r0, #mmsp2_regs\r
-    orr     r0, r0, #0x3B00\r
-    orr     r1, r0, #0x0042\r
-    mov     r3, #0\r
-    strh    r3, [r1]                @ disable interrupts\r
-    orr     r2, r0, #0x003E\r
-    strh    r3, [r2]                @ remove busy flag\r
-    mov     r3, #1\r
-    strh    r3, [r1]                @ enable interrupts\r
-\r
-    mrs     r0, cpsr\r
-    bic     r0, r0, #0x80\r
-    msr     cpsr_c, r0              @ enable interrupts\r
+@ drain write buffer\r
+.global drain_wb\r
 \r
+drain_wb:\r
     mov     r0, #0\r
-    mcr     p15, 0, r0, c7, c0, 4   @ wait for IRQ\r
-@    mcr     p15, 0, r0, c15, c8, 2\r
-    nop\r
-    nop\r
-    b       .b_reserved\r
-\r
-.pool\r
+    mcr     p15, 0, r0, c7, c10, 4\r
+    bx      lr\r
 \r
 \r
 .global set_if_not_changed @ int *val, int oldval, int newval\r
@@ -208,4 +263,10 @@ set_if_not_changed:
     strne  r3, [r0] @ restore value which was changed there by other core\r
     bx     lr\r
 \r
-@ vim:filetype=armasm:\r
+\r
+\r
+@ pad the protected region.\r
+.rept 1024\r
+.long 0\r
+.endr\r
+\r
index c208e67..8953196 100644 (file)
@@ -1,30 +1,33 @@
 #include "../../../Pico/sound/ym2612.h"\r
 #include "../helix/pub/mp3dec.h"\r
 \r
+// max 16 jobs\r
 enum _940_job_t {\r
        JOB940_INITALL = 1,\r
+       JOB940_INVALIDATE_DCACHE,\r
        JOB940_YM2612RESETCHIP,\r
        JOB940_YM2612UPDATEONE,\r
-       JOB940_PICOSTATELOAD,\r
        JOB940_MP3DECODE,\r
-       JOB940_NUMJOBS\r
+       JOB940_PICOSTATELOAD,\r
 };\r
 \r
-#define MAX_940JOBS    2\r
+//#define MAX_940JOBS  2\r
 \r
 typedef struct\r
 {\r
-       YM2612  ym2612;                                 /* current state of the emulated YM2612 */\r
+       YM2612  ym2612;                         /* current state of the emulated YM2612 */\r
        HMP3Decoder mp3dec;                     /* mp3 decoder's handle */\r
-       int     mix_buffer[44100/50*2];         /* this is where the YM2612 samples will be mixed to */\r
+       int     ym_buffer[44100/50*2];          /* this is where the YM2612 samples will be mixed to */\r
        short   mp3_buffer[2][1152*2];          /* buffers for mp3 decoder's output */\r
 } _940_data_t;\r
 \r
 \r
 typedef struct\r
 {\r
-       int             jobs[MAX_940JOBS];                      /* jobs for second core */\r
-       int             busy_;                                  /* unused */\r
+       int             vstarts[8];                             /* debug: 00: number of starts from each of 8 vectors */\r
+       int             last_lr;                                /* debug: 20: last exception's lr */\r
+//     int             jobs[MAX_940JOBS];                      /* jobs for second core */\r
+//     int             busy_;                                  /* unused */\r
        int             length;                                 /* number of samples to mix (882 max) */\r
        int             stereo;                                 /* mix samples as stereo, doubles sample count automatically */\r
        int             baseclock;                              /* ym2612 settings */\r
@@ -35,11 +38,8 @@ typedef struct
        int             mp3_len;                        /* data len of loaded mp3 */\r
        int             mp3_offs;                       /* current playback offset (just after last decoded frame) */\r
        int             mp3_buffsel;                    /* which output buffer to decode to */\r
-       int             vstarts[8];                             /* debug: number of starts from each of 8 vectors */\r
-       int             loopc;                                  /* debug: main loop counter */\r
+       int             loopc;                          /* debug: main loop counter */\r
        int             mp3_errors;                     /* debug: mp3 decoder's error counter */\r
        int             mp3_lasterr;                    /* debug: mp3 decoder's last error */\r
-       int             last_irq_pc;                    /* debug: PC value when IRQ happened */\r
        int             lastjob;                        /* debug: last job id */\r
-       int             lastbusy;                       /* debug: */\r
 } _940_ctl_t;\r
diff --git a/platform/gp2x/mp3.c b/platform/gp2x/mp3.c
new file mode 100644 (file)
index 0000000..e37b774
--- /dev/null
@@ -0,0 +1,134 @@
+#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();
+}
+
diff --git a/platform/gp2x/mp3.h b/platform/gp2x/mp3.h
new file mode 100644 (file)
index 0000000..022d89c
--- /dev/null
@@ -0,0 +1,4 @@
+
+void mp3_update_local(int *buffer, int length, int stereo);
+void mp3_start_local(void);
+