]>
Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* |
2 | * Simulator for the Hitachi H8/300 architecture. | |
3 | * | |
4 | * Written by Steve Chamberlain of Cygnus Support. [email protected] | |
5 | * | |
6 | * This file is part of H8/300 sim | |
7 | * | |
8 | * | |
9 | * THIS SOFTWARE IS NOT COPYRIGHTED | |
10 | * | |
11 | * Cygnus offers the following for use in the public domain. Cygnus makes no | |
12 | * warranty with regard to the software or its performance and the user | |
13 | * accepts the software "AS IS" with all faults. | |
14 | * | |
15 | * CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS | |
16 | * SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY | |
17 | * AND FITNESS FOR A PARTICULAR PURPOSE. | |
18 | */ | |
19 | ||
20 | #include "config.h" | |
21 | ||
22 | #include <stdio.h> | |
23 | #include <signal.h> | |
24 | #ifdef HAVE_TIME_H | |
25 | #include <time.h> | |
26 | #endif | |
27 | #ifdef HAVE_STDLIB_H | |
28 | #include <stdlib.h> | |
29 | #endif | |
30 | #ifdef HAVE_SYS_PARAM_H | |
31 | #include <sys/param.h> | |
32 | #endif | |
c906108c SS |
33 | #include "ansidecl.h" |
34 | #include "bfd.h" | |
3c25f8c7 AC |
35 | #include "gdb/callback.h" |
36 | #include "gdb/remote-sim.h" | |
c906108c SS |
37 | |
38 | #ifndef SIGTRAP | |
39 | # define SIGTRAP 5 | |
40 | #endif | |
41 | ||
42 | int debug; | |
43 | ||
44 | host_callback *sim_callback; | |
45 | ||
46 | static SIM_OPEN_KIND sim_kind; | |
47 | static char *myname; | |
48 | ||
49 | /* FIXME: Needs to live in header file. | |
50 | This header should also include the things in remote-sim.h. | |
51 | One could move this to remote-sim.h but this function isn't needed | |
52 | by gdb. */ | |
53 | void sim_set_simcache_size PARAMS ((int)); | |
54 | ||
de9b1892 | 55 | #define X(op, size) op * 4 + size |
c906108c | 56 | |
de9b1892 | 57 | #define SP (h8300hmode ? SL : SW) |
c906108c SS |
58 | #define SB 0 |
59 | #define SW 1 | |
60 | #define SL 2 | |
61 | #define OP_REG 1 | |
62 | #define OP_DEC 2 | |
63 | #define OP_DISP 3 | |
64 | #define OP_INC 4 | |
65 | #define OP_PCREL 5 | |
66 | #define OP_MEM 6 | |
67 | #define OP_CCR 7 | |
68 | #define OP_IMM 8 | |
69 | #define OP_ABS 10 | |
fc974602 | 70 | #define OP_EXR 11 |
c906108c SS |
71 | #define h8_opcodes ops |
72 | #define DEFINE_TABLE | |
73 | #include "opcode/h8300.h" | |
74 | ||
75 | #include "inst.h" | |
76 | ||
0ef9643e | 77 | /* The rate at which to call the host's poll_quit callback. */ |
7a292a7a SS |
78 | |
79 | #define POLL_QUIT_INTERVAL 0x80000 | |
80 | ||
c906108c | 81 | #define LOW_BYTE(x) ((x) & 0xff) |
de9b1892 KH |
82 | #define HIGH_BYTE(x) (((x) >> 8) & 0xff) |
83 | #define P(X,Y) ((X << 8) | Y) | |
c906108c | 84 | |
f6225c96 AV |
85 | #define BUILDSR() cpu.ccr = (I << 7) | (UI << 6)| (H<<5) | (U<<4) | \ |
86 | (N << 3) | (Z << 2) | (V<<1) | C; | |
c906108c | 87 | |
fc974602 | 88 | #define BUILDEXR() \ |
d1335144 | 89 | if (h8300smode) cpu.exr = (trace<<7) | intMask; |
fc974602 | 90 | |
c906108c SS |
91 | #define GETSR() \ |
92 | c = (cpu.ccr >> 0) & 1;\ | |
93 | v = (cpu.ccr >> 1) & 1;\ | |
94 | nz = !((cpu.ccr >> 2) & 1);\ | |
f6225c96 AV |
95 | n = (cpu.ccr >> 3) & 1;\ |
96 | u = (cpu.ccr >> 4) & 1;\ | |
97 | h = (cpu.ccr >> 5) & 1;\ | |
98 | ui = ((cpu.ccr >> 6) & 1);\ | |
99 | intMaskBit = (cpu.ccr >> 7) & 1; | |
c906108c | 100 | |
fc974602 | 101 | #define GETEXR() \ |
d1335144 | 102 | if (h8300smode) { \ |
fc974602 AV |
103 | trace = (cpu.exr >> 7) & 1;\ |
104 | intMask = cpu.exr & 7; } | |
105 | ||
c906108c | 106 | #ifdef __CHAR_IS_SIGNED__ |
de9b1892 | 107 | #define SEXTCHAR(x) ((char) (x)) |
c906108c SS |
108 | #endif |
109 | ||
110 | #ifndef SEXTCHAR | |
111 | #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff): x & 0xff) | |
112 | #endif | |
113 | ||
114 | #define UEXTCHAR(x) ((x) & 0xff) | |
115 | #define UEXTSHORT(x) ((x) & 0xffff) | |
de9b1892 | 116 | #define SEXTSHORT(x) ((short) (x)) |
c906108c SS |
117 | |
118 | static cpu_state_type cpu; | |
119 | ||
120 | int h8300hmode = 0; | |
121 | int h8300smode = 0; | |
122 | ||
123 | static int memory_size; | |
124 | ||
c906108c SS |
125 | static int |
126 | get_now () | |
127 | { | |
3b02cf92 | 128 | return time (0); /* WinXX HAS UNIX like 'time', so why not using it? */ |
c906108c SS |
129 | } |
130 | ||
131 | static int | |
132 | now_persec () | |
133 | { | |
134 | return 1; | |
135 | } | |
136 | ||
c906108c SS |
137 | static int |
138 | bitfrom (x) | |
139 | { | |
140 | switch (x & SIZE) | |
141 | { | |
142 | case L_8: | |
143 | return SB; | |
144 | case L_16: | |
145 | return SW; | |
146 | case L_32: | |
147 | return SL; | |
148 | case L_P: | |
149 | return h8300hmode ? SL : SW; | |
150 | } | |
151 | } | |
152 | ||
0ef9643e | 153 | static unsigned int |
c906108c SS |
154 | lvalue (x, rn) |
155 | { | |
156 | switch (x / 4) | |
157 | { | |
158 | case OP_DISP: | |
159 | if (rn == 8) | |
160 | { | |
161 | return X (OP_IMM, SP); | |
162 | } | |
163 | return X (OP_REG, SP); | |
164 | ||
165 | case OP_MEM: | |
c906108c | 166 | return X (OP_MEM, SP); |
0ef9643e | 167 | |
c906108c | 168 | default: |
3b02cf92 | 169 | abort (); /* ?? May be something more usefull? */ |
c906108c SS |
170 | } |
171 | } | |
172 | ||
173 | static unsigned int | |
174 | decode (addr, data, dst) | |
175 | int addr; | |
176 | unsigned char *data; | |
177 | decoded_inst *dst; | |
178 | ||
179 | { | |
180 | int rs = 0; | |
181 | int rd = 0; | |
182 | int rdisp = 0; | |
183 | int abs = 0; | |
c906108c | 184 | int bit = 0; |
0ef9643e | 185 | int plen = 0; |
6d028502 | 186 | struct h8_opcode *q; |
c906108c | 187 | int size = 0; |
0ef9643e | 188 | |
c906108c SS |
189 | dst->dst.type = -1; |
190 | dst->src.type = -1; | |
0ef9643e JL |
191 | |
192 | /* Find the exact opcode/arg combo. */ | |
6d028502 | 193 | for (q = h8_opcodes; q->name; q++) |
c906108c | 194 | { |
6d028502 | 195 | op_type *nib = q->data.nib; |
c906108c SS |
196 | unsigned int len = 0; |
197 | ||
c906108c SS |
198 | while (1) |
199 | { | |
200 | op_type looking_for = *nib; | |
201 | int thisnib = data[len >> 1]; | |
202 | ||
203 | thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf); | |
204 | ||
205 | if (looking_for < 16 && looking_for >= 0) | |
206 | { | |
207 | if (looking_for != thisnib) | |
208 | goto fail; | |
209 | } | |
210 | else | |
211 | { | |
212 | if ((int) looking_for & (int) B31) | |
213 | { | |
214 | if (!(((int) thisnib & 0x8) != 0)) | |
215 | goto fail; | |
0ef9643e JL |
216 | |
217 | looking_for = (op_type) ((int) looking_for & ~(int) B31); | |
c906108c SS |
218 | thisnib &= 0x7; |
219 | } | |
0ef9643e | 220 | |
c906108c SS |
221 | if ((int) looking_for & (int) B30) |
222 | { | |
223 | if (!(((int) thisnib & 0x8) == 0)) | |
224 | goto fail; | |
0ef9643e | 225 | |
c906108c SS |
226 | looking_for = (op_type) ((int) looking_for & ~(int) B30); |
227 | } | |
0ef9643e | 228 | |
c906108c SS |
229 | if (looking_for & DBIT) |
230 | { | |
0a17cd59 AC |
231 | /* Exclude adds/subs by looking at bit 0 and 2, and |
232 | make sure the operand size, either w or l, | |
233 | matches by looking at bit 1. */ | |
234 | if ((looking_for & 7) != (thisnib & 7)) | |
c906108c | 235 | goto fail; |
0ef9643e | 236 | |
c906108c SS |
237 | abs = (thisnib & 0x8) ? 2 : 1; |
238 | } | |
239 | else if (looking_for & (REG | IND | INC | DEC)) | |
240 | { | |
241 | if (looking_for & REG) | |
242 | { | |
0ef9643e | 243 | /* Can work out size from the register. */ |
c906108c SS |
244 | size = bitfrom (looking_for); |
245 | } | |
246 | if (looking_for & SRC) | |
0ef9643e | 247 | rs = thisnib; |
c906108c | 248 | else |
0ef9643e | 249 | rd = thisnib; |
c906108c SS |
250 | } |
251 | else if (looking_for & L_16) | |
252 | { | |
253 | abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1]; | |
254 | plen = 16; | |
255 | if (looking_for & (PCREL | DISP)) | |
256 | { | |
257 | abs = (short) (abs); | |
258 | } | |
259 | } | |
260 | else if (looking_for & ABSJMP) | |
261 | { | |
0ef9643e | 262 | abs = (data[1] << 16) | (data[2] << 8) | (data[3]); |
c906108c SS |
263 | } |
264 | else if (looking_for & MEMIND) | |
265 | { | |
266 | abs = data[1]; | |
267 | } | |
268 | else if (looking_for & L_32) | |
269 | { | |
270 | int i = len >> 1; | |
0ef9643e | 271 | |
c906108c SS |
272 | abs = (data[i] << 24) |
273 | | (data[i + 1] << 16) | |
274 | | (data[i + 2] << 8) | |
275 | | (data[i + 3]); | |
276 | ||
277 | plen = 32; | |
278 | } | |
279 | else if (looking_for & L_24) | |
280 | { | |
281 | int i = len >> 1; | |
0ef9643e | 282 | |
c906108c SS |
283 | abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]); |
284 | plen = 24; | |
285 | } | |
286 | else if (looking_for & IGNORE) | |
287 | { | |
0ef9643e | 288 | ; |
c906108c SS |
289 | } |
290 | else if (looking_for & DISPREG) | |
291 | { | |
292 | rdisp = thisnib & 0x7; | |
293 | } | |
294 | else if (looking_for & KBIT) | |
295 | { | |
296 | switch (thisnib) | |
297 | { | |
298 | case 9: | |
299 | abs = 4; | |
300 | break; | |
301 | case 8: | |
302 | abs = 2; | |
303 | break; | |
304 | case 0: | |
305 | abs = 1; | |
306 | break; | |
0a17cd59 AC |
307 | default: |
308 | goto fail; | |
c906108c SS |
309 | } |
310 | } | |
311 | else if (looking_for & L_8) | |
312 | { | |
313 | plen = 8; | |
314 | ||
315 | if (looking_for & PCREL) | |
316 | { | |
317 | abs = SEXTCHAR (data[len >> 1]); | |
318 | } | |
319 | else if (looking_for & ABS8MEM) | |
320 | { | |
321 | plen = 8; | |
322 | abs = h8300hmode ? ~0xff0000ff : ~0xffff00ff; | |
0ef9643e | 323 | abs |= data[len >> 1] & 0xff; |
c906108c | 324 | } |
0ef9643e | 325 | else |
c906108c SS |
326 | { |
327 | abs = data[len >> 1] & 0xff; | |
328 | } | |
329 | } | |
330 | else if (looking_for & L_3) | |
331 | { | |
332 | plen = 3; | |
333 | ||
334 | bit = thisnib; | |
335 | } | |
336 | else if (looking_for == E) | |
337 | { | |
338 | dst->op = q; | |
339 | ||
0ef9643e | 340 | /* Fill in the args. */ |
c906108c SS |
341 | { |
342 | op_type *args = q->args.nib; | |
343 | int hadone = 0; | |
344 | ||
345 | while (*args != E) | |
346 | { | |
347 | int x = *args; | |
348 | int rn = (x & DST) ? rd : rs; | |
349 | ea_type *p; | |
350 | ||
351 | if (x & DST) | |
0ef9643e | 352 | p = &(dst->dst); |
c906108c | 353 | else |
0ef9643e | 354 | p = &(dst->src); |
c906108c | 355 | |
0ef9643e | 356 | if (x & L_3) |
c906108c SS |
357 | { |
358 | p->type = X (OP_IMM, size); | |
359 | p->literal = bit; | |
360 | } | |
361 | else if (x & (IMM | KBIT | DBIT)) | |
362 | { | |
363 | p->type = X (OP_IMM, size); | |
364 | p->literal = abs; | |
365 | } | |
366 | else if (x & REG) | |
367 | { | |
6d028502 KH |
368 | /* Reset the size. |
369 | Some ops (like mul) have two sizes. */ | |
c906108c SS |
370 | |
371 | size = bitfrom (x); | |
372 | p->type = X (OP_REG, size); | |
373 | p->reg = rn; | |
374 | } | |
375 | else if (x & INC) | |
376 | { | |
377 | p->type = X (OP_INC, size); | |
378 | p->reg = rn & 0x7; | |
379 | } | |
380 | else if (x & DEC) | |
381 | { | |
382 | p->type = X (OP_DEC, size); | |
383 | p->reg = rn & 0x7; | |
384 | } | |
385 | else if (x & IND) | |
386 | { | |
387 | p->type = X (OP_DISP, size); | |
388 | p->reg = rn & 0x7; | |
389 | p->literal = 0; | |
390 | } | |
391 | else if (x & (ABS | ABSJMP | ABS8MEM)) | |
392 | { | |
393 | p->type = X (OP_DISP, size); | |
394 | p->literal = abs; | |
395 | p->reg = 8; | |
396 | } | |
397 | else if (x & MEMIND) | |
398 | { | |
399 | p->type = X (OP_MEM, size); | |
400 | p->literal = abs; | |
401 | } | |
402 | else if (x & PCREL) | |
403 | { | |
404 | p->type = X (OP_PCREL, size); | |
405 | p->literal = abs + addr + 2; | |
406 | if (x & L_16) | |
407 | p->literal += 2; | |
408 | } | |
409 | else if (x & ABSJMP) | |
410 | { | |
411 | p->type = X (OP_IMM, SP); | |
412 | p->literal = abs; | |
413 | } | |
414 | else if (x & DISP) | |
415 | { | |
416 | p->type = X (OP_DISP, size); | |
417 | p->literal = abs; | |
418 | p->reg = rdisp & 0x7; | |
419 | } | |
420 | else if (x & CCR) | |
421 | { | |
422 | p->type = OP_CCR; | |
423 | } | |
fc974602 AV |
424 | else if (x & EXR) |
425 | { | |
426 | p->type = OP_EXR; | |
427 | } | |
c906108c SS |
428 | else |
429 | printf ("Hmmmm %x", x); | |
430 | ||
431 | args++; | |
432 | } | |
433 | } | |
434 | ||
0ef9643e JL |
435 | /* But a jmp or a jsr gets automagically lvalued, |
436 | since we branch to their address not their | |
437 | contents. */ | |
c906108c SS |
438 | if (q->how == O (O_JSR, SB) |
439 | || q->how == O (O_JMP, SB)) | |
440 | { | |
441 | dst->src.type = lvalue (dst->src.type, dst->src.reg); | |
442 | } | |
443 | ||
444 | if (dst->dst.type == -1) | |
445 | dst->dst = dst->src; | |
446 | ||
447 | dst->opcode = q->how; | |
448 | dst->cycles = q->time; | |
449 | ||
0ef9643e | 450 | /* And a jsr to 0xc4 is turned into a magic trap. */ |
c906108c SS |
451 | |
452 | if (dst->opcode == O (O_JSR, SB)) | |
453 | { | |
454 | if (dst->src.literal == 0xc4) | |
455 | { | |
456 | dst->opcode = O (O_SYSCALL, SB); | |
457 | } | |
458 | } | |
459 | ||
460 | dst->next_pc = addr + len / 2; | |
461 | return; | |
462 | } | |
463 | else | |
98ecb0a7 | 464 | printf ("Don't understand %x \n", looking_for); |
c906108c SS |
465 | } |
466 | ||
467 | len++; | |
468 | nib++; | |
469 | } | |
470 | ||
471 | fail: | |
6d028502 | 472 | ; |
c906108c SS |
473 | } |
474 | ||
0ef9643e | 475 | /* Fell off the end. */ |
c906108c SS |
476 | dst->opcode = O (O_ILL, SB); |
477 | } | |
478 | ||
c906108c SS |
479 | static void |
480 | compile (pc) | |
481 | { | |
482 | int idx; | |
483 | ||
2ea716f6 | 484 | /* Find the next cache entry to use. */ |
c906108c SS |
485 | idx = cpu.cache_top + 1; |
486 | cpu.compiles++; | |
487 | if (idx >= cpu.csize) | |
488 | { | |
489 | idx = 1; | |
490 | } | |
491 | cpu.cache_top = idx; | |
492 | ||
2ea716f6 | 493 | /* Throw away its old meaning. */ |
c906108c SS |
494 | cpu.cache_idx[cpu.cache[idx].oldpc] = 0; |
495 | ||
2ea716f6 | 496 | /* Set to new address. */ |
c906108c SS |
497 | cpu.cache[idx].oldpc = pc; |
498 | ||
2ea716f6 | 499 | /* Fill in instruction info. */ |
c906108c SS |
500 | decode (pc, cpu.memory + pc, cpu.cache + idx); |
501 | ||
2ea716f6 | 502 | /* Point to new cache entry. */ |
c906108c SS |
503 | cpu.cache_idx[pc] = idx; |
504 | } | |
505 | ||
506 | ||
507 | static unsigned char *breg[18]; | |
508 | static unsigned short *wreg[18]; | |
509 | static unsigned int *lreg[18]; | |
510 | ||
511 | #define GET_B_REG(x) *(breg[x]) | |
512 | #define SET_B_REG(x,y) (*(breg[x])) = (y) | |
513 | #define GET_W_REG(x) *(wreg[x]) | |
514 | #define SET_W_REG(x,y) (*(wreg[x])) = (y) | |
515 | ||
516 | #define GET_L_REG(x) *(lreg[x]) | |
517 | #define SET_L_REG(x,y) (*(lreg[x])) = (y) | |
518 | ||
519 | #define GET_MEMORY_L(x) \ | |
520 | (x < memory_size \ | |
521 | ? ((cpu.memory[x+0] << 24) | (cpu.memory[x+1] << 16) \ | |
522 | | (cpu.memory[x+2] << 8) | cpu.memory[x+3]) \ | |
523 | : ((cpu.eightbit[(x+0) & 0xff] << 24) | (cpu.eightbit[(x+1) & 0xff] << 16) \ | |
524 | | (cpu.eightbit[(x+2) & 0xff] << 8) | cpu.eightbit[(x+3) & 0xff])) | |
525 | ||
526 | #define GET_MEMORY_W(x) \ | |
527 | (x < memory_size \ | |
528 | ? ((cpu.memory[x+0] << 8) | (cpu.memory[x+1] << 0)) \ | |
529 | : ((cpu.eightbit[(x+0) & 0xff] << 8) | (cpu.eightbit[(x+1) & 0xff] << 0))) | |
530 | ||
531 | ||
532 | #define GET_MEMORY_B(x) \ | |
533 | (x < memory_size ? (cpu.memory[x]) : (cpu.eightbit[x & 0xff])) | |
534 | ||
535 | #define SET_MEMORY_L(x,y) \ | |
536 | { register unsigned char *_p; register int __y = y; \ | |
537 | _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \ | |
538 | _p[0] = (__y)>>24; _p[1] = (__y)>>16; \ | |
539 | _p[2] = (__y)>>8; _p[3] = (__y)>>0;} | |
540 | ||
541 | #define SET_MEMORY_W(x,y) \ | |
542 | { register unsigned char *_p; register int __y = y; \ | |
543 | _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \ | |
544 | _p[0] = (__y)>>8; _p[1] =(__y);} | |
545 | ||
546 | #define SET_MEMORY_B(x,y) \ | |
547 | (x < memory_size ? (cpu.memory[(x)] = y) : (cpu.eightbit[x & 0xff] = y)) | |
548 | ||
549 | int | |
550 | fetch (arg, n) | |
551 | ea_type *arg; | |
552 | { | |
553 | int rn = arg->reg; | |
554 | int abs = arg->literal; | |
555 | int r; | |
556 | int t; | |
557 | ||
558 | switch (arg->type) | |
559 | { | |
560 | case X (OP_REG, SB): | |
561 | return GET_B_REG (rn); | |
562 | case X (OP_REG, SW): | |
563 | return GET_W_REG (rn); | |
564 | case X (OP_REG, SL): | |
565 | return GET_L_REG (rn); | |
566 | case X (OP_IMM, SB): | |
567 | case X (OP_IMM, SW): | |
568 | case X (OP_IMM, SL): | |
569 | return abs; | |
570 | case X (OP_DEC, SB): | |
571 | abort (); | |
572 | ||
573 | case X (OP_INC, SB): | |
574 | t = GET_L_REG (rn); | |
575 | t &= cpu.mask; | |
576 | r = GET_MEMORY_B (t); | |
577 | t++; | |
578 | t = t & cpu.mask; | |
579 | SET_L_REG (rn, t); | |
580 | return r; | |
581 | break; | |
582 | case X (OP_INC, SW): | |
583 | t = GET_L_REG (rn); | |
584 | t &= cpu.mask; | |
585 | r = GET_MEMORY_W (t); | |
586 | t += 2; | |
587 | t = t & cpu.mask; | |
588 | SET_L_REG (rn, t); | |
589 | return r; | |
590 | case X (OP_INC, SL): | |
591 | t = GET_L_REG (rn); | |
592 | t &= cpu.mask; | |
593 | r = GET_MEMORY_L (t); | |
594 | ||
595 | t += 4; | |
596 | t = t & cpu.mask; | |
597 | SET_L_REG (rn, t); | |
598 | return r; | |
599 | ||
600 | case X (OP_DISP, SB): | |
601 | t = GET_L_REG (rn) + abs; | |
602 | t &= cpu.mask; | |
603 | return GET_MEMORY_B (t); | |
604 | ||
605 | case X (OP_DISP, SW): | |
606 | t = GET_L_REG (rn) + abs; | |
607 | t &= cpu.mask; | |
608 | return GET_MEMORY_W (t); | |
609 | ||
610 | case X (OP_DISP, SL): | |
611 | t = GET_L_REG (rn) + abs; | |
612 | t &= cpu.mask; | |
613 | return GET_MEMORY_L (t); | |
614 | ||
615 | case X (OP_MEM, SL): | |
616 | t = GET_MEMORY_L (abs); | |
617 | t &= cpu.mask; | |
618 | return t; | |
619 | ||
620 | case X (OP_MEM, SW): | |
621 | t = GET_MEMORY_W (abs); | |
622 | t &= cpu.mask; | |
623 | return t; | |
624 | ||
625 | default: | |
3b02cf92 | 626 | abort (); /* ?? May be something more usefull? */ |
c906108c SS |
627 | |
628 | } | |
629 | } | |
630 | ||
631 | ||
de9b1892 | 632 | static void |
c906108c SS |
633 | store (arg, n) |
634 | ea_type *arg; | |
635 | int n; | |
636 | { | |
637 | int rn = arg->reg; | |
638 | int abs = arg->literal; | |
639 | int t; | |
640 | ||
641 | switch (arg->type) | |
642 | { | |
643 | case X (OP_REG, SB): | |
644 | SET_B_REG (rn, n); | |
645 | break; | |
646 | case X (OP_REG, SW): | |
647 | SET_W_REG (rn, n); | |
648 | break; | |
649 | case X (OP_REG, SL): | |
650 | SET_L_REG (rn, n); | |
651 | break; | |
652 | ||
653 | case X (OP_DEC, SB): | |
654 | t = GET_L_REG (rn) - 1; | |
655 | t &= cpu.mask; | |
656 | SET_L_REG (rn, t); | |
657 | SET_MEMORY_B (t, n); | |
658 | ||
659 | break; | |
660 | case X (OP_DEC, SW): | |
661 | t = (GET_L_REG (rn) - 2) & cpu.mask; | |
662 | SET_L_REG (rn, t); | |
663 | SET_MEMORY_W (t, n); | |
664 | break; | |
665 | ||
666 | case X (OP_DEC, SL): | |
667 | t = (GET_L_REG (rn) - 4) & cpu.mask; | |
668 | SET_L_REG (rn, t); | |
669 | SET_MEMORY_L (t, n); | |
670 | break; | |
671 | ||
672 | case X (OP_DISP, SB): | |
673 | t = GET_L_REG (rn) + abs; | |
674 | t &= cpu.mask; | |
675 | SET_MEMORY_B (t, n); | |
676 | break; | |
677 | ||
678 | case X (OP_DISP, SW): | |
679 | t = GET_L_REG (rn) + abs; | |
680 | t &= cpu.mask; | |
681 | SET_MEMORY_W (t, n); | |
682 | break; | |
683 | ||
684 | case X (OP_DISP, SL): | |
685 | t = GET_L_REG (rn) + abs; | |
686 | t &= cpu.mask; | |
687 | SET_MEMORY_L (t, n); | |
688 | break; | |
689 | default: | |
690 | abort (); | |
691 | } | |
692 | } | |
693 | ||
694 | ||
695 | static union | |
696 | { | |
697 | short int i; | |
698 | struct | |
699 | { | |
700 | char low; | |
701 | char high; | |
702 | } | |
703 | u; | |
704 | } | |
705 | ||
706 | littleendian; | |
707 | ||
de9b1892 | 708 | static void |
c906108c SS |
709 | init_pointers () |
710 | { | |
711 | static int init; | |
712 | ||
713 | if (!init) | |
714 | { | |
715 | int i; | |
716 | ||
717 | init = 1; | |
718 | littleendian.i = 1; | |
719 | ||
a8cdafbd AV |
720 | if (h8300smode) |
721 | memory_size = H8300S_MSIZE; | |
722 | else if (h8300hmode) | |
c906108c SS |
723 | memory_size = H8300H_MSIZE; |
724 | else | |
725 | memory_size = H8300_MSIZE; | |
726 | cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size); | |
727 | cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size); | |
728 | cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256); | |
729 | ||
2ea716f6 | 730 | /* `msize' must be a power of two. */ |
c906108c SS |
731 | if ((memory_size & (memory_size - 1)) != 0) |
732 | abort (); | |
733 | cpu.mask = memory_size - 1; | |
734 | ||
735 | for (i = 0; i < 9; i++) | |
736 | { | |
737 | cpu.regs[i] = 0; | |
738 | } | |
739 | ||
740 | for (i = 0; i < 8; i++) | |
741 | { | |
742 | unsigned char *p = (unsigned char *) (cpu.regs + i); | |
743 | unsigned char *e = (unsigned char *) (cpu.regs + i + 1); | |
744 | unsigned short *q = (unsigned short *) (cpu.regs + i); | |
745 | unsigned short *u = (unsigned short *) (cpu.regs + i + 1); | |
746 | cpu.regs[i] = 0x00112233; | |
747 | while (p < e) | |
748 | { | |
749 | if (*p == 0x22) | |
750 | { | |
751 | breg[i] = p; | |
752 | } | |
753 | if (*p == 0x33) | |
754 | { | |
755 | breg[i + 8] = p; | |
756 | } | |
757 | p++; | |
758 | } | |
759 | while (q < u) | |
760 | { | |
761 | if (*q == 0x2233) | |
762 | { | |
763 | wreg[i] = q; | |
764 | } | |
765 | if (*q == 0x0011) | |
766 | { | |
767 | wreg[i + 8] = q; | |
768 | } | |
769 | q++; | |
770 | } | |
771 | cpu.regs[i] = 0; | |
772 | lreg[i] = &cpu.regs[i]; | |
773 | } | |
774 | ||
775 | lreg[8] = &cpu.regs[8]; | |
776 | ||
2ea716f6 | 777 | /* Initialize the seg registers. */ |
c906108c SS |
778 | if (!cpu.cache) |
779 | sim_set_simcache_size (CSIZE); | |
780 | } | |
781 | } | |
782 | ||
783 | static void | |
784 | control_c (sig, code, scp, addr) | |
785 | int sig; | |
786 | int code; | |
787 | char *scp; | |
788 | char *addr; | |
789 | { | |
790 | cpu.state = SIM_STATE_STOPPED; | |
791 | cpu.exception = SIGINT; | |
792 | } | |
793 | ||
794 | #define C (c != 0) | |
795 | #define Z (nz == 0) | |
796 | #define V (v != 0) | |
797 | #define N (n != 0) | |
f6225c96 AV |
798 | #define U (u != 0) |
799 | #define H (h != 0) | |
800 | #define UI (ui != 0) | |
801 | #define I (intMaskBit != 0) | |
c906108c SS |
802 | |
803 | static int | |
804 | mop (code, bsize, sign) | |
805 | decoded_inst *code; | |
806 | int bsize; | |
807 | int sign; | |
808 | { | |
809 | int multiplier; | |
810 | int multiplicand; | |
811 | int result; | |
812 | int n, nz; | |
813 | ||
814 | if (sign) | |
815 | { | |
816 | multiplicand = | |
817 | bsize ? SEXTCHAR (GET_W_REG (code->dst.reg)) : | |
818 | SEXTSHORT (GET_W_REG (code->dst.reg)); | |
819 | multiplier = | |
820 | bsize ? SEXTCHAR (GET_B_REG (code->src.reg)) : | |
821 | SEXTSHORT (GET_W_REG (code->src.reg)); | |
822 | } | |
823 | else | |
824 | { | |
825 | multiplicand = bsize ? UEXTCHAR (GET_W_REG (code->dst.reg)) : | |
826 | UEXTSHORT (GET_W_REG (code->dst.reg)); | |
827 | multiplier = | |
828 | bsize ? UEXTCHAR (GET_B_REG (code->src.reg)) : | |
829 | UEXTSHORT (GET_W_REG (code->src.reg)); | |
830 | ||
831 | } | |
832 | result = multiplier * multiplicand; | |
833 | ||
834 | if (sign) | |
835 | { | |
836 | n = result & (bsize ? 0x8000 : 0x80000000); | |
837 | nz = result & (bsize ? 0xffff : 0xffffffff); | |
838 | } | |
839 | if (bsize) | |
840 | { | |
841 | SET_W_REG (code->dst.reg, result); | |
842 | } | |
843 | else | |
844 | { | |
845 | SET_L_REG (code->dst.reg, result); | |
846 | } | |
de9b1892 KH |
847 | #if 0 |
848 | return ((n == 1) << 1) | (nz == 1); | |
849 | #endif | |
c906108c SS |
850 | } |
851 | ||
852 | #define ONOT(name, how) \ | |
d1335144 | 853 | case O (name, SB): \ |
c906108c SS |
854 | { \ |
855 | int t; \ | |
856 | int hm = 0x80; \ | |
857 | rd = GET_B_REG (code->src.reg); \ | |
858 | how; \ | |
859 | goto shift8; \ | |
860 | } \ | |
d1335144 | 861 | case O (name, SW): \ |
c906108c SS |
862 | { \ |
863 | int t; \ | |
864 | int hm = 0x8000; \ | |
865 | rd = GET_W_REG (code->src.reg); \ | |
866 | how; \ | |
867 | goto shift16; \ | |
868 | } \ | |
d1335144 | 869 | case O (name, SL): \ |
c906108c SS |
870 | { \ |
871 | int t; \ | |
872 | int hm = 0x80000000; \ | |
873 | rd = GET_L_REG (code->src.reg); \ | |
874 | how; \ | |
875 | goto shift32; \ | |
876 | } | |
877 | ||
878 | #define OSHIFTS(name, how1, how2) \ | |
d1335144 | 879 | case O (name, SB): \ |
c906108c SS |
880 | { \ |
881 | int t; \ | |
882 | int hm = 0x80; \ | |
883 | rd = GET_B_REG (code->src.reg); \ | |
884 | if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \ | |
885 | { \ | |
886 | how1; \ | |
887 | } \ | |
888 | else \ | |
889 | { \ | |
890 | how2; \ | |
891 | } \ | |
892 | goto shift8; \ | |
893 | } \ | |
d1335144 | 894 | case O (name, SW): \ |
c906108c SS |
895 | { \ |
896 | int t; \ | |
897 | int hm = 0x8000; \ | |
898 | rd = GET_W_REG (code->src.reg); \ | |
899 | if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \ | |
900 | { \ | |
901 | how1; \ | |
902 | } \ | |
903 | else \ | |
904 | { \ | |
905 | how2; \ | |
906 | } \ | |
907 | goto shift16; \ | |
908 | } \ | |
d1335144 | 909 | case O (name, SL): \ |
c906108c SS |
910 | { \ |
911 | int t; \ | |
912 | int hm = 0x80000000; \ | |
913 | rd = GET_L_REG (code->src.reg); \ | |
914 | if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \ | |
915 | { \ | |
916 | how1; \ | |
917 | } \ | |
918 | else \ | |
919 | { \ | |
920 | how2; \ | |
921 | } \ | |
922 | goto shift32; \ | |
923 | } | |
924 | ||
925 | #define OBITOP(name,f, s, op) \ | |
d1335144 | 926 | case O (name, SB): \ |
c906108c SS |
927 | { \ |
928 | int m; \ | |
929 | int b; \ | |
930 | if (f) ea = fetch (&code->dst); \ | |
d1335144 | 931 | m=1<< fetch (&code->src); \ |
c906108c | 932 | op; \ |
d1335144 | 933 | if (s) store (&code->dst,ea); goto next; \ |
c906108c SS |
934 | } |
935 | ||
936 | int | |
937 | sim_stop (sd) | |
938 | SIM_DESC sd; | |
939 | { | |
940 | cpu.state = SIM_STATE_STOPPED; | |
941 | cpu.exception = SIGINT; | |
942 | return 1; | |
943 | } | |
944 | ||
6147b1f6 AV |
945 | #define R0_REGNUM 0 |
946 | #define R1_REGNUM 1 | |
947 | #define R2_REGNUM 2 | |
948 | #define R3_REGNUM 3 | |
949 | #define R4_REGNUM 4 | |
950 | #define R5_REGNUM 5 | |
951 | #define R6_REGNUM 6 | |
952 | #define R7_REGNUM 7 | |
953 | ||
954 | #define SP_REGNUM R7_REGNUM /* Contains address of top of stack */ | |
955 | #define FP_REGNUM R6_REGNUM /* Contains address of executing | |
956 | * stack frame */ | |
957 | ||
958 | #define CCR_REGNUM 8 /* Contains processor status */ | |
959 | #define PC_REGNUM 9 /* Contains program counter */ | |
960 | ||
961 | #define CYCLE_REGNUM 10 | |
962 | ||
963 | #define EXR_REGNUM 11 | |
964 | #define INST_REGNUM 12 | |
965 | #define TICK_REGNUM 13 | |
966 | ||
c906108c SS |
967 | void |
968 | sim_resume (sd, step, siggnal) | |
969 | SIM_DESC sd; | |
970 | { | |
971 | static int init1; | |
972 | int cycles = 0; | |
973 | int insts = 0; | |
974 | int tick_start = get_now (); | |
975 | void (*prev) (); | |
976 | int poll_count = 0; | |
977 | int res; | |
978 | int tmp; | |
979 | int rd; | |
980 | int ea; | |
981 | int bit; | |
982 | int pc; | |
f6225c96 | 983 | int c, nz, v, n, u, h, ui, intMaskBit; |
fc974602 | 984 | int trace, intMask; |
c906108c SS |
985 | int oldmask; |
986 | init_pointers (); | |
987 | ||
988 | prev = signal (SIGINT, control_c); | |
989 | ||
990 | if (step) | |
991 | { | |
992 | cpu.state = SIM_STATE_STOPPED; | |
993 | cpu.exception = SIGTRAP; | |
994 | } | |
995 | else | |
996 | { | |
997 | cpu.state = SIM_STATE_RUNNING; | |
998 | cpu.exception = 0; | |
999 | } | |
1000 | ||
1001 | pc = cpu.pc; | |
1002 | ||
1003 | /* The PC should never be odd. */ | |
1004 | if (pc & 0x1) | |
1005 | abort (); | |
1006 | ||
1007 | GETSR (); | |
fc974602 AV |
1008 | GETEXR (); |
1009 | ||
c906108c SS |
1010 | oldmask = cpu.mask; |
1011 | if (!h8300hmode) | |
1012 | cpu.mask = 0xffff; | |
1013 | do | |
1014 | { | |
1015 | int cidx; | |
1016 | decoded_inst *code; | |
1017 | ||
1018 | top: | |
1019 | cidx = cpu.cache_idx[pc]; | |
1020 | code = cpu.cache + cidx; | |
1021 | ||
1022 | ||
1023 | #define ALUOP(STORE, NAME, HOW) \ | |
d1335144 KH |
1024 | case O (NAME,SB): HOW; if (STORE)goto alu8;else goto just_flags_alu8; \ |
1025 | case O (NAME, SW): HOW; if (STORE)goto alu16;else goto just_flags_alu16; \ | |
1026 | case O (NAME,SL): HOW; if (STORE)goto alu32;else goto just_flags_alu32; | |
c906108c SS |
1027 | |
1028 | ||
1029 | #define LOGOP(NAME, HOW) \ | |
d1335144 KH |
1030 | case O (NAME,SB): HOW; goto log8;\ |
1031 | case O (NAME, SW): HOW; goto log16;\ | |
1032 | case O (NAME,SL): HOW; goto log32; | |
c906108c SS |
1033 | |
1034 | ||
1035 | ||
1036 | #if ADEBUG | |
1037 | if (debug) | |
1038 | { | |
1039 | printf ("%x %d %s\n", pc, code->opcode, | |
1040 | code->op ? code->op->name : "**"); | |
1041 | } | |
1042 | cpu.stats[code->opcode]++; | |
1043 | ||
1044 | #endif | |
1045 | ||
3b02cf92 | 1046 | if (code->opcode) |
c3f4437e KH |
1047 | { |
1048 | cycles += code->cycles; | |
1049 | insts++; | |
1050 | } | |
3b02cf92 | 1051 | |
c906108c SS |
1052 | switch (code->opcode) |
1053 | { | |
1054 | case 0: | |
1055 | /* | |
1056 | * This opcode is a fake for when we get to an | |
1057 | * instruction which hasnt been compiled | |
1058 | */ | |
1059 | compile (pc); | |
1060 | goto top; | |
1061 | break; | |
1062 | ||
1063 | ||
1064 | case O (O_SUBX, SB): | |
1065 | rd = fetch (&code->dst); | |
1066 | ea = fetch (&code->src); | |
1067 | ea = -(ea + C); | |
1068 | res = rd + ea; | |
1069 | goto alu8; | |
1070 | ||
1071 | case O (O_ADDX, SB): | |
1072 | rd = fetch (&code->dst); | |
1073 | ea = fetch (&code->src); | |
1074 | ea = C + ea; | |
1075 | res = rd + ea; | |
1076 | goto alu8; | |
1077 | ||
d1335144 KH |
1078 | #define EA ea = fetch (&code->src); |
1079 | #define RD_EA ea = fetch (&code->src); rd = fetch (&code->dst); | |
c906108c SS |
1080 | |
1081 | ALUOP (1, O_SUB, RD_EA; | |
1082 | ea = -ea; | |
1083 | res = rd + ea); | |
1084 | ALUOP (1, O_NEG, EA; | |
1085 | ea = -ea; | |
1086 | rd = 0; | |
1087 | res = rd + ea); | |
1088 | ||
1089 | case O (O_ADD, SB): | |
1090 | rd = GET_B_REG (code->dst.reg); | |
1091 | ea = fetch (&code->src); | |
1092 | res = rd + ea; | |
1093 | goto alu8; | |
1094 | case O (O_ADD, SW): | |
1095 | rd = GET_W_REG (code->dst.reg); | |
1096 | ea = fetch (&code->src); | |
1097 | res = rd + ea; | |
1098 | goto alu16; | |
1099 | case O (O_ADD, SL): | |
1100 | rd = GET_L_REG (code->dst.reg); | |
1101 | ea = fetch (&code->src); | |
1102 | res = rd + ea; | |
1103 | goto alu32; | |
1104 | ||
1105 | ||
1106 | LOGOP (O_AND, RD_EA; | |
1107 | res = rd & ea); | |
1108 | ||
1109 | LOGOP (O_OR, RD_EA; | |
1110 | res = rd | ea); | |
1111 | ||
1112 | LOGOP (O_XOR, RD_EA; | |
1113 | res = rd ^ ea); | |
1114 | ||
1115 | ||
1116 | case O (O_MOV_TO_MEM, SB): | |
1117 | res = GET_B_REG (code->src.reg); | |
1118 | goto log8; | |
1119 | case O (O_MOV_TO_MEM, SW): | |
1120 | res = GET_W_REG (code->src.reg); | |
1121 | goto log16; | |
1122 | case O (O_MOV_TO_MEM, SL): | |
1123 | res = GET_L_REG (code->src.reg); | |
1124 | goto log32; | |
1125 | ||
1126 | ||
1127 | case O (O_MOV_TO_REG, SB): | |
1128 | res = fetch (&code->src); | |
1129 | SET_B_REG (code->dst.reg, res); | |
1130 | goto just_flags_log8; | |
1131 | case O (O_MOV_TO_REG, SW): | |
1132 | res = fetch (&code->src); | |
1133 | SET_W_REG (code->dst.reg, res); | |
1134 | goto just_flags_log16; | |
1135 | case O (O_MOV_TO_REG, SL): | |
1136 | res = fetch (&code->src); | |
1137 | SET_L_REG (code->dst.reg, res); | |
1138 | goto just_flags_log32; | |
1139 | ||
6147b1f6 AV |
1140 | case O (O_EEPMOV, SB): |
1141 | case O (O_EEPMOV, SW): | |
d1335144 | 1142 | if (h8300hmode||h8300smode) |
c3f4437e KH |
1143 | { |
1144 | register unsigned char *_src,*_dst; | |
d1335144 | 1145 | unsigned int count = (code->opcode == O(O_EEPMOV, SW))?cpu.regs[R4_REGNUM]&0xffff: |
c3f4437e KH |
1146 | cpu.regs[R4_REGNUM]&0xff; |
1147 | ||
1148 | _src = cpu.regs[R5_REGNUM] < memory_size ? cpu.memory+cpu.regs[R5_REGNUM] : | |
1149 | cpu.eightbit + (cpu.regs[R5_REGNUM] & 0xff); | |
d1335144 | 1150 | if ((_src+count)>=(cpu.memory+memory_size)) |
c3f4437e | 1151 | { |
d1335144 | 1152 | if ((_src+count)>=(cpu.eightbit+0x100)) |
c3f4437e KH |
1153 | goto illegal; |
1154 | } | |
1155 | _dst = cpu.regs[R6_REGNUM] < memory_size ? cpu.memory+cpu.regs[R6_REGNUM] : | |
6147b1f6 | 1156 | cpu.eightbit + (cpu.regs[R6_REGNUM] & 0xff); |
d1335144 | 1157 | if ((_dst+count)>=(cpu.memory+memory_size)) |
c3f4437e | 1158 | { |
d1335144 | 1159 | if ((_dst+count)>=(cpu.eightbit+0x100)) |
c3f4437e KH |
1160 | goto illegal; |
1161 | } | |
1162 | memcpy(_dst,_src,count); | |
1163 | ||
1164 | cpu.regs[R5_REGNUM]+=count; | |
1165 | cpu.regs[R6_REGNUM]+=count; | |
d1335144 | 1166 | cpu.regs[R4_REGNUM]&=(code->opcode == O(O_EEPMOV, SW))?(~0xffff):(~0xff); |
c3f4437e KH |
1167 | cycles += 2*count; |
1168 | goto next; | |
1169 | } | |
1170 | goto illegal; | |
c906108c SS |
1171 | |
1172 | case O (O_ADDS, SL): | |
1173 | SET_L_REG (code->dst.reg, | |
1174 | GET_L_REG (code->dst.reg) | |
1175 | + code->src.literal); | |
1176 | ||
1177 | goto next; | |
1178 | ||
1179 | case O (O_SUBS, SL): | |
1180 | SET_L_REG (code->dst.reg, | |
1181 | GET_L_REG (code->dst.reg) | |
1182 | - code->src.literal); | |
1183 | goto next; | |
1184 | ||
1185 | case O (O_CMP, SB): | |
1186 | rd = fetch (&code->dst); | |
1187 | ea = fetch (&code->src); | |
1188 | ea = -ea; | |
1189 | res = rd + ea; | |
1190 | goto just_flags_alu8; | |
1191 | ||
1192 | case O (O_CMP, SW): | |
1193 | rd = fetch (&code->dst); | |
1194 | ea = fetch (&code->src); | |
1195 | ea = -ea; | |
1196 | res = rd + ea; | |
1197 | goto just_flags_alu16; | |
1198 | ||
1199 | case O (O_CMP, SL): | |
1200 | rd = fetch (&code->dst); | |
1201 | ea = fetch (&code->src); | |
1202 | ea = -ea; | |
1203 | res = rd + ea; | |
1204 | goto just_flags_alu32; | |
1205 | ||
1206 | ||
1207 | case O (O_DEC, SB): | |
1208 | rd = GET_B_REG (code->src.reg); | |
1209 | ea = -1; | |
1210 | res = rd + ea; | |
1211 | SET_B_REG (code->src.reg, res); | |
1212 | goto just_flags_inc8; | |
1213 | ||
1214 | case O (O_DEC, SW): | |
1215 | rd = GET_W_REG (code->dst.reg); | |
1216 | ea = -code->src.literal; | |
1217 | res = rd + ea; | |
1218 | SET_W_REG (code->dst.reg, res); | |
1219 | goto just_flags_inc16; | |
1220 | ||
1221 | case O (O_DEC, SL): | |
1222 | rd = GET_L_REG (code->dst.reg); | |
1223 | ea = -code->src.literal; | |
1224 | res = rd + ea; | |
1225 | SET_L_REG (code->dst.reg, res); | |
1226 | goto just_flags_inc32; | |
1227 | ||
1228 | ||
1229 | case O (O_INC, SB): | |
1230 | rd = GET_B_REG (code->src.reg); | |
1231 | ea = 1; | |
1232 | res = rd + ea; | |
1233 | SET_B_REG (code->src.reg, res); | |
1234 | goto just_flags_inc8; | |
1235 | ||
1236 | case O (O_INC, SW): | |
1237 | rd = GET_W_REG (code->dst.reg); | |
1238 | ea = code->src.literal; | |
1239 | res = rd + ea; | |
1240 | SET_W_REG (code->dst.reg, res); | |
1241 | goto just_flags_inc16; | |
1242 | ||
1243 | case O (O_INC, SL): | |
1244 | rd = GET_L_REG (code->dst.reg); | |
1245 | ea = code->src.literal; | |
1246 | res = rd + ea; | |
1247 | SET_L_REG (code->dst.reg, res); | |
1248 | goto just_flags_inc32; | |
1249 | ||
c906108c | 1250 | #define GET_CCR(x) BUILDSR();x = cpu.ccr |
d1335144 | 1251 | #define GET_EXR(x) BUILDEXR ();x = cpu.exr |
c906108c | 1252 | |
6147b1f6 AV |
1253 | case O (O_LDC, SB): |
1254 | case O (O_LDC, SW): | |
d1335144 | 1255 | res = fetch (&code->src); |
6147b1f6 AV |
1256 | goto setc; |
1257 | case O (O_STC, SB): | |
1258 | case O (O_STC, SW): | |
d1335144 | 1259 | if (code->src.type == OP_CCR) |
c3f4437e | 1260 | { |
d1335144 | 1261 | GET_CCR (res); |
c3f4437e | 1262 | } |
d1335144 | 1263 | else if (code->src.type == OP_EXR && h8300smode) |
c3f4437e | 1264 | { |
d1335144 | 1265 | GET_EXR (res); |
c3f4437e KH |
1266 | } |
1267 | else | |
6147b1f6 AV |
1268 | goto illegal; |
1269 | store (&code->dst, res); | |
1270 | goto next; | |
1271 | ||
c906108c | 1272 | case O (O_ANDC, SB): |
d1335144 | 1273 | if (code->dst.type == OP_CCR) |
c3f4437e KH |
1274 | { |
1275 | GET_CCR (rd); | |
1276 | } | |
d1335144 | 1277 | else if (code->dst.type == OP_EXR && h8300smode) |
c3f4437e KH |
1278 | { |
1279 | GET_EXR (rd); | |
1280 | } | |
1281 | else | |
fc974602 | 1282 | goto illegal; |
c906108c SS |
1283 | ea = code->src.literal; |
1284 | res = rd & ea; | |
1285 | goto setc; | |
1286 | ||
1287 | case O (O_ORC, SB): | |
d1335144 | 1288 | if (code->dst.type == OP_CCR) |
c3f4437e KH |
1289 | { |
1290 | GET_CCR (rd); | |
1291 | } | |
d1335144 | 1292 | else if (code->dst.type == OP_EXR && h8300smode) |
c3f4437e KH |
1293 | { |
1294 | GET_EXR (rd); | |
1295 | } | |
1296 | else | |
fc974602 | 1297 | goto illegal; |
c906108c SS |
1298 | ea = code->src.literal; |
1299 | res = rd | ea; | |
1300 | goto setc; | |
1301 | ||
1302 | case O (O_XORC, SB): | |
d1335144 | 1303 | if (code->dst.type == OP_CCR) |
c3f4437e KH |
1304 | { |
1305 | GET_CCR (rd); | |
1306 | } | |
d1335144 | 1307 | else if (code->dst.type == OP_EXR && h8300smode) |
c3f4437e KH |
1308 | { |
1309 | GET_EXR (rd); | |
1310 | } | |
1311 | else | |
fc974602 | 1312 | goto illegal; |
c906108c SS |
1313 | ea = code->src.literal; |
1314 | res = rd ^ ea; | |
1315 | goto setc; | |
1316 | ||
1317 | ||
1318 | case O (O_BRA, SB): | |
1319 | if (1) | |
1320 | goto condtrue; | |
1321 | goto next; | |
1322 | ||
1323 | case O (O_BRN, SB): | |
1324 | if (0) | |
1325 | goto condtrue; | |
1326 | goto next; | |
1327 | ||
1328 | case O (O_BHI, SB): | |
1329 | if ((C || Z) == 0) | |
1330 | goto condtrue; | |
1331 | goto next; | |
1332 | ||
1333 | ||
1334 | case O (O_BLS, SB): | |
1335 | if ((C || Z)) | |
1336 | goto condtrue; | |
1337 | goto next; | |
1338 | ||
1339 | case O (O_BCS, SB): | |
1340 | if ((C == 1)) | |
1341 | goto condtrue; | |
1342 | goto next; | |
1343 | ||
1344 | case O (O_BCC, SB): | |
1345 | if ((C == 0)) | |
1346 | goto condtrue; | |
1347 | goto next; | |
1348 | ||
1349 | case O (O_BEQ, SB): | |
1350 | if (Z) | |
1351 | goto condtrue; | |
1352 | goto next; | |
1353 | case O (O_BGT, SB): | |
1354 | if (((Z || (N ^ V)) == 0)) | |
1355 | goto condtrue; | |
1356 | goto next; | |
1357 | ||
1358 | ||
1359 | case O (O_BLE, SB): | |
1360 | if (((Z || (N ^ V)) == 1)) | |
1361 | goto condtrue; | |
1362 | goto next; | |
1363 | ||
1364 | case O (O_BGE, SB): | |
1365 | if ((N ^ V) == 0) | |
1366 | goto condtrue; | |
1367 | goto next; | |
1368 | case O (O_BLT, SB): | |
1369 | if ((N ^ V)) | |
1370 | goto condtrue; | |
1371 | goto next; | |
1372 | case O (O_BMI, SB): | |
1373 | if ((N)) | |
1374 | goto condtrue; | |
1375 | goto next; | |
1376 | case O (O_BNE, SB): | |
1377 | if ((Z == 0)) | |
1378 | goto condtrue; | |
1379 | goto next; | |
1380 | ||
1381 | case O (O_BPL, SB): | |
1382 | if (N == 0) | |
1383 | goto condtrue; | |
1384 | goto next; | |
1385 | case O (O_BVC, SB): | |
1386 | if ((V == 0)) | |
1387 | goto condtrue; | |
1388 | goto next; | |
1389 | case O (O_BVS, SB): | |
1390 | if ((V == 1)) | |
1391 | goto condtrue; | |
1392 | goto next; | |
1393 | ||
1394 | case O (O_SYSCALL, SB): | |
1395 | { | |
1396 | char c = cpu.regs[2]; | |
1397 | sim_callback->write_stdout (sim_callback, &c, 1); | |
1398 | } | |
1399 | goto next; | |
1400 | ||
1401 | ONOT (O_NOT, rd = ~rd; v = 0;); | |
1402 | OSHIFTS (O_SHLL, | |
1403 | c = rd & hm; v = 0; rd <<= 1, | |
1404 | c = rd & (hm >> 1); v = 0; rd <<= 2); | |
1405 | OSHIFTS (O_SHLR, | |
1406 | c = rd & 1; v = 0; rd = (unsigned int) rd >> 1, | |
1407 | c = rd & 2; v = 0; rd = (unsigned int) rd >> 2); | |
1408 | OSHIFTS (O_SHAL, | |
1409 | c = rd & hm; v = (rd & hm) != ((rd & (hm >> 1)) << 1); rd <<= 1, | |
1410 | c = rd & (hm >> 1); v = (rd & (hm >> 1)) != ((rd & (hm >> 2)) << 2); rd <<= 2); | |
1411 | OSHIFTS (O_SHAR, | |
1412 | t = rd & hm; c = rd & 1; v = 0; rd >>= 1; rd |= t, | |
d1335144 | 1413 | t = rd & hm; c = rd & 2; v = 0; rd >>= 2; rd |= t | t >> 1); |
c906108c SS |
1414 | OSHIFTS (O_ROTL, |
1415 | c = rd & hm; v = 0; rd <<= 1; rd |= C, | |
1416 | c = rd & hm; v = 0; rd <<= 1; rd |= C; c = rd & hm; rd <<= 1; rd |= C); | |
1417 | OSHIFTS (O_ROTR, | |
1418 | c = rd & 1; v = 0; rd = (unsigned int) rd >> 1; if (c) rd |= hm, | |
1419 | c = rd & 1; v = 0; rd = (unsigned int) rd >> 1; if (c) rd |= hm; c = rd & 1; rd = (unsigned int) rd >> 1; if (c) rd |= hm); | |
1420 | OSHIFTS (O_ROTXL, | |
1421 | t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0, | |
1422 | t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0; t = rd & hm; rd <<= 1; rd |= C; c = t); | |
1423 | OSHIFTS (O_ROTXR, | |
1424 | t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t; v = 0, | |
1425 | t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t; v = 0; t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t); | |
1426 | ||
1427 | case O (O_JMP, SB): | |
1428 | { | |
1429 | pc = fetch (&code->src); | |
1430 | goto end; | |
1431 | ||
1432 | } | |
1433 | ||
1434 | case O (O_JSR, SB): | |
1435 | { | |
1436 | int tmp; | |
1437 | pc = fetch (&code->src); | |
1438 | call: | |
1439 | tmp = cpu.regs[7]; | |
1440 | ||
1441 | if (h8300hmode) | |
1442 | { | |
1443 | tmp -= 4; | |
1444 | SET_MEMORY_L (tmp, code->next_pc); | |
1445 | } | |
1446 | else | |
1447 | { | |
1448 | tmp -= 2; | |
1449 | SET_MEMORY_W (tmp, code->next_pc); | |
1450 | } | |
1451 | cpu.regs[7] = tmp; | |
1452 | ||
1453 | goto end; | |
1454 | } | |
1455 | case O (O_BSR, SB): | |
1456 | pc = code->src.literal; | |
1457 | goto call; | |
1458 | ||
1459 | case O (O_RTS, SN): | |
1460 | { | |
1461 | int tmp; | |
1462 | ||
1463 | tmp = cpu.regs[7]; | |
1464 | ||
1465 | if (h8300hmode) | |
1466 | { | |
1467 | pc = GET_MEMORY_L (tmp); | |
1468 | tmp += 4; | |
1469 | } | |
1470 | else | |
1471 | { | |
1472 | pc = GET_MEMORY_W (tmp); | |
1473 | tmp += 2; | |
1474 | } | |
1475 | ||
1476 | cpu.regs[7] = tmp; | |
1477 | goto end; | |
1478 | } | |
1479 | ||
1480 | case O (O_ILL, SB): | |
1481 | cpu.state = SIM_STATE_STOPPED; | |
1482 | cpu.exception = SIGILL; | |
1483 | goto end; | |
1484 | case O (O_SLEEP, SN): | |
c906108c SS |
1485 | /* FIXME: Doesn't this break for breakpoints when r0 |
1486 | contains just the right (er, wrong) value? */ | |
1487 | cpu.state = SIM_STATE_STOPPED; | |
97ee9e5a FCE |
1488 | /* The format of r0 is defined by target newlib. Expand |
1489 | the macros here instead of looking for .../sys/wait.h. */ | |
1490 | #define SIM_WIFEXITED(v) (((v) & 0xff) == 0) | |
1491 | #define SIM_WIFSIGNALED(v) (((v) & 0x7f) > 0 && (((v) & 0x7f) < 0x7f)) | |
c3f4437e | 1492 | if (! SIM_WIFEXITED (cpu.regs[0]) && SIM_WIFSIGNALED (cpu.regs[0])) |
c906108c SS |
1493 | cpu.exception = SIGILL; |
1494 | else | |
1495 | cpu.exception = SIGTRAP; | |
c906108c SS |
1496 | goto end; |
1497 | case O (O_BPT, SN): | |
1498 | cpu.state = SIM_STATE_STOPPED; | |
1499 | cpu.exception = SIGTRAP; | |
1500 | goto end; | |
1501 | ||
1502 | OBITOP (O_BNOT, 1, 1, ea ^= m); | |
1503 | OBITOP (O_BTST, 1, 0, nz = ea & m); | |
1504 | OBITOP (O_BCLR, 1, 1, ea &= ~m); | |
c3f4437e | 1505 | OBITOP (O_BSET, 1, 1, ea |= m); |
c906108c SS |
1506 | OBITOP (O_BLD, 1, 0, c = ea & m); |
1507 | OBITOP (O_BILD, 1, 0, c = !(ea & m)); | |
1508 | OBITOP (O_BST, 1, 1, ea &= ~m; | |
1509 | if (C) ea |= m); | |
1510 | OBITOP (O_BIST, 1, 1, ea &= ~m; | |
1511 | if (!C) ea |= m); | |
1512 | OBITOP (O_BAND, 1, 0, c = (ea & m) && C); | |
1513 | OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C); | |
1514 | OBITOP (O_BOR, 1, 0, c = (ea & m) || C); | |
1515 | OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C); | |
1516 | OBITOP (O_BXOR, 1, 0, c = (ea & m) != C); | |
1517 | OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C); | |
1518 | ||
de9b1892 KH |
1519 | #define MOP(bsize, signed) \ |
1520 | mop (code, bsize, signed); \ | |
1521 | goto next; | |
c906108c SS |
1522 | |
1523 | case O (O_MULS, SB): | |
1524 | MOP (1, 1); | |
1525 | break; | |
1526 | case O (O_MULS, SW): | |
1527 | MOP (0, 1); | |
1528 | break; | |
1529 | case O (O_MULU, SB): | |
1530 | MOP (1, 0); | |
1531 | break; | |
1532 | case O (O_MULU, SW): | |
1533 | MOP (0, 0); | |
1534 | break; | |
1535 | ||
6147b1f6 | 1536 | case O (O_TAS, SB): |
d1335144 | 1537 | if (!h8300smode || code->src.type != X (OP_REG, SL)) |
c3f4437e | 1538 | goto illegal; |
d1335144 | 1539 | switch (code->src.reg) |
c3f4437e KH |
1540 | { |
1541 | case R0_REGNUM: | |
1542 | case R1_REGNUM: | |
1543 | case R4_REGNUM: | |
1544 | case R5_REGNUM: | |
1545 | break; | |
1546 | default: | |
1547 | goto illegal; | |
1548 | } | |
1549 | res = fetch (&code->src); | |
1550 | store (&code->src,res|0x80); | |
6147b1f6 | 1551 | goto just_flags_log8; |
c906108c SS |
1552 | |
1553 | case O (O_DIVU, SB): | |
1554 | { | |
1555 | rd = GET_W_REG (code->dst.reg); | |
1556 | ea = GET_B_REG (code->src.reg); | |
1557 | if (ea) | |
1558 | { | |
de9b1892 KH |
1559 | tmp = (unsigned) rd % ea; |
1560 | rd = (unsigned) rd / ea; | |
c906108c SS |
1561 | } |
1562 | SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8)); | |
1563 | n = ea & 0x80; | |
1564 | nz = ea & 0xff; | |
1565 | ||
1566 | goto next; | |
1567 | } | |
1568 | case O (O_DIVU, SW): | |
1569 | { | |
1570 | rd = GET_L_REG (code->dst.reg); | |
1571 | ea = GET_W_REG (code->src.reg); | |
1572 | n = ea & 0x8000; | |
1573 | nz = ea & 0xffff; | |
1574 | if (ea) | |
1575 | { | |
de9b1892 KH |
1576 | tmp = (unsigned) rd % ea; |
1577 | rd = (unsigned) rd / ea; | |
c906108c SS |
1578 | } |
1579 | SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16)); | |
1580 | goto next; | |
1581 | } | |
1582 | ||
1583 | case O (O_DIVS, SB): | |
1584 | { | |
1585 | ||
1586 | rd = SEXTSHORT (GET_W_REG (code->dst.reg)); | |
1587 | ea = SEXTCHAR (GET_B_REG (code->src.reg)); | |
1588 | if (ea) | |
1589 | { | |
1590 | tmp = (int) rd % (int) ea; | |
1591 | rd = (int) rd / (int) ea; | |
1592 | n = rd & 0x8000; | |
1593 | nz = 1; | |
1594 | } | |
1595 | else | |
1596 | nz = 0; | |
1597 | SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8)); | |
1598 | goto next; | |
1599 | } | |
1600 | case O (O_DIVS, SW): | |
1601 | { | |
1602 | rd = GET_L_REG (code->dst.reg); | |
1603 | ea = SEXTSHORT (GET_W_REG (code->src.reg)); | |
1604 | if (ea) | |
1605 | { | |
1606 | tmp = (int) rd % (int) ea; | |
1607 | rd = (int) rd / (int) ea; | |
1608 | n = rd & 0x80000000; | |
1609 | nz = 1; | |
1610 | } | |
1611 | else | |
1612 | nz = 0; | |
1613 | SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16)); | |
1614 | goto next; | |
1615 | } | |
1616 | case O (O_EXTS, SW): | |
1617 | rd = GET_B_REG (code->src.reg + 8) & 0xff; /* Yes, src, not dst. */ | |
1618 | ea = rd & 0x80 ? -256 : 0; | |
1619 | res = rd + ea; | |
1620 | goto log16; | |
1621 | case O (O_EXTS, SL): | |
1622 | rd = GET_W_REG (code->src.reg) & 0xffff; | |
1623 | ea = rd & 0x8000 ? -65536 : 0; | |
1624 | res = rd + ea; | |
1625 | goto log32; | |
1626 | case O (O_EXTU, SW): | |
1627 | rd = GET_B_REG (code->src.reg + 8) & 0xff; | |
1628 | ea = 0; | |
1629 | res = rd + ea; | |
1630 | goto log16; | |
1631 | case O (O_EXTU, SL): | |
1632 | rd = GET_W_REG (code->src.reg) & 0xffff; | |
1633 | ea = 0; | |
1634 | res = rd + ea; | |
1635 | goto log32; | |
1636 | ||
1637 | case O (O_NOP, SN): | |
1638 | goto next; | |
1639 | ||
1640 | case O (O_STM, SL): | |
1641 | { | |
1642 | int nregs, firstreg, i; | |
1643 | ||
1644 | nregs = GET_MEMORY_B (pc + 1); | |
1645 | nregs >>= 4; | |
1646 | nregs &= 0xf; | |
1647 | firstreg = GET_MEMORY_B (pc + 3); | |
1648 | firstreg &= 0xf; | |
1649 | for (i = firstreg; i <= firstreg + nregs; i++) | |
1650 | { | |
1651 | cpu.regs[7] -= 4; | |
1652 | SET_MEMORY_L (cpu.regs[7], cpu.regs[i]); | |
1653 | } | |
1654 | } | |
1655 | goto next; | |
1656 | ||
1657 | case O (O_LDM, SL): | |
1658 | { | |
1659 | int nregs, firstreg, i; | |
1660 | ||
1661 | nregs = GET_MEMORY_B (pc + 1); | |
1662 | nregs >>= 4; | |
1663 | nregs &= 0xf; | |
1664 | firstreg = GET_MEMORY_B (pc + 3); | |
1665 | firstreg &= 0xf; | |
1666 | for (i = firstreg; i >= firstreg - nregs; i--) | |
1667 | { | |
1668 | cpu.regs[i] = GET_MEMORY_L (cpu.regs[7]); | |
1669 | cpu.regs[7] += 4; | |
1670 | } | |
1671 | } | |
1672 | goto next; | |
1673 | ||
1674 | default: | |
fc974602 | 1675 | illegal: |
c906108c SS |
1676 | cpu.state = SIM_STATE_STOPPED; |
1677 | cpu.exception = SIGILL; | |
1678 | goto end; | |
1679 | ||
1680 | } | |
1681 | abort (); | |
1682 | ||
1683 | setc: | |
d1335144 | 1684 | if (code->dst.type == OP_CCR) |
c3f4437e KH |
1685 | { |
1686 | cpu.ccr = res; | |
1687 | GETSR (); | |
1688 | } | |
d1335144 | 1689 | else if (code->dst.type == OP_EXR && h8300smode) |
c3f4437e KH |
1690 | { |
1691 | cpu.exr = res; | |
1692 | GETEXR (); | |
1693 | } | |
fc974602 | 1694 | else |
c3f4437e | 1695 | goto illegal; |
fc974602 | 1696 | |
c906108c SS |
1697 | goto next; |
1698 | ||
1699 | condtrue: | |
1700 | /* When a branch works */ | |
1701 | pc = code->src.literal; | |
1702 | goto end; | |
1703 | ||
1704 | /* Set the cond codes from res */ | |
1705 | bitop: | |
1706 | ||
1707 | /* Set the flags after an 8 bit inc/dec operation */ | |
1708 | just_flags_inc8: | |
1709 | n = res & 0x80; | |
1710 | nz = res & 0xff; | |
1711 | v = (rd & 0x7f) == 0x7f; | |
1712 | goto next; | |
1713 | ||
1714 | ||
1715 | /* Set the flags after an 16 bit inc/dec operation */ | |
1716 | just_flags_inc16: | |
1717 | n = res & 0x8000; | |
1718 | nz = res & 0xffff; | |
1719 | v = (rd & 0x7fff) == 0x7fff; | |
1720 | goto next; | |
1721 | ||
1722 | ||
1723 | /* Set the flags after an 32 bit inc/dec operation */ | |
1724 | just_flags_inc32: | |
1725 | n = res & 0x80000000; | |
1726 | nz = res & 0xffffffff; | |
1727 | v = (rd & 0x7fffffff) == 0x7fffffff; | |
1728 | goto next; | |
1729 | ||
1730 | ||
1731 | shift8: | |
1732 | /* Set flags after an 8 bit shift op, carry,overflow set in insn */ | |
1733 | n = (rd & 0x80); | |
1734 | nz = rd & 0xff; | |
1735 | SET_B_REG (code->src.reg, rd); | |
1736 | goto next; | |
1737 | ||
1738 | shift16: | |
1739 | /* Set flags after an 16 bit shift op, carry,overflow set in insn */ | |
1740 | n = (rd & 0x8000); | |
1741 | nz = rd & 0xffff; | |
1742 | SET_W_REG (code->src.reg, rd); | |
1743 | goto next; | |
1744 | ||
1745 | shift32: | |
1746 | /* Set flags after an 32 bit shift op, carry,overflow set in insn */ | |
1747 | n = (rd & 0x80000000); | |
1748 | nz = rd & 0xffffffff; | |
1749 | SET_L_REG (code->src.reg, rd); | |
1750 | goto next; | |
1751 | ||
1752 | log32: | |
1753 | store (&code->dst, res); | |
1754 | just_flags_log32: | |
1755 | /* flags after a 32bit logical operation */ | |
1756 | n = res & 0x80000000; | |
1757 | nz = res & 0xffffffff; | |
1758 | v = 0; | |
1759 | goto next; | |
1760 | ||
1761 | log16: | |
1762 | store (&code->dst, res); | |
1763 | just_flags_log16: | |
1764 | /* flags after a 16bit logical operation */ | |
1765 | n = res & 0x8000; | |
1766 | nz = res & 0xffff; | |
1767 | v = 0; | |
1768 | goto next; | |
1769 | ||
1770 | ||
1771 | log8: | |
1772 | store (&code->dst, res); | |
1773 | just_flags_log8: | |
1774 | n = res & 0x80; | |
1775 | nz = res & 0xff; | |
1776 | v = 0; | |
1777 | goto next; | |
1778 | ||
1779 | alu8: | |
1780 | SET_B_REG (code->dst.reg, res); | |
1781 | just_flags_alu8: | |
1782 | n = res & 0x80; | |
1783 | nz = res & 0xff; | |
1784 | c = (res & 0x100); | |
1785 | switch (code->opcode / 4) | |
1786 | { | |
1787 | case O_ADD: | |
1788 | v = ((rd & 0x80) == (ea & 0x80) | |
1789 | && (rd & 0x80) != (res & 0x80)); | |
1790 | break; | |
1791 | case O_SUB: | |
1792 | case O_CMP: | |
1793 | v = ((rd & 0x80) != (-ea & 0x80) | |
1794 | && (rd & 0x80) != (res & 0x80)); | |
1795 | break; | |
1796 | case O_NEG: | |
1797 | v = (rd == 0x80); | |
1798 | break; | |
1799 | } | |
1800 | goto next; | |
1801 | ||
1802 | alu16: | |
1803 | SET_W_REG (code->dst.reg, res); | |
1804 | just_flags_alu16: | |
1805 | n = res & 0x8000; | |
1806 | nz = res & 0xffff; | |
1807 | c = (res & 0x10000); | |
1808 | switch (code->opcode / 4) | |
1809 | { | |
1810 | case O_ADD: | |
1811 | v = ((rd & 0x8000) == (ea & 0x8000) | |
1812 | && (rd & 0x8000) != (res & 0x8000)); | |
1813 | break; | |
1814 | case O_SUB: | |
1815 | case O_CMP: | |
1816 | v = ((rd & 0x8000) != (-ea & 0x8000) | |
1817 | && (rd & 0x8000) != (res & 0x8000)); | |
1818 | break; | |
1819 | case O_NEG: | |
1820 | v = (rd == 0x8000); | |
1821 | break; | |
1822 | } | |
1823 | goto next; | |
1824 | ||
1825 | alu32: | |
1826 | SET_L_REG (code->dst.reg, res); | |
1827 | just_flags_alu32: | |
1828 | n = res & 0x80000000; | |
1829 | nz = res & 0xffffffff; | |
1830 | switch (code->opcode / 4) | |
1831 | { | |
1832 | case O_ADD: | |
1833 | v = ((rd & 0x80000000) == (ea & 0x80000000) | |
1834 | && (rd & 0x80000000) != (res & 0x80000000)); | |
1835 | c = ((unsigned) res < (unsigned) rd) || ((unsigned) res < (unsigned) ea); | |
1836 | break; | |
1837 | case O_SUB: | |
1838 | case O_CMP: | |
1839 | v = ((rd & 0x80000000) != (-ea & 0x80000000) | |
1840 | && (rd & 0x80000000) != (res & 0x80000000)); | |
1841 | c = (unsigned) rd < (unsigned) -ea; | |
1842 | break; | |
1843 | case O_NEG: | |
1844 | v = (rd == 0x80000000); | |
1845 | c = res != 0; | |
1846 | break; | |
1847 | } | |
1848 | goto next; | |
1849 | ||
1850 | next:; | |
1851 | pc = code->next_pc; | |
1852 | ||
1853 | end: | |
1854 | ; | |
de9b1892 KH |
1855 | #if 0 |
1856 | if (cpu.regs[8]) | |
1857 | abort (); | |
1858 | #endif | |
c906108c SS |
1859 | |
1860 | if (--poll_count < 0) | |
1861 | { | |
7a292a7a | 1862 | poll_count = POLL_QUIT_INTERVAL; |
c906108c SS |
1863 | if ((*sim_callback->poll_quit) != NULL |
1864 | && (*sim_callback->poll_quit) (sim_callback)) | |
1865 | sim_stop (sd); | |
1866 | } | |
1867 | ||
1868 | } | |
1869 | while (cpu.state == SIM_STATE_RUNNING); | |
1870 | cpu.ticks += get_now () - tick_start; | |
1871 | cpu.cycles += cycles; | |
1872 | cpu.insts += insts; | |
de9b1892 | 1873 | |
c906108c SS |
1874 | cpu.pc = pc; |
1875 | BUILDSR (); | |
d1335144 | 1876 | BUILDEXR (); |
c906108c SS |
1877 | cpu.mask = oldmask; |
1878 | signal (SIGINT, prev); | |
1879 | } | |
1880 | ||
1881 | int | |
1882 | sim_trace (sd) | |
1883 | SIM_DESC sd; | |
1884 | { | |
2ea716f6 | 1885 | /* FIXME: Unfinished. */ |
c906108c SS |
1886 | abort (); |
1887 | } | |
1888 | ||
1889 | int | |
1890 | sim_write (sd, addr, buffer, size) | |
1891 | SIM_DESC sd; | |
1892 | SIM_ADDR addr; | |
1893 | unsigned char *buffer; | |
1894 | int size; | |
1895 | { | |
1896 | int i; | |
1897 | ||
1898 | init_pointers (); | |
1899 | if (addr < 0) | |
1900 | return 0; | |
1901 | for (i = 0; i < size; i++) | |
1902 | { | |
1903 | if (addr < memory_size) | |
1904 | { | |
1905 | cpu.memory[addr + i] = buffer[i]; | |
1906 | cpu.cache_idx[addr + i] = 0; | |
1907 | } | |
1908 | else | |
1909 | cpu.eightbit[(addr + i) & 0xff] = buffer[i]; | |
1910 | } | |
1911 | return size; | |
1912 | } | |
1913 | ||
1914 | int | |
1915 | sim_read (sd, addr, buffer, size) | |
1916 | SIM_DESC sd; | |
1917 | SIM_ADDR addr; | |
1918 | unsigned char *buffer; | |
1919 | int size; | |
1920 | { | |
1921 | init_pointers (); | |
1922 | if (addr < 0) | |
1923 | return 0; | |
1924 | if (addr < memory_size) | |
1925 | memcpy (buffer, cpu.memory + addr, size); | |
1926 | else | |
1927 | memcpy (buffer, cpu.eightbit + (addr & 0xff), size); | |
1928 | return size; | |
1929 | } | |
1930 | ||
1931 | ||
c906108c SS |
1932 | int |
1933 | sim_store_register (sd, rn, value, length) | |
1934 | SIM_DESC sd; | |
1935 | int rn; | |
1936 | unsigned char *value; | |
1937 | int length; | |
1938 | { | |
1939 | int longval; | |
1940 | int shortval; | |
1941 | int intval; | |
1942 | longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3]; | |
1943 | shortval = (value[0] << 8) | (value[1]); | |
1944 | intval = h8300hmode ? longval : shortval; | |
1945 | ||
1946 | init_pointers (); | |
1947 | switch (rn) | |
1948 | { | |
1949 | case PC_REGNUM: | |
1950 | cpu.pc = intval; | |
1951 | break; | |
1952 | default: | |
1953 | abort (); | |
1954 | case R0_REGNUM: | |
1955 | case R1_REGNUM: | |
1956 | case R2_REGNUM: | |
1957 | case R3_REGNUM: | |
1958 | case R4_REGNUM: | |
1959 | case R5_REGNUM: | |
1960 | case R6_REGNUM: | |
1961 | case R7_REGNUM: | |
1962 | cpu.regs[rn] = intval; | |
1963 | break; | |
1964 | case CCR_REGNUM: | |
1965 | cpu.ccr = intval; | |
1966 | break; | |
fc974602 AV |
1967 | case EXR_REGNUM: |
1968 | cpu.exr = intval; | |
1969 | break; | |
c906108c SS |
1970 | case CYCLE_REGNUM: |
1971 | cpu.cycles = longval; | |
1972 | break; | |
1973 | ||
1974 | case INST_REGNUM: | |
1975 | cpu.insts = longval; | |
1976 | break; | |
1977 | ||
1978 | case TICK_REGNUM: | |
1979 | cpu.ticks = longval; | |
1980 | break; | |
1981 | } | |
1982 | return -1; | |
1983 | } | |
1984 | ||
1985 | int | |
1986 | sim_fetch_register (sd, rn, buf, length) | |
1987 | SIM_DESC sd; | |
1988 | int rn; | |
1989 | unsigned char *buf; | |
1990 | int length; | |
1991 | { | |
1992 | int v; | |
1993 | int longreg = 0; | |
1994 | ||
1995 | init_pointers (); | |
1996 | ||
d1335144 | 1997 | if (!h8300smode && rn >=EXR_REGNUM) |
c3f4437e | 1998 | rn++; |
c906108c SS |
1999 | switch (rn) |
2000 | { | |
2001 | default: | |
2002 | abort (); | |
3b02cf92 | 2003 | case CCR_REGNUM: |
c906108c SS |
2004 | v = cpu.ccr; |
2005 | break; | |
fc974602 AV |
2006 | case EXR_REGNUM: |
2007 | v = cpu.exr; | |
2008 | break; | |
3b02cf92 | 2009 | case PC_REGNUM: |
c906108c SS |
2010 | v = cpu.pc; |
2011 | break; | |
2012 | case R0_REGNUM: | |
2013 | case R1_REGNUM: | |
2014 | case R2_REGNUM: | |
2015 | case R3_REGNUM: | |
2016 | case R4_REGNUM: | |
2017 | case R5_REGNUM: | |
2018 | case R6_REGNUM: | |
2019 | case R7_REGNUM: | |
2020 | v = cpu.regs[rn]; | |
2021 | break; | |
3b02cf92 | 2022 | case CYCLE_REGNUM: |
c906108c SS |
2023 | v = cpu.cycles; |
2024 | longreg = 1; | |
2025 | break; | |
3b02cf92 | 2026 | case TICK_REGNUM: |
c906108c SS |
2027 | v = cpu.ticks; |
2028 | longreg = 1; | |
2029 | break; | |
3b02cf92 | 2030 | case INST_REGNUM: |
c906108c SS |
2031 | v = cpu.insts; |
2032 | longreg = 1; | |
2033 | break; | |
2034 | } | |
2035 | if (h8300hmode || longreg) | |
2036 | { | |
2037 | buf[0] = v >> 24; | |
2038 | buf[1] = v >> 16; | |
2039 | buf[2] = v >> 8; | |
2040 | buf[3] = v >> 0; | |
2041 | } | |
2042 | else | |
2043 | { | |
2044 | buf[0] = v >> 8; | |
2045 | buf[1] = v; | |
2046 | } | |
2047 | return -1; | |
2048 | } | |
2049 | ||
2050 | void | |
2051 | sim_stop_reason (sd, reason, sigrc) | |
2052 | SIM_DESC sd; | |
2053 | enum sim_stop *reason; | |
2054 | int *sigrc; | |
2055 | { | |
2056 | #if 0 /* FIXME: This should work but we can't use it. | |
2057 | grep for SLEEP above. */ | |
2058 | switch (cpu.state) | |
2059 | { | |
2060 | case SIM_STATE_EXITED : *reason = sim_exited; break; | |
2061 | case SIM_STATE_SIGNALLED : *reason = sim_signalled; break; | |
2062 | case SIM_STATE_STOPPED : *reason = sim_stopped; break; | |
2063 | default : abort (); | |
2064 | } | |
2065 | #else | |
2066 | *reason = sim_stopped; | |
2067 | #endif | |
2068 | *sigrc = cpu.exception; | |
2069 | } | |
2070 | ||
2071 | /* FIXME: Rename to sim_set_mem_size. */ | |
2072 | ||
2073 | void | |
2074 | sim_size (n) | |
2075 | int n; | |
2076 | { | |
2077 | /* Memory size is fixed. */ | |
2078 | } | |
2079 | ||
2080 | void | |
2081 | sim_set_simcache_size (n) | |
2082 | { | |
2083 | if (cpu.cache) | |
2084 | free (cpu.cache); | |
2085 | if (n < 2) | |
2086 | n = 2; | |
2087 | cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n); | |
2088 | memset (cpu.cache, 0, sizeof (decoded_inst) * n); | |
2089 | cpu.csize = n; | |
2090 | } | |
2091 | ||
2092 | ||
2093 | void | |
2094 | sim_info (sd, verbose) | |
2095 | SIM_DESC sd; | |
2096 | int verbose; | |
2097 | { | |
2098 | double timetaken = (double) cpu.ticks / (double) now_persec (); | |
2099 | double virttime = cpu.cycles / 10.0e6; | |
2100 | ||
2101 | (*sim_callback->printf_filtered) (sim_callback, | |
2102 | "\n\n#instructions executed %10d\n", | |
2103 | cpu.insts); | |
2104 | (*sim_callback->printf_filtered) (sim_callback, | |
2105 | "#cycles (v approximate) %10d\n", | |
2106 | cpu.cycles); | |
2107 | (*sim_callback->printf_filtered) (sim_callback, | |
2108 | "#real time taken %10.4f\n", | |
2109 | timetaken); | |
2110 | (*sim_callback->printf_filtered) (sim_callback, | |
2111 | "#virtual time taked %10.4f\n", | |
2112 | virttime); | |
2113 | if (timetaken != 0.0) | |
2114 | (*sim_callback->printf_filtered) (sim_callback, | |
2115 | "#simulation ratio %10.4f\n", | |
2116 | virttime / timetaken); | |
2117 | (*sim_callback->printf_filtered) (sim_callback, | |
2118 | "#compiles %10d\n", | |
2119 | cpu.compiles); | |
2120 | (*sim_callback->printf_filtered) (sim_callback, | |
2121 | "#cache size %10d\n", | |
2122 | cpu.csize); | |
2123 | ||
2124 | #ifdef ADEBUG | |
2125 | /* This to be conditional on `what' (aka `verbose'), | |
2126 | however it was never passed as non-zero. */ | |
2127 | if (1) | |
2128 | { | |
2129 | int i; | |
2130 | for (i = 0; i < O_LAST; i++) | |
2131 | { | |
2132 | if (cpu.stats[i]) | |
2133 | (*sim_callback->printf_filtered) (sim_callback, | |
2134 | "%d: %d\n", i, cpu.stats[i]); | |
2135 | } | |
2136 | } | |
2137 | #endif | |
2138 | } | |
2139 | ||
2ea716f6 KH |
2140 | /* Indicate whether the cpu is an H8/300 or H8/300H. |
2141 | FLAG is non-zero for the H8/300H. */ | |
c906108c SS |
2142 | |
2143 | void | |
a8cdafbd AV |
2144 | set_h8300h (h_flag, s_flag) |
2145 | int h_flag, s_flag; | |
c906108c SS |
2146 | { |
2147 | /* FIXME: Much of the code in sim_load can be moved to sim_open. | |
2148 | This function being replaced by a sim_open:ARGV configuration | |
2ea716f6 | 2149 | option. */ |
a8cdafbd AV |
2150 | h8300hmode = h_flag; |
2151 | h8300smode = s_flag; | |
c906108c SS |
2152 | } |
2153 | ||
2154 | SIM_DESC | |
2155 | sim_open (kind, ptr, abfd, argv) | |
2156 | SIM_OPEN_KIND kind; | |
2157 | struct host_callback_struct *ptr; | |
2158 | struct _bfd *abfd; | |
2159 | char **argv; | |
2160 | { | |
2ea716f6 | 2161 | /* FIXME: Much of the code in sim_load can be moved here. */ |
c906108c SS |
2162 | |
2163 | sim_kind = kind; | |
2164 | myname = argv[0]; | |
2165 | sim_callback = ptr; | |
2ea716f6 | 2166 | /* Fudge our descriptor. */ |
c906108c SS |
2167 | return (SIM_DESC) 1; |
2168 | } | |
2169 | ||
2170 | void | |
2171 | sim_close (sd, quitting) | |
2172 | SIM_DESC sd; | |
2173 | int quitting; | |
2174 | { | |
2ea716f6 | 2175 | /* Nothing to do. */ |
c906108c SS |
2176 | } |
2177 | ||
2178 | /* Called by gdb to load a program into memory. */ | |
2179 | ||
2180 | SIM_RC | |
2181 | sim_load (sd, prog, abfd, from_tty) | |
2182 | SIM_DESC sd; | |
2183 | char *prog; | |
2184 | bfd *abfd; | |
2185 | int from_tty; | |
2186 | { | |
2187 | bfd *prog_bfd; | |
2188 | ||
2ea716f6 KH |
2189 | /* FIXME: The code below that sets a specific variant of the H8/300 |
2190 | being simulated should be moved to sim_open(). */ | |
c906108c | 2191 | |
2ea716f6 | 2192 | /* See if the file is for the H8/300 or H8/300H. */ |
c906108c SS |
2193 | /* ??? This may not be the most efficient way. The z8k simulator |
2194 | does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */ | |
2195 | if (abfd != NULL) | |
2196 | prog_bfd = abfd; | |
2197 | else | |
2198 | prog_bfd = bfd_openr (prog, "coff-h8300"); | |
2199 | if (prog_bfd != NULL) | |
2200 | { | |
2201 | /* Set the cpu type. We ignore failure from bfd_check_format | |
2202 | and bfd_openr as sim_load_file checks too. */ | |
de9b1892 | 2203 | if (bfd_check_format (prog_bfd, bfd_object)) |
c906108c SS |
2204 | { |
2205 | unsigned long mach = bfd_get_mach (prog_bfd); | |
a8cdafbd AV |
2206 | set_h8300h (mach == bfd_mach_h8300h || mach == bfd_mach_h8300s, |
2207 | mach == bfd_mach_h8300s); | |
c906108c SS |
2208 | } |
2209 | } | |
2210 | ||
2211 | /* If we're using gdb attached to the simulator, then we have to | |
2212 | reallocate memory for the simulator. | |
2213 | ||
2214 | When gdb first starts, it calls fetch_registers (among other | |
2215 | functions), which in turn calls init_pointers, which allocates | |
2216 | simulator memory. | |
2217 | ||
2218 | The problem is when we do that, we don't know whether we're | |
2ea716f6 | 2219 | debugging an H8/300 or H8/300H program. |
c906108c SS |
2220 | |
2221 | This is the first point at which we can make that determination, | |
2222 | so we just reallocate memory now; this will also allow us to handle | |
2ea716f6 | 2223 | switching between H8/300 and H8/300H programs without exiting |
c906108c | 2224 | gdb. */ |
a8cdafbd AV |
2225 | |
2226 | if (h8300smode) | |
2227 | memory_size = H8300S_MSIZE; | |
2228 | else if (h8300hmode) | |
c906108c SS |
2229 | memory_size = H8300H_MSIZE; |
2230 | else | |
2231 | memory_size = H8300_MSIZE; | |
2232 | ||
2233 | if (cpu.memory) | |
2234 | free (cpu.memory); | |
2235 | if (cpu.cache_idx) | |
2236 | free (cpu.cache_idx); | |
2237 | if (cpu.eightbit) | |
2238 | free (cpu.eightbit); | |
2239 | ||
2240 | cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size); | |
2241 | cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size); | |
2242 | cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256); | |
2243 | ||
2ea716f6 | 2244 | /* `msize' must be a power of two. */ |
c906108c SS |
2245 | if ((memory_size & (memory_size - 1)) != 0) |
2246 | abort (); | |
2247 | cpu.mask = memory_size - 1; | |
2248 | ||
2249 | if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd, | |
2250 | sim_kind == SIM_OPEN_DEBUG, | |
2251 | 0, sim_write) | |
2252 | == NULL) | |
2253 | { | |
2254 | /* Close the bfd if we opened it. */ | |
2255 | if (abfd == NULL && prog_bfd != NULL) | |
2256 | bfd_close (prog_bfd); | |
2257 | return SIM_RC_FAIL; | |
2258 | } | |
2259 | ||
2260 | /* Close the bfd if we opened it. */ | |
2261 | if (abfd == NULL && prog_bfd != NULL) | |
2262 | bfd_close (prog_bfd); | |
2263 | return SIM_RC_OK; | |
2264 | } | |
2265 | ||
2266 | SIM_RC | |
2267 | sim_create_inferior (sd, abfd, argv, env) | |
2268 | SIM_DESC sd; | |
2269 | struct _bfd *abfd; | |
2270 | char **argv; | |
2271 | char **env; | |
2272 | { | |
2273 | if (abfd != NULL) | |
2274 | cpu.pc = bfd_get_start_address (abfd); | |
2275 | else | |
2276 | cpu.pc = 0; | |
2277 | return SIM_RC_OK; | |
2278 | } | |
2279 | ||
2280 | void | |
2281 | sim_do_command (sd, cmd) | |
2282 | SIM_DESC sd; | |
2283 | char *cmd; | |
2284 | { | |
2285 | (*sim_callback->printf_filtered) (sim_callback, | |
2286 | "This simulator does not accept any commands.\n"); | |
2287 | } | |
2288 | ||
2289 | void | |
2290 | sim_set_callbacks (ptr) | |
2291 | struct host_callback_struct *ptr; | |
2292 | { | |
2293 | sim_callback = ptr; | |
2294 | } |