2 * vim:shiftwidth=2:expandtab
5 * - tcache, block descriptor, link buffer overflows result in sh2_translate()
6 * failure, followed by full tcache invalidation for that region
7 * - jumps between blocks are tracked for SMC handling (in block_links[]),
8 * except jumps between different tcaches
11 * - static register allocation
12 * - remaining register caching and tracking in temporaries
13 * - block-local branch linking
14 * - block linking (except between tcaches)
15 * - some constant propagation
18 * - proper SMC handling
19 * - better constant propagation
28 #include "../../pico/pico_int.h"
31 #include "../drc/cmn.h"
35 #define PROPAGATE_CONSTANTS 1
36 #define LINK_BRANCHES 1
38 // max literal offset from the block end
39 #define MAX_LITERAL_OFFSET 32*2
47 #define dbg(l,...) { \
48 if ((l) & DRC_DEBUG) \
49 elprintf(EL_STATUS, ##__VA_ARGS__); \
52 #include "mame/sh2dasm.h"
53 #include <platform/linux/host_dasm.h>
54 static int insns_compiled, hash_collisions, host_insn_count;
63 static u8 *tcache_dsm_ptrs[3];
64 static char sh2dasm_buff[64];
65 #define do_host_disasm(tcid) \
66 host_dasm(tcache_dsm_ptrs[tcid], tcache_ptr - tcache_dsm_ptrs[tcid]); \
67 tcache_dsm_ptrs[tcid] = tcache_ptr
69 #define do_host_disasm(x)
72 #if (DRC_DEBUG & 4) || defined(PDB)
73 static void REGPARM(3) *sh2_drc_log_entry(void *block, SH2 *sh2, u32 sr)
76 dbg(4, "= %csh2 enter %08x %p, c=%d", sh2->is_slave ? 's' : 'm',
77 sh2->pc, block, (signed int)sr >> 12);
78 pdb_step(sh2, sh2->pc);
85 #define BLOCK_CYCLE_LIMIT 100
86 #define MAX_BLOCK_SIZE (BLOCK_CYCLE_LIMIT * 6 * 6)
87 #define TCACHE_BUFFERS 3
89 // we have 3 translation cache buffers, split from one drc/cmn buffer.
90 // BIOS shares tcache with data array because it's only used for init
91 // and can be discarded early
92 // XXX: need to tune sizes
93 static const int tcache_sizes[TCACHE_BUFFERS] = {
94 DRC_TCACHE_SIZE * 6 / 8, // ROM, DRAM
95 DRC_TCACHE_SIZE / 8, // BIOS, data array in master sh2
96 DRC_TCACHE_SIZE / 8, // ... slave
99 static u8 *tcache_bases[TCACHE_BUFFERS];
100 static u8 *tcache_ptrs[TCACHE_BUFFERS];
102 // ptr for code emiters
103 static u8 *tcache_ptr;
105 typedef struct block_desc_ {
106 u32 addr; // SH2 PC address
107 void *tcache_ptr; // translated block for above PC
108 struct block_desc_ *next; // next block with the same PC hash
114 typedef struct block_link_ {
116 void *jump; // insn address
117 // struct block_link_ *next;
120 static const int block_max_counts[TCACHE_BUFFERS] = {
125 static block_desc *block_tables[TCACHE_BUFFERS];
126 static block_link *block_links[TCACHE_BUFFERS];
127 static int block_counts[TCACHE_BUFFERS];
128 static int block_link_counts[TCACHE_BUFFERS];
130 // host register tracking
133 HR_CACHED, // 'val' has sh2_reg_e
134 // HR_CONST, // 'val' has a constant
135 HR_TEMP, // reg used for temp storage
139 HRF_DIRTY = 1 << 0, // reg has "dirty" value to be written to ctx
140 HRF_LOCKED = 1 << 1, // HR_CACHED can't be evicted
144 u32 hreg:5; // "host" reg
145 u32 greg:5; // "guest" reg
148 u32 stamp:16; // kind of a timestamp
151 // note: reg_temp[] must have at least the amount of
152 // registers used by handlers in worst case (currently 4)
154 #include "../drc/emit_arm.c"
156 static const int reg_map_g2h[] = {
165 static temp_reg_t reg_temp[] = {
174 #elif defined(__i386__)
175 #include "../drc/emit_x86.c"
177 static const int reg_map_g2h[] = {
186 // ax, cx, dx are usually temporaries by convention
187 static temp_reg_t reg_temp[] = {
195 #error unsupported arch
203 #define T_save 0x00000800
210 #define MAX_HASH_ENTRIES 1024
211 #define HASH_MASK (MAX_HASH_ENTRIES - 1)
212 static void **hash_table;
214 #define HASH_FUNC(hash_tab, addr) \
215 ((block_desc **)(hash_tab))[(addr) & HASH_MASK]
217 static void REGPARM(1) (*sh2_drc_entry)(SH2 *sh2);
218 static void (*sh2_drc_dispatcher)(void);
219 static void (*sh2_drc_exit)(void);
220 static void (*sh2_drc_test_irq)(void);
222 static u32 REGPARM(2) (*sh2_drc_read8)(u32 a, SH2 *sh2);
223 static u32 REGPARM(2) (*sh2_drc_read16)(u32 a, SH2 *sh2);
224 static u32 REGPARM(2) (*sh2_drc_read32)(u32 a, SH2 *sh2);
225 static void REGPARM(2) (*sh2_drc_write8)(u32 a, u32 d);
226 static void REGPARM(2) (*sh2_drc_write8_slot)(u32 a, u32 d);
227 static void REGPARM(2) (*sh2_drc_write16)(u32 a, u32 d);
228 static void REGPARM(2) (*sh2_drc_write16_slot)(u32 a, u32 d);
229 static int REGPARM(3) (*sh2_drc_write32)(u32 a, u32 d, SH2 *sh2);
231 extern void REGPARM(2) sh2_do_op(SH2 *sh2, int opcode);
233 // address space stuff
234 static void *dr_get_pc_base(u32 pc, int is_slave)
239 if ((pc & ~0x7ff) == 0) {
241 ret = is_slave ? Pico32xMem->sh2_rom_s : Pico32xMem->sh2_rom_m;
244 else if ((pc & 0xfffff000) == 0xc0000000) {
246 ret = Pico32xMem->data_array[is_slave];
249 else if ((pc & 0xc6000000) == 0x06000000) {
251 ret = Pico32xMem->sdram;
254 else if ((pc & 0xc6000000) == 0x02000000) {
261 return (void *)-1; // NULL is valid value
263 return (char *)ret - (pc & ~mask);
266 static int dr_ctx_get_mem_ptr(u32 a, u32 *mask)
270 if ((a & ~0x7ff) == 0) {
272 poffs = offsetof(SH2, p_bios);
275 else if ((a & 0xfffff000) == 0xc0000000) {
277 poffs = offsetof(SH2, p_da);
280 else if ((a & 0xc6000000) == 0x06000000) {
282 poffs = offsetof(SH2, p_sdram);
285 else if ((a & 0xc6000000) == 0x02000000) {
287 poffs = offsetof(SH2, p_rom);
294 static block_desc *dr_get_bd(u32 pc, int is_slave, int *tcache_id)
298 // we have full block id tables for data_array and RAM
299 // BIOS goes to data_array table too
300 if ((pc & 0xe0000000) == 0xc0000000 || (pc & ~0xfff) == 0) {
301 int blkid = Pico32xMem->drcblk_da[is_slave][(pc & 0xfff) >> SH2_DRCBLK_DA_SHIFT];
302 *tcache_id = 1 + is_slave;
304 return &block_tables[*tcache_id][blkid >> 1];
307 else if ((pc & 0xc6000000) == 0x06000000) {
308 int blkid = Pico32xMem->drcblk_ram[(pc & 0x3ffff) >> SH2_DRCBLK_RAM_SHIFT];
310 return &block_tables[0][blkid >> 1];
313 else if ((pc & 0xc6000000) == 0x02000000) {
314 block_desc *bd = HASH_FUNC(hash_table, pc);
316 for (; bd != NULL; bd = bd->next)
324 // ---------------------------------------------------------------
327 static void REGPARM(1) flush_tcache(int tcid)
329 dbg(1, "tcache #%d flush! (%d/%d, bds %d/%d)", tcid,
330 tcache_ptrs[tcid] - tcache_bases[tcid], tcache_sizes[tcid],
331 block_counts[tcid], block_max_counts[tcid]);
333 block_counts[tcid] = 0;
334 block_link_counts[tcid] = 0;
335 tcache_ptrs[tcid] = tcache_bases[tcid];
336 if (tcid == 0) { // ROM, RAM
337 memset(hash_table, 0, sizeof(hash_table[0]) * MAX_HASH_ENTRIES);
338 memset(Pico32xMem->drcblk_ram, 0, sizeof(Pico32xMem->drcblk_ram));
341 memset(Pico32xMem->drcblk_da[tcid - 1], 0, sizeof(Pico32xMem->drcblk_da[0]));
343 tcache_dsm_ptrs[tcid] = tcache_bases[tcid];
348 // add block links (tracked branches)
349 static int dr_add_block_link(u32 target_pc, void *jump, int tcache_id)
351 block_link *bl = block_links[tcache_id];
352 int cnt = block_link_counts[tcache_id];
354 if (cnt >= block_max_counts[tcache_id] * 2) {
355 printf("bl overflow for tcache %d\n", tcache_id);
359 bl[cnt].target_pc = target_pc;
361 block_link_counts[tcache_id]++;
367 static block_desc *dr_add_block(u32 addr, int is_slave, int *blk_id)
373 bd = dr_get_bd(addr, is_slave, &tcache_id);
375 dbg(1, "block override for %08x", addr);
376 bd->tcache_ptr = tcache_ptr;
377 *blk_id = bd - block_tables[tcache_id];
381 bcount = &block_counts[tcache_id];
382 if (*bcount >= block_max_counts[tcache_id]) {
383 printf("bd overflow for tcache %d\n", tcache_id);
387 (*bcount)++; // not using descriptor 0
389 bd = &block_tables[tcache_id][*bcount];
391 bd->tcache_ptr = tcache_ptr;
395 if ((addr & 0xc6000000) == 0x02000000) { // ROM
396 bd->next = HASH_FUNC(hash_table, addr);
397 HASH_FUNC(hash_table, addr) = bd;
399 if (bd->next != NULL) {
400 printf(" hash collision with %08x\n", bd->next->addr);
409 static void REGPARM(3) *dr_lookup_block(u32 pc, int is_slave, int *tcache_id)
411 block_desc *bd = NULL;
414 bd = dr_get_bd(pc, is_slave, tcache_id);
416 block = bd->tcache_ptr;
425 static void *dr_prepare_ext_branch(u32 pc, SH2 *sh2, int tcache_id)
428 int target_tcache_id;
432 target = dr_lookup_block(pc, sh2->is_slave, &target_tcache_id);
433 if (target_tcache_id == tcache_id) {
434 // allow linking blocks only from local cache
435 ret = dr_add_block_link(pc, tcache_ptr, tcache_id);
439 if (target == NULL || target_tcache_id != tcache_id)
440 target = sh2_drc_dispatcher;
444 return sh2_drc_dispatcher;
448 static void dr_link_blocks(void *target, u32 pc, int tcache_id)
451 block_link *bl = block_links[tcache_id];
452 int cnt = block_link_counts[tcache_id];
455 for (i = 0; i < cnt; i++) {
456 if (bl[i].target_pc == pc) {
457 dbg(1, "- link from %p", bl[i].jump);
458 emith_jump_patch(bl[i].jump, target);
459 // XXX: sync ARM caches (old jump should be fine)?
465 #define ADD_TO_ARRAY(array, count, item, failcode) \
466 array[count++] = item; \
467 if (count >= ARRAY_SIZE(array)) { \
468 printf("warning: " #array " overflow\n"); \
472 static int find_in_array(u32 *array, size_t size, u32 what)
475 for (i = 0; i < size; i++)
476 if (what == array[i])
482 // ---------------------------------------------------------------
484 // register cache / constant propagation stuff
491 static int rcache_get_reg_(sh2_reg_e r, rc_gr_mode mode, int do_locking);
493 // guest regs with constants
494 static u32 dr_gcregs[24];
495 // a mask of constant/dirty regs
496 static u32 dr_gcregs_mask;
497 static u32 dr_gcregs_dirty;
499 #if PROPAGATE_CONSTANTS
500 static void gconst_new(sh2_reg_e r, u32 val)
504 dr_gcregs_mask |= 1 << r;
505 dr_gcregs_dirty |= 1 << r;
508 // throw away old r that we might have cached
509 for (i = ARRAY_SIZE(reg_temp) - 1; i >= 0; i--) {
510 if ((reg_temp[i].type == HR_CACHED) &&
511 reg_temp[i].greg == r) {
512 reg_temp[i].type = HR_FREE;
513 reg_temp[i].flags = 0;
519 static int gconst_get(sh2_reg_e r, u32 *val)
521 if (dr_gcregs_mask & (1 << r)) {
528 static int gconst_check(sh2_reg_e r)
530 if ((dr_gcregs_mask | dr_gcregs_dirty) & (1 << r))
535 // update hr if dirty, else do nothing
536 static int gconst_try_read(int hr, sh2_reg_e r)
538 if (dr_gcregs_dirty & (1 << r)) {
539 emith_move_r_imm(hr, dr_gcregs[r]);
540 dr_gcregs_dirty &= ~(1 << r);
546 static void gconst_check_evict(sh2_reg_e r)
548 if (dr_gcregs_mask & (1 << r))
549 // no longer cached in reg, make dirty again
550 dr_gcregs_dirty |= 1 << r;
553 static void gconst_kill(sh2_reg_e r)
555 dr_gcregs_mask &= ~(1 << r);
556 dr_gcregs_dirty &= ~(1 << r);
559 static void gconst_clean(void)
563 for (i = 0; i < ARRAY_SIZE(dr_gcregs); i++)
564 if (dr_gcregs_dirty & (1 << i)) {
565 // using RC_GR_READ here: it will call gconst_try_read,
566 // cache the reg and mark it dirty.
567 rcache_get_reg_(i, RC_GR_READ, 0);
571 static void gconst_invalidate(void)
573 dr_gcregs_mask = dr_gcregs_dirty = 0;
576 static u16 rcache_counter;
578 static temp_reg_t *rcache_evict(void)
580 // evict reg with oldest stamp
582 u16 min_stamp = (u16)-1;
584 for (i = 0; i < ARRAY_SIZE(reg_temp); i++) {
585 if (reg_temp[i].type == HR_CACHED && !(reg_temp[i].flags & HRF_LOCKED) &&
586 reg_temp[i].stamp <= min_stamp) {
587 min_stamp = reg_temp[i].stamp;
593 printf("no registers to evict, aborting\n");
598 if (reg_temp[i].type == HR_CACHED) {
599 if (reg_temp[i].flags & HRF_DIRTY)
601 emith_ctx_write(reg_temp[i].hreg, reg_temp[i].greg * 4);
602 gconst_check_evict(reg_temp[i].greg);
605 reg_temp[i].type = HR_FREE;
606 reg_temp[i].flags = 0;
610 static int get_reg_static(sh2_reg_e r, rc_gr_mode mode)
612 int i = reg_map_g2h[r];
614 if (mode != RC_GR_WRITE)
615 gconst_try_read(i, r);
620 // note: must not be called when doing conditional code
621 static int rcache_get_reg_(sh2_reg_e r, rc_gr_mode mode, int do_locking)
626 // maybe statically mapped?
627 ret = get_reg_static(r, mode);
633 // maybe already cached?
634 // if so, prefer against gconst (they must be in sync)
635 for (i = ARRAY_SIZE(reg_temp) - 1; i >= 0; i--) {
636 if (reg_temp[i].type == HR_CACHED && reg_temp[i].greg == r) {
637 reg_temp[i].stamp = rcache_counter;
638 if (mode != RC_GR_READ)
639 reg_temp[i].flags |= HRF_DIRTY;
640 ret = reg_temp[i].hreg;
646 for (i = ARRAY_SIZE(reg_temp) - 1; i >= 0; i--) {
647 if (reg_temp[i].type == HR_FREE) {
656 tr->type = HR_CACHED;
658 tr->flags |= HRF_LOCKED;
659 if (mode != RC_GR_READ)
660 tr->flags |= HRF_DIRTY;
662 tr->stamp = rcache_counter;
665 if (mode != RC_GR_WRITE) {
666 if (gconst_check(r)) {
667 if (gconst_try_read(ret, r))
668 tr->flags |= HRF_DIRTY;
671 emith_ctx_read(tr->hreg, r * 4);
675 if (mode != RC_GR_READ)
681 static int rcache_get_reg(sh2_reg_e r, rc_gr_mode mode)
683 return rcache_get_reg_(r, mode, 1);
686 static int rcache_get_tmp(void)
691 for (i = 0; i < ARRAY_SIZE(reg_temp); i++)
692 if (reg_temp[i].type == HR_FREE) {
704 static int rcache_get_arg_id(int arg)
707 host_arg2reg(r, arg);
709 for (i = 0; i < ARRAY_SIZE(reg_temp); i++)
710 if (reg_temp[i].hreg == r)
713 if (i == ARRAY_SIZE(reg_temp))
714 // let's just say it's untracked arg reg
717 if (reg_temp[i].type == HR_CACHED) {
719 if (reg_temp[i].flags & HRF_DIRTY)
720 emith_ctx_write(reg_temp[i].hreg, reg_temp[i].greg * 4);
721 gconst_check_evict(reg_temp[i].greg);
723 else if (reg_temp[i].type == HR_TEMP) {
724 printf("arg %d reg %d already used, aborting\n", arg, r);
728 reg_temp[i].type = HR_FREE;
729 reg_temp[i].flags = 0;
734 // get a reg to be used as function arg
735 static int rcache_get_tmp_arg(int arg)
737 int id = rcache_get_arg_id(arg);
738 reg_temp[id].type = HR_TEMP;
740 return reg_temp[id].hreg;
743 // same but caches a reg. RC_GR_READ only.
744 static int rcache_get_reg_arg(int arg, sh2_reg_e r)
746 int i, srcr, dstr, dstid;
749 dstid = rcache_get_arg_id(arg);
750 dstr = reg_temp[dstid].hreg;
752 // maybe already statically mapped?
753 srcr = get_reg_static(r, RC_GR_READ);
757 // maybe already cached?
758 for (i = ARRAY_SIZE(reg_temp) - 1; i >= 0; i--) {
759 if ((reg_temp[i].type == HR_CACHED) &&
760 reg_temp[i].greg == r)
762 srcr = reg_temp[i].hreg;
769 if (gconst_check(r)) {
770 if (gconst_try_read(srcr, r))
774 emith_ctx_read(srcr, r * 4);
778 emith_move_r_r(dstr, srcr);
780 reg_temp[dstid].stamp = ++rcache_counter;
781 reg_temp[dstid].type = HR_CACHED;
782 reg_temp[dstid].greg = r;
783 reg_temp[dstid].flags |= HRF_LOCKED;
785 reg_temp[dstid].flags |= HRF_DIRTY;
789 static void rcache_free_tmp(int hr)
792 for (i = 0; i < ARRAY_SIZE(reg_temp); i++)
793 if (reg_temp[i].hreg == hr)
796 if (i == ARRAY_SIZE(reg_temp) || reg_temp[i].type != HR_TEMP) {
797 printf("rcache_free_tmp fail: #%i hr %d, type %d\n", i, hr, reg_temp[i].type);
801 reg_temp[i].type = HR_FREE;
802 reg_temp[i].flags = 0;
805 static void rcache_unlock(int hr)
808 for (i = 0; i < ARRAY_SIZE(reg_temp); i++)
809 if (reg_temp[i].type == HR_CACHED && reg_temp[i].hreg == hr)
810 reg_temp[i].flags &= ~HRF_LOCKED;
813 static void rcache_unlock_all(void)
816 for (i = 0; i < ARRAY_SIZE(reg_temp); i++)
817 reg_temp[i].flags &= ~HRF_LOCKED;
820 static void rcache_clean(void)
825 for (i = 0; i < ARRAY_SIZE(reg_temp); i++)
826 if (reg_temp[i].type == HR_CACHED && (reg_temp[i].flags & HRF_DIRTY)) {
828 emith_ctx_write(reg_temp[i].hreg, reg_temp[i].greg * 4);
829 reg_temp[i].flags &= ~HRF_DIRTY;
833 static void rcache_invalidate(void)
836 for (i = 0; i < ARRAY_SIZE(reg_temp); i++) {
837 reg_temp[i].type = HR_FREE;
838 reg_temp[i].flags = 0;
845 static void rcache_flush(void)
851 // ---------------------------------------------------------------
853 static int emit_get_rbase_and_offs(u32 a, u32 *offs)
859 poffs = dr_ctx_get_mem_ptr(a, &mask);
863 // XXX: could use some related reg
864 hr = rcache_get_tmp();
865 emith_ctx_read(hr, poffs);
866 emith_add_r_imm(hr, a & mask & ~0xff);
867 *offs = a & 0xff; // XXX: ARM oriented..
871 static void emit_move_r_imm32(sh2_reg_e dst, u32 imm)
873 #if PROPAGATE_CONSTANTS
874 gconst_new(dst, imm);
876 int hr = rcache_get_reg(dst, RC_GR_WRITE);
877 emith_move_r_imm(hr, imm);
881 static void emit_move_r_r(sh2_reg_e dst, sh2_reg_e src)
883 int hr_d = rcache_get_reg(dst, RC_GR_WRITE);
884 int hr_s = rcache_get_reg(src, RC_GR_READ);
886 emith_move_r_r(hr_d, hr_s);
889 // T must be clear, and comparison done just before this
890 static void emit_or_t_if_eq(int srr)
892 EMITH_SJMP_START(DCOND_NE);
893 emith_or_r_imm_c(DCOND_EQ, srr, T);
894 EMITH_SJMP_END(DCOND_NE);
897 // arguments must be ready
898 // reg cache must be clean before call
899 static int emit_memhandler_read_(int size, int ram_check)
902 host_arg2reg(arg0, 0);
906 // must writeback cycles for poll detection stuff
908 if (reg_map_g2h[SHR_SR] != -1)
909 emith_ctx_write(reg_map_g2h[SHR_SR], SHR_SR * 4);
911 arg1 = rcache_get_tmp_arg(1);
912 emith_move_r_r(arg1, CONTEXT_REG);
915 if (ram_check && Pico.rom == (void *)0x02000000 && Pico32xMem->sdram == (void *)0x06000000) {
916 int tmp = rcache_get_tmp();
917 emith_and_r_r_imm(tmp, arg0, 0xfb000000);
918 emith_cmp_r_imm(tmp, 0x02000000);
921 EMITH_SJMP3_START(DCOND_NE);
922 emith_eor_r_imm_c(DCOND_EQ, arg0, 1);
923 emith_read8_r_r_offs_c(DCOND_EQ, arg0, arg0, 0);
924 EMITH_SJMP3_MID(DCOND_NE);
925 emith_call_cond(DCOND_NE, sh2_drc_read8);
929 EMITH_SJMP3_START(DCOND_NE);
930 emith_read16_r_r_offs_c(DCOND_EQ, arg0, arg0, 0);
931 EMITH_SJMP3_MID(DCOND_NE);
932 emith_call_cond(DCOND_NE, sh2_drc_read16);
936 EMITH_SJMP3_START(DCOND_NE);
937 emith_read_r_r_offs_c(DCOND_EQ, arg0, arg0, 0);
938 emith_ror_c(DCOND_EQ, arg0, arg0, 16);
939 EMITH_SJMP3_MID(DCOND_NE);
940 emith_call_cond(DCOND_NE, sh2_drc_read32);
950 emith_call(sh2_drc_read8);
953 emith_call(sh2_drc_read16);
956 emith_call(sh2_drc_read32);
961 // assuming arg0 and retval reg matches
962 return rcache_get_tmp_arg(0);
965 static int emit_memhandler_read(int size)
967 return emit_memhandler_read_(size, 1);
970 static int emit_memhandler_read_rr(sh2_reg_e rd, sh2_reg_e rs, u32 offs, int size)
972 int hr, hr2, ram_check = 1;
975 if (gconst_get(rs, &val)) {
976 hr = emit_get_rbase_and_offs(val + offs, &offs2);
978 hr2 = rcache_get_reg(rd, RC_GR_WRITE);
981 emith_read8_r_r_offs(hr2, hr, offs2 ^ 1);
982 emith_sext(hr2, hr2, 8);
985 emith_read16_r_r_offs(hr2, hr, offs2);
986 emith_sext(hr2, hr2, 16);
989 emith_read_r_r_offs(hr2, hr, offs2);
990 emith_ror(hr2, hr2, 16);
1000 hr = rcache_get_reg_arg(0, rs);
1002 emith_add_r_imm(hr, offs);
1003 hr = emit_memhandler_read_(size, ram_check);
1004 hr2 = rcache_get_reg(rd, RC_GR_WRITE);
1006 emith_sext(hr2, hr, (size == 1) ? 16 : 8);
1008 emith_move_r_r(hr2, hr);
1009 rcache_free_tmp(hr);
1014 static void emit_memhandler_write(int size, u32 pc, int delay)
1017 host_arg2reg(ctxr, 2);
1020 // XXX: consider inlining sh2_drc_write8
1022 emith_call(sh2_drc_write8_slot);
1024 emit_move_r_imm32(SHR_PC, pc);
1026 emith_call(sh2_drc_write8);
1031 emith_call(sh2_drc_write16_slot);
1033 emit_move_r_imm32(SHR_PC, pc);
1035 emith_call(sh2_drc_write16);
1039 emith_move_r_r(ctxr, CONTEXT_REG);
1040 emith_call(sh2_drc_write32);
1043 rcache_invalidate();
1047 static int emit_indirect_indexed_read(int rx, int ry, int size)
1050 a0 = rcache_get_reg_arg(0, rx);
1051 t = rcache_get_reg(ry, RC_GR_READ);
1052 emith_add_r_r(a0, t);
1053 return emit_memhandler_read(size);
1057 static void emit_indirect_read_double(u32 *rnr, u32 *rmr, int rn, int rm, int size)
1061 rcache_get_reg_arg(0, rn);
1062 tmp = emit_memhandler_read(size);
1063 emith_ctx_write(tmp, offsetof(SH2, drc_tmp));
1064 rcache_free_tmp(tmp);
1065 tmp = rcache_get_reg(rn, RC_GR_RMW);
1066 emith_add_r_imm(tmp, 1 << size);
1069 rcache_get_reg_arg(0, rm);
1070 *rmr = emit_memhandler_read(size);
1071 *rnr = rcache_get_tmp();
1072 emith_ctx_read(*rnr, offsetof(SH2, drc_tmp));
1073 tmp = rcache_get_reg(rm, RC_GR_RMW);
1074 emith_add_r_imm(tmp, 1 << size);
1078 static void emit_do_static_regs(int is_write, int tmpr)
1082 for (i = 0; i < ARRAY_SIZE(reg_map_g2h); i++) {
1087 for (count = 1; i < ARRAY_SIZE(reg_map_g2h) - 1; i++, r++) {
1088 if (reg_map_g2h[i + 1] != r + 1)
1094 // i, r point to last item
1096 emith_ctx_write_multiple(r - count + 1, (i - count + 1) * 4, count, tmpr);
1098 emith_ctx_read_multiple(r - count + 1, (i - count + 1) * 4, count, tmpr);
1101 emith_ctx_write(r, i * 4);
1103 emith_ctx_read(r, i * 4);
1108 static void emit_block_entry(void)
1110 int arg0, arg1, arg2;
1112 host_arg2reg(arg0, 0);
1113 host_arg2reg(arg1, 1);
1114 host_arg2reg(arg2, 2);
1116 #if (DRC_DEBUG & 4) || defined(PDB)
1117 emit_do_static_regs(1, arg2);
1118 emith_move_r_r(arg1, CONTEXT_REG);
1119 emith_move_r_r(arg2, rcache_get_reg(SHR_SR, RC_GR_READ));
1120 emith_call(sh2_drc_log_entry);
1121 rcache_invalidate();
1123 emith_tst_r_r(arg0, arg0);
1124 EMITH_SJMP_START(DCOND_EQ);
1125 emith_jump_reg_c(DCOND_NE, arg0);
1126 EMITH_SJMP_END(DCOND_EQ);
1129 #define DELAYED_OP \
1132 #define DELAY_SAVE_T(sr) { \
1133 emith_bic_r_imm(sr, T_save); \
1134 emith_tst_r_imm(sr, T); \
1135 EMITH_SJMP_START(DCOND_EQ); \
1136 emith_or_r_imm_c(DCOND_NE, sr, T_save); \
1137 EMITH_SJMP_END(DCOND_EQ); \
1138 drcf.use_saved_t = 1; \
1141 #define FLUSH_CYCLES(sr) \
1143 emith_sub_r_imm(sr, cycles << 12); \
1147 #define CHECK_UNHANDLED_BITS(mask) { \
1148 if ((op & (mask)) != 0) \
1152 #define FETCH_OP(pc) \
1153 dr_pc_base[(pc) / 2]
1155 #define FETCH32(a) \
1156 ((dr_pc_base[(a) / 2] << 16) | dr_pc_base[(a) / 2 + 1])
1161 #define GET_Rm GET_Fx
1166 #define CHECK_FX_LT(n) \
1167 if (GET_Fx() >= n) \
1170 #define MAX_LOCAL_BRANCHES 32
1172 // op_flags: data from 1st pass
1173 #define OP_FLAGS(pc) op_flags[((pc) - base_pc) / 2]
1174 #define OF_DELAY_OP (1 << 0)
1176 static void REGPARM(2) *sh2_translate(SH2 *sh2, int tcache_id)
1178 // XXX: maybe use structs instead?
1179 u32 branch_target_pc[MAX_LOCAL_BRANCHES];
1180 void *branch_target_ptr[MAX_LOCAL_BRANCHES];
1181 int branch_target_blkid[MAX_LOCAL_BRANCHES];
1182 int branch_target_count = 0;
1183 void *branch_patch_ptr[MAX_LOCAL_BRANCHES];
1184 u32 branch_patch_pc[MAX_LOCAL_BRANCHES];
1185 int branch_patch_count = 0;
1186 int pending_branch_cond = -1;
1187 int pending_branch_pc = 0;
1188 u8 op_flags[BLOCK_CYCLE_LIMIT + 1];
1192 u32 use_saved_t:1; // delayed op modifies T
1195 // PC of current, first, last, last_target_blk SH2 insn
1196 u32 pc, base_pc, end_pc, out_pc;
1197 u32 last_inlined_literal = 0;
1199 block_desc *this_block;
1210 // get base/validate PC
1211 dr_pc_base = dr_get_pc_base(base_pc, sh2->is_slave);
1212 if (dr_pc_base == (void *)-1) {
1213 printf("invalid PC, aborting: %08x\n", base_pc);
1214 // FIXME: be less destructive
1218 tcache_ptr = tcache_ptrs[tcache_id];
1219 this_block = dr_add_block(base_pc, sh2->is_slave, &blkid_main);
1220 if (this_block == NULL)
1223 // predict tcache overflow
1224 tmp = tcache_ptr - tcache_bases[tcache_id];
1225 if (tmp > tcache_sizes[tcache_id] - MAX_BLOCK_SIZE) {
1226 printf("tcache %d overflow\n", tcache_id);
1230 block_entry = tcache_ptr;
1231 dbg(1, "== %csh2 block #%d,%d %08x -> %p", sh2->is_slave ? 's' : 'm',
1232 tcache_id, blkid_main, base_pc, block_entry);
1234 dr_link_blocks(tcache_ptr, base_pc, tcache_id);
1236 // 1st pass: scan forward for local branches
1237 memset(op_flags, 0, sizeof(op_flags));
1238 for (cycles = 0, pc = base_pc; cycles < BLOCK_CYCLE_LIMIT; cycles++, pc += 2) {
1240 if ((op & 0xf000) == 0xa000 || (op & 0xf000) == 0xb000) { // BRA, BSR
1241 signed int offs = ((signed int)(op << 20) >> 19);
1243 OP_FLAGS(pc) |= OF_DELAY_OP;
1244 ADD_TO_ARRAY(branch_target_pc, branch_target_count, pc + offs + 2,);
1247 if ((op & 0xf000) == 0) {
1249 if (op == 0x1b) // SLEEP
1251 if (op == 0x23 || op == 0x03 || op == 0x0b || op == 0x2b) { // BRAF, BSRF, RTS, RTE
1253 OP_FLAGS(pc) |= OF_DELAY_OP;
1258 if ((op & 0xf0df) == 0x400b) { // JMP, JSR
1260 OP_FLAGS(pc) |= OF_DELAY_OP;
1263 if ((op & 0xf900) == 0x8900) { // BT(S), BF(S)
1264 signed int offs = ((signed int)(op << 24) >> 23);
1266 OP_FLAGS(pc + 2) |= OF_DELAY_OP;
1267 ADD_TO_ARRAY(branch_target_pc, branch_target_count, pc + offs + 4, break);
1269 if ((op & 0xff00) == 0xc300) // TRAPA
1275 // clean branch_targets that are not really local,
1276 // and that land on delay slots
1277 for (i = 0, tmp = 0; i < branch_target_count; i++) {
1278 pc = branch_target_pc[i];
1279 if (base_pc <= pc && pc <= end_pc && !(OP_FLAGS(pc) & OF_DELAY_OP))
1280 branch_target_pc[tmp++] = branch_target_pc[i];
1282 branch_target_count = tmp;
1283 memset(branch_target_ptr, 0, sizeof(branch_target_ptr[0]) * branch_target_count);
1284 memset(branch_target_blkid, 0, sizeof(branch_target_blkid[0]) * branch_target_count);
1286 // -------------------------------------------------
1287 // 2nd pass: actual compilation
1290 for (cycles = 0; pc <= end_pc || drcf.delayed_op; )
1294 if (drcf.delayed_op > 0)
1299 i = find_in_array(branch_target_pc, branch_target_count, pc);
1304 /* make "subblock" - just a mid-block entry */
1305 block_desc *subblock;
1307 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1309 // decide if to flush rcache
1310 if ((op & 0xf0ff) == 0x4010 && FETCH_OP(pc + 2) == 0x8bfd) // DT; BF #-2
1314 do_host_disasm(tcache_id);
1316 subblock = dr_add_block(pc, sh2->is_slave, &branch_target_blkid[i]);
1317 if (subblock == NULL)
1320 dbg(1, "-- %csh2 subblock #%d,%d %08x -> %p", sh2->is_slave ? 's' : 'm',
1321 tcache_id, branch_target_blkid[i], pc, tcache_ptr);
1323 // since we made a block entry, link any other blocks that jump to current pc
1324 dr_link_blocks(tcache_ptr, pc, tcache_id);
1326 branch_target_ptr[i] = tcache_ptr;
1329 emit_move_r_imm32(SHR_PC, pc);
1333 sr = rcache_get_reg(SHR_SR, RC_GR_READ);
1334 emith_cmp_r_imm(sr, 0);
1335 emith_jump_cond(DCOND_LE, sh2_drc_exit);
1336 do_host_disasm(tcache_id);
1342 DasmSH2(sh2dasm_buff, pc, op);
1343 printf("%08x %04x %s\n", pc, op, sh2dasm_buff);
1355 switch ((op >> 12) & 0x0f)
1357 /////////////////////////////////////////////
1362 tmp = rcache_get_reg(GET_Rn(), RC_GR_WRITE);
1365 case 0: // STC SR,Rn 0000nnnn00000010
1368 case 1: // STC GBR,Rn 0000nnnn00010010
1371 case 2: // STC VBR,Rn 0000nnnn00100010
1377 tmp3 = rcache_get_reg(tmp2, RC_GR_READ);
1378 emith_move_r_r(tmp, tmp3);
1380 emith_clear_msb(tmp, tmp, 22); // reserved bits defined by ISA as 0
1383 CHECK_UNHANDLED_BITS(0xd0);
1384 // BRAF Rm 0000mmmm00100011
1385 // BSRF Rm 0000mmmm00000011
1387 tmp = rcache_get_reg(SHR_PC, RC_GR_WRITE);
1388 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_READ);
1389 emith_move_r_r(tmp, tmp2);
1391 emith_add_r_imm(tmp, pc + 2);
1393 tmp3 = rcache_get_reg(SHR_PR, RC_GR_WRITE);
1394 emith_move_r_imm(tmp3, pc + 2);
1395 emith_add_r_r(tmp, tmp3);
1400 case 0x04: // MOV.B Rm,@(R0,Rn) 0000nnnnmmmm0100
1401 case 0x05: // MOV.W Rm,@(R0,Rn) 0000nnnnmmmm0101
1402 case 0x06: // MOV.L Rm,@(R0,Rn) 0000nnnnmmmm0110
1404 tmp = rcache_get_reg_arg(1, GET_Rm());
1405 tmp2 = rcache_get_reg_arg(0, SHR_R0);
1406 tmp3 = rcache_get_reg(GET_Rn(), RC_GR_READ);
1407 emith_add_r_r(tmp2, tmp3);
1408 emit_memhandler_write(op & 3, pc, drcf.delayed_op);
1411 // MUL.L Rm,Rn 0000nnnnmmmm0111
1412 tmp = rcache_get_reg(GET_Rn(), RC_GR_READ);
1413 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1414 tmp3 = rcache_get_reg(SHR_MACL, RC_GR_WRITE);
1415 emith_mul(tmp3, tmp2, tmp);
1419 CHECK_UNHANDLED_BITS(0xf00);
1422 case 0: // CLRT 0000000000001000
1423 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1424 if (drcf.delayed_op)
1426 emith_bic_r_imm(sr, T);
1428 case 1: // SETT 0000000000011000
1429 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1430 if (drcf.delayed_op)
1432 emith_or_r_imm(sr, T);
1434 case 2: // CLRMAC 0000000000101000
1435 emit_move_r_imm32(SHR_MACL, 0);
1436 emit_move_r_imm32(SHR_MACH, 0);
1445 case 0: // NOP 0000000000001001
1446 CHECK_UNHANDLED_BITS(0xf00);
1448 case 1: // DIV0U 0000000000011001
1449 CHECK_UNHANDLED_BITS(0xf00);
1450 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1451 if (drcf.delayed_op)
1453 emith_bic_r_imm(sr, M|Q|T);
1455 case 2: // MOVT Rn 0000nnnn00101001
1456 sr = rcache_get_reg(SHR_SR, RC_GR_READ);
1457 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_WRITE);
1458 emith_clear_msb(tmp2, sr, 31);
1465 tmp = rcache_get_reg(GET_Rn(), RC_GR_WRITE);
1468 case 0: // STS MACH,Rn 0000nnnn00001010
1471 case 1: // STS MACL,Rn 0000nnnn00011010
1474 case 2: // STS PR,Rn 0000nnnn00101010
1480 tmp2 = rcache_get_reg(tmp2, RC_GR_READ);
1481 emith_move_r_r(tmp, tmp2);
1484 CHECK_UNHANDLED_BITS(0xf00);
1487 case 0: // RTS 0000000000001011
1489 emit_move_r_r(SHR_PC, SHR_PR);
1493 case 1: // SLEEP 0000000000011011
1494 tmp = rcache_get_reg(SHR_SR, RC_GR_RMW);
1495 emith_clear_msb(tmp, tmp, 20); // clear cycles
1496 out_pc = out_pc - 2;
1499 case 2: // RTE 0000000000101011
1502 emit_memhandler_read_rr(SHR_PC, SHR_SP, 0, 2);
1504 tmp = rcache_get_reg_arg(0, SHR_SP);
1505 emith_add_r_imm(tmp, 4);
1506 tmp = emit_memhandler_read(2);
1507 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1508 emith_write_sr(sr, tmp);
1509 rcache_free_tmp(tmp);
1510 tmp = rcache_get_reg(SHR_SP, RC_GR_RMW);
1511 emith_add_r_imm(tmp, 4*2);
1520 case 0x0c: // MOV.B @(R0,Rm),Rn 0000nnnnmmmm1100
1521 case 0x0d: // MOV.W @(R0,Rm),Rn 0000nnnnmmmm1101
1522 case 0x0e: // MOV.L @(R0,Rm),Rn 0000nnnnmmmm1110
1523 tmp = emit_indirect_indexed_read(SHR_R0, GET_Rm(), op & 3);
1524 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_WRITE);
1525 if ((op & 3) != 2) {
1526 emith_sext(tmp2, tmp, (op & 1) ? 16 : 8);
1528 emith_move_r_r(tmp2, tmp);
1529 rcache_free_tmp(tmp);
1531 case 0x0f: // MAC.L @Rm+,@Rn+ 0000nnnnmmmm1111
1532 emit_indirect_read_double(&tmp, &tmp2, GET_Rn(), GET_Rm(), 2);
1533 tmp4 = rcache_get_reg(SHR_MACH, RC_GR_RMW);
1534 /* MS 16 MAC bits unused if saturated */
1535 sr = rcache_get_reg(SHR_SR, RC_GR_READ);
1536 emith_tst_r_imm(sr, S);
1537 EMITH_SJMP_START(DCOND_EQ);
1538 emith_clear_msb_c(DCOND_NE, tmp4, tmp4, 16);
1539 EMITH_SJMP_END(DCOND_EQ);
1541 tmp3 = rcache_get_reg(SHR_MACL, RC_GR_RMW); // might evict SR
1542 emith_mula_s64(tmp3, tmp4, tmp, tmp2);
1543 rcache_free_tmp(tmp2);
1544 sr = rcache_get_reg(SHR_SR, RC_GR_READ); // reget just in case
1545 emith_tst_r_imm(sr, S);
1547 EMITH_JMP_START(DCOND_EQ);
1548 emith_asr(tmp, tmp4, 15);
1549 emith_cmp_r_imm(tmp, -1); // negative overflow (0x80000000..0xffff7fff)
1550 EMITH_SJMP_START(DCOND_GE);
1551 emith_move_r_imm_c(DCOND_LT, tmp4, 0x8000);
1552 emith_move_r_imm_c(DCOND_LT, tmp3, 0x0000);
1553 EMITH_SJMP_END(DCOND_GE);
1554 emith_cmp_r_imm(tmp, 0); // positive overflow (0x00008000..0x7fffffff)
1555 EMITH_SJMP_START(DCOND_LE);
1556 emith_move_r_imm_c(DCOND_GT, tmp4, 0x00007fff);
1557 emith_move_r_imm_c(DCOND_GT, tmp3, 0xffffffff);
1558 EMITH_SJMP_END(DCOND_LE);
1559 EMITH_JMP_END(DCOND_EQ);
1561 rcache_free_tmp(tmp);
1567 /////////////////////////////////////////////
1569 // MOV.L Rm,@(disp,Rn) 0001nnnnmmmmdddd
1571 tmp = rcache_get_reg_arg(0, GET_Rn());
1572 tmp2 = rcache_get_reg_arg(1, GET_Rm());
1574 emith_add_r_imm(tmp, (op & 0x0f) * 4);
1575 emit_memhandler_write(2, pc, drcf.delayed_op);
1581 case 0x00: // MOV.B Rm,@Rn 0010nnnnmmmm0000
1582 case 0x01: // MOV.W Rm,@Rn 0010nnnnmmmm0001
1583 case 0x02: // MOV.L Rm,@Rn 0010nnnnmmmm0010
1585 rcache_get_reg_arg(0, GET_Rn());
1586 rcache_get_reg_arg(1, GET_Rm());
1587 emit_memhandler_write(op & 3, pc, drcf.delayed_op);
1589 case 0x04: // MOV.B Rm,@–Rn 0010nnnnmmmm0100
1590 case 0x05: // MOV.W Rm,@–Rn 0010nnnnmmmm0101
1591 case 0x06: // MOV.L Rm,@–Rn 0010nnnnmmmm0110
1592 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1593 emith_sub_r_imm(tmp, (1 << (op & 3)));
1595 rcache_get_reg_arg(0, GET_Rn());
1596 rcache_get_reg_arg(1, GET_Rm());
1597 emit_memhandler_write(op & 3, pc, drcf.delayed_op);
1599 case 0x07: // DIV0S Rm,Rn 0010nnnnmmmm0111
1600 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1601 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_READ);
1602 tmp3 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1603 if (drcf.delayed_op)
1605 emith_bic_r_imm(sr, M|Q|T);
1606 emith_tst_r_imm(tmp2, (1<<31));
1607 EMITH_SJMP_START(DCOND_EQ);
1608 emith_or_r_imm_c(DCOND_NE, sr, Q);
1609 EMITH_SJMP_END(DCOND_EQ);
1610 emith_tst_r_imm(tmp3, (1<<31));
1611 EMITH_SJMP_START(DCOND_EQ);
1612 emith_or_r_imm_c(DCOND_NE, sr, M);
1613 EMITH_SJMP_END(DCOND_EQ);
1614 emith_teq_r_r(tmp2, tmp3);
1615 EMITH_SJMP_START(DCOND_PL);
1616 emith_or_r_imm_c(DCOND_MI, sr, T);
1617 EMITH_SJMP_END(DCOND_PL);
1619 case 0x08: // TST Rm,Rn 0010nnnnmmmm1000
1620 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1621 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_READ);
1622 tmp3 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1623 if (drcf.delayed_op)
1625 emith_bic_r_imm(sr, T);
1626 emith_tst_r_r(tmp2, tmp3);
1627 emit_or_t_if_eq(sr);
1629 case 0x09: // AND Rm,Rn 0010nnnnmmmm1001
1630 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1631 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1632 emith_and_r_r(tmp, tmp2);
1634 case 0x0a: // XOR Rm,Rn 0010nnnnmmmm1010
1635 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1636 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1637 emith_eor_r_r(tmp, tmp2);
1639 case 0x0b: // OR Rm,Rn 0010nnnnmmmm1011
1640 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1641 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1642 emith_or_r_r(tmp, tmp2);
1644 case 0x0c: // CMP/STR Rm,Rn 0010nnnnmmmm1100
1645 tmp = rcache_get_tmp();
1646 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_READ);
1647 tmp3 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1648 emith_eor_r_r_r(tmp, tmp2, tmp3);
1649 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1650 if (drcf.delayed_op)
1652 emith_bic_r_imm(sr, T);
1653 emith_tst_r_imm(tmp, 0x000000ff);
1654 emit_or_t_if_eq(tmp);
1655 emith_tst_r_imm(tmp, 0x0000ff00);
1656 emit_or_t_if_eq(tmp);
1657 emith_tst_r_imm(tmp, 0x00ff0000);
1658 emit_or_t_if_eq(tmp);
1659 emith_tst_r_imm(tmp, 0xff000000);
1660 emit_or_t_if_eq(tmp);
1661 rcache_free_tmp(tmp);
1663 case 0x0d: // XTRCT Rm,Rn 0010nnnnmmmm1101
1664 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1665 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1666 emith_lsr(tmp, tmp, 16);
1667 emith_or_r_r_lsl(tmp, tmp2, 16);
1669 case 0x0e: // MULU.W Rm,Rn 0010nnnnmmmm1110
1670 case 0x0f: // MULS.W Rm,Rn 0010nnnnmmmm1111
1671 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_READ);
1672 tmp = rcache_get_reg(SHR_MACL, RC_GR_WRITE);
1674 emith_sext(tmp, tmp2, 16);
1676 emith_clear_msb(tmp, tmp2, 16);
1677 tmp3 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1678 tmp2 = rcache_get_tmp();
1680 emith_sext(tmp2, tmp3, 16);
1682 emith_clear_msb(tmp2, tmp3, 16);
1683 emith_mul(tmp, tmp, tmp2);
1684 rcache_free_tmp(tmp2);
1685 // FIXME: causes timing issues in Doom?
1691 /////////////////////////////////////////////
1695 case 0x00: // CMP/EQ Rm,Rn 0011nnnnmmmm0000
1696 case 0x02: // CMP/HS Rm,Rn 0011nnnnmmmm0010
1697 case 0x03: // CMP/GE Rm,Rn 0011nnnnmmmm0011
1698 case 0x06: // CMP/HI Rm,Rn 0011nnnnmmmm0110
1699 case 0x07: // CMP/GT Rm,Rn 0011nnnnmmmm0111
1700 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1701 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_READ);
1702 tmp3 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1703 if (drcf.delayed_op)
1705 emith_bic_r_imm(sr, T);
1706 emith_cmp_r_r(tmp2, tmp3);
1709 case 0x00: // CMP/EQ
1710 emit_or_t_if_eq(sr);
1712 case 0x02: // CMP/HS
1713 EMITH_SJMP_START(DCOND_LO);
1714 emith_or_r_imm_c(DCOND_HS, sr, T);
1715 EMITH_SJMP_END(DCOND_LO);
1717 case 0x03: // CMP/GE
1718 EMITH_SJMP_START(DCOND_LT);
1719 emith_or_r_imm_c(DCOND_GE, sr, T);
1720 EMITH_SJMP_END(DCOND_LT);
1722 case 0x06: // CMP/HI
1723 EMITH_SJMP_START(DCOND_LS);
1724 emith_or_r_imm_c(DCOND_HI, sr, T);
1725 EMITH_SJMP_END(DCOND_LS);
1727 case 0x07: // CMP/GT
1728 EMITH_SJMP_START(DCOND_LE);
1729 emith_or_r_imm_c(DCOND_GT, sr, T);
1730 EMITH_SJMP_END(DCOND_LE);
1734 case 0x04: // DIV1 Rm,Rn 0011nnnnmmmm0100
1735 // Q1 = carry(Rn = (Rn << 1) | T)
1737 // Q2 = carry(Rn += Rm)
1739 // Q2 = carry(Rn -= Rm)
1741 // T = (Q == M) = !(Q ^ M) = !(Q1 ^ Q2)
1742 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1743 tmp3 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1744 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1745 if (drcf.delayed_op)
1747 emith_tpop_carry(sr, 0);
1748 emith_adcf_r_r(tmp2, tmp2);
1749 emith_tpush_carry(sr, 0); // keep Q1 in T for now
1750 tmp4 = rcache_get_tmp();
1751 emith_and_r_r_imm(tmp4, sr, M);
1752 emith_eor_r_r_lsr(sr, tmp4, M_SHIFT - Q_SHIFT); // Q ^= M
1753 rcache_free_tmp(tmp4);
1754 // add or sub, invert T if carry to get Q1 ^ Q2
1755 // in: (Q ^ M) passed in Q, Q1 in T
1756 emith_sh2_div1_step(tmp2, tmp3, sr);
1757 emith_bic_r_imm(sr, Q);
1758 emith_tst_r_imm(sr, M);
1759 EMITH_SJMP_START(DCOND_EQ);
1760 emith_or_r_imm_c(DCOND_NE, sr, Q); // Q = M
1761 EMITH_SJMP_END(DCOND_EQ);
1762 emith_tst_r_imm(sr, T);
1763 EMITH_SJMP_START(DCOND_EQ);
1764 emith_eor_r_imm_c(DCOND_NE, sr, Q); // Q = M ^ Q1 ^ Q2
1765 EMITH_SJMP_END(DCOND_EQ);
1766 emith_eor_r_imm(sr, T); // T = !(Q1 ^ Q2)
1768 case 0x05: // DMULU.L Rm,Rn 0011nnnnmmmm0101
1769 tmp = rcache_get_reg(GET_Rn(), RC_GR_READ);
1770 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1771 tmp3 = rcache_get_reg(SHR_MACL, RC_GR_WRITE);
1772 tmp4 = rcache_get_reg(SHR_MACH, RC_GR_WRITE);
1773 emith_mul_u64(tmp3, tmp4, tmp, tmp2);
1775 case 0x08: // SUB Rm,Rn 0011nnnnmmmm1000
1776 case 0x0c: // ADD Rm,Rn 0011nnnnmmmm1100
1777 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1778 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1780 emith_add_r_r(tmp, tmp2);
1782 emith_sub_r_r(tmp, tmp2);
1784 case 0x0a: // SUBC Rm,Rn 0011nnnnmmmm1010
1785 case 0x0e: // ADDC Rm,Rn 0011nnnnmmmm1110
1786 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1787 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1788 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1789 if (drcf.delayed_op)
1791 if (op & 4) { // adc
1792 emith_tpop_carry(sr, 0);
1793 emith_adcf_r_r(tmp, tmp2);
1794 emith_tpush_carry(sr, 0);
1796 emith_tpop_carry(sr, 1);
1797 emith_sbcf_r_r(tmp, tmp2);
1798 emith_tpush_carry(sr, 1);
1801 case 0x0b: // SUBV Rm,Rn 0011nnnnmmmm1011
1802 case 0x0f: // ADDV Rm,Rn 0011nnnnmmmm1111
1803 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1804 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1805 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1806 if (drcf.delayed_op)
1808 emith_bic_r_imm(sr, T);
1810 emith_addf_r_r(tmp, tmp2);
1812 emith_subf_r_r(tmp, tmp2);
1813 EMITH_SJMP_START(DCOND_VC);
1814 emith_or_r_imm_c(DCOND_VS, sr, T);
1815 EMITH_SJMP_END(DCOND_VC);
1817 case 0x0d: // DMULS.L Rm,Rn 0011nnnnmmmm1101
1818 tmp = rcache_get_reg(GET_Rn(), RC_GR_READ);
1819 tmp2 = rcache_get_reg(GET_Rm(), RC_GR_READ);
1820 tmp3 = rcache_get_reg(SHR_MACL, RC_GR_WRITE);
1821 tmp4 = rcache_get_reg(SHR_MACH, RC_GR_WRITE);
1822 emith_mul_s64(tmp3, tmp4, tmp, tmp2);
1827 /////////////////////////////////////////////
1834 case 0: // SHLL Rn 0100nnnn00000000
1835 case 2: // SHAL Rn 0100nnnn00100000
1836 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1837 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1838 if (drcf.delayed_op)
1840 emith_tpop_carry(sr, 0); // dummy
1841 emith_lslf(tmp, tmp, 1);
1842 emith_tpush_carry(sr, 0);
1844 case 1: // DT Rn 0100nnnn00010000
1845 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1846 if (drcf.delayed_op)
1848 if (FETCH_OP(pc) == 0x8bfd) { // BF #-2
1849 if (gconst_get(GET_Rn(), &tmp)) {
1850 // XXX: limit burned cycles
1851 emit_move_r_imm32(GET_Rn(), 0);
1852 emith_or_r_imm(sr, T);
1853 cycles += tmp * 4 + 1; // +1 syncs with noconst version, not sure why
1857 emith_sh2_dtbf_loop();
1860 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1861 emith_bic_r_imm(sr, T);
1862 emith_subf_r_imm(tmp, 1);
1863 emit_or_t_if_eq(sr);
1870 case 0: // SHLR Rn 0100nnnn00000001
1871 case 2: // SHAR Rn 0100nnnn00100001
1872 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1873 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1874 if (drcf.delayed_op)
1876 emith_tpop_carry(sr, 0); // dummy
1878 emith_asrf(tmp, tmp, 1);
1880 emith_lsrf(tmp, tmp, 1);
1881 emith_tpush_carry(sr, 0);
1883 case 1: // CMP/PZ Rn 0100nnnn00010001
1884 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1885 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1886 if (drcf.delayed_op)
1888 emith_bic_r_imm(sr, T);
1889 emith_cmp_r_imm(tmp, 0);
1890 EMITH_SJMP_START(DCOND_LT);
1891 emith_or_r_imm_c(DCOND_GE, sr, T);
1892 EMITH_SJMP_END(DCOND_LT);
1900 case 0x02: // STS.L MACH,@–Rn 0100nnnn00000010
1903 case 0x12: // STS.L MACL,@–Rn 0100nnnn00010010
1906 case 0x22: // STS.L PR,@–Rn 0100nnnn00100010
1909 case 0x03: // STC.L SR,@–Rn 0100nnnn00000011
1912 case 0x13: // STC.L GBR,@–Rn 0100nnnn00010011
1915 case 0x23: // STC.L VBR,@–Rn 0100nnnn00100011
1921 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1922 emith_sub_r_imm(tmp2, 4);
1924 rcache_get_reg_arg(0, GET_Rn());
1925 tmp3 = rcache_get_reg_arg(1, tmp);
1927 emith_clear_msb(tmp3, tmp3, 22); // reserved bits defined by ISA as 0
1928 emit_memhandler_write(2, pc, drcf.delayed_op);
1934 case 0x04: // ROTL Rn 0100nnnn00000100
1935 case 0x05: // ROTR Rn 0100nnnn00000101
1936 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1937 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1938 if (drcf.delayed_op)
1940 emith_tpop_carry(sr, 0); // dummy
1942 emith_rorf(tmp, tmp, 1);
1944 emith_rolf(tmp, tmp, 1);
1945 emith_tpush_carry(sr, 0);
1947 case 0x24: // ROTCL Rn 0100nnnn00100100
1948 case 0x25: // ROTCR Rn 0100nnnn00100101
1949 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1950 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1951 if (drcf.delayed_op)
1953 emith_tpop_carry(sr, 0);
1958 emith_tpush_carry(sr, 0);
1960 case 0x15: // CMP/PL Rn 0100nnnn00010101
1961 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
1962 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
1963 if (drcf.delayed_op)
1965 emith_bic_r_imm(sr, T);
1966 emith_cmp_r_imm(tmp, 0);
1967 EMITH_SJMP_START(DCOND_LE);
1968 emith_or_r_imm_c(DCOND_GT, sr, T);
1969 EMITH_SJMP_END(DCOND_LE);
1977 case 0x06: // LDS.L @Rm+,MACH 0100mmmm00000110
1980 case 0x16: // LDS.L @Rm+,MACL 0100mmmm00010110
1983 case 0x26: // LDS.L @Rm+,PR 0100mmmm00100110
1986 case 0x07: // LDC.L @Rm+,SR 0100mmmm00000111
1989 case 0x17: // LDC.L @Rm+,GBR 0100mmmm00010111
1992 case 0x27: // LDC.L @Rm+,VBR 0100mmmm00100111
1998 rcache_get_reg_arg(0, GET_Rn());
1999 tmp2 = emit_memhandler_read(2);
2000 if (tmp == SHR_SR) {
2001 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2002 if (drcf.delayed_op)
2004 emith_write_sr(sr, tmp2);
2007 tmp = rcache_get_reg(tmp, RC_GR_WRITE);
2008 emith_move_r_r(tmp, tmp2);
2010 rcache_free_tmp(tmp2);
2011 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
2012 emith_add_r_imm(tmp, 4);
2019 // SHLL2 Rn 0100nnnn00001000
2020 // SHLR2 Rn 0100nnnn00001001
2024 // SHLL8 Rn 0100nnnn00011000
2025 // SHLR8 Rn 0100nnnn00011001
2029 // SHLL16 Rn 0100nnnn00101000
2030 // SHLR16 Rn 0100nnnn00101001
2036 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_RMW);
2038 emith_lsr(tmp2, tmp2, tmp);
2040 emith_lsl(tmp2, tmp2, tmp);
2045 case 0: // LDS Rm,MACH 0100mmmm00001010
2048 case 1: // LDS Rm,MACL 0100mmmm00011010
2051 case 2: // LDS Rm,PR 0100mmmm00101010
2057 emit_move_r_r(tmp2, GET_Rn());
2062 case 0: // JSR @Rm 0100mmmm00001011
2063 case 2: // JMP @Rm 0100mmmm00101011
2066 emit_move_r_imm32(SHR_PR, pc + 2);
2067 emit_move_r_r(SHR_PC, (op >> 8) & 0x0f);
2071 case 1: // TAS.B @Rn 0100nnnn00011011
2072 // XXX: is TAS working on 32X?
2073 rcache_get_reg_arg(0, GET_Rn());
2074 tmp = emit_memhandler_read(0);
2075 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2076 if (drcf.delayed_op)
2078 emith_bic_r_imm(sr, T);
2079 emith_cmp_r_imm(tmp, 0);
2080 emit_or_t_if_eq(sr);
2082 emith_or_r_imm(tmp, 0x80);
2083 tmp2 = rcache_get_tmp_arg(1); // assuming it differs to tmp
2084 emith_move_r_r(tmp2, tmp);
2085 rcache_free_tmp(tmp);
2086 rcache_get_reg_arg(0, GET_Rn());
2087 emit_memhandler_write(0, pc, drcf.delayed_op);
2095 tmp = rcache_get_reg(GET_Rn(), RC_GR_READ);
2098 case 0: // LDC Rm,SR 0100mmmm00001110
2101 case 1: // LDC Rm,GBR 0100mmmm00011110
2104 case 2: // LDC Rm,VBR 0100mmmm00101110
2110 if (tmp2 == SHR_SR) {
2111 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2112 if (drcf.delayed_op)
2114 emith_write_sr(sr, tmp);
2117 tmp2 = rcache_get_reg(tmp2, RC_GR_WRITE);
2118 emith_move_r_r(tmp2, tmp);
2122 // MAC.W @Rm+,@Rn+ 0100nnnnmmmm1111
2123 emit_indirect_read_double(&tmp, &tmp2, GET_Rn(), GET_Rm(), 1);
2124 emith_sext(tmp, tmp, 16);
2125 emith_sext(tmp2, tmp2, 16);
2126 tmp3 = rcache_get_reg(SHR_MACL, RC_GR_RMW);
2127 tmp4 = rcache_get_reg(SHR_MACH, RC_GR_RMW);
2128 emith_mula_s64(tmp3, tmp4, tmp, tmp2);
2129 rcache_free_tmp(tmp2);
2130 // XXX: MACH should be untouched when S is set?
2131 sr = rcache_get_reg(SHR_SR, RC_GR_READ);
2132 emith_tst_r_imm(sr, S);
2133 EMITH_JMP_START(DCOND_EQ);
2135 emith_asr(tmp, tmp3, 31);
2136 emith_eorf_r_r(tmp, tmp4); // tmp = ((signed)macl >> 31) ^ mach
2137 EMITH_JMP_START(DCOND_EQ);
2138 emith_move_r_imm(tmp3, 0x80000000);
2139 emith_tst_r_r(tmp4, tmp4);
2140 EMITH_SJMP_START(DCOND_MI);
2141 emith_sub_r_imm_c(DCOND_PL, tmp3, 1); // positive
2142 EMITH_SJMP_END(DCOND_MI);
2143 EMITH_JMP_END(DCOND_EQ);
2145 EMITH_JMP_END(DCOND_EQ);
2146 rcache_free_tmp(tmp);
2152 /////////////////////////////////////////////
2154 // MOV.L @(disp,Rm),Rn 0101nnnnmmmmdddd
2155 emit_memhandler_read_rr(GET_Rn(), GET_Rm(), (op & 0x0f) * 4, 2);
2158 /////////////////////////////////////////////
2162 case 0x00: // MOV.B @Rm,Rn 0110nnnnmmmm0000
2163 case 0x01: // MOV.W @Rm,Rn 0110nnnnmmmm0001
2164 case 0x02: // MOV.L @Rm,Rn 0110nnnnmmmm0010
2165 case 0x04: // MOV.B @Rm+,Rn 0110nnnnmmmm0100
2166 case 0x05: // MOV.W @Rm+,Rn 0110nnnnmmmm0101
2167 case 0x06: // MOV.L @Rm+,Rn 0110nnnnmmmm0110
2168 emit_memhandler_read_rr(GET_Rn(), GET_Rm(), 0, op & 3);
2169 if ((op & 7) >= 4 && GET_Rn() != GET_Rm()) {
2170 tmp = rcache_get_reg(GET_Rm(), RC_GR_RMW);
2171 emith_add_r_imm(tmp, (1 << (op & 3)));
2176 tmp = rcache_get_reg(GET_Rm(), RC_GR_READ);
2177 tmp2 = rcache_get_reg(GET_Rn(), RC_GR_WRITE);
2180 case 0x03: // MOV Rm,Rn 0110nnnnmmmm0011
2181 emith_move_r_r(tmp2, tmp);
2183 case 0x07: // NOT Rm,Rn 0110nnnnmmmm0111
2184 emith_mvn_r_r(tmp2, tmp);
2186 case 0x08: // SWAP.B Rm,Rn 0110nnnnmmmm1000
2189 tmp3 = rcache_get_tmp();
2190 tmp4 = rcache_get_tmp();
2191 emith_lsr(tmp3, tmp, 16);
2192 emith_or_r_r_lsl(tmp3, tmp, 24);
2193 emith_and_r_r_imm(tmp4, tmp, 0xff00);
2194 emith_or_r_r_lsl(tmp3, tmp4, 8);
2195 emith_rol(tmp2, tmp3, 16);
2196 rcache_free_tmp(tmp4);
2198 rcache_free_tmp(tmp3);
2200 case 0x09: // SWAP.W Rm,Rn 0110nnnnmmmm1001
2201 emith_rol(tmp2, tmp, 16);
2203 case 0x0a: // NEGC Rm,Rn 0110nnnnmmmm1010
2204 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2205 if (drcf.delayed_op)
2207 emith_tpop_carry(sr, 1);
2208 emith_negcf_r_r(tmp2, tmp);
2209 emith_tpush_carry(sr, 1);
2211 case 0x0b: // NEG Rm,Rn 0110nnnnmmmm1011
2212 emith_neg_r_r(tmp2, tmp);
2214 case 0x0c: // EXTU.B Rm,Rn 0110nnnnmmmm1100
2215 emith_clear_msb(tmp2, tmp, 24);
2217 case 0x0d: // EXTU.W Rm,Rn 0110nnnnmmmm1101
2218 emith_clear_msb(tmp2, tmp, 16);
2220 case 0x0e: // EXTS.B Rm,Rn 0110nnnnmmmm1110
2221 emith_sext(tmp2, tmp, 8);
2223 case 0x0f: // EXTS.W Rm,Rn 0110nnnnmmmm1111
2224 emith_sext(tmp2, tmp, 16);
2231 /////////////////////////////////////////////
2233 // ADD #imm,Rn 0111nnnniiiiiiii
2234 tmp = rcache_get_reg(GET_Rn(), RC_GR_RMW);
2235 if (op & 0x80) { // adding negative
2236 emith_sub_r_imm(tmp, -op & 0xff);
2238 emith_add_r_imm(tmp, op & 0xff);
2241 /////////////////////////////////////////////
2243 switch (op & 0x0f00)
2245 case 0x0000: // MOV.B R0,@(disp,Rn) 10000000nnnndddd
2246 case 0x0100: // MOV.W R0,@(disp,Rn) 10000001nnnndddd
2248 tmp = rcache_get_reg_arg(0, GET_Rm());
2249 tmp2 = rcache_get_reg_arg(1, SHR_R0);
2250 tmp3 = (op & 0x100) >> 8;
2252 emith_add_r_imm(tmp, (op & 0x0f) << tmp3);
2253 emit_memhandler_write(tmp3, pc, drcf.delayed_op);
2255 case 0x0400: // MOV.B @(disp,Rm),R0 10000100mmmmdddd
2256 case 0x0500: // MOV.W @(disp,Rm),R0 10000101mmmmdddd
2257 tmp = (op & 0x100) >> 8;
2258 emit_memhandler_read_rr(SHR_R0, GET_Rm(), (op & 0x0f) << tmp, tmp);
2260 case 0x0800: // CMP/EQ #imm,R0 10001000iiiiiiii
2261 // XXX: could use cmn
2262 tmp = rcache_get_tmp();
2263 tmp2 = rcache_get_reg(0, RC_GR_READ);
2264 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2265 if (drcf.delayed_op)
2267 emith_move_r_imm_s8(tmp, op & 0xff);
2268 emith_bic_r_imm(sr, T);
2269 emith_cmp_r_r(tmp2, tmp);
2270 emit_or_t_if_eq(sr);
2271 rcache_free_tmp(tmp);
2273 case 0x0d00: // BT/S label 10001101dddddddd
2274 case 0x0f00: // BF/S label 10001111dddddddd
2278 case 0x0900: // BT label 10001001dddddddd
2279 case 0x0b00: // BF label 10001011dddddddd
2280 // will handle conditional branches later
2281 pending_branch_cond = (op & 0x0200) ? DCOND_EQ : DCOND_NE;
2282 i = ((signed int)(op << 24) >> 23);
2283 pending_branch_pc = pc + i + 2;
2289 /////////////////////////////////////////////
2291 // MOV.W @(disp,PC),Rn 1001nnnndddddddd
2292 tmp = pc + (op & 0xff) * 2 + 2;
2293 #if PROPAGATE_CONSTANTS
2294 if (tmp < end_pc + MAX_LITERAL_OFFSET) {
2295 gconst_new(GET_Rn(), (u32)(int)(signed short)FETCH_OP(tmp));
2296 if (last_inlined_literal < tmp)
2297 last_inlined_literal = tmp;
2302 tmp2 = rcache_get_tmp_arg(0);
2303 emith_move_r_imm(tmp2, tmp);
2304 tmp2 = emit_memhandler_read(1);
2305 tmp3 = rcache_get_reg(GET_Rn(), RC_GR_WRITE);
2306 emith_sext(tmp3, tmp2, 16);
2307 rcache_free_tmp(tmp2);
2311 /////////////////////////////////////////////
2313 // BRA label 1010dddddddddddd
2315 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2316 tmp = ((signed int)(op << 20) >> 19);
2317 out_pc = pc + tmp + 2;
2319 emith_clear_msb(sr, sr, 20); // burn cycles
2323 /////////////////////////////////////////////
2325 // BSR label 1011dddddddddddd
2327 emit_move_r_imm32(SHR_PR, pc + 2);
2328 tmp = ((signed int)(op << 20) >> 19);
2329 out_pc = pc + tmp + 2;
2333 /////////////////////////////////////////////
2335 switch (op & 0x0f00)
2337 case 0x0000: // MOV.B R0,@(disp,GBR) 11000000dddddddd
2338 case 0x0100: // MOV.W R0,@(disp,GBR) 11000001dddddddd
2339 case 0x0200: // MOV.L R0,@(disp,GBR) 11000010dddddddd
2341 tmp = rcache_get_reg_arg(0, SHR_GBR);
2342 tmp2 = rcache_get_reg_arg(1, SHR_R0);
2343 tmp3 = (op & 0x300) >> 8;
2344 emith_add_r_imm(tmp, (op & 0xff) << tmp3);
2345 emit_memhandler_write(tmp3, pc, drcf.delayed_op);
2347 case 0x0400: // MOV.B @(disp,GBR),R0 11000100dddddddd
2348 case 0x0500: // MOV.W @(disp,GBR),R0 11000101dddddddd
2349 case 0x0600: // MOV.L @(disp,GBR),R0 11000110dddddddd
2350 tmp = (op & 0x300) >> 8;
2351 emit_memhandler_read_rr(SHR_R0, SHR_GBR, (op & 0xff) << tmp, tmp);
2353 case 0x0300: // TRAPA #imm 11000011iiiiiiii
2354 tmp = rcache_get_reg(SHR_SP, RC_GR_RMW);
2355 emith_sub_r_imm(tmp, 4*2);
2357 tmp = rcache_get_reg_arg(0, SHR_SP);
2358 emith_add_r_imm(tmp, 4);
2359 tmp = rcache_get_reg_arg(1, SHR_SR);
2360 emith_clear_msb(tmp, tmp, 22);
2361 emit_memhandler_write(2, pc, drcf.delayed_op);
2363 rcache_get_reg_arg(0, SHR_SP);
2364 tmp = rcache_get_tmp_arg(1);
2365 emith_move_r_imm(tmp, pc);
2366 emit_memhandler_write(2, pc, drcf.delayed_op);
2368 emit_memhandler_read_rr(SHR_PC, SHR_VBR, (op & 0xff) * 4, 2);
2372 case 0x0700: // MOVA @(disp,PC),R0 11000111dddddddd
2373 emit_move_r_imm32(SHR_R0, (pc + (op & 0xff) * 4 + 2) & ~3);
2375 case 0x0800: // TST #imm,R0 11001000iiiiiiii
2376 tmp = rcache_get_reg(SHR_R0, RC_GR_READ);
2377 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2378 if (drcf.delayed_op)
2380 emith_bic_r_imm(sr, T);
2381 emith_tst_r_imm(tmp, op & 0xff);
2382 emit_or_t_if_eq(sr);
2384 case 0x0900: // AND #imm,R0 11001001iiiiiiii
2385 tmp = rcache_get_reg(SHR_R0, RC_GR_RMW);
2386 emith_and_r_imm(tmp, op & 0xff);
2388 case 0x0a00: // XOR #imm,R0 11001010iiiiiiii
2389 tmp = rcache_get_reg(SHR_R0, RC_GR_RMW);
2390 emith_eor_r_imm(tmp, op & 0xff);
2392 case 0x0b00: // OR #imm,R0 11001011iiiiiiii
2393 tmp = rcache_get_reg(SHR_R0, RC_GR_RMW);
2394 emith_or_r_imm(tmp, op & 0xff);
2396 case 0x0c00: // TST.B #imm,@(R0,GBR) 11001100iiiiiiii
2397 tmp = emit_indirect_indexed_read(SHR_R0, SHR_GBR, 0);
2398 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2399 if (drcf.delayed_op)
2401 emith_bic_r_imm(sr, T);
2402 emith_tst_r_imm(tmp, op & 0xff);
2403 emit_or_t_if_eq(sr);
2404 rcache_free_tmp(tmp);
2407 case 0x0d00: // AND.B #imm,@(R0,GBR) 11001101iiiiiiii
2408 tmp = emit_indirect_indexed_read(SHR_R0, SHR_GBR, 0);
2409 emith_and_r_imm(tmp, op & 0xff);
2411 case 0x0e00: // XOR.B #imm,@(R0,GBR) 11001110iiiiiiii
2412 tmp = emit_indirect_indexed_read(SHR_R0, SHR_GBR, 0);
2413 emith_eor_r_imm(tmp, op & 0xff);
2415 case 0x0f00: // OR.B #imm,@(R0,GBR) 11001111iiiiiiii
2416 tmp = emit_indirect_indexed_read(SHR_R0, SHR_GBR, 0);
2417 emith_or_r_imm(tmp, op & 0xff);
2419 tmp2 = rcache_get_tmp_arg(1);
2420 emith_move_r_r(tmp2, tmp);
2421 rcache_free_tmp(tmp);
2422 tmp3 = rcache_get_reg_arg(0, SHR_GBR);
2423 tmp4 = rcache_get_reg(SHR_R0, RC_GR_READ);
2424 emith_add_r_r(tmp3, tmp4);
2425 emit_memhandler_write(0, pc, drcf.delayed_op);
2431 /////////////////////////////////////////////
2433 // MOV.L @(disp,PC),Rn 1101nnnndddddddd
2434 tmp = (pc + (op & 0xff) * 4 + 2) & ~3;
2435 #if PROPAGATE_CONSTANTS
2436 if (tmp < end_pc + MAX_LITERAL_OFFSET) {
2437 gconst_new(GET_Rn(), FETCH32(tmp));
2438 if (last_inlined_literal < tmp)
2439 last_inlined_literal = tmp;
2444 tmp2 = rcache_get_tmp_arg(0);
2445 emith_move_r_imm(tmp2, tmp);
2446 tmp2 = emit_memhandler_read(2);
2447 tmp3 = rcache_get_reg(GET_Rn(), RC_GR_WRITE);
2448 emith_move_r_r(tmp3, tmp2);
2449 rcache_free_tmp(tmp2);
2453 /////////////////////////////////////////////
2455 // MOV #imm,Rn 1110nnnniiiiiiii
2456 emit_move_r_imm32(GET_Rn(), (u32)(signed int)(signed char)op);
2461 elprintf(EL_ANOMALY, "%csh2 drc: unhandled op %04x @ %08x",
2462 sh2->is_slave ? 's' : 'm', op, pc - 2);
2463 #ifdef DRC_DEBUG_INTERP
2464 emit_move_r_imm32(SHR_PC, pc - 2);
2466 emith_pass_arg_r(0, CONTEXT_REG);
2467 emith_pass_arg_imm(1, op);
2468 emith_call(sh2_do_op);
2474 rcache_unlock_all();
2476 // conditional branch handling (with/without delay)
2477 if (pending_branch_cond != -1 && drcf.delayed_op != 2)
2479 u32 target_pc = pending_branch_pc;
2482 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2486 if (drcf.use_saved_t)
2487 emith_tst_r_imm(sr, T_save);
2489 emith_tst_r_imm(sr, T);
2492 if (find_in_array(branch_target_pc, branch_target_count, target_pc) >= 0) {
2494 // XXX: jumps back can be linked already
2495 branch_patch_pc[branch_patch_count] = target_pc;
2496 branch_patch_ptr[branch_patch_count] = tcache_ptr;
2497 emith_jump_cond_patchable(pending_branch_cond, tcache_ptr);
2499 branch_patch_count++;
2500 if (branch_patch_count == MAX_LOCAL_BRANCHES) {
2501 printf("warning: too many local branches\n");
2508 // can't resolve branch locally, make a block exit
2509 emit_move_r_imm32(SHR_PC, target_pc);
2512 target = dr_prepare_ext_branch(target_pc, sh2, tcache_id);
2515 emith_jump_cond_patchable(pending_branch_cond, target);
2518 drcf.use_saved_t = 0;
2519 pending_branch_cond = -1;
2523 // XXX: delay slots..
2524 if (drcf.test_irq && drcf.delayed_op != 2) {
2525 if (!drcf.delayed_op)
2526 emit_move_r_imm32(SHR_PC, pc);
2527 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2530 emith_call(sh2_drc_test_irq);
2534 do_host_disasm(tcache_id);
2536 if (out_pc != 0 && drcf.delayed_op != 2)
2540 tmp = rcache_get_reg(SHR_SR, RC_GR_RMW);
2544 if (out_pc == (u32)-1) {
2545 // indirect jump -> back to dispatcher
2546 emith_jump(sh2_drc_dispatcher);
2551 emit_move_r_imm32(SHR_PC, out_pc);
2554 target = dr_prepare_ext_branch(out_pc, sh2, tcache_id);
2557 emith_jump_patchable(target);
2560 // link local branches
2561 for (i = 0; i < branch_patch_count; i++) {
2564 t = find_in_array(branch_target_pc, branch_target_count, branch_patch_pc[i]);
2565 target = branch_target_ptr[t];
2566 if (target == NULL) {
2567 // flush pc and go back to dispatcher (should no longer happen)
2568 printf("stray branch to %08x %p\n", branch_patch_pc[i], tcache_ptr);
2569 target = tcache_ptr;
2570 emit_move_r_imm32(SHR_PC, branch_patch_pc[i]);
2572 emith_jump(sh2_drc_dispatcher);
2574 emith_jump_patch(branch_patch_ptr[i], target);
2578 if (last_inlined_literal > end_pc)
2579 end_pc = last_inlined_literal + 4;
2581 // mark memory blocks as containing compiled code
2582 // override any overlay blocks as they become unreachable anyway
2583 if (tcache_id != 0 || (this_block->addr & 0xc7fc0000) == 0x06000000)
2585 u16 *drc_ram_blk = NULL;
2586 u32 mask = 0, shift = 0;
2588 if (tcache_id != 0) {
2590 drc_ram_blk = Pico32xMem->drcblk_da[sh2->is_slave];
2591 shift = SH2_DRCBLK_DA_SHIFT;
2594 else if ((this_block->addr & 0xc7fc0000) == 0x06000000) {
2596 drc_ram_blk = Pico32xMem->drcblk_ram;
2597 shift = SH2_DRCBLK_RAM_SHIFT;
2601 drc_ram_blk[(base_pc >> shift) & mask] = (blkid_main << 1) | 1;
2602 for (pc = base_pc + 2; pc < end_pc; pc += 2)
2603 drc_ram_blk[(pc >> shift) & mask] = blkid_main << 1;
2605 // mark subblocks too
2606 for (i = 0; i < branch_target_count; i++)
2607 if (branch_target_blkid[i] != 0)
2608 drc_ram_blk[(branch_target_pc[i] >> shift) & mask] =
2609 branch_target_blkid[i] << 1;
2612 tcache_ptrs[tcache_id] = tcache_ptr;
2614 host_instructions_updated(block_entry, tcache_ptr);
2616 do_host_disasm(tcache_id);
2617 dbg(1, " block #%d,%d tcache %d/%d, insns %d -> %d %.3f",
2618 tcache_id, block_counts[tcache_id],
2619 tcache_ptr - tcache_bases[tcache_id], tcache_sizes[tcache_id],
2620 insns_compiled, host_insn_count, (double)host_insn_count / insns_compiled);
2621 if ((sh2->pc & 0xc6000000) == 0x02000000) // ROM
2622 dbg(1, " hash collisions %d/%d", hash_collisions, block_counts[tcache_id]);
2625 tcache_dsm_ptrs[tcache_id] = block_entry;
2626 do_host_disasm(tcache_id);
2637 static void sh2_generate_utils(void)
2639 int arg0, arg1, arg2, sr, tmp;
2640 void *sh2_drc_write_end, *sh2_drc_write_slot_end;
2642 sh2_drc_write32 = p32x_sh2_write32;
2643 sh2_drc_read8 = p32x_sh2_read8;
2644 sh2_drc_read16 = p32x_sh2_read16;
2645 sh2_drc_read32 = p32x_sh2_read32;
2647 host_arg2reg(arg0, 0);
2648 host_arg2reg(arg1, 1);
2649 host_arg2reg(arg2, 2);
2650 emith_move_r_r(arg0, arg0); // nop
2652 // sh2_drc_exit(void)
2653 sh2_drc_exit = (void *)tcache_ptr;
2654 emit_do_static_regs(1, arg2);
2655 emith_sh2_drc_exit();
2657 // sh2_drc_dispatcher(void)
2658 sh2_drc_dispatcher = (void *)tcache_ptr;
2659 sr = rcache_get_reg(SHR_SR, RC_GR_READ);
2660 emith_cmp_r_imm(sr, 0);
2661 emith_jump_cond(DCOND_LT, sh2_drc_exit);
2662 rcache_invalidate();
2663 emith_ctx_read(arg0, SHR_PC * 4);
2664 emith_ctx_read(arg1, offsetof(SH2, is_slave));
2665 emith_add_r_r_imm(arg2, CONTEXT_REG, offsetof(SH2, drc_tmp));
2666 emith_call(dr_lookup_block);
2668 // lookup failed, call sh2_translate()
2669 emith_move_r_r(arg0, CONTEXT_REG);
2670 emith_ctx_read(arg1, offsetof(SH2, drc_tmp)); // tcache_id
2671 emith_call(sh2_translate);
2673 // sh2_translate() failed, flush cache and retry
2674 emith_ctx_read(arg0, offsetof(SH2, drc_tmp));
2675 emith_call(flush_tcache);
2676 emith_move_r_r(arg0, CONTEXT_REG);
2677 emith_ctx_read(arg1, offsetof(SH2, drc_tmp));
2678 emith_call(sh2_translate);
2680 // XXX: can't translate, fail
2683 // sh2_drc_test_irq(void)
2684 // assumes it's called from main function (may jump to dispatcher)
2685 sh2_drc_test_irq = (void *)tcache_ptr;
2686 emith_ctx_read(arg1, offsetof(SH2, pending_level));
2687 sr = rcache_get_reg(SHR_SR, RC_GR_READ);
2688 emith_lsr(arg0, sr, I_SHIFT);
2689 emith_and_r_imm(arg0, 0x0f);
2690 emith_cmp_r_r(arg1, arg0); // pending_level > ((sr >> 4) & 0x0f)?
2691 EMITH_SJMP_START(DCOND_GT);
2692 emith_ret_c(DCOND_LE); // nope, return
2693 EMITH_SJMP_END(DCOND_GT);
2695 tmp = rcache_get_reg(SHR_SP, RC_GR_RMW);
2696 emith_sub_r_imm(tmp, 4*2);
2699 tmp = rcache_get_reg_arg(0, SHR_SP);
2700 emith_add_r_imm(tmp, 4);
2701 tmp = rcache_get_reg_arg(1, SHR_SR);
2702 emith_clear_msb(tmp, tmp, 22);
2703 emith_move_r_r(arg2, CONTEXT_REG);
2704 emith_call(p32x_sh2_write32); // XXX: use sh2_drc_write32?
2705 rcache_invalidate();
2707 rcache_get_reg_arg(0, SHR_SP);
2708 emith_ctx_read(arg1, SHR_PC * 4);
2709 emith_move_r_r(arg2, CONTEXT_REG);
2710 emith_call(p32x_sh2_write32);
2711 rcache_invalidate();
2712 // update I, cycles, do callback
2713 emith_ctx_read(arg1, offsetof(SH2, pending_level));
2714 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2715 emith_bic_r_imm(sr, I);
2716 emith_or_r_r_lsl(sr, arg1, I_SHIFT);
2717 emith_sub_r_imm(sr, 13 << 12); // at least 13 cycles
2719 emith_move_r_r(arg0, CONTEXT_REG);
2720 emith_call_ctx(offsetof(SH2, irq_callback)); // vector = sh2->irq_callback(sh2, level);
2722 emith_lsl(arg0, arg0, 2);
2723 emith_ctx_read(arg1, SHR_VBR * 4);
2724 emith_add_r_r(arg0, arg1);
2725 emit_memhandler_read(2);
2726 emith_ctx_write(arg0, SHR_PC * 4);
2728 emith_add_r_imm(xSP, 4); // fix stack
2730 emith_jump(sh2_drc_dispatcher);
2731 rcache_invalidate();
2733 // sh2_drc_entry(SH2 *sh2)
2734 sh2_drc_entry = (void *)tcache_ptr;
2735 emith_sh2_drc_entry();
2736 emith_move_r_r(CONTEXT_REG, arg0); // move ctx, arg0
2737 emit_do_static_regs(0, arg2);
2738 emith_call(sh2_drc_test_irq);
2739 emith_jump(sh2_drc_dispatcher);
2741 // write-caused irq detection
2742 sh2_drc_write_end = tcache_ptr;
2743 emith_tst_r_r(arg0, arg0);
2744 EMITH_SJMP_START(DCOND_NE);
2745 emith_jump_ctx_c(DCOND_EQ, offsetof(SH2, drc_tmp)); // return
2746 EMITH_SJMP_END(DCOND_NE);
2747 emith_call(sh2_drc_test_irq);
2748 emith_jump_ctx(offsetof(SH2, drc_tmp));
2750 // write-caused irq detection for writes in delay slot
2751 sh2_drc_write_slot_end = tcache_ptr;
2752 emith_tst_r_r(arg0, arg0);
2753 EMITH_SJMP_START(DCOND_NE);
2754 emith_jump_ctx_c(DCOND_EQ, offsetof(SH2, drc_tmp));
2755 EMITH_SJMP_END(DCOND_NE);
2756 // just burn cycles to get back to dispatcher after branch is handled
2757 sr = rcache_get_reg(SHR_SR, RC_GR_RMW);
2758 emith_ctx_write(sr, offsetof(SH2, irq_cycles));
2759 emith_clear_msb(sr, sr, 20); // clear cycles
2761 emith_jump_ctx(offsetof(SH2, drc_tmp));
2763 // sh2_drc_write8(u32 a, u32 d)
2764 sh2_drc_write8 = (void *)tcache_ptr;
2765 emith_ret_to_ctx(offsetof(SH2, drc_tmp));
2766 emith_ctx_read(arg2, offsetof(SH2, write8_tab));
2767 emith_sh2_wcall(arg0, arg2, sh2_drc_write_end);
2769 // sh2_drc_write16(u32 a, u32 d)
2770 sh2_drc_write16 = (void *)tcache_ptr;
2771 emith_ret_to_ctx(offsetof(SH2, drc_tmp));
2772 emith_ctx_read(arg2, offsetof(SH2, write16_tab));
2773 emith_sh2_wcall(arg0, arg2, sh2_drc_write_end);
2775 // sh2_drc_write8_slot(u32 a, u32 d)
2776 sh2_drc_write8_slot = (void *)tcache_ptr;
2777 emith_ret_to_ctx(offsetof(SH2, drc_tmp));
2778 emith_ctx_read(arg2, offsetof(SH2, write8_tab));
2779 emith_sh2_wcall(arg0, arg2, sh2_drc_write_slot_end);
2781 // sh2_drc_write16_slot(u32 a, u32 d)
2782 sh2_drc_write16_slot = (void *)tcache_ptr;
2783 emith_ret_to_ctx(offsetof(SH2, drc_tmp));
2784 emith_ctx_read(arg2, offsetof(SH2, write16_tab));
2785 emith_sh2_wcall(arg0, arg2, sh2_drc_write_slot_end);
2789 #define MAKE_READ_WRAPPER(func) { \
2790 void *tmp = (void *)tcache_ptr; \
2793 emith_ctx_read(arg2, offsetof(SH2, pdb_io_csum[0])); \
2794 emith_addf_r_r(arg2, arg0); \
2795 emith_ctx_write(arg2, offsetof(SH2, pdb_io_csum[0])); \
2796 emith_ctx_read(arg2, offsetof(SH2, pdb_io_csum[1])); \
2797 emith_adc_r_imm(arg2, 0x01000000); \
2798 emith_ctx_write(arg2, offsetof(SH2, pdb_io_csum[1])); \
2799 emith_pop_and_ret(); \
2802 #define MAKE_WRITE_WRAPPER(func) { \
2803 void *tmp = (void *)tcache_ptr; \
2804 emith_ctx_read(arg2, offsetof(SH2, pdb_io_csum[0])); \
2805 emith_addf_r_r(arg2, arg1); \
2806 emith_ctx_write(arg2, offsetof(SH2, pdb_io_csum[0])); \
2807 emith_ctx_read(arg2, offsetof(SH2, pdb_io_csum[1])); \
2808 emith_adc_r_imm(arg2, 0x01000000); \
2809 emith_ctx_write(arg2, offsetof(SH2, pdb_io_csum[1])); \
2810 emith_move_r_r(arg2, CONTEXT_REG); \
2815 MAKE_READ_WRAPPER(sh2_drc_read8);
2816 MAKE_READ_WRAPPER(sh2_drc_read16);
2817 MAKE_READ_WRAPPER(sh2_drc_read32);
2818 MAKE_WRITE_WRAPPER(sh2_drc_write8);
2819 MAKE_WRITE_WRAPPER(sh2_drc_write8_slot);
2820 MAKE_WRITE_WRAPPER(sh2_drc_write16);
2821 MAKE_WRITE_WRAPPER(sh2_drc_write16_slot);
2822 MAKE_WRITE_WRAPPER(sh2_drc_write32);
2824 host_dasm_new_symbol(sh2_drc_read8);
2825 host_dasm_new_symbol(sh2_drc_read16);
2826 host_dasm_new_symbol(sh2_drc_read32);
2827 host_dasm_new_symbol(sh2_drc_write32);
2831 rcache_invalidate();
2833 host_dasm_new_symbol(sh2_drc_entry);
2834 host_dasm_new_symbol(sh2_drc_dispatcher);
2835 host_dasm_new_symbol(sh2_drc_exit);
2836 host_dasm_new_symbol(sh2_drc_test_irq);
2837 host_dasm_new_symbol(sh2_drc_write_end);
2838 host_dasm_new_symbol(sh2_drc_write_slot_end);
2839 host_dasm_new_symbol(sh2_drc_write8);
2840 host_dasm_new_symbol(sh2_drc_write8_slot);
2841 host_dasm_new_symbol(sh2_drc_write16);
2842 host_dasm_new_symbol(sh2_drc_write16_slot);
2846 static void *sh2_smc_rm_block_entry(block_desc *bd, int tcache_id)
2848 // XXX: kill links somehow?
2849 dbg(1, " killing entry %08x, blkid %d", bd->addr, bd - block_tables[tcache_id]);
2851 // since we never reuse space of dead blocks,
2852 // insert jump to dispatcher for blocks that are linked to this point
2853 emith_jump_at(bd->tcache_ptr, sh2_drc_dispatcher);
2854 return bd->tcache_ptr;
2857 static void sh2_smc_rm_block(u32 a, u16 *drc_ram_blk, int tcache_id, u32 shift, u32 mask)
2859 //block_link *bl = block_links[tcache_id];
2860 //int bl_count = block_link_counts[tcache_id];
2861 block_desc *btab = block_tables[tcache_id];
2862 u16 *p = drc_ram_blk + ((a & mask) >> shift);
2863 u16 *pe = drc_ram_blk + (mask >> shift);
2864 void *tcache_min, *tcache_max;
2865 int main_id, prev_id = 0;
2867 while (p > drc_ram_blk && (*p & 1) == 0)
2871 printf("smc rm: missing block start for %08x?\n", a);
2873 tcache_min = tcache_max = sh2_smc_rm_block_entry(&btab[main_id], tcache_id);
2875 for (*p++ = 0; p <= pe && *p != 0 && !(*p & 1); p++) {
2877 if (id != main_id && id != prev_id)
2878 tcache_max = sh2_smc_rm_block_entry(&btab[*p >> 1], tcache_id);
2883 host_instructions_updated(tcache_min, (void *)((char *)tcache_max + 4));
2886 void sh2_drc_wcheck_ram(unsigned int a, int val, int cpuid)
2888 dbg(1, "%csh2 smc check @%08x", cpuid ? 's' : 'm', a);
2889 sh2_smc_rm_block(a, Pico32xMem->drcblk_ram, 0, SH2_DRCBLK_RAM_SHIFT, 0x3ffff);
2892 void sh2_drc_wcheck_da(unsigned int a, int val, int cpuid)
2894 dbg(1, "%csh2 smc check @%08x", cpuid ? 's' : 'm', a);
2895 sh2_smc_rm_block(a, Pico32xMem->drcblk_da[cpuid],
2896 1 + cpuid, SH2_DRCBLK_DA_SHIFT, 0xfff);
2899 void sh2_execute(SH2 *sh2c, int cycles)
2904 sh2c->cycles_aim += cycles;
2905 cycles = sh2c->cycles_aim - sh2c->cycles_done;
2907 // cycles are kept in SHR_SR unused bits (upper 20)
2908 // bit19 contains T saved for delay slot
2909 // others are usual SH2 flags
2911 sh2c->sr |= cycles << 12;
2912 sh2_drc_entry(sh2c);
2915 ret_cycles = (signed int)sh2c->sr >> 12;
2917 printf("warning: drc returned with cycles: %d\n", ret_cycles);
2919 sh2c->cycles_done += cycles - ret_cycles;
2923 void block_stats(void)
2925 int c, b, i, total = 0;
2927 printf("block stats:\n");
2928 for (b = 0; b < ARRAY_SIZE(block_tables); b++)
2929 for (i = 0; i < block_counts[b]; i++)
2930 if (block_tables[b][i].addr != 0)
2931 total += block_tables[b][i].refcount;
2933 for (c = 0; c < 10; c++) {
2934 block_desc *blk, *maxb = NULL;
2936 for (b = 0; b < ARRAY_SIZE(block_tables); b++) {
2937 for (i = 0; i < block_counts[b]; i++) {
2938 blk = &block_tables[b][i];
2939 if (blk->addr != 0 && blk->refcount > max) {
2940 max = blk->refcount;
2947 printf("%08x %9d %2.3f%%\n", maxb->addr, maxb->refcount,
2948 (double)maxb->refcount / total * 100.0);
2952 for (b = 0; b < ARRAY_SIZE(block_tables); b++)
2953 for (i = 0; i < block_counts[b]; i++)
2954 block_tables[b][i].refcount = 0;
2957 #define block_stats()
2960 void sh2_drc_flush_all(void)
2968 void sh2_drc_mem_setup(SH2 *sh2)
2970 // fill the convenience pointers
2971 sh2->p_bios = sh2->is_slave ? Pico32xMem->sh2_rom_s : Pico32xMem->sh2_rom_m;
2972 sh2->p_da = Pico32xMem->data_array[sh2->is_slave];
2973 sh2->p_sdram = Pico32xMem->sdram;
2974 sh2->p_rom = Pico.rom;
2977 int sh2_drc_init(SH2 *sh2)
2981 if (block_tables[0] == NULL)
2983 for (i = 0; i < TCACHE_BUFFERS; i++) {
2984 block_tables[i] = calloc(block_max_counts[i], sizeof(*block_tables[0]));
2985 if (block_tables[i] == NULL)
2987 // max 2 block links (exits) per block
2988 block_links[i] = calloc(block_max_counts[i] * 2, sizeof(*block_links[0]));
2989 if (block_links[i] == NULL)
2992 memset(block_counts, 0, sizeof(block_counts));
2993 memset(block_link_counts, 0, sizeof(block_link_counts));
2996 tcache_ptr = tcache;
2997 sh2_generate_utils();
2998 host_instructions_updated(tcache, tcache_ptr);
3000 tcache_bases[0] = tcache_ptrs[0] = tcache_ptr;
3001 for (i = 1; i < ARRAY_SIZE(tcache_bases); i++)
3002 tcache_bases[i] = tcache_ptrs[i] = tcache_bases[i - 1] + tcache_sizes[i - 1];
3005 PicoOpt |= POPT_DIS_VDP_FIFO;
3008 for (i = 0; i < ARRAY_SIZE(block_tables); i++)
3009 tcache_dsm_ptrs[i] = tcache_bases[i];
3011 tcache_dsm_ptrs[0] = tcache;
3015 hash_collisions = 0;
3019 if (hash_table == NULL) {
3020 hash_table = calloc(sizeof(hash_table[0]), MAX_HASH_ENTRIES);
3021 if (hash_table == NULL)
3028 sh2_drc_finish(sh2);
3032 void sh2_drc_finish(SH2 *sh2)
3036 if (block_tables[0] != NULL) {
3039 for (i = 0; i < TCACHE_BUFFERS; i++) {
3041 printf("~~~ tcache %d\n", i);
3042 tcache_dsm_ptrs[i] = tcache_bases[i];
3043 tcache_ptr = tcache_ptrs[i];
3047 if (block_tables[i] != NULL)
3048 free(block_tables[i]);
3049 block_tables[i] = NULL;
3050 if (block_links[i] == NULL)
3051 free(block_links[i]);
3052 block_links[i] = NULL;
3058 if (hash_table != NULL) {