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