some more risky timing changes
[picodrive.git] / pico / pico.c
index bbc514f..f0c54d1 100644 (file)
@@ -22,7 +22,6 @@ int PicoAutoRgnOrder;
 \r
 struct PicoSRAM SRam;\r
 int emustatus;         // rapid_ym2612, multi_ym_updates\r
-int scanlines_total;\r
 \r
 void (*PicoWriteSound)(int len) = NULL; // called at the best time to send sound buffer (PsndOut) to hardware\r
 void (*PicoResetHook)(void) = NULL;\r
@@ -36,6 +35,10 @@ void PicoInit(void)
   memset(&PicoPad,0,sizeof(PicoPad));\r
   memset(&PicoPadInt,0,sizeof(PicoPadInt));\r
 \r
+  Pico.est.Pico_video = &Pico.video;\r
+  Pico.est.Pico_vram = Pico.vram;\r
+  Pico.est.PicoOpt = &PicoOpt;\r
+\r
   // Init CPUs:\r
   SekInit();\r
   z80_init(); // init even if we aren't going to use it\r
@@ -43,6 +46,9 @@ void PicoInit(void)
   PicoInitMCD();\r
   PicoSVPInit();\r
   Pico32xInit();\r
+\r
+  PicoDrawInit();\r
+  PicoDraw2Init();\r
 }\r
 \r
 // to be called once on emu exit\r
@@ -61,6 +67,7 @@ void PicoExit(void)
 void PicoPower(void)\r
 {\r
   Pico.m.frame_count = 0;\r
+  SekCycleCnt = SekCycleAim = 0;\r
 \r
   // clear all memory of the emulated machine\r
   memset(&Pico.ram,0,(unsigned char *)&Pico.rom - Pico.ram);\r
@@ -167,19 +174,15 @@ int PicoReset(void)
   }\r
 \r
   SekReset();\r
+  // ..but do not reset SekCycle* to not desync with addons\r
+\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(PicoAHW & PAHW_MCD);\r
-  SekCycleCntT = SekCycleCnt = SekCycleAim = 0;\r
-\r
-  if (PicoAHW & PAHW_MCD)\r
-    // needed for MCD to reset properly, probably some bug hides behind this..\r
-    memset(Pico.ioports,0,sizeof(Pico.ioports));\r
 \r
   Pico.m.dirtyPal = 1;\r
 \r
   Pico.m.z80_bank68k = 0;\r
   Pico.m.z80_reset = 1;\r
-  memset(Pico.zram, 0, sizeof(Pico.zram)); // ??\r
 \r
   PicoDetectRegion();\r
   Pico.video.status = 0x3428 | Pico.m.pal; // 'always set' bits | vblank | collision | pal\r
@@ -223,31 +226,27 @@ void PicoLoopPrepare(void)
     // force setting possibly changed..\r
     Pico.m.pal = (PicoRegionOverride == 2 || PicoRegionOverride == 8) ? 1 : 0;\r
 \r
-  // FIXME: PAL has 313 scanlines..\r
-  scanlines_total = Pico.m.pal ? 312 : 262;\r
-\r
   Pico.m.dirtyPal = 1;\r
   rendstatus_old = -1;\r
 }\r
 \r
-\r
-// dma2vram settings are just hacks to unglitch Legend of Galahad (needs <= 104 to work)\r
-// same for Outrunners (92-121, when active is set to 24)\r
-// 96 is VR hack\r
+// this table is wrong and should be removed\r
+// keeping it for now to compensate wrong timing elswhere, mainly for Outrunners\r
 static const int dma_timings[] = {\r
-  96,  167, 166,  83, // vblank: 32cell: dma2vram dma2[vs|c]ram vram_fill vram_copy\r
-  102, 205, 204, 102, // vblank: 40cell:\r
-  16,   16,  15,   8, // active: 32cell:\r
-  24,   18,  17,   9  // ...\r
+   83, 166,  83,  83, // vblank: 32cell: dma2vram dma2[vs|c]ram vram_fill vram_copy\r
+  102, 204, 102, 102, // vblank: 40cell:\r
+    8,  16,   8,   8, // active: 32cell:\r
+   17,  18,   9,   9  // ...\r
 };\r
 \r
 static const int dma_bsycles[] = {\r
-  (488<<8)/96,  (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)/16,  (488<<8)/16,  (488<<8)/15,  (488<<8)/8,\r
-  (488<<8)/24,  (488<<8)/18,  (488<<8)/17,  (488<<8)/9\r
+  (488<<8)/83,  (488<<8)/166, (488<<8)/83,  (488<<8)/83,\r
+  (488<<8)/102, (488<<8)/204, (488<<8)/102, (488<<8)/102,\r
+  (488<<8)/8,   (488<<8)/16,  (488<<8)/8,   (488<<8)/8,\r
+  (488<<8)/9,   (488<<8)/18,  (488<<8)/9,   (488<<8)/9\r
 };\r
 \r
+// grossly inaccurate.. FIXME FIXXXMEE\r
 PICO_INTERNAL int CheckDMA(void)\r
 {\r
   int burn = 0, xfers_can, dma_op = Pico.video.reg[0x17]>>6; // see gens for 00 and 01 modes\r
@@ -271,29 +270,35 @@ PICO_INTERNAL int CheckDMA(void)
     Pico.m.dma_xfers -= xfers_can;\r
   }\r
 \r
-  elprintf(EL_VDPDMA, "~Dma %i op=%i can=%i burn=%i [%i]", Pico.m.dma_xfers, dma_op1, xfers_can, burn, SekCyclesDone());\r
+  elprintf(EL_VDPDMA, "~Dma %i op=%i can=%i burn=%i [%u]",\r
+    Pico.m.dma_xfers, dma_op1, xfers_can, burn, SekCyclesDone());\r
   //dprintf("~aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);\r
   return burn;\r
 }\r
 \r
 #include "pico_cmn.c"\r
 \r
-int z80stopCycle;\r
-int z80_cycle_cnt;        /* 'done' z80 cycles before z80_run() */\r
+unsigned int last_z80_sync; /* in 68k cycles */\r
+int z80_cycle_cnt;\r
 int z80_cycle_aim;\r
 int z80_scanline;\r
 int z80_scanline_cycles;  /* cycles done until z80_scanline */\r
 \r
 /* sync z80 to 68k */\r
-PICO_INTERNAL void PicoSyncZ80(int m68k_cycles_done)\r
+PICO_INTERNAL void PicoSyncZ80(unsigned int m68k_cycles_done)\r
 {\r
+  int m68k_cnt;\r
   int cnt;\r
-  z80_cycle_aim = cycles_68k_to_z80(m68k_cycles_done);\r
+\r
+  m68k_cnt = m68k_cycles_done - last_z80_sync;\r
+  z80_cycle_aim += cycles_68k_to_z80(m68k_cnt);\r
   cnt = z80_cycle_aim - z80_cycle_cnt;\r
+  last_z80_sync = m68k_cycles_done;\r
 \r
   pprof_start(z80);\r
 \r
-  elprintf(EL_BUSREQ, "z80 sync %i (%i|%i -> %i|%i)", cnt, z80_cycle_cnt, z80_cycle_cnt / 228,\r
+  elprintf(EL_BUSREQ, "z80 sync %i (%u|%u -> %u|%u)", cnt,\r
+    z80_cycle_cnt, z80_cycle_cnt / 228,\r
     z80_cycle_aim, z80_cycle_aim / 228);\r
 \r
   if (cnt > 0)\r
@@ -314,14 +319,13 @@ void PicoFrame(void)
     goto end;\r
   }\r
 \r
-  // TODO: MCD+32X\r
-  if (PicoAHW & PAHW_MCD) {\r
-    PicoFrameMCD();\r
+  if (PicoAHW & PAHW_32X) {\r
+    PicoFrame32x(); // also does MCD+32X\r
     goto end;\r
   }\r
 \r
-  if (PicoAHW & PAHW_32X) {\r
-    PicoFrame32x();\r
+  if (PicoAHW & PAHW_MCD) {\r
+    PicoFrameMCD();\r
     goto end;\r
   }\r
 \r