step-frame added to debug
[picodrive.git] / Pico / Pico.c
index 93d04e7..5684e99 100644 (file)
@@ -1,7 +1,7 @@
-// This is part of Pico Library\r
+// PicoDrive\r
 \r
 // (c) Copyright 2004 Dave, All rights reserved.\r
-// (c) Copyright 2006 notaz, All rights reserved.\r
+// (c) Copyright 2006-2008 notaz, All rights reserved.\r
 // Free for non-commercial use.\r
 \r
 // For commercial use, separate licencing terms must be obtained.\r
@@ -16,6 +16,7 @@ int PicoOpt = 0;
 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 PicoPadInt[2];     // internal copy\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
@@ -23,14 +24,15 @@ struct PicoSRAM SRam = {0,};
 \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
+void (*PicoLineHook)(void) = NULL;\r
 \r
 // to be called once on emu init\r
-int PicoInit(void)\r
+void PicoInit(void)\r
 {\r
   // Blank space for state:\r
   memset(&Pico,0,sizeof(Pico));\r
   memset(&PicoPad,0,sizeof(PicoPad));\r
+  memset(&PicoPadInt,0,sizeof(PicoPadInt));\r
 \r
   // Init CPUs:\r
   SekInit();\r
@@ -40,8 +42,6 @@ int PicoInit(void)
   PicoSVPInit();\r
 \r
   SRam.data=0;\r
-\r
-  return 0;\r
 }\r
 \r
 // to be called once on emu exit\r
@@ -58,6 +58,8 @@ void PicoPower(void)
 {\r
   unsigned char sram_reg=Pico.m.sram_reg; // must be preserved\r
 \r
+  Pico.m.frame_count = 0;\r
+\r
   // clear all memory of the emulated machine\r
   memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram);\r
 \r
@@ -145,6 +147,7 @@ int PicoReset(void)
 \r
   PicoMemReset();\r
   SekReset();\r
+  memset(&PicoPadInt,0,sizeof(PicoPadInt));\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
@@ -156,8 +159,11 @@ int PicoReset(void)
 \r
   Pico.m.dirtyPal = 1;\r
 \r
+  Pico.m.z80_bank68k = 0;\r
+  memset(Pico.zram, 0, sizeof(Pico.zram)); // ??\r
+\r
   PicoDetectRegion();\r
-  Pico.video.status = 0x3408 | Pico.m.pal; // 'always set' bits | vblank | pal\r
+  Pico.video.status = 0x3428 | Pico.m.pal; // 'always set' bits | vblank | collision | pal\r
 \r
   PsndReset(); // pal must be known here\r
 \r
@@ -169,6 +175,12 @@ int PicoReset(void)
     PicoResetMCD();\r
     return 0;\r
   }\r
+  else {\r
+    // reinit, so that checksum checks pass\r
+    SekFinishIdleDet();\r
+    if (!(PicoOpt & POPT_DIS_IDLE_DET))\r
+      SekInitIdleDet();\r
+  }\r
 \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
@@ -185,17 +197,17 @@ int PicoReset(void)
 // 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
-16,   16,  15,   8, // active: 32cell:\r
-24,   18,  17,   9  // ...\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
 };\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)/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
 };\r
 \r
 PICO_INTERNAL int CheckDMA(void)\r
@@ -209,7 +221,8 @@ PICO_INTERNAL int CheckDMA(void)
   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
   xfers_can = dma_timings[dma_op];\r
-  if(xfers <= xfers_can) {\r
+  if(xfers <= xfers_can)\r
+  {\r
     if(dma_op&2) Pico.video.status&=~2; // dma no longer busy\r
     else {\r
       burn = xfers * dma_bsycles[dma_op] >> 8; // have to be approximate because can't afford division..\r
@@ -229,7 +242,7 @@ static __inline void SekRunM68k(int cyc)
 {\r
   int cyc_do;\r
   SekCycleAim+=cyc;\r
-  if((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;\r
+  if ((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;\r
 #if defined(EMU_CORE_DEBUG)\r
   // this means we do run-compare\r
   SekCycleCnt+=CM_compareRun(cyc_do, 0);\r
@@ -240,38 +253,8 @@ static __inline void SekRunM68k(int cyc)
 #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
-static __inline void SekStep(void)\r
-{\r
-  // this is required for timing sensitive stuff to work\r
-  int realaim=SekCycleAim; SekCycleAim=SekCycleCnt+1;\r
-#if defined(EMU_CORE_DEBUG)\r
-  SekCycleCnt+=CM_compareRun(1, 0);\r
-#elif defined(EMU_C68K)\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
+  SekCycleCnt+=fm68k_emulate(cyc_do+1, 0, 0);\r
 #endif\r
-  SekCycleAim=realaim;\r
-}\r
-\r
-static int CheckIdle(void)\r
-{\r
-  int i, state[0x24];\r
-\r
-  // See if the state is the same after 2 steps:\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 1;\r
 }\r
 \r
 \r
@@ -320,7 +303,7 @@ PICO_INTERNAL void PicoSyncZ80(int m68k_cycles_done)
   z80_cycle_aim = cycles_68k_to_z80(m68k_cycles_done);\r
   cnt = z80_cycle_aim - z80_cycle_cnt;\r
 \r
-  elprintf(EL_ANOMALY, "z80 sync %i (%i|%i -> %i|%i)", cnt, z80_cycle_cnt, z80_cycle_cnt / 228,\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 (cnt > 0)\r
@@ -328,206 +311,27 @@ PICO_INTERNAL void PicoSyncZ80(int m68k_cycles_done)
 }\r
 \r
 \r
-// Simple frame without H-Ints\r
-static int PicoFrameSimple(void)\r
+void PicoFrame(void)\r
 {\r
-  struct PicoVideo *pv=&Pico.video;\r
-  int y=0,lines_step=0,sects,line_last;\r
-  int cycles_68k_vblock,cycles_68k_block;\r
-\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  = 7308;\r
-      cycles_68k_vblock = 11694;\r
-      lines_step = 15;\r
-    } else {\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  = 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
-  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&=~0x88; // clear V-Int, come out of vblank\r
-\r
-  // Run in sections:\r
-  for (sects=16; sects; sects--)\r
-  {\r
-    if (CheckIdle()) break;\r
-\r
-    SekRunM68k(cycles_68k_block);\r
-    if (PicoLineHook) PicoLineHook(lines_step);\r
-  }\r
-\r
-  // do remaining sections without 68k\r
-  if (sects) {\r
-    SekCycleCnt += sects * cycles_68k_block;\r
-    SekCycleAim += sects * cycles_68k_block;\r
-\r
-    if (PicoLineHook) PicoLineHook(sects*lines_step);\r
-  }\r
-\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
-  {\r
-    if (!(PicoOpt&POPT_ALT_RENDERER))\r
-    {\r
-      // Draw the screen\r
-#if CAN_HANDLE_240_LINES\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
-      }\r
-#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
-      sects = 3;\r
-      lines_step = 24;\r
-    } else {\r
-      sects = 4;\r
-      lines_step = 22;\r
-    }\r
-  } else {\r
-    sects = 2;\r
-    lines_step = 19;\r
-  }\r
-\r
-  if (pv->reg[1]&0x20) SekInterrupt(6); // Set IRQ\r
-  if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))\r
-    z80_int();\r
-\r
-  while (1)\r
-  {\r
-    SekRunM68k(cycles_68k_vblock);\r
-    if (PicoLineHook) PicoLineHook(lines_step);\r
-\r
-    sects--;\r
-    if (sects == 0) break;\r
-    if (CheckIdle()) break;\r
-  }\r
-\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
-int PicoFrame(void)\r
-{\r
-  int acc;\r
-\r
   Pico.m.frame_count++;\r
 \r
   if (PicoAHW & PAHW_MCD) {\r
     PicoFrameMCD();\r
-    return 0;\r
+    return;\r
   }\r
 \r
-  // be accurate if we are asked for this\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&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&POPT_ALT_RENDERER))\r
     PicoFrameStart();\r
 \r
-  if (acc)\r
-       PicoFrameHints();\r
-  else PicoFrameSimple();\r
-\r
-  return 0;\r
+  PicoFrameHints();\r
 }\r
 \r
 void PicoFrameDrawOnly(void)\r
 {\r
-  int y;\r
   PicoFrameStart();\r
-  for (y=0;y<224;y++) PicoLine(y);\r
+  PicoDrawSync(223, 0);\r
 }\r
 \r
 void PicoGetInternal(pint_t which, pint_ret_t *r)\r
@@ -544,88 +348,3 @@ void PicoGetInternal(pint_t which, pint_ret_t *r)
 // callback to output message from emu\r
 void (*PicoMessage)(const char *msg)=NULL;\r
 \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
-{\r
-#if 1\r
-  static char dstr[1024];\r
-  struct PicoVideo *pv=&Pico.video;\r
-  unsigned char *reg=pv->reg, r;\r
-  char *dstrp;\r
-\r
-  dstrp = dstr;\r
-  sprintf(dstrp, "mode set 1: %02x\n", (r=reg[0])); dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "display_disable: %i, M3: %i, palette: %i, ?, hints: %i\n", bit(r,0), bit(r,1), bit(r,2), bit(r,4));\r
-  dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "mode set 2: %02x\n", (r=reg[1])); dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "SMS/gen: %i, pal: %i, dma: %i, vints: %i, disp: %i, TMS: %i\n", bit(r,2), bit(r,3), bit(r,4),\r
-       bit(r,5), bit(r,6), bit(r,7)); dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "mode set 3: %02x\n", (r=reg[0xB])); dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "LSCR: %i, HSCR: %i, 2cell vscroll: %i, IE2: %i\n", bit(r,0), bit(r,1), bit(r,2), bit(r,3)); dstrp+=strlen(dstrp);\r
-  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 (%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
-#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", 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, 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
-  if (strlen(dstr) > sizeof(dstr))\r
-    printf("warning: debug buffer overflow (%i/%i)\n", strlen(dstr), sizeof(dstr));\r
-\r
-#else\r
-  struct PicoVideo *pvid=&Pico.video;\r
-  int table=0;\r
-  int i,u,n,link=0;\r
-  static char dstr[1024*8];\r
-  dstr[0] = 0;\r
-\r
-  table=pvid->reg[5]&0x7f;\r
-  if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
-  table<<=8; // Get sprite table address/2\r
-\r
-  for (i=u=n=0; u < 80 && n < 20; u++)\r
-  {\r
-    unsigned int *sprite;\r
-    int code, code2, sx, sy, height;\r
-\r
-    sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
-\r
-    // get sprite info\r
-    code = sprite[0];\r
-\r
-    // check if it is on this line\r
-    sy = (code&0x1ff);//-0x80;\r
-    height = ((code>>24)&3)+1;\r
-\r
-    // masking sprite?\r
-    code2 = sprite[1];\r
-    sx = (code2>>16)&0x1ff;\r
-\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
-  return dstr;\r
-}\r
-#endif\r