2 * Simulator for the Renesas (formerly Hitachi) H8/300 architecture.
4 * Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com
6 * This file is part of H8/300 sim
9 * THIS SOFTWARE IS NOT COPYRIGHTED
11 * Cygnus offers the following for use in the public domain. Cygnus makes no
12 * warranty with regard to the software or its performance and the user
13 * accepts the software "AS IS" with all faults.
15 * CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS
16 * SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE.
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
34 #include "gdb/sim-h8300.h"
36 #include "sys/types.h"
37 #include "sim-options.h"
45 /* FIXME: Needs to live in header file.
46 This header should also include the things in remote-sim.h.
47 One could move this to remote-sim.h but this function isn't needed
49 static void set_simcache_size (SIM_DESC
, int);
51 #define X(op, size) (op * 4 + size)
53 #define SP (h8300hmode && !h8300_normal_mode ? SL : SW)
55 #define h8_opcodes ops
57 #include "opcode/h8300.h"
59 /* CPU data object: */
62 h8_get_pc (SIM_DESC sd
)
64 return (STATE_CPU (sd
, 0)) -> pc
;
68 h8_set_pc (SIM_DESC sd
, unsigned int val
)
70 (STATE_CPU (sd
, 0)) -> pc
= val
;
74 h8_get_ccr (SIM_DESC sd
)
76 return (STATE_CPU (sd
, 0)) -> regs
[CCR_REGNUM
];
80 h8_set_ccr (SIM_DESC sd
, unsigned int val
)
82 (STATE_CPU (sd
, 0)) -> regs
[CCR_REGNUM
] = val
;
86 h8_get_exr (SIM_DESC sd
)
88 return (STATE_CPU (sd
, 0)) -> regs
[EXR_REGNUM
];
92 h8_set_exr (SIM_DESC sd
, unsigned int val
)
94 (STATE_CPU (sd
, 0)) -> regs
[EXR_REGNUM
] = val
;
98 h8_get_sbr (SIM_DESC sd
)
100 return (STATE_CPU (sd
, 0)) -> regs
[SBR_REGNUM
];
104 h8_set_sbr (SIM_DESC sd
, int val
)
106 (STATE_CPU (sd
, 0)) -> regs
[SBR_REGNUM
] = val
;
110 h8_get_vbr (SIM_DESC sd
)
112 return (STATE_CPU (sd
, 0)) -> regs
[VBR_REGNUM
];
116 h8_set_vbr (SIM_DESC sd
, int val
)
118 (STATE_CPU (sd
, 0)) -> regs
[VBR_REGNUM
] = val
;
122 h8_get_cache_top (SIM_DESC sd
)
124 return sd
-> cache_top
;
128 h8_set_cache_top (SIM_DESC sd
, int val
)
130 sd
-> cache_top
= val
;
134 h8_get_mask (SIM_DESC sd
)
136 return (STATE_CPU (sd
, 0)) -> mask
;
140 h8_set_mask (SIM_DESC sd
, int val
)
142 (STATE_CPU (sd
, 0)) -> mask
= val
;
146 h8_get_exception (SIM_DESC sd
)
148 return (STATE_CPU (sd
, 0)) -> exception
;
152 h8_set_exception (SIM_DESC sd
, int val
)
154 (STATE_CPU (sd
, 0)) -> exception
= val
;
157 static enum h8300_sim_state
158 h8_get_state (SIM_DESC sd
)
164 h8_set_state (SIM_DESC sd
, enum h8300_sim_state val
)
170 h8_get_cycles (SIM_DESC sd
)
172 return (STATE_CPU (sd
, 0)) -> regs
[CYCLE_REGNUM
];
176 h8_set_cycles (SIM_DESC sd
, unsigned int val
)
178 (STATE_CPU (sd
, 0)) -> regs
[CYCLE_REGNUM
] = val
;
182 h8_get_insts (SIM_DESC sd
)
184 return (STATE_CPU (sd
, 0)) -> regs
[INST_REGNUM
];
188 h8_set_insts (SIM_DESC sd
, unsigned int val
)
190 (STATE_CPU (sd
, 0)) -> regs
[INST_REGNUM
] = val
;
194 h8_get_ticks (SIM_DESC sd
)
196 return (STATE_CPU (sd
, 0)) -> regs
[TICK_REGNUM
];
200 h8_set_ticks (SIM_DESC sd
, unsigned int val
)
202 (STATE_CPU (sd
, 0)) -> regs
[TICK_REGNUM
] = val
;
206 h8_get_mach (SIM_DESC sd
)
208 return (STATE_CPU (sd
, 0)) -> regs
[MACH_REGNUM
];
212 h8_set_mach (SIM_DESC sd
, unsigned int val
)
214 (STATE_CPU (sd
, 0)) -> regs
[MACH_REGNUM
] = val
;
218 h8_get_macl (SIM_DESC sd
)
220 return (STATE_CPU (sd
, 0)) -> regs
[MACL_REGNUM
];
224 h8_set_macl (SIM_DESC sd
, unsigned int val
)
226 (STATE_CPU (sd
, 0)) -> regs
[MACL_REGNUM
] = val
;
230 h8_get_compiles (SIM_DESC sd
)
232 return sd
-> compiles
;
236 h8_increment_compiles (SIM_DESC sd
)
241 static unsigned int *
242 h8_get_reg_buf (SIM_DESC sd
)
244 return &(((STATE_CPU (sd
, 0)) -> regs
)[0]);
248 h8_get_reg (SIM_DESC sd
, int regnum
)
250 return (STATE_CPU (sd
, 0)) -> regs
[regnum
];
254 h8_set_reg (SIM_DESC sd
, int regnum
, int val
)
256 (STATE_CPU (sd
, 0)) -> regs
[regnum
] = val
;
261 h8_get_stats (SIM_DESC sd
, int idx
)
263 return sd
-> stats
[idx
];
267 h8_increment_stats (SIM_DESC sd
, int idx
)
273 static unsigned short *
274 h8_get_cache_idx_buf (SIM_DESC sd
)
276 return sd
-> cache_idx
;
280 h8_set_cache_idx_buf (SIM_DESC sd
, unsigned short *ptr
)
282 sd
-> cache_idx
= ptr
;
285 static unsigned short
286 h8_get_cache_idx (SIM_DESC sd
, unsigned int idx
)
288 if (idx
> sd
->memory_size
)
289 return (unsigned short) -1;
290 return sd
-> cache_idx
[idx
];
294 h8_set_cache_idx (SIM_DESC sd
, int idx
, unsigned int val
)
296 sd
-> cache_idx
[idx
] = (unsigned short) val
;
299 static unsigned char *
300 h8_get_memory_buf (SIM_DESC sd
)
302 return (STATE_CPU (sd
, 0)) -> memory
;
306 h8_set_memory_buf (SIM_DESC sd
, unsigned char *ptr
)
308 (STATE_CPU (sd
, 0)) -> memory
= ptr
;
312 h8_get_memory (SIM_DESC sd
, int idx
)
314 return (STATE_CPU (sd
, 0)) -> memory
[idx
];
318 h8_set_memory (SIM_DESC sd
, int idx
, unsigned int val
)
320 (STATE_CPU (sd
, 0)) -> memory
[idx
] = (unsigned char) val
;
323 static unsigned char *
324 h8_get_eightbit_buf (SIM_DESC sd
)
326 return (STATE_CPU (sd
, 0)) -> eightbit
;
330 h8_set_eightbit_buf (SIM_DESC sd
, unsigned char *ptr
)
332 (STATE_CPU (sd
, 0)) -> eightbit
= ptr
;
336 h8_get_eightbit (SIM_DESC sd
, int idx
)
338 return (STATE_CPU (sd
, 0)) -> eightbit
[idx
];
342 h8_set_eightbit (SIM_DESC sd
, int idx
, unsigned int val
)
344 (STATE_CPU (sd
, 0)) -> eightbit
[idx
] = (unsigned char) val
;
348 h8_get_delayed_branch (SIM_DESC sd
)
350 return (STATE_CPU (sd
, 0)) -> delayed_branch
;
354 h8_set_delayed_branch (SIM_DESC sd
, unsigned int dest
)
356 (STATE_CPU (sd
, 0)) -> delayed_branch
= dest
;
360 h8_get_command_line (SIM_DESC sd
)
362 return (STATE_CPU (sd
, 0)) -> command_line
;
366 h8_set_command_line (SIM_DESC sd
, char ** val
)
368 (STATE_CPU (sd
, 0)) -> command_line
= val
;
372 h8_get_cmdline_arg (SIM_DESC sd
, int index
)
374 return (STATE_CPU (sd
, 0)) -> command_line
[index
];
378 h8_set_cmdline_arg (SIM_DESC sd
, int index
, char * val
)
380 (STATE_CPU (sd
, 0)) -> command_line
[index
] = val
;
383 /* MAC Saturation Mode */
385 h8_get_macS (SIM_DESC sd
)
387 return (STATE_CPU (sd
, 0)) -> macS
;
391 h8_set_macS (SIM_DESC sd
, int val
)
393 (STATE_CPU (sd
, 0)) -> macS
= (val
!= 0);
398 h8_get_macZ (SIM_DESC sd
)
400 return (STATE_CPU (sd
, 0)) -> macZ
;
404 h8_set_macZ (SIM_DESC sd
, int val
)
406 (STATE_CPU (sd
, 0)) -> macZ
= (val
!= 0);
409 /* MAC Negative Flag */
411 h8_get_macN (SIM_DESC sd
)
413 return (STATE_CPU (sd
, 0)) -> macN
;
417 h8_set_macN (SIM_DESC sd
, int val
)
419 (STATE_CPU (sd
, 0)) -> macN
= (val
!= 0);
422 /* MAC Overflow Flag */
424 h8_get_macV (SIM_DESC sd
)
426 return (STATE_CPU (sd
, 0)) -> macV
;
430 h8_set_macV (SIM_DESC sd
, int val
)
432 (STATE_CPU (sd
, 0)) -> macV
= (val
!= 0);
435 /* End CPU data object. */
437 /* The rate at which to call the host's poll_quit callback. */
439 enum { POLL_QUIT_INTERVAL
= 0x80000 };
441 #define LOW_BYTE(x) ((x) & 0xff)
442 #define HIGH_BYTE(x) (((x) >> 8) & 0xff)
443 #define P(X, Y) ((X << 8) | Y)
452 #define I (intMaskBit != 0)
454 #define BUILDSR(SD) \
455 h8_set_ccr (SD, (I << 7) | (UI << 6) | (H << 5) | (U << 4) \
456 | (N << 3) | (Z << 2) | (V << 1) | C)
459 /* Get Status Register (flags). */ \
460 c = (h8_get_ccr (sd) >> 0) & 1; \
461 v = (h8_get_ccr (sd) >> 1) & 1; \
462 nz = !((h8_get_ccr (sd) >> 2) & 1); \
463 n = (h8_get_ccr (sd) >> 3) & 1; \
464 u = (h8_get_ccr (sd) >> 4) & 1; \
465 h = (h8_get_ccr (sd) >> 5) & 1; \
466 ui = ((h8_get_ccr (sd) >> 6) & 1); \
467 intMaskBit = (h8_get_ccr (sd) >> 7) & 1
470 #ifdef __CHAR_IS_SIGNED__
471 #define SEXTCHAR(x) ((char) (x))
475 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff) : x & 0xff)
478 #define UEXTCHAR(x) ((x) & 0xff)
479 #define UEXTSHORT(x) ((x) & 0xffff)
480 #define SEXTSHORT(x) ((short) (x))
484 int h8300_normal_mode
= 0;
487 static int memory_size
;
492 return time (0); /* WinXX HAS UNIX like 'time', so why not use it? */
514 return (h8300hmode
&& !h8300_normal_mode
)? SL
: SW
;
519 /* Simulate an indirection / dereference.
520 return 0 for success, -1 for failure.
524 lvalue (SIM_DESC sd
, int x
, int rn
, unsigned int *val
)
526 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
528 if (val
== NULL
) /* Paranoia. */
534 if (rn
== ZERO_REGNUM
)
535 *val
= X (OP_IMM
, SP
);
537 *val
= X (OP_REG
, SP
);
540 *val
= X (OP_MEM
, SP
);
543 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
552 if (h8300smode
&& !h8300_normal_mode
)
554 else if (h8300hmode
&& !h8300_normal_mode
)
561 decode (SIM_DESC sd
, int addr
, unsigned char *data
, decoded_inst
*dst
)
563 int cst
[3] = {0, 0, 0};
564 int reg
[3] = {0, 0, 0};
565 int rdisp
[3] = {0, 0, 0};
567 const struct h8_opcode
*q
;
572 /* Find the exact opcode/arg combo. */
573 for (q
= h8_opcodes
; q
->name
; q
++)
575 const op_type
*nib
= q
->data
.nib
;
576 unsigned int len
= 0;
578 if ((q
->available
== AV_H8SX
&& !h8300sxmode
) ||
579 (q
->available
== AV_H8S
&& !h8300smode
) ||
580 (q
->available
== AV_H8H
&& !h8300hmode
))
583 cst
[0] = cst
[1] = cst
[2] = 0;
584 reg
[0] = reg
[1] = reg
[2] = 0;
585 rdisp
[0] = rdisp
[1] = rdisp
[2] = 0;
589 op_type looking_for
= *nib
;
590 int thisnib
= data
[len
/ 2];
592 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
593 opnum
= ((looking_for
& OP3
) ? 2 :
594 (looking_for
& DST
) ? 1 : 0);
596 if (looking_for
< 16 && looking_for
>= 0)
598 if (looking_for
!= thisnib
)
603 if (looking_for
& B31
)
605 if (!((thisnib
& 0x8) != 0))
608 looking_for
= (op_type
) (looking_for
& ~B31
);
611 else if (looking_for
& B30
)
613 if (!((thisnib
& 0x8) == 0))
616 looking_for
= (op_type
) (looking_for
& ~B30
);
619 if (looking_for
& B21
)
621 if (!((thisnib
& 0x4) != 0))
624 looking_for
= (op_type
) (looking_for
& ~B21
);
627 else if (looking_for
& B20
)
629 if (!((thisnib
& 0x4) == 0))
632 looking_for
= (op_type
) (looking_for
& ~B20
);
635 if (looking_for
& B11
)
637 if (!((thisnib
& 0x2) != 0))
640 looking_for
= (op_type
) (looking_for
& ~B11
);
643 else if (looking_for
& B10
)
645 if (!((thisnib
& 0x2) == 0))
648 looking_for
= (op_type
) (looking_for
& ~B10
);
651 if (looking_for
& B01
)
653 if (!((thisnib
& 0x1) != 0))
656 looking_for
= (op_type
) (looking_for
& ~B01
);
659 else if (looking_for
& B00
)
661 if (!((thisnib
& 0x1) == 0))
664 looking_for
= (op_type
) (looking_for
& ~B00
);
667 if (looking_for
& IGNORE
)
669 /* Hitachi has declared that IGNORE must be zero. */
673 else if ((looking_for
& MODE
) == DATA
)
675 ; /* Skip embedded data. */
677 else if ((looking_for
& MODE
) == DBIT
)
679 /* Exclude adds/subs by looking at bit 0 and 2, and
680 make sure the operand size, either w or l,
681 matches by looking at bit 1. */
682 if ((looking_for
& 7) != (thisnib
& 7))
685 cst
[opnum
] = (thisnib
& 0x8) ? 2 : 1;
687 else if ((looking_for
& MODE
) == REG
||
688 (looking_for
& MODE
) == LOWREG
||
689 (looking_for
& MODE
) == IND
||
690 (looking_for
& MODE
) == PREINC
||
691 (looking_for
& MODE
) == POSTINC
||
692 (looking_for
& MODE
) == PREDEC
||
693 (looking_for
& MODE
) == POSTDEC
)
695 reg
[opnum
] = thisnib
;
697 else if (looking_for
& CTRL
)
700 if (((looking_for
& MODE
) == CCR
&& (thisnib
!= C_CCR
)) ||
701 ((looking_for
& MODE
) == EXR
&& (thisnib
!= C_EXR
)) ||
702 ((looking_for
& MODE
) == MACH
&& (thisnib
!= C_MACH
)) ||
703 ((looking_for
& MODE
) == MACL
&& (thisnib
!= C_MACL
)) ||
704 ((looking_for
& MODE
) == VBR
&& (thisnib
!= C_VBR
)) ||
705 ((looking_for
& MODE
) == SBR
&& (thisnib
!= C_SBR
)))
707 if (((looking_for
& MODE
) == CCR_EXR
&&
708 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
)) ||
709 ((looking_for
& MODE
) == VBR_SBR
&&
710 (thisnib
!= C_VBR
&& thisnib
!= C_SBR
)) ||
711 ((looking_for
& MODE
) == MACREG
&&
712 (thisnib
!= C_MACH
&& thisnib
!= C_MACL
)))
714 if (((looking_for
& MODE
) == CC_EX_VB_SB
&&
715 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
&&
716 thisnib
!= C_VBR
&& thisnib
!= C_SBR
)))
719 reg
[opnum
] = thisnib
;
721 else if ((looking_for
& MODE
) == ABS
)
723 /* Absolute addresses are unsigned. */
724 switch (looking_for
& SIZE
)
727 cst
[opnum
] = UEXTCHAR (data
[len
/ 2]);
731 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
735 (data
[len
/ 2 + 0] << 24) +
736 (data
[len
/ 2 + 1] << 16) +
737 (data
[len
/ 2 + 2] << 8) +
741 printf ("decode: bad size ABS: %d\n",
742 (looking_for
& SIZE
));
746 else if ((looking_for
& MODE
) == DISP
||
747 (looking_for
& MODE
) == PCREL
||
748 (looking_for
& MODE
) == INDEXB
||
749 (looking_for
& MODE
) == INDEXW
||
750 (looking_for
& MODE
) == INDEXL
)
752 switch (looking_for
& SIZE
)
755 cst
[opnum
] = thisnib
& 3;
758 cst
[opnum
] = SEXTCHAR (data
[len
/ 2]);
761 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
762 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
765 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
769 (data
[len
/ 2 + 0] << 24) +
770 (data
[len
/ 2 + 1] << 16) +
771 (data
[len
/ 2 + 2] << 8) +
775 printf ("decode: bad size DISP/PCREL/INDEX: %d\n",
776 (looking_for
& SIZE
));
780 else if ((looking_for
& SIZE
) == L_16
||
781 (looking_for
& SIZE
) == L_16U
)
783 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
784 /* Immediates are always unsigned. */
785 if ((looking_for
& SIZE
) != L_16U
&&
786 (looking_for
& MODE
) != IMM
)
787 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
789 else if (looking_for
& ABSJMP
)
791 switch (looking_for
& SIZE
) {
793 cst
[opnum
] = (data
[1] << 16) | (data
[2] << 8) | (data
[3]);
797 (data
[len
/ 2 + 0] << 24) +
798 (data
[len
/ 2 + 1] << 16) +
799 (data
[len
/ 2 + 2] << 8) +
803 printf ("decode: bad size ABSJMP: %d\n",
804 (looking_for
& SIZE
));
808 else if ((looking_for
& MODE
) == MEMIND
)
810 cst
[opnum
] = data
[1];
812 else if ((looking_for
& MODE
) == VECIND
)
814 if(h8300_normal_mode
)
815 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 2;
817 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 4;
818 cst
[opnum
] += h8_get_vbr (sd
); /* Add vector base reg. */
820 else if ((looking_for
& SIZE
) == L_32
)
825 (data
[i
+ 0] << 24) |
826 (data
[i
+ 1] << 16) |
830 else if ((looking_for
& SIZE
) == L_24
)
835 (data
[i
+ 0] << 16) |
839 else if (looking_for
& DISPREG
)
841 rdisp
[opnum
] = thisnib
& 0x7;
843 else if ((looking_for
& MODE
) == KBIT
)
860 else if ((looking_for
& SIZE
) == L_8
)
862 if ((looking_for
& MODE
) == ABS
)
864 /* Will be combined with contents of SBR_REGNUM
865 by fetch (). For all modes except h8sx, this
866 will always contain the value 0xFFFFFF00. */
867 cst
[opnum
] = data
[len
/ 2] & 0xff;
871 cst
[opnum
] = data
[len
/ 2] & 0xff;
874 else if ((looking_for
& SIZE
) == L_2
)
876 cst
[opnum
] = thisnib
& 3;
878 else if ((looking_for
& SIZE
) == L_3
||
879 (looking_for
& SIZE
) == L_3NZ
)
881 cst
[opnum
] = thisnib
& 7;
882 if (cst
[opnum
] == 0 && (looking_for
& SIZE
) == L_3NZ
)
885 else if ((looking_for
& SIZE
) == L_4
)
887 cst
[opnum
] = thisnib
& 15;
889 else if ((looking_for
& SIZE
) == L_5
)
891 cst
[opnum
] = data
[len
/ 2] & 0x1f;
893 else if (looking_for
== E
)
898 /* Fill in the args. */
900 const op_type
*args
= q
->args
.nib
;
905 nargs
< 3 && *args
!= E
;
911 opnum
= ((x
& OP3
) ? 2 :
920 if ((x
& MODE
) == IMM
||
921 (x
& MODE
) == KBIT
||
924 /* Use the instruction to determine
926 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
927 p
->literal
= cst
[opnum
];
929 else if ((x
& MODE
) == CONST_2
||
930 (x
& MODE
) == CONST_4
||
931 (x
& MODE
) == CONST_8
||
932 (x
& MODE
) == CONST_16
)
934 /* Use the instruction to determine
936 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
938 case CONST_2
: p
->literal
= 2; break;
939 case CONST_4
: p
->literal
= 4; break;
940 case CONST_8
: p
->literal
= 8; break;
941 case CONST_16
: p
->literal
= 16; break;
944 else if ((x
& MODE
) == REG
)
946 p
->type
= X (OP_REG
, bitfrom (x
));
949 else if ((x
& MODE
) == LOWREG
)
951 p
->type
= X (OP_LOWREG
, bitfrom (x
));
954 else if ((x
& MODE
) == PREINC
)
956 /* Use the instruction to determine
958 p
->type
= X (OP_PREINC
, OP_SIZE (q
->how
));
959 p
->reg
= reg
[opnum
] & 0x7;
961 else if ((x
& MODE
) == POSTINC
)
963 /* Use the instruction to determine
965 p
->type
= X (OP_POSTINC
, OP_SIZE (q
->how
));
966 p
->reg
= reg
[opnum
] & 0x7;
968 else if ((x
& MODE
) == PREDEC
)
970 /* Use the instruction to determine
972 p
->type
= X (OP_PREDEC
, OP_SIZE (q
->how
));
973 p
->reg
= reg
[opnum
] & 0x7;
975 else if ((x
& MODE
) == POSTDEC
)
977 /* Use the instruction to determine
979 p
->type
= X (OP_POSTDEC
, OP_SIZE (q
->how
));
980 p
->reg
= reg
[opnum
] & 0x7;
982 else if ((x
& MODE
) == IND
)
984 /* Note: an indirect is transformed into
985 a displacement of zero.
987 /* Use the instruction to determine
989 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
990 p
->reg
= reg
[opnum
] & 0x7;
992 if (OP_KIND (q
->how
) == O_JSR
||
993 OP_KIND (q
->how
) == O_JMP
)
994 if (lvalue (sd
, p
->type
, p
->reg
, (unsigned int *)&p
->type
))
997 else if ((x
& MODE
) == ABS
)
999 /* Note: a 16 or 32 bit ABS is transformed into a
1000 displacement from pseudo-register ZERO_REGNUM,
1001 which is always zero. An 8 bit ABS becomes
1002 a displacement from SBR_REGNUM.
1004 /* Use the instruction to determine
1005 the operand size. */
1006 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1007 p
->literal
= cst
[opnum
];
1009 /* 8-bit ABS is displacement from SBR.
1010 16 and 32-bit ABS are displacement from ZERO.
1011 (SBR will always be zero except for h8/sx)
1013 if ((x
& SIZE
) == L_8
)
1014 p
->reg
= SBR_REGNUM
;
1016 p
->reg
= ZERO_REGNUM
;;
1018 else if ((x
& MODE
) == MEMIND
||
1019 (x
& MODE
) == VECIND
)
1021 /* Size doesn't matter. */
1022 p
->type
= X (OP_MEM
, SB
);
1023 p
->literal
= cst
[opnum
];
1024 if (OP_KIND (q
->how
) == O_JSR
||
1025 OP_KIND (q
->how
) == O_JMP
)
1026 if (lvalue (sd
, p
->type
, p
->reg
, (unsigned int *)&p
->type
))
1029 else if ((x
& MODE
) == PCREL
)
1031 /* Size doesn't matter. */
1032 p
->type
= X (OP_PCREL
, SB
);
1033 p
->literal
= cst
[opnum
];
1035 else if (x
& ABSJMP
)
1037 p
->type
= X (OP_IMM
, SP
);
1038 p
->literal
= cst
[opnum
];
1040 else if ((x
& MODE
) == INDEXB
)
1042 p
->type
= X (OP_INDEXB
, OP_SIZE (q
->how
));
1043 p
->literal
= cst
[opnum
];
1044 p
->reg
= rdisp
[opnum
];
1046 else if ((x
& MODE
) == INDEXW
)
1048 p
->type
= X (OP_INDEXW
, OP_SIZE (q
->how
));
1049 p
->literal
= cst
[opnum
];
1050 p
->reg
= rdisp
[opnum
];
1052 else if ((x
& MODE
) == INDEXL
)
1054 p
->type
= X (OP_INDEXL
, OP_SIZE (q
->how
));
1055 p
->literal
= cst
[opnum
];
1056 p
->reg
= rdisp
[opnum
];
1058 else if ((x
& MODE
) == DISP
)
1060 /* Yuck -- special for mova args. */
1061 if (strncmp (q
->name
, "mova", 4) == 0 &&
1064 /* Mova can have a DISP2 dest, with an
1065 INDEXB or INDEXW src. The multiplier
1066 for the displacement value is determined
1067 by the src operand, not by the insn. */
1069 switch (OP_KIND (dst
->src
.type
))
1072 p
->type
= X (OP_DISP
, SB
);
1073 p
->literal
= cst
[opnum
];
1076 p
->type
= X (OP_DISP
, SW
);
1077 p
->literal
= cst
[opnum
] * 2;
1085 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1086 p
->literal
= cst
[opnum
];
1087 /* DISP2 is special. */
1088 if ((x
& SIZE
) == L_2
)
1089 switch (OP_SIZE (q
->how
))
1092 case SW
: p
->literal
*= 2; break;
1093 case SL
: p
->literal
*= 4; break;
1096 p
->reg
= rdisp
[opnum
];
1103 p
->type
= X (OP_CCR
, SB
);
1106 p
->type
= X (OP_EXR
, SB
);
1109 p
->type
= X (OP_MACH
, SL
);
1112 p
->type
= X (OP_MACL
, SL
);
1115 p
->type
= X (OP_VBR
, SL
);
1118 p
->type
= X (OP_SBR
, SL
);
1122 else if ((x
& MODE
) == CCR
)
1126 else if ((x
& MODE
) == EXR
)
1131 printf ("Hmmmm 0x%x...\n", x
);
1137 /* Unary operators: treat src and dst as equivalent. */
1138 if (dst
->dst
.type
== -1)
1139 dst
->dst
= dst
->src
;
1140 if (dst
->src
.type
== -1)
1141 dst
->src
= dst
->dst
;
1143 dst
->opcode
= q
->how
;
1144 dst
->cycles
= q
->time
;
1146 /* And jsr's to these locations are turned into
1149 if (OP_KIND (dst
->opcode
) == O_JSR
)
1151 switch (dst
->src
.literal
)
1154 dst
->opcode
= O (O_SYS_OPEN
, SB
);
1157 dst
->opcode
= O (O_SYS_READ
, SB
);
1160 dst
->opcode
= O (O_SYS_WRITE
, SB
);
1163 dst
->opcode
= O (O_SYS_LSEEK
, SB
);
1166 dst
->opcode
= O (O_SYS_CLOSE
, SB
);
1169 dst
->opcode
= O (O_SYS_STAT
, SB
);
1172 dst
->opcode
= O (O_SYS_FSTAT
, SB
);
1175 dst
->opcode
= O (O_SYS_CMDLINE
, SB
);
1178 /* End of Processing for system calls. */
1181 dst
->next_pc
= addr
+ len
/ 2;
1185 printf ("Don't understand 0x%x \n", looking_for
);
1196 /* Fell off the end. */
1197 dst
->opcode
= O (O_ILL
, SB
);
1201 compile (SIM_DESC sd
, int pc
)
1205 /* Find the next cache entry to use. */
1206 idx
= h8_get_cache_top (sd
) + 1;
1207 h8_increment_compiles (sd
);
1208 if (idx
>= sd
->sim_cache_size
)
1212 h8_set_cache_top (sd
, idx
);
1214 /* Throw away its old meaning. */
1215 h8_set_cache_idx (sd
, sd
->sim_cache
[idx
].oldpc
, 0);
1217 /* Set to new address. */
1218 sd
->sim_cache
[idx
].oldpc
= pc
;
1220 /* Fill in instruction info. */
1221 decode (sd
, pc
, h8_get_memory_buf (sd
) + pc
, sd
->sim_cache
+ idx
);
1223 /* Point to new cache entry. */
1224 h8_set_cache_idx (sd
, pc
, idx
);
1228 static unsigned char *breg
[32];
1229 static unsigned short *wreg
[16];
1231 #define GET_B_REG(X) *(breg[X])
1232 #define SET_B_REG(X, Y) (*(breg[X])) = (Y)
1233 #define GET_W_REG(X) *(wreg[X])
1234 #define SET_W_REG(X, Y) (*(wreg[X])) = (Y)
1235 #define GET_L_REG(X) h8_get_reg (sd, X)
1236 #define SET_L_REG(X, Y) h8_set_reg (sd, X, Y)
1238 #define GET_MEMORY_L(X) \
1239 ((X) < memory_size \
1240 ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16) \
1241 | (h8_get_memory (sd, (X)+2) << 8) | (h8_get_memory (sd, (X)+3) << 0)) \
1242 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 24) \
1243 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 16) \
1244 | (h8_get_eightbit (sd, ((X)+2) & 0xff) << 8) \
1245 | (h8_get_eightbit (sd, ((X)+3) & 0xff) << 0)))
1247 #define GET_MEMORY_W(X) \
1248 ((X) < memory_size \
1249 ? ((h8_get_memory (sd, (X)+0) << 8) \
1250 | (h8_get_memory (sd, (X)+1) << 0)) \
1251 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 8) \
1252 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 0)))
1255 #define GET_MEMORY_B(X) \
1256 ((X) < memory_size ? (h8_get_memory (sd, (X))) \
1257 : (h8_get_eightbit (sd, (X) & 0xff)))
1259 #define SET_MEMORY_L(X, Y) \
1260 { register unsigned char *_p; register int __y = (Y); \
1261 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1262 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1263 _p[0] = __y >> 24; _p[1] = __y >> 16; \
1264 _p[2] = __y >> 8; _p[3] = __y >> 0; \
1267 #define SET_MEMORY_W(X, Y) \
1268 { register unsigned char *_p; register int __y = (Y); \
1269 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1270 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1271 _p[0] = __y >> 8; _p[1] = __y; \
1274 #define SET_MEMORY_B(X, Y) \
1275 ((X) < memory_size ? (h8_set_memory (sd, (X), (Y))) \
1276 : (h8_set_eightbit (sd, (X) & 0xff, (Y))))
1278 /* Simulate a memory fetch.
1279 Return 0 for success, -1 for failure.
1283 fetch_1 (SIM_DESC sd
, ea_type
*arg
, int *val
, int twice
)
1285 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1287 int abs
= arg
->literal
;
1292 return -1; /* Paranoia. */
1296 /* Indexed register plus displacement mode:
1298 This new family of addressing modes are similar to OP_DISP
1299 (register plus displacement), with two differences:
1300 1) INDEXB uses only the least significant byte of the register,
1301 INDEXW uses only the least significant word, and
1302 INDEXL uses the entire register (just like OP_DISP).
1304 2) The displacement value in abs is multiplied by two
1305 for SW-sized operations, and by four for SL-size.
1307 This gives nine possible variations.
1310 case X (OP_INDEXB
, SB
):
1311 case X (OP_INDEXB
, SW
):
1312 case X (OP_INDEXB
, SL
):
1313 case X (OP_INDEXW
, SB
):
1314 case X (OP_INDEXW
, SW
):
1315 case X (OP_INDEXW
, SL
):
1316 case X (OP_INDEXL
, SB
):
1317 case X (OP_INDEXL
, SW
):
1318 case X (OP_INDEXL
, SL
):
1320 switch (OP_KIND (arg
->type
)) {
1321 case OP_INDEXB
: t
&= 0xff; break;
1322 case OP_INDEXW
: t
&= 0xffff; break;
1326 switch (OP_SIZE (arg
->type
)) {
1328 *val
= GET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (sd
));
1331 *val
= GET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (sd
));
1334 *val
= GET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (sd
));
1339 case X (OP_LOWREG
, SB
):
1340 *val
= GET_L_REG (rn
) & 0xff;
1342 case X (OP_LOWREG
, SW
):
1343 *val
= GET_L_REG (rn
) & 0xffff;
1346 case X (OP_REG
, SB
): /* Register direct, byte. */
1347 *val
= GET_B_REG (rn
);
1349 case X (OP_REG
, SW
): /* Register direct, word. */
1350 *val
= GET_W_REG (rn
);
1352 case X (OP_REG
, SL
): /* Register direct, long. */
1353 *val
= GET_L_REG (rn
);
1355 case X (OP_IMM
, SB
): /* Immediate, byte. */
1356 case X (OP_IMM
, SW
): /* Immediate, word. */
1357 case X (OP_IMM
, SL
): /* Immediate, long. */
1360 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr: byte. */
1362 r
= GET_MEMORY_B (t
& h8_get_mask (sd
));
1368 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr: word. */
1370 r
= GET_MEMORY_W (t
& h8_get_mask (sd
));
1376 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr: long. */
1378 r
= GET_MEMORY_L (t
& h8_get_mask (sd
));
1385 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr: byte. */
1387 r
= GET_MEMORY_B (t
& h8_get_mask (sd
));
1393 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr: word. */
1395 r
= GET_MEMORY_W (t
& h8_get_mask (sd
));
1401 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr: long. */
1403 r
= GET_MEMORY_L (t
& h8_get_mask (sd
));
1410 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr: byte. */
1411 t
= GET_L_REG (rn
) - 1;
1413 t
&= h8_get_mask (sd
);
1414 *val
= GET_MEMORY_B (t
);
1417 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr: word. */
1418 t
= GET_L_REG (rn
) - 2;
1420 t
&= h8_get_mask (sd
);
1421 *val
= GET_MEMORY_W (t
);
1424 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr: long. */
1425 t
= GET_L_REG (rn
) - 4;
1427 t
&= h8_get_mask (sd
);
1428 *val
= GET_MEMORY_L (t
);
1431 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr: byte. */
1432 t
= GET_L_REG (rn
) + 1;
1434 t
&= h8_get_mask (sd
);
1435 *val
= GET_MEMORY_B (t
);
1438 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr: long. */
1439 t
= GET_L_REG (rn
) + 2;
1441 t
&= h8_get_mask (sd
);
1442 *val
= GET_MEMORY_W (t
);
1445 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr: long. */
1446 t
= GET_L_REG (rn
) + 4;
1448 t
&= h8_get_mask (sd
);
1449 *val
= GET_MEMORY_L (t
);
1452 case X (OP_DISP
, SB
): /* Register indirect w/displacement: byte. */
1453 t
= GET_L_REG (rn
) + abs
;
1454 t
&= h8_get_mask (sd
);
1455 *val
= GET_MEMORY_B (t
);
1458 case X (OP_DISP
, SW
): /* Register indirect w/displacement: word. */
1459 t
= GET_L_REG (rn
) + abs
;
1460 t
&= h8_get_mask (sd
);
1461 *val
= GET_MEMORY_W (t
);
1464 case X (OP_DISP
, SL
): /* Register indirect w/displacement: long. */
1465 t
= GET_L_REG (rn
) + abs
;
1466 t
&= h8_get_mask (sd
);
1467 *val
=GET_MEMORY_L (t
);
1470 case X (OP_MEM
, SL
): /* Absolute memory address, long. */
1471 t
= GET_MEMORY_L (abs
);
1472 t
&= h8_get_mask (sd
);
1476 case X (OP_MEM
, SW
): /* Absolute memory address, word. */
1477 t
= GET_MEMORY_W (abs
);
1478 t
&= h8_get_mask (sd
);
1482 case X (OP_PCREL
, SB
): /* PC relative (for jump, branch etc). */
1483 case X (OP_PCREL
, SW
):
1484 case X (OP_PCREL
, SL
):
1485 case X (OP_PCREL
, SN
):
1489 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1491 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
1494 return 0; /* Success. */
1500 fetch (SIM_DESC sd
, ea_type
*arg
, int *val
)
1502 return fetch_1 (sd
, arg
, val
, 0);
1505 /* Fetch which will be followed by a store to the same location.
1506 The difference being that we don't want to do a post-increment
1507 or post-decrement at this time: we'll do it when we store. */
1510 fetch2 (SIM_DESC sd
, ea_type
*arg
, int *val
)
1512 return fetch_1 (sd
, arg
, val
, 1);
1515 /* Simulate a memory store.
1516 Return 0 for success, -1 for failure.
1520 store_1 (SIM_DESC sd
, ea_type
*arg
, int n
, int twice
)
1522 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1524 int abs
= arg
->literal
;
1529 /* Indexed register plus displacement mode:
1531 This new family of addressing modes are similar to OP_DISP
1532 (register plus displacement), with two differences:
1533 1) INDEXB uses only the least significant byte of the register,
1534 INDEXW uses only the least significant word, and
1535 INDEXL uses the entire register (just like OP_DISP).
1537 2) The displacement value in abs is multiplied by two
1538 for SW-sized operations, and by four for SL-size.
1540 This gives nine possible variations.
1543 case X (OP_INDEXB
, SB
):
1544 case X (OP_INDEXB
, SW
):
1545 case X (OP_INDEXB
, SL
):
1546 case X (OP_INDEXW
, SB
):
1547 case X (OP_INDEXW
, SW
):
1548 case X (OP_INDEXW
, SL
):
1549 case X (OP_INDEXL
, SB
):
1550 case X (OP_INDEXL
, SW
):
1551 case X (OP_INDEXL
, SL
):
1553 switch (OP_KIND (arg
->type
)) {
1554 case OP_INDEXB
: t
&= 0xff; break;
1555 case OP_INDEXW
: t
&= 0xffff; break;
1559 switch (OP_SIZE (arg
->type
)) {
1561 SET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (sd
), n
);
1564 SET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (sd
), n
);
1567 SET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (sd
), n
);
1572 case X (OP_REG
, SB
): /* Register direct, byte. */
1575 case X (OP_REG
, SW
): /* Register direct, word. */
1578 case X (OP_REG
, SL
): /* Register direct, long. */
1582 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr, byte. */
1587 t
&= h8_get_mask (sd
);
1588 SET_MEMORY_B (t
, n
);
1591 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr, word. */
1596 t
&= h8_get_mask (sd
);
1597 SET_MEMORY_W (t
, n
);
1600 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr, long. */
1605 t
&= h8_get_mask (sd
);
1606 SET_MEMORY_L (t
, n
);
1609 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr, byte. */
1614 t
&= h8_get_mask (sd
);
1615 SET_MEMORY_B (t
, n
);
1618 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr, word. */
1623 t
&= h8_get_mask (sd
);
1624 SET_MEMORY_W (t
, n
);
1627 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr, long. */
1632 t
&= h8_get_mask (sd
);
1633 SET_MEMORY_L (t
, n
);
1636 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr, byte. */
1638 SET_L_REG (rn
, t
- 1);
1639 t
&= h8_get_mask (sd
);
1640 SET_MEMORY_B (t
, n
);
1643 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr, word. */
1645 SET_L_REG (rn
, t
- 2);
1646 t
&= h8_get_mask (sd
);
1647 SET_MEMORY_W (t
, n
);
1650 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr, long. */
1652 SET_L_REG (rn
, t
- 4);
1653 t
&= h8_get_mask (sd
);
1654 SET_MEMORY_L (t
, n
);
1657 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr, byte. */
1659 SET_L_REG (rn
, t
+ 1);
1660 t
&= h8_get_mask (sd
);
1661 SET_MEMORY_B (t
, n
);
1664 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr, word. */
1666 SET_L_REG (rn
, t
+ 2);
1667 t
&= h8_get_mask (sd
);
1668 SET_MEMORY_W (t
, n
);
1671 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr, long. */
1673 SET_L_REG (rn
, t
+ 4);
1674 t
&= h8_get_mask (sd
);
1675 SET_MEMORY_L (t
, n
);
1678 case X (OP_DISP
, SB
): /* Register indirect w/displacement, byte. */
1679 t
= GET_L_REG (rn
) + abs
;
1680 t
&= h8_get_mask (sd
);
1681 SET_MEMORY_B (t
, n
);
1684 case X (OP_DISP
, SW
): /* Register indirect w/displacement, word. */
1685 t
= GET_L_REG (rn
) + abs
;
1686 t
&= h8_get_mask (sd
);
1687 SET_MEMORY_W (t
, n
);
1690 case X (OP_DISP
, SL
): /* Register indirect w/displacement, long. */
1691 t
= GET_L_REG (rn
) + abs
;
1692 t
&= h8_get_mask (sd
);
1693 SET_MEMORY_L (t
, n
);
1697 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1698 case X (OP_MEM
, SW
): /* Why isn't this implemented? */
1699 case X (OP_MEM
, SL
): /* Why isn't this implemented? */
1701 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
1710 store (SIM_DESC sd
, ea_type
*arg
, int n
)
1712 return store_1 (sd
, arg
, n
, 0);
1715 /* Store which follows a fetch from the same location.
1716 The difference being that we don't want to do a pre-increment
1717 or pre-decrement at this time: it was already done when we fetched. */
1720 store2 (SIM_DESC sd
, ea_type
*arg
, int n
)
1722 return store_1 (sd
, arg
, n
, 1);
1725 /* Flag to be set whenever a new SIM_DESC object is created. */
1726 static int init_pointers_needed
= 1;
1729 init_pointers (SIM_DESC sd
)
1731 if (init_pointers_needed
)
1735 if (h8300smode
&& !h8300_normal_mode
)
1736 memory_size
= H8300S_MSIZE
;
1737 else if (h8300hmode
&& !h8300_normal_mode
)
1738 memory_size
= H8300H_MSIZE
;
1740 memory_size
= H8300_MSIZE
;
1741 /* `msize' must be a power of two. */
1742 if ((memory_size
& (memory_size
- 1)) != 0)
1746 "init_pointers: bad memory size %d, defaulting to %d.\n",
1747 memory_size
, memory_size
= H8300S_MSIZE
);
1750 if (h8_get_memory_buf (sd
))
1751 free (h8_get_memory_buf (sd
));
1752 if (h8_get_cache_idx_buf (sd
))
1753 free (h8_get_cache_idx_buf (sd
));
1754 if (h8_get_eightbit_buf (sd
))
1755 free (h8_get_eightbit_buf (sd
));
1757 h8_set_memory_buf (sd
, (unsigned char *)
1758 calloc (sizeof (char), memory_size
));
1759 h8_set_cache_idx_buf (sd
, (unsigned short *)
1760 calloc (sizeof (short), memory_size
));
1761 sd
->memory_size
= memory_size
;
1762 h8_set_eightbit_buf (sd
, (unsigned char *) calloc (sizeof (char), 256));
1764 h8_set_mask (sd
, memory_size
- 1);
1766 memset (h8_get_reg_buf (sd
), 0, sizeof (((STATE_CPU (sd
, 0))->regs
)));
1768 for (i
= 0; i
< 8; i
++)
1770 /* FIXME: rewrite using local buffer. */
1771 unsigned char *p
= (unsigned char *) (h8_get_reg_buf (sd
) + i
);
1772 unsigned char *e
= (unsigned char *) (h8_get_reg_buf (sd
) + i
+ 1);
1773 unsigned short *q
= (unsigned short *) (h8_get_reg_buf (sd
) + i
);
1774 unsigned short *u
= (unsigned short *) (h8_get_reg_buf (sd
) + i
+ 1);
1775 h8_set_reg (sd
, i
, 0x00112233);
1790 wreg
[i
] = wreg
[i
+ 8] = 0;
1804 if (wreg
[i
] == 0 || wreg
[i
+ 8] == 0)
1805 sim_io_printf (sd
, "init_pointers: internal error.\n");
1807 h8_set_reg (sd
, i
, 0);
1810 init_pointers_needed
= 0;
1812 /* Initialize the seg registers. */
1814 set_simcache_size (sd
, CSIZE
);
1818 #define OBITOP(name, f, s, op) \
1819 case O (name, SB): \
1824 if (fetch (sd, &code->dst, &ea)) \
1826 if (fetch (sd, &code->src, &tmp)) \
1828 m = 1 << (tmp & 7); \
1831 if (store (sd, &code->dst,ea)) \
1837 step_once (SIM_DESC sd
, SIM_CPU
*cpu
)
1841 int tick_start
= get_now ();
1848 int c
, nz
, v
, n
, u
, h
, ui
, intMaskBit
;
1851 host_callback
*sim_callback
= STATE_CALLBACK (sd
);
1855 pc
= h8_get_pc (sd
);
1857 /* The PC should never be odd. */
1860 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGBUS
);
1864 /* Get Status Register (flags). */
1867 if (h8300smode
) /* Get exr. */
1869 trace
= (h8_get_exr (sd
) >> 7) & 1;
1870 intMask
= h8_get_exr (sd
) & 7;
1873 oldmask
= h8_get_mask (sd
);
1874 if (!h8300hmode
|| h8300_normal_mode
)
1875 h8_set_mask (sd
, 0xffff);
1878 unsigned short cidx
;
1882 cidx
= h8_get_cache_idx (sd
, pc
);
1883 if (cidx
== (unsigned short) -1 ||
1884 cidx
>= sd
->sim_cache_size
)
1887 code
= sd
->sim_cache
+ cidx
;
1892 printf ("%x %d %s\n", pc
, code
->opcode
,
1893 code
->op
? code
->op
->name
: "**");
1895 h8_increment_stats (sd
, code
->opcode
);
1900 cycles
+= code
->cycles
;
1904 switch (code
->opcode
)
1908 * This opcode is a fake for when we get to an
1909 * instruction which hasnt been compiled
1915 case O (O_MOVAB
, SL
):
1916 case O (O_MOVAW
, SL
):
1917 case O (O_MOVAL
, SL
):
1918 /* 1) Evaluate 2nd argument (dst).
1919 2) Mask / zero extend according to whether 1st argument (src)
1920 is INDEXB, INDEXW, or INDEXL.
1921 3) Left-shift the result by 0, 1 or 2, according to size of mova
1922 (mova/b, mova/w, mova/l).
1923 4) Add literal value of 1st argument (src).
1924 5) Store result in 3rd argument (op3).
1927 /* Alas, since this is the only instruction with 3 arguments,
1928 decode doesn't handle them very well. Some fix-up is required.
1930 a) The size of dst is determined by whether src is
1931 INDEXB or INDEXW. */
1933 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
1934 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SB
);
1935 else if (OP_KIND (code
->src
.type
) == OP_INDEXW
)
1936 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SW
);
1938 /* b) If op3 == null, then this is the short form of the insn.
1939 Dst is the dispreg of src, and op3 is the 32-bit form
1940 of the same register.
1943 if (code
->op3
.type
== 0)
1945 /* Short form: src == INDEXB/INDEXW, dst == op3 == 0.
1946 We get to compose dst and op3 as follows:
1948 op3 is a 32-bit register, ID == src.reg.
1949 dst is the same register, but 8 or 16 bits
1950 depending on whether src is INDEXB or INDEXW.
1953 code
->op3
.type
= X (OP_REG
, SL
);
1954 code
->op3
.reg
= code
->src
.reg
;
1955 code
->op3
.literal
= 0;
1957 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
1959 code
->dst
.type
= X (OP_REG
, SB
);
1960 code
->dst
.reg
= code
->op3
.reg
+ 8;
1963 code
->dst
.type
= X (OP_REG
, SW
);
1966 if (fetch (sd
, &code
->dst
, &ea
))
1969 switch (OP_KIND (code
->src
.type
)) {
1970 case OP_INDEXB
: ea
= ea
& 0xff; break;
1971 case OP_INDEXW
: ea
= ea
& 0xffff; break;
1972 case OP_INDEXL
: break;
1973 default: goto illegal
;
1976 switch (code
->opcode
) {
1977 case O (O_MOVAB
, SL
): break;
1978 case O (O_MOVAW
, SL
): ea
= ea
<< 1; break;
1979 case O (O_MOVAL
, SL
): ea
= ea
<< 2; break;
1980 default: goto illegal
;
1983 ea
= ea
+ code
->src
.literal
;
1985 if (store (sd
, &code
->op3
, ea
))
1990 case O (O_SUBX
, SB
): /* subx, extended sub */
1991 if (fetch2 (sd
, &code
->dst
, &rd
))
1993 if (fetch (sd
, &code
->src
, &ea
))
1999 case O (O_SUBX
, SW
): /* subx, extended sub */
2000 if (fetch2 (sd
, &code
->dst
, &rd
))
2002 if (fetch (sd
, &code
->src
, &ea
))
2008 case O (O_SUBX
, SL
): /* subx, extended sub */
2009 if (fetch2 (sd
, &code
->dst
, &rd
))
2011 if (fetch (sd
, &code
->src
, &ea
))
2017 case O (O_ADDX
, SB
): /* addx, extended add */
2018 if (fetch2 (sd
, &code
->dst
, &rd
))
2020 if (fetch (sd
, &code
->src
, &ea
))
2026 case O (O_ADDX
, SW
): /* addx, extended add */
2027 if (fetch2 (sd
, &code
->dst
, &rd
))
2029 if (fetch (sd
, &code
->src
, &ea
))
2035 case O (O_ADDX
, SL
): /* addx, extended add */
2036 if (fetch2 (sd
, &code
->dst
, &rd
))
2038 if (fetch (sd
, &code
->src
, &ea
))
2044 case O (O_SUB
, SB
): /* sub.b */
2045 /* Fetch rd and ea. */
2046 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2052 case O (O_SUB
, SW
): /* sub.w */
2053 /* Fetch rd and ea. */
2054 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2060 case O (O_SUB
, SL
): /* sub.l */
2061 /* Fetch rd and ea. */
2062 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2068 case O (O_NEG
, SB
): /* neg.b */
2070 if (fetch2 (sd
, &code
->src
, &ea
))
2077 case O (O_NEG
, SW
): /* neg.w */
2079 if (fetch2 (sd
, &code
->src
, &ea
))
2086 case O (O_NEG
, SL
): /* neg.l */
2088 if (fetch2 (sd
, &code
->src
, &ea
))
2095 case O (O_ADD
, SB
): /* add.b */
2096 if (fetch2 (sd
, &code
->dst
, &rd
))
2098 if (fetch (sd
, &code
->src
, &ea
))
2103 case O (O_ADD
, SW
): /* add.w */
2104 if (fetch2 (sd
, &code
->dst
, &rd
))
2106 if (fetch (sd
, &code
->src
, &ea
))
2111 case O (O_ADD
, SL
): /* add.l */
2112 if (fetch2 (sd
, &code
->dst
, &rd
))
2114 if (fetch (sd
, &code
->src
, &ea
))
2119 case O (O_AND
, SB
): /* and.b */
2120 /* Fetch rd and ea. */
2121 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2126 case O (O_AND
, SW
): /* and.w */
2127 /* Fetch rd and ea. */
2128 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2133 case O (O_AND
, SL
): /* and.l */
2134 /* Fetch rd and ea. */
2135 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2140 case O (O_OR
, SB
): /* or.b */
2141 /* Fetch rd and ea. */
2142 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2147 case O (O_OR
, SW
): /* or.w */
2148 /* Fetch rd and ea. */
2149 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2154 case O (O_OR
, SL
): /* or.l */
2155 /* Fetch rd and ea. */
2156 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2161 case O (O_XOR
, SB
): /* xor.b */
2162 /* Fetch rd and ea. */
2163 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2168 case O (O_XOR
, SW
): /* xor.w */
2169 /* Fetch rd and ea. */
2170 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2175 case O (O_XOR
, SL
): /* xor.l */
2176 /* Fetch rd and ea. */
2177 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2183 if (fetch (sd
, &code
->src
, &res
))
2185 if (store (sd
, &code
->dst
, res
))
2187 goto just_flags_log8
;
2189 if (fetch (sd
, &code
->src
, &res
))
2191 if (store (sd
, &code
->dst
, res
))
2193 goto just_flags_log16
;
2195 if (fetch (sd
, &code
->src
, &res
))
2197 if (store (sd
, &code
->dst
, res
))
2199 goto just_flags_log32
;
2201 case O (O_MOVMD
, SB
): /* movmd.b */
2208 rd
= GET_MEMORY_B (GET_L_REG (5));
2209 SET_MEMORY_B (GET_L_REG (6), rd
);
2210 SET_L_REG (5, GET_L_REG (5) + 1);
2211 SET_L_REG (6, GET_L_REG (6) + 1);
2216 case O (O_MOVMD
, SW
): /* movmd.w */
2223 rd
= GET_MEMORY_W (GET_L_REG (5));
2224 SET_MEMORY_W (GET_L_REG (6), rd
);
2225 SET_L_REG (5, GET_L_REG (5) + 2);
2226 SET_L_REG (6, GET_L_REG (6) + 2);
2231 case O (O_MOVMD
, SL
): /* movmd.l */
2238 rd
= GET_MEMORY_L (GET_L_REG (5));
2239 SET_MEMORY_L (GET_L_REG (6), rd
);
2240 SET_L_REG (5, GET_L_REG (5) + 4);
2241 SET_L_REG (6, GET_L_REG (6) + 4);
2246 case O (O_MOVSD
, SB
): /* movsd.b */
2247 /* This instruction implements strncpy, with a conditional branch.
2248 r4 contains n, r5 contains src, and r6 contains dst.
2249 The 16-bit displacement operand is added to the pc
2250 if and only if the end of string is reached before
2251 n bytes are transferred. */
2253 ea
= GET_L_REG (4) & 0xffff;
2259 rd
= GET_MEMORY_B (GET_L_REG (5));
2260 SET_MEMORY_B (GET_L_REG (6), rd
);
2261 SET_L_REG (5, GET_L_REG (5) + 1);
2262 SET_L_REG (6, GET_L_REG (6) + 1);
2269 case O (O_EEPMOV
, SB
): /* eepmov.b */
2270 case O (O_EEPMOV
, SW
): /* eepmov.w */
2271 if (h8300hmode
|| h8300smode
)
2273 register unsigned char *_src
, *_dst
;
2274 unsigned int count
= ((code
->opcode
== O (O_EEPMOV
, SW
))
2275 ? h8_get_reg (sd
, R4_REGNUM
) & 0xffff
2276 : h8_get_reg (sd
, R4_REGNUM
) & 0xff);
2278 _src
= (h8_get_reg (sd
, R5_REGNUM
) < memory_size
2279 ? h8_get_memory_buf (sd
) + h8_get_reg (sd
, R5_REGNUM
)
2280 : h8_get_eightbit_buf (sd
) +
2281 (h8_get_reg (sd
, R5_REGNUM
) & 0xff));
2282 if ((_src
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2284 if ((_src
+ count
) >= (h8_get_eightbit_buf (sd
) + 0x100))
2287 _dst
= (h8_get_reg (sd
, R6_REGNUM
) < memory_size
2288 ? h8_get_memory_buf (sd
) + h8_get_reg (sd
, R6_REGNUM
)
2289 : h8_get_eightbit_buf (sd
) +
2290 (h8_get_reg (sd
, R6_REGNUM
) & 0xff));
2292 if ((_dst
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2294 if ((_dst
+ count
) >= (h8_get_eightbit_buf (sd
) + 0x100))
2297 memcpy (_dst
, _src
, count
);
2299 h8_set_reg (sd
, R5_REGNUM
, h8_get_reg (sd
, R5_REGNUM
) + count
);
2300 h8_set_reg (sd
, R6_REGNUM
, h8_get_reg (sd
, R6_REGNUM
) + count
);
2301 h8_set_reg (sd
, R4_REGNUM
, h8_get_reg (sd
, R4_REGNUM
) &
2302 ((code
->opcode
== O (O_EEPMOV
, SW
))
2303 ? (~0xffff) : (~0xff)));
2304 cycles
+= 2 * count
;
2309 case O (O_ADDS
, SL
): /* adds (.l) */
2311 * This insn only uses register operands, but still
2312 * it would be cleaner to use fetch and store... */
2313 SET_L_REG (code
->dst
.reg
,
2314 GET_L_REG (code
->dst
.reg
)
2315 + code
->src
.literal
);
2319 case O (O_SUBS
, SL
): /* subs (.l) */
2321 * This insn only uses register operands, but still
2322 * it would be cleaner to use fetch and store... */
2323 SET_L_REG (code
->dst
.reg
,
2324 GET_L_REG (code
->dst
.reg
)
2325 - code
->src
.literal
);
2328 case O (O_CMP
, SB
): /* cmp.b */
2329 if (fetch (sd
, &code
->dst
, &rd
))
2331 if (fetch (sd
, &code
->src
, &ea
))
2335 goto just_flags_alu8
;
2337 case O (O_CMP
, SW
): /* cmp.w */
2338 if (fetch (sd
, &code
->dst
, &rd
))
2340 if (fetch (sd
, &code
->src
, &ea
))
2344 goto just_flags_alu16
;
2346 case O (O_CMP
, SL
): /* cmp.l */
2347 if (fetch (sd
, &code
->dst
, &rd
))
2349 if (fetch (sd
, &code
->src
, &ea
))
2353 goto just_flags_alu32
;
2355 case O (O_DEC
, SB
): /* dec.b */
2357 * This insn only uses register operands, but still
2358 * it would be cleaner to use fetch and store... */
2359 rd
= GET_B_REG (code
->src
.reg
);
2362 SET_B_REG (code
->src
.reg
, res
);
2363 goto just_flags_inc8
;
2365 case O (O_DEC
, SW
): /* dec.w */
2367 * This insn only uses register operands, but still
2368 * it would be cleaner to use fetch and store... */
2369 rd
= GET_W_REG (code
->dst
.reg
);
2370 ea
= -code
->src
.literal
;
2372 SET_W_REG (code
->dst
.reg
, res
);
2373 goto just_flags_inc16
;
2375 case O (O_DEC
, SL
): /* dec.l */
2377 * This insn only uses register operands, but still
2378 * it would be cleaner to use fetch and store... */
2379 rd
= GET_L_REG (code
->dst
.reg
);
2380 ea
= -code
->src
.literal
;
2382 SET_L_REG (code
->dst
.reg
, res
);
2383 goto just_flags_inc32
;
2385 case O (O_INC
, SB
): /* inc.b */
2387 * This insn only uses register operands, but still
2388 * it would be cleaner to use fetch and store... */
2389 rd
= GET_B_REG (code
->src
.reg
);
2392 SET_B_REG (code
->src
.reg
, res
);
2393 goto just_flags_inc8
;
2395 case O (O_INC
, SW
): /* inc.w */
2397 * This insn only uses register operands, but still
2398 * it would be cleaner to use fetch and store... */
2399 rd
= GET_W_REG (code
->dst
.reg
);
2400 ea
= code
->src
.literal
;
2402 SET_W_REG (code
->dst
.reg
, res
);
2403 goto just_flags_inc16
;
2405 case O (O_INC
, SL
): /* inc.l */
2407 * This insn only uses register operands, but still
2408 * it would be cleaner to use fetch and store... */
2409 rd
= GET_L_REG (code
->dst
.reg
);
2410 ea
= code
->src
.literal
;
2412 SET_L_REG (code
->dst
.reg
, res
);
2413 goto just_flags_inc32
;
2415 case O (O_LDC
, SB
): /* ldc.b */
2416 if (fetch (sd
, &code
->src
, &res
))
2420 case O (O_LDC
, SW
): /* ldc.w */
2421 if (fetch (sd
, &code
->src
, &res
))
2424 /* Word operand, value from MSB, must be shifted. */
2428 case O (O_LDC
, SL
): /* ldc.l */
2429 if (fetch (sd
, &code
->src
, &res
))
2431 switch (code
->dst
.type
) {
2432 case X (OP_SBR
, SL
):
2433 h8_set_sbr (sd
, res
);
2435 case X (OP_VBR
, SL
):
2436 h8_set_vbr (sd
, res
);
2443 case O (O_STC
, SW
): /* stc.w */
2444 case O (O_STC
, SB
): /* stc.b */
2445 if (code
->src
.type
== X (OP_CCR
, SB
))
2448 res
= h8_get_ccr (sd
);
2450 else if (code
->src
.type
== X (OP_EXR
, SB
) && h8300smode
)
2453 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2454 res
= h8_get_exr (sd
);
2459 /* Word operand, value to MSB, must be shifted. */
2460 if (code
->opcode
== X (O_STC
, SW
))
2462 if (store (sd
, &code
->dst
, res
))
2465 case O (O_STC
, SL
): /* stc.l */
2466 switch (code
->src
.type
) {
2467 case X (OP_SBR
, SL
):
2468 res
= h8_get_sbr (sd
);
2470 case X (OP_VBR
, SL
):
2471 res
= h8_get_vbr (sd
);
2476 if (store (sd
, &code
->dst
, res
))
2480 case O (O_ANDC
, SB
): /* andc.b */
2481 if (code
->dst
.type
== X (OP_CCR
, SB
))
2484 rd
= h8_get_ccr (sd
);
2486 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2489 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2490 rd
= h8_get_exr (sd
);
2494 ea
= code
->src
.literal
;
2498 case O (O_ORC
, SB
): /* orc.b */
2499 if (code
->dst
.type
== X (OP_CCR
, SB
))
2502 rd
= h8_get_ccr (sd
);
2504 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2507 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2508 rd
= h8_get_exr (sd
);
2512 ea
= code
->src
.literal
;
2516 case O (O_XORC
, SB
): /* xorc.b */
2517 if (code
->dst
.type
== X (OP_CCR
, SB
))
2520 rd
= h8_get_ccr (sd
);
2522 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2525 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2526 rd
= h8_get_exr (sd
);
2530 ea
= code
->src
.literal
;
2534 case O (O_BRAS
, SB
): /* bra/s */
2535 /* This is basically an ordinary branch, with a delay slot. */
2536 if (fetch (sd
, &code
->src
, &res
))
2544 /* Execution continues at next instruction, but
2545 delayed_branch is set up for next cycle. */
2546 h8_set_delayed_branch (sd
, code
->next_pc
+ res
);
2550 case O (O_BRAB
, SB
): /* bra rd.b */
2551 case O (O_BRAW
, SW
): /* bra rd.w */
2552 case O (O_BRAL
, SL
): /* bra erd.l */
2553 if (fetch (sd
, &code
->src
, &rd
))
2555 switch (OP_SIZE (code
->opcode
)) {
2556 case SB
: rd
&= 0xff; break;
2557 case SW
: rd
&= 0xffff; break;
2558 case SL
: rd
&= 0xffffffff; break;
2560 pc
= code
->next_pc
+ rd
;
2563 case O (O_BRABC
, SB
): /* bra/bc, branch if bit clear */
2564 case O (O_BRABS
, SB
): /* bra/bs, branch if bit set */
2565 case O (O_BSRBC
, SB
): /* bsr/bc, call if bit clear */
2566 case O (O_BSRBS
, SB
): /* bsr/bs, call if bit set */
2567 if (fetch (sd
, &code
->dst
, &rd
) ||
2568 fetch (sd
, &code
->src
, &bit
))
2571 if (code
->opcode
== O (O_BRABC
, SB
) || /* branch if clear */
2572 code
->opcode
== O (O_BSRBC
, SB
)) /* call if clear */
2574 if ((rd
& (1 << bit
))) /* no branch */
2577 else /* branch/call if set */
2579 if (!(rd
& (1 << bit
))) /* no branch */
2583 if (fetch (sd
, &code
->op3
, &res
)) /* branch */
2585 pc
= code
->next_pc
+ res
;
2587 if (code
->opcode
== O (O_BRABC
, SB
) ||
2588 code
->opcode
== O (O_BRABS
, SB
)) /* branch */
2596 case O (O_BRA
, SB
): /* bra, branch always */
2601 case O (O_BRN
, SB
): /* brn, ;-/ branch never? */
2606 case O (O_BHI
, SB
): /* bhi */
2612 case O (O_BLS
, SB
): /* bls */
2617 case O (O_BCS
, SB
): /* bcs, branch if carry set */
2622 case O (O_BCC
, SB
): /* bcc, branch if carry clear */
2627 case O (O_BEQ
, SB
): /* beq, branch if zero set */
2631 case O (O_BGT
, SB
): /* bgt */
2632 if (((Z
|| (N
^ V
)) == 0))
2636 case O (O_BLE
, SB
): /* ble */
2637 if (((Z
|| (N
^ V
)) == 1))
2641 case O (O_BGE
, SB
): /* bge */
2645 case O (O_BLT
, SB
): /* blt */
2649 case O (O_BMI
, SB
): /* bmi */
2653 case O (O_BNE
, SB
): /* bne, branch if zero clear */
2658 case O (O_BPL
, SB
): /* bpl */
2662 case O (O_BVC
, SB
): /* bvc */
2666 case O (O_BVS
, SB
): /* bvs */
2671 /* Trap for Command Line setup. */
2672 case O (O_SYS_CMDLINE
, SB
):
2674 int i
= 0; /* Loop counter. */
2675 int j
= 0; /* Loop counter. */
2676 int ind_arg_len
= 0; /* Length of each argument. */
2677 int no_of_args
= 0; /* The no. or cmdline args. */
2678 int current_location
= 0; /* Location of string. */
2679 int old_sp
= 0; /* The Initial Stack Pointer. */
2680 int no_of_slots
= 0; /* No. of slots required on the stack
2681 for storing cmdline args. */
2682 int sp_move
= 0; /* No. of locations by which the stack needs
2684 int new_sp
= 0; /* The final stack pointer location passed
2686 int *argv_ptrs
; /* Pointers of argv strings to be stored. */
2687 int argv_ptrs_location
= 0; /* Location of pointers to cmdline
2688 args on the stack. */
2689 int char_ptr_size
= 0; /* Size of a character pointer on
2691 int addr_cmdline
= 0; /* Memory location where cmdline has
2693 int size_cmdline
= 0; /* Size of cmdline. */
2695 /* Set the address of 256 free locations where command line is
2697 addr_cmdline
= cmdline_location();
2698 h8_set_reg (sd
, 0, addr_cmdline
);
2700 /* Counting the no. of commandline arguments. */
2701 for (i
= 0; h8_get_cmdline_arg (sd
, i
) != NULL
; i
++)
2704 /* No. of arguments in the command line. */
2707 /* Current location is just a temporary variable,which we are
2708 setting to the point to the start of our commandline string. */
2709 current_location
= addr_cmdline
;
2711 /* Allocating space for storing pointers of the command line
2713 argv_ptrs
= (int *) malloc (sizeof (int) * no_of_args
);
2715 /* Setting char_ptr_size to the sizeof (char *) on the different
2717 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2726 for (i
= 0; i
< no_of_args
; i
++)
2730 /* The size of the commandline argument. */
2731 ind_arg_len
= strlen (h8_get_cmdline_arg (sd
, i
)) + 1;
2733 /* The total size of the command line string. */
2734 size_cmdline
+= ind_arg_len
;
2736 /* As we have only 256 bytes, we need to provide a graceful
2737 exit. Anyways, a program using command line arguments
2738 where we cannot store all the command line arguments
2739 given may behave unpredictably. */
2740 if (size_cmdline
>= 256)
2742 h8_set_reg (sd
, 0, 0);
2747 /* current_location points to the memory where the next
2748 commandline argument is stored. */
2749 argv_ptrs
[i
] = current_location
;
2750 for (j
= 0; j
< ind_arg_len
; j
++)
2752 SET_MEMORY_B ((current_location
+
2753 (sizeof (char) * j
)),
2754 *(h8_get_cmdline_arg (sd
, i
) +
2755 sizeof (char) * j
));
2758 /* Setting current_location to the starting of next
2760 current_location
+= ind_arg_len
;
2764 /* This is the original position of the stack pointer. */
2765 old_sp
= h8_get_reg (sd
, SP_REGNUM
);
2767 /* We need space from the stack to store the pointers to argvs. */
2768 /* As we will infringe on the stack, we need to shift the stack
2769 pointer so that the data is not overwritten. We calculate how
2770 much space is required. */
2771 sp_move
= (no_of_args
) * (char_ptr_size
);
2773 /* The final position of stack pointer, we have thus taken some
2774 space from the stack. */
2775 new_sp
= old_sp
- sp_move
;
2777 /* Temporary variable holding value where the argv pointers need
2779 argv_ptrs_location
= new_sp
;
2781 /* The argv pointers are stored at sequential locations. As per
2783 for (i
= 0; i
< no_of_args
; i
++)
2785 /* Saving the argv pointer. */
2786 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2788 SET_MEMORY_L (argv_ptrs_location
, argv_ptrs
[i
]);
2792 SET_MEMORY_W (argv_ptrs_location
, argv_ptrs
[i
]);
2795 /* The next location where the pointer to the next argv
2796 string has to be stored. */
2797 argv_ptrs_location
+= char_ptr_size
;
2800 /* Required by POSIX, Setting 0x0 at the end of the list of argv
2802 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2804 SET_MEMORY_L (old_sp
, 0x0);
2808 SET_MEMORY_W (old_sp
, 0x0);
2811 /* Freeing allocated memory. */
2813 for (i
= 0; i
<= no_of_args
; i
++)
2815 free (h8_get_cmdline_arg (sd
, i
));
2817 free (h8_get_command_line (sd
));
2819 /* The no. of argv arguments are returned in Reg 0. */
2820 h8_set_reg (sd
, 0, no_of_args
);
2821 /* The Pointer to argv in Register 1. */
2822 h8_set_reg (sd
, 1, new_sp
);
2823 /* Setting the stack pointer to the new value. */
2824 h8_set_reg (sd
, SP_REGNUM
, new_sp
);
2828 /* System call processing starts. */
2829 case O (O_SYS_OPEN
, SB
):
2831 int len
= 0; /* Length of filename. */
2832 char *filename
; /* Filename would go here. */
2833 char temp_char
; /* Temporary character */
2834 int mode
= 0; /* Mode bits for the file. */
2835 int open_return
; /* Return value of open, file descriptor. */
2836 int i
; /* Loop counter */
2837 int filename_ptr
; /* Pointer to filename in cpu memory. */
2839 /* Setting filename_ptr to first argument of open, */
2840 /* and trying to get mode. */
2841 if ((h8300sxmode
|| h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2843 filename_ptr
= GET_L_REG (0);
2844 mode
= GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
) + 4);
2848 filename_ptr
= GET_W_REG (0);
2849 mode
= GET_MEMORY_W (h8_get_reg (sd
, SP_REGNUM
) + 2);
2852 /* Trying to find the length of the filename. */
2853 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
2856 while (temp_char
!= '\0')
2858 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
2862 /* Allocating space for the filename. */
2863 filename
= (char *) malloc (sizeof (char) * len
);
2865 /* String copying the filename from memory. */
2866 for (i
= 0; i
< len
; i
++)
2868 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
2869 filename
[i
] = temp_char
;
2872 /* Callback to open and return the file descriptor. */
2873 open_return
= sim_callback
->open (sim_callback
, filename
, mode
);
2875 /* Return value in register 0. */
2876 h8_set_reg (sd
, 0, open_return
);
2878 /* Freeing memory used for filename. */
2883 case O (O_SYS_READ
, SB
):
2885 char *char_ptr
; /* Where characters read would be stored. */
2886 int fd
; /* File descriptor */
2887 int buf_size
; /* BUF_SIZE parameter in read. */
2888 int i
= 0; /* Temporary Loop counter */
2889 int read_return
= 0; /* Return value from callback to
2892 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2893 buf_size
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2895 char_ptr
= (char *) malloc (sizeof (char) * buf_size
);
2897 /* Callback to read and return the no. of characters read. */
2899 sim_callback
->read (sim_callback
, fd
, char_ptr
, buf_size
);
2901 /* The characters read are stored in cpu memory. */
2902 for (i
= 0; i
< buf_size
; i
++)
2904 SET_MEMORY_B ((h8_get_reg (sd
, 1) + (sizeof (char) * i
)),
2905 *(char_ptr
+ (sizeof (char) * i
)));
2908 /* Return value in Register 0. */
2909 h8_set_reg (sd
, 0, read_return
);
2911 /* Freeing memory used as buffer. */
2916 case O (O_SYS_WRITE
, SB
):
2918 int fd
; /* File descriptor */
2919 char temp_char
; /* Temporary character */
2920 int len
; /* Length of write, Parameter II to write. */
2921 int char_ptr
; /* Character Pointer, Parameter I of write. */
2922 char *ptr
; /* Where characters to be written are stored.
2924 int write_return
; /* Return value from callback to write. */
2925 int i
= 0; /* Loop counter */
2927 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2928 char_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2929 len
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2931 /* Allocating space for the characters to be written. */
2932 ptr
= (char *) malloc (sizeof (char) * len
);
2934 /* Fetching the characters from cpu memory. */
2935 for (i
= 0; i
< len
; i
++)
2937 temp_char
= GET_MEMORY_B (char_ptr
+ i
);
2941 /* Callback write and return the no. of characters written. */
2942 write_return
= sim_callback
->write (sim_callback
, fd
, ptr
, len
);
2944 /* Return value in Register 0. */
2945 h8_set_reg (sd
, 0, write_return
);
2947 /* Freeing memory used as buffer. */
2952 case O (O_SYS_LSEEK
, SB
):
2954 int fd
; /* File descriptor */
2955 int offset
; /* Offset */
2956 int origin
; /* Origin */
2957 int lseek_return
; /* Return value from callback to lseek. */
2959 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2960 offset
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2961 origin
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2963 /* Callback lseek and return offset. */
2965 sim_callback
->lseek (sim_callback
, fd
, offset
, origin
);
2967 /* Return value in register 0. */
2968 h8_set_reg (sd
, 0, lseek_return
);
2972 case O (O_SYS_CLOSE
, SB
):
2974 int fd
; /* File descriptor */
2975 int close_return
; /* Return value from callback to close. */
2977 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2979 /* Callback close and return. */
2980 close_return
= sim_callback
->close (sim_callback
, fd
);
2982 /* Return value in register 0. */
2983 h8_set_reg (sd
, 0, close_return
);
2987 case O (O_SYS_FSTAT
, SB
):
2989 int fd
; /* File descriptor */
2990 struct stat stat_rec
; /* Stat record */
2991 int fstat_return
; /* Return value from callback to stat. */
2992 int stat_ptr
; /* Pointer to stat record. */
2993 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
2995 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2997 /* Setting stat_ptr to second argument of stat. */
2998 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
3000 /* Callback stat and return. */
3001 fstat_return
= sim_callback
->to_fstat (sim_callback
, fd
,
3004 /* Have stat_ptr point to starting of stat_rec. */
3005 temp_stat_ptr
= (char *) (&stat_rec
);
3007 /* Setting up the stat structure returned. */
3008 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
3010 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
3012 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
3014 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
3016 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
3018 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
3020 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
3022 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
3024 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
3026 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
3028 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
3030 /* Return value in register 0. */
3031 h8_set_reg (sd
, 0, fstat_return
);
3035 case O (O_SYS_STAT
, SB
):
3037 int len
= 0; /* Length of filename. */
3038 char *filename
; /* Filename would go here. */
3039 char temp_char
; /* Temporary character */
3040 int filename_ptr
; /* Pointer to filename in cpu memory. */
3041 struct stat stat_rec
; /* Stat record */
3042 int stat_return
; /* Return value from callback to stat */
3043 int stat_ptr
; /* Pointer to stat record. */
3044 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
3045 int i
= 0; /* Loop Counter */
3047 /* Setting filename_ptr to first argument of open. */
3048 filename_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
3050 /* Trying to find the length of the filename. */
3051 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
3054 while (temp_char
!= '\0')
3056 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
3060 /* Allocating space for the filename. */
3061 filename
= (char *) malloc (sizeof (char) * len
);
3063 /* String copying the filename from memory. */
3064 for (i
= 0; i
< len
; i
++)
3066 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
3067 filename
[i
] = temp_char
;
3070 /* Setting stat_ptr to second argument of stat. */
3071 /* stat_ptr = h8_get_reg (sd, 1); */
3072 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
3074 /* Callback stat and return. */
3076 sim_callback
->to_stat (sim_callback
, filename
, &stat_rec
);
3078 /* Have stat_ptr point to starting of stat_rec. */
3079 temp_stat_ptr
= (char *) (&stat_rec
);
3081 /* Freeing memory used for filename. */
3084 /* Setting up the stat structure returned. */
3085 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
3087 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
3089 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
3091 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
3093 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
3095 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
3097 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
3099 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
3101 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
3103 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
3105 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
3107 /* Return value in register 0. */
3108 h8_set_reg (sd
, 0, stat_return
);
3111 /* End of system call processing. */
3113 case O (O_NOT
, SB
): /* not.b */
3114 if (fetch2 (sd
, &code
->src
, &rd
))
3120 case O (O_NOT
, SW
): /* not.w */
3121 if (fetch2 (sd
, &code
->src
, &rd
))
3127 case O (O_NOT
, SL
): /* not.l */
3128 if (fetch2 (sd
, &code
->src
, &rd
))
3134 case O (O_SHLL
, SB
): /* shll.b */
3135 case O (O_SHLR
, SB
): /* shlr.b */
3136 if (fetch2 (sd
, &code
->dst
, &rd
))
3139 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3140 ea
= 1; /* unary op */
3141 else /* binary op */
3142 fetch (sd
, &code
->src
, &ea
);
3144 if (code
->opcode
== O (O_SHLL
, SB
))
3147 c
= rd
& (0x80 >> (ea
- 1));
3153 c
= rd
& (1 << (ea
- 1));
3154 rd
= (unsigned char) rd
>> ea
;
3158 case O (O_SHLL
, SW
): /* shll.w */
3159 case O (O_SHLR
, SW
): /* shlr.w */
3160 if (fetch2 (sd
, &code
->dst
, &rd
))
3163 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3164 ea
= 1; /* unary op */
3166 fetch (sd
, &code
->src
, &ea
);
3168 if (code
->opcode
== O (O_SHLL
, SW
))
3171 c
= rd
& (0x8000 >> (ea
- 1));
3177 c
= rd
& (1 << (ea
- 1));
3178 rd
= (unsigned short) rd
>> ea
;
3182 case O (O_SHLL
, SL
): /* shll.l */
3183 case O (O_SHLR
, SL
): /* shlr.l */
3184 if (fetch2 (sd
, &code
->dst
, &rd
))
3187 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3188 ea
= 1; /* unary op */
3190 fetch (sd
, &code
->src
, &ea
);
3192 if (code
->opcode
== O (O_SHLL
, SL
))
3195 c
= rd
& (0x80000000 >> (ea
- 1));
3201 c
= rd
& (1 << (ea
- 1));
3202 rd
= (unsigned int) rd
>> ea
;
3206 case O (O_SHAL
, SB
):
3207 case O (O_SHAR
, SB
):
3208 if (fetch2 (sd
, &code
->dst
, &rd
))
3211 if (code
->src
.type
== X (OP_IMM
, SB
))
3212 fetch (sd
, &code
->src
, &ea
);
3216 if (code
->opcode
== O (O_SHAL
, SB
))
3218 c
= rd
& (0x80 >> (ea
- 1));
3219 res
= rd
>> (7 - ea
);
3220 v
= ((res
& 1) && !(res
& 2))
3221 || (!(res
& 1) && (res
& 2));
3226 c
= rd
& (1 << (ea
- 1));
3228 rd
= ((signed char) rd
) >> ea
;
3232 case O (O_SHAL
, SW
):
3233 case O (O_SHAR
, SW
):
3234 if (fetch2 (sd
, &code
->dst
, &rd
))
3237 if (code
->src
.type
== X (OP_IMM
, SW
))
3238 fetch (sd
, &code
->src
, &ea
);
3242 if (code
->opcode
== O (O_SHAL
, SW
))
3244 c
= rd
& (0x8000 >> (ea
- 1));
3245 res
= rd
>> (15 - ea
);
3246 v
= ((res
& 1) && !(res
& 2))
3247 || (!(res
& 1) && (res
& 2));
3252 c
= rd
& (1 << (ea
- 1));
3254 rd
= ((signed short) rd
) >> ea
;
3258 case O (O_SHAL
, SL
):
3259 case O (O_SHAR
, SL
):
3260 if (fetch2 (sd
, &code
->dst
, &rd
))
3263 if (code
->src
.type
== X (OP_IMM
, SL
))
3264 fetch (sd
, &code
->src
, &ea
);
3268 if (code
->opcode
== O (O_SHAL
, SL
))
3270 c
= rd
& (0x80000000 >> (ea
- 1));
3271 res
= rd
>> (31 - ea
);
3272 v
= ((res
& 1) && !(res
& 2))
3273 || (!(res
& 1) && (res
& 2));
3278 c
= rd
& (1 << (ea
- 1));
3280 rd
= ((signed int) rd
) >> ea
;
3284 case O (O_ROTL
, SB
):
3285 case O (O_ROTR
, SB
):
3286 if (fetch2 (sd
, &code
->dst
, &rd
))
3289 if (code
->src
.type
== X (OP_IMM
, SB
))
3290 fetch (sd
, &code
->src
, &ea
);
3295 if (code
->opcode
== O (O_ROTL
, SB
))
3305 rd
= ((unsigned char) rd
) >> 1;
3313 case O (O_ROTL
, SW
):
3314 case O (O_ROTR
, SW
):
3315 if (fetch2 (sd
, &code
->dst
, &rd
))
3318 if (code
->src
.type
== X (OP_IMM
, SW
))
3319 fetch (sd
, &code
->src
, &ea
);
3324 if (code
->opcode
== O (O_ROTL
, SW
))
3334 rd
= ((unsigned short) rd
) >> 1;
3342 case O (O_ROTL
, SL
):
3343 case O (O_ROTR
, SL
):
3344 if (fetch2 (sd
, &code
->dst
, &rd
))
3347 if (code
->src
.type
== X (OP_IMM
, SL
))
3348 fetch (sd
, &code
->src
, &ea
);
3353 if (code
->opcode
== O (O_ROTL
, SL
))
3355 c
= rd
& 0x80000000;
3363 rd
= ((unsigned int) rd
) >> 1;
3371 case O (O_ROTXL
, SB
):
3372 case O (O_ROTXR
, SB
):
3373 if (fetch2 (sd
, &code
->dst
, &rd
))
3376 if (code
->src
.type
== X (OP_IMM
, SB
))
3377 fetch (sd
, &code
->src
, &ea
);
3382 if (code
->opcode
== O (O_ROTXL
, SB
))
3393 rd
= ((unsigned char) rd
) >> 1;
3402 case O (O_ROTXL
, SW
):
3403 case O (O_ROTXR
, SW
):
3404 if (fetch2 (sd
, &code
->dst
, &rd
))
3407 if (code
->src
.type
== X (OP_IMM
, SW
))
3408 fetch (sd
, &code
->src
, &ea
);
3413 if (code
->opcode
== O (O_ROTXL
, SW
))
3424 rd
= ((unsigned short) rd
) >> 1;
3433 case O (O_ROTXL
, SL
):
3434 case O (O_ROTXR
, SL
):
3435 if (fetch2 (sd
, &code
->dst
, &rd
))
3438 if (code
->src
.type
== X (OP_IMM
, SL
))
3439 fetch (sd
, &code
->src
, &ea
);
3444 if (code
->opcode
== O (O_ROTXL
, SL
))
3446 res
= rd
& 0x80000000;
3455 rd
= ((unsigned int) rd
) >> 1;
3466 case O (O_JMP
, SB
): /* jmp */
3468 fetch (sd
, &code
->src
, &pc
);
3473 case O (O_JSR
, SB
): /* jsr, jump to subroutine */
3475 if (fetch (sd
, &code
->src
, &pc
))
3478 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3480 if (h8300hmode
&& !h8300_normal_mode
)
3483 SET_MEMORY_L (tmp
, code
->next_pc
);
3488 SET_MEMORY_W (tmp
, code
->next_pc
);
3490 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3496 case O (O_BSR
, SB
): /* bsr, branch to subroutine */
3497 if (fetch (sd
, &code
->src
, &res
))
3499 pc
= code
->next_pc
+ res
;
3502 case O (O_RTE
, SN
): /* rte, return from exception */
3504 /* Pops exr and ccr before pc -- otherwise identical to rts. */
3505 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3507 if (h8300smode
) /* pop exr */
3509 h8_set_exr (sd
, GET_MEMORY_L (tmp
));
3512 if (h8300hmode
&& !h8300_normal_mode
)
3514 h8_set_ccr (sd
, GET_MEMORY_L (tmp
));
3516 pc
= GET_MEMORY_L (tmp
);
3521 h8_set_ccr (sd
, GET_MEMORY_W (tmp
));
3523 pc
= GET_MEMORY_W (tmp
);
3528 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3531 case O (O_RTS
, SN
): /* rts, return from subroutine */
3533 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3535 if (h8300hmode
&& !h8300_normal_mode
)
3537 pc
= GET_MEMORY_L (tmp
);
3542 pc
= GET_MEMORY_W (tmp
);
3546 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3549 case O (O_ILL
, SB
): /* illegal */
3550 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
3553 case O (O_SLEEP
, SN
): /* sleep */
3554 /* Check for magic numbers in r1 and r2. */
3555 if ((h8_get_reg (sd
, R1_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC1
&&
3556 (h8_get_reg (sd
, R2_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC2
&&
3557 SIM_WIFEXITED (h8_get_reg (sd
, 0)))
3559 /* This trap comes from _exit, not from gdb. */
3560 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_exited
,
3561 SIM_WEXITSTATUS (h8_get_reg (sd
, 0)));
3564 /* Unfortunately this won't really work, because
3565 when we take a breakpoint trap, R0 has a "random",
3566 user-defined value. Don't see any immediate solution. */
3567 else if (SIM_WIFSTOPPED (h8_get_reg (sd
, 0)))
3569 /* Pass the stop signal up to gdb. */
3570 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
,
3571 SIM_WSTOPSIG (h8_get_reg (sd
, 0)));
3576 /* Treat it as a sigtrap. */
3577 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGTRAP
);
3581 case O (O_TRAPA
, SB
): /* trapa */
3582 if (fetch (sd
, &code
->src
, &res
))
3583 goto end
; /* res is vector number. */
3585 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3586 if(h8300_normal_mode
)
3589 SET_MEMORY_W (tmp
, code
->next_pc
);
3591 SET_MEMORY_W (tmp
, h8_get_ccr (sd
));
3596 SET_MEMORY_L (tmp
, code
->next_pc
);
3598 SET_MEMORY_L (tmp
, h8_get_ccr (sd
));
3606 SET_MEMORY_L (tmp
, h8_get_exr (sd
));
3609 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3611 if(h8300_normal_mode
)
3612 pc
= GET_MEMORY_L (0x10 + res
* 2); /* Vector addresses are 0x10,0x12,0x14 and 0x16 */
3614 pc
= GET_MEMORY_L (0x20 + res
* 4);
3618 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGTRAP
);
3621 case O (O_BSETEQ
, SB
):
3626 case O (O_BSETNE
, SB
):
3631 case O (O_BCLREQ
, SB
):
3636 case O (O_BCLRNE
, SB
):
3641 OBITOP (O_BNOT
, 1, 1, ea
^= m
); /* bnot */
3642 OBITOP (O_BTST
, 1, 0, nz
= ea
& m
); /* btst */
3644 OBITOP (O_BSET
, 1, 1, ea
|= m
); /* bset */
3646 OBITOP (O_BCLR
, 1, 1, ea
&= ~m
); /* bclr */
3647 OBITOP (O_BLD
, 1, 0, c
= ea
& m
); /* bld */
3648 OBITOP (O_BILD
, 1, 0, c
= !(ea
& m
)); /* bild */
3649 OBITOP (O_BST
, 1, 1, ea
&= ~m
;
3650 if (C
) ea
|= m
); /* bst */
3651 OBITOP (O_BIST
, 1, 1, ea
&= ~m
;
3652 if (!C
) ea
|= m
); /* bist */
3653 OBITOP (O_BSTZ
, 1, 1, ea
&= ~m
;
3654 if (Z
) ea
|= m
); /* bstz */
3655 OBITOP (O_BISTZ
, 1, 1, ea
&= ~m
;
3656 if (!Z
) ea
|= m
); /* bistz */
3657 OBITOP (O_BAND
, 1, 0, c
= (ea
& m
) && C
); /* band */
3658 OBITOP (O_BIAND
, 1, 0, c
= !(ea
& m
) && C
); /* biand */
3659 OBITOP (O_BOR
, 1, 0, c
= (ea
& m
) || C
); /* bor */
3660 OBITOP (O_BIOR
, 1, 0, c
= !(ea
& m
) || C
); /* bior */
3661 OBITOP (O_BXOR
, 1, 0, c
= ((ea
& m
) != 0)!= C
); /* bxor */
3662 OBITOP (O_BIXOR
, 1, 0, c
= !(ea
& m
) != C
); /* bixor */
3664 case O (O_BFLD
, SB
): /* bfld */
3667 if (fetch (sd
, &code
->src
, &bit
))
3672 if (fetch (sd
, &code
->dst
, &ea
))
3682 if (store (sd
, &code
->op3
, ea
))
3687 case O(O_BFST
, SB
): /* bfst */
3688 /* bitfield store */
3689 /* NOTE: the imm8 value is in dst, and the ea value
3690 (which is actually the destination) is in op3.
3691 It has to be that way, to avoid breaking the assembler. */
3693 if (fetch (sd
, &code
->dst
, &bit
)) /* imm8 */
3695 if (bit
== 0) /* noop -- nothing to do. */
3698 if (fetch (sd
, &code
->src
, &rd
)) /* reg8 src */
3701 if (fetch2 (sd
, &code
->op3
, &ea
)) /* ea dst */
3704 /* Left-shift the register data into position. */
3705 for (tmp
= bit
; !(tmp
& 1); tmp
>>= 1)
3708 /* Combine it with the neighboring bits. */
3709 ea
= (ea
& ~bit
) | (rd
& bit
);
3712 if (store2 (sd
, &code
->op3
, ea
))
3716 case O (O_CLRMAC
, SN
): /* clrmac */
3717 h8_set_mach (sd
, 0);
3718 h8_set_macl (sd
, 0);
3719 h8_set_macZ (sd
, 1);
3720 h8_set_macV (sd
, 0);
3721 h8_set_macN (sd
, 0);
3724 case O (O_STMAC
, SL
): /* stmac, 260 */
3725 switch (code
->src
.type
) {
3726 case X (OP_MACH
, SL
):
3727 res
= h8_get_mach (sd
);
3728 if (res
& 0x200) /* sign extend */
3731 case X (OP_MACL
, SL
):
3732 res
= h8_get_macl (sd
);
3734 default: goto illegal
;
3736 nz
= !h8_get_macZ (sd
);
3737 n
= h8_get_macN (sd
);
3738 v
= h8_get_macV (sd
);
3740 if (store (sd
, &code
->dst
, res
))
3745 case O (O_LDMAC
, SL
): /* ldmac, 179 */
3746 if (fetch (sd
, &code
->src
, &rd
))
3749 switch (code
->dst
.type
) {
3750 case X (OP_MACH
, SL
):
3751 rd
&= 0x3ff; /* Truncate to 10 bits */
3752 h8_set_mach (sd
, rd
);
3754 case X (OP_MACL
, SL
):
3755 h8_set_macl (sd
, rd
);
3757 default: goto illegal
;
3759 h8_set_macV (sd
, 0);
3763 if (fetch (sd
, &code
->src
, &rd
) ||
3764 fetch (sd
, &code
->dst
, &res
))
3767 /* Ye gods, this is non-portable!
3768 However, the existing mul/div code is similar. */
3769 res
= SEXTSHORT (res
) * SEXTSHORT (rd
);
3771 if (h8_get_macS (sd
)) /* Saturating mode */
3773 long long mac
= h8_get_macl (sd
);
3775 if (mac
& 0x80000000) /* sign extend */
3776 mac
|= 0xffffffff00000000LL
;
3779 if (mac
> 0x7fffffff || mac
< 0xffffffff80000000LL
)
3780 h8_set_macV (sd
, 1);
3781 h8_set_macZ (sd
, (mac
== 0));
3782 h8_set_macN (sd
, (mac
< 0));
3783 h8_set_macl (sd
, (int) mac
);
3785 else /* "Less Saturating" mode */
3787 long long mac
= h8_get_mach (sd
);
3789 mac
+= h8_get_macl (sd
);
3791 if (mac
& 0x20000000000LL
) /* sign extend */
3792 mac
|= 0xfffffc0000000000LL
;
3795 if (mac
> 0x1ffffffffffLL
||
3796 mac
< (long long) 0xfffffe0000000000LL
)
3797 h8_set_macV (sd
, 1);
3798 h8_set_macZ (sd
, (mac
== 0));
3799 h8_set_macN (sd
, (mac
< 0));
3800 h8_set_macl (sd
, (int) mac
);
3802 h8_set_mach (sd
, (int) (mac
& 0x3ff));
3806 case O (O_MULS
, SW
): /* muls.w */
3807 if (fetch (sd
, &code
->src
, &ea
) ||
3808 fetch (sd
, &code
->dst
, &rd
))
3811 ea
= SEXTSHORT (ea
);
3812 res
= SEXTSHORT (ea
* SEXTSHORT (rd
));
3816 if (store (sd
, &code
->dst
, res
))
3821 case O (O_MULS
, SL
): /* muls.l */
3822 if (fetch (sd
, &code
->src
, &ea
) ||
3823 fetch (sd
, &code
->dst
, &rd
))
3828 n
= res
& 0x80000000;
3829 nz
= res
& 0xffffffff;
3830 if (store (sd
, &code
->dst
, res
))
3834 case O (O_MULSU
, SL
): /* muls/u.l */
3835 if (fetch (sd
, &code
->src
, &ea
) ||
3836 fetch (sd
, &code
->dst
, &rd
))
3839 /* Compute upper 32 bits of the 64-bit result. */
3840 res
= (((long long) ea
) * ((long long) rd
)) >> 32;
3842 n
= res
& 0x80000000;
3843 nz
= res
& 0xffffffff;
3844 if (store (sd
, &code
->dst
, res
))
3848 case O (O_MULU
, SW
): /* mulu.w */
3849 if (fetch (sd
, &code
->src
, &ea
) ||
3850 fetch (sd
, &code
->dst
, &rd
))
3853 res
= UEXTSHORT ((UEXTSHORT (ea
) * UEXTSHORT (rd
)));
3855 /* Don't set Z or N. */
3856 if (store (sd
, &code
->dst
, res
))
3861 case O (O_MULU
, SL
): /* mulu.l */
3862 if (fetch (sd
, &code
->src
, &ea
) ||
3863 fetch (sd
, &code
->dst
, &rd
))
3868 /* Don't set Z or N. */
3869 if (store (sd
, &code
->dst
, res
))
3874 case O (O_MULUU
, SL
): /* mulu/u.l */
3875 if (fetch (sd
, &code
->src
, &ea
) ||
3876 fetch (sd
, &code
->dst
, &rd
))
3879 /* Compute upper 32 bits of the 64-bit result. */
3880 res
= (((unsigned long long) (unsigned) ea
) *
3881 ((unsigned long long) (unsigned) rd
)) >> 32;
3883 /* Don't set Z or N. */
3884 if (store (sd
, &code
->dst
, res
))
3889 case O (O_MULXS
, SB
): /* mulxs.b */
3890 if (fetch (sd
, &code
->src
, &ea
) ||
3891 fetch (sd
, &code
->dst
, &rd
))
3895 res
= ea
* SEXTCHAR (rd
);
3899 if (store (sd
, &code
->dst
, res
))
3904 case O (O_MULXS
, SW
): /* mulxs.w */
3905 if (fetch (sd
, &code
->src
, &ea
) ||
3906 fetch (sd
, &code
->dst
, &rd
))
3909 ea
= SEXTSHORT (ea
);
3910 res
= ea
* SEXTSHORT (rd
& 0xffff);
3912 n
= res
& 0x80000000;
3913 nz
= res
& 0xffffffff;
3914 if (store (sd
, &code
->dst
, res
))
3919 case O (O_MULXU
, SB
): /* mulxu.b */
3920 if (fetch (sd
, &code
->src
, &ea
) ||
3921 fetch (sd
, &code
->dst
, &rd
))
3924 res
= UEXTCHAR (ea
) * UEXTCHAR (rd
);
3926 if (store (sd
, &code
->dst
, res
))
3931 case O (O_MULXU
, SW
): /* mulxu.w */
3932 if (fetch (sd
, &code
->src
, &ea
) ||
3933 fetch (sd
, &code
->dst
, &rd
))
3936 res
= UEXTSHORT (ea
) * UEXTSHORT (rd
);
3938 if (store (sd
, &code
->dst
, res
))
3943 case O (O_TAS
, SB
): /* tas (test and set) */
3944 if (!h8300sxmode
) /* h8sx can use any register. */
3945 switch (code
->src
.reg
)
3956 if (fetch (sd
, &code
->src
, &res
))
3958 if (store (sd
, &code
->src
, res
| 0x80))
3961 goto just_flags_log8
;
3963 case O (O_DIVU
, SW
): /* divu.w */
3964 if (fetch (sd
, &code
->src
, &ea
) ||
3965 fetch (sd
, &code
->dst
, &rd
))
3971 res
= (unsigned) (UEXTSHORT (rd
) / UEXTSHORT (ea
));
3975 if (store (sd
, &code
->dst
, res
))
3979 case O (O_DIVU
, SL
): /* divu.l */
3980 if (fetch (sd
, &code
->src
, &ea
) ||
3981 fetch (sd
, &code
->dst
, &rd
))
3984 n
= ea
& 0x80000000;
3985 nz
= ea
& 0xffffffff;
3987 res
= (unsigned) rd
/ ea
;
3991 if (store (sd
, &code
->dst
, res
))
3995 case O (O_DIVS
, SW
): /* divs.w */
3996 if (fetch (sd
, &code
->src
, &ea
) ||
3997 fetch (sd
, &code
->dst
, &rd
))
4002 res
= SEXTSHORT (rd
) / SEXTSHORT (ea
);
4012 if (store (sd
, &code
->dst
, res
))
4016 case O (O_DIVS
, SL
): /* divs.l */
4017 if (fetch (sd
, &code
->src
, &ea
) ||
4018 fetch (sd
, &code
->dst
, &rd
))
4032 n
= res
& 0x80000000;
4033 if (store (sd
, &code
->dst
, res
))
4037 case O (O_DIVXU
, SB
): /* divxu.b */
4038 if (fetch (sd
, &code
->src
, &ea
) ||
4039 fetch (sd
, &code
->dst
, &rd
))
4042 rd
= UEXTSHORT (rd
);
4049 tmp
= (unsigned) rd
% ea
;
4050 res
= (unsigned) rd
/ ea
;
4058 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
4062 case O (O_DIVXU
, SW
): /* divxu.w */
4063 if (fetch (sd
, &code
->src
, &ea
) ||
4064 fetch (sd
, &code
->dst
, &rd
))
4067 ea
= UEXTSHORT (ea
);
4073 tmp
= (unsigned) rd
% ea
;
4074 res
= (unsigned) rd
/ ea
;
4082 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
4086 case O (O_DIVXS
, SB
): /* divxs.b */
4087 if (fetch (sd
, &code
->src
, &ea
) ||
4088 fetch (sd
, &code
->dst
, &rd
))
4091 rd
= SEXTSHORT (rd
);
4096 tmp
= (int) rd
% (int) ea
;
4097 res
= (int) rd
/ (int) ea
;
4108 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
4112 case O (O_DIVXS
, SW
): /* divxs.w */
4113 if (fetch (sd
, &code
->src
, &ea
) ||
4114 fetch (sd
, &code
->dst
, &rd
))
4117 ea
= SEXTSHORT (ea
);
4121 tmp
= (int) rd
% (int) ea
;
4122 res
= (int) rd
/ (int) ea
;
4132 n
= res
& 0x80000000;
4133 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
4137 case O (O_EXTS
, SW
): /* exts.w, signed extend */
4138 if (fetch2 (sd
, &code
->dst
, &rd
))
4140 ea
= rd
& 0x80 ? -256 : 0;
4141 res
= (rd
& 0xff) + ea
;
4144 case O (O_EXTS
, SL
): /* exts.l, signed extend */
4145 if (fetch2 (sd
, &code
->dst
, &rd
))
4147 if (code
->src
.type
== X (OP_IMM
, SL
))
4149 if (fetch (sd
, &code
->src
, &ea
))
4152 if (ea
== 2) /* exts.l #2, nn */
4154 /* Sign-extend from 8-bit to 32-bit. */
4155 ea
= rd
& 0x80 ? -256 : 0;
4156 res
= (rd
& 0xff) + ea
;
4160 /* Sign-extend from 16-bit to 32-bit. */
4161 ea
= rd
& 0x8000 ? -65536 : 0;
4162 res
= (rd
& 0xffff) + ea
;
4165 case O (O_EXTU
, SW
): /* extu.w, unsigned extend */
4166 if (fetch2 (sd
, &code
->dst
, &rd
))
4169 res
= (rd
& 0xff) + ea
;
4172 case O (O_EXTU
, SL
): /* extu.l, unsigned extend */
4173 if (fetch2 (sd
, &code
->dst
, &rd
))
4175 if (code
->src
.type
== X (OP_IMM
, SL
))
4177 if (fetch (sd
, &code
->src
, &ea
))
4180 if (ea
== 2) /* extu.l #2, nn */
4182 /* Zero-extend from 8-bit to 32-bit. */
4184 res
= (rd
& 0xff) + ea
;
4188 /* Zero-extend from 16-bit to 32-bit. */
4190 res
= (rd
& 0xffff) + ea
;
4193 case O (O_NOP
, SN
): /* nop */
4196 case O (O_STM
, SL
): /* stm, store to memory */
4198 int nregs
, firstreg
, i
;
4200 nregs
= GET_MEMORY_B (pc
+ 1);
4203 firstreg
= code
->src
.reg
;
4205 for (i
= firstreg
; i
<= firstreg
+ nregs
; i
++)
4207 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) - 4);
4208 SET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
), h8_get_reg (sd
, i
));
4213 case O (O_LDM
, SL
): /* ldm, load from memory */
4214 case O (O_RTEL
, SN
): /* rte/l, ldm plus rte */
4215 case O (O_RTSL
, SN
): /* rts/l, ldm plus rts */
4217 int nregs
, firstreg
, i
;
4219 nregs
= ((GET_MEMORY_B (pc
+ 1) >> 4) & 0xf);
4220 firstreg
= code
->dst
.reg
& 0xf;
4221 for (i
= firstreg
; i
>= firstreg
- nregs
; i
--)
4223 h8_set_reg (sd
, i
, GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
)));
4224 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) + 4);
4227 switch (code
->opcode
) {
4228 case O (O_RTEL
, SN
):
4230 case O (O_RTSL
, SN
):
4239 /* Decimal Adjust Addition. This is for BCD arithmetic. */
4240 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch? */
4241 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4242 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4243 res
= res
; /* Value added == 0. */
4244 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4245 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4246 res
= res
+ 0x6; /* Value added == 6. */
4247 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4248 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4249 res
= res
+ 0x6; /* Value added == 6. */
4250 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4251 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4252 res
= res
+ 0x60; /* Value added == 60. */
4253 else if (!c
&& (9 <= (res
>> 4) && (res
>> 4) <= 15) &&
4254 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4255 res
= res
+ 0x66; /* Value added == 66. */
4256 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4257 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4258 res
= res
+ 0x66; /* Value added == 66. */
4259 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4260 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4261 res
= res
+ 0x60; /* Value added == 60. */
4262 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4263 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4264 res
= res
+ 0x66; /* Value added == 66. */
4265 else if (c
&& (1 <= (res
>> 4) && (res
>> 4) <= 3) &&
4266 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4267 res
= res
+ 0x66; /* Value added == 66. */
4272 /* Decimal Adjust Subtraction. This is for BCD arithmetic. */
4273 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch, fetch2... */
4274 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4275 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4276 res
= res
; /* Value added == 0. */
4277 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4278 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4279 res
= res
+ 0xfa; /* Value added == 0xfa. */
4280 else if ( c
&& (7 <= (res
>> 4) && (res
>> 4) <= 15) &&
4281 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4282 res
= res
+ 0xa0; /* Value added == 0xa0. */
4283 else if (c
&& (6 <= (res
>> 4) && (res
>> 4) <= 15) &&
4284 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4285 res
= res
+ 0x9a; /* Value added == 0x9a. */
4291 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
4296 sim_io_printf (sd
, "sim_resume: internal error.\n");
4297 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
4301 if (code
->dst
.type
== X (OP_CCR
, SB
) ||
4302 code
->dst
.type
== X (OP_CCR
, SW
))
4304 h8_set_ccr (sd
, res
);
4307 else if (h8300smode
&&
4308 (code
->dst
.type
== X (OP_EXR
, SB
) ||
4309 code
->dst
.type
== X (OP_EXR
, SW
)))
4311 h8_set_exr (sd
, res
);
4312 if (h8300smode
) /* Get exr. */
4314 trace
= (h8_get_exr (sd
) >> 7) & 1;
4315 intMask
= h8_get_exr (sd
) & 7;
4324 /* When a branch works */
4325 if (fetch (sd
, &code
->src
, &res
))
4327 if (res
& 1) /* bad address */
4329 pc
= code
->next_pc
+ res
;
4332 /* Set the cond codes from res */
4335 /* Set the flags after an 8 bit inc/dec operation */
4339 v
= (rd
& 0x7f) == 0x7f;
4342 /* Set the flags after an 16 bit inc/dec operation */
4346 v
= (rd
& 0x7fff) == 0x7fff;
4349 /* Set the flags after an 32 bit inc/dec operation */
4351 n
= res
& 0x80000000;
4352 nz
= res
& 0xffffffff;
4353 v
= (rd
& 0x7fffffff) == 0x7fffffff;
4357 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
4360 if (store2 (sd
, &code
->dst
, rd
))
4365 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
4368 if (store2 (sd
, &code
->dst
, rd
))
4373 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
4374 n
= (rd
& 0x80000000);
4375 nz
= rd
& 0xffffffff;
4376 if (store2 (sd
, &code
->dst
, rd
))
4381 if (store2 (sd
, &code
->dst
, res
))
4385 /* flags after a 32bit logical operation */
4386 n
= res
& 0x80000000;
4387 nz
= res
& 0xffffffff;
4392 if (store2 (sd
, &code
->dst
, res
))
4396 /* flags after a 16bit logical operation */
4403 if (store2 (sd
, &code
->dst
, res
))
4413 if (store2 (sd
, &code
->dst
, res
))
4420 switch (code
->opcode
/ 4)
4424 v
= ((rd
& 0x80) == (ea
& 0x80)
4425 && (rd
& 0x80) != (res
& 0x80));
4430 v
= ((rd
& 0x80) != (-ea
& 0x80)
4431 && (rd
& 0x80) != (res
& 0x80));
4438 break; /* No effect on v flag. */
4443 if (store2 (sd
, &code
->dst
, res
))
4449 c
= (res
& 0x10000);
4450 switch (code
->opcode
/ 4)
4454 v
= ((rd
& 0x8000) == (ea
& 0x8000)
4455 && (rd
& 0x8000) != (res
& 0x8000));
4460 v
= ((rd
& 0x8000) != (-ea
& 0x8000)
4461 && (rd
& 0x8000) != (res
& 0x8000));
4470 if (store2 (sd
, &code
->dst
, res
))
4474 n
= res
& 0x80000000;
4475 nz
= res
& 0xffffffff;
4476 switch (code
->opcode
/ 4)
4480 v
= ((rd
& 0x80000000) == (ea
& 0x80000000)
4481 && (rd
& 0x80000000) != (res
& 0x80000000));
4482 c
= ((unsigned) res
< (unsigned) rd
) ||
4483 ((unsigned) res
< (unsigned) ea
);
4488 v
= ((rd
& 0x80000000) != (-ea
& 0x80000000)
4489 && (rd
& 0x80000000) != (res
& 0x80000000));
4490 c
= (unsigned) rd
< (unsigned) -ea
;
4493 v
= (rd
== 0x80000000);
4500 if ((res
= h8_get_delayed_branch (sd
)) != 0)
4503 h8_set_delayed_branch (sd
, 0);
4511 h8_set_ticks (sd
, h8_get_ticks (sd
) + get_now () - tick_start
);
4512 h8_set_cycles (sd
, h8_get_cycles (sd
) + cycles
);
4513 h8_set_insts (sd
, h8_get_insts (sd
) + insts
);
4518 h8_set_exr (sd
, (trace
<<7) | intMask
);
4520 h8_set_mask (sd
, oldmask
);
4524 sim_engine_run (SIM_DESC sd
,
4525 int next_cpu_nr
, /* ignore */
4526 int nr_cpus
, /* ignore */
4531 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4533 cpu
= STATE_CPU (sd
, 0);
4537 step_once (sd
, cpu
);
4538 if (sim_events_tick (sd
))
4539 sim_events_process (sd
);
4544 sim_write (SIM_DESC sd
, SIM_ADDR addr
, const unsigned char *buffer
, int size
)
4551 for (i
= 0; i
< size
; i
++)
4553 if (addr
< memory_size
)
4555 h8_set_memory (sd
, addr
+ i
, buffer
[i
]);
4556 h8_set_cache_idx (sd
, addr
+ i
, 0);
4560 h8_set_eightbit (sd
, (addr
+ i
) & 0xff, buffer
[i
]);
4567 sim_read (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
4572 if (addr
< memory_size
)
4573 memcpy (buffer
, h8_get_memory_buf (sd
) + addr
, size
);
4575 memcpy (buffer
, h8_get_eightbit_buf (sd
) + (addr
& 0xff), size
);
4580 h8300_reg_store (SIM_CPU
*cpu
, int rn
, unsigned char *value
, int length
)
4585 longval
= (value
[0] << 24) | (value
[1] << 16) | (value
[2] << 8) | value
[3];
4586 shortval
= (value
[0] << 8) | (value
[1]);
4587 intval
= h8300hmode
? longval
: shortval
;
4589 init_pointers (CPU_STATE (cpu
));
4593 if(h8300_normal_mode
)
4594 cpu
->pc
= shortval
; /* PC for Normal mode is 2 bytes */
4614 cpu
->regs
[rn
] = intval
;
4619 cpu
->regs
[rn
] = longval
;
4626 h8300_reg_fetch (SIM_CPU
*cpu
, int rn
, unsigned char *buf
, int length
)
4631 init_pointers (CPU_STATE (cpu
));
4633 if (!h8300smode
&& rn
>= EXR_REGNUM
)
4668 /* In Normal mode PC is 2 byte, but other registers are 4 byte */
4669 if ((h8300hmode
|| longreg
) && !(rn
== PC_REGNUM
&& h8300_normal_mode
))
4686 set_simcache_size (SIM_DESC sd
, int n
)
4689 free (sd
->sim_cache
);
4692 sd
->sim_cache
= (decoded_inst
*) malloc (sizeof (decoded_inst
) * n
);
4693 memset (sd
->sim_cache
, 0, sizeof (decoded_inst
) * n
);
4694 sd
->sim_cache_size
= n
;
4699 sim_info (SIM_DESC sd
, int verbose
)
4701 double timetaken
= (double) h8_get_ticks (sd
) / (double) now_persec ();
4702 double virttime
= h8_get_cycles (sd
) / 10.0e6
;
4704 sim_io_printf (sd
, "\n\n#instructions executed %10d\n", h8_get_insts (sd
));
4705 sim_io_printf (sd
, "#cycles (v approximate) %10d\n", h8_get_cycles (sd
));
4706 sim_io_printf (sd
, "#real time taken %10.4f\n", timetaken
);
4707 sim_io_printf (sd
, "#virtual time taken %10.4f\n", virttime
);
4708 if (timetaken
!= 0.0)
4709 sim_io_printf (sd
, "#simulation ratio %10.4f\n", virttime
/ timetaken
);
4710 sim_io_printf (sd
, "#compiles %10d\n", h8_get_compiles (sd
));
4711 sim_io_printf (sd
, "#cache size %10d\n", sd
->sim_cache_size
);
4714 /* This to be conditional on `what' (aka `verbose'),
4715 however it was never passed as non-zero. */
4719 for (i
= 0; i
< O_LAST
; i
++)
4721 if (h8_get_stats (sd
, i
))
4722 sim_io_printf (sd
, "%d: %d\n", i
, h8_get_stats (sd
, i
));
4728 /* Indicate whether the cpu is an H8/300 or H8/300H.
4729 FLAG is non-zero for the H8/300H. */
4732 set_h8300h (unsigned long machine
)
4734 /* FIXME: Much of the code in sim_load can be moved to sim_open.
4735 This function being replaced by a sim_open:ARGV configuration
4738 h8300hmode
= h8300smode
= h8300sxmode
= h8300_normal_mode
= 0;
4740 if (machine
== bfd_mach_h8300sx
|| machine
== bfd_mach_h8300sxn
)
4743 if (machine
== bfd_mach_h8300s
|| machine
== bfd_mach_h8300sn
|| h8300sxmode
)
4746 if (machine
== bfd_mach_h8300h
|| machine
== bfd_mach_h8300hn
|| h8300smode
)
4749 if(machine
== bfd_mach_h8300hn
|| machine
== bfd_mach_h8300sn
|| machine
== bfd_mach_h8300sxn
)
4750 h8300_normal_mode
= 1;
4753 /* H8300-specific options.
4754 TODO: These really should be merged into the common model modules. */
4762 h8300_option_handler (SIM_DESC sd
, sim_cpu
*cpu ATTRIBUTE_UNUSED
, int opt
,
4763 char *arg
, int is_command ATTRIBUTE_UNUSED
)
4765 switch ((H8300_OPTIONS
) opt
)
4768 set_h8300h (bfd_mach_h8300h
);
4771 set_h8300h (bfd_mach_h8300s
);
4773 case OPTION_H8300SX
:
4774 set_h8300h (bfd_mach_h8300sx
);
4778 /* We'll actually never get here; the caller handles the error
4780 sim_io_eprintf (sd
, "Unknown option `%s'\n", arg
);
4787 static const OPTION h8300_options
[] =
4789 { {"h8300h", no_argument
, NULL
, OPTION_H8300H
},
4790 'h', NULL
, "Indicate the CPU is H8/300H",
4791 h8300_option_handler
},
4792 { {"h8300s", no_argument
, NULL
, OPTION_H8300S
},
4793 'S', NULL
, "Indicate the CPU is H8S",
4794 h8300_option_handler
},
4795 { {"h8300sx", no_argument
, NULL
, OPTION_H8300SX
},
4796 'x', NULL
, "Indicate the CPU is H8SX",
4797 h8300_option_handler
},
4798 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
4802 h8300_pc_get (sim_cpu
*cpu
)
4808 h8300_pc_set (sim_cpu
*cpu
, sim_cia pc
)
4813 /* Cover function of sim_state_free to free the cpu buffers as well. */
4816 free_state (SIM_DESC sd
)
4818 if (STATE_MODULES (sd
) != NULL
)
4819 sim_module_uninstall (sd
);
4821 /* Fixme: free buffers in _sim_cpu. */
4822 sim_state_free (sd
);
4826 sim_open (SIM_OPEN_KIND kind
,
4827 struct host_callback_struct
*callback
,
4835 sd
= sim_state_alloc (kind
, callback
);
4837 /* The cpu data is kept in a separately allocated chunk of memory. */
4838 if (sim_cpu_alloc_all (sd
, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK
)
4844 cpu
= STATE_CPU (sd
, 0);
4845 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4846 cpu
->regs
[SBR_REGNUM
] = 0xFFFFFF00;
4847 /* sim_cpu object is new, so some initialization is needed. */
4848 init_pointers_needed
= 1;
4850 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
4856 if (sim_add_option_table (sd
, NULL
, h8300_options
) != SIM_RC_OK
)
4862 /* The parser will print an error message for us, so we silently return. */
4863 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
4865 /* Uninstall the modules to avoid memory leaks,
4866 file descriptor leaks, etc. */
4871 /* Check for/establish the a reference program image. */
4872 if (sim_analyze_program (sd
,
4873 (STATE_PROG_ARGV (sd
) != NULL
4874 ? *STATE_PROG_ARGV (sd
)
4875 : NULL
), abfd
) != SIM_RC_OK
)
4881 /* Establish any remaining configuration options. */
4882 if (sim_config (sd
) != SIM_RC_OK
)
4888 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
4890 /* Uninstall the modules to avoid memory leaks,
4891 file descriptor leaks, etc. */
4896 /* CPU specific initialization. */
4897 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
4899 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
4901 CPU_REG_FETCH (cpu
) = h8300_reg_fetch
;
4902 CPU_REG_STORE (cpu
) = h8300_reg_store
;
4903 CPU_PC_FETCH (cpu
) = h8300_pc_get
;
4904 CPU_PC_STORE (cpu
) = h8300_pc_set
;
4907 /* sim_hw_configure (sd); */
4909 /* FIXME: Much of the code in sim_load can be moved here. */
4914 /* Called by gdb to load a program into memory. */
4917 sim_load (SIM_DESC sd
, const char *prog
, bfd
*abfd
, int from_tty
)
4921 /* FIXME: The code below that sets a specific variant of the H8/300
4922 being simulated should be moved to sim_open(). */
4924 /* See if the file is for the H8/300 or H8/300H. */
4925 /* ??? This may not be the most efficient way. The z8k simulator
4926 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
4930 prog_bfd
= bfd_openr (prog
, NULL
);
4931 if (prog_bfd
!= NULL
)
4933 /* Set the cpu type. We ignore failure from bfd_check_format
4934 and bfd_openr as sim_load_file checks too. */
4935 if (bfd_check_format (prog_bfd
, bfd_object
))
4937 set_h8300h (bfd_get_mach (prog_bfd
));
4941 /* If we're using gdb attached to the simulator, then we have to
4942 reallocate memory for the simulator.
4944 When gdb first starts, it calls fetch_registers (among other
4945 functions), which in turn calls init_pointers, which allocates
4948 The problem is when we do that, we don't know whether we're
4949 debugging an H8/300 or H8/300H program.
4951 This is the first point at which we can make that determination,
4952 so we just reallocate memory now; this will also allow us to handle
4953 switching between H8/300 and H8/300H programs without exiting
4956 if (h8300smode
&& !h8300_normal_mode
)
4957 memory_size
= H8300S_MSIZE
;
4958 else if (h8300hmode
&& !h8300_normal_mode
)
4959 memory_size
= H8300H_MSIZE
;
4961 memory_size
= H8300_MSIZE
;
4963 if (h8_get_memory_buf (sd
))
4964 free (h8_get_memory_buf (sd
));
4965 if (h8_get_cache_idx_buf (sd
))
4966 free (h8_get_cache_idx_buf (sd
));
4967 if (h8_get_eightbit_buf (sd
))
4968 free (h8_get_eightbit_buf (sd
));
4970 h8_set_memory_buf (sd
, (unsigned char *)
4971 calloc (sizeof (char), memory_size
));
4972 h8_set_cache_idx_buf (sd
, (unsigned short *)
4973 calloc (sizeof (short), memory_size
));
4974 sd
->memory_size
= memory_size
;
4975 h8_set_eightbit_buf (sd
, (unsigned char *) calloc (sizeof (char), 256));
4977 /* `msize' must be a power of two. */
4978 if ((memory_size
& (memory_size
- 1)) != 0)
4980 sim_io_printf (sd
, "sim_load: bad memory size.\n");
4983 h8_set_mask (sd
, memory_size
- 1);
4985 if (sim_load_file (sd
, STATE_MY_NAME (sd
), STATE_CALLBACK (sd
), prog
,
4986 prog_bfd
, STATE_OPEN_KIND (sd
) == SIM_OPEN_DEBUG
,
4990 /* Close the bfd if we opened it. */
4991 if (abfd
== NULL
&& prog_bfd
!= NULL
)
4992 bfd_close (prog_bfd
);
4996 /* Close the bfd if we opened it. */
4997 if (abfd
== NULL
&& prog_bfd
!= NULL
)
4998 bfd_close (prog_bfd
);
5003 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
5004 char * const *argv
, char * const *env
)
5011 h8_set_pc (sd
, bfd_get_start_address (abfd
));
5015 /* Command Line support. */
5018 /* Counting the no. of commandline arguments. */
5019 for (no_of_args
= 0; argv
[no_of_args
] != NULL
; no_of_args
++)
5022 /* Allocating memory for the argv pointers. */
5023 h8_set_command_line (sd
, (char **) malloc ((sizeof (char *))
5024 * (no_of_args
+ 1)));
5026 for (i
= 0; i
< no_of_args
; i
++)
5028 /* Copying the argument string. */
5029 h8_set_cmdline_arg (sd
, i
, (char *) strdup (argv
[i
]));
5031 h8_set_cmdline_arg (sd
, i
, NULL
);