32x: memhandler improvements
[picodrive.git] / pico / 32x / memory.c
index 1e07e7d..fd6ff45 100644 (file)
@@ -171,45 +171,6 @@ static u32 sh2_comm_faker(u32 a)
 }
 #endif
 
-// DMAC handling
-static struct {
-  unsigned int sar0, dar0, tcr0; // src addr, dst addr, transfer count
-  unsigned int chcr0; // chan ctl
-  unsigned int sar1, dar1, tcr1; // same for chan 1
-  unsigned int chcr1;
-  int pad[4];
-  unsigned int dmaor;
-} * dmac0;
-
-static void dma_68k2sh2_do(void)
-{
-  unsigned short *dreqlen = &Pico32x.regs[0x10 / 2];
-  int i;
-
-  if (dmac0->tcr0 != *dreqlen)
-    elprintf(EL_32X|EL_ANOMALY, "tcr0 and dreq len differ: %d != %d", dmac0->tcr0, *dreqlen);
-
-  // HACK: assume bus is busy and SH2 is halted
-  msh2.state |= SH2_STATE_SLEEP;
-
-  for (i = 0; i < Pico32x.dmac_ptr && dmac0->tcr0 > 0; i++) {
-    elprintf(EL_32X, "dmaw [%08x] %04x, left %d", dmac0->dar0, Pico32x.dmac_fifo[i], *dreqlen);
-    p32x_sh2_write16(dmac0->dar0, Pico32x.dmac_fifo[i], &msh2);
-    dmac0->dar0 += 2;
-    dmac0->tcr0--;
-    (*dreqlen)--;
-  }
-
-  Pico32x.dmac_ptr = 0; // HACK
-  Pico32x.regs[6 / 2] &= ~P32XS_FULL;
-  if (*dreqlen == 0)
-    Pico32x.regs[6 / 2] &= ~P32XS_68S; // transfer complete
-  if (dmac0->tcr0 == 0) {
-    dmac0->chcr0 |= 2; // DMA has ended normally
-    p32x_sh2_poll_event(&sh2s[0], SH2_STATE_SLEEP, SekCyclesDoneT());
-  }
-}
-
 // ------------------------------------------------------------------
 // 68k regs
 
@@ -252,7 +213,7 @@ static u32 p32x_reg_read16(u32 a)
   }
 
   if ((a & 0x30) == 0x30)
-    return p32x_pwm_read16(a);
+    return p32x_pwm_read16(a, NULL, SekCyclesDoneT());
 
 out:
   return Pico32x.regs[a / 2];
@@ -276,15 +237,21 @@ static void p32x_reg_write8(u32 a, u32 d)
       r[0] = (r[0] & ~P32XS_nRES) | (d & P32XS_nRES);
       return;
     case 3: // irq ctl
-      if ((d & 1) && !(Pico32x.sh2irqi[0] & P32XI_CMD)) {
+      if ((d & 1) != !!(Pico32x.sh2irqi[0] & P32XI_CMD)) {
         p32x_sync_sh2s(SekCyclesDoneT());
-        Pico32x.sh2irqi[0] |= P32XI_CMD;
-        p32x_update_irls(NULL);
+        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)) {
+      if (!!(d & 2) != !!(Pico32x.sh2irqi[1] & P32XI_CMD)) {
         p32x_sync_sh2s(SekCyclesDoneT());
-        Pico32x.sh2irqi[1] |= P32XI_CMD;
-        p32x_update_irls(NULL);
+        if (d & 2)
+          Pico32x.sh2irqi[1] |= P32XI_CMD;
+        else
+          Pico32x.sh2irqi[1] &= ~P32XI_CMD;
+        p32x_update_irls(NULL, SekCyclesDoneT2());
       }
       return;
     case 5: // bank
@@ -347,11 +314,11 @@ static void p32x_reg_write16(u32 a, u32 d)
         elprintf(EL_32X|EL_ANOMALY, "DREQ FIFO w16 without 68S?");
         return;
       }
-      if (Pico32x.dmac_ptr < DMAC_FIFO_LEN) {
-        Pico32x.dmac_fifo[Pico32x.dmac_ptr++] = d;
-        if ((Pico32x.dmac_ptr & 3) == 0 && (dmac0->chcr0 & 3) == 1 && (dmac0->dmaor & 1))
-          dma_68k2sh2_do();
-        if (Pico32x.dmac_ptr == DMAC_FIFO_LEN)
+      if (Pico32x.dmac0_fifo_ptr < DMAC_FIFO_LEN) {
+        Pico32x.dmac_fifo[Pico32x.dmac0_fifo_ptr++] = d;
+        if ((Pico32x.dmac0_fifo_ptr & 3) == 0)
+          p32x_dreq0_trigger();
+        if (Pico32x.dmac0_fifo_ptr == DMAC_FIFO_LEN)
           r[6 / 2] |= P32XS_FULL;
       }
       break;
@@ -385,7 +352,7 @@ static void p32x_reg_write16(u32 a, u32 d)
   }
   // PWM
   else if ((a & 0x30) == 0x30) {
-    p32x_pwm_write16(a, d);
+    p32x_pwm_write16(a, d, NULL, SekCyclesDoneT());
     return;
   }
 
@@ -465,17 +432,18 @@ static void p32x_vdp_write16(u32 a, u32 d, SH2 *sh2)
 // ------------------------------------------------------------------
 // SH2 regs
 
-static u32 p32x_sh2reg_read16(u32 a, int cpuid)
+static u32 p32x_sh2reg_read16(u32 a, SH2 *sh2)
 {
   u16 *r = Pico32x.regs;
   a &= 0xfe; // ?
 
   switch (a) {
     case 0x00: // adapter/irq ctl
-      return (r[0] & P32XS_FM) | Pico32x.sh2_regs[0] | Pico32x.sh2irq_mask[cpuid];
+      return (r[0] & P32XS_FM) | Pico32x.sh2_regs[0]
+        | Pico32x.sh2irq_mask[sh2->is_slave];
     case 0x04: // H count (often as comm too)
-      sh2_poll_detect(&sh2s[cpuid], a, SH2_STATE_CPOLL);
-      sh2s_sync_on_read(&sh2s[cpuid]);
+      sh2_poll_detect(sh2, a, SH2_STATE_CPOLL);
+      sh2s_sync_on_read(sh2);
       return Pico32x.sh2_regs[4 / 2];
     case 0x10: // DREQ len
       return r[a / 2];
@@ -490,22 +458,22 @@ static u32 p32x_sh2reg_read16(u32 a, int cpuid)
     if (Pico32x.comm_dirty_68k & comreg)
       Pico32x.comm_dirty_68k &= ~comreg;
     else
-      sh2_poll_detect(&sh2s[cpuid], a, SH2_STATE_CPOLL);
-    sh2s_sync_on_read(&sh2s[cpuid]);
+      sh2_poll_detect(sh2, a, SH2_STATE_CPOLL);
+    sh2s_sync_on_read(sh2);
     return r[a / 2];
   }
   if ((a & 0x30) == 0x30) {
-    return p32x_pwm_read16(a);
+    return p32x_pwm_read16(a, sh2, sh2_cycles_done_m68k(sh2));
   }
 
   return 0;
 }
 
-static void p32x_sh2reg_write8(u32 a, u32 d, int cpuid)
+static void p32x_sh2reg_write8(u32 a, u32 d, SH2 *sh2)
 {
   a &= 0xff;
 
-  sh2s[cpuid].poll_addr = 0;
+  sh2->poll_addr = 0;
 
   switch (a) {
     case 0: // FM
@@ -515,20 +483,20 @@ static void p32x_sh2reg_write8(u32 a, u32 d, int cpuid)
     case 1: // HEN/irq masks
       if ((d ^ Pico32x.sh2_regs[0]) & 0x80)
         elprintf(EL_ANOMALY|EL_32X, "HEN");
-      Pico32x.sh2irq_mask[cpuid] = d & 0x8f;
+      Pico32x.sh2irq_mask[sh2->is_slave] = d & 0x8f;
       Pico32x.sh2_regs[0] &= ~0x80;
       Pico32x.sh2_regs[0] |= d & 0x80;
       if (d & 1)
-        p32x_pwm_schedule_sh2(&sh2s[cpuid]);
-      p32x_update_irls(&sh2s[cpuid]);
+        p32x_pwm_schedule_sh2(sh2);
+      p32x_update_irls(sh2, 0);
       return;
     case 5: // H count
       d &= 0xff;
       if (Pico32x.sh2_regs[4 / 2] != d) {
         Pico32x.sh2_regs[4 / 2] = d;
-        p32x_sh2_poll_event(&sh2s[cpuid ^ 1], SH2_STATE_CPOLL,
-          sh2_cycles_done_m68k(&sh2s[cpuid]));
-        sh2_end_run(&sh2s[cpuid], 4);
+        p32x_sh2_poll_event(sh2->other_sh2, SH2_STATE_CPOLL,
+          sh2_cycles_done_m68k(sh2));
+        sh2_end_run(sh2, 4);
       }
       return;
   }
@@ -541,19 +509,19 @@ static void p32x_sh2reg_write8(u32 a, u32 d, int cpuid)
 
     r8[a ^ 1] = d;
     p32x_m68k_poll_event(P32XF_68KCPOLL);
-    p32x_sh2_poll_event(&sh2s[cpuid ^ 1], SH2_STATE_CPOLL,
-      sh2_cycles_done_m68k(&sh2s[cpuid]));
+    p32x_sh2_poll_event(sh2->other_sh2, SH2_STATE_CPOLL,
+      sh2_cycles_done_m68k(sh2));
     comreg = 1 << (a & 0x0f) / 2;
     Pico32x.comm_dirty_sh2 |= comreg;
     return;
   }
 }
 
-static void p32x_sh2reg_write16(u32 a, u32 d, int cpuid)
+static void p32x_sh2reg_write16(u32 a, u32 d, SH2 *sh2)
 {
   a &= 0xfe;
 
-  sh2s[cpuid].poll_addr = 0;
+  sh2->poll_addr = 0;
 
   // comm
   if ((a & 0x30) == 0x20) {
@@ -563,15 +531,15 @@ static void p32x_sh2reg_write16(u32 a, u32 d, int cpuid)
 
     Pico32x.regs[a / 2] = d;
     p32x_m68k_poll_event(P32XF_68KCPOLL);
-    p32x_sh2_poll_event(&sh2s[cpuid ^ 1], SH2_STATE_CPOLL,
-      sh2_cycles_done_m68k(&sh2s[cpuid]));
+    p32x_sh2_poll_event(sh2->other_sh2, SH2_STATE_CPOLL,
+      sh2_cycles_done_m68k(sh2));
     comreg = 1 << (a & 0x0f) / 2;
     Pico32x.comm_dirty_sh2 |= comreg;
     return;
   }
   // PWM
   else if ((a & 0x30) == 0x30) {
-    p32x_pwm_write16(a, d);
+    p32x_pwm_write16(a, d, sh2, sh2_cycles_done_m68k(sh2));
     return;
   }
 
@@ -583,163 +551,22 @@ static void p32x_sh2reg_write16(u32 a, u32 d, int cpuid)
     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[cpuid] &= ~P32XI_CMD; goto irls;
+    case 0x1a: Pico32x.sh2irqi[sh2->is_slave] &= ~P32XI_CMD; goto irls;
     case 0x1c:
       Pico32x.sh2irqs &= ~P32XI_PWM;
-      if (!(Pico32x.emu_flags & P32XF_PWM_PEND))
-        p32x_pwm_schedule_sh2(&sh2s[cpuid]);
+      p32x_pwm_schedule_sh2(sh2);
       goto irls;
   }
 
-  p32x_sh2reg_write8(a | 1, d, cpuid);
+  p32x_sh2reg_write8(a | 1, d, sh2);
   return;
 
 irls:
-  p32x_update_irls(&sh2s[cpuid]);
+  p32x_update_irls(sh2, 0);
 }
 
 // ------------------------------------------------------------------
-// SH2 internal peripherals
-// we keep them in little endian format
-static u32 sh2_peripheral_read8(u32 a, int id)
-{
-  u8 *r = (void *)Pico32xMem->sh2_peri_regs[id];
-  u32 d;
-
-  a &= 0x1ff;
-  d = PREG8(r, a);
-
-  elprintf(EL_32X, "%csh2 peri r8  [%08x]       %02x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
-  return d;
-}
-
-static u32 sh2_peripheral_read16(u32 a, int id)
-{
-  u16 *r = (void *)Pico32xMem->sh2_peri_regs[id];
-  u32 d;
-
-  a &= 0x1ff;
-  d = r[(a / 2) ^ 1];
-
-  elprintf(EL_32X, "%csh2 peri r16 [%08x]     %04x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
-  return d;
-}
-
-static u32 sh2_peripheral_read32(u32 a, int id)
-{
-  u32 d;
-  a &= 0x1fc;
-  d = Pico32xMem->sh2_peri_regs[id][a / 4];
-
-  elprintf(EL_32X, "%csh2 peri r32 [%08x] %08x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
-  return d;
-}
-
-static int REGPARM(3) sh2_peripheral_write8(u32 a, u32 d, int id)
-{
-  u8 *r = (void *)Pico32xMem->sh2_peri_regs[id];
-  elprintf(EL_32X, "%csh2 peri w8  [%08x]       %02x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
-
-  a &= 0x1ff;
-  PREG8(r, a) = d;
-
-  // X-men SCI hack
-  if ((a == 2 &&  (d & 0x20)) || // transmiter enabled
-      (a == 4 && !(d & 0x80))) { // valid data in TDR
-    void *oregs = Pico32xMem->sh2_peri_regs[id ^ 1];
-    if ((PREG8(oregs, 2) & 0x50) == 0x50) { // receiver + irq enabled
-      int level = PREG8(oregs, 0x60) >> 4;
-      int vector = PREG8(oregs, 0x63) & 0x7f;
-      elprintf(EL_32X, "%csh2 SCI recv irq (%d, %d)", (id ^ 1) ? 's' : 'm', level, vector);
-      sh2_internal_irq(&sh2s[id ^ 1], level, vector);
-      return 1;
-    }
-  }
-  return 0;
-}
-
-static int REGPARM(3) sh2_peripheral_write16(u32 a, u32 d, int id)
-{
-  u16 *r = (void *)Pico32xMem->sh2_peri_regs[id];
-  elprintf(EL_32X, "%csh2 peri w16 [%08x]     %04x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
-
-  a &= 0x1ff;
-
-  // evil WDT
-  if (a == 0x80) {
-    if ((d & 0xff00) == 0xa500) { // WTCSR
-      PREG8(r, 0x80) = d;
-      p32x_timers_recalc();
-    }
-    if ((d & 0xff00) == 0x5a00) // WTCNT
-      PREG8(r, 0x81) = d;
-    return 0;
-  }
-
-  r[(a / 2) ^ 1] = d;
-  return 0;
-}
-
-static void sh2_peripheral_write32(u32 a, u32 d, int id)
-{
-  u32 *r = Pico32xMem->sh2_peri_regs[id];
-  elprintf(EL_32X, "%csh2 peri w32 [%08x] %08x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
-
-  a &= 0x1fc;
-  r[a / 4] = d;
-
-  switch (a) {
-    // division unit (TODO: verify):
-    case 0x104: // DVDNT: divident L, starts divide
-      elprintf(EL_32X, "%csh2 divide %08x / %08x", id ? 's' : 'm', d, r[0x100 / 4]);
-      if (r[0x100 / 4]) {
-        signed int divisor = r[0x100 / 4];
-                       r[0x118 / 4] = r[0x110 / 4] = (signed int)d % divisor;
-        r[0x104 / 4] = r[0x11c / 4] = r[0x114 / 4] = (signed int)d / divisor;
-      }
-      else
-        r[0x110 / 4] = r[0x114 / 4] = r[0x118 / 4] = r[0x11c / 4] = 0; // ?
-      break;
-    case 0x114:
-      elprintf(EL_32X, "%csh2 divide %08x%08x / %08x @%08x",
-        id ? 's' : 'm', r[0x110 / 4], d, r[0x100 / 4], sh2_pc(id));
-      if (r[0x100 / 4]) {
-        signed long long divident = (signed long long)r[0x110 / 4] << 32 | d;
-        signed int divisor = r[0x100 / 4];
-        // XXX: undocumented mirroring to 0x118,0x11c?
-        r[0x118 / 4] = r[0x110 / 4] = divident % divisor;
-        divident /= divisor;
-        r[0x11c / 4] = r[0x114 / 4] = divident;
-        divident >>= 31;
-        if ((unsigned long long)divident + 1 > 1) {
-          //elprintf(EL_32X, "%csh2 divide overflow! @%08x", id ? 's' : 'm', sh2_pc(id));
-          r[0x11c / 4] = r[0x114 / 4] = divident > 0 ? 0x7fffffff : 0x80000000; // overflow
-        }
-      }
-      else
-        r[0x110 / 4] = r[0x114 / 4] = r[0x118 / 4] = r[0x11c / 4] = 0; // ?
-      break;
-  }
-
-  if ((a == 0x1b0 || a == 0x18c) && (dmac0->chcr0 & 3) == 1 && (dmac0->dmaor & 1)) {
-    elprintf(EL_32X, "sh2 DMA %08x -> %08x, cnt %d, chcr %04x @%06x",
-      dmac0->sar0, dmac0->dar0, dmac0->tcr0, dmac0->chcr0, sh2_pc(id));
-    dmac0->tcr0 &= 0xffffff;
-
-    // HACK: assume 68k starts writing soon and end the timeslice
-    sh2_end_run(&sh2s[id], 16);
-
-    // DREQ is only sent after first 4 words are written.
-    // we do multiple of 4 words to avoid messing up alignment
-    if (dmac0->sar0 == 0x20004012 && Pico32x.dmac_ptr && (Pico32x.dmac_ptr & 3) == 0) {
-      elprintf(EL_32X, "68k -> sh2 DMA");
-      dma_68k2sh2_do();
-    }
-  }
-}
-
-// ------------------------------------------------------------------
-// 32x handlers
+// 32x 68k handlers
 
 // after ADEN
 static u32 PicoRead8_32x_on(u32 a)
@@ -978,6 +805,44 @@ void PicoWrite16_32x(u32 a, u32 d)
   elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
 }
 
+/* quirk: in both normal and overwrite areas only nonzero values go through */
+#define sh2_write8_dramN(n) \
+  if ((d & 0xff) != 0) { \
+    u8 *dram = (u8 *)Pico32xMem->dram[n]; \
+    dram[(a & 0x1ffff) ^ 1] = d; \
+  }
+
+static void m68k_write8_dram0_ow(u32 a, u32 d)
+{
+  sh2_write8_dramN(0);
+}
+
+static void m68k_write8_dram1_ow(u32 a, u32 d)
+{
+  sh2_write8_dramN(1);
+}
+
+#define sh2_write16_dramN(n) \
+  u16 *pd = &Pico32xMem->dram[n][(a & 0x1ffff) / 2]; \
+  if (!(a & 0x20000)) { \
+    *pd = d; \
+    return; \
+  } \
+  /* overwrite */ \
+  if (!(d & 0xff00)) d |= *pd & 0xff00; \
+  if (!(d & 0x00ff)) d |= *pd & 0x00ff; \
+  *pd = d;
+
+static void m68k_write16_dram0_ow(u32 a, u32 d)
+{
+  sh2_write16_dramN(0);
+}
+
+static void m68k_write16_dram1_ow(u32 a, u32 d)
+{
+  sh2_write16_dramN(1);
+}
+
 // -----------------------------------------------------------------
 
 // hint vector is writeable
@@ -1033,33 +898,33 @@ static void bank_switch(int b)
 // -----------------------------------------------------------------
 
 // read8
-static u32 sh2_read8_unmapped(u32 a, int id)
+static u32 sh2_read8_unmapped(u32 a, SH2 *sh2)
 {
   elprintf(EL_UIO, "%csh2 unmapped r8  [%08x]       %02x @%06x",
-    id ? 's' : 'm', a, 0, sh2_pc(id));
+    sh2->is_slave ? 's' : 'm', a, 0, sh2_pc(sh2));
   return 0;
 }
 
-static u32 sh2_read8_cs0(u32 a, int id)
+static u32 sh2_read8_cs0(u32 a, SH2 *sh2)
 {
   u32 d = 0;
 
   // 0x3ff00 is veridied
   if ((a & 0x3ff00) == 0x4000) {
-    d = p32x_sh2reg_read16(a, id);
+    d = p32x_sh2reg_read16(a, sh2);
     goto out_16to8;
   }
 
   if ((a & 0x3ff00) == 0x4100) {
     d = p32x_vdp_read16(a);
-    sh2_poll_detect(&sh2s[id], a, SH2_STATE_VPOLL);
+    sh2_poll_detect(sh2, a, SH2_STATE_VPOLL);
     goto out_16to8;
   }
 
   // TODO: mirroring?
-  if (id == 0 && a < sizeof(Pico32xMem->sh2_rom_m))
+  if (!sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_m))
     return Pico32xMem->sh2_rom_m[a ^ 1];
-  if (id == 1 && a < sizeof(Pico32xMem->sh2_rom_s))
+  if (sh2->is_slave  && a < sizeof(Pico32xMem->sh2_rom_s))
     return Pico32xMem->sh2_rom_s[a ^ 1];
 
   if ((a & 0x3fe00) == 0x4200) {
@@ -1067,7 +932,7 @@ static u32 sh2_read8_cs0(u32 a, int id)
     goto out_16to8;
   }
 
-  return sh2_read8_unmapped(a, id);
+  return sh2_read8_unmapped(a, sh2);
 
 out_16to8:
   if (a & 1)
@@ -1076,29 +941,29 @@ out_16to8:
     d >>= 8;
 
   elprintf(EL_32X, "%csh2 r8  [%08x]       %02x @%06x",
-    id ? 's' : 'm', a, d, sh2_pc(id));
+    sh2->is_slave ? 's' : 'm', a, d, sh2_pc(sh2));
   return d;
 }
 
-static u32 sh2_read8_da(u32 a, int id)
+static u32 sh2_read8_da(u32 a, SH2 *sh2)
 {
-  return Pico32xMem->data_array[id][(a & 0xfff) ^ 1];
+  return sh2->data_array[(a & 0xfff) ^ 1];
 }
 
 // read16
-static u32 sh2_read16_unmapped(u32 a, int id)
+static u32 sh2_read16_unmapped(u32 a, SH2 *sh2)
 {
   elprintf(EL_UIO, "%csh2 unmapped r16 [%08x]     %04x @%06x",
-    id ? 's' : 'm', a, 0, sh2_pc(id));
+    sh2->is_slave ? 's' : 'm', a, 0, sh2_pc(sh2));
   return 0;
 }
 
-static u32 sh2_read16_cs0(u32 a, int id)
+static u32 sh2_read16_cs0(u32 a, SH2 *sh2)
 {
   u32 d = 0;
 
   if ((a & 0x3ff00) == 0x4000) {
-    d = p32x_sh2reg_read16(a, id);
+    d = p32x_sh2reg_read16(a, sh2);
     if (!(EL_LOGMASK & EL_PWM) && (a & 0x30) == 0x30) // hide PWM
       return d;
     goto out;
@@ -1106,13 +971,13 @@ static u32 sh2_read16_cs0(u32 a, int id)
 
   if ((a & 0x3ff00) == 0x4100) {
     d = p32x_vdp_read16(a);
-    sh2_poll_detect(&sh2s[id], a, SH2_STATE_VPOLL);
+    sh2_poll_detect(sh2, a, SH2_STATE_VPOLL);
     goto out;
   }
 
-  if (id == 0 && a < sizeof(Pico32xMem->sh2_rom_m))
+  if (!sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_m))
     return *(u16 *)(Pico32xMem->sh2_rom_m + a);
-  if (id == 1 && a < sizeof(Pico32xMem->sh2_rom_s))
+  if (sh2->is_slave  && a < sizeof(Pico32xMem->sh2_rom_s))
     return *(u16 *)(Pico32xMem->sh2_rom_s + a);
 
   if ((a & 0x3fe00) == 0x4200) {
@@ -1120,183 +985,159 @@ static u32 sh2_read16_cs0(u32 a, int id)
     goto out;
   }
 
-  return sh2_read16_unmapped(a, id);
+  return sh2_read16_unmapped(a, sh2);
 
 out:
   elprintf(EL_32X, "%csh2 r16 [%08x]     %04x @%06x",
-    id ? 's' : 'm', a, d, sh2_pc(id));
+    sh2->is_slave ? 's' : 'm', a, d, sh2_pc(sh2));
   return d;
 }
 
-static u32 sh2_read16_da(u32 a, int id)
+static u32 sh2_read16_da(u32 a, SH2 *sh2)
 {
-  return ((u16 *)Pico32xMem->data_array[id])[(a & 0xfff) / 2];
+  return ((u16 *)sh2->data_array)[(a & 0xfff) / 2];
 }
 
-static int REGPARM(3) sh2_write_ignore(u32 a, u32 d, int id)
+// writes
+static void REGPARM(3) sh2_write_ignore(u32 a, u32 d, SH2 *sh2)
 {
-  return 0;
 }
 
 // write8
-static int REGPARM(3) sh2_write8_unmapped(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write8_unmapped(u32 a, u32 d, SH2 *sh2)
 {
   elprintf(EL_UIO, "%csh2 unmapped w8  [%08x]       %02x @%06x",
-    id ? 's' : 'm', a, d & 0xff, sh2_pc(id));
-  return 0;
+    sh2->is_slave ? 's' : 'm', a, d & 0xff, sh2_pc(sh2));
 }
 
-static int REGPARM(3) sh2_write8_cs0(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write8_cs0(u32 a, u32 d, SH2 *sh2)
 {
   elprintf(EL_32X, "%csh2 w8  [%08x]       %02x @%06x",
-    id ? 's' : 'm', a, d & 0xff, sh2_pc(id));
+    sh2->is_slave ? 's' : 'm', a, d & 0xff, sh2_pc(sh2));
 
   if (Pico32x.regs[0] & P32XS_FM) {
     if ((a & 0x3ff00) == 0x4100) {
-      sh2s[id].poll_addr = 0;
+      sh2->poll_addr = 0;
       p32x_vdp_write8(a, d);
-      return 0;
+      return;
     }
   }
 
   if ((a & 0x3ff00) == 0x4000) {
-    p32x_sh2reg_write8(a, d, id);
-    return 1;
+    p32x_sh2reg_write8(a, d, sh2);
+    return;
   }
 
-  return sh2_write8_unmapped(a, d, id);
+  sh2_write8_unmapped(a, d, sh2);
 }
 
-/* quirk: in both normal and overwrite areas only nonzero values go through */
-#define sh2_write8_dramN(n) \
-  if ((d & 0xff) != 0) { \
-    u8 *dram = (u8 *)Pico32xMem->dram[n]; \
-    dram[(a & 0x1ffff) ^ 1] = d; \
-  } \
-  return 0;
-
-static int REGPARM(3) sh2_write8_dram0(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write8_dram0(u32 a, u32 d, SH2 *sh2)
 {
   sh2_write8_dramN(0);
 }
 
-static int REGPARM(3) sh2_write8_dram1(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write8_dram1(u32 a, u32 d, SH2 *sh2)
 {
   sh2_write8_dramN(1);
 }
 
-static int REGPARM(3) sh2_write8_sdram(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write8_sdram(u32 a, u32 d, SH2 *sh2)
 {
   u32 a1 = a & 0x3ffff;
 #ifdef DRC_SH2
   int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT];
   if (t)
-    sh2_drc_wcheck_ram(a, t, id);
+    sh2_drc_wcheck_ram(a, t, sh2->is_slave);
 #endif
   Pico32xMem->sdram[a1 ^ 1] = d;
-  return 0;
 }
 
-static int REGPARM(3) sh2_write8_da(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write8_da(u32 a, u32 d, SH2 *sh2)
 {
   u32 a1 = a & 0xfff;
 #ifdef DRC_SH2
+  int id = sh2->is_slave;
   int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT];
   if (t)
     sh2_drc_wcheck_da(a, t, id);
 #endif
-  Pico32xMem->data_array[id][a1 ^ 1] = d;
-  return 0;
+  sh2->data_array[a1 ^ 1] = d;
 }
 
 // write16
-static int REGPARM(3) sh2_write16_unmapped(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write16_unmapped(u32 a, u32 d, SH2 *sh2)
 {
   elprintf(EL_UIO, "%csh2 unmapped w16 [%08x]     %04x @%06x",
-    id ? 's' : 'm', a, d & 0xffff, sh2_pc(id));
-  return 0;
+    sh2->is_slave ? 's' : 'm', a, d & 0xffff, sh2_pc(sh2));
 }
 
-static int REGPARM(3) sh2_write16_cs0(u32 a, u32 d, int id)
+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",
-      id ? 's' : 'm', a, d & 0xffff, sh2_pc(id));
+      sh2->is_slave ? 's' : 'm', a, d & 0xffff, sh2_pc(sh2));
 
   if (Pico32x.regs[0] & P32XS_FM) {
     if ((a & 0x3ff00) == 0x4100) {
-      sh2s[id].poll_addr = 0;
-      p32x_vdp_write16(a, d, &sh2s[id]);
-      return 0;
+      sh2->poll_addr = 0;
+      p32x_vdp_write16(a, d, sh2);
+      return;
     }
 
     if ((a & 0x3fe00) == 0x4200) {
       Pico32xMem->pal[(a & 0x1ff) / 2] = d;
       Pico32x.dirty_pal = 1;
-      return 0;
+      return;
     }
   }
 
   if ((a & 0x3ff00) == 0x4000) {
-    p32x_sh2reg_write16(a, d, id);
-    return 1;
+    p32x_sh2reg_write16(a, d, sh2);
+    return;
   }
 
-  return sh2_write16_unmapped(a, d, id);
+  sh2_write16_unmapped(a, d, sh2);
 }
 
-#define sh2_write16_dramN(n) \
-  u16 *pd = &Pico32xMem->dram[n][(a & 0x1ffff) / 2]; \
-  if (!(a & 0x20000)) { \
-    *pd = d; \
-    return 0; \
-  } \
-  /* overwrite */ \
-  if (!(d & 0xff00)) d |= *pd & 0xff00; \
-  if (!(d & 0x00ff)) d |= *pd & 0x00ff; \
-  *pd = d; \
-  return 0
-
-static int REGPARM(3) sh2_write16_dram0(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write16_dram0(u32 a, u32 d, SH2 *sh2)
 {
   sh2_write16_dramN(0);
 }
 
-static int REGPARM(3) sh2_write16_dram1(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write16_dram1(u32 a, u32 d, SH2 *sh2)
 {
   sh2_write16_dramN(1);
 }
 
-static int REGPARM(3) sh2_write16_sdram(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write16_sdram(u32 a, u32 d, SH2 *sh2)
 {
   u32 a1 = a & 0x3ffff;
 #ifdef DRC_SH2
   int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT];
   if (t)
-    sh2_drc_wcheck_ram(a, t, id);
+    sh2_drc_wcheck_ram(a, t, sh2->is_slave);
 #endif
   ((u16 *)Pico32xMem->sdram)[a1 / 2] = d;
-  return 0;
 }
 
-static int REGPARM(3) sh2_write16_da(u32 a, u32 d, int id)
+static void REGPARM(3) sh2_write16_da(u32 a, u32 d, SH2 *sh2)
 {
   u32 a1 = a & 0xfff;
 #ifdef DRC_SH2
+  int id = sh2->is_slave;
   int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT];
   if (t)
     sh2_drc_wcheck_da(a, t, id);
 #endif
-  ((u16 *)Pico32xMem->data_array[id])[a1 / 2] = d;
-  return 0;
+  ((u16 *)sh2->data_array)[a1 / 2] = d;
 }
 
 
-typedef u32 (sh2_read_handler)(u32 a, int id);
-typedef int REGPARM(3) (sh2_write_handler)(u32 a, u32 d, int id);
+typedef u32 (sh2_read_handler)(u32 a, SH2 *sh2);
+typedef void REGPARM(3) (sh2_write_handler)(u32 a, u32 d, SH2 *sh2);
 
 #define SH2MAP_ADDR2OFFS_R(a) \
-  ((((a) >> 25) & 3) | (((a) >> 27) & 0x1c))
+  ((u32)(a) >> SH2_READ_SHIFT)
 
 #define SH2MAP_ADDR2OFFS_W(a) \
   ((u32)(a) >> SH2_WRITE_SHIFT)
@@ -1309,7 +1150,7 @@ u32 REGPARM(2) p32x_sh2_read8(u32 a, SH2 *sh2)
   sh2_map += SH2MAP_ADDR2OFFS_R(a);
   p = sh2_map->addr;
   if (map_flag_set(p))
-    return ((sh2_read_handler *)(p << 1))(a, sh2->is_slave);
+    return ((sh2_read_handler *)(p << 1))(a, sh2);
   else
     return *(u8 *)((p << 1) + ((a & sh2_map->mask) ^ 1));
 }
@@ -1322,7 +1163,7 @@ u32 REGPARM(2) p32x_sh2_read16(u32 a, SH2 *sh2)
   sh2_map += SH2MAP_ADDR2OFFS_R(a);
   p = sh2_map->addr;
   if (map_flag_set(p))
-    return ((sh2_read_handler *)(p << 1))(a, sh2->is_slave);
+    return ((sh2_read_handler *)(p << 1))(a, sh2);
   else
     return *(u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
 }
@@ -1344,48 +1185,46 @@ u32 REGPARM(2) p32x_sh2_read32(u32 a, SH2 *sh2)
   }
 
   if (offs == 0x1f)
-    return sh2_peripheral_read32(a, sh2->is_slave);
+    return sh2_peripheral_read32(a, sh2);
 
   handler = (sh2_read_handler *)(p << 1);
-  return (handler(a, sh2->is_slave) << 16) | handler(a + 2, sh2->is_slave);
+  return (handler(a, sh2) << 16) | handler(a + 2, sh2);
 }
 
-// return nonzero if write potentially causes an interrupt (used by drc)
-int REGPARM(3) p32x_sh2_write8(u32 a, u32 d, SH2 *sh2)
+void REGPARM(3) p32x_sh2_write8(u32 a, u32 d, SH2 *sh2)
 {
   const void **sh2_wmap = sh2->write8_tab;
   sh2_write_handler *wh;
 
   wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)];
-  return wh(a, d, sh2->is_slave);
+  wh(a, d, sh2);
 }
 
-int REGPARM(3) p32x_sh2_write16(u32 a, u32 d, SH2 *sh2)
+void REGPARM(3) p32x_sh2_write16(u32 a, u32 d, SH2 *sh2)
 {
   const void **sh2_wmap = sh2->write16_tab;
   sh2_write_handler *wh;
 
   wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)];
-  return wh(a, d, sh2->is_slave);
+  wh(a, d, sh2);
 }
 
-int REGPARM(3) p32x_sh2_write32(u32 a, u32 d, SH2 *sh2)
+void REGPARM(3) p32x_sh2_write32(u32 a, u32 d, SH2 *sh2)
 {
   const void **sh2_wmap = sh2->write16_tab;
-  sh2_write_handler *handler;
+  sh2_write_handler *wh;
   u32 offs;
 
   offs = SH2MAP_ADDR2OFFS_W(a);
 
   if (offs == SH2MAP_ADDR2OFFS_W(0xffffc000)) {
-    sh2_peripheral_write32(a, d, sh2->is_slave);
-    return 0;
+    sh2_peripheral_write32(a, d, sh2);
+    return;
   }
 
-  handler = sh2_wmap[offs];
-  handler(a, d >> 16, sh2->is_slave);
-  handler(a + 2, d, sh2->is_slave);
-  return 0;
+  wh = sh2_wmap[offs];
+  wh(a, d >> 16, sh2);
+  wh(a + 2, d, sh2);
 }
 
 // -----------------------------------------------------------------
@@ -1512,7 +1351,7 @@ static void get_bios(void)
 #define MAP_MEMORY(m) ((uptr)(m) >> 1)
 #define MAP_HANDLER(h) ( ((uptr)(h) >> 1) | ((uptr)1 << (sizeof(uptr) * 8 - 1)) )
 
-static sh2_memmap sh2_read8_map[0x20], sh2_read16_map[0x20];
+static sh2_memmap sh2_read8_map[0x80], sh2_read16_map[0x80];
 // for writes we are using handlers only
 static sh2_write_handler *sh2_write8_map[0x80], *sh2_write16_map[0x80];
 
@@ -1520,12 +1359,16 @@ void Pico32xSwapDRAM(int b)
 {
   cpu68k_map_set(m68k_read8_map,   0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
   cpu68k_map_set(m68k_read16_map,  0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
-  cpu68k_map_set(m68k_write8_map,  0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
-  cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
+  cpu68k_map_set(m68k_read8_map,   0x860000, 0x87ffff, Pico32xMem->dram[b], 0);
+  cpu68k_map_set(m68k_read16_map,  0x860000, 0x87ffff, Pico32xMem->dram[b], 0);
+  cpu68k_map_set(m68k_write8_map,  0x840000, 0x87ffff,
+                 b ? m68k_write8_dram1_ow : m68k_write8_dram0_ow, 1);
+  cpu68k_map_set(m68k_write16_map, 0x840000, 0x87ffff,
+                 b ? m68k_write16_dram1_ow : m68k_write16_dram0_ow, 1);
 
   // SH2
-  sh2_read8_map[2].addr   = sh2_read8_map[6].addr   =
-  sh2_read16_map[2].addr  = sh2_read16_map[6].addr  = MAP_MEMORY(Pico32xMem->dram[b]);
+  sh2_read8_map[0x04/2].addr  = sh2_read8_map[0x24/2].addr  =
+  sh2_read16_map[0x04/2].addr = sh2_read16_map[0x24/2].addr = MAP_MEMORY(Pico32xMem->dram[b]);
 
   sh2_write8_map[0x04/2]  = sh2_write8_map[0x24/2]  = b ? sh2_write8_dram1 : sh2_write8_dram0;
   sh2_write16_map[0x04/2] = sh2_write16_map[0x24/2] = b ? sh2_write16_dram1 : sh2_write16_dram0;
@@ -1542,8 +1385,6 @@ void PicoMemSetup32x(void)
     return;
   }
 
-  dmac0 = (void *)&Pico32xMem->sh2_peri_regs[0][0x180 / 4];
-
   get_bios();
 
   // cartridge area becomes unmapped
@@ -1599,35 +1440,35 @@ void PicoMemSetup32x(void)
   }
 
   // CS0
-  sh2_read8_map[0].addr   = sh2_read8_map[4].addr   = MAP_HANDLER(sh2_read8_cs0);
-  sh2_read16_map[0].addr  = sh2_read16_map[4].addr  = MAP_HANDLER(sh2_read16_cs0);
+  sh2_read8_map[0x00/2].addr  = sh2_read8_map[0x20/2].addr  = MAP_HANDLER(sh2_read8_cs0);
+  sh2_read16_map[0x00/2].addr = sh2_read16_map[0x20/2].addr = MAP_HANDLER(sh2_read16_cs0);
   sh2_write8_map[0x00/2]  = sh2_write8_map[0x20/2]  = sh2_write8_cs0;
   sh2_write16_map[0x00/2] = sh2_write16_map[0x20/2] = sh2_write16_cs0;
   // CS1 - ROM
-  sh2_read8_map[1].addr   = sh2_read8_map[5].addr   =
-  sh2_read16_map[1].addr  = sh2_read16_map[5].addr  = MAP_MEMORY(Pico.rom);
-  sh2_read8_map[1].mask   = sh2_read8_map[5].mask   =
-  sh2_read16_map[1].mask  = sh2_read16_map[5].mask  = 0x3fffff; // FIXME
+  sh2_read8_map[0x02/2].addr  = sh2_read8_map[0x22/2].addr  =
+  sh2_read16_map[0x02/2].addr = sh2_read16_map[0x22/2].addr = MAP_MEMORY(Pico.rom);
+  sh2_read8_map[0x02/2].mask  = sh2_read8_map[0x22/2].mask  =
+  sh2_read16_map[0x02/2].mask = sh2_read16_map[0x22/2].mask = 0x3fffff; // FIXME
   // CS2 - DRAM - done by Pico32xSwapDRAM()
-  sh2_read8_map[2].mask   = sh2_read8_map[6].mask   =
-  sh2_read16_map[2].mask  = sh2_read16_map[6].mask  = 0x01ffff;
+  sh2_read8_map[0x04/2].mask  = sh2_read8_map[0x24/2].mask  =
+  sh2_read16_map[0x04/2].mask = sh2_read16_map[0x24/2].mask = 0x01ffff;
   // CS3 - SDRAM
-  sh2_read8_map[3].addr   = sh2_read8_map[7].addr   =
-  sh2_read16_map[3].addr  = sh2_read16_map[7].addr  = MAP_MEMORY(Pico32xMem->sdram);
-  sh2_write8_map[0x06/2]  = sh2_write8_map[0x26/2]  = sh2_write8_sdram;
-  sh2_write16_map[0x06/2] = sh2_write16_map[0x26/2] = sh2_write16_sdram;
-  sh2_read8_map[3].mask   = sh2_read8_map[7].mask   =
-  sh2_read16_map[3].mask  = sh2_read16_map[7].mask  = 0x03ffff;
+  sh2_read8_map[0x06/2].addr   = sh2_read8_map[0x26/2].addr   =
+  sh2_read16_map[0x06/2].addr  = sh2_read16_map[0x26/2].addr  = MAP_MEMORY(Pico32xMem->sdram);
+  sh2_write8_map[0x06/2]       = sh2_write8_map[0x26/2]       = sh2_write8_sdram;
+  sh2_write16_map[0x06/2]      = sh2_write16_map[0x26/2]      = sh2_write16_sdram;
+  sh2_read8_map[0x06/2].mask   = sh2_read8_map[0x26/2].mask   =
+  sh2_read16_map[0x06/2].mask  = sh2_read16_map[0x26/2].mask  = 0x03ffff;
   // SH2 data array
-  sh2_read8_map[0x18].addr   = MAP_HANDLER(sh2_read8_da);
-  sh2_read16_map[0x18].addr  = MAP_HANDLER(sh2_read16_da);
-  sh2_write8_map[0xc0/2]     = sh2_write8_da;
-  sh2_write16_map[0xc0/2]    = sh2_write16_da;
+  sh2_read8_map[0xc0/2].addr  = MAP_HANDLER(sh2_read8_da);
+  sh2_read16_map[0xc0/2].addr = MAP_HANDLER(sh2_read16_da);
+  sh2_write8_map[0xc0/2]      = sh2_write8_da;
+  sh2_write16_map[0xc0/2]     = sh2_write16_da;
   // SH2 IO
-  sh2_read8_map[0x1f].addr   = MAP_HANDLER(sh2_peripheral_read8);
-  sh2_read16_map[0x1f].addr  = MAP_HANDLER(sh2_peripheral_read16);
-  sh2_write8_map[0xff/2]     = sh2_peripheral_write8;
-  sh2_write16_map[0xff/2]    = sh2_peripheral_write16;
+  sh2_read8_map[0xff/2].addr  = MAP_HANDLER(sh2_peripheral_read8);
+  sh2_read16_map[0xff/2].addr = MAP_HANDLER(sh2_peripheral_read16);
+  sh2_write8_map[0xff/2]      = sh2_peripheral_write8;
+  sh2_write16_map[0xff/2]     = sh2_peripheral_write16;
 
   // map DRAM area, both 68k and SH2
   Pico32xSwapDRAM(1);