accurate_sprites performance improvement, PSP is untested
[picodrive.git] / Pico / Pico.c
index e6a3b29..7146e2c 100644 (file)
@@ -8,23 +8,22 @@
 \r
 \r
 #include "PicoInt.h"\r
-#include "sound/sound.h"\r
 #include "sound/ym2612.h"\r
 \r
-int PicoVer=0x0110;\r
+int PicoVer=0x0133;\r
 struct Pico Pico;\r
-int PicoOpt=0; // disable everything by default\r
-int PicoSkipFrame=0; // skip rendering frame?\r
+int PicoOpt = 0;\r
+int PicoSkipFrame = 0; // skip rendering frame?\r
+int emustatus = 0;     // rapid_ym2612, multi_ym_updates\r
+int PicoPad[2];        // Joypads, format is SACB RLDU\r
+int PicoAHW = 0;       // active addon hardware: scd_active, 32x_active, svp_active, pico_active\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)(int len) = 0; // called once per frame at the best time to send sound buffer (PsndOut) to hardware\r
+struct PicoSRAM SRam = {0,};\r
 \r
-struct PicoSRAM SRam;\r
-int z80startCycle, z80stopCycle; // in 68k cycles\r
-//int z80ExtraCycles = 0;\r
-int PicoPad[2];  // Joypads, format is SACB RLDU\r
-int PicoMCD = 0; // mega CD status: scd_started, reset_pending\r
+void (*PicoWriteSound)(int len) = NULL; // called at the best time to send sound buffer (PsndOut) to hardware\r
+void (*PicoResetHook)(void) = NULL;\r
+void (*PicoLineHook)(int count) = NULL;\r
 \r
 // to be called once on emu init\r
 int PicoInit(void)\r
@@ -38,10 +37,9 @@ int PicoInit(void)
   z80_init(); // init even if we aren't going to use it\r
 \r
   PicoInitMCD();\r
+  PicoSVPInit();\r
 \r
-  // notaz: sram\r
   SRam.data=0;\r
-  SRam.resize=1;\r
 \r
   return 0;\r
 }\r
@@ -49,60 +47,51 @@ int PicoInit(void)
 // to be called once on emu exit\r
 void PicoExit(void)\r
 {\r
-  if (PicoMCD&1)\r
+  if (PicoAHW & PAHW_MCD)\r
     PicoExitMCD();\r
   z80_exit();\r
 \r
-  // notaz: sram\r
-  if(SRam.data) free(SRam.data); SRam.data=0;\r
+  if (SRam.data) free(SRam.data); SRam.data=0;\r
 }\r
 \r
-int PicoReset(int hard)\r
+void PicoPower(void)\r
 {\r
-  unsigned int region=0;\r
-  int support=0,hw=0,i=0;\r
-  unsigned char pal=0;\r
+  unsigned char sram_reg=Pico.m.sram_reg; // must be preserved\r
 \r
-  if (Pico.romsize<=0) return 1;\r
+  // clear all memory of the emulated machine\r
+  memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram);\r
 \r
-  // setup correct memory map\r
-  if (PicoMCD & 1)\r
-       PicoMemSetupCD();\r
-  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.video,0,sizeof(Pico.video));\r
   memset(&Pico.m,0,sizeof(Pico.m));\r
-  Pico.video.pending_ints=0;\r
-  emustatus = 0;\r
 \r
-  if(hard) {\r
-    // clear all memory of the emulated machine\r
-    memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram);\r
-  }\r
+  Pico.video.pending_ints=0;\r
+  z80_reset();\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
   Pico.video.reg[0xf] = 0x02;\r
-  Pico.m.dirtyPal = 1;\r
 \r
-  if(PicoRegionOverride)\r
+  if (PicoAHW & PAHW_MCD)\r
+    PicoPowerMCD();\r
+\r
+  Pico.m.sram_reg=sram_reg;\r
+  PicoReset();\r
+}\r
+\r
+PICO_INTERNAL void PicoDetectRegion(void)\r
+{\r
+  int support=0, hw=0, i;\r
+  unsigned char pal=0;\r
+\r
+  if (PicoRegionOverride)\r
   {\r
     support = PicoRegionOverride;\r
   }\r
   else\r
   {\r
     // Read cartridge region data:\r
-    region=PicoRead32(0x1f0);\r
+    int region=PicoRead32(0x1f0);\r
 \r
     for (i=0;i<4;i++)\r
     {\r
@@ -143,133 +132,115 @@ int PicoReset(int hard)
 \r
   Pico.m.hardware=(unsigned char)(hw|0x20); // No disk attached\r
   Pico.m.pal=pal;\r
-  Pico.video.status = 0x3408 | pal; // always set bits | vblank | pal\r
+}\r
 \r
-  sound_reset(); // pal must be known here\r
+int PicoReset(void)\r
+{\r
+  unsigned char sram_reg=Pico.m.sram_reg; // must be preserved\r
 \r
-  if (PicoMCD & 1) {\r
-    PicoResetMCD(hard);\r
-    return 0;\r
-  }\r
+  if (Pico.romsize<=0) return 1;\r
 \r
+  /* must call now, so that banking is reset, and correct vectors get fetched */\r
+  if (PicoResetHook) PicoResetHook();\r
 \r
-  // notaz: sram\r
-  if(SRam.resize) {\r
-    int sram_size = 0;\r
-    if(SRam.data) free(SRam.data); SRam.data=0;\r
-    Pico.m.sram_reg = 0;\r
-\r
-    if(*(Pico.rom+0x1B1) == 'R' && *(Pico.rom+0x1B0) == 'A') {\r
-      if(*(Pico.rom+0x1B2) & 0x40) {\r
-        // EEPROM SRAM\r
-        // what kind of EEPROMs are actually used? X24C02? X24C04? (X24C01 has only 128), but we will support up to 8K\r
-        SRam.start = PicoRead32(0x1B4) & ~1; // zero address is used for clock by some games\r
-        SRam.end   = PicoRead32(0x1B8);\r
-        sram_size  = 0x2000;\r
-        Pico.m.sram_reg = 4;\r
-      } else {\r
-        // normal SRAM\r
-        SRam.start = PicoRead32(0x1B4) & 0xFFFF00;\r
-        SRam.end   = PicoRead32(0x1B8) | 1;\r
-        sram_size  = SRam.end - SRam.start + 1;\r
-      }\r
-      Pico.m.sram_reg |= 0x10; // SRAM was detected\r
-    }\r
-    if(sram_size <= 0) {\r
-      // some games may have bad headers, like S&K and Sonic3\r
-      SRam.start = 0x200000;\r
-      SRam.end   = 0x203FFF;\r
-      sram_size  = 0x004000;\r
-    }\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(PicoAHW & PAHW_MCD);\r
+  SekCycleCntT=0;\r
 \r
-    // enable sram access by default if it doesn't overlap with ROM\r
-    if(Pico.romsize <= SRam.start) Pico.m.sram_reg |= 1;\r
-    SRam.reg_back = Pico.m.sram_reg;\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
+  emustatus = 0;\r
 \r
-    if(sram_size) {\r
-      SRam.data = (unsigned char *) calloc(sram_size, 1);\r
-      if(!SRam.data) return 1;\r
-    }\r
-    SRam.resize=0;\r
-    // 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
+  Pico.m.dirtyPal = 1;\r
+\r
+  PicoDetectRegion();\r
+  Pico.video.status = 0x3428 | Pico.m.pal; // 'always set' bits | vblank | collision | pal\r
+\r
+  PsndReset(); // pal must be known here\r
+\r
+  // create an empty "dma" to cause 68k exec start at random frame location\r
+  if (Pico.m.dma_xfers == 0 && !(PicoOpt&POPT_DIS_VDP_FIFO))\r
+    Pico.m.dma_xfers = rand() & 0x1fff;\r
+\r
+  if (PicoAHW & PAHW_MCD) {\r
+    PicoResetMCD();\r
+    return 0;\r
   }\r
 \r
-  Pico.m.sram_reg = SRam.reg_back; // restore sram_reg\r
-  SRam.changed = 0;\r
+  // reset sram state; enable sram access by default if it doesn't overlap with ROM\r
+  Pico.m.sram_reg=sram_reg&0x14;\r
+  if (!(Pico.m.sram_reg&4) && Pico.romsize <= SRam.start) Pico.m.sram_reg |= 1;\r
+\r
+  elprintf(EL_STATUS, "sram: det: %i; eeprom: %i; start: %06x; end: %06x",\r
+    (Pico.m.sram_reg>>4)&1, (Pico.m.sram_reg>>2)&1, SRam.start, SRam.end);\r
 \r
   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
+\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
+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
-8,    16,  15,   8, // active: 32cell:\r
-9,    18,  17,   9  // ...\r
+16,   16,  15,   8, // active: 32cell:\r
+24,   18,  17,   9  // ...\r
 };\r
 \r
-static int dma_bsycles[] = {\r
-(488<<8)/83,  (488<<8)/167, (488<<8)/166, (488<<8)/83,\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)/8,   (488<<8)/16,  (488<<8)/15,  (488<<8)/8,\r
-(488<<8)/9,   (488<<8)/18,  (488<<8)/17,  (488<<8)/9\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
 };\r
 \r
-//static\r
-int CheckDMA(void)\r
+PICO_INTERNAL 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 burn = 0, xfers_can, dma_op = Pico.video.reg[0x17]>>6; // see gens for 00 and 01 modes\r
+  int xfers = Pico.m.dma_xfers;\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
+  xfers_can = dma_timings[dma_op];\r
+  if(xfers <= xfers_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
+      burn = xfers * dma_bsycles[dma_op] >> 8; // have to be approximate because can't afford division..\r
     }\r
-    Pico.m.dma_bytes = 0;\r
+    Pico.m.dma_xfers = 0;\r
   } else {\r
     if(!(dma_op&2)) burn = 488;\r
-    Pico.m.dma_bytes -= bytes_can;\r
+    Pico.m.dma_xfers -= xfers_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
+  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
   return burn;\r
 }\r
 \r
-static __inline void SekRun(int cyc)\r
+static __inline void SekRunM68k(int cyc)\r
 {\r
   int cyc_do;\r
   SekCycleAim+=cyc;\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
+#if defined(EMU_CORE_DEBUG)\r
+  // this means we do run-compare\r
+  SekCycleCnt+=CM_compareRun(cyc_do, 0);\r
 #elif defined(EMU_C68K)\r
-  PicoCpu.cycles=cyc_do;\r
-  CycloneRun(&PicoCpu);\r
-  SekCycleCnt+=cyc_do-PicoCpu.cycles;\r
-#elif defined(EMU_A68K)\r
-  m68k_ICount=cyc_do;\r
-  M68000_RUN();\r
-  SekCycleCnt+=cyc_do-m68k_ICount;\r
+  PicoCpuCM68k.cycles=cyc_do;\r
+  CycloneRun(&PicoCpuCM68k);\r
+  SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;\r
 #elif defined(EMU_M68K)\r
   SekCycleCnt+=m68k_execute(cyc_do);\r
+#elif defined(EMU_F68K)\r
+  SekCycleCnt+=fm68k_emulate(cyc_do+1, 0);\r
 #endif\r
 }\r
 \r
@@ -277,297 +248,174 @@ static __inline void SekStep(void)
 {\r
   // this is required for timing sensitive stuff to work\r
   int realaim=SekCycleAim; SekCycleAim=SekCycleCnt+1;\r
-#if   defined(EMU_C68K) && defined(EMU_M68K)\r
-  // this means we do run-compare Cyclone vs Musashi\r
-  SekCycleCnt+=CM_compareRun(1);\r
+#if defined(EMU_CORE_DEBUG)\r
+  SekCycleCnt+=CM_compareRun(1, 0);\r
 #elif defined(EMU_C68K)\r
-  PicoCpu.cycles=1;\r
-  CycloneRun(&PicoCpu);\r
-  SekCycleCnt+=1-PicoCpu.cycles;\r
-#elif defined(EMU_A68K)\r
-  m68k_ICount=1;\r
-  M68000_RUN();\r
-  SekCycleCnt+=1-m68k_ICount;\r
+  PicoCpuCM68k.cycles=1;\r
+  CycloneRun(&PicoCpuCM68k);\r
+  SekCycleCnt+=1-PicoCpuCM68k.cycles;\r
 #elif defined(EMU_M68K)\r
   SekCycleCnt+=m68k_execute(1);\r
+#elif defined(EMU_F68K)\r
+  SekCycleCnt+=fm68k_emulate(1, 0);\r
 #endif\r
   SekCycleAim=realaim;\r
 }\r
 \r
 static int CheckIdle(void)\r
 {\r
-#if 1\r
-  unsigned char state[0x88];\r
-\r
-  memset(state,0,sizeof(state));\r
+  int i, state[0x24];\r
 \r
   // See if the state is the same after 2 steps:\r
-  SekState(state); SekStep(); SekStep(); SekState(state+0x44);\r
-  if (memcmp(state,state+0x44,0x44)==0) return 1;\r
-#else\r
-  unsigned char state[0x44];\r
-  static unsigned char oldstate[0x44];\r
-\r
-  SekState(state);\r
-  if(memcmp(state,oldstate,0x40)==0) return 1;\r
-  memcpy(oldstate, state, 0x40);\r
-#endif\r
+  SekState(state); SekStep(); SekStep(); SekState(state+0x12);\r
+  for (i = 0x11; i >= 0; i--)\r
+    if (state[i] != state[i+0x12]) return 0;\r
 \r
-  return 0;\r
+  return 1;\r
 }\r
 \r
+\r
 // to be called on 224 or line_sample scanlines only\r
 static __inline void getSamples(int y)\r
 {\r
+#if SIMPLE_WRITE_SOUND\r
+  if (y != 224) return;\r
+  PsndRender(0, PsndLen);\r
+  if (PicoWriteSound) PicoWriteSound(PsndLen);\r
+  PsndClear();\r
+#else\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
-         curr_pos += sound_render(curr_pos, PsndLen-PsndLen/2);\r
-    else curr_pos  = sound_render(0, PsndLen);\r
+         curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
+    else curr_pos  = PsndRender(0, PsndLen);\r
     if (emustatus&1) emustatus|=2; else emustatus&=~2;\r
     if (PicoWriteSound) PicoWriteSound(curr_pos);\r
     // clear sound buffer\r
-    sound_clear();\r
+    PsndClear();\r
   }\r
   else if(emustatus & 3) {\r
     emustatus|= 2;\r
     emustatus&=~1;\r
-    curr_pos = sound_render(0, PsndLen/2);\r
+    curr_pos = PsndRender(0, PsndLen/2);\r
   }\r
+#endif\r
 }\r
 \r
-//extern UINT32 mz80GetRegisterValue(void *, UINT32);\r
-\r
-// Accurate but slower frame which does hints\r
-static int PicoFrameHints(void)\r
-{\r
-  struct PicoVideo *pv=&Pico.video;\r
-  int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;\r
-  const int cycles_68k=488,cycles_z80=228; // both PAL and NTSC compile to same values\r
-  int skip=PicoSkipFrame || (PicoOpt&0x10);\r
-  int hint; // Hint counter\r
-\r
-  if(Pico.m.pal) { //\r
-    //cycles_68k = (int) ((double) OSC_PAL  /  7 / 50 / 312 + 0.4); // should compile to a constant (488)\r
-    //cycles_z80 = (int) ((double) OSC_PAL  / 15 / 50 / 312 + 0.4); // 228\r
-    lines  = 312;    // Steve Snake says there are 313 lines, but this seems to also work well\r
-    line_sample = 68;\r
-    if(pv->reg[1]&8) lines_vis = 240;\r
-  } else {\r
-    //cycles_68k = (int) ((double) OSC_NTSC /  7 / 60 / 262 + 0.4); // 488\r
-    //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228\r
-    lines  = 262;\r
-    line_sample = 93;\r
-  }\r
-\r
-  SekCyclesReset();\r
-  //z80ExtraCycles = 0;\r
-\r
-  if(PicoOpt&4)\r
-    z80CycleAim = 0;\r
-//    z80_resetCycles();\r
-\r
-  pv->status&=~0x88; // clear V-Int, come out of vblank\r
 \r
-  hint=pv->reg[10]; // Load H-Int counter\r
-  //dprintf("-hint: %i", hint);\r
+#include "PicoFrameHints.c"\r
 \r
-  for (y=0;y<lines;y++)\r
-  {\r
-    Pico.m.scanline=(short)y;\r
 \r
-    // pad delay (for 6 button pads)\r
-    if(PicoOpt&0x20) {\r
-      if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;\r
-      if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;\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
-    // H-Interrupts:\r
-    if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe\r
-    {\r
-      //dprintf("rhint:old @ %06x", SekPc);\r
-      hint=pv->reg[10]; // Reload H-Int counter\r
-      pv->pending_ints|=0x10;\r
-      if (pv->reg[0]&0x10) SekInterrupt(4);\r
-      //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);\r
-      //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));\r
-    }\r
-\r
-    // V-Interrupt:\r
-    if (y == lines_vis)\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
-        z80_int();\r
-      //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));\r
-    }\r
-\r
-    // decide if we draw this line\r
-#if CAN_HANDLE_240_LINES\r
-    if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )\r
-#else\r
-    if(!skip && y<224)\r
-#endif\r
-      PicoLine(y);\r
-\r
-    if(PicoOpt&1)\r
-      sound_timers_and_dac(y);\r
-\r
-    // get samples from sound chips\r
-    if(y == 32 && PsndOut)\r
-      emustatus &= ~1;\r
-    else if((y == 224 || y == line_sample) && PsndOut)\r
-      getSamples(y);\r
-\r
-    // Run scanline:\r
-    if (Pico.m.dma_bytes) SekCyclesBurn(CheckDMA());\r
-    SekRun(cycles_68k);\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
-\r
-  // draw a frame just after vblank in alternative render mode\r
-  if(!PicoSkipFrame && (PicoOpt&0x10))\r
-    PicoFrameFull();\r
-\r
-  return 0;\r
-}\r
-\r
-// helper z80 runner\r
-static void PicoRunZ80Simple(int line_from, int line_to)\r
+/* sync z80 to 68k */\r
+PICO_INTERNAL void PicoSyncZ80(int m68k_cycles_done)\r
 {\r
-  int line_from_r=line_from, line_to_r=line_to, line = line_from;\r
-  int line_sample = Pico.m.pal ? 68 : 93;\r
-  extern const unsigned short vcounts[];\r
+  int cnt;\r
+  z80_cycle_aim = cycles_68k_to_z80(m68k_cycles_done);\r
+  cnt = z80_cycle_aim - z80_cycle_cnt;\r
 \r
-  if(!(PicoOpt&4) || Pico.m.z80Run == 0) { line_from_r = line_to_r; line_to_r = 0; }\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
 \r
-  if(z80startCycle != 0x01000000) {\r
-    line_from_r = vcounts[z80startCycle>>8]+1;\r
-    z80startCycle = 0x01000000;\r
-  }\r
-  if(z80stopCycle != 0x01000000) {\r
-    line_to_r = vcounts[z80stopCycle>>8]+1;\r
-    z80stopCycle = 0x01000000;\r
-  }\r
-\r
-  if(PicoOpt&1) {\r
-    // we have ym2612 enabled, so we have to run Z80 in lines, so we could update DAC and timers\r
-    for(; line < line_to; line++) {\r
-      sound_timers_and_dac(line);\r
-      if((line == 224 || line == line_sample) && PsndOut) getSamples(line);\r
-      if(line == 32 && PsndOut) emustatus &= ~1;\r
-      if(line >= line_from_r && line < line_to_r)\r
-        z80_run(228);\r
-    }\r
-  } else if(line_to_r-line_from_r > 0) {\r
-    z80_run(228*(line_to_r-line_from_r));\r
-    // samples will be taken by caller\r
-  }\r
+  if (cnt > 0)\r
+    z80_cycle_cnt += z80_run(cnt);\r
 }\r
 \r
+\r
 // Simple frame without H-Ints\r
 static int PicoFrameSimple(void)\r
 {\r
   struct PicoVideo *pv=&Pico.video;\r
-  int y=0,line=0,lines=0,lines_step=0,sects;\r
+  int y=0,lines_step=0,sects,line_last;\r
   int cycles_68k_vblock,cycles_68k_block;\r
 \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
+  // split to 16 run calls for active scan, for vblank split to 2 (ntsc), 3 (pal 240), 4 (pal 224)\r
+  if (Pico.m.pal)\r
+  {\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
-      cycles_68k_vblock = (int) ((double) OSC_PAL  /  7 / 50 / 312 * 24 + 0.4); // 3 sects, 3*24=72, 35163?\r
+      cycles_68k_block  = 7308;\r
+      cycles_68k_vblock = 11694;\r
       lines_step = 15;\r
     } else {\r
-      cycles_68k_block  = (int) ((double) OSC_PAL  /  7 / 50 / 312 * 14 + 0.4); // 16*14=224\r
-      cycles_68k_vblock = (int) ((double) OSC_PAL  /  7 / 50 / 312 * 22 + 0.4); // 4 sects, 4*22=88\r
+      cycles_68k_block  = 6821;\r
+      cycles_68k_vblock = 10719;\r
       lines_step = 14;\r
     }\r
+    line_last = 312-1;\r
   } else {\r
     // M68k cycles/frame: 127840.71\r
-    cycles_68k_block  = (int) ((double) OSC_NTSC /  7 / 60 / 262 * 14 + 0.4); // 16*14=224, 6831\r
-    cycles_68k_vblock = (int) ((double) OSC_NTSC /  7 / 60 / 262 * 19 + 0.4); // 2 sects, 2*19=38, 18544\r
+    cycles_68k_block  = 6841; // (488*224+148)/16.0, -4\r
+    cycles_68k_vblock = 9164; // (38*488-148-68)/2.0, 0\r
     lines_step = 14;\r
+    line_last = 262-1;\r
+  }\r
+\r
+  // a hack for VR, to get it running in fast mode\r
+  if (PicoAHW & PAHW_SVP)\r
+    Pico.ram[0xd864^1] = 0x1a;\r
+\r
+  // we don't emulate DMA timing in this mode\r
+  if (Pico.m.dma_xfers) {\r
+    Pico.m.dma_xfers=0;\r
+    Pico.video.status&=~2;\r
   }\r
 \r
+  // VDP FIFO too\r
+  pv->lwrite_cnt = 0;\r
+  Pico.video.status|=0x200;\r
+\r
   Pico.m.scanline=-1;\r
+  PsndDacLine = 0;\r
 \r
   SekCyclesReset();\r
-\r
-  if(PicoOpt&4)\r
-    z80_resetCycles();\r
+  z80_resetCycles();\r
+  timers_cycle();\r
 \r
   // 6 button pad: let's just say it timed out now\r
   Pico.m.padTHPhase[0]=Pico.m.padTHPhase[1]=0;\r
 \r
   // ---- Active Scan ----\r
-  pv->status&=~88; // clear V-Int, come out of vblank\r
+  pv->status&=~0x88; // clear V-Int, come out of vblank\r
 \r
   // Run in sections:\r
-  for(sects=16; sects; sects--)\r
+  for (sects=16; sects; sects--)\r
   {\r
     if (CheckIdle()) break;\r
 \r
-    lines += lines_step;\r
-    SekRun(cycles_68k_block);\r
-\r
-    PicoRunZ80Simple(line, lines);\r
-    line=lines;\r
+    SekRunM68k(cycles_68k_block);\r
+    if (PicoLineHook) PicoLineHook(lines_step);\r
   }\r
 \r
-  // run Z80 for remaining sections\r
-  if(sects) {\r
-    int c = sects*cycles_68k_block;\r
+  // do remaining sections without 68k\r
+  if (sects) {\r
+    SekCycleCnt += sects * cycles_68k_block;\r
+    SekCycleAim += sects * cycles_68k_block;\r
 \r
-    lines += sects*lines_step;\r
-    PicoRunZ80Simple(line, lines);\r
-    // this is for approriate line counter, etc\r
-    SekCycleCnt += c;\r
-    SekCycleAim += c;\r
+    if (PicoLineHook) PicoLineHook(sects*lines_step);\r
   }\r
 \r
-  // here we render sound if ym2612 is disabled\r
-  if(!(PicoOpt&1) && PsndOut) {\r
-    int len = sound_render(0, PsndLen);\r
-    if(PicoWriteSound) PicoWriteSound(len);\r
-    // clear sound buffer\r
-    sound_clear();\r
+  // another hack for VR (it needs hints to work)\r
+  if (PicoAHW & PAHW_SVP) {\r
+    Pico.ram[0xd864^1] = 1;\r
+    pv->pending_ints|=0x10;\r
+    if (pv->reg[0]&0x10) SekInterrupt(4);\r
+    SekRunM68k(160);\r
   }\r
 \r
   // render screen\r
-  if(!PicoSkipFrame) {\r
-    if(!(PicoOpt&0x10))\r
+  if (!PicoSkipFrame)\r
+  {\r
+    if (!(PicoOpt&POPT_ALT_RENDERER))\r
+    {\r
       // Draw the screen\r
 #if CAN_HANDLE_240_LINES\r
-      if(pv->reg[1]&8) {\r
+      if (pv->reg[1]&8) {\r
         for (y=0;y<240;y++) PicoLine(y);\r
       } else {\r
         for (y=0;y<224;y++) PicoLine(y);\r
@@ -575,52 +423,74 @@ static int PicoFrameSimple(void)
 #else\r
       for (y=0;y<224;y++) PicoLine(y);\r
 #endif\r
+    }\r
     else PicoFrameFull();\r
+#ifdef DRAW_FINISH_FUNC\r
+    DRAW_FINISH_FUNC();\r
+#endif\r
+  }\r
+\r
+  // a gap between flags set and vint\r
+  pv->pending_ints|=0x20;\r
+  pv->status|=8; // go into vblank\r
+  SekRunM68k(68+4);\r
+\r
+  if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))\r
+    PicoSyncZ80(SekCycleCnt);\r
+\r
+  // render sound\r
+  if (PsndOut)\r
+  {\r
+    int len;\r
+    if (ym2612.dacen && PsndDacLine <= lines_step*16)\r
+      PsndDoDAC(lines_step*16);\r
+    len = PsndRender(0, PsndLen);\r
+    if (PicoWriteSound) PicoWriteSound(len);\r
+    // clear sound buffer\r
+    PsndClear();\r
   }\r
 \r
   // ---- V-Blanking period ----\r
   // fix line counts\r
   if(Pico.m.pal) {\r
     if(pv->reg[1]&8) { // 240 lines\r
-      lines = line = 240;\r
       sects = 3;\r
       lines_step = 24;\r
     } else {\r
-      lines = line = 224;\r
       sects = 4;\r
       lines_step = 22;\r
     }\r
   } else {\r
-    lines = line = 224;\r
     sects = 2;\r
     lines_step = 19;\r
   }\r
 \r
-  //dprintf("vint: @ %06x [%i]", SekPc, SekCycleCnt);\r
-  pv->pending_ints|=0x20;\r
   if (pv->reg[1]&0x20) SekInterrupt(6); // Set IRQ\r
-  pv->status|=0x88; // V-Int happened / go into vblank\r
-  if(Pico.m.z80Run && (PicoOpt&4)) // ?\r
+  if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))\r
     z80_int();\r
 \r
-  while(sects) {\r
-    lines += lines_step;\r
-\r
-    SekRun(cycles_68k_vblock);\r
-\r
-    PicoRunZ80Simple(line, lines);\r
-    line=lines;\r
+  while (1)\r
+  {\r
+    SekRunM68k(cycles_68k_vblock);\r
+    if (PicoLineHook) PicoLineHook(lines_step);\r
 \r
     sects--;\r
-    if(sects && CheckIdle()) break;\r
+    if (sects == 0) break;\r
+    if (CheckIdle()) break;\r
   }\r
 \r
-  // run Z80 for remaining sections\r
-  if(sects) {\r
-    lines += sects*lines_step;\r
-    PicoRunZ80Simple(line, lines);\r
+  if (sects) {\r
+    SekCycleCnt += sects * cycles_68k_vblock;\r
+    SekCycleAim += sects * cycles_68k_vblock;\r
+    if (PicoLineHook) PicoLineHook(sects*lines_step);\r
   }\r
 \r
+  // must sync z80 before return, and extend last DAC sample\r
+  if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))\r
+    PicoSyncZ80(SekCycleCnt);\r
+  if (PsndOut && ym2612.dacen && PsndDacLine <= line_last)\r
+    PsndDoDAC(line_last);\r
+\r
   return 0;\r
 }\r
 \r
@@ -630,23 +500,23 @@ int PicoFrame(void)
 \r
   Pico.m.frame_count++;\r
 \r
-  if (PicoMCD & 1) {\r
+  if (PicoAHW & PAHW_MCD) {\r
     PicoFrameMCD();\r
     return 0;\r
   }\r
 \r
   // be accurate if we are asked for this\r
-  if(PicoOpt&0x40) acc=1;\r
+  if (PicoOpt&POPT_ACC_TIMING) acc=1;\r
   // don't be accurate in alternative render mode, as hint effects will not be rendered anyway\r
-  else if(PicoOpt&0x10) acc = 0;\r
+  else if (PicoOpt&POPT_ALT_RENDERER) acc = 0;\r
   else acc=Pico.video.reg[0]&0x10; // be accurate if hints are used\r
 \r
   //if(Pico.video.reg[12]&0x2) Pico.video.status ^= 0x10; // change odd bit in interlace mode\r
 \r
-  if(!(PicoOpt&0x10))\r
+  if (!(PicoOpt&POPT_ALT_RENDERER))\r
     PicoFrameStart();\r
 \r
-  if(acc)\r
+  if (acc)\r
        PicoFrameHints();\r
   else PicoFrameSimple();\r
 \r
@@ -660,11 +530,21 @@ void PicoFrameDrawOnly(void)
   for (y=0;y<224;y++) PicoLine(y);\r
 }\r
 \r
+void PicoGetInternal(pint_t which, pint_ret_t *r)\r
+{\r
+  switch (which)\r
+  {\r
+    case PI_ROM:         r->vptr = Pico.rom; break;\r
+    case PI_ISPAL:       r->vint = Pico.m.pal; break;\r
+    case PI_IS40_CELL:   r->vint = Pico.video.reg[12]&1; break;\r
+    case PI_IS240_LINES: r->vint = Pico.m.pal && (Pico.video.reg[1]&8); break;\r
+  }\r
+}\r
+\r
 // callback to output message from emu\r
 void (*PicoMessage)(const char *msg)=NULL;\r
 \r
-#if defined(__DEBUG_PRINT) || defined(__GP2X__)\r
-// tmp debug: dump some stuff\r
+#if 1 // defined(__DEBUG_PRINT)\r
 #define bit(r, x) ((r>>x)&1)\r
 void z80_debug(char *dstr);\r
 char *debugString(void)\r
@@ -687,18 +567,23 @@ char *debugString(void)
   sprintf(dstrp, "mode set 4: %02x\n", (r=reg[0xC])); dstrp+=strlen(dstrp);\r
   sprintf(dstrp, "interlace: %i%i, cells: %i, shadow: %i\n", bit(r,2), bit(r,1), (r&0x80) ? 40 : 32,  bit(r,3));\r
   dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "scroll size: w: %i, h: %i  SRAM: %i; eeprom: %i\n", reg[0x10]&3, (reg[0x10]&0x30)>>4,\r
-       bit(Pico.m.sram_reg, 4), bit(Pico.m.sram_reg, 2)); dstrp+=strlen(dstrp);\r
+  sprintf(dstrp, "scroll size: w: %i, h: %i  SRAM: %i; eeprom: %i (%i)\n", reg[0x10]&3, (reg[0x10]&0x30)>>4,\r
+       bit(Pico.m.sram_reg, 4), bit(Pico.m.sram_reg, 2), SRam.eeprom_type); dstrp+=strlen(dstrp);\r
+  sprintf(dstrp, "sram range: %06x-%06x, reg: %02x\n", SRam.start, SRam.end, Pico.m.sram_reg); dstrp+=strlen(dstrp);\r
   sprintf(dstrp, "pend int: v:%i, h:%i, vdp status: %04x\n", bit(pv->pending_ints,5), bit(pv->pending_ints,4), pv->status);\r
   dstrp+=strlen(dstrp);\r
-#ifdef EMU_C68K\r
-  sprintf(dstrp, "M68k: PC: %06x, st_flg: %x, cycles: %u\n", SekPc, PicoCpu.state_flags, SekCyclesDoneT());\r
+#if defined(EMU_C68K)\r
+  sprintf(dstrp, "M68k: PC: %06x, st_flg: %x, cycles: %u\n", SekPc, PicoCpuCM68k.state_flags, SekCyclesDoneT());\r
   dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "d0=%08x, a0=%08x, osp=%08x, irql=%i\n", PicoCpu.d[0], PicoCpu.a[0], PicoCpu.osp, PicoCpu.irq); dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "d1=%08x, a1=%08x,  sr=%04x\n", PicoCpu.d[1], PicoCpu.a[1], CycloneGetSr(&PicoCpu)); dstrp+=strlen(dstrp);\r
+  sprintf(dstrp, "d0=%08x, a0=%08x, osp=%08x, irql=%i\n", PicoCpuCM68k.d[0], PicoCpuCM68k.a[0], PicoCpuCM68k.osp, PicoCpuCM68k.irq); dstrp+=strlen(dstrp);\r
+  sprintf(dstrp, "d1=%08x, a1=%08x,  sr=%04x\n", PicoCpuCM68k.d[1], PicoCpuCM68k.a[1], CycloneGetSr(&PicoCpuCM68k)); dstrp+=strlen(dstrp);\r
   for(r=2; r < 8; r++) {\r
-    sprintf(dstrp, "d%i=%08x, a%i=%08x\n", r, PicoCpu.d[r], r, PicoCpu.a[r]); dstrp+=strlen(dstrp);\r
+    sprintf(dstrp, "d%i=%08x, a%i=%08x\n", r, PicoCpuCM68k.d[r], r, PicoCpuCM68k.a[r]); dstrp+=strlen(dstrp);\r
   }\r
+#elif defined(EMU_M68K)\r
+  sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuMM68k.int_level>>8); dstrp+=strlen(dstrp);\r
+#elif defined(EMU_F68K)\r
+  sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuFM68k.interrupts[0]); dstrp+=strlen(dstrp);\r
 #endif\r
   sprintf(dstrp, "z80Run: %i, pal: %i, frame#: %i\n", Pico.m.z80Run, Pico.m.pal, Pico.m.frame_count); dstrp+=strlen(dstrp);\r
   z80_debug(dstrp); dstrp+=strlen(dstrp);\r
@@ -734,21 +619,13 @@ char *debugString(void)
     code2 = sprite[1];\r
     sx = (code2>>16)&0x1ff;\r
 \r
-    dprintf("#%02i x: %03i y: %03i %ix%i", u, sx, sy, ((code>>26)&3)+1, height);\r
+    printf("#%02i x: %03i y: %03i %ix%i\n", u, sx, sy, ((code>>26)&3)+1, height);\r
 \r
     link=(code>>16)&0x7f;\r
     if(!link) break; // End of sprites\r
   }\r
 #endif\r
 \r
-#if 0\r
-  {\r
-    FILE *f = fopen("zram", "wb");\r
-    fwrite(Pico.zram, 1, 0x2000, f);\r
-    fclose(f);\r
-  }\r
-#endif\r
-\r
   return dstr;\r
 }\r
 #endif\r