]>
Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* |
2 | * Simulator for the Hitachi H8/300 architecture. | |
3 | * | |
4 | * Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com | |
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" | |
35 | #include "callback.h" | |
36 | #include "remote-sim.h" | |
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 AV |
88 | #define BUILDEXR() \ |
89 | if( h8300smode ) cpu.exr = ( trace<<7 ) | intMask; | |
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 AV |
101 | #define GETEXR() \ |
102 | if( h8300smode ) { \ | |
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) \ | |
853 | case O(name, SB): \ | |
854 | { \ | |
855 | int t; \ | |
856 | int hm = 0x80; \ | |
857 | rd = GET_B_REG (code->src.reg); \ | |
858 | how; \ | |
859 | goto shift8; \ | |
860 | } \ | |
861 | case O(name, SW): \ | |
862 | { \ | |
863 | int t; \ | |
864 | int hm = 0x8000; \ | |
865 | rd = GET_W_REG (code->src.reg); \ | |
866 | how; \ | |
867 | goto shift16; \ | |
868 | } \ | |
869 | case O(name, SL): \ | |
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) \ | |
879 | case O(name, SB): \ | |
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 | } \ | |
894 | case O(name, SW): \ | |
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 | } \ | |
909 | case O(name, SL): \ | |
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) \ | |
926 | case O(name, SB): \ | |
927 | { \ | |
928 | int m; \ | |
929 | int b; \ | |
930 | if (f) ea = fetch (&code->dst); \ | |
931 | m=1<< fetch(&code->src); \ | |
932 | op; \ | |
933 | if(s) store (&code->dst,ea); goto next; \ | |
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 | ||
945 | void | |
946 | sim_resume (sd, step, siggnal) | |
947 | SIM_DESC sd; | |
948 | { | |
949 | static int init1; | |
950 | int cycles = 0; | |
951 | int insts = 0; | |
952 | int tick_start = get_now (); | |
953 | void (*prev) (); | |
954 | int poll_count = 0; | |
955 | int res; | |
956 | int tmp; | |
957 | int rd; | |
958 | int ea; | |
959 | int bit; | |
960 | int pc; | |
f6225c96 | 961 | int c, nz, v, n, u, h, ui, intMaskBit; |
fc974602 | 962 | int trace, intMask; |
c906108c SS |
963 | int oldmask; |
964 | init_pointers (); | |
965 | ||
966 | prev = signal (SIGINT, control_c); | |
967 | ||
968 | if (step) | |
969 | { | |
970 | cpu.state = SIM_STATE_STOPPED; | |
971 | cpu.exception = SIGTRAP; | |
972 | } | |
973 | else | |
974 | { | |
975 | cpu.state = SIM_STATE_RUNNING; | |
976 | cpu.exception = 0; | |
977 | } | |
978 | ||
979 | pc = cpu.pc; | |
980 | ||
981 | /* The PC should never be odd. */ | |
982 | if (pc & 0x1) | |
983 | abort (); | |
984 | ||
985 | GETSR (); | |
fc974602 AV |
986 | GETEXR (); |
987 | ||
c906108c SS |
988 | oldmask = cpu.mask; |
989 | if (!h8300hmode) | |
990 | cpu.mask = 0xffff; | |
991 | do | |
992 | { | |
993 | int cidx; | |
994 | decoded_inst *code; | |
995 | ||
996 | top: | |
997 | cidx = cpu.cache_idx[pc]; | |
998 | code = cpu.cache + cidx; | |
999 | ||
1000 | ||
1001 | #define ALUOP(STORE, NAME, HOW) \ | |
1002 | case O(NAME,SB): HOW; if(STORE)goto alu8;else goto just_flags_alu8; \ | |
1003 | case O(NAME, SW): HOW; if(STORE)goto alu16;else goto just_flags_alu16; \ | |
1004 | case O(NAME,SL): HOW; if(STORE)goto alu32;else goto just_flags_alu32; | |
1005 | ||
1006 | ||
1007 | #define LOGOP(NAME, HOW) \ | |
1008 | case O(NAME,SB): HOW; goto log8;\ | |
1009 | case O(NAME, SW): HOW; goto log16;\ | |
1010 | case O(NAME,SL): HOW; goto log32; | |
1011 | ||
1012 | ||
1013 | ||
1014 | #if ADEBUG | |
1015 | if (debug) | |
1016 | { | |
1017 | printf ("%x %d %s\n", pc, code->opcode, | |
1018 | code->op ? code->op->name : "**"); | |
1019 | } | |
1020 | cpu.stats[code->opcode]++; | |
1021 | ||
1022 | #endif | |
1023 | ||
3b02cf92 AV |
1024 | if (code->opcode) |
1025 | { | |
1026 | cycles += code->cycles; | |
1027 | insts++; | |
1028 | } | |
1029 | ||
c906108c SS |
1030 | switch (code->opcode) |
1031 | { | |
1032 | case 0: | |
1033 | /* | |
1034 | * This opcode is a fake for when we get to an | |
1035 | * instruction which hasnt been compiled | |
1036 | */ | |
1037 | compile (pc); | |
1038 | goto top; | |
1039 | break; | |
1040 | ||
1041 | ||
1042 | case O (O_SUBX, SB): | |
1043 | rd = fetch (&code->dst); | |
1044 | ea = fetch (&code->src); | |
1045 | ea = -(ea + C); | |
1046 | res = rd + ea; | |
1047 | goto alu8; | |
1048 | ||
1049 | case O (O_ADDX, SB): | |
1050 | rd = fetch (&code->dst); | |
1051 | ea = fetch (&code->src); | |
1052 | ea = C + ea; | |
1053 | res = rd + ea; | |
1054 | goto alu8; | |
1055 | ||
1056 | #define EA ea = fetch(&code->src); | |
1057 | #define RD_EA ea = fetch(&code->src); rd = fetch(&code->dst); | |
1058 | ||
1059 | ALUOP (1, O_SUB, RD_EA; | |
1060 | ea = -ea; | |
1061 | res = rd + ea); | |
1062 | ALUOP (1, O_NEG, EA; | |
1063 | ea = -ea; | |
1064 | rd = 0; | |
1065 | res = rd + ea); | |
1066 | ||
1067 | case O (O_ADD, SB): | |
1068 | rd = GET_B_REG (code->dst.reg); | |
1069 | ea = fetch (&code->src); | |
1070 | res = rd + ea; | |
1071 | goto alu8; | |
1072 | case O (O_ADD, SW): | |
1073 | rd = GET_W_REG (code->dst.reg); | |
1074 | ea = fetch (&code->src); | |
1075 | res = rd + ea; | |
1076 | goto alu16; | |
1077 | case O (O_ADD, SL): | |
1078 | rd = GET_L_REG (code->dst.reg); | |
1079 | ea = fetch (&code->src); | |
1080 | res = rd + ea; | |
1081 | goto alu32; | |
1082 | ||
1083 | ||
1084 | LOGOP (O_AND, RD_EA; | |
1085 | res = rd & ea); | |
1086 | ||
1087 | LOGOP (O_OR, RD_EA; | |
1088 | res = rd | ea); | |
1089 | ||
1090 | LOGOP (O_XOR, RD_EA; | |
1091 | res = rd ^ ea); | |
1092 | ||
1093 | ||
1094 | case O (O_MOV_TO_MEM, SB): | |
1095 | res = GET_B_REG (code->src.reg); | |
1096 | goto log8; | |
1097 | case O (O_MOV_TO_MEM, SW): | |
1098 | res = GET_W_REG (code->src.reg); | |
1099 | goto log16; | |
1100 | case O (O_MOV_TO_MEM, SL): | |
1101 | res = GET_L_REG (code->src.reg); | |
1102 | goto log32; | |
1103 | ||
1104 | ||
1105 | case O (O_MOV_TO_REG, SB): | |
1106 | res = fetch (&code->src); | |
1107 | SET_B_REG (code->dst.reg, res); | |
1108 | goto just_flags_log8; | |
1109 | case O (O_MOV_TO_REG, SW): | |
1110 | res = fetch (&code->src); | |
1111 | SET_W_REG (code->dst.reg, res); | |
1112 | goto just_flags_log16; | |
1113 | case O (O_MOV_TO_REG, SL): | |
1114 | res = fetch (&code->src); | |
1115 | SET_L_REG (code->dst.reg, res); | |
1116 | goto just_flags_log32; | |
1117 | ||
1118 | ||
1119 | case O (O_ADDS, SL): | |
1120 | SET_L_REG (code->dst.reg, | |
1121 | GET_L_REG (code->dst.reg) | |
1122 | + code->src.literal); | |
1123 | ||
1124 | goto next; | |
1125 | ||
1126 | case O (O_SUBS, SL): | |
1127 | SET_L_REG (code->dst.reg, | |
1128 | GET_L_REG (code->dst.reg) | |
1129 | - code->src.literal); | |
1130 | goto next; | |
1131 | ||
1132 | case O (O_CMP, SB): | |
1133 | rd = fetch (&code->dst); | |
1134 | ea = fetch (&code->src); | |
1135 | ea = -ea; | |
1136 | res = rd + ea; | |
1137 | goto just_flags_alu8; | |
1138 | ||
1139 | case O (O_CMP, SW): | |
1140 | rd = fetch (&code->dst); | |
1141 | ea = fetch (&code->src); | |
1142 | ea = -ea; | |
1143 | res = rd + ea; | |
1144 | goto just_flags_alu16; | |
1145 | ||
1146 | case O (O_CMP, SL): | |
1147 | rd = fetch (&code->dst); | |
1148 | ea = fetch (&code->src); | |
1149 | ea = -ea; | |
1150 | res = rd + ea; | |
1151 | goto just_flags_alu32; | |
1152 | ||
1153 | ||
1154 | case O (O_DEC, SB): | |
1155 | rd = GET_B_REG (code->src.reg); | |
1156 | ea = -1; | |
1157 | res = rd + ea; | |
1158 | SET_B_REG (code->src.reg, res); | |
1159 | goto just_flags_inc8; | |
1160 | ||
1161 | case O (O_DEC, SW): | |
1162 | rd = GET_W_REG (code->dst.reg); | |
1163 | ea = -code->src.literal; | |
1164 | res = rd + ea; | |
1165 | SET_W_REG (code->dst.reg, res); | |
1166 | goto just_flags_inc16; | |
1167 | ||
1168 | case O (O_DEC, SL): | |
1169 | rd = GET_L_REG (code->dst.reg); | |
1170 | ea = -code->src.literal; | |
1171 | res = rd + ea; | |
1172 | SET_L_REG (code->dst.reg, res); | |
1173 | goto just_flags_inc32; | |
1174 | ||
1175 | ||
1176 | case O (O_INC, SB): | |
1177 | rd = GET_B_REG (code->src.reg); | |
1178 | ea = 1; | |
1179 | res = rd + ea; | |
1180 | SET_B_REG (code->src.reg, res); | |
1181 | goto just_flags_inc8; | |
1182 | ||
1183 | case O (O_INC, SW): | |
1184 | rd = GET_W_REG (code->dst.reg); | |
1185 | ea = code->src.literal; | |
1186 | res = rd + ea; | |
1187 | SET_W_REG (code->dst.reg, res); | |
1188 | goto just_flags_inc16; | |
1189 | ||
1190 | case O (O_INC, SL): | |
1191 | rd = GET_L_REG (code->dst.reg); | |
1192 | ea = code->src.literal; | |
1193 | res = rd + ea; | |
1194 | SET_L_REG (code->dst.reg, res); | |
1195 | goto just_flags_inc32; | |
1196 | ||
1197 | ||
1198 | #define GET_CCR(x) BUILDSR();x = cpu.ccr | |
fc974602 | 1199 | #define GET_EXR(x) BUILDEXR();x = cpu.exr |
c906108c SS |
1200 | |
1201 | case O (O_ANDC, SB): | |
fc974602 AV |
1202 | if(code->dst.type==OP_CCR) |
1203 | { | |
1204 | GET_CCR (rd); | |
1205 | } | |
1206 | else if(code->dst.type==OP_EXR && h8300smode) | |
1207 | { | |
1208 | GET_EXR (rd); | |
1209 | } | |
1210 | else | |
1211 | goto illegal; | |
c906108c SS |
1212 | ea = code->src.literal; |
1213 | res = rd & ea; | |
1214 | goto setc; | |
1215 | ||
1216 | case O (O_ORC, SB): | |
fc974602 AV |
1217 | if(code->dst.type==OP_CCR) |
1218 | { | |
1219 | GET_CCR (rd); | |
1220 | } | |
1221 | else if(code->dst.type==OP_EXR && h8300smode) | |
1222 | { | |
1223 | GET_EXR (rd); | |
1224 | } | |
1225 | else | |
1226 | goto illegal; | |
c906108c SS |
1227 | ea = code->src.literal; |
1228 | res = rd | ea; | |
1229 | goto setc; | |
1230 | ||
1231 | case O (O_XORC, SB): | |
fc974602 AV |
1232 | if(code->dst.type==OP_CCR) |
1233 | { | |
1234 | GET_CCR (rd); | |
1235 | } | |
1236 | else if(code->dst.type==OP_EXR && h8300smode) | |
1237 | { | |
1238 | GET_EXR (rd); | |
1239 | } | |
1240 | else | |
1241 | goto illegal; | |
c906108c SS |
1242 | ea = code->src.literal; |
1243 | res = rd ^ ea; | |
1244 | goto setc; | |
1245 | ||
1246 | ||
1247 | case O (O_BRA, SB): | |
1248 | if (1) | |
1249 | goto condtrue; | |
1250 | goto next; | |
1251 | ||
1252 | case O (O_BRN, SB): | |
1253 | if (0) | |
1254 | goto condtrue; | |
1255 | goto next; | |
1256 | ||
1257 | case O (O_BHI, SB): | |
1258 | if ((C || Z) == 0) | |
1259 | goto condtrue; | |
1260 | goto next; | |
1261 | ||
1262 | ||
1263 | case O (O_BLS, SB): | |
1264 | if ((C || Z)) | |
1265 | goto condtrue; | |
1266 | goto next; | |
1267 | ||
1268 | case O (O_BCS, SB): | |
1269 | if ((C == 1)) | |
1270 | goto condtrue; | |
1271 | goto next; | |
1272 | ||
1273 | case O (O_BCC, SB): | |
1274 | if ((C == 0)) | |
1275 | goto condtrue; | |
1276 | goto next; | |
1277 | ||
1278 | case O (O_BEQ, SB): | |
1279 | if (Z) | |
1280 | goto condtrue; | |
1281 | goto next; | |
1282 | case O (O_BGT, SB): | |
1283 | if (((Z || (N ^ V)) == 0)) | |
1284 | goto condtrue; | |
1285 | goto next; | |
1286 | ||
1287 | ||
1288 | case O (O_BLE, SB): | |
1289 | if (((Z || (N ^ V)) == 1)) | |
1290 | goto condtrue; | |
1291 | goto next; | |
1292 | ||
1293 | case O (O_BGE, SB): | |
1294 | if ((N ^ V) == 0) | |
1295 | goto condtrue; | |
1296 | goto next; | |
1297 | case O (O_BLT, SB): | |
1298 | if ((N ^ V)) | |
1299 | goto condtrue; | |
1300 | goto next; | |
1301 | case O (O_BMI, SB): | |
1302 | if ((N)) | |
1303 | goto condtrue; | |
1304 | goto next; | |
1305 | case O (O_BNE, SB): | |
1306 | if ((Z == 0)) | |
1307 | goto condtrue; | |
1308 | goto next; | |
1309 | ||
1310 | case O (O_BPL, SB): | |
1311 | if (N == 0) | |
1312 | goto condtrue; | |
1313 | goto next; | |
1314 | case O (O_BVC, SB): | |
1315 | if ((V == 0)) | |
1316 | goto condtrue; | |
1317 | goto next; | |
1318 | case O (O_BVS, SB): | |
1319 | if ((V == 1)) | |
1320 | goto condtrue; | |
1321 | goto next; | |
1322 | ||
1323 | case O (O_SYSCALL, SB): | |
1324 | { | |
1325 | char c = cpu.regs[2]; | |
1326 | sim_callback->write_stdout (sim_callback, &c, 1); | |
1327 | } | |
1328 | goto next; | |
1329 | ||
1330 | ONOT (O_NOT, rd = ~rd; v = 0;); | |
1331 | OSHIFTS (O_SHLL, | |
1332 | c = rd & hm; v = 0; rd <<= 1, | |
1333 | c = rd & (hm >> 1); v = 0; rd <<= 2); | |
1334 | OSHIFTS (O_SHLR, | |
1335 | c = rd & 1; v = 0; rd = (unsigned int) rd >> 1, | |
1336 | c = rd & 2; v = 0; rd = (unsigned int) rd >> 2); | |
1337 | OSHIFTS (O_SHAL, | |
1338 | c = rd & hm; v = (rd & hm) != ((rd & (hm >> 1)) << 1); rd <<= 1, | |
1339 | c = rd & (hm >> 1); v = (rd & (hm >> 1)) != ((rd & (hm >> 2)) << 2); rd <<= 2); | |
1340 | OSHIFTS (O_SHAR, | |
1341 | t = rd & hm; c = rd & 1; v = 0; rd >>= 1; rd |= t, | |
1342 | t = rd & hm; c = rd & 2; v = 0; rd >>= 2; rd |= t | t >> 1 ); | |
1343 | OSHIFTS (O_ROTL, | |
1344 | c = rd & hm; v = 0; rd <<= 1; rd |= C, | |
1345 | c = rd & hm; v = 0; rd <<= 1; rd |= C; c = rd & hm; rd <<= 1; rd |= C); | |
1346 | OSHIFTS (O_ROTR, | |
1347 | c = rd & 1; v = 0; rd = (unsigned int) rd >> 1; if (c) rd |= hm, | |
1348 | 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); | |
1349 | OSHIFTS (O_ROTXL, | |
1350 | t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0, | |
1351 | t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0; t = rd & hm; rd <<= 1; rd |= C; c = t); | |
1352 | OSHIFTS (O_ROTXR, | |
1353 | t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t; v = 0, | |
1354 | 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); | |
1355 | ||
1356 | case O (O_JMP, SB): | |
1357 | { | |
1358 | pc = fetch (&code->src); | |
1359 | goto end; | |
1360 | ||
1361 | } | |
1362 | ||
1363 | case O (O_JSR, SB): | |
1364 | { | |
1365 | int tmp; | |
1366 | pc = fetch (&code->src); | |
1367 | call: | |
1368 | tmp = cpu.regs[7]; | |
1369 | ||
1370 | if (h8300hmode) | |
1371 | { | |
1372 | tmp -= 4; | |
1373 | SET_MEMORY_L (tmp, code->next_pc); | |
1374 | } | |
1375 | else | |
1376 | { | |
1377 | tmp -= 2; | |
1378 | SET_MEMORY_W (tmp, code->next_pc); | |
1379 | } | |
1380 | cpu.regs[7] = tmp; | |
1381 | ||
1382 | goto end; | |
1383 | } | |
1384 | case O (O_BSR, SB): | |
1385 | pc = code->src.literal; | |
1386 | goto call; | |
1387 | ||
1388 | case O (O_RTS, SN): | |
1389 | { | |
1390 | int tmp; | |
1391 | ||
1392 | tmp = cpu.regs[7]; | |
1393 | ||
1394 | if (h8300hmode) | |
1395 | { | |
1396 | pc = GET_MEMORY_L (tmp); | |
1397 | tmp += 4; | |
1398 | } | |
1399 | else | |
1400 | { | |
1401 | pc = GET_MEMORY_W (tmp); | |
1402 | tmp += 2; | |
1403 | } | |
1404 | ||
1405 | cpu.regs[7] = tmp; | |
1406 | goto end; | |
1407 | } | |
1408 | ||
1409 | case O (O_ILL, SB): | |
1410 | cpu.state = SIM_STATE_STOPPED; | |
1411 | cpu.exception = SIGILL; | |
1412 | goto end; | |
1413 | case O (O_SLEEP, SN): | |
c906108c SS |
1414 | /* FIXME: Doesn't this break for breakpoints when r0 |
1415 | contains just the right (er, wrong) value? */ | |
1416 | cpu.state = SIM_STATE_STOPPED; | |
97ee9e5a FCE |
1417 | /* The format of r0 is defined by target newlib. Expand |
1418 | the macros here instead of looking for .../sys/wait.h. */ | |
1419 | #define SIM_WIFEXITED(v) (((v) & 0xff) == 0) | |
1420 | #define SIM_WIFSIGNALED(v) (((v) & 0x7f) > 0 && (((v) & 0x7f) < 0x7f)) | |
1421 | if (! SIM_WIFEXITED (cpu.regs[0]) && SIM_WIFSIGNALED (cpu.regs[0])) | |
c906108c SS |
1422 | cpu.exception = SIGILL; |
1423 | else | |
1424 | cpu.exception = SIGTRAP; | |
c906108c SS |
1425 | goto end; |
1426 | case O (O_BPT, SN): | |
1427 | cpu.state = SIM_STATE_STOPPED; | |
1428 | cpu.exception = SIGTRAP; | |
1429 | goto end; | |
1430 | ||
1431 | OBITOP (O_BNOT, 1, 1, ea ^= m); | |
1432 | OBITOP (O_BTST, 1, 0, nz = ea & m); | |
1433 | OBITOP (O_BCLR, 1, 1, ea &= ~m); | |
1434 | OBITOP (O_BSET, 1, 1, ea |= m); | |
1435 | OBITOP (O_BLD, 1, 0, c = ea & m); | |
1436 | OBITOP (O_BILD, 1, 0, c = !(ea & m)); | |
1437 | OBITOP (O_BST, 1, 1, ea &= ~m; | |
1438 | if (C) ea |= m); | |
1439 | OBITOP (O_BIST, 1, 1, ea &= ~m; | |
1440 | if (!C) ea |= m); | |
1441 | OBITOP (O_BAND, 1, 0, c = (ea & m) && C); | |
1442 | OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C); | |
1443 | OBITOP (O_BOR, 1, 0, c = (ea & m) || C); | |
1444 | OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C); | |
1445 | OBITOP (O_BXOR, 1, 0, c = (ea & m) != C); | |
1446 | OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C); | |
1447 | ||
de9b1892 KH |
1448 | #define MOP(bsize, signed) \ |
1449 | mop (code, bsize, signed); \ | |
1450 | goto next; | |
c906108c SS |
1451 | |
1452 | case O (O_MULS, SB): | |
1453 | MOP (1, 1); | |
1454 | break; | |
1455 | case O (O_MULS, SW): | |
1456 | MOP (0, 1); | |
1457 | break; | |
1458 | case O (O_MULU, SB): | |
1459 | MOP (1, 0); | |
1460 | break; | |
1461 | case O (O_MULU, SW): | |
1462 | MOP (0, 0); | |
1463 | break; | |
1464 | ||
1465 | ||
1466 | case O (O_DIVU, SB): | |
1467 | { | |
1468 | rd = GET_W_REG (code->dst.reg); | |
1469 | ea = GET_B_REG (code->src.reg); | |
1470 | if (ea) | |
1471 | { | |
de9b1892 KH |
1472 | tmp = (unsigned) rd % ea; |
1473 | rd = (unsigned) rd / ea; | |
c906108c SS |
1474 | } |
1475 | SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8)); | |
1476 | n = ea & 0x80; | |
1477 | nz = ea & 0xff; | |
1478 | ||
1479 | goto next; | |
1480 | } | |
1481 | case O (O_DIVU, SW): | |
1482 | { | |
1483 | rd = GET_L_REG (code->dst.reg); | |
1484 | ea = GET_W_REG (code->src.reg); | |
1485 | n = ea & 0x8000; | |
1486 | nz = ea & 0xffff; | |
1487 | if (ea) | |
1488 | { | |
de9b1892 KH |
1489 | tmp = (unsigned) rd % ea; |
1490 | rd = (unsigned) rd / ea; | |
c906108c SS |
1491 | } |
1492 | SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16)); | |
1493 | goto next; | |
1494 | } | |
1495 | ||
1496 | case O (O_DIVS, SB): | |
1497 | { | |
1498 | ||
1499 | rd = SEXTSHORT (GET_W_REG (code->dst.reg)); | |
1500 | ea = SEXTCHAR (GET_B_REG (code->src.reg)); | |
1501 | if (ea) | |
1502 | { | |
1503 | tmp = (int) rd % (int) ea; | |
1504 | rd = (int) rd / (int) ea; | |
1505 | n = rd & 0x8000; | |
1506 | nz = 1; | |
1507 | } | |
1508 | else | |
1509 | nz = 0; | |
1510 | SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8)); | |
1511 | goto next; | |
1512 | } | |
1513 | case O (O_DIVS, SW): | |
1514 | { | |
1515 | rd = GET_L_REG (code->dst.reg); | |
1516 | ea = SEXTSHORT (GET_W_REG (code->src.reg)); | |
1517 | if (ea) | |
1518 | { | |
1519 | tmp = (int) rd % (int) ea; | |
1520 | rd = (int) rd / (int) ea; | |
1521 | n = rd & 0x80000000; | |
1522 | nz = 1; | |
1523 | } | |
1524 | else | |
1525 | nz = 0; | |
1526 | SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16)); | |
1527 | goto next; | |
1528 | } | |
1529 | case O (O_EXTS, SW): | |
1530 | rd = GET_B_REG (code->src.reg + 8) & 0xff; /* Yes, src, not dst. */ | |
1531 | ea = rd & 0x80 ? -256 : 0; | |
1532 | res = rd + ea; | |
1533 | goto log16; | |
1534 | case O (O_EXTS, SL): | |
1535 | rd = GET_W_REG (code->src.reg) & 0xffff; | |
1536 | ea = rd & 0x8000 ? -65536 : 0; | |
1537 | res = rd + ea; | |
1538 | goto log32; | |
1539 | case O (O_EXTU, SW): | |
1540 | rd = GET_B_REG (code->src.reg + 8) & 0xff; | |
1541 | ea = 0; | |
1542 | res = rd + ea; | |
1543 | goto log16; | |
1544 | case O (O_EXTU, SL): | |
1545 | rd = GET_W_REG (code->src.reg) & 0xffff; | |
1546 | ea = 0; | |
1547 | res = rd + ea; | |
1548 | goto log32; | |
1549 | ||
1550 | case O (O_NOP, SN): | |
1551 | goto next; | |
1552 | ||
1553 | case O (O_STM, SL): | |
1554 | { | |
1555 | int nregs, firstreg, i; | |
1556 | ||
1557 | nregs = GET_MEMORY_B (pc + 1); | |
1558 | nregs >>= 4; | |
1559 | nregs &= 0xf; | |
1560 | firstreg = GET_MEMORY_B (pc + 3); | |
1561 | firstreg &= 0xf; | |
1562 | for (i = firstreg; i <= firstreg + nregs; i++) | |
1563 | { | |
1564 | cpu.regs[7] -= 4; | |
1565 | SET_MEMORY_L (cpu.regs[7], cpu.regs[i]); | |
1566 | } | |
1567 | } | |
1568 | goto next; | |
1569 | ||
1570 | case O (O_LDM, SL): | |
1571 | { | |
1572 | int nregs, firstreg, i; | |
1573 | ||
1574 | nregs = GET_MEMORY_B (pc + 1); | |
1575 | nregs >>= 4; | |
1576 | nregs &= 0xf; | |
1577 | firstreg = GET_MEMORY_B (pc + 3); | |
1578 | firstreg &= 0xf; | |
1579 | for (i = firstreg; i >= firstreg - nregs; i--) | |
1580 | { | |
1581 | cpu.regs[i] = GET_MEMORY_L (cpu.regs[7]); | |
1582 | cpu.regs[7] += 4; | |
1583 | } | |
1584 | } | |
1585 | goto next; | |
1586 | ||
1587 | default: | |
fc974602 | 1588 | illegal: |
c906108c SS |
1589 | cpu.state = SIM_STATE_STOPPED; |
1590 | cpu.exception = SIGILL; | |
1591 | goto end; | |
1592 | ||
1593 | } | |
1594 | abort (); | |
1595 | ||
1596 | setc: | |
fc974602 AV |
1597 | if(code->dst.type==OP_CCR) |
1598 | { | |
1599 | cpu.ccr = res; | |
1600 | GETSR (); | |
1601 | } | |
1602 | else if(code->dst.type==OP_EXR && h8300smode) | |
1603 | { | |
1604 | cpu.exr = res; | |
1605 | GETEXR (); | |
1606 | } | |
1607 | else | |
1608 | goto illegal; | |
1609 | ||
c906108c SS |
1610 | goto next; |
1611 | ||
1612 | condtrue: | |
1613 | /* When a branch works */ | |
1614 | pc = code->src.literal; | |
1615 | goto end; | |
1616 | ||
1617 | /* Set the cond codes from res */ | |
1618 | bitop: | |
1619 | ||
1620 | /* Set the flags after an 8 bit inc/dec operation */ | |
1621 | just_flags_inc8: | |
1622 | n = res & 0x80; | |
1623 | nz = res & 0xff; | |
1624 | v = (rd & 0x7f) == 0x7f; | |
1625 | goto next; | |
1626 | ||
1627 | ||
1628 | /* Set the flags after an 16 bit inc/dec operation */ | |
1629 | just_flags_inc16: | |
1630 | n = res & 0x8000; | |
1631 | nz = res & 0xffff; | |
1632 | v = (rd & 0x7fff) == 0x7fff; | |
1633 | goto next; | |
1634 | ||
1635 | ||
1636 | /* Set the flags after an 32 bit inc/dec operation */ | |
1637 | just_flags_inc32: | |
1638 | n = res & 0x80000000; | |
1639 | nz = res & 0xffffffff; | |
1640 | v = (rd & 0x7fffffff) == 0x7fffffff; | |
1641 | goto next; | |
1642 | ||
1643 | ||
1644 | shift8: | |
1645 | /* Set flags after an 8 bit shift op, carry,overflow set in insn */ | |
1646 | n = (rd & 0x80); | |
1647 | nz = rd & 0xff; | |
1648 | SET_B_REG (code->src.reg, rd); | |
1649 | goto next; | |
1650 | ||
1651 | shift16: | |
1652 | /* Set flags after an 16 bit shift op, carry,overflow set in insn */ | |
1653 | n = (rd & 0x8000); | |
1654 | nz = rd & 0xffff; | |
1655 | SET_W_REG (code->src.reg, rd); | |
1656 | goto next; | |
1657 | ||
1658 | shift32: | |
1659 | /* Set flags after an 32 bit shift op, carry,overflow set in insn */ | |
1660 | n = (rd & 0x80000000); | |
1661 | nz = rd & 0xffffffff; | |
1662 | SET_L_REG (code->src.reg, rd); | |
1663 | goto next; | |
1664 | ||
1665 | log32: | |
1666 | store (&code->dst, res); | |
1667 | just_flags_log32: | |
1668 | /* flags after a 32bit logical operation */ | |
1669 | n = res & 0x80000000; | |
1670 | nz = res & 0xffffffff; | |
1671 | v = 0; | |
1672 | goto next; | |
1673 | ||
1674 | log16: | |
1675 | store (&code->dst, res); | |
1676 | just_flags_log16: | |
1677 | /* flags after a 16bit logical operation */ | |
1678 | n = res & 0x8000; | |
1679 | nz = res & 0xffff; | |
1680 | v = 0; | |
1681 | goto next; | |
1682 | ||
1683 | ||
1684 | log8: | |
1685 | store (&code->dst, res); | |
1686 | just_flags_log8: | |
1687 | n = res & 0x80; | |
1688 | nz = res & 0xff; | |
1689 | v = 0; | |
1690 | goto next; | |
1691 | ||
1692 | alu8: | |
1693 | SET_B_REG (code->dst.reg, res); | |
1694 | just_flags_alu8: | |
1695 | n = res & 0x80; | |
1696 | nz = res & 0xff; | |
1697 | c = (res & 0x100); | |
1698 | switch (code->opcode / 4) | |
1699 | { | |
1700 | case O_ADD: | |
1701 | v = ((rd & 0x80) == (ea & 0x80) | |
1702 | && (rd & 0x80) != (res & 0x80)); | |
1703 | break; | |
1704 | case O_SUB: | |
1705 | case O_CMP: | |
1706 | v = ((rd & 0x80) != (-ea & 0x80) | |
1707 | && (rd & 0x80) != (res & 0x80)); | |
1708 | break; | |
1709 | case O_NEG: | |
1710 | v = (rd == 0x80); | |
1711 | break; | |
1712 | } | |
1713 | goto next; | |
1714 | ||
1715 | alu16: | |
1716 | SET_W_REG (code->dst.reg, res); | |
1717 | just_flags_alu16: | |
1718 | n = res & 0x8000; | |
1719 | nz = res & 0xffff; | |
1720 | c = (res & 0x10000); | |
1721 | switch (code->opcode / 4) | |
1722 | { | |
1723 | case O_ADD: | |
1724 | v = ((rd & 0x8000) == (ea & 0x8000) | |
1725 | && (rd & 0x8000) != (res & 0x8000)); | |
1726 | break; | |
1727 | case O_SUB: | |
1728 | case O_CMP: | |
1729 | v = ((rd & 0x8000) != (-ea & 0x8000) | |
1730 | && (rd & 0x8000) != (res & 0x8000)); | |
1731 | break; | |
1732 | case O_NEG: | |
1733 | v = (rd == 0x8000); | |
1734 | break; | |
1735 | } | |
1736 | goto next; | |
1737 | ||
1738 | alu32: | |
1739 | SET_L_REG (code->dst.reg, res); | |
1740 | just_flags_alu32: | |
1741 | n = res & 0x80000000; | |
1742 | nz = res & 0xffffffff; | |
1743 | switch (code->opcode / 4) | |
1744 | { | |
1745 | case O_ADD: | |
1746 | v = ((rd & 0x80000000) == (ea & 0x80000000) | |
1747 | && (rd & 0x80000000) != (res & 0x80000000)); | |
1748 | c = ((unsigned) res < (unsigned) rd) || ((unsigned) res < (unsigned) ea); | |
1749 | break; | |
1750 | case O_SUB: | |
1751 | case O_CMP: | |
1752 | v = ((rd & 0x80000000) != (-ea & 0x80000000) | |
1753 | && (rd & 0x80000000) != (res & 0x80000000)); | |
1754 | c = (unsigned) rd < (unsigned) -ea; | |
1755 | break; | |
1756 | case O_NEG: | |
1757 | v = (rd == 0x80000000); | |
1758 | c = res != 0; | |
1759 | break; | |
1760 | } | |
1761 | goto next; | |
1762 | ||
1763 | next:; | |
1764 | pc = code->next_pc; | |
1765 | ||
1766 | end: | |
1767 | ; | |
de9b1892 KH |
1768 | #if 0 |
1769 | if (cpu.regs[8]) | |
1770 | abort (); | |
1771 | #endif | |
c906108c SS |
1772 | |
1773 | if (--poll_count < 0) | |
1774 | { | |
7a292a7a | 1775 | poll_count = POLL_QUIT_INTERVAL; |
c906108c SS |
1776 | if ((*sim_callback->poll_quit) != NULL |
1777 | && (*sim_callback->poll_quit) (sim_callback)) | |
1778 | sim_stop (sd); | |
1779 | } | |
1780 | ||
1781 | } | |
1782 | while (cpu.state == SIM_STATE_RUNNING); | |
1783 | cpu.ticks += get_now () - tick_start; | |
1784 | cpu.cycles += cycles; | |
1785 | cpu.insts += insts; | |
de9b1892 | 1786 | |
c906108c SS |
1787 | cpu.pc = pc; |
1788 | BUILDSR (); | |
fc974602 | 1789 | BUILDEXR(); |
c906108c SS |
1790 | cpu.mask = oldmask; |
1791 | signal (SIGINT, prev); | |
1792 | } | |
1793 | ||
1794 | int | |
1795 | sim_trace (sd) | |
1796 | SIM_DESC sd; | |
1797 | { | |
2ea716f6 | 1798 | /* FIXME: Unfinished. */ |
c906108c SS |
1799 | abort (); |
1800 | } | |
1801 | ||
1802 | int | |
1803 | sim_write (sd, addr, buffer, size) | |
1804 | SIM_DESC sd; | |
1805 | SIM_ADDR addr; | |
1806 | unsigned char *buffer; | |
1807 | int size; | |
1808 | { | |
1809 | int i; | |
1810 | ||
1811 | init_pointers (); | |
1812 | if (addr < 0) | |
1813 | return 0; | |
1814 | for (i = 0; i < size; i++) | |
1815 | { | |
1816 | if (addr < memory_size) | |
1817 | { | |
1818 | cpu.memory[addr + i] = buffer[i]; | |
1819 | cpu.cache_idx[addr + i] = 0; | |
1820 | } | |
1821 | else | |
1822 | cpu.eightbit[(addr + i) & 0xff] = buffer[i]; | |
1823 | } | |
1824 | return size; | |
1825 | } | |
1826 | ||
1827 | int | |
1828 | sim_read (sd, addr, buffer, size) | |
1829 | SIM_DESC sd; | |
1830 | SIM_ADDR addr; | |
1831 | unsigned char *buffer; | |
1832 | int size; | |
1833 | { | |
1834 | init_pointers (); | |
1835 | if (addr < 0) | |
1836 | return 0; | |
1837 | if (addr < memory_size) | |
1838 | memcpy (buffer, cpu.memory + addr, size); | |
1839 | else | |
1840 | memcpy (buffer, cpu.eightbit + (addr & 0xff), size); | |
1841 | return size; | |
1842 | } | |
1843 | ||
1844 | ||
1845 | #define R0_REGNUM 0 | |
1846 | #define R1_REGNUM 1 | |
1847 | #define R2_REGNUM 2 | |
1848 | #define R3_REGNUM 3 | |
1849 | #define R4_REGNUM 4 | |
1850 | #define R5_REGNUM 5 | |
1851 | #define R6_REGNUM 6 | |
1852 | #define R7_REGNUM 7 | |
1853 | ||
1854 | #define SP_REGNUM R7_REGNUM /* Contains address of top of stack */ | |
1855 | #define FP_REGNUM R6_REGNUM /* Contains address of executing | |
2ea716f6 | 1856 | * stack frame */ |
c906108c SS |
1857 | |
1858 | #define CCR_REGNUM 8 /* Contains processor status */ | |
1859 | #define PC_REGNUM 9 /* Contains program counter */ | |
1860 | ||
1861 | #define CYCLE_REGNUM 10 | |
fc974602 | 1862 | #define EXR_REGNUM 11 /* Contains extended processor status */ |
c906108c SS |
1863 | #define INST_REGNUM 11 |
1864 | #define TICK_REGNUM 12 | |
1865 | ||
1866 | ||
1867 | int | |
1868 | sim_store_register (sd, rn, value, length) | |
1869 | SIM_DESC sd; | |
1870 | int rn; | |
1871 | unsigned char *value; | |
1872 | int length; | |
1873 | { | |
1874 | int longval; | |
1875 | int shortval; | |
1876 | int intval; | |
1877 | longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3]; | |
1878 | shortval = (value[0] << 8) | (value[1]); | |
1879 | intval = h8300hmode ? longval : shortval; | |
1880 | ||
1881 | init_pointers (); | |
1882 | switch (rn) | |
1883 | { | |
1884 | case PC_REGNUM: | |
1885 | cpu.pc = intval; | |
1886 | break; | |
1887 | default: | |
1888 | abort (); | |
1889 | case R0_REGNUM: | |
1890 | case R1_REGNUM: | |
1891 | case R2_REGNUM: | |
1892 | case R3_REGNUM: | |
1893 | case R4_REGNUM: | |
1894 | case R5_REGNUM: | |
1895 | case R6_REGNUM: | |
1896 | case R7_REGNUM: | |
1897 | cpu.regs[rn] = intval; | |
1898 | break; | |
1899 | case CCR_REGNUM: | |
1900 | cpu.ccr = intval; | |
1901 | break; | |
fc974602 AV |
1902 | case EXR_REGNUM: |
1903 | cpu.exr = intval; | |
1904 | break; | |
c906108c SS |
1905 | case CYCLE_REGNUM: |
1906 | cpu.cycles = longval; | |
1907 | break; | |
1908 | ||
1909 | case INST_REGNUM: | |
1910 | cpu.insts = longval; | |
1911 | break; | |
1912 | ||
1913 | case TICK_REGNUM: | |
1914 | cpu.ticks = longval; | |
1915 | break; | |
1916 | } | |
1917 | return -1; | |
1918 | } | |
1919 | ||
1920 | int | |
1921 | sim_fetch_register (sd, rn, buf, length) | |
1922 | SIM_DESC sd; | |
1923 | int rn; | |
1924 | unsigned char *buf; | |
1925 | int length; | |
1926 | { | |
1927 | int v; | |
1928 | int longreg = 0; | |
1929 | ||
1930 | init_pointers (); | |
1931 | ||
fc974602 AV |
1932 | if(!h8300smode && rn >=EXR_REGNUM) |
1933 | rn++; | |
c906108c SS |
1934 | switch (rn) |
1935 | { | |
1936 | default: | |
1937 | abort (); | |
3b02cf92 | 1938 | case CCR_REGNUM: |
c906108c SS |
1939 | v = cpu.ccr; |
1940 | break; | |
fc974602 AV |
1941 | case EXR_REGNUM: |
1942 | v = cpu.exr; | |
1943 | break; | |
3b02cf92 | 1944 | case PC_REGNUM: |
c906108c SS |
1945 | v = cpu.pc; |
1946 | break; | |
1947 | case R0_REGNUM: | |
1948 | case R1_REGNUM: | |
1949 | case R2_REGNUM: | |
1950 | case R3_REGNUM: | |
1951 | case R4_REGNUM: | |
1952 | case R5_REGNUM: | |
1953 | case R6_REGNUM: | |
1954 | case R7_REGNUM: | |
1955 | v = cpu.regs[rn]; | |
1956 | break; | |
3b02cf92 | 1957 | case CYCLE_REGNUM: |
c906108c SS |
1958 | v = cpu.cycles; |
1959 | longreg = 1; | |
1960 | break; | |
3b02cf92 | 1961 | case TICK_REGNUM: |
c906108c SS |
1962 | v = cpu.ticks; |
1963 | longreg = 1; | |
1964 | break; | |
3b02cf92 | 1965 | case INST_REGNUM: |
c906108c SS |
1966 | v = cpu.insts; |
1967 | longreg = 1; | |
1968 | break; | |
1969 | } | |
1970 | if (h8300hmode || longreg) | |
1971 | { | |
1972 | buf[0] = v >> 24; | |
1973 | buf[1] = v >> 16; | |
1974 | buf[2] = v >> 8; | |
1975 | buf[3] = v >> 0; | |
1976 | } | |
1977 | else | |
1978 | { | |
1979 | buf[0] = v >> 8; | |
1980 | buf[1] = v; | |
1981 | } | |
1982 | return -1; | |
1983 | } | |
1984 | ||
1985 | void | |
1986 | sim_stop_reason (sd, reason, sigrc) | |
1987 | SIM_DESC sd; | |
1988 | enum sim_stop *reason; | |
1989 | int *sigrc; | |
1990 | { | |
1991 | #if 0 /* FIXME: This should work but we can't use it. | |
1992 | grep for SLEEP above. */ | |
1993 | switch (cpu.state) | |
1994 | { | |
1995 | case SIM_STATE_EXITED : *reason = sim_exited; break; | |
1996 | case SIM_STATE_SIGNALLED : *reason = sim_signalled; break; | |
1997 | case SIM_STATE_STOPPED : *reason = sim_stopped; break; | |
1998 | default : abort (); | |
1999 | } | |
2000 | #else | |
2001 | *reason = sim_stopped; | |
2002 | #endif | |
2003 | *sigrc = cpu.exception; | |
2004 | } | |
2005 | ||
2006 | /* FIXME: Rename to sim_set_mem_size. */ | |
2007 | ||
2008 | void | |
2009 | sim_size (n) | |
2010 | int n; | |
2011 | { | |
2012 | /* Memory size is fixed. */ | |
2013 | } | |
2014 | ||
2015 | void | |
2016 | sim_set_simcache_size (n) | |
2017 | { | |
2018 | if (cpu.cache) | |
2019 | free (cpu.cache); | |
2020 | if (n < 2) | |
2021 | n = 2; | |
2022 | cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n); | |
2023 | memset (cpu.cache, 0, sizeof (decoded_inst) * n); | |
2024 | cpu.csize = n; | |
2025 | } | |
2026 | ||
2027 | ||
2028 | void | |
2029 | sim_info (sd, verbose) | |
2030 | SIM_DESC sd; | |
2031 | int verbose; | |
2032 | { | |
2033 | double timetaken = (double) cpu.ticks / (double) now_persec (); | |
2034 | double virttime = cpu.cycles / 10.0e6; | |
2035 | ||
2036 | (*sim_callback->printf_filtered) (sim_callback, | |
2037 | "\n\n#instructions executed %10d\n", | |
2038 | cpu.insts); | |
2039 | (*sim_callback->printf_filtered) (sim_callback, | |
2040 | "#cycles (v approximate) %10d\n", | |
2041 | cpu.cycles); | |
2042 | (*sim_callback->printf_filtered) (sim_callback, | |
2043 | "#real time taken %10.4f\n", | |
2044 | timetaken); | |
2045 | (*sim_callback->printf_filtered) (sim_callback, | |
2046 | "#virtual time taked %10.4f\n", | |
2047 | virttime); | |
2048 | if (timetaken != 0.0) | |
2049 | (*sim_callback->printf_filtered) (sim_callback, | |
2050 | "#simulation ratio %10.4f\n", | |
2051 | virttime / timetaken); | |
2052 | (*sim_callback->printf_filtered) (sim_callback, | |
2053 | "#compiles %10d\n", | |
2054 | cpu.compiles); | |
2055 | (*sim_callback->printf_filtered) (sim_callback, | |
2056 | "#cache size %10d\n", | |
2057 | cpu.csize); | |
2058 | ||
2059 | #ifdef ADEBUG | |
2060 | /* This to be conditional on `what' (aka `verbose'), | |
2061 | however it was never passed as non-zero. */ | |
2062 | if (1) | |
2063 | { | |
2064 | int i; | |
2065 | for (i = 0; i < O_LAST; i++) | |
2066 | { | |
2067 | if (cpu.stats[i]) | |
2068 | (*sim_callback->printf_filtered) (sim_callback, | |
2069 | "%d: %d\n", i, cpu.stats[i]); | |
2070 | } | |
2071 | } | |
2072 | #endif | |
2073 | } | |
2074 | ||
2ea716f6 KH |
2075 | /* Indicate whether the cpu is an H8/300 or H8/300H. |
2076 | FLAG is non-zero for the H8/300H. */ | |
c906108c SS |
2077 | |
2078 | void | |
a8cdafbd AV |
2079 | set_h8300h (h_flag, s_flag) |
2080 | int h_flag, s_flag; | |
c906108c SS |
2081 | { |
2082 | /* FIXME: Much of the code in sim_load can be moved to sim_open. | |
2083 | This function being replaced by a sim_open:ARGV configuration | |
2ea716f6 | 2084 | option. */ |
a8cdafbd AV |
2085 | h8300hmode = h_flag; |
2086 | h8300smode = s_flag; | |
c906108c SS |
2087 | } |
2088 | ||
2089 | SIM_DESC | |
2090 | sim_open (kind, ptr, abfd, argv) | |
2091 | SIM_OPEN_KIND kind; | |
2092 | struct host_callback_struct *ptr; | |
2093 | struct _bfd *abfd; | |
2094 | char **argv; | |
2095 | { | |
2ea716f6 | 2096 | /* FIXME: Much of the code in sim_load can be moved here. */ |
c906108c SS |
2097 | |
2098 | sim_kind = kind; | |
2099 | myname = argv[0]; | |
2100 | sim_callback = ptr; | |
2ea716f6 | 2101 | /* Fudge our descriptor. */ |
c906108c SS |
2102 | return (SIM_DESC) 1; |
2103 | } | |
2104 | ||
2105 | void | |
2106 | sim_close (sd, quitting) | |
2107 | SIM_DESC sd; | |
2108 | int quitting; | |
2109 | { | |
2ea716f6 | 2110 | /* Nothing to do. */ |
c906108c SS |
2111 | } |
2112 | ||
2113 | /* Called by gdb to load a program into memory. */ | |
2114 | ||
2115 | SIM_RC | |
2116 | sim_load (sd, prog, abfd, from_tty) | |
2117 | SIM_DESC sd; | |
2118 | char *prog; | |
2119 | bfd *abfd; | |
2120 | int from_tty; | |
2121 | { | |
2122 | bfd *prog_bfd; | |
2123 | ||
2ea716f6 KH |
2124 | /* FIXME: The code below that sets a specific variant of the H8/300 |
2125 | being simulated should be moved to sim_open(). */ | |
c906108c | 2126 | |
2ea716f6 | 2127 | /* See if the file is for the H8/300 or H8/300H. */ |
c906108c SS |
2128 | /* ??? This may not be the most efficient way. The z8k simulator |
2129 | does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */ | |
2130 | if (abfd != NULL) | |
2131 | prog_bfd = abfd; | |
2132 | else | |
2133 | prog_bfd = bfd_openr (prog, "coff-h8300"); | |
2134 | if (prog_bfd != NULL) | |
2135 | { | |
2136 | /* Set the cpu type. We ignore failure from bfd_check_format | |
2137 | and bfd_openr as sim_load_file checks too. */ | |
de9b1892 | 2138 | if (bfd_check_format (prog_bfd, bfd_object)) |
c906108c SS |
2139 | { |
2140 | unsigned long mach = bfd_get_mach (prog_bfd); | |
a8cdafbd AV |
2141 | set_h8300h (mach == bfd_mach_h8300h || mach == bfd_mach_h8300s, |
2142 | mach == bfd_mach_h8300s); | |
c906108c SS |
2143 | } |
2144 | } | |
2145 | ||
2146 | /* If we're using gdb attached to the simulator, then we have to | |
2147 | reallocate memory for the simulator. | |
2148 | ||
2149 | When gdb first starts, it calls fetch_registers (among other | |
2150 | functions), which in turn calls init_pointers, which allocates | |
2151 | simulator memory. | |
2152 | ||
2153 | The problem is when we do that, we don't know whether we're | |
2ea716f6 | 2154 | debugging an H8/300 or H8/300H program. |
c906108c SS |
2155 | |
2156 | This is the first point at which we can make that determination, | |
2157 | so we just reallocate memory now; this will also allow us to handle | |
2ea716f6 | 2158 | switching between H8/300 and H8/300H programs without exiting |
c906108c | 2159 | gdb. */ |
a8cdafbd AV |
2160 | |
2161 | if (h8300smode) | |
2162 | memory_size = H8300S_MSIZE; | |
2163 | else if (h8300hmode) | |
c906108c SS |
2164 | memory_size = H8300H_MSIZE; |
2165 | else | |
2166 | memory_size = H8300_MSIZE; | |
2167 | ||
2168 | if (cpu.memory) | |
2169 | free (cpu.memory); | |
2170 | if (cpu.cache_idx) | |
2171 | free (cpu.cache_idx); | |
2172 | if (cpu.eightbit) | |
2173 | free (cpu.eightbit); | |
2174 | ||
2175 | cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size); | |
2176 | cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size); | |
2177 | cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256); | |
2178 | ||
2ea716f6 | 2179 | /* `msize' must be a power of two. */ |
c906108c SS |
2180 | if ((memory_size & (memory_size - 1)) != 0) |
2181 | abort (); | |
2182 | cpu.mask = memory_size - 1; | |
2183 | ||
2184 | if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd, | |
2185 | sim_kind == SIM_OPEN_DEBUG, | |
2186 | 0, sim_write) | |
2187 | == NULL) | |
2188 | { | |
2189 | /* Close the bfd if we opened it. */ | |
2190 | if (abfd == NULL && prog_bfd != NULL) | |
2191 | bfd_close (prog_bfd); | |
2192 | return SIM_RC_FAIL; | |
2193 | } | |
2194 | ||
2195 | /* Close the bfd if we opened it. */ | |
2196 | if (abfd == NULL && prog_bfd != NULL) | |
2197 | bfd_close (prog_bfd); | |
2198 | return SIM_RC_OK; | |
2199 | } | |
2200 | ||
2201 | SIM_RC | |
2202 | sim_create_inferior (sd, abfd, argv, env) | |
2203 | SIM_DESC sd; | |
2204 | struct _bfd *abfd; | |
2205 | char **argv; | |
2206 | char **env; | |
2207 | { | |
2208 | if (abfd != NULL) | |
2209 | cpu.pc = bfd_get_start_address (abfd); | |
2210 | else | |
2211 | cpu.pc = 0; | |
2212 | return SIM_RC_OK; | |
2213 | } | |
2214 | ||
2215 | void | |
2216 | sim_do_command (sd, cmd) | |
2217 | SIM_DESC sd; | |
2218 | char *cmd; | |
2219 | { | |
2220 | (*sim_callback->printf_filtered) (sim_callback, | |
2221 | "This simulator does not accept any commands.\n"); | |
2222 | } | |
2223 | ||
2224 | void | |
2225 | sim_set_callbacks (ptr) | |
2226 | struct host_callback_struct *ptr; | |
2227 | { | |
2228 | sim_callback = ptr; | |
2229 | } |