2 * Simulator for the 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.
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
35 #include "gdb/callback.h"
36 #include "gdb/remote-sim.h"
37 #include "gdb/sim-h8300.h"
45 host_callback
*sim_callback
;
47 static SIM_OPEN_KIND sim_kind
;
50 /* FIXME: Needs to live in header file.
51 This header should also include the things in remote-sim.h.
52 One could move this to remote-sim.h but this function isn't needed
54 void sim_set_simcache_size
PARAMS ((int));
56 #define X(op, size) op * 4 + size
58 #define SP (h8300hmode ? SL : SW)
72 #define h8_opcodes ops
74 #include "opcode/h8300.h"
78 /* The rate at which to call the host's poll_quit callback. */
80 #define POLL_QUIT_INTERVAL 0x80000
82 #define LOW_BYTE(x) ((x) & 0xff)
83 #define HIGH_BYTE(x) (((x) >> 8) & 0xff)
84 #define P(X,Y) ((X << 8) | Y)
87 cpu.ccr = ((I << 7) | (UI << 6) | (H << 5) | (U << 4) \
88 | (N << 3) | (Z << 2) | (V << 1) | C);
91 if (h8300smode) cpu.exr = (trace<<7) | intMask;
94 c = (cpu.ccr >> 0) & 1;\
95 v = (cpu.ccr >> 1) & 1;\
96 nz = !((cpu.ccr >> 2) & 1);\
97 n = (cpu.ccr >> 3) & 1;\
98 u = (cpu.ccr >> 4) & 1;\
99 h = (cpu.ccr >> 5) & 1;\
100 ui = ((cpu.ccr >> 6) & 1);\
101 intMaskBit = (cpu.ccr >> 7) & 1;
106 trace = (cpu.exr >> 7) & 1; \
107 intMask = cpu.exr & 7; \
110 #ifdef __CHAR_IS_SIGNED__
111 #define SEXTCHAR(x) ((char) (x))
115 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff) : x & 0xff)
118 #define UEXTCHAR(x) ((x) & 0xff)
119 #define UEXTSHORT(x) ((x) & 0xffff)
120 #define SEXTSHORT(x) ((short) (x))
122 static cpu_state_type cpu
;
127 static int memory_size
;
132 return time (0); /* WinXX HAS UNIX like 'time', so why not using it? */
153 return h8300hmode
? SL
: SW
;
158 lvalue (int x
, int rn
)
165 return X (OP_IMM
, SP
);
167 return X (OP_REG
, SP
);
170 return X (OP_MEM
, SP
);
173 abort (); /* ?? May be something more usefull? */
178 decode (int addr
, unsigned char *data
, decoded_inst
*dst
)
192 /* Find the exact opcode/arg combo. */
193 for (q
= h8_opcodes
; q
->name
; q
++)
195 op_type
*nib
= q
->data
.nib
;
196 unsigned int len
= 0;
200 op_type looking_for
= *nib
;
201 int thisnib
= data
[len
>> 1];
203 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
205 if (looking_for
< 16 && looking_for
>= 0)
207 if (looking_for
!= thisnib
)
212 if ((int) looking_for
& (int) B31
)
214 if (!(((int) thisnib
& 0x8) != 0))
217 looking_for
= (op_type
) ((int) looking_for
& ~(int) B31
);
221 if ((int) looking_for
& (int) B30
)
223 if (!(((int) thisnib
& 0x8) == 0))
226 looking_for
= (op_type
) ((int) looking_for
& ~(int) B30
);
229 if (looking_for
& DBIT
)
231 /* Exclude adds/subs by looking at bit 0 and 2, and
232 make sure the operand size, either w or l,
233 matches by looking at bit 1. */
234 if ((looking_for
& 7) != (thisnib
& 7))
237 abs
= (thisnib
& 0x8) ? 2 : 1;
239 else if (looking_for
& (REG
| IND
| INC
| DEC
))
241 if (looking_for
& REG
)
243 /* Can work out size from the register. */
244 size
= bitfrom (looking_for
);
246 if (looking_for
& SRC
)
251 else if (looking_for
& L_16
)
253 abs
= (data
[len
>> 1]) * 256 + data
[(len
+ 2) >> 1];
255 if (looking_for
& (PCREL
| DISP
))
260 else if (looking_for
& ABSJMP
)
262 abs
= (data
[1] << 16) | (data
[2] << 8) | (data
[3]);
264 else if (looking_for
& MEMIND
)
268 else if (looking_for
& L_32
)
272 abs
= (data
[i
] << 24)
273 | (data
[i
+ 1] << 16)
279 else if (looking_for
& L_24
)
283 abs
= (data
[i
] << 16) | (data
[i
+ 1] << 8) | (data
[i
+ 2]);
286 else if (looking_for
& IGNORE
)
290 else if (looking_for
& DISPREG
)
292 rdisp
= thisnib
& 0x7;
294 else if (looking_for
& KBIT
)
311 else if (looking_for
& L_8
)
315 if (looking_for
& PCREL
)
317 abs
= SEXTCHAR (data
[len
>> 1]);
319 else if (looking_for
& ABS8MEM
)
322 abs
= h8300hmode
? ~0xff0000ff : ~0xffff00ff;
323 abs
|= data
[len
>> 1] & 0xff;
327 abs
= data
[len
>> 1] & 0xff;
330 else if (looking_for
& L_3
)
336 else if (looking_for
== E
)
340 /* Fill in the args. */
342 op_type
*args
= q
->args
.nib
;
348 int rn
= (x
& DST
) ? rd
: rs
;
358 p
->type
= X (OP_IMM
, size
);
361 else if (x
& (IMM
| KBIT
| DBIT
))
363 p
->type
= X (OP_IMM
, size
);
369 Some ops (like mul) have two sizes. */
372 p
->type
= X (OP_REG
, size
);
377 p
->type
= X (OP_INC
, size
);
382 p
->type
= X (OP_DEC
, size
);
387 p
->type
= X (OP_DISP
, size
);
391 else if (x
& (ABS
| ABSJMP
| ABS8MEM
))
393 p
->type
= X (OP_DISP
, size
);
399 p
->type
= X (OP_MEM
, size
);
404 p
->type
= X (OP_PCREL
, size
);
405 p
->literal
= abs
+ addr
+ 2;
411 p
->type
= X (OP_IMM
, SP
);
416 p
->type
= X (OP_DISP
, size
);
418 p
->reg
= rdisp
& 0x7;
429 printf ("Hmmmm %x", x
);
435 /* But a jmp or a jsr gets automagically lvalued,
436 since we branch to their address not their
438 if (q
->how
== O (O_JSR
, SB
)
439 || q
->how
== O (O_JMP
, SB
))
441 dst
->src
.type
= lvalue (dst
->src
.type
, dst
->src
.reg
);
444 if (dst
->dst
.type
== -1)
447 dst
->opcode
= q
->how
;
448 dst
->cycles
= q
->time
;
450 /* And a jsr to 0xc4 is turned into a magic trap. */
452 if (dst
->opcode
== O (O_JSR
, SB
))
454 if (dst
->src
.literal
== 0xc4)
456 dst
->opcode
= O (O_SYSCALL
, SB
);
460 dst
->next_pc
= addr
+ len
/ 2;
464 printf ("Don't understand %x \n", looking_for
);
475 /* Fell off the end. */
476 dst
->opcode
= O (O_ILL
, SB
);
484 /* Find the next cache entry to use. */
485 idx
= cpu
.cache_top
+ 1;
487 if (idx
>= cpu
.csize
)
493 /* Throw away its old meaning. */
494 cpu
.cache_idx
[cpu
.cache
[idx
].oldpc
] = 0;
496 /* Set to new address. */
497 cpu
.cache
[idx
].oldpc
= pc
;
499 /* Fill in instruction info. */
500 decode (pc
, cpu
.memory
+ pc
, cpu
.cache
+ idx
);
502 /* Point to new cache entry. */
503 cpu
.cache_idx
[pc
] = idx
;
507 static unsigned char *breg
[18];
508 static unsigned short *wreg
[18];
509 static unsigned int *lreg
[18];
511 #define GET_B_REG(x) *(breg[x])
512 #define SET_B_REG(x,y) (*(breg[x])) = (y)
513 #define GET_W_REG(x) *(wreg[x])
514 #define SET_W_REG(x,y) (*(wreg[x])) = (y)
516 #define GET_L_REG(x) *(lreg[x])
517 #define SET_L_REG(x,y) (*(lreg[x])) = (y)
519 #define GET_MEMORY_L(x) \
521 ? ((cpu.memory[x+0] << 24) | (cpu.memory[x+1] << 16) \
522 | (cpu.memory[x+2] << 8) | cpu.memory[x+3]) \
523 : ((cpu.eightbit[(x+0) & 0xff] << 24) | (cpu.eightbit[(x+1) & 0xff] << 16) \
524 | (cpu.eightbit[(x+2) & 0xff] << 8) | cpu.eightbit[(x+3) & 0xff]))
526 #define GET_MEMORY_W(x) \
528 ? ((cpu.memory[x+0] << 8) | (cpu.memory[x+1] << 0)) \
529 : ((cpu.eightbit[(x+0) & 0xff] << 8) | (cpu.eightbit[(x+1) & 0xff] << 0)))
532 #define GET_MEMORY_B(x) \
533 (x < memory_size ? (cpu.memory[x]) : (cpu.eightbit[x & 0xff]))
535 #define SET_MEMORY_L(x,y) \
536 { register unsigned char *_p; register int __y = y; \
537 _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
538 _p[0] = (__y)>>24; _p[1] = (__y)>>16; \
539 _p[2] = (__y)>>8; _p[3] = (__y)>>0;}
541 #define SET_MEMORY_W(x,y) \
542 { register unsigned char *_p; register int __y = y; \
543 _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
544 _p[0] = (__y)>>8; _p[1] =(__y);}
546 #define SET_MEMORY_B(x,y) \
547 (x < memory_size ? (cpu.memory[(x)] = y) : (cpu.eightbit[x & 0xff] = y))
553 int abs
= arg
->literal
;
560 return GET_B_REG (rn
);
562 return GET_W_REG (rn
);
564 return GET_L_REG (rn
);
575 r
= GET_MEMORY_B (t
);
584 r
= GET_MEMORY_W (t
);
592 r
= GET_MEMORY_L (t
);
599 case X (OP_DISP
, SB
):
600 t
= GET_L_REG (rn
) + abs
;
602 return GET_MEMORY_B (t
);
604 case X (OP_DISP
, SW
):
605 t
= GET_L_REG (rn
) + abs
;
607 return GET_MEMORY_W (t
);
609 case X (OP_DISP
, SL
):
610 t
= GET_L_REG (rn
) + abs
;
612 return GET_MEMORY_L (t
);
615 t
= GET_MEMORY_L (abs
);
620 t
= GET_MEMORY_W (abs
);
625 abort (); /* ?? May be something more usefull? */
632 store (ea_type
*arg
, int n
)
635 int abs
= arg
->literal
;
651 t
= GET_L_REG (rn
) - 1;
658 t
= (GET_L_REG (rn
) - 2) & cpu
.mask
;
664 t
= (GET_L_REG (rn
) - 4) & cpu
.mask
;
669 case X (OP_DISP
, SB
):
670 t
= GET_L_REG (rn
) + abs
;
675 case X (OP_DISP
, SW
):
676 t
= GET_L_REG (rn
) + abs
;
681 case X (OP_DISP
, SL
):
682 t
= GET_L_REG (rn
) + abs
;
718 memory_size
= H8300S_MSIZE
;
720 memory_size
= H8300H_MSIZE
;
722 memory_size
= H8300_MSIZE
;
723 cpu
.memory
= (unsigned char *) calloc (sizeof (char), memory_size
);
724 cpu
.cache_idx
= (unsigned short *) calloc (sizeof (short), memory_size
);
725 cpu
.eightbit
= (unsigned char *) calloc (sizeof (char), 256);
727 /* `msize' must be a power of two. */
728 if ((memory_size
& (memory_size
- 1)) != 0)
730 cpu
.mask
= memory_size
- 1;
732 for (i
= 0; i
< 9; i
++)
737 for (i
= 0; i
< 8; i
++)
739 unsigned char *p
= (unsigned char *) (cpu
.regs
+ i
);
740 unsigned char *e
= (unsigned char *) (cpu
.regs
+ i
+ 1);
741 unsigned short *q
= (unsigned short *) (cpu
.regs
+ i
);
742 unsigned short *u
= (unsigned short *) (cpu
.regs
+ i
+ 1);
743 cpu
.regs
[i
] = 0x00112233;
769 lreg
[i
] = &cpu
.regs
[i
];
772 lreg
[8] = &cpu
.regs
[8];
774 /* Initialize the seg registers. */
776 sim_set_simcache_size (CSIZE
);
783 cpu
.state
= SIM_STATE_STOPPED
;
784 cpu
.exception
= SIGINT
;
794 #define I (intMaskBit != 0)
797 mop (decoded_inst
*code
, int bsize
, int sign
)
807 bsize
? SEXTCHAR (GET_W_REG (code
->dst
.reg
)) :
808 SEXTSHORT (GET_W_REG (code
->dst
.reg
));
810 bsize
? SEXTCHAR (GET_B_REG (code
->src
.reg
)) :
811 SEXTSHORT (GET_W_REG (code
->src
.reg
));
815 multiplicand
= bsize
? UEXTCHAR (GET_W_REG (code
->dst
.reg
)) :
816 UEXTSHORT (GET_W_REG (code
->dst
.reg
));
818 bsize
? UEXTCHAR (GET_B_REG (code
->src
.reg
)) :
819 UEXTSHORT (GET_W_REG (code
->src
.reg
));
822 result
= multiplier
* multiplicand
;
826 n
= result
& (bsize
? 0x8000 : 0x80000000);
827 nz
= result
& (bsize
? 0xffff : 0xffffffff);
831 SET_W_REG (code
->dst
.reg
, result
);
835 SET_L_REG (code
->dst
.reg
, result
);
838 return ((n
== 1) << 1) | (nz
== 1);
842 #define ONOT(name, how) \
847 rd = GET_B_REG (code->src.reg); \
855 rd = GET_W_REG (code->src.reg); \
862 int hm = 0x80000000; \
863 rd = GET_L_REG (code->src.reg); \
868 #define OSHIFTS(name, how1, how2) \
873 rd = GET_B_REG (code->src.reg); \
874 if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
888 rd = GET_W_REG (code->src.reg); \
889 if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
902 int hm = 0x80000000; \
903 rd = GET_L_REG (code->src.reg); \
904 if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
915 #define OBITOP(name,f, s, op) \
920 if (f) ea = fetch (&code->dst); \
921 m=1<< fetch (&code->src); \
923 if (s) store (&code->dst,ea); goto next; \
927 sim_stop (SIM_DESC sd
)
929 cpu
.state
= SIM_STATE_STOPPED
;
930 cpu
.exception
= SIGINT
;
943 #define SP_REGNUM R7_REGNUM /* Contains address of top of stack */
944 #define FP_REGNUM R6_REGNUM /* Contains address of executing
947 #define CCR_REGNUM 8 /* Contains processor status */
948 #define PC_REGNUM 9 /* Contains program counter */
950 #define CYCLE_REGNUM 10
952 #define EXR_REGNUM 11
953 #define INST_REGNUM 12
954 #define TICK_REGNUM 13
957 sim_resume (SIM_DESC sd
, int step
, int siggnal
)
962 int tick_start
= get_now ();
971 int c
, nz
, v
, n
, u
, h
, ui
, intMaskBit
;
976 prev
= signal (SIGINT
, control_c
);
980 cpu
.state
= SIM_STATE_STOPPED
;
981 cpu
.exception
= SIGTRAP
;
985 cpu
.state
= SIM_STATE_RUNNING
;
991 /* The PC should never be odd. */
1007 cidx
= cpu
.cache_idx
[pc
];
1008 code
= cpu
.cache
+ cidx
;
1011 #define ALUOP(STORE, NAME, HOW) \
1012 case O (NAME, SB): HOW; if (STORE) goto alu8; else goto just_flags_alu8; \
1013 case O (NAME, SW): HOW; if (STORE) goto alu16; else goto just_flags_alu16; \
1014 case O (NAME, SL): HOW; if (STORE) goto alu32; else goto just_flags_alu32;
1017 #define LOGOP(NAME, HOW) \
1018 case O (NAME, SB): HOW; goto log8; \
1019 case O (NAME, SW): HOW; goto log16; \
1020 case O (NAME, SL): HOW; goto log32;
1027 printf ("%x %d %s\n", pc
, code
->opcode
,
1028 code
->op
? code
->op
->name
: "**");
1030 cpu
.stats
[code
->opcode
]++;
1036 cycles
+= code
->cycles
;
1040 switch (code
->opcode
)
1044 * This opcode is a fake for when we get to an
1045 * instruction which hasnt been compiled
1052 case O (O_SUBX
, SB
):
1053 rd
= fetch (&code
->dst
);
1054 ea
= fetch (&code
->src
);
1059 case O (O_ADDX
, SB
):
1060 rd
= fetch (&code
->dst
);
1061 ea
= fetch (&code
->src
);
1066 #define EA ea = fetch (&code->src);
1067 #define RD_EA ea = fetch (&code->src); rd = fetch (&code->dst);
1069 ALUOP (1, O_SUB
, RD_EA
;
1072 ALUOP (1, O_NEG
, EA
;
1078 rd
= GET_B_REG (code
->dst
.reg
);
1079 ea
= fetch (&code
->src
);
1083 rd
= GET_W_REG (code
->dst
.reg
);
1084 ea
= fetch (&code
->src
);
1088 rd
= GET_L_REG (code
->dst
.reg
);
1089 ea
= fetch (&code
->src
);
1094 LOGOP (O_AND
, RD_EA
;
1100 LOGOP (O_XOR
, RD_EA
;
1104 case O (O_MOV_TO_MEM
, SB
):
1105 res
= GET_B_REG (code
->src
.reg
);
1107 case O (O_MOV_TO_MEM
, SW
):
1108 res
= GET_W_REG (code
->src
.reg
);
1110 case O (O_MOV_TO_MEM
, SL
):
1111 res
= GET_L_REG (code
->src
.reg
);
1115 case O (O_MOV_TO_REG
, SB
):
1116 res
= fetch (&code
->src
);
1117 SET_B_REG (code
->dst
.reg
, res
);
1118 goto just_flags_log8
;
1119 case O (O_MOV_TO_REG
, SW
):
1120 res
= fetch (&code
->src
);
1121 SET_W_REG (code
->dst
.reg
, res
);
1122 goto just_flags_log16
;
1123 case O (O_MOV_TO_REG
, SL
):
1124 res
= fetch (&code
->src
);
1125 SET_L_REG (code
->dst
.reg
, res
);
1126 goto just_flags_log32
;
1128 case O (O_EEPMOV
, SB
):
1129 case O (O_EEPMOV
, SW
):
1130 if (h8300hmode
|| h8300smode
)
1132 register unsigned char *_src
, *_dst
;
1133 unsigned int count
= ((code
->opcode
== O (O_EEPMOV
, SW
))
1134 ? cpu
.regs
[R4_REGNUM
] & 0xffff
1135 : cpu
.regs
[R4_REGNUM
] & 0xff);
1137 _src
= (cpu
.regs
[R5_REGNUM
] < memory_size
1138 ? cpu
.memory
+ cpu
.regs
[R5_REGNUM
]
1139 : cpu
.eightbit
+ (cpu
.regs
[R5_REGNUM
] & 0xff));
1140 if ((_src
+ count
) >= (cpu
.memory
+ memory_size
))
1142 if ((_src
+ count
) >= (cpu
.eightbit
+ 0x100))
1145 _dst
= (cpu
.regs
[R6_REGNUM
] < memory_size
1146 ? cpu
.memory
+ cpu
.regs
[R6_REGNUM
]
1147 : cpu
.eightbit
+ (cpu
.regs
[R6_REGNUM
] & 0xff));
1148 if ((_dst
+ count
) >= (cpu
.memory
+ memory_size
))
1150 if ((_dst
+ count
) >= (cpu
.eightbit
+ 0x100))
1153 memcpy (_dst
, _src
, count
);
1155 cpu
.regs
[R5_REGNUM
] += count
;
1156 cpu
.regs
[R6_REGNUM
] += count
;
1157 cpu
.regs
[R4_REGNUM
] &= ((code
->opcode
== O (O_EEPMOV
, SW
))
1158 ? (~0xffff) : (~0xff));
1159 cycles
+= 2 * count
;
1164 case O (O_ADDS
, SL
):
1165 SET_L_REG (code
->dst
.reg
,
1166 GET_L_REG (code
->dst
.reg
)
1167 + code
->src
.literal
);
1171 case O (O_SUBS
, SL
):
1172 SET_L_REG (code
->dst
.reg
,
1173 GET_L_REG (code
->dst
.reg
)
1174 - code
->src
.literal
);
1178 rd
= fetch (&code
->dst
);
1179 ea
= fetch (&code
->src
);
1182 goto just_flags_alu8
;
1185 rd
= fetch (&code
->dst
);
1186 ea
= fetch (&code
->src
);
1189 goto just_flags_alu16
;
1192 rd
= fetch (&code
->dst
);
1193 ea
= fetch (&code
->src
);
1196 goto just_flags_alu32
;
1200 rd
= GET_B_REG (code
->src
.reg
);
1203 SET_B_REG (code
->src
.reg
, res
);
1204 goto just_flags_inc8
;
1207 rd
= GET_W_REG (code
->dst
.reg
);
1208 ea
= -code
->src
.literal
;
1210 SET_W_REG (code
->dst
.reg
, res
);
1211 goto just_flags_inc16
;
1214 rd
= GET_L_REG (code
->dst
.reg
);
1215 ea
= -code
->src
.literal
;
1217 SET_L_REG (code
->dst
.reg
, res
);
1218 goto just_flags_inc32
;
1222 rd
= GET_B_REG (code
->src
.reg
);
1225 SET_B_REG (code
->src
.reg
, res
);
1226 goto just_flags_inc8
;
1229 rd
= GET_W_REG (code
->dst
.reg
);
1230 ea
= code
->src
.literal
;
1232 SET_W_REG (code
->dst
.reg
, res
);
1233 goto just_flags_inc16
;
1236 rd
= GET_L_REG (code
->dst
.reg
);
1237 ea
= code
->src
.literal
;
1239 SET_L_REG (code
->dst
.reg
, res
);
1240 goto just_flags_inc32
;
1242 #define GET_CCR(x) BUILDSR();x = cpu.ccr
1243 #define GET_EXR(x) BUILDEXR ();x = cpu.exr
1247 res
= fetch (&code
->src
);
1251 if (code
->src
.type
== OP_CCR
)
1255 else if (code
->src
.type
== OP_EXR
&& h8300smode
)
1261 store (&code
->dst
, res
);
1264 case O (O_ANDC
, SB
):
1265 if (code
->dst
.type
== OP_CCR
)
1269 else if (code
->dst
.type
== OP_EXR
&& h8300smode
)
1275 ea
= code
->src
.literal
;
1280 if (code
->dst
.type
== OP_CCR
)
1284 else if (code
->dst
.type
== OP_EXR
&& h8300smode
)
1290 ea
= code
->src
.literal
;
1294 case O (O_XORC
, SB
):
1295 if (code
->dst
.type
== OP_CCR
)
1299 else if (code
->dst
.type
== OP_EXR
&& h8300smode
)
1305 ea
= code
->src
.literal
;
1346 if (((Z
|| (N
^ V
)) == 0))
1352 if (((Z
|| (N
^ V
)) == 1))
1386 case O (O_SYSCALL
, SB
):
1388 char c
= cpu
.regs
[2];
1389 sim_callback
->write_stdout (sim_callback
, &c
, 1);
1393 ONOT (O_NOT
, rd
= ~rd
; v
= 0;);
1395 c
= rd
& hm
; v
= 0; rd
<<= 1,
1396 c
= rd
& (hm
>> 1); v
= 0; rd
<<= 2);
1398 c
= rd
& 1; v
= 0; rd
= (unsigned int) rd
>> 1,
1399 c
= rd
& 2; v
= 0; rd
= (unsigned int) rd
>> 2);
1401 c
= rd
& hm
; v
= (rd
& hm
) != ((rd
& (hm
>> 1)) << 1); rd
<<= 1,
1402 c
= rd
& (hm
>> 1); v
= (rd
& (hm
>> 1)) != ((rd
& (hm
>> 2)) << 2); rd
<<= 2);
1404 t
= rd
& hm
; c
= rd
& 1; v
= 0; rd
>>= 1; rd
|= t
,
1405 t
= rd
& hm
; c
= rd
& 2; v
= 0; rd
>>= 2; rd
|= t
| t
>> 1);
1407 c
= rd
& hm
; v
= 0; rd
<<= 1; rd
|= C
,
1408 c
= rd
& hm
; v
= 0; rd
<<= 1; rd
|= C
; c
= rd
& hm
; rd
<<= 1; rd
|= C
);
1410 c
= rd
& 1; v
= 0; rd
= (unsigned int) rd
>> 1; if (c
) rd
|= hm
,
1411 c
= rd
& 1; v
= 0; rd
= (unsigned int) rd
>> 1; if (c
) rd
|= hm
; c
= rd
& 1; rd
= (unsigned int) rd
>> 1; if (c
) rd
|= hm
);
1413 t
= rd
& hm
; rd
<<= 1; rd
|= C
; c
= t
; v
= 0,
1414 t
= rd
& hm
; rd
<<= 1; rd
|= C
; c
= t
; v
= 0; t
= rd
& hm
; rd
<<= 1; rd
|= C
; c
= t
);
1416 t
= rd
& 1; rd
= (unsigned int) rd
>> 1; if (C
) rd
|= hm
; c
= t
; v
= 0,
1417 t
= rd
& 1; rd
= (unsigned int) rd
>> 1; if (C
) rd
|= hm
; c
= t
; v
= 0; t
= rd
& 1; rd
= (unsigned int) rd
>> 1; if (C
) rd
|= hm
; c
= t
);
1421 pc
= fetch (&code
->src
);
1429 pc
= fetch (&code
->src
);
1436 SET_MEMORY_L (tmp
, code
->next_pc
);
1441 SET_MEMORY_W (tmp
, code
->next_pc
);
1448 pc
= code
->src
.literal
;
1459 pc
= GET_MEMORY_L (tmp
);
1464 pc
= GET_MEMORY_W (tmp
);
1473 cpu
.state
= SIM_STATE_STOPPED
;
1474 cpu
.exception
= SIGILL
;
1476 case O (O_SLEEP
, SN
):
1477 /* FIXME: Doesn't this break for breakpoints when r0
1478 contains just the right (er, wrong) value? */
1479 cpu
.state
= SIM_STATE_STOPPED
;
1480 /* The format of r0 is defined by target newlib. Expand
1481 the macros here instead of looking for .../sys/wait.h. */
1482 #define SIM_WIFEXITED(v) (((v) & 0xff) == 0)
1483 #define SIM_WIFSIGNALED(v) (((v) & 0x7f) > 0 && (((v) & 0x7f) < 0x7f))
1484 if (! SIM_WIFEXITED (cpu
.regs
[0]) && SIM_WIFSIGNALED (cpu
.regs
[0]))
1485 cpu
.exception
= SIGILL
;
1487 cpu
.exception
= SIGTRAP
;
1490 cpu
.state
= SIM_STATE_STOPPED
;
1491 cpu
.exception
= SIGTRAP
;
1494 OBITOP (O_BNOT
, 1, 1, ea
^= m
);
1495 OBITOP (O_BTST
, 1, 0, nz
= ea
& m
);
1496 OBITOP (O_BCLR
, 1, 1, ea
&= ~m
);
1497 OBITOP (O_BSET
, 1, 1, ea
|= m
);
1498 OBITOP (O_BLD
, 1, 0, c
= ea
& m
);
1499 OBITOP (O_BILD
, 1, 0, c
= !(ea
& m
));
1500 OBITOP (O_BST
, 1, 1, ea
&= ~m
;
1502 OBITOP (O_BIST
, 1, 1, ea
&= ~m
;
1504 OBITOP (O_BAND
, 1, 0, c
= (ea
& m
) && C
);
1505 OBITOP (O_BIAND
, 1, 0, c
= !(ea
& m
) && C
);
1506 OBITOP (O_BOR
, 1, 0, c
= (ea
& m
) || C
);
1507 OBITOP (O_BIOR
, 1, 0, c
= !(ea
& m
) || C
);
1508 OBITOP (O_BXOR
, 1, 0, c
= ((ea
& m
) != 0) != C
);
1509 OBITOP (O_BIXOR
, 1, 0, c
= !(ea
& m
) != C
);
1511 #define MOP(bsize, signed) \
1512 mop (code, bsize, signed); \
1515 case O (O_MULS
, SB
):
1518 case O (O_MULS
, SW
):
1521 case O (O_MULU
, SB
):
1524 case O (O_MULU
, SW
):
1529 if (!h8300smode
|| code
->src
.type
!= X (OP_REG
, SL
))
1531 switch (code
->src
.reg
)
1541 res
= fetch (&code
->src
);
1542 store (&code
->src
, res
| 0x80);
1543 goto just_flags_log8
;
1545 case O (O_DIVU
, SB
):
1547 rd
= GET_W_REG (code
->dst
.reg
);
1548 ea
= GET_B_REG (code
->src
.reg
);
1551 tmp
= (unsigned) rd
% ea
;
1552 rd
= (unsigned) rd
/ ea
;
1554 SET_W_REG (code
->dst
.reg
, (rd
& 0xff) | (tmp
<< 8));
1560 case O (O_DIVU
, SW
):
1562 rd
= GET_L_REG (code
->dst
.reg
);
1563 ea
= GET_W_REG (code
->src
.reg
);
1568 tmp
= (unsigned) rd
% ea
;
1569 rd
= (unsigned) rd
/ ea
;
1571 SET_L_REG (code
->dst
.reg
, (rd
& 0xffff) | (tmp
<< 16));
1575 case O (O_DIVS
, SB
):
1578 rd
= SEXTSHORT (GET_W_REG (code
->dst
.reg
));
1579 ea
= SEXTCHAR (GET_B_REG (code
->src
.reg
));
1582 tmp
= (int) rd
% (int) ea
;
1583 rd
= (int) rd
/ (int) ea
;
1589 SET_W_REG (code
->dst
.reg
, (rd
& 0xff) | (tmp
<< 8));
1592 case O (O_DIVS
, SW
):
1594 rd
= GET_L_REG (code
->dst
.reg
);
1595 ea
= SEXTSHORT (GET_W_REG (code
->src
.reg
));
1598 tmp
= (int) rd
% (int) ea
;
1599 rd
= (int) rd
/ (int) ea
;
1600 n
= rd
& 0x80000000;
1605 SET_L_REG (code
->dst
.reg
, (rd
& 0xffff) | (tmp
<< 16));
1608 case O (O_EXTS
, SW
):
1609 rd
= GET_B_REG (code
->src
.reg
+ 8) & 0xff; /* Yes, src, not dst. */
1610 ea
= rd
& 0x80 ? -256 : 0;
1613 case O (O_EXTS
, SL
):
1614 rd
= GET_W_REG (code
->src
.reg
) & 0xffff;
1615 ea
= rd
& 0x8000 ? -65536 : 0;
1618 case O (O_EXTU
, SW
):
1619 rd
= GET_B_REG (code
->src
.reg
+ 8) & 0xff;
1623 case O (O_EXTU
, SL
):
1624 rd
= GET_W_REG (code
->src
.reg
) & 0xffff;
1634 int nregs
, firstreg
, i
;
1636 nregs
= GET_MEMORY_B (pc
+ 1);
1639 firstreg
= GET_MEMORY_B (pc
+ 3);
1641 for (i
= firstreg
; i
<= firstreg
+ nregs
; i
++)
1644 SET_MEMORY_L (cpu
.regs
[7], cpu
.regs
[i
]);
1651 int nregs
, firstreg
, i
;
1653 nregs
= GET_MEMORY_B (pc
+ 1);
1656 firstreg
= GET_MEMORY_B (pc
+ 3);
1658 for (i
= firstreg
; i
>= firstreg
- nregs
; i
--)
1660 cpu
.regs
[i
] = GET_MEMORY_L (cpu
.regs
[7]);
1668 cpu
.state
= SIM_STATE_STOPPED
;
1669 cpu
.exception
= SIGILL
;
1676 if (code
->dst
.type
== OP_CCR
)
1681 else if (code
->dst
.type
== OP_EXR
&& h8300smode
)
1692 /* When a branch works */
1693 pc
= code
->src
.literal
;
1696 /* Set the cond codes from res */
1699 /* Set the flags after an 8 bit inc/dec operation */
1703 v
= (rd
& 0x7f) == 0x7f;
1707 /* Set the flags after an 16 bit inc/dec operation */
1711 v
= (rd
& 0x7fff) == 0x7fff;
1715 /* Set the flags after an 32 bit inc/dec operation */
1717 n
= res
& 0x80000000;
1718 nz
= res
& 0xffffffff;
1719 v
= (rd
& 0x7fffffff) == 0x7fffffff;
1724 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
1727 SET_B_REG (code
->src
.reg
, rd
);
1731 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
1734 SET_W_REG (code
->src
.reg
, rd
);
1738 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
1739 n
= (rd
& 0x80000000);
1740 nz
= rd
& 0xffffffff;
1741 SET_L_REG (code
->src
.reg
, rd
);
1745 store (&code
->dst
, res
);
1747 /* flags after a 32bit logical operation */
1748 n
= res
& 0x80000000;
1749 nz
= res
& 0xffffffff;
1754 store (&code
->dst
, res
);
1756 /* flags after a 16bit logical operation */
1764 store (&code
->dst
, res
);
1772 SET_B_REG (code
->dst
.reg
, res
);
1777 switch (code
->opcode
/ 4)
1780 v
= ((rd
& 0x80) == (ea
& 0x80)
1781 && (rd
& 0x80) != (res
& 0x80));
1785 v
= ((rd
& 0x80) != (-ea
& 0x80)
1786 && (rd
& 0x80) != (res
& 0x80));
1795 SET_W_REG (code
->dst
.reg
, res
);
1799 c
= (res
& 0x10000);
1800 switch (code
->opcode
/ 4)
1803 v
= ((rd
& 0x8000) == (ea
& 0x8000)
1804 && (rd
& 0x8000) != (res
& 0x8000));
1808 v
= ((rd
& 0x8000) != (-ea
& 0x8000)
1809 && (rd
& 0x8000) != (res
& 0x8000));
1818 SET_L_REG (code
->dst
.reg
, res
);
1820 n
= res
& 0x80000000;
1821 nz
= res
& 0xffffffff;
1822 switch (code
->opcode
/ 4)
1825 v
= ((rd
& 0x80000000) == (ea
& 0x80000000)
1826 && (rd
& 0x80000000) != (res
& 0x80000000));
1827 c
= ((unsigned) res
< (unsigned) rd
) || ((unsigned) res
< (unsigned) ea
);
1831 v
= ((rd
& 0x80000000) != (-ea
& 0x80000000)
1832 && (rd
& 0x80000000) != (res
& 0x80000000));
1833 c
= (unsigned) rd
< (unsigned) -ea
;
1836 v
= (rd
== 0x80000000);
1852 if (--poll_count
< 0)
1854 poll_count
= POLL_QUIT_INTERVAL
;
1855 if ((*sim_callback
->poll_quit
) != NULL
1856 && (*sim_callback
->poll_quit
) (sim_callback
))
1861 while (cpu
.state
== SIM_STATE_RUNNING
);
1862 cpu
.ticks
+= get_now () - tick_start
;
1863 cpu
.cycles
+= cycles
;
1870 signal (SIGINT
, prev
);
1874 sim_trace (SIM_DESC sd
)
1876 /* FIXME: Unfinished. */
1881 sim_write (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
1888 for (i
= 0; i
< size
; i
++)
1890 if (addr
< memory_size
)
1892 cpu
.memory
[addr
+ i
] = buffer
[i
];
1893 cpu
.cache_idx
[addr
+ i
] = 0;
1896 cpu
.eightbit
[(addr
+ i
) & 0xff] = buffer
[i
];
1902 sim_read (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
1907 if (addr
< memory_size
)
1908 memcpy (buffer
, cpu
.memory
+ addr
, size
);
1910 memcpy (buffer
, cpu
.eightbit
+ (addr
& 0xff), size
);
1916 sim_store_register (SIM_DESC sd
, int rn
, unsigned char *value
, int length
)
1921 longval
= (value
[0] << 24) | (value
[1] << 16) | (value
[2] << 8) | value
[3];
1922 shortval
= (value
[0] << 8) | (value
[1]);
1923 intval
= h8300hmode
? longval
: shortval
;
1941 cpu
.regs
[rn
] = intval
;
1950 cpu
.cycles
= longval
;
1954 cpu
.insts
= longval
;
1958 cpu
.ticks
= longval
;
1965 sim_fetch_register (SIM_DESC sd
, int rn
, unsigned char *buf
, int length
)
1972 if (!h8300smode
&& rn
>= EXR_REGNUM
)
2010 if (h8300hmode
|| longreg
)
2026 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason
, int *sigrc
)
2028 #if 0 /* FIXME: This should work but we can't use it.
2029 grep for SLEEP above. */
2032 case SIM_STATE_EXITED
: *reason
= sim_exited
; break;
2033 case SIM_STATE_SIGNALLED
: *reason
= sim_signalled
; break;
2034 case SIM_STATE_STOPPED
: *reason
= sim_stopped
; break;
2038 *reason
= sim_stopped
;
2040 *sigrc
= cpu
.exception
;
2043 /* FIXME: Rename to sim_set_mem_size. */
2048 /* Memory size is fixed. */
2052 sim_set_simcache_size (int n
)
2058 cpu
.cache
= (decoded_inst
*) malloc (sizeof (decoded_inst
) * n
);
2059 memset (cpu
.cache
, 0, sizeof (decoded_inst
) * n
);
2065 sim_info (SIM_DESC sd
, int verbose
)
2067 double timetaken
= (double) cpu
.ticks
/ (double) now_persec ();
2068 double virttime
= cpu
.cycles
/ 10.0e6
;
2070 (*sim_callback
->printf_filtered
) (sim_callback
,
2071 "\n\n#instructions executed %10d\n",
2073 (*sim_callback
->printf_filtered
) (sim_callback
,
2074 "#cycles (v approximate) %10d\n",
2076 (*sim_callback
->printf_filtered
) (sim_callback
,
2077 "#real time taken %10.4f\n",
2079 (*sim_callback
->printf_filtered
) (sim_callback
,
2080 "#virtual time taked %10.4f\n",
2082 if (timetaken
!= 0.0)
2083 (*sim_callback
->printf_filtered
) (sim_callback
,
2084 "#simulation ratio %10.4f\n",
2085 virttime
/ timetaken
);
2086 (*sim_callback
->printf_filtered
) (sim_callback
,
2089 (*sim_callback
->printf_filtered
) (sim_callback
,
2090 "#cache size %10d\n",
2094 /* This to be conditional on `what' (aka `verbose'),
2095 however it was never passed as non-zero. */
2099 for (i
= 0; i
< O_LAST
; i
++)
2102 (*sim_callback
->printf_filtered
) (sim_callback
,
2103 "%d: %d\n", i
, cpu
.stats
[i
]);
2109 /* Indicate whether the cpu is an H8/300 or H8/300H.
2110 FLAG is non-zero for the H8/300H. */
2113 set_h8300h (int h_flag
, int s_flag
)
2115 /* FIXME: Much of the code in sim_load can be moved to sim_open.
2116 This function being replaced by a sim_open:ARGV configuration
2118 h8300hmode
= h_flag
;
2119 h8300smode
= s_flag
;
2123 sim_open (SIM_OPEN_KIND kind
,
2124 struct host_callback_struct
*ptr
,
2128 /* FIXME: Much of the code in sim_load can be moved here. */
2133 /* Fudge our descriptor. */
2134 return (SIM_DESC
) 1;
2138 sim_close (SIM_DESC sd
, int quitting
)
2140 /* Nothing to do. */
2143 /* Called by gdb to load a program into memory. */
2146 sim_load (SIM_DESC sd
, char *prog
, bfd
*abfd
, int from_tty
)
2150 /* FIXME: The code below that sets a specific variant of the H8/300
2151 being simulated should be moved to sim_open(). */
2153 /* See if the file is for the H8/300 or H8/300H. */
2154 /* ??? This may not be the most efficient way. The z8k simulator
2155 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
2159 prog_bfd
= bfd_openr (prog
, "coff-h8300");
2160 if (prog_bfd
!= NULL
)
2162 /* Set the cpu type. We ignore failure from bfd_check_format
2163 and bfd_openr as sim_load_file checks too. */
2164 if (bfd_check_format (prog_bfd
, bfd_object
))
2166 unsigned long mach
= bfd_get_mach (prog_bfd
);
2167 set_h8300h (mach
== bfd_mach_h8300h
|| mach
== bfd_mach_h8300s
,
2168 mach
== bfd_mach_h8300s
);
2172 /* If we're using gdb attached to the simulator, then we have to
2173 reallocate memory for the simulator.
2175 When gdb first starts, it calls fetch_registers (among other
2176 functions), which in turn calls init_pointers, which allocates
2179 The problem is when we do that, we don't know whether we're
2180 debugging an H8/300 or H8/300H program.
2182 This is the first point at which we can make that determination,
2183 so we just reallocate memory now; this will also allow us to handle
2184 switching between H8/300 and H8/300H programs without exiting
2188 memory_size
= H8300S_MSIZE
;
2189 else if (h8300hmode
)
2190 memory_size
= H8300H_MSIZE
;
2192 memory_size
= H8300_MSIZE
;
2197 free (cpu
.cache_idx
);
2199 free (cpu
.eightbit
);
2201 cpu
.memory
= (unsigned char *) calloc (sizeof (char), memory_size
);
2202 cpu
.cache_idx
= (unsigned short *) calloc (sizeof (short), memory_size
);
2203 cpu
.eightbit
= (unsigned char *) calloc (sizeof (char), 256);
2205 /* `msize' must be a power of two. */
2206 if ((memory_size
& (memory_size
- 1)) != 0)
2208 cpu
.mask
= memory_size
- 1;
2210 if (sim_load_file (sd
, myname
, sim_callback
, prog
, prog_bfd
,
2211 sim_kind
== SIM_OPEN_DEBUG
,
2215 /* Close the bfd if we opened it. */
2216 if (abfd
== NULL
&& prog_bfd
!= NULL
)
2217 bfd_close (prog_bfd
);
2221 /* Close the bfd if we opened it. */
2222 if (abfd
== NULL
&& prog_bfd
!= NULL
)
2223 bfd_close (prog_bfd
);
2228 sim_create_inferior (SIM_DESC sd
, struct _bfd
*abfd
, char **argv
, char **env
)
2231 cpu
.pc
= bfd_get_start_address (abfd
);
2238 sim_do_command (SIM_DESC sd
, char *cmd
)
2240 (*sim_callback
->printf_filtered
) (sim_callback
,
2241 "This simulator does not accept any commands.\n");
2245 sim_set_callbacks (struct host_callback_struct
*ptr
)
This page took 0.072189 seconds and 5 git commands to generate.