2 * vim:shiftwidth=2:expandtab
8 #include "../../pico/pico_int.h"
11 #include "../drc/cmn.h"
18 #define dbg(l,...) { \
19 if ((l) & DRC_DEBUG) \
20 elprintf(EL_STATUS, ##__VA_ARGS__); \
23 #include "mame/sh2dasm.h"
24 #include <platform/linux/host_dasm.h>
25 static int insns_compiled, hash_collisions, host_insn_count;
34 static u8 *tcache_dsm_ptrs[3];
35 static char sh2dasm_buff[64];
36 #define do_host_disasm(tcid) \
37 host_dasm(tcache_dsm_ptrs[tcid], tcache_ptr - tcache_dsm_ptrs[tcid]); \
38 tcache_dsm_ptrs[tcid] = tcache_ptr
40 #define do_host_disasm(x)
43 #define BLOCK_CYCLE_LIMIT 100
44 #define MAX_BLOCK_SIZE (BLOCK_CYCLE_LIMIT * 6 * 6)
46 // we have 3 translation cache buffers, split from one drc/cmn buffer.
47 // BIOS shares tcache with data array because it's only used for init
48 // and can be discarded early
49 static const int tcache_sizes[3] = {
50 DRC_TCACHE_SIZE * 6 / 8, // ROM, DRAM
51 DRC_TCACHE_SIZE / 8, // BIOS, data array in master sh2
52 DRC_TCACHE_SIZE / 8, // ... slave
55 static u8 *tcache_bases[3];
56 static u8 *tcache_ptrs[3];
58 // ptr for code emiters
59 static u8 *tcache_ptr;
61 // host register tracking
64 HR_CACHED, // 'val' has sh2_reg_e
66 HR_CONST, // 'val' has constant
67 HR_TEMP, // reg used for temp storage
73 u16 stamp; // kind of a timestamp
78 #include "../drc/emit_arm.c"
80 static const int reg_map_g2h[] = {
89 static temp_reg_t reg_temp[] = {
99 #include "../drc/emit_x86.c"
101 static const int reg_map_g2h[] = {
110 // ax, cx, dx are usually temporaries
111 static temp_reg_t reg_temp[] = {
120 SHR_R0 = 0, SHR_R15 = 15,
121 SHR_PC, SHR_PPC, SHR_PR, SHR_SR,
122 SHR_GBR, SHR_VBR, SHR_MACH, SHR_MACL,
125 typedef struct block_desc_ {
126 u32 addr; // SH2 PC address
127 u32 end_addr; // TODO rm?
128 void *tcache_ptr; // translated block for above PC
129 struct block_desc_ *next; // next block with the same PC hash
135 static const int block_max_counts[3] = {
140 static block_desc *block_tables[3];
141 static int block_counts[3];
144 #define MAX_HASH_ENTRIES 1024
145 #define HASH_MASK (MAX_HASH_ENTRIES - 1)
146 static void **hash_table;
148 extern void sh2_drc_entry(SH2 *sh2, void *block);
149 extern void sh2_drc_exit(void);
152 extern void REGPARM(2) sh2_do_op(SH2 *sh2, int opcode);
153 static void REGPARM(1) sh2_test_irq(SH2 *sh2);
155 static void flush_tcache(int tcid)
157 dbg(1, "tcache #%d flush! (%d/%d, bds %d/%d)", tcid,
158 tcache_ptrs[tcid] - tcache_bases[tcid], tcache_sizes[tcid],
159 block_counts[tcid], block_max_counts[tcid]);
161 block_counts[tcid] = 0;
162 tcache_ptrs[tcid] = tcache_bases[tcid];
163 if (tcid == 0) { // ROM, RAM
164 memset(hash_table, 0, sizeof(hash_table[0]) * MAX_HASH_ENTRIES);
165 memset(Pico32xMem->drcblk_ram, 0, sizeof(Pico32xMem->drcblk_ram));
168 memset(Pico32xMem->drcblk_da[tcid - 1], 0, sizeof(Pico32xMem->drcblk_da[0]));
170 tcache_dsm_ptrs[tcid] = tcache_bases[tcid];
174 static void *dr_find_block(block_desc *tab, u32 addr)
176 for (tab = tab->next; tab != NULL; tab = tab->next)
177 if (tab->addr == addr)
181 return tab->tcache_ptr;
183 printf("block miss for %08x\n", addr);
187 static block_desc *dr_add_block(u32 addr, int tcache_id, int *blk_id)
189 int *bcount = &block_counts[tcache_id];
192 if (*bcount >= block_max_counts[tcache_id])
195 bd = &block_tables[tcache_id][*bcount];
197 bd->tcache_ptr = tcache_ptr;
204 #define HASH_FUNC(hash_tab, addr) \
205 ((block_desc **)(hash_tab))[(addr) & HASH_MASK]
207 // ---------------------------------------------------------------
210 static u16 rcache_counter;
212 static temp_reg_t *rcache_evict(void)
214 // evict reg with oldest stamp
216 u16 min_stamp = (u16)-1;
218 for (i = 0; i < ARRAY_SIZE(reg_temp); i++) {
219 if (reg_temp[i].type == HR_CACHED || reg_temp[i].type == HR_CACHED_DIRTY)
220 if (reg_temp[i].stamp <= min_stamp) {
221 min_stamp = reg_temp[i].stamp;
227 printf("no registers to ec=vict, aborting\n");
232 if (reg_temp[i].type == HR_CACHED_DIRTY) {
234 emith_ctx_write(reg_temp[i].reg, reg_temp[i].val * 4);
246 static int rcache_get_reg(sh2_reg_e r, rc_gr_mode mode)
251 // maybe already statically mapped?
258 // maybe already cached?
259 for (i = ARRAY_SIZE(reg_temp) - 1; i >= 0; i--) {
260 if ((reg_temp[i].type == HR_CACHED || reg_temp[i].type == HR_CACHED_DIRTY) &&
261 reg_temp[i].val == r)
263 reg_temp[i].stamp = rcache_counter;
264 if (mode != RC_GR_READ)
265 reg_temp[i].type = HR_CACHED_DIRTY;
266 return reg_temp[i].reg;
271 for (i = ARRAY_SIZE(reg_temp) - 1; i >= 0; i--) {
272 if (reg_temp[i].type == HR_FREE || reg_temp[i].type == HR_CONST) {
281 if (mode != RC_GR_WRITE)
282 emith_ctx_read(tr->reg, r * 4);
284 tr->type = mode != RC_GR_READ ? HR_CACHED_DIRTY : HR_CACHED;
286 tr->stamp = rcache_counter;
290 static int rcache_get_tmp(void)
295 for (i = 0; i < ARRAY_SIZE(reg_temp); i++)
296 if (reg_temp[i].type == HR_FREE || reg_temp[i].type == HR_CONST) {
308 static void rcache_free_tmp(int hr)
311 for (i = 0; i < ARRAY_SIZE(reg_temp); i++)
312 if (reg_temp[i].reg == hr)
315 if (i == ARRAY_SIZE(reg_temp) || reg_temp[i].type != HR_TEMP)
316 printf("rcache_free_tmp fail: #%i hr %d, type %d\n", i, hr, reg_temp[i].type);
319 static void rcache_flush(void)
322 for (i = 0; i < ARRAY_SIZE(reg_temp); i++) {
323 if (reg_temp[i].type == HR_CACHED_DIRTY) {
325 emith_ctx_write(reg_temp[i].reg, reg_temp[i].val * 4);
327 reg_temp[i].type = HR_FREE;
332 // ---------------------------------------------------------------
334 static void emit_move_r_imm32(sh2_reg_e dst, u32 imm)
336 int hr = rcache_get_reg(dst, RC_GR_WRITE);
337 emith_move_r_imm(hr, imm);
340 static void emit_move_r_r(sh2_reg_e dst, sh2_reg_e src)
342 int hr_d = rcache_get_reg(dst, RC_GR_WRITE);
343 int hr_s = rcache_get_reg(src, RC_GR_READ);
345 emith_move_r_r(hr_d, hr_s);
349 static int sh2_translate_op4(int op)
355 emith_pass_arg(2, sh2, op);
356 emith_call(sh2_do_op);
367 #define CHECK_UNHANDLED_BITS(mask) { \
368 if ((op & (mask)) != 0) \
372 static void *sh2_translate(SH2 *sh2, block_desc *other_block)
375 block_desc *this_block;
376 unsigned int pc = sh2->pc;
377 int op, delayed_op = 0, test_irq = 0;
378 int tcache_id = 0, blkid = 0;
384 if ((tmp != 0 && tmp != 1 && tmp != 6) || sh2->pc == 0) {
385 printf("invalid PC, aborting: %08x\n", sh2->pc);
386 // FIXME: be less destructive
390 if ((sh2->pc & 0xe0000000) == 0xc0000000 || (sh2->pc & ~0xfff) == 0) {
391 // data_array, BIOS have separate tcache (shared)
392 tcache_id = 1 + sh2->is_slave;
395 tcache_ptr = tcache_ptrs[tcache_id];
396 this_block = dr_add_block(pc, tcache_id, &blkid);
398 tmp = tcache_ptr - tcache_bases[tcache_id];
399 if (tmp > tcache_sizes[tcache_id] - MAX_BLOCK_SIZE || this_block == NULL) {
400 flush_tcache(tcache_id);
401 tcache_ptr = tcache_ptrs[tcache_id];
402 other_block = NULL; // also gone too due to flush
403 this_block = dr_add_block(pc, tcache_id, &blkid);
406 this_block->next = other_block;
407 if ((sh2->pc & 0xc6000000) == 0x02000000) // ROM
408 HASH_FUNC(hash_table, pc) = this_block;
410 block_entry = tcache_ptr;
412 printf("== %csh2 block #%d,%d %08x -> %p\n", sh2->is_slave ? 's' : 'm',
413 tcache_id, block_counts[tcache_id], pc, block_entry);
414 if (other_block != NULL) {
415 printf(" hash collision with %08x\n", other_block->addr);
420 while (cycles < BLOCK_CYCLE_LIMIT || delayed_op)
425 op = p32x_sh2_read16(pc, sh2);
430 DasmSH2(sh2dasm_buff, pc, op);
431 printf("%08x %04x %s\n", pc, op, sh2dasm_buff);
438 switch ((op >> 12) & 0x0f)
443 CHECK_UNHANDLED_BITS(0xd0);
444 // BRAF Rm 0000mmmm00100011
445 // BSRF Rm 0000mmmm00000011
448 emit_move_r_imm32(SHR_PR, pc + 2);
449 tmp = rcache_get_reg(SHR_PPC, RC_GR_WRITE);
450 tmp2 = rcache_get_reg((op >> 8) & 0x0f, RC_GR_READ);
451 emith_move_r_r(tmp, tmp2);
452 emith_add_r_imm(tmp, pc + 2);
456 CHECK_UNHANDLED_BITS(0xf0);
457 // NOP 0000000000001001
460 CHECK_UNHANDLED_BITS(0xd0);
463 // RTS 0000000000001011
464 emit_move_r_r(SHR_PPC, SHR_PR);
467 // RTE 0000000000101011
468 //emit_move_r_r(SHR_PC, SHR_PR);
469 emit_move_r_imm32(SHR_PC, pc - 2);
471 emith_pass_arg_r(0, CONTEXT_REG);
472 emith_pass_arg_imm(1, op);
473 emith_call(sh2_do_op);
474 emit_move_r_r(SHR_PPC, SHR_PC);
485 if ((op & 0xf0) != 1)
487 // DT Rn 0100nnnn00010000
490 if ((op & 0xf0) != 0)
492 // LDC.L @Rm+,SR 0100mmmm00000111
496 if ((op & 0xd0) != 0)
498 // JMP @Rm 0100mmmm00101011
499 // JSR @Rm 0100mmmm00001011
502 emit_move_r_imm32(SHR_PR, pc + 2);
503 emit_move_r_r(SHR_PPC, (op >> 8) & 0x0f);
507 if ((op & 0xf0) != 0)
509 // LDC Rm,SR 0100mmmm00001110
516 switch (op & 0x0f00) {
517 // BT/S label 10001101dddddddd
519 // BF/S label 10001111dddddddd
524 // BT label 10001001dddddddd
526 // BF label 10001011dddddddd
528 tmp = ((signed int)(op << 24) >> 23);
529 tmp2 = delayed_op ? SHR_PPC : SHR_PC;
530 emit_move_r_imm32(tmp2, pc + (delayed_op ? 2 : 0));
532 EMITH_CONDITIONAL(emit_move_r_imm32(tmp2, pc + tmp + 2), (op & 0x0200) ? 1 : 0);
541 // BRA label 1010dddddddddddd
544 tmp = ((signed int)(op << 20) >> 19);
545 emit_move_r_imm32(SHR_PPC, pc + tmp + 2);
550 // BSR label 1011dddddddddddd
552 emit_move_r_imm32(SHR_PR, pc + 2);
557 emit_move_r_imm32(SHR_PC, pc - 2);
559 emith_pass_arg_r(0, CONTEXT_REG);
560 emith_pass_arg_imm(1, op);
561 emith_call(sh2_do_op);
567 emit_move_r_r(SHR_PC, SHR_PPC);
569 if (test_irq && delayed_op != 2) {
571 emith_pass_arg_r(0, CONTEXT_REG);
572 emith_call(sh2_test_irq);
578 do_host_disasm(tcache_id);
582 this_block->end_addr = pc;
584 // mark memory blocks as containing compiled code
585 if ((sh2->pc & 0xe0000000) == 0xc0000000 || (sh2->pc & ~0xfff) == 0) {
587 u16 *drcblk = Pico32xMem->drcblk_da[sh2->is_slave];
588 tmp = (this_block->addr & 0xfff) >> SH2_DRCBLK_DA_SHIFT;
589 tmp2 = (this_block->end_addr & 0xfff) >> SH2_DRCBLK_DA_SHIFT;
590 Pico32xMem->drcblk_da[sh2->is_slave][tmp] = (blkid << 1) | 1;
591 for (++tmp; tmp < tmp2; tmp++) {
593 break; // dont overwrite overlay block
594 drcblk[tmp] = blkid << 1;
597 else if ((this_block->addr & 0xc7fc0000) == 0x06000000) { // DRAM
598 tmp = (this_block->addr & 0x3ffff) >> SH2_DRCBLK_RAM_SHIFT;
599 tmp2 = (this_block->end_addr & 0x3ffff) >> SH2_DRCBLK_RAM_SHIFT;
600 Pico32xMem->drcblk_ram[tmp] = (blkid << 1) | 1;
601 for (++tmp; tmp < tmp2; tmp++) {
602 if (Pico32xMem->drcblk_ram[tmp])
604 Pico32xMem->drcblk_ram[tmp] = blkid << 1;
608 tmp = rcache_get_reg(SHR_SR, RC_GR_RMW);
609 emith_sub_r_imm(tmp, cycles << 12);
611 emith_jump(sh2_drc_exit);
612 tcache_ptrs[tcache_id] = tcache_ptr;
615 cache_flush_d_inval_i(block_entry, tcache_ptr);
618 do_host_disasm(tcache_id);
619 dbg(1, " block #%d,%d tcache %d/%d, insns %d -> %d %.3f",
620 tcache_id, block_counts[tcache_id],
621 tcache_ptr - tcache_bases[tcache_id], tcache_sizes[tcache_id],
622 insns_compiled, host_insn_count, (double)host_insn_count / insns_compiled);
623 if ((sh2->pc & 0xc6000000) == 0x02000000) // ROM
624 dbg(1, " hash collisions %d/%d", hash_collisions, block_counts[tcache_id]);
633 do_host_disasm(tcache_id);
638 void __attribute__((noinline)) sh2_drc_dispatcher(SH2 *sh2)
640 while (((signed int)sh2->sr >> 12) > 0)
643 block_desc *bd = NULL;
645 // FIXME: must avoid doing it so often..
648 // we have full block id tables for data_array and RAM
649 // BIOS goes to data_array table too
650 if ((sh2->pc & 0xff000000) == 0xc0000000 || (sh2->pc & ~0xfff) == 0) {
651 int blkid = Pico32xMem->drcblk_da[sh2->is_slave][(sh2->pc & 0xfff) >> SH2_DRCBLK_DA_SHIFT];
653 bd = &block_tables[1 + sh2->is_slave][blkid >> 1];
654 block = bd->tcache_ptr;
658 else if ((sh2->pc & 0xc6000000) == 0x06000000) {
659 int blkid = Pico32xMem->drcblk_ram[(sh2->pc & 0x3ffff) >> SH2_DRCBLK_RAM_SHIFT];
661 bd = &block_tables[0][blkid >> 1];
662 block = bd->tcache_ptr;
666 else if ((sh2->pc & 0xc6000000) == 0x02000000) {
667 bd = HASH_FUNC(hash_table, sh2->pc);
670 if (bd->addr == sh2->pc)
671 block = bd->tcache_ptr;
673 block = dr_find_block(bd, sh2->pc);
678 block = sh2_translate(sh2, bd);
680 dbg(4, "= %csh2 enter %08x %p, c=%d", sh2->is_slave ? 's' : 'm',
681 sh2->pc, block, (signed int)sh2->sr >> 12);
686 sh2_drc_entry(sh2, block);
690 static void sh2_smc_rm_block(u16 *drcblk, u16 *p, block_desc *btab, u32 a)
693 block_desc *bd = btab + id;
695 dbg(1, " killing block %08x", bd->addr);
696 bd->addr = bd->end_addr = 0;
698 while (p > drcblk && (p[-1] >> 1) == id)
701 // check for possible overlay block
702 if (p > 0 && p[-1] != 0) {
703 bd = btab + (p[-1] >> 1);
704 if (bd->addr <= a && a < bd->end_addr)
705 sh2_smc_rm_block(drcblk, p - 1, btab, a);
711 while ((*p >> 1) == id);
714 void sh2_drc_wcheck_ram(unsigned int a, int val, int cpuid)
716 u16 *drcblk = Pico32xMem->drcblk_ram;
717 u16 *p = drcblk + ((a & 0x3ffff) >> SH2_DRCBLK_RAM_SHIFT);
719 dbg(1, "%csh2 smc check @%08x", cpuid ? 's' : 'm', a);
720 sh2_smc_rm_block(drcblk, p, block_tables[0], a);
723 void sh2_drc_wcheck_da(unsigned int a, int val, int cpuid)
725 u16 *drcblk = Pico32xMem->drcblk_da[cpuid];
726 u16 *p = drcblk + ((a & 0xfff) >> SH2_DRCBLK_DA_SHIFT);
728 dbg(1, "%csh2 smc check @%08x", cpuid ? 's' : 'm', a);
729 sh2_smc_rm_block(drcblk, p, block_tables[1 + cpuid], a);
732 void sh2_execute(SH2 *sh2, int cycles)
734 sh2->cycles_aim += cycles;
735 cycles = sh2->cycles_aim - sh2->cycles_done;
737 // cycles are kept in SHR_SR unused bits (upper 20)
739 sh2->sr |= cycles << 12;
740 sh2_drc_dispatcher(sh2);
742 sh2->cycles_done += cycles - ((signed int)sh2->sr >> 12);
745 static void REGPARM(1) sh2_test_irq(SH2 *sh2)
747 if (sh2->pending_level > ((sh2->sr >> 4) & 0x0f))
749 if (sh2->pending_irl > sh2->pending_int_irq)
750 sh2_do_irq(sh2, sh2->pending_irl, 64 + sh2->pending_irl/2);
752 sh2_do_irq(sh2, sh2->pending_int_irq, sh2->pending_int_vector);
753 sh2->pending_int_irq = 0; // auto-clear
754 sh2->pending_level = sh2->pending_irl;
760 static void block_stats(void)
762 int c, b, i, total = 0;
764 for (b = 0; b < ARRAY_SIZE(block_tables); b++)
765 for (i = 0; i < block_counts[b]; i++)
766 if (block_tables[b][i].addr != 0)
767 total += block_tables[b][i].refcount;
769 for (c = 0; c < 10; c++) {
770 block_desc *blk, *maxb = NULL;
772 for (b = 0; b < ARRAY_SIZE(block_tables); b++) {
773 for (i = 0; i < block_counts[b]; i++) {
774 blk = &block_tables[b][i];
775 if (blk->addr != 0 && blk->refcount > max) {
783 printf("%08x %9d %2.3f%%\n", maxb->addr, maxb->refcount,
784 (double)maxb->refcount / total * 100.0);
788 for (b = 0; b < ARRAY_SIZE(block_tables); b++)
789 for (i = 0; i < block_counts[b]; i++)
790 block_tables[b][i].refcount = 0;
793 #define block_stats()
796 void sh2_drc_flush_all(void)
804 int sh2_drc_init(SH2 *sh2)
806 if (block_tables[0] == NULL) {
811 cnt = block_max_counts[0] + block_max_counts[1] + block_max_counts[2];
812 block_tables[0] = calloc(cnt, sizeof(*block_tables[0]));
813 if (block_tables[0] == NULL)
816 memset(block_counts, 0, sizeof(block_counts));
817 tcache_bases[0] = tcache_ptrs[0] = tcache;
819 for (i = 1; i < ARRAY_SIZE(block_tables); i++) {
820 block_tables[i] = block_tables[i - 1] + block_max_counts[i - 1];
821 tcache_bases[i] = tcache_ptrs[i] = tcache_bases[i - 1] + tcache_sizes[i - 1];
825 PicoOpt |= POPT_DIS_VDP_FIFO;
828 for (i = 0; i < ARRAY_SIZE(block_tables); i++)
829 tcache_dsm_ptrs[i] = tcache_bases[i];
836 if (hash_table == NULL) {
837 hash_table = calloc(sizeof(hash_table[0]), MAX_HASH_ENTRIES);
838 if (hash_table == NULL)
845 void sh2_drc_finish(SH2 *sh2)
847 if (block_tables[0] != NULL) {
849 free(block_tables[0]);
850 memset(block_tables, 0, sizeof(block_tables));
855 if (hash_table != NULL) {