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>
36 #include "remote-sim.h"
44 host_callback *sim_callback;
46 static SIM_OPEN_KIND sim_kind;
49 /* FIXME: Needs to live in header file.
50 This header should also include the things in remote-sim.h.
51 One could move this to remote-sim.h but this function isn't needed
53 void sim_set_simcache_size PARAMS ((int));
55 #define X(op, size) op*4+size
57 #define SP (h8300hmode ? SL:SW)
70 #define h8_opcodes ops
72 #include "opcode/h8300.h"
76 /* The rate at which to call the host's poll_quit callback. */
78 #define POLL_QUIT_INTERVAL 0x80000
80 #define LOW_BYTE(x) ((x) & 0xff)
81 #define HIGH_BYTE(x) (((x)>>8) & 0xff)
82 #define P(X,Y) ((X<<8) | Y)
84 #define BUILDSR() cpu.ccr = (N << 3) | (Z << 2) | (V<<1) | C;
87 c = (cpu.ccr >> 0) & 1;\
88 v = (cpu.ccr >> 1) & 1;\
89 nz = !((cpu.ccr >> 2) & 1);\
90 n = (cpu.ccr >> 3) & 1;
92 #ifdef __CHAR_IS_SIGNED__
93 #define SEXTCHAR(x) ((char)(x))
97 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff): x & 0xff)
100 #define UEXTCHAR(x) ((x) & 0xff)
101 #define UEXTSHORT(x) ((x) & 0xffff)
102 #define SEXTSHORT(x) ((short)(x))
104 static cpu_state_type cpu;
109 static int memory_size;
138 return h8300hmode ? SL : SW;
150 return X (OP_IMM, SP);
152 return X (OP_REG, SP);
155 return X (OP_MEM, SP);
163 decode (addr, data, dst)
175 struct h8_opcode *q = h8_opcodes;
181 /* Find the exact opcode/arg combo. */
185 unsigned int len = 0;
191 op_type looking_for = *nib;
192 int thisnib = data[len >> 1];
194 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
196 if (looking_for < 16 && looking_for >= 0)
198 if (looking_for != thisnib)
203 if ((int) looking_for & (int) B31)
205 if (!(((int) thisnib & 0x8) != 0))
208 looking_for = (op_type) ((int) looking_for & ~(int) B31);
212 if ((int) looking_for & (int) B30)
214 if (!(((int) thisnib & 0x8) == 0))
217 looking_for = (op_type) ((int) looking_for & ~(int) B30);
220 if (looking_for & DBIT)
222 /* Exclude adds/subs by looking at bit 0 and 2, and
223 make sure the operand size, either w or l,
224 matches by looking at bit 1. */
225 if ((looking_for & 7) != (thisnib & 7))
228 abs = (thisnib & 0x8) ? 2 : 1;
230 else if (looking_for & (REG | IND | INC | DEC))
232 if (looking_for & REG)
234 /* Can work out size from the register. */
235 size = bitfrom (looking_for);
237 if (looking_for & SRC)
242 else if (looking_for & L_16)
244 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
246 if (looking_for & (PCREL | DISP))
251 else if (looking_for & ABSJMP)
253 abs = (data[1] << 16) | (data[2] << 8) | (data[3]);
255 else if (looking_for & MEMIND)
259 else if (looking_for & L_32)
263 abs = (data[i] << 24)
264 | (data[i + 1] << 16)
270 else if (looking_for & L_24)
274 abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
277 else if (looking_for & IGNORE)
281 else if (looking_for & DISPREG)
283 rdisp = thisnib & 0x7;
285 else if (looking_for & KBIT)
302 else if (looking_for & L_8)
306 if (looking_for & PCREL)
308 abs = SEXTCHAR (data[len >> 1]);
310 else if (looking_for & ABS8MEM)
313 abs = h8300hmode ? ~0xff0000ff : ~0xffff00ff;
314 abs |= data[len >> 1] & 0xff;
318 abs = data[len >> 1] & 0xff;
321 else if (looking_for & L_3)
327 else if (looking_for == E)
331 /* Fill in the args. */
333 op_type *args = q->args.nib;
339 int rn = (x & DST) ? rd : rs;
349 p->type = X (OP_IMM, size);
352 else if (x & (IMM | KBIT | DBIT))
354 p->type = X (OP_IMM, size);
359 /* Reset the size, some
360 ops (like mul) have two sizes */
363 p->type = X (OP_REG, size);
368 p->type = X (OP_INC, size);
373 p->type = X (OP_DEC, size);
378 p->type = X (OP_DISP, size);
382 else if (x & (ABS | ABSJMP | ABS8MEM))
384 p->type = X (OP_DISP, size);
390 p->type = X (OP_MEM, size);
395 p->type = X (OP_PCREL, size);
396 p->literal = abs + addr + 2;
402 p->type = X (OP_IMM, SP);
407 p->type = X (OP_DISP, size);
409 p->reg = rdisp & 0x7;
416 printf ("Hmmmm %x", x);
422 /* But a jmp or a jsr gets automagically lvalued,
423 since we branch to their address not their
425 if (q->how == O (O_JSR, SB)
426 || q->how == O (O_JMP, SB))
428 dst->src.type = lvalue (dst->src.type, dst->src.reg);
431 if (dst->dst.type == -1)
434 dst->opcode = q->how;
435 dst->cycles = q->time;
437 /* And a jsr to 0xc4 is turned into a magic trap. */
439 if (dst->opcode == O (O_JSR, SB))
441 if (dst->src.literal == 0xc4)
443 dst->opcode = O (O_SYSCALL, SB);
447 dst->next_pc = addr + len / 2;
451 printf ("Don't understand %x \n", looking_for);
462 /* Fell off the end. */
463 dst->opcode = O (O_ILL, SB);
471 /* find the next cache entry to use */
473 idx = cpu.cache_top + 1;
475 if (idx >= cpu.csize)
481 /* Throw away its old meaning */
482 cpu.cache_idx[cpu.cache[idx].oldpc] = 0;
484 /* set to new address */
485 cpu.cache[idx].oldpc = pc;
487 /* fill in instruction info */
488 decode (pc, cpu.memory + pc, cpu.cache + idx);
490 /* point to new cache entry */
491 cpu.cache_idx[pc] = idx;
495 static unsigned char *breg[18];
496 static unsigned short *wreg[18];
497 static unsigned int *lreg[18];
499 #define GET_B_REG(x) *(breg[x])
500 #define SET_B_REG(x,y) (*(breg[x])) = (y)
501 #define GET_W_REG(x) *(wreg[x])
502 #define SET_W_REG(x,y) (*(wreg[x])) = (y)
504 #define GET_L_REG(x) *(lreg[x])
505 #define SET_L_REG(x,y) (*(lreg[x])) = (y)
507 #define GET_MEMORY_L(x) \
509 ? ((cpu.memory[x+0] << 24) | (cpu.memory[x+1] << 16) \
510 | (cpu.memory[x+2] << 8) | cpu.memory[x+3]) \
511 : ((cpu.eightbit[(x+0) & 0xff] << 24) | (cpu.eightbit[(x+1) & 0xff] << 16) \
512 | (cpu.eightbit[(x+2) & 0xff] << 8) | cpu.eightbit[(x+3) & 0xff]))
514 #define GET_MEMORY_W(x) \
516 ? ((cpu.memory[x+0] << 8) | (cpu.memory[x+1] << 0)) \
517 : ((cpu.eightbit[(x+0) & 0xff] << 8) | (cpu.eightbit[(x+1) & 0xff] << 0)))
520 #define GET_MEMORY_B(x) \
521 (x < memory_size ? (cpu.memory[x]) : (cpu.eightbit[x & 0xff]))
523 #define SET_MEMORY_L(x,y) \
524 { register unsigned char *_p; register int __y = y; \
525 _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
526 _p[0] = (__y)>>24; _p[1] = (__y)>>16; \
527 _p[2] = (__y)>>8; _p[3] = (__y)>>0;}
529 #define SET_MEMORY_W(x,y) \
530 { register unsigned char *_p; register int __y = y; \
531 _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
532 _p[0] = (__y)>>8; _p[1] =(__y);}
534 #define SET_MEMORY_B(x,y) \
535 (x < memory_size ? (cpu.memory[(x)] = y) : (cpu.eightbit[x & 0xff] = y))
542 int abs = arg->literal;
549 return GET_B_REG (rn);
551 return GET_W_REG (rn);
553 return GET_L_REG (rn);
564 r = GET_MEMORY_B (t);
573 r = GET_MEMORY_W (t);
581 r = GET_MEMORY_L (t);
588 case X (OP_DISP, SB):
589 t = GET_L_REG (rn) + abs;
591 return GET_MEMORY_B (t);
593 case X (OP_DISP, SW):
594 t = GET_L_REG (rn) + abs;
596 return GET_MEMORY_W (t);
598 case X (OP_DISP, SL):
599 t = GET_L_REG (rn) + abs;
601 return GET_MEMORY_L (t);
604 t = GET_MEMORY_L (abs);
609 t = GET_MEMORY_W (abs);
627 int abs = arg->literal;
643 t = GET_L_REG (rn) - 1;
650 t = (GET_L_REG (rn) - 2) & cpu.mask;
656 t = (GET_L_REG (rn) - 4) & cpu.mask;
661 case X (OP_DISP, SB):
662 t = GET_L_REG (rn) + abs;
667 case X (OP_DISP, SW):
668 t = GET_L_REG (rn) + abs;
673 case X (OP_DISP, SL):
674 t = GET_L_REG (rn) + abs;
711 memory_size = H8300H_MSIZE;
713 memory_size = H8300_MSIZE;
714 cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
715 cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
716 cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
718 /* `msize' must be a power of two */
719 if ((memory_size & (memory_size - 1)) != 0)
721 cpu.mask = memory_size - 1;
723 for (i = 0; i < 9; i++)
728 for (i = 0; i < 8; i++)
730 unsigned char *p = (unsigned char *) (cpu.regs + i);
731 unsigned char *e = (unsigned char *) (cpu.regs + i + 1);
732 unsigned short *q = (unsigned short *) (cpu.regs + i);
733 unsigned short *u = (unsigned short *) (cpu.regs + i + 1);
734 cpu.regs[i] = 0x00112233;
760 lreg[i] = &cpu.regs[i];
763 lreg[8] = &cpu.regs[8];
765 /* initialize the seg registers */
767 sim_set_simcache_size (CSIZE);
772 control_c (sig, code, scp, addr)
778 cpu.state = SIM_STATE_STOPPED;
779 cpu.exception = SIGINT;
788 mop (code, bsize, sign)
801 bsize ? SEXTCHAR (GET_W_REG (code->dst.reg)) :
802 SEXTSHORT (GET_W_REG (code->dst.reg));
804 bsize ? SEXTCHAR (GET_B_REG (code->src.reg)) :
805 SEXTSHORT (GET_W_REG (code->src.reg));
809 multiplicand = bsize ? UEXTCHAR (GET_W_REG (code->dst.reg)) :
810 UEXTSHORT (GET_W_REG (code->dst.reg));
812 bsize ? UEXTCHAR (GET_B_REG (code->src.reg)) :
813 UEXTSHORT (GET_W_REG (code->src.reg));
816 result = multiplier * multiplicand;
820 n = result & (bsize ? 0x8000 : 0x80000000);
821 nz = result & (bsize ? 0xffff : 0xffffffff);
825 SET_W_REG (code->dst.reg, result);
829 SET_L_REG (code->dst.reg, result);
831 /* return ((n==1) << 1) | (nz==1); */
835 #define ONOT(name, how) \
840 rd = GET_B_REG (code->src.reg); \
848 rd = GET_W_REG (code->src.reg); \
855 int hm = 0x80000000; \
856 rd = GET_L_REG (code->src.reg); \
861 #define OSHIFTS(name, how1, how2) \
866 rd = GET_B_REG (code->src.reg); \
867 if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
881 rd = GET_W_REG (code->src.reg); \
882 if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
895 int hm = 0x80000000; \
896 rd = GET_L_REG (code->src.reg); \
897 if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
908 #define OBITOP(name,f, s, op) \
913 if (f) ea = fetch (&code->dst); \
914 m=1<< fetch(&code->src); \
916 if(s) store (&code->dst,ea); goto next; \
923 cpu.state = SIM_STATE_STOPPED;
924 cpu.exception = SIGINT;
929 sim_resume (sd, step, siggnal)
935 int tick_start = get_now ();
948 prev = signal (SIGINT, control_c);
952 cpu.state = SIM_STATE_STOPPED;
953 cpu.exception = SIGTRAP;
957 cpu.state = SIM_STATE_RUNNING;
963 /* The PC should never be odd. */
977 cidx = cpu.cache_idx[pc];
978 code = cpu.cache + cidx;
981 #define ALUOP(STORE, NAME, HOW) \
982 case O(NAME,SB): HOW; if(STORE)goto alu8;else goto just_flags_alu8; \
983 case O(NAME, SW): HOW; if(STORE)goto alu16;else goto just_flags_alu16; \
984 case O(NAME,SL): HOW; if(STORE)goto alu32;else goto just_flags_alu32;
987 #define LOGOP(NAME, HOW) \
988 case O(NAME,SB): HOW; goto log8;\
989 case O(NAME, SW): HOW; goto log16;\
990 case O(NAME,SL): HOW; goto log32;
997 printf ("%x %d %s\n", pc, code->opcode,
998 code->op ? code->op->name : "**");
1000 cpu.stats[code->opcode]++;
1004 cycles += code->cycles;
1006 switch (code->opcode)
1010 * This opcode is a fake for when we get to an
1011 * instruction which hasnt been compiled
1018 case O (O_SUBX, SB):
1019 rd = fetch (&code->dst);
1020 ea = fetch (&code->src);
1025 case O (O_ADDX, SB):
1026 rd = fetch (&code->dst);
1027 ea = fetch (&code->src);
1032 #define EA ea = fetch(&code->src);
1033 #define RD_EA ea = fetch(&code->src); rd = fetch(&code->dst);
1035 ALUOP (1, O_SUB, RD_EA;
1038 ALUOP (1, O_NEG, EA;
1044 rd = GET_B_REG (code->dst.reg);
1045 ea = fetch (&code->src);
1049 rd = GET_W_REG (code->dst.reg);
1050 ea = fetch (&code->src);
1054 rd = GET_L_REG (code->dst.reg);
1055 ea = fetch (&code->src);
1060 LOGOP (O_AND, RD_EA;
1066 LOGOP (O_XOR, RD_EA;
1070 case O (O_MOV_TO_MEM, SB):
1071 res = GET_B_REG (code->src.reg);
1073 case O (O_MOV_TO_MEM, SW):
1074 res = GET_W_REG (code->src.reg);
1076 case O (O_MOV_TO_MEM, SL):
1077 res = GET_L_REG (code->src.reg);
1081 case O (O_MOV_TO_REG, SB):
1082 res = fetch (&code->src);
1083 SET_B_REG (code->dst.reg, res);
1084 goto just_flags_log8;
1085 case O (O_MOV_TO_REG, SW):
1086 res = fetch (&code->src);
1087 SET_W_REG (code->dst.reg, res);
1088 goto just_flags_log16;
1089 case O (O_MOV_TO_REG, SL):
1090 res = fetch (&code->src);
1091 SET_L_REG (code->dst.reg, res);
1092 goto just_flags_log32;
1095 case O (O_ADDS, SL):
1096 SET_L_REG (code->dst.reg,
1097 GET_L_REG (code->dst.reg)
1098 + code->src.literal);
1102 case O (O_SUBS, SL):
1103 SET_L_REG (code->dst.reg,
1104 GET_L_REG (code->dst.reg)
1105 - code->src.literal);
1109 rd = fetch (&code->dst);
1110 ea = fetch (&code->src);
1113 goto just_flags_alu8;
1116 rd = fetch (&code->dst);
1117 ea = fetch (&code->src);
1120 goto just_flags_alu16;
1123 rd = fetch (&code->dst);
1124 ea = fetch (&code->src);
1127 goto just_flags_alu32;
1131 rd = GET_B_REG (code->src.reg);
1134 SET_B_REG (code->src.reg, res);
1135 goto just_flags_inc8;
1138 rd = GET_W_REG (code->dst.reg);
1139 ea = -code->src.literal;
1141 SET_W_REG (code->dst.reg, res);
1142 goto just_flags_inc16;
1145 rd = GET_L_REG (code->dst.reg);
1146 ea = -code->src.literal;
1148 SET_L_REG (code->dst.reg, res);
1149 goto just_flags_inc32;
1153 rd = GET_B_REG (code->src.reg);
1156 SET_B_REG (code->src.reg, res);
1157 goto just_flags_inc8;
1160 rd = GET_W_REG (code->dst.reg);
1161 ea = code->src.literal;
1163 SET_W_REG (code->dst.reg, res);
1164 goto just_flags_inc16;
1167 rd = GET_L_REG (code->dst.reg);
1168 ea = code->src.literal;
1170 SET_L_REG (code->dst.reg, res);
1171 goto just_flags_inc32;
1174 #define GET_CCR(x) BUILDSR();x = cpu.ccr
1176 case O (O_ANDC, SB):
1178 ea = code->src.literal;
1184 ea = code->src.literal;
1188 case O (O_XORC, SB):
1190 ea = code->src.literal;
1231 if (((Z || (N ^ V)) == 0))
1237 if (((Z || (N ^ V)) == 1))
1271 case O (O_SYSCALL, SB):
1273 char c = cpu.regs[2];
1274 sim_callback->write_stdout (sim_callback, &c, 1);
1278 ONOT (O_NOT, rd = ~rd; v = 0;);
1280 c = rd & hm; v = 0; rd <<= 1,
1281 c = rd & (hm >> 1); v = 0; rd <<= 2);
1283 c = rd & 1; v = 0; rd = (unsigned int) rd >> 1,
1284 c = rd & 2; v = 0; rd = (unsigned int) rd >> 2);
1286 c = rd & hm; v = (rd & hm) != ((rd & (hm >> 1)) << 1); rd <<= 1,
1287 c = rd & (hm >> 1); v = (rd & (hm >> 1)) != ((rd & (hm >> 2)) << 2); rd <<= 2);
1289 t = rd & hm; c = rd & 1; v = 0; rd >>= 1; rd |= t,
1290 t = rd & hm; c = rd & 2; v = 0; rd >>= 2; rd |= t | t >> 1 );
1292 c = rd & hm; v = 0; rd <<= 1; rd |= C,
1293 c = rd & hm; v = 0; rd <<= 1; rd |= C; c = rd & hm; rd <<= 1; rd |= C);
1295 c = rd & 1; v = 0; rd = (unsigned int) rd >> 1; if (c) rd |= hm,
1296 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);
1298 t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0,
1299 t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0; t = rd & hm; rd <<= 1; rd |= C; c = t);
1301 t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t; v = 0,
1302 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);
1306 pc = fetch (&code->src);
1314 pc = fetch (&code->src);
1321 SET_MEMORY_L (tmp, code->next_pc);
1326 SET_MEMORY_W (tmp, code->next_pc);
1333 pc = code->src.literal;
1344 pc = GET_MEMORY_L (tmp);
1349 pc = GET_MEMORY_W (tmp);
1358 cpu.state = SIM_STATE_STOPPED;
1359 cpu.exception = SIGILL;
1361 case O (O_SLEEP, SN):
1362 /* FIXME: Doesn't this break for breakpoints when r0
1363 contains just the right (er, wrong) value? */
1364 cpu.state = SIM_STATE_STOPPED;
1365 /* The format of r0 is defined by target newlib. Expand
1366 the macros here instead of looking for .../sys/wait.h. */
1367 #define SIM_WIFEXITED(v) (((v) & 0xff) == 0)
1368 #define SIM_WIFSIGNALED(v) (((v) & 0x7f) > 0 && (((v) & 0x7f) < 0x7f))
1369 if (! SIM_WIFEXITED (cpu.regs[0]) && SIM_WIFSIGNALED (cpu.regs[0]))
1370 cpu.exception = SIGILL;
1372 cpu.exception = SIGTRAP;
1375 cpu.state = SIM_STATE_STOPPED;
1376 cpu.exception = SIGTRAP;
1379 OBITOP (O_BNOT, 1, 1, ea ^= m);
1380 OBITOP (O_BTST, 1, 0, nz = ea & m);
1381 OBITOP (O_BCLR, 1, 1, ea &= ~m);
1382 OBITOP (O_BSET, 1, 1, ea |= m);
1383 OBITOP (O_BLD, 1, 0, c = ea & m);
1384 OBITOP (O_BILD, 1, 0, c = !(ea & m));
1385 OBITOP (O_BST, 1, 1, ea &= ~m;
1387 OBITOP (O_BIST, 1, 1, ea &= ~m;
1389 OBITOP (O_BAND, 1, 0, c = (ea & m) && C);
1390 OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C);
1391 OBITOP (O_BOR, 1, 0, c = (ea & m) || C);
1392 OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C);
1393 OBITOP (O_BXOR, 1, 0, c = (ea & m) != C);
1394 OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C);
1397 #define MOP(bsize, signed) mop(code, bsize,signed); goto next;
1399 case O (O_MULS, SB):
1402 case O (O_MULS, SW):
1405 case O (O_MULU, SB):
1408 case O (O_MULU, SW):
1413 case O (O_DIVU, SB):
1415 rd = GET_W_REG (code->dst.reg);
1416 ea = GET_B_REG (code->src.reg);
1419 tmp = (unsigned)rd % ea;
1420 rd = (unsigned)rd / ea;
1422 SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
1428 case O (O_DIVU, SW):
1430 rd = GET_L_REG (code->dst.reg);
1431 ea = GET_W_REG (code->src.reg);
1436 tmp = (unsigned)rd % ea;
1437 rd = (unsigned)rd / ea;
1439 SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
1443 case O (O_DIVS, SB):
1446 rd = SEXTSHORT (GET_W_REG (code->dst.reg));
1447 ea = SEXTCHAR (GET_B_REG (code->src.reg));
1450 tmp = (int) rd % (int) ea;
1451 rd = (int) rd / (int) ea;
1457 SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
1460 case O (O_DIVS, SW):
1462 rd = GET_L_REG (code->dst.reg);
1463 ea = SEXTSHORT (GET_W_REG (code->src.reg));
1466 tmp = (int) rd % (int) ea;
1467 rd = (int) rd / (int) ea;
1468 n = rd & 0x80000000;
1473 SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
1476 case O (O_EXTS, SW):
1477 rd = GET_B_REG (code->src.reg + 8) & 0xff; /* Yes, src, not dst. */
1478 ea = rd & 0x80 ? -256 : 0;
1481 case O (O_EXTS, SL):
1482 rd = GET_W_REG (code->src.reg) & 0xffff;
1483 ea = rd & 0x8000 ? -65536 : 0;
1486 case O (O_EXTU, SW):
1487 rd = GET_B_REG (code->src.reg + 8) & 0xff;
1491 case O (O_EXTU, SL):
1492 rd = GET_W_REG (code->src.reg) & 0xffff;
1502 int nregs, firstreg, i;
1504 nregs = GET_MEMORY_B (pc + 1);
1507 firstreg = GET_MEMORY_B (pc + 3);
1509 for (i = firstreg; i <= firstreg + nregs; i++)
1512 SET_MEMORY_L (cpu.regs[7], cpu.regs[i]);
1519 int nregs, firstreg, i;
1521 nregs = GET_MEMORY_B (pc + 1);
1524 firstreg = GET_MEMORY_B (pc + 3);
1526 for (i = firstreg; i >= firstreg - nregs; i--)
1528 cpu.regs[i] = GET_MEMORY_L (cpu.regs[7]);
1535 cpu.state = SIM_STATE_STOPPED;
1536 cpu.exception = SIGILL;
1548 /* When a branch works */
1549 pc = code->src.literal;
1552 /* Set the cond codes from res */
1555 /* Set the flags after an 8 bit inc/dec operation */
1559 v = (rd & 0x7f) == 0x7f;
1563 /* Set the flags after an 16 bit inc/dec operation */
1567 v = (rd & 0x7fff) == 0x7fff;
1571 /* Set the flags after an 32 bit inc/dec operation */
1573 n = res & 0x80000000;
1574 nz = res & 0xffffffff;
1575 v = (rd & 0x7fffffff) == 0x7fffffff;
1580 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
1583 SET_B_REG (code->src.reg, rd);
1587 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
1590 SET_W_REG (code->src.reg, rd);
1594 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
1595 n = (rd & 0x80000000);
1596 nz = rd & 0xffffffff;
1597 SET_L_REG (code->src.reg, rd);
1601 store (&code->dst, res);
1603 /* flags after a 32bit logical operation */
1604 n = res & 0x80000000;
1605 nz = res & 0xffffffff;
1610 store (&code->dst, res);
1612 /* flags after a 16bit logical operation */
1620 store (&code->dst, res);
1628 SET_B_REG (code->dst.reg, res);
1633 switch (code->opcode / 4)
1636 v = ((rd & 0x80) == (ea & 0x80)
1637 && (rd & 0x80) != (res & 0x80));
1641 v = ((rd & 0x80) != (-ea & 0x80)
1642 && (rd & 0x80) != (res & 0x80));
1651 SET_W_REG (code->dst.reg, res);
1655 c = (res & 0x10000);
1656 switch (code->opcode / 4)
1659 v = ((rd & 0x8000) == (ea & 0x8000)
1660 && (rd & 0x8000) != (res & 0x8000));
1664 v = ((rd & 0x8000) != (-ea & 0x8000)
1665 && (rd & 0x8000) != (res & 0x8000));
1674 SET_L_REG (code->dst.reg, res);
1676 n = res & 0x80000000;
1677 nz = res & 0xffffffff;
1678 switch (code->opcode / 4)
1681 v = ((rd & 0x80000000) == (ea & 0x80000000)
1682 && (rd & 0x80000000) != (res & 0x80000000));
1683 c = ((unsigned) res < (unsigned) rd) || ((unsigned) res < (unsigned) ea);
1687 v = ((rd & 0x80000000) != (-ea & 0x80000000)
1688 && (rd & 0x80000000) != (res & 0x80000000));
1689 c = (unsigned) rd < (unsigned) -ea;
1692 v = (rd == 0x80000000);
1703 /* if (cpu.regs[8] ) abort(); */
1705 if (--poll_count < 0)
1707 poll_count = POLL_QUIT_INTERVAL;
1708 if ((*sim_callback->poll_quit) != NULL
1709 && (*sim_callback->poll_quit) (sim_callback))
1714 while (cpu.state == SIM_STATE_RUNNING);
1715 cpu.ticks += get_now () - tick_start;
1716 cpu.cycles += cycles;
1722 signal (SIGINT, prev);
1729 /* FIXME: unfinished */
1734 sim_write (sd, addr, buffer, size)
1737 unsigned char *buffer;
1745 for (i = 0; i < size; i++)
1747 if (addr < memory_size)
1749 cpu.memory[addr + i] = buffer[i];
1750 cpu.cache_idx[addr + i] = 0;
1753 cpu.eightbit[(addr + i) & 0xff] = buffer[i];
1759 sim_read (sd, addr, buffer, size)
1762 unsigned char *buffer;
1768 if (addr < memory_size)
1769 memcpy (buffer, cpu.memory + addr, size);
1771 memcpy (buffer, cpu.eightbit + (addr & 0xff), size);
1785 #define SP_REGNUM R7_REGNUM /* Contains address of top of stack */
1786 #define FP_REGNUM R6_REGNUM /* Contains address of executing
1789 #define CCR_REGNUM 8 /* Contains processor status */
1790 #define PC_REGNUM 9 /* Contains program counter */
1792 #define CYCLE_REGNUM 10
1793 #define INST_REGNUM 11
1794 #define TICK_REGNUM 12
1798 sim_store_register (sd, rn, value, length)
1801 unsigned char *value;
1807 longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
1808 shortval = (value[0] << 8) | (value[1]);
1809 intval = h8300hmode ? longval : shortval;
1827 cpu.regs[rn] = intval;
1833 cpu.cycles = longval;
1837 cpu.insts = longval;
1841 cpu.ticks = longval;
1848 sim_fetch_register (sd, rn, buf, length)
1892 if (h8300hmode || longreg)
1908 sim_stop_reason (sd, reason, sigrc)
1910 enum sim_stop *reason;
1913 #if 0 /* FIXME: This should work but we can't use it.
1914 grep for SLEEP above. */
1917 case SIM_STATE_EXITED : *reason = sim_exited; break;
1918 case SIM_STATE_SIGNALLED : *reason = sim_signalled; break;
1919 case SIM_STATE_STOPPED : *reason = sim_stopped; break;
1923 *reason = sim_stopped;
1925 *sigrc = cpu.exception;
1928 /* FIXME: Rename to sim_set_mem_size. */
1934 /* Memory size is fixed. */
1938 sim_set_simcache_size (n)
1944 cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
1945 memset (cpu.cache, 0, sizeof (decoded_inst) * n);
1951 sim_info (sd, verbose)
1955 double timetaken = (double) cpu.ticks / (double) now_persec ();
1956 double virttime = cpu.cycles / 10.0e6;
1958 (*sim_callback->printf_filtered) (sim_callback,
1959 "\n\n#instructions executed %10d\n",
1961 (*sim_callback->printf_filtered) (sim_callback,
1962 "#cycles (v approximate) %10d\n",
1964 (*sim_callback->printf_filtered) (sim_callback,
1965 "#real time taken %10.4f\n",
1967 (*sim_callback->printf_filtered) (sim_callback,
1968 "#virtual time taked %10.4f\n",
1970 if (timetaken != 0.0)
1971 (*sim_callback->printf_filtered) (sim_callback,
1972 "#simulation ratio %10.4f\n",
1973 virttime / timetaken);
1974 (*sim_callback->printf_filtered) (sim_callback,
1977 (*sim_callback->printf_filtered) (sim_callback,
1978 "#cache size %10d\n",
1982 /* This to be conditional on `what' (aka `verbose'),
1983 however it was never passed as non-zero. */
1987 for (i = 0; i < O_LAST; i++)
1990 (*sim_callback->printf_filtered) (sim_callback,
1991 "%d: %d\n", i, cpu.stats[i]);
1997 /* Indicate whether the cpu is an h8/300 or h8/300h.
1998 FLAG is non-zero for the h8/300h. */
2004 /* FIXME: Much of the code in sim_load can be moved to sim_open.
2005 This function being replaced by a sim_open:ARGV configuration
2011 sim_open (kind, ptr, abfd, argv)
2013 struct host_callback_struct *ptr;
2017 /* FIXME: Much of the code in sim_load can be moved here */
2022 /* fudge our descriptor */
2023 return (SIM_DESC) 1;
2027 sim_close (sd, quitting)
2034 /* Called by gdb to load a program into memory. */
2037 sim_load (sd, prog, abfd, from_tty)
2045 /* FIXME: The code below that sets a specific variant of the h8/300
2046 being simulated should be moved to sim_open(). */
2048 /* See if the file is for the h8/300 or h8/300h. */
2049 /* ??? This may not be the most efficient way. The z8k simulator
2050 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
2054 prog_bfd = bfd_openr (prog, "coff-h8300");
2055 if (prog_bfd != NULL)
2057 /* Set the cpu type. We ignore failure from bfd_check_format
2058 and bfd_openr as sim_load_file checks too. */
2059 if (bfd_check_format (prog_bfd, bfd_object))
2061 unsigned long mach = bfd_get_mach (prog_bfd);
2062 set_h8300h (mach == bfd_mach_h8300h
2063 || mach == bfd_mach_h8300s);
2067 /* If we're using gdb attached to the simulator, then we have to
2068 reallocate memory for the simulator.
2070 When gdb first starts, it calls fetch_registers (among other
2071 functions), which in turn calls init_pointers, which allocates
2074 The problem is when we do that, we don't know whether we're
2075 debugging an h8/300 or h8/300h program.
2077 This is the first point at which we can make that determination,
2078 so we just reallocate memory now; this will also allow us to handle
2079 switching between h8/300 and h8/300h programs without exiting
2082 memory_size = H8300H_MSIZE;
2084 memory_size = H8300_MSIZE;
2089 free (cpu.cache_idx);
2091 free (cpu.eightbit);
2093 cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
2094 cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
2095 cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
2097 /* `msize' must be a power of two */
2098 if ((memory_size & (memory_size - 1)) != 0)
2100 cpu.mask = memory_size - 1;
2102 if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd,
2103 sim_kind == SIM_OPEN_DEBUG,
2107 /* Close the bfd if we opened it. */
2108 if (abfd == NULL && prog_bfd != NULL)
2109 bfd_close (prog_bfd);
2113 /* Close the bfd if we opened it. */
2114 if (abfd == NULL && prog_bfd != NULL)
2115 bfd_close (prog_bfd);
2120 sim_create_inferior (sd, abfd, argv, env)
2127 cpu.pc = bfd_get_start_address (abfd);
2134 sim_do_command (sd, cmd)
2138 (*sim_callback->printf_filtered) (sim_callback,
2139 "This simulator does not accept any commands.\n");
2143 sim_set_callbacks (ptr)
2144 struct host_callback_struct *ptr;