32x: improve irq handling + few bugfixes
[picodrive.git] / cpu / sh2 / sh2.c
index 87343b0..80c458f 100644 (file)
@@ -1,15 +1,19 @@
 #include <string.h>
 #include "sh2.h"
+#include "../debug.h"
 #include "compiler.h"
 
 #define I 0xf0
 
+SH2 *sh2; // active sh2
+
 int sh2_init(SH2 *sh2, int is_slave)
 {
        int ret = 0;
 
        memset(sh2, 0, sizeof(*sh2));
        sh2->is_slave = is_slave;
+       pdb_register_cpu(sh2, PDBCT_SH2, is_slave ? "ssh2" : "msh2");
 #ifdef DRC_SH2
        ret = sh2_drc_init(sh2);
 #endif
@@ -25,8 +29,8 @@ void sh2_finish(SH2 *sh2)
 
 void sh2_reset(SH2 *sh2)
 {
-       sh2->pc = p32x_sh2_read32(0, sh2->is_slave);
-       sh2->r[15] = p32x_sh2_read32(4, sh2->is_slave);
+       sh2->pc = p32x_sh2_read32(0, sh2);
+       sh2->r[15] = p32x_sh2_read32(4, sh2);
        sh2->sr = I;
        sh2->vbr = 0;
        sh2->pending_int_irq = 0;
@@ -34,33 +38,39 @@ void sh2_reset(SH2 *sh2)
 
 void sh2_do_irq(SH2 *sh2, int level, int vector)
 {
-       sh2->irq_callback(sh2->is_slave, level);
-
        sh2->r[15] -= 4;
-       p32x_sh2_write32(sh2->r[15], sh2->sr, sh2->is_slave);   /* push SR onto stack */
+       p32x_sh2_write32(sh2->r[15], sh2->sr, sh2);     /* push SR onto stack */
        sh2->r[15] -= 4;
-       p32x_sh2_write32(sh2->r[15], sh2->pc, sh2->is_slave);   /* push PC onto stack */
+       p32x_sh2_write32(sh2->r[15], sh2->pc, sh2);     /* push PC onto stack */
 
        /* set I flags in SR */
        sh2->sr = (sh2->sr & ~I) | (level << 4);
 
        /* fetch PC */
-       sh2->pc = p32x_sh2_read32(sh2->vbr + vector * 4, sh2->is_slave);
+       sh2->pc = p32x_sh2_read32(sh2->vbr + vector * 4, sh2);
 
        /* 13 cycles at best */
        sh2->cycles_done += 13;
 //     sh2->icount -= 13;
 }
 
-void sh2_irl_irq(SH2 *sh2, int level)
+void sh2_irl_irq(SH2 *sh2, int level, int nested_call)
 {
        sh2->pending_irl = level;
-       if (level > sh2->pending_int_irq)
-               sh2->pending_level = level;
-       else
-               sh2->pending_level = sh2->pending_int_irq;
+       if (level < sh2->pending_int_irq)
+               level = sh2->pending_int_irq;
+       sh2->pending_level = level;
 
-       sh2->test_irq = 1;
+       if (!nested_call) {
+               // not in memhandler, so handle this now (recompiler friendly)
+               // do this to avoid missing irqs that other SH2 might clear
+               if (level > ((sh2->sr >> 4) & 0x0f)) {
+                       int vector = sh2->irq_callback(sh2, level);
+                       sh2_do_irq(sh2, level, vector);
+               }
+       }
+       else
+               sh2->test_irq = 1;
 }
 
 void sh2_internal_irq(SH2 *sh2, int level, int vector)