5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
17 int sh2_init(SH2 *sh2, int is_slave)
21 memset(sh2, 0, offsetof(SH2, mult_m68k_to_sh2));
22 sh2->is_slave = is_slave;
23 pdb_register_cpu(sh2, PDBCT_SH2, is_slave ? "ssh2" : "msh2");
25 ret = sh2_drc_init(sh2);
30 void sh2_finish(SH2 *sh2)
37 void sh2_reset(SH2 *sh2)
39 sh2->pc = p32x_sh2_read32(0, sh2);
40 sh2->r[15] = p32x_sh2_read32(4, sh2);
43 sh2->pending_int_irq = 0;
46 void sh2_do_irq(SH2 *sh2, int level, int vector)
49 p32x_sh2_write32(sh2->r[15], sh2->sr, sh2); /* push SR onto stack */
51 p32x_sh2_write32(sh2->r[15], sh2->pc, sh2); /* push PC onto stack */
53 /* set I flags in SR */
54 sh2->sr = (sh2->sr & ~I) | (level << 4);
57 sh2->pc = p32x_sh2_read32(sh2->vbr + vector * 4, sh2);
59 /* 13 cycles at best */
63 int sh2_irl_irq(SH2 *sh2, int level, int nested_call)
67 sh2->pending_irl = level;
68 if (level < sh2->pending_int_irq)
69 level = sh2->pending_int_irq;
70 sh2->pending_level = level;
72 taken = (level > ((sh2->sr >> 4) & 0x0f));
75 // not in memhandler, so handle this now (recompiler friendly)
76 // do this to avoid missing irqs that other SH2 might clear
77 int vector = sh2->irq_callback(sh2, level);
78 sh2_do_irq(sh2, level, vector);
79 sh2->m68krcycles_done += C_SH2_TO_M68K(*sh2, 13);
87 void sh2_internal_irq(SH2 *sh2, int level, int vector)
89 // FIXME: multiple internal irqs not handled..
90 // assuming internal irqs never clear until accepted
91 sh2->pending_int_irq = level;
92 sh2->pending_int_vector = vector;
93 if (level > sh2->pending_level)
94 sh2->pending_level = level;
99 #define SH2_REG_SIZE (offsetof(SH2, macl) + sizeof(sh2->macl))
101 void sh2_pack(const SH2 *sh2, unsigned char *buff)
105 memcpy(buff, sh2, SH2_REG_SIZE);
106 p = (void *)(buff + SH2_REG_SIZE);
108 p[0] = sh2->pending_int_irq;
109 p[1] = sh2->pending_int_vector;
112 void sh2_unpack(SH2 *sh2, const unsigned char *buff)
116 memcpy(sh2, buff, SH2_REG_SIZE);
117 p = (void *)(buff + SH2_REG_SIZE);
119 sh2->pending_int_irq = p[0];
120 sh2->pending_int_vector = p[1];
129 #include <pico/memory.h>
131 static SH2 sh2ref[2];
132 static int current_slave = -1;
133 static unsigned int mem_val;
136 #define SH2MAP_ADDR2OFFS_R(a) \
137 ((((a) >> 25) & 3) | (((a) >> 27) & 0x1c))
139 static unsigned int local_read32(SH2 *sh2, u32 a)
141 const sh2_memmap *sh2_map = sh2->read16_map;
144 sh2_map += SH2MAP_ADDR2OFFS_R(a);
146 if (!map_flag_set(p)) {
147 u16 *pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~3));
148 return (pd[0] << 16) | pd[1];
154 static void write_uint(unsigned char ctl, unsigned int v)
156 fwrite(&ctl, 1, 1, f);
157 fwrite(&v, sizeof(v), 1, f);
160 void do_sh2_trace(SH2 *current, int cycles)
162 SH2 *sh2o = &sh2ref[current->is_slave];
163 u32 *regs_a = (void *)current;
164 u32 *regs_o = (void *)sh2o;
170 f = fopen("tracelog", "wb");
172 if (current->is_slave != current_slave) {
173 current_slave = current->is_slave;
174 v = 0x80 | current->is_slave;
178 for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
181 if (regs_a[i] != regs_o[i]) {
182 write_uint(i, regs_a[i]);
183 regs_o[i] = regs_a[i];
187 if (current->ea != sh2o->ea) {
188 write_uint(0x82, current->ea);
189 sh2o->ea = current->ea;
191 val = local_read32(current, current->ea);
192 if (mem_val != val) {
193 write_uint(0x83, val);
196 write_uint(0x84, cycles);
199 static const char *regnames[] = {
200 "r0", "r1", "r2", "r3",
201 "r4", "r5", "r6", "r7",
202 "r8", "r9", "r10", "r11",
203 "r12", "r13", "r14", "r15",
204 "pc", "ppc", "pr", "sr",
205 "gbr", "vbr", "mach","macl",
208 void do_sh2_cmp(SH2 *current)
210 static int current_slave;
211 static u32 current_val;
212 SH2 *sh2o = &sh2ref[current->is_slave];
213 u32 *regs_a = (void *)current;
214 u32 *regs_o = (void *)sh2o;
224 f = fopen("tracelog", "rb");
227 ret = fread(&code, 1, 1, f);
231 fread(&cycles_o, 1, 4, f);
238 current_slave = code & 1;
241 fread(&sh2o->ea, 4, 1, f);
244 fread(¤t_val, 4, 1, f);
247 if (code < offsetof(SH2, read8_map) / 4)
248 fread(regs_o + code, 4, 1, f);
250 printf("invalid code: %02x\n", code);
262 if (current->is_slave != current_slave) {
263 printf("bad slave: %d %d\n", current->is_slave,
268 for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
269 if (i == 17 || i == 19) // ppc, sr
271 if (regs_a[i] != regs_o[i]) {
272 printf("bad %4s: %08x %08x\n",
273 regnames[i], regs_a[i], regs_o[i]);
278 sr = current->sr & 0x3f3;
279 cycles = (signed int)current->sr >> 12;
281 if (sr != sh2o->sr) {
282 printf("bad SR: %03x %03x\n", sr, sh2o->sr);
286 if (cycles != cycles_o) {
287 printf("bad cycles: %d %d\n", cycles, cycles_o);
291 val = local_read32(current, sh2o->ea);
292 if (val != current_val) {
293 printf("bad val @%08x: %08x %08x\n", sh2o->ea, val, current_val);
298 sh2o->ppc = current->pc;
304 csh2 = current->is_slave ? 's' : 'm';
305 for (i = 0; i < 16/2; i++)
306 printf("%csh2 r%d: %08x r%02d: %08x\n", csh2,
307 i, sh2o->r[i], i+8, sh2o->r[i+8]);
308 printf("%csh2 PC: %08x , %08x\n", csh2, sh2o->pc, sh2o->ppc);
309 printf("%csh2 SR: %03x PR: %08x\n", csh2, sh2o->sr, sh2o->pr);