2 * Simulator for the Hitachi H8/300 architecture.
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.
29 #include <sys/param.h>
33 #include "remote-sim.h"
39 #define X(op, size) op*4+size
41 #define SP (h8300hmode ? SL:SW)
54 #define h8_opcodes ops
56 #include "opcode/h8300.h"
60 #define LOW_BYTE(x) ((x) & 0xff)
61 #define HIGH_BYTE(x) (((x)>>8) & 0xff)
62 #define P(X,Y) ((X<<8) | Y)
64 #define BUILDSR() cpu.ccr = (N << 3) | (Z << 2) | (V<<1) | C;
67 c = (cpu.ccr >> 0) & 1;\
68 v = (cpu.ccr >> 1) & 1;\
69 nz = !((cpu.ccr >> 2) & 1);\
70 n = (cpu.ccr >> 3) & 1;
72 #ifdef __CHAR_IS_SIGNED__
73 #define SEXTCHAR(x) ((char)(x))
77 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff): x & 0xff)
80 #define UEXTCHAR(x) ((x) & 0xff)
81 #define UEXTSHORT(x) ((x) & 0xffff)
82 #define SEXTSHORT(x) ((short)(x))
84 static cpu_state_type cpu;
89 static int memory_size;
120 return h8300hmode ? SL : SW;
133 return X (OP_IMM, SP);
135 return X (OP_REG, SP);
139 return X (OP_MEM, SP);
146 decode (addr, data, dst)
159 struct h8_opcode *q = h8_opcodes;
163 /* Find the exact opcode/arg combo */
167 unsigned int len = 0;
173 op_type looking_for = *nib;
174 int thisnib = data[len >> 1];
176 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
178 if (looking_for < 16 && looking_for >= 0)
180 if (looking_for != thisnib)
185 if ((int) looking_for & (int) B31)
187 if (!(((int) thisnib & 0x8) != 0))
189 looking_for = (op_type) ((int) looking_for & ~(int)
193 if ((int) looking_for & (int) B30)
195 if (!(((int) thisnib & 0x8) == 0))
197 looking_for = (op_type) ((int) looking_for & ~(int) B30);
199 if (looking_for & DBIT)
201 if ((looking_for & 5) != (thisnib & 5))
203 abs = (thisnib & 0x8) ? 2 : 1;
205 else if (looking_for & (REG | IND | INC | DEC))
207 if (looking_for & REG)
210 * Can work out size from the
213 size = bitfrom (looking_for);
215 if (looking_for & SRC)
224 else if (looking_for & L_16)
226 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
228 if (looking_for & (PCREL | DISP))
233 else if (looking_for & ABSJMP)
240 else if (looking_for & MEMIND)
244 else if (looking_for & L_32)
247 abs = (data[i] << 24)
248 | (data[i + 1] << 16)
254 else if (looking_for & L_24)
257 abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
260 else if (looking_for & IGNORE)
264 else if (looking_for & DISPREG)
266 rdisp = thisnib & 0x7;
268 else if (looking_for & KBIT)
283 else if (looking_for & L_8)
287 if (looking_for & PCREL)
289 abs = SEXTCHAR (data[len >> 1]);
291 else if (looking_for & ABS8MEM)
294 abs = h8300hmode ? ~0xff0000ff : ~0xffff00ff;
295 abs |= data[len >> 1] & 0xff ;
299 abs = data[len >> 1] & 0xff;
302 else if (looking_for & L_3)
308 else if (looking_for == E)
312 /* Fill in the args */
314 op_type *args = q->args.nib;
320 int rn = (x & DST) ? rd : rs;
334 p->type = X (OP_IMM, size);
337 else if (x & (IMM | KBIT | DBIT))
339 p->type = X (OP_IMM, size);
344 /* Reset the size, some
345 ops (like mul) have two sizes */
348 p->type = X (OP_REG, size);
353 p->type = X (OP_INC, size);
358 p->type = X (OP_DEC, size);
363 p->type = X (OP_DISP, size);
367 else if (x & (ABS | ABSJMP | ABS8MEM))
369 p->type = X (OP_DISP, size);
375 p->type = X (OP_MEM, size);
380 p->type = X (OP_PCREL, size);
381 p->literal = abs + addr + 2;
387 p->type = X (OP_IMM, SP);
392 p->type = X (OP_DISP, size);
394 p->reg = rdisp & 0x7;
401 printf ("Hmmmm %x", x);
408 * But a jmp or a jsr gets
409 * automagically lvalued, since we
410 * branch to their address not their
413 if (q->how == O (O_JSR, SB)
414 || q->how == O (O_JMP, SB))
416 dst->src.type = lvalue (dst->src.type, dst->src.reg);
419 if (dst->dst.type == -1)
422 dst->opcode = q->how;
423 dst->cycles = q->time;
425 /* And a jsr to 0xc4 is turned into a magic trap */
427 if (dst->opcode == O (O_JSR, SB))
429 if (dst->src.literal == 0xc4)
431 dst->opcode = O (O_SYSCALL, SB);
435 dst->next_pc = addr + len / 2;
440 printf ("Dont understand %x \n", looking_for);
452 dst->opcode = O (O_ILL, SB);
461 /* find the next cache entry to use */
463 idx = cpu.cache_top + 1;
465 if (idx >= cpu.csize)
471 /* Throw away its old meaning */
472 cpu.cache_idx[cpu.cache[idx].oldpc] = 0;
474 /* set to new address */
475 cpu.cache[idx].oldpc = pc;
477 /* fill in instruction info */
478 decode (pc, cpu.memory + pc, cpu.cache + idx);
480 /* point to new cache entry */
481 cpu.cache_idx[pc] = idx;
485 static unsigned char *breg[18];
486 static unsigned short *wreg[18];
487 static unsigned int *lreg[18];
489 #define GET_B_REG(x) *(breg[x])
490 #define SET_B_REG(x,y) (*(breg[x])) = (y)
491 #define GET_W_REG(x) *(wreg[x])
492 #define SET_W_REG(x,y) (*(wreg[x])) = (y)
494 #define GET_L_REG(x) *(lreg[x])
495 #define SET_L_REG(x,y) (*(lreg[x])) = (y)
497 #define GET_MEMORY_L(x) \
499 ? ((cpu.memory[x+0] << 24) | (cpu.memory[x+1] << 16) \
500 | (cpu.memory[x+2] << 8) | cpu.memory[x+3]) \
501 : ((cpu.eightbit[(x+0) & 0xff] << 24) | (cpu.eightbit[(x+1) & 0xff] << 16) \
502 | (cpu.eightbit[(x+2) & 0xff] << 8) | cpu.eightbit[(x+3) & 0xff]))
504 #define GET_MEMORY_W(x) \
506 ? ((cpu.memory[x+0] << 8) | (cpu.memory[x+1] << 0)) \
507 : ((cpu.eightbit[(x+0) & 0xff] << 8) | (cpu.eightbit[(x+1) & 0xff] << 0)))
510 #define GET_MEMORY_B(x) \
511 (x < memory_size ? (cpu.memory[x]) : (cpu.eightbit[x & 0xff]))
513 #define SET_MEMORY_L(x,y) \
514 { register unsigned char *_p; register int __y = y; \
515 _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
516 _p[0] = (__y)>>24; _p[1] = (__y)>>16; \
517 _p[2] = (__y)>>8; _p[3] = (__y)>>0;}
519 #define SET_MEMORY_W(x,y) \
520 { register unsigned char *_p; register int __y = y; \
521 _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
522 _p[0] = (__y)>>8; _p[1] =(__y);}
524 #define SET_MEMORY_B(x,y) \
525 (x < memory_size ? (cpu.memory[(x)] = y) : (cpu.eightbit[x & 0xff] = y))
532 int abs = arg->literal;
539 return GET_B_REG (rn);
541 return GET_W_REG (rn);
543 return GET_L_REG (rn);
554 r = GET_MEMORY_B (t);
563 r = GET_MEMORY_W (t);
571 r = GET_MEMORY_L (t);
578 case X (OP_DISP, SB):
579 t = GET_L_REG (rn) + abs;
581 return GET_MEMORY_B (t);
583 case X (OP_DISP, SW):
584 t = GET_L_REG (rn) + abs;
586 return GET_MEMORY_W (t);
588 case X (OP_DISP, SL):
589 t = GET_L_REG (rn) + abs;
591 return GET_MEMORY_L (t);
594 t = GET_MEMORY_L (abs);
599 t = GET_MEMORY_W (abs);
617 int abs = arg->literal;
633 t = GET_L_REG (rn) - 1;
640 t = (GET_L_REG (rn) - 2) & cpu.mask;
646 t = (GET_L_REG (rn) - 4) & cpu.mask;
651 case X (OP_DISP, SB):
652 t = GET_L_REG (rn) + abs;
657 case X (OP_DISP, SW):
658 t = GET_L_REG (rn) + abs;
663 case X (OP_DISP, SL):
664 t = GET_L_REG (rn) + abs;
701 memory_size = H8300H_MSIZE;
703 memory_size = H8300_MSIZE;
704 cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
705 cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
706 cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
708 /* `msize' must be a power of two */
709 if ((memory_size & (memory_size - 1)) != 0)
711 cpu.mask = memory_size - 1;
713 for (i = 0; i < 9; i++)
718 for (i = 0; i < 8; i++)
720 unsigned char *p = (unsigned char *) (cpu.regs + i);
721 unsigned char *e = (unsigned char *) (cpu.regs + i + 1);
722 unsigned short *q = (unsigned short *) (cpu.regs + i);
723 unsigned short *u = (unsigned short *) (cpu.regs + i + 1);
724 cpu.regs[i] = 0x00112233;
750 lreg[i] = &cpu.regs[i];
753 lreg[8] = &cpu.regs[8];
755 /* initialize the seg registers */
762 control_c (sig, code, scp, addr)
768 cpu.exception = SIGINT;
777 mop (code, bsize, sign)
790 bsize ? SEXTCHAR (GET_W_REG (code->dst.reg)) :
791 SEXTSHORT (GET_W_REG (code->dst.reg));
793 bsize ? SEXTCHAR (GET_B_REG (code->src.reg)) :
794 SEXTSHORT (GET_W_REG (code->src.reg));
798 multiplicand = bsize ? UEXTCHAR (GET_W_REG (code->dst.reg)) :
799 UEXTSHORT (GET_W_REG (code->dst.reg));
801 bsize ? UEXTCHAR (GET_B_REG (code->src.reg)) :
802 UEXTSHORT (GET_W_REG (code->src.reg));
805 result = multiplier * multiplicand;
809 n = result & (bsize ? 0x8000 : 0x80000000);
810 nz = result & (bsize ? 0xffff : 0xffffffff);
814 SET_W_REG (code->dst.reg, result);
818 SET_L_REG (code->dst.reg, result);
820 /* return ((n==1) << 1) | (nz==1); */
824 #define ONOT(name, how) \
829 rd = GET_B_REG (code->src.reg); \
837 rd = GET_W_REG (code->src.reg); \
844 int hm = 0x80000000; \
845 rd = GET_L_REG (code->src.reg); \
850 #define OSHIFTS(name, how1, how2) \
855 rd = GET_B_REG (code->src.reg); \
856 if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
870 rd = GET_W_REG (code->src.reg); \
871 if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
884 int hm = 0x80000000; \
885 rd = GET_L_REG (code->src.reg); \
886 if ((GET_MEMORY_B (pc + 1) & 0x40) == 0) \
897 #define OBITOP(name,f, s, op) \
902 if (f) ea = fetch (&code->dst); \
903 m=1<< fetch(&code->src); \
905 if(s) store (&code->dst,ea); goto next; \
909 sim_resume (step, siggnal)
914 int tick_start = get_now ();
927 prev = signal (SIGINT, control_c);
931 cpu.exception = SIGTRAP;
940 /* The PC should never be odd. */
954 cidx = cpu.cache_idx[pc];
955 code = cpu.cache + cidx;
958 #define ALUOP(STORE, NAME, HOW) \
959 case O(NAME,SB): HOW; if(STORE)goto alu8;else goto just_flags_alu8; \
960 case O(NAME, SW): HOW; if(STORE)goto alu16;else goto just_flags_alu16; \
961 case O(NAME,SL): HOW; if(STORE)goto alu32;else goto just_flags_alu32;
964 #define LOGOP(NAME, HOW) \
965 case O(NAME,SB): HOW; goto log8;\
966 case O(NAME, SW): HOW; goto log16;\
967 case O(NAME,SL): HOW; goto log32;
974 printf ("%x %d %s\n", pc, code->opcode,
975 code->op ? code->op->name : "**");
977 cpu.stats[code->opcode]++;
981 cycles += code->cycles;
983 switch (code->opcode)
987 * This opcode is a fake for when we get to an
988 * instruction which hasnt been compiled
996 rd = fetch (&code->dst);
997 ea = fetch (&code->src);
1002 case O (O_ADDX, SB):
1003 rd = fetch (&code->dst);
1004 ea = fetch (&code->src);
1009 #define EA ea = fetch(&code->src);
1010 #define RD_EA ea = fetch(&code->src); rd = fetch(&code->dst);
1012 ALUOP (1, O_SUB, RD_EA;
1015 ALUOP (1, O_NEG, EA;
1021 rd = GET_B_REG (code->dst.reg);
1022 ea = fetch (&code->src);
1026 rd = GET_W_REG (code->dst.reg);
1027 ea = fetch (&code->src);
1031 rd = GET_L_REG (code->dst.reg);
1032 ea = fetch (&code->src);
1037 LOGOP (O_AND, RD_EA;
1043 LOGOP (O_XOR, RD_EA;
1047 case O (O_MOV_TO_MEM, SB):
1048 res = GET_B_REG (code->src.reg);
1050 case O (O_MOV_TO_MEM, SW):
1051 res = GET_W_REG (code->src.reg);
1053 case O (O_MOV_TO_MEM, SL):
1054 res = GET_L_REG (code->src.reg);
1058 case O (O_MOV_TO_REG, SB):
1059 res = fetch (&code->src);
1060 SET_B_REG (code->dst.reg, res);
1061 goto just_flags_log8;
1062 case O (O_MOV_TO_REG, SW):
1063 res = fetch (&code->src);
1064 SET_W_REG (code->dst.reg, res);
1065 goto just_flags_log16;
1066 case O (O_MOV_TO_REG, SL):
1067 res = fetch (&code->src);
1068 SET_L_REG (code->dst.reg, res);
1069 goto just_flags_log32;
1072 case O (O_ADDS, SL):
1073 SET_L_REG (code->dst.reg,
1074 GET_L_REG (code->dst.reg)
1075 + code->src.literal);
1079 case O (O_SUBS, SL):
1080 SET_L_REG (code->dst.reg,
1081 GET_L_REG (code->dst.reg)
1082 - code->src.literal);
1086 rd = fetch (&code->dst);
1087 ea = fetch (&code->src);
1090 goto just_flags_alu8;
1093 rd = fetch (&code->dst);
1094 ea = fetch (&code->src);
1097 goto just_flags_alu16;
1100 rd = fetch (&code->dst);
1101 ea = fetch (&code->src);
1104 goto just_flags_alu32;
1108 rd = GET_B_REG (code->src.reg);
1111 SET_B_REG (code->src.reg, res);
1112 goto just_flags_inc8;
1115 rd = GET_W_REG (code->dst.reg);
1116 ea = -code->src.literal;
1118 SET_W_REG (code->dst.reg, res);
1119 goto just_flags_inc16;
1122 rd = GET_L_REG (code->dst.reg);
1123 ea = -code->src.literal;
1125 SET_L_REG (code->dst.reg, res);
1126 goto just_flags_inc32;
1130 rd = GET_B_REG (code->src.reg);
1133 SET_B_REG (code->src.reg, res);
1134 goto just_flags_inc8;
1137 rd = GET_W_REG (code->dst.reg);
1138 ea = code->src.literal;
1140 SET_W_REG (code->dst.reg, res);
1141 goto just_flags_inc16;
1144 rd = GET_L_REG (code->dst.reg);
1145 ea = code->src.literal;
1147 SET_L_REG (code->dst.reg, res);
1148 goto just_flags_inc32;
1151 #define GET_CCR(x) BUILDSR();x = cpu.ccr
1153 case O (O_ANDC, SB):
1155 ea = code->src.literal;
1161 ea = code->src.literal;
1165 case O (O_XORC, SB):
1167 ea = code->src.literal;
1208 if (((Z || (N ^ V)) == 0))
1214 if (((Z || (N ^ V)) == 1))
1248 case O (O_SYSCALL, SB):
1249 printf ("%c", cpu.regs[2]);
1252 ONOT (O_NOT, rd = ~rd; v = 0;);
1254 c = rd & hm; v = 0; rd <<= 1,
1255 c = rd & (hm >> 1); v = 0; rd <<= 2);
1257 c = rd & 1; v = 0; rd = (unsigned int) rd >> 1,
1258 c = rd & 2; v = 0; rd = (unsigned int) rd >> 2);
1260 c = rd & hm; v = (rd & hm) != ((rd & (hm >> 1)) << 1); rd <<= 1,
1261 c = rd & (hm >> 1); v = (rd & (hm >> 1)) != ((rd & (hm >> 2)) << 2); rd <<= 2);
1263 t = rd & hm; c = rd & 1; v = 0; rd >>= 1; rd |= t,
1264 t = rd & hm; c = rd & 2; v = 0; rd >>= 2; rd |= t | t >> 1 );
1266 c = rd & hm; v = 0; rd <<= 1; rd |= C,
1267 c = rd & (hm >> 1); v = 0; rd <<= 2; rd |= C);
1269 c = rd & 1; v = 0; rd = (unsigned int) rd >> 1; if (c) rd |= hm,
1270 c = rd & 2; v = 0; rd = (unsigned int) rd >> 2; if (c) rd |= hm);
1272 t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0,
1273 t = rd & (hm >> 1); rd <<= 2; rd |= C; c = t; v = 0);
1275 t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t; v = 0,
1276 t = rd & 2; rd = (unsigned int) rd >> 2; if (C) rd |= hm; c = t; v = 0);
1280 pc = fetch (&code->src);
1288 pc = fetch (&code->src);
1295 SET_MEMORY_L (tmp, code->next_pc);
1300 SET_MEMORY_W (tmp, code->next_pc);
1307 pc = code->src.literal;
1318 pc = GET_MEMORY_L (tmp);
1323 pc = GET_MEMORY_W (tmp);
1332 cpu.exception = SIGILL;
1334 case O (O_SLEEP, SB):
1335 /* The format of r0 is defined by devo/include/wait.h.
1336 cpu.exception handling needs some cleanup: we need to make the
1337 the handling of normal exits vs signals, etc. more sensible. */
1338 if (! WIFEXITED (cpu.regs[0]) && WIFSIGNALED (cpu.regs[0]))
1339 cpu.exception = SIGILL;
1341 cpu.exception = SIGTRAP;
1344 cpu.exception = SIGTRAP;
1347 OBITOP (O_BNOT, 1, 1, ea ^= m);
1348 OBITOP (O_BTST, 1, 0, nz = ea & m);
1349 OBITOP (O_BCLR, 1, 1, ea &= ~m);
1350 OBITOP (O_BSET, 1, 1, ea |= m);
1351 OBITOP (O_BLD, 1, 0, c = ea & m);
1352 OBITOP (O_BILD, 1, 0, c = !(ea & m));
1353 OBITOP (O_BST, 1, 1, ea &= ~m;
1355 OBITOP (O_BIST, 1, 1, ea &= ~m;
1357 OBITOP (O_BAND, 1, 0, c = (ea & m) && C);
1358 OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C);
1359 OBITOP (O_BOR, 1, 0, c = (ea & m) || C);
1360 OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C);
1361 OBITOP (O_BXOR, 1, 0, c = (ea & m) != C);
1362 OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C);
1365 #define MOP(bsize, signed) mop(code, bsize,signed); goto next;
1367 case O (O_MULS, SB):
1370 case O (O_MULS, SW):
1373 case O (O_MULU, SB):
1376 case O (O_MULU, SW):
1381 case O (O_DIVU, SB):
1383 rd = GET_W_REG (code->dst.reg);
1384 ea = GET_B_REG (code->src.reg);
1387 tmp = (unsigned)rd % ea;
1388 rd = (unsigned)rd / ea;
1390 SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
1396 case O (O_DIVU, SW):
1398 rd = GET_L_REG (code->dst.reg);
1399 ea = GET_W_REG (code->src.reg);
1404 tmp = (unsigned)rd % ea;
1405 rd = (unsigned)rd / ea;
1407 SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
1411 case O (O_DIVS, SB):
1414 rd = SEXTSHORT (GET_W_REG (code->dst.reg));
1415 ea = SEXTCHAR (GET_B_REG (code->src.reg));
1418 tmp = (int) rd % (int) ea;
1419 rd = (int) rd / (int) ea;
1425 SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
1428 case O (O_DIVS, SW):
1430 rd = GET_L_REG (code->dst.reg);
1431 ea = SEXTSHORT (GET_W_REG (code->src.reg));
1434 tmp = (int) rd % (int) ea;
1435 rd = (int) rd / (int) ea;
1436 n = rd & 0x80000000;
1441 SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
1444 case O (O_EXTS, SW):
1445 rd = GET_B_REG (code->src.reg + 8) & 0xff; /* Yes, src, not dst. */
1446 ea = rd & 0x80 ? -256 : 0;
1449 case O (O_EXTS, SL):
1450 rd = GET_W_REG (code->src.reg) & 0xffff;
1451 ea = rd & 0x8000 ? -65536 : 0;
1454 case O (O_EXTU, SW):
1455 rd = GET_B_REG (code->src.reg + 8) & 0xff;
1459 case O (O_EXTU, SL):
1460 rd = GET_W_REG (code->src.reg) & 0xffff;
1470 int nregs, firstreg, i;
1472 nregs = GET_MEMORY_B (pc + 1);
1475 firstreg = GET_MEMORY_B (pc + 3);
1477 for (i = firstreg; i <= firstreg + nregs; i++)
1480 SET_MEMORY_L (cpu.regs[7], cpu.regs[i]);
1487 int nregs, firstreg, i;
1489 nregs = GET_MEMORY_B (pc + 1);
1492 firstreg = GET_MEMORY_B (pc + 3);
1494 for (i = firstreg; i >= firstreg - nregs; i--)
1496 cpu.regs[i] = GET_MEMORY_L (cpu.regs[7]);
1503 cpu.exception = SIGILL;
1515 /* When a branch works */
1516 pc = code->src.literal;
1519 /* Set the cond codes from res */
1522 /* Set the flags after an 8 bit inc/dec operation */
1526 v = (rd & 0x7f) == 0x7f;
1530 /* Set the flags after an 16 bit inc/dec operation */
1534 v = (rd & 0x7fff) == 0x7fff;
1538 /* Set the flags after an 32 bit inc/dec operation */
1540 n = res & 0x80000000;
1541 nz = res & 0xffffffff;
1542 v = (rd & 0x7fffffff) == 0x7fffffff;
1547 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
1550 SET_B_REG (code->src.reg, rd);
1554 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
1557 SET_W_REG (code->src.reg, rd);
1561 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
1562 n = (rd & 0x80000000);
1563 nz = rd & 0xffffffff;
1564 SET_L_REG (code->src.reg, rd);
1568 store (&code->dst, res);
1570 /* flags after a 32bit logical operation */
1571 n = res & 0x80000000;
1572 nz = res & 0xffffffff;
1577 store (&code->dst, res);
1579 /* flags after a 16bit logical operation */
1587 store (&code->dst, res);
1595 SET_B_REG (code->dst.reg, res);
1600 switch (code->opcode / 4)
1603 v = ((rd & 0x80) == (ea & 0x80)
1604 && (rd & 0x80) != (res & 0x80));
1608 v = ((rd & 0x80) != (-ea & 0x80)
1609 && (rd & 0x80) != (res & 0x80));
1618 SET_W_REG (code->dst.reg, res);
1622 c = (res & 0x10000);
1623 switch (code->opcode / 4)
1626 v = ((rd & 0x8000) == (ea & 0x8000)
1627 && (rd & 0x8000) != (res & 0x8000));
1631 v = ((rd & 0x8000) != (-ea & 0x8000)
1632 && (rd & 0x8000) != (res & 0x8000));
1641 SET_L_REG (code->dst.reg, res);
1643 n = res & 0x80000000;
1644 nz = res & 0xffffffff;
1645 switch (code->opcode / 4)
1648 v = ((rd & 0x80000000) == (ea & 0x80000000)
1649 && (rd & 0x80000000) != (res & 0x80000000));
1650 c = ((unsigned) res < (unsigned) rd) || ((unsigned) res < (unsigned) ea);
1654 v = ((rd & 0x80000000) != (-ea & 0x80000000)
1655 && (rd & 0x80000000) != (res & 0x80000000));
1656 c = (unsigned) rd < (unsigned) -ea;
1659 v = (rd == 0x80000000);
1670 /* if (cpu.regs[8] ) abort(); */
1673 /* Poll after every 100th insn, */
1674 if (poll_count++ > 100)
1677 if (win32pollquit())
1683 #if defined(__GO32__)
1684 /* Poll after every 100th insn, */
1685 if (poll_count++ > 100)
1697 while (!cpu.exception);
1698 cpu.ticks += get_now () - tick_start;
1699 cpu.cycles += cycles;
1705 signal (SIGINT, prev);
1710 sim_write (addr, buffer, size)
1712 unsigned char *buffer;
1720 for (i = 0; i < size; i++)
1722 if (addr < memory_size)
1724 cpu.memory[addr + i] = buffer[i];
1725 cpu.cache_idx[addr + i] = 0;
1728 cpu.eightbit[(addr + i) & 0xff] = buffer[i];
1734 sim_read (addr, buffer, size)
1736 unsigned char *buffer;
1742 if (addr < memory_size)
1743 memcpy (buffer, cpu.memory + addr, size);
1745 memcpy (buffer, cpu.eightbit + (addr & 0xff), size);
1759 #define SP_REGNUM R7_REGNUM /* Contains address of top of stack */
1760 #define FP_REGNUM R6_REGNUM /* Contains address of executing
1763 #define CCR_REGNUM 8 /* Contains processor status */
1764 #define PC_REGNUM 9 /* Contains program counter */
1766 #define CYCLE_REGNUM 10
1767 #define INST_REGNUM 11
1768 #define TICK_REGNUM 12
1772 sim_store_register (rn, value)
1774 unsigned char *value;
1779 longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
1780 shortval = (value[0] << 8) | (value[1]);
1781 intval = h8300hmode ? longval : shortval;
1799 cpu.regs[rn] = intval;
1805 cpu.cycles = longval;
1809 cpu.insts = longval;
1813 cpu.ticks = longval;
1819 sim_fetch_register (rn, buf)
1861 if (h8300hmode || longreg)
1876 sim_stop_reason (reason, sigrc)
1877 enum sim_stop *reason;
1880 *reason = sim_stopped;
1881 *sigrc = cpu.exception;
1890 cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
1891 memset (cpu.cache, 0, sizeof (decoded_inst) * n);
1900 double timetaken = (double) cpu.ticks / (double) now_persec ();
1901 double virttime = cpu.cycles / 10.0e6;
1903 printf_filtered ("\n\n#instructions executed %10d\n", cpu.insts);
1904 printf_filtered ("#cycles (v approximate) %10d\n", cpu.cycles);
1905 printf_filtered ("#real time taken %10.4f\n", timetaken);
1906 printf_filtered ("#virtual time taked %10.4f\n", virttime);
1907 if (timetaken != 0.0)
1908 printf_filtered ("#simulation ratio %10.4f\n", virttime / timetaken);
1909 printf_filtered ("#compiles %10d\n", cpu.compiles);
1910 printf_filtered ("#cache size %10d\n", cpu.csize);
1916 for (i = 0; i < O_LAST; i++)
1919 printf_filtered ("%d: %d\n", i, cpu.stats[i]);
1925 /* Indicate whether the cpu is an h8/300 or h8/300h.
1926 FLAG is non-zero for the h8/300h. */
1949 sim_close (quitting)
1955 /* Called by gdb to load a program into memory. */
1958 sim_load (prog, from_tty)
1964 /* See if the file is for the h8/300 or h8/300h. */
1965 /* ??? This may not be the most efficient way. The z8k simulator
1966 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
1967 if ((abfd = bfd_openr (prog, "coff-h8300")) != 0)
1969 if (bfd_check_format (abfd, bfd_object))
1971 set_h8300h (abfd->arch_info->mach == bfd_mach_h8300h
1972 || abfd->arch_info->mach == bfd_mach_h8300s);
1977 /* If we're using gdb attached to the simulator, then we have to
1978 reallocate memory for the simulator.
1980 When gdb first starts, it calls fetch_registers (among other
1981 functions), which in turn calls init_pointers, which allocates
1984 The problem is when we do that, we don't know whether we're
1985 debugging an h8/300 or h8/300h program.
1987 This is the first point at which we can make that determination,
1988 so we just reallocate memory now; this will also allow us to handle
1989 switching between h8/300 and h8/300h programs without exiting
1992 memory_size = H8300H_MSIZE;
1994 memory_size = H8300_MSIZE;
1999 free (cpu.cache_idx);
2001 free (cpu.eightbit);
2003 cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
2004 cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
2005 cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
2007 /* `msize' must be a power of two */
2008 if ((memory_size & (memory_size - 1)) != 0)
2010 cpu.mask = memory_size - 1;
2012 /* Return non-zero so gdb will handle it. */
2017 sim_create_inferior (start_address, argv, env)
2018 SIM_ADDR start_address;
2022 cpu.pc = start_address;
2026 sim_do_command (cmd)
2029 printf_filtered ("This simulator does not accept any commands.\n");
2035 sim_set_callbacks (ptr)
2036 struct host_callback_struct *ptr;