32x: fix savestates
[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         sh2->test_irq = 1;
122 }
123
124 #ifdef DRC_CMP
125
126 /* trace/compare */
127 #include <stdio.h>
128 #include <stdlib.h>
129 #include <pico/memory.h>
130
131 static SH2 sh2ref[2];
132 static int current_slave = -1;
133 static unsigned int mem_val;
134 static FILE *f;
135
136 #define SH2MAP_ADDR2OFFS_R(a) \
137   ((((a) >> 25) & 3) | (((a) >> 27) & 0x1c))
138
139 static unsigned int local_read32(SH2 *sh2, u32 a)
140 {
141         const sh2_memmap *sh2_map = sh2->read16_map;
142         uptr p;
143
144         sh2_map += SH2MAP_ADDR2OFFS_R(a);
145         p = sh2_map->addr;
146         if (!map_flag_set(p)) {
147                 u16 *pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~3));
148                 return (pd[0] << 16) | pd[1];
149         }
150
151         return 0;
152 }
153
154 static void write_uint(unsigned char ctl, unsigned int v)
155 {
156         fwrite(&ctl, 1, 1, f);
157         fwrite(&v, sizeof(v), 1, f);
158 }
159
160 void do_sh2_trace(SH2 *current, int cycles)
161 {
162         SH2 *sh2o = &sh2ref[current->is_slave];
163         u32 *regs_a = (void *)current;
164         u32 *regs_o = (void *)sh2o;
165         unsigned char v;
166         u32 val;
167         int i;
168
169         if (f == NULL)
170                 f = fopen("tracelog", "wb");
171
172         if (current->is_slave != current_slave) {
173                 current_slave = current->is_slave;
174                 v = 0x80 | current->is_slave;
175                 fwrite(&v, 1, 1, f);
176         }
177
178         for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
179                 if (i == 17) // ppc
180                         continue;
181                 if (regs_a[i] != regs_o[i]) {
182                         write_uint(i, regs_a[i]);
183                         regs_o[i] = regs_a[i];
184                 }
185         }
186
187         if (current->ea != sh2o->ea) {
188                 write_uint(0x82, current->ea);
189                 sh2o->ea = current->ea;
190         }
191         val = local_read32(current, current->ea);
192         if (mem_val != val) {
193                 write_uint(0x83, val);
194                 mem_val = val;
195         }
196         write_uint(0x84, cycles);
197 }
198
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",
206 };
207
208 void do_sh2_cmp(SH2 *current)
209 {
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;
215         unsigned char code;
216         int cycles_o = 666;
217         u32 sr, val;
218         int bad = 0;
219         int cycles;
220         int i, ret;
221         char csh2;
222
223         if (f == NULL)
224                 f = fopen("tracelog", "rb");
225
226         while (1) {
227                 ret = fread(&code, 1, 1, f);
228                 if (ret <= 0)
229                         break;
230                 if (code == 0x84) {
231                         fread(&cycles_o, 1, 4, f);
232                         break;
233                 }
234
235                 switch (code) {
236                 case 0x80:
237                 case 0x81:
238                         current_slave = code & 1;
239                         break;
240                 case 0x82:
241                         fread(&sh2o->ea, 4, 1, f);
242                         break;
243                 case 0x83:
244                         fread(&current_val, 4, 1, f);
245                         break;
246                 default:
247                         if (code < offsetof(SH2, read8_map) / 4)
248                                 fread(regs_o + code, 4, 1, f);
249                         else {
250                                 printf("invalid code: %02x\n", code);
251                                 goto end;
252                         }
253                         break;
254                 }
255         }
256
257         if (ret <= 0) {
258                 printf("EOF?\n");
259                 goto end;
260         }
261
262         if (current->is_slave != current_slave) {
263                 printf("bad slave: %d %d\n", current->is_slave,
264                         current_slave);
265                 bad = 1;
266         }
267
268         for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
269                 if (i == 17 || i == 19) // ppc, sr
270                         continue;
271                 if (regs_a[i] != regs_o[i]) {
272                         printf("bad %4s: %08x %08x\n",
273                                 regnames[i], regs_a[i], regs_o[i]);
274                         bad = 1;
275                 }
276         }
277
278         sr = current->sr & 0x3f3;
279         cycles = (signed int)current->sr >> 12;
280
281         if (sr != sh2o->sr) {
282                 printf("bad SR:  %03x %03x\n", sr, sh2o->sr);
283                 bad = 1;
284         }
285
286         if (cycles != cycles_o) {
287                 printf("bad cycles: %d %d\n", cycles, cycles_o);
288                 bad = 1;
289         }
290
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);
294                 bad = 1;
295         }
296
297         if (!bad) {
298                 sh2o->ppc = current->pc;
299                 return;
300         }
301
302 end:
303         printf("--\n");
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);
310         exit(1);
311 }
312
313 #endif // DRC_CMP