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 ? 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))
515 static int memory_size
;
520 return time (0); /* WinXX HAS UNIX like 'time', so why not use it? */
542 return h8300hmode
? SL
: SW
;
547 /* Simulate an indirection / dereference.
548 return 0 for success, -1 for failure.
552 lvalue (SIM_DESC sd
, int x
, int rn
, unsigned int *val
)
554 if (val
== NULL
) /* Paranoia. */
560 if (rn
== ZERO_REGNUM
)
561 *val
= X (OP_IMM
, SP
);
563 *val
= X (OP_REG
, SP
);
566 *val
= X (OP_MEM
, SP
);
569 sim_engine_set_run_state (sd
, sim_stopped
, SIGSEGV
);
587 decode (SIM_DESC sd
, int addr
, unsigned char *data
, decoded_inst
*dst
)
589 int cst
[3] = {0, 0, 0};
590 int reg
[3] = {0, 0, 0};
591 int rdisp
[3] = {0, 0, 0};
593 const struct h8_opcode
*q
;
598 /* Find the exact opcode/arg combo. */
599 for (q
= h8_opcodes
; q
->name
; q
++)
601 op_type
*nib
= q
->data
.nib
;
602 unsigned int len
= 0;
604 if ((q
->available
== AV_H8SX
&& !h8300sxmode
) ||
605 (q
->available
== AV_H8S
&& !h8300smode
) ||
606 (q
->available
== AV_H8H
&& !h8300hmode
))
609 cst
[0] = cst
[1] = cst
[2] = 0;
610 reg
[0] = reg
[1] = reg
[2] = 0;
611 rdisp
[0] = rdisp
[1] = rdisp
[2] = 0;
615 op_type looking_for
= *nib
;
616 int thisnib
= data
[len
/ 2];
618 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
619 opnum
= ((looking_for
& OP3
) ? 2 :
620 (looking_for
& DST
) ? 1 : 0);
622 if (looking_for
< 16 && looking_for
>= 0)
624 if (looking_for
!= thisnib
)
629 if (looking_for
& B31
)
631 if (!((thisnib
& 0x8) != 0))
634 looking_for
= (op_type
) (looking_for
& ~B31
);
637 else if (looking_for
& B30
)
639 if (!((thisnib
& 0x8) == 0))
642 looking_for
= (op_type
) (looking_for
& ~B30
);
645 if (looking_for
& B21
)
647 if (!((thisnib
& 0x4) != 0))
650 looking_for
= (op_type
) (looking_for
& ~B21
);
653 else if (looking_for
& B20
)
655 if (!((thisnib
& 0x4) == 0))
658 looking_for
= (op_type
) (looking_for
& ~B20
);
661 if (looking_for
& B11
)
663 if (!((thisnib
& 0x2) != 0))
666 looking_for
= (op_type
) (looking_for
& ~B11
);
669 else if (looking_for
& B10
)
671 if (!((thisnib
& 0x2) == 0))
674 looking_for
= (op_type
) (looking_for
& ~B10
);
677 if (looking_for
& B01
)
679 if (!((thisnib
& 0x1) != 0))
682 looking_for
= (op_type
) (looking_for
& ~B01
);
685 else if (looking_for
& B00
)
687 if (!((thisnib
& 0x1) == 0))
690 looking_for
= (op_type
) (looking_for
& ~B00
);
693 if (looking_for
& IGNORE
)
695 /* Hitachi has declared that IGNORE must be zero. */
699 else if ((looking_for
& MODE
) == DATA
)
701 ; /* Skip embedded data. */
703 else if ((looking_for
& MODE
) == DBIT
)
705 /* Exclude adds/subs by looking at bit 0 and 2, and
706 make sure the operand size, either w or l,
707 matches by looking at bit 1. */
708 if ((looking_for
& 7) != (thisnib
& 7))
711 cst
[opnum
] = (thisnib
& 0x8) ? 2 : 1;
713 else if ((looking_for
& MODE
) == REG
||
714 (looking_for
& MODE
) == LOWREG
||
715 (looking_for
& MODE
) == IND
||
716 (looking_for
& MODE
) == PREINC
||
717 (looking_for
& MODE
) == POSTINC
||
718 (looking_for
& MODE
) == PREDEC
||
719 (looking_for
& MODE
) == POSTDEC
)
721 reg
[opnum
] = thisnib
;
723 else if (looking_for
& CTRL
)
726 if (((looking_for
& MODE
) == CCR
&& (thisnib
!= C_CCR
)) ||
727 ((looking_for
& MODE
) == EXR
&& (thisnib
!= C_EXR
)) ||
728 ((looking_for
& MODE
) == MACH
&& (thisnib
!= C_MACH
)) ||
729 ((looking_for
& MODE
) == MACL
&& (thisnib
!= C_MACL
)) ||
730 ((looking_for
& MODE
) == VBR
&& (thisnib
!= C_VBR
)) ||
731 ((looking_for
& MODE
) == SBR
&& (thisnib
!= C_SBR
)))
733 if (((looking_for
& MODE
) == CCR_EXR
&&
734 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
)) ||
735 ((looking_for
& MODE
) == VBR_SBR
&&
736 (thisnib
!= C_VBR
&& thisnib
!= C_SBR
)) ||
737 ((looking_for
& MODE
) == MACREG
&&
738 (thisnib
!= C_MACH
&& thisnib
!= C_MACL
)))
740 if (((looking_for
& MODE
) == CC_EX_VB_SB
&&
741 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
&&
742 thisnib
!= C_VBR
&& thisnib
!= C_SBR
)))
745 reg
[opnum
] = thisnib
;
747 else if ((looking_for
& MODE
) == ABS
)
749 /* Absolute addresses are unsigned. */
750 switch (looking_for
& SIZE
)
753 cst
[opnum
] = UEXTCHAR (data
[len
/ 2]);
757 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
761 (data
[len
/ 2 + 0] << 24) +
762 (data
[len
/ 2 + 1] << 16) +
763 (data
[len
/ 2 + 2] << 8) +
767 printf ("decode: bad size ABS: %d\n",
768 (looking_for
& SIZE
));
772 else if ((looking_for
& MODE
) == DISP
||
773 (looking_for
& MODE
) == PCREL
||
774 (looking_for
& MODE
) == INDEXB
||
775 (looking_for
& MODE
) == INDEXW
||
776 (looking_for
& MODE
) == INDEXL
)
778 switch (looking_for
& SIZE
)
781 cst
[opnum
] = thisnib
& 3;
784 cst
[opnum
] = SEXTCHAR (data
[len
/ 2]);
787 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
788 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
791 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
795 (data
[len
/ 2 + 0] << 24) +
796 (data
[len
/ 2 + 1] << 16) +
797 (data
[len
/ 2 + 2] << 8) +
801 printf ("decode: bad size DISP/PCREL/INDEX: %d\n",
802 (looking_for
& SIZE
));
806 else if ((looking_for
& SIZE
) == L_16
||
807 (looking_for
& SIZE
) == L_16U
)
809 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
810 /* Immediates are always unsigned. */
811 if ((looking_for
& SIZE
) != L_16U
&&
812 (looking_for
& MODE
) != IMM
)
813 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
815 else if (looking_for
& ABSJMP
)
817 switch (looking_for
& SIZE
) {
819 cst
[opnum
] = (data
[1] << 16) | (data
[2] << 8) | (data
[3]);
823 (data
[len
/ 2 + 0] << 24) +
824 (data
[len
/ 2 + 1] << 16) +
825 (data
[len
/ 2 + 2] << 8) +
829 printf ("decode: bad size ABSJMP: %d\n",
830 (looking_for
& SIZE
));
834 else if ((looking_for
& MODE
) == MEMIND
)
836 cst
[opnum
] = data
[1];
838 else if ((looking_for
& MODE
) == VECIND
)
840 /* FIXME: Multiplier should be 2 for "normal" mode. */
841 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 4;
842 cst
[opnum
] += h8_get_vbr (sd
); /* Add vector base reg. */
844 else if ((looking_for
& SIZE
) == L_32
)
849 (data
[i
+ 0] << 24) |
850 (data
[i
+ 1] << 16) |
854 else if ((looking_for
& SIZE
) == L_24
)
859 (data
[i
+ 0] << 16) |
863 else if (looking_for
& DISPREG
)
865 rdisp
[opnum
] = thisnib
& 0x7;
867 else if ((looking_for
& MODE
) == KBIT
)
884 else if ((looking_for
& SIZE
) == L_8
)
886 if ((looking_for
& MODE
) == ABS
)
888 /* Will be combined with contents of SBR_REGNUM
889 by fetch (). For all modes except h8sx, this
890 will always contain the value 0xFFFFFF00. */
891 cst
[opnum
] = data
[len
/ 2] & 0xff;
895 cst
[opnum
] = data
[len
/ 2] & 0xff;
898 else if ((looking_for
& SIZE
) == L_2
)
900 cst
[opnum
] = thisnib
& 3;
902 else if ((looking_for
& SIZE
) == L_3
||
903 (looking_for
& SIZE
) == L_3NZ
)
905 cst
[opnum
] = thisnib
& 7;
906 if (cst
[opnum
] == 0 && (looking_for
& SIZE
) == L_3NZ
)
909 else if ((looking_for
& SIZE
) == L_4
)
911 cst
[opnum
] = thisnib
& 15;
913 else if ((looking_for
& SIZE
) == L_5
)
915 cst
[opnum
] = data
[len
/ 2] & 0x1f;
917 else if (looking_for
== E
)
922 /* Fill in the args. */
924 op_type
*args
= q
->args
.nib
;
929 nargs
< 3 && *args
!= E
;
935 opnum
= ((x
& OP3
) ? 2 :
944 if ((x
& MODE
) == IMM
||
945 (x
& MODE
) == KBIT
||
948 /* Use the instruction to determine
950 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
951 p
->literal
= cst
[opnum
];
953 else if ((x
& MODE
) == CONST_2
||
954 (x
& MODE
) == CONST_4
||
955 (x
& MODE
) == CONST_8
||
956 (x
& MODE
) == CONST_16
)
958 /* Use the instruction to determine
960 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
962 case CONST_2
: p
->literal
= 2; break;
963 case CONST_4
: p
->literal
= 4; break;
964 case CONST_8
: p
->literal
= 8; break;
965 case CONST_16
: p
->literal
= 16; break;
968 else if ((x
& MODE
) == REG
)
970 p
->type
= X (OP_REG
, bitfrom (x
));
973 else if ((x
& MODE
) == LOWREG
)
975 p
->type
= X (OP_LOWREG
, bitfrom (x
));
978 else if ((x
& MODE
) == PREINC
)
980 /* Use the instruction to determine
982 p
->type
= X (OP_PREINC
, OP_SIZE (q
->how
));
983 p
->reg
= reg
[opnum
] & 0x7;
985 else if ((x
& MODE
) == POSTINC
)
987 /* Use the instruction to determine
989 p
->type
= X (OP_POSTINC
, OP_SIZE (q
->how
));
990 p
->reg
= reg
[opnum
] & 0x7;
992 else if ((x
& MODE
) == PREDEC
)
994 /* Use the instruction to determine
996 p
->type
= X (OP_PREDEC
, OP_SIZE (q
->how
));
997 p
->reg
= reg
[opnum
] & 0x7;
999 else if ((x
& MODE
) == POSTDEC
)
1001 /* Use the instruction to determine
1002 the operand size. */
1003 p
->type
= X (OP_POSTDEC
, OP_SIZE (q
->how
));
1004 p
->reg
= reg
[opnum
] & 0x7;
1006 else if ((x
& MODE
) == IND
)
1008 /* Note: an indirect is transformed into
1009 a displacement of zero.
1011 /* Use the instruction to determine
1012 the operand size. */
1013 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1014 p
->reg
= reg
[opnum
] & 0x7;
1016 if (OP_KIND (q
->how
) == O_JSR
||
1017 OP_KIND (q
->how
) == O_JMP
)
1018 if (lvalue (sd
, p
->type
, p
->reg
, &p
->type
))
1021 else if ((x
& MODE
) == ABS
)
1023 /* Note: a 16 or 32 bit ABS is transformed into a
1024 displacement from pseudo-register ZERO_REGNUM,
1025 which is always zero. An 8 bit ABS becomes
1026 a displacement from SBR_REGNUM.
1028 /* Use the instruction to determine
1029 the operand size. */
1030 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1031 p
->literal
= cst
[opnum
];
1033 /* 8-bit ABS is displacement from SBR.
1034 16 and 32-bit ABS are displacement from ZERO.
1035 (SBR will always be zero except for h8/sx)
1037 if ((x
& SIZE
) == L_8
)
1038 p
->reg
= SBR_REGNUM
;
1040 p
->reg
= ZERO_REGNUM
;;
1042 else if ((x
& MODE
) == MEMIND
||
1043 (x
& MODE
) == VECIND
)
1045 /* Size doesn't matter. */
1046 p
->type
= X (OP_MEM
, SB
);
1047 p
->literal
= cst
[opnum
];
1048 if (OP_KIND (q
->how
) == O_JSR
||
1049 OP_KIND (q
->how
) == O_JMP
)
1050 if (lvalue (sd
, p
->type
, p
->reg
, &p
->type
))
1053 else if ((x
& MODE
) == PCREL
)
1055 /* Size doesn't matter. */
1056 p
->type
= X (OP_PCREL
, SB
);
1057 p
->literal
= cst
[opnum
];
1059 else if (x
& ABSJMP
)
1061 p
->type
= X (OP_IMM
, SP
);
1062 p
->literal
= cst
[opnum
];
1064 else if ((x
& MODE
) == INDEXB
)
1066 p
->type
= X (OP_INDEXB
, OP_SIZE (q
->how
));
1067 p
->literal
= cst
[opnum
];
1068 p
->reg
= rdisp
[opnum
];
1070 else if ((x
& MODE
) == INDEXW
)
1072 p
->type
= X (OP_INDEXW
, OP_SIZE (q
->how
));
1073 p
->literal
= cst
[opnum
];
1074 p
->reg
= rdisp
[opnum
];
1076 else if ((x
& MODE
) == INDEXL
)
1078 p
->type
= X (OP_INDEXL
, OP_SIZE (q
->how
));
1079 p
->literal
= cst
[opnum
];
1080 p
->reg
= rdisp
[opnum
];
1082 else if ((x
& MODE
) == DISP
)
1084 /* Yuck -- special for mova args. */
1085 if (strncmp (q
->name
, "mova", 4) == 0 &&
1088 /* Mova can have a DISP2 dest, with an
1089 INDEXB or INDEXW src. The multiplier
1090 for the displacement value is determined
1091 by the src operand, not by the insn. */
1093 switch (OP_KIND (dst
->src
.type
))
1096 p
->type
= X (OP_DISP
, SB
);
1097 p
->literal
= cst
[opnum
];
1100 p
->type
= X (OP_DISP
, SW
);
1101 p
->literal
= cst
[opnum
] * 2;
1109 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
1110 p
->literal
= cst
[opnum
];
1111 /* DISP2 is special. */
1112 if ((x
& SIZE
) == L_2
)
1113 switch (OP_SIZE (q
->how
))
1116 case SW
: p
->literal
*= 2; break;
1117 case SL
: p
->literal
*= 4; break;
1120 p
->reg
= rdisp
[opnum
];
1127 p
->type
= X (OP_CCR
, SB
);
1130 p
->type
= X (OP_EXR
, SB
);
1133 p
->type
= X (OP_MACH
, SL
);
1136 p
->type
= X (OP_MACL
, SL
);
1139 p
->type
= X (OP_VBR
, SL
);
1142 p
->type
= X (OP_SBR
, SL
);
1146 else if ((x
& MODE
) == CCR
)
1150 else if ((x
& MODE
) == EXR
)
1155 printf ("Hmmmm 0x%x...\n", x
);
1161 /* Unary operators: treat src and dst as equivalent. */
1162 if (dst
->dst
.type
== -1)
1163 dst
->dst
= dst
->src
;
1164 if (dst
->src
.type
== -1)
1165 dst
->src
= dst
->dst
;
1167 dst
->opcode
= q
->how
;
1168 dst
->cycles
= q
->time
;
1170 /* And jsr's to these locations are turned into
1173 if (OP_KIND (dst
->opcode
) == O_JSR
)
1175 switch (dst
->src
.literal
)
1178 dst
->opcode
= O (O_SYS_OPEN
, SB
);
1181 dst
->opcode
= O (O_SYS_READ
, SB
);
1184 dst
->opcode
= O (O_SYS_WRITE
, SB
);
1187 dst
->opcode
= O (O_SYS_LSEEK
, SB
);
1190 dst
->opcode
= O (O_SYS_CLOSE
, SB
);
1193 dst
->opcode
= O (O_SYS_STAT
, SB
);
1196 dst
->opcode
= O (O_SYS_FSTAT
, SB
);
1199 dst
->opcode
= O (O_SYS_CMDLINE
, SB
);
1202 /* End of Processing for system calls. */
1205 dst
->next_pc
= addr
+ len
/ 2;
1209 printf ("Don't understand 0x%x \n", looking_for
);
1220 /* Fell off the end. */
1221 dst
->opcode
= O (O_ILL
, SB
);
1225 compile (SIM_DESC sd
, int pc
)
1229 /* Find the next cache entry to use. */
1230 idx
= h8_get_cache_top (sd
) + 1;
1231 h8_increment_compiles (sd
);
1232 if (idx
>= sd
->sim_cache_size
)
1236 h8_set_cache_top (sd
, idx
);
1238 /* Throw away its old meaning. */
1239 h8_set_cache_idx (sd
, sd
->sim_cache
[idx
].oldpc
, 0);
1241 /* Set to new address. */
1242 sd
->sim_cache
[idx
].oldpc
= pc
;
1244 /* Fill in instruction info. */
1245 decode (sd
, pc
, h8_get_memory_buf (sd
) + pc
, sd
->sim_cache
+ idx
);
1247 /* Point to new cache entry. */
1248 h8_set_cache_idx (sd
, pc
, idx
);
1252 static unsigned char *breg
[32];
1253 static unsigned short *wreg
[16];
1254 static unsigned int *lreg
[18];
1256 #define GET_B_REG(X) *(breg[X])
1257 #define SET_B_REG(X, Y) (*(breg[X])) = (Y)
1258 #define GET_W_REG(X) *(wreg[X])
1259 #define SET_W_REG(X, Y) (*(wreg[X])) = (Y)
1260 #define GET_L_REG(X) h8_get_reg (sd, X)
1261 #define SET_L_REG(X, Y) h8_set_reg (sd, X, Y)
1263 #define GET_MEMORY_L(X) \
1264 ((X) < memory_size \
1265 ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16) \
1266 | (h8_get_memory (sd, (X)+2) << 8) | (h8_get_memory (sd, (X)+3) << 0)) \
1267 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 24) \
1268 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 16) \
1269 | (h8_get_eightbit (sd, ((X)+2) & 0xff) << 8) \
1270 | (h8_get_eightbit (sd, ((X)+3) & 0xff) << 0)))
1272 #define GET_MEMORY_W(X) \
1273 ((X) < memory_size \
1274 ? ((h8_get_memory (sd, (X)+0) << 8) \
1275 | (h8_get_memory (sd, (X)+1) << 0)) \
1276 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 8) \
1277 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 0)))
1280 #define GET_MEMORY_B(X) \
1281 ((X) < memory_size ? (h8_get_memory (sd, (X))) \
1282 : (h8_get_eightbit (sd, (X) & 0xff)))
1284 #define SET_MEMORY_L(X, Y) \
1285 { register unsigned char *_p; register int __y = (Y); \
1286 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1287 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1288 _p[0] = __y >> 24; _p[1] = __y >> 16; \
1289 _p[2] = __y >> 8; _p[3] = __y >> 0; \
1292 #define SET_MEMORY_W(X, Y) \
1293 { register unsigned char *_p; register int __y = (Y); \
1294 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1295 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1296 _p[0] = __y >> 8; _p[1] = __y; \
1299 #define SET_MEMORY_B(X, Y) \
1300 ((X) < memory_size ? (h8_set_memory (sd, (X), (Y))) \
1301 : (h8_set_eightbit (sd, (X) & 0xff, (Y))))
1303 /* Simulate a memory fetch.
1304 Return 0 for success, -1 for failure.
1308 fetch_1 (SIM_DESC sd
, ea_type
*arg
, int *val
, int twice
)
1311 int abs
= arg
->literal
;
1316 return -1; /* Paranoia. */
1320 /* Indexed register plus displacement mode:
1322 This new family of addressing modes are similar to OP_DISP
1323 (register plus displacement), with two differences:
1324 1) INDEXB uses only the least significant byte of the register,
1325 INDEXW uses only the least significant word, and
1326 INDEXL uses the entire register (just like OP_DISP).
1328 2) The displacement value in abs is multiplied by two
1329 for SW-sized operations, and by four for SL-size.
1331 This gives nine possible variations.
1334 case X (OP_INDEXB
, SB
):
1335 case X (OP_INDEXB
, SW
):
1336 case X (OP_INDEXB
, SL
):
1337 case X (OP_INDEXW
, SB
):
1338 case X (OP_INDEXW
, SW
):
1339 case X (OP_INDEXW
, SL
):
1340 case X (OP_INDEXL
, SB
):
1341 case X (OP_INDEXL
, SW
):
1342 case X (OP_INDEXL
, SL
):
1344 switch (OP_KIND (arg
->type
)) {
1345 case OP_INDEXB
: t
&= 0xff; break;
1346 case OP_INDEXW
: t
&= 0xffff; break;
1350 switch (OP_SIZE (arg
->type
)) {
1352 *val
= GET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (sd
));
1355 *val
= GET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (sd
));
1358 *val
= GET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (sd
));
1363 case X (OP_LOWREG
, SB
):
1364 *val
= GET_L_REG (rn
) & 0xff;
1366 case X (OP_LOWREG
, SW
):
1367 *val
= GET_L_REG (rn
) & 0xffff;
1370 case X (OP_REG
, SB
): /* Register direct, byte. */
1371 *val
= GET_B_REG (rn
);
1373 case X (OP_REG
, SW
): /* Register direct, word. */
1374 *val
= GET_W_REG (rn
);
1376 case X (OP_REG
, SL
): /* Register direct, long. */
1377 *val
= GET_L_REG (rn
);
1379 case X (OP_IMM
, SB
): /* Immediate, byte. */
1380 case X (OP_IMM
, SW
): /* Immediate, word. */
1381 case X (OP_IMM
, SL
): /* Immediate, long. */
1384 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr: byte. */
1386 t
&= h8_get_mask (sd
);
1387 r
= GET_MEMORY_B (t
);
1390 t
= t
& h8_get_mask (sd
);
1394 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr: word. */
1396 t
&= h8_get_mask (sd
);
1397 r
= GET_MEMORY_W (t
);
1400 t
= t
& h8_get_mask (sd
);
1404 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr: long. */
1406 t
&= h8_get_mask (sd
);
1407 r
= GET_MEMORY_L (t
);
1410 t
= t
& h8_get_mask (sd
);
1415 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr: byte. */
1417 t
&= h8_get_mask (sd
);
1418 r
= GET_MEMORY_B (t
);
1421 t
= t
& h8_get_mask (sd
);
1425 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr: word. */
1427 t
&= h8_get_mask (sd
);
1428 r
= GET_MEMORY_W (t
);
1431 t
= t
& h8_get_mask (sd
);
1435 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr: long. */
1437 t
&= h8_get_mask (sd
);
1438 r
= GET_MEMORY_L (t
);
1441 t
= t
& h8_get_mask (sd
);
1446 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr: byte. */
1447 t
= GET_L_REG (rn
) - 1;
1448 t
&= h8_get_mask (sd
);
1450 *val
= GET_MEMORY_B (t
);
1453 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr: word. */
1454 t
= GET_L_REG (rn
) - 2;
1455 t
&= h8_get_mask (sd
);
1457 *val
= GET_MEMORY_W (t
);
1460 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr: long. */
1461 t
= GET_L_REG (rn
) - 4;
1462 t
&= h8_get_mask (sd
);
1464 *val
= GET_MEMORY_L (t
);
1467 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr: byte. */
1468 t
= GET_L_REG (rn
) + 1;
1469 t
&= h8_get_mask (sd
);
1471 *val
= GET_MEMORY_B (t
);
1474 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr: long. */
1475 t
= GET_L_REG (rn
) + 2;
1476 t
&= h8_get_mask (sd
);
1478 *val
= GET_MEMORY_W (t
);
1481 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr: long. */
1482 t
= GET_L_REG (rn
) + 4;
1483 t
&= h8_get_mask (sd
);
1485 *val
= GET_MEMORY_L (t
);
1488 case X (OP_DISP
, SB
): /* Register indirect w/displacement: byte. */
1489 t
= GET_L_REG (rn
) + abs
;
1490 t
&= h8_get_mask (sd
);
1491 *val
= GET_MEMORY_B (t
);
1494 case X (OP_DISP
, SW
): /* Register indirect w/displacement: word. */
1495 t
= GET_L_REG (rn
) + abs
;
1496 t
&= h8_get_mask (sd
);
1497 *val
= GET_MEMORY_W (t
);
1500 case X (OP_DISP
, SL
): /* Register indirect w/displacement: long. */
1501 t
= GET_L_REG (rn
) + abs
;
1502 t
&= h8_get_mask (sd
);
1503 *val
=GET_MEMORY_L (t
);
1506 case X (OP_MEM
, SL
): /* Absolute memory address, long. */
1507 t
= GET_MEMORY_L (abs
);
1508 t
&= h8_get_mask (sd
);
1512 case X (OP_MEM
, SW
): /* Absolute memory address, word. */
1513 t
= GET_MEMORY_W (abs
);
1514 t
&= h8_get_mask (sd
);
1518 case X (OP_PCREL
, SB
): /* PC relative (for jump, branch etc). */
1519 case X (OP_PCREL
, SW
):
1520 case X (OP_PCREL
, SL
):
1521 case X (OP_PCREL
, SN
):
1525 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1527 sim_engine_set_run_state (sd
, sim_stopped
, SIGSEGV
);
1530 return 0; /* Success. */
1536 fetch (SIM_DESC sd
, ea_type
*arg
, int *val
)
1538 return fetch_1 (sd
, arg
, val
, 0);
1541 /* Fetch which will be followed by a store to the same location.
1542 The difference being that we don't want to do a post-increment
1543 or post-decrement at this time: we'll do it when we store. */
1546 fetch2 (SIM_DESC sd
, ea_type
*arg
, int *val
)
1548 return fetch_1 (sd
, arg
, val
, 1);
1551 /* Simulate a memory store.
1552 Return 0 for success, -1 for failure.
1556 store_1 (SIM_DESC sd
, ea_type
*arg
, int n
, int twice
)
1559 int abs
= arg
->literal
;
1564 /* Indexed register plus displacement mode:
1566 This new family of addressing modes are similar to OP_DISP
1567 (register plus displacement), with two differences:
1568 1) INDEXB uses only the least significant byte of the register,
1569 INDEXW uses only the least significant word, and
1570 INDEXL uses the entire register (just like OP_DISP).
1572 2) The displacement value in abs is multiplied by two
1573 for SW-sized operations, and by four for SL-size.
1575 This gives nine possible variations.
1578 case X (OP_INDEXB
, SB
):
1579 case X (OP_INDEXB
, SW
):
1580 case X (OP_INDEXB
, SL
):
1581 case X (OP_INDEXW
, SB
):
1582 case X (OP_INDEXW
, SW
):
1583 case X (OP_INDEXW
, SL
):
1584 case X (OP_INDEXL
, SB
):
1585 case X (OP_INDEXL
, SW
):
1586 case X (OP_INDEXL
, SL
):
1588 switch (OP_KIND (arg
->type
)) {
1589 case OP_INDEXB
: t
&= 0xff; break;
1590 case OP_INDEXW
: t
&= 0xffff; break;
1594 switch (OP_SIZE (arg
->type
)) {
1596 SET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (sd
), n
);
1599 SET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (sd
), n
);
1602 SET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (sd
), n
);
1607 case X (OP_REG
, SB
): /* Register direct, byte. */
1610 case X (OP_REG
, SW
): /* Register direct, word. */
1613 case X (OP_REG
, SL
): /* Register direct, long. */
1617 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr, byte. */
1621 t
&= h8_get_mask (sd
);
1623 SET_MEMORY_B (t
, n
);
1626 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr, word. */
1630 t
&= h8_get_mask (sd
);
1632 SET_MEMORY_W (t
, n
);
1635 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr, long. */
1639 t
&= h8_get_mask (sd
);
1641 SET_MEMORY_L (t
, n
);
1644 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr, byte. */
1648 t
&= h8_get_mask (sd
);
1650 SET_MEMORY_B (t
, n
);
1653 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr, word. */
1657 t
&= h8_get_mask (sd
);
1659 SET_MEMORY_W (t
, n
);
1662 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr, long. */
1666 t
&= h8_get_mask (sd
);
1668 SET_MEMORY_L (t
, n
);
1671 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr, byte. */
1672 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1673 SET_MEMORY_B (t
, n
);
1674 SET_L_REG (rn
, t
- 1);
1677 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr, word. */
1678 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1679 SET_MEMORY_W (t
, n
);
1680 SET_L_REG (rn
, t
- 2);
1683 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr, long. */
1684 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1685 SET_MEMORY_L (t
, n
);
1686 SET_L_REG (rn
, t
- 4);
1689 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr, byte. */
1690 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1691 SET_MEMORY_B (t
, n
);
1692 SET_L_REG (rn
, t
+ 1);
1695 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr, word. */
1696 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1697 SET_MEMORY_W (t
, n
);
1698 SET_L_REG (rn
, t
+ 2);
1701 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr, long. */
1702 t
= GET_L_REG (rn
) & h8_get_mask (sd
);
1703 SET_MEMORY_L (t
, n
);
1704 SET_L_REG (rn
, t
+ 4);
1707 case X (OP_DISP
, SB
): /* Register indirect w/displacement, byte. */
1708 t
= GET_L_REG (rn
) + abs
;
1709 t
&= h8_get_mask (sd
);
1710 SET_MEMORY_B (t
, n
);
1713 case X (OP_DISP
, SW
): /* Register indirect w/displacement, word. */
1714 t
= GET_L_REG (rn
) + abs
;
1715 t
&= h8_get_mask (sd
);
1716 SET_MEMORY_W (t
, n
);
1719 case X (OP_DISP
, SL
): /* Register indirect w/displacement, long. */
1720 t
= GET_L_REG (rn
) + abs
;
1721 t
&= h8_get_mask (sd
);
1722 SET_MEMORY_L (t
, n
);
1726 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1727 case X (OP_MEM
, SW
): /* Why isn't this implemented? */
1728 case X (OP_MEM
, SL
): /* Why isn't this implemented? */
1730 sim_engine_set_run_state (sd
, sim_stopped
, SIGSEGV
);
1739 store (SIM_DESC sd
, ea_type
*arg
, int n
)
1741 return store_1 (sd
, arg
, n
, 0);
1744 /* Store which follows a fetch from the same location.
1745 The difference being that we don't want to do a pre-increment
1746 or pre-decrement at this time: it was already done when we fetched. */
1749 store2 (SIM_DESC sd
, ea_type
*arg
, int n
)
1751 return store_1 (sd
, arg
, n
, 1);
1765 /* Flag to be set whenever a new SIM_DESC object is created. */
1766 static int init_pointers_needed
= 1;
1769 init_pointers (SIM_DESC sd
)
1771 if (init_pointers_needed
)
1778 memory_size
= H8300S_MSIZE
;
1779 else if (h8300hmode
)
1780 memory_size
= H8300H_MSIZE
;
1782 memory_size
= H8300_MSIZE
;
1783 /* `msize' must be a power of two. */
1784 if ((memory_size
& (memory_size
- 1)) != 0)
1786 (*sim_callback
->printf_filtered
)
1788 "init_pointers: bad memory size %d, defaulting to %d.\n",
1789 memory_size
, memory_size
= H8300S_MSIZE
);
1792 if (h8_get_memory_buf (sd
))
1793 free (h8_get_memory_buf (sd
));
1794 if (h8_get_cache_idx_buf (sd
))
1795 free (h8_get_cache_idx_buf (sd
));
1796 if (h8_get_eightbit_buf (sd
))
1797 free (h8_get_eightbit_buf (sd
));
1799 h8_set_memory_buf (sd
, (unsigned char *)
1800 calloc (sizeof (char), memory_size
));
1801 h8_set_cache_idx_buf (sd
, (unsigned short *)
1802 calloc (sizeof (short), memory_size
));
1803 sd
->memory_size
= memory_size
;
1804 h8_set_eightbit_buf (sd
, (unsigned char *) calloc (sizeof (char), 256));
1806 h8_set_mask (sd
, memory_size
- 1);
1808 memset (h8_get_reg_buf (sd
), 0, sizeof (((STATE_CPU (sd
, 0))->regs
)));
1810 for (i
= 0; i
< 8; i
++)
1812 /* FIXME: rewrite using local buffer. */
1813 unsigned char *p
= (unsigned char *) (h8_get_reg_buf (sd
) + i
);
1814 unsigned char *e
= (unsigned char *) (h8_get_reg_buf (sd
) + i
+ 1);
1815 unsigned short *q
= (unsigned short *) (h8_get_reg_buf (sd
) + i
);
1816 unsigned short *u
= (unsigned short *) (h8_get_reg_buf (sd
) + i
+ 1);
1817 h8_set_reg (sd
, i
, 0x00112233);
1832 wreg
[i
] = wreg
[i
+ 8] = 0;
1846 if (wreg
[i
] == 0 || wreg
[i
+ 8] == 0)
1847 (*sim_callback
->printf_filtered
) (sim_callback
,
1848 "init_pointers: internal error.\n");
1850 h8_set_reg (sd
, i
, 0);
1851 lreg
[i
] = h8_get_reg_buf (sd
) + i
;
1854 /* Note: sim uses pseudo-register ZERO as a zero register. */
1855 lreg
[ZERO_REGNUM
] = h8_get_reg_buf (sd
) + ZERO_REGNUM
;
1856 init_pointers_needed
= 0;
1858 /* Initialize the seg registers. */
1860 set_simcache_size (sd
, CSIZE
);
1864 /* Grotty global variable for use by control_c signal handler. */
1865 static SIM_DESC control_c_sim_desc
;
1870 sim_engine_set_run_state (control_c_sim_desc
, sim_stopped
, SIGINT
);
1874 sim_stop (SIM_DESC sd
)
1876 /* FIXME: use a real signal value. */
1877 sim_engine_set_run_state (sd
, sim_stopped
, SIGINT
);
1881 #define OBITOP(name, f, s, op) \
1882 case O (name, SB): \
1887 if (fetch (sd, &code->dst, &ea)) \
1889 if (fetch (sd, &code->src, &tmp)) \
1894 if (store (sd, &code->dst,ea)) \
1900 sim_resume (SIM_DESC sd
, int step
, int siggnal
)
1905 int tick_start
= get_now ();
1914 int c
, nz
, v
, n
, u
, h
, ui
, intMaskBit
;
1917 enum sim_stop reason
;
1922 control_c_sim_desc
= sd
;
1923 prev
= signal (SIGINT
, control_c
);
1927 sim_engine_set_run_state (sd
, sim_stopped
, SIGTRAP
);
1931 sim_engine_set_run_state (sd
, sim_running
, 0);
1934 pc
= h8_get_pc (sd
);
1936 /* The PC should never be odd. */
1939 sim_engine_set_run_state (sd
, sim_stopped
, SIGBUS
);
1943 /* Get Status Register (flags). */
1946 if (h8300smode
) /* Get exr. */
1948 trace
= (h8_get_exr (sd
) >> 7) & 1;
1949 intMask
= h8_get_exr (sd
) & 7;
1952 oldmask
= h8_get_mask (sd
);
1954 h8_set_mask (sd
, 0xffff);
1957 unsigned short cidx
;
1961 cidx
= h8_get_cache_idx (sd
, pc
);
1962 if (cidx
== (unsigned short) -1 ||
1963 cidx
>= sd
->sim_cache_size
)
1966 code
= sd
->sim_cache
+ cidx
;
1971 printf ("%x %d %s\n", pc
, code
->opcode
,
1972 code
->op
? code
->op
->name
: "**");
1974 h8_increment_stats (sd
, code
->opcode
);
1979 cycles
+= code
->cycles
;
1983 switch (code
->opcode
)
1987 * This opcode is a fake for when we get to an
1988 * instruction which hasnt been compiled
1994 case O (O_MOVAB
, SL
):
1995 case O (O_MOVAW
, SL
):
1996 case O (O_MOVAL
, SL
):
1997 /* 1) Evaluate 2nd argument (dst).
1998 2) Mask / zero extend according to whether 1st argument (src)
1999 is INDEXB, INDEXW, or INDEXL.
2000 3) Left-shift the result by 0, 1 or 2, according to size of mova
2001 (mova/b, mova/w, mova/l).
2002 4) Add literal value of 1st argument (src).
2003 5) Store result in 3rd argument (op3).
2006 /* Alas, since this is the only instruction with 3 arguments,
2007 decode doesn't handle them very well. Some fix-up is required.
2009 a) The size of dst is determined by whether src is
2010 INDEXB or INDEXW. */
2012 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
2013 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SB
);
2014 else if (OP_KIND (code
->src
.type
) == OP_INDEXW
)
2015 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SW
);
2017 /* b) If op3 == null, then this is the short form of the insn.
2018 Dst is the dispreg of src, and op3 is the 32-bit form
2019 of the same register.
2022 if (code
->op3
.type
== 0)
2024 /* Short form: src == INDEXB/INDEXW, dst == op3 == 0.
2025 We get to compose dst and op3 as follows:
2027 op3 is a 32-bit register, ID == src.reg.
2028 dst is the same register, but 8 or 16 bits
2029 depending on whether src is INDEXB or INDEXW.
2032 code
->op3
.type
= X (OP_REG
, SL
);
2033 code
->op3
.reg
= code
->src
.reg
;
2034 code
->op3
.literal
= 0;
2036 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
2037 code
->dst
.type
= X (OP_REG
, SB
);
2039 code
->dst
.type
= X (OP_REG
, SW
);
2042 if (fetch (sd
, &code
->dst
, &ea
))
2045 switch (OP_KIND (code
->src
.type
)) {
2046 case OP_INDEXB
: ea
= ea
& 0xff; break;
2047 case OP_INDEXW
: ea
= ea
& 0xffff; break;
2048 case OP_INDEXL
: break;
2049 default: goto illegal
;
2052 switch (code
->opcode
) {
2053 case O (O_MOVAB
, SL
): break;
2054 case O (O_MOVAW
, SL
): ea
= ea
<< 1; break;
2055 case O (O_MOVAL
, SL
): ea
= ea
<< 2; break;
2056 default: goto illegal
;
2059 ea
= ea
+ code
->src
.literal
;
2061 if (store (sd
, &code
->op3
, ea
))
2066 case O (O_SUBX
, SB
): /* subx, extended sub */
2067 if (fetch2 (sd
, &code
->dst
, &rd
))
2069 if (fetch (sd
, &code
->src
, &ea
))
2075 case O (O_SUBX
, SW
): /* subx, extended sub */
2076 if (fetch2 (sd
, &code
->dst
, &rd
))
2078 if (fetch (sd
, &code
->src
, &ea
))
2084 case O (O_SUBX
, SL
): /* subx, extended sub */
2085 if (fetch2 (sd
, &code
->dst
, &rd
))
2087 if (fetch (sd
, &code
->src
, &ea
))
2093 case O (O_ADDX
, SB
): /* addx, extended add */
2094 if (fetch2 (sd
, &code
->dst
, &rd
))
2096 if (fetch (sd
, &code
->src
, &ea
))
2102 case O (O_ADDX
, SW
): /* addx, extended add */
2103 if (fetch2 (sd
, &code
->dst
, &rd
))
2105 if (fetch (sd
, &code
->src
, &ea
))
2111 case O (O_ADDX
, SL
): /* addx, extended add */
2112 if (fetch2 (sd
, &code
->dst
, &rd
))
2114 if (fetch (sd
, &code
->src
, &ea
))
2120 case O (O_SUB
, SB
): /* sub.b */
2121 /* Fetch rd and ea. */
2122 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2128 case O (O_SUB
, SW
): /* sub.w */
2129 /* Fetch rd and ea. */
2130 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2136 case O (O_SUB
, SL
): /* sub.l */
2137 /* Fetch rd and ea. */
2138 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2144 case O (O_NEG
, SB
): /* neg.b */
2146 if (fetch2 (sd
, &code
->src
, &ea
))
2153 case O (O_NEG
, SW
): /* neg.w */
2155 if (fetch2 (sd
, &code
->src
, &ea
))
2162 case O (O_NEG
, SL
): /* neg.l */
2164 if (fetch2 (sd
, &code
->src
, &ea
))
2171 case O (O_ADD
, SB
): /* add.b */
2172 if (fetch2 (sd
, &code
->dst
, &rd
))
2174 if (fetch (sd
, &code
->src
, &ea
))
2179 case O (O_ADD
, SW
): /* add.w */
2180 if (fetch2 (sd
, &code
->dst
, &rd
))
2182 if (fetch (sd
, &code
->src
, &ea
))
2187 case O (O_ADD
, SL
): /* add.l */
2188 if (fetch2 (sd
, &code
->dst
, &rd
))
2190 if (fetch (sd
, &code
->src
, &ea
))
2195 case O (O_AND
, SB
): /* and.b */
2196 /* Fetch rd and ea. */
2197 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2202 case O (O_AND
, SW
): /* and.w */
2203 /* Fetch rd and ea. */
2204 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2209 case O (O_AND
, SL
): /* and.l */
2210 /* Fetch rd and ea. */
2211 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2216 case O (O_OR
, SB
): /* or.b */
2217 /* Fetch rd and ea. */
2218 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2223 case O (O_OR
, SW
): /* or.w */
2224 /* Fetch rd and ea. */
2225 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2230 case O (O_OR
, SL
): /* or.l */
2231 /* Fetch rd and ea. */
2232 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2237 case O (O_XOR
, SB
): /* xor.b */
2238 /* Fetch rd and ea. */
2239 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2244 case O (O_XOR
, SW
): /* xor.w */
2245 /* Fetch rd and ea. */
2246 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2251 case O (O_XOR
, SL
): /* xor.l */
2252 /* Fetch rd and ea. */
2253 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2259 if (fetch (sd
, &code
->src
, &res
))
2261 if (store (sd
, &code
->dst
, res
))
2263 goto just_flags_log8
;
2265 if (fetch (sd
, &code
->src
, &res
))
2267 if (store (sd
, &code
->dst
, res
))
2269 goto just_flags_log16
;
2271 if (fetch (sd
, &code
->src
, &res
))
2273 if (store (sd
, &code
->dst
, res
))
2275 goto just_flags_log32
;
2277 case O (O_MOVMD
, SB
): /* movmd.b */
2284 rd
= GET_MEMORY_B (GET_L_REG (5));
2285 SET_MEMORY_B (GET_L_REG (6), rd
);
2286 SET_L_REG (5, GET_L_REG (5) + 1);
2287 SET_L_REG (6, GET_L_REG (6) + 1);
2292 case O (O_MOVMD
, SW
): /* movmd.w */
2299 rd
= GET_MEMORY_W (GET_L_REG (5));
2300 SET_MEMORY_W (GET_L_REG (6), rd
);
2301 SET_L_REG (5, GET_L_REG (5) + 2);
2302 SET_L_REG (6, GET_L_REG (6) + 2);
2307 case O (O_MOVMD
, SL
): /* movmd.l */
2314 rd
= GET_MEMORY_L (GET_L_REG (5));
2315 SET_MEMORY_L (GET_L_REG (6), rd
);
2316 SET_L_REG (5, GET_L_REG (5) + 4);
2317 SET_L_REG (6, GET_L_REG (6) + 4);
2322 case O (O_MOVSD
, SB
): /* movsd.b */
2323 /* This instruction implements strncpy, with a conditional branch.
2324 r4 contains n, r5 contains src, and r6 contains dst.
2325 The 16-bit displacement operand is added to the pc
2326 if and only if the end of string is reached before
2327 n bytes are transferred. */
2329 ea
= GET_L_REG (4) & 0xffff;
2335 rd
= GET_MEMORY_B (GET_L_REG (5));
2336 SET_MEMORY_B (GET_L_REG (6), rd
);
2337 SET_L_REG (5, GET_L_REG (5) + 1);
2338 SET_L_REG (6, GET_L_REG (6) + 1);
2345 case O (O_EEPMOV
, SB
): /* eepmov.b */
2346 case O (O_EEPMOV
, SW
): /* eepmov.w */
2347 if (h8300hmode
|| h8300smode
)
2349 register unsigned char *_src
, *_dst
;
2350 unsigned int count
= ((code
->opcode
== O (O_EEPMOV
, SW
))
2351 ? h8_get_reg (sd
, R4_REGNUM
) & 0xffff
2352 : h8_get_reg (sd
, R4_REGNUM
) & 0xff);
2354 _src
= (h8_get_reg (sd
, R5_REGNUM
) < memory_size
2355 ? h8_get_memory_buf (sd
) + h8_get_reg (sd
, R5_REGNUM
)
2356 : h8_get_eightbit_buf (sd
) +
2357 (h8_get_reg (sd
, R5_REGNUM
) & 0xff));
2358 if ((_src
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2360 if ((_src
+ count
) >= (h8_get_eightbit_buf (sd
) + 0x100))
2363 _dst
= (h8_get_reg (sd
, R6_REGNUM
) < memory_size
2364 ? h8_get_memory_buf (sd
) + h8_get_reg (sd
, R6_REGNUM
)
2365 : h8_get_eightbit_buf (sd
) +
2366 (h8_get_reg (sd
, R6_REGNUM
) & 0xff));
2368 if ((_dst
+ count
) >= (h8_get_memory_buf (sd
) + memory_size
))
2370 if ((_dst
+ count
) >= (h8_get_eightbit_buf (sd
) + 0x100))
2373 memcpy (_dst
, _src
, count
);
2375 h8_set_reg (sd
, R5_REGNUM
, h8_get_reg (sd
, R5_REGNUM
) + count
);
2376 h8_set_reg (sd
, R6_REGNUM
, h8_get_reg (sd
, R6_REGNUM
) + count
);
2377 h8_set_reg (sd
, R4_REGNUM
, h8_get_reg (sd
, R4_REGNUM
) &
2378 ((code
->opcode
== O (O_EEPMOV
, SW
))
2379 ? (~0xffff) : (~0xff)));
2380 cycles
+= 2 * count
;
2385 case O (O_ADDS
, SL
): /* adds (.l) */
2387 * This insn only uses register operands, but still
2388 * it would be cleaner to use fetch and store... */
2389 SET_L_REG (code
->dst
.reg
,
2390 GET_L_REG (code
->dst
.reg
)
2391 + code
->src
.literal
);
2395 case O (O_SUBS
, SL
): /* subs (.l) */
2397 * This insn only uses register operands, but still
2398 * it would be cleaner to use fetch and store... */
2399 SET_L_REG (code
->dst
.reg
,
2400 GET_L_REG (code
->dst
.reg
)
2401 - code
->src
.literal
);
2404 case O (O_CMP
, SB
): /* cmp.b */
2405 if (fetch (sd
, &code
->dst
, &rd
))
2407 if (fetch (sd
, &code
->src
, &ea
))
2411 goto just_flags_alu8
;
2413 case O (O_CMP
, SW
): /* cmp.w */
2414 if (fetch (sd
, &code
->dst
, &rd
))
2416 if (fetch (sd
, &code
->src
, &ea
))
2420 goto just_flags_alu16
;
2422 case O (O_CMP
, SL
): /* cmp.l */
2423 if (fetch (sd
, &code
->dst
, &rd
))
2425 if (fetch (sd
, &code
->src
, &ea
))
2429 goto just_flags_alu32
;
2431 case O (O_DEC
, SB
): /* dec.b */
2433 * This insn only uses register operands, but still
2434 * it would be cleaner to use fetch and store... */
2435 rd
= GET_B_REG (code
->src
.reg
);
2438 SET_B_REG (code
->src
.reg
, res
);
2439 goto just_flags_inc8
;
2441 case O (O_DEC
, SW
): /* dec.w */
2443 * This insn only uses register operands, but still
2444 * it would be cleaner to use fetch and store... */
2445 rd
= GET_W_REG (code
->dst
.reg
);
2446 ea
= -code
->src
.literal
;
2448 SET_W_REG (code
->dst
.reg
, res
);
2449 goto just_flags_inc16
;
2451 case O (O_DEC
, SL
): /* dec.l */
2453 * This insn only uses register operands, but still
2454 * it would be cleaner to use fetch and store... */
2455 rd
= GET_L_REG (code
->dst
.reg
);
2456 ea
= -code
->src
.literal
;
2458 SET_L_REG (code
->dst
.reg
, res
);
2459 goto just_flags_inc32
;
2461 case O (O_INC
, SB
): /* inc.b */
2463 * This insn only uses register operands, but still
2464 * it would be cleaner to use fetch and store... */
2465 rd
= GET_B_REG (code
->src
.reg
);
2468 SET_B_REG (code
->src
.reg
, res
);
2469 goto just_flags_inc8
;
2471 case O (O_INC
, SW
): /* inc.w */
2473 * This insn only uses register operands, but still
2474 * it would be cleaner to use fetch and store... */
2475 rd
= GET_W_REG (code
->dst
.reg
);
2476 ea
= code
->src
.literal
;
2478 SET_W_REG (code
->dst
.reg
, res
);
2479 goto just_flags_inc16
;
2481 case O (O_INC
, SL
): /* inc.l */
2483 * This insn only uses register operands, but still
2484 * it would be cleaner to use fetch and store... */
2485 rd
= GET_L_REG (code
->dst
.reg
);
2486 ea
= code
->src
.literal
;
2488 SET_L_REG (code
->dst
.reg
, res
);
2489 goto just_flags_inc32
;
2491 case O (O_LDC
, SB
): /* ldc.b */
2492 if (fetch (sd
, &code
->src
, &res
))
2496 case O (O_LDC
, SW
): /* ldc.w */
2497 if (fetch (sd
, &code
->src
, &res
))
2500 /* Word operand, value from MSB, must be shifted. */
2504 case O (O_LDC
, SL
): /* ldc.l */
2505 if (fetch (sd
, &code
->src
, &res
))
2507 switch (code
->dst
.type
) {
2508 case X (OP_SBR
, SL
):
2509 h8_set_sbr (sd
, res
);
2511 case X (OP_VBR
, SL
):
2512 h8_set_vbr (sd
, res
);
2519 case O (O_STC
, SW
): /* stc.w */
2520 case O (O_STC
, SB
): /* stc.b */
2521 if (code
->src
.type
== X (OP_CCR
, SB
))
2524 res
= h8_get_ccr (sd
);
2526 else if (code
->src
.type
== X (OP_EXR
, SB
) && h8300smode
)
2529 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2530 res
= h8_get_exr (sd
);
2535 /* Word operand, value to MSB, must be shifted. */
2536 if (code
->opcode
== X (O_STC
, SW
))
2538 if (store (sd
, &code
->dst
, res
))
2541 case O (O_STC
, SL
): /* stc.l */
2542 switch (code
->src
.type
) {
2543 case X (OP_SBR
, SL
):
2544 res
= h8_get_sbr (sd
);
2546 case X (OP_VBR
, SL
):
2547 res
= h8_get_vbr (sd
);
2552 if (store (sd
, &code
->dst
, res
))
2556 case O (O_ANDC
, SB
): /* andc.b */
2557 if (code
->dst
.type
== X (OP_CCR
, SB
))
2560 rd
= h8_get_ccr (sd
);
2562 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2565 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2566 res
= h8_get_exr (sd
);
2570 ea
= code
->src
.literal
;
2574 case O (O_ORC
, SB
): /* orc.b */
2575 if (code
->dst
.type
== X (OP_CCR
, SB
))
2578 rd
= h8_get_ccr (sd
);
2580 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2583 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2584 rd
= h8_get_exr (sd
);
2588 ea
= code
->src
.literal
;
2592 case O (O_XORC
, SB
): /* xorc.b */
2593 if (code
->dst
.type
== X (OP_CCR
, SB
))
2596 rd
= h8_get_ccr (sd
);
2598 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2601 h8_set_exr (sd
, (trace
<< 7) | intMask
);
2602 rd
= h8_get_exr (sd
);
2606 ea
= code
->src
.literal
;
2610 case O (O_BRAS
, SB
): /* bra/s */
2611 /* This is basically an ordinary branch, with a delay slot. */
2612 if (fetch (sd
, &code
->src
, &res
))
2620 /* Execution continues at next instruction, but
2621 delayed_branch is set up for next cycle. */
2622 h8_set_delayed_branch (sd
, code
->next_pc
+ res
);
2626 case O (O_BRAB
, SB
): /* bra rd.b */
2627 case O (O_BRAW
, SW
): /* bra rd.w */
2628 case O (O_BRAL
, SL
): /* bra erd.l */
2629 if (fetch (sd
, &code
->src
, &rd
))
2631 switch (OP_SIZE (code
->opcode
)) {
2632 case SB
: rd
&= 0xff; break;
2633 case SW
: rd
&= 0xffff; break;
2634 case SL
: rd
&= 0xffffffff; break;
2636 pc
= code
->next_pc
+ rd
;
2639 case O (O_BRABC
, SB
): /* bra/bc, branch if bit clear */
2640 case O (O_BRABS
, SB
): /* bra/bs, branch if bit set */
2641 case O (O_BSRBC
, SB
): /* bsr/bc, call if bit clear */
2642 case O (O_BSRBS
, SB
): /* bsr/bs, call if bit set */
2643 if (fetch (sd
, &code
->dst
, &rd
) ||
2644 fetch (sd
, &code
->src
, &bit
))
2647 if (code
->opcode
== O (O_BRABC
, SB
) || /* branch if clear */
2648 code
->opcode
== O (O_BSRBC
, SB
)) /* call if clear */
2650 if ((rd
& (1 << bit
))) /* no branch */
2653 else /* branch/call if set */
2655 if (!(rd
& (1 << bit
))) /* no branch */
2659 if (fetch (sd
, &code
->op3
, &res
)) /* branch */
2661 pc
= code
->next_pc
+ res
;
2663 if (code
->opcode
== O (O_BRABC
, SB
) ||
2664 code
->opcode
== O (O_BRABS
, SB
)) /* branch */
2672 case O (O_BRA
, SB
): /* bra, branch always */
2677 case O (O_BRN
, SB
): /* brn, ;-/ branch never? */
2682 case O (O_BHI
, SB
): /* bhi */
2688 case O (O_BLS
, SB
): /* bls */
2693 case O (O_BCS
, SB
): /* bcs, branch if carry set */
2698 case O (O_BCC
, SB
): /* bcc, branch if carry clear */
2703 case O (O_BEQ
, SB
): /* beq, branch if zero set */
2707 case O (O_BGT
, SB
): /* bgt */
2708 if (((Z
|| (N
^ V
)) == 0))
2712 case O (O_BLE
, SB
): /* ble */
2713 if (((Z
|| (N
^ V
)) == 1))
2717 case O (O_BGE
, SB
): /* bge */
2721 case O (O_BLT
, SB
): /* blt */
2725 case O (O_BMI
, SB
): /* bmi */
2729 case O (O_BNE
, SB
): /* bne, branch if zero clear */
2734 case O (O_BPL
, SB
): /* bpl */
2738 case O (O_BVC
, SB
): /* bvc */
2742 case O (O_BVS
, SB
): /* bvs */
2747 /* Trap for Command Line setup. */
2748 case O (O_SYS_CMDLINE
, SB
):
2750 int i
= 0; /* Loop counter. */
2751 int j
= 0; /* Loop counter. */
2752 int ind_arg_len
= 0; /* Length of each argument. */
2753 int no_of_args
= 0; /* The no. or cmdline args. */
2754 int current_location
= 0; /* Location of string. */
2755 int old_sp
= 0; /* The Initial Stack Pointer. */
2756 int no_of_slots
= 0; /* No. of slots required on the stack
2757 for storing cmdline args. */
2758 int sp_move
= 0; /* No. of locations by which the stack needs
2760 int new_sp
= 0; /* The final stack pointer location passed
2762 int *argv_ptrs
; /* Pointers of argv strings to be stored. */
2763 int argv_ptrs_location
= 0; /* Location of pointers to cmdline
2764 args on the stack. */
2765 int char_ptr_size
= 0; /* Size of a character pointer on
2767 int addr_cmdline
= 0; /* Memory location where cmdline has
2769 int size_cmdline
= 0; /* Size of cmdline. */
2771 /* Set the address of 256 free locations where command line is
2773 addr_cmdline
= cmdline_location();
2774 h8_set_reg (sd
, 0, addr_cmdline
);
2776 /* Counting the no. of commandline arguments. */
2777 for (i
= 0; h8_get_cmdline_arg (sd
, i
) != NULL
; i
++)
2780 /* No. of arguments in the command line. */
2783 /* Current location is just a temporary variable,which we are
2784 setting to the point to the start of our commandline string. */
2785 current_location
= addr_cmdline
;
2787 /* Allocating space for storing pointers of the command line
2789 argv_ptrs
= (int *) malloc (sizeof (int) * no_of_args
);
2791 /* Setting char_ptr_size to the sizeof (char *) on the different
2793 if (h8300hmode
|| h8300smode
)
2802 for (i
= 0; i
< no_of_args
; i
++)
2806 /* The size of the commandline argument. */
2807 ind_arg_len
= strlen (h8_get_cmdline_arg (sd
, i
) + 1);
2809 /* The total size of the command line string. */
2810 size_cmdline
+= ind_arg_len
;
2812 /* As we have only 256 bytes, we need to provide a graceful
2813 exit. Anyways, a program using command line arguments
2814 where we cannot store all the command line arguments
2815 given may behave unpredictably. */
2816 if (size_cmdline
>= 256)
2818 h8_set_reg (sd
, 0, 0);
2823 /* current_location points to the memory where the next
2824 commandline argument is stored. */
2825 argv_ptrs
[i
] = current_location
;
2826 for (j
= 0; j
< ind_arg_len
; j
++)
2828 SET_MEMORY_B ((current_location
+
2829 (sizeof (char) * j
)),
2830 *(h8_get_cmdline_arg (sd
, i
) +
2831 sizeof (char) * j
));
2834 /* Setting current_location to the starting of next
2836 current_location
+= ind_arg_len
;
2840 /* This is the original position of the stack pointer. */
2841 old_sp
= h8_get_reg (sd
, SP_REGNUM
);
2843 /* We need space from the stack to store the pointers to argvs. */
2844 /* As we will infringe on the stack, we need to shift the stack
2845 pointer so that the data is not overwritten. We calculate how
2846 much space is required. */
2847 sp_move
= (no_of_args
) * (char_ptr_size
);
2849 /* The final position of stack pointer, we have thus taken some
2850 space from the stack. */
2851 new_sp
= old_sp
- sp_move
;
2853 /* Temporary variable holding value where the argv pointers need
2855 argv_ptrs_location
= new_sp
;
2857 /* The argv pointers are stored at sequential locations. As per
2859 for (i
= 0; i
< no_of_args
; i
++)
2861 /* Saving the argv pointer. */
2862 if (h8300hmode
|| h8300smode
)
2864 SET_MEMORY_L (argv_ptrs_location
, argv_ptrs
[i
]);
2868 SET_MEMORY_W (argv_ptrs_location
, argv_ptrs
[i
]);
2871 /* The next location where the pointer to the next argv
2872 string has to be stored. */
2873 argv_ptrs_location
+= char_ptr_size
;
2876 /* Required by POSIX, Setting 0x0 at the end of the list of argv
2878 if (h8300hmode
|| h8300smode
)
2880 SET_MEMORY_L (old_sp
, 0x0);
2884 SET_MEMORY_W (old_sp
, 0x0);
2887 /* Freeing allocated memory. */
2889 for (i
= 0; i
<= no_of_args
; i
++)
2891 free (h8_get_cmdline_arg (sd
, i
));
2893 free (h8_get_command_line (sd
));
2895 /* The no. of argv arguments are returned in Reg 0. */
2896 h8_set_reg (sd
, 0, no_of_args
);
2897 /* The Pointer to argv in Register 1. */
2898 h8_set_reg (sd
, 1, new_sp
);
2899 /* Setting the stack pointer to the new value. */
2900 h8_set_reg (sd
, SP_REGNUM
, new_sp
);
2904 /* System call processing starts. */
2905 case O (O_SYS_OPEN
, SB
):
2907 int len
= 0; /* Length of filename. */
2908 char *filename
; /* Filename would go here. */
2909 char temp_char
; /* Temporary character */
2910 int mode
= 0; /* Mode bits for the file. */
2911 int open_return
; /* Return value of open, file descriptor. */
2912 int i
; /* Loop counter */
2913 int filename_ptr
; /* Pointer to filename in cpu memory. */
2915 /* Setting filename_ptr to first argument of open, */
2916 /* and trying to get mode. */
2917 if (h8300sxmode
|| h8300hmode
|| h8300smode
)
2919 filename_ptr
= GET_L_REG (0);
2920 mode
= GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
) + 4);
2924 filename_ptr
= GET_W_REG (0);
2925 mode
= GET_MEMORY_W (h8_get_reg (sd
, SP_REGNUM
) + 2);
2928 /* Trying to find the length of the filename. */
2929 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
2932 while (temp_char
!= '\0')
2934 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
2938 /* Allocating space for the filename. */
2939 filename
= (char *) malloc (sizeof (char) * len
);
2941 /* String copying the filename from memory. */
2942 for (i
= 0; i
< len
; i
++)
2944 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
2945 filename
[i
] = temp_char
;
2948 /* Callback to open and return the file descriptor. */
2949 open_return
= sim_callback
->open (sim_callback
, filename
, mode
);
2951 /* Return value in register 0. */
2952 h8_set_reg (sd
, 0, open_return
);
2954 /* Freeing memory used for filename. */
2959 case O (O_SYS_READ
, SB
):
2961 char *char_ptr
; /* Where characters read would be stored. */
2962 int fd
; /* File descriptor */
2963 int buf_size
; /* BUF_SIZE parameter in read. */
2964 int i
= 0; /* Temporary Loop counter */
2965 int read_return
= 0; /* Return value from callback to
2968 fd
= h8300hmode
? GET_L_REG (0) : GET_W_REG (0);
2969 buf_size
= h8300hmode
? GET_L_REG (2) : GET_W_REG (2);
2971 char_ptr
= (char *) malloc (sizeof (char) * buf_size
);
2973 /* Callback to read and return the no. of characters read. */
2975 sim_callback
->read (sim_callback
, fd
, char_ptr
, buf_size
);
2977 /* The characters read are stored in cpu memory. */
2978 for (i
= 0; i
< buf_size
; i
++)
2980 SET_MEMORY_B ((h8_get_reg (sd
, 1) + (sizeof (char) * i
)),
2981 *(char_ptr
+ (sizeof (char) * i
)));
2984 /* Return value in Register 0. */
2985 h8_set_reg (sd
, 0, read_return
);
2987 /* Freeing memory used as buffer. */
2992 case O (O_SYS_WRITE
, SB
):
2994 int fd
; /* File descriptor */
2995 char temp_char
; /* Temporary character */
2996 int len
; /* Length of write, Parameter II to write. */
2997 int char_ptr
; /* Character Pointer, Parameter I of write. */
2998 char *ptr
; /* Where characters to be written are stored.
3000 int write_return
; /* Return value from callback to write. */
3001 int i
= 0; /* Loop counter */
3003 fd
= h8300hmode
? GET_L_REG (0) : GET_W_REG (0);
3004 char_ptr
= h8300hmode
? GET_L_REG (1) : GET_W_REG (1);
3005 len
= h8300hmode
? GET_L_REG (2) : GET_W_REG (2);
3007 /* Allocating space for the characters to be written. */
3008 ptr
= (char *) malloc (sizeof (char) * len
);
3010 /* Fetching the characters from cpu memory. */
3011 for (i
= 0; i
< len
; i
++)
3013 temp_char
= GET_MEMORY_B (char_ptr
+ i
);
3017 /* Callback write and return the no. of characters written. */
3018 write_return
= sim_callback
->write (sim_callback
, fd
, ptr
, len
);
3020 /* Return value in Register 0. */
3021 h8_set_reg (sd
, 0, write_return
);
3023 /* Freeing memory used as buffer. */
3028 case O (O_SYS_LSEEK
, SB
):
3030 int fd
; /* File descriptor */
3031 int offset
; /* Offset */
3032 int origin
; /* Origin */
3033 int lseek_return
; /* Return value from callback to lseek. */
3035 fd
= h8300hmode
? GET_L_REG (0) : GET_W_REG (0);
3036 offset
= h8300hmode
? GET_L_REG (1) : GET_W_REG (1);
3037 origin
= h8300hmode
? GET_L_REG (2) : GET_W_REG (2);
3039 /* Callback lseek and return offset. */
3041 sim_callback
->lseek (sim_callback
, fd
, offset
, origin
);
3043 /* Return value in register 0. */
3044 h8_set_reg (sd
, 0, lseek_return
);
3048 case O (O_SYS_CLOSE
, SB
):
3050 int fd
; /* File descriptor */
3051 int close_return
; /* Return value from callback to close. */
3053 fd
= h8300hmode
? GET_L_REG (0) : GET_W_REG (0);
3055 /* Callback close and return. */
3056 close_return
= sim_callback
->close (sim_callback
, fd
);
3058 /* Return value in register 0. */
3059 h8_set_reg (sd
, 0, close_return
);
3063 case O (O_SYS_FSTAT
, SB
):
3065 int fd
; /* File descriptor */
3066 struct stat stat_rec
; /* Stat record */
3067 int fstat_return
; /* Return value from callback to stat. */
3068 int stat_ptr
; /* Pointer to stat record. */
3069 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
3071 fd
= h8300hmode
? GET_L_REG (0) : GET_W_REG (0);
3073 /* Setting stat_ptr to second argument of stat. */
3074 stat_ptr
= h8300hmode
? GET_L_REG (1) : GET_W_REG (1);
3076 /* Callback stat and return. */
3077 fstat_return
= sim_callback
->fstat (sim_callback
, fd
, &stat_rec
);
3079 /* Have stat_ptr point to starting of stat_rec. */
3080 temp_stat_ptr
= (char *) (&stat_rec
);
3082 /* Setting up the stat structure returned. */
3083 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
3085 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
3087 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
3089 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
3091 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
3093 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
3095 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
3097 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
3099 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
3101 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
3103 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
3105 /* Return value in register 0. */
3106 h8_set_reg (sd
, 0, fstat_return
);
3110 case O (O_SYS_STAT
, SB
):
3112 int len
= 0; /* Length of filename. */
3113 char *filename
; /* Filename would go here. */
3114 char temp_char
; /* Temporary character */
3115 int filename_ptr
; /* Pointer to filename in cpu memory. */
3116 struct stat stat_rec
; /* Stat record */
3117 int stat_return
; /* Return value from callback to stat */
3118 int stat_ptr
; /* Pointer to stat record. */
3119 char *temp_stat_ptr
; /* Temporary stat_rec pointer. */
3120 int i
= 0; /* Loop Counter */
3122 /* Setting filename_ptr to first argument of open. */
3123 filename_ptr
= h8300hmode
? GET_L_REG (0) : GET_W_REG (0);
3125 /* Trying to find the length of the filename. */
3126 temp_char
= GET_MEMORY_B (h8_get_reg (sd
, 0));
3129 while (temp_char
!= '\0')
3131 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
3135 /* Allocating space for the filename. */
3136 filename
= (char *) malloc (sizeof (char) * len
);
3138 /* String copying the filename from memory. */
3139 for (i
= 0; i
< len
; i
++)
3141 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
3142 filename
[i
] = temp_char
;
3145 /* Setting stat_ptr to second argument of stat. */
3146 /* stat_ptr = h8_get_reg (sd, 1); */
3147 stat_ptr
= h8300hmode
? GET_L_REG (1) : GET_W_REG (1);
3149 /* Callback stat and return. */
3151 sim_callback
->stat (sim_callback
, filename
, &stat_rec
);
3153 /* Have stat_ptr point to starting of stat_rec. */
3154 temp_stat_ptr
= (char *) (&stat_rec
);
3156 /* Freeing memory used for filename. */
3159 /* Setting up the stat structure returned. */
3160 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
3162 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
3164 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
3166 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
3168 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
3170 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
3172 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
3174 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
3176 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
3178 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
3180 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
3182 /* Return value in register 0. */
3183 h8_set_reg (sd
, 0, stat_return
);
3186 /* End of system call processing. */
3188 case O (O_NOT
, SB
): /* not.b */
3189 if (fetch2 (sd
, &code
->src
, &rd
))
3195 case O (O_NOT
, SW
): /* not.w */
3196 if (fetch2 (sd
, &code
->src
, &rd
))
3202 case O (O_NOT
, SL
): /* not.l */
3203 if (fetch2 (sd
, &code
->src
, &rd
))
3209 case O (O_SHLL
, SB
): /* shll.b */
3210 case O (O_SHLR
, SB
): /* shlr.b */
3211 if (fetch2 (sd
, &code
->dst
, &rd
))
3214 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3215 ea
= 1; /* unary op */
3216 else /* binary op */
3217 fetch (sd
, &code
->src
, &ea
);
3219 if (code
->opcode
== O (O_SHLL
, SB
))
3222 c
= rd
& (0x80 >> (ea
- 1));
3228 c
= rd
& (1 << (ea
- 1));
3229 rd
= (unsigned char) rd
>> ea
;
3233 case O (O_SHLL
, SW
): /* shll.w */
3234 case O (O_SHLR
, SW
): /* shlr.w */
3235 if (fetch2 (sd
, &code
->dst
, &rd
))
3238 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3239 ea
= 1; /* unary op */
3241 fetch (sd
, &code
->src
, &ea
);
3243 if (code
->opcode
== O (O_SHLL
, SW
))
3246 c
= rd
& (0x8000 >> (ea
- 1));
3252 c
= rd
& (1 << (ea
- 1));
3253 rd
= (unsigned short) rd
>> ea
;
3257 case O (O_SHLL
, SL
): /* shll.l */
3258 case O (O_SHLR
, SL
): /* shlr.l */
3259 if (fetch2 (sd
, &code
->dst
, &rd
))
3262 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3263 ea
= 1; /* unary op */
3265 fetch (sd
, &code
->src
, &ea
);
3267 if (code
->opcode
== O (O_SHLL
, SL
))
3270 c
= rd
& (0x80000000 >> (ea
- 1));
3276 c
= rd
& (1 << (ea
- 1));
3277 rd
= (unsigned int) rd
>> ea
;
3281 case O (O_SHAL
, SB
):
3282 case O (O_SHAR
, SB
):
3283 if (fetch2 (sd
, &code
->dst
, &rd
))
3286 if (code
->src
.type
== X (OP_IMM
, SB
))
3287 fetch (sd
, &code
->src
, &ea
);
3291 if (code
->opcode
== O (O_SHAL
, SB
))
3293 c
= rd
& (0x80 >> (ea
- 1));
3294 res
= rd
>> (7 - ea
);
3295 v
= ((res
& 1) && !(res
& 2))
3296 || (!(res
& 1) && (res
& 2));
3301 c
= rd
& (1 << (ea
- 1));
3303 rd
= ((signed char) rd
) >> ea
;
3307 case O (O_SHAL
, SW
):
3308 case O (O_SHAR
, SW
):
3309 if (fetch2 (sd
, &code
->dst
, &rd
))
3312 if (code
->src
.type
== X (OP_IMM
, SW
))
3313 fetch (sd
, &code
->src
, &ea
);
3317 if (code
->opcode
== O (O_SHAL
, SW
))
3319 c
= rd
& (0x8000 >> (ea
- 1));
3320 res
= rd
>> (15 - ea
);
3321 v
= ((res
& 1) && !(res
& 2))
3322 || (!(res
& 1) && (res
& 2));
3327 c
= rd
& (1 << (ea
- 1));
3329 rd
= ((signed short) rd
) >> ea
;
3333 case O (O_SHAL
, SL
):
3334 case O (O_SHAR
, SL
):
3335 if (fetch2 (sd
, &code
->dst
, &rd
))
3338 if (code
->src
.type
== X (OP_IMM
, SL
))
3339 fetch (sd
, &code
->src
, &ea
);
3343 if (code
->opcode
== O (O_SHAL
, SL
))
3345 c
= rd
& (0x80000000 >> (ea
- 1));
3346 res
= rd
>> (31 - ea
);
3347 v
= ((res
& 1) && !(res
& 2))
3348 || (!(res
& 1) && (res
& 2));
3353 c
= rd
& (1 << (ea
- 1));
3355 rd
= ((signed int) rd
) >> ea
;
3359 case O (O_ROTL
, SB
):
3360 case O (O_ROTR
, SB
):
3361 if (fetch2 (sd
, &code
->dst
, &rd
))
3364 if (code
->src
.type
== X (OP_IMM
, SB
))
3365 fetch (sd
, &code
->src
, &ea
);
3370 if (code
->opcode
== O (O_ROTL
, SB
))
3380 rd
= ((unsigned char) rd
) >> 1;
3388 case O (O_ROTL
, SW
):
3389 case O (O_ROTR
, SW
):
3390 if (fetch2 (sd
, &code
->dst
, &rd
))
3393 if (code
->src
.type
== X (OP_IMM
, SW
))
3394 fetch (sd
, &code
->src
, &ea
);
3399 if (code
->opcode
== O (O_ROTL
, SW
))
3409 rd
= ((unsigned short) rd
) >> 1;
3417 case O (O_ROTL
, SL
):
3418 case O (O_ROTR
, SL
):
3419 if (fetch2 (sd
, &code
->dst
, &rd
))
3422 if (code
->src
.type
== X (OP_IMM
, SL
))
3423 fetch (sd
, &code
->src
, &ea
);
3428 if (code
->opcode
== O (O_ROTL
, SL
))
3430 c
= rd
& 0x80000000;
3438 rd
= ((unsigned int) rd
) >> 1;
3446 case O (O_ROTXL
, SB
):
3447 case O (O_ROTXR
, SB
):
3448 if (fetch2 (sd
, &code
->dst
, &rd
))
3451 if (code
->src
.type
== X (OP_IMM
, SB
))
3452 fetch (sd
, &code
->src
, &ea
);
3457 if (code
->opcode
== O (O_ROTXL
, SB
))
3468 rd
= ((unsigned char) rd
) >> 1;
3477 case O (O_ROTXL
, SW
):
3478 case O (O_ROTXR
, SW
):
3479 if (fetch2 (sd
, &code
->dst
, &rd
))
3482 if (code
->src
.type
== X (OP_IMM
, SW
))
3483 fetch (sd
, &code
->src
, &ea
);
3488 if (code
->opcode
== O (O_ROTXL
, SW
))
3499 rd
= ((unsigned short) rd
) >> 1;
3508 case O (O_ROTXL
, SL
):
3509 case O (O_ROTXR
, SL
):
3510 if (fetch2 (sd
, &code
->dst
, &rd
))
3513 if (code
->src
.type
== X (OP_IMM
, SL
))
3514 fetch (sd
, &code
->src
, &ea
);
3519 if (code
->opcode
== O (O_ROTXL
, SL
))
3521 res
= rd
& 0x80000000;
3530 rd
= ((unsigned int) rd
) >> 1;
3541 case O (O_JMP
, SB
): /* jmp */
3543 fetch (sd
, &code
->src
, &pc
);
3548 case O (O_JSR
, SB
): /* jsr, jump to subroutine */
3550 if (fetch (sd
, &code
->src
, &pc
))
3553 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3558 SET_MEMORY_L (tmp
, code
->next_pc
);
3563 SET_MEMORY_W (tmp
, code
->next_pc
);
3565 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3571 case O (O_BSR
, SB
): /* bsr, branch to subroutine */
3572 if (fetch (sd
, &code
->src
, &res
))
3574 pc
= code
->next_pc
+ res
;
3577 case O (O_RTE
, SN
): /* rte, return from exception */
3579 /* Pops exr and ccr before pc -- otherwise identical to rts. */
3580 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3582 if (h8300smode
) /* pop exr */
3584 h8_set_exr (sd
, GET_MEMORY_L (tmp
));
3589 h8_set_ccr (sd
, GET_MEMORY_L (tmp
));
3591 pc
= GET_MEMORY_L (tmp
);
3596 h8_set_ccr (sd
, GET_MEMORY_W (tmp
));
3598 pc
= GET_MEMORY_W (tmp
);
3603 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3606 case O (O_RTS
, SN
): /* rts, return from subroutine */
3608 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3612 pc
= GET_MEMORY_L (tmp
);
3617 pc
= GET_MEMORY_W (tmp
);
3621 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3624 case O (O_ILL
, SB
): /* illegal */
3625 sim_engine_set_run_state (sd
, sim_stopped
, SIGILL
);
3628 case O (O_SLEEP
, SN
): /* sleep */
3629 /* Check for magic numbers in r1 and r2. */
3630 if ((h8_get_reg (sd
, R1_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC1
&&
3631 (h8_get_reg (sd
, R2_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC2
&&
3632 SIM_WIFEXITED (h8_get_reg (sd
, 0)))
3634 /* This trap comes from _exit, not from gdb. */
3635 sim_engine_set_run_state (sd
, sim_exited
,
3636 SIM_WEXITSTATUS (h8_get_reg (sd
, 0)));
3639 /* Unfortunately this won't really work, because
3640 when we take a breakpoint trap, R0 has a "random",
3641 user-defined value. Don't see any immediate solution. */
3642 else if (SIM_WIFSTOPPED (h8_get_reg (sd
, 0)))
3644 /* Pass the stop signal up to gdb. */
3645 sim_engine_set_run_state (sd
, sim_stopped
,
3646 SIM_WSTOPSIG (h8_get_reg (sd
, 0)));
3651 /* Treat it as a sigtrap. */
3652 sim_engine_set_run_state (sd
, sim_stopped
, SIGTRAP
);
3656 case O (O_TRAPA
, SB
): /* trapa */
3657 if (fetch (sd
, &code
->src
, &res
))
3658 goto end
; /* res is vector number. */
3660 tmp
= h8_get_reg (sd
, SP_REGNUM
);
3662 SET_MEMORY_L (tmp
, code
->next_pc
);
3664 SET_MEMORY_L (tmp
, h8_get_ccr (sd
));
3671 SET_MEMORY_L (tmp
, h8_get_exr (sd
));
3674 h8_set_reg (sd
, SP_REGNUM
, tmp
);
3676 /* FIXME: "normal" mode should use 2-byte ptrs. */
3677 pc
= GET_MEMORY_L (0x20 + res
* 4);
3681 sim_engine_set_run_state (sd
, sim_stopped
, SIGTRAP
);
3684 case O (O_BSETEQ
, SB
):
3689 case O (O_BSETNE
, SB
):
3694 case O (O_BCLREQ
, SB
):
3699 case O (O_BCLRNE
, SB
):
3704 OBITOP (O_BNOT
, 1, 1, ea
^= m
); /* bnot */
3705 OBITOP (O_BTST
, 1, 0, nz
= ea
& m
); /* btst */
3707 OBITOP (O_BSET
, 1, 1, ea
|= m
); /* bset */
3709 OBITOP (O_BCLR
, 1, 1, ea
&= ~m
); /* bclr */
3710 OBITOP (O_BLD
, 1, 0, c
= ea
& m
); /* bld */
3711 OBITOP (O_BILD
, 1, 0, c
= !(ea
& m
)); /* bild */
3712 OBITOP (O_BST
, 1, 1, ea
&= ~m
;
3713 if (C
) ea
|= m
); /* bst */
3714 OBITOP (O_BIST
, 1, 1, ea
&= ~m
;
3715 if (!C
) ea
|= m
); /* bist */
3716 OBITOP (O_BSTZ
, 1, 1, ea
&= ~m
;
3717 if (Z
) ea
|= m
); /* bstz */
3718 OBITOP (O_BISTZ
, 1, 1, ea
&= ~m
;
3719 if (!Z
) ea
|= m
); /* bistz */
3720 OBITOP (O_BAND
, 1, 0, c
= (ea
& m
) && C
); /* band */
3721 OBITOP (O_BIAND
, 1, 0, c
= !(ea
& m
) && C
); /* biand */
3722 OBITOP (O_BOR
, 1, 0, c
= (ea
& m
) || C
); /* bor */
3723 OBITOP (O_BIOR
, 1, 0, c
= !(ea
& m
) || C
); /* bior */
3724 OBITOP (O_BXOR
, 1, 0, c
= ((ea
& m
) != 0)!= C
); /* bxor */
3725 OBITOP (O_BIXOR
, 1, 0, c
= !(ea
& m
) != C
); /* bixor */
3727 case O (O_BFLD
, SB
): /* bfld */
3730 if (fetch (sd
, &code
->src
, &bit
))
3735 if (fetch (sd
, &code
->dst
, &ea
))
3745 if (store (sd
, &code
->op3
, ea
))
3750 case O(O_BFST
, SB
): /* bfst */
3751 /* bitfield store */
3752 /* NOTE: the imm8 value is in dst, and the ea value
3753 (which is actually the destination) is in op3.
3754 It has to be that way, to avoid breaking the assembler. */
3756 if (fetch (sd
, &code
->dst
, &bit
)) /* imm8 */
3758 if (bit
== 0) /* noop -- nothing to do. */
3761 if (fetch (sd
, &code
->src
, &rd
)) /* reg8 src */
3764 if (fetch2 (sd
, &code
->op3
, &ea
)) /* ea dst */
3767 /* Left-shift the register data into position. */
3768 for (tmp
= bit
; !(tmp
& 1); tmp
>>= 1)
3771 /* Combine it with the neighboring bits. */
3772 ea
= (ea
& ~bit
) | (rd
& bit
);
3775 if (store2 (sd
, &code
->op3
, ea
))
3779 case O (O_CLRMAC
, SN
): /* clrmac */
3780 h8_set_mach (sd
, 0);
3781 h8_set_macl (sd
, 0);
3782 h8_set_macZ (sd
, 1);
3783 h8_set_macV (sd
, 0);
3784 h8_set_macN (sd
, 0);
3787 case O (O_STMAC
, SL
): /* stmac, 260 */
3788 switch (code
->src
.type
) {
3789 case X (OP_MACH
, SL
):
3790 res
= h8_get_mach (sd
);
3791 if (res
& 0x200) /* sign extend */
3794 case X (OP_MACL
, SL
):
3795 res
= h8_get_macl (sd
);
3797 default: goto illegal
;
3799 nz
= !h8_get_macZ (sd
);
3800 n
= h8_get_macN (sd
);
3801 v
= h8_get_macV (sd
);
3803 if (store (sd
, &code
->dst
, res
))
3808 case O (O_LDMAC
, SL
): /* ldmac, 179 */
3809 if (fetch (sd
, &code
->src
, &rd
))
3812 switch (code
->dst
.type
) {
3813 case X (OP_MACH
, SL
):
3814 rd
&= 0x3ff; /* Truncate to 10 bits */
3815 h8_set_mach (sd
, rd
);
3817 case X (OP_MACL
, SL
):
3818 h8_set_macl (sd
, rd
);
3820 default: goto illegal
;
3822 h8_set_macV (sd
, 0);
3826 if (fetch (sd
, &code
->src
, &rd
) ||
3827 fetch (sd
, &code
->dst
, &res
))
3830 /* Ye gods, this is non-portable!
3831 However, the existing mul/div code is similar. */
3832 res
= SEXTSHORT (res
) * SEXTSHORT (rd
);
3834 if (h8_get_macS (sd
)) /* Saturating mode */
3836 long long mac
= h8_get_macl (sd
);
3838 if (mac
& 0x80000000) /* sign extend */
3839 mac
|= 0xffffffff00000000LL
;
3842 if (mac
> 0x7fffffff || mac
< 0xffffffff80000000LL
)
3843 h8_set_macV (sd
, 1);
3844 h8_set_macZ (sd
, (mac
== 0));
3845 h8_set_macN (sd
, (mac
< 0));
3846 h8_set_macl (sd
, (int) mac
);
3848 else /* "Less Saturating" mode */
3850 long long mac
= h8_get_mach (sd
);
3852 mac
+= h8_get_macl (sd
);
3854 if (mac
& 0x20000000000LL
) /* sign extend */
3855 mac
|= 0xfffffc0000000000LL
;
3858 if (mac
> 0x1ffffffffffLL
||
3859 mac
< (long long) 0xfffffe0000000000LL
)
3860 h8_set_macV (sd
, 1);
3861 h8_set_macZ (sd
, (mac
== 0));
3862 h8_set_macN (sd
, (mac
< 0));
3863 h8_set_macl (sd
, (int) mac
);
3865 h8_set_mach (sd
, (int) (mac
& 0x3ff));
3869 case O (O_MULS
, SW
): /* muls.w */
3870 if (fetch (sd
, &code
->src
, &ea
) ||
3871 fetch (sd
, &code
->dst
, &rd
))
3874 /* FIXME: is this the right place to be doing sign extend? */
3875 if (OP_KIND (code
->src
.type
) == OP_IMM
&&
3879 ea
= SEXTSHORT (ea
);
3881 res
= SEXTSHORT (ea
* SEXTSHORT (rd
));
3885 if (store (sd
, &code
->dst
, res
))
3890 case O (O_MULS
, SL
): /* muls.l */
3891 if (fetch (sd
, &code
->src
, &ea
) ||
3892 fetch (sd
, &code
->dst
, &rd
))
3895 /* FIXME: is this the right place to be doing sign extend? */
3896 if (OP_KIND (code
->src
.type
) == OP_IMM
&&
3902 n
= res
& 0x80000000;
3903 nz
= res
& 0xffffffff;
3904 if (store (sd
, &code
->dst
, res
))
3908 case O (O_MULSU
, SL
): /* muls/u.l */
3909 if (fetch (sd
, &code
->src
, &ea
) ||
3910 fetch (sd
, &code
->dst
, &rd
))
3913 /* FIXME: is this the right place to be doing sign extend? */
3914 if (OP_KIND (code
->src
.type
) == OP_IMM
&&
3918 /* Compute upper 32 bits of the 64-bit result. */
3919 res
= (((long long) ea
) * ((long long) rd
)) >> 32;
3921 n
= res
& 0x80000000;
3922 nz
= res
& 0xffffffff;
3923 if (store (sd
, &code
->dst
, res
))
3927 case O (O_MULU
, SW
): /* mulu.w */
3928 if (fetch (sd
, &code
->src
, &ea
) ||
3929 fetch (sd
, &code
->dst
, &rd
))
3932 res
= UEXTSHORT ((UEXTSHORT (ea
) * UEXTSHORT (rd
)));
3934 /* Don't set Z or N. */
3935 if (store (sd
, &code
->dst
, res
))
3940 case O (O_MULU
, SL
): /* mulu.l */
3941 if (fetch (sd
, &code
->src
, &ea
) ||
3942 fetch (sd
, &code
->dst
, &rd
))
3947 /* Don't set Z or N. */
3948 if (store (sd
, &code
->dst
, res
))
3953 case O (O_MULUU
, SL
): /* mulu/u.l */
3954 if (fetch (sd
, &code
->src
, &ea
) ||
3955 fetch (sd
, &code
->dst
, &rd
))
3958 /* Compute upper 32 bits of the 64-bit result. */
3959 res
= (((unsigned long long) (unsigned) ea
) *
3960 ((unsigned long long) (unsigned) rd
)) >> 32;
3962 /* Don't set Z or N. */
3963 if (store (sd
, &code
->dst
, res
))
3968 case O (O_MULXS
, SB
): /* mulxs.b */
3969 if (fetch (sd
, &code
->src
, &ea
) ||
3970 fetch (sd
, &code
->dst
, &rd
))
3973 /* FIXME: is this the right place to be doing sign extend? */
3974 if (OP_KIND (code
->src
.type
) == OP_IMM
&&
3980 res
= ea
* SEXTCHAR (rd
);
3984 if (store (sd
, &code
->dst
, res
))
3989 case O (O_MULXS
, SW
): /* mulxs.w */
3990 if (fetch (sd
, &code
->src
, &ea
) ||
3991 fetch (sd
, &code
->dst
, &rd
))
3994 /* FIXME: is this the right place to be doing sign extend? */
3995 if (OP_KIND (code
->src
.type
) == OP_IMM
&&
3999 ea
= SEXTSHORT (ea
);
4001 res
= ea
* SEXTSHORT (rd
& 0xffff);
4003 n
= res
& 0x80000000;
4004 nz
= res
& 0xffffffff;
4005 if (store (sd
, &code
->dst
, res
))
4010 case O (O_MULXU
, SB
): /* mulxu.b */
4011 if (fetch (sd
, &code
->src
, &ea
) ||
4012 fetch (sd
, &code
->dst
, &rd
))
4015 res
= UEXTCHAR (ea
) * UEXTCHAR (rd
);
4017 if (store (sd
, &code
->dst
, res
))
4022 case O (O_MULXU
, SW
): /* mulxu.w */
4023 if (fetch (sd
, &code
->src
, &ea
) ||
4024 fetch (sd
, &code
->dst
, &rd
))
4027 res
= UEXTSHORT (ea
) * UEXTSHORT (rd
);
4029 if (store (sd
, &code
->dst
, res
))
4034 case O (O_TAS
, SB
): /* tas (test and set) */
4035 if (!h8300sxmode
) /* h8sx can use any register. */
4036 switch (code
->src
.reg
)
4047 if (fetch (sd
, &code
->src
, &res
))
4049 if (store (sd
, &code
->src
, res
| 0x80))
4052 goto just_flags_log8
;
4054 case O (O_DIVU
, SW
): /* divu.w */
4055 if (fetch (sd
, &code
->src
, &ea
) ||
4056 fetch (sd
, &code
->dst
, &rd
))
4062 res
= (unsigned) (UEXTSHORT (rd
) / UEXTSHORT (ea
));
4066 if (store (sd
, &code
->dst
, res
))
4070 case O (O_DIVU
, SL
): /* divu.l */
4071 if (fetch (sd
, &code
->src
, &ea
) ||
4072 fetch (sd
, &code
->dst
, &rd
))
4075 n
= ea
& 0x80000000;
4076 nz
= ea
& 0xffffffff;
4078 res
= (unsigned) rd
/ ea
;
4082 if (store (sd
, &code
->dst
, res
))
4086 case O (O_DIVS
, SW
): /* divs.w */
4087 if (fetch (sd
, &code
->src
, &ea
) ||
4088 fetch (sd
, &code
->dst
, &rd
))
4091 /* FIXME: is this the right place to be doing sign extend? */
4092 if (OP_KIND (code
->src
.type
) == OP_IMM
&&
4098 res
= SEXTSHORT (rd
) / SEXTSHORT (ea
);
4108 if (store (sd
, &code
->dst
, res
))
4112 case O (O_DIVS
, SL
): /* divs.l */
4113 if (fetch (sd
, &code
->src
, &ea
) ||
4114 fetch (sd
, &code
->dst
, &rd
))
4117 /* FIXME: is this the right place to be doing sign extend? */
4118 if (OP_KIND (code
->src
.type
) == OP_IMM
&&
4133 n
= res
& 0x80000000;
4134 if (store (sd
, &code
->dst
, res
))
4138 case O (O_DIVXU
, SB
): /* divxu.b */
4139 if (fetch (sd
, &code
->src
, &ea
) ||
4140 fetch (sd
, &code
->dst
, &rd
))
4143 rd
= UEXTSHORT (rd
);
4150 tmp
= (unsigned) rd
% ea
;
4151 res
= (unsigned) rd
/ ea
;
4159 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
4163 case O (O_DIVXU
, SW
): /* divxu.w */
4164 if (fetch (sd
, &code
->src
, &ea
) ||
4165 fetch (sd
, &code
->dst
, &rd
))
4168 ea
= UEXTSHORT (ea
);
4174 tmp
= (unsigned) rd
% ea
;
4175 res
= (unsigned) rd
/ ea
;
4183 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
4187 case O (O_DIVXS
, SB
): /* divxs.b */
4188 if (fetch (sd
, &code
->src
, &ea
) ||
4189 fetch (sd
, &code
->dst
, &rd
))
4192 rd
= SEXTSHORT (rd
);
4194 /* FIXME: is this the right place to be doing sign extend? */
4195 if (OP_KIND (code
->src
.type
) == OP_IMM
&&
4203 tmp
= (int) rd
% (int) ea
;
4204 res
= (int) rd
/ (int) ea
;
4215 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
4219 case O (O_DIVXS
, SW
): /* divxs.w */
4220 if (fetch (sd
, &code
->src
, &ea
) ||
4221 fetch (sd
, &code
->dst
, &rd
))
4224 /* FIXME: is this the right place to be doing sign extend? */
4225 if (OP_KIND (code
->src
.type
) == OP_IMM
&&
4229 ea
= SEXTSHORT (ea
);
4233 tmp
= (int) rd
% (int) ea
;
4234 res
= (int) rd
/ (int) ea
;
4244 n
= res
& 0x80000000;
4245 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
4249 case O (O_EXTS
, SW
): /* exts.w, signed extend */
4250 if (fetch2 (sd
, &code
->dst
, &rd
))
4252 ea
= rd
& 0x80 ? -256 : 0;
4253 res
= (rd
& 0xff) + ea
;
4256 case O (O_EXTS
, SL
): /* exts.l, signed extend */
4257 if (fetch2 (sd
, &code
->dst
, &rd
))
4259 if (code
->src
.type
== X (OP_IMM
, SL
))
4261 if (fetch (sd
, &code
->src
, &ea
))
4264 if (ea
== 2) /* exts.l #2, nn */
4266 /* Sign-extend from 8-bit to 32-bit. */
4267 ea
= rd
& 0x80 ? -256 : 0;
4268 res
= (rd
& 0xff) + ea
;
4272 /* Sign-extend from 16-bit to 32-bit. */
4273 ea
= rd
& 0x8000 ? -65536 : 0;
4274 res
= (rd
& 0xffff) + ea
;
4277 case O (O_EXTU
, SW
): /* extu.w, unsigned extend */
4278 if (fetch2 (sd
, &code
->dst
, &rd
))
4281 res
= (rd
& 0xff) + ea
;
4284 case O (O_EXTU
, SL
): /* extu.l, unsigned extend */
4285 if (fetch2 (sd
, &code
->dst
, &rd
))
4287 if (code
->src
.type
== X (OP_IMM
, SL
))
4289 if (fetch (sd
, &code
->src
, &ea
))
4292 if (ea
== 2) /* extu.l #2, nn */
4294 /* Zero-extend from 8-bit to 32-bit. */
4296 res
= (rd
& 0xff) + ea
;
4300 /* Zero-extend from 16-bit to 32-bit. */
4302 res
= (rd
& 0xffff) + ea
;
4305 case O (O_NOP
, SN
): /* nop */
4308 case O (O_STM
, SL
): /* stm, store to memory */
4310 int nregs
, firstreg
, i
;
4312 nregs
= GET_MEMORY_B (pc
+ 1);
4315 firstreg
= code
->src
.reg
;
4317 for (i
= firstreg
; i
<= firstreg
+ nregs
; i
++)
4319 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) - 4);
4320 SET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
), h8_get_reg (sd
, i
));
4325 case O (O_LDM
, SL
): /* ldm, load from memory */
4326 case O (O_RTEL
, SN
): /* rte/l, ldm plus rte */
4327 case O (O_RTSL
, SN
): /* rts/l, ldm plus rts */
4329 int nregs
, firstreg
, i
;
4331 nregs
= ((GET_MEMORY_B (pc
+ 1) >> 4) & 0xf);
4332 firstreg
= code
->dst
.reg
& 0xf;
4333 for (i
= firstreg
; i
>= firstreg
- nregs
; i
--)
4335 h8_set_reg (sd
, i
, GET_MEMORY_L (h8_get_reg (sd
, SP_REGNUM
)));
4336 h8_set_reg (sd
, SP_REGNUM
, h8_get_reg (sd
, SP_REGNUM
) + 4);
4339 switch (code
->opcode
) {
4340 case O (O_RTEL
, SN
):
4342 case O (O_RTSL
, SN
):
4351 /* Decimal Adjust Addition. This is for BCD arithmetic. */
4352 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch? */
4353 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4354 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4355 res
= res
; /* Value added == 0. */
4356 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4357 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4358 res
= res
+ 0x6; /* Value added == 6. */
4359 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4360 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4361 res
= res
+ 0x6; /* Value added == 6. */
4362 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4363 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4364 res
= res
+ 0x60; /* Value added == 60. */
4365 else if (!c
&& (9 <= (res
>> 4) && (res
>> 4) <= 15) &&
4366 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4367 res
= res
+ 0x66; /* Value added == 66. */
4368 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4369 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4370 res
= res
+ 0x66; /* Value added == 66. */
4371 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4372 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4373 res
= res
+ 0x60; /* Value added == 60. */
4374 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4375 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4376 res
= res
+ 0x66; /* Value added == 66. */
4377 else if (c
&& (1 <= (res
>> 4) && (res
>> 4) <= 3) &&
4378 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4379 res
= res
+ 0x66; /* Value added == 66. */
4384 /* Decimal Adjust Subtraction. This is for BCD arithmetic. */
4385 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch, fetch2... */
4386 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4387 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4388 res
= res
; /* Value added == 0. */
4389 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4390 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4391 res
= res
+ 0xfa; /* Value added == 0xfa. */
4392 else if ( c
&& (7 <= (res
>> 4) && (res
>> 4) <= 15) &&
4393 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4394 res
= res
+ 0xa0; /* Value added == 0xa0. */
4395 else if (c
&& (6 <= (res
>> 4) && (res
>> 4) <= 15) &&
4396 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4397 res
= res
+ 0x9a; /* Value added == 0x9a. */
4403 sim_engine_set_run_state (sd
, sim_stopped
, SIGILL
);
4408 (*sim_callback
->printf_filtered
) (sim_callback
,
4409 "sim_resume: internal error.\n");
4410 sim_engine_set_run_state (sd
, sim_stopped
, SIGILL
);
4414 if (code
->dst
.type
== X (OP_CCR
, SB
) ||
4415 code
->dst
.type
== X (OP_CCR
, SW
))
4417 h8_set_ccr (sd
, res
);
4420 else if (h8300smode
&&
4421 (code
->dst
.type
== X (OP_EXR
, SB
) ||
4422 code
->dst
.type
== X (OP_EXR
, SW
)))
4424 h8_set_exr (sd
, res
);
4425 if (h8300smode
) /* Get exr. */
4427 trace
= (h8_get_exr (sd
) >> 7) & 1;
4428 intMask
= h8_get_exr (sd
) & 7;
4437 /* When a branch works */
4438 if (fetch (sd
, &code
->src
, &res
))
4440 if (res
& 1) /* bad address */
4442 pc
= code
->next_pc
+ res
;
4445 /* Set the cond codes from res */
4448 /* Set the flags after an 8 bit inc/dec operation */
4452 v
= (rd
& 0x7f) == 0x7f;
4455 /* Set the flags after an 16 bit inc/dec operation */
4459 v
= (rd
& 0x7fff) == 0x7fff;
4462 /* Set the flags after an 32 bit inc/dec operation */
4464 n
= res
& 0x80000000;
4465 nz
= res
& 0xffffffff;
4466 v
= (rd
& 0x7fffffff) == 0x7fffffff;
4470 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
4473 if (store2 (sd
, &code
->dst
, rd
))
4478 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
4481 if (store2 (sd
, &code
->dst
, rd
))
4486 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
4487 n
= (rd
& 0x80000000);
4488 nz
= rd
& 0xffffffff;
4489 if (store2 (sd
, &code
->dst
, rd
))
4494 if (store2 (sd
, &code
->dst
, res
))
4498 /* flags after a 32bit logical operation */
4499 n
= res
& 0x80000000;
4500 nz
= res
& 0xffffffff;
4505 if (store2 (sd
, &code
->dst
, res
))
4509 /* flags after a 16bit logical operation */
4516 if (store2 (sd
, &code
->dst
, res
))
4526 if (store2 (sd
, &code
->dst
, res
))
4533 switch (code
->opcode
/ 4)
4537 v
= ((rd
& 0x80) == (ea
& 0x80)
4538 && (rd
& 0x80) != (res
& 0x80));
4543 v
= ((rd
& 0x80) != (-ea
& 0x80)
4544 && (rd
& 0x80) != (res
& 0x80));
4551 break; /* No effect on v flag. */
4556 if (store2 (sd
, &code
->dst
, res
))
4562 c
= (res
& 0x10000);
4563 switch (code
->opcode
/ 4)
4567 v
= ((rd
& 0x8000) == (ea
& 0x8000)
4568 && (rd
& 0x8000) != (res
& 0x8000));
4573 v
= ((rd
& 0x8000) != (-ea
& 0x8000)
4574 && (rd
& 0x8000) != (res
& 0x8000));
4583 if (store2 (sd
, &code
->dst
, res
))
4587 n
= res
& 0x80000000;
4588 nz
= res
& 0xffffffff;
4589 switch (code
->opcode
/ 4)
4593 v
= ((rd
& 0x80000000) == (ea
& 0x80000000)
4594 && (rd
& 0x80000000) != (res
& 0x80000000));
4595 c
= ((unsigned) res
< (unsigned) rd
) ||
4596 ((unsigned) res
< (unsigned) ea
);
4601 v
= ((rd
& 0x80000000) != (-ea
& 0x80000000)
4602 && (rd
& 0x80000000) != (res
& 0x80000000));
4603 c
= (unsigned) rd
< (unsigned) -ea
;
4606 v
= (rd
== 0x80000000);
4613 if ((res
= h8_get_delayed_branch (sd
)) != 0)
4616 h8_set_delayed_branch (sd
, 0);
4623 if (--poll_count
< 0)
4625 poll_count
= POLL_QUIT_INTERVAL
;
4626 if ((*sim_callback
->poll_quit
) != NULL
4627 && (*sim_callback
->poll_quit
) (sim_callback
))
4628 sim_engine_set_run_state (sd
, sim_stopped
, SIGINT
);
4630 sim_engine_get_run_state (sd
, &reason
, &sigrc
);
4631 } while (reason
== sim_running
);
4633 h8_set_ticks (sd
, h8_get_ticks (sd
) + get_now () - tick_start
);
4634 h8_set_cycles (sd
, h8_get_cycles (sd
) + cycles
);
4635 h8_set_insts (sd
, h8_get_insts (sd
) + insts
);
4640 h8_set_exr (sd
, (trace
<<7) | intMask
);
4642 h8_set_mask (sd
, oldmask
);
4643 signal (SIGINT
, prev
);
4647 sim_trace (SIM_DESC sd
)
4649 /* FIXME: Unfinished. */
4650 (*sim_callback
->printf_filtered
) (sim_callback
,
4651 "sim_trace: trace not supported.\n");
4652 return 1; /* Done. */
4656 sim_write (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
4663 for (i
= 0; i
< size
; i
++)
4665 if (addr
< memory_size
)
4667 h8_set_memory (sd
, addr
+ i
, buffer
[i
]);
4668 h8_set_cache_idx (sd
, addr
+ i
, 0);
4672 h8_set_eightbit (sd
, (addr
+ i
) & 0xff, buffer
[i
]);
4679 sim_read (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
4684 if (addr
< memory_size
)
4685 memcpy (buffer
, h8_get_memory_buf (sd
) + addr
, size
);
4687 memcpy (buffer
, h8_get_eightbit_buf (sd
) + (addr
& 0xff), size
);
4693 sim_store_register (SIM_DESC sd
, int rn
, unsigned char *value
, int length
)
4698 longval
= (value
[0] << 24) | (value
[1] << 16) | (value
[2] << 8) | value
[3];
4699 shortval
= (value
[0] << 8) | (value
[1]);
4700 intval
= h8300hmode
? longval
: shortval
;
4706 h8_set_pc (sd
, intval
);
4709 (*sim_callback
->printf_filtered
) (sim_callback
,
4710 "sim_store_register: bad regnum %d.\n",
4720 h8_set_reg (sd
, rn
, intval
);
4723 h8_set_ccr (sd
, intval
);
4726 h8_set_exr (sd
, intval
);
4729 h8_set_sbr (sd
, intval
);
4732 h8_set_vbr (sd
, intval
);
4735 h8_set_mach (sd
, intval
);
4738 h8_set_macl (sd
, intval
);
4741 h8_set_cycles (sd
, longval
);
4745 h8_set_insts (sd
, longval
);
4749 h8_set_ticks (sd
, longval
);
4756 sim_fetch_register (SIM_DESC sd
, int rn
, unsigned char *buf
, int length
)
4763 if (!h8300smode
&& rn
>= EXR_REGNUM
)
4768 (*sim_callback
->printf_filtered
) (sim_callback
,
4769 "sim_fetch_register: bad regnum %d.\n",
4774 v
= h8_get_ccr (sd
);
4777 v
= h8_get_exr (sd
);
4783 v
= h8_get_sbr (sd
);
4786 v
= h8_get_vbr (sd
);
4789 v
= h8_get_mach (sd
);
4792 v
= h8_get_macl (sd
);
4802 v
= h8_get_reg (sd
, rn
);
4805 v
= h8_get_cycles (sd
);
4809 v
= h8_get_ticks (sd
);
4813 v
= h8_get_insts (sd
);
4817 if (h8300hmode
|| longreg
)
4833 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason
, int *sigrc
)
4835 sim_engine_get_run_state (sd
, reason
, sigrc
);
4838 /* FIXME: Rename to sim_set_mem_size. */
4843 /* Memory size is fixed. */
4847 set_simcache_size (SIM_DESC sd
, int n
)
4850 free (sd
->sim_cache
);
4853 sd
->sim_cache
= (decoded_inst
*) malloc (sizeof (decoded_inst
) * n
);
4854 memset (sd
->sim_cache
, 0, sizeof (decoded_inst
) * n
);
4855 sd
->sim_cache_size
= n
;
4860 sim_info (SIM_DESC sd
, int verbose
)
4862 double timetaken
= (double) h8_get_ticks (sd
) / (double) now_persec ();
4863 double virttime
= h8_get_cycles (sd
) / 10.0e6
;
4865 (*sim_callback
->printf_filtered
) (sim_callback
,
4866 "\n\n#instructions executed %10d\n",
4868 (*sim_callback
->printf_filtered
) (sim_callback
,
4869 "#cycles (v approximate) %10d\n",
4870 h8_get_cycles (sd
));
4871 (*sim_callback
->printf_filtered
) (sim_callback
,
4872 "#real time taken %10.4f\n",
4874 (*sim_callback
->printf_filtered
) (sim_callback
,
4875 "#virtual time taken %10.4f\n",
4877 if (timetaken
!= 0.0)
4878 (*sim_callback
->printf_filtered
) (sim_callback
,
4879 "#simulation ratio %10.4f\n",
4880 virttime
/ timetaken
);
4881 (*sim_callback
->printf_filtered
) (sim_callback
,
4883 h8_get_compiles (sd
));
4884 (*sim_callback
->printf_filtered
) (sim_callback
,
4885 "#cache size %10d\n",
4886 sd
->sim_cache_size
);
4889 /* This to be conditional on `what' (aka `verbose'),
4890 however it was never passed as non-zero. */
4894 for (i
= 0; i
< O_LAST
; i
++)
4896 if (h8_get_stats (sd
, i
))
4897 (*sim_callback
->printf_filtered
) (sim_callback
, "%d: %d\n",
4898 i
, h8_get_stats (sd
, i
));
4904 /* Indicate whether the cpu is an H8/300 or H8/300H.
4905 FLAG is non-zero for the H8/300H. */
4908 set_h8300h (unsigned long machine
)
4910 /* FIXME: Much of the code in sim_load can be moved to sim_open.
4911 This function being replaced by a sim_open:ARGV configuration
4914 if (machine
== bfd_mach_h8300sx
|| machine
== bfd_mach_h8300sxn
)
4917 if (machine
== bfd_mach_h8300s
|| machine
== bfd_mach_h8300sn
|| h8300sxmode
)
4920 if (machine
== bfd_mach_h8300h
|| machine
== bfd_mach_h8300hn
|| h8300smode
)
4924 /* Cover function of sim_state_free to free the cpu buffers as well. */
4927 free_state (SIM_DESC sd
)
4929 if (STATE_MODULES (sd
) != NULL
)
4930 sim_module_uninstall (sd
);
4932 /* Fixme: free buffers in _sim_cpu. */
4933 sim_state_free (sd
);
4937 sim_open (SIM_OPEN_KIND kind
,
4938 struct host_callback_struct
*callback
,
4945 sd
= sim_state_alloc (kind
, callback
);
4946 sd
->cpu
= sim_cpu_alloc (sd
, 0);
4947 cpu
= STATE_CPU (sd
, 0);
4948 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4949 sim_state_initialize (sd
, cpu
);
4950 /* sim_cpu object is new, so some initialization is needed. */
4951 init_pointers_needed
= 1;
4953 /* For compatibility (FIXME: is this right?). */
4954 current_alignment
= NONSTRICT_ALIGNMENT
;
4955 current_target_byte_order
= BIG_ENDIAN
;
4957 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
4963 /* getopt will print the error message so we just have to exit if
4964 this fails. FIXME: Hmmm... in the case of gdb we need getopt
4965 to call print_filtered. */
4966 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
4968 /* Uninstall the modules to avoid memory leaks,
4969 file descriptor leaks, etc. */
4974 /* Check for/establish the a reference program image. */
4975 if (sim_analyze_program (sd
,
4976 (STATE_PROG_ARGV (sd
) != NULL
4977 ? *STATE_PROG_ARGV (sd
)
4978 : NULL
), abfd
) != SIM_RC_OK
)
4984 /* Establish any remaining configuration options. */
4985 if (sim_config (sd
) != SIM_RC_OK
)
4991 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
4993 /* Uninstall the modules to avoid memory leaks,
4994 file descriptor leaks, etc. */
4999 /* sim_hw_configure (sd); */
5001 /* FIXME: Much of the code in sim_load can be moved here. */
5005 sim_callback
= callback
;
5010 sim_close (SIM_DESC sd
, int quitting
)
5012 /* Nothing to do. */
5015 /* Called by gdb to load a program into memory. */
5018 sim_load (SIM_DESC sd
, char *prog
, bfd
*abfd
, int from_tty
)
5022 /* FIXME: The code below that sets a specific variant of the H8/300
5023 being simulated should be moved to sim_open(). */
5025 /* See if the file is for the H8/300 or H8/300H. */
5026 /* ??? This may not be the most efficient way. The z8k simulator
5027 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
5031 prog_bfd
= bfd_openr (prog
, "coff-h8300");
5032 if (prog_bfd
!= NULL
)
5034 /* Set the cpu type. We ignore failure from bfd_check_format
5035 and bfd_openr as sim_load_file checks too. */
5036 if (bfd_check_format (prog_bfd
, bfd_object
))
5038 set_h8300h (bfd_get_mach (prog_bfd
));
5042 /* If we're using gdb attached to the simulator, then we have to
5043 reallocate memory for the simulator.
5045 When gdb first starts, it calls fetch_registers (among other
5046 functions), which in turn calls init_pointers, which allocates
5049 The problem is when we do that, we don't know whether we're
5050 debugging an H8/300 or H8/300H program.
5052 This is the first point at which we can make that determination,
5053 so we just reallocate memory now; this will also allow us to handle
5054 switching between H8/300 and H8/300H programs without exiting
5058 memory_size
= H8300S_MSIZE
;
5059 else if (h8300hmode
)
5060 memory_size
= H8300H_MSIZE
;
5062 memory_size
= H8300_MSIZE
;
5064 if (h8_get_memory_buf (sd
))
5065 free (h8_get_memory_buf (sd
));
5066 if (h8_get_cache_idx_buf (sd
))
5067 free (h8_get_cache_idx_buf (sd
));
5068 if (h8_get_eightbit_buf (sd
))
5069 free (h8_get_eightbit_buf (sd
));
5071 h8_set_memory_buf (sd
, (unsigned char *)
5072 calloc (sizeof (char), memory_size
));
5073 h8_set_cache_idx_buf (sd
, (unsigned short *)
5074 calloc (sizeof (short), memory_size
));
5075 h8_set_eightbit_buf (sd
, (unsigned char *) calloc (sizeof (char), 256));
5077 /* `msize' must be a power of two. */
5078 if ((memory_size
& (memory_size
- 1)) != 0)
5080 (*sim_callback
->printf_filtered
) (sim_callback
,
5081 "sim_load: bad memory size.\n");
5084 h8_set_mask (sd
, memory_size
- 1);
5086 if (sim_load_file (sd
, myname
, sim_callback
, prog
, prog_bfd
,
5087 sim_kind
== SIM_OPEN_DEBUG
,
5091 /* Close the bfd if we opened it. */
5092 if (abfd
== NULL
&& prog_bfd
!= NULL
)
5093 bfd_close (prog_bfd
);
5097 /* Close the bfd if we opened it. */
5098 if (abfd
== NULL
&& prog_bfd
!= NULL
)
5099 bfd_close (prog_bfd
);
5104 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
, char **argv
, char **env
)
5111 h8_set_pc (sd
, bfd_get_start_address (abfd
));
5115 /* Command Line support. */
5118 /* Counting the no. of commandline arguments. */
5119 for (no_of_args
= 0; argv
[no_of_args
] != NULL
; no_of_args
++)
5122 /* Allocating memory for the argv pointers. */
5123 h8_set_command_line (sd
, (char **) malloc ((sizeof (char *))
5124 * (no_of_args
+ 1)));
5126 for (i
= 0; i
< no_of_args
; i
++)
5128 /* Copying the argument string. */
5129 h8_set_cmdline_arg (sd
, i
, (char *) strdup (argv
[i
]));
5131 h8_set_cmdline_arg (sd
, i
, NULL
);
5138 sim_do_command (SIM_DESC sd
, char *cmd
)
5140 (*sim_callback
->printf_filtered
) (sim_callback
,
5141 "This simulator does not accept any commands.\n");
5145 sim_set_callbacks (struct host_callback_struct
*ptr
)