1 /* Simulator for Motorola's MCore processor
2 Copyright (C) 1999-2022 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/>. */
20 /* This must come before any other includes. */
26 #include <sys/param.h>
29 #include "sim/callback.h"
30 #include "libiberty.h"
35 #include "sim-signal.h"
36 #include "sim-syscall.h"
37 #include "sim-options.h"
39 #include "target-newlib-syscall.h"
41 #define target_big_endian (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN)
45 mcore_extract_unsigned_integer (unsigned char *addr
, int len
)
49 unsigned char * startaddr
= (unsigned char *)addr
;
50 unsigned char * endaddr
= startaddr
+ len
;
52 if (len
> (int) sizeof (unsigned long))
53 printf ("That operation is not available on integers of more than %zu bytes.",
54 sizeof (unsigned long));
56 /* Start at the most significant end of the integer, and work towards
57 the least significant. */
60 if (! target_big_endian
)
62 for (p
= endaddr
; p
> startaddr
;)
63 retval
= (retval
<< 8) | * -- p
;
67 for (p
= startaddr
; p
< endaddr
;)
68 retval
= (retval
<< 8) | * p
++;
75 mcore_store_unsigned_integer (unsigned char *addr
, int len
, unsigned long val
)
78 unsigned char * startaddr
= (unsigned char *)addr
;
79 unsigned char * endaddr
= startaddr
+ len
;
81 if (! target_big_endian
)
83 for (p
= startaddr
; p
< endaddr
;)
91 for (p
= endaddr
; p
> startaddr
;)
99 static int memcycles
= 1;
101 #define gr cpu->active_gregs
102 #define cr cpu->regs.cregs
117 /* maniuplate the carry bit */
118 #define C_ON() (sr & 1)
119 #define C_VALUE() (sr & 1)
120 #define C_OFF() ((sr & 1) == 0)
121 #define SET_C() {sr |= 1;}
122 #define CLR_C() {sr &= 0xfffffffe;}
123 #define NEW_C(v) {CLR_C(); sr |= ((v) & 1);}
125 #define SR_AF() ((sr >> 1) & 1)
126 static void set_active_regs (SIM_CPU
*cpu
)
129 cpu
->active_gregs
= cpu
->regs
.alt_gregs
;
131 cpu
->active_gregs
= cpu
->regs
.gregs
;
134 #define TRAPCODE 1 /* r1 holds which function we want */
135 #define PARM1 2 /* first parameter */
139 #define RET1 2 /* register for return values. */
141 /* Default to a 8 Mbyte (== 2^23) memory space. */
142 #define DEFAULT_MEMORY_SIZE 0x800000
145 set_initial_gprs (SIM_CPU
*cpu
)
147 /* Set up machine just out of reset. */
151 /* Clean out the GPRs and alternate GPRs. */
152 memset (&cpu
->regs
.gregs
, 0, sizeof(cpu
->regs
.gregs
));
153 memset (&cpu
->regs
.alt_gregs
, 0, sizeof(cpu
->regs
.alt_gregs
));
155 /* Make our register set point to the right place. */
156 set_active_regs (cpu
);
158 /* ABI specifies initial values for these registers. */
159 gr
[0] = DEFAULT_MEMORY_SIZE
- 4;
161 /* dac fix, the stack address must be 8-byte aligned! */
162 gr
[0] = gr
[0] - gr
[0] % 8;
169 /* Simulate a monitor trap. */
172 handle_trap1 (SIM_DESC sd
, SIM_CPU
*cpu
)
174 /* XXX: We don't pass back the actual errno value. */
175 gr
[RET1
] = sim_syscall (cpu
, gr
[TRAPCODE
], gr
[PARM1
], gr
[PARM2
], gr
[PARM3
],
180 process_stub (SIM_DESC sd
, SIM_CPU
*cpu
, int what
)
182 /* These values should match those in libgloss/mcore/syscalls.s. */
189 case 10: /* _unlink */
190 case 19: /* _lseek */
191 case 43: /* _times */
193 handle_trap1 (sd
, cpu
);
197 if (STATE_VERBOSE_P (sd
))
198 fprintf (stderr
, "Unhandled stub opcode: %d\n", what
);
204 util (SIM_DESC sd
, SIM_CPU
*cpu
, unsigned what
)
209 sim_engine_halt (sd
, cpu
, NULL
, cpu
->regs
.pc
, sim_exited
, gr
[PARM1
]);
213 if (STATE_VERBOSE_P (sd
))
214 fprintf (stderr
, "WARNING: printf unimplemented\n");
218 if (STATE_VERBOSE_P (sd
))
219 fprintf (stderr
, "WARNING: scanf unimplemented\n");
223 gr
[RET1
] = cpu
->insts
;
227 process_stub (sd
, cpu
, gr
[1]);
231 if (STATE_VERBOSE_P (sd
))
232 fprintf (stderr
, "Unhandled util code: %x\n", what
);
237 /* For figuring out whether we carried; addc/subc use this. */
239 iu_carry (unsigned long a
, unsigned long b
, int cin
)
243 x
= (a
& 0xffff) + (b
& 0xffff) + cin
;
244 x
= (x
>> 16) + (a
>> 16) + (b
>> 16);
250 /* TODO: Convert to common watchpoints. */
251 #undef WATCHFUNCTIONS
252 #ifdef WATCHFUNCTIONS
269 #define RD (inst & 0xF)
270 #define RS ((inst >> 4) & 0xF)
271 #define RX ((inst >> 8) & 0xF)
272 #define IMM5 ((inst >> 4) & 0x1F)
273 #define IMM4 ((inst) & 0xF)
275 #define rbat(X) sim_core_read_1 (cpu, 0, read_map, X)
276 #define rhat(X) sim_core_read_2 (cpu, 0, read_map, X)
277 #define rlat(X) sim_core_read_4 (cpu, 0, read_map, X)
278 #define wbat(X, D) sim_core_write_1 (cpu, 0, write_map, X, D)
279 #define what(X, D) sim_core_write_2 (cpu, 0, write_map, X, D)
280 #define wlat(X, D) sim_core_write_4 (cpu, 0, write_map, X, D)
282 static int tracing
= 0;
285 sim_engine_halt (sd, cpu, NULL, pc, sim_stopped, SIM_SIGILL)
288 step_once (SIM_DESC sd
, SIM_CPU
*cpu
)
299 #ifdef WATCHFUNCTIONS
303 pc
= CPU_PC_GET (cpu
);
305 /* Fetch the initial instructions that we'll decode. */
306 ibuf
= rlat (pc
& 0xFFFFFFFC);
313 /* make our register set point to the right place */
314 set_active_regs (cpu
);
316 #ifdef WATCHFUNCTIONS
317 /* make a hash to speed exec loop, hope it's nonzero */
320 for (w
= 1; w
<= ENDWL
; w
++)
321 WLhash
= WLhash
& WL
[w
];
324 /* TODO: Unindent this block. */
332 if (! target_big_endian
)
335 inst
= ibuf
& 0xFFFF;
340 if (! target_big_endian
)
341 inst
= ibuf
& 0xFFFF;
346 #ifdef WATCHFUNCTIONS
347 /* now scan list of watch addresses, if match, count it and
348 note return address and count cycles until pc=return address */
350 if ((WLincyc
== 1) && (pc
== WLendpc
))
352 cycs
= (cpu
->cycles
+ (insts
+ bonus_cycles
+
353 (memops
* memcycles
)) - WLbcyc
);
355 if (WLcnts
[WLW
] == 1)
362 if (cycs
> WLmax
[WLW
])
367 if (cycs
< WLmin
[WLW
])
377 /* Optimize with a hash to speed loop. */
380 if ((WLhash
== 0) || ((WLhash
& pc
) != 0))
382 for (w
=1; w
<= ENDWL
; w
++)
387 WLbcyc
= cpu
->cycles
+ insts
388 + bonus_cycles
+ (memops
* memcycles
);
400 fprintf (stderr
, "%.4lx: inst = %.4x ", pc
, inst
);
416 sim_engine_halt (sd
, cpu
, NULL
, pc
- 2,
417 sim_stopped
, SIM_SIGTRAP
);
428 set_active_regs (cpu
);
436 set_active_regs (cpu
);
440 if (STATE_VERBOSE_P (sd
))
441 fprintf (stderr
, "WARNING: stop unimplemented\n");
445 if (STATE_VERBOSE_P (sd
))
446 fprintf (stderr
, "WARNING: wait unimplemented\n");
450 if (STATE_VERBOSE_P (sd
))
451 fprintf (stderr
, "WARNING: doze unimplemented\n");
455 ILLEGAL (); /* illegal */
458 case 0x8: /* trap 0 */
459 case 0xA: /* trap 2 */
460 case 0xB: /* trap 3 */
461 sim_engine_halt (sd
, cpu
, NULL
, pc
,
462 sim_stopped
, SIM_SIGTRAP
);
465 case 0xC: /* trap 4 */
466 case 0xD: /* trap 5 */
467 case 0xE: /* trap 6 */
468 ILLEGAL (); /* illegal */
471 case 0xF: /* trap 7 */
472 sim_engine_halt (sd
, cpu
, NULL
, pc
, /* integer div-by-0 */
473 sim_stopped
, SIM_SIGTRAP
);
476 case 0x9: /* trap 1 */
477 handle_trap1 (sd
, cpu
);
483 ILLEGAL (); /* illegal */
495 int regno
= 4; /* always r4-r7 */
501 gr
[regno
] = rlat (addr
);
505 while ((regno
&0x3) != 0);
511 int regno
= 4; /* always r4-r7 */
517 wlat (addr
, gr
[regno
]);
521 while ((regno
& 0x3) != 0);
529 /* bonus cycle is really only needed if
530 the next insn shifts the last reg loaded.
537 gr
[regno
] = rlat (addr
);
548 /* this should be removed! */
549 /* bonus_cycles ++; */
551 memops
+= 16 - regno
;
554 wlat (addr
, gr
[regno
]);
575 if (tracing
&& RD
== 15)
576 fprintf (stderr
, "Func return, r2 = %lxx, r3 = %lx\n",
591 for (i
= 0; !(tmp
& 0x80000000) && i
< 32; i
++)
600 tmp
= ((tmp
& 0xaaaaaaaa) >> 1) | ((tmp
& 0x55555555) << 1);
601 tmp
= ((tmp
& 0xcccccccc) >> 2) | ((tmp
& 0x33333333) << 2);
602 tmp
= ((tmp
& 0xf0f0f0f0) >> 4) | ((tmp
& 0x0f0f0f0f) << 4);
603 tmp
= ((tmp
& 0xff00ff00) >> 8) | ((tmp
& 0x00ff00ff) << 8);
604 gr
[RD
] = ((tmp
& 0xffff0000) >> 16) | ((tmp
& 0x0000ffff) << 16);
612 case 0x0: /* xtrb3 */
613 gr
[1] = (gr
[RD
]) & 0xFF;
616 case 0x1: /* xtrb2 */
617 gr
[1] = (gr
[RD
]>>8) & 0xFF;
620 case 0x2: /* xtrb1 */
621 gr
[1] = (gr
[RD
]>>16) & 0xFF;
624 case 0x3: /* xtrb0 */
625 gr
[1] = (gr
[RD
]>>24) & 0xFF;
628 case 0x4: /* zextb */
629 gr
[RD
] &= 0x000000FF;
631 case 0x5: /* sextb */
640 case 0x6: /* zexth */
641 gr
[RD
] &= 0x0000FFFF;
643 case 0x7: /* sexth */
652 case 0x8: /* declt */
654 NEW_C ((long)gr
[RD
] < 0);
656 case 0x9: /* tstnbz */
659 NEW_C ((tmp
& 0xFF000000) != 0 &&
660 (tmp
& 0x00FF0000) != 0 && (tmp
& 0x0000FF00) != 0 &&
661 (tmp
& 0x000000FF) != 0);
664 case 0xA: /* decgt */
666 NEW_C ((long)gr
[RD
] > 0);
668 case 0xB: /* decne */
670 NEW_C ((long)gr
[RD
] != 0);
681 if (gr
[RD
] & 0x80000000)
682 gr
[RD
] = ~gr
[RD
] + 1;
689 case 0x02: /* movt */
693 case 0x03: /* mult */
694 /* consume 2 bits per cycle from rs, until rs is 0 */
696 unsigned int t
= gr
[RS
];
698 for (ticks
= 0; t
!= 0 ; t
>>= 2)
700 bonus_cycles
+= ticks
;
702 bonus_cycles
+= 2; /* min. is 3, so add 2, plus ticks above */
704 fprintf (stderr
, " mult %lx by %lx to give %lx",
705 gr
[RD
], gr
[RS
], gr
[RD
] * gr
[RS
]);
706 gr
[RD
] = gr
[RD
] * gr
[RS
];
708 case 0x04: /* loopt */
711 pc
+= (IMM4
<< 1) - 32;
715 --gr
[RS
]; /* not RD! */
716 NEW_C (((long)gr
[RS
]) > 0);
718 case 0x05: /* subu */
721 case 0x06: /* addc */
723 unsigned long tmp
, a
, b
;
726 gr
[RD
] = a
+ b
+ C_VALUE ();
727 tmp
= iu_carry (a
, b
, C_VALUE ());
731 case 0x07: /* subc */
733 unsigned long tmp
, a
, b
;
736 gr
[RD
] = a
- b
+ C_VALUE () - 1;
737 tmp
= iu_carry (a
,~b
, C_VALUE ());
741 case 0x08: /* illegal */
742 case 0x09: /* illegal*/
745 case 0x0A: /* movf */
751 unsigned long dst
, src
;
754 /* We must not rely solely upon the native shift operations, since they
755 may not match the M*Core's behaviour on boundary conditions. */
756 dst
= src
> 31 ? 0 : dst
>> src
;
760 case 0x0C: /* cmphs */
761 NEW_C ((unsigned long )gr
[RD
] >=
762 (unsigned long)gr
[RS
]);
764 case 0x0D: /* cmplt */
765 NEW_C ((long)gr
[RD
] < (long)gr
[RS
]);
768 NEW_C ((gr
[RD
] & gr
[RS
]) != 0);
770 case 0x0F: /* cmpne */
771 NEW_C (gr
[RD
] != gr
[RS
]);
773 case 0x10: case 0x11: /* mfcr */
777 if (r
<= LAST_VALID_CREG
)
787 fprintf (stderr
, "MOV %lx into reg %d", gr
[RD
], RD
);
790 case 0x13: /* bgenr */
794 gr
[RD
] = 1 << (gr
[RS
] & 0x1F);
797 case 0x14: /* rsub */
798 gr
[RD
] = gr
[RS
] - gr
[RD
];
813 case 0x18: case 0x19: /* mtcr */
817 if (r
<= LAST_VALID_CREG
)
822 /* we might have changed register sets... */
823 set_active_regs (cpu
);
828 /* We must not rely solely upon the native shift operations, since they
829 may not match the M*Core's behaviour on boundary conditions. */
831 gr
[RD
] = ((long) gr
[RD
]) < 0 ? -1 : 0;
833 gr
[RD
] = (long) gr
[RD
] >> gr
[RS
];
837 /* We must not rely solely upon the native shift operations, since they
838 may not match the M*Core's behaviour on boundary conditions. */
839 gr
[RD
] = gr
[RS
] > 31 ? 0 : gr
[RD
] << gr
[RS
];
842 case 0x1C: /* addu */
847 gr
[RD
] += gr
[RS
] << 1;
854 case 0x1F: /* andn */
857 case 0x20: case 0x21: /* addi */
861 case 0x22: case 0x23: /* cmplti */
863 int tmp
= (IMM5
+ 1);
874 case 0x24: case 0x25: /* subi */
878 case 0x26: case 0x27: /* illegal */
881 case 0x28: case 0x29: /* rsubi */
885 case 0x2A: case 0x2B: /* cmpnei */
896 case 0x2C: case 0x2D: /* bmaski, divu */
910 /* unsigned divide */
911 gr
[RD
] = (word
) ((unsigned int) gr
[RD
] / (unsigned int)gr
[1] );
913 /* compute bonus_cycles for divu */
914 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32); r1nlz
++)
917 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32); rxnlz
++)
923 exe
+= 5 + r1nlz
- rxnlz
;
925 if (exe
>= (2 * memcycles
- 1))
927 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
930 else if (imm
== 0 || imm
>= 8)
936 gr
[RD
] = (1 << imm
) - 1;
945 case 0x2E: case 0x2F: /* andi */
946 gr
[RD
] = gr
[RD
] & IMM5
;
948 case 0x30: case 0x31: /* bclri */
949 gr
[RD
] = gr
[RD
] & ~(1<<IMM5
);
951 case 0x32: case 0x33: /* bgeni, divs */
960 /* compute bonus_cycles for divu */
965 if (((rx
< 0) && (r1
> 0)) || ((rx
>= 0) && (r1
< 0)))
973 /* signed divide, general registers are of type int, so / op is OK */
974 gr
[RD
] = gr
[RD
] / gr
[1];
976 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32) ; r1nlz
++ )
979 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32) ; rxnlz
++ )
985 exe
+= 6 + r1nlz
- rxnlz
+ sc
;
987 if (exe
>= (2 * memcycles
- 1))
989 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
995 gr
[RD
] = (1 << IMM5
);
1004 case 0x34: case 0x35: /* bseti */
1005 gr
[RD
] = gr
[RD
] | (1 << IMM5
);
1007 case 0x36: case 0x37: /* btsti */
1008 NEW_C (gr
[RD
] >> IMM5
);
1010 case 0x38: case 0x39: /* xsr, rotli */
1012 unsigned imm
= IMM5
;
1013 unsigned long tmp
= gr
[RD
];
1019 gr
[RD
] = (cbit
<< 31) | (tmp
>> 1);
1022 gr
[RD
] = (tmp
<< imm
) | (tmp
>> (32 - imm
));
1025 case 0x3A: case 0x3B: /* asrc, asri */
1027 unsigned imm
= IMM5
;
1035 gr
[RD
] = tmp
>> imm
;
1038 case 0x3C: case 0x3D: /* lslc, lsli */
1040 unsigned imm
= IMM5
;
1041 unsigned long tmp
= gr
[RD
];
1048 gr
[RD
] = tmp
<< imm
;
1051 case 0x3E: case 0x3F: /* lsrc, lsri */
1053 unsigned imm
= IMM5
;
1054 unsigned long tmp
= gr
[RD
];
1061 gr
[RD
] = tmp
>> imm
;
1064 case 0x40: case 0x41: case 0x42: case 0x43:
1065 case 0x44: case 0x45: case 0x46: case 0x47:
1066 case 0x48: case 0x49: case 0x4A: case 0x4B:
1067 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1071 util (sd
, cpu
, inst
& 0xFF);
1073 case 0x51: case 0x52: case 0x53:
1074 case 0x54: case 0x55: case 0x56: case 0x57:
1075 case 0x58: case 0x59: case 0x5A: case 0x5B:
1076 case 0x5C: case 0x5D: case 0x5E: case 0x5F:
1079 case 0x60: case 0x61: case 0x62: case 0x63: /* movi */
1080 case 0x64: case 0x65: case 0x66: case 0x67:
1081 gr
[RD
] = (inst
>> 4) & 0x7F;
1083 case 0x68: case 0x69: case 0x6A: case 0x6B:
1084 case 0x6C: case 0x6D: case 0x6E: case 0x6F: /* illegal */
1087 case 0x71: case 0x72: case 0x73:
1088 case 0x74: case 0x75: case 0x76: case 0x77:
1089 case 0x78: case 0x79: case 0x7A: case 0x7B:
1090 case 0x7C: case 0x7D: case 0x7E: /* lrw */
1091 gr
[RX
] = rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1093 fprintf (stderr
, "LRW of 0x%x from 0x%lx to reg %d",
1094 rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC),
1095 (pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC, RX
);
1098 case 0x7F: /* jsri */
1102 "func call: r2 = %lx r3 = %lx r4 = %lx r5 = %lx r6 = %lx r7 = %lx\n",
1103 gr
[2], gr
[3], gr
[4], gr
[5], gr
[6], gr
[7]);
1104 case 0x70: /* jmpi */
1105 pc
= rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1111 case 0x80: case 0x81: case 0x82: case 0x83:
1112 case 0x84: case 0x85: case 0x86: case 0x87:
1113 case 0x88: case 0x89: case 0x8A: case 0x8B:
1114 case 0x8C: case 0x8D: case 0x8E: case 0x8F: /* ld */
1115 gr
[RX
] = rlat (gr
[RD
] + ((inst
>> 2) & 0x003C));
1117 fprintf (stderr
, "load reg %d from 0x%lx with 0x%lx",
1119 gr
[RD
] + ((inst
>> 2) & 0x003C), gr
[RX
]);
1122 case 0x90: case 0x91: case 0x92: case 0x93:
1123 case 0x94: case 0x95: case 0x96: case 0x97:
1124 case 0x98: case 0x99: case 0x9A: case 0x9B:
1125 case 0x9C: case 0x9D: case 0x9E: case 0x9F: /* st */
1126 wlat (gr
[RD
] + ((inst
>> 2) & 0x003C), gr
[RX
]);
1128 fprintf (stderr
, "store reg %d (containing 0x%lx) to 0x%lx",
1130 gr
[RD
] + ((inst
>> 2) & 0x003C));
1133 case 0xA0: case 0xA1: case 0xA2: case 0xA3:
1134 case 0xA4: case 0xA5: case 0xA6: case 0xA7:
1135 case 0xA8: case 0xA9: case 0xAA: case 0xAB:
1136 case 0xAC: case 0xAD: case 0xAE: case 0xAF: /* ld.b */
1137 gr
[RX
] = rbat (gr
[RD
] + RS
);
1140 case 0xB0: case 0xB1: case 0xB2: case 0xB3:
1141 case 0xB4: case 0xB5: case 0xB6: case 0xB7:
1142 case 0xB8: case 0xB9: case 0xBA: case 0xBB:
1143 case 0xBC: case 0xBD: case 0xBE: case 0xBF: /* st.b */
1144 wbat (gr
[RD
] + RS
, gr
[RX
]);
1147 case 0xC0: case 0xC1: case 0xC2: case 0xC3:
1148 case 0xC4: case 0xC5: case 0xC6: case 0xC7:
1149 case 0xC8: case 0xC9: case 0xCA: case 0xCB:
1150 case 0xCC: case 0xCD: case 0xCE: case 0xCF: /* ld.h */
1151 gr
[RX
] = rhat (gr
[RD
] + ((inst
>> 3) & 0x001E));
1154 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
1155 case 0xD4: case 0xD5: case 0xD6: case 0xD7:
1156 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1157 case 0xDC: case 0xDD: case 0xDE: case 0xDF: /* st.h */
1158 what (gr
[RD
] + ((inst
>> 3) & 0x001E), gr
[RX
]);
1161 case 0xE8: case 0xE9: case 0xEA: case 0xEB:
1162 case 0xEC: case 0xED: case 0xEE: case 0xEF: /* bf */
1166 disp
= inst
& 0x03FF;
1174 case 0xE0: case 0xE1: case 0xE2: case 0xE3:
1175 case 0xE4: case 0xE5: case 0xE6: case 0xE7: /* bt */
1179 disp
= inst
& 0x03FF;
1188 case 0xF8: case 0xF9: case 0xFA: case 0xFB:
1189 case 0xFC: case 0xFD: case 0xFE: case 0xFF: /* bsr */
1191 case 0xF0: case 0xF1: case 0xF2: case 0xF3:
1192 case 0xF4: case 0xF5: case 0xF6: case 0xF7: /* br */
1195 disp
= inst
& 0x03FF;
1207 fprintf (stderr
, "\n");
1211 ibuf
= rlat (pc
& 0xFFFFFFFC);
1216 /* Hide away the things we've cached while executing. */
1217 CPU_PC_SET (cpu
, pc
);
1218 cpu
->insts
+= insts
; /* instructions done ... */
1219 cpu
->cycles
+= insts
; /* and each takes a cycle */
1220 cpu
->cycles
+= bonus_cycles
; /* and extra cycles for branches */
1221 cpu
->cycles
+= memops
* memcycles
; /* and memop cycle delays */
1225 sim_engine_run (SIM_DESC sd
,
1226 int next_cpu_nr
, /* ignore */
1227 int nr_cpus
, /* ignore */
1228 int siggnal
) /* ignore */
1232 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1234 cpu
= STATE_CPU (sd
, 0);
1238 step_once (sd
, cpu
);
1239 if (sim_events_tick (sd
))
1240 sim_events_process (sd
);
1245 mcore_reg_store (SIM_CPU
*cpu
, int rn
, unsigned char *memory
, int length
)
1247 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1253 /* misalignment safe */
1254 ival
= mcore_extract_unsigned_integer (memory
, 4);
1255 cpu
->asints
[rn
] = ival
;
1265 mcore_reg_fetch (SIM_CPU
*cpu
, int rn
, unsigned char *memory
, int length
)
1267 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1271 long ival
= cpu
->asints
[rn
];
1273 /* misalignment-safe */
1274 mcore_store_unsigned_integer (memory
, 4, ival
);
1284 sim_info (SIM_DESC sd
, int verbose
)
1286 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1287 #ifdef WATCHFUNCTIONS
1290 double virttime
= cpu
->cycles
/ 36.0e6
;
1291 host_callback
*callback
= STATE_CALLBACK (sd
);
1293 callback
->printf_filtered (callback
, "\n\n# instructions executed %10d\n",
1295 callback
->printf_filtered (callback
, "# cycles %10d\n",
1297 callback
->printf_filtered (callback
, "# pipeline stalls %10d\n",
1299 callback
->printf_filtered (callback
, "# virtual time taken %10.4f\n",
1302 #ifdef WATCHFUNCTIONS
1303 callback
->printf_filtered (callback
, "\nNumber of watched functions: %d\n",
1308 for (w
= 1; w
<= ENDWL
; w
++)
1310 callback
->printf_filtered (callback
, "WL = %s %8x\n",WLstr
[w
],WL
[w
]);
1311 callback
->printf_filtered (callback
, " calls = %d, cycles = %d\n",
1312 WLcnts
[w
],WLcyc
[w
]);
1315 callback
->printf_filtered (callback
,
1316 " maxcpc = %d, mincpc = %d, avecpc = %d\n",
1317 WLmax
[w
],WLmin
[w
],WLcyc
[w
]/WLcnts
[w
]);
1321 callback
->printf_filtered (callback
,
1322 "Total cycles for watched functions: %d\n",wcyc
);
1327 mcore_pc_get (sim_cpu
*cpu
)
1329 return cpu
->regs
.pc
;
1333 mcore_pc_set (sim_cpu
*cpu
, sim_cia pc
)
1339 free_state (SIM_DESC sd
)
1341 if (STATE_MODULES (sd
) != NULL
)
1342 sim_module_uninstall (sd
);
1343 sim_cpu_free_all (sd
);
1344 sim_state_free (sd
);
1348 sim_open (SIM_OPEN_KIND kind
, host_callback
*cb
,
1349 struct bfd
*abfd
, char * const *argv
)
1352 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
1353 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1355 /* Set default options before parsing user options. */
1356 cb
->syscall_map
= cb_mcore_syscall_map
;
1358 /* The cpu data is kept in a separately allocated chunk of memory. */
1359 if (sim_cpu_alloc_all (sd
, 1) != SIM_RC_OK
)
1365 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
1371 /* The parser will print an error message for us, so we silently return. */
1372 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
1378 /* Check for/establish the a reference program image. */
1379 if (sim_analyze_program (sd
, STATE_PROG_FILE (sd
), abfd
) != SIM_RC_OK
)
1385 /* Configure/verify the target byte order and other runtime
1386 configuration options. */
1387 if (sim_config (sd
) != SIM_RC_OK
)
1389 sim_module_uninstall (sd
);
1393 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
1395 /* Uninstall the modules to avoid memory leaks,
1396 file descriptor leaks, etc. */
1397 sim_module_uninstall (sd
);
1401 /* CPU specific initialization. */
1402 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1404 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
1406 CPU_REG_FETCH (cpu
) = mcore_reg_fetch
;
1407 CPU_REG_STORE (cpu
) = mcore_reg_store
;
1408 CPU_PC_FETCH (cpu
) = mcore_pc_get
;
1409 CPU_PC_STORE (cpu
) = mcore_pc_set
;
1411 set_initial_gprs (cpu
); /* Reset the GPR registers. */
1414 /* Default to a 8 Mbyte (== 2^23) memory space. */
1415 sim_do_commandf (sd
, "memory-size %#x", DEFAULT_MEMORY_SIZE
);
1421 sim_create_inferior (SIM_DESC sd
, struct bfd
*prog_bfd
,
1422 char * const *argv
, char * const *env
)
1424 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1430 unsigned long strings
;
1431 unsigned long pointers
;
1432 unsigned long hi_stack
;
1435 /* Set the initial register set. */
1436 set_initial_gprs (cpu
);
1438 hi_stack
= DEFAULT_MEMORY_SIZE
- 4;
1439 CPU_PC_SET (cpu
, bfd_get_start_address (prog_bfd
));
1441 /* Calculate the argument and environment strings. */
1447 l
= strlen (*avp
) + 1; /* include the null */
1448 s_length
+= (l
+ 3) & ~3; /* make it a 4 byte boundary */
1456 l
= strlen (*avp
) + 1; /* include the null */
1457 s_length
+= (l
+ 3) & ~ 3;/* make it a 4 byte boundary */
1461 /* Claim some memory for the pointers and strings. */
1462 pointers
= hi_stack
- sizeof(word
) * (nenv
+1+nargs
+1);
1463 pointers
&= ~3; /* must be 4-byte aligned */
1466 strings
= gr
[0] - s_length
;
1467 strings
&= ~3; /* want to make it 4-byte aligned */
1469 /* dac fix, the stack address must be 8-byte aligned! */
1470 gr
[0] = gr
[0] - gr
[0] % 8;
1472 /* Loop through the arguments and fill them in. */
1476 /* No strings to fill in. */
1481 gr
[PARM2
] = pointers
;
1485 /* Save where we're putting it. */
1486 wlat (pointers
, strings
);
1488 /* Copy the string. */
1489 l
= strlen (* avp
) + 1;
1490 sim_core_write_buffer (sd
, cpu
, write_map
, *avp
, strings
, l
);
1492 /* Bump the pointers. */
1498 /* A null to finish the list. */
1503 /* Now do the environment pointers. */
1506 /* No strings to fill in. */
1511 gr
[PARM3
] = pointers
;
1516 /* Save where we're putting it. */
1517 wlat (pointers
, strings
);
1519 /* Copy the string. */
1520 l
= strlen (* avp
) + 1;
1521 sim_core_write_buffer (sd
, cpu
, write_map
, *avp
, strings
, l
);
1523 /* Bump the pointers. */
1529 /* A null to finish the list. */