work on 'vblank on line start' problem, var changes, mask defines
[picodrive.git] / Pico / Pico.c
index 6d9d525..2edc8ee 100644 (file)
 \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\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; // rapid_ym2612, multi_ym_updates\r
+int z80startCycle, z80stopCycle; // in 68k cycles\r
+struct PicoSRAM SRam = {0,};\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
-void (*PicoLineHook)(void) = NULL;\r
-\r
-struct PicoSRAM SRam = {0,};\r
-int z80startCycle, z80stopCycle; // in 68k cycles\r
-int PicoPad[2];  // Joypads, format is SACB RLDU\r
-int PicoMCD = 0; // mega CD status: scd_started\r
+void (*PicoLineHook)(int count) = NULL;\r
 \r
 // to be called once on emu init\r
 int PicoInit(void)\r
@@ -38,6 +38,7 @@ int PicoInit(void)
   z80_init(); // init even if we aren't going to use it\r
 \r
   PicoInitMCD();\r
+  PicoSVPInit();\r
 \r
   SRam.data=0;\r
 \r
@@ -47,14 +48,36 @@ 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
-  if(SRam.data) free(SRam.data); SRam.data=0;\r
+  if (SRam.data) free(SRam.data); SRam.data=0;\r
+}\r
+\r
+void PicoPower(void)\r
+{\r
+  // clear all memory of the emulated machine\r
+  memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram);\r
+\r
+  memset(&Pico.video,0,sizeof(Pico.video));\r
+  memset(&Pico.m,0,sizeof(Pico.m));\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
+\r
+  if (PicoAHW & PAHW_MCD)\r
+    PicoPowerMCD();\r
+\r
+  PicoReset();\r
 }\r
 \r
-int PicoReset(int hard)\r
+int PicoReset(void)\r
 {\r
   unsigned int region=0;\r
   int support=0,hw=0,i=0;\r
@@ -63,30 +86,20 @@ int PicoReset(int hard)
 \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
   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
+  SekSetRealTAS(PicoAHW & PAHW_MCD);\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.m,0,sizeof(Pico.m));\r
-  Pico.video.pending_ints=0;\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(hard) {\r
-    // clear all memory of the emulated machine\r
-    memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram);\r
-  }\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
@@ -137,14 +150,16 @@ 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
+  Pico.video.status = 0x3408 | pal; // 'always set' bits | vblank | pal\r
 \r
   PsndReset(); // pal must be known here\r
 \r
-  if (PicoResetHook) PicoResetHook();\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 (PicoMCD & 1) {\r
-    PicoResetMCD(hard);\r
+  if (PicoAHW & PAHW_MCD) {\r
+    PicoResetMCD();\r
     return 0;\r
   }\r
 \r
@@ -161,15 +176,16 @@ int PicoReset(int hard)
 \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
-83,  167, 166,  83, // vblank: 32cell: dma2vram dma2[vs|c]ram vram_fill vram_copy\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)/82,  (488<<8)/167, (488<<8)/166, (488<<8)/83,\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
@@ -241,12 +257,12 @@ static __inline void SekStep(void)
 \r
 static int CheckIdle(void)\r
 {\r
-  int i, state[0x22];\r
+  int i, state[0x24];\r
 \r
   // See if the state is the same after 2 steps:\r
-  SekState(state); SekStep(); SekStep(); SekState(state+0x11);\r
-  for (i = 0x10; i >= 0; i--)\r
-    if (state[i] != state[i+0x11]) return 0;\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
@@ -255,6 +271,12 @@ static int CheckIdle(void)
 // 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
@@ -271,6 +293,7 @@ static __inline void getSamples(int y)
     emustatus&=~1;\r
     curr_pos = PsndRender(0, PsndLen/2);\r
   }\r
+#endif\r
 }\r
 \r
 \r
@@ -283,7 +306,7 @@ static void PicoRunZ80Simple(int line_from, int line_to)
   int line_from_r=line_from, line_to_r=line_to, line=0;\r
   int line_sample = Pico.m.pal ? 68 : 93;\r
 \r
-  if (!(PicoOpt&4) || Pico.m.z80Run == 0) line_to_r = 0;\r
+  if (!(PicoOpt&POPT_EN_Z80) || Pico.m.z80Run == 0) line_to_r = 0;\r
   else {\r
     extern const unsigned short vcounts[];\r
     if (z80startCycle) {\r
@@ -294,7 +317,7 @@ static void PicoRunZ80Simple(int line_from, int line_to)
     z80startCycle = SekCyclesDone();\r
   }\r
 \r
-  if (PicoOpt&1) {\r
+  if (PicoOpt&POPT_EN_FM) {\r
     // we have ym2612 enabled, so we have to run Z80 in lines, so we could update DAC and timers\r
     for (line = line_from; line < line_to; line++) {\r
       Psnd_timers_and_dac(line);\r
@@ -334,6 +357,10 @@ static int PicoFrameSimple(void)
     lines_step = 14;\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
@@ -364,6 +391,7 @@ static int PicoFrameSimple(void)
     SekRunM68k(cycles_68k_block);\r
 \r
     PicoRunZ80Simple(line, lines);\r
+    if (PicoLineHook) PicoLineHook(lines_step);\r
     line=lines;\r
   }\r
 \r
@@ -377,12 +405,22 @@ static int PicoFrameSimple(void)
 \r
     lines += sects*lines_step;\r
     PicoRunZ80Simple(line, lines);\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&0x10))\r
+    if (!(PicoOpt&POPT_ALT_RENDERER))\r
+    {\r
       // Draw the screen\r
 #if CAN_HANDLE_240_LINES\r
       if (pv->reg[1]&8) {\r
@@ -393,6 +431,7 @@ 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
@@ -400,7 +439,7 @@ static int PicoFrameSimple(void)
   }\r
 \r
   // here we render sound if ym2612 is disabled\r
-  if (!(PicoOpt&1) && PsndOut) {\r
+  if (!(PicoOpt&POPT_EN_FM) && PsndOut) {\r
     int len = PsndRender(0, PsndLen);\r
     if (PicoWriteSound) PicoWriteSound(len);\r
     // clear sound buffer\r
@@ -431,15 +470,17 @@ static int PicoFrameSimple(void)
   }\r
 \r
   if (pv->reg[1]&0x20) SekInterrupt(6); // Set IRQ\r
-  if (Pico.m.z80Run && (PicoOpt&4))\r
+  if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))\r
     z80_int();\r
 \r
-  while (sects) {\r
+  while (sects)\r
+  {\r
     lines += lines_step;\r
 \r
     SekRunM68k(cycles_68k_vblock);\r
 \r
     PicoRunZ80Simple(line, lines);\r
+    if (PicoLineHook) PicoLineHook(lines_step);\r
     line=lines;\r
 \r
     sects--;\r
@@ -450,6 +491,7 @@ static int PicoFrameSimple(void)
   if (sects) {\r
     lines += sects*lines_step;\r
     PicoRunZ80Simple(line, lines);\r
+    if (PicoLineHook) PicoLineHook(sects*lines_step);\r
   }\r
 \r
   return 0;\r
@@ -461,23 +503,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
@@ -491,6 +533,17 @@ void PicoFrameDrawOnly(void)
   for (y=0;y<224;y++) PicoLine(y);\r
 }\r
 \r
+int PicoGetStat(pstat_t which)\r
+{\r
+  switch (which)\r
+  {\r
+    case PS_PAL:       return Pico.m.pal;\r
+    case PS_40_CELL:   return Pico.video.reg[12]&1;\r
+    case PS_240_LINES: return Pico.m.pal && (Pico.video.reg[1]&8);\r
+  }\r
+  return 0;\r
+}\r
+\r
 // callback to output message from emu\r
 void (*PicoMessage)(const char *msg)=NULL;\r
 \r
@@ -577,14 +630,6 @@ char *debugString(void)
   }\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