4 #include "../compiler.c"
5 #define BUSY_LOOP_HACKS 0
7 #define BUSY_LOOP_HACKS 1
12 typedef signed char INT8;
13 typedef signed short INT16;
14 typedef signed int INT32;
15 typedef unsigned int UINT32;
16 typedef unsigned short UINT16;
17 typedef unsigned char UINT8;
22 // this nasty conversion is needed for drc-expecting memhandlers
23 #define MAKE_READFUNC(name, cname) \
24 static __inline unsigned int name(SH2 *sh2, unsigned int a) \
27 sh2->sr |= sh2->icount << 12; \
28 ret = cname(a, sh2); \
29 sh2->icount = (signed int)sh2->sr >> 12; \
34 #define MAKE_WRITEFUNC(name, cname) \
35 static __inline void name(SH2 *sh2, unsigned int a, unsigned int d) \
37 sh2->sr |= sh2->icount << 12; \
39 sh2->icount = (signed int)sh2->sr >> 12; \
43 MAKE_READFUNC(RB, p32x_sh2_read8)
44 MAKE_READFUNC(RW, p32x_sh2_read16)
45 MAKE_READFUNC(RL, p32x_sh2_read32)
46 MAKE_WRITEFUNC(WB, p32x_sh2_write8)
47 MAKE_WRITEFUNC(WW, p32x_sh2_write16)
48 MAKE_WRITEFUNC(WL, p32x_sh2_write32)
52 #define RB(sh2, a) p32x_sh2_read8(a, sh2)
53 #define RW(sh2, a) p32x_sh2_read16(a, sh2)
54 #define RL(sh2, a) p32x_sh2_read32(a, sh2)
55 #define WB(sh2, a, d) p32x_sh2_write8(a, d, sh2)
56 #define WW(sh2, a, d) p32x_sh2_write16(a, d, sh2)
57 #define WL(sh2, a, d) p32x_sh2_write32(a, d, sh2)
61 // some stuff from sh2comn.h
70 #define FLAGS (M|Q|I|S|T)
72 #define Rn ((opcode>>8)&15)
73 #define Rm ((opcode>>4)&15)
77 extern void lprintf(const char *fmt, ...);
78 #define logerror lprintf
82 static unsigned int op_refs[0x10000];
85 # define LRNM (LRN|LRM)
86 # define rlog(rnm) { \
98 # define rlog1(x) sh2_stats.r[x]++
99 # define rlog2(x1,x2) sh2_stats.r[x1]++; sh2_stats.r[x2]++
110 int sh2_execute_interpreter(SH2 *sh2, int cycles)
114 sh2->icount = cycles;
116 if (sh2->icount <= 0)
123 sh2->ppc = sh2->delay;
124 opcode = RW(sh2, sh2->delay);
126 // TODO: more branch types
127 if ((opcode >> 13) == 5) { // BRA/BSR
129 WL(sh2, sh2->r[15], sh2->sr);
131 WL(sh2, sh2->r[15], sh2->pc);
132 sh2->pc = RL(sh2, sh2->vbr + 6 * 4);
142 opcode = RW(sh2, sh2->pc);
148 switch (opcode & ( 15 << 12))
150 case 0<<12: op0000(sh2, opcode); break;
151 case 1<<12: op0001(sh2, opcode); break;
152 case 2<<12: op0010(sh2, opcode); break;
153 case 3<<12: op0011(sh2, opcode); break;
154 case 4<<12: op0100(sh2, opcode); break;
155 case 5<<12: op0101(sh2, opcode); break;
156 case 6<<12: op0110(sh2, opcode); break;
157 case 7<<12: op0111(sh2, opcode); break;
158 case 8<<12: op1000(sh2, opcode); break;
159 case 9<<12: op1001(sh2, opcode); break;
160 case 10<<12: op1010(sh2, opcode); break;
161 case 11<<12: op1011(sh2, opcode); break;
162 case 12<<12: op1100(sh2, opcode); break;
163 case 13<<12: op1101(sh2, opcode); break;
164 case 14<<12: op1110(sh2, opcode); break;
165 default: op1111(sh2, opcode); break;
170 if (sh2->test_irq && !sh2->delay && sh2->pending_level > ((sh2->sr >> 4) & 0x0f))
172 int level = sh2->pending_level;
173 int vector = sh2->irq_callback(sh2, level);
174 sh2_do_irq(sh2, level, vector);
179 while (sh2->icount > 0 || sh2->delay); /* can't interrupt before delay */
187 int sh2_execute_interpreter(SH2 *sh2, int cycles)
189 static unsigned int base_pc_[2] = { 0, 0 };
190 static unsigned int end_pc_[2] = { 0, 0 };
191 static unsigned char op_flags_[2][BLOCK_INSN_LIMIT];
192 unsigned int *base_pc = &base_pc_[sh2->is_slave];
193 unsigned int *end_pc = &end_pc_[sh2->is_slave];
194 unsigned char *op_flags = op_flags_[sh2->is_slave];
195 unsigned int pc_expect;
198 sh2->icount = sh2->cycles_timeslice = cycles;
200 if (sh2->pending_level > ((sh2->sr >> 4) & 0x0f))
202 int level = sh2->pending_level;
203 int vector = sh2->irq_callback(sh2, level);
204 sh2_do_irq(sh2, level, vector);
208 if (sh2->icount <= 0)
214 if (sh2->pc < *base_pc || sh2->pc >= *end_pc) {
216 scan_block(*base_pc, sh2->is_slave,
217 op_flags, end_pc, NULL);
219 if ((op_flags[(sh2->pc - *base_pc) / 2]
220 & OF_BTARGET) || sh2->pc == *base_pc
221 || pc_expect != sh2->pc) // branched
228 do_sh2_trace(sh2, sh2->icount);
234 sh2->ppc = sh2->delay;
235 opcode = RW(sh2, sh2->delay);
241 opcode = RW(sh2, sh2->pc);
247 switch (opcode & ( 15 << 12))
249 case 0<<12: op0000(sh2, opcode); break;
250 case 1<<12: op0001(sh2, opcode); break;
251 case 2<<12: op0010(sh2, opcode); break;
252 case 3<<12: op0011(sh2, opcode); break;
253 case 4<<12: op0100(sh2, opcode); break;
254 case 5<<12: op0101(sh2, opcode); break;
255 case 6<<12: op0110(sh2, opcode); break;
256 case 7<<12: op0111(sh2, opcode); break;
257 case 8<<12: op1000(sh2, opcode); break;
258 case 9<<12: op1001(sh2, opcode); break;
259 case 10<<12: op1010(sh2, opcode); break;
260 case 11<<12: op1011(sh2, opcode); break;
261 case 12<<12: op1100(sh2, opcode); break;
262 case 13<<12: op1101(sh2, opcode); break;
263 case 14<<12: op1110(sh2, opcode); break;
264 default: op1111(sh2, opcode); break;
269 if (sh2->test_irq && !sh2->delay && sh2->pending_level > ((sh2->sr >> 4) & 0x0f))
271 int level = sh2->pending_level;
272 int vector = sh2->irq_callback(sh2, level);
273 sh2_do_irq(sh2, level, vector);
291 void sh2_dump_stats(void)
293 static const char *rnames[] = {
294 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7",
295 "R8", "R9", "R10", "R11", "R12", "R13", "R14", "SP",
296 "PC", "", "PR", "SR", "GBR", "VBR", "MACH", "MACL"
304 for (i = 0; i < 24; i++)
305 total += sh2_stats.r[i];
307 for (i = 0; i < 24; i++) {
308 if (i == 16 || i == 17 || i == 19)
310 printf("r %6.3f%% %-4s %9d\n", (double)sh2_stats.r[i] * 100.0 / total,
311 rnames[i], sh2_stats.r[i]);
314 memset(&sh2_stats, 0, sizeof(sh2_stats));
319 for (i = 0; i < 0x10000; i++)
322 for (u = 0; u < 16; u++) {
324 for (i = 0; i < 0x10000; i++) {
325 if (op_refs[i] > max) {
330 DasmSH2(buff, 0, op);
331 printf("i %6.3f%% %9d %s\n", (double)op_refs[op] * 100.0 / total,
335 memset(op_refs, 0, sizeof(op_refs));