32x: add preliminary hint emulation
[picodrive.git] / pico / pico_cmn.c
index cc77f5f..2b558c1 100644 (file)
@@ -1,10 +1,16 @@
-// common code for Pico.c and cd/Pico.c
-// (c) Copyright 2007,2008 Grazvydas "notaz" Ignotas
+/*
+ * common code for pico.c and cd/pico.c
+ * (C) notaz, 2007-2009
+ *
+ * This work is licensed under the terms of MAME license.
+ * See COPYING file in the top-level directory.
+ */
 
 #define CYCLES_M68K_LINE     488 // suitable for both PAL/NTSC
 #define CYCLES_M68K_VINT_LAG  68
 #define CYCLES_M68K_ASD      148
 #define CYCLES_S68K_LINE     795
+#define CYCLES_S68K_VINT_LAG 111
 #define CYCLES_S68K_ASD      241
 
 // pad delay (for 6 button pads)
   }
 
 // CPUS_RUN
-#ifndef PICO_CD
+#ifndef CPUS_RUN
 #define CPUS_RUN(m68k_cycles,s68k_cycles) \
-    SekRunM68k(m68k_cycles);
-#else
-#define CPUS_RUN(m68k_cycles,s68k_cycles) \
-{ \
-    if ((PicoOpt&POPT_EN_MCD_PSYNC) && (Pico_mcd->m.busreq&3) == 1) { \
-      SekRunPS(m68k_cycles, s68k_cycles); /* "better/perfect sync" */ \
-    } else { \
-      SekRunM68k(m68k_cycles); \
-      if ((Pico_mcd->m.busreq&3) == 1) /* no busreq/no reset */ \
-        SekRunS68k(s68k_cycles); \
-    } \
-}
+  SekRunM68k(m68k_cycles)
+#endif
+
+static __inline void SekRunM68k(int cyc)
+{
+  int cyc_do;
+  pprof_start(m68k);
+  pevt_log_m68k_o(EVT_RUN_START);
+
+  SekCycleAim+=cyc;
+  if ((cyc_do=SekCycleAim-SekCycleCnt) <= 0)
+    goto out;
+
+#if defined(EMU_CORE_DEBUG)
+  // this means we do run-compare
+  SekCycleCnt+=CM_compareRun(cyc_do, 0);
+#elif defined(EMU_C68K)
+  PicoCpuCM68k.cycles=cyc_do;
+  CycloneRun(&PicoCpuCM68k);
+  SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;
+#elif defined(EMU_M68K)
+  SekCycleCnt+=m68k_execute(cyc_do);
+#elif defined(EMU_F68K)
+  SekCycleCnt+=fm68k_emulate(cyc_do, 0, 0);
 #endif
 
-// Accurate but slower frame which does hints
+out:
+  SekTrace(0);
+  pevt_log_m68k_o(EVT_RUN_END);
+  pprof_end(m68k);
+}
+
 static int PicoFrameHints(void)
 {
   struct PicoVideo *pv=&Pico.video;
   int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
   int hint; // Hint counter
 
+  pevt_log_m68k_o(EVT_FRAME_START);
   pv->v_counter = Pico.m.scanline = 0;
 
   if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
@@ -64,6 +88,7 @@ static int PicoFrameHints(void)
   SekCyclesResetS68k();
 #endif
   PsndDacLine = 0;
+  emustatus &= ~1;
 
   pv->status&=~0x88; // clear V-Int, come out of vblank
 
@@ -118,19 +143,18 @@ static int PicoFrameHints(void)
       }
     }
 
-#ifndef PICO_CD
     // get samples from sound chips
-    if (y == 32 && PsndOut)
-      emustatus &= ~1;
-    else if ((y == 224 || y == line_sample) && PsndOut)
+    if ((y == 224 || y == line_sample) && PsndOut)
     {
       if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
         PicoSyncZ80(SekCycleCnt);
       if (ym2612.dacen && PsndDacLine <= y)
         PsndDoDAC(y);
-      getSamples(y);
-    }
+#ifdef PICO_32X
+      p32x_sync_sh2s(SekCyclesDoneT2());
 #endif
+      PsndGetSamples(y);
+    }
 
     // Run scanline:
     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
@@ -141,6 +165,7 @@ static int PicoFrameHints(void)
 #else
     if (PicoLineHook) PicoLineHook();
 #endif
+    pevt_log_m68k_o(EVT_NEXT_LINE);
   }
 
   if (!skip)
@@ -183,7 +208,7 @@ static int PicoFrameHints(void)
   // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
   // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
   // also delay between last H-int and V-int (Golden Axe 3)
-  SekRunM68k(CYCLES_M68K_VINT_LAG);
+  CPUS_RUN(CYCLES_M68K_VINT_LAG, CYCLES_S68K_VINT_LAG);
 
   if (pv->reg[1]&0x20) {
     elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
@@ -195,31 +220,33 @@ static int PicoFrameHints(void)
     z80_int();
   }
 
-  // get samples from sound chips
-#ifndef PICO_CD
-  if (y == 224)
+#ifdef PICO_32X
+  p32x_sync_sh2s(SekCyclesDoneT2());
+  p32x_start_blank();
 #endif
-    if (PsndOut)
-    {
-      if (ym2612.dacen && PsndDacLine <= y)
-        PsndDoDAC(y);
-      getSamples(y);
-    }
+
+  // get samples from sound chips
+  if (y == 224 && PsndOut)
+  {
+    if (ym2612.dacen && PsndDacLine <= y)
+      PsndDoDAC(y);
+    PsndGetSamples(y);
+  }
 
   // Run scanline:
   if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
   CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
-    CYCLES_S68K_LINE - CYCLES_S68K_ASD);
+    CYCLES_S68K_LINE - CYCLES_S68K_VINT_LAG - CYCLES_S68K_ASD);
 
 #ifdef PICO_CD
   update_chips();
 #else
   if (PicoLineHook) PicoLineHook();
 #endif
+  pevt_log_m68k_o(EVT_NEXT_LINE);
 
-  // PAL line count might actually be 313 according to Steve Snake, but that would complicate things.
-  lines = Pico.m.pal ? 312 : 262;
-  vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens
+  lines = scanlines_total;
+  vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
 
   for (y++; y < lines; y++)
   {
@@ -244,6 +271,7 @@ static int PicoFrameHints(void)
 #else
     if (PicoLineHook) PicoLineHook();
 #endif
+    pevt_log_m68k_o(EVT_NEXT_LINE);
   }
 
   // sync z80
@@ -252,6 +280,9 @@ static int PicoFrameHints(void)
   if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
     PsndDoDAC(lines-1);
 
+#ifdef PICO_32X
+  p32x_sync_sh2s(SekCyclesDoneT2());
+#endif
   timers_cycle();
 
   return 0;