[ARM] "svc" insn check at irrelevant address in ARM unwind info sniffer
[binutils-gdb.git] / sim / mcore / interp.c
blob68f74f33ed94aa996cf1a6bfc0360537217bf8d0
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/>. */
20 #include "config.h"
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/times.h>
25 #include <sys/param.h>
26 #include <unistd.h>
27 #include "bfd.h"
28 #include "gdb/callback.h"
29 #include "libiberty.h"
30 #include "gdb/remote-sim.h"
32 #include "sim-main.h"
33 #include "sim-base.h"
34 #include "sim-syscall.h"
35 #include "sim-options.h"
37 #define target_big_endian (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN)
40 static unsigned long
41 mcore_extract_unsigned_integer (unsigned char *addr, int len)
43 unsigned long retval;
44 unsigned char * p;
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. */
54 retval = 0;
56 if (! target_big_endian)
58 for (p = endaddr; p > startaddr;)
59 retval = (retval << 8) | * -- p;
61 else
63 for (p = startaddr; p < endaddr;)
64 retval = (retval << 8) | * p ++;
67 return retval;
70 static void
71 mcore_store_unsigned_integer (unsigned char *addr, int len, unsigned long val)
73 unsigned char * p;
74 unsigned char * startaddr = (unsigned char *)addr;
75 unsigned char * endaddr = startaddr + len;
77 if (! target_big_endian)
79 for (p = startaddr; p < endaddr;)
81 * p ++ = val & 0xff;
82 val >>= 8;
85 else
87 for (p = endaddr; p > startaddr;)
89 * -- p = val & 0xff;
90 val >>= 8;
95 static int memcycles = 1;
97 #define gr cpu->active_gregs
98 #define cr cpu->regs.cregs
99 #define sr cr[0]
100 #define vbr cr[1]
101 #define esr cr[2]
102 #define fsr cr[3]
103 #define epc cr[4]
104 #define fpc cr[5]
105 #define ss0 cr[6]
106 #define ss1 cr[7]
107 #define ss2 cr[8]
108 #define ss3 cr[9]
109 #define ss4 cr[10]
110 #define gcr cr[11]
111 #define gsr cr[12]
113 /* maniuplate the carry bit */
114 #define C_ON() (sr & 1)
115 #define C_VALUE() (sr & 1)
116 #define C_OFF() ((sr & 1) == 0)
117 #define SET_C() {sr |= 1;}
118 #define CLR_C() {sr &= 0xfffffffe;}
119 #define NEW_C(v) {CLR_C(); sr |= ((v) & 1);}
121 #define SR_AF() ((sr >> 1) & 1)
122 static void set_active_regs (SIM_CPU *cpu)
124 if (SR_AF())
125 cpu->active_gregs = cpu->regs.alt_gregs;
126 else
127 cpu->active_gregs = cpu->regs.gregs;
130 #define TRAPCODE 1 /* r1 holds which function we want */
131 #define PARM1 2 /* first parameter */
132 #define PARM2 3
133 #define PARM3 4
134 #define PARM4 5
135 #define RET1 2 /* register for return values. */
137 /* Default to a 8 Mbyte (== 2^23) memory space. */
138 #define DEFAULT_MEMORY_SIZE 0x800000
140 static void
141 set_initial_gprs (SIM_CPU *cpu)
143 /* Set up machine just out of reset. */
144 CPU_PC_SET (cpu, 0);
145 sr = 0;
147 /* Clean out the GPRs and alternate GPRs. */
148 memset (&cpu->regs.gregs, 0, sizeof(cpu->regs.gregs));
149 memset (&cpu->regs.alt_gregs, 0, sizeof(cpu->regs.alt_gregs));
151 /* Make our register set point to the right place. */
152 set_active_regs (cpu);
154 /* ABI specifies initial values for these registers. */
155 gr[0] = DEFAULT_MEMORY_SIZE - 4;
157 /* dac fix, the stack address must be 8-byte aligned! */
158 gr[0] = gr[0] - gr[0] % 8;
159 gr[PARM1] = 0;
160 gr[PARM2] = 0;
161 gr[PARM3] = 0;
162 gr[PARM4] = gr[0];
165 /* Simulate a monitor trap. */
167 static void
168 handle_trap1 (SIM_DESC sd, SIM_CPU *cpu)
170 /* XXX: We don't pass back the actual errno value. */
171 gr[RET1] = sim_syscall (cpu, gr[TRAPCODE], gr[PARM1], gr[PARM2], gr[PARM3],
172 gr[PARM4]);
175 static void
176 process_stub (SIM_DESC sd, SIM_CPU *cpu, int what)
178 /* These values should match those in libgloss/mcore/syscalls.s. */
179 switch (what)
181 case 3: /* _read */
182 case 4: /* _write */
183 case 5: /* _open */
184 case 6: /* _close */
185 case 10: /* _unlink */
186 case 19: /* _lseek */
187 case 43: /* _times */
188 gr[TRAPCODE] = what;
189 handle_trap1 (sd, cpu);
190 break;
192 default:
193 if (STATE_VERBOSE_P (sd))
194 fprintf (stderr, "Unhandled stub opcode: %d\n", what);
195 break;
199 static void
200 util (SIM_DESC sd, SIM_CPU *cpu, unsigned what)
202 switch (what)
204 case 0: /* exit */
205 sim_engine_halt (sd, cpu, NULL, cpu->regs.pc, sim_exited, gr[PARM1]);
206 break;
208 case 1: /* printf */
209 if (STATE_VERBOSE_P (sd))
210 fprintf (stderr, "WARNING: printf unimplemented\n");
211 break;
213 case 2: /* scanf */
214 if (STATE_VERBOSE_P (sd))
215 fprintf (stderr, "WARNING: scanf unimplemented\n");
216 break;
218 case 3: /* utime */
219 gr[RET1] = cpu->insts;
220 break;
222 case 0xFF:
223 process_stub (sd, cpu, gr[1]);
224 break;
226 default:
227 if (STATE_VERBOSE_P (sd))
228 fprintf (stderr, "Unhandled util code: %x\n", what);
229 break;
233 /* For figuring out whether we carried; addc/subc use this. */
234 static int
235 iu_carry (unsigned long a, unsigned long b, int cin)
237 unsigned long x;
239 x = (a & 0xffff) + (b & 0xffff) + cin;
240 x = (x >> 16) + (a >> 16) + (b >> 16);
241 x >>= 16;
243 return (x != 0);
246 /* TODO: Convert to common watchpoints. */
247 #undef WATCHFUNCTIONS
248 #ifdef WATCHFUNCTIONS
250 #define MAXWL 80
251 word WL[MAXWL];
252 char * WLstr[MAXWL];
254 int ENDWL=0;
255 int WLincyc;
256 int WLcyc[MAXWL];
257 int WLcnts[MAXWL];
258 int WLmax[MAXWL];
259 int WLmin[MAXWL];
260 word WLendpc;
261 int WLbcyc;
262 int WLW;
263 #endif
265 #define RD (inst & 0xF)
266 #define RS ((inst >> 4) & 0xF)
267 #define RX ((inst >> 8) & 0xF)
268 #define IMM5 ((inst >> 4) & 0x1F)
269 #define IMM4 ((inst) & 0xF)
271 #define rbat(X) sim_core_read_1 (cpu, 0, read_map, X)
272 #define rhat(X) sim_core_read_2 (cpu, 0, read_map, X)
273 #define rlat(X) sim_core_read_4 (cpu, 0, read_map, X)
274 #define wbat(X, D) sim_core_write_1 (cpu, 0, write_map, X, D)
275 #define what(X, D) sim_core_write_2 (cpu, 0, write_map, X, D)
276 #define wlat(X, D) sim_core_write_4 (cpu, 0, write_map, X, D)
278 static int tracing = 0;
280 #define ILLEGAL() \
281 sim_engine_halt (sd, cpu, NULL, pc, sim_stopped, SIM_SIGILL)
283 static void
284 step_once (SIM_DESC sd, SIM_CPU *cpu)
286 int needfetch;
287 word ibuf;
288 word pc;
289 unsigned short inst;
290 int memops;
291 int bonus_cycles;
292 int insts;
293 int w;
294 int cycs;
295 #ifdef WATCHFUNCTIONS
296 word WLhash;
297 #endif
299 pc = CPU_PC_GET (cpu);
301 /* Fetch the initial instructions that we'll decode. */
302 ibuf = rlat (pc & 0xFFFFFFFC);
303 needfetch = 0;
305 memops = 0;
306 bonus_cycles = 0;
307 insts = 0;
309 /* make our register set point to the right place */
310 set_active_regs (cpu);
312 #ifdef WATCHFUNCTIONS
313 /* make a hash to speed exec loop, hope it's nonzero */
314 WLhash = 0xFFFFFFFF;
316 for (w = 1; w <= ENDWL; w++)
317 WLhash = WLhash & WL[w];
318 #endif
320 /* TODO: Unindent this block. */
322 word oldpc;
324 insts ++;
326 if (pc & 02)
328 if (! target_big_endian)
329 inst = ibuf >> 16;
330 else
331 inst = ibuf & 0xFFFF;
332 needfetch = 1;
334 else
336 if (! target_big_endian)
337 inst = ibuf & 0xFFFF;
338 else
339 inst = ibuf >> 16;
342 #ifdef WATCHFUNCTIONS
343 /* now scan list of watch addresses, if match, count it and
344 note return address and count cycles until pc=return address */
346 if ((WLincyc == 1) && (pc == WLendpc))
348 cycs = (cpu->cycles + (insts + bonus_cycles +
349 (memops * memcycles)) - WLbcyc);
351 if (WLcnts[WLW] == 1)
353 WLmax[WLW] = cycs;
354 WLmin[WLW] = cycs;
355 WLcyc[WLW] = 0;
358 if (cycs > WLmax[WLW])
360 WLmax[WLW] = cycs;
363 if (cycs < WLmin[WLW])
365 WLmin[WLW] = cycs;
368 WLcyc[WLW] += cycs;
369 WLincyc = 0;
370 WLendpc = 0;
373 /* Optimize with a hash to speed loop. */
374 if (WLincyc == 0)
376 if ((WLhash == 0) || ((WLhash & pc) != 0))
378 for (w=1; w <= ENDWL; w++)
380 if (pc == WL[w])
382 WLcnts[w]++;
383 WLbcyc = cpu->cycles + insts
384 + bonus_cycles + (memops * memcycles);
385 WLendpc = gr[15];
386 WLincyc = 1;
387 WLW = w;
388 break;
393 #endif
395 if (tracing)
396 fprintf (stderr, "%.4lx: inst = %.4x ", pc, inst);
398 oldpc = pc;
400 pc += 2;
402 switch (inst >> 8)
404 case 0x00:
405 switch RS
407 case 0x0:
408 switch RD
410 case 0x0: /* bkpt */
411 pc -= 2;
412 sim_engine_halt (sd, cpu, NULL, pc - 2,
413 sim_stopped, SIM_SIGTRAP);
414 break;
416 case 0x1: /* sync */
417 break;
419 case 0x2: /* rte */
420 pc = epc;
421 sr = esr;
422 needfetch = 1;
424 set_active_regs (cpu);
425 break;
427 case 0x3: /* rfi */
428 pc = fpc;
429 sr = fsr;
430 needfetch = 1;
432 set_active_regs (cpu);
433 break;
435 case 0x4: /* stop */
436 if (STATE_VERBOSE_P (sd))
437 fprintf (stderr, "WARNING: stop unimplemented\n");
438 break;
440 case 0x5: /* wait */
441 if (STATE_VERBOSE_P (sd))
442 fprintf (stderr, "WARNING: wait unimplemented\n");
443 break;
445 case 0x6: /* doze */
446 if (STATE_VERBOSE_P (sd))
447 fprintf (stderr, "WARNING: doze unimplemented\n");
448 break;
450 case 0x7:
451 ILLEGAL (); /* illegal */
452 break;
454 case 0x8: /* trap 0 */
455 case 0xA: /* trap 2 */
456 case 0xB: /* trap 3 */
457 sim_engine_halt (sd, cpu, NULL, pc,
458 sim_stopped, SIM_SIGTRAP);
459 break;
461 case 0xC: /* trap 4 */
462 case 0xD: /* trap 5 */
463 case 0xE: /* trap 6 */
464 ILLEGAL (); /* illegal */
465 break;
467 case 0xF: /* trap 7 */
468 sim_engine_halt (sd, cpu, NULL, pc, /* integer div-by-0 */
469 sim_stopped, SIM_SIGTRAP);
470 break;
472 case 0x9: /* trap 1 */
473 handle_trap1 (sd, cpu);
474 break;
476 break;
478 case 0x1:
479 ILLEGAL (); /* illegal */
480 break;
482 case 0x2: /* mvc */
483 gr[RD] = C_VALUE();
484 break;
485 case 0x3: /* mvcv */
486 gr[RD] = C_OFF();
487 break;
488 case 0x4: /* ldq */
490 word addr = gr[RD];
491 int regno = 4; /* always r4-r7 */
493 bonus_cycles++;
494 memops += 4;
497 gr[regno] = rlat (addr);
498 addr += 4;
499 regno++;
501 while ((regno&0x3) != 0);
503 break;
504 case 0x5: /* stq */
506 word addr = gr[RD];
507 int regno = 4; /* always r4-r7 */
509 memops += 4;
510 bonus_cycles++;
513 wlat (addr, gr[regno]);
514 addr += 4;
515 regno++;
517 while ((regno & 0x3) != 0);
519 break;
520 case 0x6: /* ldm */
522 word addr = gr[0];
523 int regno = RD;
525 /* bonus cycle is really only needed if
526 the next insn shifts the last reg loaded.
528 bonus_cycles++;
530 memops += 16-regno;
531 while (regno <= 0xF)
533 gr[regno] = rlat (addr);
534 addr += 4;
535 regno++;
538 break;
539 case 0x7: /* stm */
541 word addr = gr[0];
542 int regno = RD;
544 /* this should be removed! */
545 /* bonus_cycles ++; */
547 memops += 16 - regno;
548 while (regno <= 0xF)
550 wlat (addr, gr[regno]);
551 addr += 4;
552 regno++;
555 break;
557 case 0x8: /* dect */
558 gr[RD] -= C_VALUE();
559 break;
560 case 0x9: /* decf */
561 gr[RD] -= C_OFF();
562 break;
563 case 0xA: /* inct */
564 gr[RD] += C_VALUE();
565 break;
566 case 0xB: /* incf */
567 gr[RD] += C_OFF();
568 break;
569 case 0xC: /* jmp */
570 pc = gr[RD];
571 if (tracing && RD == 15)
572 fprintf (stderr, "Func return, r2 = %lxx, r3 = %lx\n",
573 gr[2], gr[3]);
574 bonus_cycles++;
575 needfetch = 1;
576 break;
577 case 0xD: /* jsr */
578 gr[15] = pc;
579 pc = gr[RD];
580 bonus_cycles++;
581 needfetch = 1;
582 break;
583 case 0xE: /* ff1 */
585 word tmp, i;
586 tmp = gr[RD];
587 for (i = 0; !(tmp & 0x80000000) && i < 32; i++)
588 tmp <<= 1;
589 gr[RD] = i;
591 break;
592 case 0xF: /* brev */
594 word tmp;
595 tmp = gr[RD];
596 tmp = ((tmp & 0xaaaaaaaa) >> 1) | ((tmp & 0x55555555) << 1);
597 tmp = ((tmp & 0xcccccccc) >> 2) | ((tmp & 0x33333333) << 2);
598 tmp = ((tmp & 0xf0f0f0f0) >> 4) | ((tmp & 0x0f0f0f0f) << 4);
599 tmp = ((tmp & 0xff00ff00) >> 8) | ((tmp & 0x00ff00ff) << 8);
600 gr[RD] = ((tmp & 0xffff0000) >> 16) | ((tmp & 0x0000ffff) << 16);
602 break;
604 break;
605 case 0x01:
606 switch RS
608 case 0x0: /* xtrb3 */
609 gr[1] = (gr[RD]) & 0xFF;
610 NEW_C (gr[RD] != 0);
611 break;
612 case 0x1: /* xtrb2 */
613 gr[1] = (gr[RD]>>8) & 0xFF;
614 NEW_C (gr[RD] != 0);
615 break;
616 case 0x2: /* xtrb1 */
617 gr[1] = (gr[RD]>>16) & 0xFF;
618 NEW_C (gr[RD] != 0);
619 break;
620 case 0x3: /* xtrb0 */
621 gr[1] = (gr[RD]>>24) & 0xFF;
622 NEW_C (gr[RD] != 0);
623 break;
624 case 0x4: /* zextb */
625 gr[RD] &= 0x000000FF;
626 break;
627 case 0x5: /* sextb */
629 long tmp;
630 tmp = gr[RD];
631 tmp <<= 24;
632 tmp >>= 24;
633 gr[RD] = tmp;
635 break;
636 case 0x6: /* zexth */
637 gr[RD] &= 0x0000FFFF;
638 break;
639 case 0x7: /* sexth */
641 long tmp;
642 tmp = gr[RD];
643 tmp <<= 16;
644 tmp >>= 16;
645 gr[RD] = tmp;
647 break;
648 case 0x8: /* declt */
649 --gr[RD];
650 NEW_C ((long)gr[RD] < 0);
651 break;
652 case 0x9: /* tstnbz */
654 word tmp = gr[RD];
655 NEW_C ((tmp & 0xFF000000) != 0 &&
656 (tmp & 0x00FF0000) != 0 && (tmp & 0x0000FF00) != 0 &&
657 (tmp & 0x000000FF) != 0);
659 break;
660 case 0xA: /* decgt */
661 --gr[RD];
662 NEW_C ((long)gr[RD] > 0);
663 break;
664 case 0xB: /* decne */
665 --gr[RD];
666 NEW_C ((long)gr[RD] != 0);
667 break;
668 case 0xC: /* clrt */
669 if (C_ON())
670 gr[RD] = 0;
671 break;
672 case 0xD: /* clrf */
673 if (C_OFF())
674 gr[RD] = 0;
675 break;
676 case 0xE: /* abs */
677 if (gr[RD] & 0x80000000)
678 gr[RD] = ~gr[RD] + 1;
679 break;
680 case 0xF: /* not */
681 gr[RD] = ~gr[RD];
682 break;
684 break;
685 case 0x02: /* movt */
686 if (C_ON())
687 gr[RD] = gr[RS];
688 break;
689 case 0x03: /* mult */
690 /* consume 2 bits per cycle from rs, until rs is 0 */
692 unsigned int t = gr[RS];
693 int ticks;
694 for (ticks = 0; t != 0 ; t >>= 2)
695 ticks++;
696 bonus_cycles += ticks;
698 bonus_cycles += 2; /* min. is 3, so add 2, plus ticks above */
699 if (tracing)
700 fprintf (stderr, " mult %lx by %lx to give %lx",
701 gr[RD], gr[RS], gr[RD] * gr[RS]);
702 gr[RD] = gr[RD] * gr[RS];
703 break;
704 case 0x04: /* loopt */
705 if (C_ON())
707 pc += (IMM4 << 1) - 32;
708 bonus_cycles ++;
709 needfetch = 1;
711 --gr[RS]; /* not RD! */
712 NEW_C (((long)gr[RS]) > 0);
713 break;
714 case 0x05: /* subu */
715 gr[RD] -= gr[RS];
716 break;
717 case 0x06: /* addc */
719 unsigned long tmp, a, b;
720 a = gr[RD];
721 b = gr[RS];
722 gr[RD] = a + b + C_VALUE ();
723 tmp = iu_carry (a, b, C_VALUE ());
724 NEW_C (tmp);
726 break;
727 case 0x07: /* subc */
729 unsigned long tmp, a, b;
730 a = gr[RD];
731 b = gr[RS];
732 gr[RD] = a - b + C_VALUE () - 1;
733 tmp = iu_carry (a,~b, C_VALUE ());
734 NEW_C (tmp);
736 break;
737 case 0x08: /* illegal */
738 case 0x09: /* illegal*/
739 ILLEGAL ();
740 break;
741 case 0x0A: /* movf */
742 if (C_OFF())
743 gr[RD] = gr[RS];
744 break;
745 case 0x0B: /* lsr */
747 unsigned long dst, src;
748 dst = gr[RD];
749 src = gr[RS];
750 /* We must not rely solely upon the native shift operations, since they
751 may not match the M*Core's behaviour on boundary conditions. */
752 dst = src > 31 ? 0 : dst >> src;
753 gr[RD] = dst;
755 break;
756 case 0x0C: /* cmphs */
757 NEW_C ((unsigned long )gr[RD] >=
758 (unsigned long)gr[RS]);
759 break;
760 case 0x0D: /* cmplt */
761 NEW_C ((long)gr[RD] < (long)gr[RS]);
762 break;
763 case 0x0E: /* tst */
764 NEW_C ((gr[RD] & gr[RS]) != 0);
765 break;
766 case 0x0F: /* cmpne */
767 NEW_C (gr[RD] != gr[RS]);
768 break;
769 case 0x10: case 0x11: /* mfcr */
771 unsigned r;
772 r = IMM5;
773 if (r <= LAST_VALID_CREG)
774 gr[RD] = cr[r];
775 else
776 ILLEGAL ();
778 break;
780 case 0x12: /* mov */
781 gr[RD] = gr[RS];
782 if (tracing)
783 fprintf (stderr, "MOV %lx into reg %d", gr[RD], RD);
784 break;
786 case 0x13: /* bgenr */
787 if (gr[RS] & 0x20)
788 gr[RD] = 0;
789 else
790 gr[RD] = 1 << (gr[RS] & 0x1F);
791 break;
793 case 0x14: /* rsub */
794 gr[RD] = gr[RS] - gr[RD];
795 break;
797 case 0x15: /* ixw */
798 gr[RD] += gr[RS]<<2;
799 break;
801 case 0x16: /* and */
802 gr[RD] &= gr[RS];
803 break;
805 case 0x17: /* xor */
806 gr[RD] ^= gr[RS];
807 break;
809 case 0x18: case 0x19: /* mtcr */
811 unsigned r;
812 r = IMM5;
813 if (r <= LAST_VALID_CREG)
814 cr[r] = gr[RD];
815 else
816 ILLEGAL ();
818 /* we might have changed register sets... */
819 set_active_regs (cpu);
821 break;
823 case 0x1A: /* asr */
824 /* We must not rely solely upon the native shift operations, since they
825 may not match the M*Core's behaviour on boundary conditions. */
826 if (gr[RS] > 30)
827 gr[RD] = ((long) gr[RD]) < 0 ? -1 : 0;
828 else
829 gr[RD] = (long) gr[RD] >> gr[RS];
830 break;
832 case 0x1B: /* lsl */
833 /* We must not rely solely upon the native shift operations, since they
834 may not match the M*Core's behaviour on boundary conditions. */
835 gr[RD] = gr[RS] > 31 ? 0 : gr[RD] << gr[RS];
836 break;
838 case 0x1C: /* addu */
839 gr[RD] += gr[RS];
840 break;
842 case 0x1D: /* ixh */
843 gr[RD] += gr[RS] << 1;
844 break;
846 case 0x1E: /* or */
847 gr[RD] |= gr[RS];
848 break;
850 case 0x1F: /* andn */
851 gr[RD] &= ~gr[RS];
852 break;
853 case 0x20: case 0x21: /* addi */
854 gr[RD] =
855 gr[RD] + (IMM5 + 1);
856 break;
857 case 0x22: case 0x23: /* cmplti */
859 int tmp = (IMM5 + 1);
860 if (gr[RD] < tmp)
862 SET_C();
864 else
866 CLR_C();
869 break;
870 case 0x24: case 0x25: /* subi */
871 gr[RD] =
872 gr[RD] - (IMM5 + 1);
873 break;
874 case 0x26: case 0x27: /* illegal */
875 ILLEGAL ();
876 break;
877 case 0x28: case 0x29: /* rsubi */
878 gr[RD] =
879 IMM5 - gr[RD];
880 break;
881 case 0x2A: case 0x2B: /* cmpnei */
882 if (gr[RD] != IMM5)
884 SET_C();
886 else
888 CLR_C();
890 break;
892 case 0x2C: case 0x2D: /* bmaski, divu */
894 unsigned imm = IMM5;
896 if (imm == 1)
898 int exe;
899 int rxnlz, r1nlz;
900 unsigned int rx, r1;
902 rx = gr[RD];
903 r1 = gr[1];
904 exe = 0;
906 /* unsigned divide */
907 gr[RD] = (word) ((unsigned int) gr[RD] / (unsigned int)gr[1] );
909 /* compute bonus_cycles for divu */
910 for (r1nlz = 0; ((r1 & 0x80000000) == 0) && (r1nlz < 32); r1nlz ++)
911 r1 = r1 << 1;
913 for (rxnlz = 0; ((rx & 0x80000000) == 0) && (rxnlz < 32); rxnlz ++)
914 rx = rx << 1;
916 if (r1nlz < rxnlz)
917 exe += 4;
918 else
919 exe += 5 + r1nlz - rxnlz;
921 if (exe >= (2 * memcycles - 1))
923 bonus_cycles += exe - (2 * memcycles) + 1;
926 else if (imm == 0 || imm >= 8)
928 /* bmaski */
929 if (imm == 0)
930 gr[RD] = -1;
931 else
932 gr[RD] = (1 << imm) - 1;
934 else
936 /* illegal */
937 ILLEGAL ();
940 break;
941 case 0x2E: case 0x2F: /* andi */
942 gr[RD] = gr[RD] & IMM5;
943 break;
944 case 0x30: case 0x31: /* bclri */
945 gr[RD] = gr[RD] & ~(1<<IMM5);
946 break;
947 case 0x32: case 0x33: /* bgeni, divs */
949 unsigned imm = IMM5;
950 if (imm == 1)
952 int exe,sc;
953 int rxnlz, r1nlz;
954 signed int rx, r1;
956 /* compute bonus_cycles for divu */
957 rx = gr[RD];
958 r1 = gr[1];
959 exe = 0;
961 if (((rx < 0) && (r1 > 0)) || ((rx >= 0) && (r1 < 0)))
962 sc = 1;
963 else
964 sc = 0;
966 rx = abs (rx);
967 r1 = abs (r1);
969 /* signed divide, general registers are of type int, so / op is OK */
970 gr[RD] = gr[RD] / gr[1];
972 for (r1nlz = 0; ((r1 & 0x80000000) == 0) && (r1nlz < 32) ; r1nlz ++ )
973 r1 = r1 << 1;
975 for (rxnlz = 0; ((rx & 0x80000000) == 0) && (rxnlz < 32) ; rxnlz ++ )
976 rx = rx << 1;
978 if (r1nlz < rxnlz)
979 exe += 5;
980 else
981 exe += 6 + r1nlz - rxnlz + sc;
983 if (exe >= (2 * memcycles - 1))
985 bonus_cycles += exe - (2 * memcycles) + 1;
988 else if (imm >= 7)
990 /* bgeni */
991 gr[RD] = (1 << IMM5);
993 else
995 /* illegal */
996 ILLEGAL ();
998 break;
1000 case 0x34: case 0x35: /* bseti */
1001 gr[RD] = gr[RD] | (1 << IMM5);
1002 break;
1003 case 0x36: case 0x37: /* btsti */
1004 NEW_C (gr[RD] >> IMM5);
1005 break;
1006 case 0x38: case 0x39: /* xsr, rotli */
1008 unsigned imm = IMM5;
1009 unsigned long tmp = gr[RD];
1010 if (imm == 0)
1012 word cbit;
1013 cbit = C_VALUE();
1014 NEW_C (tmp);
1015 gr[RD] = (cbit << 31) | (tmp >> 1);
1017 else
1018 gr[RD] = (tmp << imm) | (tmp >> (32 - imm));
1020 break;
1021 case 0x3A: case 0x3B: /* asrc, asri */
1023 unsigned imm = IMM5;
1024 long tmp = gr[RD];
1025 if (imm == 0)
1027 NEW_C (tmp);
1028 gr[RD] = tmp >> 1;
1030 else
1031 gr[RD] = tmp >> imm;
1033 break;
1034 case 0x3C: case 0x3D: /* lslc, lsli */
1036 unsigned imm = IMM5;
1037 unsigned long tmp = gr[RD];
1038 if (imm == 0)
1040 NEW_C (tmp >> 31);
1041 gr[RD] = tmp << 1;
1043 else
1044 gr[RD] = tmp << imm;
1046 break;
1047 case 0x3E: case 0x3F: /* lsrc, lsri */
1049 unsigned imm = IMM5;
1050 unsigned long tmp = gr[RD];
1051 if (imm == 0)
1053 NEW_C (tmp);
1054 gr[RD] = tmp >> 1;
1056 else
1057 gr[RD] = tmp >> imm;
1059 break;
1060 case 0x40: case 0x41: case 0x42: case 0x43:
1061 case 0x44: case 0x45: case 0x46: case 0x47:
1062 case 0x48: case 0x49: case 0x4A: case 0x4B:
1063 case 0x4C: case 0x4D: case 0x4E: case 0x4F:
1064 ILLEGAL ();
1065 break;
1066 case 0x50:
1067 util (sd, cpu, inst & 0xFF);
1068 break;
1069 case 0x51: case 0x52: case 0x53:
1070 case 0x54: case 0x55: case 0x56: case 0x57:
1071 case 0x58: case 0x59: case 0x5A: case 0x5B:
1072 case 0x5C: case 0x5D: case 0x5E: case 0x5F:
1073 ILLEGAL ();
1074 break;
1075 case 0x60: case 0x61: case 0x62: case 0x63: /* movi */
1076 case 0x64: case 0x65: case 0x66: case 0x67:
1077 gr[RD] = (inst >> 4) & 0x7F;
1078 break;
1079 case 0x68: case 0x69: case 0x6A: case 0x6B:
1080 case 0x6C: case 0x6D: case 0x6E: case 0x6F: /* illegal */
1081 ILLEGAL ();
1082 break;
1083 case 0x71: case 0x72: case 0x73:
1084 case 0x74: case 0x75: case 0x76: case 0x77:
1085 case 0x78: case 0x79: case 0x7A: case 0x7B:
1086 case 0x7C: case 0x7D: case 0x7E: /* lrw */
1087 gr[RX] = rlat ((pc + ((inst & 0xFF) << 2)) & 0xFFFFFFFC);
1088 if (tracing)
1089 fprintf (stderr, "LRW of 0x%x from 0x%lx to reg %d",
1090 rlat ((pc + ((inst & 0xFF) << 2)) & 0xFFFFFFFC),
1091 (pc + ((inst & 0xFF) << 2)) & 0xFFFFFFFC, RX);
1092 memops++;
1093 break;
1094 case 0x7F: /* jsri */
1095 gr[15] = pc;
1096 if (tracing)
1097 fprintf (stderr,
1098 "func call: r2 = %lx r3 = %lx r4 = %lx r5 = %lx r6 = %lx r7 = %lx\n",
1099 gr[2], gr[3], gr[4], gr[5], gr[6], gr[7]);
1100 case 0x70: /* jmpi */
1101 pc = rlat ((pc + ((inst & 0xFF) << 2)) & 0xFFFFFFFC);
1102 memops++;
1103 bonus_cycles++;
1104 needfetch = 1;
1105 break;
1107 case 0x80: case 0x81: case 0x82: case 0x83:
1108 case 0x84: case 0x85: case 0x86: case 0x87:
1109 case 0x88: case 0x89: case 0x8A: case 0x8B:
1110 case 0x8C: case 0x8D: case 0x8E: case 0x8F: /* ld */
1111 gr[RX] = rlat (gr[RD] + ((inst >> 2) & 0x003C));
1112 if (tracing)
1113 fprintf (stderr, "load reg %d from 0x%lx with 0x%lx",
1115 gr[RD] + ((inst >> 2) & 0x003C), gr[RX]);
1116 memops++;
1117 break;
1118 case 0x90: case 0x91: case 0x92: case 0x93:
1119 case 0x94: case 0x95: case 0x96: case 0x97:
1120 case 0x98: case 0x99: case 0x9A: case 0x9B:
1121 case 0x9C: case 0x9D: case 0x9E: case 0x9F: /* st */
1122 wlat (gr[RD] + ((inst >> 2) & 0x003C), gr[RX]);
1123 if (tracing)
1124 fprintf (stderr, "store reg %d (containing 0x%lx) to 0x%lx",
1125 RX, gr[RX],
1126 gr[RD] + ((inst >> 2) & 0x003C));
1127 memops++;
1128 break;
1129 case 0xA0: case 0xA1: case 0xA2: case 0xA3:
1130 case 0xA4: case 0xA5: case 0xA6: case 0xA7:
1131 case 0xA8: case 0xA9: case 0xAA: case 0xAB:
1132 case 0xAC: case 0xAD: case 0xAE: case 0xAF: /* ld.b */
1133 gr[RX] = rbat (gr[RD] + RS);
1134 memops++;
1135 break;
1136 case 0xB0: case 0xB1: case 0xB2: case 0xB3:
1137 case 0xB4: case 0xB5: case 0xB6: case 0xB7:
1138 case 0xB8: case 0xB9: case 0xBA: case 0xBB:
1139 case 0xBC: case 0xBD: case 0xBE: case 0xBF: /* st.b */
1140 wbat (gr[RD] + RS, gr[RX]);
1141 memops++;
1142 break;
1143 case 0xC0: case 0xC1: case 0xC2: case 0xC3:
1144 case 0xC4: case 0xC5: case 0xC6: case 0xC7:
1145 case 0xC8: case 0xC9: case 0xCA: case 0xCB:
1146 case 0xCC: case 0xCD: case 0xCE: case 0xCF: /* ld.h */
1147 gr[RX] = rhat (gr[RD] + ((inst >> 3) & 0x001E));
1148 memops++;
1149 break;
1150 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
1151 case 0xD4: case 0xD5: case 0xD6: case 0xD7:
1152 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1153 case 0xDC: case 0xDD: case 0xDE: case 0xDF: /* st.h */
1154 what (gr[RD] + ((inst >> 3) & 0x001E), gr[RX]);
1155 memops++;
1156 break;
1157 case 0xE8: case 0xE9: case 0xEA: case 0xEB:
1158 case 0xEC: case 0xED: case 0xEE: case 0xEF: /* bf */
1159 if (C_OFF())
1161 int disp;
1162 disp = inst & 0x03FF;
1163 if (inst & 0x0400)
1164 disp |= 0xFFFFFC00;
1165 pc += disp<<1;
1166 bonus_cycles++;
1167 needfetch = 1;
1169 break;
1170 case 0xE0: case 0xE1: case 0xE2: case 0xE3:
1171 case 0xE4: case 0xE5: case 0xE6: case 0xE7: /* bt */
1172 if (C_ON())
1174 int disp;
1175 disp = inst & 0x03FF;
1176 if (inst & 0x0400)
1177 disp |= 0xFFFFFC00;
1178 pc += disp<<1;
1179 bonus_cycles++;
1180 needfetch = 1;
1182 break;
1184 case 0xF8: case 0xF9: case 0xFA: case 0xFB:
1185 case 0xFC: case 0xFD: case 0xFE: case 0xFF: /* bsr */
1186 gr[15] = pc;
1187 case 0xF0: case 0xF1: case 0xF2: case 0xF3:
1188 case 0xF4: case 0xF5: case 0xF6: case 0xF7: /* br */
1190 int disp;
1191 disp = inst & 0x03FF;
1192 if (inst & 0x0400)
1193 disp |= 0xFFFFFC00;
1194 pc += disp<<1;
1195 bonus_cycles++;
1196 needfetch = 1;
1198 break;
1202 if (tracing)
1203 fprintf (stderr, "\n");
1205 if (needfetch)
1207 ibuf = rlat (pc & 0xFFFFFFFC);
1208 needfetch = 0;
1212 /* Hide away the things we've cached while executing. */
1213 CPU_PC_SET (cpu, pc);
1214 cpu->insts += insts; /* instructions done ... */
1215 cpu->cycles += insts; /* and each takes a cycle */
1216 cpu->cycles += bonus_cycles; /* and extra cycles for branches */
1217 cpu->cycles += memops * memcycles; /* and memop cycle delays */
1220 void
1221 sim_engine_run (SIM_DESC sd,
1222 int next_cpu_nr, /* ignore */
1223 int nr_cpus, /* ignore */
1224 int siggnal) /* ignore */
1226 sim_cpu *cpu;
1228 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1230 cpu = STATE_CPU (sd, 0);
1232 while (1)
1234 step_once (sd, cpu);
1235 if (sim_events_tick (sd))
1236 sim_events_process (sd);
1240 static int
1241 mcore_reg_store (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
1243 if (rn < NUM_MCORE_REGS && rn >= 0)
1245 if (length == 4)
1247 long ival;
1249 /* misalignment safe */
1250 ival = mcore_extract_unsigned_integer (memory, 4);
1251 cpu->asints[rn] = ival;
1254 return 4;
1256 else
1257 return 0;
1260 static int
1261 mcore_reg_fetch (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
1263 if (rn < NUM_MCORE_REGS && rn >= 0)
1265 if (length == 4)
1267 long ival = cpu->asints[rn];
1269 /* misalignment-safe */
1270 mcore_store_unsigned_integer (memory, 4, ival);
1273 return 4;
1275 else
1276 return 0;
1279 void
1280 sim_info (SIM_DESC sd, int verbose)
1282 SIM_CPU *cpu = STATE_CPU (sd, 0);
1283 #ifdef WATCHFUNCTIONS
1284 int w, wcyc;
1285 #endif
1286 double virttime = cpu->cycles / 36.0e6;
1287 host_callback *callback = STATE_CALLBACK (sd);
1289 callback->printf_filtered (callback, "\n\n# instructions executed %10d\n",
1290 cpu->insts);
1291 callback->printf_filtered (callback, "# cycles %10d\n",
1292 cpu->cycles);
1293 callback->printf_filtered (callback, "# pipeline stalls %10d\n",
1294 cpu->stalls);
1295 callback->printf_filtered (callback, "# virtual time taken %10.4f\n",
1296 virttime);
1298 #ifdef WATCHFUNCTIONS
1299 callback->printf_filtered (callback, "\nNumber of watched functions: %d\n",
1300 ENDWL);
1302 wcyc = 0;
1304 for (w = 1; w <= ENDWL; w++)
1306 callback->printf_filtered (callback, "WL = %s %8x\n",WLstr[w],WL[w]);
1307 callback->printf_filtered (callback, " calls = %d, cycles = %d\n",
1308 WLcnts[w],WLcyc[w]);
1310 if (WLcnts[w] != 0)
1311 callback->printf_filtered (callback,
1312 " maxcpc = %d, mincpc = %d, avecpc = %d\n",
1313 WLmax[w],WLmin[w],WLcyc[w]/WLcnts[w]);
1314 wcyc += WLcyc[w];
1317 callback->printf_filtered (callback,
1318 "Total cycles for watched functions: %d\n",wcyc);
1319 #endif
1322 static sim_cia
1323 mcore_pc_get (sim_cpu *cpu)
1325 return cpu->regs.pc;
1328 static void
1329 mcore_pc_set (sim_cpu *cpu, sim_cia pc)
1331 cpu->regs.pc = pc;
1334 static void
1335 free_state (SIM_DESC sd)
1337 if (STATE_MODULES (sd) != NULL)
1338 sim_module_uninstall (sd);
1339 sim_cpu_free_all (sd);
1340 sim_state_free (sd);
1343 SIM_DESC
1344 sim_open (SIM_OPEN_KIND kind, host_callback *cb, struct bfd *abfd, char **argv)
1346 int i;
1347 SIM_DESC sd = sim_state_alloc (kind, cb);
1348 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1350 /* The cpu data is kept in a separately allocated chunk of memory. */
1351 if (sim_cpu_alloc_all (sd, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK)
1353 free_state (sd);
1354 return 0;
1357 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
1359 free_state (sd);
1360 return 0;
1363 /* getopt will print the error message so we just have to exit if this fails.
1364 FIXME: Hmmm... in the case of gdb we need getopt to call
1365 print_filtered. */
1366 if (sim_parse_args (sd, argv) != SIM_RC_OK)
1368 free_state (sd);
1369 return 0;
1372 /* Check for/establish the a reference program image. */
1373 if (sim_analyze_program (sd,
1374 (STATE_PROG_ARGV (sd) != NULL
1375 ? *STATE_PROG_ARGV (sd)
1376 : NULL), abfd) != SIM_RC_OK)
1378 free_state (sd);
1379 return 0;
1382 /* Configure/verify the target byte order and other runtime
1383 configuration options. */
1384 if (sim_config (sd) != SIM_RC_OK)
1386 sim_module_uninstall (sd);
1387 return 0;
1390 if (sim_post_argv_init (sd) != SIM_RC_OK)
1392 /* Uninstall the modules to avoid memory leaks,
1393 file descriptor leaks, etc. */
1394 sim_module_uninstall (sd);
1395 return 0;
1398 /* CPU specific initialization. */
1399 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1401 SIM_CPU *cpu = STATE_CPU (sd, i);
1403 CPU_REG_FETCH (cpu) = mcore_reg_fetch;
1404 CPU_REG_STORE (cpu) = mcore_reg_store;
1405 CPU_PC_FETCH (cpu) = mcore_pc_get;
1406 CPU_PC_STORE (cpu) = mcore_pc_set;
1408 set_initial_gprs (cpu); /* Reset the GPR registers. */
1411 /* Default to a 8 Mbyte (== 2^23) memory space. */
1412 sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEMORY_SIZE);
1414 return sd;
1417 SIM_RC
1418 sim_create_inferior (SIM_DESC sd, struct bfd *prog_bfd, char **argv, char **env)
1420 SIM_CPU *cpu = STATE_CPU (sd, 0);
1421 char ** avp;
1422 int nargs = 0;
1423 int nenv = 0;
1424 int s_length;
1425 int l;
1426 unsigned long strings;
1427 unsigned long pointers;
1428 unsigned long hi_stack;
1431 /* Set the initial register set. */
1432 set_initial_gprs (cpu);
1434 hi_stack = DEFAULT_MEMORY_SIZE - 4;
1435 CPU_PC_SET (cpu, bfd_get_start_address (prog_bfd));
1437 /* Calculate the argument and environment strings. */
1438 s_length = 0;
1439 nargs = 0;
1440 avp = argv;
1441 while (avp && *avp)
1443 l = strlen (*avp) + 1; /* include the null */
1444 s_length += (l + 3) & ~3; /* make it a 4 byte boundary */
1445 nargs++; avp++;
1448 nenv = 0;
1449 avp = env;
1450 while (avp && *avp)
1452 l = strlen (*avp) + 1; /* include the null */
1453 s_length += (l + 3) & ~ 3;/* make it a 4 byte boundary */
1454 nenv++; avp++;
1457 /* Claim some memory for the pointers and strings. */
1458 pointers = hi_stack - sizeof(word) * (nenv+1+nargs+1);
1459 pointers &= ~3; /* must be 4-byte aligned */
1460 gr[0] = pointers;
1462 strings = gr[0] - s_length;
1463 strings &= ~3; /* want to make it 4-byte aligned */
1464 gr[0] = strings;
1465 /* dac fix, the stack address must be 8-byte aligned! */
1466 gr[0] = gr[0] - gr[0] % 8;
1468 /* Loop through the arguments and fill them in. */
1469 gr[PARM1] = nargs;
1470 if (nargs == 0)
1472 /* No strings to fill in. */
1473 gr[PARM2] = 0;
1475 else
1477 gr[PARM2] = pointers;
1478 avp = argv;
1479 while (avp && *avp)
1481 /* Save where we're putting it. */
1482 wlat (pointers, strings);
1484 /* Copy the string. */
1485 l = strlen (* avp) + 1;
1486 sim_core_write_buffer (sd, cpu, write_map, *avp, strings, l);
1488 /* Bump the pointers. */
1489 avp++;
1490 pointers += 4;
1491 strings += l+1;
1494 /* A null to finish the list. */
1495 wlat (pointers, 0);
1496 pointers += 4;
1499 /* Now do the environment pointers. */
1500 if (nenv == 0)
1502 /* No strings to fill in. */
1503 gr[PARM3] = 0;
1505 else
1507 gr[PARM3] = pointers;
1508 avp = env;
1510 while (avp && *avp)
1512 /* Save where we're putting it. */
1513 wlat (pointers, strings);
1515 /* Copy the string. */
1516 l = strlen (* avp) + 1;
1517 sim_core_write_buffer (sd, cpu, write_map, *avp, strings, l);
1519 /* Bump the pointers. */
1520 avp++;
1521 pointers += 4;
1522 strings += l+1;
1525 /* A null to finish the list. */
1526 wlat (pointers, 0);
1527 pointers += 4;
1530 return SIM_RC_OK;