drc: lots of new debug code
[picodrive.git] / cpu / sh2 / sh2.c
1 /*
2  * PicoDrive
3  * (C) notaz, 2009,2010
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8 #include <string.h>
9 #include <stddef.h>
10
11 #include "sh2.h"
12 #include "../debug.h"
13 #include "compiler.h"
14
15 #define I 0xf0
16
17 int sh2_init(SH2 *sh2, int is_slave)
18 {
19         int ret = 0;
20
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");
24 #ifdef DRC_SH2
25         ret = sh2_drc_init(sh2);
26 #endif
27         return ret;
28 }
29
30 void sh2_finish(SH2 *sh2)
31 {
32 #ifdef DRC_SH2
33         sh2_drc_finish(sh2);
34 #endif
35 }
36
37 void sh2_reset(SH2 *sh2)
38 {
39         sh2->pc = p32x_sh2_read32(0, sh2);
40         sh2->r[15] = p32x_sh2_read32(4, sh2);
41         sh2->sr = I;
42         sh2->vbr = 0;
43         sh2->pending_int_irq = 0;
44 }
45
46 void sh2_do_irq(SH2 *sh2, int level, int vector)
47 {
48         sh2->r[15] -= 4;
49         p32x_sh2_write32(sh2->r[15], sh2->sr, sh2);     /* push SR onto stack */
50         sh2->r[15] -= 4;
51         p32x_sh2_write32(sh2->r[15], sh2->pc, sh2);     /* push PC onto stack */
52
53         /* set I flags in SR */
54         sh2->sr = (sh2->sr & ~I) | (level << 4);
55
56         /* fetch PC */
57         sh2->pc = p32x_sh2_read32(sh2->vbr + vector * 4, sh2);
58
59         /* 13 cycles at best */
60         sh2->icount -= 13;
61 }
62
63 int sh2_irl_irq(SH2 *sh2, int level, int nested_call)
64 {
65         int taken;
66
67         sh2->pending_irl = level;
68         if (level < sh2->pending_int_irq)
69                 level = sh2->pending_int_irq;
70         sh2->pending_level = level;
71
72         taken = (level > ((sh2->sr >> 4) & 0x0f));
73         if (taken) {
74                 if (!nested_call) {
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);
80                 }
81                 else
82                         sh2->test_irq = 1;
83         }
84         return taken;
85 }
86
87 void sh2_internal_irq(SH2 *sh2, int level, int vector)
88 {
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;
95
96         sh2->test_irq = 1;
97 }
98
99 #define SH2_REG_SIZE (offsetof(SH2, macl) + sizeof(sh2->macl))
100
101 void sh2_pack(const SH2 *sh2, unsigned char *buff)
102 {
103         unsigned int *p;
104
105         memcpy(buff, sh2, SH2_REG_SIZE);
106         p = (void *)(buff + SH2_REG_SIZE);
107
108         p[0] = sh2->pending_int_irq;
109         p[1] = sh2->pending_int_vector;
110 }
111
112 void sh2_unpack(SH2 *sh2, const unsigned char *buff)
113 {
114         unsigned int *p;
115
116         memcpy(sh2, buff, SH2_REG_SIZE);
117         p = (void *)(buff + SH2_REG_SIZE);
118
119         sh2->pending_int_irq = p[0];
120         sh2->pending_int_vector = p[1];
121 }
122
123 #ifdef DRC_CMP
124
125 /* trace/compare */
126 #include <stdio.h>
127 #include <stdlib.h>
128 #include <pico/memory.h>
129
130 static SH2 sh2ref[2];
131 static int current_slave = -1;
132 static unsigned int mem_val;
133 static FILE *f;
134
135 #define SH2MAP_ADDR2OFFS_R(a) \
136   ((((a) >> 25) & 3) | (((a) >> 27) & 0x1c))
137
138 static unsigned int local_read32(SH2 *sh2, u32 a)
139 {
140         const sh2_memmap *sh2_map = sh2->read16_map;
141         uptr p;
142
143         sh2_map += SH2MAP_ADDR2OFFS_R(a);
144         p = sh2_map->addr;
145         if (!map_flag_set(p)) {
146                 u16 *pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~3));
147                 return (pd[0] << 16) | pd[1];
148         }
149
150         return 0;
151 }
152
153 static void write_uint(unsigned char ctl, unsigned int v)
154 {
155         fwrite(&ctl, 1, 1, f);
156         fwrite(&v, sizeof(v), 1, f);
157 }
158
159 void do_sh2_trace(SH2 *current, int cycles)
160 {
161         SH2 *sh2o = &sh2ref[current->is_slave];
162         u32 *regs_a = (void *)current;
163         u32 *regs_o = (void *)sh2o;
164         unsigned char v;
165         u32 val;
166         int i;
167
168         if (f == NULL)
169                 f = fopen("tracelog", "wb");
170
171         if (current->is_slave != current_slave) {
172                 current_slave = current->is_slave;
173                 v = 0x80 | current->is_slave;
174                 fwrite(&v, 1, 1, f);
175         }
176
177         for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
178                 if (i == 17) // ppc
179                         continue;
180                 if (regs_a[i] != regs_o[i]) {
181                         write_uint(i, regs_a[i]);
182                         regs_o[i] = regs_a[i];
183                 }
184         }
185
186         if (current->ea != sh2o->ea) {
187                 write_uint(0x82, current->ea);
188                 sh2o->ea = current->ea;
189         }
190         val = local_read32(current, current->ea);
191         if (mem_val != val) {
192                 write_uint(0x83, val);
193                 mem_val = val;
194         }
195         write_uint(0x84, cycles);
196 }
197
198 static const char *regnames[] = {
199         "r0",  "r1",  "r2",  "r3",
200         "r4",  "r5",  "r6",  "r7",
201         "r8",  "r9",  "r10", "r11",
202         "r12", "r13", "r14", "r15",
203         "pc",  "ppc", "pr",  "sr",
204         "gbr", "vbr", "mach","macl",
205 };
206
207 void do_sh2_cmp(SH2 *current)
208 {
209         static int current_slave;
210         static u32 current_val;
211         SH2 *sh2o = &sh2ref[current->is_slave];
212         u32 *regs_a = (void *)current;
213         u32 *regs_o = (void *)sh2o;
214         unsigned char code;
215         int cycles_o = 666;
216         u32 sr, val;
217         int bad = 0;
218         int cycles;
219         int i, ret;
220         char csh2;
221
222         if (f == NULL)
223                 f = fopen("tracelog", "rb");
224
225         while (1) {
226                 ret = fread(&code, 1, 1, f);
227                 if (ret <= 0)
228                         break;
229                 if (code == 0x84) {
230                         fread(&cycles_o, 1, 4, f);
231                         break;
232                 }
233
234                 switch (code) {
235                 case 0x80:
236                 case 0x81:
237                         current_slave = code & 1;
238                         break;
239                 case 0x82:
240                         fread(&sh2o->ea, 4, 1, f);
241                         break;
242                 case 0x83:
243                         fread(&current_val, 4, 1, f);
244                         break;
245                 default:
246                         if (code < offsetof(SH2, read8_map) / 4)
247                                 fread(regs_o + code, 4, 1, f);
248                         else {
249                                 printf("invalid code: %02x\n", code);
250                                 goto end;
251                         }
252                         break;
253                 }
254         }
255
256         if (ret <= 0) {
257                 printf("EOF?\n");
258                 goto end;
259         }
260
261         if (current->is_slave != current_slave) {
262                 printf("bad slave: %d %d\n", current->is_slave,
263                         current_slave);
264                 bad = 1;
265         }
266
267         for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
268                 if (i == 17 || i == 19) // ppc, sr
269                         continue;
270                 if (regs_a[i] != regs_o[i]) {
271                         printf("bad %4s: %08x %08x\n",
272                                 regnames[i], regs_a[i], regs_o[i]);
273                         bad = 1;
274                 }
275         }
276
277         sr = current->sr & 0x3f3;
278         cycles = (signed int)current->sr >> 12;
279
280         if (sr != sh2o->sr) {
281                 printf("bad SR:  %03x %03x\n", sr, sh2o->sr);
282                 bad = 1;
283         }
284
285         if (cycles != cycles_o) {
286                 printf("bad cycles: %d %d\n", cycles, cycles_o);
287                 bad = 1;
288         }
289
290         val = local_read32(current, sh2o->ea);
291         if (val != current_val) {
292                 printf("bad val @%08x: %08x %08x\n", sh2o->ea, val, current_val);
293                 bad = 1;
294         }
295
296         if (!bad) {
297                 sh2o->ppc = current->pc;
298                 return;
299         }
300
301 end:
302         printf("--\n");
303         csh2 = current->is_slave ? 's' : 'm';
304         for (i = 0; i < 16/2; i++)
305                 printf("%csh2 r%d: %08x r%02d: %08x\n", csh2,
306                         i, sh2o->r[i], i+8, sh2o->r[i+8]);
307         printf("%csh2 PC: %08x  ,   %08x\n", csh2, sh2o->pc, sh2o->ppc);
308         printf("%csh2 SR:      %03x  PR: %08x\n", csh2, sh2o->sr, sh2o->pr);
309         exit(1);
310 }
311
312 #endif // DRC_CMP