1.32 release
[picodrive.git] / Pico / Pico.c
index 1f201f4..104aef0 100644 (file)
@@ -18,7 +18,7 @@ int PicoSkipFrame=0; // skip rendering frame?
 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
@@ -49,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
@@ -70,12 +71,15 @@ int PicoReset(int hard)
   else PicoMemSetup();\r
   PicoMemReset();\r
   SekReset();\r
+  // s68k doesn't have the TAS quirk, so we just globally set normal TAS handler in MCD mode (used by Batman games).\r
+  SekSetRealTAS(PicoMCD & 1);\r
   SekCycleCntT=0;\r
   z80_reset();\r
 \r
   // reset VDP state, VRAM and PicoMisc\r
   //memset(&Pico.video,0,sizeof(Pico.video));\r
   //memset(&Pico.vram,0,sizeof(Pico.vram));\r
+  memset(Pico.ioports,0,sizeof(Pico.ioports)); // needed for MCD to reset properly\r
   memset(&Pico.m,0,sizeof(Pico.m));\r
   Pico.video.pending_ints=0;\r
   emustatus = 0;\r
@@ -145,7 +149,6 @@ int PicoReset(int hard)
 \r
   if (PicoMCD & 1) {\r
     PicoResetMCD(hard);\r
-    SRam.data = 0;\r
     return 0;\r
   }\r
 \r
@@ -191,6 +194,8 @@ int PicoReset(int hard)
     // Dino Dini's Soccer malfunctions if SRAM is not filled with 0xff\r
     if (strncmp((char *)Pico.rom+0x150, "IDOND NI'I", 10) == 0)\r
       memset(SRam.data, 0xff, sram_size);\r
+    dprintf("sram: det: %i; eeprom: %i; start: %06x; end: %06x\n",\r
+      (Pico.m.sram_reg>>4)&1, (Pico.m.sram_reg>>2)&1, SRam.start, SRam.end);\r
   }\r
 \r
   Pico.m.sram_reg = SRam.reg_back; // restore sram_reg\r
@@ -314,20 +319,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
@@ -424,11 +431,18 @@ static int PicoFrameHints(void)
       getSamples(y);\r
 \r
     // Run scanline:\r
-    if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();\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
-      z80CycleAim+=cycles_z80;\r
+    if ((PicoOpt&4) && Pico.m.z80Run) {\r
+      if (Pico.m.z80Run & 2) z80CycleAim+=cycles_z80;\r
+      else {\r
+        int cnt = SekCyclesDone() - z80startCycle;\r
+        cnt = (cnt>>1)-(cnt>>5);\r
+        //if (cnt > cycles_z80) printf("FIXME: z80 cycles: %i\n", cnt);\r
+        if (cnt > cycles_z80) cnt = cycles_z80;\r
+        Pico.m.z80Run |= 2;\r
+        z80CycleAim+=cnt;\r
+      }\r
       total_z80+=z80_run(z80CycleAim-total_z80);\r
     }\r
   }\r
@@ -480,7 +494,13 @@ static int PicoFrameSimple(void)
   int y=0,line=0,lines=0,lines_step=0,sects;\r
   int cycles_68k_vblock,cycles_68k_block;\r
 \r
-  if(Pico.m.pal) {\r
+  // we don't emulate DMA timing in this mode\r
+  if (Pico.m.dma_bytes) {\r
+    Pico.m.dma_bytes=0;\r
+    Pico.video.status&=~2;\r
+  }\r
+\r
+  if (Pico.m.pal) {\r
     // M68k cycles/frame: 152009.78\r
     if(pv->reg[1]&8) { // 240 lines\r
       cycles_68k_block  = (int) ((double) OSC_PAL  /  7 / 50 / 312 * 15 + 0.4); // 16 sects, 16*15=240, 7308\r
@@ -536,10 +556,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
@@ -633,19 +653,8 @@ int PicoFrame(void)
   return 0;\r
 }\r
 \r
-static int DefaultCram(int cram)\r
-{\r
-  int high=0x0841;\r
-  // Convert 0000bbbb ggggrrrr\r
-  // to      rrrr1ggg g10bbbb1\r
-  high|=(cram&0x00f)<<12; // Red\r
-  high|=(cram&0x0f0)<< 3; // Green\r
-  high|=(cram&0xf00)>> 7; // Blue\r
-  return high;\r
-}\r
-\r
-// Function to convert Megadrive Cram into a native colour:\r
-int (*PicoCram)(int cram)=DefaultCram;\r
+// callback to output message from emu\r
+void (*PicoMessage)(const char *msg)=NULL;\r
 \r
 #if defined(__DEBUG_PRINT) || defined(WIN32)\r
 // tmp debug: dump some stuff\r