rework sh2 sync, again..
[picodrive.git] / pico / 32x / pwm.c
index 9475c00..36261d7 100644 (file)
@@ -1,16 +1,28 @@
+/*
+ * PicoDrive
+ * (C) notaz, 2009,2010
+ *
+ * This work is licensed under the terms of MAME license.
+ * See COPYING file in the top-level directory.
+ */
 #include "../pico_int.h"
 
-static int pwm_line_samples;
+static int pwm_cycle_counter;
 static int pwm_cycles;
 static int pwm_mult;
 static int pwm_ptr;
-int pwm_frame_smp_cnt;
 
+static int pwm_smp_cnt;
+static int pwm_smp_expect;
 
-void p32x_pwm_refresh(void)
+static int timer_cycles[2];
+static int timer_tick_cycles[2];
+
+// timers. This includes PWM timer in 32x and internal SH2 timers
+void p32x_timers_recalc(void)
 {
   int cycles = Pico32x.regs[0x32 / 2];
-  int frame_samples;
+  int tmp, i;
 
   cycles = (cycles - 1) & 0x0fff;
   if (cycles < 500) {
@@ -19,35 +31,96 @@ void p32x_pwm_refresh(void)
   }
   pwm_cycles = cycles;
   pwm_mult = 0x10000 / cycles;
-  if (Pico.m.pal)
-    frame_samples = OSC_PAL / 7 * 3 / 50 / cycles;
-  else
-    frame_samples = OSC_NTSC / 7 * 3 / 60 / cycles;
 
-  pwm_line_samples = (frame_samples << 16) / scanlines_total;
+  // SH2 timer step
+  for (i = 0; i < 2; i++) {
+    tmp = PREG8(Pico32xMem->sh2_peri_regs[i], 0x80) & 7;
+    // Sclk cycles per timer tick
+    if (tmp)
+      cycles = 0x20 << tmp;
+    else
+      cycles = 2;
+    timer_tick_cycles[i] = cycles;
+    elprintf(EL_32X, "WDT cycles[%d] = %d", i, cycles);
+  }
 }
 
-// irq for every sample??
-// FIXME: we need to hit more than once per line :(
-void p32x_pwm_irq_check(int new_line)
+// PWM irq for every tm samples
+void p32x_timers_do(unsigned int cycles)
 {
-  int tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
-  if (tm == 0)
-    return; // TODO: verify
-
-  if (new_line)
-    Pico32x.pwm_irq_sample_cnt += pwm_line_samples;
-  if (Pico32x.pwm_irq_sample_cnt >= (tm << 16)) {
-    Pico32x.pwm_irq_sample_cnt -= tm << 16;
-    Pico32x.sh2irqs |= P32XI_PWM;
-    p32x_update_irls();
+  int cnt, i;
+
+  cycles *= 3;
+
+  // since we run things in async fashion, allow pwm to lag behind
+  // but don't allow our "queue" to be infinite
+  cnt = pwm_smp_expect - pwm_smp_cnt;
+  if (cnt <= 0 || cnt * pwm_cycles < OSC_NTSC/7*3 / 60 / 2) {
+    pwm_cycle_counter += cycles;
+    while (pwm_cycle_counter > pwm_cycles) {
+      pwm_cycle_counter -= pwm_cycles;
+      pwm_smp_expect++;
+    }
   }
+
+  // WDT timers
+  for (i = 0; i < 2; i++) {
+    void *pregs = Pico32xMem->sh2_peri_regs[i];
+    if (PREG8(pregs, 0x80) & 0x20) { // TME
+      timer_cycles[i] += cycles;
+      cnt = PREG8(pregs, 0x81);
+      while (timer_cycles[i] >= timer_tick_cycles[i]) {
+        timer_cycles[i] -= timer_tick_cycles[i];
+        cnt++;
+      }
+      if (cnt >= 0x100) {
+        int level = PREG8(pregs, 0xe3) >> 4;
+        int vector = PREG8(pregs, 0xe4) & 0x7f;
+        elprintf(EL_32X, "%csh2 WDT irq (%d, %d)",
+          i ? 's' : 'm', level, vector);
+        sh2_internal_irq(&sh2s[i], level, vector);
+        cnt &= 0xff;
+      }
+      PREG8(pregs, 0x81) = cnt;
+    }
+  }
+}
+
+static int p32x_pwm_schedule_(void)
+{
+  int tm;
+
+  if (Pico32x.emu_flags & P32XF_PWM_PEND)
+    return 0; // already scheduled
+  if (Pico32x.sh2irqs & P32XI_PWM)
+    return 0; // previous not acked
+  if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
+    return 0; // masked by everyone
+
+  Pico32x.emu_flags |= P32XF_PWM_PEND;
+  tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
+  tm = ((tm - 1) & 0x0f) + 1;
+  return pwm_cycles * tm / 3;
+}
+
+void p32x_pwm_schedule(unsigned int now)
+{
+  int after = p32x_pwm_schedule_();
+  if (after != 0)
+    p32x_event_schedule(now, P32X_EVENT_PWM, after);
+}
+
+void p32x_pwm_schedule_sh2(SH2 *sh2)
+{
+  int after = p32x_pwm_schedule_();
+  if (after != 0)
+    p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
 }
 
 unsigned int p32x_pwm_read16(unsigned int a)
 {
   unsigned int d = 0;
-  int predict;
+  int diff;
 
   a &= 0x0e;
   switch (a) {
@@ -58,12 +131,12 @@ unsigned int p32x_pwm_read16(unsigned int a)
     case 4: // L ch
     case 6: // R ch
     case 8: // MONO
-      predict = (pwm_line_samples * Pico.m.scanline) >> 16;
-      elprintf(EL_PWM, "pwm: read status: ptr %d/%d, predict %d",
-        pwm_frame_smp_cnt, (pwm_line_samples * scanlines_total) >> 16, predict);
-      if (pwm_frame_smp_cnt > predict + 3)
+      diff = pwm_smp_cnt - pwm_smp_expect;
+      elprintf(EL_PWM, "pwm: read status: ptr %d/%d %d",
+        pwm_smp_cnt, pwm_smp_expect, diff);
+      if (diff > 3)
         d |= P32XP_FULL;
-      else if (pwm_frame_smp_cnt == 0 || pwm_frame_smp_cnt < predict - 1)
+      else if (diff < 0)
         d |= P32XP_EMPTY;
       break;
   }
@@ -78,7 +151,7 @@ void p32x_pwm_write16(unsigned int a, unsigned int d)
     Pico32x.regs[0x30 / 2] = d;
   else if (a == 2) { // cycle
     Pico32x.regs[0x32 / 2] = d & 0x0fff;
-    p32x_pwm_refresh();
+    p32x_timers_recalc();
     Pico32x.pwm_irq_sample_cnt = 0; // resets?
   }
   else if (a <= 8) {
@@ -95,16 +168,16 @@ void p32x_pwm_write16(unsigned int a, unsigned int d)
       Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
 
     if (a >= 6) { // R or MONO
-      pwm_frame_smp_cnt++;
+      pwm_smp_cnt++;
       pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
-        elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x", pwm_frame_smp_cnt, pwm_ptr, d);
+      elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x",
+          pwm_smp_cnt, pwm_ptr, d);
     }
   }
 }
 
 void p32x_pwm_update(int *buf32, int length, int stereo)
 {
-  extern int pwm_ptr;
   short *pwmb;
   int step;
   int p = 0;
@@ -143,3 +216,4 @@ void p32x_pwm_update(int *buf32, int length, int stereo)
   pwm_ptr = 0;
 }
 
+// vim:shiftwidth=2:ts=2:expandtab