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.
20 /* This must come before any other includes. */
26 #ifdef HAVE_SYS_PARAM_H
27 #include <sys/param.h>
32 #include "gdb/sim-h8300.h"
34 #include "sys/types.h"
35 #include "sim-options.h"
36 #include "sim-signal.h"
37 #include "sim/callback.h"
45 static int memory_size
;
47 #define X(op, size) (op * 4 + size)
49 #define SP (h8300hmode && !h8300_normal_mode ? SL : SW)
51 #define h8_opcodes ops
53 #include "opcode/h8300.h"
55 /* CPU data object: */
58 h8_get_pc (SIM_DESC sd
)
60 return (STATE_CPU (sd
, 0)) -> pc
;
64 h8_set_pc (SIM_DESC sd
, unsigned int val
)
66 (STATE_CPU (sd
, 0)) -> pc
= val
;
70 h8_get_ccr (SIM_DESC sd
)
72 return (STATE_CPU (sd
, 0)) -> regs
[CCR_REGNUM
];
76 h8_set_ccr (SIM_DESC sd
, unsigned int val
)
78 (STATE_CPU (sd
, 0)) -> regs
[CCR_REGNUM
] = val
;
82 h8_get_exr (SIM_DESC sd
)
84 return (STATE_CPU (sd
, 0)) -> regs
[EXR_REGNUM
];
88 h8_set_exr (SIM_DESC sd
, unsigned int val
)
90 (STATE_CPU (sd
, 0)) -> regs
[EXR_REGNUM
] = val
;
94 h8_get_sbr (SIM_DESC sd
)
96 return (STATE_CPU (sd
, 0)) -> regs
[SBR_REGNUM
];
100 h8_set_sbr (SIM_DESC sd
, int val
)
102 (STATE_CPU (sd
, 0)) -> regs
[SBR_REGNUM
] = val
;
106 h8_get_vbr (SIM_DESC sd
)
108 return (STATE_CPU (sd
, 0)) -> regs
[VBR_REGNUM
];
112 h8_set_vbr (SIM_DESC sd
, int val
)
114 (STATE_CPU (sd
, 0)) -> regs
[VBR_REGNUM
] = val
;
118 h8_get_mask (SIM_DESC sd
)
120 return (STATE_CPU (sd
, 0)) -> mask
;
124 h8_set_mask (SIM_DESC sd
, int val
)
126 (STATE_CPU (sd
, 0)) -> mask
= val
;
130 h8_get_exception (SIM_DESC sd
)
132 return (STATE_CPU (sd
, 0)) -> exception
;
136 h8_set_exception (SIM_DESC sd
, int val
)
138 (STATE_CPU (sd
, 0)) -> exception
= val
;
141 static enum h8300_sim_state
142 h8_get_state (SIM_DESC sd
)
144 return H8300_SIM_STATE (sd
)->state
;
148 h8_set_state (SIM_DESC sd
, enum h8300_sim_state val
)
150 H8300_SIM_STATE (sd
)->state
= val
;
154 h8_get_cycles (SIM_DESC sd
)
156 return (STATE_CPU (sd
, 0)) -> regs
[CYCLE_REGNUM
];
160 h8_set_cycles (SIM_DESC sd
, unsigned int val
)
162 (STATE_CPU (sd
, 0)) -> regs
[CYCLE_REGNUM
] = val
;
166 h8_get_insts (SIM_DESC sd
)
168 return (STATE_CPU (sd
, 0)) -> regs
[INST_REGNUM
];
172 h8_set_insts (SIM_DESC sd
, unsigned int val
)
174 (STATE_CPU (sd
, 0)) -> regs
[INST_REGNUM
] = val
;
178 h8_get_ticks (SIM_DESC sd
)
180 return (STATE_CPU (sd
, 0)) -> regs
[TICK_REGNUM
];
184 h8_set_ticks (SIM_DESC sd
, unsigned int val
)
186 (STATE_CPU (sd
, 0)) -> regs
[TICK_REGNUM
] = val
;
190 h8_get_mach (SIM_DESC sd
)
192 return (STATE_CPU (sd
, 0)) -> regs
[MACH_REGNUM
];
196 h8_set_mach (SIM_DESC sd
, unsigned int val
)
198 (STATE_CPU (sd
, 0)) -> regs
[MACH_REGNUM
] = val
;
202 h8_get_macl (SIM_DESC sd
)
204 return (STATE_CPU (sd
, 0)) -> regs
[MACL_REGNUM
];
208 h8_set_macl (SIM_DESC sd
, unsigned int val
)
210 (STATE_CPU (sd
, 0)) -> regs
[MACL_REGNUM
] = val
;
213 static unsigned int *
214 h8_get_reg_buf (SIM_DESC sd
)
216 return &(((STATE_CPU (sd
, 0)) -> regs
)[0]);
220 h8_get_reg (SIM_DESC sd
, int regnum
)
222 return (STATE_CPU (sd
, 0)) -> regs
[regnum
];
226 h8_set_reg (SIM_DESC sd
, int regnum
, int val
)
228 (STATE_CPU (sd
, 0)) -> regs
[regnum
] = val
;
233 h8_get_stats (SIM_DESC sd
, int idx
)
235 return H8300_SIM_STATE (sd
)->stats
[idx
];
239 h8_increment_stats (SIM_DESC sd
, int idx
)
241 H8300_SIM_STATE (sd
)->stats
[idx
] ++;
245 static unsigned char *
246 h8_get_memory_buf (SIM_DESC sd
)
248 return (STATE_CPU (sd
, 0)) -> memory
;
252 h8_set_memory_buf (SIM_DESC sd
, unsigned char *ptr
)
254 (STATE_CPU (sd
, 0)) -> memory
= ptr
;
258 h8_get_memory (SIM_DESC sd
, int idx
)
260 ASSERT (idx
< memory_size
);
261 return (STATE_CPU (sd
, 0)) -> memory
[idx
];
265 h8_set_memory (SIM_DESC sd
, int idx
, unsigned int val
)
267 ASSERT (idx
< memory_size
);
268 (STATE_CPU (sd
, 0)) -> memory
[idx
] = (unsigned char) val
;
272 h8_get_delayed_branch (SIM_DESC sd
)
274 return (STATE_CPU (sd
, 0)) -> delayed_branch
;
278 h8_set_delayed_branch (SIM_DESC sd
, unsigned int dest
)
280 (STATE_CPU (sd
, 0)) -> delayed_branch
= dest
;
284 h8_get_command_line (SIM_DESC sd
)
286 return (STATE_CPU (sd
, 0)) -> command_line
;
290 h8_set_command_line (SIM_DESC sd
, char ** val
)
292 (STATE_CPU (sd
, 0)) -> command_line
= val
;
296 h8_get_cmdline_arg (SIM_DESC sd
, int index
)
298 return (STATE_CPU (sd
, 0)) -> command_line
[index
];
302 h8_set_cmdline_arg (SIM_DESC sd
, int index
, char * val
)
304 (STATE_CPU (sd
, 0)) -> command_line
[index
] = val
;
307 /* MAC Saturation Mode */
309 h8_get_macS (SIM_DESC sd
)
311 return (STATE_CPU (sd
, 0)) -> macS
;
316 h8_set_macS (SIM_DESC sd
, int val
)
318 (STATE_CPU (sd
, 0)) -> macS
= (val
!= 0);
324 h8_get_macZ (SIM_DESC sd
)
326 return (STATE_CPU (sd
, 0)) -> macZ
;
330 h8_set_macZ (SIM_DESC sd
, int val
)
332 (STATE_CPU (sd
, 0)) -> macZ
= (val
!= 0);
335 /* MAC Negative Flag */
337 h8_get_macN (SIM_DESC sd
)
339 return (STATE_CPU (sd
, 0)) -> macN
;
343 h8_set_macN (SIM_DESC sd
, int val
)
345 (STATE_CPU (sd
, 0)) -> macN
= (val
!= 0);
348 /* MAC Overflow Flag */
350 h8_get_macV (SIM_DESC sd
)
352 return (STATE_CPU (sd
, 0)) -> macV
;
356 h8_set_macV (SIM_DESC sd
, int val
)
358 (STATE_CPU (sd
, 0)) -> macV
= (val
!= 0);
361 /* End CPU data object. */
363 /* The rate at which to call the host's poll_quit callback. */
365 enum { POLL_QUIT_INTERVAL
= 0x80000 };
367 #define LOW_BYTE(x) ((x) & 0xff)
368 #define HIGH_BYTE(x) (((x) >> 8) & 0xff)
369 #define P(X, Y) ((X << 8) | Y)
378 #define I (intMaskBit != 0)
380 #define BUILDSR(SD) \
381 h8_set_ccr (SD, (I << 7) | (UI << 6) | (H << 5) | (U << 4) \
382 | (N << 3) | (Z << 2) | (V << 1) | C)
385 /* Get Status Register (flags). */ \
386 c = (h8_get_ccr (sd) >> 0) & 1; \
387 v = (h8_get_ccr (sd) >> 1) & 1; \
388 nz = !((h8_get_ccr (sd) >> 2) & 1); \
389 n = (h8_get_ccr (sd) >> 3) & 1; \
390 u = (h8_get_ccr (sd) >> 4) & 1; \
391 h = (h8_get_ccr (sd) >> 5) & 1; \
392 ui = ((h8_get_ccr (sd) >> 6) & 1); \
393 intMaskBit = (h8_get_ccr (sd) >> 7) & 1
396 #ifdef __CHAR_IS_SIGNED__
397 #define SEXTCHAR(x) ((char) (x))
401 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff) : x & 0xff)
404 #define UEXTCHAR(x) ((x) & 0xff)
405 #define UEXTSHORT(x) ((x) & 0xffff)
406 #define SEXTSHORT(x) ((short) (x))
410 int h8300_normal_mode
= 0;
416 return time (0); /* WinXX HAS UNIX like 'time', so why not use it? */
438 return (h8300hmode
&& !h8300_normal_mode
)? SL
: SW
;
443 /* Simulate an indirection / dereference.
444 return 0 for success, -1 for failure.
448 lvalue (SIM_DESC sd
, int x
, int rn
, unsigned int *val
)
450 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
452 if (val
== NULL
) /* Paranoia. */
458 if (rn
== ZERO_REGNUM
)
459 *val
= X (OP_IMM
, SP
);
461 *val
= X (OP_REG
, SP
);
464 *val
= X (OP_MEM
, SP
);
467 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
474 cmdline_location(void)
476 if (h8300smode
&& !h8300_normal_mode
)
478 else if (h8300hmode
&& !h8300_normal_mode
)
485 decode (SIM_DESC sd
, int addr
, unsigned char *data
, decoded_inst
*dst
)
487 int cst
[3] = {0, 0, 0};
488 int reg
[3] = {0, 0, 0};
489 int rdisp
[3] = {0, 0, 0};
491 const struct h8_opcode
*q
;
497 /* Find the exact opcode/arg combo. */
498 for (q
= h8_opcodes
; q
->name
; q
++)
500 const op_type
*nib
= q
->data
.nib
;
501 unsigned int len
= 0;
503 if ((q
->available
== AV_H8SX
&& !h8300sxmode
) ||
504 (q
->available
== AV_H8S
&& !h8300smode
) ||
505 (q
->available
== AV_H8H
&& !h8300hmode
))
508 cst
[0] = cst
[1] = cst
[2] = 0;
509 reg
[0] = reg
[1] = reg
[2] = 0;
510 rdisp
[0] = rdisp
[1] = rdisp
[2] = 0;
514 op_type looking_for
= *nib
;
515 int thisnib
= data
[len
/ 2];
517 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
518 opnum
= ((looking_for
& OP3
) ? 2 :
519 (looking_for
& DST
) ? 1 : 0);
521 if (looking_for
< 16 && looking_for
>= 0)
523 if (looking_for
!= thisnib
)
528 if (looking_for
& B31
)
530 if (!((thisnib
& 0x8) != 0))
533 looking_for
= (op_type
) (looking_for
& ~B31
);
536 else if (looking_for
& B30
)
538 if (!((thisnib
& 0x8) == 0))
541 looking_for
= (op_type
) (looking_for
& ~B30
);
544 if (looking_for
& B21
)
546 if (!((thisnib
& 0x4) != 0))
549 looking_for
= (op_type
) (looking_for
& ~B21
);
552 else if (looking_for
& B20
)
554 if (!((thisnib
& 0x4) == 0))
557 looking_for
= (op_type
) (looking_for
& ~B20
);
560 if (looking_for
& B11
)
562 if (!((thisnib
& 0x2) != 0))
565 looking_for
= (op_type
) (looking_for
& ~B11
);
568 else if (looking_for
& B10
)
570 if (!((thisnib
& 0x2) == 0))
573 looking_for
= (op_type
) (looking_for
& ~B10
);
576 if (looking_for
& B01
)
578 if (!((thisnib
& 0x1) != 0))
581 looking_for
= (op_type
) (looking_for
& ~B01
);
584 else if (looking_for
& B00
)
586 if (!((thisnib
& 0x1) == 0))
589 looking_for
= (op_type
) (looking_for
& ~B00
);
592 if (looking_for
& IGNORE
)
594 /* Hitachi has declared that IGNORE must be zero. */
598 else if ((looking_for
& MODE
) == DATA
)
600 ; /* Skip embedded data. */
602 else if ((looking_for
& MODE
) == DBIT
)
604 /* Exclude adds/subs by looking at bit 0 and 2, and
605 make sure the operand size, either w or l,
606 matches by looking at bit 1. */
607 if ((looking_for
& 7) != (thisnib
& 7))
610 cst
[opnum
] = (thisnib
& 0x8) ? 2 : 1;
612 else if ((looking_for
& MODE
) == REG
||
613 (looking_for
& MODE
) == LOWREG
||
614 (looking_for
& MODE
) == IND
||
615 (looking_for
& MODE
) == PREINC
||
616 (looking_for
& MODE
) == POSTINC
||
617 (looking_for
& MODE
) == PREDEC
||
618 (looking_for
& MODE
) == POSTDEC
)
620 reg
[opnum
] = thisnib
;
622 else if (looking_for
& CTRL
)
625 if (((looking_for
& MODE
) == CCR
&& (thisnib
!= C_CCR
)) ||
626 ((looking_for
& MODE
) == EXR
&& (thisnib
!= C_EXR
)) ||
627 ((looking_for
& MODE
) == MACH
&& (thisnib
!= C_MACH
)) ||
628 ((looking_for
& MODE
) == MACL
&& (thisnib
!= C_MACL
)) ||
629 ((looking_for
& MODE
) == VBR
&& (thisnib
!= C_VBR
)) ||
630 ((looking_for
& MODE
) == SBR
&& (thisnib
!= C_SBR
)))
632 if (((looking_for
& MODE
) == CCR_EXR
&&
633 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
)) ||
634 ((looking_for
& MODE
) == VBR_SBR
&&
635 (thisnib
!= C_VBR
&& thisnib
!= C_SBR
)) ||
636 ((looking_for
& MODE
) == MACREG
&&
637 (thisnib
!= C_MACH
&& thisnib
!= C_MACL
)))
639 if (((looking_for
& MODE
) == CC_EX_VB_SB
&&
640 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
&&
641 thisnib
!= C_VBR
&& thisnib
!= C_SBR
)))
644 reg
[opnum
] = thisnib
;
646 else if ((looking_for
& MODE
) == ABS
)
648 /* Absolute addresses are unsigned. */
649 switch (looking_for
& SIZE
)
652 cst
[opnum
] = UEXTCHAR (data
[len
/ 2]);
656 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
660 (data
[len
/ 2 + 0] << 24) +
661 (data
[len
/ 2 + 1] << 16) +
662 (data
[len
/ 2 + 2] << 8) +
666 printf ("decode: bad size ABS: %d\n",
667 (looking_for
& SIZE
));
671 else if ((looking_for
& MODE
) == DISP
||
672 (looking_for
& MODE
) == PCREL
||
673 (looking_for
& MODE
) == INDEXB
||
674 (looking_for
& MODE
) == INDEXW
||
675 (looking_for
& MODE
) == INDEXL
)
677 switch (looking_for
& SIZE
)
680 cst
[opnum
] = thisnib
& 3;
683 cst
[opnum
] = SEXTCHAR (data
[len
/ 2]);
686 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
687 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
690 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
694 (data
[len
/ 2 + 0] << 24) +
695 (data
[len
/ 2 + 1] << 16) +
696 (data
[len
/ 2 + 2] << 8) +
700 printf ("decode: bad size DISP/PCREL/INDEX: %d\n",
701 (looking_for
& SIZE
));
705 else if ((looking_for
& SIZE
) == L_16
||
706 (looking_for
& SIZE
) == L_16U
)
708 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
709 /* Immediates are always unsigned. */
710 if ((looking_for
& SIZE
) != L_16U
&&
711 (looking_for
& MODE
) != IMM
)
712 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
714 else if (looking_for
& ABSJMP
)
716 switch (looking_for
& SIZE
) {
718 cst
[opnum
] = (data
[1] << 16) | (data
[2] << 8) | (data
[3]);
722 (data
[len
/ 2 + 0] << 24) +
723 (data
[len
/ 2 + 1] << 16) +
724 (data
[len
/ 2 + 2] << 8) +
728 printf ("decode: bad size ABSJMP: %d\n",
729 (looking_for
& SIZE
));
733 else if ((looking_for
& MODE
) == MEMIND
)
735 cst
[opnum
] = data
[1];
737 else if ((looking_for
& MODE
) == VECIND
)
739 if(h8300_normal_mode
)
740 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 2;
742 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 4;
743 cst
[opnum
] += h8_get_vbr (sd
); /* Add vector base reg. */
745 else if ((looking_for
& SIZE
) == L_32
)
750 (data
[i
+ 0] << 24) |
751 (data
[i
+ 1] << 16) |
755 else if ((looking_for
& SIZE
) == L_24
)
760 (data
[i
+ 0] << 16) |
764 else if (looking_for
& DISPREG
)
766 rdisp
[opnum
] = thisnib
& 0x7;
768 else if ((looking_for
& MODE
) == KBIT
)
785 else if ((looking_for
& SIZE
) == L_8
)
787 if ((looking_for
& MODE
) == ABS
)
789 /* Will be combined with contents of SBR_REGNUM
790 by fetch (). For all modes except h8sx, this
791 will always contain the value 0xFFFFFF00. */
792 cst
[opnum
] = data
[len
/ 2] & 0xff;
796 cst
[opnum
] = data
[len
/ 2] & 0xff;
799 else if ((looking_for
& SIZE
) == L_2
)
801 cst
[opnum
] = thisnib
& 3;
803 else if ((looking_for
& SIZE
) == L_3
||
804 (looking_for
& SIZE
) == L_3NZ
)
806 cst
[opnum
] = thisnib
& 7;
807 if (cst
[opnum
] == 0 && (looking_for
& SIZE
) == L_3NZ
)
810 else if ((looking_for
& SIZE
) == L_4
)
812 cst
[opnum
] = thisnib
& 15;
814 else if ((looking_for
& SIZE
) == L_5
)
816 cst
[opnum
] = data
[len
/ 2] & 0x1f;
818 else if (looking_for
== E
)
823 /* Fill in the args. */
825 const op_type
*args
= q
->args
.nib
;
830 nargs
< 3 && *args
!= E
;
836 opnum
= ((x
& OP3
) ? 2 :
845 if ((x
& MODE
) == IMM
||
846 (x
& MODE
) == KBIT
||
849 /* Use the instruction to determine
851 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
852 p
->literal
= cst
[opnum
];
854 else if ((x
& MODE
) == CONST_2
||
855 (x
& MODE
) == CONST_4
||
856 (x
& MODE
) == CONST_8
||
857 (x
& MODE
) == CONST_16
)
859 /* Use the instruction to determine
861 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
863 case CONST_2
: p
->literal
= 2; break;
864 case CONST_4
: p
->literal
= 4; break;
865 case CONST_8
: p
->literal
= 8; break;
866 case CONST_16
: p
->literal
= 16; break;
869 else if ((x
& MODE
) == REG
)
871 p
->type
= X (OP_REG
, bitfrom (x
));
874 else if ((x
& MODE
) == LOWREG
)
876 p
->type
= X (OP_LOWREG
, bitfrom (x
));
879 else if ((x
& MODE
) == PREINC
)
881 /* Use the instruction to determine
883 p
->type
= X (OP_PREINC
, OP_SIZE (q
->how
));
884 p
->reg
= reg
[opnum
] & 0x7;
886 else if ((x
& MODE
) == POSTINC
)
888 /* Use the instruction to determine
890 p
->type
= X (OP_POSTINC
, OP_SIZE (q
->how
));
891 p
->reg
= reg
[opnum
] & 0x7;
893 else if ((x
& MODE
) == PREDEC
)
895 /* Use the instruction to determine
897 p
->type
= X (OP_PREDEC
, OP_SIZE (q
->how
));
898 p
->reg
= reg
[opnum
] & 0x7;
900 else if ((x
& MODE
) == POSTDEC
)
902 /* Use the instruction to determine
904 p
->type
= X (OP_POSTDEC
, OP_SIZE (q
->how
));
905 p
->reg
= reg
[opnum
] & 0x7;
907 else if ((x
& MODE
) == IND
)
909 /* Note: an indirect is transformed into
910 a displacement of zero.
912 /* Use the instruction to determine
914 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
915 p
->reg
= reg
[opnum
] & 0x7;
917 if (OP_KIND (q
->how
) == O_JSR
||
918 OP_KIND (q
->how
) == O_JMP
)
919 if (lvalue (sd
, p
->type
, p
->reg
, (unsigned int *)&p
->type
))
922 else if ((x
& MODE
) == ABS
)
924 /* Note: a 16 or 32 bit ABS is transformed into a
925 displacement from pseudo-register ZERO_REGNUM,
926 which is always zero. An 8 bit ABS becomes
927 a displacement from SBR_REGNUM.
929 /* Use the instruction to determine
931 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
932 p
->literal
= cst
[opnum
];
934 /* 8-bit ABS is displacement from SBR.
935 16 and 32-bit ABS are displacement from ZERO.
936 (SBR will always be zero except for h8/sx)
938 if ((x
& SIZE
) == L_8
)
941 p
->reg
= ZERO_REGNUM
;;
943 else if ((x
& MODE
) == MEMIND
||
944 (x
& MODE
) == VECIND
)
946 /* Size doesn't matter. */
947 p
->type
= X (OP_MEM
, SB
);
948 p
->literal
= cst
[opnum
];
949 if (OP_KIND (q
->how
) == O_JSR
||
950 OP_KIND (q
->how
) == O_JMP
)
951 if (lvalue (sd
, p
->type
, p
->reg
, (unsigned int *)&p
->type
))
954 else if ((x
& MODE
) == PCREL
)
956 /* Size doesn't matter. */
957 p
->type
= X (OP_PCREL
, SB
);
958 p
->literal
= cst
[opnum
];
962 p
->type
= X (OP_IMM
, SP
);
963 p
->literal
= cst
[opnum
];
965 else if ((x
& MODE
) == INDEXB
)
967 p
->type
= X (OP_INDEXB
, OP_SIZE (q
->how
));
968 p
->literal
= cst
[opnum
];
969 p
->reg
= rdisp
[opnum
];
971 else if ((x
& MODE
) == INDEXW
)
973 p
->type
= X (OP_INDEXW
, OP_SIZE (q
->how
));
974 p
->literal
= cst
[opnum
];
975 p
->reg
= rdisp
[opnum
];
977 else if ((x
& MODE
) == INDEXL
)
979 p
->type
= X (OP_INDEXL
, OP_SIZE (q
->how
));
980 p
->literal
= cst
[opnum
];
981 p
->reg
= rdisp
[opnum
];
983 else if ((x
& MODE
) == DISP
)
985 /* Yuck -- special for mova args. */
986 if (strncmp (q
->name
, "mova", 4) == 0 &&
989 /* Mova can have a DISP2 dest, with an
990 INDEXB or INDEXW src. The multiplier
991 for the displacement value is determined
992 by the src operand, not by the insn. */
994 switch (OP_KIND (dst
->src
.type
))
997 p
->type
= X (OP_DISP
, SB
);
998 p
->literal
= cst
[opnum
];
1001 p
->type
= X (OP_DISP
, SW
);
1002 p
->literal
= cst
[opnum
] * 2;
1010 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1011 p
->literal
= cst
[opnum
];
1012 /* DISP2 is special. */
1013 if ((x
& SIZE
) == L_2
)
1014 switch (OP_SIZE (q
->how
))
1017 case SW
: p
->literal
*= 2; break;
1018 case SL
: p
->literal
*= 4; break;
1021 p
->reg
= rdisp
[opnum
];
1028 p
->type
= X (OP_CCR
, SB
);
1031 p
->type
= X (OP_EXR
, SB
);
1034 p
->type
= X (OP_MACH
, SL
);
1037 p
->type
= X (OP_MACL
, SL
);
1040 p
->type
= X (OP_VBR
, SL
);
1043 p
->type
= X (OP_SBR
, SL
);
1047 else if ((x
& MODE
) == CCR
)
1051 else if ((x
& MODE
) == EXR
)
1056 printf ("Hmmmm 0x%x...\n", x
);
1062 /* Unary operators: treat src and dst as equivalent. */
1063 if (dst
->dst
.type
== -1)
1064 dst
->dst
= dst
->src
;
1065 if (dst
->src
.type
== -1)
1066 dst
->src
= dst
->dst
;
1068 dst
->opcode
= q
->how
;
1069 dst
->cycles
= q
->time
;
1071 /* And jsr's to these locations are turned into
1074 if (OP_KIND (dst
->opcode
) == O_JSR
)
1076 switch (dst
->src
.literal
)
1079 dst
->opcode
= O (O_SYS_OPEN
, SB
);
1082 dst
->opcode
= O (O_SYS_READ
, SB
);
1085 dst
->opcode
= O (O_SYS_WRITE
, SB
);
1088 dst
->opcode
= O (O_SYS_LSEEK
, SB
);
1091 dst
->opcode
= O (O_SYS_CLOSE
, SB
);
1094 dst
->opcode
= O (O_SYS_STAT
, SB
);
1097 dst
->opcode
= O (O_SYS_FSTAT
, SB
);
1100 dst
->opcode
= O (O_SYS_CMDLINE
, SB
);
1103 /* End of Processing for system calls. */
1106 /* Use same register is specified for source
1108 The value of source will be the value after
1109 address calculation. */
1110 if (OP_KIND (dst
->opcode
) != O_CMP
&&
1111 OP_KIND (dst
->src
.type
) == OP_REG
&&
1112 (dst
->src
.reg
& 7) == dst
->dst
.reg
) {
1113 switch (OP_KIND (dst
->dst
.type
))
1116 dst
->src
.type
= X (OP_REG_DEC
,
1117 OP_SIZE (dst
->dst
.type
));
1120 dst
->src
.type
= X (OP_REG_INC
,
1121 OP_SIZE (dst
->dst
.type
));
1124 if (OP_KIND (dst
->opcode
) == O_MOV
)
1125 dst
->src
.type
= X (OP_REG_INC
,
1126 OP_SIZE (dst
->dst
.type
));
1129 if (OP_KIND (dst
->opcode
) == O_MOV
)
1130 dst
->src
.type
= X (OP_REG_DEC
,
1131 OP_SIZE (dst
->dst
.type
));
1135 dst
->next_pc
= addr
+ len
/ 2;
1139 printf ("Don't understand 0x%x \n", looking_for
);
1150 /* Fell off the end. */
1151 dst
->opcode
= O (O_ILL
, SB
);
1154 static unsigned char *breg
[32];
1155 static unsigned short *wreg
[16];
1157 #define GET_B_REG(X) *(breg[X])
1158 #define SET_B_REG(X, Y) (*(breg[X])) = (Y)
1159 #define GET_W_REG(X) *(wreg[X])
1160 #define SET_W_REG(X, Y) (*(wreg[X])) = (Y)
1161 #define GET_L_REG(X) h8_get_reg (sd, X)
1162 #define SET_L_REG(X, Y) h8_set_reg (sd, X, Y)
1164 #define GET_MEMORY_L(X) \
1165 ((X) < memory_size \
1166 ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16) \
1167 | (h8_get_memory (sd, (X)+2) << 8) | (h8_get_memory (sd, (X)+3) << 0)) \
1170 #define GET_MEMORY_W(X) \
1171 ((X) < memory_size \
1172 ? ((h8_get_memory (sd, (X)+0) << 8) | (h8_get_memory (sd, (X)+1) << 0)) \
1175 #define GET_MEMORY_B(X) \
1176 ((X) < memory_size ? h8_get_memory (sd, (X)) : 0)
1178 #define SET_MEMORY_L(X, Y) \
1179 { register unsigned char *_p; register int __y = (Y); \
1180 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : 0); \
1181 _p[0] = __y >> 24; _p[1] = __y >> 16; \
1182 _p[2] = __y >> 8; _p[3] = __y >> 0; \
1185 #define SET_MEMORY_W(X, Y) \
1186 { register unsigned char *_p; register int __y = (Y); \
1187 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : 0); \
1188 _p[0] = __y >> 8; _p[1] = __y; \
1191 #define SET_MEMORY_B(X, Y) \
1192 ((X) < memory_size ? h8_set_memory (sd, (X), (Y)) : 0)
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_REG_DEC
, SB
): /* Register direct, affected decrement byte. */
1406 *val
= GET_B_REG (rn
) - 1;
1408 case X (OP_REG_DEC
, SW
): /* Register direct, affected decrement word. */
1409 *val
= GET_W_REG (rn
) - 2;
1411 case X (OP_REG_DEC
, SL
): /* Register direct, affected decrement long. */
1412 *val
= GET_L_REG (rn
) - 4;
1414 case X (OP_REG_INC
, SB
): /* Register direct, affected increment byte. */
1415 *val
= GET_B_REG (rn
) + 1;
1417 case X (OP_REG_INC
, SW
): /* Register direct, affected increment word. */
1418 *val
= GET_W_REG (rn
) + 2;
1420 case X (OP_REG_INC
, SL
): /* Register direct, affected increment long. */
1421 *val
= GET_L_REG (rn
) + 4;
1424 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1426 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
1429 return 0; /* Success. */
1435 fetch (SIM_DESC sd
, ea_type
*arg
, int *val
)
1437 return fetch_1 (sd
, arg
, val
, 0);
1440 /* Fetch which will be followed by a store to the same location.
1441 The difference being that we don't want to do a post-increment
1442 or post-decrement at this time: we'll do it when we store. */
1445 fetch2 (SIM_DESC sd
, ea_type
*arg
, int *val
)
1447 return fetch_1 (sd
, arg
, val
, 1);
1450 /* Simulate a memory store.
1451 Return 0 for success, -1 for failure.
1455 store_1 (SIM_DESC sd
, ea_type
*arg
, int n
, int twice
)
1457 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1459 int abs
= arg
->literal
;
1464 /* Indexed register plus displacement mode:
1466 This new family of addressing modes are similar to OP_DISP
1467 (register plus displacement), with two differences:
1468 1) INDEXB uses only the least significant byte of the register,
1469 INDEXW uses only the least significant word, and
1470 INDEXL uses the entire register (just like OP_DISP).
1472 2) The displacement value in abs is multiplied by two
1473 for SW-sized operations, and by four for SL-size.
1475 This gives nine possible variations.
1478 case X (OP_INDEXB
, SB
):
1479 case X (OP_INDEXB
, SW
):
1480 case X (OP_INDEXB
, SL
):
1481 case X (OP_INDEXW
, SB
):
1482 case X (OP_INDEXW
, SW
):
1483 case X (OP_INDEXW
, SL
):
1484 case X (OP_INDEXL
, SB
):
1485 case X (OP_INDEXL
, SW
):
1486 case X (OP_INDEXL
, SL
):
1488 switch (OP_KIND (arg
->type
)) {
1489 case OP_INDEXB
: t
&= 0xff; break;
1490 case OP_INDEXW
: t
&= 0xffff; break;
1494 switch (OP_SIZE (arg
->type
)) {
1496 SET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (sd
), n
);
1499 SET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (sd
), n
);
1502 SET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (sd
), n
);
1507 case X (OP_REG
, SB
): /* Register direct, byte. */
1510 case X (OP_REG
, SW
): /* Register direct, word. */
1513 case X (OP_REG
, SL
): /* Register direct, long. */
1517 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr, byte. */
1522 t
&= h8_get_mask (sd
);
1523 SET_MEMORY_B (t
, n
);
1526 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr, word. */
1531 t
&= h8_get_mask (sd
);
1532 SET_MEMORY_W (t
, n
);
1535 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr, long. */
1540 t
&= h8_get_mask (sd
);
1541 SET_MEMORY_L (t
, n
);
1544 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr, byte. */
1549 t
&= h8_get_mask (sd
);
1550 SET_MEMORY_B (t
, n
);
1553 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr, word. */
1558 t
&= h8_get_mask (sd
);
1559 SET_MEMORY_W (t
, n
);
1562 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr, long. */
1567 t
&= h8_get_mask (sd
);
1568 SET_MEMORY_L (t
, n
);
1571 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr, byte. */
1573 SET_L_REG (rn
, t
- 1);
1574 t
&= h8_get_mask (sd
);
1575 SET_MEMORY_B (t
, n
);
1578 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr, word. */
1580 SET_L_REG (rn
, t
- 2);
1581 t
&= h8_get_mask (sd
);
1582 SET_MEMORY_W (t
, n
);
1585 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr, long. */
1587 SET_L_REG (rn
, t
- 4);
1588 t
&= h8_get_mask (sd
);
1589 SET_MEMORY_L (t
, n
);
1592 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr, byte. */
1594 SET_L_REG (rn
, t
+ 1);
1595 t
&= h8_get_mask (sd
);
1596 SET_MEMORY_B (t
, n
);
1599 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr, word. */
1601 SET_L_REG (rn
, t
+ 2);
1602 t
&= h8_get_mask (sd
);
1603 SET_MEMORY_W (t
, n
);
1606 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr, long. */
1608 SET_L_REG (rn
, t
+ 4);
1609 t
&= h8_get_mask (sd
);
1610 SET_MEMORY_L (t
, n
);
1613 case X (OP_DISP
, SB
): /* Register indirect w/displacement, byte. */
1614 t
= GET_L_REG (rn
) + abs
;
1615 t
&= h8_get_mask (sd
);
1616 SET_MEMORY_B (t
, n
);
1619 case X (OP_DISP
, SW
): /* Register indirect w/displacement, word. */
1620 t
= GET_L_REG (rn
) + abs
;
1621 t
&= h8_get_mask (sd
);
1622 SET_MEMORY_W (t
, n
);
1625 case X (OP_DISP
, SL
): /* Register indirect w/displacement, long. */
1626 t
= GET_L_REG (rn
) + abs
;
1627 t
&= h8_get_mask (sd
);
1628 SET_MEMORY_L (t
, n
);
1632 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1633 case X (OP_MEM
, SW
): /* Why isn't this implemented? */
1634 case X (OP_MEM
, SL
): /* Why isn't this implemented? */
1636 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
1645 store (SIM_DESC sd
, ea_type
*arg
, int n
)
1647 return store_1 (sd
, arg
, n
, 0);
1650 /* Store which follows a fetch from the same location.
1651 The difference being that we don't want to do a pre-increment
1652 or pre-decrement at this time: it was already done when we fetched. */
1655 store2 (SIM_DESC sd
, ea_type
*arg
, int n
)
1657 return store_1 (sd
, arg
, n
, 1);
1660 /* Flag to be set whenever a new SIM_DESC object is created. */
1661 static int init_pointers_needed
= 1;
1664 init_pointers (SIM_DESC sd
)
1666 struct h8300_sim_state
*state
= H8300_SIM_STATE (sd
);
1668 if (init_pointers_needed
)
1672 if (h8300smode
&& !h8300_normal_mode
)
1673 memory_size
= H8300S_MSIZE
;
1674 else if (h8300hmode
&& !h8300_normal_mode
)
1675 memory_size
= H8300H_MSIZE
;
1677 memory_size
= H8300_MSIZE
;
1678 /* `msize' must be a power of two. */
1679 if ((memory_size
& (memory_size
- 1)) != 0)
1683 "init_pointers: bad memory size %d, defaulting to %d.\n",
1684 memory_size
, H8300S_MSIZE
);
1685 memory_size
= H8300S_MSIZE
;
1688 if (h8_get_memory_buf (sd
))
1689 free (h8_get_memory_buf (sd
));
1691 h8_set_memory_buf (sd
, (unsigned char *)
1692 calloc (sizeof (char), memory_size
));
1693 state
->memory_size
= memory_size
;
1695 h8_set_mask (sd
, memory_size
- 1);
1697 memset (h8_get_reg_buf (sd
), 0, sizeof (((STATE_CPU (sd
, 0))->regs
)));
1699 for (i
= 0; i
< 8; i
++)
1701 /* FIXME: rewrite using local buffer. */
1702 unsigned char *p
= (unsigned char *) (h8_get_reg_buf (sd
) + i
);
1703 unsigned char *e
= (unsigned char *) (h8_get_reg_buf (sd
) + i
+ 1);
1704 unsigned short *q
= (unsigned short *) (h8_get_reg_buf (sd
) + i
);
1705 unsigned short *u
= (unsigned short *) (h8_get_reg_buf (sd
) + i
+ 1);
1706 h8_set_reg (sd
, i
, 0x00112233);
1721 wreg
[i
] = wreg
[i
+ 8] = 0;
1735 if (wreg
[i
] == 0 || wreg
[i
+ 8] == 0)
1736 sim_io_printf (sd
, "init_pointers: internal error.\n");
1738 h8_set_reg (sd
, i
, 0);
1741 init_pointers_needed
= 0;
1745 #define OBITOP(name, f, s, op) \
1746 case O (name, SB): \
1751 if (fetch (sd, &code->dst, &ea)) \
1753 if (fetch (sd, &code->src, &tmp)) \
1755 m = 1 << (tmp & 7); \
1758 if (store (sd, &code->dst,ea)) \
1764 step_once (SIM_DESC sd
, SIM_CPU
*cpu
)
1768 int tick_start
= get_now ();
1775 int c
, nz
, v
, n
, u
, h
, ui
, intMaskBit
;
1779 const struct h8300_sim_state
*state
= H8300_SIM_STATE (sd
);
1780 host_callback
*sim_callback
= STATE_CALLBACK (sd
);
1784 pc
= h8_get_pc (sd
);
1786 /* The PC should never be odd. */
1789 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGBUS
);
1793 /* Get Status Register (flags). */
1796 if (h8300smode
) /* Get exr. */
1798 trace
= (h8_get_exr (sd
) >> 7) & 1;
1799 intMask
= h8_get_exr (sd
) & 7;
1802 oldmask
= h8_get_mask (sd
);
1803 if (!h8300hmode
|| h8300_normal_mode
)
1804 h8_set_mask (sd
, 0xffff);
1807 decoded_inst _code
, *code
= &_code
;
1808 memset (code
, 0, sizeof (*code
));
1809 decode (sd
, pc
, h8_get_memory_buf (sd
) + pc
, code
);
1815 printf ("%x %d %s\n", pc
, code
->opcode
,
1816 code
->op
? code
->op
->name
: "**");
1818 h8_increment_stats (sd
, code
->opcode
);
1823 cycles
+= code
->cycles
;
1827 switch (code
->opcode
)
1829 case O (O_MOVAB
, SL
):
1830 case O (O_MOVAW
, SL
):
1831 case O (O_MOVAL
, SL
):
1832 /* 1) Evaluate 2nd argument (dst).
1833 2) Mask / zero extend according to whether 1st argument (src)
1834 is INDEXB, INDEXW, or INDEXL.
1835 3) Left-shift the result by 0, 1 or 2, according to size of mova
1836 (mova/b, mova/w, mova/l).
1837 4) Add literal value of 1st argument (src).
1838 5) Store result in 3rd argument (op3).
1841 /* Alas, since this is the only instruction with 3 arguments,
1842 decode doesn't handle them very well. Some fix-up is required.
1844 a) The size of dst is determined by whether src is
1845 INDEXB or INDEXW. */
1847 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
1848 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SB
);
1849 else if (OP_KIND (code
->src
.type
) == OP_INDEXW
)
1850 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SW
);
1852 /* b) If op3 == null, then this is the short form of the insn.
1853 Dst is the dispreg of src, and op3 is the 32-bit form
1854 of the same register.
1857 if (code
->op3
.type
== -1)
1859 /* Short form: src == INDEXB/INDEXW, dst == op3 == 0.
1860 We get to compose dst and op3 as follows:
1862 op3 is a 32-bit register, ID == src.reg.
1863 dst is the same register, but 8 or 16 bits
1864 depending on whether src is INDEXB or INDEXW.
1867 code
->op3
.type
= X (OP_REG
, SL
);
1868 code
->op3
.reg
= code
->src
.reg
;
1869 code
->op3
.literal
= 0;
1871 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
1873 code
->dst
.type
= X (OP_REG
, SB
);
1874 code
->dst
.reg
= code
->op3
.reg
+ 8;
1877 code
->dst
.type
= X (OP_REG
, SW
);
1880 if (fetch (sd
, &code
->dst
, &ea
))
1883 switch (OP_KIND (code
->src
.type
)) {
1884 case OP_INDEXB
: ea
= ea
& 0xff; break;
1885 case OP_INDEXW
: ea
= ea
& 0xffff; break;
1886 case OP_INDEXL
: break;
1887 default: goto illegal
;
1890 switch (code
->opcode
) {
1891 case O (O_MOVAB
, SL
): break;
1892 case O (O_MOVAW
, SL
): ea
= ea
<< 1; break;
1893 case O (O_MOVAL
, SL
): ea
= ea
<< 2; break;
1894 default: goto illegal
;
1897 ea
= ea
+ code
->src
.literal
;
1899 if (store (sd
, &code
->op3
, ea
))
1904 case O (O_SUBX
, SB
): /* subx, extended sub */
1905 if (fetch2 (sd
, &code
->dst
, &rd
))
1907 if (fetch (sd
, &code
->src
, &ea
))
1913 case O (O_SUBX
, SW
): /* subx, extended sub */
1914 if (fetch2 (sd
, &code
->dst
, &rd
))
1916 if (fetch (sd
, &code
->src
, &ea
))
1922 case O (O_SUBX
, SL
): /* subx, extended sub */
1923 if (fetch2 (sd
, &code
->dst
, &rd
))
1925 if (fetch (sd
, &code
->src
, &ea
))
1931 case O (O_ADDX
, SB
): /* addx, extended add */
1932 if (fetch2 (sd
, &code
->dst
, &rd
))
1934 if (fetch (sd
, &code
->src
, &ea
))
1940 case O (O_ADDX
, SW
): /* addx, extended add */
1941 if (fetch2 (sd
, &code
->dst
, &rd
))
1943 if (fetch (sd
, &code
->src
, &ea
))
1949 case O (O_ADDX
, SL
): /* addx, extended add */
1950 if (fetch2 (sd
, &code
->dst
, &rd
))
1952 if (fetch (sd
, &code
->src
, &ea
))
1958 case O (O_SUB
, SB
): /* sub.b */
1959 /* Fetch rd and ea. */
1960 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
1966 case O (O_SUB
, SW
): /* sub.w */
1967 /* Fetch rd and ea. */
1968 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
1974 case O (O_SUB
, SL
): /* sub.l */
1975 /* Fetch rd and ea. */
1976 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
1982 case O (O_NEG
, SB
): /* neg.b */
1984 if (fetch2 (sd
, &code
->src
, &ea
))
1991 case O (O_NEG
, SW
): /* neg.w */
1993 if (fetch2 (sd
, &code
->src
, &ea
))
2000 case O (O_NEG
, SL
): /* neg.l */
2002 if (fetch2 (sd
, &code
->src
, &ea
))
2009 case O (O_ADD
, SB
): /* add.b */
2010 if (fetch2 (sd
, &code
->dst
, &rd
))
2012 if (fetch (sd
, &code
->src
, &ea
))
2017 case O (O_ADD
, SW
): /* add.w */
2018 if (fetch2 (sd
, &code
->dst
, &rd
))
2020 if (fetch (sd
, &code
->src
, &ea
))
2025 case O (O_ADD
, SL
): /* add.l */
2026 if (fetch2 (sd
, &code
->dst
, &rd
))
2028 if (fetch (sd
, &code
->src
, &ea
))
2033 case O (O_AND
, SB
): /* and.b */
2034 /* Fetch rd and ea. */
2035 if (fetch2 (sd
, &code
->dst
, &rd
) || fetch (sd
, &code
->src
, &ea
))
2040 case O (O_AND
, SW
): /* and.w */
2041 /* Fetch rd and ea. */
2042 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2047 case O (O_AND
, SL
): /* and.l */
2048 /* Fetch rd and ea. */
2049 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2054 case O (O_OR
, SB
): /* or.b */
2055 /* Fetch rd and ea. */
2056 if (fetch2 (sd
, &code
->dst
, &rd
) || fetch (sd
, &code
->src
, &ea
))
2061 case O (O_OR
, SW
): /* or.w */
2062 /* Fetch rd and ea. */
2063 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2068 case O (O_OR
, SL
): /* or.l */
2069 /* Fetch rd and ea. */
2070 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2075 case O (O_XOR
, SB
): /* xor.b */
2076 /* Fetch rd and ea. */
2077 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2082 case O (O_XOR
, SW
): /* xor.w */
2083 /* Fetch rd and ea. */
2084 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2089 case O (O_XOR
, SL
): /* xor.l */
2090 /* Fetch rd and ea. */
2091 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2097 if (fetch (sd
, &code
->src
, &res
))
2099 if (store (sd
, &code
->dst
, res
))
2101 goto just_flags_log8
;
2103 if (fetch (sd
, &code
->src
, &res
))
2105 if (store (sd
, &code
->dst
, res
))
2107 goto just_flags_log16
;
2109 if (fetch (sd
, &code
->src
, &res
))
2111 if (store (sd
, &code
->dst
, res
))
2113 goto just_flags_log32
;
2115 case O (O_MOVMD
, SB
): /* movmd.b */
2122 rd
= GET_MEMORY_B (GET_L_REG (5));
2123 SET_MEMORY_B (GET_L_REG (6), rd
);
2124 SET_L_REG (5, GET_L_REG (5) + 1);
2125 SET_L_REG (6, GET_L_REG (6) + 1);
2130 case O (O_MOVMD
, SW
): /* movmd.w */
2137 rd
= GET_MEMORY_W (GET_L_REG (5));
2138 SET_MEMORY_W (GET_L_REG (6), rd
);
2139 SET_L_REG (5, GET_L_REG (5) + 2);
2140 SET_L_REG (6, GET_L_REG (6) + 2);
2145 case O (O_MOVMD
, SL
): /* movmd.l */
2152 rd
= GET_MEMORY_L (GET_L_REG (5));
2153 SET_MEMORY_L (GET_L_REG (6), rd
);
2154 SET_L_REG (5, GET_L_REG (5) + 4);
2155 SET_L_REG (6, GET_L_REG (6) + 4);
2160 case O (O_MOVSD
, SB
): /* movsd.b */
2161 /* This instruction implements strncpy, with a conditional branch.
2162 r4 contains n, r5 contains src, and r6 contains dst.
2163 The 16-bit displacement operand is added to the pc
2164 if and only if the end of string is reached before
2165 n bytes are transferred. */
2167 ea
= GET_L_REG (4) & 0xffff;
2173 rd
= GET_MEMORY_B (GET_L_REG (5));
2174 SET_MEMORY_B (GET_L_REG (6), rd
);
2175 SET_L_REG (5, GET_L_REG (5) + 1);
2176 SET_L_REG (6, GET_L_REG (6) + 1);
2183 case O (O_EEPMOV
, SB
): /* eepmov.b */
2184 case O (O_EEPMOV
, SW
): /* eepmov.w */
2185 if (h8300hmode
|| h8300smode
)
2187 register unsigned char *_src
, *_dst
;
2188 unsigned int count
= ((code
->opcode
== O (O_EEPMOV
, SW
))
2189 ? h8_get_reg (sd
, R4_REGNUM
) & 0xffff
2190 : h8_get_reg (sd
, R4_REGNUM
) & 0xff);
2192 _src
= h8_get_memory_buf (sd
) + h8_get_reg (sd
, R5_REGNUM
);
2193 if ((_src
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2195 _dst
= h8_get_memory_buf (sd
) + h8_get_reg (sd
, R6_REGNUM
);
2196 if ((_dst
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2198 memcpy (_dst
, _src
, count
);
2200 h8_set_reg (sd
, R5_REGNUM
, h8_get_reg (sd
, R5_REGNUM
) + count
);
2201 h8_set_reg (sd
, R6_REGNUM
, h8_get_reg (sd
, R6_REGNUM
) + count
);
2202 h8_set_reg (sd
, R4_REGNUM
, h8_get_reg (sd
, R4_REGNUM
) &
2203 ((code
->opcode
== O (O_EEPMOV
, SW
))
2204 ? (~0xffff) : (~0xff)));
2205 cycles
+= 2 * count
;
2210 case O (O_ADDS
, SL
): /* adds (.l) */
2212 * This insn only uses register operands, but still
2213 * it would be cleaner to use fetch and store... */
2214 SET_L_REG (code
->dst
.reg
,
2215 GET_L_REG (code
->dst
.reg
)
2216 + code
->src
.literal
);
2220 case O (O_SUBS
, SL
): /* subs (.l) */
2222 * This insn only uses register operands, but still
2223 * it would be cleaner to use fetch and store... */
2224 SET_L_REG (code
->dst
.reg
,
2225 GET_L_REG (code
->dst
.reg
)
2226 - code
->src
.literal
);
2229 case O (O_CMP
, SB
): /* cmp.b */
2230 if (fetch (sd
, &code
->dst
, &rd
))
2232 if (fetch (sd
, &code
->src
, &ea
))
2236 goto just_flags_alu8
;
2238 case O (O_CMP
, SW
): /* cmp.w */
2239 if (fetch (sd
, &code
->dst
, &rd
))
2241 if (fetch (sd
, &code
->src
, &ea
))
2245 goto just_flags_alu16
;
2247 case O (O_CMP
, SL
): /* cmp.l */
2248 if (fetch (sd
, &code
->dst
, &rd
))
2250 if (fetch (sd
, &code
->src
, &ea
))
2254 goto just_flags_alu32
;
2256 case O (O_DEC
, SB
): /* dec.b */
2258 * This insn only uses register operands, but still
2259 * it would be cleaner to use fetch and store... */
2260 rd
= GET_B_REG (code
->src
.reg
);
2263 SET_B_REG (code
->src
.reg
, res
);
2264 goto just_flags_inc8
;
2266 case O (O_DEC
, SW
): /* dec.w */
2268 * This insn only uses register operands, but still
2269 * it would be cleaner to use fetch and store... */
2270 rd
= GET_W_REG (code
->dst
.reg
);
2271 ea
= -code
->src
.literal
;
2273 SET_W_REG (code
->dst
.reg
, res
);
2274 goto just_flags_inc16
;
2276 case O (O_DEC
, SL
): /* dec.l */
2278 * This insn only uses register operands, but still
2279 * it would be cleaner to use fetch and store... */
2280 rd
= GET_L_REG (code
->dst
.reg
);
2281 ea
= -code
->src
.literal
;
2283 SET_L_REG (code
->dst
.reg
, res
);
2284 goto just_flags_inc32
;
2286 case O (O_INC
, SB
): /* inc.b */
2288 * This insn only uses register operands, but still
2289 * it would be cleaner to use fetch and store... */
2290 rd
= GET_B_REG (code
->src
.reg
);
2293 SET_B_REG (code
->src
.reg
, res
);
2294 goto just_flags_inc8
;
2296 case O (O_INC
, SW
): /* inc.w */
2298 * This insn only uses register operands, but still
2299 * it would be cleaner to use fetch and store... */
2300 rd
= GET_W_REG (code
->dst
.reg
);
2301 ea
= code
->src
.literal
;
2303 SET_W_REG (code
->dst
.reg
, res
);
2304 goto just_flags_inc16
;
2306 case O (O_INC
, SL
): /* inc.l */
2308 * This insn only uses register operands, but still
2309 * it would be cleaner to use fetch and store... */
2310 rd
= GET_L_REG (code
->dst
.reg
);
2311 ea
= code
->src
.literal
;
2313 SET_L_REG (code
->dst
.reg
, res
);
2314 goto just_flags_inc32
;
2316 case O (O_LDC
, SB
): /* ldc.b */
2317 if (fetch (sd
, &code
->src
, &res
))
2321 case O (O_LDC
, SW
): /* ldc.w */
2322 if (fetch (sd
, &code
->src
, &res
))
2325 /* Word operand, value from MSB, must be shifted. */
2329 case O (O_LDC
, SL
): /* ldc.l */
2330 if (fetch (sd
, &code
->src
, &res
))
2332 switch (code
->dst
.type
) {
2333 case X (OP_SBR
, SL
):
2334 h8_set_sbr (sd
, res
);
2336 case X (OP_VBR
, SL
):
2337 h8_set_vbr (sd
, res
);
2344 case O (O_STC
, SW
): /* stc.w */
2345 case O (O_STC
, SB
): /* stc.b */
2346 if (code
->src
.type
== X (OP_CCR
, SB
))
2349 res
= h8_get_ccr (sd
);
2351 else if (code
->src
.type
== X (OP_EXR
, SB
) && h8300smode
)
2354 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2355 res
= h8_get_exr (sd
);
2360 /* Word operand, value to MSB, must be shifted. */
2361 if (code
->opcode
== X (O_STC
, SW
))
2363 if (store (sd
, &code
->dst
, res
))
2366 case O (O_STC
, SL
): /* stc.l */
2367 switch (code
->src
.type
) {
2368 case X (OP_SBR
, SL
):
2369 res
= h8_get_sbr (sd
);
2371 case X (OP_VBR
, SL
):
2372 res
= h8_get_vbr (sd
);
2377 if (store (sd
, &code
->dst
, res
))
2381 case O (O_ANDC
, SB
): /* andc.b */
2382 if (code
->dst
.type
== X (OP_CCR
, SB
))
2385 rd
= h8_get_ccr (sd
);
2387 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2390 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2391 rd
= h8_get_exr (sd
);
2395 ea
= code
->src
.literal
;
2399 case O (O_ORC
, SB
): /* orc.b */
2400 if (code
->dst
.type
== X (OP_CCR
, SB
))
2403 rd
= h8_get_ccr (sd
);
2405 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2408 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2409 rd
= h8_get_exr (sd
);
2413 ea
= code
->src
.literal
;
2417 case O (O_XORC
, SB
): /* xorc.b */
2418 if (code
->dst
.type
== X (OP_CCR
, SB
))
2421 rd
= h8_get_ccr (sd
);
2423 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2426 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2427 rd
= h8_get_exr (sd
);
2431 ea
= code
->src
.literal
;
2435 case O (O_BRAS
, SB
): /* bra/s */
2436 /* This is basically an ordinary branch, with a delay slot. */
2437 if (fetch (sd
, &code
->src
, &res
))
2445 /* Execution continues at next instruction, but
2446 delayed_branch is set up for next cycle. */
2447 h8_set_delayed_branch (sd
, code
->next_pc
+ res
);
2451 case O (O_BRAB
, SB
): /* bra rd.b */
2452 case O (O_BRAW
, SW
): /* bra rd.w */
2453 case O (O_BRAL
, SL
): /* bra erd.l */
2454 if (fetch (sd
, &code
->src
, &rd
))
2456 switch (OP_SIZE (code
->opcode
)) {
2457 case SB
: rd
&= 0xff; break;
2458 case SW
: rd
&= 0xffff; break;
2459 case SL
: rd
&= 0xffffffff; break;
2461 pc
= code
->next_pc
+ rd
;
2464 case O (O_BRABC
, SB
): /* bra/bc, branch if bit clear */
2465 case O (O_BRABS
, SB
): /* bra/bs, branch if bit set */
2466 case O (O_BSRBC
, SB
): /* bsr/bc, call if bit clear */
2467 case O (O_BSRBS
, SB
): /* bsr/bs, call if bit set */
2468 if (fetch (sd
, &code
->dst
, &rd
) ||
2469 fetch (sd
, &code
->src
, &bit
))
2472 if (code
->opcode
== O (O_BRABC
, SB
) || /* branch if clear */
2473 code
->opcode
== O (O_BSRBC
, SB
)) /* call if clear */
2475 if ((rd
& (1 << bit
))) /* no branch */
2478 else /* branch/call if set */
2480 if (!(rd
& (1 << bit
))) /* no branch */
2484 if (fetch (sd
, &code
->op3
, &res
)) /* branch */
2486 pc
= code
->next_pc
+ res
;
2488 if (code
->opcode
== O (O_BRABC
, SB
) ||
2489 code
->opcode
== O (O_BRABS
, SB
)) /* branch */
2497 case O (O_BRA
, SB
): /* bra, branch always */
2502 case O (O_BRN
, SB
): /* brn, ;-/ branch never? */
2507 case O (O_BHI
, SB
): /* bhi */
2513 case O (O_BLS
, SB
): /* bls */
2518 case O (O_BCS
, SB
): /* bcs, branch if carry set */
2523 case O (O_BCC
, SB
): /* bcc, branch if carry clear */
2528 case O (O_BEQ
, SB
): /* beq, branch if zero set */
2532 case O (O_BGT
, SB
): /* bgt */
2533 if (((Z
|| (N
^ V
)) == 0))
2537 case O (O_BLE
, SB
): /* ble */
2538 if (((Z
|| (N
^ V
)) == 1))
2542 case O (O_BGE
, SB
): /* bge */
2546 case O (O_BLT
, SB
): /* blt */
2550 case O (O_BMI
, SB
): /* bmi */
2554 case O (O_BNE
, SB
): /* bne, branch if zero clear */
2559 case O (O_BPL
, SB
): /* bpl */
2563 case O (O_BVC
, SB
): /* bvc */
2567 case O (O_BVS
, SB
): /* bvs */
2572 /* Trap for Command Line setup. */
2573 case O (O_SYS_CMDLINE
, SB
):
2575 int i
= 0; /* Loop counter. */
2576 int j
= 0; /* Loop counter. */
2577 int ind_arg_len
= 0; /* Length of each argument. */
2578 int no_of_args
= 0; /* The no. or cmdline args. */
2579 int current_location
= 0; /* Location of string. */
2580 int old_sp
= 0; /* The Initial Stack Pointer. */
2581 int no_of_slots
= 0; /* No. of slots required on the stack
2582 for storing cmdline args. */
2583 int sp_move
= 0; /* No. of locations by which the stack needs
2585 int new_sp
= 0; /* The final stack pointer location passed
2587 int *argv_ptrs
; /* Pointers of argv strings to be stored. */
2588 int argv_ptrs_location
= 0; /* Location of pointers to cmdline
2589 args on the stack. */
2590 int char_ptr_size
= 0; /* Size of a character pointer on
2592 int addr_cmdline
= 0; /* Memory location where cmdline has
2594 int size_cmdline
= 0; /* Size of cmdline. */
2596 /* Set the address of 256 free locations where command line is
2598 addr_cmdline
= cmdline_location();
2599 h8_set_reg (sd
, 0, addr_cmdline
);
2601 /* Counting the no. of commandline arguments. */
2602 for (i
= 0; h8_get_cmdline_arg (sd
, i
) != NULL
; i
++)
2605 /* No. of arguments in the command line. */
2608 /* Current location is just a temporary variable,which we are
2609 setting to the point to the start of our commandline string. */
2610 current_location
= addr_cmdline
;
2612 /* Allocating space for storing pointers of the command line
2614 argv_ptrs
= (int *) malloc (sizeof (int) * no_of_args
);
2616 /* Setting char_ptr_size to the sizeof (char *) on the different
2618 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2627 for (i
= 0; i
< no_of_args
; i
++)
2631 /* The size of the commandline argument. */
2632 ind_arg_len
= strlen (h8_get_cmdline_arg (sd
, i
)) + 1;
2634 /* The total size of the command line string. */
2635 size_cmdline
+= ind_arg_len
;
2637 /* As we have only 256 bytes, we need to provide a graceful
2638 exit. Anyways, a program using command line arguments
2639 where we cannot store all the command line arguments
2640 given may behave unpredictably. */
2641 if (size_cmdline
>= 256)
2643 h8_set_reg (sd
, 0, 0);
2648 /* current_location points to the memory where the next
2649 commandline argument is stored. */
2650 argv_ptrs
[i
] = current_location
;
2651 for (j
= 0; j
< ind_arg_len
; j
++)
2653 SET_MEMORY_B ((current_location
+
2654 (sizeof (char) * j
)),
2655 *(h8_get_cmdline_arg (sd
, i
) +
2656 sizeof (char) * j
));
2659 /* Setting current_location to the starting of next
2661 current_location
+= ind_arg_len
;
2665 /* This is the original position of the stack pointer. */
2666 old_sp
= h8_get_reg (sd
, SP_REGNUM
);
2668 /* We need space from the stack to store the pointers to argvs. */
2669 /* As we will infringe on the stack, we need to shift the stack
2670 pointer so that the data is not overwritten. We calculate how
2671 much space is required. */
2672 sp_move
= (no_of_args
) * (char_ptr_size
);
2674 /* The final position of stack pointer, we have thus taken some
2675 space from the stack. */
2676 new_sp
= old_sp
- sp_move
;
2678 /* Temporary variable holding value where the argv pointers need
2680 argv_ptrs_location
= new_sp
;
2682 /* The argv pointers are stored at sequential locations. As per
2684 for (i
= 0; i
< no_of_args
; i
++)
2686 /* Saving the argv pointer. */
2687 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2689 SET_MEMORY_L (argv_ptrs_location
, argv_ptrs
[i
]);
2693 SET_MEMORY_W (argv_ptrs_location
, argv_ptrs
[i
]);
2696 /* The next location where the pointer to the next argv
2697 string has to be stored. */
2698 argv_ptrs_location
+= char_ptr_size
;
2701 /* Required by POSIX, Setting 0x0 at the end of the list of argv
2703 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2705 SET_MEMORY_L (old_sp
, 0x0);
2709 SET_MEMORY_W (old_sp
, 0x0);
2712 /* Freeing allocated memory. */
2714 for (i
= 0; i
<= no_of_args
; i
++)
2716 free (h8_get_cmdline_arg (sd
, i
));
2718 free (h8_get_command_line (sd
));
2720 /* The no. of argv arguments are returned in Reg 0. */
2721 h8_set_reg (sd
, 0, no_of_args
);
2722 /* The Pointer to argv in Register 1. */
2723 h8_set_reg (sd
, 1, new_sp
);
2724 /* Setting the stack pointer to the new value. */
2725 h8_set_reg (sd
, SP_REGNUM
, new_sp
);
2729 /* System call processing starts. */
2730 case O (O_SYS_OPEN
, SB
):
2732 int len
= 0; /* Length of filename. */
2733 char *filename
; /* Filename would go here. */
2734 char temp_char
; /* Temporary character */
2735 int mode
= 0; /* Mode bits for the file. */
2736 int open_return
; /* Return value of open, file descriptor. */
2737 int i
; /* Loop counter */
2738 int filename_ptr
; /* Pointer to filename in cpu memory. */
2740 /* Setting filename_ptr to first argument of open, */
2741 /* and trying to get mode. */
2742 if ((h8300sxmode
|| h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2744 filename_ptr
= GET_L_REG (0);
2745 mode
= GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
) + 4);
2749 filename_ptr
= GET_W_REG (0);
2750 mode
= GET_MEMORY_W (h8_get_reg (sd
, SP_REGNUM
) + 2);
2753 /* Trying to find the length of the filename. */
2754 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
2757 while (temp_char
!= '\0')
2759 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
2763 /* Allocating space for the filename. */
2764 filename
= (char *) malloc (sizeof (char) * len
);
2766 /* String copying the filename from memory. */
2767 for (i
= 0; i
< len
; i
++)
2769 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
2770 filename
[i
] = temp_char
;
2773 /* Callback to open and return the file descriptor. */
2774 open_return
= sim_callback
->open (sim_callback
, filename
, mode
);
2776 /* Return value in register 0. */
2777 h8_set_reg (sd
, 0, open_return
);
2779 /* Freeing memory used for filename. */
2784 case O (O_SYS_READ
, SB
):
2786 char *char_ptr
; /* Where characters read would be stored. */
2787 int fd
; /* File descriptor */
2788 int buf_size
; /* BUF_SIZE parameter in read. */
2789 int i
= 0; /* Temporary Loop counter */
2790 int read_return
= 0; /* Return value from callback to
2793 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2794 buf_size
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2796 char_ptr
= (char *) malloc (sizeof (char) * buf_size
);
2798 /* Callback to read and return the no. of characters read. */
2800 sim_callback
->read (sim_callback
, fd
, char_ptr
, buf_size
);
2802 /* The characters read are stored in cpu memory. */
2803 for (i
= 0; i
< buf_size
; i
++)
2805 SET_MEMORY_B ((h8_get_reg (sd
, 1) + (sizeof (char) * i
)),
2806 *(char_ptr
+ (sizeof (char) * i
)));
2809 /* Return value in Register 0. */
2810 h8_set_reg (sd
, 0, read_return
);
2812 /* Freeing memory used as buffer. */
2817 case O (O_SYS_WRITE
, SB
):
2819 int fd
; /* File descriptor */
2820 char temp_char
; /* Temporary character */
2821 int len
; /* Length of write, Parameter II to write. */
2822 int char_ptr
; /* Character Pointer, Parameter I of write. */
2823 char *ptr
; /* Where characters to be written are stored.
2825 int write_return
; /* Return value from callback to write. */
2826 int i
= 0; /* Loop counter */
2828 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2829 char_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2830 len
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2832 /* Allocating space for the characters to be written. */
2833 ptr
= (char *) malloc (sizeof (char) * len
);
2835 /* Fetching the characters from cpu memory. */
2836 for (i
= 0; i
< len
; i
++)
2838 temp_char
= GET_MEMORY_B (char_ptr
+ i
);
2842 /* Callback write and return the no. of characters written. */
2843 write_return
= sim_callback
->write (sim_callback
, fd
, ptr
, len
);
2845 /* Return value in Register 0. */
2846 h8_set_reg (sd
, 0, write_return
);
2848 /* Freeing memory used as buffer. */
2853 case O (O_SYS_LSEEK
, SB
):
2855 int fd
; /* File descriptor */
2856 int offset
; /* Offset */
2857 int origin
; /* Origin */
2858 int lseek_return
; /* Return value from callback to lseek. */
2860 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2861 offset
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2862 origin
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2864 /* Callback lseek and return offset. */
2866 sim_callback
->lseek (sim_callback
, fd
, offset
, origin
);
2868 /* Return value in register 0. */
2869 h8_set_reg (sd
, 0, lseek_return
);
2873 case O (O_SYS_CLOSE
, SB
):
2875 int fd
; /* File descriptor */
2876 int close_return
; /* Return value from callback to close. */
2878 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2880 /* Callback close and return. */
2881 close_return
= sim_callback
->close (sim_callback
, fd
);
2883 /* Return value in register 0. */
2884 h8_set_reg (sd
, 0, close_return
);
2888 case O (O_SYS_FSTAT
, SB
):
2890 int fd
; /* File descriptor */
2891 struct stat stat_rec
; /* Stat record */
2892 int fstat_return
; /* Return value from callback to stat. */
2893 int stat_ptr
; /* Pointer to stat record. */
2894 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
2896 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2898 /* Setting stat_ptr to second argument of stat. */
2899 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2901 /* Callback stat and return. */
2902 fstat_return
= sim_callback
->to_fstat (sim_callback
, fd
,
2905 /* Have stat_ptr point to starting of stat_rec. */
2906 temp_stat_ptr
= (char *) (&stat_rec
);
2908 /* Setting up the stat structure returned. */
2909 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
2911 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
2913 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
2915 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
2917 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
2919 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
2921 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
2923 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
2925 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
2927 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
2929 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
2931 /* Return value in register 0. */
2932 h8_set_reg (sd
, 0, fstat_return
);
2936 case O (O_SYS_STAT
, SB
):
2938 int len
= 0; /* Length of filename. */
2939 char *filename
; /* Filename would go here. */
2940 char temp_char
; /* Temporary character */
2941 int filename_ptr
; /* Pointer to filename in cpu memory. */
2942 struct stat stat_rec
; /* Stat record */
2943 int stat_return
; /* Return value from callback to stat */
2944 int stat_ptr
; /* Pointer to stat record. */
2945 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
2946 int i
= 0; /* Loop Counter */
2948 /* Setting filename_ptr to first argument of open. */
2949 filename_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2951 /* Trying to find the length of the filename. */
2952 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
2955 while (temp_char
!= '\0')
2957 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
2961 /* Allocating space for the filename. */
2962 filename
= (char *) malloc (sizeof (char) * len
);
2964 /* String copying the filename from memory. */
2965 for (i
= 0; i
< len
; i
++)
2967 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
2968 filename
[i
] = temp_char
;
2971 /* Setting stat_ptr to second argument of stat. */
2972 /* stat_ptr = h8_get_reg (sd, 1); */
2973 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2975 /* Callback stat and return. */
2977 sim_callback
->to_stat (sim_callback
, filename
, &stat_rec
);
2979 /* Have stat_ptr point to starting of stat_rec. */
2980 temp_stat_ptr
= (char *) (&stat_rec
);
2982 /* Freeing memory used for filename. */
2985 /* Setting up the stat structure returned. */
2986 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
2988 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
2990 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
2992 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
2994 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
2996 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
2998 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
3000 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
3002 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
3004 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
3006 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
3008 /* Return value in register 0. */
3009 h8_set_reg (sd
, 0, stat_return
);
3012 /* End of system call processing. */
3014 case O (O_NOT
, SB
): /* not.b */
3015 if (fetch2 (sd
, &code
->src
, &rd
))
3021 case O (O_NOT
, SW
): /* not.w */
3022 if (fetch2 (sd
, &code
->src
, &rd
))
3028 case O (O_NOT
, SL
): /* not.l */
3029 if (fetch2 (sd
, &code
->src
, &rd
))
3035 case O (O_SHLL
, SB
): /* shll.b */
3036 case O (O_SHLR
, SB
): /* shlr.b */
3037 if (fetch2 (sd
, &code
->dst
, &rd
))
3040 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3041 ea
= 1; /* unary op */
3042 else /* binary op */
3043 fetch (sd
, &code
->src
, &ea
);
3045 if (code
->opcode
== O (O_SHLL
, SB
))
3048 c
= rd
& (0x80 >> (ea
- 1));
3054 c
= rd
& (1 << (ea
- 1));
3055 rd
= (unsigned char) rd
>> ea
;
3059 case O (O_SHLL
, SW
): /* shll.w */
3060 case O (O_SHLR
, SW
): /* shlr.w */
3061 if (fetch2 (sd
, &code
->dst
, &rd
))
3064 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3065 ea
= 1; /* unary op */
3067 fetch (sd
, &code
->src
, &ea
);
3069 if (code
->opcode
== O (O_SHLL
, SW
))
3072 c
= rd
& (0x8000 >> (ea
- 1));
3078 c
= rd
& (1 << (ea
- 1));
3079 rd
= (unsigned short) rd
>> ea
;
3083 case O (O_SHLL
, SL
): /* shll.l */
3084 case O (O_SHLR
, SL
): /* shlr.l */
3085 if (fetch2 (sd
, &code
->dst
, &rd
))
3088 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3089 ea
= 1; /* unary op */
3091 fetch (sd
, &code
->src
, &ea
);
3093 if (code
->opcode
== O (O_SHLL
, SL
))
3096 c
= rd
& (0x80000000 >> (ea
- 1));
3102 c
= rd
& (1 << (ea
- 1));
3103 rd
= (unsigned int) rd
>> ea
;
3107 case O (O_SHAL
, SB
):
3108 case O (O_SHAR
, SB
):
3109 if (fetch2 (sd
, &code
->dst
, &rd
))
3112 if (code
->src
.type
== X (OP_IMM
, SB
))
3113 fetch (sd
, &code
->src
, &ea
);
3117 if (code
->opcode
== O (O_SHAL
, SB
))
3119 c
= rd
& (0x80 >> (ea
- 1));
3120 res
= rd
>> (7 - ea
);
3121 v
= ((res
& 1) && !(res
& 2))
3122 || (!(res
& 1) && (res
& 2));
3127 c
= rd
& (1 << (ea
- 1));
3129 rd
= ((signed char) rd
) >> ea
;
3133 case O (O_SHAL
, SW
):
3134 case O (O_SHAR
, SW
):
3135 if (fetch2 (sd
, &code
->dst
, &rd
))
3138 if (code
->src
.type
== X (OP_IMM
, SW
))
3139 fetch (sd
, &code
->src
, &ea
);
3143 if (code
->opcode
== O (O_SHAL
, SW
))
3145 c
= rd
& (0x8000 >> (ea
- 1));
3146 res
= rd
>> (15 - ea
);
3147 v
= ((res
& 1) && !(res
& 2))
3148 || (!(res
& 1) && (res
& 2));
3153 c
= rd
& (1 << (ea
- 1));
3155 rd
= ((signed short) rd
) >> ea
;
3159 case O (O_SHAL
, SL
):
3160 case O (O_SHAR
, SL
):
3161 if (fetch2 (sd
, &code
->dst
, &rd
))
3164 if (code
->src
.type
== X (OP_IMM
, SL
))
3165 fetch (sd
, &code
->src
, &ea
);
3169 if (code
->opcode
== O (O_SHAL
, SL
))
3171 c
= rd
& (0x80000000 >> (ea
- 1));
3172 res
= rd
>> (31 - ea
);
3173 v
= ((res
& 1) && !(res
& 2))
3174 || (!(res
& 1) && (res
& 2));
3179 c
= rd
& (1 << (ea
- 1));
3181 rd
= ((signed int) rd
) >> ea
;
3185 case O (O_ROTL
, SB
):
3186 case O (O_ROTR
, SB
):
3187 if (fetch2 (sd
, &code
->dst
, &rd
))
3190 if (code
->src
.type
== X (OP_IMM
, SB
))
3191 fetch (sd
, &code
->src
, &ea
);
3196 if (code
->opcode
== O (O_ROTL
, SB
))
3206 rd
= ((unsigned char) rd
) >> 1;
3214 case O (O_ROTL
, SW
):
3215 case O (O_ROTR
, SW
):
3216 if (fetch2 (sd
, &code
->dst
, &rd
))
3219 if (code
->src
.type
== X (OP_IMM
, SW
))
3220 fetch (sd
, &code
->src
, &ea
);
3225 if (code
->opcode
== O (O_ROTL
, SW
))
3235 rd
= ((unsigned short) rd
) >> 1;
3243 case O (O_ROTL
, SL
):
3244 case O (O_ROTR
, SL
):
3245 if (fetch2 (sd
, &code
->dst
, &rd
))
3248 if (code
->src
.type
== X (OP_IMM
, SL
))
3249 fetch (sd
, &code
->src
, &ea
);
3254 if (code
->opcode
== O (O_ROTL
, SL
))
3256 c
= rd
& 0x80000000;
3264 rd
= ((unsigned int) rd
) >> 1;
3272 case O (O_ROTXL
, SB
):
3273 case O (O_ROTXR
, SB
):
3274 if (fetch2 (sd
, &code
->dst
, &rd
))
3277 if (code
->src
.type
== X (OP_IMM
, SB
))
3278 fetch (sd
, &code
->src
, &ea
);
3283 if (code
->opcode
== O (O_ROTXL
, SB
))
3294 rd
= ((unsigned char) rd
) >> 1;
3303 case O (O_ROTXL
, SW
):
3304 case O (O_ROTXR
, SW
):
3305 if (fetch2 (sd
, &code
->dst
, &rd
))
3308 if (code
->src
.type
== X (OP_IMM
, SW
))
3309 fetch (sd
, &code
->src
, &ea
);
3314 if (code
->opcode
== O (O_ROTXL
, SW
))
3325 rd
= ((unsigned short) rd
) >> 1;
3334 case O (O_ROTXL
, SL
):
3335 case O (O_ROTXR
, SL
):
3336 if (fetch2 (sd
, &code
->dst
, &rd
))
3339 if (code
->src
.type
== X (OP_IMM
, SL
))
3340 fetch (sd
, &code
->src
, &ea
);
3345 if (code
->opcode
== O (O_ROTXL
, SL
))
3347 res
= rd
& 0x80000000;
3356 rd
= ((unsigned int) rd
) >> 1;
3367 case O (O_JMP
, SB
): /* jmp */
3369 fetch (sd
, &code
->src
, &pc
);
3374 case O (O_JSR
, SB
): /* jsr, jump to subroutine */
3376 if (fetch (sd
, &code
->src
, &pc
))
3379 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3381 if (h8300hmode
&& !h8300_normal_mode
)
3384 SET_MEMORY_L (tmp
, code
->next_pc
);
3389 SET_MEMORY_W (tmp
, code
->next_pc
);
3391 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3397 case O (O_BSR
, SB
): /* bsr, branch to subroutine */
3398 if (fetch (sd
, &code
->src
, &res
))
3400 pc
= code
->next_pc
+ res
;
3403 case O (O_RTE
, SN
): /* rte, return from exception */
3405 /* Pops exr and ccr before pc -- otherwise identical to rts. */
3406 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3408 if (h8300smode
) /* pop exr */
3410 h8_set_exr (sd
, GET_MEMORY_L (tmp
));
3413 if (h8300hmode
&& !h8300_normal_mode
)
3415 h8_set_ccr (sd
, GET_MEMORY_L (tmp
));
3417 pc
= GET_MEMORY_L (tmp
);
3422 h8_set_ccr (sd
, GET_MEMORY_W (tmp
));
3424 pc
= GET_MEMORY_W (tmp
);
3429 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3432 case O (O_RTS
, SN
): /* rts, return from subroutine */
3434 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3436 if (h8300hmode
&& !h8300_normal_mode
)
3438 pc
= GET_MEMORY_L (tmp
);
3443 pc
= GET_MEMORY_W (tmp
);
3447 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3450 case O (O_ILL
, SB
): /* illegal */
3451 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
3454 case O (O_SLEEP
, SN
): /* sleep */
3455 /* Check for magic numbers in r1 and r2. */
3456 if ((h8_get_reg (sd
, R1_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC1
&&
3457 (h8_get_reg (sd
, R2_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC2
&&
3458 SIM_WIFEXITED (h8_get_reg (sd
, 0)))
3460 /* This trap comes from _exit, not from gdb. */
3461 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_exited
,
3462 SIM_WEXITSTATUS (h8_get_reg (sd
, 0)));
3465 /* Unfortunately this won't really work, because
3466 when we take a breakpoint trap, R0 has a "random",
3467 user-defined value. Don't see any immediate solution. */
3468 else if (SIM_WIFSTOPPED (h8_get_reg (sd
, 0)))
3470 /* Pass the stop signal up to gdb. */
3471 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
,
3472 SIM_WSTOPSIG (h8_get_reg (sd
, 0)));
3477 /* Treat it as a sigtrap. */
3478 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGTRAP
);
3482 case O (O_TRAPA
, SB
): /* trapa */
3483 if (fetch (sd
, &code
->src
, &res
))
3484 goto end
; /* res is vector number. */
3486 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3487 if(h8300_normal_mode
)
3490 SET_MEMORY_W (tmp
, code
->next_pc
);
3492 SET_MEMORY_W (tmp
, h8_get_ccr (sd
));
3497 SET_MEMORY_L (tmp
, code
->next_pc
);
3499 SET_MEMORY_L (tmp
, h8_get_ccr (sd
));
3507 SET_MEMORY_L (tmp
, h8_get_exr (sd
));
3510 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3512 if(h8300_normal_mode
)
3513 pc
= GET_MEMORY_L (0x10 + res
* 2); /* Vector addresses are 0x10,0x12,0x14 and 0x16 */
3515 pc
= GET_MEMORY_L (0x20 + res
* 4);
3519 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGTRAP
);
3522 case O (O_BSETEQ
, SB
):
3527 case O (O_BSETNE
, SB
):
3532 case O (O_BCLREQ
, SB
):
3537 case O (O_BCLRNE
, SB
):
3542 OBITOP (O_BNOT
, 1, 1, ea
^= m
); /* bnot */
3543 OBITOP (O_BTST
, 1, 0, nz
= ea
& m
); /* btst */
3545 OBITOP (O_BSET
, 1, 1, ea
|= m
); /* bset */
3547 OBITOP (O_BCLR
, 1, 1, ea
&= ~m
); /* bclr */
3548 OBITOP (O_BLD
, 1, 0, c
= ea
& m
); /* bld */
3549 OBITOP (O_BILD
, 1, 0, c
= !(ea
& m
)); /* bild */
3550 OBITOP (O_BST
, 1, 1, ea
&= ~m
;
3551 if (C
) ea
|= m
); /* bst */
3552 OBITOP (O_BIST
, 1, 1, ea
&= ~m
;
3553 if (!C
) ea
|= m
); /* bist */
3554 OBITOP (O_BSTZ
, 1, 1, ea
&= ~m
;
3555 if (Z
) ea
|= m
); /* bstz */
3556 OBITOP (O_BISTZ
, 1, 1, ea
&= ~m
;
3557 if (!Z
) ea
|= m
); /* bistz */
3558 OBITOP (O_BAND
, 1, 0, c
= (ea
& m
) && C
); /* band */
3559 OBITOP (O_BIAND
, 1, 0, c
= !(ea
& m
) && C
); /* biand */
3560 OBITOP (O_BOR
, 1, 0, c
= (ea
& m
) || C
); /* bor */
3561 OBITOP (O_BIOR
, 1, 0, c
= !(ea
& m
) || C
); /* bior */
3562 OBITOP (O_BXOR
, 1, 0, c
= ((ea
& m
) != 0)!= C
); /* bxor */
3563 OBITOP (O_BIXOR
, 1, 0, c
= !(ea
& m
) != C
); /* bixor */
3565 case O (O_BFLD
, SB
): /* bfld */
3568 if (fetch (sd
, &code
->src
, &bit
))
3573 if (fetch (sd
, &code
->dst
, &ea
))
3583 if (store (sd
, &code
->op3
, ea
))
3588 case O(O_BFST
, SB
): /* bfst */
3589 /* bitfield store */
3590 /* NOTE: the imm8 value is in dst, and the ea value
3591 (which is actually the destination) is in op3.
3592 It has to be that way, to avoid breaking the assembler. */
3594 if (fetch (sd
, &code
->dst
, &bit
)) /* imm8 */
3596 if (bit
== 0) /* noop -- nothing to do. */
3599 if (fetch (sd
, &code
->src
, &rd
)) /* reg8 src */
3602 if (fetch2 (sd
, &code
->op3
, &ea
)) /* ea dst */
3605 /* Left-shift the register data into position. */
3606 for (tmp
= bit
; !(tmp
& 1); tmp
>>= 1)
3609 /* Combine it with the neighboring bits. */
3610 ea
= (ea
& ~bit
) | (rd
& bit
);
3613 if (store2 (sd
, &code
->op3
, ea
))
3617 case O (O_CLRMAC
, SN
): /* clrmac */
3618 h8_set_mach (sd
, 0);
3619 h8_set_macl (sd
, 0);
3620 h8_set_macZ (sd
, 1);
3621 h8_set_macV (sd
, 0);
3622 h8_set_macN (sd
, 0);
3625 case O (O_STMAC
, SL
): /* stmac, 260 */
3626 switch (code
->src
.type
) {
3627 case X (OP_MACH
, SL
):
3628 res
= h8_get_mach (sd
);
3629 if (res
& 0x200) /* sign extend */
3632 case X (OP_MACL
, SL
):
3633 res
= h8_get_macl (sd
);
3635 default: goto illegal
;
3637 nz
= !h8_get_macZ (sd
);
3638 n
= h8_get_macN (sd
);
3639 v
= h8_get_macV (sd
);
3641 if (store (sd
, &code
->dst
, res
))
3646 case O (O_LDMAC
, SL
): /* ldmac, 179 */
3647 if (fetch (sd
, &code
->src
, &rd
))
3650 switch (code
->dst
.type
) {
3651 case X (OP_MACH
, SL
):
3652 rd
&= 0x3ff; /* Truncate to 10 bits */
3653 h8_set_mach (sd
, rd
);
3655 case X (OP_MACL
, SL
):
3656 h8_set_macl (sd
, rd
);
3658 default: goto illegal
;
3660 h8_set_macV (sd
, 0);
3664 if (fetch (sd
, &code
->src
, &rd
) ||
3665 fetch (sd
, &code
->dst
, &res
))
3668 /* Ye gods, this is non-portable!
3669 However, the existing mul/div code is similar. */
3670 res
= SEXTSHORT (res
) * SEXTSHORT (rd
);
3672 if (h8_get_macS (sd
)) /* Saturating mode */
3674 long long mac
= h8_get_macl (sd
);
3676 if (mac
& 0x80000000) /* sign extend */
3677 mac
|= 0xffffffff00000000LL
;
3680 if (mac
> 0x7fffffff || mac
< 0xffffffff80000000LL
)
3681 h8_set_macV (sd
, 1);
3682 h8_set_macZ (sd
, (mac
== 0));
3683 h8_set_macN (sd
, (mac
< 0));
3684 h8_set_macl (sd
, (int) mac
);
3686 else /* "Less Saturating" mode */
3688 long long mac
= h8_get_mach (sd
);
3690 mac
+= h8_get_macl (sd
);
3692 if (mac
& 0x20000000000LL
) /* sign extend */
3693 mac
|= 0xfffffc0000000000LL
;
3696 if (mac
> 0x1ffffffffffLL
||
3697 mac
< (long long) 0xfffffe0000000000LL
)
3698 h8_set_macV (sd
, 1);
3699 h8_set_macZ (sd
, (mac
== 0));
3700 h8_set_macN (sd
, (mac
< 0));
3701 h8_set_macl (sd
, (int) mac
);
3703 h8_set_mach (sd
, (int) (mac
& 0x3ff));
3707 case O (O_MULS
, SW
): /* muls.w */
3708 if (fetch (sd
, &code
->src
, &ea
) ||
3709 fetch (sd
, &code
->dst
, &rd
))
3712 ea
= SEXTSHORT (ea
);
3713 res
= SEXTSHORT (ea
* SEXTSHORT (rd
));
3717 if (store (sd
, &code
->dst
, res
))
3722 case O (O_MULS
, SL
): /* muls.l */
3723 if (fetch (sd
, &code
->src
, &ea
) ||
3724 fetch (sd
, &code
->dst
, &rd
))
3729 n
= res
& 0x80000000;
3730 nz
= res
& 0xffffffff;
3731 if (store (sd
, &code
->dst
, res
))
3735 case O (O_MULSU
, SL
): /* muls/u.l */
3736 if (fetch (sd
, &code
->src
, &ea
) ||
3737 fetch (sd
, &code
->dst
, &rd
))
3740 /* Compute upper 32 bits of the 64-bit result. */
3741 res
= (((long long) ea
) * ((long long) rd
)) >> 32;
3743 n
= res
& 0x80000000;
3744 nz
= res
& 0xffffffff;
3745 if (store (sd
, &code
->dst
, res
))
3749 case O (O_MULU
, SW
): /* mulu.w */
3750 if (fetch (sd
, &code
->src
, &ea
) ||
3751 fetch (sd
, &code
->dst
, &rd
))
3754 res
= UEXTSHORT ((UEXTSHORT (ea
) * UEXTSHORT (rd
)));
3756 /* Don't set Z or N. */
3757 if (store (sd
, &code
->dst
, res
))
3762 case O (O_MULU
, SL
): /* mulu.l */
3763 if (fetch (sd
, &code
->src
, &ea
) ||
3764 fetch (sd
, &code
->dst
, &rd
))
3769 /* Don't set Z or N. */
3770 if (store (sd
, &code
->dst
, res
))
3775 case O (O_MULUU
, SL
): /* mulu/u.l */
3776 if (fetch (sd
, &code
->src
, &ea
) ||
3777 fetch (sd
, &code
->dst
, &rd
))
3780 /* Compute upper 32 bits of the 64-bit result. */
3781 res
= (((unsigned long long) (unsigned) ea
) *
3782 ((unsigned long long) (unsigned) rd
)) >> 32;
3784 /* Don't set Z or N. */
3785 if (store (sd
, &code
->dst
, res
))
3790 case O (O_MULXS
, SB
): /* mulxs.b */
3791 if (fetch (sd
, &code
->src
, &ea
) ||
3792 fetch (sd
, &code
->dst
, &rd
))
3796 res
= ea
* SEXTCHAR (rd
);
3800 if (store (sd
, &code
->dst
, res
))
3805 case O (O_MULXS
, SW
): /* mulxs.w */
3806 if (fetch (sd
, &code
->src
, &ea
) ||
3807 fetch (sd
, &code
->dst
, &rd
))
3810 ea
= SEXTSHORT (ea
);
3811 res
= ea
* SEXTSHORT (rd
& 0xffff);
3813 n
= res
& 0x80000000;
3814 nz
= res
& 0xffffffff;
3815 if (store (sd
, &code
->dst
, res
))
3820 case O (O_MULXU
, SB
): /* mulxu.b */
3821 if (fetch (sd
, &code
->src
, &ea
) ||
3822 fetch (sd
, &code
->dst
, &rd
))
3825 res
= UEXTCHAR (ea
) * UEXTCHAR (rd
);
3827 if (store (sd
, &code
->dst
, res
))
3832 case O (O_MULXU
, SW
): /* mulxu.w */
3833 if (fetch (sd
, &code
->src
, &ea
) ||
3834 fetch (sd
, &code
->dst
, &rd
))
3837 res
= UEXTSHORT (ea
) * UEXTSHORT (rd
);
3839 if (store (sd
, &code
->dst
, res
))
3844 case O (O_TAS
, SB
): /* tas (test and set) */
3845 if (!h8300sxmode
) /* h8sx can use any register. */
3846 switch (code
->src
.reg
)
3857 if (fetch (sd
, &code
->src
, &res
))
3859 if (store (sd
, &code
->src
, res
| 0x80))
3862 goto just_flags_log8
;
3864 case O (O_DIVU
, SW
): /* divu.w */
3865 if (fetch (sd
, &code
->src
, &ea
) ||
3866 fetch (sd
, &code
->dst
, &rd
))
3872 res
= (unsigned) (UEXTSHORT (rd
) / UEXTSHORT (ea
));
3876 if (store (sd
, &code
->dst
, res
))
3880 case O (O_DIVU
, SL
): /* divu.l */
3881 if (fetch (sd
, &code
->src
, &ea
) ||
3882 fetch (sd
, &code
->dst
, &rd
))
3885 n
= ea
& 0x80000000;
3886 nz
= ea
& 0xffffffff;
3888 res
= (unsigned) rd
/ ea
;
3892 if (store (sd
, &code
->dst
, res
))
3896 case O (O_DIVS
, SW
): /* divs.w */
3897 if (fetch (sd
, &code
->src
, &ea
) ||
3898 fetch (sd
, &code
->dst
, &rd
))
3903 res
= SEXTSHORT (rd
) / SEXTSHORT (ea
);
3913 if (store (sd
, &code
->dst
, res
))
3917 case O (O_DIVS
, SL
): /* divs.l */
3918 if (fetch (sd
, &code
->src
, &ea
) ||
3919 fetch (sd
, &code
->dst
, &rd
))
3933 n
= res
& 0x80000000;
3934 if (store (sd
, &code
->dst
, res
))
3938 case O (O_DIVXU
, SB
): /* divxu.b */
3939 if (fetch (sd
, &code
->src
, &ea
) ||
3940 fetch (sd
, &code
->dst
, &rd
))
3943 rd
= UEXTSHORT (rd
);
3950 tmp
= (unsigned) rd
% ea
;
3951 res
= (unsigned) rd
/ ea
;
3959 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
3963 case O (O_DIVXU
, SW
): /* divxu.w */
3964 if (fetch (sd
, &code
->src
, &ea
) ||
3965 fetch (sd
, &code
->dst
, &rd
))
3968 ea
= UEXTSHORT (ea
);
3974 tmp
= (unsigned) rd
% ea
;
3975 res
= (unsigned) rd
/ ea
;
3983 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
3987 case O (O_DIVXS
, SB
): /* divxs.b */
3988 if (fetch (sd
, &code
->src
, &ea
) ||
3989 fetch (sd
, &code
->dst
, &rd
))
3992 rd
= SEXTSHORT (rd
);
3997 tmp
= (int) rd
% (int) ea
;
3998 res
= (int) rd
/ (int) ea
;
4009 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
4013 case O (O_DIVXS
, SW
): /* divxs.w */
4014 if (fetch (sd
, &code
->src
, &ea
) ||
4015 fetch (sd
, &code
->dst
, &rd
))
4018 ea
= SEXTSHORT (ea
);
4022 tmp
= (int) rd
% (int) ea
;
4023 res
= (int) rd
/ (int) ea
;
4033 n
= res
& 0x80000000;
4034 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
4038 case O (O_EXTS
, SW
): /* exts.w, signed extend */
4039 if (fetch2 (sd
, &code
->dst
, &rd
))
4041 ea
= rd
& 0x80 ? -256 : 0;
4042 res
= (rd
& 0xff) + ea
;
4045 case O (O_EXTS
, SL
): /* exts.l, signed extend */
4046 if (fetch2 (sd
, &code
->dst
, &rd
))
4048 if (code
->src
.type
== X (OP_IMM
, SL
))
4050 if (fetch (sd
, &code
->src
, &ea
))
4053 if (ea
== 2) /* exts.l #2, nn */
4055 /* Sign-extend from 8-bit to 32-bit. */
4056 ea
= rd
& 0x80 ? -256 : 0;
4057 res
= (rd
& 0xff) + ea
;
4061 /* Sign-extend from 16-bit to 32-bit. */
4062 ea
= rd
& 0x8000 ? -65536 : 0;
4063 res
= (rd
& 0xffff) + ea
;
4066 case O (O_EXTU
, SW
): /* extu.w, unsigned extend */
4067 if (fetch2 (sd
, &code
->dst
, &rd
))
4070 res
= (rd
& 0xff) + ea
;
4073 case O (O_EXTU
, SL
): /* extu.l, unsigned extend */
4074 if (fetch2 (sd
, &code
->dst
, &rd
))
4076 if (code
->src
.type
== X (OP_IMM
, SL
))
4078 if (fetch (sd
, &code
->src
, &ea
))
4081 if (ea
== 2) /* extu.l #2, nn */
4083 /* Zero-extend from 8-bit to 32-bit. */
4085 res
= (rd
& 0xff) + ea
;
4089 /* Zero-extend from 16-bit to 32-bit. */
4091 res
= (rd
& 0xffff) + ea
;
4094 case O (O_NOP
, SN
): /* nop */
4097 case O (O_STM
, SL
): /* stm, store to memory */
4099 int nregs
, firstreg
, i
;
4101 nregs
= GET_MEMORY_B (pc
+ 1);
4104 firstreg
= code
->src
.reg
;
4106 for (i
= firstreg
; i
<= firstreg
+ nregs
; i
++)
4108 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) - 4);
4109 SET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
), h8_get_reg (sd
, i
));
4114 case O (O_LDM
, SL
): /* ldm, load from memory */
4115 case O (O_RTEL
, SN
): /* rte/l, ldm plus rte */
4116 case O (O_RTSL
, SN
): /* rts/l, ldm plus rts */
4118 int nregs
, firstreg
, i
;
4120 nregs
= ((GET_MEMORY_B (pc
+ 1) >> 4) & 0xf);
4121 firstreg
= code
->dst
.reg
& 0xf;
4122 for (i
= firstreg
; i
>= firstreg
- nregs
; i
--)
4124 h8_set_reg (sd
, i
, GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
)));
4125 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) + 4);
4128 switch (code
->opcode
) {
4129 case O (O_RTEL
, SN
):
4131 case O (O_RTSL
, SN
):
4140 /* Decimal Adjust Addition. This is for BCD arithmetic. */
4141 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch? */
4142 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4143 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4144 res
= res
; /* Value added == 0. */
4145 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4146 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4147 res
= res
+ 0x6; /* Value added == 6. */
4148 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4149 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4150 res
= res
+ 0x6; /* Value added == 6. */
4151 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4152 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4153 res
= res
+ 0x60; /* Value added == 60. */
4154 else if (!c
&& (9 <= (res
>> 4) && (res
>> 4) <= 15) &&
4155 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4156 res
= res
+ 0x66; /* Value added == 66. */
4157 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4158 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4159 res
= res
+ 0x66; /* Value added == 66. */
4160 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4161 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4162 res
= res
+ 0x60; /* Value added == 60. */
4163 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4164 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4165 res
= res
+ 0x66; /* Value added == 66. */
4166 else if (c
&& (1 <= (res
>> 4) && (res
>> 4) <= 3) &&
4167 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4168 res
= res
+ 0x66; /* Value added == 66. */
4173 /* Decimal Adjust Subtraction. This is for BCD arithmetic. */
4174 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch, fetch2... */
4175 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4176 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4177 res
= res
; /* Value added == 0. */
4178 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4179 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4180 res
= res
+ 0xfa; /* Value added == 0xfa. */
4181 else if ( c
&& (7 <= (res
>> 4) && (res
>> 4) <= 15) &&
4182 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4183 res
= res
+ 0xa0; /* Value added == 0xa0. */
4184 else if (c
&& (6 <= (res
>> 4) && (res
>> 4) <= 15) &&
4185 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4186 res
= res
+ 0x9a; /* Value added == 0x9a. */
4192 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
4197 sim_io_printf (sd
, "sim_resume: internal error.\n");
4198 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
4202 if (code
->dst
.type
== X (OP_CCR
, SB
) ||
4203 code
->dst
.type
== X (OP_CCR
, SW
))
4205 h8_set_ccr (sd
, res
);
4208 else if (h8300smode
&&
4209 (code
->dst
.type
== X (OP_EXR
, SB
) ||
4210 code
->dst
.type
== X (OP_EXR
, SW
)))
4212 h8_set_exr (sd
, res
);
4213 if (h8300smode
) /* Get exr. */
4215 trace
= (h8_get_exr (sd
) >> 7) & 1;
4216 intMask
= h8_get_exr (sd
) & 7;
4225 /* When a branch works */
4226 if (fetch (sd
, &code
->src
, &res
))
4228 if (res
& 1) /* bad address */
4230 pc
= code
->next_pc
+ res
;
4233 /* Set the cond codes from res */
4236 /* Set the flags after an 8 bit inc/dec operation */
4240 v
= (rd
& 0x7f) == 0x7f;
4243 /* Set the flags after an 16 bit inc/dec operation */
4247 v
= (rd
& 0x7fff) == 0x7fff;
4250 /* Set the flags after an 32 bit inc/dec operation */
4252 n
= res
& 0x80000000;
4253 nz
= res
& 0xffffffff;
4254 v
= (rd
& 0x7fffffff) == 0x7fffffff;
4258 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
4261 if (store2 (sd
, &code
->dst
, rd
))
4266 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
4269 if (store2 (sd
, &code
->dst
, rd
))
4274 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
4275 n
= (rd
& 0x80000000);
4276 nz
= rd
& 0xffffffff;
4277 if (store2 (sd
, &code
->dst
, rd
))
4282 if (store2 (sd
, &code
->dst
, res
))
4286 /* flags after a 32bit logical operation */
4287 n
= res
& 0x80000000;
4288 nz
= res
& 0xffffffff;
4293 if (store2 (sd
, &code
->dst
, res
))
4297 /* flags after a 16bit logical operation */
4304 if (store2 (sd
, &code
->dst
, res
))
4314 if (store2 (sd
, &code
->dst
, res
))
4321 switch (code
->opcode
/ 4)
4325 v
= ((rd
& 0x80) == (ea
& 0x80)
4326 && (rd
& 0x80) != (res
& 0x80));
4331 v
= ((rd
& 0x80) != (-ea
& 0x80)
4332 && (rd
& 0x80) != (res
& 0x80));
4339 break; /* No effect on v flag. */
4344 if (store2 (sd
, &code
->dst
, res
))
4350 c
= (res
& 0x10000);
4351 switch (code
->opcode
/ 4)
4355 v
= ((rd
& 0x8000) == (ea
& 0x8000)
4356 && (rd
& 0x8000) != (res
& 0x8000));
4361 v
= ((rd
& 0x8000) != (-ea
& 0x8000)
4362 && (rd
& 0x8000) != (res
& 0x8000));
4371 if (store2 (sd
, &code
->dst
, res
))
4375 n
= res
& 0x80000000;
4376 nz
= res
& 0xffffffff;
4377 switch (code
->opcode
/ 4)
4381 v
= ((rd
& 0x80000000) == (ea
& 0x80000000)
4382 && (rd
& 0x80000000) != (res
& 0x80000000));
4383 c
= ((unsigned) res
< (unsigned) rd
) ||
4384 ((unsigned) res
< (unsigned) ea
);
4389 v
= ((rd
& 0x80000000) != (-ea
& 0x80000000)
4390 && (rd
& 0x80000000) != (res
& 0x80000000));
4391 c
= (unsigned) rd
< (unsigned) -ea
;
4394 v
= (rd
== 0x80000000);
4401 if ((res
= h8_get_delayed_branch (sd
)) != 0)
4404 h8_set_delayed_branch (sd
, 0);
4412 h8_set_ticks (sd
, h8_get_ticks (sd
) + get_now () - tick_start
);
4413 h8_set_cycles (sd
, h8_get_cycles (sd
) + cycles
);
4414 h8_set_insts (sd
, h8_get_insts (sd
) + insts
);
4419 h8_set_exr (sd
, (trace
<<7) | intMask
);
4421 h8_set_mask (sd
, oldmask
);
4425 sim_engine_run (SIM_DESC sd
,
4426 int next_cpu_nr
, /* ignore */
4427 int nr_cpus
, /* ignore */
4432 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4434 cpu
= STATE_CPU (sd
, 0);
4438 step_once (sd
, cpu
);
4439 if (sim_events_tick (sd
))
4440 sim_events_process (sd
);
4445 sim_write (SIM_DESC sd
, SIM_ADDR addr
, const unsigned char *buffer
, int size
)
4452 for (i
= 0; i
< size
; i
++)
4454 if (addr
< memory_size
)
4456 h8_set_memory (sd
, addr
+ i
, buffer
[i
]);
4465 sim_read (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
4470 if (addr
+ size
< memory_size
)
4471 memcpy (buffer
, h8_get_memory_buf (sd
) + addr
, size
);
4478 h8300_reg_store (SIM_CPU
*cpu
, int rn
, unsigned char *value
, int length
)
4483 longval
= (value
[0] << 24) | (value
[1] << 16) | (value
[2] << 8) | value
[3];
4484 shortval
= (value
[0] << 8) | (value
[1]);
4485 intval
= h8300hmode
? longval
: shortval
;
4487 init_pointers (CPU_STATE (cpu
));
4491 if(h8300_normal_mode
)
4492 cpu
->pc
= shortval
; /* PC for Normal mode is 2 bytes */
4512 cpu
->regs
[rn
] = intval
;
4517 cpu
->regs
[rn
] = longval
;
4524 h8300_reg_fetch (SIM_CPU
*cpu
, int rn
, unsigned char *buf
, int length
)
4529 init_pointers (CPU_STATE (cpu
));
4531 if (!h8300smode
&& rn
>= EXR_REGNUM
)
4566 /* In Normal mode PC is 2 byte, but other registers are 4 byte */
4567 if ((h8300hmode
|| longreg
) && !(rn
== PC_REGNUM
&& h8300_normal_mode
))
4584 sim_info (SIM_DESC sd
, int verbose
)
4586 const struct h8300_sim_state
*state
= H8300_SIM_STATE (sd
);
4587 double timetaken
= (double) h8_get_ticks (sd
) / (double) now_persec ();
4588 double virttime
= h8_get_cycles (sd
) / 10.0e6
;
4590 sim_io_printf (sd
, "\n\n#instructions executed %10d\n", h8_get_insts (sd
));
4591 sim_io_printf (sd
, "#cycles (v approximate) %10d\n", h8_get_cycles (sd
));
4592 sim_io_printf (sd
, "#real time taken %10.4f\n", timetaken
);
4593 sim_io_printf (sd
, "#virtual time taken %10.4f\n", virttime
);
4594 if (timetaken
!= 0.0)
4595 sim_io_printf (sd
, "#simulation ratio %10.4f\n", virttime
/ timetaken
);
4598 /* This to be conditional on `what' (aka `verbose'),
4599 however it was never passed as non-zero. */
4603 for (i
= 0; i
< O_LAST
; i
++)
4605 if (h8_get_stats (sd
, i
))
4606 sim_io_printf (sd
, "%d: %d\n", i
, h8_get_stats (sd
, i
));
4612 /* Indicate whether the cpu is an H8/300 or H8/300H.
4613 FLAG is non-zero for the H8/300H. */
4616 set_h8300h (unsigned long machine
)
4618 /* FIXME: Much of the code in sim_load can be moved to sim_open.
4619 This function being replaced by a sim_open:ARGV configuration
4622 h8300hmode
= h8300smode
= h8300sxmode
= h8300_normal_mode
= 0;
4624 if (machine
== bfd_mach_h8300sx
|| machine
== bfd_mach_h8300sxn
)
4627 if (machine
== bfd_mach_h8300s
|| machine
== bfd_mach_h8300sn
|| h8300sxmode
)
4630 if (machine
== bfd_mach_h8300h
|| machine
== bfd_mach_h8300hn
|| h8300smode
)
4633 if(machine
== bfd_mach_h8300hn
|| machine
== bfd_mach_h8300sn
|| machine
== bfd_mach_h8300sxn
)
4634 h8300_normal_mode
= 1;
4637 /* H8300-specific options.
4638 TODO: These really should be merged into the common model modules. */
4646 h8300_option_handler (SIM_DESC sd
, sim_cpu
*cpu ATTRIBUTE_UNUSED
, int opt
,
4647 char *arg
, int is_command ATTRIBUTE_UNUSED
)
4649 switch ((H8300_OPTIONS
) opt
)
4652 set_h8300h (bfd_mach_h8300h
);
4655 set_h8300h (bfd_mach_h8300s
);
4657 case OPTION_H8300SX
:
4658 set_h8300h (bfd_mach_h8300sx
);
4662 /* We'll actually never get here; the caller handles the error
4664 sim_io_eprintf (sd
, "Unknown option `%s'\n", arg
);
4671 static const OPTION h8300_options
[] =
4673 { {"h8300h", no_argument
, NULL
, OPTION_H8300H
},
4674 'h', NULL
, "Indicate the CPU is H8/300H",
4675 h8300_option_handler
},
4676 { {"h8300s", no_argument
, NULL
, OPTION_H8300S
},
4677 'S', NULL
, "Indicate the CPU is H8S",
4678 h8300_option_handler
},
4679 { {"h8300sx", no_argument
, NULL
, OPTION_H8300SX
},
4680 'x', NULL
, "Indicate the CPU is H8SX",
4681 h8300_option_handler
},
4682 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
4686 h8300_pc_get (sim_cpu
*cpu
)
4692 h8300_pc_set (sim_cpu
*cpu
, sim_cia pc
)
4697 /* Cover function of sim_state_free to free the cpu buffers as well. */
4700 free_state (SIM_DESC sd
)
4702 if (STATE_MODULES (sd
) != NULL
)
4703 sim_module_uninstall (sd
);
4705 /* Fixme: free buffers in _sim_cpu. */
4706 sim_state_free (sd
);
4710 sim_open (SIM_OPEN_KIND kind
,
4711 struct host_callback_struct
*callback
,
4719 sd
= sim_state_alloc_extra (kind
, callback
, sizeof (struct h8300_sim_state
));
4721 /* Set default options before parsing user options. */
4722 current_target_byte_order
= BFD_ENDIAN_BIG
;
4724 /* The cpu data is kept in a separately allocated chunk of memory. */
4725 if (sim_cpu_alloc_all (sd
, 1) != SIM_RC_OK
)
4731 cpu
= STATE_CPU (sd
, 0);
4732 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4733 cpu
->regs
[SBR_REGNUM
] = 0xFFFFFF00;
4734 /* sim_cpu object is new, so some initialization is needed. */
4735 init_pointers_needed
= 1;
4737 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
4743 if (sim_add_option_table (sd
, NULL
, h8300_options
) != SIM_RC_OK
)
4749 /* The parser will print an error message for us, so we silently return. */
4750 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
4752 /* Uninstall the modules to avoid memory leaks,
4753 file descriptor leaks, etc. */
4758 /* Check for/establish the a reference program image. */
4759 if (sim_analyze_program (sd
, STATE_PROG_FILE (sd
), abfd
) != SIM_RC_OK
)
4765 /* Establish any remaining configuration options. */
4766 if (sim_config (sd
) != SIM_RC_OK
)
4772 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
4774 /* Uninstall the modules to avoid memory leaks,
4775 file descriptor leaks, etc. */
4780 /* CPU specific initialization. */
4781 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
4783 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
4785 CPU_REG_FETCH (cpu
) = h8300_reg_fetch
;
4786 CPU_REG_STORE (cpu
) = h8300_reg_store
;
4787 CPU_PC_FETCH (cpu
) = h8300_pc_get
;
4788 CPU_PC_STORE (cpu
) = h8300_pc_set
;
4791 /* sim_hw_configure (sd); */
4793 /* FIXME: Much of the code in sim_load can be moved here. */
4798 /* Called by gdb to load a program into memory. */
4801 sim_load (SIM_DESC sd
, const char *prog
, bfd
*abfd
, int from_tty
)
4803 struct h8300_sim_state
*state
= H8300_SIM_STATE (sd
);
4806 /* FIXME: The code below that sets a specific variant of the H8/300
4807 being simulated should be moved to sim_open(). */
4809 /* See if the file is for the H8/300 or H8/300H. */
4810 /* ??? This may not be the most efficient way. The z8k simulator
4811 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
4815 prog_bfd
= bfd_openr (prog
, NULL
);
4816 if (prog_bfd
!= NULL
)
4818 /* Set the cpu type. We ignore failure from bfd_check_format
4819 and bfd_openr as sim_load_file checks too. */
4820 if (bfd_check_format (prog_bfd
, bfd_object
))
4822 set_h8300h (bfd_get_mach (prog_bfd
));
4826 /* If we're using gdb attached to the simulator, then we have to
4827 reallocate memory for the simulator.
4829 When gdb first starts, it calls fetch_registers (among other
4830 functions), which in turn calls init_pointers, which allocates
4833 The problem is when we do that, we don't know whether we're
4834 debugging an H8/300 or H8/300H program.
4836 This is the first point at which we can make that determination,
4837 so we just reallocate memory now; this will also allow us to handle
4838 switching between H8/300 and H8/300H programs without exiting
4841 if (h8300smode
&& !h8300_normal_mode
)
4842 memory_size
= H8300S_MSIZE
;
4843 else if (h8300hmode
&& !h8300_normal_mode
)
4844 memory_size
= H8300H_MSIZE
;
4846 memory_size
= H8300_MSIZE
;
4848 if (h8_get_memory_buf (sd
))
4849 free (h8_get_memory_buf (sd
));
4851 h8_set_memory_buf (sd
, (unsigned char *)
4852 calloc (sizeof (char), memory_size
));
4853 state
->memory_size
= memory_size
;
4855 /* `msize' must be a power of two. */
4856 if ((memory_size
& (memory_size
- 1)) != 0)
4858 sim_io_printf (sd
, "sim_load: bad memory size.\n");
4861 h8_set_mask (sd
, memory_size
- 1);
4863 if (sim_load_file (sd
, STATE_MY_NAME (sd
), STATE_CALLBACK (sd
), prog
,
4864 prog_bfd
, STATE_OPEN_KIND (sd
) == SIM_OPEN_DEBUG
,
4868 /* Close the bfd if we opened it. */
4869 if (abfd
== NULL
&& prog_bfd
!= NULL
)
4870 bfd_close (prog_bfd
);
4874 /* Close the bfd if we opened it. */
4875 if (abfd
== NULL
&& prog_bfd
!= NULL
)
4876 bfd_close (prog_bfd
);
4881 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
4882 char * const *argv
, char * const *env
)
4889 h8_set_pc (sd
, bfd_get_start_address (abfd
));
4893 /* Command Line support. */
4896 /* Counting the no. of commandline arguments. */
4897 for (no_of_args
= 0; argv
[no_of_args
] != NULL
; no_of_args
++)
4900 /* Allocating memory for the argv pointers. */
4901 h8_set_command_line (sd
, (char **) malloc ((sizeof (char *))
4902 * (no_of_args
+ 1)));
4904 for (i
= 0; i
< no_of_args
; i
++)
4906 /* Copying the argument string. */
4907 h8_set_cmdline_arg (sd
, i
, (char *) strdup (argv
[i
]));
4909 h8_set_cmdline_arg (sd
, i
, NULL
);