2 * Simulator for the Renesas (formerly 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.
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
33 #include "gdb/sim-h8300.h"
35 #include "sys/types.h"
43 host_callback *sim_callback;
45 static SIM_OPEN_KIND sim_kind;
48 /* FIXME: Needs to live in header file.
49 This header should also include the things in remote-sim.h.
50 One could move this to remote-sim.h but this function isn't needed
52 static void set_simcache_size (SIM_DESC, int);
54 #define X(op, size) (op * 4 + size)
56 #define SP (h8300hmode && !h8300_normal_mode ? SL : SW)
58 #define h8_opcodes ops
60 #include "opcode/h8300.h"
62 /* CPU data object: */
65 sim_state_initialize (SIM_DESC sd, sim_cpu *cpu)
67 /* FIXME: not really necessary, since sim_cpu_alloc calls zalloc. */
69 memset (&cpu->regs, 0, sizeof(cpu->regs));
70 cpu->regs[SBR_REGNUM] = 0xFFFFFF00;
72 cpu->delayed_branch = 0;
77 /* Initialize local simulator state. */
79 sd->sim_cache_size = 0;
85 memset (&cpu->stats, 0, sizeof (cpu->stats));
91 h8_get_pc (SIM_DESC sd)
93 return (STATE_CPU (sd, 0)) -> pc;
97 h8_set_pc (SIM_DESC sd, unsigned int val)
99 (STATE_CPU (sd, 0)) -> pc = val;
103 h8_get_ccr (SIM_DESC sd)
105 return (STATE_CPU (sd, 0)) -> regs[CCR_REGNUM];
109 h8_set_ccr (SIM_DESC sd, unsigned int val)
111 (STATE_CPU (sd, 0)) -> regs[CCR_REGNUM] = val;
115 h8_get_exr (SIM_DESC sd)
117 return (STATE_CPU (sd, 0)) -> regs[EXR_REGNUM];
121 h8_set_exr (SIM_DESC sd, unsigned int val)
123 (STATE_CPU (sd, 0)) -> regs[EXR_REGNUM] = val;
127 h8_get_sbr (SIM_DESC sd)
129 return (STATE_CPU (sd, 0)) -> regs[SBR_REGNUM];
133 h8_set_sbr (SIM_DESC sd, int val)
135 (STATE_CPU (sd, 0)) -> regs[SBR_REGNUM] = val;
139 h8_get_vbr (SIM_DESC sd)
141 return (STATE_CPU (sd, 0)) -> regs[VBR_REGNUM];
145 h8_set_vbr (SIM_DESC sd, int val)
147 (STATE_CPU (sd, 0)) -> regs[VBR_REGNUM] = val;
151 h8_get_cache_top (SIM_DESC sd)
153 return sd -> cache_top;
157 h8_set_cache_top (SIM_DESC sd, int val)
159 sd -> cache_top = val;
163 h8_get_mask (SIM_DESC sd)
165 return (STATE_CPU (sd, 0)) -> mask;
169 h8_set_mask (SIM_DESC sd, int val)
171 (STATE_CPU (sd, 0)) -> mask = val;
175 h8_get_exception (SIM_DESC sd)
177 return (STATE_CPU (sd, 0)) -> exception;
181 h8_set_exception (SIM_DESC sd, int val)
183 (STATE_CPU (sd, 0)) -> exception = val;
186 static enum h8300_sim_state
187 h8_get_state (SIM_DESC sd)
193 h8_set_state (SIM_DESC sd, enum h8300_sim_state val)
199 h8_get_cycles (SIM_DESC sd)
201 return (STATE_CPU (sd, 0)) -> regs[CYCLE_REGNUM];
205 h8_set_cycles (SIM_DESC sd, unsigned int val)
207 (STATE_CPU (sd, 0)) -> regs[CYCLE_REGNUM] = val;
211 h8_get_insts (SIM_DESC sd)
213 return (STATE_CPU (sd, 0)) -> regs[INST_REGNUM];
217 h8_set_insts (SIM_DESC sd, unsigned int val)
219 (STATE_CPU (sd, 0)) -> regs[INST_REGNUM] = val;
223 h8_get_ticks (SIM_DESC sd)
225 return (STATE_CPU (sd, 0)) -> regs[TICK_REGNUM];
229 h8_set_ticks (SIM_DESC sd, unsigned int val)
231 (STATE_CPU (sd, 0)) -> regs[TICK_REGNUM] = val;
235 h8_get_mach (SIM_DESC sd)
237 return (STATE_CPU (sd, 0)) -> regs[MACH_REGNUM];
241 h8_set_mach (SIM_DESC sd, unsigned int val)
243 (STATE_CPU (sd, 0)) -> regs[MACH_REGNUM] = val;
247 h8_get_macl (SIM_DESC sd)
249 return (STATE_CPU (sd, 0)) -> regs[MACL_REGNUM];
253 h8_set_macl (SIM_DESC sd, unsigned int val)
255 (STATE_CPU (sd, 0)) -> regs[MACL_REGNUM] = val;
259 h8_get_compiles (SIM_DESC sd)
261 return sd -> compiles;
265 h8_increment_compiles (SIM_DESC sd)
270 static unsigned int *
271 h8_get_reg_buf (SIM_DESC sd)
273 return &(((STATE_CPU (sd, 0)) -> regs)[0]);
277 h8_get_reg (SIM_DESC sd, int regnum)
279 return (STATE_CPU (sd, 0)) -> regs[regnum];
283 h8_set_reg (SIM_DESC sd, int regnum, int val)
285 (STATE_CPU (sd, 0)) -> regs[regnum] = val;
290 h8_get_stats (SIM_DESC sd, int idx)
292 return sd -> stats[idx];
296 h8_increment_stats (SIM_DESC sd, int idx)
302 static unsigned short *
303 h8_get_cache_idx_buf (SIM_DESC sd)
305 return sd -> cache_idx;
309 h8_set_cache_idx_buf (SIM_DESC sd, unsigned short *ptr)
311 sd -> cache_idx = ptr;
314 static unsigned short
315 h8_get_cache_idx (SIM_DESC sd, unsigned int idx)
317 if (idx > sd->memory_size)
318 return (unsigned short) -1;
319 return sd -> cache_idx[idx];
323 h8_set_cache_idx (SIM_DESC sd, int idx, unsigned int val)
325 sd -> cache_idx[idx] = (unsigned short) val;
328 static unsigned char *
329 h8_get_memory_buf (SIM_DESC sd)
331 return (STATE_CPU (sd, 0)) -> memory;
335 h8_set_memory_buf (SIM_DESC sd, unsigned char *ptr)
337 (STATE_CPU (sd, 0)) -> memory = ptr;
341 h8_get_memory (SIM_DESC sd, int idx)
343 return (STATE_CPU (sd, 0)) -> memory[idx];
347 h8_set_memory (SIM_DESC sd, int idx, unsigned int val)
349 (STATE_CPU (sd, 0)) -> memory[idx] = (unsigned char) val;
352 static unsigned char *
353 h8_get_eightbit_buf (SIM_DESC sd)
355 return (STATE_CPU (sd, 0)) -> eightbit;
359 h8_set_eightbit_buf (SIM_DESC sd, unsigned char *ptr)
361 (STATE_CPU (sd, 0)) -> eightbit = ptr;
365 h8_get_eightbit (SIM_DESC sd, int idx)
367 return (STATE_CPU (sd, 0)) -> eightbit[idx];
371 h8_set_eightbit (SIM_DESC sd, int idx, unsigned int val)
373 (STATE_CPU (sd, 0)) -> eightbit[idx] = (unsigned char) val;
377 h8_get_delayed_branch (SIM_DESC sd)
379 return (STATE_CPU (sd, 0)) -> delayed_branch;
383 h8_set_delayed_branch (SIM_DESC sd, unsigned int dest)
385 (STATE_CPU (sd, 0)) -> delayed_branch = dest;
389 h8_get_command_line (SIM_DESC sd)
391 return (STATE_CPU (sd, 0)) -> command_line;
395 h8_set_command_line (SIM_DESC sd, char ** val)
397 (STATE_CPU (sd, 0)) -> command_line = val;
401 h8_get_cmdline_arg (SIM_DESC sd, int index)
403 return (STATE_CPU (sd, 0)) -> command_line[index];
407 h8_set_cmdline_arg (SIM_DESC sd, int index, char * val)
409 (STATE_CPU (sd, 0)) -> command_line[index] = val;
412 /* MAC Saturation Mode */
414 h8_get_macS (SIM_DESC sd)
416 return (STATE_CPU (sd, 0)) -> macS;
420 h8_set_macS (SIM_DESC sd, int val)
422 (STATE_CPU (sd, 0)) -> macS = (val != 0);
427 h8_get_macZ (SIM_DESC sd)
429 return (STATE_CPU (sd, 0)) -> macZ;
433 h8_set_macZ (SIM_DESC sd, int val)
435 (STATE_CPU (sd, 0)) -> macZ = (val != 0);
438 /* MAC Negative Flag */
440 h8_get_macN (SIM_DESC sd)
442 return (STATE_CPU (sd, 0)) -> macN;
446 h8_set_macN (SIM_DESC sd, int val)
448 (STATE_CPU (sd, 0)) -> macN = (val != 0);
451 /* MAC Overflow Flag */
453 h8_get_macV (SIM_DESC sd)
455 return (STATE_CPU (sd, 0)) -> macV;
459 h8_set_macV (SIM_DESC sd, int val)
461 (STATE_CPU (sd, 0)) -> macV = (val != 0);
464 /* End CPU data object. */
466 /* The rate at which to call the host's poll_quit callback. */
468 enum { POLL_QUIT_INTERVAL = 0x80000 };
470 #define LOW_BYTE(x) ((x) & 0xff)
471 #define HIGH_BYTE(x) (((x) >> 8) & 0xff)
472 #define P(X, Y) ((X << 8) | Y)
481 #define I (intMaskBit != 0)
483 #define BUILDSR(SD) \
484 h8_set_ccr (SD, (I << 7) | (UI << 6) | (H << 5) | (U << 4) \
485 | (N << 3) | (Z << 2) | (V << 1) | C)
488 /* Get Status Register (flags). */ \
489 c = (h8_get_ccr (sd) >> 0) & 1; \
490 v = (h8_get_ccr (sd) >> 1) & 1; \
491 nz = !((h8_get_ccr (sd) >> 2) & 1); \
492 n = (h8_get_ccr (sd) >> 3) & 1; \
493 u = (h8_get_ccr (sd) >> 4) & 1; \
494 h = (h8_get_ccr (sd) >> 5) & 1; \
495 ui = ((h8_get_ccr (sd) >> 6) & 1); \
496 intMaskBit = (h8_get_ccr (sd) >> 7) & 1
499 #ifdef __CHAR_IS_SIGNED__
500 #define SEXTCHAR(x) ((char) (x))
504 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff) : x & 0xff)
507 #define UEXTCHAR(x) ((x) & 0xff)
508 #define UEXTSHORT(x) ((x) & 0xffff)
509 #define SEXTSHORT(x) ((short) (x))
513 int h8300_normal_mode = 0;
516 static int memory_size;
521 return time (0); /* WinXX HAS UNIX like 'time', so why not use it? */
543 return (h8300hmode && !h8300_normal_mode)? SL : SW;
548 /* Simulate an indirection / dereference.
549 return 0 for success, -1 for failure.
553 lvalue (SIM_DESC sd, int x, int rn, unsigned int *val)
555 if (val == NULL) /* Paranoia. */
561 if (rn == ZERO_REGNUM)
562 *val = X (OP_IMM, SP);
564 *val = X (OP_REG, SP);
567 *val = X (OP_MEM, SP);
570 sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
579 if (h8300smode && !h8300_normal_mode)
581 else if (h8300hmode && !h8300_normal_mode)
588 decode (SIM_DESC sd, int addr, unsigned char *data, decoded_inst *dst)
590 int cst[3] = {0, 0, 0};
591 int reg[3] = {0, 0, 0};
592 int rdisp[3] = {0, 0, 0};
594 const struct h8_opcode *q;
599 /* Find the exact opcode/arg combo. */
600 for (q = h8_opcodes; q->name; q++)
602 op_type *nib = q->data.nib;
603 unsigned int len = 0;
605 if ((q->available == AV_H8SX && !h8300sxmode) ||
606 (q->available == AV_H8S && !h8300smode) ||
607 (q->available == AV_H8H && !h8300hmode))
610 cst[0] = cst[1] = cst[2] = 0;
611 reg[0] = reg[1] = reg[2] = 0;
612 rdisp[0] = rdisp[1] = rdisp[2] = 0;
616 op_type looking_for = *nib;
617 int thisnib = data[len / 2];
619 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
620 opnum = ((looking_for & OP3) ? 2 :
621 (looking_for & DST) ? 1 : 0);
623 if (looking_for < 16 && looking_for >= 0)
625 if (looking_for != thisnib)
630 if (looking_for & B31)
632 if (!((thisnib & 0x8) != 0))
635 looking_for = (op_type) (looking_for & ~B31);
638 else if (looking_for & B30)
640 if (!((thisnib & 0x8) == 0))
643 looking_for = (op_type) (looking_for & ~B30);
646 if (looking_for & B21)
648 if (!((thisnib & 0x4) != 0))
651 looking_for = (op_type) (looking_for & ~B21);
654 else if (looking_for & B20)
656 if (!((thisnib & 0x4) == 0))
659 looking_for = (op_type) (looking_for & ~B20);
662 if (looking_for & B11)
664 if (!((thisnib & 0x2) != 0))
667 looking_for = (op_type) (looking_for & ~B11);
670 else if (looking_for & B10)
672 if (!((thisnib & 0x2) == 0))
675 looking_for = (op_type) (looking_for & ~B10);
678 if (looking_for & B01)
680 if (!((thisnib & 0x1) != 0))
683 looking_for = (op_type) (looking_for & ~B01);
686 else if (looking_for & B00)
688 if (!((thisnib & 0x1) == 0))
691 looking_for = (op_type) (looking_for & ~B00);
694 if (looking_for & IGNORE)
696 /* Hitachi has declared that IGNORE must be zero. */
700 else if ((looking_for & MODE) == DATA)
702 ; /* Skip embedded data. */
704 else if ((looking_for & MODE) == DBIT)
706 /* Exclude adds/subs by looking at bit 0 and 2, and
707 make sure the operand size, either w or l,
708 matches by looking at bit 1. */
709 if ((looking_for & 7) != (thisnib & 7))
712 cst[opnum] = (thisnib & 0x8) ? 2 : 1;
714 else if ((looking_for & MODE) == REG ||
715 (looking_for & MODE) == LOWREG ||
716 (looking_for & MODE) == IND ||
717 (looking_for & MODE) == PREINC ||
718 (looking_for & MODE) == POSTINC ||
719 (looking_for & MODE) == PREDEC ||
720 (looking_for & MODE) == POSTDEC)
722 reg[opnum] = thisnib;
724 else if (looking_for & CTRL)
727 if (((looking_for & MODE) == CCR && (thisnib != C_CCR)) ||
728 ((looking_for & MODE) == EXR && (thisnib != C_EXR)) ||
729 ((looking_for & MODE) == MACH && (thisnib != C_MACH)) ||
730 ((looking_for & MODE) == MACL && (thisnib != C_MACL)) ||
731 ((looking_for & MODE) == VBR && (thisnib != C_VBR)) ||
732 ((looking_for & MODE) == SBR && (thisnib != C_SBR)))
734 if (((looking_for & MODE) == CCR_EXR &&
735 (thisnib != C_CCR && thisnib != C_EXR)) ||
736 ((looking_for & MODE) == VBR_SBR &&
737 (thisnib != C_VBR && thisnib != C_SBR)) ||
738 ((looking_for & MODE) == MACREG &&
739 (thisnib != C_MACH && thisnib != C_MACL)))
741 if (((looking_for & MODE) == CC_EX_VB_SB &&
742 (thisnib != C_CCR && thisnib != C_EXR &&
743 thisnib != C_VBR && thisnib != C_SBR)))
746 reg[opnum] = thisnib;
748 else if ((looking_for & MODE) == ABS)
750 /* Absolute addresses are unsigned. */
751 switch (looking_for & SIZE)
754 cst[opnum] = UEXTCHAR (data[len / 2]);
758 cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
762 (data[len / 2 + 0] << 24) +
763 (data[len / 2 + 1] << 16) +
764 (data[len / 2 + 2] << 8) +
768 printf ("decode: bad size ABS: %d\n",
769 (looking_for & SIZE));
773 else if ((looking_for & MODE) == DISP ||
774 (looking_for & MODE) == PCREL ||
775 (looking_for & MODE) == INDEXB ||
776 (looking_for & MODE) == INDEXW ||
777 (looking_for & MODE) == INDEXL)
779 switch (looking_for & SIZE)
782 cst[opnum] = thisnib & 3;
785 cst[opnum] = SEXTCHAR (data[len / 2]);
788 cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
789 cst[opnum] = (short) cst[opnum]; /* Sign extend. */
792 cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
796 (data[len / 2 + 0] << 24) +
797 (data[len / 2 + 1] << 16) +
798 (data[len / 2 + 2] << 8) +
802 printf ("decode: bad size DISP/PCREL/INDEX: %d\n",
803 (looking_for & SIZE));
807 else if ((looking_for & SIZE) == L_16 ||
808 (looking_for & SIZE) == L_16U)
810 cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
811 /* Immediates are always unsigned. */
812 if ((looking_for & SIZE) != L_16U &&
813 (looking_for & MODE) != IMM)
814 cst[opnum] = (short) cst[opnum]; /* Sign extend. */
816 else if (looking_for & ABSJMP)
818 switch (looking_for & SIZE) {
820 cst[opnum] = (data[1] << 16) | (data[2] << 8) | (data[3]);
824 (data[len / 2 + 0] << 24) +
825 (data[len / 2 + 1] << 16) +
826 (data[len / 2 + 2] << 8) +
830 printf ("decode: bad size ABSJMP: %d\n",
831 (looking_for & SIZE));
835 else if ((looking_for & MODE) == MEMIND)
837 cst[opnum] = data[1];
839 else if ((looking_for & MODE) == VECIND)
841 if(h8300_normal_mode)
842 cst[opnum] = ((data[1] & 0x7f) + 0x80) * 2;
844 cst[opnum] = ((data[1] & 0x7f) + 0x80) * 4;
845 cst[opnum] += h8_get_vbr (sd); /* Add vector base reg. */
847 else if ((looking_for & SIZE) == L_32)
852 (data[i + 0] << 24) |
853 (data[i + 1] << 16) |
857 else if ((looking_for & SIZE) == L_24)
862 (data[i + 0] << 16) |
866 else if (looking_for & DISPREG)
868 rdisp[opnum] = thisnib & 0x7;
870 else if ((looking_for & MODE) == KBIT)
887 else if ((looking_for & SIZE) == L_8)
889 if ((looking_for & MODE) == ABS)
891 /* Will be combined with contents of SBR_REGNUM
892 by fetch (). For all modes except h8sx, this
893 will always contain the value 0xFFFFFF00. */
894 cst[opnum] = data[len / 2] & 0xff;
898 cst[opnum] = data[len / 2] & 0xff;
901 else if ((looking_for & SIZE) == L_2)
903 cst[opnum] = thisnib & 3;
905 else if ((looking_for & SIZE) == L_3 ||
906 (looking_for & SIZE) == L_3NZ)
908 cst[opnum] = thisnib & 7;
909 if (cst[opnum] == 0 && (looking_for & SIZE) == L_3NZ)
912 else if ((looking_for & SIZE) == L_4)
914 cst[opnum] = thisnib & 15;
916 else if ((looking_for & SIZE) == L_5)
918 cst[opnum] = data[len / 2] & 0x1f;
920 else if (looking_for == E)
925 /* Fill in the args. */
927 op_type *args = q->args.nib;
932 nargs < 3 && *args != E;
938 opnum = ((x & OP3) ? 2 :
947 if ((x & MODE) == IMM ||
948 (x & MODE) == KBIT ||
951 /* Use the instruction to determine
953 p->type = X (OP_IMM, OP_SIZE (q->how));
954 p->literal = cst[opnum];
956 else if ((x & MODE) == CONST_2 ||
957 (x & MODE) == CONST_4 ||
958 (x & MODE) == CONST_8 ||
959 (x & MODE) == CONST_16)
961 /* Use the instruction to determine
963 p->type = X (OP_IMM, OP_SIZE (q->how));
965 case CONST_2: p->literal = 2; break;
966 case CONST_4: p->literal = 4; break;
967 case CONST_8: p->literal = 8; break;
968 case CONST_16: p->literal = 16; break;
971 else if ((x & MODE) == REG)
973 p->type = X (OP_REG, bitfrom (x));
976 else if ((x & MODE) == LOWREG)
978 p->type = X (OP_LOWREG, bitfrom (x));
981 else if ((x & MODE) == PREINC)
983 /* Use the instruction to determine
985 p->type = X (OP_PREINC, OP_SIZE (q->how));
986 p->reg = reg[opnum] & 0x7;
988 else if ((x & MODE) == POSTINC)
990 /* Use the instruction to determine
992 p->type = X (OP_POSTINC, OP_SIZE (q->how));
993 p->reg = reg[opnum] & 0x7;
995 else if ((x & MODE) == PREDEC)
997 /* Use the instruction to determine
999 p->type = X (OP_PREDEC, OP_SIZE (q->how));
1000 p->reg = reg[opnum] & 0x7;
1002 else if ((x & MODE) == POSTDEC)
1004 /* Use the instruction to determine
1005 the operand size. */
1006 p->type = X (OP_POSTDEC, OP_SIZE (q->how));
1007 p->reg = reg[opnum] & 0x7;
1009 else if ((x & MODE) == IND)
1011 /* Note: an indirect is transformed into
1012 a displacement of zero.
1014 /* Use the instruction to determine
1015 the operand size. */
1016 p->type = X (OP_DISP, OP_SIZE (q->how));
1017 p->reg = reg[opnum] & 0x7;
1019 if (OP_KIND (q->how) == O_JSR ||
1020 OP_KIND (q->how) == O_JMP)
1021 if (lvalue (sd, p->type, p->reg, &p->type))
1024 else if ((x & MODE) == ABS)
1026 /* Note: a 16 or 32 bit ABS is transformed into a
1027 displacement from pseudo-register ZERO_REGNUM,
1028 which is always zero. An 8 bit ABS becomes
1029 a displacement from SBR_REGNUM.
1031 /* Use the instruction to determine
1032 the operand size. */
1033 p->type = X (OP_DISP, OP_SIZE (q->how));
1034 p->literal = cst[opnum];
1036 /* 8-bit ABS is displacement from SBR.
1037 16 and 32-bit ABS are displacement from ZERO.
1038 (SBR will always be zero except for h8/sx)
1040 if ((x & SIZE) == L_8)
1041 p->reg = SBR_REGNUM;
1043 p->reg = ZERO_REGNUM;;
1045 else if ((x & MODE) == MEMIND ||
1046 (x & MODE) == VECIND)
1048 /* Size doesn't matter. */
1049 p->type = X (OP_MEM, SB);
1050 p->literal = cst[opnum];
1051 if (OP_KIND (q->how) == O_JSR ||
1052 OP_KIND (q->how) == O_JMP)
1053 if (lvalue (sd, p->type, p->reg, &p->type))
1056 else if ((x & MODE) == PCREL)
1058 /* Size doesn't matter. */
1059 p->type = X (OP_PCREL, SB);
1060 p->literal = cst[opnum];
1062 else if (x & ABSJMP)
1064 p->type = X (OP_IMM, SP);
1065 p->literal = cst[opnum];
1067 else if ((x & MODE) == INDEXB)
1069 p->type = X (OP_INDEXB, OP_SIZE (q->how));
1070 p->literal = cst[opnum];
1071 p->reg = rdisp[opnum];
1073 else if ((x & MODE) == INDEXW)
1075 p->type = X (OP_INDEXW, OP_SIZE (q->how));
1076 p->literal = cst[opnum];
1077 p->reg = rdisp[opnum];
1079 else if ((x & MODE) == INDEXL)
1081 p->type = X (OP_INDEXL, OP_SIZE (q->how));
1082 p->literal = cst[opnum];
1083 p->reg = rdisp[opnum];
1085 else if ((x & MODE) == DISP)
1087 /* Yuck -- special for mova args. */
1088 if (strncmp (q->name, "mova", 4) == 0 &&
1091 /* Mova can have a DISP2 dest, with an
1092 INDEXB or INDEXW src. The multiplier
1093 for the displacement value is determined
1094 by the src operand, not by the insn. */
1096 switch (OP_KIND (dst->src.type))
1099 p->type = X (OP_DISP, SB);
1100 p->literal = cst[opnum];
1103 p->type = X (OP_DISP, SW);
1104 p->literal = cst[opnum] * 2;
1112 p->type = X (OP_DISP, OP_SIZE (q->how));
1113 p->literal = cst[opnum];
1114 /* DISP2 is special. */
1115 if ((x & SIZE) == L_2)
1116 switch (OP_SIZE (q->how))
1119 case SW: p->literal *= 2; break;
1120 case SL: p->literal *= 4; break;
1123 p->reg = rdisp[opnum];
1130 p->type = X (OP_CCR, SB);
1133 p->type = X (OP_EXR, SB);
1136 p->type = X (OP_MACH, SL);
1139 p->type = X (OP_MACL, SL);
1142 p->type = X (OP_VBR, SL);
1145 p->type = X (OP_SBR, SL);
1149 else if ((x & MODE) == CCR)
1153 else if ((x & MODE) == EXR)
1158 printf ("Hmmmm 0x%x...\n", x);
1164 /* Unary operators: treat src and dst as equivalent. */
1165 if (dst->dst.type == -1)
1166 dst->dst = dst->src;
1167 if (dst->src.type == -1)
1168 dst->src = dst->dst;
1170 dst->opcode = q->how;
1171 dst->cycles = q->time;
1173 /* And jsr's to these locations are turned into
1176 if (OP_KIND (dst->opcode) == O_JSR)
1178 switch (dst->src.literal)
1181 dst->opcode = O (O_SYS_OPEN, SB);
1184 dst->opcode = O (O_SYS_READ, SB);
1187 dst->opcode = O (O_SYS_WRITE, SB);
1190 dst->opcode = O (O_SYS_LSEEK, SB);
1193 dst->opcode = O (O_SYS_CLOSE, SB);
1196 dst->opcode = O (O_SYS_STAT, SB);
1199 dst->opcode = O (O_SYS_FSTAT, SB);
1202 dst->opcode = O (O_SYS_CMDLINE, SB);
1205 /* End of Processing for system calls. */
1208 dst->next_pc = addr + len / 2;
1212 printf ("Don't understand 0x%x \n", looking_for);
1223 /* Fell off the end. */
1224 dst->opcode = O (O_ILL, SB);
1228 compile (SIM_DESC sd, int pc)
1232 /* Find the next cache entry to use. */
1233 idx = h8_get_cache_top (sd) + 1;
1234 h8_increment_compiles (sd);
1235 if (idx >= sd->sim_cache_size)
1239 h8_set_cache_top (sd, idx);
1241 /* Throw away its old meaning. */
1242 h8_set_cache_idx (sd, sd->sim_cache[idx].oldpc, 0);
1244 /* Set to new address. */
1245 sd->sim_cache[idx].oldpc = pc;
1247 /* Fill in instruction info. */
1248 decode (sd, pc, h8_get_memory_buf (sd) + pc, sd->sim_cache + idx);
1250 /* Point to new cache entry. */
1251 h8_set_cache_idx (sd, pc, idx);
1255 static unsigned char *breg[32];
1256 static unsigned short *wreg[16];
1257 static unsigned int *lreg[18];
1259 #define GET_B_REG(X) *(breg[X])
1260 #define SET_B_REG(X, Y) (*(breg[X])) = (Y)
1261 #define GET_W_REG(X) *(wreg[X])
1262 #define SET_W_REG(X, Y) (*(wreg[X])) = (Y)
1263 #define GET_L_REG(X) h8_get_reg (sd, X)
1264 #define SET_L_REG(X, Y) h8_set_reg (sd, X, Y)
1266 #define GET_MEMORY_L(X) \
1267 ((X) < memory_size \
1268 ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16) \
1269 | (h8_get_memory (sd, (X)+2) << 8) | (h8_get_memory (sd, (X)+3) << 0)) \
1270 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 24) \
1271 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 16) \
1272 | (h8_get_eightbit (sd, ((X)+2) & 0xff) << 8) \
1273 | (h8_get_eightbit (sd, ((X)+3) & 0xff) << 0)))
1275 #define GET_MEMORY_W(X) \
1276 ((X) < memory_size \
1277 ? ((h8_get_memory (sd, (X)+0) << 8) \
1278 | (h8_get_memory (sd, (X)+1) << 0)) \
1279 : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 8) \
1280 | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 0)))
1283 #define GET_MEMORY_B(X) \
1284 ((X) < memory_size ? (h8_get_memory (sd, (X))) \
1285 : (h8_get_eightbit (sd, (X) & 0xff)))
1287 #define SET_MEMORY_L(X, Y) \
1288 { register unsigned char *_p; register int __y = (Y); \
1289 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1290 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1291 _p[0] = __y >> 24; _p[1] = __y >> 16; \
1292 _p[2] = __y >> 8; _p[3] = __y >> 0; \
1295 #define SET_MEMORY_W(X, Y) \
1296 { register unsigned char *_p; register int __y = (Y); \
1297 _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
1298 h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1299 _p[0] = __y >> 8; _p[1] = __y; \
1302 #define SET_MEMORY_B(X, Y) \
1303 ((X) < memory_size ? (h8_set_memory (sd, (X), (Y))) \
1304 : (h8_set_eightbit (sd, (X) & 0xff, (Y))))
1306 /* Simulate a memory fetch.
1307 Return 0 for success, -1 for failure.
1311 fetch_1 (SIM_DESC sd, ea_type *arg, int *val, int twice)
1314 int abs = arg->literal;
1319 return -1; /* Paranoia. */
1323 /* Indexed register plus displacement mode:
1325 This new family of addressing modes are similar to OP_DISP
1326 (register plus displacement), with two differences:
1327 1) INDEXB uses only the least significant byte of the register,
1328 INDEXW uses only the least significant word, and
1329 INDEXL uses the entire register (just like OP_DISP).
1331 2) The displacement value in abs is multiplied by two
1332 for SW-sized operations, and by four for SL-size.
1334 This gives nine possible variations.
1337 case X (OP_INDEXB, SB):
1338 case X (OP_INDEXB, SW):
1339 case X (OP_INDEXB, SL):
1340 case X (OP_INDEXW, SB):
1341 case X (OP_INDEXW, SW):
1342 case X (OP_INDEXW, SL):
1343 case X (OP_INDEXL, SB):
1344 case X (OP_INDEXL, SW):
1345 case X (OP_INDEXL, SL):
1347 switch (OP_KIND (arg->type)) {
1348 case OP_INDEXB: t &= 0xff; break;
1349 case OP_INDEXW: t &= 0xffff; break;
1353 switch (OP_SIZE (arg->type)) {
1355 *val = GET_MEMORY_B ((t * 1 + abs) & h8_get_mask (sd));
1358 *val = GET_MEMORY_W ((t * 2 + abs) & h8_get_mask (sd));
1361 *val = GET_MEMORY_L ((t * 4 + abs) & h8_get_mask (sd));
1366 case X (OP_LOWREG, SB):
1367 *val = GET_L_REG (rn) & 0xff;
1369 case X (OP_LOWREG, SW):
1370 *val = GET_L_REG (rn) & 0xffff;
1373 case X (OP_REG, SB): /* Register direct, byte. */
1374 *val = GET_B_REG (rn);
1376 case X (OP_REG, SW): /* Register direct, word. */
1377 *val = GET_W_REG (rn);
1379 case X (OP_REG, SL): /* Register direct, long. */
1380 *val = GET_L_REG (rn);
1382 case X (OP_IMM, SB): /* Immediate, byte. */
1383 case X (OP_IMM, SW): /* Immediate, word. */
1384 case X (OP_IMM, SL): /* Immediate, long. */
1387 case X (OP_POSTINC, SB): /* Register indirect w/post-incr: byte. */
1389 t &= h8_get_mask (sd);
1390 r = GET_MEMORY_B (t);
1393 t = t & h8_get_mask (sd);
1397 case X (OP_POSTINC, SW): /* Register indirect w/post-incr: word. */
1399 t &= h8_get_mask (sd);
1400 r = GET_MEMORY_W (t);
1403 t = t & h8_get_mask (sd);
1407 case X (OP_POSTINC, SL): /* Register indirect w/post-incr: long. */
1409 t &= h8_get_mask (sd);
1410 r = GET_MEMORY_L (t);
1413 t = t & h8_get_mask (sd);
1418 case X (OP_POSTDEC, SB): /* Register indirect w/post-decr: byte. */
1420 t &= h8_get_mask (sd);
1421 r = GET_MEMORY_B (t);
1424 t = t & h8_get_mask (sd);
1428 case X (OP_POSTDEC, SW): /* Register indirect w/post-decr: word. */
1430 t &= h8_get_mask (sd);
1431 r = GET_MEMORY_W (t);
1434 t = t & h8_get_mask (sd);
1438 case X (OP_POSTDEC, SL): /* Register indirect w/post-decr: long. */
1440 t &= h8_get_mask (sd);
1441 r = GET_MEMORY_L (t);
1444 t = t & h8_get_mask (sd);
1449 case X (OP_PREDEC, SB): /* Register indirect w/pre-decr: byte. */
1450 t = GET_L_REG (rn) - 1;
1451 t &= h8_get_mask (sd);
1453 *val = GET_MEMORY_B (t);
1456 case X (OP_PREDEC, SW): /* Register indirect w/pre-decr: word. */
1457 t = GET_L_REG (rn) - 2;
1458 t &= h8_get_mask (sd);
1460 *val = GET_MEMORY_W (t);
1463 case X (OP_PREDEC, SL): /* Register indirect w/pre-decr: long. */
1464 t = GET_L_REG (rn) - 4;
1465 t &= h8_get_mask (sd);
1467 *val = GET_MEMORY_L (t);
1470 case X (OP_PREINC, SB): /* Register indirect w/pre-incr: byte. */
1471 t = GET_L_REG (rn) + 1;
1472 t &= h8_get_mask (sd);
1474 *val = GET_MEMORY_B (t);
1477 case X (OP_PREINC, SW): /* Register indirect w/pre-incr: long. */
1478 t = GET_L_REG (rn) + 2;
1479 t &= h8_get_mask (sd);
1481 *val = GET_MEMORY_W (t);
1484 case X (OP_PREINC, SL): /* Register indirect w/pre-incr: long. */
1485 t = GET_L_REG (rn) + 4;
1486 t &= h8_get_mask (sd);
1488 *val = GET_MEMORY_L (t);
1491 case X (OP_DISP, SB): /* Register indirect w/displacement: byte. */
1492 t = GET_L_REG (rn) + abs;
1493 t &= h8_get_mask (sd);
1494 *val = GET_MEMORY_B (t);
1497 case X (OP_DISP, SW): /* Register indirect w/displacement: word. */
1498 t = GET_L_REG (rn) + abs;
1499 t &= h8_get_mask (sd);
1500 *val = GET_MEMORY_W (t);
1503 case X (OP_DISP, SL): /* Register indirect w/displacement: long. */
1504 t = GET_L_REG (rn) + abs;
1505 t &= h8_get_mask (sd);
1506 *val =GET_MEMORY_L (t);
1509 case X (OP_MEM, SL): /* Absolute memory address, long. */
1510 t = GET_MEMORY_L (abs);
1511 t &= h8_get_mask (sd);
1515 case X (OP_MEM, SW): /* Absolute memory address, word. */
1516 t = GET_MEMORY_W (abs);
1517 t &= h8_get_mask (sd);
1521 case X (OP_PCREL, SB): /* PC relative (for jump, branch etc). */
1522 case X (OP_PCREL, SW):
1523 case X (OP_PCREL, SL):
1524 case X (OP_PCREL, SN):
1528 case X (OP_MEM, SB): /* Why isn't this implemented? */
1530 sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
1533 return 0; /* Success. */
1539 fetch (SIM_DESC sd, ea_type *arg, int *val)
1541 return fetch_1 (sd, arg, val, 0);
1544 /* Fetch which will be followed by a store to the same location.
1545 The difference being that we don't want to do a post-increment
1546 or post-decrement at this time: we'll do it when we store. */
1549 fetch2 (SIM_DESC sd, ea_type *arg, int *val)
1551 return fetch_1 (sd, arg, val, 1);
1554 /* Simulate a memory store.
1555 Return 0 for success, -1 for failure.
1559 store_1 (SIM_DESC sd, ea_type *arg, int n, int twice)
1562 int abs = arg->literal;
1567 /* Indexed register plus displacement mode:
1569 This new family of addressing modes are similar to OP_DISP
1570 (register plus displacement), with two differences:
1571 1) INDEXB uses only the least significant byte of the register,
1572 INDEXW uses only the least significant word, and
1573 INDEXL uses the entire register (just like OP_DISP).
1575 2) The displacement value in abs is multiplied by two
1576 for SW-sized operations, and by four for SL-size.
1578 This gives nine possible variations.
1581 case X (OP_INDEXB, SB):
1582 case X (OP_INDEXB, SW):
1583 case X (OP_INDEXB, SL):
1584 case X (OP_INDEXW, SB):
1585 case X (OP_INDEXW, SW):
1586 case X (OP_INDEXW, SL):
1587 case X (OP_INDEXL, SB):
1588 case X (OP_INDEXL, SW):
1589 case X (OP_INDEXL, SL):
1591 switch (OP_KIND (arg->type)) {
1592 case OP_INDEXB: t &= 0xff; break;
1593 case OP_INDEXW: t &= 0xffff; break;
1597 switch (OP_SIZE (arg->type)) {
1599 SET_MEMORY_B ((t * 1 + abs) & h8_get_mask (sd), n);
1602 SET_MEMORY_W ((t * 2 + abs) & h8_get_mask (sd), n);
1605 SET_MEMORY_L ((t * 4 + abs) & h8_get_mask (sd), n);
1610 case X (OP_REG, SB): /* Register direct, byte. */
1613 case X (OP_REG, SW): /* Register direct, word. */
1616 case X (OP_REG, SL): /* Register direct, long. */
1620 case X (OP_PREDEC, SB): /* Register indirect w/pre-decr, byte. */
1624 t &= h8_get_mask (sd);
1626 SET_MEMORY_B (t, n);
1629 case X (OP_PREDEC, SW): /* Register indirect w/pre-decr, word. */
1633 t &= h8_get_mask (sd);
1635 SET_MEMORY_W (t, n);
1638 case X (OP_PREDEC, SL): /* Register indirect w/pre-decr, long. */
1642 t &= h8_get_mask (sd);
1644 SET_MEMORY_L (t, n);
1647 case X (OP_PREINC, SB): /* Register indirect w/pre-incr, byte. */
1651 t &= h8_get_mask (sd);
1653 SET_MEMORY_B (t, n);
1656 case X (OP_PREINC, SW): /* Register indirect w/pre-incr, word. */
1660 t &= h8_get_mask (sd);
1662 SET_MEMORY_W (t, n);
1665 case X (OP_PREINC, SL): /* Register indirect w/pre-incr, long. */
1669 t &= h8_get_mask (sd);
1671 SET_MEMORY_L (t, n);
1674 case X (OP_POSTDEC, SB): /* Register indirect w/post-decr, byte. */
1675 t = GET_L_REG (rn) & h8_get_mask (sd);
1676 SET_MEMORY_B (t, n);
1677 SET_L_REG (rn, t - 1);
1680 case X (OP_POSTDEC, SW): /* Register indirect w/post-decr, word. */
1681 t = GET_L_REG (rn) & h8_get_mask (sd);
1682 SET_MEMORY_W (t, n);
1683 SET_L_REG (rn, t - 2);
1686 case X (OP_POSTDEC, SL): /* Register indirect w/post-decr, long. */
1687 t = GET_L_REG (rn) & h8_get_mask (sd);
1688 SET_MEMORY_L (t, n);
1689 SET_L_REG (rn, t - 4);
1692 case X (OP_POSTINC, SB): /* Register indirect w/post-incr, byte. */
1693 t = GET_L_REG (rn) & h8_get_mask (sd);
1694 SET_MEMORY_B (t, n);
1695 SET_L_REG (rn, t + 1);
1698 case X (OP_POSTINC, SW): /* Register indirect w/post-incr, word. */
1699 t = GET_L_REG (rn) & h8_get_mask (sd);
1700 SET_MEMORY_W (t, n);
1701 SET_L_REG (rn, t + 2);
1704 case X (OP_POSTINC, SL): /* Register indirect w/post-incr, long. */
1705 t = GET_L_REG (rn) & h8_get_mask (sd);
1706 SET_MEMORY_L (t, n);
1707 SET_L_REG (rn, t + 4);
1710 case X (OP_DISP, SB): /* Register indirect w/displacement, byte. */
1711 t = GET_L_REG (rn) + abs;
1712 t &= h8_get_mask (sd);
1713 SET_MEMORY_B (t, n);
1716 case X (OP_DISP, SW): /* Register indirect w/displacement, word. */
1717 t = GET_L_REG (rn) + abs;
1718 t &= h8_get_mask (sd);
1719 SET_MEMORY_W (t, n);
1722 case X (OP_DISP, SL): /* Register indirect w/displacement, long. */
1723 t = GET_L_REG (rn) + abs;
1724 t &= h8_get_mask (sd);
1725 SET_MEMORY_L (t, n);
1729 case X (OP_MEM, SB): /* Why isn't this implemented? */
1730 case X (OP_MEM, SW): /* Why isn't this implemented? */
1731 case X (OP_MEM, SL): /* Why isn't this implemented? */
1733 sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
1742 store (SIM_DESC sd, ea_type *arg, int n)
1744 return store_1 (sd, arg, n, 0);
1747 /* Store which follows a fetch from the same location.
1748 The difference being that we don't want to do a pre-increment
1749 or pre-decrement at this time: it was already done when we fetched. */
1752 store2 (SIM_DESC sd, ea_type *arg, int n)
1754 return store_1 (sd, arg, n, 1);
1768 /* Flag to be set whenever a new SIM_DESC object is created. */
1769 static int init_pointers_needed = 1;
1772 init_pointers (SIM_DESC sd)
1774 if (init_pointers_needed)
1780 if (h8300smode && !h8300_normal_mode)
1781 memory_size = H8300S_MSIZE;
1782 else if (h8300hmode && !h8300_normal_mode)
1783 memory_size = H8300H_MSIZE;
1785 memory_size = H8300_MSIZE;
1786 /* `msize' must be a power of two. */
1787 if ((memory_size & (memory_size - 1)) != 0)
1789 (*sim_callback->printf_filtered)
1791 "init_pointers: bad memory size %d, defaulting to %d.\n",
1792 memory_size, memory_size = H8300S_MSIZE);
1795 if (h8_get_memory_buf (sd))
1796 free (h8_get_memory_buf (sd));
1797 if (h8_get_cache_idx_buf (sd))
1798 free (h8_get_cache_idx_buf (sd));
1799 if (h8_get_eightbit_buf (sd))
1800 free (h8_get_eightbit_buf (sd));
1802 h8_set_memory_buf (sd, (unsigned char *)
1803 calloc (sizeof (char), memory_size));
1804 h8_set_cache_idx_buf (sd, (unsigned short *)
1805 calloc (sizeof (short), memory_size));
1806 sd->memory_size = memory_size;
1807 h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
1809 h8_set_mask (sd, memory_size - 1);
1811 memset (h8_get_reg_buf (sd), 0, sizeof (((STATE_CPU (sd, 0))->regs)));
1813 for (i = 0; i < 8; i++)
1815 /* FIXME: rewrite using local buffer. */
1816 unsigned char *p = (unsigned char *) (h8_get_reg_buf (sd) + i);
1817 unsigned char *e = (unsigned char *) (h8_get_reg_buf (sd) + i + 1);
1818 unsigned short *q = (unsigned short *) (h8_get_reg_buf (sd) + i);
1819 unsigned short *u = (unsigned short *) (h8_get_reg_buf (sd) + i + 1);
1820 h8_set_reg (sd, i, 0x00112233);
1835 wreg[i] = wreg[i + 8] = 0;
1849 if (wreg[i] == 0 || wreg[i + 8] == 0)
1850 (*sim_callback->printf_filtered) (sim_callback,
1851 "init_pointers: internal error.\n");
1853 h8_set_reg (sd, i, 0);
1854 lreg[i] = h8_get_reg_buf (sd) + i;
1857 /* Note: sim uses pseudo-register ZERO as a zero register. */
1858 lreg[ZERO_REGNUM] = h8_get_reg_buf (sd) + ZERO_REGNUM;
1859 init_pointers_needed = 0;
1861 /* Initialize the seg registers. */
1863 set_simcache_size (sd, CSIZE);
1867 /* Grotty global variable for use by control_c signal handler. */
1868 static SIM_DESC control_c_sim_desc;
1873 sim_engine_set_run_state (control_c_sim_desc, sim_stopped, SIGINT);
1877 sim_stop (SIM_DESC sd)
1879 /* FIXME: use a real signal value. */
1880 sim_engine_set_run_state (sd, sim_stopped, SIGINT);
1884 #define OBITOP(name, f, s, op) \
1885 case O (name, SB): \
1890 if (fetch (sd, &code->dst, &ea)) \
1892 if (fetch (sd, &code->src, &tmp)) \
1897 if (store (sd, &code->dst,ea)) \
1903 sim_resume (SIM_DESC sd, int step, int siggnal)
1908 int tick_start = get_now ();
1917 int c, nz, v, n, u, h, ui, intMaskBit;
1920 enum sim_stop reason;
1925 control_c_sim_desc = sd;
1926 prev = signal (SIGINT, control_c);
1930 sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
1934 sim_engine_set_run_state (sd, sim_running, 0);
1937 pc = h8_get_pc (sd);
1939 /* The PC should never be odd. */
1942 sim_engine_set_run_state (sd, sim_stopped, SIGBUS);
1946 /* Get Status Register (flags). */
1949 if (h8300smode) /* Get exr. */
1951 trace = (h8_get_exr (sd) >> 7) & 1;
1952 intMask = h8_get_exr (sd) & 7;
1955 oldmask = h8_get_mask (sd);
1956 if (!h8300hmode || h8300_normal_mode)
1957 h8_set_mask (sd, 0xffff);
1960 unsigned short cidx;
1964 cidx = h8_get_cache_idx (sd, pc);
1965 if (cidx == (unsigned short) -1 ||
1966 cidx >= sd->sim_cache_size)
1969 code = sd->sim_cache + cidx;
1974 printf ("%x %d %s\n", pc, code->opcode,
1975 code->op ? code->op->name : "**");
1977 h8_increment_stats (sd, code->opcode);
1982 cycles += code->cycles;
1986 switch (code->opcode)
1990 * This opcode is a fake for when we get to an
1991 * instruction which hasnt been compiled
1997 case O (O_MOVAB, SL):
1998 case O (O_MOVAW, SL):
1999 case O (O_MOVAL, SL):
2000 /* 1) Evaluate 2nd argument (dst).
2001 2) Mask / zero extend according to whether 1st argument (src)
2002 is INDEXB, INDEXW, or INDEXL.
2003 3) Left-shift the result by 0, 1 or 2, according to size of mova
2004 (mova/b, mova/w, mova/l).
2005 4) Add literal value of 1st argument (src).
2006 5) Store result in 3rd argument (op3).
2009 /* Alas, since this is the only instruction with 3 arguments,
2010 decode doesn't handle them very well. Some fix-up is required.
2012 a) The size of dst is determined by whether src is
2013 INDEXB or INDEXW. */
2015 if (OP_KIND (code->src.type) == OP_INDEXB)
2016 code->dst.type = X (OP_KIND (code->dst.type), SB);
2017 else if (OP_KIND (code->src.type) == OP_INDEXW)
2018 code->dst.type = X (OP_KIND (code->dst.type), SW);
2020 /* b) If op3 == null, then this is the short form of the insn.
2021 Dst is the dispreg of src, and op3 is the 32-bit form
2022 of the same register.
2025 if (code->op3.type == 0)
2027 /* Short form: src == INDEXB/INDEXW, dst == op3 == 0.
2028 We get to compose dst and op3 as follows:
2030 op3 is a 32-bit register, ID == src.reg.
2031 dst is the same register, but 8 or 16 bits
2032 depending on whether src is INDEXB or INDEXW.
2035 code->op3.type = X (OP_REG, SL);
2036 code->op3.reg = code->src.reg;
2037 code->op3.literal = 0;
2039 if (OP_KIND (code->src.type) == OP_INDEXB)
2040 code->dst.type = X (OP_REG, SB);
2042 code->dst.type = X (OP_REG, SW);
2045 if (fetch (sd, &code->dst, &ea))
2048 switch (OP_KIND (code->src.type)) {
2049 case OP_INDEXB: ea = ea & 0xff; break;
2050 case OP_INDEXW: ea = ea & 0xffff; break;
2051 case OP_INDEXL: break;
2052 default: goto illegal;
2055 switch (code->opcode) {
2056 case O (O_MOVAB, SL): break;
2057 case O (O_MOVAW, SL): ea = ea << 1; break;
2058 case O (O_MOVAL, SL): ea = ea << 2; break;
2059 default: goto illegal;
2062 ea = ea + code->src.literal;
2064 if (store (sd, &code->op3, ea))
2069 case O (O_SUBX, SB): /* subx, extended sub */
2070 if (fetch2 (sd, &code->dst, &rd))
2072 if (fetch (sd, &code->src, &ea))
2078 case O (O_SUBX, SW): /* subx, extended sub */
2079 if (fetch2 (sd, &code->dst, &rd))
2081 if (fetch (sd, &code->src, &ea))
2087 case O (O_SUBX, SL): /* subx, extended sub */
2088 if (fetch2 (sd, &code->dst, &rd))
2090 if (fetch (sd, &code->src, &ea))
2096 case O (O_ADDX, SB): /* addx, extended add */
2097 if (fetch2 (sd, &code->dst, &rd))
2099 if (fetch (sd, &code->src, &ea))
2105 case O (O_ADDX, SW): /* addx, extended add */
2106 if (fetch2 (sd, &code->dst, &rd))
2108 if (fetch (sd, &code->src, &ea))
2114 case O (O_ADDX, SL): /* addx, extended add */
2115 if (fetch2 (sd, &code->dst, &rd))
2117 if (fetch (sd, &code->src, &ea))
2123 case O (O_SUB, SB): /* sub.b */
2124 /* Fetch rd and ea. */
2125 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2131 case O (O_SUB, SW): /* sub.w */
2132 /* Fetch rd and ea. */
2133 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2139 case O (O_SUB, SL): /* sub.l */
2140 /* Fetch rd and ea. */
2141 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2147 case O (O_NEG, SB): /* neg.b */
2149 if (fetch2 (sd, &code->src, &ea))
2156 case O (O_NEG, SW): /* neg.w */
2158 if (fetch2 (sd, &code->src, &ea))
2165 case O (O_NEG, SL): /* neg.l */
2167 if (fetch2 (sd, &code->src, &ea))
2174 case O (O_ADD, SB): /* add.b */
2175 if (fetch2 (sd, &code->dst, &rd))
2177 if (fetch (sd, &code->src, &ea))
2182 case O (O_ADD, SW): /* add.w */
2183 if (fetch2 (sd, &code->dst, &rd))
2185 if (fetch (sd, &code->src, &ea))
2190 case O (O_ADD, SL): /* add.l */
2191 if (fetch2 (sd, &code->dst, &rd))
2193 if (fetch (sd, &code->src, &ea))
2198 case O (O_AND, SB): /* and.b */
2199 /* Fetch rd and ea. */
2200 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2205 case O (O_AND, SW): /* and.w */
2206 /* Fetch rd and ea. */
2207 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2212 case O (O_AND, SL): /* and.l */
2213 /* Fetch rd and ea. */
2214 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2219 case O (O_OR, SB): /* or.b */
2220 /* Fetch rd and ea. */
2221 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2226 case O (O_OR, SW): /* or.w */
2227 /* Fetch rd and ea. */
2228 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2233 case O (O_OR, SL): /* or.l */
2234 /* Fetch rd and ea. */
2235 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2240 case O (O_XOR, SB): /* xor.b */
2241 /* Fetch rd and ea. */
2242 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2247 case O (O_XOR, SW): /* xor.w */
2248 /* Fetch rd and ea. */
2249 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2254 case O (O_XOR, SL): /* xor.l */
2255 /* Fetch rd and ea. */
2256 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2262 if (fetch (sd, &code->src, &res))
2264 if (store (sd, &code->dst, res))
2266 goto just_flags_log8;
2268 if (fetch (sd, &code->src, &res))
2270 if (store (sd, &code->dst, res))
2272 goto just_flags_log16;
2274 if (fetch (sd, &code->src, &res))
2276 if (store (sd, &code->dst, res))
2278 goto just_flags_log32;
2280 case O (O_MOVMD, SB): /* movmd.b */
2287 rd = GET_MEMORY_B (GET_L_REG (5));
2288 SET_MEMORY_B (GET_L_REG (6), rd);
2289 SET_L_REG (5, GET_L_REG (5) + 1);
2290 SET_L_REG (6, GET_L_REG (6) + 1);
2295 case O (O_MOVMD, SW): /* movmd.w */
2302 rd = GET_MEMORY_W (GET_L_REG (5));
2303 SET_MEMORY_W (GET_L_REG (6), rd);
2304 SET_L_REG (5, GET_L_REG (5) + 2);
2305 SET_L_REG (6, GET_L_REG (6) + 2);
2310 case O (O_MOVMD, SL): /* movmd.l */
2317 rd = GET_MEMORY_L (GET_L_REG (5));
2318 SET_MEMORY_L (GET_L_REG (6), rd);
2319 SET_L_REG (5, GET_L_REG (5) + 4);
2320 SET_L_REG (6, GET_L_REG (6) + 4);
2325 case O (O_MOVSD, SB): /* movsd.b */
2326 /* This instruction implements strncpy, with a conditional branch.
2327 r4 contains n, r5 contains src, and r6 contains dst.
2328 The 16-bit displacement operand is added to the pc
2329 if and only if the end of string is reached before
2330 n bytes are transferred. */
2332 ea = GET_L_REG (4) & 0xffff;
2338 rd = GET_MEMORY_B (GET_L_REG (5));
2339 SET_MEMORY_B (GET_L_REG (6), rd);
2340 SET_L_REG (5, GET_L_REG (5) + 1);
2341 SET_L_REG (6, GET_L_REG (6) + 1);
2348 case O (O_EEPMOV, SB): /* eepmov.b */
2349 case O (O_EEPMOV, SW): /* eepmov.w */
2350 if (h8300hmode || h8300smode)
2352 register unsigned char *_src, *_dst;
2353 unsigned int count = ((code->opcode == O (O_EEPMOV, SW))
2354 ? h8_get_reg (sd, R4_REGNUM) & 0xffff
2355 : h8_get_reg (sd, R4_REGNUM) & 0xff);
2357 _src = (h8_get_reg (sd, R5_REGNUM) < memory_size
2358 ? h8_get_memory_buf (sd) + h8_get_reg (sd, R5_REGNUM)
2359 : h8_get_eightbit_buf (sd) +
2360 (h8_get_reg (sd, R5_REGNUM) & 0xff));
2361 if ((_src + count) >= (h8_get_memory_buf (sd) + memory_size))
2363 if ((_src + count) >= (h8_get_eightbit_buf (sd) + 0x100))
2366 _dst = (h8_get_reg (sd, R6_REGNUM) < memory_size
2367 ? h8_get_memory_buf (sd) + h8_get_reg (sd, R6_REGNUM)
2368 : h8_get_eightbit_buf (sd) +
2369 (h8_get_reg (sd, R6_REGNUM) & 0xff));
2371 if ((_dst + count) >= (h8_get_memory_buf (sd) + memory_size))
2373 if ((_dst + count) >= (h8_get_eightbit_buf (sd) + 0x100))
2376 memcpy (_dst, _src, count);
2378 h8_set_reg (sd, R5_REGNUM, h8_get_reg (sd, R5_REGNUM) + count);
2379 h8_set_reg (sd, R6_REGNUM, h8_get_reg (sd, R6_REGNUM) + count);
2380 h8_set_reg (sd, R4_REGNUM, h8_get_reg (sd, R4_REGNUM) &
2381 ((code->opcode == O (O_EEPMOV, SW))
2382 ? (~0xffff) : (~0xff)));
2383 cycles += 2 * count;
2388 case O (O_ADDS, SL): /* adds (.l) */
2390 * This insn only uses register operands, but still
2391 * it would be cleaner to use fetch and store... */
2392 SET_L_REG (code->dst.reg,
2393 GET_L_REG (code->dst.reg)
2394 + code->src.literal);
2398 case O (O_SUBS, SL): /* subs (.l) */
2400 * This insn only uses register operands, but still
2401 * it would be cleaner to use fetch and store... */
2402 SET_L_REG (code->dst.reg,
2403 GET_L_REG (code->dst.reg)
2404 - code->src.literal);
2407 case O (O_CMP, SB): /* cmp.b */
2408 if (fetch (sd, &code->dst, &rd))
2410 if (fetch (sd, &code->src, &ea))
2414 goto just_flags_alu8;
2416 case O (O_CMP, SW): /* cmp.w */
2417 if (fetch (sd, &code->dst, &rd))
2419 if (fetch (sd, &code->src, &ea))
2423 goto just_flags_alu16;
2425 case O (O_CMP, SL): /* cmp.l */
2426 if (fetch (sd, &code->dst, &rd))
2428 if (fetch (sd, &code->src, &ea))
2432 goto just_flags_alu32;
2434 case O (O_DEC, SB): /* dec.b */
2436 * This insn only uses register operands, but still
2437 * it would be cleaner to use fetch and store... */
2438 rd = GET_B_REG (code->src.reg);
2441 SET_B_REG (code->src.reg, res);
2442 goto just_flags_inc8;
2444 case O (O_DEC, SW): /* dec.w */
2446 * This insn only uses register operands, but still
2447 * it would be cleaner to use fetch and store... */
2448 rd = GET_W_REG (code->dst.reg);
2449 ea = -code->src.literal;
2451 SET_W_REG (code->dst.reg, res);
2452 goto just_flags_inc16;
2454 case O (O_DEC, SL): /* dec.l */
2456 * This insn only uses register operands, but still
2457 * it would be cleaner to use fetch and store... */
2458 rd = GET_L_REG (code->dst.reg);
2459 ea = -code->src.literal;
2461 SET_L_REG (code->dst.reg, res);
2462 goto just_flags_inc32;
2464 case O (O_INC, SB): /* inc.b */
2466 * This insn only uses register operands, but still
2467 * it would be cleaner to use fetch and store... */
2468 rd = GET_B_REG (code->src.reg);
2471 SET_B_REG (code->src.reg, res);
2472 goto just_flags_inc8;
2474 case O (O_INC, SW): /* inc.w */
2476 * This insn only uses register operands, but still
2477 * it would be cleaner to use fetch and store... */
2478 rd = GET_W_REG (code->dst.reg);
2479 ea = code->src.literal;
2481 SET_W_REG (code->dst.reg, res);
2482 goto just_flags_inc16;
2484 case O (O_INC, SL): /* inc.l */
2486 * This insn only uses register operands, but still
2487 * it would be cleaner to use fetch and store... */
2488 rd = GET_L_REG (code->dst.reg);
2489 ea = code->src.literal;
2491 SET_L_REG (code->dst.reg, res);
2492 goto just_flags_inc32;
2494 case O (O_LDC, SB): /* ldc.b */
2495 if (fetch (sd, &code->src, &res))
2499 case O (O_LDC, SW): /* ldc.w */
2500 if (fetch (sd, &code->src, &res))
2503 /* Word operand, value from MSB, must be shifted. */
2507 case O (O_LDC, SL): /* ldc.l */
2508 if (fetch (sd, &code->src, &res))
2510 switch (code->dst.type) {
2511 case X (OP_SBR, SL):
2512 h8_set_sbr (sd, res);
2514 case X (OP_VBR, SL):
2515 h8_set_vbr (sd, res);
2522 case O (O_STC, SW): /* stc.w */
2523 case O (O_STC, SB): /* stc.b */
2524 if (code->src.type == X (OP_CCR, SB))
2527 res = h8_get_ccr (sd);
2529 else if (code->src.type == X (OP_EXR, SB) && h8300smode)
2532 h8_set_exr (sd, (trace << 7) | intMask);
2533 res = h8_get_exr (sd);
2538 /* Word operand, value to MSB, must be shifted. */
2539 if (code->opcode == X (O_STC, SW))
2541 if (store (sd, &code->dst, res))
2544 case O (O_STC, SL): /* stc.l */
2545 switch (code->src.type) {
2546 case X (OP_SBR, SL):
2547 res = h8_get_sbr (sd);
2549 case X (OP_VBR, SL):
2550 res = h8_get_vbr (sd);
2555 if (store (sd, &code->dst, res))
2559 case O (O_ANDC, SB): /* andc.b */
2560 if (code->dst.type == X (OP_CCR, SB))
2563 rd = h8_get_ccr (sd);
2565 else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
2568 h8_set_exr (sd, (trace << 7) | intMask);
2569 res = h8_get_exr (sd);
2573 ea = code->src.literal;
2577 case O (O_ORC, SB): /* orc.b */
2578 if (code->dst.type == X (OP_CCR, SB))
2581 rd = h8_get_ccr (sd);
2583 else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
2586 h8_set_exr (sd, (trace << 7) | intMask);
2587 rd = h8_get_exr (sd);
2591 ea = code->src.literal;
2595 case O (O_XORC, SB): /* xorc.b */
2596 if (code->dst.type == X (OP_CCR, SB))
2599 rd = h8_get_ccr (sd);
2601 else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
2604 h8_set_exr (sd, (trace << 7) | intMask);
2605 rd = h8_get_exr (sd);
2609 ea = code->src.literal;
2613 case O (O_BRAS, SB): /* bra/s */
2614 /* This is basically an ordinary branch, with a delay slot. */
2615 if (fetch (sd, &code->src, &res))
2623 /* Execution continues at next instruction, but
2624 delayed_branch is set up for next cycle. */
2625 h8_set_delayed_branch (sd, code->next_pc + res);
2629 case O (O_BRAB, SB): /* bra rd.b */
2630 case O (O_BRAW, SW): /* bra rd.w */
2631 case O (O_BRAL, SL): /* bra erd.l */
2632 if (fetch (sd, &code->src, &rd))
2634 switch (OP_SIZE (code->opcode)) {
2635 case SB: rd &= 0xff; break;
2636 case SW: rd &= 0xffff; break;
2637 case SL: rd &= 0xffffffff; break;
2639 pc = code->next_pc + rd;
2642 case O (O_BRABC, SB): /* bra/bc, branch if bit clear */
2643 case O (O_BRABS, SB): /* bra/bs, branch if bit set */
2644 case O (O_BSRBC, SB): /* bsr/bc, call if bit clear */
2645 case O (O_BSRBS, SB): /* bsr/bs, call if bit set */
2646 if (fetch (sd, &code->dst, &rd) ||
2647 fetch (sd, &code->src, &bit))
2650 if (code->opcode == O (O_BRABC, SB) || /* branch if clear */
2651 code->opcode == O (O_BSRBC, SB)) /* call if clear */
2653 if ((rd & (1 << bit))) /* no branch */
2656 else /* branch/call if set */
2658 if (!(rd & (1 << bit))) /* no branch */
2662 if (fetch (sd, &code->op3, &res)) /* branch */
2664 pc = code->next_pc + res;
2666 if (code->opcode == O (O_BRABC, SB) ||
2667 code->opcode == O (O_BRABS, SB)) /* branch */
2675 case O (O_BRA, SB): /* bra, branch always */
2680 case O (O_BRN, SB): /* brn, ;-/ branch never? */
2685 case O (O_BHI, SB): /* bhi */
2691 case O (O_BLS, SB): /* bls */
2696 case O (O_BCS, SB): /* bcs, branch if carry set */
2701 case O (O_BCC, SB): /* bcc, branch if carry clear */
2706 case O (O_BEQ, SB): /* beq, branch if zero set */
2710 case O (O_BGT, SB): /* bgt */
2711 if (((Z || (N ^ V)) == 0))
2715 case O (O_BLE, SB): /* ble */
2716 if (((Z || (N ^ V)) == 1))
2720 case O (O_BGE, SB): /* bge */
2724 case O (O_BLT, SB): /* blt */
2728 case O (O_BMI, SB): /* bmi */
2732 case O (O_BNE, SB): /* bne, branch if zero clear */
2737 case O (O_BPL, SB): /* bpl */
2741 case O (O_BVC, SB): /* bvc */
2745 case O (O_BVS, SB): /* bvs */
2750 /* Trap for Command Line setup. */
2751 case O (O_SYS_CMDLINE, SB):
2753 int i = 0; /* Loop counter. */
2754 int j = 0; /* Loop counter. */
2755 int ind_arg_len = 0; /* Length of each argument. */
2756 int no_of_args = 0; /* The no. or cmdline args. */
2757 int current_location = 0; /* Location of string. */
2758 int old_sp = 0; /* The Initial Stack Pointer. */
2759 int no_of_slots = 0; /* No. of slots required on the stack
2760 for storing cmdline args. */
2761 int sp_move = 0; /* No. of locations by which the stack needs
2763 int new_sp = 0; /* The final stack pointer location passed
2765 int *argv_ptrs; /* Pointers of argv strings to be stored. */
2766 int argv_ptrs_location = 0; /* Location of pointers to cmdline
2767 args on the stack. */
2768 int char_ptr_size = 0; /* Size of a character pointer on
2770 int addr_cmdline = 0; /* Memory location where cmdline has
2772 int size_cmdline = 0; /* Size of cmdline. */
2774 /* Set the address of 256 free locations where command line is
2776 addr_cmdline = cmdline_location();
2777 h8_set_reg (sd, 0, addr_cmdline);
2779 /* Counting the no. of commandline arguments. */
2780 for (i = 0; h8_get_cmdline_arg (sd, i) != NULL; i++)
2783 /* No. of arguments in the command line. */
2786 /* Current location is just a temporary variable,which we are
2787 setting to the point to the start of our commandline string. */
2788 current_location = addr_cmdline;
2790 /* Allocating space for storing pointers of the command line
2792 argv_ptrs = (int *) malloc (sizeof (int) * no_of_args);
2794 /* Setting char_ptr_size to the sizeof (char *) on the different
2796 if ((h8300hmode || h8300smode) && !h8300_normal_mode)
2805 for (i = 0; i < no_of_args; i++)
2809 /* The size of the commandline argument. */
2810 ind_arg_len = strlen (h8_get_cmdline_arg (sd, i) + 1);
2812 /* The total size of the command line string. */
2813 size_cmdline += ind_arg_len;
2815 /* As we have only 256 bytes, we need to provide a graceful
2816 exit. Anyways, a program using command line arguments
2817 where we cannot store all the command line arguments
2818 given may behave unpredictably. */
2819 if (size_cmdline >= 256)
2821 h8_set_reg (sd, 0, 0);
2826 /* current_location points to the memory where the next
2827 commandline argument is stored. */
2828 argv_ptrs[i] = current_location;
2829 for (j = 0; j < ind_arg_len; j++)
2831 SET_MEMORY_B ((current_location +
2832 (sizeof (char) * j)),
2833 *(h8_get_cmdline_arg (sd, i) +
2834 sizeof (char) * j));
2837 /* Setting current_location to the starting of next
2839 current_location += ind_arg_len;
2843 /* This is the original position of the stack pointer. */
2844 old_sp = h8_get_reg (sd, SP_REGNUM);
2846 /* We need space from the stack to store the pointers to argvs. */
2847 /* As we will infringe on the stack, we need to shift the stack
2848 pointer so that the data is not overwritten. We calculate how
2849 much space is required. */
2850 sp_move = (no_of_args) * (char_ptr_size);
2852 /* The final position of stack pointer, we have thus taken some
2853 space from the stack. */
2854 new_sp = old_sp - sp_move;
2856 /* Temporary variable holding value where the argv pointers need
2858 argv_ptrs_location = new_sp;
2860 /* The argv pointers are stored at sequential locations. As per
2862 for (i = 0; i < no_of_args; i++)
2864 /* Saving the argv pointer. */
2865 if ((h8300hmode || h8300smode) && !h8300_normal_mode)
2867 SET_MEMORY_L (argv_ptrs_location, argv_ptrs[i]);
2871 SET_MEMORY_W (argv_ptrs_location, argv_ptrs[i]);
2874 /* The next location where the pointer to the next argv
2875 string has to be stored. */
2876 argv_ptrs_location += char_ptr_size;
2879 /* Required by POSIX, Setting 0x0 at the end of the list of argv
2881 if ((h8300hmode || h8300smode) && !h8300_normal_mode)
2883 SET_MEMORY_L (old_sp, 0x0);
2887 SET_MEMORY_W (old_sp, 0x0);
2890 /* Freeing allocated memory. */
2892 for (i = 0; i <= no_of_args; i++)
2894 free (h8_get_cmdline_arg (sd, i));
2896 free (h8_get_command_line (sd));
2898 /* The no. of argv arguments are returned in Reg 0. */
2899 h8_set_reg (sd, 0, no_of_args);
2900 /* The Pointer to argv in Register 1. */
2901 h8_set_reg (sd, 1, new_sp);
2902 /* Setting the stack pointer to the new value. */
2903 h8_set_reg (sd, SP_REGNUM, new_sp);
2907 /* System call processing starts. */
2908 case O (O_SYS_OPEN, SB):
2910 int len = 0; /* Length of filename. */
2911 char *filename; /* Filename would go here. */
2912 char temp_char; /* Temporary character */
2913 int mode = 0; /* Mode bits for the file. */
2914 int open_return; /* Return value of open, file descriptor. */
2915 int i; /* Loop counter */
2916 int filename_ptr; /* Pointer to filename in cpu memory. */
2918 /* Setting filename_ptr to first argument of open, */
2919 /* and trying to get mode. */
2920 if ((h8300sxmode || h8300hmode || h8300smode) && !h8300_normal_mode)
2922 filename_ptr = GET_L_REG (0);
2923 mode = GET_MEMORY_L (h8_get_reg (sd, SP_REGNUM) + 4);
2927 filename_ptr = GET_W_REG (0);
2928 mode = GET_MEMORY_W (h8_get_reg (sd, SP_REGNUM) + 2);
2931 /* Trying to find the length of the filename. */
2932 temp_char = GET_MEMORY_B (h8_get_reg (sd, 0));
2935 while (temp_char != '\0')
2937 temp_char = GET_MEMORY_B (filename_ptr + len);
2941 /* Allocating space for the filename. */
2942 filename = (char *) malloc (sizeof (char) * len);
2944 /* String copying the filename from memory. */
2945 for (i = 0; i < len; i++)
2947 temp_char = GET_MEMORY_B (filename_ptr + i);
2948 filename[i] = temp_char;
2951 /* Callback to open and return the file descriptor. */
2952 open_return = sim_callback->open (sim_callback, filename, mode);
2954 /* Return value in register 0. */
2955 h8_set_reg (sd, 0, open_return);
2957 /* Freeing memory used for filename. */
2962 case O (O_SYS_READ, SB):
2964 char *char_ptr; /* Where characters read would be stored. */
2965 int fd; /* File descriptor */
2966 int buf_size; /* BUF_SIZE parameter in read. */
2967 int i = 0; /* Temporary Loop counter */
2968 int read_return = 0; /* Return value from callback to
2971 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
2972 buf_size = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (2) : GET_W_REG (2);
2974 char_ptr = (char *) malloc (sizeof (char) * buf_size);
2976 /* Callback to read and return the no. of characters read. */
2978 sim_callback->read (sim_callback, fd, char_ptr, buf_size);
2980 /* The characters read are stored in cpu memory. */
2981 for (i = 0; i < buf_size; i++)
2983 SET_MEMORY_B ((h8_get_reg (sd, 1) + (sizeof (char) * i)),
2984 *(char_ptr + (sizeof (char) * i)));
2987 /* Return value in Register 0. */
2988 h8_set_reg (sd, 0, read_return);
2990 /* Freeing memory used as buffer. */
2995 case O (O_SYS_WRITE, SB):
2997 int fd; /* File descriptor */
2998 char temp_char; /* Temporary character */
2999 int len; /* Length of write, Parameter II to write. */
3000 int char_ptr; /* Character Pointer, Parameter I of write. */
3001 char *ptr; /* Where characters to be written are stored.
3003 int write_return; /* Return value from callback to write. */
3004 int i = 0; /* Loop counter */
3006 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3007 char_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
3008 len = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (2) : GET_W_REG (2);
3010 /* Allocating space for the characters to be written. */
3011 ptr = (char *) malloc (sizeof (char) * len);
3013 /* Fetching the characters from cpu memory. */
3014 for (i = 0; i < len; i++)
3016 temp_char = GET_MEMORY_B (char_ptr + i);
3020 /* Callback write and return the no. of characters written. */
3021 write_return = sim_callback->write (sim_callback, fd, ptr, len);
3023 /* Return value in Register 0. */
3024 h8_set_reg (sd, 0, write_return);
3026 /* Freeing memory used as buffer. */
3031 case O (O_SYS_LSEEK, SB):
3033 int fd; /* File descriptor */
3034 int offset; /* Offset */
3035 int origin; /* Origin */
3036 int lseek_return; /* Return value from callback to lseek. */
3038 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3039 offset = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
3040 origin = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (2) : GET_W_REG (2);
3042 /* Callback lseek and return offset. */
3044 sim_callback->lseek (sim_callback, fd, offset, origin);
3046 /* Return value in register 0. */
3047 h8_set_reg (sd, 0, lseek_return);
3051 case O (O_SYS_CLOSE, SB):
3053 int fd; /* File descriptor */
3054 int close_return; /* Return value from callback to close. */
3056 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3058 /* Callback close and return. */
3059 close_return = sim_callback->close (sim_callback, fd);
3061 /* Return value in register 0. */
3062 h8_set_reg (sd, 0, close_return);
3066 case O (O_SYS_FSTAT, SB):
3068 int fd; /* File descriptor */
3069 struct stat stat_rec; /* Stat record */
3070 int fstat_return; /* Return value from callback to stat. */
3071 int stat_ptr; /* Pointer to stat record. */
3072 char *temp_stat_ptr; /* Temporary stat_rec pointer. */
3074 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3076 /* Setting stat_ptr to second argument of stat. */
3077 stat_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
3079 /* Callback stat and return. */
3080 fstat_return = sim_callback->fstat (sim_callback, fd, &stat_rec);
3082 /* Have stat_ptr point to starting of stat_rec. */
3083 temp_stat_ptr = (char *) (&stat_rec);
3085 /* Setting up the stat structure returned. */
3086 SET_MEMORY_W (stat_ptr, stat_rec.st_dev);
3088 SET_MEMORY_W (stat_ptr, stat_rec.st_ino);
3090 SET_MEMORY_L (stat_ptr, stat_rec.st_mode);
3092 SET_MEMORY_W (stat_ptr, stat_rec.st_nlink);
3094 SET_MEMORY_W (stat_ptr, stat_rec.st_uid);
3096 SET_MEMORY_W (stat_ptr, stat_rec.st_gid);
3098 SET_MEMORY_W (stat_ptr, stat_rec.st_rdev);
3100 SET_MEMORY_L (stat_ptr, stat_rec.st_size);
3102 SET_MEMORY_L (stat_ptr, stat_rec.st_atime);
3104 SET_MEMORY_L (stat_ptr, stat_rec.st_mtime);
3106 SET_MEMORY_L (stat_ptr, stat_rec.st_ctime);
3108 /* Return value in register 0. */
3109 h8_set_reg (sd, 0, fstat_return);
3113 case O (O_SYS_STAT, SB):
3115 int len = 0; /* Length of filename. */
3116 char *filename; /* Filename would go here. */
3117 char temp_char; /* Temporary character */
3118 int filename_ptr; /* Pointer to filename in cpu memory. */
3119 struct stat stat_rec; /* Stat record */
3120 int stat_return; /* Return value from callback to stat */
3121 int stat_ptr; /* Pointer to stat record. */
3122 char *temp_stat_ptr; /* Temporary stat_rec pointer. */
3123 int i = 0; /* Loop Counter */
3125 /* Setting filename_ptr to first argument of open. */
3126 filename_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3128 /* Trying to find the length of the filename. */
3129 temp_char = GET_MEMORY_B (h8_get_reg (sd, 0));
3132 while (temp_char != '\0')
3134 temp_char = GET_MEMORY_B (filename_ptr + len);
3138 /* Allocating space for the filename. */
3139 filename = (char *) malloc (sizeof (char) * len);
3141 /* String copying the filename from memory. */
3142 for (i = 0; i < len; i++)
3144 temp_char = GET_MEMORY_B (filename_ptr + i);
3145 filename[i] = temp_char;
3148 /* Setting stat_ptr to second argument of stat. */
3149 /* stat_ptr = h8_get_reg (sd, 1); */
3150 stat_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
3152 /* Callback stat and return. */
3154 sim_callback->stat (sim_callback, filename, &stat_rec);
3156 /* Have stat_ptr point to starting of stat_rec. */
3157 temp_stat_ptr = (char *) (&stat_rec);
3159 /* Freeing memory used for filename. */
3162 /* Setting up the stat structure returned. */
3163 SET_MEMORY_W (stat_ptr, stat_rec.st_dev);
3165 SET_MEMORY_W (stat_ptr, stat_rec.st_ino);
3167 SET_MEMORY_L (stat_ptr, stat_rec.st_mode);
3169 SET_MEMORY_W (stat_ptr, stat_rec.st_nlink);
3171 SET_MEMORY_W (stat_ptr, stat_rec.st_uid);
3173 SET_MEMORY_W (stat_ptr, stat_rec.st_gid);
3175 SET_MEMORY_W (stat_ptr, stat_rec.st_rdev);
3177 SET_MEMORY_L (stat_ptr, stat_rec.st_size);
3179 SET_MEMORY_L (stat_ptr, stat_rec.st_atime);
3181 SET_MEMORY_L (stat_ptr, stat_rec.st_mtime);
3183 SET_MEMORY_L (stat_ptr, stat_rec.st_ctime);
3185 /* Return value in register 0. */
3186 h8_set_reg (sd, 0, stat_return);
3189 /* End of system call processing. */
3191 case O (O_NOT, SB): /* not.b */
3192 if (fetch2 (sd, &code->src, &rd))
3198 case O (O_NOT, SW): /* not.w */
3199 if (fetch2 (sd, &code->src, &rd))
3205 case O (O_NOT, SL): /* not.l */
3206 if (fetch2 (sd, &code->src, &rd))
3212 case O (O_SHLL, SB): /* shll.b */
3213 case O (O_SHLR, SB): /* shlr.b */
3214 if (fetch2 (sd, &code->dst, &rd))
3217 if (memcmp (&code->src, &code->dst, sizeof (code->src)) == 0)
3218 ea = 1; /* unary op */
3219 else /* binary op */
3220 fetch (sd, &code->src, &ea);
3222 if (code->opcode == O (O_SHLL, SB))
3225 c = rd & (0x80 >> (ea - 1));
3231 c = rd & (1 << (ea - 1));
3232 rd = (unsigned char) rd >> ea;
3236 case O (O_SHLL, SW): /* shll.w */
3237 case O (O_SHLR, SW): /* shlr.w */
3238 if (fetch2 (sd, &code->dst, &rd))
3241 if (memcmp (&code->src, &code->dst, sizeof (code->src)) == 0)
3242 ea = 1; /* unary op */
3244 fetch (sd, &code->src, &ea);
3246 if (code->opcode == O (O_SHLL, SW))
3249 c = rd & (0x8000 >> (ea - 1));
3255 c = rd & (1 << (ea - 1));
3256 rd = (unsigned short) rd >> ea;
3260 case O (O_SHLL, SL): /* shll.l */
3261 case O (O_SHLR, SL): /* shlr.l */
3262 if (fetch2 (sd, &code->dst, &rd))
3265 if (memcmp (&code->src, &code->dst, sizeof (code->src)) == 0)
3266 ea = 1; /* unary op */
3268 fetch (sd, &code->src, &ea);
3270 if (code->opcode == O (O_SHLL, SL))
3273 c = rd & (0x80000000 >> (ea - 1));
3279 c = rd & (1 << (ea - 1));
3280 rd = (unsigned int) rd >> ea;
3284 case O (O_SHAL, SB):
3285 case O (O_SHAR, SB):
3286 if (fetch2 (sd, &code->dst, &rd))
3289 if (code->src.type == X (OP_IMM, SB))
3290 fetch (sd, &code->src, &ea);
3294 if (code->opcode == O (O_SHAL, SB))
3296 c = rd & (0x80 >> (ea - 1));
3297 res = rd >> (7 - ea);
3298 v = ((res & 1) && !(res & 2))
3299 || (!(res & 1) && (res & 2));
3304 c = rd & (1 << (ea - 1));
3306 rd = ((signed char) rd) >> ea;
3310 case O (O_SHAL, SW):
3311 case O (O_SHAR, SW):
3312 if (fetch2 (sd, &code->dst, &rd))
3315 if (code->src.type == X (OP_IMM, SW))
3316 fetch (sd, &code->src, &ea);
3320 if (code->opcode == O (O_SHAL, SW))
3322 c = rd & (0x8000 >> (ea - 1));
3323 res = rd >> (15 - ea);
3324 v = ((res & 1) && !(res & 2))
3325 || (!(res & 1) && (res & 2));
3330 c = rd & (1 << (ea - 1));
3332 rd = ((signed short) rd) >> ea;
3336 case O (O_SHAL, SL):
3337 case O (O_SHAR, SL):
3338 if (fetch2 (sd, &code->dst, &rd))
3341 if (code->src.type == X (OP_IMM, SL))
3342 fetch (sd, &code->src, &ea);
3346 if (code->opcode == O (O_SHAL, SL))
3348 c = rd & (0x80000000 >> (ea - 1));
3349 res = rd >> (31 - ea);
3350 v = ((res & 1) && !(res & 2))
3351 || (!(res & 1) && (res & 2));
3356 c = rd & (1 << (ea - 1));
3358 rd = ((signed int) rd) >> ea;
3362 case O (O_ROTL, SB):
3363 case O (O_ROTR, SB):
3364 if (fetch2 (sd, &code->dst, &rd))
3367 if (code->src.type == X (OP_IMM, SB))
3368 fetch (sd, &code->src, &ea);
3373 if (code->opcode == O (O_ROTL, SB))
3383 rd = ((unsigned char) rd) >> 1;
3391 case O (O_ROTL, SW):
3392 case O (O_ROTR, SW):
3393 if (fetch2 (sd, &code->dst, &rd))
3396 if (code->src.type == X (OP_IMM, SW))
3397 fetch (sd, &code->src, &ea);
3402 if (code->opcode == O (O_ROTL, SW))
3412 rd = ((unsigned short) rd) >> 1;
3420 case O (O_ROTL, SL):
3421 case O (O_ROTR, SL):
3422 if (fetch2 (sd, &code->dst, &rd))
3425 if (code->src.type == X (OP_IMM, SL))
3426 fetch (sd, &code->src, &ea);
3431 if (code->opcode == O (O_ROTL, SL))
3433 c = rd & 0x80000000;
3441 rd = ((unsigned int) rd) >> 1;
3449 case O (O_ROTXL, SB):
3450 case O (O_ROTXR, SB):
3451 if (fetch2 (sd, &code->dst, &rd))
3454 if (code->src.type == X (OP_IMM, SB))
3455 fetch (sd, &code->src, &ea);
3460 if (code->opcode == O (O_ROTXL, SB))
3471 rd = ((unsigned char) rd) >> 1;
3480 case O (O_ROTXL, SW):
3481 case O (O_ROTXR, SW):
3482 if (fetch2 (sd, &code->dst, &rd))
3485 if (code->src.type == X (OP_IMM, SW))
3486 fetch (sd, &code->src, &ea);
3491 if (code->opcode == O (O_ROTXL, SW))
3502 rd = ((unsigned short) rd) >> 1;
3511 case O (O_ROTXL, SL):
3512 case O (O_ROTXR, SL):
3513 if (fetch2 (sd, &code->dst, &rd))
3516 if (code->src.type == X (OP_IMM, SL))
3517 fetch (sd, &code->src, &ea);
3522 if (code->opcode == O (O_ROTXL, SL))
3524 res = rd & 0x80000000;
3533 rd = ((unsigned int) rd) >> 1;
3544 case O (O_JMP, SB): /* jmp */
3546 fetch (sd, &code->src, &pc);
3551 case O (O_JSR, SB): /* jsr, jump to subroutine */
3553 if (fetch (sd, &code->src, &pc))
3556 tmp = h8_get_reg (sd, SP_REGNUM);
3558 if (h8300hmode && !h8300_normal_mode)
3561 SET_MEMORY_L (tmp, code->next_pc);
3566 SET_MEMORY_W (tmp, code->next_pc);
3568 h8_set_reg (sd, SP_REGNUM, tmp);
3574 case O (O_BSR, SB): /* bsr, branch to subroutine */
3575 if (fetch (sd, &code->src, &res))
3577 pc = code->next_pc + res;
3580 case O (O_RTE, SN): /* rte, return from exception */
3582 /* Pops exr and ccr before pc -- otherwise identical to rts. */
3583 tmp = h8_get_reg (sd, SP_REGNUM);
3585 if (h8300smode) /* pop exr */
3587 h8_set_exr (sd, GET_MEMORY_L (tmp));
3590 if (h8300hmode && !h8300_normal_mode)
3592 h8_set_ccr (sd, GET_MEMORY_L (tmp));
3594 pc = GET_MEMORY_L (tmp);
3599 h8_set_ccr (sd, GET_MEMORY_W (tmp));
3601 pc = GET_MEMORY_W (tmp);
3606 h8_set_reg (sd, SP_REGNUM, tmp);
3609 case O (O_RTS, SN): /* rts, return from subroutine */
3611 tmp = h8_get_reg (sd, SP_REGNUM);
3613 if (h8300hmode && !h8300_normal_mode)
3615 pc = GET_MEMORY_L (tmp);
3620 pc = GET_MEMORY_W (tmp);
3624 h8_set_reg (sd, SP_REGNUM, tmp);
3627 case O (O_ILL, SB): /* illegal */
3628 sim_engine_set_run_state (sd, sim_stopped, SIGILL);
3631 case O (O_SLEEP, SN): /* sleep */
3632 /* Check for magic numbers in r1 and r2. */
3633 if ((h8_get_reg (sd, R1_REGNUM) & 0xffff) == LIBC_EXIT_MAGIC1 &&
3634 (h8_get_reg (sd, R2_REGNUM) & 0xffff) == LIBC_EXIT_MAGIC2 &&
3635 SIM_WIFEXITED (h8_get_reg (sd, 0)))
3637 /* This trap comes from _exit, not from gdb. */
3638 sim_engine_set_run_state (sd, sim_exited,
3639 SIM_WEXITSTATUS (h8_get_reg (sd, 0)));
3642 /* Unfortunately this won't really work, because
3643 when we take a breakpoint trap, R0 has a "random",
3644 user-defined value. Don't see any immediate solution. */
3645 else if (SIM_WIFSTOPPED (h8_get_reg (sd, 0)))
3647 /* Pass the stop signal up to gdb. */
3648 sim_engine_set_run_state (sd, sim_stopped,
3649 SIM_WSTOPSIG (h8_get_reg (sd, 0)));
3654 /* Treat it as a sigtrap. */
3655 sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
3659 case O (O_TRAPA, SB): /* trapa */
3660 if (fetch (sd, &code->src, &res))
3661 goto end; /* res is vector number. */
3663 tmp = h8_get_reg (sd, SP_REGNUM);
3664 if(h8300_normal_mode)
3667 SET_MEMORY_W (tmp, code->next_pc);
3669 SET_MEMORY_W (tmp, h8_get_ccr (sd));
3674 SET_MEMORY_L (tmp, code->next_pc);
3676 SET_MEMORY_L (tmp, h8_get_ccr (sd));
3684 SET_MEMORY_L (tmp, h8_get_exr (sd));
3687 h8_set_reg (sd, SP_REGNUM, tmp);
3689 if(h8300_normal_mode)
3690 pc = GET_MEMORY_L (0x10 + res * 2); /* Vector addresses are 0x10,0x12,0x14 and 0x16 */
3692 pc = GET_MEMORY_L (0x20 + res * 4);
3696 sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
3699 case O (O_BSETEQ, SB):
3704 case O (O_BSETNE, SB):
3709 case O (O_BCLREQ, SB):
3714 case O (O_BCLRNE, SB):
3719 OBITOP (O_BNOT, 1, 1, ea ^= m); /* bnot */
3720 OBITOP (O_BTST, 1, 0, nz = ea & m); /* btst */
3722 OBITOP (O_BSET, 1, 1, ea |= m); /* bset */
3724 OBITOP (O_BCLR, 1, 1, ea &= ~m); /* bclr */
3725 OBITOP (O_BLD, 1, 0, c = ea & m); /* bld */
3726 OBITOP (O_BILD, 1, 0, c = !(ea & m)); /* bild */
3727 OBITOP (O_BST, 1, 1, ea &= ~m;
3728 if (C) ea |= m); /* bst */
3729 OBITOP (O_BIST, 1, 1, ea &= ~m;
3730 if (!C) ea |= m); /* bist */
3731 OBITOP (O_BSTZ, 1, 1, ea &= ~m;
3732 if (Z) ea |= m); /* bstz */
3733 OBITOP (O_BISTZ, 1, 1, ea &= ~m;
3734 if (!Z) ea |= m); /* bistz */
3735 OBITOP (O_BAND, 1, 0, c = (ea & m) && C); /* band */
3736 OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C); /* biand */
3737 OBITOP (O_BOR, 1, 0, c = (ea & m) || C); /* bor */
3738 OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C); /* bior */
3739 OBITOP (O_BXOR, 1, 0, c = ((ea & m) != 0)!= C); /* bxor */
3740 OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C); /* bixor */
3742 case O (O_BFLD, SB): /* bfld */
3745 if (fetch (sd, &code->src, &bit))
3750 if (fetch (sd, &code->dst, &ea))
3760 if (store (sd, &code->op3, ea))
3765 case O(O_BFST, SB): /* bfst */
3766 /* bitfield store */
3767 /* NOTE: the imm8 value is in dst, and the ea value
3768 (which is actually the destination) is in op3.
3769 It has to be that way, to avoid breaking the assembler. */
3771 if (fetch (sd, &code->dst, &bit)) /* imm8 */
3773 if (bit == 0) /* noop -- nothing to do. */
3776 if (fetch (sd, &code->src, &rd)) /* reg8 src */
3779 if (fetch2 (sd, &code->op3, &ea)) /* ea dst */
3782 /* Left-shift the register data into position. */
3783 for (tmp = bit; !(tmp & 1); tmp >>= 1)
3786 /* Combine it with the neighboring bits. */
3787 ea = (ea & ~bit) | (rd & bit);
3790 if (store2 (sd, &code->op3, ea))
3794 case O (O_CLRMAC, SN): /* clrmac */
3795 h8_set_mach (sd, 0);
3796 h8_set_macl (sd, 0);
3797 h8_set_macZ (sd, 1);
3798 h8_set_macV (sd, 0);
3799 h8_set_macN (sd, 0);
3802 case O (O_STMAC, SL): /* stmac, 260 */
3803 switch (code->src.type) {
3804 case X (OP_MACH, SL):
3805 res = h8_get_mach (sd);
3806 if (res & 0x200) /* sign extend */
3809 case X (OP_MACL, SL):
3810 res = h8_get_macl (sd);
3812 default: goto illegal;
3814 nz = !h8_get_macZ (sd);
3815 n = h8_get_macN (sd);
3816 v = h8_get_macV (sd);
3818 if (store (sd, &code->dst, res))
3823 case O (O_LDMAC, SL): /* ldmac, 179 */
3824 if (fetch (sd, &code->src, &rd))
3827 switch (code->dst.type) {
3828 case X (OP_MACH, SL):
3829 rd &= 0x3ff; /* Truncate to 10 bits */
3830 h8_set_mach (sd, rd);
3832 case X (OP_MACL, SL):
3833 h8_set_macl (sd, rd);
3835 default: goto illegal;
3837 h8_set_macV (sd, 0);
3841 if (fetch (sd, &code->src, &rd) ||
3842 fetch (sd, &code->dst, &res))
3845 /* Ye gods, this is non-portable!
3846 However, the existing mul/div code is similar. */
3847 res = SEXTSHORT (res) * SEXTSHORT (rd);
3849 if (h8_get_macS (sd)) /* Saturating mode */
3851 long long mac = h8_get_macl (sd);
3853 if (mac & 0x80000000) /* sign extend */
3854 mac |= 0xffffffff00000000LL;
3857 if (mac > 0x7fffffff || mac < 0xffffffff80000000LL)
3858 h8_set_macV (sd, 1);
3859 h8_set_macZ (sd, (mac == 0));
3860 h8_set_macN (sd, (mac < 0));
3861 h8_set_macl (sd, (int) mac);
3863 else /* "Less Saturating" mode */
3865 long long mac = h8_get_mach (sd);
3867 mac += h8_get_macl (sd);
3869 if (mac & 0x20000000000LL) /* sign extend */
3870 mac |= 0xfffffc0000000000LL;
3873 if (mac > 0x1ffffffffffLL ||
3874 mac < (long long) 0xfffffe0000000000LL)
3875 h8_set_macV (sd, 1);
3876 h8_set_macZ (sd, (mac == 0));
3877 h8_set_macN (sd, (mac < 0));
3878 h8_set_macl (sd, (int) mac);
3880 h8_set_mach (sd, (int) (mac & 0x3ff));
3884 case O (O_MULS, SW): /* muls.w */
3885 if (fetch (sd, &code->src, &ea) ||
3886 fetch (sd, &code->dst, &rd))
3889 /* FIXME: is this the right place to be doing sign extend? */
3890 if (OP_KIND (code->src.type) == OP_IMM &&
3894 ea = SEXTSHORT (ea);
3896 res = SEXTSHORT (ea * SEXTSHORT (rd));
3900 if (store (sd, &code->dst, res))
3905 case O (O_MULS, SL): /* muls.l */
3906 if (fetch (sd, &code->src, &ea) ||
3907 fetch (sd, &code->dst, &rd))
3910 /* FIXME: is this the right place to be doing sign extend? */
3911 if (OP_KIND (code->src.type) == OP_IMM &&
3917 n = res & 0x80000000;
3918 nz = res & 0xffffffff;
3919 if (store (sd, &code->dst, res))
3923 case O (O_MULSU, SL): /* muls/u.l */
3924 if (fetch (sd, &code->src, &ea) ||
3925 fetch (sd, &code->dst, &rd))
3928 /* FIXME: is this the right place to be doing sign extend? */
3929 if (OP_KIND (code->src.type) == OP_IMM &&
3933 /* Compute upper 32 bits of the 64-bit result. */
3934 res = (((long long) ea) * ((long long) rd)) >> 32;
3936 n = res & 0x80000000;
3937 nz = res & 0xffffffff;
3938 if (store (sd, &code->dst, res))
3942 case O (O_MULU, SW): /* mulu.w */
3943 if (fetch (sd, &code->src, &ea) ||
3944 fetch (sd, &code->dst, &rd))
3947 res = UEXTSHORT ((UEXTSHORT (ea) * UEXTSHORT (rd)));
3949 /* Don't set Z or N. */
3950 if (store (sd, &code->dst, res))
3955 case O (O_MULU, SL): /* mulu.l */
3956 if (fetch (sd, &code->src, &ea) ||
3957 fetch (sd, &code->dst, &rd))
3962 /* Don't set Z or N. */
3963 if (store (sd, &code->dst, res))
3968 case O (O_MULUU, SL): /* mulu/u.l */
3969 if (fetch (sd, &code->src, &ea) ||
3970 fetch (sd, &code->dst, &rd))
3973 /* Compute upper 32 bits of the 64-bit result. */
3974 res = (((unsigned long long) (unsigned) ea) *
3975 ((unsigned long long) (unsigned) rd)) >> 32;
3977 /* Don't set Z or N. */
3978 if (store (sd, &code->dst, res))
3983 case O (O_MULXS, SB): /* mulxs.b */
3984 if (fetch (sd, &code->src, &ea) ||
3985 fetch (sd, &code->dst, &rd))
3988 /* FIXME: is this the right place to be doing sign extend? */
3989 if (OP_KIND (code->src.type) == OP_IMM &&
3995 res = ea * SEXTCHAR (rd);
3999 if (store (sd, &code->dst, res))
4004 case O (O_MULXS, SW): /* mulxs.w */
4005 if (fetch (sd, &code->src, &ea) ||
4006 fetch (sd, &code->dst, &rd))
4009 /* FIXME: is this the right place to be doing sign extend? */
4010 if (OP_KIND (code->src.type) == OP_IMM &&
4014 ea = SEXTSHORT (ea);
4016 res = ea * SEXTSHORT (rd & 0xffff);
4018 n = res & 0x80000000;
4019 nz = res & 0xffffffff;
4020 if (store (sd, &code->dst, res))
4025 case O (O_MULXU, SB): /* mulxu.b */
4026 if (fetch (sd, &code->src, &ea) ||
4027 fetch (sd, &code->dst, &rd))
4030 res = UEXTCHAR (ea) * UEXTCHAR (rd);
4032 if (store (sd, &code->dst, res))
4037 case O (O_MULXU, SW): /* mulxu.w */
4038 if (fetch (sd, &code->src, &ea) ||
4039 fetch (sd, &code->dst, &rd))
4042 res = UEXTSHORT (ea) * UEXTSHORT (rd);
4044 if (store (sd, &code->dst, res))
4049 case O (O_TAS, SB): /* tas (test and set) */
4050 if (!h8300sxmode) /* h8sx can use any register. */
4051 switch (code->src.reg)
4062 if (fetch (sd, &code->src, &res))
4064 if (store (sd, &code->src, res | 0x80))
4067 goto just_flags_log8;
4069 case O (O_DIVU, SW): /* divu.w */
4070 if (fetch (sd, &code->src, &ea) ||
4071 fetch (sd, &code->dst, &rd))
4077 res = (unsigned) (UEXTSHORT (rd) / UEXTSHORT (ea));
4081 if (store (sd, &code->dst, res))
4085 case O (O_DIVU, SL): /* divu.l */
4086 if (fetch (sd, &code->src, &ea) ||
4087 fetch (sd, &code->dst, &rd))
4090 n = ea & 0x80000000;
4091 nz = ea & 0xffffffff;
4093 res = (unsigned) rd / ea;
4097 if (store (sd, &code->dst, res))
4101 case O (O_DIVS, SW): /* divs.w */
4102 if (fetch (sd, &code->src, &ea) ||
4103 fetch (sd, &code->dst, &rd))
4106 /* FIXME: is this the right place to be doing sign extend? */
4107 if (OP_KIND (code->src.type) == OP_IMM &&
4113 res = SEXTSHORT (rd) / SEXTSHORT (ea);
4123 if (store (sd, &code->dst, res))
4127 case O (O_DIVS, SL): /* divs.l */
4128 if (fetch (sd, &code->src, &ea) ||
4129 fetch (sd, &code->dst, &rd))
4132 /* FIXME: is this the right place to be doing sign extend? */
4133 if (OP_KIND (code->src.type) == OP_IMM &&
4148 n = res & 0x80000000;
4149 if (store (sd, &code->dst, res))
4153 case O (O_DIVXU, SB): /* divxu.b */
4154 if (fetch (sd, &code->src, &ea) ||
4155 fetch (sd, &code->dst, &rd))
4158 rd = UEXTSHORT (rd);
4165 tmp = (unsigned) rd % ea;
4166 res = (unsigned) rd / ea;
4174 if (store (sd, &code->dst, (res & 0xff) | (tmp << 8)))
4178 case O (O_DIVXU, SW): /* divxu.w */
4179 if (fetch (sd, &code->src, &ea) ||
4180 fetch (sd, &code->dst, &rd))
4183 ea = UEXTSHORT (ea);
4189 tmp = (unsigned) rd % ea;
4190 res = (unsigned) rd / ea;
4198 if (store (sd, &code->dst, (res & 0xffff) | (tmp << 16)))
4202 case O (O_DIVXS, SB): /* divxs.b */
4203 if (fetch (sd, &code->src, &ea) ||
4204 fetch (sd, &code->dst, &rd))
4207 rd = SEXTSHORT (rd);
4209 /* FIXME: is this the right place to be doing sign extend? */
4210 if (OP_KIND (code->src.type) == OP_IMM &&
4218 tmp = (int) rd % (int) ea;
4219 res = (int) rd / (int) ea;
4230 if (store (sd, &code->dst, (res & 0xff) | (tmp << 8)))
4234 case O (O_DIVXS, SW): /* divxs.w */
4235 if (fetch (sd, &code->src, &ea) ||
4236 fetch (sd, &code->dst, &rd))
4239 /* FIXME: is this the right place to be doing sign extend? */
4240 if (OP_KIND (code->src.type) == OP_IMM &&
4244 ea = SEXTSHORT (ea);
4248 tmp = (int) rd % (int) ea;
4249 res = (int) rd / (int) ea;
4259 n = res & 0x80000000;
4260 if (store (sd, &code->dst, (res & 0xffff) | (tmp << 16)))
4264 case O (O_EXTS, SW): /* exts.w, signed extend */
4265 if (fetch2 (sd, &code->dst, &rd))
4267 ea = rd & 0x80 ? -256 : 0;
4268 res = (rd & 0xff) + ea;
4271 case O (O_EXTS, SL): /* exts.l, signed extend */
4272 if (fetch2 (sd, &code->dst, &rd))
4274 if (code->src.type == X (OP_IMM, SL))
4276 if (fetch (sd, &code->src, &ea))
4279 if (ea == 2) /* exts.l #2, nn */
4281 /* Sign-extend from 8-bit to 32-bit. */
4282 ea = rd & 0x80 ? -256 : 0;
4283 res = (rd & 0xff) + ea;
4287 /* Sign-extend from 16-bit to 32-bit. */
4288 ea = rd & 0x8000 ? -65536 : 0;
4289 res = (rd & 0xffff) + ea;
4292 case O (O_EXTU, SW): /* extu.w, unsigned extend */
4293 if (fetch2 (sd, &code->dst, &rd))
4296 res = (rd & 0xff) + ea;
4299 case O (O_EXTU, SL): /* extu.l, unsigned extend */
4300 if (fetch2 (sd, &code->dst, &rd))
4302 if (code->src.type == X (OP_IMM, SL))
4304 if (fetch (sd, &code->src, &ea))
4307 if (ea == 2) /* extu.l #2, nn */
4309 /* Zero-extend from 8-bit to 32-bit. */
4311 res = (rd & 0xff) + ea;
4315 /* Zero-extend from 16-bit to 32-bit. */
4317 res = (rd & 0xffff) + ea;
4320 case O (O_NOP, SN): /* nop */
4323 case O (O_STM, SL): /* stm, store to memory */
4325 int nregs, firstreg, i;
4327 nregs = GET_MEMORY_B (pc + 1);
4330 firstreg = code->src.reg;
4332 for (i = firstreg; i <= firstreg + nregs; i++)
4334 h8_set_reg (sd, SP_REGNUM, h8_get_reg (sd, SP_REGNUM) - 4);
4335 SET_MEMORY_L (h8_get_reg (sd, SP_REGNUM), h8_get_reg (sd, i));
4340 case O (O_LDM, SL): /* ldm, load from memory */
4341 case O (O_RTEL, SN): /* rte/l, ldm plus rte */
4342 case O (O_RTSL, SN): /* rts/l, ldm plus rts */
4344 int nregs, firstreg, i;
4346 nregs = ((GET_MEMORY_B (pc + 1) >> 4) & 0xf);
4347 firstreg = code->dst.reg & 0xf;
4348 for (i = firstreg; i >= firstreg - nregs; i--)
4350 h8_set_reg (sd, i, GET_MEMORY_L (h8_get_reg (sd, SP_REGNUM)));
4351 h8_set_reg (sd, SP_REGNUM, h8_get_reg (sd, SP_REGNUM) + 4);
4354 switch (code->opcode) {
4355 case O (O_RTEL, SN):
4357 case O (O_RTSL, SN):
4366 /* Decimal Adjust Addition. This is for BCD arithmetic. */
4367 res = GET_B_REG (code->src.reg); /* FIXME fetch? */
4368 if (!c && (0 <= (res >> 4) && (res >> 4) <= 9) &&
4369 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4370 res = res; /* Value added == 0. */
4371 else if (!c && (0 <= (res >> 4) && (res >> 4) <= 8) &&
4372 !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
4373 res = res + 0x6; /* Value added == 6. */
4374 else if (!c && (0 <= (res >> 4) && (res >> 4) <= 9) &&
4375 h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
4376 res = res + 0x6; /* Value added == 6. */
4377 else if (!c && (10 <= (res >> 4) && (res >> 4) <= 15) &&
4378 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4379 res = res + 0x60; /* Value added == 60. */
4380 else if (!c && (9 <= (res >> 4) && (res >> 4) <= 15) &&
4381 !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
4382 res = res + 0x66; /* Value added == 66. */
4383 else if (!c && (10 <= (res >> 4) && (res >> 4) <= 15) &&
4384 h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
4385 res = res + 0x66; /* Value added == 66. */
4386 else if ( c && (1 <= (res >> 4) && (res >> 4) <= 2) &&
4387 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4388 res = res + 0x60; /* Value added == 60. */
4389 else if ( c && (1 <= (res >> 4) && (res >> 4) <= 2) &&
4390 !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
4391 res = res + 0x66; /* Value added == 66. */
4392 else if (c && (1 <= (res >> 4) && (res >> 4) <= 3) &&
4393 h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
4394 res = res + 0x66; /* Value added == 66. */
4399 /* Decimal Adjust Subtraction. This is for BCD arithmetic. */
4400 res = GET_B_REG (code->src.reg); /* FIXME fetch, fetch2... */
4401 if (!c && (0 <= (res >> 4) && (res >> 4) <= 9) &&
4402 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4403 res = res; /* Value added == 0. */
4404 else if (!c && (0 <= (res >> 4) && (res >> 4) <= 8) &&
4405 h && (6 <= (res & 0xf) && (res & 0xf) <= 15))
4406 res = res + 0xfa; /* Value added == 0xfa. */
4407 else if ( c && (7 <= (res >> 4) && (res >> 4) <= 15) &&
4408 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4409 res = res + 0xa0; /* Value added == 0xa0. */
4410 else if (c && (6 <= (res >> 4) && (res >> 4) <= 15) &&
4411 h && (6 <= (res & 0xf) && (res & 0xf) <= 15))
4412 res = res + 0x9a; /* Value added == 0x9a. */
4418 sim_engine_set_run_state (sd, sim_stopped, SIGILL);
4423 (*sim_callback->printf_filtered) (sim_callback,
4424 "sim_resume: internal error.\n");
4425 sim_engine_set_run_state (sd, sim_stopped, SIGILL);
4429 if (code->dst.type == X (OP_CCR, SB) ||
4430 code->dst.type == X (OP_CCR, SW))
4432 h8_set_ccr (sd, res);
4435 else if (h8300smode &&
4436 (code->dst.type == X (OP_EXR, SB) ||
4437 code->dst.type == X (OP_EXR, SW)))
4439 h8_set_exr (sd, res);
4440 if (h8300smode) /* Get exr. */
4442 trace = (h8_get_exr (sd) >> 7) & 1;
4443 intMask = h8_get_exr (sd) & 7;
4452 /* When a branch works */
4453 if (fetch (sd, &code->src, &res))
4455 if (res & 1) /* bad address */
4457 pc = code->next_pc + res;
4460 /* Set the cond codes from res */
4463 /* Set the flags after an 8 bit inc/dec operation */
4467 v = (rd & 0x7f) == 0x7f;
4470 /* Set the flags after an 16 bit inc/dec operation */
4474 v = (rd & 0x7fff) == 0x7fff;
4477 /* Set the flags after an 32 bit inc/dec operation */
4479 n = res & 0x80000000;
4480 nz = res & 0xffffffff;
4481 v = (rd & 0x7fffffff) == 0x7fffffff;
4485 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
4488 if (store2 (sd, &code->dst, rd))
4493 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
4496 if (store2 (sd, &code->dst, rd))
4501 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
4502 n = (rd & 0x80000000);
4503 nz = rd & 0xffffffff;
4504 if (store2 (sd, &code->dst, rd))
4509 if (store2 (sd, &code->dst, res))
4513 /* flags after a 32bit logical operation */
4514 n = res & 0x80000000;
4515 nz = res & 0xffffffff;
4520 if (store2 (sd, &code->dst, res))
4524 /* flags after a 16bit logical operation */
4531 if (store2 (sd, &code->dst, res))
4541 if (store2 (sd, &code->dst, res))
4548 switch (code->opcode / 4)
4552 v = ((rd & 0x80) == (ea & 0x80)
4553 && (rd & 0x80) != (res & 0x80));
4558 v = ((rd & 0x80) != (-ea & 0x80)
4559 && (rd & 0x80) != (res & 0x80));
4566 break; /* No effect on v flag. */
4571 if (store2 (sd, &code->dst, res))
4577 c = (res & 0x10000);
4578 switch (code->opcode / 4)
4582 v = ((rd & 0x8000) == (ea & 0x8000)
4583 && (rd & 0x8000) != (res & 0x8000));
4588 v = ((rd & 0x8000) != (-ea & 0x8000)
4589 && (rd & 0x8000) != (res & 0x8000));
4598 if (store2 (sd, &code->dst, res))
4602 n = res & 0x80000000;
4603 nz = res & 0xffffffff;
4604 switch (code->opcode / 4)
4608 v = ((rd & 0x80000000) == (ea & 0x80000000)
4609 && (rd & 0x80000000) != (res & 0x80000000));
4610 c = ((unsigned) res < (unsigned) rd) ||
4611 ((unsigned) res < (unsigned) ea);
4616 v = ((rd & 0x80000000) != (-ea & 0x80000000)
4617 && (rd & 0x80000000) != (res & 0x80000000));
4618 c = (unsigned) rd < (unsigned) -ea;
4621 v = (rd == 0x80000000);
4628 if ((res = h8_get_delayed_branch (sd)) != 0)
4631 h8_set_delayed_branch (sd, 0);
4638 if (--poll_count < 0)
4640 poll_count = POLL_QUIT_INTERVAL;
4641 if ((*sim_callback->poll_quit) != NULL
4642 && (*sim_callback->poll_quit) (sim_callback))
4643 sim_engine_set_run_state (sd, sim_stopped, SIGINT);
4645 sim_engine_get_run_state (sd, &reason, &sigrc);
4646 } while (reason == sim_running);
4648 h8_set_ticks (sd, h8_get_ticks (sd) + get_now () - tick_start);
4649 h8_set_cycles (sd, h8_get_cycles (sd) + cycles);
4650 h8_set_insts (sd, h8_get_insts (sd) + insts);
4655 h8_set_exr (sd, (trace<<7) | intMask);
4657 h8_set_mask (sd, oldmask);
4658 signal (SIGINT, prev);
4662 sim_trace (SIM_DESC sd)
4664 /* FIXME: Unfinished. */
4665 (*sim_callback->printf_filtered) (sim_callback,
4666 "sim_trace: trace not supported.\n");
4667 return 1; /* Done. */
4671 sim_write (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
4678 for (i = 0; i < size; i++)
4680 if (addr < memory_size)
4682 h8_set_memory (sd, addr + i, buffer[i]);
4683 h8_set_cache_idx (sd, addr + i, 0);
4687 h8_set_eightbit (sd, (addr + i) & 0xff, buffer[i]);
4694 sim_read (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
4699 if (addr < memory_size)
4700 memcpy (buffer, h8_get_memory_buf (sd) + addr, size);
4702 memcpy (buffer, h8_get_eightbit_buf (sd) + (addr & 0xff), size);
4708 sim_store_register (SIM_DESC sd, int rn, unsigned char *value, int length)
4713 longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
4714 shortval = (value[0] << 8) | (value[1]);
4715 intval = h8300hmode ? longval : shortval;
4721 if(h8300_normal_mode)
4722 h8_set_pc (sd, shortval); /* PC for Normal mode is 2 bytes */
4724 h8_set_pc (sd, intval);
4727 (*sim_callback->printf_filtered) (sim_callback,
4728 "sim_store_register: bad regnum %d.\n",
4738 h8_set_reg (sd, rn, intval);
4741 h8_set_ccr (sd, intval);
4744 h8_set_exr (sd, intval);
4747 h8_set_sbr (sd, intval);
4750 h8_set_vbr (sd, intval);
4753 h8_set_mach (sd, intval);
4756 h8_set_macl (sd, intval);
4759 h8_set_cycles (sd, longval);
4763 h8_set_insts (sd, longval);
4767 h8_set_ticks (sd, longval);
4774 sim_fetch_register (SIM_DESC sd, int rn, unsigned char *buf, int length)
4781 if (!h8300smode && rn >= EXR_REGNUM)
4786 (*sim_callback->printf_filtered) (sim_callback,
4787 "sim_fetch_register: bad regnum %d.\n",
4792 v = h8_get_ccr (sd);
4795 v = h8_get_exr (sd);
4801 v = h8_get_sbr (sd);
4804 v = h8_get_vbr (sd);
4807 v = h8_get_mach (sd);
4810 v = h8_get_macl (sd);
4820 v = h8_get_reg (sd, rn);
4823 v = h8_get_cycles (sd);
4827 v = h8_get_ticks (sd);
4831 v = h8_get_insts (sd);
4835 /* In Normal mode PC is 2 byte, but other registers are 4 byte */
4836 if ((h8300hmode || longreg) && !(rn == PC_REGNUM && h8300_normal_mode))
4852 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
4854 sim_engine_get_run_state (sd, reason, sigrc);
4857 /* FIXME: Rename to sim_set_mem_size. */
4862 /* Memory size is fixed. */
4866 set_simcache_size (SIM_DESC sd, int n)
4869 free (sd->sim_cache);
4872 sd->sim_cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
4873 memset (sd->sim_cache, 0, sizeof (decoded_inst) * n);
4874 sd->sim_cache_size = n;
4879 sim_info (SIM_DESC sd, int verbose)
4881 double timetaken = (double) h8_get_ticks (sd) / (double) now_persec ();
4882 double virttime = h8_get_cycles (sd) / 10.0e6;
4884 (*sim_callback->printf_filtered) (sim_callback,
4885 "\n\n#instructions executed %10d\n",
4887 (*sim_callback->printf_filtered) (sim_callback,
4888 "#cycles (v approximate) %10d\n",
4889 h8_get_cycles (sd));
4890 (*sim_callback->printf_filtered) (sim_callback,
4891 "#real time taken %10.4f\n",
4893 (*sim_callback->printf_filtered) (sim_callback,
4894 "#virtual time taken %10.4f\n",
4896 if (timetaken != 0.0)
4897 (*sim_callback->printf_filtered) (sim_callback,
4898 "#simulation ratio %10.4f\n",
4899 virttime / timetaken);
4900 (*sim_callback->printf_filtered) (sim_callback,
4902 h8_get_compiles (sd));
4903 (*sim_callback->printf_filtered) (sim_callback,
4904 "#cache size %10d\n",
4905 sd->sim_cache_size);
4908 /* This to be conditional on `what' (aka `verbose'),
4909 however it was never passed as non-zero. */
4913 for (i = 0; i < O_LAST; i++)
4915 if (h8_get_stats (sd, i))
4916 (*sim_callback->printf_filtered) (sim_callback, "%d: %d\n",
4917 i, h8_get_stats (sd, i));
4923 /* Indicate whether the cpu is an H8/300 or H8/300H.
4924 FLAG is non-zero for the H8/300H. */
4927 set_h8300h (unsigned long machine)
4929 /* FIXME: Much of the code in sim_load can be moved to sim_open.
4930 This function being replaced by a sim_open:ARGV configuration
4933 if (machine == bfd_mach_h8300sx || machine == bfd_mach_h8300sxn)
4936 if (machine == bfd_mach_h8300s || machine == bfd_mach_h8300sn || h8300sxmode)
4939 if (machine == bfd_mach_h8300h || machine == bfd_mach_h8300hn || h8300smode)
4942 if(machine == bfd_mach_h8300hn || machine == bfd_mach_h8300sn || machine == bfd_mach_h8300sxn)
4943 h8300_normal_mode = 1;
4946 /* Cover function of sim_state_free to free the cpu buffers as well. */
4949 free_state (SIM_DESC sd)
4951 if (STATE_MODULES (sd) != NULL)
4952 sim_module_uninstall (sd);
4954 /* Fixme: free buffers in _sim_cpu. */
4955 sim_state_free (sd);
4959 sim_open (SIM_OPEN_KIND kind,
4960 struct host_callback_struct *callback,
4967 sd = sim_state_alloc (kind, callback);
4968 sd->cpu = sim_cpu_alloc (sd, 0);
4969 cpu = STATE_CPU (sd, 0);
4970 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
4971 sim_state_initialize (sd, cpu);
4972 /* sim_cpu object is new, so some initialization is needed. */
4973 init_pointers_needed = 1;
4975 /* For compatibility (FIXME: is this right?). */
4976 current_alignment = NONSTRICT_ALIGNMENT;
4977 current_target_byte_order = BIG_ENDIAN;
4979 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
4985 /* getopt will print the error message so we just have to exit if
4986 this fails. FIXME: Hmmm... in the case of gdb we need getopt
4987 to call print_filtered. */
4988 if (sim_parse_args (sd, argv) != SIM_RC_OK)
4990 /* Uninstall the modules to avoid memory leaks,
4991 file descriptor leaks, etc. */
4996 /* Check for/establish the a reference program image. */
4997 if (sim_analyze_program (sd,
4998 (STATE_PROG_ARGV (sd) != NULL
4999 ? *STATE_PROG_ARGV (sd)
5000 : NULL), abfd) != SIM_RC_OK)
5006 /* Establish any remaining configuration options. */
5007 if (sim_config (sd) != SIM_RC_OK)
5013 if (sim_post_argv_init (sd) != SIM_RC_OK)
5015 /* Uninstall the modules to avoid memory leaks,
5016 file descriptor leaks, etc. */
5021 /* sim_hw_configure (sd); */
5023 /* FIXME: Much of the code in sim_load can be moved here. */
5027 sim_callback = callback;
5032 sim_close (SIM_DESC sd, int quitting)
5034 /* Nothing to do. */
5037 /* Called by gdb to load a program into memory. */
5040 sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
5044 /* FIXME: The code below that sets a specific variant of the H8/300
5045 being simulated should be moved to sim_open(). */
5047 /* See if the file is for the H8/300 or H8/300H. */
5048 /* ??? This may not be the most efficient way. The z8k simulator
5049 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
5053 prog_bfd = bfd_openr (prog, "coff-h8300");
5054 if (prog_bfd != NULL)
5056 /* Set the cpu type. We ignore failure from bfd_check_format
5057 and bfd_openr as sim_load_file checks too. */
5058 if (bfd_check_format (prog_bfd, bfd_object))
5060 set_h8300h (bfd_get_mach (prog_bfd));
5064 /* If we're using gdb attached to the simulator, then we have to
5065 reallocate memory for the simulator.
5067 When gdb first starts, it calls fetch_registers (among other
5068 functions), which in turn calls init_pointers, which allocates
5071 The problem is when we do that, we don't know whether we're
5072 debugging an H8/300 or H8/300H program.
5074 This is the first point at which we can make that determination,
5075 so we just reallocate memory now; this will also allow us to handle
5076 switching between H8/300 and H8/300H programs without exiting
5079 if (h8300smode && !h8300_normal_mode)
5080 memory_size = H8300S_MSIZE;
5081 else if (h8300hmode && !h8300_normal_mode)
5082 memory_size = H8300H_MSIZE;
5084 memory_size = H8300_MSIZE;
5086 if (h8_get_memory_buf (sd))
5087 free (h8_get_memory_buf (sd));
5088 if (h8_get_cache_idx_buf (sd))
5089 free (h8_get_cache_idx_buf (sd));
5090 if (h8_get_eightbit_buf (sd))
5091 free (h8_get_eightbit_buf (sd));
5093 h8_set_memory_buf (sd, (unsigned char *)
5094 calloc (sizeof (char), memory_size));
5095 h8_set_cache_idx_buf (sd, (unsigned short *)
5096 calloc (sizeof (short), memory_size));
5097 h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
5099 /* `msize' must be a power of two. */
5100 if ((memory_size & (memory_size - 1)) != 0)
5102 (*sim_callback->printf_filtered) (sim_callback,
5103 "sim_load: bad memory size.\n");
5106 h8_set_mask (sd, memory_size - 1);
5108 if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd,
5109 sim_kind == SIM_OPEN_DEBUG,
5113 /* Close the bfd if we opened it. */
5114 if (abfd == NULL && prog_bfd != NULL)
5115 bfd_close (prog_bfd);
5119 /* Close the bfd if we opened it. */
5120 if (abfd == NULL && prog_bfd != NULL)
5121 bfd_close (prog_bfd);
5126 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
5133 h8_set_pc (sd, bfd_get_start_address (abfd));
5137 /* Command Line support. */
5140 /* Counting the no. of commandline arguments. */
5141 for (no_of_args = 0; argv[no_of_args] != NULL; no_of_args++)
5144 /* Allocating memory for the argv pointers. */
5145 h8_set_command_line (sd, (char **) malloc ((sizeof (char *))
5146 * (no_of_args + 1)));
5148 for (i = 0; i < no_of_args; i++)
5150 /* Copying the argument string. */
5151 h8_set_cmdline_arg (sd, i, (char *) strdup (argv[i]));
5153 h8_set_cmdline_arg (sd, i, NULL);
5160 sim_do_command (SIM_DESC sd, char *cmd)
5162 (*sim_callback->printf_filtered) (sim_callback,
5163 "This simulator does not accept any commands.\n");
5167 sim_set_callbacks (struct host_callback_struct *ptr)