drc: ignore cache-through on smc check
[picodrive.git] / cpu / sh2 / compiler.c
index 25ba9d2..0e8b65b 100644 (file)
 #define MAX_LOCAL_BRANCHES      32
 
 // debug stuff
-// 1 - warnings/errors
-// 2 - block info/smc
-// 4 - asm
-// 8 - runtime block entry log
+// 01 - warnings/errors
+// 02 - block info/smc
+// 04 - asm
+// 08 - runtime block entry log
+// 10 - smc self-check
 // {
 #ifndef DRC_DEBUG
 #define DRC_DEBUG 0
@@ -419,7 +420,7 @@ static void add_to_block_list(struct block_list **blist, struct block_desc *bloc
 static void rm_from_block_list(struct block_list **blist, struct block_desc *block)
 {
   struct block_list *prev = NULL, *current = *blist;
-  for (; current != NULL; prev = current, current = current->next) {
+  for (; current != NULL; current = current->next) {
     if (current->block == block) {
       if (prev == NULL)
         *blist = current->next;
@@ -428,6 +429,7 @@ static void rm_from_block_list(struct block_list **blist, struct block_desc *blo
       free(current);
       return;
     }
+    prev = current;
   }
   dbg(1, "can't rm block %p (%08x-%08x)",
     block, block->addr, block->addr + block->size);
@@ -514,6 +516,29 @@ missing:
   dbg(1, "rm_from_hashlist: be %p %08x missing?", be, be->pc);
 }
 
+static void unregister_links(struct block_entry *be, int tcache_id)
+{
+  struct block_link *bl_unresolved = unresolved_links[tcache_id];
+  struct block_link *bl, *bl_next;
+
+  for (bl = be->links; bl != NULL; ) {
+    bl_next = bl->next;
+    bl->next = bl_unresolved;
+    bl_unresolved = bl;
+    bl = bl_next;
+  }
+  be->links = NULL;
+  unresolved_links[tcache_id] = bl_unresolved;
+}
+
+// unlike sh2_smc_rm_block, the block stays and can still be accessed
+// by other already directly linked blocks, just not preferred
+static void kill_block_entry(struct block_entry *be, int tcache_id)
+{
+  rm_from_hashlist(be, tcache_id);
+  unregister_links(be, tcache_id);
+}
+
 static struct block_desc *dr_add_block(u32 addr, u16 size_lit,
   u16 size_nolit, int is_slave, int *blk_id)
 {
@@ -524,8 +549,10 @@ static struct block_desc *dr_add_block(u32 addr, u16 size_lit,
 
   // do a lookup to get tcache_id and override check
   be = dr_get_entry(addr, is_slave, &tcache_id);
-  if (be != NULL)
-    dbg(1, "block override for %08x", addr);
+  if (be != NULL) {
+    dbg(1, "block override for %08x, was %p", addr, be->tcache_ptr);
+    kill_block_entry(be, tcache_id);
+  }
 
   bcount = &block_counts[tcache_id];
   if (*bcount >= block_max_counts[tcache_id]) {
@@ -1017,7 +1044,8 @@ static void rcache_unlock_all(void)
     reg_temp[i].flags &= ~HRF_LOCKED;
 }
 
-static inline u32 rcache_used_hreg_mask(void)
+#ifdef DRC_CMP
+static u32 rcache_used_hreg_mask(void)
 {
   u32 mask = 0;
   int i;
@@ -1028,6 +1056,7 @@ static inline u32 rcache_used_hreg_mask(void)
 
   return mask;
 }
+#endif
 
 static void rcache_clean(void)
 {
@@ -1478,13 +1507,22 @@ static void REGPARM(2) *sh2_translate(SH2 *sh2, int tcache_id)
 
         // make block entry
         v = block->entry_count;
-        if (v < ARRAY_SIZE(block->entryp)) {
+        if (v < ARRAY_SIZE(block->entryp))
+        {
+          struct block_entry *be_old;
+
           block->entryp[v].pc = pc;
           block->entryp[v].tcache_ptr = tcache_ptr;
           block->entryp[v].links = NULL;
 #if (DRC_DEBUG & 2)
           block->entryp[v].block = block;
 #endif
+          be_old = dr_get_entry(pc, sh2->is_slave, &tcache_id);
+          if (be_old != NULL) {
+            dbg(1, "entry override for %08x, was %p", pc, be_old->tcache_ptr);
+            kill_block_entry(be_old, tcache_id);
+          }
+
           add_to_hashlist(&block->entryp[v], tcache_id);
           block->entry_count++;
 
@@ -1512,6 +1550,22 @@ static void REGPARM(2) *sh2_translate(SH2 *sh2, int tcache_id)
       emit_move_r_imm32(SHR_PC, pc);
       rcache_clean();
 
+#if (DRC_DEBUG & 0x10)
+      rcache_get_reg_arg(0, SHR_PC);
+      tmp = emit_memhandler_read(2);
+      tmp2 = rcache_get_tmp();
+      tmp3 = rcache_get_tmp();
+      emith_move_r_imm(tmp2, FETCH32(pc));
+      emith_move_r_imm(tmp3, 0);
+      emith_cmp_r_r(tmp, tmp2);
+      EMITH_SJMP_START(DCOND_EQ);
+      emith_read_r_r_offs_c(DCOND_NE, tmp3, tmp3, 0); // crash
+      EMITH_SJMP_END(DCOND_EQ);
+      rcache_free_tmp(tmp);
+      rcache_free_tmp(tmp2);
+      rcache_free_tmp(tmp3);
+#endif
+
       // check cycles
       sr = rcache_get_reg(SHR_SR, RC_GR_READ);
       emith_cmp_r_imm(sr, 0);
@@ -2590,8 +2644,9 @@ static void REGPARM(2) *sh2_translate(SH2 *sh2, int tcache_id)
 
     default:
     default_:
-      elprintf_sh2(sh2, EL_ANOMALY,
-        "drc: illegal op %04x @ %08x", op, pc - 2);
+      if (!(op_flags[i] & OF_B_IN_DS))
+        elprintf_sh2(sh2, EL_ANOMALY,
+          "drc: illegal op %04x @ %08x", op, pc - 2);
 
       tmp = rcache_get_reg(SHR_SP, RC_GR_RMW);
       emith_sub_r_imm(tmp, 4*2);
@@ -2604,10 +2659,16 @@ static void REGPARM(2) *sh2_translate(SH2 *sh2, int tcache_id)
       // push PC
       rcache_get_reg_arg(0, SHR_SP);
       tmp = rcache_get_tmp_arg(1);
-      emith_move_r_imm(tmp, pc - 2);
+      if (drcf.pending_branch_indirect) {
+        tmp2 = rcache_get_reg(SHR_PC, RC_GR_READ);
+        emith_move_r_r(tmp, tmp2);
+      }
+      else
+        emith_move_r_imm(tmp, pc - 2);
       emit_memhandler_write(2);
       // obtain new PC
-      emit_memhandler_read_rr(SHR_PC, SHR_VBR, 4 * 4, 2);
+      v = (op_flags[i] & OF_B_IN_DS) ? 6 : 4;
+      emit_memhandler_read_rr(SHR_PC, SHR_VBR, v * 4, 2);
       // indirect jump -> back to dispatcher
       rcache_flush();
       emith_jump(sh2_drc_dispatcher);
@@ -2983,13 +3044,12 @@ static void sh2_generate_utils(void)
 #endif
 }
 
-static void sh2_smc_rm_block_entry(struct block_desc *bd, int tcache_id, u32 ram_mask)
+static void sh2_smc_rm_block(struct block_desc *bd, int tcache_id, u32 ram_mask)
 {
-  struct block_link *bl, *bl_next, *bl_unresolved;
   u32 i, addr, end_addr;
   void *tmp;
 
-  dbg(2, "  killing entry %08x-%08x-%08x, blkid %d,%d",
+  dbg(2, "  killing block %08x-%08x-%08x, blkid %d,%d",
     bd->addr, bd->addr + bd->size_nolit, bd->addr + bd->size,
     tcache_id, bd - block_tables[tcache_id]);
   if (bd->addr == 0 || bd->entry_count == 0) {
@@ -3006,7 +3066,6 @@ static void sh2_smc_rm_block_entry(struct block_desc *bd, int tcache_id, u32 ram
   }
 
   tmp = tcache_ptr;
-  bl_unresolved = unresolved_links[tcache_id];
 
   // remove from hash table, make incoming links unresolved
   // XXX: maybe patch branches w/flush instead?
@@ -3022,41 +3081,48 @@ static void sh2_smc_rm_block_entry(struct block_desc *bd, int tcache_id, u32 ram
 
     host_instructions_updated(bd->entryp[i].tcache_ptr, tcache_ptr);
 
-    for (bl = bd->entryp[i].links; bl != NULL; ) {
-      bl_next = bl->next;
-      bl->next = bl_unresolved;
-      bl_unresolved = bl;
-      bl = bl_next;
-    }
+    unregister_links(&bd->entryp[i], tcache_id);
   }
 
   tcache_ptr = tmp;
-  unresolved_links[tcache_id] = bl_unresolved;
 
   bd->addr = bd->size = bd->size_nolit = 0;
   bd->entry_count = 0;
 }
 
-static void sh2_smc_rm_block(u32 a, u16 *drc_ram_blk, int tcache_id, u32 shift, u32 mask)
+/*
+04205:243: == msh2 block #0,200 060017a8-060017f0 -> 0x27cb9c
+ 060017a8 d11c MOV.L   @($70,PC),R1  ; @$0600181c
+
+04230:261: msh2 xsh w32 [260017a8] d225e304
+04230:261: msh2 smc check @260017a8
+04239:226: = ssh2 enter 060017a8 0x27cb9c, c=173
+*/
+static void sh2_smc_rm_blocks(u32 a, u16 *drc_ram_blk, int tcache_id, u32 shift, u32 mask)
 {
   struct block_list **blist = NULL, *entry;
-  u32 from = ~0, to = 0, end_addr, taddr, i;
   struct block_desc *block;
+  u32 start_addr, end_addr, taddr, i;
+  u32 from = ~0, to = 0;
+
+  // ignore cache-through
+  a &= ~0x20000000;
 
   blist = &inval_lookup[tcache_id][(a & mask) / INVAL_PAGE_SIZE];
   entry = *blist;
   while (entry != NULL) {
     block = entry->block;
-    end_addr = block->addr + block->size;
-    if (block->addr <= a && a < end_addr) {
+    start_addr = block->addr & ~0x20000000;
+    end_addr = start_addr + block->size;
+    if (start_addr <= a && a < end_addr) {
       // get addr range that includes all removed blocks
-      if (from > block->addr)
-        from = block->addr;
+      if (from > start_addr)
+        from = start_addr;
       if (to < end_addr)
         to = end_addr;
 
-      sh2_smc_rm_block_entry(block, tcache_id, mask);
-      if (a >= block->addr + block->size_nolit)
+      sh2_smc_rm_block(block, tcache_id, mask);
+      if (a >= start_addr + block->size_nolit)
         literal_disabled_frames = 3;
 
       // entry lost, restart search
@@ -3079,12 +3145,13 @@ static void sh2_smc_rm_block(u32 a, u16 *drc_ram_blk, int tcache_id, u32 shift,
     for (; entry != NULL; entry = entry->next) {
       block = entry->block;
 
-      if (block->addr > a) {
-        if (to > block->addr)
-          to = block->addr;
+      start_addr = block->addr & ~0x20000000;
+      if (start_addr > a) {
+        if (to > start_addr)
+          to = start_addr;
       }
       else {
-        end_addr = block->addr + block->size;
+        end_addr = start_addr + block->size;
         if (from < end_addr)
           from = end_addr;
       }
@@ -3101,13 +3168,13 @@ static void sh2_smc_rm_block(u32 a, u16 *drc_ram_blk, int tcache_id, u32 shift,
 void sh2_drc_wcheck_ram(unsigned int a, int val, int cpuid)
 {
   dbg(2, "%csh2 smc check @%08x", cpuid ? 's' : 'm', a);
-  sh2_smc_rm_block(a, Pico32xMem->drcblk_ram, 0, SH2_DRCBLK_RAM_SHIFT, 0x3ffff);
+  sh2_smc_rm_blocks(a, Pico32xMem->drcblk_ram, 0, SH2_DRCBLK_RAM_SHIFT, 0x3ffff);
 }
 
 void sh2_drc_wcheck_da(unsigned int a, int val, int cpuid)
 {
   dbg(2, "%csh2 smc check @%08x", cpuid ? 's' : 'm', a);
-  sh2_smc_rm_block(a, Pico32xMem->drcblk_da[cpuid],
+  sh2_smc_rm_blocks(a, Pico32xMem->drcblk_da[cpuid],
     1 + cpuid, SH2_DRCBLK_DA_SHIFT, 0xfff);
 }
 
@@ -4062,6 +4129,22 @@ void scan_block(u32 base_pc, int is_slave, u8 *op_flags, u32 *end_pc_out,
         is_slave ? 's' : 'm', op, pc);
       break;
     }
+
+    if (op_flags[i] & OF_DELAY_OP) {
+      switch (opd->op) {
+      case OP_BRANCH:
+      case OP_BRANCH_CT:
+      case OP_BRANCH_CF:
+      case OP_BRANCH_R:
+      case OP_BRANCH_RF:
+        elprintf(EL_ANOMALY, "%csh2 drc: branch in DS @ %08x",
+          is_slave ? 's' : 'm', pc);
+        opd->op = OP_UNHANDLED;
+        op_flags[i] |= OF_B_IN_DS;
+        next_is_delay = 0;
+        break;
+      }
+    }
   }
   i_end = i;
   end_pc = pc;