svp compiler, early stage, works
[picodrive.git] / Pico / Pico.c
index ce3e340..0e9ee6c 100644 (file)
@@ -17,7 +17,9 @@ int PicoSkipFrame=0; // skip rendering frame?
 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
-void (*PicoWriteSound)(int len) = 0; // called once per frame at the best time to send sound buffer (PsndOut) to hardware\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
 struct PicoSRAM SRam = {0,};\r
 int z80startCycle, z80stopCycle; // in 68k cycles\r
@@ -137,7 +139,9 @@ int PicoReset(int hard)
   Pico.m.pal=pal;\r
   Pico.video.status = 0x3408 | pal; // always set bits | vblank | pal\r
 \r
-  sound_reset(); // pal must be known here\r
+  PsndReset(); // pal must be known here\r
+\r
+  if (PicoResetHook) PicoResetHook();\r
 \r
   if (PicoMCD & 1) {\r
     PicoResetMCD(hard);\r
@@ -202,12 +206,10 @@ static __inline void SekRunM68k(int cyc)
 {\r
   int cyc_do;\r
   SekCycleAim+=cyc;\r
-  //printf("aim: %i, cnt: %i\n", SekCycleAim, SekCycleCnt);\r
   if((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;\r
-  //printf("cyc_do: %i\n", 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
   PicoCpuCM68k.cycles=cyc_do;\r
   CycloneRun(&PicoCpuCM68k);\r
@@ -215,7 +217,7 @@ static __inline void SekRunM68k(int cyc)
 #elif defined(EMU_M68K)\r
   SekCycleCnt+=m68k_execute(cyc_do);\r
 #elif defined(EMU_F68K)\r
-  SekCycleCnt+=m68k_emulate(cyc_do);\r
+  SekCycleCnt+=fm68k_emulate(cyc_do+1, 0);\r
 #endif\r
 }\r
 \r
@@ -223,9 +225,8 @@ 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
   PicoCpuCM68k.cycles=1;\r
   CycloneRun(&PicoCpuCM68k);\r
@@ -233,7 +234,7 @@ static __inline void SekStep(void)
 #elif defined(EMU_M68K)\r
   SekCycleCnt+=m68k_execute(1);\r
 #elif defined(EMU_F68K)\r
-  SekCycleCnt+=m68k_emulate(1);\r
+  SekCycleCnt+=fm68k_emulate(1, 0);\r
 #endif\r
   SekCycleAim=realaim;\r
 }\r
@@ -254,22 +255,29 @@ 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
     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
 \r
@@ -296,14 +304,14 @@ static void PicoRunZ80Simple(int line_from, int line_to)
   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_from; line < line_to; line++) {\r
-      sound_timers_and_dac(line);\r
+      Psnd_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
+        z80_run_nr(228);\r
     }\r
   } else if (line_to_r-line_from_r > 0) {\r
-    z80_run(228*(line_to_r-line_from_r));\r
+    z80_run_nr(228*(line_to_r-line_from_r));\r
     // samples will be taken by caller\r
   }\r
 }\r
@@ -316,7 +324,7 @@ static int PicoFrameSimple(void)
   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 && (pv->reg[1]&8)) { // 240 lines\r
+  if (Pico.m.pal && (pv->reg[1]&8)) {\r
     if(pv->reg[1]&8) { // 240 lines\r
       cycles_68k_block  = 7329;  // (488*240+148)/16.0, -4\r
       cycles_68k_vblock = 11640; // (72*488-148-68)/3.0, 0\r
@@ -363,6 +371,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
@@ -376,14 +385,7 @@ static int PicoFrameSimple(void)
 \r
     lines += sects*lines_step;\r
     PicoRunZ80Simple(line, lines);\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
+    if (PicoLineHook) PicoLineHook(sects*lines_step);\r
   }\r
 \r
   // render screen\r
@@ -401,6 +403,17 @@ static int PicoFrameSimple(void)
       for (y=0;y<224;y++) PicoLine(y);\r
 #endif\r
     else PicoFrameFull();\r
+#ifdef DRAW_FINISH_FUNC\r
+    DRAW_FINISH_FUNC();\r
+#endif\r
+  }\r
+\r
+  // here we render sound if ym2612 is disabled\r
+  if (!(PicoOpt&1) && PsndOut) {\r
+    int len = PsndRender(0, PsndLen);\r
+    if (PicoWriteSound) PicoWriteSound(len);\r
+    // clear sound buffer\r
+    PsndClear();\r
   }\r
 \r
   // a gap between flags set and vint\r
@@ -436,6 +449,7 @@ static int PicoFrameSimple(void)
     SekRunM68k(cycles_68k_vblock);\r
 \r
     PicoRunZ80Simple(line, lines);\r
+    if (PicoLineHook) PicoLineHook(lines_step);\r
     line=lines;\r
 \r
     sects--;\r
@@ -446,6 +460,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
@@ -487,6 +502,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
@@ -573,14 +599,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