b225b6273363cc96a4e6c3084f7e960c6815f999
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; \
930 cpu
.state
= SIM_STATE_STOPPED
;
931 cpu
.exception
= SIGINT
;
944 #define SP_REGNUM R7_REGNUM /* Contains address of top of stack */
945 #define FP_REGNUM R6_REGNUM /* Contains address of executing
948 #define CCR_REGNUM 8 /* Contains processor status */
949 #define PC_REGNUM 9 /* Contains program counter */
951 #define CYCLE_REGNUM 10
953 #define EXR_REGNUM 11
954 #define INST_REGNUM 12
955 #define TICK_REGNUM 13
958 sim_resume (SIM_DESC sd
, int step
, int siggnal
)
963 int tick_start
= get_now ();
972 int c
, nz
, v
, n
, u
, h
, ui
, intMaskBit
;
977 prev
= signal (SIGINT
, control_c
);
981 cpu
.state
= SIM_STATE_STOPPED
;
982 cpu
.exception
= SIGTRAP
;
986 cpu
.state
= SIM_STATE_RUNNING
;
992 /* The PC should never be odd. */
1008 cidx
= cpu
.cache_idx
[pc
];
1009 code
= cpu
.cache
+ cidx
;
1012 #define ALUOP(STORE, NAME, HOW) \
1013 case O (NAME, SB): HOW; if (STORE) goto alu8; else goto just_flags_alu8; \
1014 case O (NAME, SW): HOW; if (STORE) goto alu16; else goto just_flags_alu16; \
1015 case O (NAME, SL): HOW; if (STORE) goto alu32; else goto just_flags_alu32;
1018 #define LOGOP(NAME, HOW) \
1019 case O (NAME, SB): HOW; goto log8; \
1020 case O (NAME, SW): HOW; goto log16; \
1021 case O (NAME, SL): HOW; goto log32;
1028 printf ("%x %d %s\n", pc
, code
->opcode
,
1029 code
->op
? code
->op
->name
: "**");
1031 cpu
.stats
[code
->opcode
]++;
1037 cycles
+= code
->cycles
;
1041 switch (code
->opcode
)
1045 * This opcode is a fake for when we get to an
1046 * instruction which hasnt been compiled
1053 case O (O_SUBX
, SB
):
1054 rd
= fetch (&code
->dst
);
1055 ea
= fetch (&code
->src
);
1060 case O (O_ADDX
, SB
):
1061 rd
= fetch (&code
->dst
);
1062 ea
= fetch (&code
->src
);
1067 #define EA ea = fetch (&code->src);
1068 #define RD_EA ea = fetch (&code->src); rd = fetch (&code->dst);
1070 ALUOP (1, O_SUB
, RD_EA
;
1073 ALUOP (1, O_NEG
, EA
;
1079 rd
= GET_B_REG (code
->dst
.reg
);
1080 ea
= fetch (&code
->src
);
1084 rd
= GET_W_REG (code
->dst
.reg
);
1085 ea
= fetch (&code
->src
);
1089 rd
= GET_L_REG (code
->dst
.reg
);
1090 ea
= fetch (&code
->src
);
1095 LOGOP (O_AND
, RD_EA
;
1101 LOGOP (O_XOR
, RD_EA
;
1105 case O (O_MOV_TO_MEM
, SB
):
1106 res
= GET_B_REG (code
->src
.reg
);
1108 case O (O_MOV_TO_MEM
, SW
):
1109 res
= GET_W_REG (code
->src
.reg
);
1111 case O (O_MOV_TO_MEM
, SL
):
1112 res
= GET_L_REG (code
->src
.reg
);
1116 case O (O_MOV_TO_REG
, SB
):
1117 res
= fetch (&code
->src
);
1118 SET_B_REG (code
->dst
.reg
, res
);
1119 goto just_flags_log8
;
1120 case O (O_MOV_TO_REG
, SW
):
1121 res
= fetch (&code
->src
);
1122 SET_W_REG (code
->dst
.reg
, res
);
1123 goto just_flags_log16
;
1124 case O (O_MOV_TO_REG
, SL
):
1125 res
= fetch (&code
->src
);
1126 SET_L_REG (code
->dst
.reg
, res
);
1127 goto just_flags_log32
;
1129 case O (O_EEPMOV
, SB
):
1130 case O (O_EEPMOV
, SW
):
1131 if (h8300hmode
|| h8300smode
)
1133 register unsigned char *_src
, *_dst
;
1134 unsigned int count
= ((code
->opcode
== O (O_EEPMOV
, SW
))
1135 ? cpu
.regs
[R4_REGNUM
] & 0xffff
1136 : cpu
.regs
[R4_REGNUM
] & 0xff);
1138 _src
= (cpu
.regs
[R5_REGNUM
] < memory_size
1139 ? cpu
.memory
+ cpu
.regs
[R5_REGNUM
]
1140 : cpu
.eightbit
+ (cpu
.regs
[R5_REGNUM
] & 0xff));
1141 if ((_src
+ count
) >= (cpu
.memory
+ memory_size
))
1143 if ((_src
+ count
) >= (cpu
.eightbit
+ 0x100))
1146 _dst
= (cpu
.regs
[R6_REGNUM
] < memory_size
1147 ? cpu
.memory
+ cpu
.regs
[R6_REGNUM
]
1148 : cpu
.eightbit
+ (cpu
.regs
[R6_REGNUM
] & 0xff));
1149 if ((_dst
+ count
) >= (cpu
.memory
+ memory_size
))
1151 if ((_dst
+ count
) >= (cpu
.eightbit
+ 0x100))
1154 memcpy (_dst
, _src
, count
);
1156 cpu
.regs
[R5_REGNUM
] += count
;
1157 cpu
.regs
[R6_REGNUM
] += count
;
1158 cpu
.regs
[R4_REGNUM
] &= ((code
->opcode
== O (O_EEPMOV
, SW
))
1159 ? (~0xffff) : (~0xff));
1160 cycles
+= 2 * count
;
1165 case O (O_ADDS
, SL
):
1166 SET_L_REG (code
->dst
.reg
,
1167 GET_L_REG (code
->dst
.reg
)
1168 + code
->src
.literal
);
1172 case O (O_SUBS
, SL
):
1173 SET_L_REG (code
->dst
.reg
,
1174 GET_L_REG (code
->dst
.reg
)
1175 - code
->src
.literal
);
1179 rd
= fetch (&code
->dst
);
1180 ea
= fetch (&code
->src
);
1183 goto just_flags_alu8
;
1186 rd
= fetch (&code
->dst
);
1187 ea
= fetch (&code
->src
);
1190 goto just_flags_alu16
;
1193 rd
= fetch (&code
->dst
);
1194 ea
= fetch (&code
->src
);
1197 goto just_flags_alu32
;
1201 rd
= GET_B_REG (code
->src
.reg
);
1204 SET_B_REG (code
->src
.reg
, res
);
1205 goto just_flags_inc8
;
1208 rd
= GET_W_REG (code
->dst
.reg
);
1209 ea
= -code
->src
.literal
;
1211 SET_W_REG (code
->dst
.reg
, res
);
1212 goto just_flags_inc16
;
1215 rd
= GET_L_REG (code
->dst
.reg
);
1216 ea
= -code
->src
.literal
;
1218 SET_L_REG (code
->dst
.reg
, res
);
1219 goto just_flags_inc32
;
1223 rd
= GET_B_REG (code
->src
.reg
);
1226 SET_B_REG (code
->src
.reg
, res
);
1227 goto just_flags_inc8
;
1230 rd
= GET_W_REG (code
->dst
.reg
);
1231 ea
= code
->src
.literal
;
1233 SET_W_REG (code
->dst
.reg
, res
);
1234 goto just_flags_inc16
;
1237 rd
= GET_L_REG (code
->dst
.reg
);
1238 ea
= code
->src
.literal
;
1240 SET_L_REG (code
->dst
.reg
, res
);
1241 goto just_flags_inc32
;
1243 #define GET_CCR(x) BUILDSR();x = cpu.ccr
1244 #define GET_EXR(x) BUILDEXR ();x = cpu.exr
1248 res
= fetch (&code
->src
);
1252 if (code
->src
.type
== OP_CCR
)
1256 else if (code
->src
.type
== OP_EXR
&& h8300smode
)
1262 store (&code
->dst
, res
);
1265 case O (O_ANDC
, SB
):
1266 if (code
->dst
.type
== OP_CCR
)
1270 else if (code
->dst
.type
== OP_EXR
&& h8300smode
)
1276 ea
= code
->src
.literal
;
1281 if (code
->dst
.type
== OP_CCR
)
1285 else if (code
->dst
.type
== OP_EXR
&& h8300smode
)
1291 ea
= code
->src
.literal
;
1295 case O (O_XORC
, SB
):
1296 if (code
->dst
.type
== OP_CCR
)
1300 else if (code
->dst
.type
== OP_EXR
&& h8300smode
)
1306 ea
= code
->src
.literal
;
1347 if (((Z
|| (N
^ V
)) == 0))
1353 if (((Z
|| (N
^ V
)) == 1))
1387 case O (O_SYSCALL
, SB
):
1389 char c
= cpu
.regs
[2];
1390 sim_callback
->write_stdout (sim_callback
, &c
, 1);
1394 ONOT (O_NOT
, rd
= ~rd
; v
= 0;);
1396 c
= rd
& hm
; v
= 0; rd
<<= 1,
1397 c
= rd
& (hm
>> 1); v
= 0; rd
<<= 2);
1399 c
= rd
& 1; v
= 0; rd
= (unsigned int) rd
>> 1,
1400 c
= rd
& 2; v
= 0; rd
= (unsigned int) rd
>> 2);
1402 c
= rd
& hm
; v
= (rd
& hm
) != ((rd
& (hm
>> 1)) << 1); rd
<<= 1,
1403 c
= rd
& (hm
>> 1); v
= (rd
& (hm
>> 1)) != ((rd
& (hm
>> 2)) << 2); rd
<<= 2);
1405 t
= rd
& hm
; c
= rd
& 1; v
= 0; rd
>>= 1; rd
|= t
,
1406 t
= rd
& hm
; c
= rd
& 2; v
= 0; rd
>>= 2; rd
|= t
| t
>> 1);
1408 c
= rd
& hm
; v
= 0; rd
<<= 1; rd
|= C
,
1409 c
= rd
& hm
; v
= 0; rd
<<= 1; rd
|= C
; c
= rd
& hm
; rd
<<= 1; rd
|= C
);
1411 c
= rd
& 1; v
= 0; rd
= (unsigned int) rd
>> 1; if (c
) rd
|= hm
,
1412 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
);
1414 t
= rd
& hm
; rd
<<= 1; rd
|= C
; c
= t
; v
= 0,
1415 t
= rd
& hm
; rd
<<= 1; rd
|= C
; c
= t
; v
= 0; t
= rd
& hm
; rd
<<= 1; rd
|= C
; c
= t
);
1417 t
= rd
& 1; rd
= (unsigned int) rd
>> 1; if (C
) rd
|= hm
; c
= t
; v
= 0,
1418 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
);
1422 pc
= fetch (&code
->src
);
1430 pc
= fetch (&code
->src
);
1437 SET_MEMORY_L (tmp
, code
->next_pc
);
1442 SET_MEMORY_W (tmp
, code
->next_pc
);
1449 pc
= code
->src
.literal
;
1460 pc
= GET_MEMORY_L (tmp
);
1465 pc
= GET_MEMORY_W (tmp
);
1474 cpu
.state
= SIM_STATE_STOPPED
;
1475 cpu
.exception
= SIGILL
;
1477 case O (O_SLEEP
, SN
):
1478 /* FIXME: Doesn't this break for breakpoints when r0
1479 contains just the right (er, wrong) value? */
1480 cpu
.state
= SIM_STATE_STOPPED
;
1481 /* The format of r0 is defined by target newlib. Expand
1482 the macros here instead of looking for .../sys/wait.h. */
1483 #define SIM_WIFEXITED(v) (((v) & 0xff) == 0)
1484 #define SIM_WIFSIGNALED(v) (((v) & 0x7f) > 0 && (((v) & 0x7f) < 0x7f))
1485 if (! SIM_WIFEXITED (cpu
.regs
[0]) && SIM_WIFSIGNALED (cpu
.regs
[0]))
1486 cpu
.exception
= SIGILL
;
1488 cpu
.exception
= SIGTRAP
;
1491 cpu
.state
= SIM_STATE_STOPPED
;
1492 cpu
.exception
= SIGTRAP
;
1495 OBITOP (O_BNOT
, 1, 1, ea
^= m
);
1496 OBITOP (O_BTST
, 1, 0, nz
= ea
& m
);
1497 OBITOP (O_BCLR
, 1, 1, ea
&= ~m
);
1498 OBITOP (O_BSET
, 1, 1, ea
|= m
);
1499 OBITOP (O_BLD
, 1, 0, c
= ea
& m
);
1500 OBITOP (O_BILD
, 1, 0, c
= !(ea
& m
));
1501 OBITOP (O_BST
, 1, 1, ea
&= ~m
;
1503 OBITOP (O_BIST
, 1, 1, ea
&= ~m
;
1505 OBITOP (O_BAND
, 1, 0, c
= (ea
& m
) && C
);
1506 OBITOP (O_BIAND
, 1, 0, c
= !(ea
& m
) && C
);
1507 OBITOP (O_BOR
, 1, 0, c
= (ea
& m
) || C
);
1508 OBITOP (O_BIOR
, 1, 0, c
= !(ea
& m
) || C
);
1509 OBITOP (O_BXOR
, 1, 0, c
= (ea
& m
) != C
);
1510 OBITOP (O_BIXOR
, 1, 0, c
= !(ea
& m
) != C
);
1512 #define MOP(bsize, signed) \
1513 mop (code, bsize, signed); \
1516 case O (O_MULS
, SB
):
1519 case O (O_MULS
, SW
):
1522 case O (O_MULU
, SB
):
1525 case O (O_MULU
, SW
):
1530 if (!h8300smode
|| code
->src
.type
!= X (OP_REG
, SL
))
1532 switch (code
->src
.reg
)
1542 res
= fetch (&code
->src
);
1543 store (&code
->src
, res
| 0x80);
1544 goto just_flags_log8
;
1546 case O (O_DIVU
, SB
):
1548 rd
= GET_W_REG (code
->dst
.reg
);
1549 ea
= GET_B_REG (code
->src
.reg
);
1552 tmp
= (unsigned) rd
% ea
;
1553 rd
= (unsigned) rd
/ ea
;
1555 SET_W_REG (code
->dst
.reg
, (rd
& 0xff) | (tmp
<< 8));
1561 case O (O_DIVU
, SW
):
1563 rd
= GET_L_REG (code
->dst
.reg
);
1564 ea
= GET_W_REG (code
->src
.reg
);
1569 tmp
= (unsigned) rd
% ea
;
1570 rd
= (unsigned) rd
/ ea
;
1572 SET_L_REG (code
->dst
.reg
, (rd
& 0xffff) | (tmp
<< 16));
1576 case O (O_DIVS
, SB
):
1579 rd
= SEXTSHORT (GET_W_REG (code
->dst
.reg
));
1580 ea
= SEXTCHAR (GET_B_REG (code
->src
.reg
));
1583 tmp
= (int) rd
% (int) ea
;
1584 rd
= (int) rd
/ (int) ea
;
1590 SET_W_REG (code
->dst
.reg
, (rd
& 0xff) | (tmp
<< 8));
1593 case O (O_DIVS
, SW
):
1595 rd
= GET_L_REG (code
->dst
.reg
);
1596 ea
= SEXTSHORT (GET_W_REG (code
->src
.reg
));
1599 tmp
= (int) rd
% (int) ea
;
1600 rd
= (int) rd
/ (int) ea
;
1601 n
= rd
& 0x80000000;
1606 SET_L_REG (code
->dst
.reg
, (rd
& 0xffff) | (tmp
<< 16));
1609 case O (O_EXTS
, SW
):
1610 rd
= GET_B_REG (code
->src
.reg
+ 8) & 0xff; /* Yes, src, not dst. */
1611 ea
= rd
& 0x80 ? -256 : 0;
1614 case O (O_EXTS
, SL
):
1615 rd
= GET_W_REG (code
->src
.reg
) & 0xffff;
1616 ea
= rd
& 0x8000 ? -65536 : 0;
1619 case O (O_EXTU
, SW
):
1620 rd
= GET_B_REG (code
->src
.reg
+ 8) & 0xff;
1624 case O (O_EXTU
, SL
):
1625 rd
= GET_W_REG (code
->src
.reg
) & 0xffff;
1635 int nregs
, firstreg
, i
;
1637 nregs
= GET_MEMORY_B (pc
+ 1);
1640 firstreg
= GET_MEMORY_B (pc
+ 3);
1642 for (i
= firstreg
; i
<= firstreg
+ nregs
; i
++)
1645 SET_MEMORY_L (cpu
.regs
[7], cpu
.regs
[i
]);
1652 int nregs
, firstreg
, i
;
1654 nregs
= GET_MEMORY_B (pc
+ 1);
1657 firstreg
= GET_MEMORY_B (pc
+ 3);
1659 for (i
= firstreg
; i
>= firstreg
- nregs
; i
--)
1661 cpu
.regs
[i
] = GET_MEMORY_L (cpu
.regs
[7]);
1669 cpu
.state
= SIM_STATE_STOPPED
;
1670 cpu
.exception
= SIGILL
;
1677 if (code
->dst
.type
== OP_CCR
)
1682 else if (code
->dst
.type
== OP_EXR
&& h8300smode
)
1693 /* When a branch works */
1694 pc
= code
->src
.literal
;
1697 /* Set the cond codes from res */
1700 /* Set the flags after an 8 bit inc/dec operation */
1704 v
= (rd
& 0x7f) == 0x7f;
1708 /* Set the flags after an 16 bit inc/dec operation */
1712 v
= (rd
& 0x7fff) == 0x7fff;
1716 /* Set the flags after an 32 bit inc/dec operation */
1718 n
= res
& 0x80000000;
1719 nz
= res
& 0xffffffff;
1720 v
= (rd
& 0x7fffffff) == 0x7fffffff;
1725 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
1728 SET_B_REG (code
->src
.reg
, rd
);
1732 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
1735 SET_W_REG (code
->src
.reg
, rd
);
1739 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
1740 n
= (rd
& 0x80000000);
1741 nz
= rd
& 0xffffffff;
1742 SET_L_REG (code
->src
.reg
, rd
);
1746 store (&code
->dst
, res
);
1748 /* flags after a 32bit logical operation */
1749 n
= res
& 0x80000000;
1750 nz
= res
& 0xffffffff;
1755 store (&code
->dst
, res
);
1757 /* flags after a 16bit logical operation */
1765 store (&code
->dst
, res
);
1773 SET_B_REG (code
->dst
.reg
, res
);
1778 switch (code
->opcode
/ 4)
1781 v
= ((rd
& 0x80) == (ea
& 0x80)
1782 && (rd
& 0x80) != (res
& 0x80));
1786 v
= ((rd
& 0x80) != (-ea
& 0x80)
1787 && (rd
& 0x80) != (res
& 0x80));
1796 SET_W_REG (code
->dst
.reg
, res
);
1800 c
= (res
& 0x10000);
1801 switch (code
->opcode
/ 4)
1804 v
= ((rd
& 0x8000) == (ea
& 0x8000)
1805 && (rd
& 0x8000) != (res
& 0x8000));
1809 v
= ((rd
& 0x8000) != (-ea
& 0x8000)
1810 && (rd
& 0x8000) != (res
& 0x8000));
1819 SET_L_REG (code
->dst
.reg
, res
);
1821 n
= res
& 0x80000000;
1822 nz
= res
& 0xffffffff;
1823 switch (code
->opcode
/ 4)
1826 v
= ((rd
& 0x80000000) == (ea
& 0x80000000)
1827 && (rd
& 0x80000000) != (res
& 0x80000000));
1828 c
= ((unsigned) res
< (unsigned) rd
) || ((unsigned) res
< (unsigned) ea
);
1832 v
= ((rd
& 0x80000000) != (-ea
& 0x80000000)
1833 && (rd
& 0x80000000) != (res
& 0x80000000));
1834 c
= (unsigned) rd
< (unsigned) -ea
;
1837 v
= (rd
== 0x80000000);
1853 if (--poll_count
< 0)
1855 poll_count
= POLL_QUIT_INTERVAL
;
1856 if ((*sim_callback
->poll_quit
) != NULL
1857 && (*sim_callback
->poll_quit
) (sim_callback
))
1862 while (cpu
.state
== SIM_STATE_RUNNING
);
1863 cpu
.ticks
+= get_now () - tick_start
;
1864 cpu
.cycles
+= cycles
;
1871 signal (SIGINT
, prev
);
1875 sim_trace (SIM_DESC sd
)
1877 /* FIXME: Unfinished. */
1882 sim_write (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
1889 for (i
= 0; i
< size
; i
++)
1891 if (addr
< memory_size
)
1893 cpu
.memory
[addr
+ i
] = buffer
[i
];
1894 cpu
.cache_idx
[addr
+ i
] = 0;
1897 cpu
.eightbit
[(addr
+ i
) & 0xff] = buffer
[i
];
1903 sim_read (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
1908 if (addr
< memory_size
)
1909 memcpy (buffer
, cpu
.memory
+ addr
, size
);
1911 memcpy (buffer
, cpu
.eightbit
+ (addr
& 0xff), size
);
1917 sim_store_register (SIM_DESC sd
, int rn
, unsigned char *value
, int length
)
1922 longval
= (value
[0] << 24) | (value
[1] << 16) | (value
[2] << 8) | value
[3];
1923 shortval
= (value
[0] << 8) | (value
[1]);
1924 intval
= h8300hmode
? longval
: shortval
;
1942 cpu
.regs
[rn
] = intval
;
1951 cpu
.cycles
= longval
;
1955 cpu
.insts
= longval
;
1959 cpu
.ticks
= longval
;
1966 sim_fetch_register (SIM_DESC sd
, int rn
, unsigned char *buf
, int length
)
1973 if (!h8300smode
&& rn
>= EXR_REGNUM
)
2011 if (h8300hmode
|| longreg
)
2027 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason
, int *sigrc
)
2029 #if 0 /* FIXME: This should work but we can't use it.
2030 grep for SLEEP above. */
2033 case SIM_STATE_EXITED
: *reason
= sim_exited
; break;
2034 case SIM_STATE_SIGNALLED
: *reason
= sim_signalled
; break;
2035 case SIM_STATE_STOPPED
: *reason
= sim_stopped
; break;
2039 *reason
= sim_stopped
;
2041 *sigrc
= cpu
.exception
;
2044 /* FIXME: Rename to sim_set_mem_size. */
2049 /* Memory size is fixed. */
2053 sim_set_simcache_size (int n
)
2059 cpu
.cache
= (decoded_inst
*) malloc (sizeof (decoded_inst
) * n
);
2060 memset (cpu
.cache
, 0, sizeof (decoded_inst
) * n
);
2066 sim_info (SIM_DESC sd
, int verbose
)
2068 double timetaken
= (double) cpu
.ticks
/ (double) now_persec ();
2069 double virttime
= cpu
.cycles
/ 10.0e6
;
2071 (*sim_callback
->printf_filtered
) (sim_callback
,
2072 "\n\n#instructions executed %10d\n",
2074 (*sim_callback
->printf_filtered
) (sim_callback
,
2075 "#cycles (v approximate) %10d\n",
2077 (*sim_callback
->printf_filtered
) (sim_callback
,
2078 "#real time taken %10.4f\n",
2080 (*sim_callback
->printf_filtered
) (sim_callback
,
2081 "#virtual time taked %10.4f\n",
2083 if (timetaken
!= 0.0)
2084 (*sim_callback
->printf_filtered
) (sim_callback
,
2085 "#simulation ratio %10.4f\n",
2086 virttime
/ timetaken
);
2087 (*sim_callback
->printf_filtered
) (sim_callback
,
2090 (*sim_callback
->printf_filtered
) (sim_callback
,
2091 "#cache size %10d\n",
2095 /* This to be conditional on `what' (aka `verbose'),
2096 however it was never passed as non-zero. */
2100 for (i
= 0; i
< O_LAST
; i
++)
2103 (*sim_callback
->printf_filtered
) (sim_callback
,
2104 "%d: %d\n", i
, cpu
.stats
[i
]);
2110 /* Indicate whether the cpu is an H8/300 or H8/300H.
2111 FLAG is non-zero for the H8/300H. */
2114 set_h8300h (int h_flag
, int s_flag
)
2116 /* FIXME: Much of the code in sim_load can be moved to sim_open.
2117 This function being replaced by a sim_open:ARGV configuration
2119 h8300hmode
= h_flag
;
2120 h8300smode
= s_flag
;
2124 sim_open (SIM_OPEN_KIND kind
,
2125 struct host_callback_struct
*ptr
,
2129 /* FIXME: Much of the code in sim_load can be moved here. */
2134 /* Fudge our descriptor. */
2135 return (SIM_DESC
) 1;
2139 sim_close (SIM_DESC sd
, int quitting
)
2141 /* Nothing to do. */
2144 /* Called by gdb to load a program into memory. */
2147 sim_load (SIM_DESC sd
, char *prog
, bfd
*abfd
, int from_tty
)
2151 /* FIXME: The code below that sets a specific variant of the H8/300
2152 being simulated should be moved to sim_open(). */
2154 /* See if the file is for the H8/300 or H8/300H. */
2155 /* ??? This may not be the most efficient way. The z8k simulator
2156 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
2160 prog_bfd
= bfd_openr (prog
, "coff-h8300");
2161 if (prog_bfd
!= NULL
)
2163 /* Set the cpu type. We ignore failure from bfd_check_format
2164 and bfd_openr as sim_load_file checks too. */
2165 if (bfd_check_format (prog_bfd
, bfd_object
))
2167 unsigned long mach
= bfd_get_mach (prog_bfd
);
2168 set_h8300h (mach
== bfd_mach_h8300h
|| mach
== bfd_mach_h8300s
,
2169 mach
== bfd_mach_h8300s
);
2173 /* If we're using gdb attached to the simulator, then we have to
2174 reallocate memory for the simulator.
2176 When gdb first starts, it calls fetch_registers (among other
2177 functions), which in turn calls init_pointers, which allocates
2180 The problem is when we do that, we don't know whether we're
2181 debugging an H8/300 or H8/300H program.
2183 This is the first point at which we can make that determination,
2184 so we just reallocate memory now; this will also allow us to handle
2185 switching between H8/300 and H8/300H programs without exiting
2189 memory_size
= H8300S_MSIZE
;
2190 else if (h8300hmode
)
2191 memory_size
= H8300H_MSIZE
;
2193 memory_size
= H8300_MSIZE
;
2198 free (cpu
.cache_idx
);
2200 free (cpu
.eightbit
);
2202 cpu
.memory
= (unsigned char *) calloc (sizeof (char), memory_size
);
2203 cpu
.cache_idx
= (unsigned short *) calloc (sizeof (short), memory_size
);
2204 cpu
.eightbit
= (unsigned char *) calloc (sizeof (char), 256);
2206 /* `msize' must be a power of two. */
2207 if ((memory_size
& (memory_size
- 1)) != 0)
2209 cpu
.mask
= memory_size
- 1;
2211 if (sim_load_file (sd
, myname
, sim_callback
, prog
, prog_bfd
,
2212 sim_kind
== SIM_OPEN_DEBUG
,
2216 /* Close the bfd if we opened it. */
2217 if (abfd
== NULL
&& prog_bfd
!= NULL
)
2218 bfd_close (prog_bfd
);
2222 /* Close the bfd if we opened it. */
2223 if (abfd
== NULL
&& prog_bfd
!= NULL
)
2224 bfd_close (prog_bfd
);
2229 sim_create_inferior (SIM_DESC sd
, struct _bfd
*abfd
, char **argv
, char **env
)
2232 cpu
.pc
= bfd_get_start_address (abfd
);
2239 sim_do_command (SIM_DESC sd
, char *cmd
)
2241 (*sim_callback
->printf_filtered
) (sim_callback
,
2242 "This simulator does not accept any commands.\n");
2246 sim_set_callbacks (struct host_callback_struct
*ptr
)