e77535b8ae5d77d93f0f36a068c7f5a6eed9d481
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
;)
96 This state is maintained in host byte order. The
97 fetch/store register functions must translate between host
98 byte order and the target processor byte order.
99 Keeping this data in target byte order simplifies the register
100 read/write functions. Keeping this data in native order improves
101 the performance of the simulator. Simulation speed is deemed more
103 /* TODO: Should be moved to sim-main.h:sim_cpu. */
105 /* The ordering of the mcore_regset structure is matched in the
106 gdb/config/mcore/tm-mcore.h file in the REGISTER_NAMES macro. */
109 word gregs
[16]; /* primary registers */
110 word alt_gregs
[16]; /* alt register file */
111 word cregs
[32]; /* control registers */
122 struct mcore_regset asregs
;
123 word asints
[1]; /* but accessed larger... */
126 #define LAST_VALID_CREG 32 /* only 0..12 implemented */
127 #define NUM_MCORE_REGS (16 + 16 + LAST_VALID_CREG + 1)
129 static int memcycles
= 1;
131 #define gr asregs.active_gregs
132 #define cr asregs.cregs
133 #define sr asregs.cregs[0]
134 #define vbr asregs.cregs[1]
135 #define esr asregs.cregs[2]
136 #define fsr asregs.cregs[3]
137 #define epc asregs.cregs[4]
138 #define fpc asregs.cregs[5]
139 #define ss0 asregs.cregs[6]
140 #define ss1 asregs.cregs[7]
141 #define ss2 asregs.cregs[8]
142 #define ss3 asregs.cregs[9]
143 #define ss4 asregs.cregs[10]
144 #define gcr asregs.cregs[11]
145 #define gsr asregs.cregs[12]
147 /* maniuplate the carry bit */
148 #define C_ON() (cpu.sr & 1)
149 #define C_VALUE() (cpu.sr & 1)
150 #define C_OFF() ((cpu.sr & 1) == 0)
151 #define SET_C() {cpu.sr |= 1;}
152 #define CLR_C() {cpu.sr &= 0xfffffffe;}
153 #define NEW_C(v) {CLR_C(); cpu.sr |= ((v) & 1);}
155 #define SR_AF() ((cpu.sr >> 1) & 1)
157 #define TRAPCODE 1 /* r1 holds which function we want */
158 #define PARM1 2 /* first parameter */
162 #define RET1 2 /* register for return values. */
164 /* Default to a 8 Mbyte (== 2^23) memory space. */
165 #define DEFAULT_MEMORY_SIZE 0x800000
168 set_initial_gprs (SIM_CPU
*scpu
)
173 /* Set up machine just out of reset. */
174 CPU_PC_SET (scpu
, 0);
177 /* Clean out the GPRs and alternate GPRs. */
178 for (i
= 0; i
< 16; i
++)
180 cpu
.asregs
.gregs
[i
] = 0;
181 cpu
.asregs
.alt_gregs
[i
] = 0;
184 /* Make our register set point to the right place. */
186 cpu
.asregs
.active_gregs
= &cpu
.asregs
.alt_gregs
[0];
188 cpu
.asregs
.active_gregs
= &cpu
.asregs
.gregs
[0];
190 /* ABI specifies initial values for these registers. */
191 cpu
.gr
[0] = DEFAULT_MEMORY_SIZE
- 4;
193 /* dac fix, the stack address must be 8-byte aligned! */
194 cpu
.gr
[0] = cpu
.gr
[0] - cpu
.gr
[0] % 8;
198 cpu
.gr
[PARM4
] = cpu
.gr
[0];
201 /* Simulate a monitor trap. */
204 handle_trap1 (SIM_DESC sd
)
206 host_callback
*cb
= STATE_CALLBACK (sd
);
209 CB_SYSCALL_INIT (&sc
);
211 sc
.func
= cpu
.gr
[TRAPCODE
];
212 sc
.arg1
= cpu
.gr
[PARM1
];
213 sc
.arg2
= cpu
.gr
[PARM2
];
214 sc
.arg3
= cpu
.gr
[PARM3
];
215 sc
.arg4
= cpu
.gr
[PARM4
];
218 sc
.p2
= (PTR
) STATE_CPU (sd
, 0);
219 sc
.read_mem
= sim_syscall_read_mem
;
220 sc
.write_mem
= sim_syscall_write_mem
;
222 cb_syscall (cb
, &sc
);
224 /* XXX: We don't pass back the actual errno value. */
225 cpu
.gr
[RET1
] = sc
.result
;
229 process_stub (SIM_DESC sd
, int what
)
231 /* These values should match those in libgloss/mcore/syscalls.s. */
238 case 10: /* _unlink */
239 case 19: /* _lseek */
240 case 43: /* _times */
241 cpu
.gr
[TRAPCODE
] = what
;
246 if (STATE_VERBOSE_P (sd
))
247 fprintf (stderr
, "Unhandled stub opcode: %d\n", what
);
253 util (SIM_DESC sd
, unsigned what
)
258 cpu
.asregs
.exception
= SIGQUIT
;
262 if (STATE_VERBOSE_P (sd
))
263 fprintf (stderr
, "WARNING: printf unimplemented\n");
267 if (STATE_VERBOSE_P (sd
))
268 fprintf (stderr
, "WARNING: scanf unimplemented\n");
272 cpu
.gr
[RET1
] = cpu
.asregs
.insts
;
276 process_stub (sd
, cpu
.gr
[1]);
280 if (STATE_VERBOSE_P (sd
))
281 fprintf (stderr
, "Unhandled util code: %x\n", what
);
286 /* For figuring out whether we carried; addc/subc use this. */
288 iu_carry (unsigned long a
, unsigned long b
, int cin
)
292 x
= (a
& 0xffff) + (b
& 0xffff) + cin
;
293 x
= (x
>> 16) + (a
>> 16) + (b
>> 16);
299 /* TODO: Convert to common watchpoints. */
300 #undef WATCHFUNCTIONS
301 #ifdef WATCHFUNCTIONS
318 #define RD (inst & 0xF)
319 #define RS ((inst >> 4) & 0xF)
320 #define RX ((inst >> 8) & 0xF)
321 #define IMM5 ((inst >> 4) & 0x1F)
322 #define IMM4 ((inst) & 0xF)
324 #define rbat(X) sim_core_read_1 (scpu, 0, read_map, X)
325 #define rhat(X) sim_core_read_2 (scpu, 0, read_map, X)
326 #define rlat(X) sim_core_read_4 (scpu, 0, read_map, X)
327 #define wbat(X, D) sim_core_write_1 (scpu, 0, write_map, X, D)
328 #define what(X, D) sim_core_write_2 (scpu, 0, write_map, X, D)
329 #define wlat(X, D) sim_core_write_4 (scpu, 0, write_map, X, D)
331 static int tracing
= 0;
334 sim_resume (SIM_DESC sd
, int step
, int siggnal
)
336 SIM_CPU
*scpu
= STATE_CPU (sd
, 0);
346 #ifdef WATCHFUNCTIONS
350 cpu
.asregs
.exception
= step
? SIGTRAP
: 0;
351 pc
= CPU_PC_GET (scpu
);
353 /* Fetch the initial instructions that we'll decode. */
354 ibuf
= rlat (pc
& 0xFFFFFFFC);
361 /* make our register set point to the right place */
363 cpu
.asregs
.active_gregs
= & cpu
.asregs
.alt_gregs
[0];
365 cpu
.asregs
.active_gregs
= & cpu
.asregs
.gregs
[0];
367 #ifdef WATCHFUNCTIONS
368 /* make a hash to speed exec loop, hope it's nonzero */
371 for (w
= 1; w
<= ENDWL
; w
++)
372 WLhash
= WLhash
& WL
[w
];
383 if (! target_big_endian
)
386 inst
= ibuf
& 0xFFFF;
391 if (! target_big_endian
)
392 inst
= ibuf
& 0xFFFF;
397 #ifdef WATCHFUNCTIONS
398 /* now scan list of watch addresses, if match, count it and
399 note return address and count cycles until pc=return address */
401 if ((WLincyc
== 1) && (pc
== WLendpc
))
403 cycs
= (cpu
.asregs
.cycles
+ (insts
+ bonus_cycles
+
404 (memops
* memcycles
)) - WLbcyc
);
406 if (WLcnts
[WLW
] == 1)
413 if (cycs
> WLmax
[WLW
])
418 if (cycs
< WLmin
[WLW
])
428 /* Optimize with a hash to speed loop. */
431 if ((WLhash
== 0) || ((WLhash
& pc
) != 0))
433 for (w
=1; w
<= ENDWL
; w
++)
438 WLbcyc
= cpu
.asregs
.cycles
+ insts
439 + bonus_cycles
+ (memops
* memcycles
);
440 WLendpc
= cpu
.gr
[15];
451 fprintf (stderr
, "%.4lx: inst = %.4x ", pc
, inst
);
466 cpu
.asregs
.exception
= SIGTRAP
;
479 cpu
.asregs
.active_gregs
= & cpu
.asregs
.alt_gregs
[0];
481 cpu
.asregs
.active_gregs
= & cpu
.asregs
.gregs
[0];
490 cpu
.asregs
.active_gregs
= &cpu
.asregs
.alt_gregs
[0];
492 cpu
.asregs
.active_gregs
= &cpu
.asregs
.gregs
[0];
496 if (STATE_VERBOSE_P (sd
))
497 fprintf (stderr
, "WARNING: stop unimplemented\n");
501 if (STATE_VERBOSE_P (sd
))
502 fprintf (stderr
, "WARNING: wait unimplemented\n");
506 if (STATE_VERBOSE_P (sd
))
507 fprintf (stderr
, "WARNING: doze unimplemented\n");
511 cpu
.asregs
.exception
= SIGILL
; /* illegal */
514 case 0x8: /* trap 0 */
515 case 0xA: /* trap 2 */
516 case 0xB: /* trap 3 */
517 cpu
.asregs
.exception
= SIGTRAP
;
520 case 0xC: /* trap 4 */
521 case 0xD: /* trap 5 */
522 case 0xE: /* trap 6 */
523 cpu
.asregs
.exception
= SIGILL
; /* illegal */
526 case 0xF: /* trap 7 */
527 cpu
.asregs
.exception
= SIGTRAP
; /* integer div-by-0 */
530 case 0x9: /* trap 1 */
537 cpu
.asregs
.exception
= SIGILL
; /* illegal */
541 cpu
.gr
[RD
] = C_VALUE();
544 cpu
.gr
[RD
] = C_OFF();
548 word addr
= cpu
.gr
[RD
];
549 int regno
= 4; /* always r4-r7 */
555 cpu
.gr
[regno
] = rlat(addr
);
559 while ((regno
&0x3) != 0);
564 word addr
= cpu
.gr
[RD
];
565 int regno
= 4; /* always r4-r7 */
571 wlat(addr
, cpu
.gr
[regno
]);
575 while ((regno
& 0x3) != 0);
580 word addr
= cpu
.gr
[0];
583 /* bonus cycle is really only needed if
584 the next insn shifts the last reg loaded.
591 cpu
.gr
[regno
] = rlat(addr
);
599 word addr
= cpu
.gr
[0];
602 /* this should be removed! */
603 /* bonus_cycles ++; */
605 memops
+= 16 - regno
;
608 wlat(addr
, cpu
.gr
[regno
]);
616 cpu
.gr
[RD
] -= C_VALUE();
619 cpu
.gr
[RD
] -= C_OFF();
622 cpu
.gr
[RD
] += C_VALUE();
625 cpu
.gr
[RD
] += C_OFF();
629 if (tracing
&& RD
== 15)
630 fprintf (stderr
, "Func return, r2 = %lxx, r3 = %lx\n",
631 cpu
.gr
[2], cpu
.gr
[3]);
645 for (i
= 0; !(tmp
& 0x80000000) && i
< 32; i
++)
654 tmp
= ((tmp
& 0xaaaaaaaa) >> 1) | ((tmp
& 0x55555555) << 1);
655 tmp
= ((tmp
& 0xcccccccc) >> 2) | ((tmp
& 0x33333333) << 2);
656 tmp
= ((tmp
& 0xf0f0f0f0) >> 4) | ((tmp
& 0x0f0f0f0f) << 4);
657 tmp
= ((tmp
& 0xff00ff00) >> 8) | ((tmp
& 0x00ff00ff) << 8);
658 cpu
.gr
[RD
] = ((tmp
& 0xffff0000) >> 16) | ((tmp
& 0x0000ffff) << 16);
666 case 0x0: /* xtrb3 */
667 cpu
.gr
[1] = (cpu
.gr
[RD
]) & 0xFF;
668 NEW_C (cpu
.gr
[RD
] != 0);
670 case 0x1: /* xtrb2 */
671 cpu
.gr
[1] = (cpu
.gr
[RD
]>>8) & 0xFF;
672 NEW_C (cpu
.gr
[RD
] != 0);
674 case 0x2: /* xtrb1 */
675 cpu
.gr
[1] = (cpu
.gr
[RD
]>>16) & 0xFF;
676 NEW_C (cpu
.gr
[RD
] != 0);
678 case 0x3: /* xtrb0 */
679 cpu
.gr
[1] = (cpu
.gr
[RD
]>>24) & 0xFF;
680 NEW_C (cpu
.gr
[RD
] != 0);
682 case 0x4: /* zextb */
683 cpu
.gr
[RD
] &= 0x000000FF;
685 case 0x5: /* sextb */
694 case 0x6: /* zexth */
695 cpu
.gr
[RD
] &= 0x0000FFFF;
697 case 0x7: /* sexth */
706 case 0x8: /* declt */
708 NEW_C ((long)cpu
.gr
[RD
] < 0);
710 case 0x9: /* tstnbz */
712 word tmp
= cpu
.gr
[RD
];
713 NEW_C ((tmp
& 0xFF000000) != 0 &&
714 (tmp
& 0x00FF0000) != 0 && (tmp
& 0x0000FF00) != 0 &&
715 (tmp
& 0x000000FF) != 0);
718 case 0xA: /* decgt */
720 NEW_C ((long)cpu
.gr
[RD
] > 0);
722 case 0xB: /* decne */
724 NEW_C ((long)cpu
.gr
[RD
] != 0);
735 if (cpu
.gr
[RD
] & 0x80000000)
736 cpu
.gr
[RD
] = ~cpu
.gr
[RD
] + 1;
739 cpu
.gr
[RD
] = ~cpu
.gr
[RD
];
743 case 0x02: /* movt */
745 cpu
.gr
[RD
] = cpu
.gr
[RS
];
747 case 0x03: /* mult */
748 /* consume 2 bits per cycle from rs, until rs is 0 */
750 unsigned int t
= cpu
.gr
[RS
];
752 for (ticks
= 0; t
!= 0 ; t
>>= 2)
754 bonus_cycles
+= ticks
;
756 bonus_cycles
+= 2; /* min. is 3, so add 2, plus ticks above */
758 fprintf (stderr
, " mult %lx by %lx to give %lx",
759 cpu
.gr
[RD
], cpu
.gr
[RS
], cpu
.gr
[RD
] * cpu
.gr
[RS
]);
760 cpu
.gr
[RD
] = cpu
.gr
[RD
] * cpu
.gr
[RS
];
762 case 0x04: /* loopt */
765 pc
+= (IMM4
<< 1) - 32;
769 --cpu
.gr
[RS
]; /* not RD! */
770 NEW_C (((long)cpu
.gr
[RS
]) > 0);
772 case 0x05: /* subu */
773 cpu
.gr
[RD
] -= cpu
.gr
[RS
];
775 case 0x06: /* addc */
777 unsigned long tmp
, a
, b
;
780 cpu
.gr
[RD
] = a
+ b
+ C_VALUE ();
781 tmp
= iu_carry (a
, b
, C_VALUE ());
785 case 0x07: /* subc */
787 unsigned long tmp
, a
, b
;
790 cpu
.gr
[RD
] = a
- b
+ C_VALUE () - 1;
791 tmp
= iu_carry (a
,~b
, C_VALUE ());
795 case 0x08: /* illegal */
796 case 0x09: /* illegal*/
797 cpu
.asregs
.exception
= SIGILL
;
799 case 0x0A: /* movf */
801 cpu
.gr
[RD
] = cpu
.gr
[RS
];
805 unsigned long dst
, src
;
808 /* We must not rely solely upon the native shift operations, since they
809 may not match the M*Core's behaviour on boundary conditions. */
810 dst
= src
> 31 ? 0 : dst
>> src
;
814 case 0x0C: /* cmphs */
815 NEW_C ((unsigned long )cpu
.gr
[RD
] >=
816 (unsigned long)cpu
.gr
[RS
]);
818 case 0x0D: /* cmplt */
819 NEW_C ((long)cpu
.gr
[RD
] < (long)cpu
.gr
[RS
]);
822 NEW_C ((cpu
.gr
[RD
] & cpu
.gr
[RS
]) != 0);
824 case 0x0F: /* cmpne */
825 NEW_C (cpu
.gr
[RD
] != cpu
.gr
[RS
]);
827 case 0x10: case 0x11: /* mfcr */
831 if (r
<= LAST_VALID_CREG
)
832 cpu
.gr
[RD
] = cpu
.cr
[r
];
834 cpu
.asregs
.exception
= SIGILL
;
839 cpu
.gr
[RD
] = cpu
.gr
[RS
];
841 fprintf (stderr
, "MOV %lx into reg %d", cpu
.gr
[RD
], RD
);
844 case 0x13: /* bgenr */
845 if (cpu
.gr
[RS
] & 0x20)
848 cpu
.gr
[RD
] = 1 << (cpu
.gr
[RS
] & 0x1F);
851 case 0x14: /* rsub */
852 cpu
.gr
[RD
] = cpu
.gr
[RS
] - cpu
.gr
[RD
];
856 cpu
.gr
[RD
] += cpu
.gr
[RS
]<<2;
860 cpu
.gr
[RD
] &= cpu
.gr
[RS
];
864 cpu
.gr
[RD
] ^= cpu
.gr
[RS
];
867 case 0x18: case 0x19: /* mtcr */
871 if (r
<= LAST_VALID_CREG
)
872 cpu
.cr
[r
] = cpu
.gr
[RD
];
874 cpu
.asregs
.exception
= SIGILL
;
876 /* we might have changed register sets... */
878 cpu
.asregs
.active_gregs
= & cpu
.asregs
.alt_gregs
[0];
880 cpu
.asregs
.active_gregs
= & cpu
.asregs
.gregs
[0];
885 /* We must not rely solely upon the native shift operations, since they
886 may not match the M*Core's behaviour on boundary conditions. */
888 cpu
.gr
[RD
] = ((long) cpu
.gr
[RD
]) < 0 ? -1 : 0;
890 cpu
.gr
[RD
] = (long) cpu
.gr
[RD
] >> cpu
.gr
[RS
];
894 /* We must not rely solely upon the native shift operations, since they
895 may not match the M*Core's behaviour on boundary conditions. */
896 cpu
.gr
[RD
] = cpu
.gr
[RS
] > 31 ? 0 : cpu
.gr
[RD
] << cpu
.gr
[RS
];
899 case 0x1C: /* addu */
900 cpu
.gr
[RD
] += cpu
.gr
[RS
];
904 cpu
.gr
[RD
] += cpu
.gr
[RS
] << 1;
908 cpu
.gr
[RD
] |= cpu
.gr
[RS
];
911 case 0x1F: /* andn */
912 cpu
.gr
[RD
] &= ~cpu
.gr
[RS
];
914 case 0x20: case 0x21: /* addi */
916 cpu
.gr
[RD
] + (IMM5
+ 1);
918 case 0x22: case 0x23: /* cmplti */
920 int tmp
= (IMM5
+ 1);
921 if (cpu
.gr
[RD
] < tmp
)
931 case 0x24: case 0x25: /* subi */
933 cpu
.gr
[RD
] - (IMM5
+ 1);
935 case 0x26: case 0x27: /* illegal */
936 cpu
.asregs
.exception
= SIGILL
;
938 case 0x28: case 0x29: /* rsubi */
942 case 0x2A: case 0x2B: /* cmpnei */
943 if (cpu
.gr
[RD
] != IMM5
)
953 case 0x2C: case 0x2D: /* bmaski, divu */
967 /* unsigned divide */
968 cpu
.gr
[RD
] = (word
) ((unsigned int) cpu
.gr
[RD
] / (unsigned int)cpu
.gr
[1] );
970 /* compute bonus_cycles for divu */
971 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32); r1nlz
++)
974 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32); rxnlz
++)
980 exe
+= 5 + r1nlz
- rxnlz
;
982 if (exe
>= (2 * memcycles
- 1))
984 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
987 else if (imm
== 0 || imm
>= 8)
993 cpu
.gr
[RD
] = (1 << imm
) - 1;
998 cpu
.asregs
.exception
= SIGILL
;
1002 case 0x2E: case 0x2F: /* andi */
1003 cpu
.gr
[RD
] = cpu
.gr
[RD
] & IMM5
;
1005 case 0x30: case 0x31: /* bclri */
1006 cpu
.gr
[RD
] = cpu
.gr
[RD
] & ~(1<<IMM5
);
1008 case 0x32: case 0x33: /* bgeni, divs */
1010 unsigned imm
= IMM5
;
1017 /* compute bonus_cycles for divu */
1022 if (((rx
< 0) && (r1
> 0)) || ((rx
>= 0) && (r1
< 0)))
1030 /* signed divide, general registers are of type int, so / op is OK */
1031 cpu
.gr
[RD
] = cpu
.gr
[RD
] / cpu
.gr
[1];
1033 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32) ; r1nlz
++ )
1036 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32) ; rxnlz
++ )
1042 exe
+= 6 + r1nlz
- rxnlz
+ sc
;
1044 if (exe
>= (2 * memcycles
- 1))
1046 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
1052 cpu
.gr
[RD
] = (1 << IMM5
);
1057 cpu
.asregs
.exception
= SIGILL
;
1061 case 0x34: case 0x35: /* bseti */
1062 cpu
.gr
[RD
] = cpu
.gr
[RD
] | (1 << IMM5
);
1064 case 0x36: case 0x37: /* btsti */
1065 NEW_C (cpu
.gr
[RD
] >> IMM5
);
1067 case 0x38: case 0x39: /* xsr, rotli */
1069 unsigned imm
= IMM5
;
1070 unsigned long tmp
= cpu
.gr
[RD
];
1076 cpu
.gr
[RD
] = (cbit
<< 31) | (tmp
>> 1);
1079 cpu
.gr
[RD
] = (tmp
<< imm
) | (tmp
>> (32 - imm
));
1082 case 0x3A: case 0x3B: /* asrc, asri */
1084 unsigned imm
= IMM5
;
1085 long tmp
= cpu
.gr
[RD
];
1089 cpu
.gr
[RD
] = tmp
>> 1;
1092 cpu
.gr
[RD
] = tmp
>> imm
;
1095 case 0x3C: case 0x3D: /* lslc, lsli */
1097 unsigned imm
= IMM5
;
1098 unsigned long tmp
= cpu
.gr
[RD
];
1102 cpu
.gr
[RD
] = tmp
<< 1;
1105 cpu
.gr
[RD
] = tmp
<< imm
;
1108 case 0x3E: case 0x3F: /* lsrc, lsri */
1110 unsigned imm
= IMM5
;
1111 unsigned long tmp
= cpu
.gr
[RD
];
1115 cpu
.gr
[RD
] = tmp
>> 1;
1118 cpu
.gr
[RD
] = tmp
>> imm
;
1121 case 0x40: case 0x41: case 0x42: case 0x43:
1122 case 0x44: case 0x45: case 0x46: case 0x47:
1123 case 0x48: case 0x49: case 0x4A: case 0x4B:
1124 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1125 cpu
.asregs
.exception
= SIGILL
;
1128 util (sd
, inst
& 0xFF);
1130 case 0x51: case 0x52: case 0x53:
1131 case 0x54: case 0x55: case 0x56: case 0x57:
1132 case 0x58: case 0x59: case 0x5A: case 0x5B:
1133 case 0x5C: case 0x5D: case 0x5E: case 0x5F:
1134 cpu
.asregs
.exception
= SIGILL
;
1136 case 0x60: case 0x61: case 0x62: case 0x63: /* movi */
1137 case 0x64: case 0x65: case 0x66: case 0x67:
1138 cpu
.gr
[RD
] = (inst
>> 4) & 0x7F;
1140 case 0x68: case 0x69: case 0x6A: case 0x6B:
1141 case 0x6C: case 0x6D: case 0x6E: case 0x6F: /* illegal */
1142 cpu
.asregs
.exception
= SIGILL
;
1144 case 0x71: case 0x72: case 0x73:
1145 case 0x74: case 0x75: case 0x76: case 0x77:
1146 case 0x78: case 0x79: case 0x7A: case 0x7B:
1147 case 0x7C: case 0x7D: case 0x7E: /* lrw */
1148 cpu
.gr
[RX
] = rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1150 fprintf (stderr
, "LRW of 0x%x from 0x%lx to reg %d",
1151 rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC),
1152 (pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC, RX
);
1155 case 0x7F: /* jsri */
1159 "func call: r2 = %lx r3 = %lx r4 = %lx r5 = %lx r6 = %lx r7 = %lx\n",
1160 cpu
.gr
[2], cpu
.gr
[3], cpu
.gr
[4], cpu
.gr
[5], cpu
.gr
[6], cpu
.gr
[7]);
1161 case 0x70: /* jmpi */
1162 pc
= rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1168 case 0x80: case 0x81: case 0x82: case 0x83:
1169 case 0x84: case 0x85: case 0x86: case 0x87:
1170 case 0x88: case 0x89: case 0x8A: case 0x8B:
1171 case 0x8C: case 0x8D: case 0x8E: case 0x8F: /* ld */
1172 cpu
.gr
[RX
] = rlat (cpu
.gr
[RD
] + ((inst
>> 2) & 0x003C));
1174 fprintf (stderr
, "load reg %d from 0x%lx with 0x%lx",
1176 cpu
.gr
[RD
] + ((inst
>> 2) & 0x003C), cpu
.gr
[RX
]);
1179 case 0x90: case 0x91: case 0x92: case 0x93:
1180 case 0x94: case 0x95: case 0x96: case 0x97:
1181 case 0x98: case 0x99: case 0x9A: case 0x9B:
1182 case 0x9C: case 0x9D: case 0x9E: case 0x9F: /* st */
1183 wlat (cpu
.gr
[RD
] + ((inst
>> 2) & 0x003C), cpu
.gr
[RX
]);
1185 fprintf (stderr
, "store reg %d (containing 0x%lx) to 0x%lx",
1187 cpu
.gr
[RD
] + ((inst
>> 2) & 0x003C));
1190 case 0xA0: case 0xA1: case 0xA2: case 0xA3:
1191 case 0xA4: case 0xA5: case 0xA6: case 0xA7:
1192 case 0xA8: case 0xA9: case 0xAA: case 0xAB:
1193 case 0xAC: case 0xAD: case 0xAE: case 0xAF: /* ld.b */
1194 cpu
.gr
[RX
] = rbat (cpu
.gr
[RD
] + RS
);
1197 case 0xB0: case 0xB1: case 0xB2: case 0xB3:
1198 case 0xB4: case 0xB5: case 0xB6: case 0xB7:
1199 case 0xB8: case 0xB9: case 0xBA: case 0xBB:
1200 case 0xBC: case 0xBD: case 0xBE: case 0xBF: /* st.b */
1201 wbat (cpu
.gr
[RD
] + RS
, cpu
.gr
[RX
]);
1204 case 0xC0: case 0xC1: case 0xC2: case 0xC3:
1205 case 0xC4: case 0xC5: case 0xC6: case 0xC7:
1206 case 0xC8: case 0xC9: case 0xCA: case 0xCB:
1207 case 0xCC: case 0xCD: case 0xCE: case 0xCF: /* ld.h */
1208 cpu
.gr
[RX
] = rhat (cpu
.gr
[RD
] + ((inst
>> 3) & 0x001E));
1211 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
1212 case 0xD4: case 0xD5: case 0xD6: case 0xD7:
1213 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1214 case 0xDC: case 0xDD: case 0xDE: case 0xDF: /* st.h */
1215 what (cpu
.gr
[RD
] + ((inst
>> 3) & 0x001E), cpu
.gr
[RX
]);
1218 case 0xE8: case 0xE9: case 0xEA: case 0xEB:
1219 case 0xEC: case 0xED: case 0xEE: case 0xEF: /* bf */
1223 disp
= inst
& 0x03FF;
1231 case 0xE0: case 0xE1: case 0xE2: case 0xE3:
1232 case 0xE4: case 0xE5: case 0xE6: case 0xE7: /* bt */
1236 disp
= inst
& 0x03FF;
1245 case 0xF8: case 0xF9: case 0xFA: case 0xFB:
1246 case 0xFC: case 0xFD: case 0xFE: case 0xFF: /* bsr */
1248 case 0xF0: case 0xF1: case 0xF2: case 0xF3:
1249 case 0xF4: case 0xF5: case 0xF6: case 0xF7: /* br */
1252 disp
= inst
& 0x03FF;
1264 fprintf (stderr
, "\n");
1268 ibuf
= rlat (pc
& 0xFFFFFFFC);
1272 while (!cpu
.asregs
.exception
);
1274 /* Hide away the things we've cached while executing. */
1275 CPU_PC_SET (scpu
, pc
);
1276 cpu
.asregs
.insts
+= insts
; /* instructions done ... */
1277 cpu
.asregs
.cycles
+= insts
; /* and each takes a cycle */
1278 cpu
.asregs
.cycles
+= bonus_cycles
; /* and extra cycles for branches */
1279 cpu
.asregs
.cycles
+= memops
* memcycles
; /* and memop cycle delays */
1283 sim_store_register (SIM_DESC sd
, int rn
, unsigned char *memory
, int length
)
1285 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1291 /* misalignment safe */
1292 ival
= mcore_extract_unsigned_integer (memory
, 4);
1293 cpu
.asints
[rn
] = ival
;
1303 sim_fetch_register (SIM_DESC sd
, int rn
, unsigned char *memory
, int length
)
1305 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1309 long ival
= cpu
.asints
[rn
];
1311 /* misalignment-safe */
1312 mcore_store_unsigned_integer (memory
, 4, ival
);
1322 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason
, int *sigrc
)
1324 if (cpu
.asregs
.exception
== SIGQUIT
)
1326 * reason
= sim_exited
;
1327 * sigrc
= cpu
.gr
[PARM1
];
1331 * reason
= sim_stopped
;
1332 * sigrc
= cpu
.asregs
.exception
;
1337 sim_info (SIM_DESC sd
, int verbose
)
1339 #ifdef WATCHFUNCTIONS
1342 double virttime
= cpu
.asregs
.cycles
/ 36.0e6
;
1343 host_callback
*callback
= STATE_CALLBACK (sd
);
1345 callback
->printf_filtered (callback
, "\n\n# instructions executed %10d\n",
1347 callback
->printf_filtered (callback
, "# cycles %10d\n",
1349 callback
->printf_filtered (callback
, "# pipeline stalls %10d\n",
1351 callback
->printf_filtered (callback
, "# virtual time taken %10.4f\n",
1354 #ifdef WATCHFUNCTIONS
1355 callback
->printf_filtered (callback
, "\nNumber of watched functions: %d\n",
1360 for (w
= 1; w
<= ENDWL
; w
++)
1362 callback
->printf_filtered (callback
, "WL = %s %8x\n",WLstr
[w
],WL
[w
]);
1363 callback
->printf_filtered (callback
, " calls = %d, cycles = %d\n",
1364 WLcnts
[w
],WLcyc
[w
]);
1367 callback
->printf_filtered (callback
,
1368 " maxcpc = %d, mincpc = %d, avecpc = %d\n",
1369 WLmax
[w
],WLmin
[w
],WLcyc
[w
]/WLcnts
[w
]);
1373 callback
->printf_filtered (callback
,
1374 "Total cycles for watched functions: %d\n",wcyc
);
1379 mcore_pc_get (sim_cpu
*cpu
)
1385 mcore_pc_set (sim_cpu
*cpu
, sim_cia pc
)
1391 free_state (SIM_DESC sd
)
1393 if (STATE_MODULES (sd
) != NULL
)
1394 sim_module_uninstall (sd
);
1395 sim_cpu_free_all (sd
);
1396 sim_state_free (sd
);
1400 sim_open (SIM_OPEN_KIND kind
, host_callback
*cb
, struct bfd
*abfd
, char **argv
)
1403 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
1404 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1406 /* The cpu data is kept in a separately allocated chunk of memory. */
1407 if (sim_cpu_alloc_all (sd
, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK
)
1413 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
1419 /* getopt will print the error message so we just have to exit if this fails.
1420 FIXME: Hmmm... in the case of gdb we need getopt to call
1422 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
1428 /* Check for/establish the a reference program image. */
1429 if (sim_analyze_program (sd
,
1430 (STATE_PROG_ARGV (sd
) != NULL
1431 ? *STATE_PROG_ARGV (sd
)
1432 : NULL
), abfd
) != SIM_RC_OK
)
1438 /* Configure/verify the target byte order and other runtime
1439 configuration options. */
1440 if (sim_config (sd
) != SIM_RC_OK
)
1442 sim_module_uninstall (sd
);
1446 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
1448 /* Uninstall the modules to avoid memory leaks,
1449 file descriptor leaks, etc. */
1450 sim_module_uninstall (sd
);
1454 /* CPU specific initialization. */
1455 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1457 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
1459 CPU_PC_FETCH (cpu
) = mcore_pc_get
;
1460 CPU_PC_STORE (cpu
) = mcore_pc_set
;
1462 set_initial_gprs (cpu
); /* Reset the GPR registers. */
1465 /* Default to a 8 Mbyte (== 2^23) memory space. */
1466 sim_do_commandf (sd
, "memory-size %#x", DEFAULT_MEMORY_SIZE
);
1472 sim_close (SIM_DESC sd
, int quitting
)
1478 sim_create_inferior (SIM_DESC sd
, struct bfd
*prog_bfd
, char **argv
, char **env
)
1480 SIM_CPU
*scpu
= STATE_CPU (sd
, 0);
1486 unsigned long strings
;
1487 unsigned long pointers
;
1488 unsigned long hi_stack
;
1491 /* Set the initial register set. */
1492 set_initial_gprs (scpu
);
1494 hi_stack
= DEFAULT_MEMORY_SIZE
- 4;
1495 CPU_PC_SET (scpu
, bfd_get_start_address (prog_bfd
));
1497 /* Calculate the argument and environment strings. */
1503 l
= strlen (*avp
) + 1; /* include the null */
1504 s_length
+= (l
+ 3) & ~3; /* make it a 4 byte boundary */
1512 l
= strlen (*avp
) + 1; /* include the null */
1513 s_length
+= (l
+ 3) & ~ 3;/* make it a 4 byte boundary */
1517 /* Claim some memory for the pointers and strings. */
1518 pointers
= hi_stack
- sizeof(word
) * (nenv
+1+nargs
+1);
1519 pointers
&= ~3; /* must be 4-byte aligned */
1520 cpu
.gr
[0] = pointers
;
1522 strings
= cpu
.gr
[0] - s_length
;
1523 strings
&= ~3; /* want to make it 4-byte aligned */
1524 cpu
.gr
[0] = strings
;
1525 /* dac fix, the stack address must be 8-byte aligned! */
1526 cpu
.gr
[0] = cpu
.gr
[0] - cpu
.gr
[0] % 8;
1528 /* Loop through the arguments and fill them in. */
1529 cpu
.gr
[PARM1
] = nargs
;
1532 /* No strings to fill in. */
1537 cpu
.gr
[PARM2
] = pointers
;
1541 /* Save where we're putting it. */
1542 wlat (pointers
, strings
);
1544 /* Copy the string. */
1545 l
= strlen (* avp
) + 1;
1546 sim_core_write_buffer (sd
, scpu
, write_map
, *avp
, strings
, l
);
1548 /* Bump the pointers. */
1554 /* A null to finish the list. */
1559 /* Now do the environment pointers. */
1562 /* No strings to fill in. */
1567 cpu
.gr
[PARM3
] = pointers
;
1572 /* Save where we're putting it. */
1573 wlat (pointers
, strings
);
1575 /* Copy the string. */
1576 l
= strlen (* avp
) + 1;
1577 sim_core_write_buffer (sd
, scpu
, write_map
, *avp
, strings
, l
);
1579 /* Bump the pointers. */
1585 /* A null to finish the list. */
This page took 0.078737 seconds and 4 git commands to generate.