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 const 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 const 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, (unsigned int *)&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, (unsigned int *)&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 r = GET_MEMORY_B (t & h8_get_mask (sd));
1395 case X (OP_POSTINC, SW): /* Register indirect w/post-incr: word. */
1397 r = GET_MEMORY_W (t & h8_get_mask (sd));
1403 case X (OP_POSTINC, SL): /* Register indirect w/post-incr: long. */
1405 r = GET_MEMORY_L (t & h8_get_mask (sd));
1412 case X (OP_POSTDEC, SB): /* Register indirect w/post-decr: byte. */
1414 r = GET_MEMORY_B (t & h8_get_mask (sd));
1420 case X (OP_POSTDEC, SW): /* Register indirect w/post-decr: word. */
1422 r = GET_MEMORY_W (t & h8_get_mask (sd));
1428 case X (OP_POSTDEC, SL): /* Register indirect w/post-decr: long. */
1430 r = GET_MEMORY_L (t & h8_get_mask (sd));
1437 case X (OP_PREDEC, SB): /* Register indirect w/pre-decr: byte. */
1438 t = GET_L_REG (rn) - 1;
1440 t &= h8_get_mask (sd);
1441 *val = GET_MEMORY_B (t);
1444 case X (OP_PREDEC, SW): /* Register indirect w/pre-decr: word. */
1445 t = GET_L_REG (rn) - 2;
1447 t &= h8_get_mask (sd);
1448 *val = GET_MEMORY_W (t);
1451 case X (OP_PREDEC, SL): /* Register indirect w/pre-decr: long. */
1452 t = GET_L_REG (rn) - 4;
1454 t &= h8_get_mask (sd);
1455 *val = GET_MEMORY_L (t);
1458 case X (OP_PREINC, SB): /* Register indirect w/pre-incr: byte. */
1459 t = GET_L_REG (rn) + 1;
1461 t &= h8_get_mask (sd);
1462 *val = GET_MEMORY_B (t);
1465 case X (OP_PREINC, SW): /* Register indirect w/pre-incr: long. */
1466 t = GET_L_REG (rn) + 2;
1468 t &= h8_get_mask (sd);
1469 *val = GET_MEMORY_W (t);
1472 case X (OP_PREINC, SL): /* Register indirect w/pre-incr: long. */
1473 t = GET_L_REG (rn) + 4;
1475 t &= h8_get_mask (sd);
1476 *val = GET_MEMORY_L (t);
1479 case X (OP_DISP, SB): /* Register indirect w/displacement: byte. */
1480 t = GET_L_REG (rn) + abs;
1481 t &= h8_get_mask (sd);
1482 *val = GET_MEMORY_B (t);
1485 case X (OP_DISP, SW): /* Register indirect w/displacement: word. */
1486 t = GET_L_REG (rn) + abs;
1487 t &= h8_get_mask (sd);
1488 *val = GET_MEMORY_W (t);
1491 case X (OP_DISP, SL): /* Register indirect w/displacement: long. */
1492 t = GET_L_REG (rn) + abs;
1493 t &= h8_get_mask (sd);
1494 *val =GET_MEMORY_L (t);
1497 case X (OP_MEM, SL): /* Absolute memory address, long. */
1498 t = GET_MEMORY_L (abs);
1499 t &= h8_get_mask (sd);
1503 case X (OP_MEM, SW): /* Absolute memory address, word. */
1504 t = GET_MEMORY_W (abs);
1505 t &= h8_get_mask (sd);
1509 case X (OP_PCREL, SB): /* PC relative (for jump, branch etc). */
1510 case X (OP_PCREL, SW):
1511 case X (OP_PCREL, SL):
1512 case X (OP_PCREL, SN):
1516 case X (OP_MEM, SB): /* Why isn't this implemented? */
1518 sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
1521 return 0; /* Success. */
1527 fetch (SIM_DESC sd, ea_type *arg, int *val)
1529 return fetch_1 (sd, arg, val, 0);
1532 /* Fetch which will be followed by a store to the same location.
1533 The difference being that we don't want to do a post-increment
1534 or post-decrement at this time: we'll do it when we store. */
1537 fetch2 (SIM_DESC sd, ea_type *arg, int *val)
1539 return fetch_1 (sd, arg, val, 1);
1542 /* Simulate a memory store.
1543 Return 0 for success, -1 for failure.
1547 store_1 (SIM_DESC sd, ea_type *arg, int n, int twice)
1550 int abs = arg->literal;
1555 /* Indexed register plus displacement mode:
1557 This new family of addressing modes are similar to OP_DISP
1558 (register plus displacement), with two differences:
1559 1) INDEXB uses only the least significant byte of the register,
1560 INDEXW uses only the least significant word, and
1561 INDEXL uses the entire register (just like OP_DISP).
1563 2) The displacement value in abs is multiplied by two
1564 for SW-sized operations, and by four for SL-size.
1566 This gives nine possible variations.
1569 case X (OP_INDEXB, SB):
1570 case X (OP_INDEXB, SW):
1571 case X (OP_INDEXB, SL):
1572 case X (OP_INDEXW, SB):
1573 case X (OP_INDEXW, SW):
1574 case X (OP_INDEXW, SL):
1575 case X (OP_INDEXL, SB):
1576 case X (OP_INDEXL, SW):
1577 case X (OP_INDEXL, SL):
1579 switch (OP_KIND (arg->type)) {
1580 case OP_INDEXB: t &= 0xff; break;
1581 case OP_INDEXW: t &= 0xffff; break;
1585 switch (OP_SIZE (arg->type)) {
1587 SET_MEMORY_B ((t * 1 + abs) & h8_get_mask (sd), n);
1590 SET_MEMORY_W ((t * 2 + abs) & h8_get_mask (sd), n);
1593 SET_MEMORY_L ((t * 4 + abs) & h8_get_mask (sd), n);
1598 case X (OP_REG, SB): /* Register direct, byte. */
1601 case X (OP_REG, SW): /* Register direct, word. */
1604 case X (OP_REG, SL): /* Register direct, long. */
1608 case X (OP_PREDEC, SB): /* Register indirect w/pre-decr, byte. */
1613 t &= h8_get_mask (sd);
1614 SET_MEMORY_B (t, n);
1617 case X (OP_PREDEC, SW): /* Register indirect w/pre-decr, word. */
1622 t &= h8_get_mask (sd);
1623 SET_MEMORY_W (t, n);
1626 case X (OP_PREDEC, SL): /* Register indirect w/pre-decr, long. */
1631 t &= h8_get_mask (sd);
1632 SET_MEMORY_L (t, n);
1635 case X (OP_PREINC, SB): /* Register indirect w/pre-incr, byte. */
1640 t &= h8_get_mask (sd);
1641 SET_MEMORY_B (t, n);
1644 case X (OP_PREINC, SW): /* Register indirect w/pre-incr, word. */
1649 t &= h8_get_mask (sd);
1650 SET_MEMORY_W (t, n);
1653 case X (OP_PREINC, SL): /* Register indirect w/pre-incr, long. */
1658 t &= h8_get_mask (sd);
1659 SET_MEMORY_L (t, n);
1662 case X (OP_POSTDEC, SB): /* Register indirect w/post-decr, byte. */
1664 SET_L_REG (rn, t - 1);
1665 t &= h8_get_mask (sd);
1666 SET_MEMORY_B (t, n);
1669 case X (OP_POSTDEC, SW): /* Register indirect w/post-decr, word. */
1671 SET_L_REG (rn, t - 2);
1672 t &= h8_get_mask (sd);
1673 SET_MEMORY_W (t, n);
1676 case X (OP_POSTDEC, SL): /* Register indirect w/post-decr, long. */
1678 SET_L_REG (rn, t - 4);
1679 t &= h8_get_mask (sd);
1680 SET_MEMORY_L (t, n);
1683 case X (OP_POSTINC, SB): /* Register indirect w/post-incr, byte. */
1685 SET_L_REG (rn, t + 1);
1686 t &= h8_get_mask (sd);
1687 SET_MEMORY_B (t, n);
1690 case X (OP_POSTINC, SW): /* Register indirect w/post-incr, word. */
1692 SET_L_REG (rn, t + 2);
1693 t &= h8_get_mask (sd);
1694 SET_MEMORY_W (t, n);
1697 case X (OP_POSTINC, SL): /* Register indirect w/post-incr, long. */
1699 SET_L_REG (rn, t + 4);
1700 t &= h8_get_mask (sd);
1701 SET_MEMORY_L (t, n);
1704 case X (OP_DISP, SB): /* Register indirect w/displacement, byte. */
1705 t = GET_L_REG (rn) + abs;
1706 t &= h8_get_mask (sd);
1707 SET_MEMORY_B (t, n);
1710 case X (OP_DISP, SW): /* Register indirect w/displacement, word. */
1711 t = GET_L_REG (rn) + abs;
1712 t &= h8_get_mask (sd);
1713 SET_MEMORY_W (t, n);
1716 case X (OP_DISP, SL): /* Register indirect w/displacement, long. */
1717 t = GET_L_REG (rn) + abs;
1718 t &= h8_get_mask (sd);
1719 SET_MEMORY_L (t, n);
1723 case X (OP_MEM, SB): /* Why isn't this implemented? */
1724 case X (OP_MEM, SW): /* Why isn't this implemented? */
1725 case X (OP_MEM, SL): /* Why isn't this implemented? */
1727 sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
1736 store (SIM_DESC sd, ea_type *arg, int n)
1738 return store_1 (sd, arg, n, 0);
1741 /* Store which follows a fetch from the same location.
1742 The difference being that we don't want to do a pre-increment
1743 or pre-decrement at this time: it was already done when we fetched. */
1746 store2 (SIM_DESC sd, ea_type *arg, int n)
1748 return store_1 (sd, arg, n, 1);
1762 /* Flag to be set whenever a new SIM_DESC object is created. */
1763 static int init_pointers_needed = 1;
1766 init_pointers (SIM_DESC sd)
1768 if (init_pointers_needed)
1774 if (h8300smode && !h8300_normal_mode)
1775 memory_size = H8300S_MSIZE;
1776 else if (h8300hmode && !h8300_normal_mode)
1777 memory_size = H8300H_MSIZE;
1779 memory_size = H8300_MSIZE;
1780 /* `msize' must be a power of two. */
1781 if ((memory_size & (memory_size - 1)) != 0)
1783 (*sim_callback->printf_filtered)
1785 "init_pointers: bad memory size %d, defaulting to %d.\n",
1786 memory_size, memory_size = H8300S_MSIZE);
1789 if (h8_get_memory_buf (sd))
1790 free (h8_get_memory_buf (sd));
1791 if (h8_get_cache_idx_buf (sd))
1792 free (h8_get_cache_idx_buf (sd));
1793 if (h8_get_eightbit_buf (sd))
1794 free (h8_get_eightbit_buf (sd));
1796 h8_set_memory_buf (sd, (unsigned char *)
1797 calloc (sizeof (char), memory_size));
1798 h8_set_cache_idx_buf (sd, (unsigned short *)
1799 calloc (sizeof (short), memory_size));
1800 sd->memory_size = memory_size;
1801 h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
1803 h8_set_mask (sd, memory_size - 1);
1805 memset (h8_get_reg_buf (sd), 0, sizeof (((STATE_CPU (sd, 0))->regs)));
1807 for (i = 0; i < 8; i++)
1809 /* FIXME: rewrite using local buffer. */
1810 unsigned char *p = (unsigned char *) (h8_get_reg_buf (sd) + i);
1811 unsigned char *e = (unsigned char *) (h8_get_reg_buf (sd) + i + 1);
1812 unsigned short *q = (unsigned short *) (h8_get_reg_buf (sd) + i);
1813 unsigned short *u = (unsigned short *) (h8_get_reg_buf (sd) + i + 1);
1814 h8_set_reg (sd, i, 0x00112233);
1829 wreg[i] = wreg[i + 8] = 0;
1843 if (wreg[i] == 0 || wreg[i + 8] == 0)
1844 (*sim_callback->printf_filtered) (sim_callback,
1845 "init_pointers: internal error.\n");
1847 h8_set_reg (sd, i, 0);
1848 lreg[i] = h8_get_reg_buf (sd) + i;
1851 /* Note: sim uses pseudo-register ZERO as a zero register. */
1852 lreg[ZERO_REGNUM] = h8_get_reg_buf (sd) + ZERO_REGNUM;
1853 init_pointers_needed = 0;
1855 /* Initialize the seg registers. */
1857 set_simcache_size (sd, CSIZE);
1861 /* Grotty global variable for use by control_c signal handler. */
1862 static SIM_DESC control_c_sim_desc;
1867 sim_engine_set_run_state (control_c_sim_desc, sim_stopped, SIGINT);
1871 sim_stop (SIM_DESC sd)
1873 /* FIXME: use a real signal value. */
1874 sim_engine_set_run_state (sd, sim_stopped, SIGINT);
1878 #define OBITOP(name, f, s, op) \
1879 case O (name, SB): \
1884 if (fetch (sd, &code->dst, &ea)) \
1886 if (fetch (sd, &code->src, &tmp)) \
1888 m = 1 << (tmp & 7); \
1891 if (store (sd, &code->dst,ea)) \
1897 sim_resume (SIM_DESC sd, int step, int siggnal)
1902 int tick_start = get_now ();
1911 int c, nz, v, n, u, h, ui, intMaskBit;
1914 enum sim_stop reason;
1919 control_c_sim_desc = sd;
1920 prev = signal (SIGINT, control_c);
1924 sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
1928 sim_engine_set_run_state (sd, sim_running, 0);
1931 pc = h8_get_pc (sd);
1933 /* The PC should never be odd. */
1936 sim_engine_set_run_state (sd, sim_stopped, SIGBUS);
1940 /* Get Status Register (flags). */
1943 if (h8300smode) /* Get exr. */
1945 trace = (h8_get_exr (sd) >> 7) & 1;
1946 intMask = h8_get_exr (sd) & 7;
1949 oldmask = h8_get_mask (sd);
1950 if (!h8300hmode || h8300_normal_mode)
1951 h8_set_mask (sd, 0xffff);
1954 unsigned short cidx;
1958 cidx = h8_get_cache_idx (sd, pc);
1959 if (cidx == (unsigned short) -1 ||
1960 cidx >= sd->sim_cache_size)
1963 code = sd->sim_cache + cidx;
1968 printf ("%x %d %s\n", pc, code->opcode,
1969 code->op ? code->op->name : "**");
1971 h8_increment_stats (sd, code->opcode);
1976 cycles += code->cycles;
1980 switch (code->opcode)
1984 * This opcode is a fake for when we get to an
1985 * instruction which hasnt been compiled
1991 case O (O_MOVAB, SL):
1992 case O (O_MOVAW, SL):
1993 case O (O_MOVAL, SL):
1994 /* 1) Evaluate 2nd argument (dst).
1995 2) Mask / zero extend according to whether 1st argument (src)
1996 is INDEXB, INDEXW, or INDEXL.
1997 3) Left-shift the result by 0, 1 or 2, according to size of mova
1998 (mova/b, mova/w, mova/l).
1999 4) Add literal value of 1st argument (src).
2000 5) Store result in 3rd argument (op3).
2003 /* Alas, since this is the only instruction with 3 arguments,
2004 decode doesn't handle them very well. Some fix-up is required.
2006 a) The size of dst is determined by whether src is
2007 INDEXB or INDEXW. */
2009 if (OP_KIND (code->src.type) == OP_INDEXB)
2010 code->dst.type = X (OP_KIND (code->dst.type), SB);
2011 else if (OP_KIND (code->src.type) == OP_INDEXW)
2012 code->dst.type = X (OP_KIND (code->dst.type), SW);
2014 /* b) If op3 == null, then this is the short form of the insn.
2015 Dst is the dispreg of src, and op3 is the 32-bit form
2016 of the same register.
2019 if (code->op3.type == 0)
2021 /* Short form: src == INDEXB/INDEXW, dst == op3 == 0.
2022 We get to compose dst and op3 as follows:
2024 op3 is a 32-bit register, ID == src.reg.
2025 dst is the same register, but 8 or 16 bits
2026 depending on whether src is INDEXB or INDEXW.
2029 code->op3.type = X (OP_REG, SL);
2030 code->op3.reg = code->src.reg;
2031 code->op3.literal = 0;
2033 if (OP_KIND (code->src.type) == OP_INDEXB)
2035 code->dst.type = X (OP_REG, SB);
2036 code->dst.reg = code->op3.reg + 8;
2039 code->dst.type = X (OP_REG, SW);
2042 if (fetch (sd, &code->dst, &ea))
2045 switch (OP_KIND (code->src.type)) {
2046 case OP_INDEXB: ea = ea & 0xff; break;
2047 case OP_INDEXW: ea = ea & 0xffff; break;
2048 case OP_INDEXL: break;
2049 default: goto illegal;
2052 switch (code->opcode) {
2053 case O (O_MOVAB, SL): break;
2054 case O (O_MOVAW, SL): ea = ea << 1; break;
2055 case O (O_MOVAL, SL): ea = ea << 2; break;
2056 default: goto illegal;
2059 ea = ea + code->src.literal;
2061 if (store (sd, &code->op3, ea))
2066 case O (O_SUBX, SB): /* subx, extended sub */
2067 if (fetch2 (sd, &code->dst, &rd))
2069 if (fetch (sd, &code->src, &ea))
2075 case O (O_SUBX, SW): /* subx, extended sub */
2076 if (fetch2 (sd, &code->dst, &rd))
2078 if (fetch (sd, &code->src, &ea))
2084 case O (O_SUBX, SL): /* subx, extended sub */
2085 if (fetch2 (sd, &code->dst, &rd))
2087 if (fetch (sd, &code->src, &ea))
2093 case O (O_ADDX, SB): /* addx, extended add */
2094 if (fetch2 (sd, &code->dst, &rd))
2096 if (fetch (sd, &code->src, &ea))
2102 case O (O_ADDX, SW): /* addx, extended add */
2103 if (fetch2 (sd, &code->dst, &rd))
2105 if (fetch (sd, &code->src, &ea))
2111 case O (O_ADDX, SL): /* addx, extended add */
2112 if (fetch2 (sd, &code->dst, &rd))
2114 if (fetch (sd, &code->src, &ea))
2120 case O (O_SUB, SB): /* sub.b */
2121 /* Fetch rd and ea. */
2122 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2128 case O (O_SUB, SW): /* sub.w */
2129 /* Fetch rd and ea. */
2130 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2136 case O (O_SUB, SL): /* sub.l */
2137 /* Fetch rd and ea. */
2138 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2144 case O (O_NEG, SB): /* neg.b */
2146 if (fetch2 (sd, &code->src, &ea))
2153 case O (O_NEG, SW): /* neg.w */
2155 if (fetch2 (sd, &code->src, &ea))
2162 case O (O_NEG, SL): /* neg.l */
2164 if (fetch2 (sd, &code->src, &ea))
2171 case O (O_ADD, SB): /* add.b */
2172 if (fetch2 (sd, &code->dst, &rd))
2174 if (fetch (sd, &code->src, &ea))
2179 case O (O_ADD, SW): /* add.w */
2180 if (fetch2 (sd, &code->dst, &rd))
2182 if (fetch (sd, &code->src, &ea))
2187 case O (O_ADD, SL): /* add.l */
2188 if (fetch2 (sd, &code->dst, &rd))
2190 if (fetch (sd, &code->src, &ea))
2195 case O (O_AND, SB): /* and.b */
2196 /* Fetch rd and ea. */
2197 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2202 case O (O_AND, SW): /* and.w */
2203 /* Fetch rd and ea. */
2204 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2209 case O (O_AND, SL): /* and.l */
2210 /* Fetch rd and ea. */
2211 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2216 case O (O_OR, SB): /* or.b */
2217 /* Fetch rd and ea. */
2218 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2223 case O (O_OR, SW): /* or.w */
2224 /* Fetch rd and ea. */
2225 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2230 case O (O_OR, SL): /* or.l */
2231 /* Fetch rd and ea. */
2232 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2237 case O (O_XOR, SB): /* xor.b */
2238 /* Fetch rd and ea. */
2239 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2244 case O (O_XOR, SW): /* xor.w */
2245 /* Fetch rd and ea. */
2246 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2251 case O (O_XOR, SL): /* xor.l */
2252 /* Fetch rd and ea. */
2253 if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd))
2259 if (fetch (sd, &code->src, &res))
2261 if (store (sd, &code->dst, res))
2263 goto just_flags_log8;
2265 if (fetch (sd, &code->src, &res))
2267 if (store (sd, &code->dst, res))
2269 goto just_flags_log16;
2271 if (fetch (sd, &code->src, &res))
2273 if (store (sd, &code->dst, res))
2275 goto just_flags_log32;
2277 case O (O_MOVMD, SB): /* movmd.b */
2284 rd = GET_MEMORY_B (GET_L_REG (5));
2285 SET_MEMORY_B (GET_L_REG (6), rd);
2286 SET_L_REG (5, GET_L_REG (5) + 1);
2287 SET_L_REG (6, GET_L_REG (6) + 1);
2292 case O (O_MOVMD, SW): /* movmd.w */
2299 rd = GET_MEMORY_W (GET_L_REG (5));
2300 SET_MEMORY_W (GET_L_REG (6), rd);
2301 SET_L_REG (5, GET_L_REG (5) + 2);
2302 SET_L_REG (6, GET_L_REG (6) + 2);
2307 case O (O_MOVMD, SL): /* movmd.l */
2314 rd = GET_MEMORY_L (GET_L_REG (5));
2315 SET_MEMORY_L (GET_L_REG (6), rd);
2316 SET_L_REG (5, GET_L_REG (5) + 4);
2317 SET_L_REG (6, GET_L_REG (6) + 4);
2322 case O (O_MOVSD, SB): /* movsd.b */
2323 /* This instruction implements strncpy, with a conditional branch.
2324 r4 contains n, r5 contains src, and r6 contains dst.
2325 The 16-bit displacement operand is added to the pc
2326 if and only if the end of string is reached before
2327 n bytes are transferred. */
2329 ea = GET_L_REG (4) & 0xffff;
2335 rd = GET_MEMORY_B (GET_L_REG (5));
2336 SET_MEMORY_B (GET_L_REG (6), rd);
2337 SET_L_REG (5, GET_L_REG (5) + 1);
2338 SET_L_REG (6, GET_L_REG (6) + 1);
2345 case O (O_EEPMOV, SB): /* eepmov.b */
2346 case O (O_EEPMOV, SW): /* eepmov.w */
2347 if (h8300hmode || h8300smode)
2349 register unsigned char *_src, *_dst;
2350 unsigned int count = ((code->opcode == O (O_EEPMOV, SW))
2351 ? h8_get_reg (sd, R4_REGNUM) & 0xffff
2352 : h8_get_reg (sd, R4_REGNUM) & 0xff);
2354 _src = (h8_get_reg (sd, R5_REGNUM) < memory_size
2355 ? h8_get_memory_buf (sd) + h8_get_reg (sd, R5_REGNUM)
2356 : h8_get_eightbit_buf (sd) +
2357 (h8_get_reg (sd, R5_REGNUM) & 0xff));
2358 if ((_src + count) >= (h8_get_memory_buf (sd) + memory_size))
2360 if ((_src + count) >= (h8_get_eightbit_buf (sd) + 0x100))
2363 _dst = (h8_get_reg (sd, R6_REGNUM) < memory_size
2364 ? h8_get_memory_buf (sd) + h8_get_reg (sd, R6_REGNUM)
2365 : h8_get_eightbit_buf (sd) +
2366 (h8_get_reg (sd, R6_REGNUM) & 0xff));
2368 if ((_dst + count) >= (h8_get_memory_buf (sd) + memory_size))
2370 if ((_dst + count) >= (h8_get_eightbit_buf (sd) + 0x100))
2373 memcpy (_dst, _src, count);
2375 h8_set_reg (sd, R5_REGNUM, h8_get_reg (sd, R5_REGNUM) + count);
2376 h8_set_reg (sd, R6_REGNUM, h8_get_reg (sd, R6_REGNUM) + count);
2377 h8_set_reg (sd, R4_REGNUM, h8_get_reg (sd, R4_REGNUM) &
2378 ((code->opcode == O (O_EEPMOV, SW))
2379 ? (~0xffff) : (~0xff)));
2380 cycles += 2 * count;
2385 case O (O_ADDS, SL): /* adds (.l) */
2387 * This insn only uses register operands, but still
2388 * it would be cleaner to use fetch and store... */
2389 SET_L_REG (code->dst.reg,
2390 GET_L_REG (code->dst.reg)
2391 + code->src.literal);
2395 case O (O_SUBS, SL): /* subs (.l) */
2397 * This insn only uses register operands, but still
2398 * it would be cleaner to use fetch and store... */
2399 SET_L_REG (code->dst.reg,
2400 GET_L_REG (code->dst.reg)
2401 - code->src.literal);
2404 case O (O_CMP, SB): /* cmp.b */
2405 if (fetch (sd, &code->dst, &rd))
2407 if (fetch (sd, &code->src, &ea))
2411 goto just_flags_alu8;
2413 case O (O_CMP, SW): /* cmp.w */
2414 if (fetch (sd, &code->dst, &rd))
2416 if (fetch (sd, &code->src, &ea))
2420 goto just_flags_alu16;
2422 case O (O_CMP, SL): /* cmp.l */
2423 if (fetch (sd, &code->dst, &rd))
2425 if (fetch (sd, &code->src, &ea))
2429 goto just_flags_alu32;
2431 case O (O_DEC, SB): /* dec.b */
2433 * This insn only uses register operands, but still
2434 * it would be cleaner to use fetch and store... */
2435 rd = GET_B_REG (code->src.reg);
2438 SET_B_REG (code->src.reg, res);
2439 goto just_flags_inc8;
2441 case O (O_DEC, SW): /* dec.w */
2443 * This insn only uses register operands, but still
2444 * it would be cleaner to use fetch and store... */
2445 rd = GET_W_REG (code->dst.reg);
2446 ea = -code->src.literal;
2448 SET_W_REG (code->dst.reg, res);
2449 goto just_flags_inc16;
2451 case O (O_DEC, SL): /* dec.l */
2453 * This insn only uses register operands, but still
2454 * it would be cleaner to use fetch and store... */
2455 rd = GET_L_REG (code->dst.reg);
2456 ea = -code->src.literal;
2458 SET_L_REG (code->dst.reg, res);
2459 goto just_flags_inc32;
2461 case O (O_INC, SB): /* inc.b */
2463 * This insn only uses register operands, but still
2464 * it would be cleaner to use fetch and store... */
2465 rd = GET_B_REG (code->src.reg);
2468 SET_B_REG (code->src.reg, res);
2469 goto just_flags_inc8;
2471 case O (O_INC, SW): /* inc.w */
2473 * This insn only uses register operands, but still
2474 * it would be cleaner to use fetch and store... */
2475 rd = GET_W_REG (code->dst.reg);
2476 ea = code->src.literal;
2478 SET_W_REG (code->dst.reg, res);
2479 goto just_flags_inc16;
2481 case O (O_INC, SL): /* inc.l */
2483 * This insn only uses register operands, but still
2484 * it would be cleaner to use fetch and store... */
2485 rd = GET_L_REG (code->dst.reg);
2486 ea = code->src.literal;
2488 SET_L_REG (code->dst.reg, res);
2489 goto just_flags_inc32;
2491 case O (O_LDC, SB): /* ldc.b */
2492 if (fetch (sd, &code->src, &res))
2496 case O (O_LDC, SW): /* ldc.w */
2497 if (fetch (sd, &code->src, &res))
2500 /* Word operand, value from MSB, must be shifted. */
2504 case O (O_LDC, SL): /* ldc.l */
2505 if (fetch (sd, &code->src, &res))
2507 switch (code->dst.type) {
2508 case X (OP_SBR, SL):
2509 h8_set_sbr (sd, res);
2511 case X (OP_VBR, SL):
2512 h8_set_vbr (sd, res);
2519 case O (O_STC, SW): /* stc.w */
2520 case O (O_STC, SB): /* stc.b */
2521 if (code->src.type == X (OP_CCR, SB))
2524 res = h8_get_ccr (sd);
2526 else if (code->src.type == X (OP_EXR, SB) && h8300smode)
2529 h8_set_exr (sd, (trace << 7) | intMask);
2530 res = h8_get_exr (sd);
2535 /* Word operand, value to MSB, must be shifted. */
2536 if (code->opcode == X (O_STC, SW))
2538 if (store (sd, &code->dst, res))
2541 case O (O_STC, SL): /* stc.l */
2542 switch (code->src.type) {
2543 case X (OP_SBR, SL):
2544 res = h8_get_sbr (sd);
2546 case X (OP_VBR, SL):
2547 res = h8_get_vbr (sd);
2552 if (store (sd, &code->dst, res))
2556 case O (O_ANDC, SB): /* andc.b */
2557 if (code->dst.type == X (OP_CCR, SB))
2560 rd = h8_get_ccr (sd);
2562 else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
2565 h8_set_exr (sd, (trace << 7) | intMask);
2566 rd = h8_get_exr (sd);
2570 ea = code->src.literal;
2574 case O (O_ORC, SB): /* orc.b */
2575 if (code->dst.type == X (OP_CCR, SB))
2578 rd = h8_get_ccr (sd);
2580 else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
2583 h8_set_exr (sd, (trace << 7) | intMask);
2584 rd = h8_get_exr (sd);
2588 ea = code->src.literal;
2592 case O (O_XORC, SB): /* xorc.b */
2593 if (code->dst.type == X (OP_CCR, SB))
2596 rd = h8_get_ccr (sd);
2598 else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
2601 h8_set_exr (sd, (trace << 7) | intMask);
2602 rd = h8_get_exr (sd);
2606 ea = code->src.literal;
2610 case O (O_BRAS, SB): /* bra/s */
2611 /* This is basically an ordinary branch, with a delay slot. */
2612 if (fetch (sd, &code->src, &res))
2620 /* Execution continues at next instruction, but
2621 delayed_branch is set up for next cycle. */
2622 h8_set_delayed_branch (sd, code->next_pc + res);
2626 case O (O_BRAB, SB): /* bra rd.b */
2627 case O (O_BRAW, SW): /* bra rd.w */
2628 case O (O_BRAL, SL): /* bra erd.l */
2629 if (fetch (sd, &code->src, &rd))
2631 switch (OP_SIZE (code->opcode)) {
2632 case SB: rd &= 0xff; break;
2633 case SW: rd &= 0xffff; break;
2634 case SL: rd &= 0xffffffff; break;
2636 pc = code->next_pc + rd;
2639 case O (O_BRABC, SB): /* bra/bc, branch if bit clear */
2640 case O (O_BRABS, SB): /* bra/bs, branch if bit set */
2641 case O (O_BSRBC, SB): /* bsr/bc, call if bit clear */
2642 case O (O_BSRBS, SB): /* bsr/bs, call if bit set */
2643 if (fetch (sd, &code->dst, &rd) ||
2644 fetch (sd, &code->src, &bit))
2647 if (code->opcode == O (O_BRABC, SB) || /* branch if clear */
2648 code->opcode == O (O_BSRBC, SB)) /* call if clear */
2650 if ((rd & (1 << bit))) /* no branch */
2653 else /* branch/call if set */
2655 if (!(rd & (1 << bit))) /* no branch */
2659 if (fetch (sd, &code->op3, &res)) /* branch */
2661 pc = code->next_pc + res;
2663 if (code->opcode == O (O_BRABC, SB) ||
2664 code->opcode == O (O_BRABS, SB)) /* branch */
2672 case O (O_BRA, SB): /* bra, branch always */
2677 case O (O_BRN, SB): /* brn, ;-/ branch never? */
2682 case O (O_BHI, SB): /* bhi */
2688 case O (O_BLS, SB): /* bls */
2693 case O (O_BCS, SB): /* bcs, branch if carry set */
2698 case O (O_BCC, SB): /* bcc, branch if carry clear */
2703 case O (O_BEQ, SB): /* beq, branch if zero set */
2707 case O (O_BGT, SB): /* bgt */
2708 if (((Z || (N ^ V)) == 0))
2712 case O (O_BLE, SB): /* ble */
2713 if (((Z || (N ^ V)) == 1))
2717 case O (O_BGE, SB): /* bge */
2721 case O (O_BLT, SB): /* blt */
2725 case O (O_BMI, SB): /* bmi */
2729 case O (O_BNE, SB): /* bne, branch if zero clear */
2734 case O (O_BPL, SB): /* bpl */
2738 case O (O_BVC, SB): /* bvc */
2742 case O (O_BVS, SB): /* bvs */
2747 /* Trap for Command Line setup. */
2748 case O (O_SYS_CMDLINE, SB):
2750 int i = 0; /* Loop counter. */
2751 int j = 0; /* Loop counter. */
2752 int ind_arg_len = 0; /* Length of each argument. */
2753 int no_of_args = 0; /* The no. or cmdline args. */
2754 int current_location = 0; /* Location of string. */
2755 int old_sp = 0; /* The Initial Stack Pointer. */
2756 int no_of_slots = 0; /* No. of slots required on the stack
2757 for storing cmdline args. */
2758 int sp_move = 0; /* No. of locations by which the stack needs
2760 int new_sp = 0; /* The final stack pointer location passed
2762 int *argv_ptrs; /* Pointers of argv strings to be stored. */
2763 int argv_ptrs_location = 0; /* Location of pointers to cmdline
2764 args on the stack. */
2765 int char_ptr_size = 0; /* Size of a character pointer on
2767 int addr_cmdline = 0; /* Memory location where cmdline has
2769 int size_cmdline = 0; /* Size of cmdline. */
2771 /* Set the address of 256 free locations where command line is
2773 addr_cmdline = cmdline_location();
2774 h8_set_reg (sd, 0, addr_cmdline);
2776 /* Counting the no. of commandline arguments. */
2777 for (i = 0; h8_get_cmdline_arg (sd, i) != NULL; i++)
2780 /* No. of arguments in the command line. */
2783 /* Current location is just a temporary variable,which we are
2784 setting to the point to the start of our commandline string. */
2785 current_location = addr_cmdline;
2787 /* Allocating space for storing pointers of the command line
2789 argv_ptrs = (int *) malloc (sizeof (int) * no_of_args);
2791 /* Setting char_ptr_size to the sizeof (char *) on the different
2793 if ((h8300hmode || h8300smode) && !h8300_normal_mode)
2802 for (i = 0; i < no_of_args; i++)
2806 /* The size of the commandline argument. */
2807 ind_arg_len = strlen (h8_get_cmdline_arg (sd, i)) + 1;
2809 /* The total size of the command line string. */
2810 size_cmdline += ind_arg_len;
2812 /* As we have only 256 bytes, we need to provide a graceful
2813 exit. Anyways, a program using command line arguments
2814 where we cannot store all the command line arguments
2815 given may behave unpredictably. */
2816 if (size_cmdline >= 256)
2818 h8_set_reg (sd, 0, 0);
2823 /* current_location points to the memory where the next
2824 commandline argument is stored. */
2825 argv_ptrs[i] = current_location;
2826 for (j = 0; j < ind_arg_len; j++)
2828 SET_MEMORY_B ((current_location +
2829 (sizeof (char) * j)),
2830 *(h8_get_cmdline_arg (sd, i) +
2831 sizeof (char) * j));
2834 /* Setting current_location to the starting of next
2836 current_location += ind_arg_len;
2840 /* This is the original position of the stack pointer. */
2841 old_sp = h8_get_reg (sd, SP_REGNUM);
2843 /* We need space from the stack to store the pointers to argvs. */
2844 /* As we will infringe on the stack, we need to shift the stack
2845 pointer so that the data is not overwritten. We calculate how
2846 much space is required. */
2847 sp_move = (no_of_args) * (char_ptr_size);
2849 /* The final position of stack pointer, we have thus taken some
2850 space from the stack. */
2851 new_sp = old_sp - sp_move;
2853 /* Temporary variable holding value where the argv pointers need
2855 argv_ptrs_location = new_sp;
2857 /* The argv pointers are stored at sequential locations. As per
2859 for (i = 0; i < no_of_args; i++)
2861 /* Saving the argv pointer. */
2862 if ((h8300hmode || h8300smode) && !h8300_normal_mode)
2864 SET_MEMORY_L (argv_ptrs_location, argv_ptrs[i]);
2868 SET_MEMORY_W (argv_ptrs_location, argv_ptrs[i]);
2871 /* The next location where the pointer to the next argv
2872 string has to be stored. */
2873 argv_ptrs_location += char_ptr_size;
2876 /* Required by POSIX, Setting 0x0 at the end of the list of argv
2878 if ((h8300hmode || h8300smode) && !h8300_normal_mode)
2880 SET_MEMORY_L (old_sp, 0x0);
2884 SET_MEMORY_W (old_sp, 0x0);
2887 /* Freeing allocated memory. */
2889 for (i = 0; i <= no_of_args; i++)
2891 free (h8_get_cmdline_arg (sd, i));
2893 free (h8_get_command_line (sd));
2895 /* The no. of argv arguments are returned in Reg 0. */
2896 h8_set_reg (sd, 0, no_of_args);
2897 /* The Pointer to argv in Register 1. */
2898 h8_set_reg (sd, 1, new_sp);
2899 /* Setting the stack pointer to the new value. */
2900 h8_set_reg (sd, SP_REGNUM, new_sp);
2904 /* System call processing starts. */
2905 case O (O_SYS_OPEN, SB):
2907 int len = 0; /* Length of filename. */
2908 char *filename; /* Filename would go here. */
2909 char temp_char; /* Temporary character */
2910 int mode = 0; /* Mode bits for the file. */
2911 int open_return; /* Return value of open, file descriptor. */
2912 int i; /* Loop counter */
2913 int filename_ptr; /* Pointer to filename in cpu memory. */
2915 /* Setting filename_ptr to first argument of open, */
2916 /* and trying to get mode. */
2917 if ((h8300sxmode || h8300hmode || h8300smode) && !h8300_normal_mode)
2919 filename_ptr = GET_L_REG (0);
2920 mode = GET_MEMORY_L (h8_get_reg (sd, SP_REGNUM) + 4);
2924 filename_ptr = GET_W_REG (0);
2925 mode = GET_MEMORY_W (h8_get_reg (sd, SP_REGNUM) + 2);
2928 /* Trying to find the length of the filename. */
2929 temp_char = GET_MEMORY_B (h8_get_reg (sd, 0));
2932 while (temp_char != '\0')
2934 temp_char = GET_MEMORY_B (filename_ptr + len);
2938 /* Allocating space for the filename. */
2939 filename = (char *) malloc (sizeof (char) * len);
2941 /* String copying the filename from memory. */
2942 for (i = 0; i < len; i++)
2944 temp_char = GET_MEMORY_B (filename_ptr + i);
2945 filename[i] = temp_char;
2948 /* Callback to open and return the file descriptor. */
2949 open_return = sim_callback->open (sim_callback, filename, mode);
2951 /* Return value in register 0. */
2952 h8_set_reg (sd, 0, open_return);
2954 /* Freeing memory used for filename. */
2959 case O (O_SYS_READ, SB):
2961 char *char_ptr; /* Where characters read would be stored. */
2962 int fd; /* File descriptor */
2963 int buf_size; /* BUF_SIZE parameter in read. */
2964 int i = 0; /* Temporary Loop counter */
2965 int read_return = 0; /* Return value from callback to
2968 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
2969 buf_size = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (2) : GET_W_REG (2);
2971 char_ptr = (char *) malloc (sizeof (char) * buf_size);
2973 /* Callback to read and return the no. of characters read. */
2975 sim_callback->read (sim_callback, fd, char_ptr, buf_size);
2977 /* The characters read are stored in cpu memory. */
2978 for (i = 0; i < buf_size; i++)
2980 SET_MEMORY_B ((h8_get_reg (sd, 1) + (sizeof (char) * i)),
2981 *(char_ptr + (sizeof (char) * i)));
2984 /* Return value in Register 0. */
2985 h8_set_reg (sd, 0, read_return);
2987 /* Freeing memory used as buffer. */
2992 case O (O_SYS_WRITE, SB):
2994 int fd; /* File descriptor */
2995 char temp_char; /* Temporary character */
2996 int len; /* Length of write, Parameter II to write. */
2997 int char_ptr; /* Character Pointer, Parameter I of write. */
2998 char *ptr; /* Where characters to be written are stored.
3000 int write_return; /* Return value from callback to write. */
3001 int i = 0; /* Loop counter */
3003 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3004 char_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
3005 len = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (2) : GET_W_REG (2);
3007 /* Allocating space for the characters to be written. */
3008 ptr = (char *) malloc (sizeof (char) * len);
3010 /* Fetching the characters from cpu memory. */
3011 for (i = 0; i < len; i++)
3013 temp_char = GET_MEMORY_B (char_ptr + i);
3017 /* Callback write and return the no. of characters written. */
3018 write_return = sim_callback->write (sim_callback, fd, ptr, len);
3020 /* Return value in Register 0. */
3021 h8_set_reg (sd, 0, write_return);
3023 /* Freeing memory used as buffer. */
3028 case O (O_SYS_LSEEK, SB):
3030 int fd; /* File descriptor */
3031 int offset; /* Offset */
3032 int origin; /* Origin */
3033 int lseek_return; /* Return value from callback to lseek. */
3035 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3036 offset = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
3037 origin = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (2) : GET_W_REG (2);
3039 /* Callback lseek and return offset. */
3041 sim_callback->lseek (sim_callback, fd, offset, origin);
3043 /* Return value in register 0. */
3044 h8_set_reg (sd, 0, lseek_return);
3048 case O (O_SYS_CLOSE, SB):
3050 int fd; /* File descriptor */
3051 int close_return; /* Return value from callback to close. */
3053 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3055 /* Callback close and return. */
3056 close_return = sim_callback->close (sim_callback, fd);
3058 /* Return value in register 0. */
3059 h8_set_reg (sd, 0, close_return);
3063 case O (O_SYS_FSTAT, SB):
3065 int fd; /* File descriptor */
3066 struct stat stat_rec; /* Stat record */
3067 int fstat_return; /* Return value from callback to stat. */
3068 int stat_ptr; /* Pointer to stat record. */
3069 char *temp_stat_ptr; /* Temporary stat_rec pointer. */
3071 fd = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3073 /* Setting stat_ptr to second argument of stat. */
3074 stat_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
3076 /* Callback stat and return. */
3077 fstat_return = sim_callback->fstat (sim_callback, fd, &stat_rec);
3079 /* Have stat_ptr point to starting of stat_rec. */
3080 temp_stat_ptr = (char *) (&stat_rec);
3082 /* Setting up the stat structure returned. */
3083 SET_MEMORY_W (stat_ptr, stat_rec.st_dev);
3085 SET_MEMORY_W (stat_ptr, stat_rec.st_ino);
3087 SET_MEMORY_L (stat_ptr, stat_rec.st_mode);
3089 SET_MEMORY_W (stat_ptr, stat_rec.st_nlink);
3091 SET_MEMORY_W (stat_ptr, stat_rec.st_uid);
3093 SET_MEMORY_W (stat_ptr, stat_rec.st_gid);
3095 SET_MEMORY_W (stat_ptr, stat_rec.st_rdev);
3097 SET_MEMORY_L (stat_ptr, stat_rec.st_size);
3099 SET_MEMORY_L (stat_ptr, stat_rec.st_atime);
3101 SET_MEMORY_L (stat_ptr, stat_rec.st_mtime);
3103 SET_MEMORY_L (stat_ptr, stat_rec.st_ctime);
3105 /* Return value in register 0. */
3106 h8_set_reg (sd, 0, fstat_return);
3110 case O (O_SYS_STAT, SB):
3112 int len = 0; /* Length of filename. */
3113 char *filename; /* Filename would go here. */
3114 char temp_char; /* Temporary character */
3115 int filename_ptr; /* Pointer to filename in cpu memory. */
3116 struct stat stat_rec; /* Stat record */
3117 int stat_return; /* Return value from callback to stat */
3118 int stat_ptr; /* Pointer to stat record. */
3119 char *temp_stat_ptr; /* Temporary stat_rec pointer. */
3120 int i = 0; /* Loop Counter */
3122 /* Setting filename_ptr to first argument of open. */
3123 filename_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (0) : GET_W_REG (0);
3125 /* Trying to find the length of the filename. */
3126 temp_char = GET_MEMORY_B (h8_get_reg (sd, 0));
3129 while (temp_char != '\0')
3131 temp_char = GET_MEMORY_B (filename_ptr + len);
3135 /* Allocating space for the filename. */
3136 filename = (char *) malloc (sizeof (char) * len);
3138 /* String copying the filename from memory. */
3139 for (i = 0; i < len; i++)
3141 temp_char = GET_MEMORY_B (filename_ptr + i);
3142 filename[i] = temp_char;
3145 /* Setting stat_ptr to second argument of stat. */
3146 /* stat_ptr = h8_get_reg (sd, 1); */
3147 stat_ptr = (h8300hmode && !h8300_normal_mode) ? GET_L_REG (1) : GET_W_REG (1);
3149 /* Callback stat and return. */
3151 sim_callback->stat (sim_callback, filename, &stat_rec);
3153 /* Have stat_ptr point to starting of stat_rec. */
3154 temp_stat_ptr = (char *) (&stat_rec);
3156 /* Freeing memory used for filename. */
3159 /* Setting up the stat structure returned. */
3160 SET_MEMORY_W (stat_ptr, stat_rec.st_dev);
3162 SET_MEMORY_W (stat_ptr, stat_rec.st_ino);
3164 SET_MEMORY_L (stat_ptr, stat_rec.st_mode);
3166 SET_MEMORY_W (stat_ptr, stat_rec.st_nlink);
3168 SET_MEMORY_W (stat_ptr, stat_rec.st_uid);
3170 SET_MEMORY_W (stat_ptr, stat_rec.st_gid);
3172 SET_MEMORY_W (stat_ptr, stat_rec.st_rdev);
3174 SET_MEMORY_L (stat_ptr, stat_rec.st_size);
3176 SET_MEMORY_L (stat_ptr, stat_rec.st_atime);
3178 SET_MEMORY_L (stat_ptr, stat_rec.st_mtime);
3180 SET_MEMORY_L (stat_ptr, stat_rec.st_ctime);
3182 /* Return value in register 0. */
3183 h8_set_reg (sd, 0, stat_return);
3186 /* End of system call processing. */
3188 case O (O_NOT, SB): /* not.b */
3189 if (fetch2 (sd, &code->src, &rd))
3195 case O (O_NOT, SW): /* not.w */
3196 if (fetch2 (sd, &code->src, &rd))
3202 case O (O_NOT, SL): /* not.l */
3203 if (fetch2 (sd, &code->src, &rd))
3209 case O (O_SHLL, SB): /* shll.b */
3210 case O (O_SHLR, SB): /* shlr.b */
3211 if (fetch2 (sd, &code->dst, &rd))
3214 if (memcmp (&code->src, &code->dst, sizeof (code->src)) == 0)
3215 ea = 1; /* unary op */
3216 else /* binary op */
3217 fetch (sd, &code->src, &ea);
3219 if (code->opcode == O (O_SHLL, SB))
3222 c = rd & (0x80 >> (ea - 1));
3228 c = rd & (1 << (ea - 1));
3229 rd = (unsigned char) rd >> ea;
3233 case O (O_SHLL, SW): /* shll.w */
3234 case O (O_SHLR, SW): /* shlr.w */
3235 if (fetch2 (sd, &code->dst, &rd))
3238 if (memcmp (&code->src, &code->dst, sizeof (code->src)) == 0)
3239 ea = 1; /* unary op */
3241 fetch (sd, &code->src, &ea);
3243 if (code->opcode == O (O_SHLL, SW))
3246 c = rd & (0x8000 >> (ea - 1));
3252 c = rd & (1 << (ea - 1));
3253 rd = (unsigned short) rd >> ea;
3257 case O (O_SHLL, SL): /* shll.l */
3258 case O (O_SHLR, SL): /* shlr.l */
3259 if (fetch2 (sd, &code->dst, &rd))
3262 if (memcmp (&code->src, &code->dst, sizeof (code->src)) == 0)
3263 ea = 1; /* unary op */
3265 fetch (sd, &code->src, &ea);
3267 if (code->opcode == O (O_SHLL, SL))
3270 c = rd & (0x80000000 >> (ea - 1));
3276 c = rd & (1 << (ea - 1));
3277 rd = (unsigned int) rd >> ea;
3281 case O (O_SHAL, SB):
3282 case O (O_SHAR, SB):
3283 if (fetch2 (sd, &code->dst, &rd))
3286 if (code->src.type == X (OP_IMM, SB))
3287 fetch (sd, &code->src, &ea);
3291 if (code->opcode == O (O_SHAL, SB))
3293 c = rd & (0x80 >> (ea - 1));
3294 res = rd >> (7 - ea);
3295 v = ((res & 1) && !(res & 2))
3296 || (!(res & 1) && (res & 2));
3301 c = rd & (1 << (ea - 1));
3303 rd = ((signed char) rd) >> ea;
3307 case O (O_SHAL, SW):
3308 case O (O_SHAR, SW):
3309 if (fetch2 (sd, &code->dst, &rd))
3312 if (code->src.type == X (OP_IMM, SW))
3313 fetch (sd, &code->src, &ea);
3317 if (code->opcode == O (O_SHAL, SW))
3319 c = rd & (0x8000 >> (ea - 1));
3320 res = rd >> (15 - ea);
3321 v = ((res & 1) && !(res & 2))
3322 || (!(res & 1) && (res & 2));
3327 c = rd & (1 << (ea - 1));
3329 rd = ((signed short) rd) >> ea;
3333 case O (O_SHAL, SL):
3334 case O (O_SHAR, SL):
3335 if (fetch2 (sd, &code->dst, &rd))
3338 if (code->src.type == X (OP_IMM, SL))
3339 fetch (sd, &code->src, &ea);
3343 if (code->opcode == O (O_SHAL, SL))
3345 c = rd & (0x80000000 >> (ea - 1));
3346 res = rd >> (31 - ea);
3347 v = ((res & 1) && !(res & 2))
3348 || (!(res & 1) && (res & 2));
3353 c = rd & (1 << (ea - 1));
3355 rd = ((signed int) rd) >> ea;
3359 case O (O_ROTL, SB):
3360 case O (O_ROTR, SB):
3361 if (fetch2 (sd, &code->dst, &rd))
3364 if (code->src.type == X (OP_IMM, SB))
3365 fetch (sd, &code->src, &ea);
3370 if (code->opcode == O (O_ROTL, SB))
3380 rd = ((unsigned char) rd) >> 1;
3388 case O (O_ROTL, SW):
3389 case O (O_ROTR, SW):
3390 if (fetch2 (sd, &code->dst, &rd))
3393 if (code->src.type == X (OP_IMM, SW))
3394 fetch (sd, &code->src, &ea);
3399 if (code->opcode == O (O_ROTL, SW))
3409 rd = ((unsigned short) rd) >> 1;
3417 case O (O_ROTL, SL):
3418 case O (O_ROTR, SL):
3419 if (fetch2 (sd, &code->dst, &rd))
3422 if (code->src.type == X (OP_IMM, SL))
3423 fetch (sd, &code->src, &ea);
3428 if (code->opcode == O (O_ROTL, SL))
3430 c = rd & 0x80000000;
3438 rd = ((unsigned int) rd) >> 1;
3446 case O (O_ROTXL, SB):
3447 case O (O_ROTXR, SB):
3448 if (fetch2 (sd, &code->dst, &rd))
3451 if (code->src.type == X (OP_IMM, SB))
3452 fetch (sd, &code->src, &ea);
3457 if (code->opcode == O (O_ROTXL, SB))
3468 rd = ((unsigned char) rd) >> 1;
3477 case O (O_ROTXL, SW):
3478 case O (O_ROTXR, SW):
3479 if (fetch2 (sd, &code->dst, &rd))
3482 if (code->src.type == X (OP_IMM, SW))
3483 fetch (sd, &code->src, &ea);
3488 if (code->opcode == O (O_ROTXL, SW))
3499 rd = ((unsigned short) rd) >> 1;
3508 case O (O_ROTXL, SL):
3509 case O (O_ROTXR, SL):
3510 if (fetch2 (sd, &code->dst, &rd))
3513 if (code->src.type == X (OP_IMM, SL))
3514 fetch (sd, &code->src, &ea);
3519 if (code->opcode == O (O_ROTXL, SL))
3521 res = rd & 0x80000000;
3530 rd = ((unsigned int) rd) >> 1;
3541 case O (O_JMP, SB): /* jmp */
3543 fetch (sd, &code->src, &pc);
3548 case O (O_JSR, SB): /* jsr, jump to subroutine */
3550 if (fetch (sd, &code->src, &pc))
3553 tmp = h8_get_reg (sd, SP_REGNUM);
3555 if (h8300hmode && !h8300_normal_mode)
3558 SET_MEMORY_L (tmp, code->next_pc);
3563 SET_MEMORY_W (tmp, code->next_pc);
3565 h8_set_reg (sd, SP_REGNUM, tmp);
3571 case O (O_BSR, SB): /* bsr, branch to subroutine */
3572 if (fetch (sd, &code->src, &res))
3574 pc = code->next_pc + res;
3577 case O (O_RTE, SN): /* rte, return from exception */
3579 /* Pops exr and ccr before pc -- otherwise identical to rts. */
3580 tmp = h8_get_reg (sd, SP_REGNUM);
3582 if (h8300smode) /* pop exr */
3584 h8_set_exr (sd, GET_MEMORY_L (tmp));
3587 if (h8300hmode && !h8300_normal_mode)
3589 h8_set_ccr (sd, GET_MEMORY_L (tmp));
3591 pc = GET_MEMORY_L (tmp);
3596 h8_set_ccr (sd, GET_MEMORY_W (tmp));
3598 pc = GET_MEMORY_W (tmp);
3603 h8_set_reg (sd, SP_REGNUM, tmp);
3606 case O (O_RTS, SN): /* rts, return from subroutine */
3608 tmp = h8_get_reg (sd, SP_REGNUM);
3610 if (h8300hmode && !h8300_normal_mode)
3612 pc = GET_MEMORY_L (tmp);
3617 pc = GET_MEMORY_W (tmp);
3621 h8_set_reg (sd, SP_REGNUM, tmp);
3624 case O (O_ILL, SB): /* illegal */
3625 sim_engine_set_run_state (sd, sim_stopped, SIGILL);
3628 case O (O_SLEEP, SN): /* sleep */
3629 /* Check for magic numbers in r1 and r2. */
3630 if ((h8_get_reg (sd, R1_REGNUM) & 0xffff) == LIBC_EXIT_MAGIC1 &&
3631 (h8_get_reg (sd, R2_REGNUM) & 0xffff) == LIBC_EXIT_MAGIC2 &&
3632 SIM_WIFEXITED (h8_get_reg (sd, 0)))
3634 /* This trap comes from _exit, not from gdb. */
3635 sim_engine_set_run_state (sd, sim_exited,
3636 SIM_WEXITSTATUS (h8_get_reg (sd, 0)));
3639 /* Unfortunately this won't really work, because
3640 when we take a breakpoint trap, R0 has a "random",
3641 user-defined value. Don't see any immediate solution. */
3642 else if (SIM_WIFSTOPPED (h8_get_reg (sd, 0)))
3644 /* Pass the stop signal up to gdb. */
3645 sim_engine_set_run_state (sd, sim_stopped,
3646 SIM_WSTOPSIG (h8_get_reg (sd, 0)));
3651 /* Treat it as a sigtrap. */
3652 sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
3656 case O (O_TRAPA, SB): /* trapa */
3657 if (fetch (sd, &code->src, &res))
3658 goto end; /* res is vector number. */
3660 tmp = h8_get_reg (sd, SP_REGNUM);
3661 if(h8300_normal_mode)
3664 SET_MEMORY_W (tmp, code->next_pc);
3666 SET_MEMORY_W (tmp, h8_get_ccr (sd));
3671 SET_MEMORY_L (tmp, code->next_pc);
3673 SET_MEMORY_L (tmp, h8_get_ccr (sd));
3681 SET_MEMORY_L (tmp, h8_get_exr (sd));
3684 h8_set_reg (sd, SP_REGNUM, tmp);
3686 if(h8300_normal_mode)
3687 pc = GET_MEMORY_L (0x10 + res * 2); /* Vector addresses are 0x10,0x12,0x14 and 0x16 */
3689 pc = GET_MEMORY_L (0x20 + res * 4);
3693 sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
3696 case O (O_BSETEQ, SB):
3701 case O (O_BSETNE, SB):
3706 case O (O_BCLREQ, SB):
3711 case O (O_BCLRNE, SB):
3716 OBITOP (O_BNOT, 1, 1, ea ^= m); /* bnot */
3717 OBITOP (O_BTST, 1, 0, nz = ea & m); /* btst */
3719 OBITOP (O_BSET, 1, 1, ea |= m); /* bset */
3721 OBITOP (O_BCLR, 1, 1, ea &= ~m); /* bclr */
3722 OBITOP (O_BLD, 1, 0, c = ea & m); /* bld */
3723 OBITOP (O_BILD, 1, 0, c = !(ea & m)); /* bild */
3724 OBITOP (O_BST, 1, 1, ea &= ~m;
3725 if (C) ea |= m); /* bst */
3726 OBITOP (O_BIST, 1, 1, ea &= ~m;
3727 if (!C) ea |= m); /* bist */
3728 OBITOP (O_BSTZ, 1, 1, ea &= ~m;
3729 if (Z) ea |= m); /* bstz */
3730 OBITOP (O_BISTZ, 1, 1, ea &= ~m;
3731 if (!Z) ea |= m); /* bistz */
3732 OBITOP (O_BAND, 1, 0, c = (ea & m) && C); /* band */
3733 OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C); /* biand */
3734 OBITOP (O_BOR, 1, 0, c = (ea & m) || C); /* bor */
3735 OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C); /* bior */
3736 OBITOP (O_BXOR, 1, 0, c = ((ea & m) != 0)!= C); /* bxor */
3737 OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C); /* bixor */
3739 case O (O_BFLD, SB): /* bfld */
3742 if (fetch (sd, &code->src, &bit))
3747 if (fetch (sd, &code->dst, &ea))
3757 if (store (sd, &code->op3, ea))
3762 case O(O_BFST, SB): /* bfst */
3763 /* bitfield store */
3764 /* NOTE: the imm8 value is in dst, and the ea value
3765 (which is actually the destination) is in op3.
3766 It has to be that way, to avoid breaking the assembler. */
3768 if (fetch (sd, &code->dst, &bit)) /* imm8 */
3770 if (bit == 0) /* noop -- nothing to do. */
3773 if (fetch (sd, &code->src, &rd)) /* reg8 src */
3776 if (fetch2 (sd, &code->op3, &ea)) /* ea dst */
3779 /* Left-shift the register data into position. */
3780 for (tmp = bit; !(tmp & 1); tmp >>= 1)
3783 /* Combine it with the neighboring bits. */
3784 ea = (ea & ~bit) | (rd & bit);
3787 if (store2 (sd, &code->op3, ea))
3791 case O (O_CLRMAC, SN): /* clrmac */
3792 h8_set_mach (sd, 0);
3793 h8_set_macl (sd, 0);
3794 h8_set_macZ (sd, 1);
3795 h8_set_macV (sd, 0);
3796 h8_set_macN (sd, 0);
3799 case O (O_STMAC, SL): /* stmac, 260 */
3800 switch (code->src.type) {
3801 case X (OP_MACH, SL):
3802 res = h8_get_mach (sd);
3803 if (res & 0x200) /* sign extend */
3806 case X (OP_MACL, SL):
3807 res = h8_get_macl (sd);
3809 default: goto illegal;
3811 nz = !h8_get_macZ (sd);
3812 n = h8_get_macN (sd);
3813 v = h8_get_macV (sd);
3815 if (store (sd, &code->dst, res))
3820 case O (O_LDMAC, SL): /* ldmac, 179 */
3821 if (fetch (sd, &code->src, &rd))
3824 switch (code->dst.type) {
3825 case X (OP_MACH, SL):
3826 rd &= 0x3ff; /* Truncate to 10 bits */
3827 h8_set_mach (sd, rd);
3829 case X (OP_MACL, SL):
3830 h8_set_macl (sd, rd);
3832 default: goto illegal;
3834 h8_set_macV (sd, 0);
3838 if (fetch (sd, &code->src, &rd) ||
3839 fetch (sd, &code->dst, &res))
3842 /* Ye gods, this is non-portable!
3843 However, the existing mul/div code is similar. */
3844 res = SEXTSHORT (res) * SEXTSHORT (rd);
3846 if (h8_get_macS (sd)) /* Saturating mode */
3848 long long mac = h8_get_macl (sd);
3850 if (mac & 0x80000000) /* sign extend */
3851 mac |= 0xffffffff00000000LL;
3854 if (mac > 0x7fffffff || mac < 0xffffffff80000000LL)
3855 h8_set_macV (sd, 1);
3856 h8_set_macZ (sd, (mac == 0));
3857 h8_set_macN (sd, (mac < 0));
3858 h8_set_macl (sd, (int) mac);
3860 else /* "Less Saturating" mode */
3862 long long mac = h8_get_mach (sd);
3864 mac += h8_get_macl (sd);
3866 if (mac & 0x20000000000LL) /* sign extend */
3867 mac |= 0xfffffc0000000000LL;
3870 if (mac > 0x1ffffffffffLL ||
3871 mac < (long long) 0xfffffe0000000000LL)
3872 h8_set_macV (sd, 1);
3873 h8_set_macZ (sd, (mac == 0));
3874 h8_set_macN (sd, (mac < 0));
3875 h8_set_macl (sd, (int) mac);
3877 h8_set_mach (sd, (int) (mac & 0x3ff));
3881 case O (O_MULS, SW): /* muls.w */
3882 if (fetch (sd, &code->src, &ea) ||
3883 fetch (sd, &code->dst, &rd))
3886 ea = SEXTSHORT (ea);
3887 res = SEXTSHORT (ea * SEXTSHORT (rd));
3891 if (store (sd, &code->dst, res))
3896 case O (O_MULS, SL): /* muls.l */
3897 if (fetch (sd, &code->src, &ea) ||
3898 fetch (sd, &code->dst, &rd))
3903 n = res & 0x80000000;
3904 nz = res & 0xffffffff;
3905 if (store (sd, &code->dst, res))
3909 case O (O_MULSU, SL): /* muls/u.l */
3910 if (fetch (sd, &code->src, &ea) ||
3911 fetch (sd, &code->dst, &rd))
3914 /* Compute upper 32 bits of the 64-bit result. */
3915 res = (((long long) ea) * ((long long) rd)) >> 32;
3917 n = res & 0x80000000;
3918 nz = res & 0xffffffff;
3919 if (store (sd, &code->dst, res))
3923 case O (O_MULU, SW): /* mulu.w */
3924 if (fetch (sd, &code->src, &ea) ||
3925 fetch (sd, &code->dst, &rd))
3928 res = UEXTSHORT ((UEXTSHORT (ea) * UEXTSHORT (rd)));
3930 /* Don't set Z or N. */
3931 if (store (sd, &code->dst, res))
3936 case O (O_MULU, SL): /* mulu.l */
3937 if (fetch (sd, &code->src, &ea) ||
3938 fetch (sd, &code->dst, &rd))
3943 /* Don't set Z or N. */
3944 if (store (sd, &code->dst, res))
3949 case O (O_MULUU, SL): /* mulu/u.l */
3950 if (fetch (sd, &code->src, &ea) ||
3951 fetch (sd, &code->dst, &rd))
3954 /* Compute upper 32 bits of the 64-bit result. */
3955 res = (((unsigned long long) (unsigned) ea) *
3956 ((unsigned long long) (unsigned) rd)) >> 32;
3958 /* Don't set Z or N. */
3959 if (store (sd, &code->dst, res))
3964 case O (O_MULXS, SB): /* mulxs.b */
3965 if (fetch (sd, &code->src, &ea) ||
3966 fetch (sd, &code->dst, &rd))
3970 res = ea * SEXTCHAR (rd);
3974 if (store (sd, &code->dst, res))
3979 case O (O_MULXS, SW): /* mulxs.w */
3980 if (fetch (sd, &code->src, &ea) ||
3981 fetch (sd, &code->dst, &rd))
3984 ea = SEXTSHORT (ea);
3985 res = ea * SEXTSHORT (rd & 0xffff);
3987 n = res & 0x80000000;
3988 nz = res & 0xffffffff;
3989 if (store (sd, &code->dst, res))
3994 case O (O_MULXU, SB): /* mulxu.b */
3995 if (fetch (sd, &code->src, &ea) ||
3996 fetch (sd, &code->dst, &rd))
3999 res = UEXTCHAR (ea) * UEXTCHAR (rd);
4001 if (store (sd, &code->dst, res))
4006 case O (O_MULXU, SW): /* mulxu.w */
4007 if (fetch (sd, &code->src, &ea) ||
4008 fetch (sd, &code->dst, &rd))
4011 res = UEXTSHORT (ea) * UEXTSHORT (rd);
4013 if (store (sd, &code->dst, res))
4018 case O (O_TAS, SB): /* tas (test and set) */
4019 if (!h8300sxmode) /* h8sx can use any register. */
4020 switch (code->src.reg)
4031 if (fetch (sd, &code->src, &res))
4033 if (store (sd, &code->src, res | 0x80))
4036 goto just_flags_log8;
4038 case O (O_DIVU, SW): /* divu.w */
4039 if (fetch (sd, &code->src, &ea) ||
4040 fetch (sd, &code->dst, &rd))
4046 res = (unsigned) (UEXTSHORT (rd) / UEXTSHORT (ea));
4050 if (store (sd, &code->dst, res))
4054 case O (O_DIVU, SL): /* divu.l */
4055 if (fetch (sd, &code->src, &ea) ||
4056 fetch (sd, &code->dst, &rd))
4059 n = ea & 0x80000000;
4060 nz = ea & 0xffffffff;
4062 res = (unsigned) rd / ea;
4066 if (store (sd, &code->dst, res))
4070 case O (O_DIVS, SW): /* divs.w */
4071 if (fetch (sd, &code->src, &ea) ||
4072 fetch (sd, &code->dst, &rd))
4077 res = SEXTSHORT (rd) / SEXTSHORT (ea);
4087 if (store (sd, &code->dst, res))
4091 case O (O_DIVS, SL): /* divs.l */
4092 if (fetch (sd, &code->src, &ea) ||
4093 fetch (sd, &code->dst, &rd))
4107 n = res & 0x80000000;
4108 if (store (sd, &code->dst, res))
4112 case O (O_DIVXU, SB): /* divxu.b */
4113 if (fetch (sd, &code->src, &ea) ||
4114 fetch (sd, &code->dst, &rd))
4117 rd = UEXTSHORT (rd);
4124 tmp = (unsigned) rd % ea;
4125 res = (unsigned) rd / ea;
4133 if (store (sd, &code->dst, (res & 0xff) | (tmp << 8)))
4137 case O (O_DIVXU, SW): /* divxu.w */
4138 if (fetch (sd, &code->src, &ea) ||
4139 fetch (sd, &code->dst, &rd))
4142 ea = UEXTSHORT (ea);
4148 tmp = (unsigned) rd % ea;
4149 res = (unsigned) rd / ea;
4157 if (store (sd, &code->dst, (res & 0xffff) | (tmp << 16)))
4161 case O (O_DIVXS, SB): /* divxs.b */
4162 if (fetch (sd, &code->src, &ea) ||
4163 fetch (sd, &code->dst, &rd))
4166 rd = SEXTSHORT (rd);
4171 tmp = (int) rd % (int) ea;
4172 res = (int) rd / (int) ea;
4183 if (store (sd, &code->dst, (res & 0xff) | (tmp << 8)))
4187 case O (O_DIVXS, SW): /* divxs.w */
4188 if (fetch (sd, &code->src, &ea) ||
4189 fetch (sd, &code->dst, &rd))
4192 ea = SEXTSHORT (ea);
4196 tmp = (int) rd % (int) ea;
4197 res = (int) rd / (int) ea;
4207 n = res & 0x80000000;
4208 if (store (sd, &code->dst, (res & 0xffff) | (tmp << 16)))
4212 case O (O_EXTS, SW): /* exts.w, signed extend */
4213 if (fetch2 (sd, &code->dst, &rd))
4215 ea = rd & 0x80 ? -256 : 0;
4216 res = (rd & 0xff) + ea;
4219 case O (O_EXTS, SL): /* exts.l, signed extend */
4220 if (fetch2 (sd, &code->dst, &rd))
4222 if (code->src.type == X (OP_IMM, SL))
4224 if (fetch (sd, &code->src, &ea))
4227 if (ea == 2) /* exts.l #2, nn */
4229 /* Sign-extend from 8-bit to 32-bit. */
4230 ea = rd & 0x80 ? -256 : 0;
4231 res = (rd & 0xff) + ea;
4235 /* Sign-extend from 16-bit to 32-bit. */
4236 ea = rd & 0x8000 ? -65536 : 0;
4237 res = (rd & 0xffff) + ea;
4240 case O (O_EXTU, SW): /* extu.w, unsigned extend */
4241 if (fetch2 (sd, &code->dst, &rd))
4244 res = (rd & 0xff) + ea;
4247 case O (O_EXTU, SL): /* extu.l, unsigned extend */
4248 if (fetch2 (sd, &code->dst, &rd))
4250 if (code->src.type == X (OP_IMM, SL))
4252 if (fetch (sd, &code->src, &ea))
4255 if (ea == 2) /* extu.l #2, nn */
4257 /* Zero-extend from 8-bit to 32-bit. */
4259 res = (rd & 0xff) + ea;
4263 /* Zero-extend from 16-bit to 32-bit. */
4265 res = (rd & 0xffff) + ea;
4268 case O (O_NOP, SN): /* nop */
4271 case O (O_STM, SL): /* stm, store to memory */
4273 int nregs, firstreg, i;
4275 nregs = GET_MEMORY_B (pc + 1);
4278 firstreg = code->src.reg;
4280 for (i = firstreg; i <= firstreg + nregs; i++)
4282 h8_set_reg (sd, SP_REGNUM, h8_get_reg (sd, SP_REGNUM) - 4);
4283 SET_MEMORY_L (h8_get_reg (sd, SP_REGNUM), h8_get_reg (sd, i));
4288 case O (O_LDM, SL): /* ldm, load from memory */
4289 case O (O_RTEL, SN): /* rte/l, ldm plus rte */
4290 case O (O_RTSL, SN): /* rts/l, ldm plus rts */
4292 int nregs, firstreg, i;
4294 nregs = ((GET_MEMORY_B (pc + 1) >> 4) & 0xf);
4295 firstreg = code->dst.reg & 0xf;
4296 for (i = firstreg; i >= firstreg - nregs; i--)
4298 h8_set_reg (sd, i, GET_MEMORY_L (h8_get_reg (sd, SP_REGNUM)));
4299 h8_set_reg (sd, SP_REGNUM, h8_get_reg (sd, SP_REGNUM) + 4);
4302 switch (code->opcode) {
4303 case O (O_RTEL, SN):
4305 case O (O_RTSL, SN):
4314 /* Decimal Adjust Addition. This is for BCD arithmetic. */
4315 res = GET_B_REG (code->src.reg); /* FIXME fetch? */
4316 if (!c && (0 <= (res >> 4) && (res >> 4) <= 9) &&
4317 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4318 res = res; /* Value added == 0. */
4319 else if (!c && (0 <= (res >> 4) && (res >> 4) <= 8) &&
4320 !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
4321 res = res + 0x6; /* Value added == 6. */
4322 else if (!c && (0 <= (res >> 4) && (res >> 4) <= 9) &&
4323 h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
4324 res = res + 0x6; /* Value added == 6. */
4325 else if (!c && (10 <= (res >> 4) && (res >> 4) <= 15) &&
4326 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4327 res = res + 0x60; /* Value added == 60. */
4328 else if (!c && (9 <= (res >> 4) && (res >> 4) <= 15) &&
4329 !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
4330 res = res + 0x66; /* Value added == 66. */
4331 else if (!c && (10 <= (res >> 4) && (res >> 4) <= 15) &&
4332 h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
4333 res = res + 0x66; /* Value added == 66. */
4334 else if ( c && (1 <= (res >> 4) && (res >> 4) <= 2) &&
4335 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4336 res = res + 0x60; /* Value added == 60. */
4337 else if ( c && (1 <= (res >> 4) && (res >> 4) <= 2) &&
4338 !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
4339 res = res + 0x66; /* Value added == 66. */
4340 else if (c && (1 <= (res >> 4) && (res >> 4) <= 3) &&
4341 h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
4342 res = res + 0x66; /* Value added == 66. */
4347 /* Decimal Adjust Subtraction. This is for BCD arithmetic. */
4348 res = GET_B_REG (code->src.reg); /* FIXME fetch, fetch2... */
4349 if (!c && (0 <= (res >> 4) && (res >> 4) <= 9) &&
4350 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4351 res = res; /* Value added == 0. */
4352 else if (!c && (0 <= (res >> 4) && (res >> 4) <= 8) &&
4353 h && (6 <= (res & 0xf) && (res & 0xf) <= 15))
4354 res = res + 0xfa; /* Value added == 0xfa. */
4355 else if ( c && (7 <= (res >> 4) && (res >> 4) <= 15) &&
4356 !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4357 res = res + 0xa0; /* Value added == 0xa0. */
4358 else if (c && (6 <= (res >> 4) && (res >> 4) <= 15) &&
4359 h && (6 <= (res & 0xf) && (res & 0xf) <= 15))
4360 res = res + 0x9a; /* Value added == 0x9a. */
4366 sim_engine_set_run_state (sd, sim_stopped, SIGILL);
4371 (*sim_callback->printf_filtered) (sim_callback,
4372 "sim_resume: internal error.\n");
4373 sim_engine_set_run_state (sd, sim_stopped, SIGILL);
4377 if (code->dst.type == X (OP_CCR, SB) ||
4378 code->dst.type == X (OP_CCR, SW))
4380 h8_set_ccr (sd, res);
4383 else if (h8300smode &&
4384 (code->dst.type == X (OP_EXR, SB) ||
4385 code->dst.type == X (OP_EXR, SW)))
4387 h8_set_exr (sd, res);
4388 if (h8300smode) /* Get exr. */
4390 trace = (h8_get_exr (sd) >> 7) & 1;
4391 intMask = h8_get_exr (sd) & 7;
4400 /* When a branch works */
4401 if (fetch (sd, &code->src, &res))
4403 if (res & 1) /* bad address */
4405 pc = code->next_pc + res;
4408 /* Set the cond codes from res */
4411 /* Set the flags after an 8 bit inc/dec operation */
4415 v = (rd & 0x7f) == 0x7f;
4418 /* Set the flags after an 16 bit inc/dec operation */
4422 v = (rd & 0x7fff) == 0x7fff;
4425 /* Set the flags after an 32 bit inc/dec operation */
4427 n = res & 0x80000000;
4428 nz = res & 0xffffffff;
4429 v = (rd & 0x7fffffff) == 0x7fffffff;
4433 /* Set flags after an 8 bit shift op, carry,overflow set in insn */
4436 if (store2 (sd, &code->dst, rd))
4441 /* Set flags after an 16 bit shift op, carry,overflow set in insn */
4444 if (store2 (sd, &code->dst, rd))
4449 /* Set flags after an 32 bit shift op, carry,overflow set in insn */
4450 n = (rd & 0x80000000);
4451 nz = rd & 0xffffffff;
4452 if (store2 (sd, &code->dst, rd))
4457 if (store2 (sd, &code->dst, res))
4461 /* flags after a 32bit logical operation */
4462 n = res & 0x80000000;
4463 nz = res & 0xffffffff;
4468 if (store2 (sd, &code->dst, res))
4472 /* flags after a 16bit logical operation */
4479 if (store2 (sd, &code->dst, res))
4489 if (store2 (sd, &code->dst, res))
4496 switch (code->opcode / 4)
4500 v = ((rd & 0x80) == (ea & 0x80)
4501 && (rd & 0x80) != (res & 0x80));
4506 v = ((rd & 0x80) != (-ea & 0x80)
4507 && (rd & 0x80) != (res & 0x80));
4514 break; /* No effect on v flag. */
4519 if (store2 (sd, &code->dst, res))
4525 c = (res & 0x10000);
4526 switch (code->opcode / 4)
4530 v = ((rd & 0x8000) == (ea & 0x8000)
4531 && (rd & 0x8000) != (res & 0x8000));
4536 v = ((rd & 0x8000) != (-ea & 0x8000)
4537 && (rd & 0x8000) != (res & 0x8000));
4546 if (store2 (sd, &code->dst, res))
4550 n = res & 0x80000000;
4551 nz = res & 0xffffffff;
4552 switch (code->opcode / 4)
4556 v = ((rd & 0x80000000) == (ea & 0x80000000)
4557 && (rd & 0x80000000) != (res & 0x80000000));
4558 c = ((unsigned) res < (unsigned) rd) ||
4559 ((unsigned) res < (unsigned) ea);
4564 v = ((rd & 0x80000000) != (-ea & 0x80000000)
4565 && (rd & 0x80000000) != (res & 0x80000000));
4566 c = (unsigned) rd < (unsigned) -ea;
4569 v = (rd == 0x80000000);
4576 if ((res = h8_get_delayed_branch (sd)) != 0)
4579 h8_set_delayed_branch (sd, 0);
4586 if (--poll_count < 0)
4588 poll_count = POLL_QUIT_INTERVAL;
4589 if ((*sim_callback->poll_quit) != NULL
4590 && (*sim_callback->poll_quit) (sim_callback))
4591 sim_engine_set_run_state (sd, sim_stopped, SIGINT);
4593 sim_engine_get_run_state (sd, &reason, &sigrc);
4594 } while (reason == sim_running);
4596 h8_set_ticks (sd, h8_get_ticks (sd) + get_now () - tick_start);
4597 h8_set_cycles (sd, h8_get_cycles (sd) + cycles);
4598 h8_set_insts (sd, h8_get_insts (sd) + insts);
4603 h8_set_exr (sd, (trace<<7) | intMask);
4605 h8_set_mask (sd, oldmask);
4606 signal (SIGINT, prev);
4610 sim_trace (SIM_DESC sd)
4612 /* FIXME: Unfinished. */
4613 (*sim_callback->printf_filtered) (sim_callback,
4614 "sim_trace: trace not supported.\n");
4615 return 1; /* Done. */
4619 sim_write (SIM_DESC sd, SIM_ADDR addr, const unsigned char *buffer, int size)
4626 for (i = 0; i < size; i++)
4628 if (addr < memory_size)
4630 h8_set_memory (sd, addr + i, buffer[i]);
4631 h8_set_cache_idx (sd, addr + i, 0);
4635 h8_set_eightbit (sd, (addr + i) & 0xff, buffer[i]);
4642 sim_read (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
4647 if (addr < memory_size)
4648 memcpy (buffer, h8_get_memory_buf (sd) + addr, size);
4650 memcpy (buffer, h8_get_eightbit_buf (sd) + (addr & 0xff), size);
4656 sim_store_register (SIM_DESC sd, int rn, unsigned char *value, int length)
4661 longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
4662 shortval = (value[0] << 8) | (value[1]);
4663 intval = h8300hmode ? longval : shortval;
4669 if(h8300_normal_mode)
4670 h8_set_pc (sd, shortval); /* PC for Normal mode is 2 bytes */
4672 h8_set_pc (sd, intval);
4675 (*sim_callback->printf_filtered) (sim_callback,
4676 "sim_store_register: bad regnum %d.\n",
4686 h8_set_reg (sd, rn, intval);
4689 h8_set_ccr (sd, intval);
4692 h8_set_exr (sd, intval);
4695 h8_set_sbr (sd, intval);
4698 h8_set_vbr (sd, intval);
4701 h8_set_mach (sd, intval);
4704 h8_set_macl (sd, intval);
4707 h8_set_cycles (sd, longval);
4711 h8_set_insts (sd, longval);
4715 h8_set_ticks (sd, longval);
4722 sim_fetch_register (SIM_DESC sd, int rn, unsigned char *buf, int length)
4729 if (!h8300smode && rn >= EXR_REGNUM)
4734 (*sim_callback->printf_filtered) (sim_callback,
4735 "sim_fetch_register: bad regnum %d.\n",
4740 v = h8_get_ccr (sd);
4743 v = h8_get_exr (sd);
4749 v = h8_get_sbr (sd);
4752 v = h8_get_vbr (sd);
4755 v = h8_get_mach (sd);
4758 v = h8_get_macl (sd);
4768 v = h8_get_reg (sd, rn);
4771 v = h8_get_cycles (sd);
4775 v = h8_get_ticks (sd);
4779 v = h8_get_insts (sd);
4783 /* In Normal mode PC is 2 byte, but other registers are 4 byte */
4784 if ((h8300hmode || longreg) && !(rn == PC_REGNUM && h8300_normal_mode))
4800 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
4802 sim_engine_get_run_state (sd, reason, sigrc);
4805 /* FIXME: Rename to sim_set_mem_size. */
4810 /* Memory size is fixed. */
4814 set_simcache_size (SIM_DESC sd, int n)
4817 free (sd->sim_cache);
4820 sd->sim_cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
4821 memset (sd->sim_cache, 0, sizeof (decoded_inst) * n);
4822 sd->sim_cache_size = n;
4827 sim_info (SIM_DESC sd, int verbose)
4829 double timetaken = (double) h8_get_ticks (sd) / (double) now_persec ();
4830 double virttime = h8_get_cycles (sd) / 10.0e6;
4832 (*sim_callback->printf_filtered) (sim_callback,
4833 "\n\n#instructions executed %10d\n",
4835 (*sim_callback->printf_filtered) (sim_callback,
4836 "#cycles (v approximate) %10d\n",
4837 h8_get_cycles (sd));
4838 (*sim_callback->printf_filtered) (sim_callback,
4839 "#real time taken %10.4f\n",
4841 (*sim_callback->printf_filtered) (sim_callback,
4842 "#virtual time taken %10.4f\n",
4844 if (timetaken != 0.0)
4845 (*sim_callback->printf_filtered) (sim_callback,
4846 "#simulation ratio %10.4f\n",
4847 virttime / timetaken);
4848 (*sim_callback->printf_filtered) (sim_callback,
4850 h8_get_compiles (sd));
4851 (*sim_callback->printf_filtered) (sim_callback,
4852 "#cache size %10d\n",
4853 sd->sim_cache_size);
4856 /* This to be conditional on `what' (aka `verbose'),
4857 however it was never passed as non-zero. */
4861 for (i = 0; i < O_LAST; i++)
4863 if (h8_get_stats (sd, i))
4864 (*sim_callback->printf_filtered) (sim_callback, "%d: %d\n",
4865 i, h8_get_stats (sd, i));
4871 /* Indicate whether the cpu is an H8/300 or H8/300H.
4872 FLAG is non-zero for the H8/300H. */
4875 set_h8300h (unsigned long machine)
4877 /* FIXME: Much of the code in sim_load can be moved to sim_open.
4878 This function being replaced by a sim_open:ARGV configuration
4881 h8300hmode = h8300smode = h8300sxmode = h8300_normal_mode = 0;
4883 if (machine == bfd_mach_h8300sx || machine == bfd_mach_h8300sxn)
4886 if (machine == bfd_mach_h8300s || machine == bfd_mach_h8300sn || h8300sxmode)
4889 if (machine == bfd_mach_h8300h || machine == bfd_mach_h8300hn || h8300smode)
4892 if(machine == bfd_mach_h8300hn || machine == bfd_mach_h8300sn || machine == bfd_mach_h8300sxn)
4893 h8300_normal_mode = 1;
4896 /* Cover function of sim_state_free to free the cpu buffers as well. */
4899 free_state (SIM_DESC sd)
4901 if (STATE_MODULES (sd) != NULL)
4902 sim_module_uninstall (sd);
4904 /* Fixme: free buffers in _sim_cpu. */
4905 sim_state_free (sd);
4909 sim_open (SIM_OPEN_KIND kind,
4910 struct host_callback_struct *callback,
4917 sd = sim_state_alloc (kind, callback);
4918 sd->cpu = sim_cpu_alloc (sd, 0);
4919 cpu = STATE_CPU (sd, 0);
4920 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
4921 sim_state_initialize (sd, cpu);
4922 /* sim_cpu object is new, so some initialization is needed. */
4923 init_pointers_needed = 1;
4925 /* For compatibility (FIXME: is this right?). */
4926 current_alignment = NONSTRICT_ALIGNMENT;
4927 current_target_byte_order = BIG_ENDIAN;
4929 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
4935 /* getopt will print the error message so we just have to exit if
4936 this fails. FIXME: Hmmm... in the case of gdb we need getopt
4937 to call print_filtered. */
4938 if (sim_parse_args (sd, argv) != SIM_RC_OK)
4940 /* Uninstall the modules to avoid memory leaks,
4941 file descriptor leaks, etc. */
4946 /* Check for/establish the a reference program image. */
4947 if (sim_analyze_program (sd,
4948 (STATE_PROG_ARGV (sd) != NULL
4949 ? *STATE_PROG_ARGV (sd)
4950 : NULL), abfd) != SIM_RC_OK)
4956 /* Establish any remaining configuration options. */
4957 if (sim_config (sd) != SIM_RC_OK)
4963 if (sim_post_argv_init (sd) != SIM_RC_OK)
4965 /* Uninstall the modules to avoid memory leaks,
4966 file descriptor leaks, etc. */
4971 /* sim_hw_configure (sd); */
4973 /* FIXME: Much of the code in sim_load can be moved here. */
4977 sim_callback = callback;
4982 sim_close (SIM_DESC sd, int quitting)
4984 /* Nothing to do. */
4987 /* Called by gdb to load a program into memory. */
4990 sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
4994 /* FIXME: The code below that sets a specific variant of the H8/300
4995 being simulated should be moved to sim_open(). */
4997 /* See if the file is for the H8/300 or H8/300H. */
4998 /* ??? This may not be the most efficient way. The z8k simulator
4999 does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO). */
5003 prog_bfd = bfd_openr (prog, NULL);
5004 if (prog_bfd != NULL)
5006 /* Set the cpu type. We ignore failure from bfd_check_format
5007 and bfd_openr as sim_load_file checks too. */
5008 if (bfd_check_format (prog_bfd, bfd_object))
5010 set_h8300h (bfd_get_mach (prog_bfd));
5014 /* If we're using gdb attached to the simulator, then we have to
5015 reallocate memory for the simulator.
5017 When gdb first starts, it calls fetch_registers (among other
5018 functions), which in turn calls init_pointers, which allocates
5021 The problem is when we do that, we don't know whether we're
5022 debugging an H8/300 or H8/300H program.
5024 This is the first point at which we can make that determination,
5025 so we just reallocate memory now; this will also allow us to handle
5026 switching between H8/300 and H8/300H programs without exiting
5029 if (h8300smode && !h8300_normal_mode)
5030 memory_size = H8300S_MSIZE;
5031 else if (h8300hmode && !h8300_normal_mode)
5032 memory_size = H8300H_MSIZE;
5034 memory_size = H8300_MSIZE;
5036 if (h8_get_memory_buf (sd))
5037 free (h8_get_memory_buf (sd));
5038 if (h8_get_cache_idx_buf (sd))
5039 free (h8_get_cache_idx_buf (sd));
5040 if (h8_get_eightbit_buf (sd))
5041 free (h8_get_eightbit_buf (sd));
5043 h8_set_memory_buf (sd, (unsigned char *)
5044 calloc (sizeof (char), memory_size));
5045 h8_set_cache_idx_buf (sd, (unsigned short *)
5046 calloc (sizeof (short), memory_size));
5047 sd->memory_size = memory_size;
5048 h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
5050 /* `msize' must be a power of two. */
5051 if ((memory_size & (memory_size - 1)) != 0)
5053 (*sim_callback->printf_filtered) (sim_callback,
5054 "sim_load: bad memory size.\n");
5057 h8_set_mask (sd, memory_size - 1);
5059 if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd,
5060 sim_kind == SIM_OPEN_DEBUG,
5064 /* Close the bfd if we opened it. */
5065 if (abfd == NULL && prog_bfd != NULL)
5066 bfd_close (prog_bfd);
5070 /* Close the bfd if we opened it. */
5071 if (abfd == NULL && prog_bfd != NULL)
5072 bfd_close (prog_bfd);
5077 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
5084 h8_set_pc (sd, bfd_get_start_address (abfd));
5088 /* Command Line support. */
5091 /* Counting the no. of commandline arguments. */
5092 for (no_of_args = 0; argv[no_of_args] != NULL; no_of_args++)
5095 /* Allocating memory for the argv pointers. */
5096 h8_set_command_line (sd, (char **) malloc ((sizeof (char *))
5097 * (no_of_args + 1)));
5099 for (i = 0; i < no_of_args; i++)
5101 /* Copying the argument string. */
5102 h8_set_cmdline_arg (sd, i, (char *) strdup (argv[i]));
5104 h8_set_cmdline_arg (sd, i, NULL);
5111 sim_do_command (SIM_DESC sd, char *cmd)
5113 (*sim_callback->printf_filtered) (sim_callback,
5114 "This simulator does not accept any commands.\n");
5118 sim_set_callbacks (struct host_callback_struct *ptr)