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.
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
33 #include "gdb/sim-h8300.h"
35 #include "sys/types.h"
43 host_callback
*sim_callback
;
45 static SIM_OPEN_KIND sim_kind
;
48 /* FIXME: Needs to live in header file.
49 This header should also include the things in remote-sim.h.
50 One could move this to remote-sim.h but this function isn't needed
52 static void set_simcache_size (SIM_DESC
, int);
54 #define X(op, size) (op * 4 + size)
56 #define SP (h8300hmode && !h8300_normal_mode ? SL : SW)
58 #define h8_opcodes ops
60 #include "opcode/h8300.h"
62 /* CPU data object: */
65 sim_state_initialize (SIM_DESC sd
, sim_cpu
*cpu
)
67 /* FIXME: not really necessary, since sim_cpu_alloc calls zalloc. */
69 memset (&cpu
->regs
, 0, sizeof(cpu
->regs
));
70 cpu
->regs
[SBR_REGNUM
] = 0xFFFFFF00;
72 cpu
->delayed_branch
= 0;
77 /* Initialize local simulator state. */
79 sd
->sim_cache_size
= 0;
85 memset (&cpu
->stats
, 0, sizeof (cpu
->stats
));
91 h8_get_pc (SIM_DESC sd
)
93 return (STATE_CPU (sd
, 0)) -> pc
;
97 h8_set_pc (SIM_DESC sd
, unsigned int val
)
99 (STATE_CPU (sd
, 0)) -> pc
= val
;
103 h8_get_ccr (SIM_DESC sd
)
105 return (STATE_CPU (sd
, 0)) -> regs
[CCR_REGNUM
];
109 h8_set_ccr (SIM_DESC sd
, unsigned int val
)
111 (STATE_CPU (sd
, 0)) -> regs
[CCR_REGNUM
] = val
;
115 h8_get_exr (SIM_DESC sd
)
117 return (STATE_CPU (sd
, 0)) -> regs
[EXR_REGNUM
];
121 h8_set_exr (SIM_DESC sd
, unsigned int val
)
123 (STATE_CPU (sd
, 0)) -> regs
[EXR_REGNUM
] = val
;
127 h8_get_sbr (SIM_DESC sd
)
129 return (STATE_CPU (sd
, 0)) -> regs
[SBR_REGNUM
];
133 h8_set_sbr (SIM_DESC sd
, int val
)
135 (STATE_CPU (sd
, 0)) -> regs
[SBR_REGNUM
] = val
;
139 h8_get_vbr (SIM_DESC sd
)
141 return (STATE_CPU (sd
, 0)) -> regs
[VBR_REGNUM
];
145 h8_set_vbr (SIM_DESC sd
, int val
)
147 (STATE_CPU (sd
, 0)) -> regs
[VBR_REGNUM
] = val
;
151 h8_get_cache_top (SIM_DESC sd
)
153 return sd
-> cache_top
;
157 h8_set_cache_top (SIM_DESC sd
, int val
)
159 sd
-> cache_top
= val
;
163 h8_get_mask (SIM_DESC sd
)
165 return (STATE_CPU (sd
, 0)) -> mask
;
169 h8_set_mask (SIM_DESC sd
, int val
)
171 (STATE_CPU (sd
, 0)) -> mask
= val
;
175 h8_get_exception (SIM_DESC sd
)
177 return (STATE_CPU (sd
, 0)) -> exception
;
181 h8_set_exception (SIM_DESC sd
, int val
)
183 (STATE_CPU (sd
, 0)) -> exception
= val
;
186 static enum h8300_sim_state
187 h8_get_state (SIM_DESC sd
)
193 h8_set_state (SIM_DESC sd
, enum h8300_sim_state val
)
199 h8_get_cycles (SIM_DESC sd
)
201 return (STATE_CPU (sd
, 0)) -> regs
[CYCLE_REGNUM
];
205 h8_set_cycles (SIM_DESC sd
, unsigned int val
)
207 (STATE_CPU (sd
, 0)) -> regs
[CYCLE_REGNUM
] = val
;
211 h8_get_insts (SIM_DESC sd
)
213 return (STATE_CPU (sd
, 0)) -> regs
[INST_REGNUM
];
217 h8_set_insts (SIM_DESC sd
, unsigned int val
)
219 (STATE_CPU (sd
, 0)) -> regs
[INST_REGNUM
] = val
;
223 h8_get_ticks (SIM_DESC sd
)
225 return (STATE_CPU (sd
, 0)) -> regs
[TICK_REGNUM
];
229 h8_set_ticks (SIM_DESC sd
, unsigned int val
)
231 (STATE_CPU (sd
, 0)) -> regs
[TICK_REGNUM
] = val
;
235 h8_get_mach (SIM_DESC sd
)
237 return (STATE_CPU (sd
, 0)) -> regs
[MACH_REGNUM
];
241 h8_set_mach (SIM_DESC sd
, unsigned int val
)
243 (STATE_CPU (sd
, 0)) -> regs
[MACH_REGNUM
] = val
;
247 h8_get_macl (SIM_DESC sd
)
249 return (STATE_CPU (sd
, 0)) -> regs
[MACL_REGNUM
];
253 h8_set_macl (SIM_DESC sd
, unsigned int val
)
255 (STATE_CPU (sd
, 0)) -> regs
[MACL_REGNUM
] = val
;
259 h8_get_compiles (SIM_DESC sd
)
261 return sd
-> compiles
;
265 h8_increment_compiles (SIM_DESC sd
)
270 static unsigned int *
271 h8_get_reg_buf (SIM_DESC sd
)
273 return &(((STATE_CPU (sd
, 0)) -> regs
)[0]);
277 h8_get_reg (SIM_DESC sd
, int regnum
)
279 return (STATE_CPU (sd
, 0)) -> regs
[regnum
];
283 h8_set_reg (SIM_DESC sd
, int regnum
, int val
)
285 (STATE_CPU (sd
, 0)) -> regs
[regnum
] = val
;
290 h8_get_stats (SIM_DESC sd
, int idx
)
292 return sd
-> stats
[idx
];
296 h8_increment_stats (SIM_DESC sd
, int idx
)
302 static unsigned short *
303 h8_get_cache_idx_buf (SIM_DESC sd
)
305 return sd
-> cache_idx
;
309 h8_set_cache_idx_buf (SIM_DESC sd
, unsigned short *ptr
)
311 sd
-> cache_idx
= ptr
;
314 static unsigned short
315 h8_get_cache_idx (SIM_DESC sd
, unsigned int idx
)
317 if (idx
> sd
->memory_size
)
318 return (unsigned short) -1;
319 return sd
-> cache_idx
[idx
];
323 h8_set_cache_idx (SIM_DESC sd
, int idx
, unsigned int val
)
325 sd
-> cache_idx
[idx
] = (unsigned short) val
;
328 static unsigned char *
329 h8_get_memory_buf (SIM_DESC sd
)
331 return (STATE_CPU (sd
, 0)) -> memory
;
335 h8_set_memory_buf (SIM_DESC sd
, unsigned char *ptr
)
337 (STATE_CPU (sd
, 0)) -> memory
= ptr
;
341 h8_get_memory (SIM_DESC sd
, int idx
)
343 return (STATE_CPU (sd
, 0)) -> memory
[idx
];
347 h8_set_memory (SIM_DESC sd
, int idx
, unsigned int val
)
349 (STATE_CPU (sd
, 0)) -> memory
[idx
] = (unsigned char) val
;
352 static unsigned char *
353 h8_get_eightbit_buf (SIM_DESC sd
)
355 return (STATE_CPU (sd
, 0)) -> eightbit
;
359 h8_set_eightbit_buf (SIM_DESC sd
, unsigned char *ptr
)
361 (STATE_CPU (sd
, 0)) -> eightbit
= ptr
;
365 h8_get_eightbit (SIM_DESC sd
, int idx
)
367 return (STATE_CPU (sd
, 0)) -> eightbit
[idx
];
371 h8_set_eightbit (SIM_DESC sd
, int idx
, unsigned int val
)
373 (STATE_CPU (sd
, 0)) -> eightbit
[idx
] = (unsigned char) val
;
377 h8_get_delayed_branch (SIM_DESC sd
)
379 return (STATE_CPU (sd
, 0)) -> delayed_branch
;
383 h8_set_delayed_branch (SIM_DESC sd
, unsigned int dest
)
385 (STATE_CPU (sd
, 0)) -> delayed_branch
= dest
;
389 h8_get_command_line (SIM_DESC sd
)
391 return (STATE_CPU (sd
, 0)) -> command_line
;
395 h8_set_command_line (SIM_DESC sd
, char ** val
)
397 (STATE_CPU (sd
, 0)) -> command_line
= val
;
401 h8_get_cmdline_arg (SIM_DESC sd
, int index
)
403 return (STATE_CPU (sd
, 0)) -> command_line
[index
];
407 h8_set_cmdline_arg (SIM_DESC sd
, int index
, char * val
)
409 (STATE_CPU (sd
, 0)) -> command_line
[index
] = val
;
412 /* MAC Saturation Mode */
414 h8_get_macS (SIM_DESC sd
)
416 return (STATE_CPU (sd
, 0)) -> macS
;
420 h8_set_macS (SIM_DESC sd
, int val
)
422 (STATE_CPU (sd
, 0)) -> macS
= (val
!= 0);
427 h8_get_macZ (SIM_DESC sd
)
429 return (STATE_CPU (sd
, 0)) -> macZ
;
433 h8_set_macZ (SIM_DESC sd
, int val
)
435 (STATE_CPU (sd
, 0)) -> macZ
= (val
!= 0);
438 /* MAC Negative Flag */
440 h8_get_macN (SIM_DESC sd
)
442 return (STATE_CPU (sd
, 0)) -> macN
;
446 h8_set_macN (SIM_DESC sd
, int val
)
448 (STATE_CPU (sd
, 0)) -> macN
= (val
!= 0);
451 /* MAC Overflow Flag */
453 h8_get_macV (SIM_DESC sd
)
455 return (STATE_CPU (sd
, 0)) -> macV
;
459 h8_set_macV (SIM_DESC sd
, int val
)
461 (STATE_CPU (sd
, 0)) -> macV
= (val
!= 0);
464 /* End CPU data object. */
466 /* The rate at which to call the host's poll_quit callback. */
468 enum { POLL_QUIT_INTERVAL
= 0x80000 };
470 #define LOW_BYTE(x) ((x) & 0xff)
471 #define HIGH_BYTE(x) (((x) >> 8) & 0xff)
472 #define P(X, Y) ((X << 8) | Y)
481 #define I (intMaskBit != 0)
483 #define BUILDSR(SD) \
484 h8_set_ccr (SD, (I << 7) | (UI << 6) | (H << 5) | (U << 4) \
485 | (N << 3) | (Z << 2) | (V << 1) | C)
488 /* Get Status Register (flags). */ \
489 c = (h8_get_ccr (sd) >> 0) & 1; \
490 v = (h8_get_ccr (sd) >> 1) & 1; \
491 nz = !((h8_get_ccr (sd) >> 2) & 1); \
492 n = (h8_get_ccr (sd) >> 3) & 1; \
493 u = (h8_get_ccr (sd) >> 4) & 1; \
494 h = (h8_get_ccr (sd) >> 5) & 1; \
495 ui = ((h8_get_ccr (sd) >> 6) & 1); \
496 intMaskBit = (h8_get_ccr (sd) >> 7) & 1
499 #ifdef __CHAR_IS_SIGNED__
500 #define SEXTCHAR(x) ((char) (x))
504 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff) : x & 0xff)
507 #define UEXTCHAR(x) ((x) & 0xff)
508 #define UEXTSHORT(x) ((x) & 0xffff)
509 #define SEXTSHORT(x) ((short) (x))
513 int h8300_normal_mode
= 0;
516 static int memory_size
;
521 return time (0); /* WinXX HAS UNIX like 'time', so why not use it? */
543 return (h8300hmode
&& !h8300_normal_mode
)? SL
: SW
;
548 /* Simulate an indirection / dereference.
549 return 0 for success, -1 for failure.
553 lvalue (SIM_DESC sd
, int x
, int rn
, unsigned int *val
)
555 if (val
== NULL
) /* Paranoia. */
561 if (rn
== ZERO_REGNUM
)
562 *val
= X (OP_IMM
, SP
);
564 *val
= X (OP_REG
, SP
);
567 *val
= X (OP_MEM
, SP
);
570 sim_engine_set_run_state (sd
, sim_stopped
, SIGSEGV
);
579 if (h8300smode
&& !h8300_normal_mode
)
581 else if (h8300hmode
&& !h8300_normal_mode
)
588 decode (SIM_DESC sd
, int addr
, unsigned char *data
, decoded_inst
*dst
)
590 int cst
[3] = {0, 0, 0};
591 int reg
[3] = {0, 0, 0};
592 int rdisp
[3] = {0, 0, 0};
594 const struct h8_opcode
*q
;
599 /* Find the exact opcode/arg combo. */
600 for (q
= h8_opcodes
; q
->name
; q
++)
602 op_type
*nib
= q
->data
.nib
;
603 unsigned int len
= 0;
605 if ((q
->available
== AV_H8SX
&& !h8300sxmode
) ||
606 (q
->available
== AV_H8S
&& !h8300smode
) ||
607 (q
->available
== AV_H8H
&& !h8300hmode
))
610 cst
[0] = cst
[1] = cst
[2] = 0;
611 reg
[0] = reg
[1] = reg
[2] = 0;
612 rdisp
[0] = rdisp
[1] = rdisp
[2] = 0;
616 op_type looking_for
= *nib
;
617 int thisnib
= data
[len
/ 2];
619 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
620 opnum
= ((looking_for
& OP3
) ? 2 :
621 (looking_for
& DST
) ? 1 : 0);
623 if (looking_for
< 16 && looking_for
>= 0)
625 if (looking_for
!= thisnib
)
630 if (looking_for
& B31
)
632 if (!((thisnib
& 0x8) != 0))
635 looking_for
= (op_type
) (looking_for
& ~B31
);
638 else if (looking_for
& B30
)
640 if (!((thisnib
& 0x8) == 0))
643 looking_for
= (op_type
) (looking_for
& ~B30
);
646 if (looking_for
& B21
)
648 if (!((thisnib
& 0x4) != 0))
651 looking_for
= (op_type
) (looking_for
& ~B21
);
654 else if (looking_for
& B20
)
656 if (!((thisnib
& 0x4) == 0))
659 looking_for
= (op_type
) (looking_for
& ~B20
);
662 if (looking_for
& B11
)
664 if (!((thisnib
& 0x2) != 0))
667 looking_for
= (op_type
) (looking_for
& ~B11
);
670 else if (looking_for
& B10
)
672 if (!((thisnib
& 0x2) == 0))
675 looking_for
= (op_type
) (looking_for
& ~B10
);
678 if (looking_for
& B01
)
680 if (!((thisnib
& 0x1) != 0))
683 looking_for
= (op_type
) (looking_for
& ~B01
);
686 else if (looking_for
& B00
)
688 if (!((thisnib
& 0x1) == 0))
691 looking_for
= (op_type
) (looking_for
& ~B00
);
694 if (looking_for
& IGNORE
)
696 /* Hitachi has declared that IGNORE must be zero. */
700 else if ((looking_for
& MODE
) == DATA
)
702 ; /* Skip embedded data. */
704 else if ((looking_for
& MODE
) == DBIT
)
706 /* Exclude adds/subs by looking at bit 0 and 2, and
707 make sure the operand size, either w or l,
708 matches by looking at bit 1. */
709 if ((looking_for
& 7) != (thisnib
& 7))
712 cst
[opnum
] = (thisnib
& 0x8) ? 2 : 1;
714 else if ((looking_for
& MODE
) == REG
||
715 (looking_for
& MODE
) == LOWREG
||
716 (looking_for
& MODE
) == IND
||
717 (looking_for
& MODE
) == PREINC
||
718 (looking_for
& MODE
) == POSTINC
||
719 (looking_for
& MODE
) == PREDEC
||
720 (looking_for
& MODE
) == POSTDEC
)
722 reg
[opnum
] = thisnib
;
724 else if (looking_for
& CTRL
)
727 if (((looking_for
& MODE
) == CCR
&& (thisnib
!= C_CCR
)) ||
728 ((looking_for
& MODE
) == EXR
&& (thisnib
!= C_EXR
)) ||
729 ((looking_for
& MODE
) == MACH
&& (thisnib
!= C_MACH
)) ||
730 ((looking_for
& MODE
) == MACL
&& (thisnib
!= C_MACL
)) ||
731 ((looking_for
& MODE
) == VBR
&& (thisnib
!= C_VBR
)) ||
732 ((looking_for
& MODE
) == SBR
&& (thisnib
!= C_SBR
)))
734 if (((looking_for
& MODE
) == CCR_EXR
&&
735 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
)) ||
736 ((looking_for
& MODE
) == VBR_SBR
&&
737 (thisnib
!= C_VBR
&& thisnib
!= C_SBR
)) ||
738 ((looking_for
& MODE
) == MACREG
&&
739 (thisnib
!= C_MACH
&& thisnib
!= C_MACL
)))
741 if (((looking_for
& MODE
) == CC_EX_VB_SB
&&
742 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
&&
743 thisnib
!= C_VBR
&& thisnib
!= C_SBR
)))
746 reg
[opnum
] = thisnib
;
748 else if ((looking_for
& MODE
) == ABS
)
750 /* Absolute addresses are unsigned. */
751 switch (looking_for
& SIZE
)
754 cst
[opnum
] = UEXTCHAR (data
[len
/ 2]);
758 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
762 (data
[len
/ 2 + 0] << 24) +
763 (data
[len
/ 2 + 1] << 16) +
764 (data
[len
/ 2 + 2] << 8) +
768 printf ("decode: bad size ABS: %d\n",
769 (looking_for
& SIZE
));
773 else if ((looking_for
& MODE
) == DISP
||
774 (looking_for
& MODE
) == PCREL
||
775 (looking_for
& MODE
) == INDEXB
||
776 (looking_for
& MODE
) == INDEXW
||
777 (looking_for
& MODE
) == INDEXL
)
779 switch (looking_for
& SIZE
)
782 cst
[opnum
] = thisnib
& 3;
785 cst
[opnum
] = SEXTCHAR (data
[len
/ 2]);
788 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
789 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
792 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
796 (data
[len
/ 2 + 0] << 24) +
797 (data
[len
/ 2 + 1] << 16) +
798 (data
[len
/ 2 + 2] << 8) +
802 printf ("decode: bad size DISP/PCREL/INDEX: %d\n",
803 (looking_for
& SIZE
));
807 else if ((looking_for
& SIZE
) == L_16
||
808 (looking_for
& SIZE
) == L_16U
)
810 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
811 /* Immediates are always unsigned. */
812 if ((looking_for
& SIZE
) != L_16U
&&
813 (looking_for
& MODE
) != IMM
)
814 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
816 else if (looking_for
& ABSJMP
)
818 switch (looking_for
& SIZE
) {
820 cst
[opnum
] = (data
[1] << 16) | (data
[2] << 8) | (data
[3]);
824 (data
[len
/ 2 + 0] << 24) +
825 (data
[len
/ 2 + 1] << 16) +
826 (data
[len
/ 2 + 2] << 8) +
830 printf ("decode: bad size ABSJMP: %d\n",
831 (looking_for
& SIZE
));
835 else if ((looking_for
& MODE
) == MEMIND
)
837 cst
[opnum
] = data
[1];
839 else if ((looking_for
& MODE
) == VECIND
)
841 if(h8300_normal_mode
)
842 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 2;
844 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 4;
845 cst
[opnum
] += h8_get_vbr (sd
); /* Add vector base reg. */
847 else if ((looking_for
& SIZE
) == L_32
)
852 (data
[i
+ 0] << 24) |
853 (data
[i
+ 1] << 16) |
857 else if ((looking_for
& SIZE
) == L_24
)
862 (data
[i
+ 0] << 16) |
866 else if (looking_for
& DISPREG
)
868 rdisp
[opnum
] = thisnib
& 0x7;
870 else if ((looking_for
& MODE
) == KBIT
)
887 else if ((looking_for
& SIZE
) == L_8
)
889 if ((looking_for
& MODE
) == ABS
)
891 /* Will be combined with contents of SBR_REGNUM
892 by fetch (). For all modes except h8sx, this
893 will always contain the value 0xFFFFFF00. */
894 cst
[opnum
] = data
[len
/ 2] & 0xff;
898 cst
[opnum
] = data
[len
/ 2] & 0xff;
901 else if ((looking_for
& SIZE
) == L_2
)
903 cst
[opnum
] = thisnib
& 3;
905 else if ((looking_for
& SIZE
) == L_3
||
906 (looking_for
& SIZE
) == L_3NZ
)
908 cst
[opnum
] = thisnib
& 7;
909 if (cst
[opnum
] == 0 && (looking_for
& SIZE
) == L_3NZ
)
912 else if ((looking_for
& SIZE
) == L_4
)
914 cst
[opnum
] = thisnib
& 15;
916 else if ((looking_for
& SIZE
) == L_5
)
918 cst
[opnum
] = data
[len
/ 2] & 0x1f;
920 else if (looking_for
== E
)
925 /* Fill in the args. */
927 op_type
*args
= q
->args
.nib
;
932 nargs
< 3 && *args
!= E
;
938 opnum
= ((x
& OP3
) ? 2 :
947 if ((x
& MODE
) == IMM
||
948 (x
& MODE
) == KBIT
||
951 /* Use the instruction to determine
953 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
954 p
->literal
= cst
[opnum
];
956 else if ((x
& MODE
) == CONST_2
||
957 (x
& MODE
) == CONST_4
||
958 (x
& MODE
) == CONST_8
||
959 (x
& MODE
) == CONST_16
)
961 /* Use the instruction to determine
963 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
965 case CONST_2
: p
->literal
= 2; break;
966 case CONST_4
: p
->literal
= 4; break;
967 case CONST_8
: p
->literal
= 8; break;
968 case CONST_16
: p
->literal
= 16; break;
971 else if ((x
& MODE
) == REG
)
973 p
->type
= X (OP_REG
, bitfrom (x
));
976 else if ((x
& MODE
) == LOWREG
)
978 p
->type
= X (OP_LOWREG
, bitfrom (x
));
981 else if ((x
& MODE
) == PREINC
)
983 /* Use the instruction to determine
985 p
->type
= X (OP_PREINC
, OP_SIZE (q
->how
));
986 p
->reg
= reg
[opnum
] & 0x7;
988 else if ((x
& MODE
) == POSTINC
)
990 /* Use the instruction to determine
992 p
->type
= X (OP_POSTINC
, OP_SIZE (q
->how
));
993 p
->reg
= reg
[opnum
] & 0x7;
995 else if ((x
& MODE
) == PREDEC
)
997 /* Use the instruction to determine
999 p
->type
= X (OP_PREDEC
, OP_SIZE (q
->how
));
1000 p
->reg
= reg
[opnum
] & 0x7;
1002 else if ((x
& MODE
) == POSTDEC
)
1004 /* Use the instruction to determine
1005 the operand size. */
1006 p
->type
= X (OP_POSTDEC
, OP_SIZE (q
->how
));
1007 p
->reg
= reg
[opnum
] & 0x7;
1009 else if ((x
& MODE
) == IND
)
1011 /* Note: an indirect is transformed into
1012 a displacement of zero.
1014 /* Use the instruction to determine
1015 the operand size. */
1016 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1017 p
->reg
= reg
[opnum
] & 0x7;
1019 if (OP_KIND (q
->how
) == O_JSR
||
1020 OP_KIND (q
->how
) == O_JMP
)
1021 if (lvalue (sd
, p
->type
, p
->reg
, &p
->type
))
1024 else if ((x
& MODE
) == ABS
)
1026 /* Note: a 16 or 32 bit ABS is transformed into a
1027 displacement from pseudo-register ZERO_REGNUM,
1028 which is always zero. An 8 bit ABS becomes
1029 a displacement from SBR_REGNUM.
1031 /* Use the instruction to determine
1032 the operand size. */
1033 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1034 p
->literal
= cst
[opnum
];
1036 /* 8-bit ABS is displacement from SBR.
1037 16 and 32-bit ABS are displacement from ZERO.
1038 (SBR will always be zero except for h8/sx)
1040 if ((x
& SIZE
) == L_8
)
1041 p
->reg
= SBR_REGNUM
;
1043 p
->reg
= ZERO_REGNUM
;;
1045 else if ((x
& MODE
) == MEMIND
||
1046 (x
& MODE
) == VECIND
)
1048 /* Size doesn't matter. */
1049 p
->type
= X (OP_MEM
, SB
);
1050 p
->literal
= cst
[opnum
];
1051 if (OP_KIND (q
->how
) == O_JSR
||
1052 OP_KIND (q
->how
) == O_JMP
)
1053 if (lvalue (sd
, p
->type
, p
->reg
, &p
->type
))
1056 else if ((x
& MODE
) == PCREL
)
1058 /* Size doesn't matter. */
1059 p
->type
= X (OP_PCREL
, SB
);
1060 p
->literal
= cst
[opnum
];
1062 else if (x
& ABSJMP
)
1064 p
->type
= X (OP_IMM
, SP
);
1065 p
->literal
= cst
[opnum
];
1067 else if ((x
& MODE
) == INDEXB
)
1069 p
->type
= X (OP_INDEXB
, OP_SIZE (q
->how
));
1070 p
->literal
= cst
[opnum
];
1071 p
->reg
= rdisp
[opnum
];
1073 else if ((x
& MODE
) == INDEXW
)
1075 p
->type
= X (OP_INDEXW
, OP_SIZE (q
->how
));
1076 p
->literal
= cst
[opnum
];
1077 p
->reg
= rdisp
[opnum
];
1079 else if ((x
& MODE
) == INDEXL
)
1081 p
->type
= X (OP_INDEXL
, OP_SIZE (q
->how
));
1082 p
->literal
= cst
[opnum
];
1083 p
->reg
= rdisp
[opnum
];
1085 else if ((x
& MODE
) == DISP
)
1087 /* Yuck -- special for mova args. */
1088 if (strncmp (q
->name
, "mova", 4) == 0 &&
1091 /* Mova can have a DISP2 dest, with an
1092 INDEXB or INDEXW src. The multiplier
1093 for the displacement value is determined
1094 by the src operand, not by the insn. */
1096 switch (OP_KIND (dst
->src
.type
))
1099 p
->type
= X (OP_DISP
, SB
);
1100 p
->literal
= cst
[opnum
];
1103 p
->type
= X (OP_DISP
, SW
);
1104 p
->literal
= cst
[opnum
] * 2;
1112 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1113 p
->literal
= cst
[opnum
];
1114 /* DISP2 is special. */
1115 if ((x
& SIZE
) == L_2
)
1116 switch (OP_SIZE (q
->how
))
1119 case SW
: p
->literal
*= 2; break;
1120 case SL
: p
->literal
*= 4; break;
1123 p
->reg
= rdisp
[opnum
];
1130 p
->type
= X (OP_CCR
, SB
);
1133 p
->type
= X (OP_EXR
, SB
);
1136 p
->type
= X (OP_MACH
, SL
);
1139 p
->type
= X (OP_MACL
, SL
);
1142 p
->type
= X (OP_VBR
, SL
);
1145 p
->type
= X (OP_SBR
, SL
);
1149 else if ((x
& MODE
) == CCR
)
1153 else if ((x
& MODE
) == EXR
)
1158 printf ("Hmmmm 0x%x...\n", x
);
1164 /* Unary operators: treat src and dst as equivalent. */
1165 if (dst
->dst
.type
== -1)
1166 dst
->dst
= dst
->src
;
1167 if (dst
->src
.type
== -1)
1168 dst
->src
= dst
->dst
;
1170 dst
->opcode
= q
->how
;
1171 dst
->cycles
= q
->time
;
1173 /* And jsr's to these locations are turned into
1176 if (OP_KIND (dst
->opcode
) == O_JSR
)
1178 switch (dst
->src
.literal
)
1181 dst
->opcode
= O (O_SYS_OPEN
, SB
);
1184 dst
->opcode
= O (O_SYS_READ
, SB
);
1187 dst
->opcode
= O (O_SYS_WRITE
, SB
);
1190 dst
->opcode
= O (O_SYS_LSEEK
, SB
);
1193 dst
->opcode
= O (O_SYS_CLOSE
, SB
);
1196 dst
->opcode
= O (O_SYS_STAT
, SB
);
1199 dst
->opcode
= O (O_SYS_FSTAT
, SB
);
1202 dst
->opcode
= O (O_SYS_CMDLINE
, SB
);
1205 /* End of Processing for system calls. */
1208 dst
->next_pc
= addr
+ len
/ 2;
1212 printf ("Don't understand 0x%x \n", looking_for
);
1223 /* Fell off the end. */
1224 dst
->opcode
= O (O_ILL
, SB
);
1228 compile (SIM_DESC sd
, int pc
)
1232 /* Find the next cache entry to use. */
1233 idx
= h8_get_cache_top (sd
) + 1;
1234 h8_increment_compiles (sd
);
1235 if (idx
>= sd
->sim_cache_size
)
1239 h8_set_cache_top (sd
, idx
);
1241 /* Throw away its old meaning. */
1242 h8_set_cache_idx (sd
, sd
->sim_cache
[idx
].oldpc
, 0);
1244 /* Set to new address. */
1245 sd
->sim_cache
[idx
].oldpc
= pc
;
1247 /* Fill in instruction info. */
1248 decode (sd
, pc
, h8_get_memory_buf (sd
) + pc
, sd
->sim_cache
+ idx
);
1250 /* Point to new cache entry. */
1251 h8_set_cache_idx (sd
, pc
, idx
);
1255 static unsigned char *breg
[32];
1256 static unsigned short *wreg
[16];
1257 static unsigned int *lreg
[18];
1259 #define GET_B_REG(X) *(breg[X])
1260 #define SET_B_REG(X, Y) (*(breg[X])) = (Y)
1261 #define GET_W_REG(X) *(wreg[X])
1262 #define SET_W_REG(X, Y) (*(wreg[X])) = (Y)
1263 #define GET_L_REG(X) h8_get_reg (sd, X)
1264 #define SET_L_REG(X, Y) h8_set_reg (sd, X, Y)
1266 #define GET_MEMORY_L(X) \
1267 ((X) < memory_size \
1268 ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16) \
1269 | (h8_get_memory (sd, (X)+2) << 8) | (h8_get_memory (sd, (X)+3) << 0)) \
1270 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 24) \
1271 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 16) \
1272 | (h8_get_eightbit (sd, ((X)+2) & 0xff) << 8) \
1273 | (h8_get_eightbit (sd, ((X)+3) & 0xff) << 0)))
1275 #define GET_MEMORY_W(X) \
1276 ((X) < memory_size \
1277 ? ((h8_get_memory (sd, (X)+0) << 8) \
1278 | (h8_get_memory (sd, (X)+1) << 0)) \
1279 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 8) \
1280 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 0)))
1283 #define GET_MEMORY_B(X) \
1284 ((X) < memory_size ? (h8_get_memory (sd, (X))) \
1285 : (h8_get_eightbit (sd, (X) & 0xff)))
1287 #define SET_MEMORY_L(X, Y) \
1288 { register unsigned char *_p; register int __y = (Y); \
1289 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1290 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1291 _p[0] = __y >> 24; _p[1] = __y >> 16; \
1292 _p[2] = __y >> 8; _p[3] = __y >> 0; \
1295 #define SET_MEMORY_W(X, Y) \
1296 { register unsigned char *_p; register int __y = (Y); \
1297 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1298 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1299 _p[0] = __y >> 8; _p[1] = __y; \
1302 #define SET_MEMORY_B(X, Y) \
1303 ((X) < memory_size ? (h8_set_memory (sd, (X), (Y))) \
1304 : (h8_set_eightbit (sd, (X) & 0xff, (Y))))
1306 /* Simulate a memory fetch.
1307 Return 0 for success, -1 for failure.
1311 fetch_1 (SIM_DESC sd
, ea_type
*arg
, int *val
, int twice
)
1314 int abs
= arg
->literal
;
1319 return -1; /* Paranoia. */
1323 /* Indexed register plus displacement mode:
1325 This new family of addressing modes are similar to OP_DISP
1326 (register plus displacement), with two differences:
1327 1) INDEXB uses only the least significant byte of the register,
1328 INDEXW uses only the least significant word, and
1329 INDEXL uses the entire register (just like OP_DISP).
1331 2) The displacement value in abs is multiplied by two
1332 for SW-sized operations, and by four for SL-size.
1334 This gives nine possible variations.
1337 case X (OP_INDEXB
, SB
):
1338 case X (OP_INDEXB
, SW
):
1339 case X (OP_INDEXB
, SL
):
1340 case X (OP_INDEXW
, SB
):
1341 case X (OP_INDEXW
, SW
):
1342 case X (OP_INDEXW
, SL
):
1343 case X (OP_INDEXL
, SB
):
1344 case X (OP_INDEXL
, SW
):
1345 case X (OP_INDEXL
, SL
):
1347 switch (OP_KIND (arg
->type
)) {
1348 case OP_INDEXB
: t
&= 0xff; break;
1349 case OP_INDEXW
: t
&= 0xffff; break;
1353 switch (OP_SIZE (arg
->type
)) {
1355 *val
= GET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (sd
));
1358 *val
= GET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (sd
));
1361 *val
= GET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (sd
));
1366 case X (OP_LOWREG
, SB
):
1367 *val
= GET_L_REG (rn
) & 0xff;
1369 case X (OP_LOWREG
, SW
):
1370 *val
= GET_L_REG (rn
) & 0xffff;
1373 case X (OP_REG
, SB
): /* Register direct, byte. */
1374 *val
= GET_B_REG (rn
);
1376 case X (OP_REG
, SW
): /* Register direct, word. */
1377 *val
= GET_W_REG (rn
);
1379 case X (OP_REG
, SL
): /* Register direct, long. */
1380 *val
= GET_L_REG (rn
);
1382 case X (OP_IMM
, SB
): /* Immediate, byte. */
1383 case X (OP_IMM
, SW
): /* Immediate, word. */
1384 case X (OP_IMM
, SL
): /* Immediate, long. */
1387 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr: byte. */
1389 t
&= h8_get_mask (sd
);
1390 r
= GET_MEMORY_B (t
);
1393 t
= t
& h8_get_mask (sd
);
1397 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr: word. */
1399 t
&= h8_get_mask (sd
);
1400 r
= GET_MEMORY_W (t
);
1403 t
= t
& h8_get_mask (sd
);
1407 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr: long. */
1409 t
&= h8_get_mask (sd
);
1410 r
= GET_MEMORY_L (t
);
1413 t
= t
& h8_get_mask (sd
);
1418 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr: byte. */
1420 t
&= h8_get_mask (sd
);
1421 r
= GET_MEMORY_B (t
);
1424 t
= t
& h8_get_mask (sd
);
1428 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr: word. */
1430 t
&= h8_get_mask (sd
);
1431 r
= GET_MEMORY_W (t
);
1434 t
= t
& h8_get_mask (sd
);
1438 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr: long. */
1440 t
&= h8_get_mask (sd
);
1441 r
= GET_MEMORY_L (t
);
1444 t
= t
& h8_get_mask (sd
);
1449 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr: byte. */
1450 t
= GET_L_REG (rn
) - 1;
1451 t
&= h8_get_mask (sd
);
1453 *val
= GET_MEMORY_B (t
);
1456 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr: word. */
1457 t
= GET_L_REG (rn
) - 2;
1458 t
&= h8_get_mask (sd
);
1460 *val
= GET_MEMORY_W (t
);
1463 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr: long. */
1464 t
= GET_L_REG (rn
) - 4;
1465 t
&= h8_get_mask (sd
);
1467 *val
= GET_MEMORY_L (t
);
1470 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr: byte. */
1471 t
= GET_L_REG (rn
) + 1;
1472 t
&= h8_get_mask (sd
);
1474 *val
= GET_MEMORY_B (t
);
1477 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr: long. */
1478 t
= GET_L_REG (rn
) + 2;
1479 t
&= h8_get_mask (sd
);
1481 *val
= GET_MEMORY_W (t
);
1484 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr: long. */
1485 t
= GET_L_REG (rn
) + 4;
1486 t
&= h8_get_mask (sd
);
1488 *val
= GET_MEMORY_L (t
);
1491 case X (OP_DISP
, SB
): /* Register indirect w/displacement: byte. */
1492 t
= GET_L_REG (rn
) + abs
;
1493 t
&= h8_get_mask (sd
);
1494 *val
= GET_MEMORY_B (t
);
1497 case X (OP_DISP
, SW
): /* Register indirect w/displacement: word. */
1498 t
= GET_L_REG (rn
) + abs
;
1499 t
&= h8_get_mask (sd
);
1500 *val
= GET_MEMORY_W (t
);
1503 case X (OP_DISP
, SL
): /* Register indirect w/displacement: long. */
1504 t
= GET_L_REG (rn
) + abs
;
1505 t
&= h8_get_mask (sd
);
1506 *val
=GET_MEMORY_L (t
);
1509 case X (OP_MEM
, SL
): /* Absolute memory address, long. */
1510 t
= GET_MEMORY_L (abs
);
1511 t
&= h8_get_mask (sd
);
1515 case X (OP_MEM
, SW
): /* Absolute memory address, word. */
1516 t
= GET_MEMORY_W (abs
);
1517 t
&= h8_get_mask (sd
);
1521 case X (OP_PCREL
, SB
): /* PC relative (for jump, branch etc). */
1522 case X (OP_PCREL
, SW
):
1523 case X (OP_PCREL
, SL
):
1524 case X (OP_PCREL
, SN
):
1528 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1530 sim_engine_set_run_state (sd
, sim_stopped
, SIGSEGV
);
1533 return 0; /* Success. */
1539 fetch (SIM_DESC sd
, ea_type
*arg
, int *val
)
1541 return fetch_1 (sd
, arg
, val
, 0);
1544 /* Fetch which will be followed by a store to the same location.
1545 The difference being that we don't want to do a post-increment
1546 or post-decrement at this time: we'll do it when we store. */
1549 fetch2 (SIM_DESC sd
, ea_type
*arg
, int *val
)
1551 return fetch_1 (sd
, arg
, val
, 1);
1554 /* Simulate a memory store.
1555 Return 0 for success, -1 for failure.
1559 store_1 (SIM_DESC sd
, ea_type
*arg
, int n
, int twice
)
1562 int abs
= arg
->literal
;
1567 /* Indexed register plus displacement mode:
1569 This new family of addressing modes are similar to OP_DISP
1570 (register plus displacement), with two differences:
1571 1) INDEXB uses only the least significant byte of the register,
1572 INDEXW uses only the least significant word, and
1573 INDEXL uses the entire register (just like OP_DISP).
1575 2) The displacement value in abs is multiplied by two
1576 for SW-sized operations, and by four for SL-size.
1578 This gives nine possible variations.
1581 case X (OP_INDEXB
, SB
):
1582 case X (OP_INDEXB
, SW
):
1583 case X (OP_INDEXB
, SL
):
1584 case X (OP_INDEXW
, SB
):
1585 case X (OP_INDEXW
, SW
):
1586 case X (OP_INDEXW
, SL
):
1587 case X (OP_INDEXL
, SB
):
1588 case X (OP_INDEXL
, SW
):
1589 case X (OP_INDEXL
, SL
):
1591 switch (OP_KIND (arg
->type
)) {
1592 case OP_INDEXB
: t
&= 0xff; break;
1593 case OP_INDEXW
: t
&= 0xffff; break;
1597 switch (OP_SIZE (arg
->type
)) {
1599 SET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (sd
), n
);
1602 SET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (sd
), n
);
1605 SET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (sd
), n
);
1610 case X (OP_REG
, SB
): /* Register direct, byte. */
1613 case X (OP_REG
, SW
): /* Register direct, word. */
1616 case X (OP_REG
, SL
): /* Register direct, long. */
1620 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr, byte. */
1624 t
&= h8_get_mask (sd
);
1626 SET_MEMORY_B (t
, n
);
1629 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr, word. */
1633 t
&= h8_get_mask (sd
);
1635 SET_MEMORY_W (t
, n
);
1638 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr, long. */
1642 t
&= h8_get_mask (sd
);
1644 SET_MEMORY_L (t
, n
);
1647 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr, byte. */
1651 t
&= h8_get_mask (sd
);
1653 SET_MEMORY_B (t
, n
);
1656 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr, word. */
1660 t
&= h8_get_mask (sd
);
1662 SET_MEMORY_W (t
, n
);
1665 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr, long. */
1669 t
&= h8_get_mask (sd
);
1671 SET_MEMORY_L (t
, n
);
1674 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr, byte. */
1675 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1676 SET_MEMORY_B (t
, n
);
1677 SET_L_REG (rn
, t
- 1);
1680 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr, word. */
1681 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1682 SET_MEMORY_W (t
, n
);
1683 SET_L_REG (rn
, t
- 2);
1686 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr, long. */
1687 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1688 SET_MEMORY_L (t
, n
);
1689 SET_L_REG (rn
, t
- 4);
1692 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr, byte. */
1693 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1694 SET_MEMORY_B (t
, n
);
1695 SET_L_REG (rn
, t
+ 1);
1698 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr, word. */
1699 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1700 SET_MEMORY_W (t
, n
);
1701 SET_L_REG (rn
, t
+ 2);
1704 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr, long. */
1705 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1706 SET_MEMORY_L (t
, n
);
1707 SET_L_REG (rn
, t
+ 4);
1710 case X (OP_DISP
, SB
): /* Register indirect w/displacement, byte. */
1711 t
= GET_L_REG (rn
) + abs
;
1712 t
&= h8_get_mask (sd
);
1713 SET_MEMORY_B (t
, n
);
1716 case X (OP_DISP
, SW
): /* Register indirect w/displacement, word. */
1717 t
= GET_L_REG (rn
) + abs
;
1718 t
&= h8_get_mask (sd
);
1719 SET_MEMORY_W (t
, n
);
1722 case X (OP_DISP
, SL
): /* Register indirect w/displacement, long. */
1723 t
= GET_L_REG (rn
) + abs
;
1724 t
&= h8_get_mask (sd
);
1725 SET_MEMORY_L (t
, n
);
1729 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1730 case X (OP_MEM
, SW
): /* Why isn't this implemented? */
1731 case X (OP_MEM
, SL
): /* Why isn't this implemented? */
1733 sim_engine_set_run_state (sd
, sim_stopped
, SIGSEGV
);
1742 store (SIM_DESC sd
, ea_type
*arg
, int n
)
1744 return store_1 (sd
, arg
, n
, 0);
1747 /* Store which follows a fetch from the same location.
1748 The difference being that we don't want to do a pre-increment
1749 or pre-decrement at this time: it was already done when we fetched. */
1752 store2 (SIM_DESC sd
, ea_type
*arg
, int n
)
1754 return store_1 (sd
, arg
, n
, 1);
1768 /* Flag to be set whenever a new SIM_DESC object is created. */
1769 static int init_pointers_needed
= 1;
1772 init_pointers (SIM_DESC sd
)
1774 if (init_pointers_needed
)
1780 if (h8300smode
&& !h8300_normal_mode
)
1781 memory_size
= H8300S_MSIZE
;
1782 else if (h8300hmode
&& !h8300_normal_mode
)
1783 memory_size
= H8300H_MSIZE
;
1785 memory_size
= H8300_MSIZE
;
1786 /* `msize' must be a power of two. */
1787 if ((memory_size
& (memory_size
- 1)) != 0)
1789 (*sim_callback
->printf_filtered
)
1791 "init_pointers: bad memory size %d, defaulting to %d.\n",
1792 memory_size
, memory_size
= H8300S_MSIZE
);
1795 if (h8_get_memory_buf (sd
))
1796 free (h8_get_memory_buf (sd
));
1797 if (h8_get_cache_idx_buf (sd
))
1798 free (h8_get_cache_idx_buf (sd
));
1799 if (h8_get_eightbit_buf (sd
))
1800 free (h8_get_eightbit_buf (sd
));
1802 h8_set_memory_buf (sd
, (unsigned char *)
1803 calloc (sizeof (char), memory_size
));
1804 h8_set_cache_idx_buf (sd
, (unsigned short *)
1805 calloc (sizeof (short), memory_size
));
1806 sd
->memory_size
= memory_size
;
1807 h8_set_eightbit_buf (sd
, (unsigned char *) calloc (sizeof (char), 256));
1809 h8_set_mask (sd
, memory_size
- 1);
1811 memset (h8_get_reg_buf (sd
), 0, sizeof (((STATE_CPU (sd
, 0))->regs
)));
1813 for (i
= 0; i
< 8; i
++)
1815 /* FIXME: rewrite using local buffer. */
1816 unsigned char *p
= (unsigned char *) (h8_get_reg_buf (sd
) + i
);
1817 unsigned char *e
= (unsigned char *) (h8_get_reg_buf (sd
) + i
+ 1);
1818 unsigned short *q
= (unsigned short *) (h8_get_reg_buf (sd
) + i
);
1819 unsigned short *u
= (unsigned short *) (h8_get_reg_buf (sd
) + i
+ 1);
1820 h8_set_reg (sd
, i
, 0x00112233);
1835 wreg
[i
] = wreg
[i
+ 8] = 0;
1849 if (wreg
[i
] == 0 || wreg
[i
+ 8] == 0)
1850 (*sim_callback
->printf_filtered
) (sim_callback
,
1851 "init_pointers: internal error.\n");
1853 h8_set_reg (sd
, i
, 0);
1854 lreg
[i
] = h8_get_reg_buf (sd
) + i
;
1857 /* Note: sim uses pseudo-register ZERO as a zero register. */
1858 lreg
[ZERO_REGNUM
] = h8_get_reg_buf (sd
) + ZERO_REGNUM
;
1859 init_pointers_needed
= 0;
1861 /* Initialize the seg registers. */
1863 set_simcache_size (sd
, CSIZE
);
1867 /* Grotty global variable for use by control_c signal handler. */
1868 static SIM_DESC control_c_sim_desc
;
1873 sim_engine_set_run_state (control_c_sim_desc
, sim_stopped
, SIGINT
);
1877 sim_stop (SIM_DESC sd
)
1879 /* FIXME: use a real signal value. */
1880 sim_engine_set_run_state (sd
, sim_stopped
, SIGINT
);
1884 #define OBITOP(name, f, s, op) \
1885 case O (name, SB): \
1890 if (fetch (sd, &code->dst, &ea)) \
1892 if (fetch (sd, &code->src, &tmp)) \
1897 if (store (sd, &code->dst,ea)) \
1903 sim_resume (SIM_DESC sd
, int step
, int siggnal
)
1908 int tick_start
= get_now ();
1917 int c
, nz
, v
, n
, u
, h
, ui
, intMaskBit
;
1920 enum sim_stop reason
;
1925 control_c_sim_desc
= sd
;
1926 prev
= signal (SIGINT
, control_c
);
1930 sim_engine_set_run_state (sd
, sim_stopped
, SIGTRAP
);
1934 sim_engine_set_run_state (sd
, sim_running
, 0);
1937 pc
= h8_get_pc (sd
);
1939 /* The PC should never be odd. */
1942 sim_engine_set_run_state (sd
, sim_stopped
, SIGBUS
);
1946 /* Get Status Register (flags). */
1949 if (h8300smode
) /* Get exr. */
1951 trace
= (h8_get_exr (sd
) >> 7) & 1;
1952 intMask
= h8_get_exr (sd
) & 7;
1955 oldmask
= h8_get_mask (sd
);
1956 if (!h8300hmode
|| h8300_normal_mode
)
1957 h8_set_mask (sd
, 0xffff);
1960 unsigned short cidx
;
1964 cidx
= h8_get_cache_idx (sd
, pc
);
1965 if (cidx
== (unsigned short) -1 ||
1966 cidx
>= sd
->sim_cache_size
)
1969 code
= sd
->sim_cache
+ cidx
;
1974 printf ("%x %d %s\n", pc
, code
->opcode
,
1975 code
->op
? code
->op
->name
: "**");
1977 h8_increment_stats (sd
, code
->opcode
);
1982 cycles
+= code
->cycles
;
1986 switch (code
->opcode
)
1990 * This opcode is a fake for when we get to an
1991 * instruction which hasnt been compiled
1997 case O (O_MOVAB
, SL
):
1998 case O (O_MOVAW
, SL
):
1999 case O (O_MOVAL
, SL
):
2000 /* 1) Evaluate 2nd argument (dst).
2001 2) Mask / zero extend according to whether 1st argument (src)
2002 is INDEXB, INDEXW, or INDEXL.
2003 3) Left-shift the result by 0, 1 or 2, according to size of mova
2004 (mova/b, mova/w, mova/l).
2005 4) Add literal value of 1st argument (src).
2006 5) Store result in 3rd argument (op3).
2009 /* Alas, since this is the only instruction with 3 arguments,
2010 decode doesn't handle them very well. Some fix-up is required.
2012 a) The size of dst is determined by whether src is
2013 INDEXB or INDEXW. */
2015 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
2016 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SB
);
2017 else if (OP_KIND (code
->src
.type
) == OP_INDEXW
)
2018 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SW
);
2020 /* b) If op3 == null, then this is the short form of the insn.
2021 Dst is the dispreg of src, and op3 is the 32-bit form
2022 of the same register.
2025 if (code
->op3
.type
== 0)
2027 /* Short form: src == INDEXB/INDEXW, dst == op3 == 0.
2028 We get to compose dst and op3 as follows:
2030 op3 is a 32-bit register, ID == src.reg.
2031 dst is the same register, but 8 or 16 bits
2032 depending on whether src is INDEXB or INDEXW.
2035 code
->op3
.type
= X (OP_REG
, SL
);
2036 code
->op3
.reg
= code
->src
.reg
;
2037 code
->op3
.literal
= 0;
2039 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
2041 code
->dst
.type
= X (OP_REG
, SB
);
2042 code
->dst
.reg
= code
->op3
.reg
+ 8;
2045 code
->dst
.type
= X (OP_REG
, SW
);
2048 if (fetch (sd
, &code
->dst
, &ea
))
2051 switch (OP_KIND (code
->src
.type
)) {
2052 case OP_INDEXB
: ea
= ea
& 0xff; break;
2053 case OP_INDEXW
: ea
= ea
& 0xffff; break;
2054 case OP_INDEXL
: break;
2055 default: goto illegal
;
2058 switch (code
->opcode
) {
2059 case O (O_MOVAB
, SL
): break;
2060 case O (O_MOVAW
, SL
): ea
= ea
<< 1; break;
2061 case O (O_MOVAL
, SL
): ea
= ea
<< 2; break;
2062 default: goto illegal
;
2065 ea
= ea
+ code
->src
.literal
;
2067 if (store (sd
, &code
->op3
, ea
))
2072 case O (O_SUBX
, SB
): /* subx, extended sub */
2073 if (fetch2 (sd
, &code
->dst
, &rd
))
2075 if (fetch (sd
, &code
->src
, &ea
))
2081 case O (O_SUBX
, SW
): /* subx, extended sub */
2082 if (fetch2 (sd
, &code
->dst
, &rd
))
2084 if (fetch (sd
, &code
->src
, &ea
))
2090 case O (O_SUBX
, SL
): /* subx, extended sub */
2091 if (fetch2 (sd
, &code
->dst
, &rd
))
2093 if (fetch (sd
, &code
->src
, &ea
))
2099 case O (O_ADDX
, SB
): /* addx, extended add */
2100 if (fetch2 (sd
, &code
->dst
, &rd
))
2102 if (fetch (sd
, &code
->src
, &ea
))
2108 case O (O_ADDX
, SW
): /* addx, extended add */
2109 if (fetch2 (sd
, &code
->dst
, &rd
))
2111 if (fetch (sd
, &code
->src
, &ea
))
2117 case O (O_ADDX
, SL
): /* addx, extended add */
2118 if (fetch2 (sd
, &code
->dst
, &rd
))
2120 if (fetch (sd
, &code
->src
, &ea
))
2126 case O (O_SUB
, SB
): /* sub.b */
2127 /* Fetch rd and ea. */
2128 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2134 case O (O_SUB
, SW
): /* sub.w */
2135 /* Fetch rd and ea. */
2136 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2142 case O (O_SUB
, SL
): /* sub.l */
2143 /* Fetch rd and ea. */
2144 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2150 case O (O_NEG
, SB
): /* neg.b */
2152 if (fetch2 (sd
, &code
->src
, &ea
))
2159 case O (O_NEG
, SW
): /* neg.w */
2161 if (fetch2 (sd
, &code
->src
, &ea
))
2168 case O (O_NEG
, SL
): /* neg.l */
2170 if (fetch2 (sd
, &code
->src
, &ea
))
2177 case O (O_ADD
, SB
): /* add.b */
2178 if (fetch2 (sd
, &code
->dst
, &rd
))
2180 if (fetch (sd
, &code
->src
, &ea
))
2185 case O (O_ADD
, SW
): /* add.w */
2186 if (fetch2 (sd
, &code
->dst
, &rd
))
2188 if (fetch (sd
, &code
->src
, &ea
))
2193 case O (O_ADD
, SL
): /* add.l */
2194 if (fetch2 (sd
, &code
->dst
, &rd
))
2196 if (fetch (sd
, &code
->src
, &ea
))
2201 case O (O_AND
, SB
): /* and.b */
2202 /* Fetch rd and ea. */
2203 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2208 case O (O_AND
, SW
): /* and.w */
2209 /* Fetch rd and ea. */
2210 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2215 case O (O_AND
, SL
): /* and.l */
2216 /* Fetch rd and ea. */
2217 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2222 case O (O_OR
, SB
): /* or.b */
2223 /* Fetch rd and ea. */
2224 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2229 case O (O_OR
, SW
): /* or.w */
2230 /* Fetch rd and ea. */
2231 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2236 case O (O_OR
, SL
): /* or.l */
2237 /* Fetch rd and ea. */
2238 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2243 case O (O_XOR
, SB
): /* xor.b */
2244 /* Fetch rd and ea. */
2245 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2250 case O (O_XOR
, SW
): /* xor.w */
2251 /* Fetch rd and ea. */
2252 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2257 case O (O_XOR
, SL
): /* xor.l */
2258 /* Fetch rd and ea. */
2259 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2265 if (fetch (sd
, &code
->src
, &res
))
2267 if (store (sd
, &code
->dst
, res
))
2269 goto just_flags_log8
;
2271 if (fetch (sd
, &code
->src
, &res
))
2273 if (store (sd
, &code
->dst
, res
))
2275 goto just_flags_log16
;
2277 if (fetch (sd
, &code
->src
, &res
))
2279 if (store (sd
, &code
->dst
, res
))
2281 goto just_flags_log32
;
2283 case O (O_MOVMD
, SB
): /* movmd.b */
2290 rd
= GET_MEMORY_B (GET_L_REG (5));
2291 SET_MEMORY_B (GET_L_REG (6), rd
);
2292 SET_L_REG (5, GET_L_REG (5) + 1);
2293 SET_L_REG (6, GET_L_REG (6) + 1);
2298 case O (O_MOVMD
, SW
): /* movmd.w */
2305 rd
= GET_MEMORY_W (GET_L_REG (5));
2306 SET_MEMORY_W (GET_L_REG (6), rd
);
2307 SET_L_REG (5, GET_L_REG (5) + 2);
2308 SET_L_REG (6, GET_L_REG (6) + 2);
2313 case O (O_MOVMD
, SL
): /* movmd.l */
2320 rd
= GET_MEMORY_L (GET_L_REG (5));
2321 SET_MEMORY_L (GET_L_REG (6), rd
);
2322 SET_L_REG (5, GET_L_REG (5) + 4);
2323 SET_L_REG (6, GET_L_REG (6) + 4);
2328 case O (O_MOVSD
, SB
): /* movsd.b */
2329 /* This instruction implements strncpy, with a conditional branch.
2330 r4 contains n, r5 contains src, and r6 contains dst.
2331 The 16-bit displacement operand is added to the pc
2332 if and only if the end of string is reached before
2333 n bytes are transferred. */
2335 ea
= GET_L_REG (4) & 0xffff;
2341 rd
= GET_MEMORY_B (GET_L_REG (5));
2342 SET_MEMORY_B (GET_L_REG (6), rd
);
2343 SET_L_REG (5, GET_L_REG (5) + 1);
2344 SET_L_REG (6, GET_L_REG (6) + 1);
2351 case O (O_EEPMOV
, SB
): /* eepmov.b */
2352 case O (O_EEPMOV
, SW
): /* eepmov.w */
2353 if (h8300hmode
|| h8300smode
)
2355 register unsigned char *_src
, *_dst
;
2356 unsigned int count
= ((code
->opcode
== O (O_EEPMOV
, SW
))
2357 ? h8_get_reg (sd
, R4_REGNUM
) & 0xffff
2358 : h8_get_reg (sd
, R4_REGNUM
) & 0xff);
2360 _src
= (h8_get_reg (sd
, R5_REGNUM
) < memory_size
2361 ? h8_get_memory_buf (sd
) + h8_get_reg (sd
, R5_REGNUM
)
2362 : h8_get_eightbit_buf (sd
) +
2363 (h8_get_reg (sd
, R5_REGNUM
) & 0xff));
2364 if ((_src
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2366 if ((_src
+ count
) >= (h8_get_eightbit_buf (sd
) + 0x100))
2369 _dst
= (h8_get_reg (sd
, R6_REGNUM
) < memory_size
2370 ? h8_get_memory_buf (sd
) + h8_get_reg (sd
, R6_REGNUM
)
2371 : h8_get_eightbit_buf (sd
) +
2372 (h8_get_reg (sd
, R6_REGNUM
) & 0xff));
2374 if ((_dst
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2376 if ((_dst
+ count
) >= (h8_get_eightbit_buf (sd
) + 0x100))
2379 memcpy (_dst
, _src
, count
);
2381 h8_set_reg (sd
, R5_REGNUM
, h8_get_reg (sd
, R5_REGNUM
) + count
);
2382 h8_set_reg (sd
, R6_REGNUM
, h8_get_reg (sd
, R6_REGNUM
) + count
);
2383 h8_set_reg (sd
, R4_REGNUM
, h8_get_reg (sd
, R4_REGNUM
) &
2384 ((code
->opcode
== O (O_EEPMOV
, SW
))
2385 ? (~0xffff) : (~0xff)));
2386 cycles
+= 2 * count
;
2391 case O (O_ADDS
, SL
): /* adds (.l) */
2393 * This insn only uses register operands, but still
2394 * it would be cleaner to use fetch and store... */
2395 SET_L_REG (code
->dst
.reg
,
2396 GET_L_REG (code
->dst
.reg
)
2397 + code
->src
.literal
);
2401 case O (O_SUBS
, SL
): /* subs (.l) */
2403 * This insn only uses register operands, but still
2404 * it would be cleaner to use fetch and store... */
2405 SET_L_REG (code
->dst
.reg
,
2406 GET_L_REG (code
->dst
.reg
)
2407 - code
->src
.literal
);
2410 case O (O_CMP
, SB
): /* cmp.b */
2411 if (fetch (sd
, &code
->dst
, &rd
))
2413 if (fetch (sd
, &code
->src
, &ea
))
2417 goto just_flags_alu8
;
2419 case O (O_CMP
, SW
): /* cmp.w */
2420 if (fetch (sd
, &code
->dst
, &rd
))
2422 if (fetch (sd
, &code
->src
, &ea
))
2426 goto just_flags_alu16
;
2428 case O (O_CMP
, SL
): /* cmp.l */
2429 if (fetch (sd
, &code
->dst
, &rd
))
2431 if (fetch (sd
, &code
->src
, &ea
))
2435 goto just_flags_alu32
;
2437 case O (O_DEC
, SB
): /* dec.b */
2439 * This insn only uses register operands, but still
2440 * it would be cleaner to use fetch and store... */
2441 rd
= GET_B_REG (code
->src
.reg
);
2444 SET_B_REG (code
->src
.reg
, res
);
2445 goto just_flags_inc8
;
2447 case O (O_DEC
, SW
): /* dec.w */
2449 * This insn only uses register operands, but still
2450 * it would be cleaner to use fetch and store... */
2451 rd
= GET_W_REG (code
->dst
.reg
);
2452 ea
= -code
->src
.literal
;
2454 SET_W_REG (code
->dst
.reg
, res
);
2455 goto just_flags_inc16
;
2457 case O (O_DEC
, SL
): /* dec.l */
2459 * This insn only uses register operands, but still
2460 * it would be cleaner to use fetch and store... */
2461 rd
= GET_L_REG (code
->dst
.reg
);
2462 ea
= -code
->src
.literal
;
2464 SET_L_REG (code
->dst
.reg
, res
);
2465 goto just_flags_inc32
;
2467 case O (O_INC
, SB
): /* inc.b */
2469 * This insn only uses register operands, but still
2470 * it would be cleaner to use fetch and store... */
2471 rd
= GET_B_REG (code
->src
.reg
);
2474 SET_B_REG (code
->src
.reg
, res
);
2475 goto just_flags_inc8
;
2477 case O (O_INC
, SW
): /* inc.w */
2479 * This insn only uses register operands, but still
2480 * it would be cleaner to use fetch and store... */
2481 rd
= GET_W_REG (code
->dst
.reg
);
2482 ea
= code
->src
.literal
;
2484 SET_W_REG (code
->dst
.reg
, res
);
2485 goto just_flags_inc16
;
2487 case O (O_INC
, SL
): /* inc.l */
2489 * This insn only uses register operands, but still
2490 * it would be cleaner to use fetch and store... */
2491 rd
= GET_L_REG (code
->dst
.reg
);
2492 ea
= code
->src
.literal
;
2494 SET_L_REG (code
->dst
.reg
, res
);
2495 goto just_flags_inc32
;
2497 case O (O_LDC
, SB
): /* ldc.b */
2498 if (fetch (sd
, &code
->src
, &res
))
2502 case O (O_LDC
, SW
): /* ldc.w */
2503 if (fetch (sd
, &code
->src
, &res
))
2506 /* Word operand, value from MSB, must be shifted. */
2510 case O (O_LDC
, SL
): /* ldc.l */
2511 if (fetch (sd
, &code
->src
, &res
))
2513 switch (code
->dst
.type
) {
2514 case X (OP_SBR
, SL
):
2515 h8_set_sbr (sd
, res
);
2517 case X (OP_VBR
, SL
):
2518 h8_set_vbr (sd
, res
);
2525 case O (O_STC
, SW
): /* stc.w */
2526 case O (O_STC
, SB
): /* stc.b */
2527 if (code
->src
.type
== X (OP_CCR
, SB
))
2530 res
= h8_get_ccr (sd
);
2532 else if (code
->src
.type
== X (OP_EXR
, SB
) && h8300smode
)
2535 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2536 res
= h8_get_exr (sd
);
2541 /* Word operand, value to MSB, must be shifted. */
2542 if (code
->opcode
== X (O_STC
, SW
))
2544 if (store (sd
, &code
->dst
, res
))
2547 case O (O_STC
, SL
): /* stc.l */
2548 switch (code
->src
.type
) {
2549 case X (OP_SBR
, SL
):
2550 res
= h8_get_sbr (sd
);
2552 case X (OP_VBR
, SL
):
2553 res
= h8_get_vbr (sd
);
2558 if (store (sd
, &code
->dst
, res
))
2562 case O (O_ANDC
, SB
): /* andc.b */
2563 if (code
->dst
.type
== X (OP_CCR
, SB
))
2566 rd
= h8_get_ccr (sd
);
2568 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2571 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2572 rd
= h8_get_exr (sd
);
2576 ea
= code
->src
.literal
;
2580 case O (O_ORC
, SB
): /* orc.b */
2581 if (code
->dst
.type
== X (OP_CCR
, SB
))
2584 rd
= h8_get_ccr (sd
);
2586 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2589 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2590 rd
= h8_get_exr (sd
);
2594 ea
= code
->src
.literal
;
2598 case O (O_XORC
, SB
): /* xorc.b */
2599 if (code
->dst
.type
== X (OP_CCR
, SB
))
2602 rd
= h8_get_ccr (sd
);
2604 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2607 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2608 rd
= h8_get_exr (sd
);
2612 ea
= code
->src
.literal
;
2616 case O (O_BRAS
, SB
): /* bra/s */
2617 /* This is basically an ordinary branch, with a delay slot. */
2618 if (fetch (sd
, &code
->src
, &res
))
2626 /* Execution continues at next instruction, but
2627 delayed_branch is set up for next cycle. */
2628 h8_set_delayed_branch (sd
, code
->next_pc
+ res
);
2632 case O (O_BRAB
, SB
): /* bra rd.b */
2633 case O (O_BRAW
, SW
): /* bra rd.w */
2634 case O (O_BRAL
, SL
): /* bra erd.l */
2635 if (fetch (sd
, &code
->src
, &rd
))
2637 switch (OP_SIZE (code
->opcode
)) {
2638 case SB
: rd
&= 0xff; break;
2639 case SW
: rd
&= 0xffff; break;
2640 case SL
: rd
&= 0xffffffff; break;
2642 pc
= code
->next_pc
+ rd
;
2645 case O (O_BRABC
, SB
): /* bra/bc, branch if bit clear */
2646 case O (O_BRABS
, SB
): /* bra/bs, branch if bit set */
2647 case O (O_BSRBC
, SB
): /* bsr/bc, call if bit clear */
2648 case O (O_BSRBS
, SB
): /* bsr/bs, call if bit set */
2649 if (fetch (sd
, &code
->dst
, &rd
) ||
2650 fetch (sd
, &code
->src
, &bit
))
2653 if (code
->opcode
== O (O_BRABC
, SB
) || /* branch if clear */
2654 code
->opcode
== O (O_BSRBC
, SB
)) /* call if clear */
2656 if ((rd
& (1 << bit
))) /* no branch */
2659 else /* branch/call if set */
2661 if (!(rd
& (1 << bit
))) /* no branch */
2665 if (fetch (sd
, &code
->op3
, &res
)) /* branch */
2667 pc
= code
->next_pc
+ res
;
2669 if (code
->opcode
== O (O_BRABC
, SB
) ||
2670 code
->opcode
== O (O_BRABS
, SB
)) /* branch */
2678 case O (O_BRA
, SB
): /* bra, branch always */
2683 case O (O_BRN
, SB
): /* brn, ;-/ branch never? */
2688 case O (O_BHI
, SB
): /* bhi */
2694 case O (O_BLS
, SB
): /* bls */
2699 case O (O_BCS
, SB
): /* bcs, branch if carry set */
2704 case O (O_BCC
, SB
): /* bcc, branch if carry clear */
2709 case O (O_BEQ
, SB
): /* beq, branch if zero set */
2713 case O (O_BGT
, SB
): /* bgt */
2714 if (((Z
|| (N
^ V
)) == 0))
2718 case O (O_BLE
, SB
): /* ble */
2719 if (((Z
|| (N
^ V
)) == 1))
2723 case O (O_BGE
, SB
): /* bge */
2727 case O (O_BLT
, SB
): /* blt */
2731 case O (O_BMI
, SB
): /* bmi */
2735 case O (O_BNE
, SB
): /* bne, branch if zero clear */
2740 case O (O_BPL
, SB
): /* bpl */
2744 case O (O_BVC
, SB
): /* bvc */
2748 case O (O_BVS
, SB
): /* bvs */
2753 /* Trap for Command Line setup. */
2754 case O (O_SYS_CMDLINE
, SB
):
2756 int i
= 0; /* Loop counter. */
2757 int j
= 0; /* Loop counter. */
2758 int ind_arg_len
= 0; /* Length of each argument. */
2759 int no_of_args
= 0; /* The no. or cmdline args. */
2760 int current_location
= 0; /* Location of string. */
2761 int old_sp
= 0; /* The Initial Stack Pointer. */
2762 int no_of_slots
= 0; /* No. of slots required on the stack
2763 for storing cmdline args. */
2764 int sp_move
= 0; /* No. of locations by which the stack needs
2766 int new_sp
= 0; /* The final stack pointer location passed
2768 int *argv_ptrs
; /* Pointers of argv strings to be stored. */
2769 int argv_ptrs_location
= 0; /* Location of pointers to cmdline
2770 args on the stack. */
2771 int char_ptr_size
= 0; /* Size of a character pointer on
2773 int addr_cmdline
= 0; /* Memory location where cmdline has
2775 int size_cmdline
= 0; /* Size of cmdline. */
2777 /* Set the address of 256 free locations where command line is
2779 addr_cmdline
= cmdline_location();
2780 h8_set_reg (sd
, 0, addr_cmdline
);
2782 /* Counting the no. of commandline arguments. */
2783 for (i
= 0; h8_get_cmdline_arg (sd
, i
) != NULL
; i
++)
2786 /* No. of arguments in the command line. */
2789 /* Current location is just a temporary variable,which we are
2790 setting to the point to the start of our commandline string. */
2791 current_location
= addr_cmdline
;
2793 /* Allocating space for storing pointers of the command line
2795 argv_ptrs
= (int *) malloc (sizeof (int) * no_of_args
);
2797 /* Setting char_ptr_size to the sizeof (char *) on the different
2799 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2808 for (i
= 0; i
< no_of_args
; i
++)
2812 /* The size of the commandline argument. */
2813 ind_arg_len
= strlen (h8_get_cmdline_arg (sd
, i
) + 1);
2815 /* The total size of the command line string. */
2816 size_cmdline
+= ind_arg_len
;
2818 /* As we have only 256 bytes, we need to provide a graceful
2819 exit. Anyways, a program using command line arguments
2820 where we cannot store all the command line arguments
2821 given may behave unpredictably. */
2822 if (size_cmdline
>= 256)
2824 h8_set_reg (sd
, 0, 0);
2829 /* current_location points to the memory where the next
2830 commandline argument is stored. */
2831 argv_ptrs
[i
] = current_location
;
2832 for (j
= 0; j
< ind_arg_len
; j
++)
2834 SET_MEMORY_B ((current_location
+
2835 (sizeof (char) * j
)),
2836 *(h8_get_cmdline_arg (sd
, i
) +
2837 sizeof (char) * j
));
2840 /* Setting current_location to the starting of next
2842 current_location
+= ind_arg_len
;
2846 /* This is the original position of the stack pointer. */
2847 old_sp
= h8_get_reg (sd
, SP_REGNUM
);
2849 /* We need space from the stack to store the pointers to argvs. */
2850 /* As we will infringe on the stack, we need to shift the stack
2851 pointer so that the data is not overwritten. We calculate how
2852 much space is required. */
2853 sp_move
= (no_of_args
) * (char_ptr_size
);
2855 /* The final position of stack pointer, we have thus taken some
2856 space from the stack. */
2857 new_sp
= old_sp
- sp_move
;
2859 /* Temporary variable holding value where the argv pointers need
2861 argv_ptrs_location
= new_sp
;
2863 /* The argv pointers are stored at sequential locations. As per
2865 for (i
= 0; i
< no_of_args
; i
++)
2867 /* Saving the argv pointer. */
2868 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2870 SET_MEMORY_L (argv_ptrs_location
, argv_ptrs
[i
]);
2874 SET_MEMORY_W (argv_ptrs_location
, argv_ptrs
[i
]);
2877 /* The next location where the pointer to the next argv
2878 string has to be stored. */
2879 argv_ptrs_location
+= char_ptr_size
;
2882 /* Required by POSIX, Setting 0x0 at the end of the list of argv
2884 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2886 SET_MEMORY_L (old_sp
, 0x0);
2890 SET_MEMORY_W (old_sp
, 0x0);
2893 /* Freeing allocated memory. */
2895 for (i
= 0; i
<= no_of_args
; i
++)
2897 free (h8_get_cmdline_arg (sd
, i
));
2899 free (h8_get_command_line (sd
));
2901 /* The no. of argv arguments are returned in Reg 0. */
2902 h8_set_reg (sd
, 0, no_of_args
);
2903 /* The Pointer to argv in Register 1. */
2904 h8_set_reg (sd
, 1, new_sp
);
2905 /* Setting the stack pointer to the new value. */
2906 h8_set_reg (sd
, SP_REGNUM
, new_sp
);
2910 /* System call processing starts. */
2911 case O (O_SYS_OPEN
, SB
):
2913 int len
= 0; /* Length of filename. */
2914 char *filename
; /* Filename would go here. */
2915 char temp_char
; /* Temporary character */
2916 int mode
= 0; /* Mode bits for the file. */
2917 int open_return
; /* Return value of open, file descriptor. */
2918 int i
; /* Loop counter */
2919 int filename_ptr
; /* Pointer to filename in cpu memory. */
2921 /* Setting filename_ptr to first argument of open, */
2922 /* and trying to get mode. */
2923 if ((h8300sxmode
|| h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2925 filename_ptr
= GET_L_REG (0);
2926 mode
= GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
) + 4);
2930 filename_ptr
= GET_W_REG (0);
2931 mode
= GET_MEMORY_W (h8_get_reg (sd
, SP_REGNUM
) + 2);
2934 /* Trying to find the length of the filename. */
2935 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
2938 while (temp_char
!= '\0')
2940 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
2944 /* Allocating space for the filename. */
2945 filename
= (char *) malloc (sizeof (char) * len
);
2947 /* String copying the filename from memory. */
2948 for (i
= 0; i
< len
; i
++)
2950 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
2951 filename
[i
] = temp_char
;
2954 /* Callback to open and return the file descriptor. */
2955 open_return
= sim_callback
->open (sim_callback
, filename
, mode
);
2957 /* Return value in register 0. */
2958 h8_set_reg (sd
, 0, open_return
);
2960 /* Freeing memory used for filename. */
2965 case O (O_SYS_READ
, SB
):
2967 char *char_ptr
; /* Where characters read would be stored. */
2968 int fd
; /* File descriptor */
2969 int buf_size
; /* BUF_SIZE parameter in read. */
2970 int i
= 0; /* Temporary Loop counter */
2971 int read_return
= 0; /* Return value from callback to
2974 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2975 buf_size
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2977 char_ptr
= (char *) malloc (sizeof (char) * buf_size
);
2979 /* Callback to read and return the no. of characters read. */
2981 sim_callback
->read (sim_callback
, fd
, char_ptr
, buf_size
);
2983 /* The characters read are stored in cpu memory. */
2984 for (i
= 0; i
< buf_size
; i
++)
2986 SET_MEMORY_B ((h8_get_reg (sd
, 1) + (sizeof (char) * i
)),
2987 *(char_ptr
+ (sizeof (char) * i
)));
2990 /* Return value in Register 0. */
2991 h8_set_reg (sd
, 0, read_return
);
2993 /* Freeing memory used as buffer. */
2998 case O (O_SYS_WRITE
, SB
):
3000 int fd
; /* File descriptor */
3001 char temp_char
; /* Temporary character */
3002 int len
; /* Length of write, Parameter II to write. */
3003 int char_ptr
; /* Character Pointer, Parameter I of write. */
3004 char *ptr
; /* Where characters to be written are stored.
3006 int write_return
; /* Return value from callback to write. */
3007 int i
= 0; /* Loop counter */
3009 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
3010 char_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
3011 len
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
3013 /* Allocating space for the characters to be written. */
3014 ptr
= (char *) malloc (sizeof (char) * len
);
3016 /* Fetching the characters from cpu memory. */
3017 for (i
= 0; i
< len
; i
++)
3019 temp_char
= GET_MEMORY_B (char_ptr
+ i
);
3023 /* Callback write and return the no. of characters written. */
3024 write_return
= sim_callback
->write (sim_callback
, fd
, ptr
, len
);
3026 /* Return value in Register 0. */
3027 h8_set_reg (sd
, 0, write_return
);
3029 /* Freeing memory used as buffer. */
3034 case O (O_SYS_LSEEK
, SB
):
3036 int fd
; /* File descriptor */
3037 int offset
; /* Offset */
3038 int origin
; /* Origin */
3039 int lseek_return
; /* Return value from callback to lseek. */
3041 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
3042 offset
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
3043 origin
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
3045 /* Callback lseek and return offset. */
3047 sim_callback
->lseek (sim_callback
, fd
, offset
, origin
);
3049 /* Return value in register 0. */
3050 h8_set_reg (sd
, 0, lseek_return
);
3054 case O (O_SYS_CLOSE
, SB
):
3056 int fd
; /* File descriptor */
3057 int close_return
; /* Return value from callback to close. */
3059 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
3061 /* Callback close and return. */
3062 close_return
= sim_callback
->close (sim_callback
, fd
);
3064 /* Return value in register 0. */
3065 h8_set_reg (sd
, 0, close_return
);
3069 case O (O_SYS_FSTAT
, SB
):
3071 int fd
; /* File descriptor */
3072 struct stat stat_rec
; /* Stat record */
3073 int fstat_return
; /* Return value from callback to stat. */
3074 int stat_ptr
; /* Pointer to stat record. */
3075 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
3077 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
3079 /* Setting stat_ptr to second argument of stat. */
3080 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
3082 /* Callback stat and return. */
3083 fstat_return
= sim_callback
->fstat (sim_callback
, fd
, &stat_rec
);
3085 /* Have stat_ptr point to starting of stat_rec. */
3086 temp_stat_ptr
= (char *) (&stat_rec
);
3088 /* Setting up the stat structure returned. */
3089 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
3091 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
3093 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
3095 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
3097 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
3099 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
3101 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
3103 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
3105 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
3107 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
3109 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
3111 /* Return value in register 0. */
3112 h8_set_reg (sd
, 0, fstat_return
);
3116 case O (O_SYS_STAT
, SB
):
3118 int len
= 0; /* Length of filename. */
3119 char *filename
; /* Filename would go here. */
3120 char temp_char
; /* Temporary character */
3121 int filename_ptr
; /* Pointer to filename in cpu memory. */
3122 struct stat stat_rec
; /* Stat record */
3123 int stat_return
; /* Return value from callback to stat */
3124 int stat_ptr
; /* Pointer to stat record. */
3125 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
3126 int i
= 0; /* Loop Counter */
3128 /* Setting filename_ptr to first argument of open. */
3129 filename_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
3131 /* Trying to find the length of the filename. */
3132 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
3135 while (temp_char
!= '\0')
3137 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
3141 /* Allocating space for the filename. */
3142 filename
= (char *) malloc (sizeof (char) * len
);
3144 /* String copying the filename from memory. */
3145 for (i
= 0; i
< len
; i
++)
3147 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
3148 filename
[i
] = temp_char
;
3151 /* Setting stat_ptr to second argument of stat. */
3152 /* stat_ptr = h8_get_reg (sd, 1); */
3153 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
3155 /* Callback stat and return. */
3157 sim_callback
->stat (sim_callback
, filename
, &stat_rec
);
3159 /* Have stat_ptr point to starting of stat_rec. */
3160 temp_stat_ptr
= (char *) (&stat_rec
);
3162 /* Freeing memory used for filename. */
3165 /* Setting up the stat structure returned. */
3166 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
3168 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
3170 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
3172 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
3174 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
3176 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
3178 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
3180 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
3182 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
3184 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
3186 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
3188 /* Return value in register 0. */
3189 h8_set_reg (sd
, 0, stat_return
);
3192 /* End of system call processing. */
3194 case O (O_NOT
, SB
): /* not.b */
3195 if (fetch2 (sd
, &code
->src
, &rd
))
3201 case O (O_NOT
, SW
): /* not.w */
3202 if (fetch2 (sd
, &code
->src
, &rd
))
3208 case O (O_NOT
, SL
): /* not.l */
3209 if (fetch2 (sd
, &code
->src
, &rd
))
3215 case O (O_SHLL
, SB
): /* shll.b */
3216 case O (O_SHLR
, SB
): /* shlr.b */
3217 if (fetch2 (sd
, &code
->dst
, &rd
))
3220 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3221 ea
= 1; /* unary op */
3222 else /* binary op */
3223 fetch (sd
, &code
->src
, &ea
);
3225 if (code
->opcode
== O (O_SHLL
, SB
))
3228 c
= rd
& (0x80 >> (ea
- 1));
3234 c
= rd
& (1 << (ea
- 1));
3235 rd
= (unsigned char) rd
>> ea
;
3239 case O (O_SHLL
, SW
): /* shll.w */
3240 case O (O_SHLR
, SW
): /* shlr.w */
3241 if (fetch2 (sd
, &code
->dst
, &rd
))
3244 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3245 ea
= 1; /* unary op */
3247 fetch (sd
, &code
->src
, &ea
);
3249 if (code
->opcode
== O (O_SHLL
, SW
))
3252 c
= rd
& (0x8000 >> (ea
- 1));
3258 c
= rd
& (1 << (ea
- 1));
3259 rd
= (unsigned short) rd
>> ea
;
3263 case O (O_SHLL
, SL
): /* shll.l */
3264 case O (O_SHLR
, SL
): /* shlr.l */
3265 if (fetch2 (sd
, &code
->dst
, &rd
))
3268 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3269 ea
= 1; /* unary op */
3271 fetch (sd
, &code
->src
, &ea
);
3273 if (code
->opcode
== O (O_SHLL
, SL
))
3276 c
= rd
& (0x80000000 >> (ea
- 1));
3282 c
= rd
& (1 << (ea
- 1));
3283 rd
= (unsigned int) rd
>> ea
;
3287 case O (O_SHAL
, SB
):
3288 case O (O_SHAR
, SB
):
3289 if (fetch2 (sd
, &code
->dst
, &rd
))
3292 if (code
->src
.type
== X (OP_IMM
, SB
))
3293 fetch (sd
, &code
->src
, &ea
);
3297 if (code
->opcode
== O (O_SHAL
, SB
))
3299 c
= rd
& (0x80 >> (ea
- 1));
3300 res
= rd
>> (7 - ea
);
3301 v
= ((res
& 1) && !(res
& 2))
3302 || (!(res
& 1) && (res
& 2));
3307 c
= rd
& (1 << (ea
- 1));
3309 rd
= ((signed char) rd
) >> ea
;
3313 case O (O_SHAL
, SW
):
3314 case O (O_SHAR
, SW
):
3315 if (fetch2 (sd
, &code
->dst
, &rd
))
3318 if (code
->src
.type
== X (OP_IMM
, SW
))
3319 fetch (sd
, &code
->src
, &ea
);
3323 if (code
->opcode
== O (O_SHAL
, SW
))
3325 c
= rd
& (0x8000 >> (ea
- 1));
3326 res
= rd
>> (15 - ea
);
3327 v
= ((res
& 1) && !(res
& 2))
3328 || (!(res
& 1) && (res
& 2));
3333 c
= rd
& (1 << (ea
- 1));
3335 rd
= ((signed short) rd
) >> ea
;
3339 case O (O_SHAL
, SL
):
3340 case O (O_SHAR
, SL
):
3341 if (fetch2 (sd
, &code
->dst
, &rd
))
3344 if (code
->src
.type
== X (OP_IMM
, SL
))
3345 fetch (sd
, &code
->src
, &ea
);
3349 if (code
->opcode
== O (O_SHAL
, SL
))
3351 c
= rd
& (0x80000000 >> (ea
- 1));
3352 res
= rd
>> (31 - ea
);
3353 v
= ((res
& 1) && !(res
& 2))
3354 || (!(res
& 1) && (res
& 2));
3359 c
= rd
& (1 << (ea
- 1));
3361 rd
= ((signed int) rd
) >> ea
;
3365 case O (O_ROTL
, SB
):
3366 case O (O_ROTR
, SB
):
3367 if (fetch2 (sd
, &code
->dst
, &rd
))
3370 if (code
->src
.type
== X (OP_IMM
, SB
))
3371 fetch (sd
, &code
->src
, &ea
);
3376 if (code
->opcode
== O (O_ROTL
, SB
))
3386 rd
= ((unsigned char) rd
) >> 1;
3394 case O (O_ROTL
, SW
):
3395 case O (O_ROTR
, SW
):
3396 if (fetch2 (sd
, &code
->dst
, &rd
))
3399 if (code
->src
.type
== X (OP_IMM
, SW
))
3400 fetch (sd
, &code
->src
, &ea
);
3405 if (code
->opcode
== O (O_ROTL
, SW
))
3415 rd
= ((unsigned short) rd
) >> 1;
3423 case O (O_ROTL
, SL
):
3424 case O (O_ROTR
, SL
):
3425 if (fetch2 (sd
, &code
->dst
, &rd
))
3428 if (code
->src
.type
== X (OP_IMM
, SL
))
3429 fetch (sd
, &code
->src
, &ea
);
3434 if (code
->opcode
== O (O_ROTL
, SL
))
3436 c
= rd
& 0x80000000;
3444 rd
= ((unsigned int) rd
) >> 1;
3452 case O (O_ROTXL
, SB
):
3453 case O (O_ROTXR
, SB
):
3454 if (fetch2 (sd
, &code
->dst
, &rd
))
3457 if (code
->src
.type
== X (OP_IMM
, SB
))
3458 fetch (sd
, &code
->src
, &ea
);
3463 if (code
->opcode
== O (O_ROTXL
, SB
))
3474 rd
= ((unsigned char) rd
) >> 1;
3483 case O (O_ROTXL
, SW
):
3484 case O (O_ROTXR
, SW
):
3485 if (fetch2 (sd
, &code
->dst
, &rd
))
3488 if (code
->src
.type
== X (OP_IMM
, SW
))
3489 fetch (sd
, &code
->src
, &ea
);
3494 if (code
->opcode
== O (O_ROTXL
, SW
))
3505 rd
= ((unsigned short) rd
) >> 1;
3514 case O (O_ROTXL
, SL
):
3515 case O (O_ROTXR
, SL
):
3516 if (fetch2 (sd
, &code
->dst
, &rd
))
3519 if (code
->src
.type
== X (OP_IMM
, SL
))
3520 fetch (sd
, &code
->src
, &ea
);
3525 if (code
->opcode
== O (O_ROTXL
, SL
))
3527 res
= rd
& 0x80000000;
3536 rd
= ((unsigned int) rd
) >> 1;
3547 case O (O_JMP
, SB
): /* jmp */
3549 fetch (sd
, &code
->src
, &pc
);
3554 case O (O_JSR
, SB
): /* jsr, jump to subroutine */
3556 if (fetch (sd
, &code
->src
, &pc
))
3559 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3561 if (h8300hmode
&& !h8300_normal_mode
)
3564 SET_MEMORY_L (tmp
, code
->next_pc
);
3569 SET_MEMORY_W (tmp
, code
->next_pc
);
3571 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3577 case O (O_BSR
, SB
): /* bsr, branch to subroutine */
3578 if (fetch (sd
, &code
->src
, &res
))
3580 pc
= code
->next_pc
+ res
;
3583 case O (O_RTE
, SN
): /* rte, return from exception */
3585 /* Pops exr and ccr before pc -- otherwise identical to rts. */
3586 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3588 if (h8300smode
) /* pop exr */
3590 h8_set_exr (sd
, GET_MEMORY_L (tmp
));
3593 if (h8300hmode
&& !h8300_normal_mode
)
3595 h8_set_ccr (sd
, GET_MEMORY_L (tmp
));
3597 pc
= GET_MEMORY_L (tmp
);
3602 h8_set_ccr (sd
, GET_MEMORY_W (tmp
));
3604 pc
= GET_MEMORY_W (tmp
);
3609 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3612 case O (O_RTS
, SN
): /* rts, return from subroutine */
3614 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3616 if (h8300hmode
&& !h8300_normal_mode
)
3618 pc
= GET_MEMORY_L (tmp
);
3623 pc
= GET_MEMORY_W (tmp
);
3627 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3630 case O (O_ILL
, SB
): /* illegal */
3631 sim_engine_set_run_state (sd
, sim_stopped
, SIGILL
);
3634 case O (O_SLEEP
, SN
): /* sleep */
3635 /* Check for magic numbers in r1 and r2. */
3636 if ((h8_get_reg (sd
, R1_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC1
&&
3637 (h8_get_reg (sd
, R2_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC2
&&
3638 SIM_WIFEXITED (h8_get_reg (sd
, 0)))
3640 /* This trap comes from _exit, not from gdb. */
3641 sim_engine_set_run_state (sd
, sim_exited
,
3642 SIM_WEXITSTATUS (h8_get_reg (sd
, 0)));
3645 /* Unfortunately this won't really work, because
3646 when we take a breakpoint trap, R0 has a "random",
3647 user-defined value. Don't see any immediate solution. */
3648 else if (SIM_WIFSTOPPED (h8_get_reg (sd
, 0)))
3650 /* Pass the stop signal up to gdb. */
3651 sim_engine_set_run_state (sd
, sim_stopped
,
3652 SIM_WSTOPSIG (h8_get_reg (sd
, 0)));
3657 /* Treat it as a sigtrap. */
3658 sim_engine_set_run_state (sd
, sim_stopped
, SIGTRAP
);
3662 case O (O_TRAPA
, SB
): /* trapa */
3663 if (fetch (sd
, &code
->src
, &res
))
3664 goto end
; /* res is vector number. */
3666 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3667 if(h8300_normal_mode
)
3670 SET_MEMORY_W (tmp
, code
->next_pc
);
3672 SET_MEMORY_W (tmp
, h8_get_ccr (sd
));
3677 SET_MEMORY_L (tmp
, code
->next_pc
);
3679 SET_MEMORY_L (tmp
, h8_get_ccr (sd
));
3687 SET_MEMORY_L (tmp
, h8_get_exr (sd
));
3690 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3692 if(h8300_normal_mode
)
3693 pc
= GET_MEMORY_L (0x10 + res
* 2); /* Vector addresses are 0x10,0x12,0x14 and 0x16 */
3695 pc
= GET_MEMORY_L (0x20 + res
* 4);
3699 sim_engine_set_run_state (sd
, sim_stopped
, SIGTRAP
);
3702 case O (O_BSETEQ
, SB
):
3707 case O (O_BSETNE
, SB
):
3712 case O (O_BCLREQ
, SB
):
3717 case O (O_BCLRNE
, SB
):
3722 OBITOP (O_BNOT
, 1, 1, ea
^= m
); /* bnot */
3723 OBITOP (O_BTST
, 1, 0, nz
= ea
& m
); /* btst */
3725 OBITOP (O_BSET
, 1, 1, ea
|= m
); /* bset */
3727 OBITOP (O_BCLR
, 1, 1, ea
&= ~m
); /* bclr */
3728 OBITOP (O_BLD
, 1, 0, c
= ea
& m
); /* bld */
3729 OBITOP (O_BILD
, 1, 0, c
= !(ea
& m
)); /* bild */
3730 OBITOP (O_BST
, 1, 1, ea
&= ~m
;
3731 if (C
) ea
|= m
); /* bst */
3732 OBITOP (O_BIST
, 1, 1, ea
&= ~m
;
3733 if (!C
) ea
|= m
); /* bist */
3734 OBITOP (O_BSTZ
, 1, 1, ea
&= ~m
;
3735 if (Z
) ea
|= m
); /* bstz */
3736 OBITOP (O_BISTZ
, 1, 1, ea
&= ~m
;
3737 if (!Z
) ea
|= m
); /* bistz */
3738 OBITOP (O_BAND
, 1, 0, c
= (ea
& m
) && C
); /* band */
3739 OBITOP (O_BIAND
, 1, 0, c
= !(ea
& m
) && C
); /* biand */
3740 OBITOP (O_BOR
, 1, 0, c
= (ea
& m
) || C
); /* bor */
3741 OBITOP (O_BIOR
, 1, 0, c
= !(ea
& m
) || C
); /* bior */
3742 OBITOP (O_BXOR
, 1, 0, c
= ((ea
& m
) != 0)!= C
); /* bxor */
3743 OBITOP (O_BIXOR
, 1, 0, c
= !(ea
& m
) != C
); /* bixor */
3745 case O (O_BFLD
, SB
): /* bfld */
3748 if (fetch (sd
, &code
->src
, &bit
))
3753 if (fetch (sd
, &code
->dst
, &ea
))
3763 if (store (sd
, &code
->op3
, ea
))
3768 case O(O_BFST
, SB
): /* bfst */
3769 /* bitfield store */
3770 /* NOTE: the imm8 value is in dst, and the ea value
3771 (which is actually the destination) is in op3.
3772 It has to be that way, to avoid breaking the assembler. */
3774 if (fetch (sd
, &code
->dst
, &bit
)) /* imm8 */
3776 if (bit
== 0) /* noop -- nothing to do. */
3779 if (fetch (sd
, &code
->src
, &rd
)) /* reg8 src */
3782 if (fetch2 (sd
, &code
->op3
, &ea
)) /* ea dst */
3785 /* Left-shift the register data into position. */
3786 for (tmp
= bit
; !(tmp
& 1); tmp
>>= 1)
3789 /* Combine it with the neighboring bits. */
3790 ea
= (ea
& ~bit
) | (rd
& bit
);
3793 if (store2 (sd
, &code
->op3
, ea
))
3797 case O (O_CLRMAC
, SN
): /* clrmac */
3798 h8_set_mach (sd
, 0);
3799 h8_set_macl (sd
, 0);
3800 h8_set_macZ (sd
, 1);
3801 h8_set_macV (sd
, 0);
3802 h8_set_macN (sd
, 0);
3805 case O (O_STMAC
, SL
): /* stmac, 260 */
3806 switch (code
->src
.type
) {
3807 case X (OP_MACH
, SL
):
3808 res
= h8_get_mach (sd
);
3809 if (res
& 0x200) /* sign extend */
3812 case X (OP_MACL
, SL
):
3813 res
= h8_get_macl (sd
);
3815 default: goto illegal
;
3817 nz
= !h8_get_macZ (sd
);
3818 n
= h8_get_macN (sd
);
3819 v
= h8_get_macV (sd
);
3821 if (store (sd
, &code
->dst
, res
))
3826 case O (O_LDMAC
, SL
): /* ldmac, 179 */
3827 if (fetch (sd
, &code
->src
, &rd
))
3830 switch (code
->dst
.type
) {
3831 case X (OP_MACH
, SL
):
3832 rd
&= 0x3ff; /* Truncate to 10 bits */
3833 h8_set_mach (sd
, rd
);
3835 case X (OP_MACL
, SL
):
3836 h8_set_macl (sd
, rd
);
3838 default: goto illegal
;
3840 h8_set_macV (sd
, 0);
3844 if (fetch (sd
, &code
->src
, &rd
) ||
3845 fetch (sd
, &code
->dst
, &res
))
3848 /* Ye gods, this is non-portable!
3849 However, the existing mul/div code is similar. */
3850 res
= SEXTSHORT (res
) * SEXTSHORT (rd
);
3852 if (h8_get_macS (sd
)) /* Saturating mode */
3854 long long mac
= h8_get_macl (sd
);
3856 if (mac
& 0x80000000) /* sign extend */
3857 mac
|= 0xffffffff00000000LL
;
3860 if (mac
> 0x7fffffff || mac
< 0xffffffff80000000LL
)
3861 h8_set_macV (sd
, 1);
3862 h8_set_macZ (sd
, (mac
== 0));
3863 h8_set_macN (sd
, (mac
< 0));
3864 h8_set_macl (sd
, (int) mac
);
3866 else /* "Less Saturating" mode */
3868 long long mac
= h8_get_mach (sd
);
3870 mac
+= h8_get_macl (sd
);
3872 if (mac
& 0x20000000000LL
) /* sign extend */
3873 mac
|= 0xfffffc0000000000LL
;
3876 if (mac
> 0x1ffffffffffLL
||
3877 mac
< (long long) 0xfffffe0000000000LL
)
3878 h8_set_macV (sd
, 1);
3879 h8_set_macZ (sd
, (mac
== 0));
3880 h8_set_macN (sd
, (mac
< 0));
3881 h8_set_macl (sd
, (int) mac
);
3883 h8_set_mach (sd
, (int) (mac
& 0x3ff));
3887 case O (O_MULS
, SW
): /* muls.w */
3888 if (fetch (sd
, &code
->src
, &ea
) ||
3889 fetch (sd
, &code
->dst
, &rd
))
3892 ea
= SEXTSHORT (ea
);
3893 res
= SEXTSHORT (ea
* SEXTSHORT (rd
));
3897 if (store (sd
, &code
->dst
, res
))
3902 case O (O_MULS
, SL
): /* muls.l */
3903 if (fetch (sd
, &code
->src
, &ea
) ||
3904 fetch (sd
, &code
->dst
, &rd
))
3909 n
= res
& 0x80000000;
3910 nz
= res
& 0xffffffff;
3911 if (store (sd
, &code
->dst
, res
))
3915 case O (O_MULSU
, SL
): /* muls/u.l */
3916 if (fetch (sd
, &code
->src
, &ea
) ||
3917 fetch (sd
, &code
->dst
, &rd
))
3920 /* Compute upper 32 bits of the 64-bit result. */
3921 res
= (((long long) ea
) * ((long long) rd
)) >> 32;
3923 n
= res
& 0x80000000;
3924 nz
= res
& 0xffffffff;
3925 if (store (sd
, &code
->dst
, res
))
3929 case O (O_MULU
, SW
): /* mulu.w */
3930 if (fetch (sd
, &code
->src
, &ea
) ||
3931 fetch (sd
, &code
->dst
, &rd
))
3934 res
= UEXTSHORT ((UEXTSHORT (ea
) * UEXTSHORT (rd
)));
3936 /* Don't set Z or N. */
3937 if (store (sd
, &code
->dst
, res
))
3942 case O (O_MULU
, SL
): /* mulu.l */
3943 if (fetch (sd
, &code
->src
, &ea
) ||
3944 fetch (sd
, &code
->dst
, &rd
))
3949 /* Don't set Z or N. */
3950 if (store (sd
, &code
->dst
, res
))
3955 case O (O_MULUU
, SL
): /* mulu/u.l */
3956 if (fetch (sd
, &code
->src
, &ea
) ||
3957 fetch (sd
, &code
->dst
, &rd
))
3960 /* Compute upper 32 bits of the 64-bit result. */
3961 res
= (((unsigned long long) (unsigned) ea
) *
3962 ((unsigned long long) (unsigned) rd
)) >> 32;
3964 /* Don't set Z or N. */
3965 if (store (sd
, &code
->dst
, res
))
3970 case O (O_MULXS
, SB
): /* mulxs.b */
3971 if (fetch (sd
, &code
->src
, &ea
) ||
3972 fetch (sd
, &code
->dst
, &rd
))
3976 res
= ea
* SEXTCHAR (rd
);
3980 if (store (sd
, &code
->dst
, res
))
3985 case O (O_MULXS
, SW
): /* mulxs.w */
3986 if (fetch (sd
, &code
->src
, &ea
) ||
3987 fetch (sd
, &code
->dst
, &rd
))
3990 ea
= SEXTSHORT (ea
);
3991 res
= ea
* SEXTSHORT (rd
& 0xffff);
3993 n
= res
& 0x80000000;
3994 nz
= res
& 0xffffffff;
3995 if (store (sd
, &code
->dst
, res
))
4000 case O (O_MULXU
, SB
): /* mulxu.b */
4001 if (fetch (sd
, &code
->src
, &ea
) ||
4002 fetch (sd
, &code
->dst
, &rd
))
4005 res
= UEXTCHAR (ea
) * UEXTCHAR (rd
);
4007 if (store (sd
, &code
->dst
, res
))
4012 case O (O_MULXU
, SW
): /* mulxu.w */
4013 if (fetch (sd
, &code
->src
, &ea
) ||
4014 fetch (sd
, &code
->dst
, &rd
))
4017 res
= UEXTSHORT (ea
) * UEXTSHORT (rd
);
4019 if (store (sd
, &code
->dst
, res
))
4024 case O (O_TAS
, SB
): /* tas (test and set) */
4025 if (!h8300sxmode
) /* h8sx can use any register. */
4026 switch (code
->src
.reg
)
4037 if (fetch (sd
, &code
->src
, &res
))
4039 if (store (sd
, &code
->src
, res
| 0x80))
4042 goto just_flags_log8
;
4044 case O (O_DIVU
, SW
): /* divu.w */
4045 if (fetch (sd
, &code
->src
, &ea
) ||
4046 fetch (sd
, &code
->dst
, &rd
))
4052 res
= (unsigned) (UEXTSHORT (rd
) / UEXTSHORT (ea
));
4056 if (store (sd
, &code
->dst
, res
))
4060 case O (O_DIVU
, SL
): /* divu.l */
4061 if (fetch (sd
, &code
->src
, &ea
) ||
4062 fetch (sd
, &code
->dst
, &rd
))
4065 n
= ea
& 0x80000000;
4066 nz
= ea
& 0xffffffff;
4068 res
= (unsigned) rd
/ ea
;
4072 if (store (sd
, &code
->dst
, res
))
4076 case O (O_DIVS
, SW
): /* divs.w */
4077 if (fetch (sd
, &code
->src
, &ea
) ||
4078 fetch (sd
, &code
->dst
, &rd
))
4083 res
= SEXTSHORT (rd
) / SEXTSHORT (ea
);
4093 if (store (sd
, &code
->dst
, res
))
4097 case O (O_DIVS
, SL
): /* divs.l */
4098 if (fetch (sd
, &code
->src
, &ea
) ||
4099 fetch (sd
, &code
->dst
, &rd
))
4113 n
= res
& 0x80000000;
4114 if (store (sd
, &code
->dst
, res
))
4118 case O (O_DIVXU
, SB
): /* divxu.b */
4119 if (fetch (sd
, &code
->src
, &ea
) ||
4120 fetch (sd
, &code
->dst
, &rd
))
4123 rd
= UEXTSHORT (rd
);
4130 tmp
= (unsigned) rd
% ea
;
4131 res
= (unsigned) rd
/ ea
;
4139 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
4143 case O (O_DIVXU
, SW
): /* divxu.w */
4144 if (fetch (sd
, &code
->src
, &ea
) ||
4145 fetch (sd
, &code
->dst
, &rd
))
4148 ea
= UEXTSHORT (ea
);
4154 tmp
= (unsigned) rd
% ea
;
4155 res
= (unsigned) rd
/ ea
;
4163 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
4167 case O (O_DIVXS
, SB
): /* divxs.b */
4168 if (fetch (sd
, &code
->src
, &ea
) ||
4169 fetch (sd
, &code
->dst
, &rd
))
4172 rd
= SEXTSHORT (rd
);
4177 tmp
= (int) rd
% (int) ea
;
4178 res
= (int) rd
/ (int) ea
;
4189 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
4193 case O (O_DIVXS
, SW
): /* divxs.w */
4194 if (fetch (sd
, &code
->src
, &ea
) ||
4195 fetch (sd
, &code
->dst
, &rd
))
4198 ea
= SEXTSHORT (ea
);
4202 tmp
= (int) rd
% (int) ea
;
4203 res
= (int) rd
/ (int) ea
;
4213 n
= res
& 0x80000000;
4214 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
4218 case O (O_EXTS
, SW
): /* exts.w, signed extend */
4219 if (fetch2 (sd
, &code
->dst
, &rd
))
4221 ea
= rd
& 0x80 ? -256 : 0;
4222 res
= (rd
& 0xff) + ea
;
4225 case O (O_EXTS
, SL
): /* exts.l, signed extend */
4226 if (fetch2 (sd
, &code
->dst
, &rd
))
4228 if (code
->src
.type
== X (OP_IMM
, SL
))
4230 if (fetch (sd
, &code
->src
, &ea
))
4233 if (ea
== 2) /* exts.l #2, nn */
4235 /* Sign-extend from 8-bit to 32-bit. */
4236 ea
= rd
& 0x80 ? -256 : 0;
4237 res
= (rd
& 0xff) + ea
;
4241 /* Sign-extend from 16-bit to 32-bit. */
4242 ea
= rd
& 0x8000 ? -65536 : 0;
4243 res
= (rd
& 0xffff) + ea
;
4246 case O (O_EXTU
, SW
): /* extu.w, unsigned extend */
4247 if (fetch2 (sd
, &code
->dst
, &rd
))
4250 res
= (rd
& 0xff) + ea
;
4253 case O (O_EXTU
, SL
): /* extu.l, unsigned extend */
4254 if (fetch2 (sd
, &code
->dst
, &rd
))
4256 if (code
->src
.type
== X (OP_IMM
, SL
))
4258 if (fetch (sd
, &code
->src
, &ea
))
4261 if (ea
== 2) /* extu.l #2, nn */
4263 /* Zero-extend from 8-bit to 32-bit. */
4265 res
= (rd
& 0xff) + ea
;
4269 /* Zero-extend from 16-bit to 32-bit. */
4271 res
= (rd
& 0xffff) + ea
;
4274 case O (O_NOP
, SN
): /* nop */
4277 case O (O_STM
, SL
): /* stm, store to memory */
4279 int nregs
, firstreg
, i
;
4281 nregs
= GET_MEMORY_B (pc
+ 1);
4284 firstreg
= code
->src
.reg
;
4286 for (i
= firstreg
; i
<= firstreg
+ nregs
; i
++)
4288 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) - 4);
4289 SET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
), h8_get_reg (sd
, i
));
4294 case O (O_LDM
, SL
): /* ldm, load from memory */
4295 case O (O_RTEL
, SN
): /* rte/l, ldm plus rte */
4296 case O (O_RTSL
, SN
): /* rts/l, ldm plus rts */
4298 int nregs
, firstreg
, i
;
4300 nregs
= ((GET_MEMORY_B (pc
+ 1) >> 4) & 0xf);
4301 firstreg
= code
->dst
.reg
& 0xf;
4302 for (i
= firstreg
; i
>= firstreg
- nregs
; i
--)
4304 h8_set_reg (sd
, i
, GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
)));
4305 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) + 4);
4308 switch (code
->opcode
) {
4309 case O (O_RTEL
, SN
):
4311 case O (O_RTSL
, SN
):
4320 /* Decimal Adjust Addition. This is for BCD arithmetic. */
4321 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch? */
4322 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4323 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4324 res
= res
; /* Value added == 0. */
4325 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4326 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4327 res
= res
+ 0x6; /* Value added == 6. */
4328 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4329 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4330 res
= res
+ 0x6; /* Value added == 6. */
4331 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4332 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4333 res
= res
+ 0x60; /* Value added == 60. */
4334 else if (!c
&& (9 <= (res
>> 4) && (res
>> 4) <= 15) &&
4335 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4336 res
= res
+ 0x66; /* Value added == 66. */
4337 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4338 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4339 res
= res
+ 0x66; /* Value added == 66. */
4340 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4341 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4342 res
= res
+ 0x60; /* Value added == 60. */
4343 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4344 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4345 res
= res
+ 0x66; /* Value added == 66. */
4346 else if (c
&& (1 <= (res
>> 4) && (res
>> 4) <= 3) &&
4347 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4348 res
= res
+ 0x66; /* Value added == 66. */
4353 /* Decimal Adjust Subtraction. This is for BCD arithmetic. */
4354 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch, fetch2... */
4355 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4356 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4357 res
= res
; /* Value added == 0. */
4358 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4359 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4360 res
= res
+ 0xfa; /* Value added == 0xfa. */
4361 else if ( c
&& (7 <= (res
>> 4) && (res
>> 4) <= 15) &&
4362 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4363 res
= res
+ 0xa0; /* Value added == 0xa0. */
4364 else if (c
&& (6 <= (res
>> 4) && (res
>> 4) <= 15) &&
4365 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4366 res
= res
+ 0x9a; /* Value added == 0x9a. */
4372 sim_engine_set_run_state (sd
, sim_stopped
, SIGILL
);
4377 (*sim_callback
->printf_filtered
) (sim_callback
,
4378 "sim_resume: internal error.\n");
4379 sim_engine_set_run_state (sd
, sim_stopped
, SIGILL
);
4383 if (code
->dst
.type
== X (OP_CCR
, SB
) ||
4384 code
->dst
.type
== X (OP_CCR
, SW
))
4386 h8_set_ccr (sd
, res
);
4389 else if (h8300smode
&&
4390 (code
->dst
.type
== X (OP_EXR
, SB
) ||
4391 code
->dst
.type
== X (OP_EXR
, SW
)))
4393 h8_set_exr (sd
, res
);
4394 if (h8300smode
) /* Get exr. */
4396 trace
= (h8_get_exr (sd
) >> 7) & 1;
4397 intMask
= h8_get_exr (sd
) & 7;
4406 /* When a branch works */
4407 if (fetch (sd
, &code
->src
, &res
))
4409 if (res
& 1) /* bad address */
4411 pc
= code
->next_pc
+ res
;
4414 /* Set the cond codes from res */
4417 /* Set the flags after an 8 bit inc/dec operation */
4421 v
= (rd
& 0x7f) == 0x7f;
4424 /* Set the flags after an 16 bit inc/dec operation */
4428 v
= (rd
& 0x7fff) == 0x7fff;
4431 /* Set the flags after an 32 bit inc/dec operation */
4433 n
= res
& 0x80000000;
4434 nz
= res
& 0xffffffff;
4435 v
= (rd
& 0x7fffffff) == 0x7fffffff;
4439 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
4442 if (store2 (sd
, &code
->dst
, rd
))
4447 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
4450 if (store2 (sd
, &code
->dst
, rd
))
4455 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
4456 n
= (rd
& 0x80000000);
4457 nz
= rd
& 0xffffffff;
4458 if (store2 (sd
, &code
->dst
, rd
))
4463 if (store2 (sd
, &code
->dst
, res
))
4467 /* flags after a 32bit logical operation */
4468 n
= res
& 0x80000000;
4469 nz
= res
& 0xffffffff;
4474 if (store2 (sd
, &code
->dst
, res
))
4478 /* flags after a 16bit logical operation */
4485 if (store2 (sd
, &code
->dst
, res
))
4495 if (store2 (sd
, &code
->dst
, res
))
4502 switch (code
->opcode
/ 4)
4506 v
= ((rd
& 0x80) == (ea
& 0x80)
4507 && (rd
& 0x80) != (res
& 0x80));
4512 v
= ((rd
& 0x80) != (-ea
& 0x80)
4513 && (rd
& 0x80) != (res
& 0x80));
4520 break; /* No effect on v flag. */
4525 if (store2 (sd
, &code
->dst
, res
))
4531 c
= (res
& 0x10000);
4532 switch (code
->opcode
/ 4)
4536 v
= ((rd
& 0x8000) == (ea
& 0x8000)
4537 && (rd
& 0x8000) != (res
& 0x8000));
4542 v
= ((rd
& 0x8000) != (-ea
& 0x8000)
4543 && (rd
& 0x8000) != (res
& 0x8000));
4552 if (store2 (sd
, &code
->dst
, res
))
4556 n
= res
& 0x80000000;
4557 nz
= res
& 0xffffffff;
4558 switch (code
->opcode
/ 4)
4562 v
= ((rd
& 0x80000000) == (ea
& 0x80000000)
4563 && (rd
& 0x80000000) != (res
& 0x80000000));
4564 c
= ((unsigned) res
< (unsigned) rd
) ||
4565 ((unsigned) res
< (unsigned) ea
);
4570 v
= ((rd
& 0x80000000) != (-ea
& 0x80000000)
4571 && (rd
& 0x80000000) != (res
& 0x80000000));
4572 c
= (unsigned) rd
< (unsigned) -ea
;
4575 v
= (rd
== 0x80000000);
4582 if ((res
= h8_get_delayed_branch (sd
)) != 0)
4585 h8_set_delayed_branch (sd
, 0);
4592 if (--poll_count
< 0)
4594 poll_count
= POLL_QUIT_INTERVAL
;
4595 if ((*sim_callback
->poll_quit
) != NULL
4596 && (*sim_callback
->poll_quit
) (sim_callback
))
4597 sim_engine_set_run_state (sd
, sim_stopped
, SIGINT
);
4599 sim_engine_get_run_state (sd
, &reason
, &sigrc
);
4600 } while (reason
== sim_running
);
4602 h8_set_ticks (sd
, h8_get_ticks (sd
) + get_now () - tick_start
);
4603 h8_set_cycles (sd
, h8_get_cycles (sd
) + cycles
);
4604 h8_set_insts (sd
, h8_get_insts (sd
) + insts
);
4609 h8_set_exr (sd
, (trace
<<7) | intMask
);
4611 h8_set_mask (sd
, oldmask
);
4612 signal (SIGINT
, prev
);
4616 sim_trace (SIM_DESC sd
)
4618 /* FIXME: Unfinished. */
4619 (*sim_callback
->printf_filtered
) (sim_callback
,
4620 "sim_trace: trace not supported.\n");
4621 return 1; /* Done. */
4625 sim_write (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
4632 for (i
= 0; i
< size
; i
++)
4634 if (addr
< memory_size
)
4636 h8_set_memory (sd
, addr
+ i
, buffer
[i
]);
4637 h8_set_cache_idx (sd
, addr
+ i
, 0);
4641 h8_set_eightbit (sd
, (addr
+ i
) & 0xff, buffer
[i
]);
4648 sim_read (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
4653 if (addr
< memory_size
)
4654 memcpy (buffer
, h8_get_memory_buf (sd
) + addr
, size
);
4656 memcpy (buffer
, h8_get_eightbit_buf (sd
) + (addr
& 0xff), size
);
4662 sim_store_register (SIM_DESC sd
, int rn
, unsigned char *value
, int length
)
4667 longval
= (value
[0] << 24) | (value
[1] << 16) | (value
[2] << 8) | value
[3];
4668 shortval
= (value
[0] << 8) | (value
[1]);
4669 intval
= h8300hmode
? longval
: shortval
;
4675 if(h8300_normal_mode
)
4676 h8_set_pc (sd
, shortval
); /* PC for Normal mode is 2 bytes */
4678 h8_set_pc (sd
, intval
);
4681 (*sim_callback
->printf_filtered
) (sim_callback
,
4682 "sim_store_register: bad regnum %d.\n",
4692 h8_set_reg (sd
, rn
, intval
);
4695 h8_set_ccr (sd
, intval
);
4698 h8_set_exr (sd
, intval
);
4701 h8_set_sbr (sd
, intval
);
4704 h8_set_vbr (sd
, intval
);
4707 h8_set_mach (sd
, intval
);
4710 h8_set_macl (sd
, intval
);
4713 h8_set_cycles (sd
, longval
);
4717 h8_set_insts (sd
, longval
);
4721 h8_set_ticks (sd
, longval
);
4728 sim_fetch_register (SIM_DESC sd
, int rn
, unsigned char *buf
, int length
)
4735 if (!h8300smode
&& rn
>= EXR_REGNUM
)
4740 (*sim_callback
->printf_filtered
) (sim_callback
,
4741 "sim_fetch_register: bad regnum %d.\n",
4746 v
= h8_get_ccr (sd
);
4749 v
= h8_get_exr (sd
);
4755 v
= h8_get_sbr (sd
);
4758 v
= h8_get_vbr (sd
);
4761 v
= h8_get_mach (sd
);
4764 v
= h8_get_macl (sd
);
4774 v
= h8_get_reg (sd
, rn
);
4777 v
= h8_get_cycles (sd
);
4781 v
= h8_get_ticks (sd
);
4785 v
= h8_get_insts (sd
);
4789 /* In Normal mode PC is 2 byte, but other registers are 4 byte */
4790 if ((h8300hmode
|| longreg
) && !(rn
== PC_REGNUM
&& h8300_normal_mode
))
4806 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason
, int *sigrc
)
4808 sim_engine_get_run_state (sd
, reason
, sigrc
);
4811 /* FIXME: Rename to sim_set_mem_size. */
4816 /* Memory size is fixed. */
4820 set_simcache_size (SIM_DESC sd
, int n
)
4823 free (sd
->sim_cache
);
4826 sd
->sim_cache
= (decoded_inst
*) malloc (sizeof (decoded_inst
) * n
);
4827 memset (sd
->sim_cache
, 0, sizeof (decoded_inst
) * n
);
4828 sd
->sim_cache_size
= n
;
4833 sim_info (SIM_DESC sd
, int verbose
)
4835 double timetaken
= (double) h8_get_ticks (sd
) / (double) now_persec ();
4836 double virttime
= h8_get_cycles (sd
) / 10.0e6
;
4838 (*sim_callback
->printf_filtered
) (sim_callback
,
4839 "\n\n#instructions executed %10d\n",
4841 (*sim_callback
->printf_filtered
) (sim_callback
,
4842 "#cycles (v approximate) %10d\n",
4843 h8_get_cycles (sd
));
4844 (*sim_callback
->printf_filtered
) (sim_callback
,
4845 "#real time taken %10.4f\n",
4847 (*sim_callback
->printf_filtered
) (sim_callback
,
4848 "#virtual time taken %10.4f\n",
4850 if (timetaken
!= 0.0)
4851 (*sim_callback
->printf_filtered
) (sim_callback
,
4852 "#simulation ratio %10.4f\n",
4853 virttime
/ timetaken
);
4854 (*sim_callback
->printf_filtered
) (sim_callback
,
4856 h8_get_compiles (sd
));
4857 (*sim_callback
->printf_filtered
) (sim_callback
,
4858 "#cache size %10d\n",
4859 sd
->sim_cache_size
);
4862 /* This to be conditional on `what' (aka `verbose'),
4863 however it was never passed as non-zero. */
4867 for (i
= 0; i
< O_LAST
; i
++)
4869 if (h8_get_stats (sd
, i
))
4870 (*sim_callback
->printf_filtered
) (sim_callback
, "%d: %d\n",
4871 i
, h8_get_stats (sd
, i
));
4877 /* Indicate whether the cpu is an H8/300 or H8/300H.
4878 FLAG is non-zero for the H8/300H. */
4881 set_h8300h (unsigned long machine
)
4883 /* FIXME: Much of the code in sim_load can be moved to sim_open.
4884 This function being replaced by a sim_open:ARGV configuration
4887 h8300hmode
= h8300smode
= h8300sxmode
= h8300_normal_mode
= 0;
4889 if (machine
== bfd_mach_h8300sx
|| machine
== bfd_mach_h8300sxn
)
4892 if (machine
== bfd_mach_h8300s
|| machine
== bfd_mach_h8300sn
|| h8300sxmode
)
4895 if (machine
== bfd_mach_h8300h
|| machine
== bfd_mach_h8300hn
|| h8300smode
)
4898 if(machine
== bfd_mach_h8300hn
|| machine
== bfd_mach_h8300sn
|| machine
== bfd_mach_h8300sxn
)
4899 h8300_normal_mode
= 1;
4902 /* Cover function of sim_state_free to free the cpu buffers as well. */
4905 free_state (SIM_DESC sd
)
4907 if (STATE_MODULES (sd
) != NULL
)
4908 sim_module_uninstall (sd
);
4910 /* Fixme: free buffers in _sim_cpu. */
4911 sim_state_free (sd
);
4915 sim_open (SIM_OPEN_KIND kind
,
4916 struct host_callback_struct
*callback
,
4923 sd
= sim_state_alloc (kind
, callback
);
4924 sd
->cpu
= sim_cpu_alloc (sd
, 0);
4925 cpu
= STATE_CPU (sd
, 0);
4926 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4927 sim_state_initialize (sd
, cpu
);
4928 /* sim_cpu object is new, so some initialization is needed. */
4929 init_pointers_needed
= 1;
4931 /* For compatibility (FIXME: is this right?). */
4932 current_alignment
= NONSTRICT_ALIGNMENT
;
4933 current_target_byte_order
= BIG_ENDIAN
;
4935 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
4941 /* getopt will print the error message so we just have to exit if
4942 this fails. FIXME: Hmmm... in the case of gdb we need getopt
4943 to call print_filtered. */
4944 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
4946 /* Uninstall the modules to avoid memory leaks,
4947 file descriptor leaks, etc. */
4952 /* Check for/establish the a reference program image. */
4953 if (sim_analyze_program (sd
,
4954 (STATE_PROG_ARGV (sd
) != NULL
4955 ? *STATE_PROG_ARGV (sd
)
4956 : NULL
), abfd
) != SIM_RC_OK
)
4962 /* Establish any remaining configuration options. */
4963 if (sim_config (sd
) != SIM_RC_OK
)
4969 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
4971 /* Uninstall the modules to avoid memory leaks,
4972 file descriptor leaks, etc. */
4977 /* sim_hw_configure (sd); */
4979 /* FIXME: Much of the code in sim_load can be moved here. */
4983 sim_callback
= callback
;
4988 sim_close (SIM_DESC sd
, int quitting
)
4990 /* Nothing to do. */
4993 /* Called by gdb to load a program into memory. */
4996 sim_load (SIM_DESC sd
, char *prog
, bfd
*abfd
, int from_tty
)
5000 /* FIXME: The code below that sets a specific variant of the H8/300
5001 being simulated should be moved to sim_open(). */
5003 /* See if the file is for the H8/300 or H8/300H. */
5004 /* ??? This may not be the most efficient way. The z8k simulator
5005 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
5009 prog_bfd
= bfd_openr (prog
, NULL
);
5010 if (prog_bfd
!= NULL
)
5012 /* Set the cpu type. We ignore failure from bfd_check_format
5013 and bfd_openr as sim_load_file checks too. */
5014 if (bfd_check_format (prog_bfd
, bfd_object
))
5016 set_h8300h (bfd_get_mach (prog_bfd
));
5020 /* If we're using gdb attached to the simulator, then we have to
5021 reallocate memory for the simulator.
5023 When gdb first starts, it calls fetch_registers (among other
5024 functions), which in turn calls init_pointers, which allocates
5027 The problem is when we do that, we don't know whether we're
5028 debugging an H8/300 or H8/300H program.
5030 This is the first point at which we can make that determination,
5031 so we just reallocate memory now; this will also allow us to handle
5032 switching between H8/300 and H8/300H programs without exiting
5035 if (h8300smode
&& !h8300_normal_mode
)
5036 memory_size
= H8300S_MSIZE
;
5037 else if (h8300hmode
&& !h8300_normal_mode
)
5038 memory_size
= H8300H_MSIZE
;
5040 memory_size
= H8300_MSIZE
;
5042 if (h8_get_memory_buf (sd
))
5043 free (h8_get_memory_buf (sd
));
5044 if (h8_get_cache_idx_buf (sd
))
5045 free (h8_get_cache_idx_buf (sd
));
5046 if (h8_get_eightbit_buf (sd
))
5047 free (h8_get_eightbit_buf (sd
));
5049 h8_set_memory_buf (sd
, (unsigned char *)
5050 calloc (sizeof (char), memory_size
));
5051 h8_set_cache_idx_buf (sd
, (unsigned short *)
5052 calloc (sizeof (short), memory_size
));
5053 sd
->memory_size
= memory_size
;
5054 h8_set_eightbit_buf (sd
, (unsigned char *) calloc (sizeof (char), 256));
5056 /* `msize' must be a power of two. */
5057 if ((memory_size
& (memory_size
- 1)) != 0)
5059 (*sim_callback
->printf_filtered
) (sim_callback
,
5060 "sim_load: bad memory size.\n");
5063 h8_set_mask (sd
, memory_size
- 1);
5065 if (sim_load_file (sd
, myname
, sim_callback
, prog
, prog_bfd
,
5066 sim_kind
== SIM_OPEN_DEBUG
,
5070 /* Close the bfd if we opened it. */
5071 if (abfd
== NULL
&& prog_bfd
!= NULL
)
5072 bfd_close (prog_bfd
);
5076 /* Close the bfd if we opened it. */
5077 if (abfd
== NULL
&& prog_bfd
!= NULL
)
5078 bfd_close (prog_bfd
);
5083 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
, char **argv
, char **env
)
5090 h8_set_pc (sd
, bfd_get_start_address (abfd
));
5094 /* Command Line support. */
5097 /* Counting the no. of commandline arguments. */
5098 for (no_of_args
= 0; argv
[no_of_args
] != NULL
; no_of_args
++)
5101 /* Allocating memory for the argv pointers. */
5102 h8_set_command_line (sd
, (char **) malloc ((sizeof (char *))
5103 * (no_of_args
+ 1)));
5105 for (i
= 0; i
< no_of_args
; i
++)
5107 /* Copying the argument string. */
5108 h8_set_cmdline_arg (sd
, i
, (char *) strdup (argv
[i
]));
5110 h8_set_cmdline_arg (sd
, i
, NULL
);
5117 sim_do_command (SIM_DESC sd
, char *cmd
)
5119 (*sim_callback
->printf_filtered
) (sim_callback
,
5120 "This simulator does not accept any commands.\n");
5124 sim_set_callbacks (struct host_callback_struct
*ptr
)