1 /* Simulator for Motorola's MCore processor
2 Copyright (C) 1999-2024 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 #include "mcore-sim.h"
43 #define target_big_endian (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN)
47 mcore_extract_unsigned_integer (const unsigned char *addr
, int len
)
51 unsigned char * startaddr
= (unsigned char *)addr
;
52 unsigned char * endaddr
= startaddr
+ len
;
54 if (len
> (int) sizeof (unsigned long))
55 printf ("That operation is not available on integers of more than %zu bytes.",
56 sizeof (unsigned long));
58 /* Start at the most significant end of the integer, and work towards
59 the least significant. */
62 if (! target_big_endian
)
64 for (p
= endaddr
; p
> startaddr
;)
65 retval
= (retval
<< 8) | * -- p
;
69 for (p
= startaddr
; p
< endaddr
;)
70 retval
= (retval
<< 8) | * p
++;
77 mcore_store_unsigned_integer (unsigned char *addr
, int len
, unsigned long val
)
80 unsigned char * startaddr
= (unsigned char *)addr
;
81 unsigned char * endaddr
= startaddr
+ len
;
83 if (! target_big_endian
)
85 for (p
= startaddr
; p
< endaddr
;)
93 for (p
= endaddr
; p
> startaddr
;)
101 static int memcycles
= 1;
103 #define gr MCORE_SIM_CPU (cpu)->active_gregs
104 #define cr MCORE_SIM_CPU (cpu)->regs.cregs
119 /* maniuplate the carry bit */
120 #define C_ON() (sr & 1)
121 #define C_VALUE() (sr & 1)
122 #define C_OFF() ((sr & 1) == 0)
123 #define SET_C() {sr |= 1;}
124 #define CLR_C() {sr &= 0xfffffffe;}
125 #define NEW_C(v) {CLR_C(); sr |= ((v) & 1);}
127 #define SR_AF() ((sr >> 1) & 1)
128 static void set_active_regs (SIM_CPU
*cpu
)
130 struct mcore_sim_cpu
*mcore_cpu
= MCORE_SIM_CPU (cpu
);
133 mcore_cpu
->active_gregs
= mcore_cpu
->regs
.alt_gregs
;
135 mcore_cpu
->active_gregs
= mcore_cpu
->regs
.gregs
;
138 #define TRAPCODE 1 /* r1 holds which function we want */
139 #define PARM1 2 /* first parameter */
143 #define RET1 2 /* register for return values. */
145 /* Default to a 8 Mbyte (== 2^23) memory space. */
146 #define DEFAULT_MEMORY_SIZE 0x800000
149 set_initial_gprs (SIM_CPU
*cpu
)
151 struct mcore_sim_cpu
*mcore_cpu
= MCORE_SIM_CPU (cpu
);
153 /* Set up machine just out of reset. */
157 /* Clean out the GPRs and alternate GPRs. */
158 memset (&mcore_cpu
->regs
.gregs
, 0, sizeof(mcore_cpu
->regs
.gregs
));
159 memset (&mcore_cpu
->regs
.alt_gregs
, 0, sizeof(mcore_cpu
->regs
.alt_gregs
));
161 /* Make our register set point to the right place. */
162 set_active_regs (cpu
);
164 /* ABI specifies initial values for these registers. */
165 gr
[0] = DEFAULT_MEMORY_SIZE
- 4;
167 /* dac fix, the stack address must be 8-byte aligned! */
168 gr
[0] = gr
[0] - gr
[0] % 8;
175 /* Simulate a monitor trap. */
178 handle_trap1 (SIM_DESC sd
, SIM_CPU
*cpu
)
180 /* XXX: We don't pass back the actual errno value. */
181 gr
[RET1
] = sim_syscall (cpu
, gr
[TRAPCODE
], gr
[PARM1
], gr
[PARM2
], gr
[PARM3
],
186 process_stub (SIM_DESC sd
, SIM_CPU
*cpu
, int what
)
188 /* These values should match those in libgloss/mcore/syscalls.s. */
195 case 10: /* _unlink */
196 case 19: /* _lseek */
197 case 43: /* _times */
199 handle_trap1 (sd
, cpu
);
203 if (STATE_VERBOSE_P (sd
))
204 fprintf (stderr
, "Unhandled stub opcode: %d\n", what
);
210 util (SIM_DESC sd
, SIM_CPU
*cpu
, unsigned what
)
212 struct mcore_sim_cpu
*mcore_cpu
= MCORE_SIM_CPU (cpu
);
217 sim_engine_halt (sd
, cpu
, NULL
, mcore_cpu
->regs
.pc
, sim_exited
, gr
[PARM1
]);
221 if (STATE_VERBOSE_P (sd
))
222 fprintf (stderr
, "WARNING: printf unimplemented\n");
226 if (STATE_VERBOSE_P (sd
))
227 fprintf (stderr
, "WARNING: scanf unimplemented\n");
231 gr
[RET1
] = mcore_cpu
->insts
;
235 process_stub (sd
, cpu
, gr
[1]);
239 if (STATE_VERBOSE_P (sd
))
240 fprintf (stderr
, "Unhandled util code: %x\n", what
);
245 /* For figuring out whether we carried; addc/subc use this. */
247 iu_carry (unsigned long a
, unsigned long b
, int cin
)
251 x
= (a
& 0xffff) + (b
& 0xffff) + cin
;
252 x
= (x
>> 16) + (a
>> 16) + (b
>> 16);
258 /* TODO: Convert to common watchpoints. */
259 #undef WATCHFUNCTIONS
260 #ifdef WATCHFUNCTIONS
277 #define RD (inst & 0xF)
278 #define RS ((inst >> 4) & 0xF)
279 #define RX ((inst >> 8) & 0xF)
280 #define IMM5 ((inst >> 4) & 0x1F)
281 #define IMM4 ((inst) & 0xF)
283 #define rbat(X) sim_core_read_1 (cpu, 0, read_map, X)
284 #define rhat(X) sim_core_read_2 (cpu, 0, read_map, X)
285 #define rlat(X) sim_core_read_4 (cpu, 0, read_map, X)
286 #define wbat(X, D) sim_core_write_1 (cpu, 0, write_map, X, D)
287 #define what(X, D) sim_core_write_2 (cpu, 0, write_map, X, D)
288 #define wlat(X, D) sim_core_write_4 (cpu, 0, write_map, X, D)
290 static int tracing
= 0;
293 sim_engine_halt (sd, cpu, NULL, pc, sim_stopped, SIM_SIGILL)
296 step_once (SIM_DESC sd
, SIM_CPU
*cpu
)
298 struct mcore_sim_cpu
*mcore_cpu
= MCORE_SIM_CPU (cpu
);
306 #ifdef WATCHFUNCTIONS
311 pc
= CPU_PC_GET (cpu
);
313 /* Fetch the initial instructions that we'll decode. */
314 ibuf
= rlat (pc
& 0xFFFFFFFC);
321 /* make our register set point to the right place */
322 set_active_regs (cpu
);
324 #ifdef WATCHFUNCTIONS
325 /* make a hash to speed exec loop, hope it's nonzero */
328 for (w
= 1; w
<= ENDWL
; w
++)
329 WLhash
= WLhash
& WL
[w
];
332 /* TODO: Unindent this block. */
338 if (! target_big_endian
)
341 inst
= ibuf
& 0xFFFF;
346 if (! target_big_endian
)
347 inst
= ibuf
& 0xFFFF;
352 #ifdef WATCHFUNCTIONS
353 /* now scan list of watch addresses, if match, count it and
354 note return address and count cycles until pc=return address */
356 if ((WLincyc
== 1) && (pc
== WLendpc
))
358 int cycs
= (mcore_cpu
->cycles
+ (insts
+ bonus_cycles
+
359 (memops
* memcycles
)) - WLbcyc
);
361 if (WLcnts
[WLW
] == 1)
368 if (cycs
> WLmax
[WLW
])
373 if (cycs
< WLmin
[WLW
])
383 /* Optimize with a hash to speed loop. */
386 if ((WLhash
== 0) || ((WLhash
& pc
) != 0))
388 for (w
=1; w
<= ENDWL
; w
++)
393 WLbcyc
= mcore_cpu
->cycles
+ insts
394 + bonus_cycles
+ (memops
* memcycles
);
406 fprintf (stderr
, "%.4x: inst = %.4x ", pc
, inst
);
420 sim_engine_halt (sd
, cpu
, NULL
, pc
- 2,
421 sim_stopped
, SIM_SIGTRAP
);
432 set_active_regs (cpu
);
440 set_active_regs (cpu
);
444 if (STATE_VERBOSE_P (sd
))
445 fprintf (stderr
, "WARNING: stop unimplemented\n");
449 if (STATE_VERBOSE_P (sd
))
450 fprintf (stderr
, "WARNING: wait unimplemented\n");
454 if (STATE_VERBOSE_P (sd
))
455 fprintf (stderr
, "WARNING: doze unimplemented\n");
459 ILLEGAL (); /* illegal */
462 case 0x8: /* trap 0 */
463 case 0xA: /* trap 2 */
464 case 0xB: /* trap 3 */
465 sim_engine_halt (sd
, cpu
, NULL
, pc
,
466 sim_stopped
, SIM_SIGTRAP
);
469 case 0xC: /* trap 4 */
470 case 0xD: /* trap 5 */
471 case 0xE: /* trap 6 */
472 ILLEGAL (); /* illegal */
475 case 0xF: /* trap 7 */
476 sim_engine_halt (sd
, cpu
, NULL
, pc
, /* integer div-by-0 */
477 sim_stopped
, SIM_SIGTRAP
);
480 case 0x9: /* trap 1 */
481 handle_trap1 (sd
, cpu
);
487 ILLEGAL (); /* illegal */
498 int32_t addr
= gr
[RD
];
499 int regno
= 4; /* always r4-r7 */
505 gr
[regno
] = rlat (addr
);
509 while ((regno
&0x3) != 0);
514 int32_t addr
= gr
[RD
];
515 int regno
= 4; /* always r4-r7 */
521 wlat (addr
, gr
[regno
]);
525 while ((regno
& 0x3) != 0);
530 int32_t addr
= gr
[0];
533 /* bonus cycle is really only needed if
534 the next insn shifts the last reg loaded.
541 gr
[regno
] = rlat (addr
);
549 int32_t addr
= gr
[0];
552 /* this should be removed! */
553 /* bonus_cycles ++; */
555 memops
+= 16 - regno
;
558 wlat (addr
, gr
[regno
]);
579 if (tracing
&& RD
== 15)
580 fprintf (stderr
, "Func return, r2 = %xx, r3 = %x\n",
595 for (i
= 0; !(tmp
& 0x80000000) && i
< 32; i
++)
604 tmp
= ((tmp
& 0xaaaaaaaa) >> 1) | ((tmp
& 0x55555555) << 1);
605 tmp
= ((tmp
& 0xcccccccc) >> 2) | ((tmp
& 0x33333333) << 2);
606 tmp
= ((tmp
& 0xf0f0f0f0) >> 4) | ((tmp
& 0x0f0f0f0f) << 4);
607 tmp
= ((tmp
& 0xff00ff00) >> 8) | ((tmp
& 0x00ff00ff) << 8);
608 gr
[RD
] = ((tmp
& 0xffff0000) >> 16) | ((tmp
& 0x0000ffff) << 16);
616 case 0x0: /* xtrb3 */
617 gr
[1] = (gr
[RD
]) & 0xFF;
620 case 0x1: /* xtrb2 */
621 gr
[1] = (gr
[RD
]>>8) & 0xFF;
624 case 0x2: /* xtrb1 */
625 gr
[1] = (gr
[RD
]>>16) & 0xFF;
628 case 0x3: /* xtrb0 */
629 gr
[1] = (gr
[RD
]>>24) & 0xFF;
632 case 0x4: /* zextb */
633 gr
[RD
] &= 0x000000FF;
635 case 0x5: /* sextb */
639 tmp
<<= (sizeof (tmp
) * 8) - 8;
640 tmp
>>= (sizeof (tmp
) * 8) - 8;
644 case 0x6: /* zexth */
645 gr
[RD
] &= 0x0000FFFF;
647 case 0x7: /* sexth */
651 tmp
<<= (sizeof (tmp
) * 8) - 16;
652 tmp
>>= (sizeof (tmp
) * 8) - 16;
656 case 0x8: /* declt */
658 NEW_C ((long)gr
[RD
] < 0);
660 case 0x9: /* tstnbz */
662 int32_t tmp
= gr
[RD
];
663 NEW_C ((tmp
& 0xFF000000) != 0 &&
664 (tmp
& 0x00FF0000) != 0 && (tmp
& 0x0000FF00) != 0 &&
665 (tmp
& 0x000000FF) != 0);
668 case 0xA: /* decgt */
670 NEW_C ((long)gr
[RD
] > 0);
672 case 0xB: /* decne */
674 NEW_C ((long)gr
[RD
] != 0);
685 if (gr
[RD
] & 0x80000000)
686 gr
[RD
] = ~gr
[RD
] + 1;
693 case 0x02: /* movt */
697 case 0x03: /* mult */
698 /* consume 2 bits per cycle from rs, until rs is 0 */
700 unsigned int t
= gr
[RS
];
702 for (ticks
= 0; t
!= 0 ; t
>>= 2)
704 bonus_cycles
+= ticks
;
706 bonus_cycles
+= 2; /* min. is 3, so add 2, plus ticks above */
708 fprintf (stderr
, " mult %x by %x to give %x",
709 gr
[RD
], gr
[RS
], gr
[RD
] * gr
[RS
]);
710 gr
[RD
] = gr
[RD
] * gr
[RS
];
712 case 0x04: /* loopt */
715 pc
+= (IMM4
<< 1) - 32;
719 --gr
[RS
]; /* not RD! */
720 NEW_C (((long)gr
[RS
]) > 0);
722 case 0x05: /* subu */
725 case 0x06: /* addc */
727 unsigned long tmp
, a
, b
;
730 gr
[RD
] = a
+ b
+ C_VALUE ();
731 tmp
= iu_carry (a
, b
, C_VALUE ());
735 case 0x07: /* subc */
737 unsigned long tmp
, a
, b
;
740 gr
[RD
] = a
- b
+ C_VALUE () - 1;
741 tmp
= iu_carry (a
,~b
, C_VALUE ());
745 case 0x08: /* illegal */
746 case 0x09: /* illegal*/
749 case 0x0A: /* movf */
758 /* We must not rely solely upon the native shift operations, since they
759 may not match the M*Core's behaviour on boundary conditions. */
760 dst
= src
> 31 ? 0 : dst
>> src
;
764 case 0x0C: /* cmphs */
765 NEW_C ((unsigned long )gr
[RD
] >=
766 (unsigned long)gr
[RS
]);
768 case 0x0D: /* cmplt */
769 NEW_C ((long)gr
[RD
] < (long)gr
[RS
]);
772 NEW_C ((gr
[RD
] & gr
[RS
]) != 0);
774 case 0x0F: /* cmpne */
775 NEW_C (gr
[RD
] != gr
[RS
]);
777 case 0x10: case 0x11: /* mfcr */
781 if (r
<= LAST_VALID_CREG
)
791 fprintf (stderr
, "MOV %x into reg %d", gr
[RD
], RD
);
794 case 0x13: /* bgenr */
798 gr
[RD
] = 1 << (gr
[RS
] & 0x1F);
801 case 0x14: /* rsub */
802 gr
[RD
] = gr
[RS
] - gr
[RD
];
817 case 0x18: case 0x19: /* mtcr */
821 if (r
<= LAST_VALID_CREG
)
826 /* we might have changed register sets... */
827 set_active_regs (cpu
);
832 /* We must not rely solely upon the native shift operations, since they
833 may not match the M*Core's behaviour on boundary conditions. */
835 gr
[RD
] = ((long) gr
[RD
]) < 0 ? -1 : 0;
837 gr
[RD
] = (long) gr
[RD
] >> gr
[RS
];
841 /* We must not rely solely upon the native shift operations, since they
842 may not match the M*Core's behaviour on boundary conditions. */
843 gr
[RD
] = gr
[RS
] > 31 ? 0 : gr
[RD
] << gr
[RS
];
846 case 0x1C: /* addu */
851 gr
[RD
] += gr
[RS
] << 1;
858 case 0x1F: /* andn */
861 case 0x20: case 0x21: /* addi */
865 case 0x22: case 0x23: /* cmplti */
867 int tmp
= (IMM5
+ 1);
878 case 0x24: case 0x25: /* subi */
882 case 0x26: case 0x27: /* illegal */
885 case 0x28: case 0x29: /* rsubi */
889 case 0x2A: case 0x2B: /* cmpnei */
900 case 0x2C: case 0x2D: /* bmaski, divu */
914 /* unsigned divide */
915 gr
[RD
] = (int32_t) ((unsigned int) gr
[RD
] / (unsigned int)gr
[1] );
917 /* compute bonus_cycles for divu */
918 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32); r1nlz
++)
921 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32); rxnlz
++)
927 exe
+= 5 + r1nlz
- rxnlz
;
929 if (exe
>= (2 * memcycles
- 1))
931 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
934 else if (imm
== 0 || imm
>= 8)
940 gr
[RD
] = (1 << imm
) - 1;
949 case 0x2E: case 0x2F: /* andi */
950 gr
[RD
] = gr
[RD
] & IMM5
;
952 case 0x30: case 0x31: /* bclri */
953 gr
[RD
] = gr
[RD
] & ~(1<<IMM5
);
955 case 0x32: case 0x33: /* bgeni, divs */
964 /* compute bonus_cycles for divu */
969 if (((rx
< 0) && (r1
> 0)) || ((rx
>= 0) && (r1
< 0)))
977 /* signed divide, general registers are of type int, so / op is OK */
978 gr
[RD
] = gr
[RD
] / gr
[1];
980 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32) ; r1nlz
++ )
983 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32) ; rxnlz
++ )
989 exe
+= 6 + r1nlz
- rxnlz
+ sc
;
991 if (exe
>= (2 * memcycles
- 1))
993 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
999 gr
[RD
] = (1 << IMM5
);
1008 case 0x34: case 0x35: /* bseti */
1009 gr
[RD
] = gr
[RD
] | (1 << IMM5
);
1011 case 0x36: case 0x37: /* btsti */
1012 NEW_C (gr
[RD
] >> IMM5
);
1014 case 0x38: case 0x39: /* xsr, rotli */
1016 unsigned imm
= IMM5
;
1017 uint32_t tmp
= gr
[RD
];
1023 gr
[RD
] = (cbit
<< 31) | (tmp
>> 1);
1026 gr
[RD
] = (tmp
<< imm
) | (tmp
>> (32 - imm
));
1029 case 0x3A: case 0x3B: /* asrc, asri */
1031 unsigned imm
= IMM5
;
1039 gr
[RD
] = tmp
>> imm
;
1042 case 0x3C: case 0x3D: /* lslc, lsli */
1044 unsigned imm
= IMM5
;
1045 unsigned long tmp
= gr
[RD
];
1052 gr
[RD
] = tmp
<< imm
;
1055 case 0x3E: case 0x3F: /* lsrc, lsri */
1057 unsigned imm
= IMM5
;
1058 uint32_t tmp
= gr
[RD
];
1065 gr
[RD
] = tmp
>> imm
;
1068 case 0x40: case 0x41: case 0x42: case 0x43:
1069 case 0x44: case 0x45: case 0x46: case 0x47:
1070 case 0x48: case 0x49: case 0x4A: case 0x4B:
1071 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1075 util (sd
, cpu
, inst
& 0xFF);
1077 case 0x51: case 0x52: case 0x53:
1078 case 0x54: case 0x55: case 0x56: case 0x57:
1079 case 0x58: case 0x59: case 0x5A: case 0x5B:
1080 case 0x5C: case 0x5D: case 0x5E: case 0x5F:
1083 case 0x60: case 0x61: case 0x62: case 0x63: /* movi */
1084 case 0x64: case 0x65: case 0x66: case 0x67:
1085 gr
[RD
] = (inst
>> 4) & 0x7F;
1087 case 0x68: case 0x69: case 0x6A: case 0x6B:
1088 case 0x6C: case 0x6D: case 0x6E: case 0x6F: /* illegal */
1091 case 0x71: case 0x72: case 0x73:
1092 case 0x74: case 0x75: case 0x76: case 0x77:
1093 case 0x78: case 0x79: case 0x7A: case 0x7B:
1094 case 0x7C: case 0x7D: case 0x7E: /* lrw */
1095 gr
[RX
] = rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1097 fprintf (stderr
, "LRW of 0x%x from 0x%x to reg %d",
1098 rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC),
1099 (pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC, RX
);
1102 case 0x7F: /* jsri */
1106 "func call: r2 = %x r3 = %x r4 = %x r5 = %x r6 = %x r7 = %x\n",
1107 gr
[2], gr
[3], gr
[4], gr
[5], gr
[6], gr
[7]);
1108 ATTRIBUTE_FALLTHROUGH
;
1109 case 0x70: /* jmpi */
1110 pc
= rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1116 case 0x80: case 0x81: case 0x82: case 0x83:
1117 case 0x84: case 0x85: case 0x86: case 0x87:
1118 case 0x88: case 0x89: case 0x8A: case 0x8B:
1119 case 0x8C: case 0x8D: case 0x8E: case 0x8F: /* ld */
1120 gr
[RX
] = rlat (gr
[RD
] + ((inst
>> 2) & 0x003C));
1122 fprintf (stderr
, "load reg %d from 0x%x with 0x%x",
1124 gr
[RD
] + ((inst
>> 2) & 0x003C), gr
[RX
]);
1127 case 0x90: case 0x91: case 0x92: case 0x93:
1128 case 0x94: case 0x95: case 0x96: case 0x97:
1129 case 0x98: case 0x99: case 0x9A: case 0x9B:
1130 case 0x9C: case 0x9D: case 0x9E: case 0x9F: /* st */
1131 wlat (gr
[RD
] + ((inst
>> 2) & 0x003C), gr
[RX
]);
1133 fprintf (stderr
, "store reg %d (containing 0x%x) to 0x%x",
1135 gr
[RD
] + ((inst
>> 2) & 0x003C));
1138 case 0xA0: case 0xA1: case 0xA2: case 0xA3:
1139 case 0xA4: case 0xA5: case 0xA6: case 0xA7:
1140 case 0xA8: case 0xA9: case 0xAA: case 0xAB:
1141 case 0xAC: case 0xAD: case 0xAE: case 0xAF: /* ld.b */
1142 gr
[RX
] = rbat (gr
[RD
] + RS
);
1145 case 0xB0: case 0xB1: case 0xB2: case 0xB3:
1146 case 0xB4: case 0xB5: case 0xB6: case 0xB7:
1147 case 0xB8: case 0xB9: case 0xBA: case 0xBB:
1148 case 0xBC: case 0xBD: case 0xBE: case 0xBF: /* st.b */
1149 wbat (gr
[RD
] + RS
, gr
[RX
]);
1152 case 0xC0: case 0xC1: case 0xC2: case 0xC3:
1153 case 0xC4: case 0xC5: case 0xC6: case 0xC7:
1154 case 0xC8: case 0xC9: case 0xCA: case 0xCB:
1155 case 0xCC: case 0xCD: case 0xCE: case 0xCF: /* ld.h */
1156 gr
[RX
] = rhat (gr
[RD
] + ((inst
>> 3) & 0x001E));
1159 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
1160 case 0xD4: case 0xD5: case 0xD6: case 0xD7:
1161 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1162 case 0xDC: case 0xDD: case 0xDE: case 0xDF: /* st.h */
1163 what (gr
[RD
] + ((inst
>> 3) & 0x001E), gr
[RX
]);
1166 case 0xE8: case 0xE9: case 0xEA: case 0xEB:
1167 case 0xEC: case 0xED: case 0xEE: case 0xEF: /* bf */
1171 disp
= inst
& 0x03FF;
1179 case 0xE0: case 0xE1: case 0xE2: case 0xE3:
1180 case 0xE4: case 0xE5: case 0xE6: case 0xE7: /* bt */
1184 disp
= inst
& 0x03FF;
1193 case 0xF8: case 0xF9: case 0xFA: case 0xFB:
1194 case 0xFC: case 0xFD: case 0xFE: case 0xFF: /* bsr */
1196 ATTRIBUTE_FALLTHROUGH
;
1197 case 0xF0: case 0xF1: case 0xF2: case 0xF3:
1198 case 0xF4: case 0xF5: case 0xF6: case 0xF7: /* br */
1201 disp
= inst
& 0x03FF;
1213 fprintf (stderr
, "\n");
1217 ibuf
= rlat (pc
& 0xFFFFFFFC);
1222 /* Hide away the things we've cached while executing. */
1223 CPU_PC_SET (cpu
, pc
);
1224 mcore_cpu
->insts
+= insts
; /* instructions done ... */
1225 mcore_cpu
->cycles
+= insts
; /* and each takes a cycle */
1226 mcore_cpu
->cycles
+= bonus_cycles
; /* and extra cycles for branches */
1227 mcore_cpu
->cycles
+= memops
* memcycles
; /* and memop cycle delays */
1231 sim_engine_run (SIM_DESC sd
,
1232 int next_cpu_nr
, /* ignore */
1233 int nr_cpus
, /* ignore */
1234 int siggnal
) /* ignore */
1238 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1240 cpu
= STATE_CPU (sd
, 0);
1244 step_once (sd
, cpu
);
1245 if (sim_events_tick (sd
))
1246 sim_events_process (sd
);
1251 mcore_reg_store (SIM_CPU
*cpu
, int rn
, const void *memory
, int length
)
1253 struct mcore_sim_cpu
*mcore_cpu
= MCORE_SIM_CPU (cpu
);
1255 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1261 /* misalignment safe */
1262 ival
= mcore_extract_unsigned_integer (memory
, 4);
1263 mcore_cpu
->asints
[rn
] = ival
;
1273 mcore_reg_fetch (SIM_CPU
*cpu
, int rn
, void *memory
, int length
)
1275 struct mcore_sim_cpu
*mcore_cpu
= MCORE_SIM_CPU (cpu
);
1277 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1281 long ival
= mcore_cpu
->asints
[rn
];
1283 /* misalignment-safe */
1284 mcore_store_unsigned_integer (memory
, 4, ival
);
1294 sim_info (SIM_DESC sd
, bool verbose
)
1296 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1297 struct mcore_sim_cpu
*mcore_cpu
= MCORE_SIM_CPU (cpu
);
1298 #ifdef WATCHFUNCTIONS
1301 double virttime
= mcore_cpu
->cycles
/ 36.0e6
;
1302 host_callback
*callback
= STATE_CALLBACK (sd
);
1304 callback
->printf_filtered (callback
, "\n\n# instructions executed %10d\n",
1306 callback
->printf_filtered (callback
, "# cycles %10d\n",
1308 callback
->printf_filtered (callback
, "# pipeline stalls %10d\n",
1310 callback
->printf_filtered (callback
, "# virtual time taken %10.4f\n",
1313 #ifdef WATCHFUNCTIONS
1314 callback
->printf_filtered (callback
, "\nNumber of watched functions: %d\n",
1319 for (w
= 1; w
<= ENDWL
; w
++)
1321 callback
->printf_filtered (callback
, "WL = %s %8x\n",WLstr
[w
],WL
[w
]);
1322 callback
->printf_filtered (callback
, " calls = %d, cycles = %d\n",
1323 WLcnts
[w
],WLcyc
[w
]);
1326 callback
->printf_filtered (callback
,
1327 " maxcpc = %d, mincpc = %d, avecpc = %d\n",
1328 WLmax
[w
],WLmin
[w
],WLcyc
[w
]/WLcnts
[w
]);
1332 callback
->printf_filtered (callback
,
1333 "Total cycles for watched functions: %d\n",wcyc
);
1338 mcore_pc_get (sim_cpu
*cpu
)
1340 return MCORE_SIM_CPU (cpu
)->regs
.pc
;
1344 mcore_pc_set (sim_cpu
*cpu
, sim_cia pc
)
1346 MCORE_SIM_CPU (cpu
)->regs
.pc
= pc
;
1350 free_state (SIM_DESC sd
)
1352 if (STATE_MODULES (sd
) != NULL
)
1353 sim_module_uninstall (sd
);
1354 sim_cpu_free_all (sd
);
1355 sim_state_free (sd
);
1359 sim_open (SIM_OPEN_KIND kind
, host_callback
*cb
,
1360 struct bfd
*abfd
, char * const *argv
)
1363 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
1364 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1366 /* Set default options before parsing user options. */
1367 cb
->syscall_map
= cb_mcore_syscall_map
;
1369 /* The cpu data is kept in a separately allocated chunk of memory. */
1370 if (sim_cpu_alloc_all_extra (sd
, 0, sizeof (struct mcore_sim_cpu
))
1377 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
1383 /* The parser will print an error message for us, so we silently return. */
1384 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
1390 /* Check for/establish the a reference program image. */
1391 if (sim_analyze_program (sd
, STATE_PROG_FILE (sd
), abfd
) != SIM_RC_OK
)
1397 /* Configure/verify the target byte order and other runtime
1398 configuration options. */
1399 if (sim_config (sd
) != SIM_RC_OK
)
1401 sim_module_uninstall (sd
);
1405 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
1407 /* Uninstall the modules to avoid memory leaks,
1408 file descriptor leaks, etc. */
1409 sim_module_uninstall (sd
);
1413 /* CPU specific initialization. */
1414 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1416 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
1418 CPU_REG_FETCH (cpu
) = mcore_reg_fetch
;
1419 CPU_REG_STORE (cpu
) = mcore_reg_store
;
1420 CPU_PC_FETCH (cpu
) = mcore_pc_get
;
1421 CPU_PC_STORE (cpu
) = mcore_pc_set
;
1423 set_initial_gprs (cpu
); /* Reset the GPR registers. */
1426 /* Default to a 8 Mbyte (== 2^23) memory space. */
1427 sim_do_commandf (sd
, "memory-size %#x", DEFAULT_MEMORY_SIZE
);
1433 sim_create_inferior (SIM_DESC sd
, struct bfd
*prog_bfd
,
1434 char * const *argv
, char * const *env
)
1436 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
1442 unsigned long strings
;
1443 unsigned long pointers
;
1444 unsigned long hi_stack
;
1447 /* Set the initial register set. */
1448 set_initial_gprs (cpu
);
1450 hi_stack
= DEFAULT_MEMORY_SIZE
- 4;
1451 CPU_PC_SET (cpu
, bfd_get_start_address (prog_bfd
));
1453 /* Calculate the argument and environment strings. */
1459 l
= strlen (*avp
) + 1; /* include the null */
1460 s_length
+= (l
+ 3) & ~3; /* make it a 4 byte boundary */
1468 l
= strlen (*avp
) + 1; /* include the null */
1469 s_length
+= (l
+ 3) & ~ 3;/* make it a 4 byte boundary */
1473 /* Claim some memory for the pointers and strings. */
1474 pointers
= hi_stack
- sizeof(int32_t) * (nenv
+1+nargs
+1);
1475 pointers
&= ~3; /* must be 4-byte aligned */
1478 strings
= gr
[0] - s_length
;
1479 strings
&= ~3; /* want to make it 4-byte aligned */
1481 /* dac fix, the stack address must be 8-byte aligned! */
1482 gr
[0] = gr
[0] - gr
[0] % 8;
1484 /* Loop through the arguments and fill them in. */
1488 /* No strings to fill in. */
1493 gr
[PARM2
] = pointers
;
1497 /* Save where we're putting it. */
1498 wlat (pointers
, strings
);
1500 /* Copy the string. */
1501 l
= strlen (* avp
) + 1;
1502 sim_core_write_buffer (sd
, cpu
, write_map
, *avp
, strings
, l
);
1504 /* Bump the pointers. */
1510 /* A null to finish the list. */
1515 /* Now do the environment pointers. */
1518 /* No strings to fill in. */
1523 gr
[PARM3
] = pointers
;
1528 /* Save where we're putting it. */
1529 wlat (pointers
, strings
);
1531 /* Copy the string. */
1532 l
= strlen (* avp
) + 1;
1533 sim_core_write_buffer (sd
, cpu
, write_map
, *avp
, strings
, l
);
1535 /* Bump the pointers. */
1541 /* A null to finish the list. */