cd: tweak the poll code further
[picodrive.git] / pico / cd / mcd.c
index 82ae1bc..0824ec0 100644 (file)
@@ -35,19 +35,22 @@ PICO_INTERNAL void PicoPowerMCD(void)
   memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
   memset(Pico_mcd->pcm_ram,    0, sizeof(Pico_mcd->pcm_ram));
   memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
-  memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size);
-}
-
-PICO_INTERNAL int PicoResetMCD(void)
-{
+  memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size,
+    formatted_bram, fmt_size);
   memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
   memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
   memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
 
-  memset(Pico_mcd->bios + 0x70, 0xff, 4); // reset hint vector (simplest way to implement reg6)
-  Pico_mcd->m.state_flags = 0;
-  Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
+  // cold reset state (tested)
+  Pico_mcd->m.state_flags = PCD_ST_S68K_RST;
+  Pico_mcd->m.busreq = 2;     // busreq on, s68k in reset
+  Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode, m68k access
+  memset(Pico_mcd->bios + 0x70, 0xff, 4);
+}
 
+PICO_INTERNAL int PicoResetMCD(void)
+{
+  // ??
   Reset_CD();
   LC89510_Reset();
   gfx_cd_reset();
@@ -79,6 +82,9 @@ static __inline void SekRunS68k(unsigned int to)
   if ((cyc_do = SekCycleAimS68k - SekCycleCntS68k) <= 0)
     return;
 
+  if (SekShouldInterrupt())
+    Pico_mcd->m.s68k_poll_a = 0;
+
   SekCycleCntS68k += cyc_do;
 #if defined(EMU_C68K)
   PicoCpuCS68k.cycles = cyc_do;
@@ -229,7 +235,7 @@ int pcd_sync_s68k(unsigned int m68k_target, int m68k_poll_sync)
   elprintf(EL_CD, "s68k sync to %u, %u->%u",
     m68k_target, now, s68k_target);
 
-  if ((Pico_mcd->m.busreq & 3) != 1) { /* busreq/reset */
+  if (Pico_mcd->m.busreq != 1) { /* busreq/reset */
     SekCycleCntS68k = SekCycleAimS68k = s68k_target;
     pcd_run_events(m68k_target);
     return 0;
@@ -260,7 +266,9 @@ static void SekSyncM68k(void);
 static inline void pcd_run_cpus_normal(int m68k_cycles)
 {
   SekCycleAim += m68k_cycles;
-  if (Pico_mcd->m.m68k_poll_cnt >= 16 && !SekShouldInterrupt()) {
+  if (SekShouldInterrupt() || Pico_mcd->m.m68k_poll_cnt < 12)
+    Pico_mcd->m.m68k_poll_cnt = 0;
+  else if (Pico_mcd->m.m68k_poll_cnt >= 16) {
     int s68k_left = pcd_sync_s68k(SekCycleAim, 1);
     if (s68k_left <= 0) {
       elprintf(EL_CDPOLL, "m68k poll [%02x] x%d @%06x",
@@ -282,6 +290,8 @@ static inline void pcd_run_cpus_lockstep(int m68k_cycles)
     SekSyncM68k();
     pcd_sync_s68k(SekCycleAim, 0);
   } while (CYCLES_GT(target, SekCycleAim));
+
+  SekCycleAim = target;
 }
 
 #define PICO_CD