cd: switch to CD controller code from genplus
[picodrive.git] / pico / cd / mcd.c
index 8451a1c..3355361 100644 (file)
@@ -11,7 +11,9 @@
 
 extern unsigned char formatted_bram[4*0x10];
 
-static unsigned int m68k_cycle_mult;
+static unsigned int mcd_m68k_cycle_mult;
+static unsigned int mcd_m68k_cycle_base;
+static unsigned int mcd_s68k_cycle_base;
 
 void (*PicoMCDopenTray)(void) = NULL;
 void (*PicoMCDcloseTray)(void) = NULL;
@@ -44,6 +46,7 @@ PICO_INTERNAL void PicoPowerMCD(void)
   memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
   memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
 
+  cdc_init();
   Reset_CD();
 
   // cold reset state (tested)
@@ -57,7 +60,9 @@ void pcd_soft_reset(void)
 {
   // Reset_CD(); // breaks Fahrenheit CD swap
 
-  LC89510_Reset();
+  Pico_mcd->m.s68k_pend_ints = 0;
+  cdc_reset();
+  CDD_Reset();
 #ifdef _ASM_CD_MEMORY_C
   //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
 #endif
@@ -116,14 +121,14 @@ static void pcd_set_cycle_mult(void)
 {
   // ~1.63 for NTSC, ~1.645 for PAL
   if (Pico.m.pal)
-    m68k_cycle_mult = ((12500000ull << 16) / (50*312*488));
+    mcd_m68k_cycle_mult = ((12500000ull << 16) / (50*312*488));
   else
-    m68k_cycle_mult = ((12500000ull << 16) / (60*262*488)) + 1;
+    mcd_m68k_cycle_mult = ((12500000ull << 16) / (60*262*488)) + 1;
 }
 
 unsigned int pcd_cycles_m68k_to_s68k(unsigned int c)
 {
-  return (long long)c * m68k_cycle_mult >> 16;
+  return (long long)c * mcd_m68k_cycle_mult >> 16;
 }
 
 /* events */
@@ -148,8 +153,7 @@ static void pcd_int3_timer_event(unsigned int now)
 
 static void pcd_dma_event(unsigned int now)
 {
-  int ddx = Pico_mcd->s68k_regs[4] & 7;
-       Update_CDC_TRansfer(ddx);
+  cdc_dma_update();
 }
 
 typedef void (event_cb)(unsigned int now);
@@ -234,10 +238,13 @@ static void pcd_run_events(unsigned int until)
 int pcd_sync_s68k(unsigned int m68k_target, int m68k_poll_sync)
 {
   #define now SekCycleCntS68k
-  unsigned int s68k_target =
-    (unsigned long long)m68k_target * m68k_cycle_mult >> 16;
+  unsigned int s68k_target;
   unsigned int target;
 
+  target = m68k_target - mcd_m68k_cycle_base;
+  s68k_target = mcd_s68k_cycle_base +
+    ((unsigned long long)target * mcd_m68k_cycle_mult >> 16);
+
   elprintf(EL_CD, "s68k sync to %u, %u->%u",
     m68k_target, now, s68k_target);
 
@@ -307,12 +314,21 @@ void pcd_run_cpus_lockstep(int m68k_cycles)
 #include "../pico_cmn.c"
 
 
+void pcd_prepare_frame(void)
+{
+  pcd_set_cycle_mult();
+
+  // need this because we can't have direct mapping between
+  // master<->slave cycle counters because of overflows
+  mcd_m68k_cycle_base = SekCycleAim;
+  mcd_s68k_cycle_base = SekCycleAimS68k;
+}
+
 PICO_INTERNAL void PicoFrameMCD(void)
 {
-  if (!(PicoOpt&POPT_ALT_RENDERER))
-    PicoFrameStart();
+  PicoFrameStart();
 
-  pcd_set_cycle_mult();
+  pcd_prepare_frame();
   PicoFrameHints();
 }
 
@@ -341,9 +357,6 @@ void pcd_state_loaded(void)
     if (Pico_mcd->s68k_regs[0x31])
       pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_TIMER3,
         Pico_mcd->s68k_regs[0x31] * 384);
-
-    if (Pico_mcd->scd.Status_CDC & 0x08)
-           Update_CDC_TRansfer(Pico_mcd->s68k_regs[4] & 7);
   }
 
   diff = cycles - Pico_mcd->pcm.update_cycles;