32x: memhandler improvements
[picodrive.git] / cpu / sh2 / compiler.c
index f76ba43..015a098 100644 (file)
@@ -270,6 +270,8 @@ typedef struct {
 #ifdef __arm__
 #include "../drc/emit_arm.c"
 
+#ifndef __MACH__
+
 static const int reg_map_g2h[] = {
    4,  5,  6,  7,
    8, -1, -1, -1,
@@ -279,6 +281,20 @@ static const int reg_map_g2h[] = {
   -1, -1, -1, -1, // SHR_GBR, SHR_VBR, SHR_MACH, SHR_MACL,
 };
 
+#else
+
+// no r9..
+static const int reg_map_g2h[] = {
+   4,  5,  6,  7,
+  -1, -1, -1, -1,
+  -1, -1, -1, -1,
+  -1, -1, -1,  8, // r12 .. sp
+  -1, -1, -1, 10, // SHR_PC,  SHR_PPC, SHR_PR,   SHR_SR,
+  -1, -1, -1, -1, // SHR_GBR, SHR_VBR, SHR_MACH, SHR_MACL,
+};
+
+#endif
+
 static temp_reg_t reg_temp[] = {
   {  0, },
   {  1, },
@@ -333,7 +349,7 @@ static u32  REGPARM(2) (*sh2_drc_read16)(u32 a, SH2 *sh2);
 static u32  REGPARM(2) (*sh2_drc_read32)(u32 a, SH2 *sh2);
 static void REGPARM(2) (*sh2_drc_write8)(u32 a, u32 d);
 static void REGPARM(2) (*sh2_drc_write16)(u32 a, u32 d);
-static int  REGPARM(3) (*sh2_drc_write32)(u32 a, u32 d, SH2 *sh2);
+static void REGPARM(3) (*sh2_drc_write32)(u32 a, u32 d, SH2 *sh2);
 
 // address space stuff
 static int dr_ctx_get_mem_ptr(u32 a, u32 *mask)
@@ -347,6 +363,7 @@ static int dr_ctx_get_mem_ptr(u32 a, u32 *mask)
   }
   else if ((a & 0xfffff000) == 0xc0000000) {
     // data array
+    // FIXME: access sh2->data_array instead
     poffs = offsetof(SH2, p_da);
     *mask = 0xfff;
   }
@@ -3143,7 +3160,7 @@ void sh2_drc_mem_setup(SH2 *sh2)
 {
   // fill the convenience pointers
   sh2->p_bios = sh2->is_slave ? Pico32xMem->sh2_rom_s : Pico32xMem->sh2_rom_m;
-  sh2->p_da = Pico32xMem->data_array[sh2->is_slave];
+  sh2->p_da = sh2->data_array;
   sh2->p_sdram = Pico32xMem->sdram;
   sh2->p_rom = Pico.rom;
 }
@@ -3261,7 +3278,7 @@ static void *dr_get_pc_base(u32 pc, int is_slave)
   }
   else if ((pc & 0xfffff000) == 0xc0000000) {
     // data array
-    ret = Pico32xMem->data_array[is_slave];
+    ret = sh2s[is_slave].data_array;
     mask = 0xfff;
   }
   else if ((pc & 0xc6000000) == 0x06000000) {
@@ -3287,6 +3304,7 @@ void scan_block(u32 base_pc, int is_slave, u8 *op_flags, u32 *end_pc_out,
   u16 *dr_pc_base;
   u32 pc, op, tmp;
   u32 end_pc, end_literals = 0;
+  u32 lowest_mova = 0;
   struct op_data *opd;
   int next_is_delay = 0;
   int end_block = 0;
@@ -3948,8 +3966,13 @@ void scan_block(u32 base_pc, int is_slave, u8 *op_flags, u32 *end_pc_out,
             tmp = 0;
         }
         opd->dest = BITMASK1(SHR_R0);
-        if (tmp)
+        if (tmp) {
           opd->imm = (tmp + 2 + (op & 0xff) * 4) & ~3;
+          if (opd->imm >= base_pc) {
+            if (lowest_mova == 0 || opd->imm < lowest_mova)
+              lowest_mova = opd->imm;
+          }
+        }
         break;
       case 0x0800: // TST #imm,R0           11001000iiiiiiii
         opd->source = BITMASK1(SHR_R0);
@@ -4059,6 +4082,20 @@ void scan_block(u32 base_pc, int is_slave, u8 *op_flags, u32 *end_pc_out,
   if (end_literals < end_pc)
     end_literals = end_pc;
 
+  // end_literals is used to decide to inline a literal or not
+  // XXX: need better detection if this actually is used in write
+  if (lowest_mova >= base_pc) {
+    if (lowest_mova < end_literals) {
+      dbg(1, "mova for %08x, block %08x", lowest_mova, base_pc);
+      end_literals = end_pc;
+    }
+    if (lowest_mova < end_pc) {
+      dbg(1, "warning: mova inside of blk for %08x, block %08x",
+        lowest_mova, base_pc);
+      end_literals = end_pc;
+    }
+  }
+
   *end_pc_out = end_pc;
   if (end_literals_out != NULL)
     *end_literals_out = end_literals;