2 * Simulator for the Renesas (formerly Hitachi) H8/300 architecture.
4 * Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com
6 * This file is part of H8/300 sim
9 * THIS SOFTWARE IS NOT COPYRIGHTED
11 * Cygnus offers the following for use in the public domain. Cygnus makes no
12 * warranty with regard to the software or its performance and the user
13 * accepts the software "AS IS" with all faults.
15 * CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS
16 * SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE.
20 /* This must come before any other includes. */
26 #ifdef HAVE_SYS_PARAM_H
27 #include <sys/param.h>
33 #include "sim/sim-h8300.h"
34 #include "sys/types.h"
35 #include "sim-options.h"
36 #include "sim-signal.h"
37 #include "sim/callback.h"
39 #include "h8300-sim.h"
47 /* Each entry in this array is an index into the main opcode
48 array for the first instruction starting with the given
50 static int nib_indices
[16];
52 static int memory_size
;
54 #define X(op, size) (op * 4 + size)
56 #define SP (h8300hmode && !h8300_normal_mode ? SL : SW)
58 #define h8_opcodes ops
60 #include "opcode/h8300.h"
62 /* CPU data object: */
65 h8_get_reg (sim_cpu
*cpu
, int regnum
)
67 return H8300_SIM_CPU (cpu
)->regs
[regnum
];
71 h8_set_reg (sim_cpu
*cpu
, int regnum
, int val
)
73 H8300_SIM_CPU (cpu
)->regs
[regnum
] = val
;
76 #define h8_get_ccr(cpu) h8_get_reg (cpu, CCR_REGNUM)
77 #define h8_set_ccr(cpu, val) h8_set_reg (cpu, CCR_REGNUM, val)
78 #define h8_get_exr(cpu) h8_get_reg (cpu, EXR_REGNUM)
79 #define h8_set_exr(cpu, val) h8_set_reg (cpu, EXR_REGNUM, val)
80 #define h8_get_sbr(cpu) h8_get_reg (cpu, SBR_REGNUM)
81 #define h8_set_sbr(cpu, val) h8_set_reg (cpu, SBR_REGNUM, val)
82 #define h8_get_vbr(cpu) h8_get_reg (cpu, VBR_REGNUM)
83 #define h8_set_vbr(cpu, val) h8_set_reg (cpu, VBR_REGNUM, val)
84 #define h8_get_cycles(cpu) h8_get_reg (cpu, CYCLE_REGNUM)
85 #define h8_set_cycles(cpu, val) h8_set_reg (cpu, CYCLE_REGNUM, val)
86 #define h8_get_insts(cpu) h8_get_reg (cpu, INST_REGNUM)
87 #define h8_set_insts(cpu, val) h8_set_reg (cpu, INST_REGNUM, val)
88 #define h8_get_ticks(cpu) h8_get_reg (cpu, TICK_REGNUM)
89 #define h8_set_ticks(cpu, val) h8_set_reg (cpu, TICK_REGNUM, val)
90 #define h8_get_mach(cpu) h8_get_reg (cpu, MACH_REGNUM)
91 #define h8_set_mach(cpu, val) h8_set_reg (cpu, MACH_REGNUM, val)
92 #define h8_get_macl(cpu) h8_get_reg (cpu, MACL_REGNUM)
93 #define h8_set_macl(cpu, val) h8_set_reg (cpu, MACL_REGNUM, val)
96 h8_get_mask (sim_cpu
*cpu
)
98 return H8300_SIM_CPU (cpu
)->mask
;
102 h8_set_mask (sim_cpu
*cpu
, int val
)
104 H8300_SIM_CPU (cpu
)->mask
= val
;
108 h8_get_exception (sim_cpu
*cpu
)
110 return H8300_SIM_CPU (cpu
)->exception
;
114 h8_set_exception (sim_cpu
*cpu
, int val
)
116 H8300_SIM_CPU (cpu
)->exception
= val
;
119 static enum h8300_sim_state
120 h8_get_state (SIM_DESC sd
)
122 return H8300_SIM_STATE (sd
)->state
;
126 h8_set_state (SIM_DESC sd
, enum h8300_sim_state val
)
128 H8300_SIM_STATE (sd
)->state
= val
;
132 static unsigned int *
133 h8_get_reg_buf (sim_cpu
*cpu
)
135 return &H8300_SIM_CPU (cpu
)->regs
[0];
140 h8_get_stats (SIM_DESC sd
, int idx
)
142 return H8300_SIM_STATE (sd
)->stats
[idx
];
146 h8_increment_stats (SIM_DESC sd
, int idx
)
148 H8300_SIM_STATE (sd
)->stats
[idx
] ++;
152 static unsigned char *
153 h8_get_memory_buf (sim_cpu
*cpu
)
155 return H8300_SIM_CPU (cpu
)->memory
;
159 h8_set_memory_buf (sim_cpu
*cpu
, unsigned char *ptr
)
161 H8300_SIM_CPU (cpu
)->memory
= ptr
;
165 h8_get_memory (sim_cpu
*cpu
, int idx
)
167 ASSERT (idx
< memory_size
);
168 return H8300_SIM_CPU (cpu
)->memory
[idx
];
172 h8_set_memory (sim_cpu
*cpu
, int idx
, unsigned int val
)
174 ASSERT (idx
< memory_size
);
175 H8300_SIM_CPU (cpu
)->memory
[idx
] = (unsigned char) val
;
179 h8_get_delayed_branch (sim_cpu
*cpu
)
181 return H8300_SIM_CPU (cpu
)->delayed_branch
;
185 h8_set_delayed_branch (sim_cpu
*cpu
, unsigned int dest
)
187 H8300_SIM_CPU (cpu
)->delayed_branch
= dest
;
191 h8_get_command_line (sim_cpu
*cpu
)
193 return H8300_SIM_CPU (cpu
)->command_line
;
197 h8_set_command_line (sim_cpu
*cpu
, char ** val
)
199 H8300_SIM_CPU (cpu
)->command_line
= val
;
203 h8_get_cmdline_arg (sim_cpu
*cpu
, int index
)
205 return H8300_SIM_CPU (cpu
)->command_line
[index
];
209 h8_set_cmdline_arg (sim_cpu
*cpu
, int index
, char * val
)
211 H8300_SIM_CPU (cpu
)->command_line
[index
] = val
;
214 /* MAC Saturation Mode */
216 h8_get_macS (sim_cpu
*cpu
)
218 return H8300_SIM_CPU (cpu
)->macS
;
223 h8_set_macS (sim_cpu
*cpu
, int val
)
225 H8300_SIM_CPU (cpu
)->macS
= (val
!= 0);
231 h8_get_macZ (sim_cpu
*cpu
)
233 return H8300_SIM_CPU (cpu
)->macZ
;
237 h8_set_macZ (sim_cpu
*cpu
, int val
)
239 H8300_SIM_CPU (cpu
)->macZ
= (val
!= 0);
242 /* MAC Negative Flag */
244 h8_get_macN (sim_cpu
*cpu
)
246 return H8300_SIM_CPU (cpu
)->macN
;
250 h8_set_macN (sim_cpu
*cpu
, int val
)
252 H8300_SIM_CPU (cpu
)->macN
= (val
!= 0);
255 /* MAC Overflow Flag */
257 h8_get_macV (sim_cpu
*cpu
)
259 return H8300_SIM_CPU (cpu
)->macV
;
263 h8_set_macV (sim_cpu
*cpu
, int val
)
265 H8300_SIM_CPU (cpu
)->macV
= (val
!= 0);
268 /* End CPU data object. */
270 /* The rate at which to call the host's poll_quit callback. */
272 enum { POLL_QUIT_INTERVAL
= 0x80000 };
274 #define LOW_BYTE(x) ((x) & 0xff)
275 #define HIGH_BYTE(x) (((x) >> 8) & 0xff)
276 #define P(X, Y) ((X << 8) | Y)
285 #define I (intMaskBit != 0)
287 #define BUILDSR(cpu) \
288 h8_set_ccr (cpu, (I << 7) | (UI << 6) | (H << 5) | (U << 4) \
289 | (N << 3) | (Z << 2) | (V << 1) | C)
292 /* Get Status Register (flags). */ \
293 c = (h8_get_ccr (cpu) >> 0) & 1; \
294 v = (h8_get_ccr (cpu) >> 1) & 1; \
295 nz = !((h8_get_ccr (cpu) >> 2) & 1); \
296 n = (h8_get_ccr (cpu) >> 3) & 1; \
297 u = (h8_get_ccr (cpu) >> 4) & 1; \
298 h = (h8_get_ccr (cpu) >> 5) & 1; \
299 ui = ((h8_get_ccr (cpu) >> 6) & 1); \
300 intMaskBit = (h8_get_ccr (cpu) >> 7) & 1
303 #ifdef __CHAR_IS_SIGNED__
304 #define SEXTCHAR(x) ((char) (x))
308 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff) : x & 0xff)
311 #define UEXTCHAR(x) ((x) & 0xff)
312 #define UEXTSHORT(x) ((x) & 0xffff)
313 #define SEXTSHORT(x) ((short) (x))
317 int h8300_normal_mode
= 0;
323 return time (0); /* WinXX HAS UNIX like 'time', so why not use it? */
345 return (h8300hmode
&& !h8300_normal_mode
)? SL
: SW
;
350 /* Simulate an indirection / dereference.
351 return 0 for success, -1 for failure.
355 lvalue (SIM_DESC sd
, sim_cpu
*cpu
, int x
, int rn
, unsigned int *val
)
357 if (val
== NULL
) /* Paranoia. */
363 if (rn
== ZERO_REGNUM
)
364 *val
= X (OP_IMM
, SP
);
366 *val
= X (OP_REG
, SP
);
369 *val
= X (OP_MEM
, SP
);
372 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
379 cmdline_location(void)
381 if (h8300smode
&& !h8300_normal_mode
)
383 else if (h8300hmode
&& !h8300_normal_mode
)
390 decode (SIM_DESC sd
, sim_cpu
*cpu
, int addr
, unsigned char *data
, decoded_inst
*dst
)
392 int cst
[3] = {0, 0, 0};
393 int reg
[3] = {0, 0, 0};
394 int rdisp
[3] = {0, 0, 0};
397 const struct h8_opcode
*q
;
403 /* We speed up instruction decoding by caching an index into
404 the main opcode array for the first instruction with the
405 given 4 bit nibble. */
406 index
= nib_indices
[(data
[0] & 0xf0) >> 4];
408 /* Find the exact opcode/arg combo, starting with the precomputed
409 index. Note this loop is performance sensitive. */
410 for (q
= &h8_opcodes
[index
]; q
->name
; q
++)
412 const op_type
*nib
= q
->data
.nib
;
413 unsigned int len
= 0;
415 if ((q
->available
== AV_H8SX
&& !h8300sxmode
) ||
416 (q
->available
== AV_H8S
&& !h8300smode
) ||
417 (q
->available
== AV_H8H
&& !h8300hmode
))
420 cst
[0] = cst
[1] = cst
[2] = 0;
421 reg
[0] = reg
[1] = reg
[2] = 0;
422 rdisp
[0] = rdisp
[1] = rdisp
[2] = 0;
426 op_type looking_for
= *nib
;
427 int thisnib
= data
[len
/ 2];
429 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
430 opnum
= ((looking_for
& OP3
) ? 2 :
431 (looking_for
& DST
) ? 1 : 0);
433 if (looking_for
< 16 && looking_for
>= 0)
435 if (looking_for
!= thisnib
)
440 if (looking_for
& B31
)
442 if (!((thisnib
& 0x8) != 0))
445 looking_for
= (op_type
) (looking_for
& ~B31
);
448 else if (looking_for
& B30
)
450 if (!((thisnib
& 0x8) == 0))
453 looking_for
= (op_type
) (looking_for
& ~B30
);
456 if (looking_for
& B21
)
458 if (!((thisnib
& 0x4) != 0))
461 looking_for
= (op_type
) (looking_for
& ~B21
);
464 else if (looking_for
& B20
)
466 if (!((thisnib
& 0x4) == 0))
469 looking_for
= (op_type
) (looking_for
& ~B20
);
472 if (looking_for
& B11
)
474 if (!((thisnib
& 0x2) != 0))
477 looking_for
= (op_type
) (looking_for
& ~B11
);
480 else if (looking_for
& B10
)
482 if (!((thisnib
& 0x2) == 0))
485 looking_for
= (op_type
) (looking_for
& ~B10
);
488 if (looking_for
& B01
)
490 if (!((thisnib
& 0x1) != 0))
493 looking_for
= (op_type
) (looking_for
& ~B01
);
496 else if (looking_for
& B00
)
498 if (!((thisnib
& 0x1) == 0))
501 looking_for
= (op_type
) (looking_for
& ~B00
);
504 if (looking_for
& IGNORE
)
506 /* Hitachi has declared that IGNORE must be zero. */
510 else if ((looking_for
& MODE
) == DATA
)
512 ; /* Skip embedded data. */
514 else if ((looking_for
& MODE
) == DBIT
)
516 /* Exclude adds/subs by looking at bit 0 and 2, and
517 make sure the operand size, either w or l,
518 matches by looking at bit 1. */
519 if ((looking_for
& 7) != (thisnib
& 7))
522 cst
[opnum
] = (thisnib
& 0x8) ? 2 : 1;
524 else if ((looking_for
& MODE
) == REG
||
525 (looking_for
& MODE
) == LOWREG
||
526 (looking_for
& MODE
) == IND
||
527 (looking_for
& MODE
) == PREINC
||
528 (looking_for
& MODE
) == POSTINC
||
529 (looking_for
& MODE
) == PREDEC
||
530 (looking_for
& MODE
) == POSTDEC
)
532 reg
[opnum
] = thisnib
;
534 else if (looking_for
& CTRL
)
537 if (((looking_for
& MODE
) == CCR
&& (thisnib
!= C_CCR
)) ||
538 ((looking_for
& MODE
) == EXR
&& (thisnib
!= C_EXR
)) ||
539 ((looking_for
& MODE
) == MACH
&& (thisnib
!= C_MACH
)) ||
540 ((looking_for
& MODE
) == MACL
&& (thisnib
!= C_MACL
)) ||
541 ((looking_for
& MODE
) == VBR
&& (thisnib
!= C_VBR
)) ||
542 ((looking_for
& MODE
) == SBR
&& (thisnib
!= C_SBR
)))
544 if (((looking_for
& MODE
) == CCR_EXR
&&
545 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
)) ||
546 ((looking_for
& MODE
) == VBR_SBR
&&
547 (thisnib
!= C_VBR
&& thisnib
!= C_SBR
)) ||
548 ((looking_for
& MODE
) == MACREG
&&
549 (thisnib
!= C_MACH
&& thisnib
!= C_MACL
)))
551 if (((looking_for
& MODE
) == CC_EX_VB_SB
&&
552 (thisnib
!= C_CCR
&& thisnib
!= C_EXR
&&
553 thisnib
!= C_VBR
&& thisnib
!= C_SBR
)))
556 reg
[opnum
] = thisnib
;
558 else if ((looking_for
& MODE
) == ABS
)
560 /* Absolute addresses are unsigned. */
561 switch (looking_for
& SIZE
)
564 cst
[opnum
] = UEXTCHAR (data
[len
/ 2]);
568 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
572 (data
[len
/ 2 + 0] << 24) +
573 (data
[len
/ 2 + 1] << 16) +
574 (data
[len
/ 2 + 2] << 8) +
578 printf ("decode: bad size ABS: %d\n",
579 (looking_for
& SIZE
));
583 else if ((looking_for
& MODE
) == DISP
||
584 (looking_for
& MODE
) == PCREL
||
585 (looking_for
& MODE
) == INDEXB
||
586 (looking_for
& MODE
) == INDEXW
||
587 (looking_for
& MODE
) == INDEXL
)
589 switch (looking_for
& SIZE
)
592 cst
[opnum
] = thisnib
& 3;
595 cst
[opnum
] = SEXTCHAR (data
[len
/ 2]);
598 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
599 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
602 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
606 (data
[len
/ 2 + 0] << 24) +
607 (data
[len
/ 2 + 1] << 16) +
608 (data
[len
/ 2 + 2] << 8) +
612 printf ("decode: bad size DISP/PCREL/INDEX: %d\n",
613 (looking_for
& SIZE
));
617 else if ((looking_for
& SIZE
) == L_16
||
618 (looking_for
& SIZE
) == L_16U
)
620 cst
[opnum
] = (data
[len
/ 2] << 8) + data
[len
/ 2 + 1];
621 /* Immediates are always unsigned. */
622 if ((looking_for
& SIZE
) != L_16U
&&
623 (looking_for
& MODE
) != IMM
)
624 cst
[opnum
] = (short) cst
[opnum
]; /* Sign extend. */
626 else if (looking_for
& ABSJMP
)
628 switch (looking_for
& SIZE
) {
630 cst
[opnum
] = (data
[1] << 16) | (data
[2] << 8) | (data
[3]);
634 (data
[len
/ 2 + 0] << 24) +
635 (data
[len
/ 2 + 1] << 16) +
636 (data
[len
/ 2 + 2] << 8) +
640 printf ("decode: bad size ABSJMP: %d\n",
641 (looking_for
& SIZE
));
645 else if ((looking_for
& MODE
) == MEMIND
)
647 cst
[opnum
] = data
[1];
649 else if ((looking_for
& MODE
) == VECIND
)
651 if(h8300_normal_mode
)
652 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 2;
654 cst
[opnum
] = ((data
[1] & 0x7f) + 0x80) * 4;
655 cst
[opnum
] += h8_get_vbr (cpu
); /* Add vector base reg. */
657 else if ((looking_for
& SIZE
) == L_32
)
662 (data
[i
+ 0] << 24) |
663 (data
[i
+ 1] << 16) |
667 else if ((looking_for
& SIZE
) == L_24
)
672 (data
[i
+ 0] << 16) |
676 else if (looking_for
& DISPREG
)
678 rdisp
[opnum
] = thisnib
& 0x7;
680 else if ((looking_for
& MODE
) == KBIT
)
697 else if ((looking_for
& SIZE
) == L_8
)
699 if ((looking_for
& MODE
) == ABS
)
701 /* Will be combined with contents of SBR_REGNUM
702 by fetch (). For all modes except h8sx, this
703 will always contain the value 0xFFFFFF00. */
704 cst
[opnum
] = data
[len
/ 2] & 0xff;
708 cst
[opnum
] = data
[len
/ 2] & 0xff;
711 else if ((looking_for
& SIZE
) == L_2
)
713 cst
[opnum
] = thisnib
& 3;
715 else if ((looking_for
& SIZE
) == L_3
||
716 (looking_for
& SIZE
) == L_3NZ
)
718 cst
[opnum
] = thisnib
& 7;
719 if (cst
[opnum
] == 0 && (looking_for
& SIZE
) == L_3NZ
)
722 else if ((looking_for
& SIZE
) == L_4
)
724 cst
[opnum
] = thisnib
& 15;
726 else if ((looking_for
& SIZE
) == L_5
)
728 cst
[opnum
] = data
[len
/ 2] & 0x1f;
730 else if (looking_for
== E
)
735 /* Fill in the args. */
737 const op_type
*args
= q
->args
.nib
;
741 nargs
< 3 && *args
!= E
;
747 opnum
= ((x
& OP3
) ? 2 :
756 if ((x
& MODE
) == IMM
||
757 (x
& MODE
) == KBIT
||
760 /* Use the instruction to determine
762 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
763 p
->literal
= cst
[opnum
];
765 else if ((x
& MODE
) == CONST_2
||
766 (x
& MODE
) == CONST_4
||
767 (x
& MODE
) == CONST_8
||
768 (x
& MODE
) == CONST_16
)
770 /* Use the instruction to determine
772 p
->type
= X (OP_IMM
, OP_SIZE (q
->how
));
774 case CONST_2
: p
->literal
= 2; break;
775 case CONST_4
: p
->literal
= 4; break;
776 case CONST_8
: p
->literal
= 8; break;
777 case CONST_16
: p
->literal
= 16; break;
780 else if ((x
& MODE
) == REG
)
782 p
->type
= X (OP_REG
, bitfrom (x
));
785 else if ((x
& MODE
) == LOWREG
)
787 p
->type
= X (OP_LOWREG
, bitfrom (x
));
790 else if ((x
& MODE
) == PREINC
)
792 /* Use the instruction to determine
794 p
->type
= X (OP_PREINC
, OP_SIZE (q
->how
));
795 p
->reg
= reg
[opnum
] & 0x7;
797 else if ((x
& MODE
) == POSTINC
)
799 /* Use the instruction to determine
801 p
->type
= X (OP_POSTINC
, OP_SIZE (q
->how
));
802 p
->reg
= reg
[opnum
] & 0x7;
804 else if ((x
& MODE
) == PREDEC
)
806 /* Use the instruction to determine
808 p
->type
= X (OP_PREDEC
, OP_SIZE (q
->how
));
809 p
->reg
= reg
[opnum
] & 0x7;
811 else if ((x
& MODE
) == POSTDEC
)
813 /* Use the instruction to determine
815 p
->type
= X (OP_POSTDEC
, OP_SIZE (q
->how
));
816 p
->reg
= reg
[opnum
] & 0x7;
818 else if ((x
& MODE
) == IND
)
820 /* Note: an indirect is transformed into
821 a displacement of zero.
823 /* Use the instruction to determine
825 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
826 p
->reg
= reg
[opnum
] & 0x7;
828 if (OP_KIND (q
->how
) == O_JSR
||
829 OP_KIND (q
->how
) == O_JMP
)
830 if (lvalue (sd
, cpu
, p
->type
, p
->reg
, (unsigned int *)&p
->type
))
833 else if ((x
& MODE
) == ABS
)
835 /* Note: a 16 or 32 bit ABS is transformed into a
836 displacement from pseudo-register ZERO_REGNUM,
837 which is always zero. An 8 bit ABS becomes
838 a displacement from SBR_REGNUM.
840 /* Use the instruction to determine
842 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
843 p
->literal
= cst
[opnum
];
845 /* 8-bit ABS is displacement from SBR.
846 16 and 32-bit ABS are displacement from ZERO.
847 (SBR will always be zero except for h8/sx)
849 if ((x
& SIZE
) == L_8
)
852 p
->reg
= ZERO_REGNUM
;;
854 else if ((x
& MODE
) == MEMIND
||
855 (x
& MODE
) == VECIND
)
857 /* Size doesn't matter. */
858 p
->type
= X (OP_MEM
, SB
);
859 p
->literal
= cst
[opnum
];
860 if (OP_KIND (q
->how
) == O_JSR
||
861 OP_KIND (q
->how
) == O_JMP
)
862 if (lvalue (sd
, cpu
, p
->type
, p
->reg
, (unsigned int *)&p
->type
))
865 else if ((x
& MODE
) == PCREL
)
867 /* Size doesn't matter. */
868 p
->type
= X (OP_PCREL
, SB
);
869 p
->literal
= cst
[opnum
];
873 p
->type
= X (OP_IMM
, SP
);
874 p
->literal
= cst
[opnum
];
876 else if ((x
& MODE
) == INDEXB
)
878 p
->type
= X (OP_INDEXB
, OP_SIZE (q
->how
));
879 p
->literal
= cst
[opnum
];
880 p
->reg
= rdisp
[opnum
];
882 else if ((x
& MODE
) == INDEXW
)
884 p
->type
= X (OP_INDEXW
, OP_SIZE (q
->how
));
885 p
->literal
= cst
[opnum
];
886 p
->reg
= rdisp
[opnum
];
888 else if ((x
& MODE
) == INDEXL
)
890 p
->type
= X (OP_INDEXL
, OP_SIZE (q
->how
));
891 p
->literal
= cst
[opnum
];
892 p
->reg
= rdisp
[opnum
];
894 else if ((x
& MODE
) == DISP
)
896 /* Yuck -- special for mova args. */
897 if (strncmp (q
->name
, "mova", 4) == 0 &&
900 /* Mova can have a DISP2 dest, with an
901 INDEXB or INDEXW src. The multiplier
902 for the displacement value is determined
903 by the src operand, not by the insn. */
905 switch (OP_KIND (dst
->src
.type
))
908 p
->type
= X (OP_DISP
, SB
);
909 p
->literal
= cst
[opnum
];
912 p
->type
= X (OP_DISP
, SW
);
913 p
->literal
= cst
[opnum
] * 2;
921 p
->type
= X (OP_DISP
, OP_SIZE (q
->how
));
922 p
->literal
= cst
[opnum
];
923 /* DISP2 is special. */
924 if ((x
& SIZE
) == L_2
)
925 switch (OP_SIZE (q
->how
))
928 case SW
: p
->literal
*= 2; break;
929 case SL
: p
->literal
*= 4; break;
932 p
->reg
= rdisp
[opnum
];
939 p
->type
= X (OP_CCR
, SB
);
942 p
->type
= X (OP_EXR
, SB
);
945 p
->type
= X (OP_MACH
, SL
);
948 p
->type
= X (OP_MACL
, SL
);
951 p
->type
= X (OP_VBR
, SL
);
954 p
->type
= X (OP_SBR
, SL
);
958 else if ((x
& MODE
) == CCR
)
962 else if ((x
& MODE
) == EXR
)
967 printf ("Hmmmm 0x%x...\n", x
);
973 /* Unary operators: treat src and dst as equivalent. */
974 if (dst
->dst
.type
== -1)
976 if (dst
->src
.type
== -1)
979 dst
->opcode
= q
->how
;
980 dst
->cycles
= q
->time
;
982 /* And jsr's to these locations are turned into
985 if (OP_KIND (dst
->opcode
) == O_JSR
)
987 switch (dst
->src
.literal
)
990 dst
->opcode
= O (O_SYS_OPEN
, SB
);
993 dst
->opcode
= O (O_SYS_READ
, SB
);
996 dst
->opcode
= O (O_SYS_WRITE
, SB
);
999 dst
->opcode
= O (O_SYS_LSEEK
, SB
);
1002 dst
->opcode
= O (O_SYS_CLOSE
, SB
);
1005 dst
->opcode
= O (O_SYS_STAT
, SB
);
1008 dst
->opcode
= O (O_SYS_FSTAT
, SB
);
1011 dst
->opcode
= O (O_SYS_CMDLINE
, SB
);
1014 /* End of Processing for system calls. */
1017 /* Use same register is specified for source
1019 The value of source will be the value after
1020 address calculation. */
1021 if (OP_KIND (dst
->opcode
) != O_CMP
&&
1022 OP_KIND (dst
->src
.type
) == OP_REG
&&
1023 (dst
->src
.reg
& 7) == dst
->dst
.reg
) {
1024 switch (OP_KIND (dst
->dst
.type
))
1027 dst
->src
.type
= X (OP_REG_DEC
,
1028 OP_SIZE (dst
->dst
.type
));
1031 dst
->src
.type
= X (OP_REG_INC
,
1032 OP_SIZE (dst
->dst
.type
));
1035 if (OP_KIND (dst
->opcode
) == O_MOV
)
1036 dst
->src
.type
= X (OP_REG_INC
,
1037 OP_SIZE (dst
->dst
.type
));
1040 if (OP_KIND (dst
->opcode
) == O_MOV
)
1041 dst
->src
.type
= X (OP_REG_DEC
,
1042 OP_SIZE (dst
->dst
.type
));
1046 dst
->next_pc
= addr
+ len
/ 2;
1050 printf ("Don't understand 0x%x \n", looking_for
);
1061 /* Fell off the end. */
1062 dst
->opcode
= O (O_ILL
, SB
);
1065 static unsigned char *breg
[32];
1066 static unsigned short *wreg
[16];
1068 #define GET_B_REG(X) *(breg[X])
1069 #define SET_B_REG(X, Y) (*(breg[X])) = (Y)
1070 #define GET_W_REG(X) *(wreg[X])
1071 #define SET_W_REG(X, Y) (*(wreg[X])) = (Y)
1072 #define GET_L_REG(X) h8_get_reg (cpu, X)
1073 #define SET_L_REG(X, Y) h8_set_reg (cpu, X, Y)
1075 #define GET_MEMORY_L(X) \
1076 ((X) < memory_size \
1077 ? ((h8_get_memory (cpu, (X)+0) << 24) | (h8_get_memory (cpu, (X)+1) << 16) \
1078 | (h8_get_memory (cpu, (X)+2) << 8) | (h8_get_memory (cpu, (X)+3) << 0)) \
1081 #define GET_MEMORY_W(X) \
1082 ((X) < memory_size \
1083 ? ((h8_get_memory (cpu, (X)+0) << 8) | (h8_get_memory (cpu, (X)+1) << 0)) \
1086 #define GET_MEMORY_B(X) \
1087 ((X) < memory_size ? h8_get_memory (cpu, (X)) : 0)
1089 #define SET_MEMORY_L(X, Y) \
1090 { register unsigned char *_p; register int __y = (Y); \
1091 _p = ((X) < memory_size ? h8_get_memory_buf (cpu) + (X) : 0); \
1092 _p[0] = __y >> 24; _p[1] = __y >> 16; \
1093 _p[2] = __y >> 8; _p[3] = __y >> 0; \
1096 #define SET_MEMORY_W(X, Y) \
1097 { register unsigned char *_p; register int __y = (Y); \
1098 _p = ((X) < memory_size ? h8_get_memory_buf (cpu) + (X) : 0); \
1099 _p[0] = __y >> 8; _p[1] = __y; \
1102 #define SET_MEMORY_B(X, Y) \
1103 ((X) < memory_size ? h8_set_memory (cpu, (X), (Y)) : 0)
1105 /* Simulate a memory fetch.
1106 Return 0 for success, -1 for failure.
1110 fetch_1 (SIM_DESC sd
, ea_type
*arg
, int *val
, int twice
)
1112 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1114 int abs
= arg
->literal
;
1119 return -1; /* Paranoia. */
1123 /* Indexed register plus displacement mode:
1125 This new family of addressing modes are similar to OP_DISP
1126 (register plus displacement), with two differences:
1127 1) INDEXB uses only the least significant byte of the register,
1128 INDEXW uses only the least significant word, and
1129 INDEXL uses the entire register (just like OP_DISP).
1131 2) The displacement value in abs is multiplied by two
1132 for SW-sized operations, and by four for SL-size.
1134 This gives nine possible variations.
1137 case X (OP_INDEXB
, SB
):
1138 case X (OP_INDEXB
, SW
):
1139 case X (OP_INDEXB
, SL
):
1140 case X (OP_INDEXW
, SB
):
1141 case X (OP_INDEXW
, SW
):
1142 case X (OP_INDEXW
, SL
):
1143 case X (OP_INDEXL
, SB
):
1144 case X (OP_INDEXL
, SW
):
1145 case X (OP_INDEXL
, SL
):
1147 switch (OP_KIND (arg
->type
)) {
1148 case OP_INDEXB
: t
&= 0xff; break;
1149 case OP_INDEXW
: t
&= 0xffff; break;
1153 switch (OP_SIZE (arg
->type
)) {
1155 *val
= GET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (cpu
));
1158 *val
= GET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (cpu
));
1161 *val
= GET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (cpu
));
1166 case X (OP_LOWREG
, SB
):
1167 *val
= GET_L_REG (rn
) & 0xff;
1169 case X (OP_LOWREG
, SW
):
1170 *val
= GET_L_REG (rn
) & 0xffff;
1173 case X (OP_REG
, SB
): /* Register direct, byte. */
1174 *val
= GET_B_REG (rn
);
1176 case X (OP_REG
, SW
): /* Register direct, word. */
1177 *val
= GET_W_REG (rn
);
1179 case X (OP_REG
, SL
): /* Register direct, long. */
1180 *val
= GET_L_REG (rn
);
1182 case X (OP_IMM
, SB
): /* Immediate, byte. */
1183 case X (OP_IMM
, SW
): /* Immediate, word. */
1184 case X (OP_IMM
, SL
): /* Immediate, long. */
1187 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr: byte. */
1189 r
= GET_MEMORY_B (t
& h8_get_mask (cpu
));
1195 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr: word. */
1197 r
= GET_MEMORY_W (t
& h8_get_mask (cpu
));
1203 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr: long. */
1205 r
= GET_MEMORY_L (t
& h8_get_mask (cpu
));
1212 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr: byte. */
1214 r
= GET_MEMORY_B (t
& h8_get_mask (cpu
));
1220 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr: word. */
1222 r
= GET_MEMORY_W (t
& h8_get_mask (cpu
));
1228 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr: long. */
1230 r
= GET_MEMORY_L (t
& h8_get_mask (cpu
));
1237 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr: byte. */
1238 t
= GET_L_REG (rn
) - 1;
1240 t
&= h8_get_mask (cpu
);
1241 *val
= GET_MEMORY_B (t
);
1244 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr: word. */
1245 t
= GET_L_REG (rn
) - 2;
1247 t
&= h8_get_mask (cpu
);
1248 *val
= GET_MEMORY_W (t
);
1251 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr: long. */
1252 t
= GET_L_REG (rn
) - 4;
1254 t
&= h8_get_mask (cpu
);
1255 *val
= GET_MEMORY_L (t
);
1258 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr: byte. */
1259 t
= GET_L_REG (rn
) + 1;
1261 t
&= h8_get_mask (cpu
);
1262 *val
= GET_MEMORY_B (t
);
1265 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr: long. */
1266 t
= GET_L_REG (rn
) + 2;
1268 t
&= h8_get_mask (cpu
);
1269 *val
= GET_MEMORY_W (t
);
1272 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr: long. */
1273 t
= GET_L_REG (rn
) + 4;
1275 t
&= h8_get_mask (cpu
);
1276 *val
= GET_MEMORY_L (t
);
1279 case X (OP_DISP
, SB
): /* Register indirect w/displacement: byte. */
1280 t
= GET_L_REG (rn
) + abs
;
1281 t
&= h8_get_mask (cpu
);
1282 *val
= GET_MEMORY_B (t
);
1285 case X (OP_DISP
, SW
): /* Register indirect w/displacement: word. */
1286 t
= GET_L_REG (rn
) + abs
;
1287 t
&= h8_get_mask (cpu
);
1288 *val
= GET_MEMORY_W (t
);
1291 case X (OP_DISP
, SL
): /* Register indirect w/displacement: long. */
1292 t
= GET_L_REG (rn
) + abs
;
1293 t
&= h8_get_mask (cpu
);
1294 *val
=GET_MEMORY_L (t
);
1297 case X (OP_MEM
, SL
): /* Absolute memory address, long. */
1298 t
= GET_MEMORY_L (abs
);
1299 t
&= h8_get_mask (cpu
);
1303 case X (OP_MEM
, SW
): /* Absolute memory address, word. */
1304 t
= GET_MEMORY_W (abs
);
1305 t
&= h8_get_mask (cpu
);
1309 case X (OP_PCREL
, SB
): /* PC relative (for jump, branch etc). */
1310 case X (OP_PCREL
, SW
):
1311 case X (OP_PCREL
, SL
):
1312 case X (OP_PCREL
, SN
):
1316 case X (OP_REG_DEC
, SB
): /* Register direct, affected decrement byte. */
1317 *val
= GET_B_REG (rn
) - 1;
1319 case X (OP_REG_DEC
, SW
): /* Register direct, affected decrement word. */
1320 *val
= GET_W_REG (rn
) - 2;
1322 case X (OP_REG_DEC
, SL
): /* Register direct, affected decrement long. */
1323 *val
= GET_L_REG (rn
) - 4;
1325 case X (OP_REG_INC
, SB
): /* Register direct, affected increment byte. */
1326 *val
= GET_B_REG (rn
) + 1;
1328 case X (OP_REG_INC
, SW
): /* Register direct, affected increment word. */
1329 *val
= GET_W_REG (rn
) + 2;
1331 case X (OP_REG_INC
, SL
): /* Register direct, affected increment long. */
1332 *val
= GET_L_REG (rn
) + 4;
1335 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1337 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
1340 return 0; /* Success. */
1346 fetch (SIM_DESC sd
, ea_type
*arg
, int *val
)
1348 return fetch_1 (sd
, arg
, val
, 0);
1351 /* Fetch which will be followed by a store to the same location.
1352 The difference being that we don't want to do a post-increment
1353 or post-decrement at this time: we'll do it when we store. */
1356 fetch2 (SIM_DESC sd
, ea_type
*arg
, int *val
)
1358 return fetch_1 (sd
, arg
, val
, 1);
1361 /* Simulate a memory store.
1362 Return 0 for success, -1 for failure.
1366 store_1 (SIM_DESC sd
, ea_type
*arg
, int n
, int twice
)
1368 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1370 int abs
= arg
->literal
;
1375 /* Indexed register plus displacement mode:
1377 This new family of addressing modes are similar to OP_DISP
1378 (register plus displacement), with two differences:
1379 1) INDEXB uses only the least significant byte of the register,
1380 INDEXW uses only the least significant word, and
1381 INDEXL uses the entire register (just like OP_DISP).
1383 2) The displacement value in abs is multiplied by two
1384 for SW-sized operations, and by four for SL-size.
1386 This gives nine possible variations.
1389 case X (OP_INDEXB
, SB
):
1390 case X (OP_INDEXB
, SW
):
1391 case X (OP_INDEXB
, SL
):
1392 case X (OP_INDEXW
, SB
):
1393 case X (OP_INDEXW
, SW
):
1394 case X (OP_INDEXW
, SL
):
1395 case X (OP_INDEXL
, SB
):
1396 case X (OP_INDEXL
, SW
):
1397 case X (OP_INDEXL
, SL
):
1399 switch (OP_KIND (arg
->type
)) {
1400 case OP_INDEXB
: t
&= 0xff; break;
1401 case OP_INDEXW
: t
&= 0xffff; break;
1405 switch (OP_SIZE (arg
->type
)) {
1407 SET_MEMORY_B ((t
* 1 + abs
) & h8_get_mask (cpu
), n
);
1410 SET_MEMORY_W ((t
* 2 + abs
) & h8_get_mask (cpu
), n
);
1413 SET_MEMORY_L ((t
* 4 + abs
) & h8_get_mask (cpu
), n
);
1418 case X (OP_REG
, SB
): /* Register direct, byte. */
1421 case X (OP_REG
, SW
): /* Register direct, word. */
1424 case X (OP_REG
, SL
): /* Register direct, long. */
1428 case X (OP_PREDEC
, SB
): /* Register indirect w/pre-decr, byte. */
1433 t
&= h8_get_mask (cpu
);
1434 SET_MEMORY_B (t
, n
);
1437 case X (OP_PREDEC
, SW
): /* Register indirect w/pre-decr, word. */
1442 t
&= h8_get_mask (cpu
);
1443 SET_MEMORY_W (t
, n
);
1446 case X (OP_PREDEC
, SL
): /* Register indirect w/pre-decr, long. */
1451 t
&= h8_get_mask (cpu
);
1452 SET_MEMORY_L (t
, n
);
1455 case X (OP_PREINC
, SB
): /* Register indirect w/pre-incr, byte. */
1460 t
&= h8_get_mask (cpu
);
1461 SET_MEMORY_B (t
, n
);
1464 case X (OP_PREINC
, SW
): /* Register indirect w/pre-incr, word. */
1469 t
&= h8_get_mask (cpu
);
1470 SET_MEMORY_W (t
, n
);
1473 case X (OP_PREINC
, SL
): /* Register indirect w/pre-incr, long. */
1478 t
&= h8_get_mask (cpu
);
1479 SET_MEMORY_L (t
, n
);
1482 case X (OP_POSTDEC
, SB
): /* Register indirect w/post-decr, byte. */
1484 SET_L_REG (rn
, t
- 1);
1485 t
&= h8_get_mask (cpu
);
1486 SET_MEMORY_B (t
, n
);
1489 case X (OP_POSTDEC
, SW
): /* Register indirect w/post-decr, word. */
1491 SET_L_REG (rn
, t
- 2);
1492 t
&= h8_get_mask (cpu
);
1493 SET_MEMORY_W (t
, n
);
1496 case X (OP_POSTDEC
, SL
): /* Register indirect w/post-decr, long. */
1498 SET_L_REG (rn
, t
- 4);
1499 t
&= h8_get_mask (cpu
);
1500 SET_MEMORY_L (t
, n
);
1503 case X (OP_POSTINC
, SB
): /* Register indirect w/post-incr, byte. */
1505 SET_L_REG (rn
, t
+ 1);
1506 t
&= h8_get_mask (cpu
);
1507 SET_MEMORY_B (t
, n
);
1510 case X (OP_POSTINC
, SW
): /* Register indirect w/post-incr, word. */
1512 SET_L_REG (rn
, t
+ 2);
1513 t
&= h8_get_mask (cpu
);
1514 SET_MEMORY_W (t
, n
);
1517 case X (OP_POSTINC
, SL
): /* Register indirect w/post-incr, long. */
1519 SET_L_REG (rn
, t
+ 4);
1520 t
&= h8_get_mask (cpu
);
1521 SET_MEMORY_L (t
, n
);
1524 case X (OP_DISP
, SB
): /* Register indirect w/displacement, byte. */
1525 t
= GET_L_REG (rn
) + abs
;
1526 t
&= h8_get_mask (cpu
);
1527 SET_MEMORY_B (t
, n
);
1530 case X (OP_DISP
, SW
): /* Register indirect w/displacement, word. */
1531 t
= GET_L_REG (rn
) + abs
;
1532 t
&= h8_get_mask (cpu
);
1533 SET_MEMORY_W (t
, n
);
1536 case X (OP_DISP
, SL
): /* Register indirect w/displacement, long. */
1537 t
= GET_L_REG (rn
) + abs
;
1538 t
&= h8_get_mask (cpu
);
1539 SET_MEMORY_L (t
, n
);
1543 case X (OP_MEM
, SB
): /* Why isn't this implemented? */
1544 case X (OP_MEM
, SW
): /* Why isn't this implemented? */
1545 case X (OP_MEM
, SL
): /* Why isn't this implemented? */
1547 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGSEGV
);
1556 store (SIM_DESC sd
, ea_type
*arg
, int n
)
1558 return store_1 (sd
, arg
, n
, 0);
1561 /* Store which follows a fetch from the same location.
1562 The difference being that we don't want to do a pre-increment
1563 or pre-decrement at this time: it was already done when we fetched. */
1566 store2 (SIM_DESC sd
, ea_type
*arg
, int n
)
1568 return store_1 (sd
, arg
, n
, 1);
1571 /* Callback for qsort. We sort first based on availablity
1572 (available instructions sort lower). When availability state
1573 is the same, then we use the first 4 bit nibble as a secondary
1576 We don't really care about 100% stability here, just that the
1577 available instructions come first and all instrutions with
1578 the same starting nibble are consecutive.
1580 We could do even better by recording frequency information into the
1581 main table and using that to sort within a nibble's group with the
1582 highest frequency instructions appearing first. */
1585 instruction_comparator (const void *p1_
, const void *p2_
)
1587 struct h8_opcode
*p1
= (struct h8_opcode
*)p1_
;
1588 struct h8_opcode
*p2
= (struct h8_opcode
*)p2_
;
1590 /* The 1st sort key is based on whether or not the
1591 instruction is even available. This reduces the
1592 number of entries we have to look at in the common
1594 bool p1_available
= !((p1
->available
== AV_H8SX
&& !h8300sxmode
)
1595 || (p1
->available
== AV_H8S
&& !h8300smode
)
1596 || (p1
->available
== AV_H8H
&& !h8300hmode
));
1598 bool p2_available
= !((p2
->available
== AV_H8SX
&& !h8300sxmode
)
1599 || (p2
->available
== AV_H8S
&& !h8300smode
)
1600 || (p2
->available
== AV_H8H
&& !h8300hmode
));
1602 /* Sort so that available instructions come before unavailable
1604 if (p1_available
!= p2_available
)
1605 return p2_available
- p1_available
;
1607 /* Secondarily sort based on the first opcode nibble. */
1608 return p1
->data
.nib
[0] - p2
->data
.nib
[0];
1612 /* OPS is the opcode array, which is initially sorted by mnenomic.
1614 Sort the array so that the instructions for the sub-architecture
1615 are at the start and unavailable instructions are at the end.
1617 Within the set of available instructions, further sort them based
1618 on the first 4 bit nibble.
1620 Then find the first index into OPS for each of the 16 possible
1621 nibbles and record that into NIB_INDICES to speed up decoding. */
1624 sort_opcodes_and_setup_nibble_indices (struct h8_opcode
*ops
)
1626 const struct h8_opcode
*q
;
1629 /* First sort the OPS array. */
1630 for (i
= 0, q
= ops
; q
->name
; q
++, i
++)
1632 qsort (ops
, i
, sizeof (struct h8_opcode
), instruction_comparator
);
1634 /* Now walk the array caching the index of the first
1635 occurrence of each 4 bit nibble. */
1636 memset (nib_indices
, -1, sizeof (nib_indices
));
1637 for (i
= 0, q
= ops
; q
->name
; q
++, i
++)
1639 int nib
= q
->data
.nib
[0];
1641 /* Record the location of the first entry with the right
1643 if (nib_indices
[nib
] == -1)
1644 nib_indices
[nib
] = i
;
1649 /* Flag to be set whenever a new SIM_DESC object is created. */
1650 static int init_pointers_needed
= 1;
1653 init_pointers (SIM_DESC sd
)
1655 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
1656 struct h8300_sim_state
*state
= H8300_SIM_STATE (sd
);
1658 if (init_pointers_needed
)
1662 if (h8300smode
&& !h8300_normal_mode
)
1663 memory_size
= H8300S_MSIZE
;
1664 else if (h8300hmode
&& !h8300_normal_mode
)
1665 memory_size
= H8300H_MSIZE
;
1667 memory_size
= H8300_MSIZE
;
1668 /* `msize' must be a power of two. */
1669 if ((memory_size
& (memory_size
- 1)) != 0)
1673 "init_pointers: bad memory size %d, defaulting to %d.\n",
1674 memory_size
, H8300S_MSIZE
);
1675 memory_size
= H8300S_MSIZE
;
1678 if (h8_get_memory_buf (cpu
))
1679 free (h8_get_memory_buf (cpu
));
1681 h8_set_memory_buf (cpu
, (unsigned char *)
1682 calloc (sizeof (char), memory_size
));
1683 state
->memory_size
= memory_size
;
1685 h8_set_mask (cpu
, memory_size
- 1);
1687 memset (h8_get_reg_buf (cpu
), 0, sizeof (H8300_SIM_CPU (cpu
)->regs
));
1689 for (i
= 0; i
< 8; i
++)
1691 /* FIXME: rewrite using local buffer. */
1692 unsigned char *p
= (unsigned char *) (h8_get_reg_buf (cpu
) + i
);
1693 unsigned char *e
= (unsigned char *) (h8_get_reg_buf (cpu
) + i
+ 1);
1694 unsigned short *q
= (unsigned short *) (h8_get_reg_buf (cpu
) + i
);
1695 unsigned short *u
= (unsigned short *) (h8_get_reg_buf (cpu
) + i
+ 1);
1696 h8_set_reg (cpu
, i
, 0x00112233);
1711 wreg
[i
] = wreg
[i
+ 8] = 0;
1725 if (wreg
[i
] == 0 || wreg
[i
+ 8] == 0)
1726 sim_io_printf (sd
, "init_pointers: internal error.\n");
1728 h8_set_reg (cpu
, i
, 0);
1731 /* Sort the opcode table and create indices to speed up decode. */
1732 sort_opcodes_and_setup_nibble_indices (ops
);
1734 init_pointers_needed
= 0;
1738 #define OBITOP(name, f, s, op) \
1739 case O (name, SB): \
1744 if (fetch (sd, &code->dst, &ea)) \
1746 if (fetch (sd, &code->src, &tmp)) \
1748 m = 1 << (tmp & 7); \
1751 if (store (sd, &code->dst,ea)) \
1757 step_once (SIM_DESC sd
, SIM_CPU
*cpu
)
1761 int tick_start
= get_now ();
1768 int c
, nz
, v
, n
, u
, h
, ui
, intMaskBit
;
1772 host_callback
*sim_callback
= STATE_CALLBACK (sd
);
1776 pc
= cpu_get_pc (cpu
);
1778 /* The PC should never be odd. */
1781 sim_engine_halt (sd
, cpu
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGBUS
);
1785 /* Get Status Register (flags). */
1788 if (h8300smode
) /* Get exr. */
1790 trace
= (h8_get_exr (cpu
) >> 7) & 1;
1791 intMask
= h8_get_exr (cpu
) & 7;
1794 oldmask
= h8_get_mask (cpu
);
1795 if (!h8300hmode
|| h8300_normal_mode
)
1796 h8_set_mask (cpu
, 0xffff);
1799 decoded_inst _code
, *code
= &_code
;
1800 memset (code
, 0, sizeof (*code
));
1801 decode (sd
, cpu
, pc
, h8_get_memory_buf (cpu
) + pc
, code
);
1807 printf ("%x %d %s\n", pc
, code
->opcode
,
1808 code
->op
? code
->op
->name
: "**");
1810 h8_increment_stats (sd
, code
->opcode
);
1815 cycles
+= code
->cycles
;
1819 switch (code
->opcode
)
1821 case O (O_MOVAB
, SL
):
1822 case O (O_MOVAW
, SL
):
1823 case O (O_MOVAL
, SL
):
1824 /* 1) Evaluate 2nd argument (dst).
1825 2) Mask / zero extend according to whether 1st argument (src)
1826 is INDEXB, INDEXW, or INDEXL.
1827 3) Left-shift the result by 0, 1 or 2, according to size of mova
1828 (mova/b, mova/w, mova/l).
1829 4) Add literal value of 1st argument (src).
1830 5) Store result in 3rd argument (op3).
1833 /* Alas, since this is the only instruction with 3 arguments,
1834 decode doesn't handle them very well. Some fix-up is required.
1836 a) The size of dst is determined by whether src is
1837 INDEXB or INDEXW. */
1839 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
1840 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SB
);
1841 else if (OP_KIND (code
->src
.type
) == OP_INDEXW
)
1842 code
->dst
.type
= X (OP_KIND (code
->dst
.type
), SW
);
1844 /* b) If op3 == null, then this is the short form of the insn.
1845 Dst is the dispreg of src, and op3 is the 32-bit form
1846 of the same register.
1849 if (code
->op3
.type
== -1)
1851 /* Short form: src == INDEXB/INDEXW, dst == op3 == 0.
1852 We get to compose dst and op3 as follows:
1854 op3 is a 32-bit register, ID == src.reg.
1855 dst is the same register, but 8 or 16 bits
1856 depending on whether src is INDEXB or INDEXW.
1859 code
->op3
.type
= X (OP_REG
, SL
);
1860 code
->op3
.reg
= code
->src
.reg
;
1861 code
->op3
.literal
= 0;
1863 if (OP_KIND (code
->src
.type
) == OP_INDEXB
)
1865 code
->dst
.type
= X (OP_REG
, SB
);
1866 code
->dst
.reg
= code
->op3
.reg
+ 8;
1869 code
->dst
.type
= X (OP_REG
, SW
);
1872 if (fetch (sd
, &code
->dst
, &ea
))
1875 switch (OP_KIND (code
->src
.type
)) {
1876 case OP_INDEXB
: ea
= ea
& 0xff; break;
1877 case OP_INDEXW
: ea
= ea
& 0xffff; break;
1878 case OP_INDEXL
: break;
1879 default: goto illegal
;
1882 switch (code
->opcode
) {
1883 case O (O_MOVAB
, SL
): break;
1884 case O (O_MOVAW
, SL
): ea
= ea
<< 1; break;
1885 case O (O_MOVAL
, SL
): ea
= ea
<< 2; break;
1886 default: goto illegal
;
1889 ea
= ea
+ code
->src
.literal
;
1891 if (store (sd
, &code
->op3
, ea
))
1896 case O (O_SUBX
, SB
): /* subx, extended sub */
1897 if (fetch2 (sd
, &code
->dst
, &rd
))
1899 if (fetch (sd
, &code
->src
, &ea
))
1905 case O (O_SUBX
, SW
): /* subx, extended sub */
1906 if (fetch2 (sd
, &code
->dst
, &rd
))
1908 if (fetch (sd
, &code
->src
, &ea
))
1914 case O (O_SUBX
, SL
): /* subx, extended sub */
1915 if (fetch2 (sd
, &code
->dst
, &rd
))
1917 if (fetch (sd
, &code
->src
, &ea
))
1923 case O (O_ADDX
, SB
): /* addx, extended add */
1924 if (fetch2 (sd
, &code
->dst
, &rd
))
1926 if (fetch (sd
, &code
->src
, &ea
))
1932 case O (O_ADDX
, SW
): /* addx, extended add */
1933 if (fetch2 (sd
, &code
->dst
, &rd
))
1935 if (fetch (sd
, &code
->src
, &ea
))
1941 case O (O_ADDX
, SL
): /* addx, extended add */
1942 if (fetch2 (sd
, &code
->dst
, &rd
))
1944 if (fetch (sd
, &code
->src
, &ea
))
1950 case O (O_SUB
, SB
): /* sub.b */
1951 /* Fetch rd and ea. */
1952 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
1958 case O (O_SUB
, SW
): /* sub.w */
1959 /* Fetch rd and ea. */
1960 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
1966 case O (O_SUB
, SL
): /* sub.l */
1967 /* Fetch rd and ea. */
1968 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
1974 case O (O_NEG
, SB
): /* neg.b */
1976 if (fetch2 (sd
, &code
->src
, &ea
))
1983 case O (O_NEG
, SW
): /* neg.w */
1985 if (fetch2 (sd
, &code
->src
, &ea
))
1992 case O (O_NEG
, SL
): /* neg.l */
1994 if (fetch2 (sd
, &code
->src
, &ea
))
2001 case O (O_ADD
, SB
): /* add.b */
2002 if (fetch2 (sd
, &code
->dst
, &rd
))
2004 if (fetch (sd
, &code
->src
, &ea
))
2009 case O (O_ADD
, SW
): /* add.w */
2010 if (fetch2 (sd
, &code
->dst
, &rd
))
2012 if (fetch (sd
, &code
->src
, &ea
))
2017 case O (O_ADD
, SL
): /* add.l */
2018 if (fetch2 (sd
, &code
->dst
, &rd
))
2020 if (fetch (sd
, &code
->src
, &ea
))
2025 case O (O_AND
, SB
): /* and.b */
2026 /* Fetch rd and ea. */
2027 if (fetch2 (sd
, &code
->dst
, &rd
) || fetch (sd
, &code
->src
, &ea
))
2032 case O (O_AND
, SW
): /* and.w */
2033 /* Fetch rd and ea. */
2034 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2039 case O (O_AND
, SL
): /* and.l */
2040 /* Fetch rd and ea. */
2041 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2046 case O (O_OR
, SB
): /* or.b */
2047 /* Fetch rd and ea. */
2048 if (fetch2 (sd
, &code
->dst
, &rd
) || fetch (sd
, &code
->src
, &ea
))
2053 case O (O_OR
, SW
): /* or.w */
2054 /* Fetch rd and ea. */
2055 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2060 case O (O_OR
, SL
): /* or.l */
2061 /* Fetch rd and ea. */
2062 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2067 case O (O_XOR
, SB
): /* xor.b */
2068 /* Fetch rd and ea. */
2069 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2074 case O (O_XOR
, SW
): /* xor.w */
2075 /* Fetch rd and ea. */
2076 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2081 case O (O_XOR
, SL
): /* xor.l */
2082 /* Fetch rd and ea. */
2083 if (fetch (sd
, &code
->src
, &ea
) || fetch2 (sd
, &code
->dst
, &rd
))
2089 if (fetch (sd
, &code
->src
, &res
))
2091 if (store (sd
, &code
->dst
, res
))
2093 goto just_flags_log8
;
2095 if (fetch (sd
, &code
->src
, &res
))
2097 if (store (sd
, &code
->dst
, res
))
2099 goto just_flags_log16
;
2101 if (fetch (sd
, &code
->src
, &res
))
2103 if (store (sd
, &code
->dst
, res
))
2105 goto just_flags_log32
;
2107 case O (O_MOVMD
, SB
): /* movmd.b */
2114 rd
= GET_MEMORY_B (GET_L_REG (5));
2115 SET_MEMORY_B (GET_L_REG (6), rd
);
2116 SET_L_REG (5, GET_L_REG (5) + 1);
2117 SET_L_REG (6, GET_L_REG (6) + 1);
2122 case O (O_MOVMD
, SW
): /* movmd.w */
2129 rd
= GET_MEMORY_W (GET_L_REG (5));
2130 SET_MEMORY_W (GET_L_REG (6), rd
);
2131 SET_L_REG (5, GET_L_REG (5) + 2);
2132 SET_L_REG (6, GET_L_REG (6) + 2);
2137 case O (O_MOVMD
, SL
): /* movmd.l */
2144 rd
= GET_MEMORY_L (GET_L_REG (5));
2145 SET_MEMORY_L (GET_L_REG (6), rd
);
2146 SET_L_REG (5, GET_L_REG (5) + 4);
2147 SET_L_REG (6, GET_L_REG (6) + 4);
2152 case O (O_MOVSD
, SB
): /* movsd.b */
2153 /* This instruction implements strncpy, with a conditional branch.
2154 r4 contains n, r5 contains src, and r6 contains dst.
2155 The 16-bit displacement operand is added to the pc
2156 if and only if the end of string is reached before
2157 n bytes are transferred. */
2159 ea
= GET_L_REG (4) & 0xffff;
2165 rd
= GET_MEMORY_B (GET_L_REG (5));
2166 SET_MEMORY_B (GET_L_REG (6), rd
);
2167 SET_L_REG (5, GET_L_REG (5) + 1);
2168 SET_L_REG (6, GET_L_REG (6) + 1);
2175 case O (O_EEPMOV
, SB
): /* eepmov.b */
2176 case O (O_EEPMOV
, SW
): /* eepmov.w */
2177 if (h8300hmode
|| h8300smode
)
2179 register unsigned char *_src
, *_dst
;
2180 unsigned int count
= ((code
->opcode
== O (O_EEPMOV
, SW
))
2181 ? h8_get_reg (cpu
, R4_REGNUM
) & 0xffff
2182 : h8_get_reg (cpu
, R4_REGNUM
) & 0xff);
2184 _src
= h8_get_memory_buf (cpu
) + h8_get_reg (cpu
, R5_REGNUM
);
2185 if ((_src
+ count
) >= (h8_get_memory_buf (cpu
) + memory_size
))
2187 _dst
= h8_get_memory_buf (cpu
) + h8_get_reg (cpu
, R6_REGNUM
);
2188 if ((_dst
+ count
) >= (h8_get_memory_buf (cpu
) + memory_size
))
2190 memcpy (_dst
, _src
, count
);
2192 h8_set_reg (cpu
, R5_REGNUM
, h8_get_reg (cpu
, R5_REGNUM
) + count
);
2193 h8_set_reg (cpu
, R6_REGNUM
, h8_get_reg (cpu
, R6_REGNUM
) + count
);
2194 h8_set_reg (cpu
, R4_REGNUM
, h8_get_reg (cpu
, R4_REGNUM
) &
2195 ((code
->opcode
== O (O_EEPMOV
, SW
))
2196 ? (~0xffff) : (~0xff)));
2197 cycles
+= 2 * count
;
2202 case O (O_ADDS
, SL
): /* adds (.l) */
2204 * This insn only uses register operands, but still
2205 * it would be cleaner to use fetch and store... */
2206 SET_L_REG (code
->dst
.reg
,
2207 GET_L_REG (code
->dst
.reg
)
2208 + code
->src
.literal
);
2212 case O (O_SUBS
, SL
): /* subs (.l) */
2214 * This insn only uses register operands, but still
2215 * it would be cleaner to use fetch and store... */
2216 SET_L_REG (code
->dst
.reg
,
2217 GET_L_REG (code
->dst
.reg
)
2218 - code
->src
.literal
);
2221 case O (O_CMP
, SB
): /* cmp.b */
2222 if (fetch (sd
, &code
->dst
, &rd
))
2224 if (fetch (sd
, &code
->src
, &ea
))
2228 goto just_flags_alu8
;
2230 case O (O_CMP
, SW
): /* cmp.w */
2231 if (fetch (sd
, &code
->dst
, &rd
))
2233 if (fetch (sd
, &code
->src
, &ea
))
2237 goto just_flags_alu16
;
2239 case O (O_CMP
, SL
): /* cmp.l */
2240 if (fetch (sd
, &code
->dst
, &rd
))
2242 if (fetch (sd
, &code
->src
, &ea
))
2246 goto just_flags_alu32
;
2248 case O (O_DEC
, SB
): /* dec.b */
2250 * This insn only uses register operands, but still
2251 * it would be cleaner to use fetch and store... */
2252 rd
= GET_B_REG (code
->src
.reg
);
2255 SET_B_REG (code
->src
.reg
, res
);
2256 goto just_flags_inc8
;
2258 case O (O_DEC
, SW
): /* dec.w */
2260 * This insn only uses register operands, but still
2261 * it would be cleaner to use fetch and store... */
2262 rd
= GET_W_REG (code
->dst
.reg
);
2263 ea
= -code
->src
.literal
;
2265 SET_W_REG (code
->dst
.reg
, res
);
2266 goto just_flags_inc16
;
2268 case O (O_DEC
, SL
): /* dec.l */
2270 * This insn only uses register operands, but still
2271 * it would be cleaner to use fetch and store... */
2272 rd
= GET_L_REG (code
->dst
.reg
);
2273 ea
= -code
->src
.literal
;
2275 SET_L_REG (code
->dst
.reg
, res
);
2276 goto just_flags_inc32
;
2278 case O (O_INC
, SB
): /* inc.b */
2280 * This insn only uses register operands, but still
2281 * it would be cleaner to use fetch and store... */
2282 rd
= GET_B_REG (code
->src
.reg
);
2285 SET_B_REG (code
->src
.reg
, res
);
2286 goto just_flags_inc8
;
2288 case O (O_INC
, SW
): /* inc.w */
2290 * This insn only uses register operands, but still
2291 * it would be cleaner to use fetch and store... */
2292 rd
= GET_W_REG (code
->dst
.reg
);
2293 ea
= code
->src
.literal
;
2295 SET_W_REG (code
->dst
.reg
, res
);
2296 goto just_flags_inc16
;
2298 case O (O_INC
, SL
): /* inc.l */
2300 * This insn only uses register operands, but still
2301 * it would be cleaner to use fetch and store... */
2302 rd
= GET_L_REG (code
->dst
.reg
);
2303 ea
= code
->src
.literal
;
2305 SET_L_REG (code
->dst
.reg
, res
);
2306 goto just_flags_inc32
;
2308 case O (O_LDC
, SB
): /* ldc.b */
2309 if (fetch (sd
, &code
->src
, &res
))
2313 case O (O_LDC
, SW
): /* ldc.w */
2314 if (fetch (sd
, &code
->src
, &res
))
2317 /* Word operand, value from MSB, must be shifted. */
2321 case O (O_LDC
, SL
): /* ldc.l */
2322 if (fetch (sd
, &code
->src
, &res
))
2324 switch (code
->dst
.type
) {
2325 case X (OP_SBR
, SL
):
2326 h8_set_sbr (cpu
, res
);
2328 case X (OP_VBR
, SL
):
2329 h8_set_vbr (cpu
, res
);
2336 case O (O_STC
, SW
): /* stc.w */
2337 case O (O_STC
, SB
): /* stc.b */
2338 if (code
->src
.type
== X (OP_CCR
, SB
))
2341 res
= h8_get_ccr (cpu
);
2343 else if (code
->src
.type
== X (OP_EXR
, SB
) && h8300smode
)
2346 h8_set_exr (cpu
, (trace
<< 7) | intMask
);
2347 res
= h8_get_exr (cpu
);
2352 /* Word operand, value to MSB, must be shifted. */
2353 if (code
->opcode
== X (O_STC
, SW
))
2355 if (store (sd
, &code
->dst
, res
))
2358 case O (O_STC
, SL
): /* stc.l */
2359 switch (code
->src
.type
) {
2360 case X (OP_SBR
, SL
):
2361 res
= h8_get_sbr (cpu
);
2363 case X (OP_VBR
, SL
):
2364 res
= h8_get_vbr (cpu
);
2369 if (store (sd
, &code
->dst
, res
))
2373 case O (O_ANDC
, SB
): /* andc.b */
2374 if (code
->dst
.type
== X (OP_CCR
, SB
))
2377 rd
= h8_get_ccr (cpu
);
2379 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2382 h8_set_exr (cpu
, (trace
<< 7) | intMask
);
2383 rd
= h8_get_exr (cpu
);
2387 ea
= code
->src
.literal
;
2391 case O (O_ORC
, SB
): /* orc.b */
2392 if (code
->dst
.type
== X (OP_CCR
, SB
))
2395 rd
= h8_get_ccr (cpu
);
2397 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2400 h8_set_exr (cpu
, (trace
<< 7) | intMask
);
2401 rd
= h8_get_exr (cpu
);
2405 ea
= code
->src
.literal
;
2409 case O (O_XORC
, SB
): /* xorc.b */
2410 if (code
->dst
.type
== X (OP_CCR
, SB
))
2413 rd
= h8_get_ccr (cpu
);
2415 else if (code
->dst
.type
== X (OP_EXR
, SB
) && h8300smode
)
2418 h8_set_exr (cpu
, (trace
<< 7) | intMask
);
2419 rd
= h8_get_exr (cpu
);
2423 ea
= code
->src
.literal
;
2427 case O (O_BRAS
, SB
): /* bra/s */
2428 /* This is basically an ordinary branch, with a delay slot. */
2429 if (fetch (sd
, &code
->src
, &res
))
2437 /* Execution continues at next instruction, but
2438 delayed_branch is set up for next cycle. */
2439 h8_set_delayed_branch (cpu
, code
->next_pc
+ res
);
2443 case O (O_BRAB
, SB
): /* bra rd.b */
2444 case O (O_BRAW
, SW
): /* bra rd.w */
2445 case O (O_BRAL
, SL
): /* bra erd.l */
2446 if (fetch (sd
, &code
->src
, &rd
))
2448 switch (OP_SIZE (code
->opcode
)) {
2449 case SB
: rd
&= 0xff; break;
2450 case SW
: rd
&= 0xffff; break;
2451 case SL
: rd
&= 0xffffffff; break;
2453 pc
= code
->next_pc
+ rd
;
2456 case O (O_BRABC
, SB
): /* bra/bc, branch if bit clear */
2457 case O (O_BRABS
, SB
): /* bra/bs, branch if bit set */
2458 case O (O_BSRBC
, SB
): /* bsr/bc, call if bit clear */
2459 case O (O_BSRBS
, SB
): /* bsr/bs, call if bit set */
2460 if (fetch (sd
, &code
->dst
, &rd
) ||
2461 fetch (sd
, &code
->src
, &bit
))
2464 if (code
->opcode
== O (O_BRABC
, SB
) || /* branch if clear */
2465 code
->opcode
== O (O_BSRBC
, SB
)) /* call if clear */
2467 if ((rd
& (1 << bit
))) /* no branch */
2470 else /* branch/call if set */
2472 if (!(rd
& (1 << bit
))) /* no branch */
2476 if (fetch (sd
, &code
->op3
, &res
)) /* branch */
2478 pc
= code
->next_pc
+ res
;
2480 if (code
->opcode
== O (O_BRABC
, SB
) ||
2481 code
->opcode
== O (O_BRABS
, SB
)) /* branch */
2489 case O (O_BRA
, SB
): /* bra, branch always */
2494 case O (O_BRN
, SB
): /* brn, ;-/ branch never? */
2499 case O (O_BHI
, SB
): /* bhi */
2505 case O (O_BLS
, SB
): /* bls */
2510 case O (O_BCS
, SB
): /* bcs, branch if carry set */
2515 case O (O_BCC
, SB
): /* bcc, branch if carry clear */
2520 case O (O_BEQ
, SB
): /* beq, branch if zero set */
2524 case O (O_BGT
, SB
): /* bgt */
2525 if (((Z
|| (N
^ V
)) == 0))
2529 case O (O_BLE
, SB
): /* ble */
2530 if (((Z
|| (N
^ V
)) == 1))
2534 case O (O_BGE
, SB
): /* bge */
2538 case O (O_BLT
, SB
): /* blt */
2542 case O (O_BMI
, SB
): /* bmi */
2546 case O (O_BNE
, SB
): /* bne, branch if zero clear */
2551 case O (O_BPL
, SB
): /* bpl */
2555 case O (O_BVC
, SB
): /* bvc */
2559 case O (O_BVS
, SB
): /* bvs */
2564 /* Trap for Command Line setup. */
2565 case O (O_SYS_CMDLINE
, SB
):
2567 int i
= 0; /* Loop counter. */
2568 int j
= 0; /* Loop counter. */
2569 int ind_arg_len
= 0; /* Length of each argument. */
2570 int no_of_args
= 0; /* The no. or cmdline args. */
2571 int current_location
= 0; /* Location of string. */
2572 int old_sp
= 0; /* The Initial Stack Pointer. */
2573 int sp_move
= 0; /* No. of locations by which the stack needs
2575 int new_sp
= 0; /* The final stack pointer location passed
2577 int *argv_ptrs
; /* Pointers of argv strings to be stored. */
2578 int argv_ptrs_location
= 0; /* Location of pointers to cmdline
2579 args on the stack. */
2580 int char_ptr_size
= 0; /* Size of a character pointer on
2582 int addr_cmdline
= 0; /* Memory location where cmdline has
2584 int size_cmdline
= 0; /* Size of cmdline. */
2586 /* Set the address of 256 free locations where command line is
2588 addr_cmdline
= cmdline_location();
2589 h8_set_reg (cpu
, 0, addr_cmdline
);
2591 /* Counting the no. of commandline arguments. */
2592 for (i
= 0; h8_get_cmdline_arg (cpu
, i
) != NULL
; i
++)
2595 /* No. of arguments in the command line. */
2598 /* Current location is just a temporary variable,which we are
2599 setting to the point to the start of our commandline string. */
2600 current_location
= addr_cmdline
;
2602 /* Allocating space for storing pointers of the command line
2604 argv_ptrs
= (int *) malloc (sizeof (int) * no_of_args
);
2606 /* Setting char_ptr_size to the sizeof (char *) on the different
2608 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2617 for (i
= 0; i
< no_of_args
; i
++)
2621 /* The size of the commandline argument. */
2622 ind_arg_len
= strlen (h8_get_cmdline_arg (cpu
, i
)) + 1;
2624 /* The total size of the command line string. */
2625 size_cmdline
+= ind_arg_len
;
2627 /* As we have only 256 bytes, we need to provide a graceful
2628 exit. Anyways, a program using command line arguments
2629 where we cannot store all the command line arguments
2630 given may behave unpredictably. */
2631 if (size_cmdline
>= 256)
2633 h8_set_reg (cpu
, 0, 0);
2638 /* current_location points to the memory where the next
2639 commandline argument is stored. */
2640 argv_ptrs
[i
] = current_location
;
2641 for (j
= 0; j
< ind_arg_len
; j
++)
2643 SET_MEMORY_B ((current_location
+
2644 (sizeof (char) * j
)),
2645 *(h8_get_cmdline_arg (cpu
, i
) +
2646 sizeof (char) * j
));
2649 /* Setting current_location to the starting of next
2651 current_location
+= ind_arg_len
;
2655 /* This is the original position of the stack pointer. */
2656 old_sp
= h8_get_reg (cpu
, SP_REGNUM
);
2658 /* We need space from the stack to store the pointers to argvs. */
2659 /* As we will infringe on the stack, we need to shift the stack
2660 pointer so that the data is not overwritten. We calculate how
2661 much space is required. */
2662 sp_move
= (no_of_args
) * (char_ptr_size
);
2664 /* The final position of stack pointer, we have thus taken some
2665 space from the stack. */
2666 new_sp
= old_sp
- sp_move
;
2668 /* Temporary variable holding value where the argv pointers need
2670 argv_ptrs_location
= new_sp
;
2672 /* The argv pointers are stored at sequential locations. As per
2674 for (i
= 0; i
< no_of_args
; i
++)
2676 /* Saving the argv pointer. */
2677 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2679 SET_MEMORY_L (argv_ptrs_location
, argv_ptrs
[i
]);
2683 SET_MEMORY_W (argv_ptrs_location
, argv_ptrs
[i
]);
2686 /* The next location where the pointer to the next argv
2687 string has to be stored. */
2688 argv_ptrs_location
+= char_ptr_size
;
2691 /* Required by POSIX, Setting 0x0 at the end of the list of argv
2693 if ((h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2695 SET_MEMORY_L (old_sp
, 0x0);
2699 SET_MEMORY_W (old_sp
, 0x0);
2702 /* Freeing allocated memory. */
2704 for (i
= 0; i
<= no_of_args
; i
++)
2706 free (h8_get_cmdline_arg (cpu
, i
));
2708 free (h8_get_command_line (cpu
));
2710 /* The no. of argv arguments are returned in Reg 0. */
2711 h8_set_reg (cpu
, 0, no_of_args
);
2712 /* The Pointer to argv in Register 1. */
2713 h8_set_reg (cpu
, 1, new_sp
);
2714 /* Setting the stack pointer to the new value. */
2715 h8_set_reg (cpu
, SP_REGNUM
, new_sp
);
2719 /* System call processing starts. */
2720 case O (O_SYS_OPEN
, SB
):
2722 int len
= 0; /* Length of filename. */
2723 char *filename
; /* Filename would go here. */
2724 char temp_char
; /* Temporary character */
2725 int mode
= 0; /* Mode bits for the file. */
2726 int open_return
; /* Return value of open, file descriptor. */
2727 int i
; /* Loop counter */
2728 int filename_ptr
; /* Pointer to filename in cpu memory. */
2730 /* Setting filename_ptr to first argument of open, */
2731 /* and trying to get mode. */
2732 if ((h8300sxmode
|| h8300hmode
|| h8300smode
) && !h8300_normal_mode
)
2734 filename_ptr
= GET_L_REG (0);
2735 mode
= GET_MEMORY_L (h8_get_reg (cpu
, SP_REGNUM
) + 4);
2739 filename_ptr
= GET_W_REG (0);
2740 mode
= GET_MEMORY_W (h8_get_reg (cpu
, SP_REGNUM
) + 2);
2743 /* Trying to find the length of the filename. */
2744 temp_char
= GET_MEMORY_B (h8_get_reg (cpu
, 0));
2747 while (temp_char
!= '\0')
2749 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
2753 /* Allocating space for the filename. */
2754 filename
= (char *) malloc (sizeof (char) * len
);
2756 /* String copying the filename from memory. */
2757 for (i
= 0; i
< len
; i
++)
2759 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
2760 filename
[i
] = temp_char
;
2763 /* Callback to open and return the file descriptor. */
2764 open_return
= sim_callback
->open (sim_callback
, filename
, mode
);
2766 /* Return value in register 0. */
2767 h8_set_reg (cpu
, 0, open_return
);
2769 /* Freeing memory used for filename. */
2774 case O (O_SYS_READ
, SB
):
2776 char *char_ptr
; /* Where characters read would be stored. */
2777 int fd
; /* File descriptor */
2778 int buf_size
; /* BUF_SIZE parameter in read. */
2779 int i
= 0; /* Temporary Loop counter */
2780 int read_return
= 0; /* Return value from callback to
2783 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2784 buf_size
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2786 char_ptr
= (char *) malloc (sizeof (char) * buf_size
);
2788 /* Callback to read and return the no. of characters read. */
2790 sim_callback
->read (sim_callback
, fd
, char_ptr
, buf_size
);
2792 /* The characters read are stored in cpu memory. */
2793 for (i
= 0; i
< buf_size
; i
++)
2795 SET_MEMORY_B ((h8_get_reg (cpu
, 1) + (sizeof (char) * i
)),
2796 *(char_ptr
+ (sizeof (char) * i
)));
2799 /* Return value in Register 0. */
2800 h8_set_reg (cpu
, 0, read_return
);
2802 /* Freeing memory used as buffer. */
2807 case O (O_SYS_WRITE
, SB
):
2809 int fd
; /* File descriptor */
2810 char temp_char
; /* Temporary character */
2811 int len
; /* Length of write, Parameter II to write. */
2812 int char_ptr
; /* Character Pointer, Parameter I of write. */
2813 char *ptr
; /* Where characters to be written are stored.
2815 int write_return
; /* Return value from callback to write. */
2816 int i
= 0; /* Loop counter */
2818 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2819 char_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2820 len
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2822 /* Allocating space for the characters to be written. */
2823 ptr
= (char *) malloc (sizeof (char) * len
);
2825 /* Fetching the characters from cpu memory. */
2826 for (i
= 0; i
< len
; i
++)
2828 temp_char
= GET_MEMORY_B (char_ptr
+ i
);
2832 /* Callback write and return the no. of characters written. */
2833 write_return
= sim_callback
->write (sim_callback
, fd
, ptr
, len
);
2835 /* Return value in Register 0. */
2836 h8_set_reg (cpu
, 0, write_return
);
2838 /* Freeing memory used as buffer. */
2843 case O (O_SYS_LSEEK
, SB
):
2845 int fd
; /* File descriptor */
2846 int offset
; /* Offset */
2847 int origin
; /* Origin */
2848 int lseek_return
; /* Return value from callback to lseek. */
2850 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2851 offset
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2852 origin
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (2) : GET_W_REG (2);
2854 /* Callback lseek and return offset. */
2856 sim_callback
->lseek (sim_callback
, fd
, offset
, origin
);
2858 /* Return value in register 0. */
2859 h8_set_reg (cpu
, 0, lseek_return
);
2863 case O (O_SYS_CLOSE
, SB
):
2865 int fd
; /* File descriptor */
2866 int close_return
; /* Return value from callback to close. */
2868 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2870 /* Callback close and return. */
2871 close_return
= sim_callback
->close (sim_callback
, fd
);
2873 /* Return value in register 0. */
2874 h8_set_reg (cpu
, 0, close_return
);
2878 case O (O_SYS_FSTAT
, SB
):
2880 int fd
; /* File descriptor */
2881 struct stat stat_rec
; /* Stat record */
2882 int fstat_return
; /* Return value from callback to stat. */
2883 int stat_ptr
; /* Pointer to stat record. */
2885 fd
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2887 /* Setting stat_ptr to second argument of stat. */
2888 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2890 /* Callback stat and return. */
2891 fstat_return
= sim_callback
->to_fstat (sim_callback
, fd
,
2894 /* Setting up the stat structure returned. */
2895 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
2897 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
2899 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
2901 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
2903 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
2905 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
2907 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
2909 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
2911 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
2913 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
2915 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
2917 /* Return value in register 0. */
2918 h8_set_reg (cpu
, 0, fstat_return
);
2922 case O (O_SYS_STAT
, SB
):
2924 int len
= 0; /* Length of filename. */
2925 char *filename
; /* Filename would go here. */
2926 char temp_char
; /* Temporary character */
2927 int filename_ptr
; /* Pointer to filename in cpu memory. */
2928 struct stat stat_rec
; /* Stat record */
2929 int stat_return
; /* Return value from callback to stat */
2930 int stat_ptr
; /* Pointer to stat record. */
2931 int i
= 0; /* Loop Counter */
2933 /* Setting filename_ptr to first argument of open. */
2934 filename_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (0) : GET_W_REG (0);
2936 /* Trying to find the length of the filename. */
2937 temp_char
= GET_MEMORY_B (h8_get_reg (cpu
, 0));
2940 while (temp_char
!= '\0')
2942 temp_char
= GET_MEMORY_B (filename_ptr
+ len
);
2946 /* Allocating space for the filename. */
2947 filename
= (char *) malloc (sizeof (char) * len
);
2949 /* String copying the filename from memory. */
2950 for (i
= 0; i
< len
; i
++)
2952 temp_char
= GET_MEMORY_B (filename_ptr
+ i
);
2953 filename
[i
] = temp_char
;
2956 /* Setting stat_ptr to second argument of stat. */
2957 /* stat_ptr = h8_get_reg (cpu, 1); */
2958 stat_ptr
= (h8300hmode
&& !h8300_normal_mode
) ? GET_L_REG (1) : GET_W_REG (1);
2960 /* Callback stat and return. */
2962 sim_callback
->to_stat (sim_callback
, filename
, &stat_rec
);
2964 /* Freeing memory used for filename. */
2967 /* Setting up the stat structure returned. */
2968 SET_MEMORY_W (stat_ptr
, stat_rec
.st_dev
);
2970 SET_MEMORY_W (stat_ptr
, stat_rec
.st_ino
);
2972 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mode
);
2974 SET_MEMORY_W (stat_ptr
, stat_rec
.st_nlink
);
2976 SET_MEMORY_W (stat_ptr
, stat_rec
.st_uid
);
2978 SET_MEMORY_W (stat_ptr
, stat_rec
.st_gid
);
2980 SET_MEMORY_W (stat_ptr
, stat_rec
.st_rdev
);
2982 SET_MEMORY_L (stat_ptr
, stat_rec
.st_size
);
2984 SET_MEMORY_L (stat_ptr
, stat_rec
.st_atime
);
2986 SET_MEMORY_L (stat_ptr
, stat_rec
.st_mtime
);
2988 SET_MEMORY_L (stat_ptr
, stat_rec
.st_ctime
);
2990 /* Return value in register 0. */
2991 h8_set_reg (cpu
, 0, stat_return
);
2994 /* End of system call processing. */
2996 case O (O_NOT
, SB
): /* not.b */
2997 if (fetch2 (sd
, &code
->src
, &rd
))
3003 case O (O_NOT
, SW
): /* not.w */
3004 if (fetch2 (sd
, &code
->src
, &rd
))
3010 case O (O_NOT
, SL
): /* not.l */
3011 if (fetch2 (sd
, &code
->src
, &rd
))
3017 case O (O_SHLL
, SB
): /* shll.b */
3018 case O (O_SHLR
, SB
): /* shlr.b */
3019 if (fetch2 (sd
, &code
->dst
, &rd
))
3022 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3023 ea
= 1; /* unary op */
3024 else /* binary op */
3025 fetch (sd
, &code
->src
, &ea
);
3027 if (code
->opcode
== O (O_SHLL
, SB
))
3030 c
= rd
& (0x80 >> (ea
- 1));
3036 c
= rd
& (1 << (ea
- 1));
3037 rd
= (unsigned char) rd
>> ea
;
3041 case O (O_SHLL
, SW
): /* shll.w */
3042 case O (O_SHLR
, SW
): /* shlr.w */
3043 if (fetch2 (sd
, &code
->dst
, &rd
))
3046 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3047 ea
= 1; /* unary op */
3049 fetch (sd
, &code
->src
, &ea
);
3051 if (code
->opcode
== O (O_SHLL
, SW
))
3054 c
= rd
& (0x8000 >> (ea
- 1));
3060 c
= rd
& (1 << (ea
- 1));
3061 rd
= (unsigned short) rd
>> ea
;
3065 case O (O_SHLL
, SL
): /* shll.l */
3066 case O (O_SHLR
, SL
): /* shlr.l */
3067 if (fetch2 (sd
, &code
->dst
, &rd
))
3070 if (memcmp (&code
->src
, &code
->dst
, sizeof (code
->src
)) == 0)
3071 ea
= 1; /* unary op */
3073 fetch (sd
, &code
->src
, &ea
);
3075 if (code
->opcode
== O (O_SHLL
, SL
))
3078 c
= rd
& (0x80000000 >> (ea
- 1));
3084 c
= rd
& (1 << (ea
- 1));
3085 rd
= (unsigned int) rd
>> ea
;
3089 case O (O_SHAL
, SB
):
3090 case O (O_SHAR
, SB
):
3091 if (fetch2 (sd
, &code
->dst
, &rd
))
3094 if (code
->src
.type
== X (OP_IMM
, SB
))
3095 fetch (sd
, &code
->src
, &ea
);
3099 if (code
->opcode
== O (O_SHAL
, SB
))
3101 c
= rd
& (0x80 >> (ea
- 1));
3102 res
= rd
>> (7 - ea
);
3103 v
= ((res
& 1) && !(res
& 2))
3104 || (!(res
& 1) && (res
& 2));
3109 c
= rd
& (1 << (ea
- 1));
3111 rd
= ((signed char) rd
) >> ea
;
3115 case O (O_SHAL
, SW
):
3116 case O (O_SHAR
, SW
):
3117 if (fetch2 (sd
, &code
->dst
, &rd
))
3120 if (code
->src
.type
== X (OP_IMM
, SW
))
3121 fetch (sd
, &code
->src
, &ea
);
3125 if (code
->opcode
== O (O_SHAL
, SW
))
3127 c
= rd
& (0x8000 >> (ea
- 1));
3128 res
= rd
>> (15 - ea
);
3129 v
= ((res
& 1) && !(res
& 2))
3130 || (!(res
& 1) && (res
& 2));
3135 c
= rd
& (1 << (ea
- 1));
3137 rd
= ((signed short) rd
) >> ea
;
3141 case O (O_SHAL
, SL
):
3142 case O (O_SHAR
, SL
):
3143 if (fetch2 (sd
, &code
->dst
, &rd
))
3146 if (code
->src
.type
== X (OP_IMM
, SL
))
3147 fetch (sd
, &code
->src
, &ea
);
3151 if (code
->opcode
== O (O_SHAL
, SL
))
3153 c
= rd
& (0x80000000 >> (ea
- 1));
3154 res
= rd
>> (31 - ea
);
3155 v
= ((res
& 1) && !(res
& 2))
3156 || (!(res
& 1) && (res
& 2));
3161 c
= rd
& (1 << (ea
- 1));
3163 rd
= ((signed int) rd
) >> ea
;
3167 case O (O_ROTL
, SB
):
3168 case O (O_ROTR
, SB
):
3169 if (fetch2 (sd
, &code
->dst
, &rd
))
3172 if (code
->src
.type
== X (OP_IMM
, SB
))
3173 fetch (sd
, &code
->src
, &ea
);
3178 if (code
->opcode
== O (O_ROTL
, SB
))
3188 rd
= ((unsigned char) rd
) >> 1;
3196 case O (O_ROTL
, SW
):
3197 case O (O_ROTR
, SW
):
3198 if (fetch2 (sd
, &code
->dst
, &rd
))
3201 if (code
->src
.type
== X (OP_IMM
, SW
))
3202 fetch (sd
, &code
->src
, &ea
);
3207 if (code
->opcode
== O (O_ROTL
, SW
))
3217 rd
= ((unsigned short) rd
) >> 1;
3225 case O (O_ROTL
, SL
):
3226 case O (O_ROTR
, SL
):
3227 if (fetch2 (sd
, &code
->dst
, &rd
))
3230 if (code
->src
.type
== X (OP_IMM
, SL
))
3231 fetch (sd
, &code
->src
, &ea
);
3236 if (code
->opcode
== O (O_ROTL
, SL
))
3238 c
= rd
& 0x80000000;
3246 rd
= ((unsigned int) rd
) >> 1;
3254 case O (O_ROTXL
, SB
):
3255 case O (O_ROTXR
, SB
):
3256 if (fetch2 (sd
, &code
->dst
, &rd
))
3259 if (code
->src
.type
== X (OP_IMM
, SB
))
3260 fetch (sd
, &code
->src
, &ea
);
3265 if (code
->opcode
== O (O_ROTXL
, SB
))
3276 rd
= ((unsigned char) rd
) >> 1;
3285 case O (O_ROTXL
, SW
):
3286 case O (O_ROTXR
, SW
):
3287 if (fetch2 (sd
, &code
->dst
, &rd
))
3290 if (code
->src
.type
== X (OP_IMM
, SW
))
3291 fetch (sd
, &code
->src
, &ea
);
3296 if (code
->opcode
== O (O_ROTXL
, SW
))
3307 rd
= ((unsigned short) rd
) >> 1;
3316 case O (O_ROTXL
, SL
):
3317 case O (O_ROTXR
, SL
):
3318 if (fetch2 (sd
, &code
->dst
, &rd
))
3321 if (code
->src
.type
== X (OP_IMM
, SL
))
3322 fetch (sd
, &code
->src
, &ea
);
3327 if (code
->opcode
== O (O_ROTXL
, SL
))
3329 res
= rd
& 0x80000000;
3338 rd
= ((unsigned int) rd
) >> 1;
3349 case O (O_JMP
, SB
): /* jmp */
3351 fetch (sd
, &code
->src
, &pc
);
3356 case O (O_JSR
, SB
): /* jsr, jump to subroutine */
3358 if (fetch (sd
, &code
->src
, &pc
))
3361 tmp
= h8_get_reg (cpu
, SP_REGNUM
);
3363 if (h8300hmode
&& !h8300_normal_mode
)
3366 SET_MEMORY_L (tmp
, code
->next_pc
);
3371 SET_MEMORY_W (tmp
, code
->next_pc
);
3373 h8_set_reg (cpu
, SP_REGNUM
, tmp
);
3379 case O (O_BSR
, SB
): /* bsr, branch to subroutine */
3380 if (fetch (sd
, &code
->src
, &res
))
3382 pc
= code
->next_pc
+ res
;
3385 case O (O_RTE
, SN
): /* rte, return from exception */
3387 /* Pops exr and ccr before pc -- otherwise identical to rts. */
3388 tmp
= h8_get_reg (cpu
, SP_REGNUM
);
3390 if (h8300smode
) /* pop exr */
3392 h8_set_exr (cpu
, GET_MEMORY_L (tmp
));
3395 if (h8300hmode
&& !h8300_normal_mode
)
3397 h8_set_ccr (cpu
, GET_MEMORY_L (tmp
));
3399 pc
= GET_MEMORY_L (tmp
);
3404 h8_set_ccr (cpu
, GET_MEMORY_W (tmp
));
3406 pc
= GET_MEMORY_W (tmp
);
3411 h8_set_reg (cpu
, SP_REGNUM
, tmp
);
3414 case O (O_RTS
, SN
): /* rts, return from subroutine */
3416 tmp
= h8_get_reg (cpu
, SP_REGNUM
);
3418 if (h8300hmode
&& !h8300_normal_mode
)
3420 pc
= GET_MEMORY_L (tmp
);
3425 pc
= GET_MEMORY_W (tmp
);
3429 h8_set_reg (cpu
, SP_REGNUM
, tmp
);
3432 case O (O_ILL
, SB
): /* illegal */
3433 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
3436 case O (O_SLEEP
, SN
): /* sleep */
3437 /* Check for magic numbers in r1 and r2. */
3438 if ((h8_get_reg (cpu
, R1_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC1
&&
3439 (h8_get_reg (cpu
, R2_REGNUM
) & 0xffff) == LIBC_EXIT_MAGIC2
&&
3440 SIM_WIFEXITED (h8_get_reg (cpu
, 0)))
3442 /* This trap comes from _exit, not from gdb. */
3443 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_exited
,
3444 SIM_WEXITSTATUS (h8_get_reg (cpu
, 0)));
3447 /* Unfortunately this won't really work, because
3448 when we take a breakpoint trap, R0 has a "random",
3449 user-defined value. Don't see any immediate solution. */
3450 else if (SIM_WIFSTOPPED (h8_get_reg (cpu
, 0)))
3452 /* Pass the stop signal up to gdb. */
3453 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
,
3454 SIM_WSTOPSIG (h8_get_reg (cpu
, 0)));
3459 /* Treat it as a sigtrap. */
3460 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGTRAP
);
3464 case O (O_TRAPA
, SB
): /* trapa */
3465 if (fetch (sd
, &code
->src
, &res
))
3466 goto end
; /* res is vector number. */
3468 tmp
= h8_get_reg (cpu
, SP_REGNUM
);
3469 if(h8300_normal_mode
)
3472 SET_MEMORY_W (tmp
, code
->next_pc
);
3474 SET_MEMORY_W (tmp
, h8_get_ccr (cpu
));
3479 SET_MEMORY_L (tmp
, code
->next_pc
);
3481 SET_MEMORY_L (tmp
, h8_get_ccr (cpu
));
3489 SET_MEMORY_L (tmp
, h8_get_exr (cpu
));
3492 h8_set_reg (cpu
, SP_REGNUM
, tmp
);
3494 if(h8300_normal_mode
)
3495 pc
= GET_MEMORY_L (0x10 + res
* 2); /* Vector addresses are 0x10,0x12,0x14 and 0x16 */
3497 pc
= GET_MEMORY_L (0x20 + res
* 4);
3501 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGTRAP
);
3504 case O (O_BSETEQ
, SB
):
3509 case O (O_BSETNE
, SB
):
3514 case O (O_BCLREQ
, SB
):
3519 case O (O_BCLRNE
, SB
):
3524 OBITOP (O_BNOT
, 1, 1, ea
^= m
); /* bnot */
3525 OBITOP (O_BTST
, 1, 0, nz
= ea
& m
); /* btst */
3527 OBITOP (O_BSET
, 1, 1, ea
|= m
); /* bset */
3529 OBITOP (O_BCLR
, 1, 1, ea
&= ~m
); /* bclr */
3530 OBITOP (O_BLD
, 1, 0, c
= ea
& m
); /* bld */
3531 OBITOP (O_BILD
, 1, 0, c
= !(ea
& m
)); /* bild */
3532 OBITOP (O_BST
, 1, 1, ea
&= ~m
;
3533 if (C
) ea
|= m
); /* bst */
3534 OBITOP (O_BIST
, 1, 1, ea
&= ~m
;
3535 if (!C
) ea
|= m
); /* bist */
3536 OBITOP (O_BSTZ
, 1, 1, ea
&= ~m
;
3537 if (Z
) ea
|= m
); /* bstz */
3538 OBITOP (O_BISTZ
, 1, 1, ea
&= ~m
;
3539 if (!Z
) ea
|= m
); /* bistz */
3540 OBITOP (O_BAND
, 1, 0, c
= (ea
& m
) && C
); /* band */
3541 OBITOP (O_BIAND
, 1, 0, c
= !(ea
& m
) && C
); /* biand */
3542 OBITOP (O_BOR
, 1, 0, c
= (ea
& m
) || C
); /* bor */
3543 OBITOP (O_BIOR
, 1, 0, c
= !(ea
& m
) || C
); /* bior */
3544 OBITOP (O_BXOR
, 1, 0, c
= ((ea
& m
) != 0)!= C
); /* bxor */
3545 OBITOP (O_BIXOR
, 1, 0, c
= !(ea
& m
) != C
); /* bixor */
3547 case O (O_BFLD
, SB
): /* bfld */
3550 if (fetch (sd
, &code
->src
, &bit
))
3555 if (fetch (sd
, &code
->dst
, &ea
))
3565 if (store (sd
, &code
->op3
, ea
))
3570 case O(O_BFST
, SB
): /* bfst */
3571 /* bitfield store */
3572 /* NOTE: the imm8 value is in dst, and the ea value
3573 (which is actually the destination) is in op3.
3574 It has to be that way, to avoid breaking the assembler. */
3576 if (fetch (sd
, &code
->dst
, &bit
)) /* imm8 */
3578 if (bit
== 0) /* noop -- nothing to do. */
3581 if (fetch (sd
, &code
->src
, &rd
)) /* reg8 src */
3584 if (fetch2 (sd
, &code
->op3
, &ea
)) /* ea dst */
3587 /* Left-shift the register data into position. */
3588 for (tmp
= bit
; !(tmp
& 1); tmp
>>= 1)
3591 /* Combine it with the neighboring bits. */
3592 ea
= (ea
& ~bit
) | (rd
& bit
);
3595 if (store2 (sd
, &code
->op3
, ea
))
3599 case O (O_CLRMAC
, SN
): /* clrmac */
3600 h8_set_mach (cpu
, 0);
3601 h8_set_macl (cpu
, 0);
3602 h8_set_macZ (cpu
, 1);
3603 h8_set_macV (cpu
, 0);
3604 h8_set_macN (cpu
, 0);
3607 case O (O_STMAC
, SL
): /* stmac, 260 */
3608 switch (code
->src
.type
) {
3609 case X (OP_MACH
, SL
):
3610 res
= h8_get_mach (cpu
);
3611 if (res
& 0x200) /* sign extend */
3614 case X (OP_MACL
, SL
):
3615 res
= h8_get_macl (cpu
);
3617 default: goto illegal
;
3619 nz
= !h8_get_macZ (cpu
);
3620 n
= h8_get_macN (cpu
);
3621 v
= h8_get_macV (cpu
);
3623 if (store (sd
, &code
->dst
, res
))
3628 case O (O_LDMAC
, SL
): /* ldmac, 179 */
3629 if (fetch (sd
, &code
->src
, &rd
))
3632 switch (code
->dst
.type
) {
3633 case X (OP_MACH
, SL
):
3634 rd
&= 0x3ff; /* Truncate to 10 bits */
3635 h8_set_mach (cpu
, rd
);
3637 case X (OP_MACL
, SL
):
3638 h8_set_macl (cpu
, rd
);
3640 default: goto illegal
;
3642 h8_set_macV (cpu
, 0);
3646 if (fetch (sd
, &code
->src
, &rd
) ||
3647 fetch (sd
, &code
->dst
, &res
))
3650 /* Ye gods, this is non-portable!
3651 However, the existing mul/div code is similar. */
3652 res
= SEXTSHORT (res
) * SEXTSHORT (rd
);
3654 if (h8_get_macS (cpu
)) /* Saturating mode */
3656 long long mac
= h8_get_macl (cpu
);
3658 if (mac
& 0x80000000) /* sign extend */
3659 mac
|= 0xffffffff00000000LL
;
3662 if (mac
> 0x7fffffff || mac
< 0xffffffff80000000LL
)
3663 h8_set_macV (cpu
, 1);
3664 h8_set_macZ (cpu
, (mac
== 0));
3665 h8_set_macN (cpu
, (mac
< 0));
3666 h8_set_macl (cpu
, (int) mac
);
3668 else /* "Less Saturating" mode */
3670 long long mac
= h8_get_mach (cpu
);
3672 mac
+= h8_get_macl (cpu
);
3674 if (mac
& 0x20000000000LL
) /* sign extend */
3675 mac
|= 0xfffffc0000000000LL
;
3678 if (mac
> 0x1ffffffffffLL
||
3679 mac
< (long long) 0xfffffe0000000000LL
)
3680 h8_set_macV (cpu
, 1);
3681 h8_set_macZ (cpu
, (mac
== 0));
3682 h8_set_macN (cpu
, (mac
< 0));
3683 h8_set_macl (cpu
, (int) mac
);
3685 h8_set_mach (cpu
, (int) (mac
& 0x3ff));
3689 case O (O_MULS
, SW
): /* muls.w */
3690 if (fetch (sd
, &code
->src
, &ea
) ||
3691 fetch (sd
, &code
->dst
, &rd
))
3694 ea
= SEXTSHORT (ea
);
3695 res
= SEXTSHORT (ea
* SEXTSHORT (rd
));
3699 if (store (sd
, &code
->dst
, res
))
3704 case O (O_MULS
, SL
): /* muls.l */
3705 if (fetch (sd
, &code
->src
, &ea
) ||
3706 fetch (sd
, &code
->dst
, &rd
))
3711 n
= res
& 0x80000000;
3712 nz
= res
& 0xffffffff;
3713 if (store (sd
, &code
->dst
, res
))
3717 case O (O_MULSU
, SL
): /* muls/u.l */
3718 if (fetch (sd
, &code
->src
, &ea
) ||
3719 fetch (sd
, &code
->dst
, &rd
))
3722 /* Compute upper 32 bits of the 64-bit result. */
3723 res
= (((long long) ea
) * ((long long) rd
)) >> 32;
3725 n
= res
& 0x80000000;
3726 nz
= res
& 0xffffffff;
3727 if (store (sd
, &code
->dst
, res
))
3731 case O (O_MULU
, SW
): /* mulu.w */
3732 if (fetch (sd
, &code
->src
, &ea
) ||
3733 fetch (sd
, &code
->dst
, &rd
))
3736 res
= UEXTSHORT ((UEXTSHORT (ea
) * UEXTSHORT (rd
)));
3738 /* Don't set Z or N. */
3739 if (store (sd
, &code
->dst
, res
))
3744 case O (O_MULU
, SL
): /* mulu.l */
3745 if (fetch (sd
, &code
->src
, &ea
) ||
3746 fetch (sd
, &code
->dst
, &rd
))
3751 /* Don't set Z or N. */
3752 if (store (sd
, &code
->dst
, res
))
3757 case O (O_MULUU
, SL
): /* mulu/u.l */
3758 if (fetch (sd
, &code
->src
, &ea
) ||
3759 fetch (sd
, &code
->dst
, &rd
))
3762 /* Compute upper 32 bits of the 64-bit result. */
3763 res
= (((unsigned long long) (unsigned) ea
) *
3764 ((unsigned long long) (unsigned) rd
)) >> 32;
3766 /* Don't set Z or N. */
3767 if (store (sd
, &code
->dst
, res
))
3772 case O (O_MULXS
, SB
): /* mulxs.b */
3773 if (fetch (sd
, &code
->src
, &ea
) ||
3774 fetch (sd
, &code
->dst
, &rd
))
3778 res
= ea
* SEXTCHAR (rd
);
3782 if (store (sd
, &code
->dst
, res
))
3787 case O (O_MULXS
, SW
): /* mulxs.w */
3788 if (fetch (sd
, &code
->src
, &ea
) ||
3789 fetch (sd
, &code
->dst
, &rd
))
3792 ea
= SEXTSHORT (ea
);
3793 res
= ea
* SEXTSHORT (rd
& 0xffff);
3795 n
= res
& 0x80000000;
3796 nz
= res
& 0xffffffff;
3797 if (store (sd
, &code
->dst
, res
))
3802 case O (O_MULXU
, SB
): /* mulxu.b */
3803 if (fetch (sd
, &code
->src
, &ea
) ||
3804 fetch (sd
, &code
->dst
, &rd
))
3807 res
= UEXTCHAR (ea
) * UEXTCHAR (rd
);
3809 if (store (sd
, &code
->dst
, res
))
3814 case O (O_MULXU
, SW
): /* mulxu.w */
3815 if (fetch (sd
, &code
->src
, &ea
) ||
3816 fetch (sd
, &code
->dst
, &rd
))
3819 res
= UEXTSHORT (ea
) * UEXTSHORT (rd
);
3821 if (store (sd
, &code
->dst
, res
))
3826 case O (O_TAS
, SB
): /* tas (test and set) */
3827 if (!h8300sxmode
) /* h8sx can use any register. */
3828 switch (code
->src
.reg
)
3839 if (fetch (sd
, &code
->src
, &res
))
3841 if (store (sd
, &code
->src
, res
| 0x80))
3844 goto just_flags_log8
;
3846 case O (O_DIVU
, SW
): /* divu.w */
3847 if (fetch (sd
, &code
->src
, &ea
) ||
3848 fetch (sd
, &code
->dst
, &rd
))
3854 res
= (unsigned) (UEXTSHORT (rd
) / UEXTSHORT (ea
));
3858 if (store (sd
, &code
->dst
, res
))
3862 case O (O_DIVU
, SL
): /* divu.l */
3863 if (fetch (sd
, &code
->src
, &ea
) ||
3864 fetch (sd
, &code
->dst
, &rd
))
3867 n
= ea
& 0x80000000;
3868 nz
= ea
& 0xffffffff;
3870 res
= (unsigned) rd
/ ea
;
3874 if (store (sd
, &code
->dst
, res
))
3878 case O (O_DIVS
, SW
): /* divs.w */
3879 if (fetch (sd
, &code
->src
, &ea
) ||
3880 fetch (sd
, &code
->dst
, &rd
))
3885 res
= SEXTSHORT (rd
) / SEXTSHORT (ea
);
3895 if (store (sd
, &code
->dst
, res
))
3899 case O (O_DIVS
, SL
): /* divs.l */
3900 if (fetch (sd
, &code
->src
, &ea
) ||
3901 fetch (sd
, &code
->dst
, &rd
))
3915 n
= res
& 0x80000000;
3916 if (store (sd
, &code
->dst
, res
))
3920 case O (O_DIVXU
, SB
): /* divxu.b */
3921 if (fetch (sd
, &code
->src
, &ea
) ||
3922 fetch (sd
, &code
->dst
, &rd
))
3925 rd
= UEXTSHORT (rd
);
3932 tmp
= (unsigned) rd
% ea
;
3933 res
= (unsigned) rd
/ ea
;
3941 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
3945 case O (O_DIVXU
, SW
): /* divxu.w */
3946 if (fetch (sd
, &code
->src
, &ea
) ||
3947 fetch (sd
, &code
->dst
, &rd
))
3950 ea
= UEXTSHORT (ea
);
3956 tmp
= (unsigned) rd
% ea
;
3957 res
= (unsigned) rd
/ ea
;
3965 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
3969 case O (O_DIVXS
, SB
): /* divxs.b */
3970 if (fetch (sd
, &code
->src
, &ea
) ||
3971 fetch (sd
, &code
->dst
, &rd
))
3974 rd
= SEXTSHORT (rd
);
3979 tmp
= (int) rd
% (int) ea
;
3980 res
= (int) rd
/ (int) ea
;
3991 if (store (sd
, &code
->dst
, (res
& 0xff) | (tmp
<< 8)))
3995 case O (O_DIVXS
, SW
): /* divxs.w */
3996 if (fetch (sd
, &code
->src
, &ea
) ||
3997 fetch (sd
, &code
->dst
, &rd
))
4000 ea
= SEXTSHORT (ea
);
4004 tmp
= (int) rd
% (int) ea
;
4005 res
= (int) rd
/ (int) ea
;
4015 n
= res
& 0x80000000;
4016 if (store (sd
, &code
->dst
, (res
& 0xffff) | (tmp
<< 16)))
4020 case O (O_EXTS
, SW
): /* exts.w, signed extend */
4021 if (fetch2 (sd
, &code
->dst
, &rd
))
4023 ea
= rd
& 0x80 ? -256 : 0;
4024 res
= (rd
& 0xff) + ea
;
4027 case O (O_EXTS
, SL
): /* exts.l, signed extend */
4028 if (fetch2 (sd
, &code
->dst
, &rd
))
4030 if (code
->src
.type
== X (OP_IMM
, SL
))
4032 if (fetch (sd
, &code
->src
, &ea
))
4035 if (ea
== 2) /* exts.l #2, nn */
4037 /* Sign-extend from 8-bit to 32-bit. */
4038 ea
= rd
& 0x80 ? -256 : 0;
4039 res
= (rd
& 0xff) + ea
;
4043 /* Sign-extend from 16-bit to 32-bit. */
4044 ea
= rd
& 0x8000 ? -65536 : 0;
4045 res
= (rd
& 0xffff) + ea
;
4048 case O (O_EXTU
, SW
): /* extu.w, unsigned extend */
4049 if (fetch2 (sd
, &code
->dst
, &rd
))
4052 res
= (rd
& 0xff) + ea
;
4055 case O (O_EXTU
, SL
): /* extu.l, unsigned extend */
4056 if (fetch2 (sd
, &code
->dst
, &rd
))
4058 if (code
->src
.type
== X (OP_IMM
, SL
))
4060 if (fetch (sd
, &code
->src
, &ea
))
4063 if (ea
== 2) /* extu.l #2, nn */
4065 /* Zero-extend from 8-bit to 32-bit. */
4067 res
= (rd
& 0xff) + ea
;
4071 /* Zero-extend from 16-bit to 32-bit. */
4073 res
= (rd
& 0xffff) + ea
;
4076 case O (O_NOP
, SN
): /* nop */
4079 case O (O_STM
, SL
): /* stm, store to memory */
4081 int nregs
, firstreg
, i
;
4083 nregs
= GET_MEMORY_B (pc
+ 1);
4086 firstreg
= code
->src
.reg
;
4088 for (i
= firstreg
; i
<= firstreg
+ nregs
; i
++)
4090 h8_set_reg (cpu
, SP_REGNUM
, h8_get_reg (cpu
, SP_REGNUM
) - 4);
4091 SET_MEMORY_L (h8_get_reg (cpu
, SP_REGNUM
), h8_get_reg (cpu
, i
));
4096 case O (O_LDM
, SL
): /* ldm, load from memory */
4097 case O (O_RTEL
, SN
): /* rte/l, ldm plus rte */
4098 case O (O_RTSL
, SN
): /* rts/l, ldm plus rts */
4100 int nregs
, firstreg
, i
;
4102 nregs
= ((GET_MEMORY_B (pc
+ 1) >> 4) & 0xf);
4103 firstreg
= code
->dst
.reg
& 0xf;
4104 for (i
= firstreg
; i
>= firstreg
- nregs
; i
--)
4106 h8_set_reg (cpu
, i
, GET_MEMORY_L (h8_get_reg (cpu
, SP_REGNUM
)));
4107 h8_set_reg (cpu
, SP_REGNUM
, h8_get_reg (cpu
, SP_REGNUM
) + 4);
4110 switch (code
->opcode
) {
4111 case O (O_RTEL
, SN
):
4113 case O (O_RTSL
, SN
):
4122 /* Decimal Adjust Addition. This is for BCD arithmetic. */
4123 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch? */
4124 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4125 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4126 /* Nothing. */; /* Value added == 0. */
4127 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4128 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4129 res
= res
+ 0x6; /* Value added == 6. */
4130 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4131 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4132 res
= res
+ 0x6; /* Value added == 6. */
4133 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4134 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4135 res
= res
+ 0x60; /* Value added == 60. */
4136 else if (!c
&& (9 <= (res
>> 4) && (res
>> 4) <= 15) &&
4137 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4138 res
= res
+ 0x66; /* Value added == 66. */
4139 else if (!c
&& (10 <= (res
>> 4) && (res
>> 4) <= 15) &&
4140 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4141 res
= res
+ 0x66; /* Value added == 66. */
4142 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4143 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4144 res
= res
+ 0x60; /* Value added == 60. */
4145 else if ( c
&& (1 <= (res
>> 4) && (res
>> 4) <= 2) &&
4146 !h
&& (10 <= (res
& 0xf) && (res
& 0xf) <= 15))
4147 res
= res
+ 0x66; /* Value added == 66. */
4148 else if (c
&& (1 <= (res
>> 4) && (res
>> 4) <= 3) &&
4149 h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 3))
4150 res
= res
+ 0x66; /* Value added == 66. */
4155 /* Decimal Adjust Subtraction. This is for BCD arithmetic. */
4156 res
= GET_B_REG (code
->src
.reg
); /* FIXME fetch, fetch2... */
4157 if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 9) &&
4158 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4159 /* Nothing. */; /* Value added == 0. */
4160 else if (!c
&& (0 <= (res
>> 4) && (res
>> 4) <= 8) &&
4161 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4162 res
= res
+ 0xfa; /* Value added == 0xfa. */
4163 else if ( c
&& (7 <= (res
>> 4) && (res
>> 4) <= 15) &&
4164 !h
&& (0 <= (res
& 0xf) && (res
& 0xf) <= 9))
4165 res
= res
+ 0xa0; /* Value added == 0xa0. */
4166 else if (c
&& (6 <= (res
>> 4) && (res
>> 4) <= 15) &&
4167 h
&& (6 <= (res
& 0xf) && (res
& 0xf) <= 15))
4168 res
= res
+ 0x9a; /* Value added == 0x9a. */
4174 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
4179 sim_io_printf (sd
, "sim_resume: internal error.\n");
4180 sim_engine_halt (sd
, cpu
, NULL
, pc
, sim_stopped
, SIM_SIGILL
);
4184 if (code
->dst
.type
== X (OP_CCR
, SB
) ||
4185 code
->dst
.type
== X (OP_CCR
, SW
))
4187 h8_set_ccr (cpu
, res
);
4190 else if (h8300smode
&&
4191 (code
->dst
.type
== X (OP_EXR
, SB
) ||
4192 code
->dst
.type
== X (OP_EXR
, SW
)))
4194 h8_set_exr (cpu
, res
);
4195 if (h8300smode
) /* Get exr. */
4197 trace
= (h8_get_exr (cpu
) >> 7) & 1;
4198 intMask
= h8_get_exr (cpu
) & 7;
4207 /* When a branch works */
4208 if (fetch (sd
, &code
->src
, &res
))
4210 if (res
& 1) /* bad address */
4212 pc
= code
->next_pc
+ res
;
4215 /* Set the cond codes from res */
4218 /* Set the flags after an 8 bit inc/dec operation */
4222 v
= (rd
& 0x7f) == 0x7f;
4225 /* Set the flags after an 16 bit inc/dec operation */
4229 v
= (rd
& 0x7fff) == 0x7fff;
4232 /* Set the flags after an 32 bit inc/dec operation */
4234 n
= res
& 0x80000000;
4235 nz
= res
& 0xffffffff;
4236 v
= (rd
& 0x7fffffff) == 0x7fffffff;
4240 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
4243 if (store2 (sd
, &code
->dst
, rd
))
4248 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
4251 if (store2 (sd
, &code
->dst
, rd
))
4256 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
4257 n
= (rd
& 0x80000000);
4258 nz
= rd
& 0xffffffff;
4259 if (store2 (sd
, &code
->dst
, rd
))
4264 if (store2 (sd
, &code
->dst
, res
))
4268 /* flags after a 32bit logical operation */
4269 n
= res
& 0x80000000;
4270 nz
= res
& 0xffffffff;
4275 if (store2 (sd
, &code
->dst
, res
))
4279 /* flags after a 16bit logical operation */
4286 if (store2 (sd
, &code
->dst
, res
))
4296 if (store2 (sd
, &code
->dst
, res
))
4303 switch (code
->opcode
/ 4)
4307 v
= ((rd
& 0x80) == (ea
& 0x80)
4308 && (rd
& 0x80) != (res
& 0x80));
4313 v
= ((rd
& 0x80) != (-ea
& 0x80)
4314 && (rd
& 0x80) != (res
& 0x80));
4321 break; /* No effect on v flag. */
4326 if (store2 (sd
, &code
->dst
, res
))
4332 c
= (res
& 0x10000);
4333 switch (code
->opcode
/ 4)
4337 v
= ((rd
& 0x8000) == (ea
& 0x8000)
4338 && (rd
& 0x8000) != (res
& 0x8000));
4343 v
= ((rd
& 0x8000) != (-ea
& 0x8000)
4344 && (rd
& 0x8000) != (res
& 0x8000));
4353 if (store2 (sd
, &code
->dst
, res
))
4357 n
= res
& 0x80000000;
4358 nz
= res
& 0xffffffff;
4359 switch (code
->opcode
/ 4)
4363 v
= ((rd
& 0x80000000) == (ea
& 0x80000000)
4364 && (rd
& 0x80000000) != (res
& 0x80000000));
4365 c
= ((unsigned) res
< (unsigned) rd
) ||
4366 ((unsigned) res
< (unsigned) ea
);
4371 v
= ((rd
& 0x80000000) != (-ea
& 0x80000000)
4372 && (rd
& 0x80000000) != (res
& 0x80000000));
4373 c
= (unsigned) rd
< (unsigned) -ea
;
4376 v
= (rd
== 0x80000000);
4383 if ((res
= h8_get_delayed_branch (cpu
)) != 0)
4386 h8_set_delayed_branch (cpu
, 0);
4394 h8_set_ticks (cpu
, h8_get_ticks (cpu
) + get_now () - tick_start
);
4395 h8_set_cycles (cpu
, h8_get_cycles (cpu
) + cycles
);
4396 h8_set_insts (cpu
, h8_get_insts (cpu
) + insts
);
4397 cpu_set_pc (cpu
, pc
);
4401 h8_set_exr (cpu
, (trace
<<7) | intMask
);
4403 h8_set_mask (cpu
, oldmask
);
4407 sim_engine_run (SIM_DESC sd
,
4408 int next_cpu_nr
, /* ignore */
4409 int nr_cpus
, /* ignore */
4414 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4416 cpu
= STATE_CPU (sd
, 0);
4420 step_once (sd
, cpu
);
4421 if (sim_events_tick (sd
))
4422 sim_events_process (sd
);
4427 sim_write (SIM_DESC sd
, uint64_t addr
, const void *buffer
, uint64_t size
)
4429 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
4431 const unsigned char *data
= buffer
;
4436 for (i
= 0; i
< size
; i
++)
4438 if (addr
< memory_size
)
4440 h8_set_memory (cpu
, addr
+ i
, data
[i
]);
4449 sim_read (SIM_DESC sd
, uint64_t addr
, void *buffer
, uint64_t size
)
4451 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
4456 if (addr
+ size
< memory_size
)
4457 memcpy (buffer
, h8_get_memory_buf (cpu
) + addr
, size
);
4464 h8300_reg_store (SIM_CPU
*cpu
, int rn
, const void *buf
, int length
)
4466 const unsigned char *value
= buf
;
4471 longval
= (value
[0] << 24) | (value
[1] << 16) | (value
[2] << 8) | value
[3];
4472 shortval
= (value
[0] << 8) | (value
[1]);
4473 intval
= h8300hmode
? longval
: shortval
;
4475 init_pointers (CPU_STATE (cpu
));
4479 if(h8300_normal_mode
)
4480 cpu_set_pc (cpu
, shortval
); /* PC for Normal mode is 2 bytes */
4482 cpu_set_pc (cpu
, intval
);
4500 h8_set_reg (cpu
, rn
, intval
);
4505 h8_set_reg (cpu
, rn
, longval
);
4512 h8300_reg_fetch (SIM_CPU
*cpu
, int rn
, void *buf
, int length
)
4514 unsigned char *value
= buf
;
4518 init_pointers (CPU_STATE (cpu
));
4520 if (!h8300smode
&& rn
>= EXR_REGNUM
)
4527 v
= cpu_get_pc (cpu
);
4543 v
= h8_get_reg (cpu
, rn
);
4548 v
= h8_get_reg (cpu
, rn
);
4555 /* In Normal mode PC is 2 byte, but other registers are 4 byte */
4556 if ((h8300hmode
|| longreg
) && !(rn
== PC_REGNUM
&& h8300_normal_mode
))
4573 sim_info (SIM_DESC sd
, bool verbose
)
4575 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
4576 double timetaken
= (double) h8_get_ticks (cpu
) / (double) now_persec ();
4577 double virttime
= h8_get_cycles (cpu
) / 10.0e6
;
4579 sim_io_printf (sd
, "\n\n#instructions executed %10d\n", h8_get_insts (cpu
));
4580 sim_io_printf (sd
, "#cycles (v approximate) %10d\n", h8_get_cycles (cpu
));
4581 sim_io_printf (sd
, "#real time taken %10.4f\n", timetaken
);
4582 sim_io_printf (sd
, "#virtual time taken %10.4f\n", virttime
);
4583 if (timetaken
!= 0.0)
4584 sim_io_printf (sd
, "#simulation ratio %10.4f\n", virttime
/ timetaken
);
4587 /* This to be conditional on `what' (aka `verbose'),
4588 however it was never passed as non-zero. */
4592 for (i
= 0; i
< O_LAST
; i
++)
4594 if (h8_get_stats (sd
, i
))
4595 sim_io_printf (sd
, "%d: %d\n", i
, h8_get_stats (sd
, i
));
4601 /* Indicate whether the cpu is an H8/300 or H8/300H.
4602 FLAG is non-zero for the H8/300H. */
4605 set_h8300h (unsigned long machine
)
4607 /* FIXME: Much of the code in sim_load can be moved to sim_open.
4608 This function being replaced by a sim_open:ARGV configuration
4611 h8300hmode
= h8300smode
= h8300sxmode
= h8300_normal_mode
= 0;
4613 if (machine
== bfd_mach_h8300sx
|| machine
== bfd_mach_h8300sxn
)
4616 if (machine
== bfd_mach_h8300s
|| machine
== bfd_mach_h8300sn
|| h8300sxmode
)
4619 if (machine
== bfd_mach_h8300h
|| machine
== bfd_mach_h8300hn
|| h8300smode
)
4622 if(machine
== bfd_mach_h8300hn
|| machine
== bfd_mach_h8300sn
|| machine
== bfd_mach_h8300sxn
)
4623 h8300_normal_mode
= 1;
4626 /* H8300-specific options.
4627 TODO: These really should be merged into the common model modules. */
4635 h8300_option_handler (SIM_DESC sd
, sim_cpu
*cpu ATTRIBUTE_UNUSED
, int opt
,
4636 char *arg
, int is_command ATTRIBUTE_UNUSED
)
4638 switch ((H8300_OPTIONS
) opt
)
4641 set_h8300h (bfd_mach_h8300h
);
4644 set_h8300h (bfd_mach_h8300s
);
4646 case OPTION_H8300SX
:
4647 set_h8300h (bfd_mach_h8300sx
);
4651 /* We'll actually never get here; the caller handles the error
4653 sim_io_eprintf (sd
, "Unknown option `%s'\n", arg
);
4660 static const OPTION h8300_options
[] =
4662 { {"h8300h", no_argument
, NULL
, OPTION_H8300H
},
4663 'h', NULL
, "Indicate the CPU is H8/300H",
4664 h8300_option_handler
},
4665 { {"h8300s", no_argument
, NULL
, OPTION_H8300S
},
4666 'S', NULL
, "Indicate the CPU is H8S",
4667 h8300_option_handler
},
4668 { {"h8300sx", no_argument
, NULL
, OPTION_H8300SX
},
4669 'x', NULL
, "Indicate the CPU is H8SX",
4670 h8300_option_handler
},
4671 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
4675 h8300_pc_get (sim_cpu
*cpu
)
4677 return H8300_SIM_CPU (cpu
)->pc
;
4681 h8300_pc_set (sim_cpu
*cpu
, sim_cia pc
)
4683 H8300_SIM_CPU (cpu
)->pc
= pc
;
4686 /* Cover function of sim_state_free to free the cpu buffers as well. */
4689 free_state (SIM_DESC sd
)
4691 if (STATE_MODULES (sd
) != NULL
)
4692 sim_module_uninstall (sd
);
4694 /* Fixme: free buffers in _sim_cpu. */
4695 sim_state_free (sd
);
4699 sim_open (SIM_OPEN_KIND kind
,
4700 struct host_callback_struct
*callback
,
4708 sd
= sim_state_alloc_extra (kind
, callback
, sizeof (struct h8300_sim_state
));
4710 /* Set default options before parsing user options. */
4711 current_target_byte_order
= BFD_ENDIAN_BIG
;
4713 /* The cpu data is kept in a separately allocated chunk of memory. */
4714 if (sim_cpu_alloc_all_extra (sd
, 0, sizeof (struct h8300_sim_cpu
))
4721 cpu
= STATE_CPU (sd
, 0);
4722 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
4723 h8_set_reg (cpu
, SBR_REGNUM
, 0xFFFFFF00);
4724 /* sim_cpu object is new, so some initialization is needed. */
4725 init_pointers_needed
= 1;
4727 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
4733 if (sim_add_option_table (sd
, NULL
, h8300_options
) != SIM_RC_OK
)
4739 /* The parser will print an error message for us, so we silently return. */
4740 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
4742 /* Uninstall the modules to avoid memory leaks,
4743 file descriptor leaks, etc. */
4748 /* Check for/establish the a reference program image. */
4749 if (sim_analyze_program (sd
, STATE_PROG_FILE (sd
), abfd
) != SIM_RC_OK
)
4755 /* Establish any remaining configuration options. */
4756 if (sim_config (sd
) != SIM_RC_OK
)
4762 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
4764 /* Uninstall the modules to avoid memory leaks,
4765 file descriptor leaks, etc. */
4770 /* CPU specific initialization. */
4771 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
4773 cpu
= STATE_CPU (sd
, i
);
4775 CPU_REG_FETCH (cpu
) = h8300_reg_fetch
;
4776 CPU_REG_STORE (cpu
) = h8300_reg_store
;
4777 CPU_PC_FETCH (cpu
) = h8300_pc_get
;
4778 CPU_PC_STORE (cpu
) = h8300_pc_set
;
4781 /* sim_hw_configure (sd); */
4783 /* FIXME: Much of the code in sim_load can be moved here. */
4788 /* Called by gdb to load a program into memory. */
4791 sim_load (SIM_DESC sd
, const char *prog
, bfd
*abfd
, int from_tty
)
4793 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
4794 struct h8300_sim_state
*state
= H8300_SIM_STATE (sd
);
4797 /* FIXME: The code below that sets a specific variant of the H8/300
4798 being simulated should be moved to sim_open(). */
4800 /* See if the file is for the H8/300 or H8/300H. */
4801 /* ??? This may not be the most efficient way. The z8k simulator
4802 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
4806 prog_bfd
= bfd_openr (prog
, NULL
);
4807 if (prog_bfd
!= NULL
)
4809 /* Set the cpu type. We ignore failure from bfd_check_format
4810 and bfd_openr as sim_load_file checks too. */
4811 if (bfd_check_format (prog_bfd
, bfd_object
))
4813 set_h8300h (bfd_get_mach (prog_bfd
));
4817 /* If we're using gdb attached to the simulator, then we have to
4818 reallocate memory for the simulator.
4820 When gdb first starts, it calls fetch_registers (among other
4821 functions), which in turn calls init_pointers, which allocates
4824 The problem is when we do that, we don't know whether we're
4825 debugging an H8/300 or H8/300H program.
4827 This is the first point at which we can make that determination,
4828 so we just reallocate memory now; this will also allow us to handle
4829 switching between H8/300 and H8/300H programs without exiting
4832 if (h8300smode
&& !h8300_normal_mode
)
4833 memory_size
= H8300S_MSIZE
;
4834 else if (h8300hmode
&& !h8300_normal_mode
)
4835 memory_size
= H8300H_MSIZE
;
4837 memory_size
= H8300_MSIZE
;
4839 if (h8_get_memory_buf (cpu
))
4840 free (h8_get_memory_buf (cpu
));
4842 h8_set_memory_buf (cpu
, (unsigned char *)
4843 calloc (sizeof (char), memory_size
));
4844 state
->memory_size
= memory_size
;
4846 /* `msize' must be a power of two. */
4847 if ((memory_size
& (memory_size
- 1)) != 0)
4849 sim_io_printf (sd
, "sim_load: bad memory size.\n");
4852 h8_set_mask (cpu
, memory_size
- 1);
4854 if (sim_load_file (sd
, STATE_MY_NAME (sd
), STATE_CALLBACK (sd
), prog
,
4855 prog_bfd
, STATE_OPEN_KIND (sd
) == SIM_OPEN_DEBUG
,
4859 /* Close the bfd if we opened it. */
4860 if (abfd
== NULL
&& prog_bfd
!= NULL
)
4861 bfd_close (prog_bfd
);
4865 /* Close the bfd if we opened it. */
4866 if (abfd
== NULL
&& prog_bfd
!= NULL
)
4867 bfd_close (prog_bfd
);
4872 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
4873 char * const *argv
, char * const *env
)
4875 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
4880 cpu_set_pc (cpu
, bfd_get_start_address (abfd
));
4882 cpu_set_pc (cpu
, 0);
4884 /* Command Line support. */
4887 /* Counting the no. of commandline arguments. */
4888 for (no_of_args
= 0; argv
[no_of_args
] != NULL
; no_of_args
++)
4891 /* Allocating memory for the argv pointers. */
4892 h8_set_command_line (cpu
, (char **) malloc ((sizeof (char *))
4893 * (no_of_args
+ 1)));
4895 for (i
= 0; i
< no_of_args
; i
++)
4897 /* Copying the argument string. */
4898 h8_set_cmdline_arg (cpu
, i
, (char *) strdup (argv
[i
]));
4900 h8_set_cmdline_arg (cpu
, i
, NULL
);