2 * Simulator for the Renesas (formerly Hitachi) H8/300 architecture.
4 * Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com
6 * This file is part of H8/300 sim
9 * THIS SOFTWARE IS NOT COPYRIGHTED
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.
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.
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
34 #include "gdb/sim-h8300.h"
36 #include "sys/types.h"
37 #include "sim-options.h"
45 #define X(op, size) (op * 4 + size)
47 #define SP (h8300hmode && !h8300_normal_mode ? SL : SW)
49 #define h8_opcodes ops
51 #include "opcode/h8300.h"
53 /* CPU data object: */
56 h8_get_pc (SIM_DESC sd
)
58 return (STATE_CPU (sd
, 0)) -> pc
;
62 h8_set_pc (SIM_DESC sd
, unsigned int val
)
64 (STATE_CPU (sd
, 0)) -> pc
= val
;
68 h8_get_ccr (SIM_DESC sd
)
70 return (STATE_CPU (sd
, 0)) -> regs
[CCR_REGNUM
];
74 h8_set_ccr (SIM_DESC sd
, unsigned int val
)
76 (STATE_CPU (sd
, 0)) -> regs
[CCR_REGNUM
] = val
;
80 h8_get_exr (SIM_DESC sd
)
82 return (STATE_CPU (sd
, 0)) -> regs
[EXR_REGNUM
];
86 h8_set_exr (SIM_DESC sd
, unsigned int val
)
88 (STATE_CPU (sd
, 0)) -> regs
[EXR_REGNUM
] = val
;
92 h8_get_sbr (SIM_DESC sd
)
94 return (STATE_CPU (sd
, 0)) -> regs
[SBR_REGNUM
];
98 h8_set_sbr (SIM_DESC sd
, int val
)
100 (STATE_CPU (sd
, 0)) -> regs
[SBR_REGNUM
] = val
;
104 h8_get_vbr (SIM_DESC sd
)
106 return (STATE_CPU (sd
, 0)) -> regs
[VBR_REGNUM
];
110 h8_set_vbr (SIM_DESC sd
, int val
)
112 (STATE_CPU (sd
, 0)) -> regs
[VBR_REGNUM
] = val
;
116 h8_get_mask (SIM_DESC sd
)
118 return (STATE_CPU (sd
, 0)) -> mask
;
122 h8_set_mask (SIM_DESC sd
, int val
)
124 (STATE_CPU (sd
, 0)) -> mask
= val
;
128 h8_get_exception (SIM_DESC sd
)
130 return (STATE_CPU (sd
, 0)) -> exception
;
134 h8_set_exception (SIM_DESC sd
, int val
)
136 (STATE_CPU (sd
, 0)) -> exception
= val
;
139 static enum h8300_sim_state
140 h8_get_state (SIM_DESC sd
)
146 h8_set_state (SIM_DESC sd
, enum h8300_sim_state val
)
152 h8_get_cycles (SIM_DESC sd
)
154 return (STATE_CPU (sd
, 0)) -> regs
[CYCLE_REGNUM
];
158 h8_set_cycles (SIM_DESC sd
, unsigned int val
)
160 (STATE_CPU (sd
, 0)) -> regs
[CYCLE_REGNUM
] = val
;
164 h8_get_insts (SIM_DESC sd
)
166 return (STATE_CPU (sd
, 0)) -> regs
[INST_REGNUM
];
170 h8_set_insts (SIM_DESC sd
, unsigned int val
)
172 (STATE_CPU (sd
, 0)) -> regs
[INST_REGNUM
] = val
;
176 h8_get_ticks (SIM_DESC sd
)
178 return (STATE_CPU (sd
, 0)) -> regs
[TICK_REGNUM
];
182 h8_set_ticks (SIM_DESC sd
, unsigned int val
)
184 (STATE_CPU (sd
, 0)) -> regs
[TICK_REGNUM
] = val
;
188 h8_get_mach (SIM_DESC sd
)
190 return (STATE_CPU (sd
, 0)) -> regs
[MACH_REGNUM
];
194 h8_set_mach (SIM_DESC sd
, unsigned int val
)
196 (STATE_CPU (sd
, 0)) -> regs
[MACH_REGNUM
] = val
;
200 h8_get_macl (SIM_DESC sd
)
202 return (STATE_CPU (sd
, 0)) -> regs
[MACL_REGNUM
];
206 h8_set_macl (SIM_DESC sd
, unsigned int val
)
208 (STATE_CPU (sd
, 0)) -> regs
[MACL_REGNUM
] = val
;
211 static unsigned int *
212 h8_get_reg_buf (SIM_DESC sd
)
214 return &(((STATE_CPU (sd
, 0)) -> regs
)[0]);
218 h8_get_reg (SIM_DESC sd
, int regnum
)
220 return (STATE_CPU (sd
, 0)) -> regs
[regnum
];
224 h8_set_reg (SIM_DESC sd
, int regnum
, int val
)
226 (STATE_CPU (sd
, 0)) -> regs
[regnum
] = val
;
231 h8_get_stats (SIM_DESC sd
, int idx
)
233 return sd
-> stats
[idx
];
237 h8_increment_stats (SIM_DESC sd
, int idx
)
243 static unsigned char *
244 h8_get_memory_buf (SIM_DESC sd
)
246 return (STATE_CPU (sd
, 0)) -> memory
;
250 h8_set_memory_buf (SIM_DESC sd
, unsigned char *ptr
)
252 (STATE_CPU (sd
, 0)) -> memory
= ptr
;
256 h8_get_memory (SIM_DESC sd
, int idx
)
258 return (STATE_CPU (sd
, 0)) -> memory
[idx
];
262 h8_set_memory (SIM_DESC sd
, int idx
, unsigned int val
)
264 (STATE_CPU (sd
, 0)) -> memory
[idx
] = (unsigned char) val
;
267 static unsigned char *
268 h8_get_eightbit_buf (SIM_DESC sd
)
270 return (STATE_CPU (sd
, 0)) -> eightbit
;
274 h8_set_eightbit_buf (SIM_DESC sd
, unsigned char *ptr
)
276 (STATE_CPU (sd
, 0)) -> eightbit
= ptr
;
280 h8_get_eightbit (SIM_DESC sd
, int idx
)
282 return (STATE_CPU (sd
, 0)) -> eightbit
[idx
];
286 h8_set_eightbit (SIM_DESC sd
, int idx
, unsigned int val
)
288 (STATE_CPU (sd
, 0)) -> eightbit
[idx
] = (unsigned char) val
;
292 h8_get_delayed_branch (SIM_DESC sd
)
294 return (STATE_CPU (sd
, 0)) -> delayed_branch
;
298 h8_set_delayed_branch (SIM_DESC sd
, unsigned int dest
)
300 (STATE_CPU (sd
, 0)) -> delayed_branch
= dest
;
304 h8_get_command_line (SIM_DESC sd
)
306 return (STATE_CPU (sd
, 0)) -> command_line
;
310 h8_set_command_line (SIM_DESC sd
, char ** val
)
312 (STATE_CPU (sd
, 0)) -> command_line
= val
;
316 h8_get_cmdline_arg (SIM_DESC sd
, int index
)
318 return (STATE_CPU (sd
, 0)) -> command_line
[index
];
322 h8_set_cmdline_arg (SIM_DESC sd
, int index
, char * val
)
324 (STATE_CPU (sd
, 0)) -> command_line
[index
] = val
;
327 /* MAC Saturation Mode */
329 h8_get_macS (SIM_DESC sd
)
331 return (STATE_CPU (sd
, 0)) -> macS
;
335 h8_set_macS (SIM_DESC sd
, int val
)
337 (STATE_CPU (sd
, 0)) -> macS
= (val
!= 0);
342 h8_get_macZ (SIM_DESC sd
)
344 return (STATE_CPU (sd
, 0)) -> macZ
;
348 h8_set_macZ (SIM_DESC sd
, int val
)
350 (STATE_CPU (sd
, 0)) -> macZ
= (val
!= 0);
353 /* MAC Negative Flag */
355 h8_get_macN (SIM_DESC sd
)
357 return (STATE_CPU (sd
, 0)) -> macN
;
361 h8_set_macN (SIM_DESC sd
, int val
)
363 (STATE_CPU (sd
, 0)) -> macN
= (val
!= 0);
366 /* MAC Overflow Flag */
368 h8_get_macV (SIM_DESC sd
)
370 return (STATE_CPU (sd
, 0)) -> macV
;
374 h8_set_macV (SIM_DESC sd
, int val
)
376 (STATE_CPU (sd
, 0)) -> macV
= (val
!= 0);
379 /* End CPU data object. */
381 /* The rate at which to call the host's poll_quit callback. */
383 enum { POLL_QUIT_INTERVAL
= 0x80000 };
385 #define LOW_BYTE(x) ((x) & 0xff)
386 #define HIGH_BYTE(x) (((x) >> 8) & 0xff)
387 #define P(X, Y) ((X << 8) | Y)
396 #define I (intMaskBit != 0)
398 #define BUILDSR(SD) \
399 h8_set_ccr (SD, (I << 7) | (UI << 6) | (H << 5) | (U << 4) \
400 | (N << 3) | (Z << 2) | (V << 1) | C)
403 /* Get Status Register (flags). */ \
404 c = (h8_get_ccr (sd) >> 0) & 1; \
405 v = (h8_get_ccr (sd) >> 1) & 1; \
406 nz = !((h8_get_ccr (sd) >> 2) & 1); \
407 n = (h8_get_ccr (sd) >> 3) & 1; \
408 u = (h8_get_ccr (sd) >> 4) & 1; \
409 h = (h8_get_ccr (sd) >> 5) & 1; \
410 ui = ((h8_get_ccr (sd) >> 6) & 1); \
411 intMaskBit = (h8_get_ccr (sd) >> 7) & 1
414 #ifdef __CHAR_IS_SIGNED__
415 #define SEXTCHAR(x) ((char) (x))
419 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff) : x & 0xff)
422 #define UEXTCHAR(x) ((x) & 0xff)
423 #define UEXTSHORT(x) ((x) & 0xffff)
424 #define SEXTSHORT(x) ((short) (x))
428 int h8300_normal_mode
= 0;
431 static int memory_size
;
436 return time (0); /* WinXX HAS UNIX like 'time', so why not use it? */
458 return (h8300hmode
&& !h8300_normal_mode
)? SL
: SW
;
463 /* Simulate an indirection / dereference.
464 return 0 for success, -1 for failure.
468 lvalue (SIM_DESC sd
, int x
, int rn
, unsigned int *val
)
470 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
472 if (val
== NULL
) /* Paranoia. */
478 if (rn
== ZERO_REGNUM
)
479 *val
= X (OP_IMM
, SP
);
481 *val
= X (OP_REG
, SP
);
484 *val
= X (OP_MEM
, SP
);
487 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
496 if (h8300smode
&& !h8300_normal_mode
)
498 else if (h8300hmode
&& !h8300_normal_mode
)
505 decode (SIM_DESC sd
, int addr
, unsigned char *data
, decoded_inst
*dst
)
507 int cst
[3] = {0, 0, 0};
508 int reg
[3] = {0, 0, 0};
509 int rdisp
[3] = {0, 0, 0};
511 const struct h8_opcode
*q
;
516 /* Find the exact opcode/arg combo. */
517 for (q
= h8_opcodes
; q
->name
; q
++)
519 const op_type
*nib
= q
->data
.nib
;
520 unsigned int len
= 0;
522 if ((q
->available
== AV_H8SX
&& !h8300sxmode
) ||
523 (q
->available
== AV_H8S
&& !h8300smode
) ||
524 (q
->available
== AV_H8H
&& !h8300hmode
))
527 cst
[0] = cst
[1] = cst
[2] = 0;
528 reg
[0] = reg
[1] = reg
[2] = 0;
529 rdisp
[0] = rdisp
[1] = rdisp
[2] = 0;
533 op_type looking_for
= *nib
;
534 int thisnib
= data
[len
/ 2];
536 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
537 opnum
= ((looking_for
& OP3
) ? 2 :
538 (looking_for
& DST
) ? 1 : 0);
540 if (looking_for
< 16 && looking_for
>= 0)
542 if (looking_for
!= thisnib
)
547 if (looking_for
& B31
)
549 if (!((thisnib
& 0x8) != 0))
552 looking_for
= (op_type
) (looking_for
& ~B31
);
555 else if (looking_for
& B30
)
557 if (!((thisnib
& 0x8) == 0))
560 looking_for
= (op_type
) (looking_for
& ~B30
);
563 if (looking_for
& B21
)
565 if (!((thisnib
& 0x4) != 0))
568 looking_for
= (op_type
) (looking_for
& ~B21
);
571 else if (looking_for
& B20
)
573 if (!((thisnib
& 0x4) == 0))
576 looking_for
= (op_type
) (looking_for
& ~B20
);
579 if (looking_for
& B11
)
581 if (!((thisnib
& 0x2) != 0))
584 looking_for
= (op_type
) (looking_for
& ~B11
);
587 else if (looking_for
& B10
)
589 if (!((thisnib
& 0x2) == 0))
592 looking_for
= (op_type
) (looking_for
& ~B10
);
595 if (looking_for
& B01
)
597 if (!((thisnib
& 0x1) != 0))
600 looking_for
= (op_type
) (looking_for
& ~B01
);
603 else if (looking_for
& B00
)
605 if (!((thisnib
& 0x1) == 0))
608 looking_for
= (op_type
) (looking_for
& ~B00
);
611 if (looking_for
& IGNORE
)
613 /* Hitachi has declared that IGNORE must be zero. */
617 else if ((looking_for
& MODE
) == DATA
)
619 ; /* Skip embedded data. */
621 else if ((looking_for
& MODE
) == DBIT
)
623 /* Exclude adds/subs by looking at bit 0 and 2, and
624 make sure the operand size, either w or l,
625 matches by looking at bit 1. */
626 if ((looking_for
& 7) != (thisnib
& 7))
629 cst
[opnum
] = (thisnib
& 0x8) ? 2 : 1;
631 else if ((looking_for
& MODE
) == REG
||
632 (looking_for
& MODE
) == LOWREG
||
633 (looking_for
& MODE
) == IND
||
634 (looking_for
& MODE
) == PREINC
||
635 (looking_for
& MODE
) == POSTINC
||
636 (looking_for
& MODE
) == PREDEC
||
637 (looking_for
& MODE
) == POSTDEC
)
639 reg
[opnum
] = thisnib
;
641 else if (looking_for
& CTRL
)
644 if (((looking_for
& MODE
) == CCR
&& (thisnib
!= C_CCR
)) ||
645 ((looking_for
& MODE
) == EXR
&& (thisnib
!= C_EXR
)) ||
646 ((looking_for
& MODE
) == MACH
&& (thisnib
!= C_MACH
)) ||
647 ((looking_for
& MODE
) == MACL
&& (thisnib
!= C_MACL
)) ||
648 ((looking_for
& MODE
) == VBR
&& (thisnib
!= C_VBR
)) ||
649 ((looking_for
& MODE
) == SBR
&& (thisnib
!= C_SBR
)))
651 if (((looking_for
& MODE
) == CCR_EXR
&&
652 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
)) ||
653 ((looking_for
& MODE
) == VBR_SBR
&&
654 (thisnib
!= C_VBR
&& thisnib
!= C_SBR
)) ||
655 ((looking_for
& MODE
) == MACREG
&&
656 (thisnib
!= C_MACH
&& thisnib
!= C_MACL
)))
658 if (((looking_for
& MODE
) == CC_EX_VB_SB
&&
659 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
&&
660 thisnib
!= C_VBR
&& thisnib
!= C_SBR
)))
663 reg
[opnum
] = thisnib
;
665 else if ((looking_for
& MODE
) == ABS
)
667 /* Absolute addresses are unsigned. */
668 switch (looking_for
& SIZE
)
671 cst
[opnum
] = UEXTCHAR (data
[len
/ 2]);
675 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
679 (data
[len
/ 2 + 0] << 24) +
680 (data
[len
/ 2 + 1] << 16) +
681 (data
[len
/ 2 + 2] << 8) +
685 printf ("decode: bad size ABS: %d\n",
686 (looking_for
& SIZE
));
690 else if ((looking_for
& MODE
) == DISP
||
691 (looking_for
& MODE
) == PCREL
||
692 (looking_for
& MODE
) == INDEXB
||
693 (looking_for
& MODE
) == INDEXW
||
694 (looking_for
& MODE
) == INDEXL
)
696 switch (looking_for
& SIZE
)
699 cst
[opnum
] = thisnib
& 3;
702 cst
[opnum
] = SEXTCHAR (data
[len
/ 2]);
705 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
706 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
709 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
713 (data
[len
/ 2 + 0] << 24) +
714 (data
[len
/ 2 + 1] << 16) +
715 (data
[len
/ 2 + 2] << 8) +
719 printf ("decode: bad size DISP/PCREL/INDEX: %d\n",
720 (looking_for
& SIZE
));
724 else if ((looking_for
& SIZE
) == L_16
||
725 (looking_for
& SIZE
) == L_16U
)
727 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
728 /* Immediates are always unsigned. */
729 if ((looking_for
& SIZE
) != L_16U
&&
730 (looking_for
& MODE
) != IMM
)
731 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
733 else if (looking_for
& ABSJMP
)
735 switch (looking_for
& SIZE
) {
737 cst
[opnum
] = (data
[1] << 16) | (data
[2] << 8) | (data
[3]);
741 (data
[len
/ 2 + 0] << 24) +
742 (data
[len
/ 2 + 1] << 16) +
743 (data
[len
/ 2 + 2] << 8) +
747 printf ("decode: bad size ABSJMP: %d\n",
748 (looking_for
& SIZE
));
752 else if ((looking_for
& MODE
) == MEMIND
)
754 cst
[opnum
] = data
[1];
756 else if ((looking_for
& MODE
) == VECIND
)
758 if(h8300_normal_mode
)
759 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 2;
761 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 4;
762 cst
[opnum
] += h8_get_vbr (sd
); /* Add vector base reg. */
764 else if ((looking_for
& SIZE
) == L_32
)
769 (data
[i
+ 0] << 24) |
770 (data
[i
+ 1] << 16) |
774 else if ((looking_for
& SIZE
) == L_24
)
779 (data
[i
+ 0] << 16) |
783 else if (looking_for
& DISPREG
)
785 rdisp
[opnum
] = thisnib
& 0x7;
787 else if ((looking_for
& MODE
) == KBIT
)
804 else if ((looking_for
& SIZE
) == L_8
)
806 if ((looking_for
& MODE
) == ABS
)
808 /* Will be combined with contents of SBR_REGNUM
809 by fetch (). For all modes except h8sx, this
810 will always contain the value 0xFFFFFF00. */
811 cst
[opnum
] = data
[len
/ 2] & 0xff;
815 cst
[opnum
] = data
[len
/ 2] & 0xff;
818 else if ((looking_for
& SIZE
) == L_2
)
820 cst
[opnum
] = thisnib
& 3;
822 else if ((looking_for
& SIZE
) == L_3
||
823 (looking_for
& SIZE
) == L_3NZ
)
825 cst
[opnum
] = thisnib
& 7;
826 if (cst
[opnum
] == 0 && (looking_for
& SIZE
) == L_3NZ
)
829 else if ((looking_for
& SIZE
) == L_4
)
831 cst
[opnum
] = thisnib
& 15;
833 else if ((looking_for
& SIZE
) == L_5
)
835 cst
[opnum
] = data
[len
/ 2] & 0x1f;
837 else if (looking_for
== E
)
842 /* Fill in the args. */
844 const op_type
*args
= q
->args
.nib
;
849 nargs
< 3 && *args
!= E
;
855 opnum
= ((x
& OP3
) ? 2 :
864 if ((x
& MODE
) == IMM
||
865 (x
& MODE
) == KBIT
||
868 /* Use the instruction to determine
870 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
871 p
->literal
= cst
[opnum
];
873 else if ((x
& MODE
) == CONST_2
||
874 (x
& MODE
) == CONST_4
||
875 (x
& MODE
) == CONST_8
||
876 (x
& MODE
) == CONST_16
)
878 /* Use the instruction to determine
880 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
882 case CONST_2
: p
->literal
= 2; break;
883 case CONST_4
: p
->literal
= 4; break;
884 case CONST_8
: p
->literal
= 8; break;
885 case CONST_16
: p
->literal
= 16; break;
888 else if ((x
& MODE
) == REG
)
890 p
->type
= X (OP_REG
, bitfrom (x
));
893 else if ((x
& MODE
) == LOWREG
)
895 p
->type
= X (OP_LOWREG
, bitfrom (x
));
898 else if ((x
& MODE
) == PREINC
)
900 /* Use the instruction to determine
902 p
->type
= X (OP_PREINC
, OP_SIZE (q
->how
));
903 p
->reg
= reg
[opnum
] & 0x7;
905 else if ((x
& MODE
) == POSTINC
)
907 /* Use the instruction to determine
909 p
->type
= X (OP_POSTINC
, OP_SIZE (q
->how
));
910 p
->reg
= reg
[opnum
] & 0x7;
912 else if ((x
& MODE
) == PREDEC
)
914 /* Use the instruction to determine
916 p
->type
= X (OP_PREDEC
, OP_SIZE (q
->how
));
917 p
->reg
= reg
[opnum
] & 0x7;
919 else if ((x
& MODE
) == POSTDEC
)
921 /* Use the instruction to determine
923 p
->type
= X (OP_POSTDEC
, OP_SIZE (q
->how
));
924 p
->reg
= reg
[opnum
] & 0x7;
926 else if ((x
& MODE
) == IND
)
928 /* Note: an indirect is transformed into
929 a displacement of zero.
931 /* Use the instruction to determine
933 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
934 p
->reg
= reg
[opnum
] & 0x7;
936 if (OP_KIND (q
->how
) == O_JSR
||
937 OP_KIND (q
->how
) == O_JMP
)
938 if (lvalue (sd
, p
->type
, p
->reg
, (unsigned int *)&p
->type
))
941 else if ((x
& MODE
) == ABS
)
943 /* Note: a 16 or 32 bit ABS is transformed into a
944 displacement from pseudo-register ZERO_REGNUM,
945 which is always zero. An 8 bit ABS becomes
946 a displacement from SBR_REGNUM.
948 /* Use the instruction to determine
950 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
951 p
->literal
= cst
[opnum
];
953 /* 8-bit ABS is displacement from SBR.
954 16 and 32-bit ABS are displacement from ZERO.
955 (SBR will always be zero except for h8/sx)
957 if ((x
& SIZE
) == L_8
)
960 p
->reg
= ZERO_REGNUM
;;
962 else if ((x
& MODE
) == MEMIND
||
963 (x
& MODE
) == VECIND
)
965 /* Size doesn't matter. */
966 p
->type
= X (OP_MEM
, SB
);
967 p
->literal
= cst
[opnum
];
968 if (OP_KIND (q
->how
) == O_JSR
||
969 OP_KIND (q
->how
) == O_JMP
)
970 if (lvalue (sd
, p
->type
, p
->reg
, (unsigned int *)&p
->type
))
973 else if ((x
& MODE
) == PCREL
)
975 /* Size doesn't matter. */
976 p
->type
= X (OP_PCREL
, SB
);
977 p
->literal
= cst
[opnum
];
981 p
->type
= X (OP_IMM
, SP
);
982 p
->literal
= cst
[opnum
];
984 else if ((x
& MODE
) == INDEXB
)
986 p
->type
= X (OP_INDEXB
, OP_SIZE (q
->how
));
987 p
->literal
= cst
[opnum
];
988 p
->reg
= rdisp
[opnum
];
990 else if ((x
& MODE
) == INDEXW
)
992 p
->type
= X (OP_INDEXW
, OP_SIZE (q
->how
));
993 p
->literal
= cst
[opnum
];
994 p
->reg
= rdisp
[opnum
];
996 else if ((x
& MODE
) == INDEXL
)
998 p
->type
= X (OP_INDEXL
, OP_SIZE (q
->how
));
999 p
->literal
= cst
[opnum
];
1000 p
->reg
= rdisp
[opnum
];
1002 else if ((x
& MODE
) == DISP
)
1004 /* Yuck -- special for mova args. */
1005 if (strncmp (q
->name
, "mova", 4) == 0 &&
1008 /* Mova can have a DISP2 dest, with an
1009 INDEXB or INDEXW src. The multiplier
1010 for the displacement value is determined
1011 by the src operand, not by the insn. */
1013 switch (OP_KIND (dst
->src
.type
))
1016 p
->type
= X (OP_DISP
, SB
);
1017 p
->literal
= cst
[opnum
];
1020 p
->type
= X (OP_DISP
, SW
);
1021 p
->literal
= cst
[opnum
] * 2;
1029 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1030 p
->literal
= cst
[opnum
];
1031 /* DISP2 is special. */
1032 if ((x
& SIZE
) == L_2
)
1033 switch (OP_SIZE (q
->how
))
1036 case SW
: p
->literal
*= 2; break;
1037 case SL
: p
->literal
*= 4; break;
1040 p
->reg
= rdisp
[opnum
];
1047 p
->type
= X (OP_CCR
, SB
);
1050 p
->type
= X (OP_EXR
, SB
);
1053 p
->type
= X (OP_MACH
, SL
);
1056 p
->type
= X (OP_MACL
, SL
);
1059 p
->type
= X (OP_VBR
, SL
);
1062 p
->type
= X (OP_SBR
, SL
);
1066 else if ((x
& MODE
) == CCR
)
1070 else if ((x
& MODE
) == EXR
)
1075 printf ("Hmmmm 0x%x...\n", x
);
1081 /* Unary operators: treat src and dst as equivalent. */
1082 if (dst
->dst
.type
== -1)
1083 dst
->dst
= dst
->src
;
1084 if (dst
->src
.type
== -1)
1085 dst
->src
= dst
->dst
;
1087 dst
->opcode
= q
->how
;
1088 dst
->cycles
= q
->time
;
1090 /* And jsr's to these locations are turned into
1093 if (OP_KIND (dst
->opcode
) == O_JSR
)
1095 switch (dst
->src
.literal
)
1098 dst
->opcode
= O (O_SYS_OPEN
, SB
);
1101 dst
->opcode
= O (O_SYS_READ
, SB
);
1104 dst
->opcode
= O (O_SYS_WRITE
, SB
);
1107 dst
->opcode
= O (O_SYS_LSEEK
, SB
);
1110 dst
->opcode
= O (O_SYS_CLOSE
, SB
);
1113 dst
->opcode
= O (O_SYS_STAT
, SB
);
1116 dst
->opcode
= O (O_SYS_FSTAT
, SB
);
1119 dst
->opcode
= O (O_SYS_CMDLINE
, SB
);
1122 /* End of Processing for system calls. */
1125 dst
->next_pc
= addr
+ len
/ 2;
1129 printf ("Don't understand 0x%x \n", looking_for
);
1140 /* Fell off the end. */
1141 dst
->opcode
= O (O_ILL
, SB
);
1144 static unsigned char *breg
[32];
1145 static unsigned short *wreg
[16];
1147 #define GET_B_REG(X) *(breg[X])
1148 #define SET_B_REG(X, Y) (*(breg[X])) = (Y)
1149 #define GET_W_REG(X) *(wreg[X])
1150 #define SET_W_REG(X, Y) (*(wreg[X])) = (Y)
1151 #define GET_L_REG(X) h8_get_reg (sd, X)
1152 #define SET_L_REG(X, Y) h8_set_reg (sd, X, Y)
1154 #define GET_MEMORY_L(X) \
1155 ((X) < memory_size \
1156 ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16) \
1157 | (h8_get_memory (sd, (X)+2) << 8) | (h8_get_memory (sd, (X)+3) << 0)) \
1158 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 24) \
1159 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 16) \
1160 | (h8_get_eightbit (sd, ((X)+2) & 0xff) << 8) \
1161 | (h8_get_eightbit (sd, ((X)+3) & 0xff) << 0)))
1163 #define GET_MEMORY_W(X) \
1164 ((X) < memory_size \
1165 ? ((h8_get_memory (sd, (X)+0) << 8) \
1166 | (h8_get_memory (sd, (X)+1) << 0)) \
1167 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 8) \
1168 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 0)))
1171 #define GET_MEMORY_B(X) \
1172 ((X) < memory_size ? (h8_get_memory (sd, (X))) \
1173 : (h8_get_eightbit (sd, (X) & 0xff)))
1175 #define SET_MEMORY_L(X, Y) \
1176 { register unsigned char *_p; register int __y = (Y); \
1177 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1178 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1179 _p[0] = __y >> 24; _p[1] = __y >> 16; \
1180 _p[2] = __y >> 8; _p[3] = __y >> 0; \
1183 #define SET_MEMORY_W(X, Y) \
1184 { register unsigned char *_p; register int __y = (Y); \
1185 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1186 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1187 _p[0] = __y >> 8; _p[1] = __y; \
1190 #define SET_MEMORY_B(X, Y) \
1191 ((X) < memory_size ? (h8_set_memory (sd, (X), (Y))) \
1192 : (h8_set_eightbit (sd, (X) & 0xff, (Y))))
1194 /* Simulate a memory fetch.
1195 Return 0 for success, -1 for failure.
1199 fetch_1 (SIM_DESC sd
, ea_type
*arg
, int *val
, int twice
)
1201 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1203 int abs
= arg
->literal
;
1208 return -1; /* Paranoia. */
1212 /* Indexed register plus displacement mode:
1214 This new family of addressing modes are similar to OP_DISP
1215 (register plus displacement), with two differences:
1216 1) INDEXB uses only the least significant byte of the register,
1217 INDEXW uses only the least significant word, and
1218 INDEXL uses the entire register (just like OP_DISP).
1220 2) The displacement value in abs is multiplied by two
1221 for SW-sized operations, and by four for SL-size.
1223 This gives nine possible variations.
1226 case X (OP_INDEXB
, SB
):
1227 case X (OP_INDEXB
, SW
):
1228 case X (OP_INDEXB
, SL
):
1229 case X (OP_INDEXW
, SB
):
1230 case X (OP_INDEXW
, SW
):
1231 case X (OP_INDEXW
, SL
):
1232 case X (OP_INDEXL
, SB
):
1233 case X (OP_INDEXL
, SW
):
1234 case X (OP_INDEXL
, SL
):
1236 switch (OP_KIND (arg
->type
)) {
1237 case OP_INDEXB
: t
&= 0xff; break;
1238 case OP_INDEXW
: t
&= 0xffff; break;
1242 switch (OP_SIZE (arg
->type
)) {
1244 *val
= GET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (sd
));
1247 *val
= GET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (sd
));
1250 *val
= GET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (sd
));
1255 case X (OP_LOWREG
, SB
):
1256 *val
= GET_L_REG (rn
) & 0xff;
1258 case X (OP_LOWREG
, SW
):
1259 *val
= GET_L_REG (rn
) & 0xffff;
1262 case X (OP_REG
, SB
): /* Register direct, byte. */
1263 *val
= GET_B_REG (rn
);
1265 case X (OP_REG
, SW
): /* Register direct, word. */
1266 *val
= GET_W_REG (rn
);
1268 case X (OP_REG
, SL
): /* Register direct, long. */
1269 *val
= GET_L_REG (rn
);
1271 case X (OP_IMM
, SB
): /* Immediate, byte. */
1272 case X (OP_IMM
, SW
): /* Immediate, word. */
1273 case X (OP_IMM
, SL
): /* Immediate, long. */
1276 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr: byte. */
1278 r
= GET_MEMORY_B (t
& h8_get_mask (sd
));
1284 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr: word. */
1286 r
= GET_MEMORY_W (t
& h8_get_mask (sd
));
1292 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr: long. */
1294 r
= GET_MEMORY_L (t
& h8_get_mask (sd
));
1301 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr: byte. */
1303 r
= GET_MEMORY_B (t
& h8_get_mask (sd
));
1309 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr: word. */
1311 r
= GET_MEMORY_W (t
& h8_get_mask (sd
));
1317 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr: long. */
1319 r
= GET_MEMORY_L (t
& h8_get_mask (sd
));
1326 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr: byte. */
1327 t
= GET_L_REG (rn
) - 1;
1329 t
&= h8_get_mask (sd
);
1330 *val
= GET_MEMORY_B (t
);
1333 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr: word. */
1334 t
= GET_L_REG (rn
) - 2;
1336 t
&= h8_get_mask (sd
);
1337 *val
= GET_MEMORY_W (t
);
1340 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr: long. */
1341 t
= GET_L_REG (rn
) - 4;
1343 t
&= h8_get_mask (sd
);
1344 *val
= GET_MEMORY_L (t
);
1347 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr: byte. */
1348 t
= GET_L_REG (rn
) + 1;
1350 t
&= h8_get_mask (sd
);
1351 *val
= GET_MEMORY_B (t
);
1354 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr: long. */
1355 t
= GET_L_REG (rn
) + 2;
1357 t
&= h8_get_mask (sd
);
1358 *val
= GET_MEMORY_W (t
);
1361 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr: long. */
1362 t
= GET_L_REG (rn
) + 4;
1364 t
&= h8_get_mask (sd
);
1365 *val
= GET_MEMORY_L (t
);
1368 case X (OP_DISP
, SB
): /* Register indirect w/displacement: byte. */
1369 t
= GET_L_REG (rn
) + abs
;
1370 t
&= h8_get_mask (sd
);
1371 *val
= GET_MEMORY_B (t
);
1374 case X (OP_DISP
, SW
): /* Register indirect w/displacement: word. */
1375 t
= GET_L_REG (rn
) + abs
;
1376 t
&= h8_get_mask (sd
);
1377 *val
= GET_MEMORY_W (t
);
1380 case X (OP_DISP
, SL
): /* Register indirect w/displacement: long. */
1381 t
= GET_L_REG (rn
) + abs
;
1382 t
&= h8_get_mask (sd
);
1383 *val
=GET_MEMORY_L (t
);
1386 case X (OP_MEM
, SL
): /* Absolute memory address, long. */
1387 t
= GET_MEMORY_L (abs
);
1388 t
&= h8_get_mask (sd
);
1392 case X (OP_MEM
, SW
): /* Absolute memory address, word. */
1393 t
= GET_MEMORY_W (abs
);
1394 t
&= h8_get_mask (sd
);
1398 case X (OP_PCREL
, SB
): /* PC relative (for jump, branch etc). */
1399 case X (OP_PCREL
, SW
):
1400 case X (OP_PCREL
, SL
):
1401 case X (OP_PCREL
, SN
):
1405 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1407 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
1410 return 0; /* Success. */
1416 fetch (SIM_DESC sd
, ea_type
*arg
, int *val
)
1418 return fetch_1 (sd
, arg
, val
, 0);
1421 /* Fetch which will be followed by a store to the same location.
1422 The difference being that we don't want to do a post-increment
1423 or post-decrement at this time: we'll do it when we store. */
1426 fetch2 (SIM_DESC sd
, ea_type
*arg
, int *val
)
1428 return fetch_1 (sd
, arg
, val
, 1);
1431 /* Simulate a memory store.
1432 Return 0 for success, -1 for failure.
1436 store_1 (SIM_DESC sd
, ea_type
*arg
, int n
, int twice
)
1438 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1440 int abs
= arg
->literal
;
1445 /* Indexed register plus displacement mode:
1447 This new family of addressing modes are similar to OP_DISP
1448 (register plus displacement), with two differences:
1449 1) INDEXB uses only the least significant byte of the register,
1450 INDEXW uses only the least significant word, and
1451 INDEXL uses the entire register (just like OP_DISP).
1453 2) The displacement value in abs is multiplied by two
1454 for SW-sized operations, and by four for SL-size.
1456 This gives nine possible variations.
1459 case X (OP_INDEXB
, SB
):
1460 case X (OP_INDEXB
, SW
):
1461 case X (OP_INDEXB
, SL
):
1462 case X (OP_INDEXW
, SB
):
1463 case X (OP_INDEXW
, SW
):
1464 case X (OP_INDEXW
, SL
):
1465 case X (OP_INDEXL
, SB
):
1466 case X (OP_INDEXL
, SW
):
1467 case X (OP_INDEXL
, SL
):
1469 switch (OP_KIND (arg
->type
)) {
1470 case OP_INDEXB
: t
&= 0xff; break;
1471 case OP_INDEXW
: t
&= 0xffff; break;
1475 switch (OP_SIZE (arg
->type
)) {
1477 SET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (sd
), n
);
1480 SET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (sd
), n
);
1483 SET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (sd
), n
);
1488 case X (OP_REG
, SB
): /* Register direct, byte. */
1491 case X (OP_REG
, SW
): /* Register direct, word. */
1494 case X (OP_REG
, SL
): /* Register direct, long. */
1498 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr, byte. */
1503 t
&= h8_get_mask (sd
);
1504 SET_MEMORY_B (t
, n
);
1507 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr, word. */
1512 t
&= h8_get_mask (sd
);
1513 SET_MEMORY_W (t
, n
);
1516 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr, long. */
1521 t
&= h8_get_mask (sd
);
1522 SET_MEMORY_L (t
, n
);
1525 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr, byte. */
1530 t
&= h8_get_mask (sd
);
1531 SET_MEMORY_B (t
, n
);
1534 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr, word. */
1539 t
&= h8_get_mask (sd
);
1540 SET_MEMORY_W (t
, n
);
1543 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr, long. */
1548 t
&= h8_get_mask (sd
);
1549 SET_MEMORY_L (t
, n
);
1552 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr, byte. */
1554 SET_L_REG (rn
, t
- 1);
1555 t
&= h8_get_mask (sd
);
1556 SET_MEMORY_B (t
, n
);
1559 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr, word. */
1561 SET_L_REG (rn
, t
- 2);
1562 t
&= h8_get_mask (sd
);
1563 SET_MEMORY_W (t
, n
);
1566 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr, long. */
1568 SET_L_REG (rn
, t
- 4);
1569 t
&= h8_get_mask (sd
);
1570 SET_MEMORY_L (t
, n
);
1573 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr, byte. */
1575 SET_L_REG (rn
, t
+ 1);
1576 t
&= h8_get_mask (sd
);
1577 SET_MEMORY_B (t
, n
);
1580 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr, word. */
1582 SET_L_REG (rn
, t
+ 2);
1583 t
&= h8_get_mask (sd
);
1584 SET_MEMORY_W (t
, n
);
1587 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr, long. */
1589 SET_L_REG (rn
, t
+ 4);
1590 t
&= h8_get_mask (sd
);
1591 SET_MEMORY_L (t
, n
);
1594 case X (OP_DISP
, SB
): /* Register indirect w/displacement, byte. */
1595 t
= GET_L_REG (rn
) + abs
;
1596 t
&= h8_get_mask (sd
);
1597 SET_MEMORY_B (t
, n
);
1600 case X (OP_DISP
, SW
): /* Register indirect w/displacement, word. */
1601 t
= GET_L_REG (rn
) + abs
;
1602 t
&= h8_get_mask (sd
);
1603 SET_MEMORY_W (t
, n
);
1606 case X (OP_DISP
, SL
): /* Register indirect w/displacement, long. */
1607 t
= GET_L_REG (rn
) + abs
;
1608 t
&= h8_get_mask (sd
);
1609 SET_MEMORY_L (t
, n
);
1613 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1614 case X (OP_MEM
, SW
): /* Why isn't this implemented? */
1615 case X (OP_MEM
, SL
): /* Why isn't this implemented? */
1617 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
1626 store (SIM_DESC sd
, ea_type
*arg
, int n
)
1628 return store_1 (sd
, arg
, n
, 0);
1631 /* Store which follows a fetch from the same location.
1632 The difference being that we don't want to do a pre-increment
1633 or pre-decrement at this time: it was already done when we fetched. */
1636 store2 (SIM_DESC sd
, ea_type
*arg
, int n
)
1638 return store_1 (sd
, arg
, n
, 1);
1641 /* Flag to be set whenever a new SIM_DESC object is created. */
1642 static int init_pointers_needed
= 1;
1645 init_pointers (SIM_DESC sd
)
1647 if (init_pointers_needed
)
1651 if (h8300smode
&& !h8300_normal_mode
)
1652 memory_size
= H8300S_MSIZE
;
1653 else if (h8300hmode
&& !h8300_normal_mode
)
1654 memory_size
= H8300H_MSIZE
;
1656 memory_size
= H8300_MSIZE
;
1657 /* `msize' must be a power of two. */
1658 if ((memory_size
& (memory_size
- 1)) != 0)
1662 "init_pointers: bad memory size %d, defaulting to %d.\n",
1663 memory_size
, memory_size
= H8300S_MSIZE
);
1666 if (h8_get_memory_buf (sd
))
1667 free (h8_get_memory_buf (sd
));
1668 if (h8_get_eightbit_buf (sd
))
1669 free (h8_get_eightbit_buf (sd
));
1671 h8_set_memory_buf (sd
, (unsigned char *)
1672 calloc (sizeof (char), memory_size
));
1673 sd
->memory_size
= memory_size
;
1674 h8_set_eightbit_buf (sd
, (unsigned char *) calloc (sizeof (char), 256));
1676 h8_set_mask (sd
, memory_size
- 1);
1678 memset (h8_get_reg_buf (sd
), 0, sizeof (((STATE_CPU (sd
, 0))->regs
)));
1680 for (i
= 0; i
< 8; i
++)
1682 /* FIXME: rewrite using local buffer. */
1683 unsigned char *p
= (unsigned char *) (h8_get_reg_buf (sd
) + i
);
1684 unsigned char *e
= (unsigned char *) (h8_get_reg_buf (sd
) + i
+ 1);
1685 unsigned short *q
= (unsigned short *) (h8_get_reg_buf (sd
) + i
);
1686 unsigned short *u
= (unsigned short *) (h8_get_reg_buf (sd
) + i
+ 1);
1687 h8_set_reg (sd
, i
, 0x00112233);
1702 wreg
[i
] = wreg
[i
+ 8] = 0;
1716 if (wreg
[i
] == 0 || wreg
[i
+ 8] == 0)
1717 sim_io_printf (sd
, "init_pointers: internal error.\n");
1719 h8_set_reg (sd
, i
, 0);
1722 init_pointers_needed
= 0;
1726 #define OBITOP(name, f, s, op) \
1727 case O (name, SB): \
1732 if (fetch (sd, &code->dst, &ea)) \
1734 if (fetch (sd, &code->src, &tmp)) \
1736 m = 1 << (tmp & 7); \
1739 if (store (sd, &code->dst,ea)) \
1745 step_once (SIM_DESC sd
, SIM_CPU
*cpu
)
1749 int tick_start
= get_now ();
1756 int c
, nz
, v
, n
, u
, h
, ui
, intMaskBit
;
1759 host_callback
*sim_callback
= STATE_CALLBACK (sd
);
1763 pc
= h8_get_pc (sd
);
1765 /* The PC should never be odd. */
1768 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGBUS
);
1772 /* Get Status Register (flags). */
1775 if (h8300smode
) /* Get exr. */
1777 trace
= (h8_get_exr (sd
) >> 7) & 1;
1778 intMask
= h8_get_exr (sd
) & 7;
1781 oldmask
= h8_get_mask (sd
);
1782 if (!h8300hmode
|| h8300_normal_mode
)
1783 h8_set_mask (sd
, 0xffff);
1786 decoded_inst _code
, *code
= &_code
;
1787 memset (code
, 0, sizeof (*code
));
1788 decode (sd
, pc
, h8_get_memory_buf (sd
) + pc
, code
);
1794 printf ("%x %d %s\n", pc
, code
->opcode
,
1795 code
->op
? code
->op
->name
: "**");
1797 h8_increment_stats (sd
, code
->opcode
);
1802 cycles
+= code
->cycles
;
1806 switch (code
->opcode
)
1808 case O (O_MOVAB
, SL
):
1809 case O (O_MOVAW
, SL
):
1810 case O (O_MOVAL
, SL
):
1811 /* 1) Evaluate 2nd argument (dst).
1812 2) Mask / zero extend according to whether 1st argument (src)
1813 is INDEXB, INDEXW, or INDEXL.
1814 3) Left-shift the result by 0, 1 or 2, according to size of mova
1815 (mova/b, mova/w, mova/l).
1816 4) Add literal value of 1st argument (src).
1817 5) Store result in 3rd argument (op3).
1820 /* Alas, since this is the only instruction with 3 arguments,
1821 decode doesn't handle them very well. Some fix-up is required.
1823 a) The size of dst is determined by whether src is
1824 INDEXB or INDEXW. */
1826 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
1827 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SB
);
1828 else if (OP_KIND (code
->src
.type
) == OP_INDEXW
)
1829 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SW
);
1831 /* b) If op3 == null, then this is the short form of the insn.
1832 Dst is the dispreg of src, and op3 is the 32-bit form
1833 of the same register.
1836 if (code
->op3
.type
== 0)
1838 /* Short form: src == INDEXB/INDEXW, dst == op3 == 0.
1839 We get to compose dst and op3 as follows:
1841 op3 is a 32-bit register, ID == src.reg.
1842 dst is the same register, but 8 or 16 bits
1843 depending on whether src is INDEXB or INDEXW.
1846 code
->op3
.type
= X (OP_REG
, SL
);
1847 code
->op3
.reg
= code
->src
.reg
;
1848 code
->op3
.literal
= 0;
1850 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
1852 code
->dst
.type
= X (OP_REG
, SB
);
1853 code
->dst
.reg
= code
->op3
.reg
+ 8;
1856 code
->dst
.type
= X (OP_REG
, SW
);
1859 if (fetch (sd
, &code
->dst
, &ea
))
1862 switch (OP_KIND (code
->src
.type
)) {
1863 case OP_INDEXB
: ea
= ea
& 0xff; break;
1864 case OP_INDEXW
: ea
= ea
& 0xffff; break;
1865 case OP_INDEXL
: break;
1866 default: goto illegal
;
1869 switch (code
->opcode
) {
1870 case O (O_MOVAB
, SL
): break;
1871 case O (O_MOVAW
, SL
): ea
= ea
<< 1; break;
1872 case O (O_MOVAL
, SL
): ea
= ea
<< 2; break;
1873 default: goto illegal
;
1876 ea
= ea
+ code
->src
.literal
;
1878 if (store (sd
, &code
->op3
, ea
))
1883 case O (O_SUBX
, SB
): /* subx, extended sub */
1884 if (fetch2 (sd
, &code
->dst
, &rd
))
1886 if (fetch (sd
, &code
->src
, &ea
))
1892 case O (O_SUBX
, SW
): /* subx, extended sub */
1893 if (fetch2 (sd
, &code
->dst
, &rd
))
1895 if (fetch (sd
, &code
->src
, &ea
))
1901 case O (O_SUBX
, SL
): /* subx, extended sub */
1902 if (fetch2 (sd
, &code
->dst
, &rd
))
1904 if (fetch (sd
, &code
->src
, &ea
))
1910 case O (O_ADDX
, SB
): /* addx, extended add */
1911 if (fetch2 (sd
, &code
->dst
, &rd
))
1913 if (fetch (sd
, &code
->src
, &ea
))
1919 case O (O_ADDX
, SW
): /* addx, extended add */
1920 if (fetch2 (sd
, &code
->dst
, &rd
))
1922 if (fetch (sd
, &code
->src
, &ea
))
1928 case O (O_ADDX
, SL
): /* addx, extended add */
1929 if (fetch2 (sd
, &code
->dst
, &rd
))
1931 if (fetch (sd
, &code
->src
, &ea
))
1937 case O (O_SUB
, SB
): /* sub.b */
1938 /* Fetch rd and ea. */
1939 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
1945 case O (O_SUB
, SW
): /* sub.w */
1946 /* Fetch rd and ea. */
1947 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
1953 case O (O_SUB
, SL
): /* sub.l */
1954 /* Fetch rd and ea. */
1955 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
1961 case O (O_NEG
, SB
): /* neg.b */
1963 if (fetch2 (sd
, &code
->src
, &ea
))
1970 case O (O_NEG
, SW
): /* neg.w */
1972 if (fetch2 (sd
, &code
->src
, &ea
))
1979 case O (O_NEG
, SL
): /* neg.l */
1981 if (fetch2 (sd
, &code
->src
, &ea
))
1988 case O (O_ADD
, SB
): /* add.b */
1989 if (fetch2 (sd
, &code
->dst
, &rd
))
1991 if (fetch (sd
, &code
->src
, &ea
))
1996 case O (O_ADD
, SW
): /* add.w */
1997 if (fetch2 (sd
, &code
->dst
, &rd
))
1999 if (fetch (sd
, &code
->src
, &ea
))
2004 case O (O_ADD
, SL
): /* add.l */
2005 if (fetch2 (sd
, &code
->dst
, &rd
))
2007 if (fetch (sd
, &code
->src
, &ea
))
2012 case O (O_AND
, SB
): /* and.b */
2013 /* Fetch rd and ea. */
2014 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2019 case O (O_AND
, SW
): /* and.w */
2020 /* Fetch rd and ea. */
2021 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2026 case O (O_AND
, SL
): /* and.l */
2027 /* Fetch rd and ea. */
2028 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2033 case O (O_OR
, SB
): /* or.b */
2034 /* Fetch rd and ea. */
2035 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2040 case O (O_OR
, SW
): /* or.w */
2041 /* Fetch rd and ea. */
2042 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2047 case O (O_OR
, SL
): /* or.l */
2048 /* Fetch rd and ea. */
2049 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2054 case O (O_XOR
, SB
): /* xor.b */
2055 /* Fetch rd and ea. */
2056 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2061 case O (O_XOR
, SW
): /* xor.w */
2062 /* Fetch rd and ea. */
2063 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2068 case O (O_XOR
, SL
): /* xor.l */
2069 /* Fetch rd and ea. */
2070 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2076 if (fetch (sd
, &code
->src
, &res
))
2078 if (store (sd
, &code
->dst
, res
))
2080 goto just_flags_log8
;
2082 if (fetch (sd
, &code
->src
, &res
))
2084 if (store (sd
, &code
->dst
, res
))
2086 goto just_flags_log16
;
2088 if (fetch (sd
, &code
->src
, &res
))
2090 if (store (sd
, &code
->dst
, res
))
2092 goto just_flags_log32
;
2094 case O (O_MOVMD
, SB
): /* movmd.b */
2101 rd
= GET_MEMORY_B (GET_L_REG (5));
2102 SET_MEMORY_B (GET_L_REG (6), rd
);
2103 SET_L_REG (5, GET_L_REG (5) + 1);
2104 SET_L_REG (6, GET_L_REG (6) + 1);
2109 case O (O_MOVMD
, SW
): /* movmd.w */
2116 rd
= GET_MEMORY_W (GET_L_REG (5));
2117 SET_MEMORY_W (GET_L_REG (6), rd
);
2118 SET_L_REG (5, GET_L_REG (5) + 2);
2119 SET_L_REG (6, GET_L_REG (6) + 2);
2124 case O (O_MOVMD
, SL
): /* movmd.l */
2131 rd
= GET_MEMORY_L (GET_L_REG (5));
2132 SET_MEMORY_L (GET_L_REG (6), rd
);
2133 SET_L_REG (5, GET_L_REG (5) + 4);
2134 SET_L_REG (6, GET_L_REG (6) + 4);
2139 case O (O_MOVSD
, SB
): /* movsd.b */
2140 /* This instruction implements strncpy, with a conditional branch.
2141 r4 contains n, r5 contains src, and r6 contains dst.
2142 The 16-bit displacement operand is added to the pc
2143 if and only if the end of string is reached before
2144 n bytes are transferred. */
2146 ea
= GET_L_REG (4) & 0xffff;
2152 rd
= GET_MEMORY_B (GET_L_REG (5));
2153 SET_MEMORY_B (GET_L_REG (6), rd
);
2154 SET_L_REG (5, GET_L_REG (5) + 1);
2155 SET_L_REG (6, GET_L_REG (6) + 1);
2162 case O (O_EEPMOV
, SB
): /* eepmov.b */
2163 case O (O_EEPMOV
, SW
): /* eepmov.w */
2164 if (h8300hmode
|| h8300smode
)
2166 register unsigned char *_src
, *_dst
;
2167 unsigned int count
= ((code
->opcode
== O (O_EEPMOV
, SW
))
2168 ? h8_get_reg (sd
, R4_REGNUM
) & 0xffff
2169 : h8_get_reg (sd
, R4_REGNUM
) & 0xff);
2171 _src
= (h8_get_reg (sd
, R5_REGNUM
) < memory_size
2172 ? h8_get_memory_buf (sd
) + h8_get_reg (sd
, R5_REGNUM
)
2173 : h8_get_eightbit_buf (sd
) +
2174 (h8_get_reg (sd
, R5_REGNUM
) & 0xff));
2175 if ((_src
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2177 if ((_src
+ count
) >= (h8_get_eightbit_buf (sd
) + 0x100))
2180 _dst
= (h8_get_reg (sd
, R6_REGNUM
) < memory_size
2181 ? h8_get_memory_buf (sd
) + h8_get_reg (sd
, R6_REGNUM
)
2182 : h8_get_eightbit_buf (sd
) +
2183 (h8_get_reg (sd
, R6_REGNUM
) & 0xff));
2185 if ((_dst
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2187 if ((_dst
+ count
) >= (h8_get_eightbit_buf (sd
) + 0x100))
2190 memcpy (_dst
, _src
, count
);
2192 h8_set_reg (sd
, R5_REGNUM
, h8_get_reg (sd
, R5_REGNUM
) + count
);
2193 h8_set_reg (sd
, R6_REGNUM
, h8_get_reg (sd
, R6_REGNUM
) + count
);
2194 h8_set_reg (sd
, R4_REGNUM
, h8_get_reg (sd
, R4_REGNUM
) &
2195 ((code
->opcode
== O (O_EEPMOV
, SW
))
2196 ? (~0xffff) : (~0xff)));
2197 cycles
+= 2 * count
;
2202 case O (O_ADDS
, SL
): /* adds (.l) */
2204 * This insn only uses register operands, but still
2205 * it would be cleaner to use fetch and store... */
2206 SET_L_REG (code
->dst
.reg
,
2207 GET_L_REG (code
->dst
.reg
)
2208 + code
->src
.literal
);
2212 case O (O_SUBS
, SL
): /* subs (.l) */
2214 * This insn only uses register operands, but still
2215 * it would be cleaner to use fetch and store... */
2216 SET_L_REG (code
->dst
.reg
,
2217 GET_L_REG (code
->dst
.reg
)
2218 - code
->src
.literal
);
2221 case O (O_CMP
, SB
): /* cmp.b */
2222 if (fetch (sd
, &code
->dst
, &rd
))
2224 if (fetch (sd
, &code
->src
, &ea
))
2228 goto just_flags_alu8
;
2230 case O (O_CMP
, SW
): /* cmp.w */
2231 if (fetch (sd
, &code
->dst
, &rd
))
2233 if (fetch (sd
, &code
->src
, &ea
))
2237 goto just_flags_alu16
;
2239 case O (O_CMP
, SL
): /* cmp.l */
2240 if (fetch (sd
, &code
->dst
, &rd
))
2242 if (fetch (sd
, &code
->src
, &ea
))
2246 goto just_flags_alu32
;
2248 case O (O_DEC
, SB
): /* dec.b */
2250 * This insn only uses register operands, but still
2251 * it would be cleaner to use fetch and store... */
2252 rd
= GET_B_REG (code
->src
.reg
);
2255 SET_B_REG (code
->src
.reg
, res
);
2256 goto just_flags_inc8
;
2258 case O (O_DEC
, SW
): /* dec.w */
2260 * This insn only uses register operands, but still
2261 * it would be cleaner to use fetch and store... */
2262 rd
= GET_W_REG (code
->dst
.reg
);
2263 ea
= -code
->src
.literal
;
2265 SET_W_REG (code
->dst
.reg
, res
);
2266 goto just_flags_inc16
;
2268 case O (O_DEC
, SL
): /* dec.l */
2270 * This insn only uses register operands, but still
2271 * it would be cleaner to use fetch and store... */
2272 rd
= GET_L_REG (code
->dst
.reg
);
2273 ea
= -code
->src
.literal
;
2275 SET_L_REG (code
->dst
.reg
, res
);
2276 goto just_flags_inc32
;
2278 case O (O_INC
, SB
): /* inc.b */
2280 * This insn only uses register operands, but still
2281 * it would be cleaner to use fetch and store... */
2282 rd
= GET_B_REG (code
->src
.reg
);
2285 SET_B_REG (code
->src
.reg
, res
);
2286 goto just_flags_inc8
;
2288 case O (O_INC
, SW
): /* inc.w */
2290 * This insn only uses register operands, but still
2291 * it would be cleaner to use fetch and store... */
2292 rd
= GET_W_REG (code
->dst
.reg
);
2293 ea
= code
->src
.literal
;
2295 SET_W_REG (code
->dst
.reg
, res
);
2296 goto just_flags_inc16
;
2298 case O (O_INC
, SL
): /* inc.l */
2300 * This insn only uses register operands, but still
2301 * it would be cleaner to use fetch and store... */
2302 rd
= GET_L_REG (code
->dst
.reg
);
2303 ea
= code
->src
.literal
;
2305 SET_L_REG (code
->dst
.reg
, res
);
2306 goto just_flags_inc32
;
2308 case O (O_LDC
, SB
): /* ldc.b */
2309 if (fetch (sd
, &code
->src
, &res
))
2313 case O (O_LDC
, SW
): /* ldc.w */
2314 if (fetch (sd
, &code
->src
, &res
))
2317 /* Word operand, value from MSB, must be shifted. */
2321 case O (O_LDC
, SL
): /* ldc.l */
2322 if (fetch (sd
, &code
->src
, &res
))
2324 switch (code
->dst
.type
) {
2325 case X (OP_SBR
, SL
):
2326 h8_set_sbr (sd
, res
);
2328 case X (OP_VBR
, SL
):
2329 h8_set_vbr (sd
, res
);
2336 case O (O_STC
, SW
): /* stc.w */
2337 case O (O_STC
, SB
): /* stc.b */
2338 if (code
->src
.type
== X (OP_CCR
, SB
))
2341 res
= h8_get_ccr (sd
);
2343 else if (code
->src
.type
== X (OP_EXR
, SB
) && h8300smode
)
2346 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2347 res
= h8_get_exr (sd
);
2352 /* Word operand, value to MSB, must be shifted. */
2353 if (code
->opcode
== X (O_STC
, SW
))
2355 if (store (sd
, &code
->dst
, res
))
2358 case O (O_STC
, SL
): /* stc.l */
2359 switch (code
->src
.type
) {
2360 case X (OP_SBR
, SL
):
2361 res
= h8_get_sbr (sd
);
2363 case X (OP_VBR
, SL
):
2364 res
= h8_get_vbr (sd
);
2369 if (store (sd
, &code
->dst
, res
))
2373 case O (O_ANDC
, SB
): /* andc.b */
2374 if (code
->dst
.type
== X (OP_CCR
, SB
))
2377 rd
= h8_get_ccr (sd
);
2379 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2382 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2383 rd
= h8_get_exr (sd
);
2387 ea
= code
->src
.literal
;
2391 case O (O_ORC
, SB
): /* orc.b */
2392 if (code
->dst
.type
== X (OP_CCR
, SB
))
2395 rd
= h8_get_ccr (sd
);
2397 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2400 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2401 rd
= h8_get_exr (sd
);
2405 ea
= code
->src
.literal
;
2409 case O (O_XORC
, SB
): /* xorc.b */
2410 if (code
->dst
.type
== X (OP_CCR
, SB
))
2413 rd
= h8_get_ccr (sd
);
2415 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2418 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2419 rd
= h8_get_exr (sd
);
2423 ea
= code
->src
.literal
;
2427 case O (O_BRAS
, SB
): /* bra/s */
2428 /* This is basically an ordinary branch, with a delay slot. */
2429 if (fetch (sd
, &code
->src
, &res
))
2437 /* Execution continues at next instruction, but
2438 delayed_branch is set up for next cycle. */
2439 h8_set_delayed_branch (sd
, code
->next_pc
+ res
);
2443 case O (O_BRAB
, SB
): /* bra rd.b */
2444 case O (O_BRAW
, SW
): /* bra rd.w */
2445 case O (O_BRAL
, SL
): /* bra erd.l */
2446 if (fetch (sd
, &code
->src
, &rd
))
2448 switch (OP_SIZE (code
->opcode
)) {
2449 case SB
: rd
&= 0xff; break;
2450 case SW
: rd
&= 0xffff; break;
2451 case SL
: rd
&= 0xffffffff; break;
2453 pc
= code
->next_pc
+ rd
;
2456 case O (O_BRABC
, SB
): /* bra/bc, branch if bit clear */
2457 case O (O_BRABS
, SB
): /* bra/bs, branch if bit set */
2458 case O (O_BSRBC
, SB
): /* bsr/bc, call if bit clear */
2459 case O (O_BSRBS
, SB
): /* bsr/bs, call if bit set */
2460 if (fetch (sd
, &code
->dst
, &rd
) ||
2461 fetch (sd
, &code
->src
, &bit
))
2464 if (code
->opcode
== O (O_BRABC
, SB
) || /* branch if clear */
2465 code
->opcode
== O (O_BSRBC
, SB
)) /* call if clear */
2467 if ((rd
& (1 << bit
))) /* no branch */
2470 else /* branch/call if set */
2472 if (!(rd
& (1 << bit
))) /* no branch */
2476 if (fetch (sd
, &code
->op3
, &res
)) /* branch */
2478 pc
= code
->next_pc
+ res
;
2480 if (code
->opcode
== O (O_BRABC
, SB
) ||
2481 code
->opcode
== O (O_BRABS
, SB
)) /* branch */
2489 case O (O_BRA
, SB
): /* bra, branch always */
2494 case O (O_BRN
, SB
): /* brn, ;-/ branch never? */
2499 case O (O_BHI
, SB
): /* bhi */
2505 case O (O_BLS
, SB
): /* bls */
2510 case O (O_BCS
, SB
): /* bcs, branch if carry set */
2515 case O (O_BCC
, SB
): /* bcc, branch if carry clear */
2520 case O (O_BEQ
, SB
): /* beq, branch if zero set */
2524 case O (O_BGT
, SB
): /* bgt */
2525 if (((Z
|| (N
^ V
)) == 0))
2529 case O (O_BLE
, SB
): /* ble */
2530 if (((Z
|| (N
^ V
)) == 1))
2534 case O (O_BGE
, SB
): /* bge */
2538 case O (O_BLT
, SB
): /* blt */
2542 case O (O_BMI
, SB
): /* bmi */
2546 case O (O_BNE
, SB
): /* bne, branch if zero clear */
2551 case O (O_BPL
, SB
): /* bpl */
2555 case O (O_BVC
, SB
): /* bvc */
2559 case O (O_BVS
, SB
): /* bvs */
2564 /* Trap for Command Line setup. */
2565 case O (O_SYS_CMDLINE
, SB
):
2567 int i
= 0; /* Loop counter. */
2568 int j
= 0; /* Loop counter. */
2569 int ind_arg_len
= 0; /* Length of each argument. */
2570 int no_of_args
= 0; /* The no. or cmdline args. */
2571 int current_location
= 0; /* Location of string. */
2572 int old_sp
= 0; /* The Initial Stack Pointer. */
2573 int no_of_slots
= 0; /* No. of slots required on the stack
2574 for storing cmdline args. */
2575 int sp_move
= 0; /* No. of locations by which the stack needs
2577 int new_sp
= 0; /* The final stack pointer location passed
2579 int *argv_ptrs
; /* Pointers of argv strings to be stored. */
2580 int argv_ptrs_location
= 0; /* Location of pointers to cmdline
2581 args on the stack. */
2582 int char_ptr_size
= 0; /* Size of a character pointer on
2584 int addr_cmdline
= 0; /* Memory location where cmdline has
2586 int size_cmdline
= 0; /* Size of cmdline. */
2588 /* Set the address of 256 free locations where command line is
2590 addr_cmdline
= cmdline_location();
2591 h8_set_reg (sd
, 0, addr_cmdline
);
2593 /* Counting the no. of commandline arguments. */
2594 for (i
= 0; h8_get_cmdline_arg (sd
, i
) != NULL
; i
++)
2597 /* No. of arguments in the command line. */
2600 /* Current location is just a temporary variable,which we are
2601 setting to the point to the start of our commandline string. */
2602 current_location
= addr_cmdline
;
2604 /* Allocating space for storing pointers of the command line
2606 argv_ptrs
= (int *) malloc (sizeof (int) * no_of_args
);
2608 /* Setting char_ptr_size to the sizeof (char *) on the different
2610 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2619 for (i
= 0; i
< no_of_args
; i
++)
2623 /* The size of the commandline argument. */
2624 ind_arg_len
= strlen (h8_get_cmdline_arg (sd
, i
)) + 1;
2626 /* The total size of the command line string. */
2627 size_cmdline
+= ind_arg_len
;
2629 /* As we have only 256 bytes, we need to provide a graceful
2630 exit. Anyways, a program using command line arguments
2631 where we cannot store all the command line arguments
2632 given may behave unpredictably. */
2633 if (size_cmdline
>= 256)
2635 h8_set_reg (sd
, 0, 0);
2640 /* current_location points to the memory where the next
2641 commandline argument is stored. */
2642 argv_ptrs
[i
] = current_location
;
2643 for (j
= 0; j
< ind_arg_len
; j
++)
2645 SET_MEMORY_B ((current_location
+
2646 (sizeof (char) * j
)),
2647 *(h8_get_cmdline_arg (sd
, i
) +
2648 sizeof (char) * j
));
2651 /* Setting current_location to the starting of next
2653 current_location
+= ind_arg_len
;
2657 /* This is the original position of the stack pointer. */
2658 old_sp
= h8_get_reg (sd
, SP_REGNUM
);
2660 /* We need space from the stack to store the pointers to argvs. */
2661 /* As we will infringe on the stack, we need to shift the stack
2662 pointer so that the data is not overwritten. We calculate how
2663 much space is required. */
2664 sp_move
= (no_of_args
) * (char_ptr_size
);
2666 /* The final position of stack pointer, we have thus taken some
2667 space from the stack. */
2668 new_sp
= old_sp
- sp_move
;
2670 /* Temporary variable holding value where the argv pointers need
2672 argv_ptrs_location
= new_sp
;
2674 /* The argv pointers are stored at sequential locations. As per
2676 for (i
= 0; i
< no_of_args
; i
++)
2678 /* Saving the argv pointer. */
2679 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2681 SET_MEMORY_L (argv_ptrs_location
, argv_ptrs
[i
]);
2685 SET_MEMORY_W (argv_ptrs_location
, argv_ptrs
[i
]);
2688 /* The next location where the pointer to the next argv
2689 string has to be stored. */
2690 argv_ptrs_location
+= char_ptr_size
;
2693 /* Required by POSIX, Setting 0x0 at the end of the list of argv
2695 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2697 SET_MEMORY_L (old_sp
, 0x0);
2701 SET_MEMORY_W (old_sp
, 0x0);
2704 /* Freeing allocated memory. */
2706 for (i
= 0; i
<= no_of_args
; i
++)
2708 free (h8_get_cmdline_arg (sd
, i
));
2710 free (h8_get_command_line (sd
));
2712 /* The no. of argv arguments are returned in Reg 0. */
2713 h8_set_reg (sd
, 0, no_of_args
);
2714 /* The Pointer to argv in Register 1. */
2715 h8_set_reg (sd
, 1, new_sp
);
2716 /* Setting the stack pointer to the new value. */
2717 h8_set_reg (sd
, SP_REGNUM
, new_sp
);
2721 /* System call processing starts. */
2722 case O (O_SYS_OPEN
, SB
):
2724 int len
= 0; /* Length of filename. */
2725 char *filename
; /* Filename would go here. */
2726 char temp_char
; /* Temporary character */
2727 int mode
= 0; /* Mode bits for the file. */
2728 int open_return
; /* Return value of open, file descriptor. */
2729 int i
; /* Loop counter */
2730 int filename_ptr
; /* Pointer to filename in cpu memory. */
2732 /* Setting filename_ptr to first argument of open, */
2733 /* and trying to get mode. */
2734 if ((h8300sxmode
|| h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2736 filename_ptr
= GET_L_REG (0);
2737 mode
= GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
) + 4);
2741 filename_ptr
= GET_W_REG (0);
2742 mode
= GET_MEMORY_W (h8_get_reg (sd
, SP_REGNUM
) + 2);
2745 /* Trying to find the length of the filename. */
2746 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
2749 while (temp_char
!= '\0')
2751 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
2755 /* Allocating space for the filename. */
2756 filename
= (char *) malloc (sizeof (char) * len
);
2758 /* String copying the filename from memory. */
2759 for (i
= 0; i
< len
; i
++)
2761 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
2762 filename
[i
] = temp_char
;
2765 /* Callback to open and return the file descriptor. */
2766 open_return
= sim_callback
->open (sim_callback
, filename
, mode
);
2768 /* Return value in register 0. */
2769 h8_set_reg (sd
, 0, open_return
);
2771 /* Freeing memory used for filename. */
2776 case O (O_SYS_READ
, SB
):
2778 char *char_ptr
; /* Where characters read would be stored. */
2779 int fd
; /* File descriptor */
2780 int buf_size
; /* BUF_SIZE parameter in read. */
2781 int i
= 0; /* Temporary Loop counter */
2782 int read_return
= 0; /* Return value from callback to
2785 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2786 buf_size
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2788 char_ptr
= (char *) malloc (sizeof (char) * buf_size
);
2790 /* Callback to read and return the no. of characters read. */
2792 sim_callback
->read (sim_callback
, fd
, char_ptr
, buf_size
);
2794 /* The characters read are stored in cpu memory. */
2795 for (i
= 0; i
< buf_size
; i
++)
2797 SET_MEMORY_B ((h8_get_reg (sd
, 1) + (sizeof (char) * i
)),
2798 *(char_ptr
+ (sizeof (char) * i
)));
2801 /* Return value in Register 0. */
2802 h8_set_reg (sd
, 0, read_return
);
2804 /* Freeing memory used as buffer. */
2809 case O (O_SYS_WRITE
, SB
):
2811 int fd
; /* File descriptor */
2812 char temp_char
; /* Temporary character */
2813 int len
; /* Length of write, Parameter II to write. */
2814 int char_ptr
; /* Character Pointer, Parameter I of write. */
2815 char *ptr
; /* Where characters to be written are stored.
2817 int write_return
; /* Return value from callback to write. */
2818 int i
= 0; /* Loop counter */
2820 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2821 char_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2822 len
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2824 /* Allocating space for the characters to be written. */
2825 ptr
= (char *) malloc (sizeof (char) * len
);
2827 /* Fetching the characters from cpu memory. */
2828 for (i
= 0; i
< len
; i
++)
2830 temp_char
= GET_MEMORY_B (char_ptr
+ i
);
2834 /* Callback write and return the no. of characters written. */
2835 write_return
= sim_callback
->write (sim_callback
, fd
, ptr
, len
);
2837 /* Return value in Register 0. */
2838 h8_set_reg (sd
, 0, write_return
);
2840 /* Freeing memory used as buffer. */
2845 case O (O_SYS_LSEEK
, SB
):
2847 int fd
; /* File descriptor */
2848 int offset
; /* Offset */
2849 int origin
; /* Origin */
2850 int lseek_return
; /* Return value from callback to lseek. */
2852 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2853 offset
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2854 origin
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2856 /* Callback lseek and return offset. */
2858 sim_callback
->lseek (sim_callback
, fd
, offset
, origin
);
2860 /* Return value in register 0. */
2861 h8_set_reg (sd
, 0, lseek_return
);
2865 case O (O_SYS_CLOSE
, SB
):
2867 int fd
; /* File descriptor */
2868 int close_return
; /* Return value from callback to close. */
2870 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2872 /* Callback close and return. */
2873 close_return
= sim_callback
->close (sim_callback
, fd
);
2875 /* Return value in register 0. */
2876 h8_set_reg (sd
, 0, close_return
);
2880 case O (O_SYS_FSTAT
, SB
):
2882 int fd
; /* File descriptor */
2883 struct stat stat_rec
; /* Stat record */
2884 int fstat_return
; /* Return value from callback to stat. */
2885 int stat_ptr
; /* Pointer to stat record. */
2886 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
2888 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2890 /* Setting stat_ptr to second argument of stat. */
2891 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2893 /* Callback stat and return. */
2894 fstat_return
= sim_callback
->to_fstat (sim_callback
, fd
,
2897 /* Have stat_ptr point to starting of stat_rec. */
2898 temp_stat_ptr
= (char *) (&stat_rec
);
2900 /* Setting up the stat structure returned. */
2901 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
2903 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
2905 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
2907 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
2909 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
2911 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
2913 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
2915 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
2917 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
2919 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
2921 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
2923 /* Return value in register 0. */
2924 h8_set_reg (sd
, 0, fstat_return
);
2928 case O (O_SYS_STAT
, SB
):
2930 int len
= 0; /* Length of filename. */
2931 char *filename
; /* Filename would go here. */
2932 char temp_char
; /* Temporary character */
2933 int filename_ptr
; /* Pointer to filename in cpu memory. */
2934 struct stat stat_rec
; /* Stat record */
2935 int stat_return
; /* Return value from callback to stat */
2936 int stat_ptr
; /* Pointer to stat record. */
2937 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
2938 int i
= 0; /* Loop Counter */
2940 /* Setting filename_ptr to first argument of open. */
2941 filename_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2943 /* Trying to find the length of the filename. */
2944 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
2947 while (temp_char
!= '\0')
2949 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
2953 /* Allocating space for the filename. */
2954 filename
= (char *) malloc (sizeof (char) * len
);
2956 /* String copying the filename from memory. */
2957 for (i
= 0; i
< len
; i
++)
2959 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
2960 filename
[i
] = temp_char
;
2963 /* Setting stat_ptr to second argument of stat. */
2964 /* stat_ptr = h8_get_reg (sd, 1); */
2965 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2967 /* Callback stat and return. */
2969 sim_callback
->to_stat (sim_callback
, filename
, &stat_rec
);
2971 /* Have stat_ptr point to starting of stat_rec. */
2972 temp_stat_ptr
= (char *) (&stat_rec
);
2974 /* Freeing memory used for filename. */
2977 /* Setting up the stat structure returned. */
2978 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
2980 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
2982 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
2984 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
2986 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
2988 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
2990 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
2992 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
2994 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
2996 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
2998 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
3000 /* Return value in register 0. */
3001 h8_set_reg (sd
, 0, stat_return
);
3004 /* End of system call processing. */
3006 case O (O_NOT
, SB
): /* not.b */
3007 if (fetch2 (sd
, &code
->src
, &rd
))
3013 case O (O_NOT
, SW
): /* not.w */
3014 if (fetch2 (sd
, &code
->src
, &rd
))
3020 case O (O_NOT
, SL
): /* not.l */
3021 if (fetch2 (sd
, &code
->src
, &rd
))
3027 case O (O_SHLL
, SB
): /* shll.b */
3028 case O (O_SHLR
, SB
): /* shlr.b */
3029 if (fetch2 (sd
, &code
->dst
, &rd
))
3032 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3033 ea
= 1; /* unary op */
3034 else /* binary op */
3035 fetch (sd
, &code
->src
, &ea
);
3037 if (code
->opcode
== O (O_SHLL
, SB
))
3040 c
= rd
& (0x80 >> (ea
- 1));
3046 c
= rd
& (1 << (ea
- 1));
3047 rd
= (unsigned char) rd
>> ea
;
3051 case O (O_SHLL
, SW
): /* shll.w */
3052 case O (O_SHLR
, SW
): /* shlr.w */
3053 if (fetch2 (sd
, &code
->dst
, &rd
))
3056 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3057 ea
= 1; /* unary op */
3059 fetch (sd
, &code
->src
, &ea
);
3061 if (code
->opcode
== O (O_SHLL
, SW
))
3064 c
= rd
& (0x8000 >> (ea
- 1));
3070 c
= rd
& (1 << (ea
- 1));
3071 rd
= (unsigned short) rd
>> ea
;
3075 case O (O_SHLL
, SL
): /* shll.l */
3076 case O (O_SHLR
, SL
): /* shlr.l */
3077 if (fetch2 (sd
, &code
->dst
, &rd
))
3080 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3081 ea
= 1; /* unary op */
3083 fetch (sd
, &code
->src
, &ea
);
3085 if (code
->opcode
== O (O_SHLL
, SL
))
3088 c
= rd
& (0x80000000 >> (ea
- 1));
3094 c
= rd
& (1 << (ea
- 1));
3095 rd
= (unsigned int) rd
>> ea
;
3099 case O (O_SHAL
, SB
):
3100 case O (O_SHAR
, SB
):
3101 if (fetch2 (sd
, &code
->dst
, &rd
))
3104 if (code
->src
.type
== X (OP_IMM
, SB
))
3105 fetch (sd
, &code
->src
, &ea
);
3109 if (code
->opcode
== O (O_SHAL
, SB
))
3111 c
= rd
& (0x80 >> (ea
- 1));
3112 res
= rd
>> (7 - ea
);
3113 v
= ((res
& 1) && !(res
& 2))
3114 || (!(res
& 1) && (res
& 2));
3119 c
= rd
& (1 << (ea
- 1));
3121 rd
= ((signed char) rd
) >> ea
;
3125 case O (O_SHAL
, SW
):
3126 case O (O_SHAR
, SW
):
3127 if (fetch2 (sd
, &code
->dst
, &rd
))
3130 if (code
->src
.type
== X (OP_IMM
, SW
))
3131 fetch (sd
, &code
->src
, &ea
);
3135 if (code
->opcode
== O (O_SHAL
, SW
))
3137 c
= rd
& (0x8000 >> (ea
- 1));
3138 res
= rd
>> (15 - ea
);
3139 v
= ((res
& 1) && !(res
& 2))
3140 || (!(res
& 1) && (res
& 2));
3145 c
= rd
& (1 << (ea
- 1));
3147 rd
= ((signed short) rd
) >> ea
;
3151 case O (O_SHAL
, SL
):
3152 case O (O_SHAR
, SL
):
3153 if (fetch2 (sd
, &code
->dst
, &rd
))
3156 if (code
->src
.type
== X (OP_IMM
, SL
))
3157 fetch (sd
, &code
->src
, &ea
);
3161 if (code
->opcode
== O (O_SHAL
, SL
))
3163 c
= rd
& (0x80000000 >> (ea
- 1));
3164 res
= rd
>> (31 - ea
);
3165 v
= ((res
& 1) && !(res
& 2))
3166 || (!(res
& 1) && (res
& 2));
3171 c
= rd
& (1 << (ea
- 1));
3173 rd
= ((signed int) rd
) >> ea
;
3177 case O (O_ROTL
, SB
):
3178 case O (O_ROTR
, SB
):
3179 if (fetch2 (sd
, &code
->dst
, &rd
))
3182 if (code
->src
.type
== X (OP_IMM
, SB
))
3183 fetch (sd
, &code
->src
, &ea
);
3188 if (code
->opcode
== O (O_ROTL
, SB
))
3198 rd
= ((unsigned char) rd
) >> 1;
3206 case O (O_ROTL
, SW
):
3207 case O (O_ROTR
, SW
):
3208 if (fetch2 (sd
, &code
->dst
, &rd
))
3211 if (code
->src
.type
== X (OP_IMM
, SW
))
3212 fetch (sd
, &code
->src
, &ea
);
3217 if (code
->opcode
== O (O_ROTL
, SW
))
3227 rd
= ((unsigned short) rd
) >> 1;
3235 case O (O_ROTL
, SL
):
3236 case O (O_ROTR
, SL
):
3237 if (fetch2 (sd
, &code
->dst
, &rd
))
3240 if (code
->src
.type
== X (OP_IMM
, SL
))
3241 fetch (sd
, &code
->src
, &ea
);
3246 if (code
->opcode
== O (O_ROTL
, SL
))
3248 c
= rd
& 0x80000000;
3256 rd
= ((unsigned int) rd
) >> 1;
3264 case O (O_ROTXL
, SB
):
3265 case O (O_ROTXR
, SB
):
3266 if (fetch2 (sd
, &code
->dst
, &rd
))
3269 if (code
->src
.type
== X (OP_IMM
, SB
))
3270 fetch (sd
, &code
->src
, &ea
);
3275 if (code
->opcode
== O (O_ROTXL
, SB
))
3286 rd
= ((unsigned char) rd
) >> 1;
3295 case O (O_ROTXL
, SW
):
3296 case O (O_ROTXR
, SW
):
3297 if (fetch2 (sd
, &code
->dst
, &rd
))
3300 if (code
->src
.type
== X (OP_IMM
, SW
))
3301 fetch (sd
, &code
->src
, &ea
);
3306 if (code
->opcode
== O (O_ROTXL
, SW
))
3317 rd
= ((unsigned short) rd
) >> 1;
3326 case O (O_ROTXL
, SL
):
3327 case O (O_ROTXR
, SL
):
3328 if (fetch2 (sd
, &code
->dst
, &rd
))
3331 if (code
->src
.type
== X (OP_IMM
, SL
))
3332 fetch (sd
, &code
->src
, &ea
);
3337 if (code
->opcode
== O (O_ROTXL
, SL
))
3339 res
= rd
& 0x80000000;
3348 rd
= ((unsigned int) rd
) >> 1;
3359 case O (O_JMP
, SB
): /* jmp */
3361 fetch (sd
, &code
->src
, &pc
);
3366 case O (O_JSR
, SB
): /* jsr, jump to subroutine */
3368 if (fetch (sd
, &code
->src
, &pc
))
3371 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3373 if (h8300hmode
&& !h8300_normal_mode
)
3376 SET_MEMORY_L (tmp
, code
->next_pc
);
3381 SET_MEMORY_W (tmp
, code
->next_pc
);
3383 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3389 case O (O_BSR
, SB
): /* bsr, branch to subroutine */
3390 if (fetch (sd
, &code
->src
, &res
))
3392 pc
= code
->next_pc
+ res
;
3395 case O (O_RTE
, SN
): /* rte, return from exception */
3397 /* Pops exr and ccr before pc -- otherwise identical to rts. */
3398 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3400 if (h8300smode
) /* pop exr */
3402 h8_set_exr (sd
, GET_MEMORY_L (tmp
));
3405 if (h8300hmode
&& !h8300_normal_mode
)
3407 h8_set_ccr (sd
, GET_MEMORY_L (tmp
));
3409 pc
= GET_MEMORY_L (tmp
);
3414 h8_set_ccr (sd
, GET_MEMORY_W (tmp
));
3416 pc
= GET_MEMORY_W (tmp
);
3421 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3424 case O (O_RTS
, SN
): /* rts, return from subroutine */
3426 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3428 if (h8300hmode
&& !h8300_normal_mode
)
3430 pc
= GET_MEMORY_L (tmp
);
3435 pc
= GET_MEMORY_W (tmp
);
3439 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3442 case O (O_ILL
, SB
): /* illegal */
3443 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
3446 case O (O_SLEEP
, SN
): /* sleep */
3447 /* Check for magic numbers in r1 and r2. */
3448 if ((h8_get_reg (sd
, R1_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC1
&&
3449 (h8_get_reg (sd
, R2_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC2
&&
3450 SIM_WIFEXITED (h8_get_reg (sd
, 0)))
3452 /* This trap comes from _exit, not from gdb. */
3453 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_exited
,
3454 SIM_WEXITSTATUS (h8_get_reg (sd
, 0)));
3457 /* Unfortunately this won't really work, because
3458 when we take a breakpoint trap, R0 has a "random",
3459 user-defined value. Don't see any immediate solution. */
3460 else if (SIM_WIFSTOPPED (h8_get_reg (sd
, 0)))
3462 /* Pass the stop signal up to gdb. */
3463 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
,
3464 SIM_WSTOPSIG (h8_get_reg (sd
, 0)));
3469 /* Treat it as a sigtrap. */
3470 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGTRAP
);
3474 case O (O_TRAPA
, SB
): /* trapa */
3475 if (fetch (sd
, &code
->src
, &res
))
3476 goto end
; /* res is vector number. */
3478 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3479 if(h8300_normal_mode
)
3482 SET_MEMORY_W (tmp
, code
->next_pc
);
3484 SET_MEMORY_W (tmp
, h8_get_ccr (sd
));
3489 SET_MEMORY_L (tmp
, code
->next_pc
);
3491 SET_MEMORY_L (tmp
, h8_get_ccr (sd
));
3499 SET_MEMORY_L (tmp
, h8_get_exr (sd
));
3502 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3504 if(h8300_normal_mode
)
3505 pc
= GET_MEMORY_L (0x10 + res
* 2); /* Vector addresses are 0x10,0x12,0x14 and 0x16 */
3507 pc
= GET_MEMORY_L (0x20 + res
* 4);
3511 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGTRAP
);
3514 case O (O_BSETEQ
, SB
):
3519 case O (O_BSETNE
, SB
):
3524 case O (O_BCLREQ
, SB
):
3529 case O (O_BCLRNE
, SB
):
3534 OBITOP (O_BNOT
, 1, 1, ea
^= m
); /* bnot */
3535 OBITOP (O_BTST
, 1, 0, nz
= ea
& m
); /* btst */
3537 OBITOP (O_BSET
, 1, 1, ea
|= m
); /* bset */
3539 OBITOP (O_BCLR
, 1, 1, ea
&= ~m
); /* bclr */
3540 OBITOP (O_BLD
, 1, 0, c
= ea
& m
); /* bld */
3541 OBITOP (O_BILD
, 1, 0, c
= !(ea
& m
)); /* bild */
3542 OBITOP (O_BST
, 1, 1, ea
&= ~m
;
3543 if (C
) ea
|= m
); /* bst */
3544 OBITOP (O_BIST
, 1, 1, ea
&= ~m
;
3545 if (!C
) ea
|= m
); /* bist */
3546 OBITOP (O_BSTZ
, 1, 1, ea
&= ~m
;
3547 if (Z
) ea
|= m
); /* bstz */
3548 OBITOP (O_BISTZ
, 1, 1, ea
&= ~m
;
3549 if (!Z
) ea
|= m
); /* bistz */
3550 OBITOP (O_BAND
, 1, 0, c
= (ea
& m
) && C
); /* band */
3551 OBITOP (O_BIAND
, 1, 0, c
= !(ea
& m
) && C
); /* biand */
3552 OBITOP (O_BOR
, 1, 0, c
= (ea
& m
) || C
); /* bor */
3553 OBITOP (O_BIOR
, 1, 0, c
= !(ea
& m
) || C
); /* bior */
3554 OBITOP (O_BXOR
, 1, 0, c
= ((ea
& m
) != 0)!= C
); /* bxor */
3555 OBITOP (O_BIXOR
, 1, 0, c
= !(ea
& m
) != C
); /* bixor */
3557 case O (O_BFLD
, SB
): /* bfld */
3560 if (fetch (sd
, &code
->src
, &bit
))
3565 if (fetch (sd
, &code
->dst
, &ea
))
3575 if (store (sd
, &code
->op3
, ea
))
3580 case O(O_BFST
, SB
): /* bfst */
3581 /* bitfield store */
3582 /* NOTE: the imm8 value is in dst, and the ea value
3583 (which is actually the destination) is in op3.
3584 It has to be that way, to avoid breaking the assembler. */
3586 if (fetch (sd
, &code
->dst
, &bit
)) /* imm8 */
3588 if (bit
== 0) /* noop -- nothing to do. */
3591 if (fetch (sd
, &code
->src
, &rd
)) /* reg8 src */
3594 if (fetch2 (sd
, &code
->op3
, &ea
)) /* ea dst */
3597 /* Left-shift the register data into position. */
3598 for (tmp
= bit
; !(tmp
& 1); tmp
>>= 1)
3601 /* Combine it with the neighboring bits. */
3602 ea
= (ea
& ~bit
) | (rd
& bit
);
3605 if (store2 (sd
, &code
->op3
, ea
))
3609 case O (O_CLRMAC
, SN
): /* clrmac */
3610 h8_set_mach (sd
, 0);
3611 h8_set_macl (sd
, 0);
3612 h8_set_macZ (sd
, 1);
3613 h8_set_macV (sd
, 0);
3614 h8_set_macN (sd
, 0);
3617 case O (O_STMAC
, SL
): /* stmac, 260 */
3618 switch (code
->src
.type
) {
3619 case X (OP_MACH
, SL
):
3620 res
= h8_get_mach (sd
);
3621 if (res
& 0x200) /* sign extend */
3624 case X (OP_MACL
, SL
):
3625 res
= h8_get_macl (sd
);
3627 default: goto illegal
;
3629 nz
= !h8_get_macZ (sd
);
3630 n
= h8_get_macN (sd
);
3631 v
= h8_get_macV (sd
);
3633 if (store (sd
, &code
->dst
, res
))
3638 case O (O_LDMAC
, SL
): /* ldmac, 179 */
3639 if (fetch (sd
, &code
->src
, &rd
))
3642 switch (code
->dst
.type
) {
3643 case X (OP_MACH
, SL
):
3644 rd
&= 0x3ff; /* Truncate to 10 bits */
3645 h8_set_mach (sd
, rd
);
3647 case X (OP_MACL
, SL
):
3648 h8_set_macl (sd
, rd
);
3650 default: goto illegal
;
3652 h8_set_macV (sd
, 0);
3656 if (fetch (sd
, &code
->src
, &rd
) ||
3657 fetch (sd
, &code
->dst
, &res
))
3660 /* Ye gods, this is non-portable!
3661 However, the existing mul/div code is similar. */
3662 res
= SEXTSHORT (res
) * SEXTSHORT (rd
);
3664 if (h8_get_macS (sd
)) /* Saturating mode */
3666 long long mac
= h8_get_macl (sd
);
3668 if (mac
& 0x80000000) /* sign extend */
3669 mac
|= 0xffffffff00000000LL
;
3672 if (mac
> 0x7fffffff || mac
< 0xffffffff80000000LL
)
3673 h8_set_macV (sd
, 1);
3674 h8_set_macZ (sd
, (mac
== 0));
3675 h8_set_macN (sd
, (mac
< 0));
3676 h8_set_macl (sd
, (int) mac
);
3678 else /* "Less Saturating" mode */
3680 long long mac
= h8_get_mach (sd
);
3682 mac
+= h8_get_macl (sd
);
3684 if (mac
& 0x20000000000LL
) /* sign extend */
3685 mac
|= 0xfffffc0000000000LL
;
3688 if (mac
> 0x1ffffffffffLL
||
3689 mac
< (long long) 0xfffffe0000000000LL
)
3690 h8_set_macV (sd
, 1);
3691 h8_set_macZ (sd
, (mac
== 0));
3692 h8_set_macN (sd
, (mac
< 0));
3693 h8_set_macl (sd
, (int) mac
);
3695 h8_set_mach (sd
, (int) (mac
& 0x3ff));
3699 case O (O_MULS
, SW
): /* muls.w */
3700 if (fetch (sd
, &code
->src
, &ea
) ||
3701 fetch (sd
, &code
->dst
, &rd
))
3704 ea
= SEXTSHORT (ea
);
3705 res
= SEXTSHORT (ea
* SEXTSHORT (rd
));
3709 if (store (sd
, &code
->dst
, res
))
3714 case O (O_MULS
, SL
): /* muls.l */
3715 if (fetch (sd
, &code
->src
, &ea
) ||
3716 fetch (sd
, &code
->dst
, &rd
))
3721 n
= res
& 0x80000000;
3722 nz
= res
& 0xffffffff;
3723 if (store (sd
, &code
->dst
, res
))
3727 case O (O_MULSU
, SL
): /* muls/u.l */
3728 if (fetch (sd
, &code
->src
, &ea
) ||
3729 fetch (sd
, &code
->dst
, &rd
))
3732 /* Compute upper 32 bits of the 64-bit result. */
3733 res
= (((long long) ea
) * ((long long) rd
)) >> 32;
3735 n
= res
& 0x80000000;
3736 nz
= res
& 0xffffffff;
3737 if (store (sd
, &code
->dst
, res
))
3741 case O (O_MULU
, SW
): /* mulu.w */
3742 if (fetch (sd
, &code
->src
, &ea
) ||
3743 fetch (sd
, &code
->dst
, &rd
))
3746 res
= UEXTSHORT ((UEXTSHORT (ea
) * UEXTSHORT (rd
)));
3748 /* Don't set Z or N. */
3749 if (store (sd
, &code
->dst
, res
))
3754 case O (O_MULU
, SL
): /* mulu.l */
3755 if (fetch (sd
, &code
->src
, &ea
) ||
3756 fetch (sd
, &code
->dst
, &rd
))
3761 /* Don't set Z or N. */
3762 if (store (sd
, &code
->dst
, res
))
3767 case O (O_MULUU
, SL
): /* mulu/u.l */
3768 if (fetch (sd
, &code
->src
, &ea
) ||
3769 fetch (sd
, &code
->dst
, &rd
))
3772 /* Compute upper 32 bits of the 64-bit result. */
3773 res
= (((unsigned long long) (unsigned) ea
) *
3774 ((unsigned long long) (unsigned) rd
)) >> 32;
3776 /* Don't set Z or N. */
3777 if (store (sd
, &code
->dst
, res
))
3782 case O (O_MULXS
, SB
): /* mulxs.b */
3783 if (fetch (sd
, &code
->src
, &ea
) ||
3784 fetch (sd
, &code
->dst
, &rd
))
3788 res
= ea
* SEXTCHAR (rd
);
3792 if (store (sd
, &code
->dst
, res
))
3797 case O (O_MULXS
, SW
): /* mulxs.w */
3798 if (fetch (sd
, &code
->src
, &ea
) ||
3799 fetch (sd
, &code
->dst
, &rd
))
3802 ea
= SEXTSHORT (ea
);
3803 res
= ea
* SEXTSHORT (rd
& 0xffff);
3805 n
= res
& 0x80000000;
3806 nz
= res
& 0xffffffff;
3807 if (store (sd
, &code
->dst
, res
))
3812 case O (O_MULXU
, SB
): /* mulxu.b */
3813 if (fetch (sd
, &code
->src
, &ea
) ||
3814 fetch (sd
, &code
->dst
, &rd
))
3817 res
= UEXTCHAR (ea
) * UEXTCHAR (rd
);
3819 if (store (sd
, &code
->dst
, res
))
3824 case O (O_MULXU
, SW
): /* mulxu.w */
3825 if (fetch (sd
, &code
->src
, &ea
) ||
3826 fetch (sd
, &code
->dst
, &rd
))
3829 res
= UEXTSHORT (ea
) * UEXTSHORT (rd
);
3831 if (store (sd
, &code
->dst
, res
))
3836 case O (O_TAS
, SB
): /* tas (test and set) */
3837 if (!h8300sxmode
) /* h8sx can use any register. */
3838 switch (code
->src
.reg
)
3849 if (fetch (sd
, &code
->src
, &res
))
3851 if (store (sd
, &code
->src
, res
| 0x80))
3854 goto just_flags_log8
;
3856 case O (O_DIVU
, SW
): /* divu.w */
3857 if (fetch (sd
, &code
->src
, &ea
) ||
3858 fetch (sd
, &code
->dst
, &rd
))
3864 res
= (unsigned) (UEXTSHORT (rd
) / UEXTSHORT (ea
));
3868 if (store (sd
, &code
->dst
, res
))
3872 case O (O_DIVU
, SL
): /* divu.l */
3873 if (fetch (sd
, &code
->src
, &ea
) ||
3874 fetch (sd
, &code
->dst
, &rd
))
3877 n
= ea
& 0x80000000;
3878 nz
= ea
& 0xffffffff;
3880 res
= (unsigned) rd
/ ea
;
3884 if (store (sd
, &code
->dst
, res
))
3888 case O (O_DIVS
, SW
): /* divs.w */
3889 if (fetch (sd
, &code
->src
, &ea
) ||
3890 fetch (sd
, &code
->dst
, &rd
))
3895 res
= SEXTSHORT (rd
) / SEXTSHORT (ea
);
3905 if (store (sd
, &code
->dst
, res
))
3909 case O (O_DIVS
, SL
): /* divs.l */
3910 if (fetch (sd
, &code
->src
, &ea
) ||
3911 fetch (sd
, &code
->dst
, &rd
))
3925 n
= res
& 0x80000000;
3926 if (store (sd
, &code
->dst
, res
))
3930 case O (O_DIVXU
, SB
): /* divxu.b */
3931 if (fetch (sd
, &code
->src
, &ea
) ||
3932 fetch (sd
, &code
->dst
, &rd
))
3935 rd
= UEXTSHORT (rd
);
3942 tmp
= (unsigned) rd
% ea
;
3943 res
= (unsigned) rd
/ ea
;
3951 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
3955 case O (O_DIVXU
, SW
): /* divxu.w */
3956 if (fetch (sd
, &code
->src
, &ea
) ||
3957 fetch (sd
, &code
->dst
, &rd
))
3960 ea
= UEXTSHORT (ea
);
3966 tmp
= (unsigned) rd
% ea
;
3967 res
= (unsigned) rd
/ ea
;
3975 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
3979 case O (O_DIVXS
, SB
): /* divxs.b */
3980 if (fetch (sd
, &code
->src
, &ea
) ||
3981 fetch (sd
, &code
->dst
, &rd
))
3984 rd
= SEXTSHORT (rd
);
3989 tmp
= (int) rd
% (int) ea
;
3990 res
= (int) rd
/ (int) ea
;
4001 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
4005 case O (O_DIVXS
, SW
): /* divxs.w */
4006 if (fetch (sd
, &code
->src
, &ea
) ||
4007 fetch (sd
, &code
->dst
, &rd
))
4010 ea
= SEXTSHORT (ea
);
4014 tmp
= (int) rd
% (int) ea
;
4015 res
= (int) rd
/ (int) ea
;
4025 n
= res
& 0x80000000;
4026 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
4030 case O (O_EXTS
, SW
): /* exts.w, signed extend */
4031 if (fetch2 (sd
, &code
->dst
, &rd
))
4033 ea
= rd
& 0x80 ? -256 : 0;
4034 res
= (rd
& 0xff) + ea
;
4037 case O (O_EXTS
, SL
): /* exts.l, signed extend */
4038 if (fetch2 (sd
, &code
->dst
, &rd
))
4040 if (code
->src
.type
== X (OP_IMM
, SL
))
4042 if (fetch (sd
, &code
->src
, &ea
))
4045 if (ea
== 2) /* exts.l #2, nn */
4047 /* Sign-extend from 8-bit to 32-bit. */
4048 ea
= rd
& 0x80 ? -256 : 0;
4049 res
= (rd
& 0xff) + ea
;
4053 /* Sign-extend from 16-bit to 32-bit. */
4054 ea
= rd
& 0x8000 ? -65536 : 0;
4055 res
= (rd
& 0xffff) + ea
;
4058 case O (O_EXTU
, SW
): /* extu.w, unsigned extend */
4059 if (fetch2 (sd
, &code
->dst
, &rd
))
4062 res
= (rd
& 0xff) + ea
;
4065 case O (O_EXTU
, SL
): /* extu.l, unsigned extend */
4066 if (fetch2 (sd
, &code
->dst
, &rd
))
4068 if (code
->src
.type
== X (OP_IMM
, SL
))
4070 if (fetch (sd
, &code
->src
, &ea
))
4073 if (ea
== 2) /* extu.l #2, nn */
4075 /* Zero-extend from 8-bit to 32-bit. */
4077 res
= (rd
& 0xff) + ea
;
4081 /* Zero-extend from 16-bit to 32-bit. */
4083 res
= (rd
& 0xffff) + ea
;
4086 case O (O_NOP
, SN
): /* nop */
4089 case O (O_STM
, SL
): /* stm, store to memory */
4091 int nregs
, firstreg
, i
;
4093 nregs
= GET_MEMORY_B (pc
+ 1);
4096 firstreg
= code
->src
.reg
;
4098 for (i
= firstreg
; i
<= firstreg
+ nregs
; i
++)
4100 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) - 4);
4101 SET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
), h8_get_reg (sd
, i
));
4106 case O (O_LDM
, SL
): /* ldm, load from memory */
4107 case O (O_RTEL
, SN
): /* rte/l, ldm plus rte */
4108 case O (O_RTSL
, SN
): /* rts/l, ldm plus rts */
4110 int nregs
, firstreg
, i
;
4112 nregs
= ((GET_MEMORY_B (pc
+ 1) >> 4) & 0xf);
4113 firstreg
= code
->dst
.reg
& 0xf;
4114 for (i
= firstreg
; i
>= firstreg
- nregs
; i
--)
4116 h8_set_reg (sd
, i
, GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
)));
4117 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) + 4);
4120 switch (code
->opcode
) {
4121 case O (O_RTEL
, SN
):
4123 case O (O_RTSL
, SN
):
4132 /* Decimal Adjust Addition. This is for BCD arithmetic. */
4133 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch? */
4134 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4135 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4136 res
= res
; /* Value added == 0. */
4137 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4138 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4139 res
= res
+ 0x6; /* Value added == 6. */
4140 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4141 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4142 res
= res
+ 0x6; /* Value added == 6. */
4143 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4144 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4145 res
= res
+ 0x60; /* Value added == 60. */
4146 else if (!c
&& (9 <= (res
>> 4) && (res
>> 4) <= 15) &&
4147 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4148 res
= res
+ 0x66; /* Value added == 66. */
4149 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4150 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4151 res
= res
+ 0x66; /* Value added == 66. */
4152 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4153 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4154 res
= res
+ 0x60; /* Value added == 60. */
4155 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4156 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4157 res
= res
+ 0x66; /* Value added == 66. */
4158 else if (c
&& (1 <= (res
>> 4) && (res
>> 4) <= 3) &&
4159 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4160 res
= res
+ 0x66; /* Value added == 66. */
4165 /* Decimal Adjust Subtraction. This is for BCD arithmetic. */
4166 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch, fetch2... */
4167 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4168 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4169 res
= res
; /* Value added == 0. */
4170 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4171 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4172 res
= res
+ 0xfa; /* Value added == 0xfa. */
4173 else if ( c
&& (7 <= (res
>> 4) && (res
>> 4) <= 15) &&
4174 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4175 res
= res
+ 0xa0; /* Value added == 0xa0. */
4176 else if (c
&& (6 <= (res
>> 4) && (res
>> 4) <= 15) &&
4177 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4178 res
= res
+ 0x9a; /* Value added == 0x9a. */
4184 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
4189 sim_io_printf (sd
, "sim_resume: internal error.\n");
4190 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
4194 if (code
->dst
.type
== X (OP_CCR
, SB
) ||
4195 code
->dst
.type
== X (OP_CCR
, SW
))
4197 h8_set_ccr (sd
, res
);
4200 else if (h8300smode
&&
4201 (code
->dst
.type
== X (OP_EXR
, SB
) ||
4202 code
->dst
.type
== X (OP_EXR
, SW
)))
4204 h8_set_exr (sd
, res
);
4205 if (h8300smode
) /* Get exr. */
4207 trace
= (h8_get_exr (sd
) >> 7) & 1;
4208 intMask
= h8_get_exr (sd
) & 7;
4217 /* When a branch works */
4218 if (fetch (sd
, &code
->src
, &res
))
4220 if (res
& 1) /* bad address */
4222 pc
= code
->next_pc
+ res
;
4225 /* Set the cond codes from res */
4228 /* Set the flags after an 8 bit inc/dec operation */
4232 v
= (rd
& 0x7f) == 0x7f;
4235 /* Set the flags after an 16 bit inc/dec operation */
4239 v
= (rd
& 0x7fff) == 0x7fff;
4242 /* Set the flags after an 32 bit inc/dec operation */
4244 n
= res
& 0x80000000;
4245 nz
= res
& 0xffffffff;
4246 v
= (rd
& 0x7fffffff) == 0x7fffffff;
4250 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
4253 if (store2 (sd
, &code
->dst
, rd
))
4258 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
4261 if (store2 (sd
, &code
->dst
, rd
))
4266 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
4267 n
= (rd
& 0x80000000);
4268 nz
= rd
& 0xffffffff;
4269 if (store2 (sd
, &code
->dst
, rd
))
4274 if (store2 (sd
, &code
->dst
, res
))
4278 /* flags after a 32bit logical operation */
4279 n
= res
& 0x80000000;
4280 nz
= res
& 0xffffffff;
4285 if (store2 (sd
, &code
->dst
, res
))
4289 /* flags after a 16bit logical operation */
4296 if (store2 (sd
, &code
->dst
, res
))
4306 if (store2 (sd
, &code
->dst
, res
))
4313 switch (code
->opcode
/ 4)
4317 v
= ((rd
& 0x80) == (ea
& 0x80)
4318 && (rd
& 0x80) != (res
& 0x80));
4323 v
= ((rd
& 0x80) != (-ea
& 0x80)
4324 && (rd
& 0x80) != (res
& 0x80));
4331 break; /* No effect on v flag. */
4336 if (store2 (sd
, &code
->dst
, res
))
4342 c
= (res
& 0x10000);
4343 switch (code
->opcode
/ 4)
4347 v
= ((rd
& 0x8000) == (ea
& 0x8000)
4348 && (rd
& 0x8000) != (res
& 0x8000));
4353 v
= ((rd
& 0x8000) != (-ea
& 0x8000)
4354 && (rd
& 0x8000) != (res
& 0x8000));
4363 if (store2 (sd
, &code
->dst
, res
))
4367 n
= res
& 0x80000000;
4368 nz
= res
& 0xffffffff;
4369 switch (code
->opcode
/ 4)
4373 v
= ((rd
& 0x80000000) == (ea
& 0x80000000)
4374 && (rd
& 0x80000000) != (res
& 0x80000000));
4375 c
= ((unsigned) res
< (unsigned) rd
) ||
4376 ((unsigned) res
< (unsigned) ea
);
4381 v
= ((rd
& 0x80000000) != (-ea
& 0x80000000)
4382 && (rd
& 0x80000000) != (res
& 0x80000000));
4383 c
= (unsigned) rd
< (unsigned) -ea
;
4386 v
= (rd
== 0x80000000);
4393 if ((res
= h8_get_delayed_branch (sd
)) != 0)
4396 h8_set_delayed_branch (sd
, 0);
4404 h8_set_ticks (sd
, h8_get_ticks (sd
) + get_now () - tick_start
);
4405 h8_set_cycles (sd
, h8_get_cycles (sd
) + cycles
);
4406 h8_set_insts (sd
, h8_get_insts (sd
) + insts
);
4411 h8_set_exr (sd
, (trace
<<7) | intMask
);
4413 h8_set_mask (sd
, oldmask
);
4417 sim_engine_run (SIM_DESC sd
,
4418 int next_cpu_nr
, /* ignore */
4419 int nr_cpus
, /* ignore */
4424 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4426 cpu
= STATE_CPU (sd
, 0);
4430 step_once (sd
, cpu
);
4431 if (sim_events_tick (sd
))
4432 sim_events_process (sd
);
4437 sim_write (SIM_DESC sd
, SIM_ADDR addr
, const unsigned char *buffer
, int size
)
4444 for (i
= 0; i
< size
; i
++)
4446 if (addr
< memory_size
)
4448 h8_set_memory (sd
, addr
+ i
, buffer
[i
]);
4452 h8_set_eightbit (sd
, (addr
+ i
) & 0xff, buffer
[i
]);
4459 sim_read (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
4464 if (addr
< memory_size
)
4465 memcpy (buffer
, h8_get_memory_buf (sd
) + addr
, size
);
4467 memcpy (buffer
, h8_get_eightbit_buf (sd
) + (addr
& 0xff), size
);
4472 h8300_reg_store (SIM_CPU
*cpu
, int rn
, unsigned char *value
, int length
)
4477 longval
= (value
[0] << 24) | (value
[1] << 16) | (value
[2] << 8) | value
[3];
4478 shortval
= (value
[0] << 8) | (value
[1]);
4479 intval
= h8300hmode
? longval
: shortval
;
4481 init_pointers (CPU_STATE (cpu
));
4485 if(h8300_normal_mode
)
4486 cpu
->pc
= shortval
; /* PC for Normal mode is 2 bytes */
4506 cpu
->regs
[rn
] = intval
;
4511 cpu
->regs
[rn
] = longval
;
4518 h8300_reg_fetch (SIM_CPU
*cpu
, int rn
, unsigned char *buf
, int length
)
4523 init_pointers (CPU_STATE (cpu
));
4525 if (!h8300smode
&& rn
>= EXR_REGNUM
)
4560 /* In Normal mode PC is 2 byte, but other registers are 4 byte */
4561 if ((h8300hmode
|| longreg
) && !(rn
== PC_REGNUM
&& h8300_normal_mode
))
4578 sim_info (SIM_DESC sd
, int verbose
)
4580 double timetaken
= (double) h8_get_ticks (sd
) / (double) now_persec ();
4581 double virttime
= h8_get_cycles (sd
) / 10.0e6
;
4583 sim_io_printf (sd
, "\n\n#instructions executed %10d\n", h8_get_insts (sd
));
4584 sim_io_printf (sd
, "#cycles (v approximate) %10d\n", h8_get_cycles (sd
));
4585 sim_io_printf (sd
, "#real time taken %10.4f\n", timetaken
);
4586 sim_io_printf (sd
, "#virtual time taken %10.4f\n", virttime
);
4587 if (timetaken
!= 0.0)
4588 sim_io_printf (sd
, "#simulation ratio %10.4f\n", virttime
/ timetaken
);
4591 /* This to be conditional on `what' (aka `verbose'),
4592 however it was never passed as non-zero. */
4596 for (i
= 0; i
< O_LAST
; i
++)
4598 if (h8_get_stats (sd
, i
))
4599 sim_io_printf (sd
, "%d: %d\n", i
, h8_get_stats (sd
, i
));
4605 /* Indicate whether the cpu is an H8/300 or H8/300H.
4606 FLAG is non-zero for the H8/300H. */
4609 set_h8300h (unsigned long machine
)
4611 /* FIXME: Much of the code in sim_load can be moved to sim_open.
4612 This function being replaced by a sim_open:ARGV configuration
4615 h8300hmode
= h8300smode
= h8300sxmode
= h8300_normal_mode
= 0;
4617 if (machine
== bfd_mach_h8300sx
|| machine
== bfd_mach_h8300sxn
)
4620 if (machine
== bfd_mach_h8300s
|| machine
== bfd_mach_h8300sn
|| h8300sxmode
)
4623 if (machine
== bfd_mach_h8300h
|| machine
== bfd_mach_h8300hn
|| h8300smode
)
4626 if(machine
== bfd_mach_h8300hn
|| machine
== bfd_mach_h8300sn
|| machine
== bfd_mach_h8300sxn
)
4627 h8300_normal_mode
= 1;
4630 /* H8300-specific options.
4631 TODO: These really should be merged into the common model modules. */
4639 h8300_option_handler (SIM_DESC sd
, sim_cpu
*cpu ATTRIBUTE_UNUSED
, int opt
,
4640 char *arg
, int is_command ATTRIBUTE_UNUSED
)
4642 switch ((H8300_OPTIONS
) opt
)
4645 set_h8300h (bfd_mach_h8300h
);
4648 set_h8300h (bfd_mach_h8300s
);
4650 case OPTION_H8300SX
:
4651 set_h8300h (bfd_mach_h8300sx
);
4655 /* We'll actually never get here; the caller handles the error
4657 sim_io_eprintf (sd
, "Unknown option `%s'\n", arg
);
4664 static const OPTION h8300_options
[] =
4666 { {"h8300h", no_argument
, NULL
, OPTION_H8300H
},
4667 'h', NULL
, "Indicate the CPU is H8/300H",
4668 h8300_option_handler
},
4669 { {"h8300s", no_argument
, NULL
, OPTION_H8300S
},
4670 'S', NULL
, "Indicate the CPU is H8S",
4671 h8300_option_handler
},
4672 { {"h8300sx", no_argument
, NULL
, OPTION_H8300SX
},
4673 'x', NULL
, "Indicate the CPU is H8SX",
4674 h8300_option_handler
},
4675 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
4679 h8300_pc_get (sim_cpu
*cpu
)
4685 h8300_pc_set (sim_cpu
*cpu
, sim_cia pc
)
4690 /* Cover function of sim_state_free to free the cpu buffers as well. */
4693 free_state (SIM_DESC sd
)
4695 if (STATE_MODULES (sd
) != NULL
)
4696 sim_module_uninstall (sd
);
4698 /* Fixme: free buffers in _sim_cpu. */
4699 sim_state_free (sd
);
4703 sim_open (SIM_OPEN_KIND kind
,
4704 struct host_callback_struct
*callback
,
4712 sd
= sim_state_alloc (kind
, callback
);
4714 /* The cpu data is kept in a separately allocated chunk of memory. */
4715 if (sim_cpu_alloc_all (sd
, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK
)
4721 cpu
= STATE_CPU (sd
, 0);
4722 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4723 cpu
->regs
[SBR_REGNUM
] = 0xFFFFFF00;
4724 /* sim_cpu object is new, so some initialization is needed. */
4725 init_pointers_needed
= 1;
4727 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
4733 if (sim_add_option_table (sd
, NULL
, h8300_options
) != SIM_RC_OK
)
4739 /* The parser will print an error message for us, so we silently return. */
4740 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
4742 /* Uninstall the modules to avoid memory leaks,
4743 file descriptor leaks, etc. */
4748 /* Check for/establish the a reference program image. */
4749 if (sim_analyze_program (sd
,
4750 (STATE_PROG_ARGV (sd
) != NULL
4751 ? *STATE_PROG_ARGV (sd
)
4752 : NULL
), abfd
) != SIM_RC_OK
)
4758 /* Establish any remaining configuration options. */
4759 if (sim_config (sd
) != SIM_RC_OK
)
4765 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
4767 /* Uninstall the modules to avoid memory leaks,
4768 file descriptor leaks, etc. */
4773 /* CPU specific initialization. */
4774 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
4776 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
4778 CPU_REG_FETCH (cpu
) = h8300_reg_fetch
;
4779 CPU_REG_STORE (cpu
) = h8300_reg_store
;
4780 CPU_PC_FETCH (cpu
) = h8300_pc_get
;
4781 CPU_PC_STORE (cpu
) = h8300_pc_set
;
4784 /* sim_hw_configure (sd); */
4786 /* FIXME: Much of the code in sim_load can be moved here. */
4791 /* Called by gdb to load a program into memory. */
4794 sim_load (SIM_DESC sd
, const char *prog
, bfd
*abfd
, int from_tty
)
4798 /* FIXME: The code below that sets a specific variant of the H8/300
4799 being simulated should be moved to sim_open(). */
4801 /* See if the file is for the H8/300 or H8/300H. */
4802 /* ??? This may not be the most efficient way. The z8k simulator
4803 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
4807 prog_bfd
= bfd_openr (prog
, NULL
);
4808 if (prog_bfd
!= NULL
)
4810 /* Set the cpu type. We ignore failure from bfd_check_format
4811 and bfd_openr as sim_load_file checks too. */
4812 if (bfd_check_format (prog_bfd
, bfd_object
))
4814 set_h8300h (bfd_get_mach (prog_bfd
));
4818 /* If we're using gdb attached to the simulator, then we have to
4819 reallocate memory for the simulator.
4821 When gdb first starts, it calls fetch_registers (among other
4822 functions), which in turn calls init_pointers, which allocates
4825 The problem is when we do that, we don't know whether we're
4826 debugging an H8/300 or H8/300H program.
4828 This is the first point at which we can make that determination,
4829 so we just reallocate memory now; this will also allow us to handle
4830 switching between H8/300 and H8/300H programs without exiting
4833 if (h8300smode
&& !h8300_normal_mode
)
4834 memory_size
= H8300S_MSIZE
;
4835 else if (h8300hmode
&& !h8300_normal_mode
)
4836 memory_size
= H8300H_MSIZE
;
4838 memory_size
= H8300_MSIZE
;
4840 if (h8_get_memory_buf (sd
))
4841 free (h8_get_memory_buf (sd
));
4842 if (h8_get_eightbit_buf (sd
))
4843 free (h8_get_eightbit_buf (sd
));
4845 h8_set_memory_buf (sd
, (unsigned char *)
4846 calloc (sizeof (char), memory_size
));
4847 sd
->memory_size
= memory_size
;
4848 h8_set_eightbit_buf (sd
, (unsigned char *) calloc (sizeof (char), 256));
4850 /* `msize' must be a power of two. */
4851 if ((memory_size
& (memory_size
- 1)) != 0)
4853 sim_io_printf (sd
, "sim_load: bad memory size.\n");
4856 h8_set_mask (sd
, memory_size
- 1);
4858 if (sim_load_file (sd
, STATE_MY_NAME (sd
), STATE_CALLBACK (sd
), prog
,
4859 prog_bfd
, STATE_OPEN_KIND (sd
) == SIM_OPEN_DEBUG
,
4863 /* Close the bfd if we opened it. */
4864 if (abfd
== NULL
&& prog_bfd
!= NULL
)
4865 bfd_close (prog_bfd
);
4869 /* Close the bfd if we opened it. */
4870 if (abfd
== NULL
&& prog_bfd
!= NULL
)
4871 bfd_close (prog_bfd
);
4876 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
4877 char * const *argv
, char * const *env
)
4884 h8_set_pc (sd
, bfd_get_start_address (abfd
));
4888 /* Command Line support. */
4891 /* Counting the no. of commandline arguments. */
4892 for (no_of_args
= 0; argv
[no_of_args
] != NULL
; no_of_args
++)
4895 /* Allocating memory for the argv pointers. */
4896 h8_set_command_line (sd
, (char **) malloc ((sizeof (char *))
4897 * (no_of_args
+ 1)));
4899 for (i
= 0; i
< no_of_args
; i
++)
4901 /* Copying the argument string. */
4902 h8_set_cmdline_arg (sd
, i
, (char *) strdup (argv
[i
]));
4904 h8_set_cmdline_arg (sd
, i
, NULL
);