]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/mcore/interp.c
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-options.h"
36 #define target_big_endian (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN)
40 mcore_extract_unsigned_integer (unsigned char *addr
, int len
)
44 unsigned char * startaddr
= (unsigned char *)addr
;
45 unsigned char * endaddr
= startaddr
+ len
;
47 if (len
> (int) sizeof (unsigned long))
48 printf ("That operation is not available on integers of more than %zu bytes.",
49 sizeof (unsigned long));
51 /* Start at the most significant end of the integer, and work towards
52 the least significant. */
55 if (! target_big_endian
)
57 for (p
= endaddr
; p
> startaddr
;)
58 retval
= (retval
<< 8) | * -- p
;
62 for (p
= startaddr
; p
< endaddr
;)
63 retval
= (retval
<< 8) | * p
++;
70 mcore_store_unsigned_integer (unsigned char *addr
, int len
, unsigned long val
)
73 unsigned char * startaddr
= (unsigned char *)addr
;
74 unsigned char * endaddr
= startaddr
+ len
;
76 if (! target_big_endian
)
78 for (p
= startaddr
; p
< endaddr
;)
86 for (p
= endaddr
; p
> startaddr
;)
95 This state is maintained in host byte order. The
96 fetch/store register functions must translate between host
97 byte order and the target processor byte order.
98 Keeping this data in target byte order simplifies the register
99 read/write functions. Keeping this data in native order improves
100 the performance of the simulator. Simulation speed is deemed more
102 /* TODO: Should be moved to sim-main.h:sim_cpu. */
104 /* The ordering of the mcore_regset structure is matched in the
105 gdb/config/mcore/tm-mcore.h file in the REGISTER_NAMES macro. */
108 word gregs
[16]; /* primary registers */
109 word alt_gregs
[16]; /* alt register file */
110 word cregs
[32]; /* control registers */
117 unsigned char * memory
;
123 struct mcore_regset asregs
;
124 word asints
[1]; /* but accessed larger... */
127 #define LAST_VALID_CREG 32 /* only 0..12 implemented */
128 #define NUM_MCORE_REGS (16 + 16 + LAST_VALID_CREG + 1)
130 static int memcycles
= 1;
132 static int issue_messages
= 0;
134 #define gr asregs.active_gregs
135 #define cr asregs.cregs
136 #define sr asregs.cregs[0]
137 #define vbr asregs.cregs[1]
138 #define esr asregs.cregs[2]
139 #define fsr asregs.cregs[3]
140 #define epc asregs.cregs[4]
141 #define fpc asregs.cregs[5]
142 #define ss0 asregs.cregs[6]
143 #define ss1 asregs.cregs[7]
144 #define ss2 asregs.cregs[8]
145 #define ss3 asregs.cregs[9]
146 #define ss4 asregs.cregs[10]
147 #define gcr asregs.cregs[11]
148 #define gsr asregs.cregs[12]
149 #define mem asregs.memory
151 /* maniuplate the carry bit */
152 #define C_ON() (cpu.sr & 1)
153 #define C_VALUE() (cpu.sr & 1)
154 #define C_OFF() ((cpu.sr & 1) == 0)
155 #define SET_C() {cpu.sr |= 1;}
156 #define CLR_C() {cpu.sr &= 0xfffffffe;}
157 #define NEW_C(v) {CLR_C(); cpu.sr |= ((v) & 1);}
159 #define SR_AF() ((cpu.sr >> 1) & 1)
161 #define TRAPCODE 1 /* r1 holds which function we want */
162 #define PARM1 2 /* first parameter */
166 #define RET1 2 /* register for return values. */
169 wbat (word x
, word v
)
171 if (((uword
)x
) >= cpu
.asregs
.msize
)
174 fprintf (stderr
, "byte write to 0x%x outside memory range\n", x
);
176 cpu
.asregs
.exception
= SIGSEGV
;
180 unsigned char *p
= cpu
.mem
+ x
;
186 wlat (word x
, word v
)
188 if (((uword
)x
) >= cpu
.asregs
.msize
)
191 fprintf (stderr
, "word write to 0x%x outside memory range\n", x
);
193 cpu
.asregs
.exception
= SIGSEGV
;
200 fprintf (stderr
, "word write to unaligned memory address: 0x%x\n", x
);
202 cpu
.asregs
.exception
= SIGBUS
;
204 else if (! target_big_endian
)
206 unsigned char * p
= cpu
.mem
+ x
;
214 unsigned char * p
= cpu
.mem
+ x
;
224 what (word x
, word v
)
226 if (((uword
)x
) >= cpu
.asregs
.msize
)
229 fprintf (stderr
, "short write to 0x%x outside memory range\n", x
);
231 cpu
.asregs
.exception
= SIGSEGV
;
238 fprintf (stderr
, "short write to unaligned memory address: 0x%x\n",
241 cpu
.asregs
.exception
= SIGBUS
;
243 else if (! target_big_endian
)
245 unsigned char * p
= cpu
.mem
+ x
;
251 unsigned char * p
= cpu
.mem
+ x
;
258 /* Read functions. */
262 if (((uword
)x
) >= cpu
.asregs
.msize
)
265 fprintf (stderr
, "byte read from 0x%x outside memory range\n", x
);
267 cpu
.asregs
.exception
= SIGSEGV
;
272 unsigned char * p
= cpu
.mem
+ x
;
280 if (((uword
) x
) >= cpu
.asregs
.msize
)
283 fprintf (stderr
, "word read from 0x%x outside memory range\n", x
);
285 cpu
.asregs
.exception
= SIGSEGV
;
293 fprintf (stderr
, "word read from unaligned address: 0x%x\n", x
);
295 cpu
.asregs
.exception
= SIGBUS
;
298 else if (! target_big_endian
)
300 unsigned char * p
= cpu
.mem
+ x
;
301 return (p
[3] << 24) | (p
[2] << 16) | (p
[1] << 8) | p
[0];
305 unsigned char * p
= cpu
.mem
+ x
;
306 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
314 if (((uword
)x
) >= cpu
.asregs
.msize
)
317 fprintf (stderr
, "short read from 0x%x outside memory range\n", x
);
319 cpu
.asregs
.exception
= SIGSEGV
;
327 fprintf (stderr
, "short read from unaligned address: 0x%x\n", x
);
329 cpu
.asregs
.exception
= SIGBUS
;
332 else if (! target_big_endian
)
334 unsigned char * p
= cpu
.mem
+ x
;
335 return (p
[1] << 8) | p
[0];
339 unsigned char * p
= cpu
.mem
+ x
;
340 return (p
[0] << 8) | p
[1];
346 /* Default to a 8 Mbyte (== 2^23) memory space. */
347 /* TODO: Delete all this custom memory logic and move to common sim helpers. */
348 static int sim_memory_size
= 23;
350 #define MEM_SIZE_FLOOR 64
354 sim_memory_size
= power
;
355 cpu
.asregs
.msize
= 1 << sim_memory_size
;
360 /* Watch out for the '0 count' problem. There's probably a better
361 way.. e.g., why do we use 64 here? */
362 if (cpu
.asregs
.msize
< 64) /* Ensure a boundary. */
363 cpu
.mem
= (unsigned char *) calloc (64, (64 + cpu
.asregs
.msize
) / 64);
365 cpu
.mem
= (unsigned char *) calloc (64, cpu
.asregs
.msize
/ 64);
371 "Not enough VM for simulation of %lu bytes of RAM\n",
374 cpu
.asregs
.msize
= 1;
375 cpu
.mem
= (unsigned char *) calloc (1, 1);
382 if (cpu
.asregs
.msize
!= (1 << sim_memory_size
))
383 sim_size (sim_memory_size
);
387 set_initial_gprs (SIM_CPU
*scpu
)
391 unsigned long memsize
;
395 /* Set up machine just out of reset. */
396 CPU_PC_SET (scpu
, 0);
399 memsize
= cpu
.asregs
.msize
/ (1024 * 1024);
401 if (issue_messages
> 1)
402 fprintf (stderr
, "Simulated memory of %lu Mbytes (0x0 .. 0x%08lx)\n",
403 memsize
, cpu
.asregs
.msize
- 1);
405 /* Clean out the GPRs and alternate GPRs. */
406 for (i
= 0; i
< 16; i
++)
408 cpu
.asregs
.gregs
[i
] = 0;
409 cpu
.asregs
.alt_gregs
[i
] = 0;
412 /* Make our register set point to the right place. */
414 cpu
.asregs
.active_gregs
= &cpu
.asregs
.alt_gregs
[0];
416 cpu
.asregs
.active_gregs
= &cpu
.asregs
.gregs
[0];
418 /* ABI specifies initial values for these registers. */
419 cpu
.gr
[0] = cpu
.asregs
.msize
- 4;
421 /* dac fix, the stack address must be 8-byte aligned! */
422 cpu
.gr
[0] = cpu
.gr
[0] - cpu
.gr
[0] % 8;
426 cpu
.gr
[PARM4
] = cpu
.gr
[0];
429 /* Read/write functions for system call interface. */
432 syscall_read_mem (host_callback
*cb
, struct cb_syscall
*sc
,
433 unsigned long taddr
, char *buf
, int bytes
)
435 memcpy (buf
, cpu
.mem
+ taddr
, bytes
);
440 syscall_write_mem (host_callback
*cb
, struct cb_syscall
*sc
,
441 unsigned long taddr
, const char *buf
, int bytes
)
443 memcpy (cpu
.mem
+ taddr
, buf
, bytes
);
447 /* Simulate a monitor trap. */
450 handle_trap1 (SIM_DESC sd
)
452 host_callback
*cb
= STATE_CALLBACK (sd
);
455 CB_SYSCALL_INIT (&sc
);
457 sc
.func
= cpu
.gr
[TRAPCODE
];
458 sc
.arg1
= cpu
.gr
[PARM1
];
459 sc
.arg2
= cpu
.gr
[PARM2
];
460 sc
.arg3
= cpu
.gr
[PARM3
];
461 sc
.arg4
= cpu
.gr
[PARM4
];
464 sc
.p2
= (PTR
) STATE_CPU (sd
, 0);
465 sc
.read_mem
= syscall_read_mem
;
466 sc
.write_mem
= syscall_write_mem
;
468 cb_syscall (cb
, &sc
);
470 /* XXX: We don't pass back the actual errno value. */
471 cpu
.gr
[RET1
] = sc
.result
;
475 process_stub (SIM_DESC sd
, int what
)
477 /* These values should match those in libgloss/mcore/syscalls.s. */
484 case 10: /* _unlink */
485 case 19: /* _lseek */
486 case 43: /* _times */
487 cpu
.gr
[TRAPCODE
] = what
;
493 fprintf (stderr
, "Unhandled stub opcode: %d\n", what
);
499 util (SIM_DESC sd
, unsigned what
)
504 cpu
.asregs
.exception
= SIGQUIT
;
513 a
[0] = (unsigned long)(cpu
.mem
+ cpu
.gr
[PARM1
]);
515 for (s
= (unsigned char *)a
[0], i
= 1 ; *s
&& i
< 6 ; s
++)
520 a
[i
] = (unsigned long)(cpu
.mem
+ cpu
.gr
[PARM1
+i
]);
522 a
[i
] = cpu
.gr
[i
+PARM1
];
527 cpu
.gr
[RET1
] = printf ((char *)a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
533 fprintf (stderr
, "WARNING: scanf unimplemented\n");
537 cpu
.gr
[RET1
] = cpu
.asregs
.insts
;
541 process_stub (sd
, cpu
.gr
[1]);
546 fprintf (stderr
, "Unhandled util code: %x\n", what
);
551 /* For figuring out whether we carried; addc/subc use this. */
553 iu_carry (unsigned long a
, unsigned long b
, int cin
)
557 x
= (a
& 0xffff) + (b
& 0xffff) + cin
;
558 x
= (x
>> 16) + (a
>> 16) + (b
>> 16);
564 /* TODO: Convert to common watchpoints. */
565 #undef WATCHFUNCTIONS
566 #ifdef WATCHFUNCTIONS
583 #define RD (inst & 0xF)
584 #define RS ((inst >> 4) & 0xF)
585 #define RX ((inst >> 8) & 0xF)
586 #define IMM5 ((inst >> 4) & 0x1F)
587 #define IMM4 ((inst) & 0xF)
589 static int tracing
= 0;
592 sim_resume (SIM_DESC sd
, int step
, int siggnal
)
594 SIM_CPU
*scpu
= STATE_CPU (sd
, 0);
604 #ifdef WATCHFUNCTIONS
608 cpu
.asregs
.exception
= step
? SIGTRAP
: 0;
609 pc
= CPU_PC_GET (scpu
);
611 /* Fetch the initial instructions that we'll decode. */
612 ibuf
= rlat (pc
& 0xFFFFFFFC);
619 /* make our register set point to the right place */
621 cpu
.asregs
.active_gregs
= & cpu
.asregs
.alt_gregs
[0];
623 cpu
.asregs
.active_gregs
= & cpu
.asregs
.gregs
[0];
625 #ifdef WATCHFUNCTIONS
626 /* make a hash to speed exec loop, hope it's nonzero */
629 for (w
= 1; w
<= ENDWL
; w
++)
630 WLhash
= WLhash
& WL
[w
];
641 if (! target_big_endian
)
644 inst
= ibuf
& 0xFFFF;
649 if (! target_big_endian
)
650 inst
= ibuf
& 0xFFFF;
655 #ifdef WATCHFUNCTIONS
656 /* now scan list of watch addresses, if match, count it and
657 note return address and count cycles until pc=return address */
659 if ((WLincyc
== 1) && (pc
== WLendpc
))
661 cycs
= (cpu
.asregs
.cycles
+ (insts
+ bonus_cycles
+
662 (memops
* memcycles
)) - WLbcyc
);
664 if (WLcnts
[WLW
] == 1)
671 if (cycs
> WLmax
[WLW
])
676 if (cycs
< WLmin
[WLW
])
686 /* Optimize with a hash to speed loop. */
689 if ((WLhash
== 0) || ((WLhash
& pc
) != 0))
691 for (w
=1; w
<= ENDWL
; w
++)
696 WLbcyc
= cpu
.asregs
.cycles
+ insts
697 + bonus_cycles
+ (memops
* memcycles
);
698 WLendpc
= cpu
.gr
[15];
709 fprintf (stderr
, "%.4x: inst = %.4x ", pc
, inst
);
724 cpu
.asregs
.exception
= SIGTRAP
;
737 cpu
.asregs
.active_gregs
= & cpu
.asregs
.alt_gregs
[0];
739 cpu
.asregs
.active_gregs
= & cpu
.asregs
.gregs
[0];
748 cpu
.asregs
.active_gregs
= &cpu
.asregs
.alt_gregs
[0];
750 cpu
.asregs
.active_gregs
= &cpu
.asregs
.gregs
[0];
755 fprintf (stderr
, "WARNING: stop unimplemented\n");
760 fprintf (stderr
, "WARNING: wait unimplemented\n");
765 fprintf (stderr
, "WARNING: doze unimplemented\n");
769 cpu
.asregs
.exception
= SIGILL
; /* illegal */
772 case 0x8: /* trap 0 */
773 case 0xA: /* trap 2 */
774 case 0xB: /* trap 3 */
775 cpu
.asregs
.exception
= SIGTRAP
;
778 case 0xC: /* trap 4 */
779 case 0xD: /* trap 5 */
780 case 0xE: /* trap 6 */
781 cpu
.asregs
.exception
= SIGILL
; /* illegal */
784 case 0xF: /* trap 7 */
785 cpu
.asregs
.exception
= SIGTRAP
; /* integer div-by-0 */
788 case 0x9: /* trap 1 */
795 cpu
.asregs
.exception
= SIGILL
; /* illegal */
799 cpu
.gr
[RD
] = C_VALUE();
802 cpu
.gr
[RD
] = C_OFF();
806 word addr
= cpu
.gr
[RD
];
807 int regno
= 4; /* always r4-r7 */
813 cpu
.gr
[regno
] = rlat(addr
);
817 while ((regno
&0x3) != 0);
822 word addr
= cpu
.gr
[RD
];
823 int regno
= 4; /* always r4-r7 */
829 wlat(addr
, cpu
.gr
[regno
]);
833 while ((regno
& 0x3) != 0);
838 word addr
= cpu
.gr
[0];
841 /* bonus cycle is really only needed if
842 the next insn shifts the last reg loaded.
849 cpu
.gr
[regno
] = rlat(addr
);
857 word addr
= cpu
.gr
[0];
860 /* this should be removed! */
861 /* bonus_cycles ++; */
863 memops
+= 16 - regno
;
866 wlat(addr
, cpu
.gr
[regno
]);
874 cpu
.gr
[RD
] -= C_VALUE();
877 cpu
.gr
[RD
] -= C_OFF();
880 cpu
.gr
[RD
] += C_VALUE();
883 cpu
.gr
[RD
] += C_OFF();
887 if (tracing
&& RD
== 15)
888 fprintf (stderr
, "Func return, r2 = %x, r3 = %x\n",
889 cpu
.gr
[2], cpu
.gr
[3]);
903 for (i
= 0; !(tmp
& 0x80000000) && i
< 32; i
++)
912 tmp
= ((tmp
& 0xaaaaaaaa) >> 1) | ((tmp
& 0x55555555) << 1);
913 tmp
= ((tmp
& 0xcccccccc) >> 2) | ((tmp
& 0x33333333) << 2);
914 tmp
= ((tmp
& 0xf0f0f0f0) >> 4) | ((tmp
& 0x0f0f0f0f) << 4);
915 tmp
= ((tmp
& 0xff00ff00) >> 8) | ((tmp
& 0x00ff00ff) << 8);
916 cpu
.gr
[RD
] = ((tmp
& 0xffff0000) >> 16) | ((tmp
& 0x0000ffff) << 16);
924 case 0x0: /* xtrb3 */
925 cpu
.gr
[1] = (cpu
.gr
[RD
]) & 0xFF;
926 NEW_C (cpu
.gr
[RD
] != 0);
928 case 0x1: /* xtrb2 */
929 cpu
.gr
[1] = (cpu
.gr
[RD
]>>8) & 0xFF;
930 NEW_C (cpu
.gr
[RD
] != 0);
932 case 0x2: /* xtrb1 */
933 cpu
.gr
[1] = (cpu
.gr
[RD
]>>16) & 0xFF;
934 NEW_C (cpu
.gr
[RD
] != 0);
936 case 0x3: /* xtrb0 */
937 cpu
.gr
[1] = (cpu
.gr
[RD
]>>24) & 0xFF;
938 NEW_C (cpu
.gr
[RD
] != 0);
940 case 0x4: /* zextb */
941 cpu
.gr
[RD
] &= 0x000000FF;
943 case 0x5: /* sextb */
952 case 0x6: /* zexth */
953 cpu
.gr
[RD
] &= 0x0000FFFF;
955 case 0x7: /* sexth */
964 case 0x8: /* declt */
966 NEW_C ((long)cpu
.gr
[RD
] < 0);
968 case 0x9: /* tstnbz */
970 word tmp
= cpu
.gr
[RD
];
971 NEW_C ((tmp
& 0xFF000000) != 0 &&
972 (tmp
& 0x00FF0000) != 0 && (tmp
& 0x0000FF00) != 0 &&
973 (tmp
& 0x000000FF) != 0);
976 case 0xA: /* decgt */
978 NEW_C ((long)cpu
.gr
[RD
] > 0);
980 case 0xB: /* decne */
982 NEW_C ((long)cpu
.gr
[RD
] != 0);
993 if (cpu
.gr
[RD
] & 0x80000000)
994 cpu
.gr
[RD
] = ~cpu
.gr
[RD
] + 1;
997 cpu
.gr
[RD
] = ~cpu
.gr
[RD
];
1001 case 0x02: /* movt */
1003 cpu
.gr
[RD
] = cpu
.gr
[RS
];
1005 case 0x03: /* mult */
1006 /* consume 2 bits per cycle from rs, until rs is 0 */
1008 unsigned int t
= cpu
.gr
[RS
];
1010 for (ticks
= 0; t
!= 0 ; t
>>= 2)
1012 bonus_cycles
+= ticks
;
1014 bonus_cycles
+= 2; /* min. is 3, so add 2, plus ticks above */
1016 fprintf (stderr
, " mult %x by %x to give %x",
1017 cpu
.gr
[RD
], cpu
.gr
[RS
], cpu
.gr
[RD
] * cpu
.gr
[RS
]);
1018 cpu
.gr
[RD
] = cpu
.gr
[RD
] * cpu
.gr
[RS
];
1020 case 0x04: /* loopt */
1023 pc
+= (IMM4
<< 1) - 32;
1027 --cpu
.gr
[RS
]; /* not RD! */
1028 NEW_C (((long)cpu
.gr
[RS
]) > 0);
1030 case 0x05: /* subu */
1031 cpu
.gr
[RD
] -= cpu
.gr
[RS
];
1033 case 0x06: /* addc */
1035 unsigned long tmp
, a
, b
;
1038 cpu
.gr
[RD
] = a
+ b
+ C_VALUE ();
1039 tmp
= iu_carry (a
, b
, C_VALUE ());
1043 case 0x07: /* subc */
1045 unsigned long tmp
, a
, b
;
1048 cpu
.gr
[RD
] = a
- b
+ C_VALUE () - 1;
1049 tmp
= iu_carry (a
,~b
, C_VALUE ());
1053 case 0x08: /* illegal */
1054 case 0x09: /* illegal*/
1055 cpu
.asregs
.exception
= SIGILL
;
1057 case 0x0A: /* movf */
1059 cpu
.gr
[RD
] = cpu
.gr
[RS
];
1061 case 0x0B: /* lsr */
1063 unsigned long dst
, src
;
1066 /* We must not rely solely upon the native shift operations, since they
1067 may not match the M*Core's behaviour on boundary conditions. */
1068 dst
= src
> 31 ? 0 : dst
>> src
;
1072 case 0x0C: /* cmphs */
1073 NEW_C ((unsigned long )cpu
.gr
[RD
] >=
1074 (unsigned long)cpu
.gr
[RS
]);
1076 case 0x0D: /* cmplt */
1077 NEW_C ((long)cpu
.gr
[RD
] < (long)cpu
.gr
[RS
]);
1079 case 0x0E: /* tst */
1080 NEW_C ((cpu
.gr
[RD
] & cpu
.gr
[RS
]) != 0);
1082 case 0x0F: /* cmpne */
1083 NEW_C (cpu
.gr
[RD
] != cpu
.gr
[RS
]);
1085 case 0x10: case 0x11: /* mfcr */
1089 if (r
<= LAST_VALID_CREG
)
1090 cpu
.gr
[RD
] = cpu
.cr
[r
];
1092 cpu
.asregs
.exception
= SIGILL
;
1096 case 0x12: /* mov */
1097 cpu
.gr
[RD
] = cpu
.gr
[RS
];
1099 fprintf (stderr
, "MOV %x into reg %d", cpu
.gr
[RD
], RD
);
1102 case 0x13: /* bgenr */
1103 if (cpu
.gr
[RS
] & 0x20)
1106 cpu
.gr
[RD
] = 1 << (cpu
.gr
[RS
] & 0x1F);
1109 case 0x14: /* rsub */
1110 cpu
.gr
[RD
] = cpu
.gr
[RS
] - cpu
.gr
[RD
];
1113 case 0x15: /* ixw */
1114 cpu
.gr
[RD
] += cpu
.gr
[RS
]<<2;
1117 case 0x16: /* and */
1118 cpu
.gr
[RD
] &= cpu
.gr
[RS
];
1121 case 0x17: /* xor */
1122 cpu
.gr
[RD
] ^= cpu
.gr
[RS
];
1125 case 0x18: case 0x19: /* mtcr */
1129 if (r
<= LAST_VALID_CREG
)
1130 cpu
.cr
[r
] = cpu
.gr
[RD
];
1132 cpu
.asregs
.exception
= SIGILL
;
1134 /* we might have changed register sets... */
1136 cpu
.asregs
.active_gregs
= & cpu
.asregs
.alt_gregs
[0];
1138 cpu
.asregs
.active_gregs
= & cpu
.asregs
.gregs
[0];
1142 case 0x1A: /* asr */
1143 /* We must not rely solely upon the native shift operations, since they
1144 may not match the M*Core's behaviour on boundary conditions. */
1145 if (cpu
.gr
[RS
] > 30)
1146 cpu
.gr
[RD
] = ((long) cpu
.gr
[RD
]) < 0 ? -1 : 0;
1148 cpu
.gr
[RD
] = (long) cpu
.gr
[RD
] >> cpu
.gr
[RS
];
1151 case 0x1B: /* lsl */
1152 /* We must not rely solely upon the native shift operations, since they
1153 may not match the M*Core's behaviour on boundary conditions. */
1154 cpu
.gr
[RD
] = cpu
.gr
[RS
] > 31 ? 0 : cpu
.gr
[RD
] << cpu
.gr
[RS
];
1157 case 0x1C: /* addu */
1158 cpu
.gr
[RD
] += cpu
.gr
[RS
];
1161 case 0x1D: /* ixh */
1162 cpu
.gr
[RD
] += cpu
.gr
[RS
] << 1;
1166 cpu
.gr
[RD
] |= cpu
.gr
[RS
];
1169 case 0x1F: /* andn */
1170 cpu
.gr
[RD
] &= ~cpu
.gr
[RS
];
1172 case 0x20: case 0x21: /* addi */
1174 cpu
.gr
[RD
] + (IMM5
+ 1);
1176 case 0x22: case 0x23: /* cmplti */
1178 int tmp
= (IMM5
+ 1);
1179 if (cpu
.gr
[RD
] < tmp
)
1189 case 0x24: case 0x25: /* subi */
1191 cpu
.gr
[RD
] - (IMM5
+ 1);
1193 case 0x26: case 0x27: /* illegal */
1194 cpu
.asregs
.exception
= SIGILL
;
1196 case 0x28: case 0x29: /* rsubi */
1200 case 0x2A: case 0x2B: /* cmpnei */
1201 if (cpu
.gr
[RD
] != IMM5
)
1211 case 0x2C: case 0x2D: /* bmaski, divu */
1213 unsigned imm
= IMM5
;
1219 unsigned int rx
, r1
;
1225 /* unsigned divide */
1226 cpu
.gr
[RD
] = (word
) ((unsigned int) cpu
.gr
[RD
] / (unsigned int)cpu
.gr
[1] );
1228 /* compute bonus_cycles for divu */
1229 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32); r1nlz
++)
1232 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32); rxnlz
++)
1238 exe
+= 5 + r1nlz
- rxnlz
;
1240 if (exe
>= (2 * memcycles
- 1))
1242 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
1245 else if (imm
== 0 || imm
>= 8)
1251 cpu
.gr
[RD
] = (1 << imm
) - 1;
1256 cpu
.asregs
.exception
= SIGILL
;
1260 case 0x2E: case 0x2F: /* andi */
1261 cpu
.gr
[RD
] = cpu
.gr
[RD
] & IMM5
;
1263 case 0x30: case 0x31: /* bclri */
1264 cpu
.gr
[RD
] = cpu
.gr
[RD
] & ~(1<<IMM5
);
1266 case 0x32: case 0x33: /* bgeni, divs */
1268 unsigned imm
= IMM5
;
1275 /* compute bonus_cycles for divu */
1280 if (((rx
< 0) && (r1
> 0)) || ((rx
>= 0) && (r1
< 0)))
1288 /* signed divide, general registers are of type int, so / op is OK */
1289 cpu
.gr
[RD
] = cpu
.gr
[RD
] / cpu
.gr
[1];
1291 for (r1nlz
= 0; ((r1
& 0x80000000) == 0) && (r1nlz
< 32) ; r1nlz
++ )
1294 for (rxnlz
= 0; ((rx
& 0x80000000) == 0) && (rxnlz
< 32) ; rxnlz
++ )
1300 exe
+= 6 + r1nlz
- rxnlz
+ sc
;
1302 if (exe
>= (2 * memcycles
- 1))
1304 bonus_cycles
+= exe
- (2 * memcycles
) + 1;
1310 cpu
.gr
[RD
] = (1 << IMM5
);
1315 cpu
.asregs
.exception
= SIGILL
;
1319 case 0x34: case 0x35: /* bseti */
1320 cpu
.gr
[RD
] = cpu
.gr
[RD
] | (1 << IMM5
);
1322 case 0x36: case 0x37: /* btsti */
1323 NEW_C (cpu
.gr
[RD
] >> IMM5
);
1325 case 0x38: case 0x39: /* xsr, rotli */
1327 unsigned imm
= IMM5
;
1328 unsigned long tmp
= cpu
.gr
[RD
];
1334 cpu
.gr
[RD
] = (cbit
<< 31) | (tmp
>> 1);
1337 cpu
.gr
[RD
] = (tmp
<< imm
) | (tmp
>> (32 - imm
));
1340 case 0x3A: case 0x3B: /* asrc, asri */
1342 unsigned imm
= IMM5
;
1343 long tmp
= cpu
.gr
[RD
];
1347 cpu
.gr
[RD
] = tmp
>> 1;
1350 cpu
.gr
[RD
] = tmp
>> imm
;
1353 case 0x3C: case 0x3D: /* lslc, lsli */
1355 unsigned imm
= IMM5
;
1356 unsigned long tmp
= cpu
.gr
[RD
];
1360 cpu
.gr
[RD
] = tmp
<< 1;
1363 cpu
.gr
[RD
] = tmp
<< imm
;
1366 case 0x3E: case 0x3F: /* lsrc, lsri */
1368 unsigned imm
= IMM5
;
1369 unsigned long tmp
= cpu
.gr
[RD
];
1373 cpu
.gr
[RD
] = tmp
>> 1;
1376 cpu
.gr
[RD
] = tmp
>> imm
;
1379 case 0x40: case 0x41: case 0x42: case 0x43:
1380 case 0x44: case 0x45: case 0x46: case 0x47:
1381 case 0x48: case 0x49: case 0x4A: case 0x4B:
1382 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1383 cpu
.asregs
.exception
= SIGILL
;
1386 util (sd
, inst
& 0xFF);
1388 case 0x51: case 0x52: case 0x53:
1389 case 0x54: case 0x55: case 0x56: case 0x57:
1390 case 0x58: case 0x59: case 0x5A: case 0x5B:
1391 case 0x5C: case 0x5D: case 0x5E: case 0x5F:
1392 cpu
.asregs
.exception
= SIGILL
;
1394 case 0x60: case 0x61: case 0x62: case 0x63: /* movi */
1395 case 0x64: case 0x65: case 0x66: case 0x67:
1396 cpu
.gr
[RD
] = (inst
>> 4) & 0x7F;
1398 case 0x68: case 0x69: case 0x6A: case 0x6B:
1399 case 0x6C: case 0x6D: case 0x6E: case 0x6F: /* illegal */
1400 cpu
.asregs
.exception
= SIGILL
;
1402 case 0x71: case 0x72: case 0x73:
1403 case 0x74: case 0x75: case 0x76: case 0x77:
1404 case 0x78: case 0x79: case 0x7A: case 0x7B:
1405 case 0x7C: case 0x7D: case 0x7E: /* lrw */
1406 cpu
.gr
[RX
] = rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1408 fprintf (stderr
, "LRW of 0x%x from 0x%x to reg %d",
1409 rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC),
1410 (pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC, RX
);
1413 case 0x7F: /* jsri */
1416 fprintf (stderr
, "func call: r2 = %x r3 = %x r4 = %x r5 = %x r6 = %x r7 = %x\n",
1417 cpu
.gr
[2], cpu
.gr
[3], cpu
.gr
[4], cpu
.gr
[5], cpu
.gr
[6], cpu
.gr
[7]);
1418 case 0x70: /* jmpi */
1419 pc
= rlat ((pc
+ ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
1425 case 0x80: case 0x81: case 0x82: case 0x83:
1426 case 0x84: case 0x85: case 0x86: case 0x87:
1427 case 0x88: case 0x89: case 0x8A: case 0x8B:
1428 case 0x8C: case 0x8D: case 0x8E: case 0x8F: /* ld */
1429 cpu
.gr
[RX
] = rlat (cpu
.gr
[RD
] + ((inst
>> 2) & 0x003C));
1431 fprintf (stderr
, "load reg %d from 0x%x with 0x%x",
1433 cpu
.gr
[RD
] + ((inst
>> 2) & 0x003C), cpu
.gr
[RX
]);
1436 case 0x90: case 0x91: case 0x92: case 0x93:
1437 case 0x94: case 0x95: case 0x96: case 0x97:
1438 case 0x98: case 0x99: case 0x9A: case 0x9B:
1439 case 0x9C: case 0x9D: case 0x9E: case 0x9F: /* st */
1440 wlat (cpu
.gr
[RD
] + ((inst
>> 2) & 0x003C), cpu
.gr
[RX
]);
1442 fprintf (stderr
, "store reg %d (containing 0x%x) to 0x%x",
1444 cpu
.gr
[RD
] + ((inst
>> 2) & 0x003C));
1447 case 0xA0: case 0xA1: case 0xA2: case 0xA3:
1448 case 0xA4: case 0xA5: case 0xA6: case 0xA7:
1449 case 0xA8: case 0xA9: case 0xAA: case 0xAB:
1450 case 0xAC: case 0xAD: case 0xAE: case 0xAF: /* ld.b */
1451 cpu
.gr
[RX
] = rbat (cpu
.gr
[RD
] + RS
);
1454 case 0xB0: case 0xB1: case 0xB2: case 0xB3:
1455 case 0xB4: case 0xB5: case 0xB6: case 0xB7:
1456 case 0xB8: case 0xB9: case 0xBA: case 0xBB:
1457 case 0xBC: case 0xBD: case 0xBE: case 0xBF: /* st.b */
1458 wbat (cpu
.gr
[RD
] + RS
, cpu
.gr
[RX
]);
1461 case 0xC0: case 0xC1: case 0xC2: case 0xC3:
1462 case 0xC4: case 0xC5: case 0xC6: case 0xC7:
1463 case 0xC8: case 0xC9: case 0xCA: case 0xCB:
1464 case 0xCC: case 0xCD: case 0xCE: case 0xCF: /* ld.h */
1465 cpu
.gr
[RX
] = rhat (cpu
.gr
[RD
] + ((inst
>> 3) & 0x001E));
1468 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
1469 case 0xD4: case 0xD5: case 0xD6: case 0xD7:
1470 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1471 case 0xDC: case 0xDD: case 0xDE: case 0xDF: /* st.h */
1472 what (cpu
.gr
[RD
] + ((inst
>> 3) & 0x001E), cpu
.gr
[RX
]);
1475 case 0xE8: case 0xE9: case 0xEA: case 0xEB:
1476 case 0xEC: case 0xED: case 0xEE: case 0xEF: /* bf */
1480 disp
= inst
& 0x03FF;
1488 case 0xE0: case 0xE1: case 0xE2: case 0xE3:
1489 case 0xE4: case 0xE5: case 0xE6: case 0xE7: /* bt */
1493 disp
= inst
& 0x03FF;
1502 case 0xF8: case 0xF9: case 0xFA: case 0xFB:
1503 case 0xFC: case 0xFD: case 0xFE: case 0xFF: /* bsr */
1505 case 0xF0: case 0xF1: case 0xF2: case 0xF3:
1506 case 0xF4: case 0xF5: case 0xF6: case 0xF7: /* br */
1509 disp
= inst
& 0x03FF;
1521 fprintf (stderr
, "\n");
1525 /* Do not let him fetch from a bad address! */
1526 if (((uword
)pc
) >= cpu
.asregs
.msize
)
1529 fprintf (stderr
, "PC loaded at 0x%x is outside of available memory! (0x%x)\n", oldpc
, pc
);
1531 cpu
.asregs
.exception
= SIGSEGV
;
1535 ibuf
= rlat (pc
& 0xFFFFFFFC);
1540 while (!cpu
.asregs
.exception
);
1542 /* Hide away the things we've cached while executing. */
1543 CPU_PC_SET (scpu
, pc
);
1544 cpu
.asregs
.insts
+= insts
; /* instructions done ... */
1545 cpu
.asregs
.cycles
+= insts
; /* and each takes a cycle */
1546 cpu
.asregs
.cycles
+= bonus_cycles
; /* and extra cycles for branches */
1547 cpu
.asregs
.cycles
+= memops
* memcycles
; /* and memop cycle delays */
1552 sim_write (SIM_DESC sd
, SIM_ADDR addr
, const unsigned char *buffer
, int size
)
1557 memcpy (& cpu
.mem
[addr
], buffer
, size
);
1563 sim_read (SIM_DESC sd
, SIM_ADDR addr
, unsigned char *buffer
, int size
)
1568 memcpy (buffer
, & cpu
.mem
[addr
], size
);
1575 sim_store_register (SIM_DESC sd
, int rn
, unsigned char *memory
, int length
)
1579 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1585 /* misalignment safe */
1586 ival
= mcore_extract_unsigned_integer (memory
, 4);
1587 cpu
.asints
[rn
] = ival
;
1597 sim_fetch_register (SIM_DESC sd
, int rn
, unsigned char *memory
, int length
)
1601 if (rn
< NUM_MCORE_REGS
&& rn
>= 0)
1605 long ival
= cpu
.asints
[rn
];
1607 /* misalignment-safe */
1608 mcore_store_unsigned_integer (memory
, 4, ival
);
1618 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason
, int *sigrc
)
1620 if (cpu
.asregs
.exception
== SIGQUIT
)
1622 * reason
= sim_exited
;
1623 * sigrc
= cpu
.gr
[PARM1
];
1627 * reason
= sim_stopped
;
1628 * sigrc
= cpu
.asregs
.exception
;
1633 sim_info (SIM_DESC sd
, int verbose
)
1635 #ifdef WATCHFUNCTIONS
1638 double virttime
= cpu
.asregs
.cycles
/ 36.0e6
;
1639 host_callback
*callback
= STATE_CALLBACK (sd
);
1641 callback
->printf_filtered (callback
, "\n\n# instructions executed %10d\n",
1643 callback
->printf_filtered (callback
, "# cycles %10d\n",
1645 callback
->printf_filtered (callback
, "# pipeline stalls %10d\n",
1647 callback
->printf_filtered (callback
, "# virtual time taken %10.4f\n",
1650 #ifdef WATCHFUNCTIONS
1651 callback
->printf_filtered (callback
, "\nNumber of watched functions: %d\n",
1656 for (w
= 1; w
<= ENDWL
; w
++)
1658 callback
->printf_filtered (callback
, "WL = %s %8x\n",WLstr
[w
],WL
[w
]);
1659 callback
->printf_filtered (callback
, " calls = %d, cycles = %d\n",
1660 WLcnts
[w
],WLcyc
[w
]);
1663 callback
->printf_filtered (callback
,
1664 " maxcpc = %d, mincpc = %d, avecpc = %d\n",
1665 WLmax
[w
],WLmin
[w
],WLcyc
[w
]/WLcnts
[w
]);
1669 callback
->printf_filtered (callback
,
1670 "Total cycles for watched functions: %d\n",wcyc
);
1675 mcore_pc_get (sim_cpu
*cpu
)
1681 mcore_pc_set (sim_cpu
*cpu
, sim_cia pc
)
1687 free_state (SIM_DESC sd
)
1689 if (STATE_MODULES (sd
) != NULL
)
1690 sim_module_uninstall (sd
);
1691 sim_cpu_free_all (sd
);
1692 sim_state_free (sd
);
1696 sim_open (SIM_OPEN_KIND kind
, host_callback
*cb
, struct bfd
*abfd
, char **argv
)
1698 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
1700 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1702 /* The cpu data is kept in a separately allocated chunk of memory. */
1703 if (sim_cpu_alloc_all (sd
, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK
)
1709 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
1715 /* getopt will print the error message so we just have to exit if this fails.
1716 FIXME: Hmmm... in the case of gdb we need getopt to call
1718 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
1724 /* Check for/establish the a reference program image. */
1725 if (sim_analyze_program (sd
,
1726 (STATE_PROG_ARGV (sd
) != NULL
1727 ? *STATE_PROG_ARGV (sd
)
1728 : NULL
), abfd
) != SIM_RC_OK
)
1734 /* Configure/verify the target byte order and other runtime
1735 configuration options. */
1736 if (sim_config (sd
) != SIM_RC_OK
)
1738 sim_module_uninstall (sd
);
1742 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
1744 /* Uninstall the modules to avoid memory leaks,
1745 file descriptor leaks, etc. */
1746 sim_module_uninstall (sd
);
1750 osize
= sim_memory_size
;
1752 if (kind
== SIM_OPEN_STANDALONE
)
1755 /* Discard and reacquire memory -- start with a clean slate. */
1756 sim_size (1); /* small */
1757 sim_size (osize
); /* and back again */
1759 /* CPU specific initialization. */
1760 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1762 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
1764 CPU_PC_FETCH (cpu
) = mcore_pc_get
;
1765 CPU_PC_STORE (cpu
) = mcore_pc_set
;
1767 set_initial_gprs (cpu
); /* Reset the GPR registers. */
1774 sim_close (SIM_DESC sd
, int quitting
)
1780 sim_create_inferior (SIM_DESC sd
, struct bfd
*prog_bfd
, char **argv
, char **env
)
1782 SIM_CPU
*scpu
= STATE_CPU (sd
, 0);
1788 unsigned long strings
;
1789 unsigned long pointers
;
1790 unsigned long hi_stack
;
1793 /* Set the initial register set. */
1796 set_initial_gprs (scpu
);
1799 hi_stack
= cpu
.asregs
.msize
- 4;
1800 CPU_PC_SET (scpu
, bfd_get_start_address (prog_bfd
));
1802 /* Calculate the argument and environment strings. */
1808 l
= strlen (*avp
) + 1; /* include the null */
1809 s_length
+= (l
+ 3) & ~3; /* make it a 4 byte boundary */
1817 l
= strlen (*avp
) + 1; /* include the null */
1818 s_length
+= (l
+ 3) & ~ 3;/* make it a 4 byte boundary */
1822 /* Claim some memory for the pointers and strings. */
1823 pointers
= hi_stack
- sizeof(word
) * (nenv
+1+nargs
+1);
1824 pointers
&= ~3; /* must be 4-byte aligned */
1825 cpu
.gr
[0] = pointers
;
1827 strings
= cpu
.gr
[0] - s_length
;
1828 strings
&= ~3; /* want to make it 4-byte aligned */
1829 cpu
.gr
[0] = strings
;
1830 /* dac fix, the stack address must be 8-byte aligned! */
1831 cpu
.gr
[0] = cpu
.gr
[0] - cpu
.gr
[0] % 8;
1833 /* Loop through the arguments and fill them in. */
1834 cpu
.gr
[PARM1
] = nargs
;
1837 /* No strings to fill in. */
1842 cpu
.gr
[PARM2
] = pointers
;
1846 /* Save where we're putting it. */
1847 wlat (pointers
, strings
);
1849 /* Copy the string. */
1850 l
= strlen (* avp
) + 1;
1851 strcpy ((char *)(cpu
.mem
+ strings
), *avp
);
1853 /* Bump the pointers. */
1859 /* A null to finish the list. */
1864 /* Now do the environment pointers. */
1867 /* No strings to fill in. */
1872 cpu
.gr
[PARM3
] = pointers
;
1877 /* Save where we're putting it. */
1878 wlat (pointers
, strings
);
1880 /* Copy the string. */
1881 l
= strlen (* avp
) + 1;
1882 strcpy ((char *)(cpu
.mem
+ strings
), *avp
);
1884 /* Bump the pointers. */
1890 /* A null to finish the list. */