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