32x: improve interrupt handling
[picodrive.git] / pico / 32x / memory.c
index c6f3a31..a6c977b 100644 (file)
@@ -103,7 +103,7 @@ static void sh2_poll_detect(SH2 *sh2, u32 a, u32 flags, int maxcnt)
   if (a == sh2->poll_addr && sh2->poll_cycles - cycles_left <= 10) {
     if (sh2->poll_cnt++ > maxcnt) {
       if (!(sh2->state & flags))
-        elprintf(EL_32X, "%csh2 state: %02x->%02x", sh2->is_slave?'s':'m',
+        elprintf_sh2(sh2, EL_32X, "state: %02x->%02x",
           sh2->state, sh2->state | flags);
 
       sh2->state |= flags;
@@ -121,8 +121,8 @@ static void sh2_poll_detect(SH2 *sh2, u32 a, u32 flags, int maxcnt)
 void p32x_sh2_poll_event(SH2 *sh2, u32 flags, u32 m68k_cycles)
 {
   if (sh2->state & flags) {
-    elprintf(EL_32X, "%csh2 state: %02x->%02x", sh2->is_slave?'s':'m',
-      sh2->state, sh2->state & ~flags);
+    elprintf_sh2(sh2, EL_32X, "state: %02x->%02x", sh2->state,
+      sh2->state & ~flags);
 
     if (sh2->m68krcycles_done < m68k_cycles)
       sh2->m68krcycles_done = m68k_cycles;
@@ -212,7 +212,7 @@ static u32 p32x_reg_read16(u32 a)
     unsigned int cycles = SekCyclesDoneT();
     if (cycles - msh2.m68krcycles_done > 64)
       p32x_sync_sh2s(cycles);
-    return ((Pico32x.sh2irqi[0] & P32XI_CMD) >> 4) | ((Pico32x.sh2irqi[1] & P32XI_CMD) >> 3);
+    goto out;
   }
 
   if ((a & 0x30) == 0x30)
@@ -269,21 +269,11 @@ static void p32x_reg_write8(u32 a, u32 d)
     case 0x02: // ignored, always 0
       return;
     case 0x03: // irq ctl
-      if ((d & 1) != !!(Pico32x.sh2irqi[0] & P32XI_CMD)) {
-        p32x_sync_sh2s(SekCyclesDoneT());
-        if (d & 1)
-          Pico32x.sh2irqi[0] |= P32XI_CMD;
-        else
-          Pico32x.sh2irqi[0] &= ~P32XI_CMD;
-        p32x_update_irls(NULL, SekCyclesDoneT2());
-      }
-      if (!!(d & 2) != !!(Pico32x.sh2irqi[1] & P32XI_CMD)) {
-        p32x_sync_sh2s(SekCyclesDoneT());
-        if (d & 2)
-          Pico32x.sh2irqi[1] |= P32XI_CMD;
-        else
-          Pico32x.sh2irqi[1] &= ~P32XI_CMD;
-        p32x_update_irls(NULL, SekCyclesDoneT2());
+      if ((d ^ r[0x02 / 2]) & 3) {
+        int cycles = SekCyclesDoneT();
+        p32x_sync_sh2s(cycles);
+        r[0x02 / 2] = d & 3;
+        p32x_update_cmd_irq(NULL, cycles);
       }
       return;
     case 0x04: // ignored, always 0
@@ -625,8 +615,9 @@ static u32 p32x_sh2reg_read16(u32 a, SH2 *sh2)
 
 static void p32x_sh2reg_write8(u32 a, u32 d, SH2 *sh2)
 {
-  a &= 0xff;
+  u32 old;
 
+  a &= 0xff;
   sh2->poll_addr = 0;
 
   switch (a) {
@@ -635,14 +626,20 @@ static void p32x_sh2reg_write8(u32 a, u32 d, SH2 *sh2)
       Pico32x.regs[0] |= (d << 8) & P32XS_FM;
       return;
     case 1: // HEN/irq masks
-      if ((d ^ Pico32x.sh2_regs[0]) & 0x80)
-        elprintf(EL_ANOMALY|EL_32X, "HEN");
-      Pico32x.sh2irq_mask[sh2->is_slave] = d & 0x8f;
+      old = Pico32x.sh2irq_mask[sh2->is_slave];
+      if ((d ^ old) & 1)
+        p32x_pwm_sync_to_sh2(sh2);
+
+      Pico32x.sh2irq_mask[sh2->is_slave] = d & 0x0f;
       Pico32x.sh2_regs[0] &= ~0x80;
       Pico32x.sh2_regs[0] |= d & 0x80;
-      if (d & 1)
+
+      if ((d ^ old) & 1)
         p32x_pwm_schedule_sh2(sh2);
-      p32x_update_irls(sh2, 0);
+      if ((old ^ d) & 2)
+        p32x_update_cmd_irq(sh2, 0);
+      if ((old ^ d) & 4)
+        p32x_schedule_hint(sh2, 0); 
       return;
     case 5: // H count
       d &= 0xff;
@@ -702,12 +699,22 @@ static void p32x_sh2reg_write16(u32 a, u32 d, SH2 *sh2)
       Pico32x.regs[0] &= ~P32XS_FM;
       Pico32x.regs[0] |= d & P32XS_FM;
       break;
-    case 0x14: Pico32x.sh2irqs &= ~P32XI_VRES; goto irls;
-    case 0x16: Pico32x.sh2irqs &= ~P32XI_VINT; goto irls;
-    case 0x18: Pico32x.sh2irqs &= ~P32XI_HINT; goto irls;
-    case 0x1a: Pico32x.sh2irqi[sh2->is_slave] &= ~P32XI_CMD; goto irls;
+    case 0x14:
+      Pico32x.sh2irqs &= ~P32XI_VRES;
+      goto irls;
+    case 0x16:
+      Pico32x.sh2irqi[sh2->is_slave] &= ~P32XI_VINT;
+      goto irls;
+    case 0x18:
+      Pico32x.sh2irqi[sh2->is_slave] &= ~P32XI_HINT;
+      goto irls;
+    case 0x1a:
+      Pico32x.regs[2 / 2] &= ~(1 << sh2->is_slave);
+      p32x_update_cmd_irq(sh2, 0);
+      return;
     case 0x1c:
-      Pico32x.sh2irqs &= ~P32XI_PWM;
+      p32x_pwm_sync_to_sh2(sh2);
+      Pico32x.sh2irqi[sh2->is_slave] &= ~P32XI_PWM;
       p32x_pwm_schedule_sh2(sh2);
       goto irls;
   }
@@ -1054,8 +1061,8 @@ static void bank_switch(int b)
 // read8
 static u32 sh2_read8_unmapped(u32 a, SH2 *sh2)
 {
-  elprintf(EL_UIO, "%csh2 unmapped r8  [%08x]       %02x @%06x",
-    sh2->is_slave ? 's' : 'm', a, 0, sh2_pc(sh2));
+  elprintf_sh2(sh2, EL_32X, "unmapped r8  [%08x]       %02x @%06x",
+    a, 0, sh2_pc(sh2));
   return 0;
 }
 
@@ -1096,8 +1103,8 @@ out_16to8:
   else
     d >>= 8;
 
-  elprintf(EL_32X, "%csh2 r8  [%08x]       %02x @%06x",
-    sh2->is_slave ? 's' : 'm', a, d, sh2_pc(sh2));
+  elprintf_sh2(sh2, EL_32X, "r8  [%08x]       %02x @%06x",
+    a, d, sh2_pc(sh2));
   return d;
 }
 
@@ -1109,8 +1116,8 @@ static u32 sh2_read8_da(u32 a, SH2 *sh2)
 // read16
 static u32 sh2_read16_unmapped(u32 a, SH2 *sh2)
 {
-  elprintf(EL_UIO, "%csh2 unmapped r16 [%08x]     %04x @%06x",
-    sh2->is_slave ? 's' : 'm', a, 0, sh2_pc(sh2));
+  elprintf_sh2(sh2, EL_32X, "unmapped r16 [%08x]     %04x @%06x",
+    a, 0, sh2_pc(sh2));
   return 0;
 }
 
@@ -1146,8 +1153,8 @@ static u32 sh2_read16_cs0(u32 a, SH2 *sh2)
   return sh2_read16_unmapped(a, sh2);
 
 out:
-  elprintf(EL_32X, "%csh2 r16 [%08x]     %04x @%06x",
-    sh2->is_slave ? 's' : 'm', a, d, sh2_pc(sh2));
+  elprintf_sh2(sh2, EL_32X, "r16 [%08x]     %04x @%06x",
+    a, d, sh2_pc(sh2));
   return d;
 }
 
@@ -1164,14 +1171,14 @@ static void REGPARM(3) sh2_write_ignore(u32 a, u32 d, SH2 *sh2)
 // write8
 static void REGPARM(3) sh2_write8_unmapped(u32 a, u32 d, SH2 *sh2)
 {
-  elprintf(EL_UIO, "%csh2 unmapped w8  [%08x]       %02x @%06x",
-    sh2->is_slave ? 's' : 'm', a, d & 0xff, sh2_pc(sh2));
+  elprintf_sh2(sh2, EL_32X, "unmapped w8  [%08x]       %02x @%06x",
+    a, d & 0xff, sh2_pc(sh2));
 }
 
 static void REGPARM(3) sh2_write8_cs0(u32 a, u32 d, SH2 *sh2)
 {
-  elprintf(EL_32X, "%csh2 w8  [%08x]       %02x @%06x",
-    sh2->is_slave ? 's' : 'm', a, d & 0xff, sh2_pc(sh2));
+  elprintf_sh2(sh2, EL_32X, "w8  [%08x]       %02x @%06x",
+    a, d & 0xff, sh2_pc(sh2));
 
   if (Pico32x.regs[0] & P32XS_FM) {
     if ((a & 0x3ff00) == 0x4100) {
@@ -1234,15 +1241,15 @@ static void REGPARM(3) sh2_write8_da(u32 a, u32 d, SH2 *sh2)
 // write16
 static void REGPARM(3) sh2_write16_unmapped(u32 a, u32 d, SH2 *sh2)
 {
-  elprintf(EL_UIO, "%csh2 unmapped w16 [%08x]     %04x @%06x",
-    sh2->is_slave ? 's' : 'm', a, d & 0xffff, sh2_pc(sh2));
+  elprintf_sh2(sh2, EL_32X, "unmapped w16 [%08x]     %04x @%06x",
+    a, d & 0xffff, sh2_pc(sh2));
 }
 
 static void REGPARM(3) sh2_write16_cs0(u32 a, u32 d, SH2 *sh2)
 {
   if (((EL_LOGMASK & EL_PWM) || (a & 0x30) != 0x30)) // hide PWM
-    elprintf(EL_32X, "%csh2 w16 [%08x]     %04x @%06x",
-      sh2->is_slave ? 's' : 'm', a, d & 0xffff, sh2_pc(sh2));
+    elprintf_sh2(sh2, EL_32X, "w16 [%08x]     %04x @%06x",
+      a, d & 0xffff, sh2_pc(sh2));
 
   if (Pico32x.regs[0] & P32XS_FM) {
     if ((a & 0x3ff00) == 0x4100) {