sound code rewrite for lower sample rates (except mp3s)
[picodrive.git] / Pico / Pico.c
index b430d66..3cee0c2 100644 (file)
 #include "sound/sound.h"\r
 #include "sound/ym2612.h"\r
 \r
-int PicoVer=0x0080;\r
+int PicoVer=0x0110;\r
 struct Pico Pico;\r
 int PicoOpt=0; // disable everything by default\r
 int PicoSkipFrame=0; // skip rendering frame?\r
 int PicoRegionOverride = 0; // override the region detection 0: Auto, 1: Japan NTSC, 2: Japan PAL, 4: US, 8: Europe\r
+int PicoAutoRgnOrder = 0;\r
 int emustatus = 0;\r
-void (*PicoWriteSound)(void) = 0; // called once per frame at the best time to send sound buffer (PsndOut) to hardware\r
+void (*PicoWriteSound)(int len) = 0; // called once per frame at the best time to send sound buffer (PsndOut) to hardware\r
 \r
 struct PicoSRAM SRam;\r
 int z80startCycle = 0, z80stopCycle = 0; // in 68k cycles\r
 //int z80ExtraCycles = 0;\r
-int PicoPad[2]; // Joypads, format is SACB RLDU\r
-int PicoMCD = 0; // mega CD status\r
+int PicoPad[2];  // Joypads, format is SACB RLDU\r
+int PicoMCD = 0; // mega CD status: scd_started, reset_pending\r
 \r
 // to be called once on emu init\r
 int PicoInit(void)\r
@@ -36,9 +37,6 @@ int PicoInit(void)
   SekInit();\r
   z80_init(); // init even if we aren't going to use it\r
 \r
-  // Setup memory callbacks:\r
-  PicoMemInit();\r
-\r
   PicoInitMCD();\r
 \r
   // notaz: sram\r
@@ -51,7 +49,8 @@ int PicoInit(void)
 // to be called once on emu exit\r
 void PicoExit(void)\r
 {\r
-  PicoExitMCD();\r
+  if (PicoMCD&1)\r
+    PicoExitMCD();\r
   z80_exit();\r
 \r
   // notaz: sram\r
@@ -66,6 +65,10 @@ int PicoReset(int hard)
 \r
   if (Pico.romsize<=0) return 1;\r
 \r
+  // setup correct memory map\r
+  if (PicoMCD & 1)\r
+       PicoMemSetupCD();\r
+  else PicoMemSetup();\r
   PicoMemReset();\r
   SekReset();\r
   SekCycleCntT=0;\r
@@ -105,9 +108,12 @@ int PicoReset(int hard)
       c=region>>(i<<3); c&=0xff;\r
       if (c<=' ') continue;\r
 \r
-           if (c=='J') support|=1;\r
-      else if (c=='U') support|=4;\r
-      else if (c=='E') support|=8;\r
+           if (c=='J')  support|=1;\r
+      else if (c=='U')  support|=4;\r
+      else if (c=='E')  support|=8;\r
+      else if (c=='j') {support|=1; break; }\r
+      else if (c=='u') {support|=4; break; }\r
+      else if (c=='e') {support|=8; break; }\r
       else\r
       {\r
         // New style code:\r
@@ -118,6 +124,13 @@ int PicoReset(int hard)
     }\r
   }\r
 \r
+  // auto detection order override\r
+  if (PicoAutoRgnOrder) {\r
+         if (((PicoAutoRgnOrder>>0)&0xf) & support) support = (PicoAutoRgnOrder>>0)&0xf;\r
+    else if (((PicoAutoRgnOrder>>4)&0xf) & support) support = (PicoAutoRgnOrder>>4)&0xf;\r
+    else if (((PicoAutoRgnOrder>>8)&0xf) & support) support = (PicoAutoRgnOrder>>8)&0xf;\r
+  }\r
+\r
   // Try to pick the best hardware value for English/50hz:\r
        if (support&8) { hw=0xc0; pal=1; } // Europe\r
   else if (support&4)   hw=0x80;          // USA\r
@@ -237,31 +250,6 @@ static __inline void SekRun(int cyc)
 {\r
   int cyc_do;\r
   SekCycleAim+=cyc;\r
-#if 0\r
-  if(Pico.m.dma_bytes) {\r
-    int burn=0;\r
-    if((Pico.video.status&8)||!(Pico.video.reg[1]&0x40)) { // vblank?\r
-      if(Pico.m.dma_bytes < 205) {\r
-       burn = Pico.m.dma_bytes*(((488<<8)/205))>>8;\r
-       Pico.m.dma_bytes = 0;\r
-      } else {\r
-        burn += 488;\r
-       Pico.m.dma_bytes -= 205;\r
-      }\r
-    } else {\r
-      if(Pico.m.dma_bytes < 18) {\r
-       burn = Pico.m.dma_bytes*(((488<<8)/18))>>8;\r
-       Pico.m.dma_bytes = 0;\r
-      } else {\r
-        burn += 488;\r
-       Pico.m.dma_bytes -= 18;\r
-      }\r
-    }\r
-    SekCycleCnt+=burn;\r
-    dprintf("~DmaSlow %i burn=%i do=%i [%i|%i]", Pico.m.dma_bytes, burn, SekCycleAim-SekCycleCnt,\r
-               Pico.m.scanline, SekCyclesDone());\r
-  }\r
-#endif\r
   //dprintf("aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);\r
   if((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;\r
   //dprintf("cyc_do: %i", cyc_do);\r
@@ -327,20 +315,22 @@ static int CheckIdle(void)
 // to be called on 224 or line_sample scanlines only\r
 static __inline void getSamples(int y)\r
 {\r
+  static int curr_pos = 0;\r
+\r
   if(y == 224) {\r
     //dprintf("sta%i: %i [%i]", (emustatus & 2), emustatus, y);\r
     if(emustatus & 2)\r
-        sound_render(PsndLen/2, PsndLen-PsndLen/2);\r
-    else sound_render(0, PsndLen);\r
+         curr_pos += sound_render(curr_pos, PsndLen-PsndLen/2);\r
+    else curr_pos  = sound_render(0, PsndLen);\r
     if (emustatus&1) emustatus|=2; else emustatus&=~2;\r
-    if (PicoWriteSound) PicoWriteSound();\r
+    if (PicoWriteSound) PicoWriteSound(curr_pos);\r
     // clear sound buffer\r
-    memset(PsndOut, 0, (PicoOpt & 8) ? (PsndLen<<2) : (PsndLen<<1));\r
+    sound_clear();\r
   }\r
   else if(emustatus & 3) {\r
     emustatus|= 2;\r
     emustatus&=~1;\r
-    sound_render(0, PsndLen/2);\r
+    curr_pos = sound_render(0, PsndLen/2);\r
   }\r
 }\r
 \r
@@ -549,10 +539,10 @@ static int PicoFrameSimple(void)
 \r
   // here we render sound if ym2612 is disabled\r
   if(!(PicoOpt&1) && PsndOut) {\r
-    sound_render(0, PsndLen);\r
-    if(PicoWriteSound) PicoWriteSound();\r
+    int len = sound_render(0, PsndLen);\r
+    if(PicoWriteSound) PicoWriteSound(len);\r
     // clear sound buffer\r
-    memset(PsndOut, 0, (PicoOpt & 8) ? (PsndLen<<2) : (PsndLen<<1));\r
+    sound_clear();\r
   }\r
 \r
   // render screen\r