1 /* Simulator for Motorola's MCore processor
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include <sys/times.h>
25 #include <sys/param.h>
28 #include "gdb/callback.h"
29 #include "libiberty.h"
30 #include "gdb/remote-sim.h"
34 #include "sim-syscall.h"
35 #include "sim-options.h"
37 #define target_big_endian (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN)
41 mcore_extract_unsigned_integer (unsigned char *addr
, int len
)
45 unsigned char * startaddr
= (unsigned char *)addr
;
46 unsigned char * endaddr
= startaddr
+ len
;
48 if (len
> (int) sizeof (unsigned long))
49 printf ("That operation is not available on integers of more than %zu bytes.",
50 sizeof (unsigned long));
52 /* Start at the most significant end of the integer, and work towards
53 the least significant. */
56 if (! target_big_endian
)
58 for (p
= endaddr
; p
> startaddr
;)
59 retval
= (retval
<< 8) | * -- p
;
63 for (p
= startaddr
; p
< endaddr
;)
64 retval
= (retval
<< 8) | * p
++;
71 mcore_store_unsigned_integer (unsigned char *addr
, int len
, unsigned long val
)
74 unsigned char * startaddr
= (unsigned char *)addr
;
75 unsigned char * endaddr
= startaddr
+ len
;
77 if (! target_big_endian
)
79 for (p
= startaddr
; p
< endaddr
;)
87 for (p
= endaddr
; p
> startaddr
;)
95 static int memcycles
= 1;
97 #define gr cpu->active_gregs
98 #define cr cpu->regs.cregs
113 /* maniuplate the carry bit */
114 #define C_ON() (sr & 1)
115 #define C_VALUE() (sr & 1)
116 #define C_OFF() ((sr & 1) == 0)
117 #define SET_C() {sr |= 1;}
118 #define CLR_C() {sr &= 0xfffffffe;}
119 #define NEW_C(v) {CLR_C(); sr |= ((v) & 1);}
121 #define SR_AF() ((sr >> 1) & 1)
122 static void set_active_regs (SIM_CPU
*cpu
)
125 cpu
->active_gregs
= cpu
->regs
.alt_gregs
;
127 cpu
->active_gregs
= cpu
->regs
.gregs
;
130 #define TRAPCODE 1 /* r1 holds which function we want */
131 #define PARM1 2 /* first parameter */
135 #define RET1 2 /* register for return values. */
137 /* Default to a 8 Mbyte (== 2^23) memory space. */
138 #define DEFAULT_MEMORY_SIZE 0x800000
141 set_initial_gprs (SIM_CPU
*cpu
)
143 /* Set up machine just out of reset. */
147 /* Clean out the GPRs and alternate GPRs. */
148 memset (&cpu
->regs
.gregs
, 0, sizeof(cpu
->regs
.gregs
));
149 memset (&cpu
->regs
.alt_gregs
, 0, sizeof(cpu
->regs
.alt_gregs
));
151 /* Make our register set point to the right place. */
152 set_active_regs (cpu
);
154 /* ABI specifies initial values for these registers. */
155 gr
[0] = DEFAULT_MEMORY_SIZE
- 4;
157 /* dac fix, the stack address must be 8-byte aligned! */
158 gr
[0] = gr
[0] - gr
[0] % 8;
165 /* Simulate a monitor trap. */
168 handle_trap1 (SIM_DESC sd
, SIM_CPU
*cpu
)
170 /* XXX: We don't pass back the actual errno value. */
171 gr
[RET1
] = sim_syscall (cpu
, gr
[TRAPCODE
], gr
[PARM1
], gr
[PARM2
], gr
[PARM3
],
176 process_stub (SIM_DESC sd
, SIM_CPU
*cpu
, int what
)
178 /* These values should match those in libgloss/mcore/syscalls.s. */
185 case 10: /* _unlink */
186 case 19: /* _lseek */
187 case 43: /* _times */
189 handle_trap1 (sd
, cpu
);
193 if (STATE_VERBOSE_P (sd
))
194 fprintf (stderr
, "Unhandled stub opcode: %d\n", what
);
200 util (SIM_DESC sd
, SIM_CPU
*cpu
, unsigned what
)
205 sim_engine_halt (sd
, cpu
, NULL
, cpu
->regs
.pc
, sim_exited
, gr
[PARM1
]);
209 if (STATE_VERBOSE_P (sd
))
210 fprintf (stderr
, "WARNING: printf unimplemented\n");
214 if (STATE_VERBOSE_P (sd
))
215 fprintf (stderr
, "WARNING: scanf unimplemented\n");
219 gr
[RET1
] = cpu
->insts
;
223 process_stub (sd
, cpu
, gr
[1]);
227 if (STATE_VERBOSE_P (sd
))
228 fprintf (stderr
, "Unhandled util code: %x\n", what
);
233 /* For figuring out whether we carried; addc/subc use this. */
235 iu_carry (unsigned long a
, unsigned long b
, int cin
)
239 x
= (a
& 0xffff) + (b
& 0xffff) + cin
;
240 x
= (x
>> 16) + (a
>> 16) + (b
>> 16);
246 /* TODO: Convert to common watchpoints. */
247 #undef WATCHFUNCTIONS
248 #ifdef WATCHFUNCTIONS
265 #define RD (inst & 0xF)
266 #define RS ((inst >> 4) & 0xF)
267 #define RX ((inst >> 8) & 0xF)
268 #define IMM5 ((inst >> 4) & 0x1F)
269 #define IMM4 ((inst) & 0xF)
271 #define rbat(X) sim_core_read_1 (cpu, 0, read_map, X)
272 #define rhat(X) sim_core_read_2 (cpu, 0, read_map, X)
273 #define rlat(X) sim_core_read_4 (cpu, 0, read_map, X)
274 #define wbat(X, D) sim_core_write_1 (cpu, 0, write_map, X, D)
275 #define what(X, D) sim_core_write_2 (cpu, 0, write_map, X, D)
276 #define wlat(X, D) sim_core_write_4 (cpu, 0, write_map, X, D)
278 static int tracing
= 0;
281 sim_engine_halt (sd, cpu, NULL, pc, sim_stopped, SIM_SIGILL)
284 step_once (SIM_DESC sd
, SIM_CPU
*cpu
)
295 #ifdef WATCHFUNCTIONS
299 pc
= CPU_PC_GET (cpu
);
301 /* Fetch the initial instructions that we'll decode. */
302 ibuf
= rlat (pc
& 0xFFFFFFFC);
309 /* make our register set point to the right place */
310 set_active_regs (cpu
);
312 #ifdef WATCHFUNCTIONS
313 /* make a hash to speed exec loop, hope it's nonzero */
316 for (w
= 1; w
<= ENDWL
; w
++)
317 WLhash
= WLhash
& WL
[w
];
320 /* TODO: Unindent this block. */
328 if (! target_big_endian
)
331 inst
= ibuf
& 0xFFFF;
336 if (! target_big_endian
)
337 inst
= ibuf
& 0xFFFF;
342 #ifdef WATCHFUNCTIONS
343 /* now scan list of watch addresses, if match, count it and
344 note return address and count cycles until pc=return address */
346 if ((WLincyc
== 1) && (pc
== WLendpc
))
348 cycs
= (cpu
->cycles
+ (insts
+ bonus_cycles
+
349 (memops
* memcycles
)) - WLbcyc
);
351 if (WLcnts
[WLW
] == 1)
358 if (cycs
> WLmax
[WLW
])
363 if (cycs
< WLmin
[WLW
])
373 /* Optimize with a hash to speed loop. */
376 if ((WLhash
== 0) || ((WLhash
& pc
) != 0))
378 for (w
=1; w
<= ENDWL
; w
++)
383 WLbcyc
= cpu
->cycles
+ insts
384 + bonus_cycles
+ (memops
* memcycles
);
396 fprintf (stderr
, "%.4lx: inst = %.4x ", pc
, inst
);
412 sim_engine_halt (sd
, cpu
, NULL
, pc
- 2,
413 sim_stopped
, SIM_SIGTRAP
);
424 set_active_regs (cpu
);
432 set_active_regs (cpu
);
436 if (STATE_VERBOSE_P (sd
))
437 fprintf (stderr
, "WARNING: stop unimplemented\n");
441 if (STATE_VERBOSE_P (sd
))
442 fprintf (stderr
, "WARNING: wait unimplemented\n");
446 if (STATE_VERBOSE_P (sd
))
447 fprintf (stderr
, "WARNING: doze unimplemented\n");
451 ILLEGAL (); /* illegal */
454 case 0x8: /* trap 0 */
455 case 0xA: /* trap 2 */
456 case 0xB: /* trap 3 */
457 sim_engine_halt (sd
, cpu
, NULL
, pc
,
458 sim_stopped
, SIM_SIGTRAP
);
461 case 0xC: /* trap 4 */
462 case 0xD: /* trap 5 */
463 case 0xE: /* trap 6 */
464 ILLEGAL (); /* illegal */
467 case 0xF: /* trap 7 */
468 sim_engine_halt (sd
, cpu
, NULL
, pc
, /* integer div-by-0 */
469 sim_stopped
, SIM_SIGTRAP
);
472 case 0x9: /* trap 1 */
473 handle_trap1 (sd
, cpu
);
479 ILLEGAL (); /* illegal */
491 int regno
= 4; /* always r4-r7 */
497 gr
[regno
] = rlat (addr
);
501 while ((regno
&0x3) != 0);
507 int regno
= 4; /* always r4-r7 */
513 wlat (addr
, gr
[regno
]);
517 while ((regno
& 0x3) != 0);
525 /* bonus cycle is really only needed if
526 the next insn shifts the last reg loaded.
533 gr
[regno
] = rlat (addr
);
544 /* this should be removed! */
545 /* bonus_cycles ++; */
547 memops
+= 16 - regno
;
550 wlat (addr
, gr
[regno
]);
571 if (tracing
&& RD
== 15)
572 fprintf (stderr
, "Func return, r2 = %lxx, r3 = %lx\n",
587 for (i
= 0; !(tmp
& 0x80000000) && i
< 32; i
++)
596 tmp
= ((tmp
& 0xaaaaaaaa) >> 1) | ((tmp
& 0x55555555) << 1);
597 tmp
= ((tmp
& 0xcccccccc) >> 2) | ((tmp
& 0x33333333) << 2);
598 tmp
= ((tmp
& 0xf0f0f0f0) >> 4) | ((tmp
& 0x0f0f0f0f) << 4);
599 tmp
= ((tmp
& 0xff00ff00) >> 8) | ((tmp
& 0x00ff00ff) << 8);
600 gr
[RD
] = ((tmp
& 0xffff0000) >> 16) | ((tmp
& 0x0000ffff) << 16);
608 case 0x0: /* xtrb3 */
609 gr
[1] = (gr
[RD
]) & 0xFF;
612 case 0x1: /* xtrb2 */
613 gr
[1] = (gr
[RD
]>>8) & 0xFF;
616 case 0x2: /* xtrb1 */
617 gr
[1] = (gr
[RD
]>>16) & 0xFF;
620 case 0x3: /* xtrb0 */
621 gr
[1] = (gr
[RD
]>>24) & 0xFF;
624 case 0x4: /* zextb */
625 gr
[RD
] &= 0x000000FF;
627 case 0x5: /* sextb */
636 case 0x6: /* zexth */
637 gr
[RD
] &= 0x0000FFFF;
639 case 0x7: /* sexth */
648 case 0x8: /* declt */
650 NEW_C ((long)gr
[RD
] < 0);
652 case 0x9: /* tstnbz */
655 NEW_C ((tmp
& 0xFF000000) != 0 &&
656 (tmp
& 0x00FF0000) != 0 && (tmp
& 0x0000FF00) != 0 &&
657 (tmp
& 0x000000FF) != 0);
660 case 0xA: /* decgt */
662 NEW_C ((long)gr
[RD
] > 0);
664 case 0xB: /* decne */
666 NEW_C ((long)gr
[RD
] != 0);
677 if (gr
[RD
] & 0x80000000)
678 gr
[RD
] = ~gr
[RD
] + 1;
685 case 0x02: /* movt */
689 case 0x03: /* mult */
690 /* consume 2 bits per cycle from rs, until rs is 0 */
692 unsigned int t
= gr
[RS
];
694 for (ticks
= 0; t
!= 0 ; t
>>= 2)
696 bonus_cycles
+= ticks
;
698 bonus_cycles
+= 2; /* min. is 3, so add 2, plus ticks above */
700 fprintf (stderr
, " mult %lx by %lx to give %lx",
701 gr
[RD
], gr
[RS
], gr
[RD
] * gr
[RS
]);
702 gr
[RD
] = gr
[RD
] * gr
[RS
];
704 case 0x04: /* loopt */
707 pc
+= (IMM4
<< 1) - 32;
711 --gr
[RS
]; /* not RD! */
712 NEW_C (((long)gr
[RS
]) > 0);
714 case 0x05: /* subu */
717 case 0x06: /* addc */
719 unsigned long tmp
, a
, b
;
722 gr
[RD
] = a
+ b
+ C_VALUE ();
723 tmp
= iu_carry (a
, b
, C_VALUE ());
727 case 0x07: /* subc */
729 unsigned long tmp
, a
, b
;
732 gr
[RD
] = a
- b
+ C_VALUE () - 1;
733 tmp
= iu_carry (a
,~b
, C_VALUE ());
737 case 0x08: /* illegal */
738 case 0x09: /* illegal*/
741 case 0x0A: /* movf */
747 unsigned long dst
, src
;
750 /* We must not rely solely upon the native shift operations, since they
751 may not match the M*Core's behaviour on boundary conditions. */
752 dst
= src
> 31 ? 0 : dst
>> src
;
756 case 0x0C: /* cmphs */
757 NEW_C ((unsigned long )gr
[RD
] >=
758 (unsigned long)gr
[RS
]);
760 case 0x0D: /* cmplt */
761 NEW_C ((long)gr
[RD
] < (long)gr
[RS
]);
764 NEW_C ((gr
[RD
] & gr
[RS
]) != 0);
766 case 0x0F: /* cmpne */
767 NEW_C (gr
[RD
] != gr
[RS
]);
769 case 0x10: case 0x11: /* mfcr */
773 if (r
<= LAST_VALID_CREG
)
783 fprintf (stderr
, "MOV %lx into reg %d", gr
[RD
], RD
);
786 case 0x13: /* bgenr */
790 gr
[RD
] = 1 << (gr
[RS
] & 0x1F);
793 case 0x14: /* rsub */
794 gr
[RD
] = gr
[RS
] - gr
[RD
];
809 case 0x18: case 0x19: /* mtcr */
813 if (r
<= LAST_VALID_CREG
)
818 /* we might have changed register sets... */
819 set_active_regs (cpu
);
824 /* We must not rely solely upon the native shift operations, since they
825 may not match the M*Core's behaviour on boundary conditions. */
827 gr
[RD
] = ((long) gr
[RD
]) < 0 ? -1 : 0;
829 gr
[RD
] = (long) gr
[RD
] >> gr
[RS
];
833 /* We must not rely solely upon the native shift operations, since they
834 may not match the M*Core's behaviour on boundary conditions. */
835 gr
[RD
] = gr
[RS
] > 31 ? 0 : gr
[RD
] << gr
[RS
];
838 case 0x1C: /* addu */
843 gr
[RD
] += gr
[RS
] << 1;
850 case 0x1F: /* andn */
853 case 0x20: case 0x21: /* addi */
857 case 0x22: case 0x23: /* cmplti */
859 int tmp
= (IMM5
+ 1);
870 case 0x24: case 0x25: /* subi */
874 case 0x26: case 0x27: /* illegal */
877 case 0x28: case 0x29: /* rsubi */
881 case 0x2A: case 0x2B: /* cmpnei */
892 case 0x2C: case 0x2D: /* bmaski, divu */
906 /* unsigned divide */
907 gr
[RD
] = (word
) ((unsigned int) gr
[RD
] / (unsigned int)gr
[1] );
909 /* compute bonus_cycles for divu */
910 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32); r1nlz
++)
913 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32); rxnlz
++)
919 exe
+= 5 + r1nlz
- rxnlz
;
921 if (exe
>= (2 * memcycles
- 1))
923 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
926 else if (imm
== 0 || imm
>= 8)
932 gr
[RD
] = (1 << imm
) - 1;
941 case 0x2E: case 0x2F: /* andi */
942 gr
[RD
] = gr
[RD
] & IMM5
;
944 case 0x30: case 0x31: /* bclri */
945 gr
[RD
] = gr
[RD
] & ~(1<<IMM5
);
947 case 0x32: case 0x33: /* bgeni, divs */
956 /* compute bonus_cycles for divu */
961 if (((rx
< 0) && (r1
> 0)) || ((rx
>= 0) && (r1
< 0)))
969 /* signed divide, general registers are of type int, so / op is OK */
970 gr
[RD
] = gr
[RD
] / gr
[1];
972 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32) ; r1nlz
++ )
975 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32) ; rxnlz
++ )
981 exe
+= 6 + r1nlz
- rxnlz
+ sc
;
983 if (exe
>= (2 * memcycles
- 1))
985 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
991 gr
[RD
] = (1 << IMM5
);
1000 case 0x34: case 0x35: /* bseti */
1001 gr
[RD
] = gr
[RD
] | (1 << IMM5
);
1003 case 0x36: case 0x37: /* btsti */
1004 NEW_C (gr
[RD
] >> IMM5
);
1006 case 0x38: case 0x39: /* xsr, rotli */
1008 unsigned imm
= IMM5
;
1009 unsigned long tmp
= gr
[RD
];
1015 gr
[RD
] = (cbit
<< 31) | (tmp
>> 1);
1018 gr
[RD
] = (tmp
<< imm
) | (tmp
>> (32 - imm
));
1021 case 0x3A: case 0x3B: /* asrc, asri */
1023 unsigned imm
= IMM5
;
1031 gr
[RD
] = tmp
>> imm
;
1034 case 0x3C: case 0x3D: /* lslc, lsli */
1036 unsigned imm
= IMM5
;
1037 unsigned long tmp
= gr
[RD
];
1044 gr
[RD
] = tmp
<< imm
;
1047 case 0x3E: case 0x3F: /* lsrc, lsri */
1049 unsigned imm
= IMM5
;
1050 unsigned long tmp
= gr
[RD
];
1057 gr
[RD
] = tmp
>> imm
;
1060 case 0x40: case 0x41: case 0x42: case 0x43:
1061 case 0x44: case 0x45: case 0x46: case 0x47:
1062 case 0x48: case 0x49: case 0x4A: case 0x4B:
1063 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1067 util (sd
, cpu
, inst
& 0xFF);
1069 case 0x51: case 0x52: case 0x53:
1070 case 0x54: case 0x55: case 0x56: case 0x57:
1071 case 0x58: case 0x59: case 0x5A: case 0x5B:
1072 case 0x5C: case 0x5D: case 0x5E: case 0x5F:
1075 case 0x60: case 0x61: case 0x62: case 0x63: /* movi */
1076 case 0x64: case 0x65: case 0x66: case 0x67:
1077 gr
[RD
] = (inst
>> 4) & 0x7F;
1079 case 0x68: case 0x69: case 0x6A: case 0x6B:
1080 case 0x6C: case 0x6D: case 0x6E: case 0x6F: /* illegal */
1083 case 0x71: case 0x72: case 0x73:
1084 case 0x74: case 0x75: case 0x76: case 0x77:
1085 case 0x78: case 0x79: case 0x7A: case 0x7B:
1086 case 0x7C: case 0x7D: case 0x7E: /* lrw */
1087 gr
[RX
] = rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1089 fprintf (stderr
, "LRW of 0x%x from 0x%lx to reg %d",
1090 rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC),
1091 (pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC, RX
);
1094 case 0x7F: /* jsri */
1098 "func call: r2 = %lx r3 = %lx r4 = %lx r5 = %lx r6 = %lx r7 = %lx\n",
1099 gr
[2], gr
[3], gr
[4], gr
[5], gr
[6], gr
[7]);
1100 case 0x70: /* jmpi */
1101 pc
= rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1107 case 0x80: case 0x81: case 0x82: case 0x83:
1108 case 0x84: case 0x85: case 0x86: case 0x87:
1109 case 0x88: case 0x89: case 0x8A: case 0x8B:
1110 case 0x8C: case 0x8D: case 0x8E: case 0x8F: /* ld */
1111 gr
[RX
] = rlat (gr
[RD
] + ((inst
>> 2) & 0x003C));
1113 fprintf (stderr
, "load reg %d from 0x%lx with 0x%lx",
1115 gr
[RD
] + ((inst
>> 2) & 0x003C), gr
[RX
]);
1118 case 0x90: case 0x91: case 0x92: case 0x93:
1119 case 0x94: case 0x95: case 0x96: case 0x97:
1120 case 0x98: case 0x99: case 0x9A: case 0x9B:
1121 case 0x9C: case 0x9D: case 0x9E: case 0x9F: /* st */
1122 wlat (gr
[RD
] + ((inst
>> 2) & 0x003C), gr
[RX
]);
1124 fprintf (stderr
, "store reg %d (containing 0x%lx) to 0x%lx",
1126 gr
[RD
] + ((inst
>> 2) & 0x003C));
1129 case 0xA0: case 0xA1: case 0xA2: case 0xA3:
1130 case 0xA4: case 0xA5: case 0xA6: case 0xA7:
1131 case 0xA8: case 0xA9: case 0xAA: case 0xAB:
1132 case 0xAC: case 0xAD: case 0xAE: case 0xAF: /* ld.b */
1133 gr
[RX
] = rbat (gr
[RD
] + RS
);
1136 case 0xB0: case 0xB1: case 0xB2: case 0xB3:
1137 case 0xB4: case 0xB5: case 0xB6: case 0xB7:
1138 case 0xB8: case 0xB9: case 0xBA: case 0xBB:
1139 case 0xBC: case 0xBD: case 0xBE: case 0xBF: /* st.b */
1140 wbat (gr
[RD
] + RS
, gr
[RX
]);
1143 case 0xC0: case 0xC1: case 0xC2: case 0xC3:
1144 case 0xC4: case 0xC5: case 0xC6: case 0xC7:
1145 case 0xC8: case 0xC9: case 0xCA: case 0xCB:
1146 case 0xCC: case 0xCD: case 0xCE: case 0xCF: /* ld.h */
1147 gr
[RX
] = rhat (gr
[RD
] + ((inst
>> 3) & 0x001E));
1150 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
1151 case 0xD4: case 0xD5: case 0xD6: case 0xD7:
1152 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1153 case 0xDC: case 0xDD: case 0xDE: case 0xDF: /* st.h */
1154 what (gr
[RD
] + ((inst
>> 3) & 0x001E), gr
[RX
]);
1157 case 0xE8: case 0xE9: case 0xEA: case 0xEB:
1158 case 0xEC: case 0xED: case 0xEE: case 0xEF: /* bf */
1162 disp
= inst
& 0x03FF;
1170 case 0xE0: case 0xE1: case 0xE2: case 0xE3:
1171 case 0xE4: case 0xE5: case 0xE6: case 0xE7: /* bt */
1175 disp
= inst
& 0x03FF;
1184 case 0xF8: case 0xF9: case 0xFA: case 0xFB:
1185 case 0xFC: case 0xFD: case 0xFE: case 0xFF: /* bsr */
1187 case 0xF0: case 0xF1: case 0xF2: case 0xF3:
1188 case 0xF4: case 0xF5: case 0xF6: case 0xF7: /* br */
1191 disp
= inst
& 0x03FF;
1203 fprintf (stderr
, "\n");
1207 ibuf
= rlat (pc
& 0xFFFFFFFC);
1212 /* Hide away the things we've cached while executing. */
1213 CPU_PC_SET (cpu
, pc
);
1214 cpu
->insts
+= insts
; /* instructions done ... */
1215 cpu
->cycles
+= insts
; /* and each takes a cycle */
1216 cpu
->cycles
+= bonus_cycles
; /* and extra cycles for branches */
1217 cpu
->cycles
+= memops
* memcycles
; /* and memop cycle delays */
1221 sim_engine_run (SIM_DESC sd
,
1222 int next_cpu_nr
, /* ignore */
1223 int nr_cpus
, /* ignore */
1224 int siggnal
) /* ignore */
1228 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1230 cpu
= STATE_CPU (sd
, 0);
1234 step_once (sd
, cpu
);
1235 if (sim_events_tick (sd
))
1236 sim_events_process (sd
);
1241 mcore_reg_store (SIM_CPU
*cpu
, int rn
, unsigned char *memory
, int length
)
1243 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1249 /* misalignment safe */
1250 ival
= mcore_extract_unsigned_integer (memory
, 4);
1251 cpu
->asints
[rn
] = ival
;
1261 mcore_reg_fetch (SIM_CPU
*cpu
, int rn
, unsigned char *memory
, int length
)
1263 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1267 long ival
= cpu
->asints
[rn
];
1269 /* misalignment-safe */
1270 mcore_store_unsigned_integer (memory
, 4, ival
);
1280 sim_info (SIM_DESC sd
, int verbose
)
1282 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1283 #ifdef WATCHFUNCTIONS
1286 double virttime
= cpu
->cycles
/ 36.0e6
;
1287 host_callback
*callback
= STATE_CALLBACK (sd
);
1289 callback
->printf_filtered (callback
, "\n\n# instructions executed %10d\n",
1291 callback
->printf_filtered (callback
, "# cycles %10d\n",
1293 callback
->printf_filtered (callback
, "# pipeline stalls %10d\n",
1295 callback
->printf_filtered (callback
, "# virtual time taken %10.4f\n",
1298 #ifdef WATCHFUNCTIONS
1299 callback
->printf_filtered (callback
, "\nNumber of watched functions: %d\n",
1304 for (w
= 1; w
<= ENDWL
; w
++)
1306 callback
->printf_filtered (callback
, "WL = %s %8x\n",WLstr
[w
],WL
[w
]);
1307 callback
->printf_filtered (callback
, " calls = %d, cycles = %d\n",
1308 WLcnts
[w
],WLcyc
[w
]);
1311 callback
->printf_filtered (callback
,
1312 " maxcpc = %d, mincpc = %d, avecpc = %d\n",
1313 WLmax
[w
],WLmin
[w
],WLcyc
[w
]/WLcnts
[w
]);
1317 callback
->printf_filtered (callback
,
1318 "Total cycles for watched functions: %d\n",wcyc
);
1323 mcore_pc_get (sim_cpu
*cpu
)
1325 return cpu
->regs
.pc
;
1329 mcore_pc_set (sim_cpu
*cpu
, sim_cia pc
)
1335 free_state (SIM_DESC sd
)
1337 if (STATE_MODULES (sd
) != NULL
)
1338 sim_module_uninstall (sd
);
1339 sim_cpu_free_all (sd
);
1340 sim_state_free (sd
);
1344 sim_open (SIM_OPEN_KIND kind
, host_callback
*cb
, struct bfd
*abfd
, char **argv
)
1347 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
1348 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1350 /* The cpu data is kept in a separately allocated chunk of memory. */
1351 if (sim_cpu_alloc_all (sd
, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK
)
1357 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
1363 /* getopt will print the error message so we just have to exit if this fails.
1364 FIXME: Hmmm... in the case of gdb we need getopt to call
1366 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
1372 /* Check for/establish the a reference program image. */
1373 if (sim_analyze_program (sd
,
1374 (STATE_PROG_ARGV (sd
) != NULL
1375 ? *STATE_PROG_ARGV (sd
)
1376 : NULL
), abfd
) != SIM_RC_OK
)
1382 /* Configure/verify the target byte order and other runtime
1383 configuration options. */
1384 if (sim_config (sd
) != SIM_RC_OK
)
1386 sim_module_uninstall (sd
);
1390 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
1392 /* Uninstall the modules to avoid memory leaks,
1393 file descriptor leaks, etc. */
1394 sim_module_uninstall (sd
);
1398 /* CPU specific initialization. */
1399 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1401 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
1403 CPU_REG_FETCH (cpu
) = mcore_reg_fetch
;
1404 CPU_REG_STORE (cpu
) = mcore_reg_store
;
1405 CPU_PC_FETCH (cpu
) = mcore_pc_get
;
1406 CPU_PC_STORE (cpu
) = mcore_pc_set
;
1408 set_initial_gprs (cpu
); /* Reset the GPR registers. */
1411 /* Default to a 8 Mbyte (== 2^23) memory space. */
1412 sim_do_commandf (sd
, "memory-size %#x", DEFAULT_MEMORY_SIZE
);
1418 sim_create_inferior (SIM_DESC sd
, struct bfd
*prog_bfd
, char **argv
, char **env
)
1420 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1426 unsigned long strings
;
1427 unsigned long pointers
;
1428 unsigned long hi_stack
;
1431 /* Set the initial register set. */
1432 set_initial_gprs (cpu
);
1434 hi_stack
= DEFAULT_MEMORY_SIZE
- 4;
1435 CPU_PC_SET (cpu
, bfd_get_start_address (prog_bfd
));
1437 /* Calculate the argument and environment strings. */
1443 l
= strlen (*avp
) + 1; /* include the null */
1444 s_length
+= (l
+ 3) & ~3; /* make it a 4 byte boundary */
1452 l
= strlen (*avp
) + 1; /* include the null */
1453 s_length
+= (l
+ 3) & ~ 3;/* make it a 4 byte boundary */
1457 /* Claim some memory for the pointers and strings. */
1458 pointers
= hi_stack
- sizeof(word
) * (nenv
+1+nargs
+1);
1459 pointers
&= ~3; /* must be 4-byte aligned */
1462 strings
= gr
[0] - s_length
;
1463 strings
&= ~3; /* want to make it 4-byte aligned */
1465 /* dac fix, the stack address must be 8-byte aligned! */
1466 gr
[0] = gr
[0] - gr
[0] % 8;
1468 /* Loop through the arguments and fill them in. */
1472 /* No strings to fill in. */
1477 gr
[PARM2
] = pointers
;
1481 /* Save where we're putting it. */
1482 wlat (pointers
, strings
);
1484 /* Copy the string. */
1485 l
= strlen (* avp
) + 1;
1486 sim_core_write_buffer (sd
, cpu
, write_map
, *avp
, strings
, l
);
1488 /* Bump the pointers. */
1494 /* A null to finish the list. */
1499 /* Now do the environment pointers. */
1502 /* No strings to fill in. */
1507 gr
[PARM3
] = pointers
;
1512 /* Save where we're putting it. */
1513 wlat (pointers
, strings
);
1515 /* Copy the string. */
1516 l
= strlen (* avp
) + 1;
1517 sim_core_write_buffer (sd
, cpu
, write_map
, *avp
, strings
, l
);
1519 /* Bump the pointers. */
1525 /* A null to finish the list. */