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