libretro: drop sram clear
[picodrive.git] / pico / pico.c
index 20648f0..bf9df26 100644 (file)
 #include "sound/ym2612.h"\r
 \r
 struct Pico Pico;\r
+struct PicoMem PicoMem;\r
 int PicoOpt;     \r
 int PicoSkipFrame;     // skip rendering frame?\r
 int PicoPad[2];        // Joypads, format is MXYZ SACB RLDU\r
 int PicoPadInt[2];     // internal copy\r
 int PicoAHW;           // active addon hardware: PAHW_*\r
+int PicoQuirks;        // game-specific quirks\r
 int PicoRegionOverride; // override the region detection 0: Auto, 1: Japan NTSC, 2: Japan PAL, 4: US, 8: Europe\r
 int PicoAutoRgnOrder;\r
 \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
@@ -32,9 +32,15 @@ void PicoInit(void)
 {\r
   // Blank space for state:\r
   memset(&Pico,0,sizeof(Pico));\r
+  memset(&PicoMem,0,sizeof(PicoMem));\r
   memset(&PicoPad,0,sizeof(PicoPad));\r
   memset(&PicoPadInt,0,sizeof(PicoPadInt));\r
 \r
+  Pico.est.Pico = &Pico;\r
+  Pico.est.PicoMem_vram = PicoMem.vram;\r
+  Pico.est.PicoMem_cram = PicoMem.cram;\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
@@ -42,6 +48,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
@@ -52,16 +61,19 @@ void PicoExit(void)
   PicoCartUnload();\r
   z80_exit();\r
 \r
-  if (SRam.data)\r
-    free(SRam.data);\r
+  free(Pico.sv.data);\r
+  Pico.sv.data = NULL;\r
+  Pico.sv.start = Pico.sv.end = 0;\r
+  pevt_dump();\r
 }\r
 \r
 void PicoPower(void)\r
 {\r
   Pico.m.frame_count = 0;\r
+  Pico.t.m68c_cnt = Pico.t.m68c_aim = 0;\r
 \r
   // clear all memory of the emulated machine\r
-  memset(&Pico.ram,0,(unsigned char *)&Pico.rom - Pico.ram);\r
+  memset(&PicoMem,0,sizeof(PicoMem));\r
 \r
   memset(&Pico.video,0,sizeof(Pico.video));\r
   memset(&Pico.m,0,sizeof(Pico.m));\r
@@ -69,6 +81,9 @@ void PicoPower(void)
   Pico.video.pending_ints=0;\r
   z80_reset();\r
 \r
+  // my MD1 VA6 console has this in IO\r
+  PicoMem.ioports[1] = PicoMem.ioports[2] = PicoMem.ioports[3] = 0xff;\r
+\r
   // default VDP register values (based on Fusion)\r
   Pico.video.reg[0] = Pico.video.reg[1] = 0x04;\r
   Pico.video.reg[0xc] = 0x81;\r
@@ -145,6 +160,10 @@ int PicoReset(void)
   if (Pico.romsize <= 0)\r
     return 1;\r
 \r
+#if defined(CPU_CMP_R) || defined(CPU_CMP_W) || defined(DRC_CMP)\r
+  PicoOpt |= POPT_DIS_VDP_FIFO|POPT_DIS_IDLE_DET;\r
+#endif\r
+\r
   /* must call now, so that banking is reset, and correct vectors get fetched */\r
   if (PicoResetHook)\r
     PicoResetHook();\r
@@ -158,19 +177,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=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
@@ -197,12 +212,12 @@ int PicoReset(void)
 \r
   // reset sram state; enable sram access by default if it doesn't overlap with ROM\r
   Pico.m.sram_reg = 0;\r
-  if ((SRam.flags & SRF_EEPROM) || Pico.romsize <= SRam.start)\r
+  if ((Pico.sv.flags & SRF_EEPROM) || Pico.romsize <= Pico.sv.start)\r
     Pico.m.sram_reg |= SRR_MAPPED;\r
 \r
-  if (SRam.flags & SRF_ENABLED)\r
-    elprintf(EL_STATUS, "sram: %06x - %06x; eeprom: %i", SRam.start, SRam.end,\r
-      !!(SRam.flags & SRF_EEPROM));\r
+  if (Pico.sv.flags & SRF_ENABLED)\r
+    elprintf(EL_STATUS, "sram: %06x - %06x; eeprom: %i", Pico.sv.start, Pico.sv.end,\r
+      !!(Pico.sv.flags & SRF_EEPROM));\r
 \r
   return 0;\r
 }\r
@@ -214,31 +229,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
@@ -262,33 +273,32 @@ 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
-  //dprintf("~aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);\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", Pico.t.m68c_aim, Pico.t.m68c_cnt);\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
-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
-  cnt = z80_cycle_aim - z80_cycle_cnt;\r
+\r
+  m68k_cnt = m68k_cycles_done - Pico.t.m68c_frame_start;\r
+  Pico.t.z80c_aim = cycles_68k_to_z80(m68k_cnt);\r
+  cnt = Pico.t.z80c_aim - Pico.t.z80c_cnt;\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
-    z80_cycle_aim, z80_cycle_aim / 228);\r
+  elprintf(EL_BUSREQ, "z80 sync %i (%u|%u -> %u|%u)", cnt,\r
+    Pico.t.z80c_cnt, Pico.t.z80c_cnt * 15 / 7 / 488,\r
+    Pico.t.z80c_aim, Pico.t.z80c_aim * 15 / 7 / 488);\r
 \r
   if (cnt > 0)\r
-    z80_cycle_cnt += z80_run(cnt);\r
+    Pico.t.z80c_cnt += z80_run(cnt);\r
 \r
   pprof_end(z80);\r
 }\r
@@ -305,14 +315,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