sound code rewrite for lower sample rates (except mp3s)
[picodrive.git] / Pico / Pico.c
index 750ffc5..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
@@ -187,11 +200,59 @@ int PicoReset(int hard)
   return 0;\r
 }\r
 \r
+static int dma_timings[] = {\r
+83,  167, 166,  83, // vblank: 32cell: dma2vram dma2[vs|c]ram vram_fill vram_copy\r
+102, 205, 204, 102, // vblank: 40cell:\r
+8,    16,  15,   8, // active: 32cell:\r
+9,    18,  17,   9  // ...\r
+};\r
+\r
+static int dma_bsycles[] = {\r
+(488<<8)/83,  (488<<8)/167, (488<<8)/166, (488<<8)/83,\r
+(488<<8)/102, (488<<8)/205, (488<<8)/204, (488<<8)/102,\r
+(488<<8)/8,   (488<<8)/16,  (488<<8)/15,  (488<<8)/8,\r
+(488<<8)/9,   (488<<8)/18,  (488<<8)/17,  (488<<8)/9\r
+};\r
+\r
+//static\r
+int CheckDMA(void)\r
+{\r
+  int burn = 0, bytes_can = 0, dma_op = Pico.video.reg[0x17]>>6; // see gens for 00 and 01 modes\r
+  int bytes = Pico.m.dma_bytes;\r
+  int dma_op1;\r
+\r
+  if(!(dma_op&2)) dma_op = (Pico.video.type==1) ? 0 : 1; // setting dma_timings offset here according to Gens\r
+  dma_op1 = dma_op;\r
+  if(Pico.video.reg[12] & 1) dma_op |= 4; // 40 cell mode?\r
+  if(!(Pico.video.status&8)&&(Pico.video.reg[1]&0x40)) dma_op|=8; // active display?\r
+  bytes_can = dma_timings[dma_op];\r
+\r
+  if(bytes <= bytes_can) {\r
+    if(dma_op&2) Pico.video.status&=~2; // dma no longer busy\r
+    else {\r
+      burn = bytes * dma_bsycles[dma_op] >> 8; // have to be approximate because can't afford division..\r
+      //SekCycleCnt-=Pico.m.dma_endcycles;\r
+      //Pico.m.dma_endcycles = 0;\r
+    }\r
+    Pico.m.dma_bytes = 0;\r
+  } else {\r
+    if(!(dma_op&2)) burn = 488;\r
+    Pico.m.dma_bytes -= bytes_can;\r
+  }\r
+\r
+  //SekCycleCnt+=burn;\r
+  dprintf("~Dma %i op=%i can=%i burn=%i [%i|%i]", Pico.m.dma_bytes, dma_op1, bytes_can, burn, Pico.m.scanline, SekCyclesDone());\r
+  //dprintf("~aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);\r
+  return burn;\r
+}\r
+\r
 static __inline void SekRun(int cyc)\r
 {\r
   int cyc_do;\r
   SekCycleAim+=cyc;\r
-  if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;\r
+  //dprintf("aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);\r
+  if((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;\r
+  //dprintf("cyc_do: %i", cyc_do);\r
 #if   defined(EMU_C68K) && defined(EMU_M68K)\r
   // this means we do run-compare Cyclone vs Musashi\r
   SekCycleCnt+=CM_compareRun(cyc_do);\r
@@ -254,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
@@ -331,14 +394,14 @@ static int PicoFrameHints(void)
     // V-Interrupt:\r
     if (y == lines_vis)\r
     {\r
-      //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);\r
-      pv->status|=0x88; // V-Int happened, go into vblank\r
-      SekRun(128); SekCycleAim-=128; // there must be a gap between H and V ints, also after vblank bit set (Mazin Saga, Bram Stoker's Dracula)\r
-      /*if(Pico.m.z80Run && (PicoOpt&4)) {\r
-        z80CycleAim+=cycles_z80/2;\r
-        total_z80+=z80_run(z80CycleAim-total_z80);\r
-        z80CycleAim-=cycles_z80/2;\r
-      }*/\r
+      dprintf("vint: @ %06x [%i|%i], aim=%i cnt=%i", SekPc, y, SekCycleCnt, SekCycleAim, SekCycleCnt);\r
+      pv->status|=0x08; // go into vblank\r
+      if(!Pico.m.dma_bytes||(Pico.video.reg[0x17]&0x80)) {\r
+        // there must be a gap between H and V ints, also after vblank bit set (Mazin Saga, Bram Stoker's Dracula)\r
+        SekRun(128); SekCycleAim-=128; // 128; ?\r
+      }\r
+      dprintf("[%i|%i], aim=%i cnt=%i @ %x", y, SekCycleCnt, SekCycleAim, SekCycleCnt, SekPc);\r
+      pv->status|=0x80; // V-Int happened\r
       pv->pending_ints|=0x20;\r
       if(pv->reg[1]&0x20) SekInterrupt(6);\r
       if(Pico.m.z80Run && (PicoOpt&4)) // ?\r
@@ -364,6 +427,7 @@ static int PicoFrameHints(void)
       getSamples(y);\r
 \r
     // Run scanline:\r
+    if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();\r
     SekRun(cycles_68k);\r
     if((PicoOpt&4) && Pico.m.z80Run) {\r
       Pico.m.z80Run|=2;\r
@@ -475,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
@@ -547,6 +611,8 @@ int PicoFrame(void)
 {\r
   int acc;\r
 \r
+  Pico.m.frame_count++;\r
+\r
   if (PicoMCD & 1) {\r
     PicoFrameMCD();\r
     return 0;\r