2 /* Simulator for the MIPS architecture.
4 This file is part of the MIPS sim
6 THIS SOFTWARE IS NOT COPYRIGHTED
8 Cygnus offers the following for use in the public domain. Cygnus
9 makes no warranty with regard to the software or it's performance
10 and the user accepts the software "AS IS" with all faults.
12 CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
13 THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 The IDT monitor (found on the VR4300 board), seems to lie about
19 register contents. It seems to treat the registers as sign-extended
20 32-bit values. This cause *REAL* problems when single-stepping 64-bit
25 /* The TRACE manifests enable the provision of extra features. If they
26 are not defined then a simpler (quicker) simulator is constructed
27 without the required run-time checks, etc. */
28 #if 1 /* 0 to allow user build selection, 1 to force inclusion */
34 #include "sim-utils.h"
35 #include "sim-options.h"
36 #include "sim-assert.h"
62 #include "libiberty.h"
64 #include "gdb/callback.h" /* GDB simulator callback interface */
65 #include "gdb/remote-sim.h" /* GDB simulator interface */
73 char* pr_addr PARAMS ((SIM_ADDR addr));
74 char* pr_uword64 PARAMS ((uword64 addr));
77 /* Within interp.c we refer to the sim_state and sim_cpu directly. */
82 /* The following reserved instruction value is used when a simulator
83 trap is required. NOTE: Care must be taken, since this value may be
84 used in later revisions of the MIPS ISA. */
86 #define RSVD_INSTRUCTION (0x00000005)
87 #define RSVD_INSTRUCTION_MASK (0xFC00003F)
89 #define RSVD_INSTRUCTION_ARG_SHIFT 6
90 #define RSVD_INSTRUCTION_ARG_MASK 0xFFFFF
93 /* Bits in the Debug register */
94 #define Debug_DBD 0x80000000 /* Debug Branch Delay */
95 #define Debug_DM 0x40000000 /* Debug Mode */
96 #define Debug_DBp 0x00000002 /* Debug Breakpoint indicator */
98 /*---------------------------------------------------------------------------*/
99 /*-- GDB simulator interface ------------------------------------------------*/
100 /*---------------------------------------------------------------------------*/
102 static void ColdReset PARAMS((SIM_DESC sd));
104 /*---------------------------------------------------------------------------*/
108 #define DELAYSLOT() {\
109 if (STATE & simDELAYSLOT)\
110 sim_io_eprintf(sd,"Delay slot already activated (branch in delay slot?)\n");\
111 STATE |= simDELAYSLOT;\
114 #define JALDELAYSLOT() {\
116 STATE |= simJALDELAYSLOT;\
120 STATE &= ~simDELAYSLOT;\
121 STATE |= simSKIPNEXT;\
124 #define CANCELDELAYSLOT() {\
126 STATE &= ~(simDELAYSLOT | simJALDELAYSLOT);\
129 #define INDELAYSLOT() ((STATE & simDELAYSLOT) != 0)
130 #define INJALDELAYSLOT() ((STATE & simJALDELAYSLOT) != 0)
132 /* Note that the monitor code essentially assumes this layout of memory.
133 If you change these, change the monitor code, too. */
134 /* FIXME Currently addresses are truncated to 32-bits, see
135 mips/sim-main.c:address_translation(). If that changes, then these
136 values will need to be extended, and tested for more carefully. */
137 #define K0BASE (0x80000000)
138 #define K0SIZE (0x20000000)
139 #define K1BASE (0xA0000000)
140 #define K1SIZE (0x20000000)
142 /* Simple run-time monitor support.
144 We emulate the monitor by placing magic reserved instructions at
145 the monitor's entry points; when we hit these instructions, instead
146 of raising an exception (as we would normally), we look at the
147 instruction and perform the appropriate monitory operation.
149 `*_monitor_base' are the physical addresses at which the corresponding
150 monitor vectors are located. `0' means none. By default,
152 The RSVD_INSTRUCTION... macros specify the magic instructions we
153 use at the monitor entry points. */
154 static int firmware_option_p = 0;
155 static SIM_ADDR idt_monitor_base = 0xBFC00000;
156 static SIM_ADDR pmon_monitor_base = 0xBFC00500;
157 static SIM_ADDR lsipmon_monitor_base = 0xBFC00200;
159 static SIM_RC sim_firmware_command (SIM_DESC sd, char* arg);
162 #define MEM_SIZE (8 << 20) /* 8 MBytes */
166 static char *tracefile = "trace.din"; /* default filename for trace log */
167 FILE *tracefh = NULL;
168 static void open_trace PARAMS((SIM_DESC sd));
171 static const char * get_insn_name (sim_cpu *, int);
173 /* simulation target board. NULL=canonical */
174 static char* board = NULL;
177 static DECLARE_OPTION_HANDLER (mips_option_handler);
180 OPTION_DINERO_TRACE = OPTION_START,
188 mips_option_handler (sd, cpu, opt, arg, is_command)
198 case OPTION_DINERO_TRACE: /* ??? */
200 /* Eventually the simTRACE flag could be treated as a toggle, to
201 allow external control of the program points being traced
202 (i.e. only from main onwards, excluding the run-time setup,
204 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
206 sim_cpu *cpu = STATE_CPU (sd, cpu_nr);
209 else if (strcmp (arg, "yes") == 0)
211 else if (strcmp (arg, "no") == 0)
213 else if (strcmp (arg, "on") == 0)
215 else if (strcmp (arg, "off") == 0)
219 fprintf (stderr, "Unrecognized dinero-trace option `%s'\n", arg);
226 Simulator constructed without dinero tracing support (for performance).\n\
227 Re-compile simulator with \"-DTRACE\" to enable this option.\n");
231 case OPTION_DINERO_FILE:
233 if (optarg != NULL) {
235 tmp = (char *)malloc(strlen(optarg) + 1);
238 sim_io_printf(sd,"Failed to allocate buffer for tracefile name \"%s\"\n",optarg);
244 sim_io_printf(sd,"Placing trace information into file \"%s\"\n",tracefile);
250 case OPTION_FIRMWARE:
251 return sim_firmware_command (sd, arg);
257 board = zalloc(strlen(arg) + 1);
268 static const OPTION mips_options[] =
270 { {"dinero-trace", optional_argument, NULL, OPTION_DINERO_TRACE},
271 '\0', "on|off", "Enable dinero tracing",
272 mips_option_handler },
273 { {"dinero-file", required_argument, NULL, OPTION_DINERO_FILE},
274 '\0', "FILE", "Write dinero trace to FILE",
275 mips_option_handler },
276 { {"firmware", required_argument, NULL, OPTION_FIRMWARE},
277 '\0', "[idt|pmon|lsipmon|none][@ADDRESS]", "Emulate ROM monitor",
278 mips_option_handler },
279 { {"board", required_argument, NULL, OPTION_BOARD},
280 '\0', "none" /* rely on compile-time string concatenation for other options */
282 #define BOARD_JMR3904 "jmr3904"
284 #define BOARD_JMR3904_PAL "jmr3904pal"
285 "|" BOARD_JMR3904_PAL
286 #define BOARD_JMR3904_DEBUG "jmr3904debug"
287 "|" BOARD_JMR3904_DEBUG
288 #define BOARD_BSP "bsp"
291 , "Customize simulation for a particular board.", mips_option_handler },
293 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
297 int interrupt_pending;
300 interrupt_event (SIM_DESC sd, void *data)
302 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
303 address_word cia = CIA_GET (cpu);
306 interrupt_pending = 0;
307 SignalExceptionInterrupt (1); /* interrupt "1" */
309 else if (!interrupt_pending)
310 sim_events_schedule (sd, 1, interrupt_event, data);
314 /*---------------------------------------------------------------------------*/
315 /*-- Device registration hook -----------------------------------------------*/
316 /*---------------------------------------------------------------------------*/
317 static void device_init(SIM_DESC sd) {
319 extern void register_devices(SIM_DESC);
320 register_devices(sd);
324 /*---------------------------------------------------------------------------*/
325 /*-- GDB simulator interface ------------------------------------------------*/
326 /*---------------------------------------------------------------------------*/
329 sim_open (kind, cb, abfd, argv)
335 SIM_DESC sd = sim_state_alloc (kind, cb);
336 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
338 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
340 /* FIXME: watchpoints code shouldn't need this */
341 STATE_WATCHPOINTS (sd)->pc = &(PC);
342 STATE_WATCHPOINTS (sd)->sizeof_pc = sizeof (PC);
343 STATE_WATCHPOINTS (sd)->interrupt_handler = interrupt_event;
345 /* Initialize the mechanism for doing insn profiling. */
346 CPU_INSN_NAME (cpu) = get_insn_name;
347 CPU_MAX_INSNS (cpu) = nr_itable_entries;
351 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
353 sim_add_option_table (sd, NULL, mips_options);
356 /* getopt will print the error message so we just have to exit if this fails.
357 FIXME: Hmmm... in the case of gdb we need getopt to call
359 if (sim_parse_args (sd, argv) != SIM_RC_OK)
361 /* Uninstall the modules to avoid memory leaks,
362 file descriptor leaks, etc. */
363 sim_module_uninstall (sd);
367 /* handle board-specific memory maps */
370 /* Allocate core managed memory */
371 sim_memopt *entry, *match = NULL;
372 address_word mem_size = 0;
375 /* For compatibility with the old code - under this (at level one)
376 are the kernel spaces K0 & K1. Both of these map to a single
377 smaller sub region */
378 sim_do_command(sd," memory region 0x7fff8000,0x8000") ; /* MTZ- 32 k stack */
380 /* Look for largest memory region defined on command-line at
382 #ifdef SIM_HAVE_FLATMEM
383 mem_size = STATE_MEM_SIZE (sd);
385 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
387 /* If we find an entry at address 0, then we will end up
388 allocating a new buffer in the "memory alias" command
389 below. The region at address 0 will be deleted. */
390 address_word size = (entry->modulo != 0
391 ? entry->modulo : entry->nr_bytes);
393 && (!match || entry->level < match->level))
395 else if (entry->addr == K0BASE || entry->addr == K1BASE)
400 for (alias = entry->alias; alias != NULL; alias = alias->next)
403 && (!match || entry->level < match->level))
405 else if (alias->addr == K0BASE || alias->addr == K1BASE)
415 /* Get existing memory region size. */
416 mem_size = (match->modulo != 0
417 ? match->modulo : match->nr_bytes);
418 /* Delete old region. */
419 sim_do_commandf (sd, "memory delete %d:0x%lx@%d",
420 match->space, match->addr, match->level);
422 else if (mem_size == 0)
424 /* Limit to KSEG1 size (512MB) */
425 if (mem_size > K1SIZE)
427 /* memory alias K1BASE@1,K1SIZE%MEMSIZE,K0BASE */
428 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx%%0x%lx,0x%0x",
429 K1BASE, K1SIZE, (long)mem_size, K0BASE);
434 else if (board != NULL
435 && (strcmp(board, BOARD_BSP) == 0))
439 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
441 /* ROM: 0x9FC0_0000 - 0x9FFF_FFFF and 0xBFC0_0000 - 0xBFFF_FFFF */
442 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx,0x%0x",
444 4 * 1024 * 1024, /* 4 MB */
447 /* SRAM: 0x8000_0000 - 0x803F_FFFF and 0xA000_0000 - 0xA03F_FFFF */
448 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx,0x%0x",
450 4 * 1024 * 1024, /* 4 MB */
453 /* DRAM: 0x8800_0000 - 0x89FF_FFFF and 0xA800_0000 - 0xA9FF_FFFF */
454 for (i=0; i<8; i++) /* 32 MB total */
456 unsigned size = 4 * 1024 * 1024; /* 4 MB */
457 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx,0x%0x",
458 0x88000000 + (i * size),
460 0xA8000000 + (i * size));
464 else if (board != NULL
465 && (strcmp(board, BOARD_JMR3904) == 0 ||
466 strcmp(board, BOARD_JMR3904_PAL) == 0 ||
467 strcmp(board, BOARD_JMR3904_DEBUG) == 0))
469 /* match VIRTUAL memory layout of JMR-TX3904 board */
472 /* --- disable monitor unless forced on by user --- */
474 if (! firmware_option_p)
476 idt_monitor_base = 0;
477 pmon_monitor_base = 0;
478 lsipmon_monitor_base = 0;
481 /* --- environment --- */
483 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
487 /* ROM: 0x9FC0_0000 - 0x9FFF_FFFF and 0xBFC0_0000 - 0xBFFF_FFFF */
488 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx,0x%0x",
490 4 * 1024 * 1024, /* 4 MB */
493 /* SRAM: 0x8000_0000 - 0x803F_FFFF and 0xA000_0000 - 0xA03F_FFFF */
494 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx,0x%0x",
496 4 * 1024 * 1024, /* 4 MB */
499 /* DRAM: 0x8800_0000 - 0x89FF_FFFF and 0xA800_0000 - 0xA9FF_FFFF */
500 for (i=0; i<8; i++) /* 32 MB total */
502 unsigned size = 4 * 1024 * 1024; /* 4 MB */
503 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx,0x%0x",
504 0x88000000 + (i * size),
506 0xA8000000 + (i * size));
509 /* Dummy memory regions for unsimulated devices - sorted by address */
511 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xB1000000, 0x400); /* ISA I/O */
512 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xB2100000, 0x004); /* ISA ctl */
513 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xB2500000, 0x004); /* LED/switch */
514 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xB2700000, 0x004); /* RTC */
515 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xB3C00000, 0x004); /* RTC */
516 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xFFFF8000, 0x900); /* DRAMC */
517 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xFFFF9000, 0x200); /* EBIF */
518 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xFFFFE000, 0x01c); /* EBIF */
519 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xFFFFF500, 0x300); /* PIO */
522 /* --- simulated devices --- */
523 sim_hw_parse (sd, "/tx3904irc@0xffffc000/reg 0xffffc000 0x20");
524 sim_hw_parse (sd, "/tx3904cpu");
525 sim_hw_parse (sd, "/tx3904tmr@0xfffff000/reg 0xfffff000 0x100");
526 sim_hw_parse (sd, "/tx3904tmr@0xfffff100/reg 0xfffff100 0x100");
527 sim_hw_parse (sd, "/tx3904tmr@0xfffff200/reg 0xfffff200 0x100");
528 sim_hw_parse (sd, "/tx3904sio@0xfffff300/reg 0xfffff300 0x100");
530 /* FIXME: poking at dv-sockser internals, use tcp backend if
531 --sockser_addr option was given.*/
532 extern char* sockser_addr;
533 if(sockser_addr == NULL)
534 sim_hw_parse (sd, "/tx3904sio@0xfffff300/backend stdio");
536 sim_hw_parse (sd, "/tx3904sio@0xfffff300/backend tcp");
538 sim_hw_parse (sd, "/tx3904sio@0xfffff400/reg 0xfffff400 0x100");
539 sim_hw_parse (sd, "/tx3904sio@0xfffff400/backend stdio");
541 /* -- device connections --- */
542 sim_hw_parse (sd, "/tx3904irc > ip level /tx3904cpu");
543 sim_hw_parse (sd, "/tx3904tmr@0xfffff000 > int tmr0 /tx3904irc");
544 sim_hw_parse (sd, "/tx3904tmr@0xfffff100 > int tmr1 /tx3904irc");
545 sim_hw_parse (sd, "/tx3904tmr@0xfffff200 > int tmr2 /tx3904irc");
546 sim_hw_parse (sd, "/tx3904sio@0xfffff300 > int sio0 /tx3904irc");
547 sim_hw_parse (sd, "/tx3904sio@0xfffff400 > int sio1 /tx3904irc");
549 /* add PAL timer & I/O module */
550 if(! strcmp(board, BOARD_JMR3904_PAL))
553 sim_hw_parse (sd, "/pal@0xffff0000");
554 sim_hw_parse (sd, "/pal@0xffff0000/reg 0xffff0000 64");
556 /* wire up interrupt ports to irc */
557 sim_hw_parse (sd, "/pal@0x31000000 > countdown tmr0 /tx3904irc");
558 sim_hw_parse (sd, "/pal@0x31000000 > timer tmr1 /tx3904irc");
559 sim_hw_parse (sd, "/pal@0x31000000 > int int0 /tx3904irc");
562 if(! strcmp(board, BOARD_JMR3904_DEBUG))
564 /* -- DEBUG: glue interrupt generators --- */
565 sim_hw_parse (sd, "/glue@0xffff0000/reg 0xffff0000 0x50");
566 sim_hw_parse (sd, "/glue@0xffff0000 > int0 int0 /tx3904irc");
567 sim_hw_parse (sd, "/glue@0xffff0000 > int1 int1 /tx3904irc");
568 sim_hw_parse (sd, "/glue@0xffff0000 > int2 int2 /tx3904irc");
569 sim_hw_parse (sd, "/glue@0xffff0000 > int3 int3 /tx3904irc");
570 sim_hw_parse (sd, "/glue@0xffff0000 > int4 int4 /tx3904irc");
571 sim_hw_parse (sd, "/glue@0xffff0000 > int5 int5 /tx3904irc");
572 sim_hw_parse (sd, "/glue@0xffff0000 > int6 int6 /tx3904irc");
573 sim_hw_parse (sd, "/glue@0xffff0000 > int7 int7 /tx3904irc");
574 sim_hw_parse (sd, "/glue@0xffff0000 > int8 dmac0 /tx3904irc");
575 sim_hw_parse (sd, "/glue@0xffff0000 > int9 dmac1 /tx3904irc");
576 sim_hw_parse (sd, "/glue@0xffff0000 > int10 dmac2 /tx3904irc");
577 sim_hw_parse (sd, "/glue@0xffff0000 > int11 dmac3 /tx3904irc");
578 sim_hw_parse (sd, "/glue@0xffff0000 > int12 sio0 /tx3904irc");
579 sim_hw_parse (sd, "/glue@0xffff0000 > int13 sio1 /tx3904irc");
580 sim_hw_parse (sd, "/glue@0xffff0000 > int14 tmr0 /tx3904irc");
581 sim_hw_parse (sd, "/glue@0xffff0000 > int15 tmr1 /tx3904irc");
582 sim_hw_parse (sd, "/glue@0xffff0000 > int16 tmr2 /tx3904irc");
583 sim_hw_parse (sd, "/glue@0xffff0000 > int17 nmi /tx3904cpu");
591 /* check for/establish the a reference program image */
592 if (sim_analyze_program (sd,
593 (STATE_PROG_ARGV (sd) != NULL
594 ? *STATE_PROG_ARGV (sd)
598 sim_module_uninstall (sd);
602 /* Configure/verify the target byte order and other runtime
603 configuration options */
604 if (sim_config (sd) != SIM_RC_OK)
606 sim_module_uninstall (sd);
610 if (sim_post_argv_init (sd) != SIM_RC_OK)
612 /* Uninstall the modules to avoid memory leaks,
613 file descriptor leaks, etc. */
614 sim_module_uninstall (sd);
618 /* verify assumptions the simulator made about the host type system.
619 This macro does not return if there is a problem */
620 SIM_ASSERT (sizeof(int) == (4 * sizeof(char)));
621 SIM_ASSERT (sizeof(word64) == (8 * sizeof(char)));
623 /* This is NASTY, in that we are assuming the size of specific
627 for (rn = 0; (rn < (LAST_EMBED_REGNUM + 1)); rn++)
630 cpu->register_widths[rn] = WITH_TARGET_WORD_BITSIZE;
631 else if ((rn >= FGR_BASE) && (rn < (FGR_BASE + NR_FGR)))
632 cpu->register_widths[rn] = WITH_TARGET_FLOATING_POINT_BITSIZE;
633 else if ((rn >= 33) && (rn <= 37))
634 cpu->register_widths[rn] = WITH_TARGET_WORD_BITSIZE;
635 else if ((rn == SRIDX)
638 || ((rn >= 72) && (rn <= 89)))
639 cpu->register_widths[rn] = 32;
641 cpu->register_widths[rn] = 0;
648 if (STATE & simTRACE)
653 sim_io_eprintf (sd, "idt@%x pmon@%x lsipmon@%x\n",
656 lsipmon_monitor_base);
659 /* Write the monitor trap address handlers into the monitor (eeprom)
660 address space. This can only be done once the target endianness
661 has been determined. */
662 if (idt_monitor_base != 0)
665 unsigned idt_monitor_size = 1 << 11;
667 /* the default monitor region */
668 sim_do_commandf (sd, "memory region 0x%x,0x%x",
669 idt_monitor_base, idt_monitor_size);
671 /* Entry into the IDT monitor is via fixed address vectors, and
672 not using machine instructions. To avoid clashing with use of
673 the MIPS TRAP system, we place our own (simulator specific)
674 "undefined" instructions into the relevant vector slots. */
675 for (loop = 0; (loop < idt_monitor_size); loop += 4)
677 address_word vaddr = (idt_monitor_base + loop);
678 unsigned32 insn = (RSVD_INSTRUCTION |
679 (((loop >> 2) & RSVD_INSTRUCTION_ARG_MASK)
680 << RSVD_INSTRUCTION_ARG_SHIFT));
682 sim_write (sd, vaddr, (char *)&insn, sizeof (insn));
686 if ((pmon_monitor_base != 0) || (lsipmon_monitor_base != 0))
688 /* The PMON monitor uses the same address space, but rather than
689 branching into it the address of a routine is loaded. We can
690 cheat for the moment, and direct the PMON routine to IDT style
691 instructions within the monitor space. This relies on the IDT
692 monitor not using the locations from 0xBFC00500 onwards as its
695 for (loop = 0; (loop < 24); loop++)
697 unsigned32 value = ((0x500 - 8) / 8); /* default UNDEFINED reason code */
713 value = ((0x500 - 16) / 8); /* not an IDT reason code */
715 case 8: /* cliexit */
718 case 11: /* flush_cache */
723 SIM_ASSERT (idt_monitor_base != 0);
724 value = ((unsigned int) idt_monitor_base + (value * 8));
727 if (pmon_monitor_base != 0)
729 address_word vaddr = (pmon_monitor_base + (loop * 4));
730 sim_write (sd, vaddr, (char *)&value, sizeof (value));
733 if (lsipmon_monitor_base != 0)
735 address_word vaddr = (lsipmon_monitor_base + (loop * 4));
736 sim_write (sd, vaddr, (char *)&value, sizeof (value));
740 /* Write an abort sequence into the TRAP (common) exception vector
741 addresses. This is to catch code executing a TRAP (et.al.)
742 instruction without installing a trap handler. */
743 if ((idt_monitor_base != 0) ||
744 (pmon_monitor_base != 0) ||
745 (lsipmon_monitor_base != 0))
747 unsigned32 halt[2] = { 0x2404002f /* addiu r4, r0, 47 */,
748 HALT_INSTRUCTION /* BREAK */ };
751 sim_write (sd, 0x80000000, (char *) halt, sizeof (halt));
752 sim_write (sd, 0x80000180, (char *) halt, sizeof (halt));
753 sim_write (sd, 0x80000200, (char *) halt, sizeof (halt));
754 /* XXX: Write here unconditionally? */
755 sim_write (sd, 0xBFC00200, (char *) halt, sizeof (halt));
756 sim_write (sd, 0xBFC00380, (char *) halt, sizeof (halt));
757 sim_write (sd, 0xBFC00400, (char *) halt, sizeof (halt));
771 tracefh = fopen(tracefile,"wb+");
774 sim_io_eprintf(sd,"Failed to create file \"%s\", writing trace information to stderr.\n",tracefile);
780 /* Return name of an insn, used by insn profiling. */
782 get_insn_name (sim_cpu *cpu, int i)
784 return itable[i].name;
788 sim_close (sd, quitting)
793 printf("DBG: sim_close: entered (quitting = %d)\n",quitting);
797 /* "quitting" is non-zero if we cannot hang on errors */
799 /* shut down modules */
800 sim_module_uninstall (sd);
802 /* Ensure that any resources allocated through the callback
803 mechanism are released: */
804 sim_io_shutdown (sd);
807 if (tracefh != NULL && tracefh != stderr)
812 /* FIXME - free SD */
819 sim_write (sd,addr,buffer,size)
822 unsigned char *buffer;
826 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
828 /* Return the number of bytes written, or zero if error. */
830 sim_io_printf(sd,"sim_write(0x%s,buffer,%d);\n",pr_addr(addr),size);
833 /* We use raw read and write routines, since we do not want to count
834 the GDB memory accesses in our statistics gathering. */
836 for (index = 0; index < size; index++)
838 address_word vaddr = (address_word)addr + index;
841 if (!address_translation (SD, CPU, NULL_CIA, vaddr, isDATA, isSTORE, &paddr, &cca, isRAW))
843 if (sim_core_write_buffer (SD, CPU, read_map, buffer + index, paddr, 1) != 1)
851 sim_read (sd,addr,buffer,size)
854 unsigned char *buffer;
858 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
860 /* Return the number of bytes read, or zero if error. */
862 sim_io_printf(sd,"sim_read(0x%s,buffer,%d);\n",pr_addr(addr),size);
865 for (index = 0; (index < size); index++)
867 address_word vaddr = (address_word)addr + index;
870 if (!address_translation (SD, CPU, NULL_CIA, vaddr, isDATA, isLOAD, &paddr, &cca, isRAW))
872 if (sim_core_read_buffer (SD, CPU, read_map, buffer + index, paddr, 1) != 1)
880 sim_store_register (sd,rn,memory,length)
883 unsigned char *memory;
886 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
887 /* NOTE: gdb (the client) stores registers in target byte order
888 while the simulator uses host byte order */
890 sim_io_printf(sd,"sim_store_register(%d,*memory=0x%s);\n",rn,pr_addr(*((SIM_ADDR *)memory)));
893 /* Unfortunately this suffers from the same problem as the register
894 numbering one. We need to know what the width of each logical
895 register number is for the architecture being simulated. */
897 if (cpu->register_widths[rn] == 0)
899 sim_io_eprintf(sd,"Invalid register width for %d (register store ignored)\n",rn);
905 if (rn >= FGR_BASE && rn < FGR_BASE + NR_FGR)
907 cpu->fpr_state[rn - FGR_BASE] = fmt_uninterpreted;
908 if (cpu->register_widths[rn] == 32)
912 cpu->fgr[rn - FGR_BASE] =
913 (unsigned32) T2H_8 (*(unsigned64*)memory);
918 cpu->fgr[rn - FGR_BASE] = T2H_4 (*(unsigned32*)memory);
926 cpu->fgr[rn - FGR_BASE] = T2H_8 (*(unsigned64*)memory);
931 cpu->fgr[rn - FGR_BASE] = T2H_4 (*(unsigned32*)memory);
937 if (cpu->register_widths[rn] == 32)
942 (unsigned32) T2H_8 (*(unsigned64*)memory);
947 cpu->registers[rn] = T2H_4 (*(unsigned32*)memory);
955 cpu->registers[rn] = T2H_8 (*(unsigned64*)memory);
960 cpu->registers[rn] = (signed32) T2H_4(*(unsigned32*)memory);
969 sim_fetch_register (sd,rn,memory,length)
972 unsigned char *memory;
975 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
976 /* NOTE: gdb (the client) stores registers in target byte order
977 while the simulator uses host byte order */
979 #if 0 /* FIXME: doesn't compile */
980 sim_io_printf(sd,"sim_fetch_register(%d=0x%s,mem) : place simulator registers into memory\n",rn,pr_addr(registers[rn]));
984 if (cpu->register_widths[rn] == 0)
986 sim_io_eprintf (sd, "Invalid register width for %d (register fetch ignored)\n",rn);
992 /* Any floating point register */
993 if (rn >= FGR_BASE && rn < FGR_BASE + NR_FGR)
995 if (cpu->register_widths[rn] == 32)
999 *(unsigned64*)memory =
1000 H2T_8 ((unsigned32) (cpu->fgr[rn - FGR_BASE]));
1005 *(unsigned32*)memory = H2T_4 (cpu->fgr[rn - FGR_BASE]);
1013 *(unsigned64*)memory = H2T_8 (cpu->fgr[rn - FGR_BASE]);
1018 *(unsigned32*)memory = H2T_4 ((unsigned32)(cpu->fgr[rn - FGR_BASE]));
1024 if (cpu->register_widths[rn] == 32)
1028 *(unsigned64*)memory =
1029 H2T_8 ((unsigned32) (cpu->registers[rn]));
1034 *(unsigned32*)memory = H2T_4 ((unsigned32)(cpu->registers[rn]));
1042 *(unsigned64*)memory =
1043 H2T_8 ((unsigned64) (cpu->registers[rn]));
1048 *(unsigned32*)memory = H2T_4 ((unsigned32)(cpu->registers[rn]));
1058 sim_create_inferior (sd, abfd, argv,env)
1066 #if 0 /* FIXME: doesn't compile */
1067 printf("DBG: sim_create_inferior entered: start_address = 0x%s\n",
1076 /* override PC value set by ColdReset () */
1078 for (cpu_nr = 0; cpu_nr < sim_engine_nr_cpus (sd); cpu_nr++)
1080 sim_cpu *cpu = STATE_CPU (sd, cpu_nr);
1081 CIA_SET (cpu, (unsigned64) bfd_get_start_address (abfd));
1085 #if 0 /* def DEBUG */
1088 /* We should really place the argv slot values into the argument
1089 registers, and onto the stack as required. However, this
1090 assumes that we have a stack defined, which is not
1091 necessarily true at the moment. */
1093 sim_io_printf(sd,"sim_create_inferior() : passed arguments ignored\n");
1094 for (cptr = argv; (cptr && *cptr); cptr++)
1095 printf("DBG: arg \"%s\"\n",*cptr);
1103 sim_do_command (sd,cmd)
1107 if (sim_args_command (sd, cmd) != SIM_RC_OK)
1108 sim_io_printf (sd, "Error: \"%s\" is not a valid MIPS simulator command.\n",
1112 /*---------------------------------------------------------------------------*/
1113 /*-- Private simulator support interface ------------------------------------*/
1114 /*---------------------------------------------------------------------------*/
1116 /* Read a null terminated string from memory, return in a buffer */
1118 fetch_str (SIM_DESC sd,
1124 while (sim_read (sd, addr + nr, &null, 1) == 1 && null != 0)
1126 buf = NZALLOC (char, nr + 1);
1127 sim_read (sd, addr, buf, nr);
1132 /* Implements the "sim firmware" command:
1133 sim firmware NAME[@ADDRESS] --- emulate ROM monitor named NAME.
1134 NAME can be idt, pmon, or lsipmon. If omitted, ADDRESS
1135 defaults to the normal address for that monitor.
1136 sim firmware none --- don't emulate any ROM monitor. Useful
1137 if you need a clean address space. */
1139 sim_firmware_command (SIM_DESC sd, char *arg)
1141 int address_present = 0;
1144 /* Signal occurrence of this option. */
1145 firmware_option_p = 1;
1147 /* Parse out the address, if present. */
1149 char *p = strchr (arg, '@');
1153 address_present = 1;
1154 p ++; /* skip over @ */
1156 address = strtoul (p, &q, 0);
1159 sim_io_printf (sd, "Invalid address given to the"
1160 "`sim firmware NAME@ADDRESS' command: %s\n",
1167 address_present = 0;
1168 address = -1; /* Dummy value. */
1172 if (! strncmp (arg, "idt", 3))
1174 idt_monitor_base = address_present ? address : 0xBFC00000;
1175 pmon_monitor_base = 0;
1176 lsipmon_monitor_base = 0;
1178 else if (! strncmp (arg, "pmon", 4))
1180 /* pmon uses indirect calls. Hook into implied idt. */
1181 pmon_monitor_base = address_present ? address : 0xBFC00500;
1182 idt_monitor_base = pmon_monitor_base - 0x500;
1183 lsipmon_monitor_base = 0;
1185 else if (! strncmp (arg, "lsipmon", 7))
1187 /* lsipmon uses indirect calls. Hook into implied idt. */
1188 pmon_monitor_base = 0;
1189 lsipmon_monitor_base = address_present ? address : 0xBFC00200;
1190 idt_monitor_base = lsipmon_monitor_base - 0x200;
1192 else if (! strncmp (arg, "none", 4))
1194 if (address_present)
1197 "The `sim firmware none' command does "
1198 "not take an `ADDRESS' argument.\n");
1201 idt_monitor_base = 0;
1202 pmon_monitor_base = 0;
1203 lsipmon_monitor_base = 0;
1207 sim_io_printf (sd, "\
1208 Unrecognized name given to the `sim firmware NAME' command: %s\n\
1209 Recognized firmware names are: `idt', `pmon', `lsipmon', and `none'.\n",
1219 /* Simple monitor interface (currently setup for the IDT and PMON monitors) */
1221 sim_monitor (SIM_DESC sd,
1224 unsigned int reason)
1227 printf("DBG: sim_monitor: entered (reason = %d)\n",reason);
1230 /* The IDT monitor actually allows two instructions per vector
1231 slot. However, the simulator currently causes a trap on each
1232 individual instruction. We cheat, and lose the bottom bit. */
1235 /* The following callback functions are available, however the
1236 monitor we are simulating does not make use of them: get_errno,
1237 isatty, lseek, rename, system, time and unlink */
1241 case 6: /* int open(char *path,int flags) */
1243 char *path = fetch_str (sd, A0);
1244 V0 = sim_io_open (sd, path, (int)A1);
1249 case 7: /* int read(int file,char *ptr,int len) */
1253 char *buf = zalloc (nr);
1254 V0 = sim_io_read (sd, fd, buf, nr);
1255 sim_write (sd, A1, buf, nr);
1260 case 8: /* int write(int file,char *ptr,int len) */
1264 char *buf = zalloc (nr);
1265 sim_read (sd, A1, buf, nr);
1266 V0 = sim_io_write (sd, fd, buf, nr);
1271 case 10: /* int close(int file) */
1273 V0 = sim_io_close (sd, (int)A0);
1277 case 2: /* Densan monitor: char inbyte(int waitflag) */
1279 if (A0 == 0) /* waitflag == NOWAIT */
1280 V0 = (unsigned_word)-1;
1282 /* Drop through to case 11 */
1284 case 11: /* char inbyte(void) */
1287 /* ensure that all output has gone... */
1288 sim_io_flush_stdout (sd);
1289 if (sim_io_read_stdin (sd, &tmp, sizeof(char)) != sizeof(char))
1291 sim_io_error(sd,"Invalid return from character read");
1292 V0 = (unsigned_word)-1;
1295 V0 = (unsigned_word)tmp;
1299 case 3: /* Densan monitor: void co(char chr) */
1300 case 12: /* void outbyte(char chr) : write a byte to "stdout" */
1302 char tmp = (char)(A0 & 0xFF);
1303 sim_io_write_stdout (sd, &tmp, sizeof(char));
1307 case 17: /* void _exit() */
1309 sim_io_eprintf (sd, "sim_monitor(17): _exit(int reason) to be coded\n");
1310 sim_engine_halt (SD, CPU, NULL, NULL_CIA, sim_exited,
1311 (unsigned int)(A0 & 0xFFFFFFFF));
1315 case 28: /* PMON flush_cache */
1318 case 55: /* void get_mem_info(unsigned int *ptr) */
1319 /* in: A0 = pointer to three word memory location */
1320 /* out: [A0 + 0] = size */
1321 /* [A0 + 4] = instruction cache size */
1322 /* [A0 + 8] = data cache size */
1325 unsigned_4 zero = 0;
1326 address_word mem_size;
1327 sim_memopt *entry, *match = NULL;
1329 /* Search for memory region mapped to KSEG0 or KSEG1. */
1330 for (entry = STATE_MEMOPT (sd);
1332 entry = entry->next)
1334 if ((entry->addr == K0BASE || entry->addr == K1BASE)
1335 && (!match || entry->level < match->level))
1340 for (alias = entry->alias;
1342 alias = alias->next)
1343 if ((alias->addr == K0BASE || alias->addr == K1BASE)
1344 && (!match || entry->level < match->level))
1349 /* Get region size, limit to KSEG1 size (512MB). */
1350 SIM_ASSERT (match != NULL);
1351 mem_size = (match->modulo != 0
1352 ? match->modulo : match->nr_bytes);
1353 if (mem_size > K1SIZE)
1358 sim_write (sd, A0 + 0, (char *)&value, 4);
1359 sim_write (sd, A0 + 4, (char *)&zero, 4);
1360 sim_write (sd, A0 + 8, (char *)&zero, 4);
1361 /* sim_io_eprintf (sd, "sim: get_mem_info() deprecated\n"); */
1365 case 158: /* PMON printf */
1366 /* in: A0 = pointer to format string */
1367 /* A1 = optional argument 1 */
1368 /* A2 = optional argument 2 */
1369 /* A3 = optional argument 3 */
1371 /* The following is based on the PMON printf source */
1373 address_word s = A0;
1375 signed_word *ap = &A1; /* 1st argument */
1376 /* This isn't the quickest way, since we call the host print
1377 routine for every character almost. But it does avoid
1378 having to allocate and manage a temporary string buffer. */
1379 /* TODO: Include check that we only use three arguments (A1,
1381 while (sim_read (sd, s++, &c, 1) && c != '\0')
1386 enum {FMT_RJUST, FMT_LJUST, FMT_RJUST0, FMT_CENTER} fmt = FMT_RJUST;
1387 int width = 0, trunc = 0, haddot = 0, longlong = 0;
1388 while (sim_read (sd, s++, &c, 1) && c != '\0')
1390 if (strchr ("dobxXulscefg%", c))
1405 else if (c >= '1' && c <= '9')
1409 while (sim_read (sd, s++, &c, 1) == 1 && isdigit (c))
1412 n = (unsigned int)strtol(tmp,NULL,10);
1425 sim_io_printf (sd, "%%");
1430 address_word p = *ap++;
1432 while (sim_read (sd, p++, &ch, 1) == 1 && ch != '\0')
1433 sim_io_printf(sd, "%c", ch);
1436 sim_io_printf(sd,"(null)");
1439 sim_io_printf (sd, "%c", (int)*ap++);
1444 sim_read (sd, s++, &c, 1);
1448 sim_read (sd, s++, &c, 1);
1451 if (strchr ("dobxXu", c))
1453 word64 lv = (word64) *ap++;
1455 sim_io_printf(sd,"<binary not supported>");
1458 sprintf (tmp, "%%%s%c", longlong ? "ll" : "", c);
1460 sim_io_printf(sd, tmp, lv);
1462 sim_io_printf(sd, tmp, (int)lv);
1465 else if (strchr ("eEfgG", c))
1467 double dbl = *(double*)(ap++);
1468 sprintf (tmp, "%%%d.%d%c", width, trunc, c);
1469 sim_io_printf (sd, tmp, dbl);
1475 sim_io_printf(sd, "%c", c);
1481 /* Unknown reason. */
1487 /* Store a word into memory. */
1490 store_word (SIM_DESC sd,
1499 if ((vaddr & 3) != 0)
1500 SignalExceptionAddressStore ();
1503 if (AddressTranslation (vaddr, isDATA, isSTORE, &paddr, &uncached,
1506 const uword64 mask = 7;
1510 paddr = (paddr & ~mask) | ((paddr & mask) ^ (ReverseEndian << 2));
1511 byte = (vaddr & mask) ^ (BigEndianCPU << 2);
1512 memval = ((uword64) val) << (8 * byte);
1513 StoreMemory (uncached, AccessLength_WORD, memval, 0, paddr, vaddr,
1519 /* Load a word from memory. */
1522 load_word (SIM_DESC sd,
1527 if ((vaddr & 3) != 0)
1529 SIM_CORE_SIGNAL (SD, cpu, cia, read_map, AccessLength_WORD+1, vaddr, read_transfer, sim_core_unaligned_signal);
1536 if (AddressTranslation (vaddr, isDATA, isLOAD, &paddr, &uncached,
1539 const uword64 mask = 0x7;
1540 const unsigned int reverse = ReverseEndian ? 1 : 0;
1541 const unsigned int bigend = BigEndianCPU ? 1 : 0;
1545 paddr = (paddr & ~mask) | ((paddr & mask) ^ (reverse << 2));
1546 LoadMemory (&memval,NULL,uncached, AccessLength_WORD, paddr, vaddr,
1548 byte = (vaddr & mask) ^ (bigend << 2);
1549 return EXTEND32 (memval >> (8 * byte));
1556 /* Simulate the mips16 entry and exit pseudo-instructions. These
1557 would normally be handled by the reserved instruction exception
1558 code, but for ease of simulation we just handle them directly. */
1561 mips16_entry (SIM_DESC sd,
1566 int aregs, sregs, rreg;
1569 printf("DBG: mips16_entry: entered (insn = 0x%08X)\n",insn);
1572 aregs = (insn & 0x700) >> 8;
1573 sregs = (insn & 0x0c0) >> 6;
1574 rreg = (insn & 0x020) >> 5;
1576 /* This should be checked by the caller. */
1585 /* This is the entry pseudo-instruction. */
1587 for (i = 0; i < aregs; i++)
1588 store_word (SD, CPU, cia, (uword64) (SP + 4 * i), GPR[i + 4]);
1596 store_word (SD, CPU, cia, (uword64) tsp, RA);
1599 for (i = 0; i < sregs; i++)
1602 store_word (SD, CPU, cia, (uword64) tsp, GPR[16 + i]);
1610 /* This is the exit pseudo-instruction. */
1617 RA = load_word (SD, CPU, cia, (uword64) tsp);
1620 for (i = 0; i < sregs; i++)
1623 GPR[i + 16] = load_word (SD, CPU, cia, (uword64) tsp);
1628 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
1632 FGR[0] = WORD64LO (GPR[4]);
1633 FPR_STATE[0] = fmt_uninterpreted;
1635 else if (aregs == 6)
1637 FGR[0] = WORD64LO (GPR[5]);
1638 FGR[1] = WORD64LO (GPR[4]);
1639 FPR_STATE[0] = fmt_uninterpreted;
1640 FPR_STATE[1] = fmt_uninterpreted;
1649 /*-- trace support ----------------------------------------------------------*/
1651 /* The TRACE support is provided (if required) in the memory accessing
1652 routines. Since we are also providing the architecture specific
1653 features, the architecture simulation code can also deal with
1654 notifying the TRACE world of cache flushes, etc. Similarly we do
1655 not need to provide profiling support in the simulator engine,
1656 since we can sample in the instruction fetch control loop. By
1657 defining the TRACE manifest, we add tracing as a run-time
1661 /* Tracing by default produces "din" format (as required by
1662 dineroIII). Each line of such a trace file *MUST* have a din label
1663 and address field. The rest of the line is ignored, so comments can
1664 be included if desired. The first field is the label which must be
1665 one of the following values:
1670 3 escape record (treated as unknown access type)
1671 4 escape record (causes cache flush)
1673 The address field is a 32bit (lower-case) hexadecimal address
1674 value. The address should *NOT* be preceded by "0x".
1676 The size of the memory transfer is not important when dealing with
1677 cache lines (as long as no more than a cache line can be
1678 transferred in a single operation :-), however more information
1679 could be given following the dineroIII requirement to allow more
1680 complete memory and cache simulators to provide better
1681 results. i.e. the University of Pisa has a cache simulator that can
1682 also take bus size and speed as (variable) inputs to calculate
1683 complete system performance (a much more useful ability when trying
1684 to construct an end product, rather than a processor). They
1685 currently have an ARM version of their tool called ChARM. */
1689 dotrace (SIM_DESC sd,
1697 if (STATE & simTRACE) {
1699 fprintf(tracefh,"%d %s ; width %d ; ",
1703 va_start(ap,comment);
1704 vfprintf(tracefh,comment,ap);
1706 fprintf(tracefh,"\n");
1708 /* NOTE: Since the "din" format will only accept 32bit addresses, and
1709 we may be generating 64bit ones, we should put the hi-32bits of the
1710 address into the comment field. */
1712 /* TODO: Provide a buffer for the trace lines. We can then avoid
1713 performing writes until the buffer is filled, or the file is
1716 /* NOTE: We could consider adding a comment field to the "din" file
1717 produced using type 3 markers (unknown access). This would then
1718 allow information about the program that the "din" is for, and
1719 the MIPs world that was being simulated, to be placed into the
1726 /*---------------------------------------------------------------------------*/
1727 /*-- simulator engine -------------------------------------------------------*/
1728 /*---------------------------------------------------------------------------*/
1731 ColdReset (SIM_DESC sd)
1734 for (cpu_nr = 0; cpu_nr < sim_engine_nr_cpus (sd); cpu_nr++)
1736 sim_cpu *cpu = STATE_CPU (sd, cpu_nr);
1737 /* RESET: Fixed PC address: */
1738 PC = (unsigned_word) UNSIGNED64 (0xFFFFFFFFBFC00000);
1739 /* The reset vector address is in the unmapped, uncached memory space. */
1741 SR &= ~(status_SR | status_TS | status_RP);
1742 SR |= (status_ERL | status_BEV);
1744 /* Cheat and allow access to the complete register set immediately */
1745 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT
1746 && WITH_TARGET_WORD_BITSIZE == 64)
1747 SR |= status_FR; /* 64bit registers */
1749 /* Ensure that any instructions with pending register updates are
1751 PENDING_INVALIDATE();
1753 /* Initialise the FPU registers to the unknown state */
1754 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
1757 for (rn = 0; (rn < 32); rn++)
1758 FPR_STATE[rn] = fmt_uninterpreted;
1767 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1768 /* Signal an exception condition. This will result in an exception
1769 that aborts the instruction. The instruction operation pseudocode
1770 will never see a return from this function call. */
1773 signal_exception (SIM_DESC sd,
1781 sim_io_printf(sd,"DBG: SignalException(%d) PC = 0x%s\n",exception,pr_addr(cia));
1784 /* Ensure that any active atomic read/modify/write operation will fail: */
1787 /* Save registers before interrupt dispatching */
1788 #ifdef SIM_CPU_EXCEPTION_TRIGGER
1789 SIM_CPU_EXCEPTION_TRIGGER(sd, cpu, cia);
1792 switch (exception) {
1794 case DebugBreakPoint:
1795 if (! (Debug & Debug_DM))
1801 Debug |= Debug_DBD; /* signaled from within in delay slot */
1802 DEPC = cia - 4; /* reference the branch instruction */
1806 Debug &= ~Debug_DBD; /* not signaled from within a delay slot */
1810 Debug |= Debug_DM; /* in debugging mode */
1811 Debug |= Debug_DBp; /* raising a DBp exception */
1813 sim_engine_restart (SD, CPU, NULL, NULL_CIA);
1817 case ReservedInstruction:
1820 unsigned int instruction;
1821 va_start(ap,exception);
1822 instruction = va_arg(ap,unsigned int);
1824 /* Provide simple monitor support using ReservedInstruction
1825 exceptions. The following code simulates the fixed vector
1826 entry points into the IDT monitor by causing a simulator
1827 trap, performing the monitor operation, and returning to
1828 the address held in the $ra register (standard PCS return
1829 address). This means we only need to pre-load the vector
1830 space with suitable instruction values. For systems were
1831 actual trap instructions are used, we would not need to
1832 perform this magic. */
1833 if ((instruction & RSVD_INSTRUCTION_MASK) == RSVD_INSTRUCTION)
1835 int reason = (instruction >> RSVD_INSTRUCTION_ARG_SHIFT) & RSVD_INSTRUCTION_ARG_MASK;
1836 if (!sim_monitor (SD, CPU, cia, reason))
1837 sim_io_error (sd, "sim_monitor: unhandled reason = %d, pc = 0x%s\n", reason, pr_addr (cia));
1839 /* NOTE: This assumes that a branch-and-link style
1840 instruction was used to enter the vector (which is the
1841 case with the current IDT monitor). */
1842 sim_engine_restart (SD, CPU, NULL, RA);
1844 /* Look for the mips16 entry and exit instructions, and
1845 simulate a handler for them. */
1846 else if ((cia & 1) != 0
1847 && (instruction & 0xf81f) == 0xe809
1848 && (instruction & 0x0c0) != 0x0c0)
1850 mips16_entry (SD, CPU, cia, instruction);
1851 sim_engine_restart (sd, NULL, NULL, NULL_CIA);
1853 /* else fall through to normal exception processing */
1854 sim_io_eprintf(sd,"ReservedInstruction at PC = 0x%s\n", pr_addr (cia));
1858 /* Store exception code into current exception id variable (used
1861 /* TODO: If not simulating exceptions then stop the simulator
1862 execution. At the moment we always stop the simulation. */
1864 #ifdef SUBTARGET_R3900
1865 /* update interrupt-related registers */
1867 /* insert exception code in bits 6:2 */
1868 CAUSE = LSMASKED32(CAUSE, 31, 7) | LSINSERTED32(exception, 6, 2);
1869 /* shift IE/KU history bits left */
1870 SR = LSMASKED32(SR, 31, 4) | LSINSERTED32(LSEXTRACTED32(SR, 3, 0), 5, 2);
1872 if (STATE & simDELAYSLOT)
1874 STATE &= ~simDELAYSLOT;
1876 EPC = (cia - 4); /* reference the branch instruction */
1881 if (SR & status_BEV)
1882 PC = (signed)0xBFC00000 + 0x180;
1884 PC = (signed)0x80000000 + 0x080;
1886 /* See figure 5-17 for an outline of the code below */
1887 if (! (SR & status_EXL))
1889 CAUSE = (exception << 2);
1890 if (STATE & simDELAYSLOT)
1892 STATE &= ~simDELAYSLOT;
1894 EPC = (cia - 4); /* reference the branch instruction */
1898 /* FIXME: TLB et.al. */
1899 /* vector = 0x180; */
1903 CAUSE = (exception << 2);
1904 /* vector = 0x180; */
1907 /* Store exception code into current exception id variable (used
1910 if (SR & status_BEV)
1911 PC = (signed)0xBFC00200 + 0x180;
1913 PC = (signed)0x80000000 + 0x180;
1916 switch ((CAUSE >> 2) & 0x1F)
1919 /* Interrupts arrive during event processing, no need to
1925 #ifdef SUBTARGET_3900
1926 /* Exception vector: BEV=0 BFC00000 / BEF=1 BFC00000 */
1927 PC = (signed)0xBFC00000;
1928 #endif /* SUBTARGET_3900 */
1931 case TLBModification:
1936 case InstructionFetch:
1938 /* The following is so that the simulator will continue from the
1939 exception handler address. */
1940 sim_engine_halt (SD, CPU, NULL, PC,
1941 sim_stopped, SIM_SIGBUS);
1943 case ReservedInstruction:
1944 case CoProcessorUnusable:
1946 sim_engine_halt (SD, CPU, NULL, PC,
1947 sim_stopped, SIM_SIGILL);
1949 case IntegerOverflow:
1951 sim_engine_halt (SD, CPU, NULL, PC,
1952 sim_stopped, SIM_SIGFPE);
1955 sim_engine_halt (SD, CPU, NULL, PC, sim_stopped, SIM_SIGTRAP);
1960 sim_engine_restart (SD, CPU, NULL, PC);
1965 sim_engine_halt (SD, CPU, NULL, PC,
1966 sim_stopped, SIM_SIGTRAP);
1968 default: /* Unknown internal exception */
1970 sim_engine_halt (SD, CPU, NULL, PC,
1971 sim_stopped, SIM_SIGABRT);
1975 case SimulatorFault:
1979 va_start(ap,exception);
1980 msg = va_arg(ap,char *);
1982 sim_engine_abort (SD, CPU, NULL_CIA,
1983 "FATAL: Simulator error \"%s\"\n",msg);
1992 /* This function implements what the MIPS32 and MIPS64 ISAs define as
1993 "UNPREDICTABLE" behaviour.
1995 About UNPREDICTABLE behaviour they say: "UNPREDICTABLE results
1996 may vary from processor implementation to processor implementation,
1997 instruction to instruction, or as a function of time on the same
1998 implementation or instruction. Software can never depend on results
1999 that are UNPREDICTABLE. ..." (MIPS64 Architecture for Programmers
2000 Volume II, The MIPS64 Instruction Set. MIPS Document MD00087 revision
2003 For UNPREDICTABLE behaviour, we print a message, if possible print
2004 the offending instructions mips.igen instruction name (provided by
2005 the caller), and stop the simulator.
2007 XXX FIXME: eventually, stopping the simulator should be made conditional
2008 on a command-line option. */
2010 unpredictable_action(sim_cpu *cpu, address_word cia)
2012 SIM_DESC sd = CPU_STATE(cpu);
2014 sim_io_eprintf(sd, "UNPREDICTABLE: PC = 0x%s\n", pr_addr (cia));
2015 sim_engine_halt (SD, CPU, NULL, cia, sim_stopped, SIM_SIGABRT);
2019 /*-- co-processor support routines ------------------------------------------*/
2022 CoProcPresent(unsigned int coproc_number)
2024 /* Return TRUE if simulator provides a model for the given co-processor number */
2029 cop_lw (SIM_DESC sd,
2034 unsigned int memword)
2039 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2042 printf("DBG: COP_LW: memword = 0x%08X (uword64)memword = 0x%s\n",memword,pr_addr(memword));
2044 StoreFPR(coproc_reg,fmt_uninterpreted_32,(uword64)memword);
2049 #if 0 /* this should be controlled by a configuration option */
2050 sim_io_printf(sd,"COP_LW(%d,%d,0x%08X) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,memword,pr_addr(cia));
2059 cop_ld (SIM_DESC sd,
2068 printf("DBG: COP_LD: coproc_num = %d, coproc_reg = %d, value = 0x%s : PC = 0x%s\n", coproc_num, coproc_reg, pr_uword64(memword), pr_addr(cia) );
2071 switch (coproc_num) {
2073 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2075 StoreFPR(coproc_reg,fmt_uninterpreted_64,memword);
2080 #if 0 /* this message should be controlled by a configuration option */
2081 sim_io_printf(sd,"COP_LD(%d,%d,0x%s) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(memword),pr_addr(cia));
2093 cop_sw (SIM_DESC sd,
2099 unsigned int value = 0;
2104 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2106 value = (unsigned int)ValueFPR(coproc_reg,fmt_uninterpreted_32);
2111 #if 0 /* should be controlled by configuration option */
2112 sim_io_printf(sd,"COP_SW(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(cia));
2121 cop_sd (SIM_DESC sd,
2131 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2133 value = ValueFPR(coproc_reg,fmt_uninterpreted_64);
2138 #if 0 /* should be controlled by configuration option */
2139 sim_io_printf(sd,"COP_SD(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(cia));
2151 decode_coproc (SIM_DESC sd,
2154 unsigned int instruction)
2156 int coprocnum = ((instruction >> 26) & 3);
2160 case 0: /* standard CPU control and cache registers */
2162 int code = ((instruction >> 21) & 0x1F);
2163 int rt = ((instruction >> 16) & 0x1F);
2164 int rd = ((instruction >> 11) & 0x1F);
2165 int tail = instruction & 0x3ff;
2166 /* R4000 Users Manual (second edition) lists the following CP0
2168 CODE><-RT><RD-><--TAIL--->
2169 DMFC0 Doubleword Move From CP0 (VR4100 = 01000000001tttttddddd00000000000)
2170 DMTC0 Doubleword Move To CP0 (VR4100 = 01000000101tttttddddd00000000000)
2171 MFC0 word Move From CP0 (VR4100 = 01000000000tttttddddd00000000000)
2172 MTC0 word Move To CP0 (VR4100 = 01000000100tttttddddd00000000000)
2173 TLBR Read Indexed TLB Entry (VR4100 = 01000010000000000000000000000001)
2174 TLBWI Write Indexed TLB Entry (VR4100 = 01000010000000000000000000000010)
2175 TLBWR Write Random TLB Entry (VR4100 = 01000010000000000000000000000110)
2176 TLBP Probe TLB for Matching Entry (VR4100 = 01000010000000000000000000001000)
2177 CACHE Cache operation (VR4100 = 101111bbbbbpppppiiiiiiiiiiiiiiii)
2178 ERET Exception return (VR4100 = 01000010000000000000000000011000)
2180 if (((code == 0x00) || (code == 0x04) /* MFC0 / MTC0 */
2181 || (code == 0x01) || (code == 0x05)) /* DMFC0 / DMTC0 */
2184 /* Clear double/single coprocessor move bit. */
2187 /* M[TF]C0 (32 bits) | DM[TF]C0 (64 bits) */
2189 switch (rd) /* NOTEs: Standard CP0 registers */
2191 /* 0 = Index R4000 VR4100 VR4300 */
2192 /* 1 = Random R4000 VR4100 VR4300 */
2193 /* 2 = EntryLo0 R4000 VR4100 VR4300 */
2194 /* 3 = EntryLo1 R4000 VR4100 VR4300 */
2195 /* 4 = Context R4000 VR4100 VR4300 */
2196 /* 5 = PageMask R4000 VR4100 VR4300 */
2197 /* 6 = Wired R4000 VR4100 VR4300 */
2198 /* 8 = BadVAddr R4000 VR4100 VR4300 */
2199 /* 9 = Count R4000 VR4100 VR4300 */
2200 /* 10 = EntryHi R4000 VR4100 VR4300 */
2201 /* 11 = Compare R4000 VR4100 VR4300 */
2202 /* 12 = SR R4000 VR4100 VR4300 */
2203 #ifdef SUBTARGET_R3900
2205 /* 3 = Config R3900 */
2207 /* 7 = Cache R3900 */
2209 /* 15 = PRID R3900 */
2215 /* 8 = BadVAddr R4000 VR4100 VR4300 */
2217 GPR[rt] = (signed_word) (signed_address) COP0_BADVADDR;
2219 COP0_BADVADDR = GPR[rt];
2222 #endif /* SUBTARGET_R3900 */
2229 /* 13 = Cause R4000 VR4100 VR4300 */
2236 /* 14 = EPC R4000 VR4100 VR4300 */
2239 GPR[rt] = (signed_word) (signed_address) EPC;
2243 /* 15 = PRId R4000 VR4100 VR4300 */
2244 #ifdef SUBTARGET_R3900
2253 /* 16 = Config R4000 VR4100 VR4300 */
2256 GPR[rt] = C0_CONFIG;
2258 C0_CONFIG = GPR[rt];
2261 #ifdef SUBTARGET_R3900
2270 /* 17 = LLAddr R4000 VR4100 VR4300 */
2272 /* 18 = WatchLo R4000 VR4100 VR4300 */
2273 /* 19 = WatchHi R4000 VR4100 VR4300 */
2274 /* 20 = XContext R4000 VR4100 VR4300 */
2275 /* 26 = PErr or ECC R4000 VR4100 VR4300 */
2276 /* 27 = CacheErr R4000 VR4100 */
2277 /* 28 = TagLo R4000 VR4100 VR4300 */
2278 /* 29 = TagHi R4000 VR4100 VR4300 */
2279 /* 30 = ErrorEPC R4000 VR4100 VR4300 */
2280 if (STATE_VERBOSE_P(SD))
2282 "Warning: PC 0x%lx:interp.c decode_coproc DEADC0DE\n",
2283 (unsigned long)cia);
2284 GPR[rt] = 0xDEADC0DE; /* CPR[0,rd] */
2285 /* CPR[0,rd] = GPR[rt]; */
2288 GPR[rt] = (signed_word) (signed32) COP0_GPR[rd];
2290 COP0_GPR[rd] = GPR[rt];
2293 sim_io_printf(sd,"Warning: MFC0 %d,%d ignored, PC=%08x (architecture specific)\n",rt,rd, (unsigned)cia);
2295 sim_io_printf(sd,"Warning: MTC0 %d,%d ignored, PC=%08x (architecture specific)\n",rt,rd, (unsigned)cia);
2299 else if (code == 0x10 && (tail & 0x3f) == 0x18)
2302 if (SR & status_ERL)
2304 /* Oops, not yet available */
2305 sim_io_printf(sd,"Warning: ERET when SR[ERL] set not handled yet");
2315 else if (code == 0x10 && (tail & 0x3f) == 0x10)
2318 #ifdef SUBTARGET_R3900
2319 /* TX39: Copy IEp/KUp -> IEc/KUc, and IEo/KUo -> IEp/KUp */
2321 /* shift IE/KU history bits right */
2322 SR = LSMASKED32(SR, 31, 4) | LSINSERTED32(LSEXTRACTED32(SR, 5, 2), 3, 0);
2324 /* TODO: CACHE register */
2325 #endif /* SUBTARGET_R3900 */
2327 else if (code == 0x10 && (tail & 0x3f) == 0x1F)
2335 sim_io_eprintf(sd,"Unrecognised COP0 instruction 0x%08X at PC = 0x%s : No handler present\n",instruction,pr_addr(cia));
2336 /* TODO: When executing an ERET or RFE instruction we should
2337 clear LLBIT, to ensure that any out-standing atomic
2338 read/modify/write sequence fails. */
2342 case 2: /* co-processor 2 */
2349 sim_io_eprintf(sd, "COP2 instruction 0x%08X at PC = 0x%s : No handler present\n",
2350 instruction,pr_addr(cia));
2355 case 1: /* should not occur (FPU co-processor) */
2356 case 3: /* should not occur (FPU co-processor) */
2357 SignalException(ReservedInstruction,instruction);
2365 /* This code copied from gdb's utils.c. Would like to share this code,
2366 but don't know of a common place where both could get to it. */
2368 /* Temporary storage using circular buffer */
2374 static char buf[NUMCELLS][CELLSIZE];
2376 if (++cell>=NUMCELLS) cell=0;
2380 /* Print routines to handle variable size regs, etc */
2382 /* Eliminate warning from compiler on 32-bit systems */
2383 static int thirty_two = 32;
2389 char *paddr_str=get_cell();
2390 switch (sizeof(addr))
2393 sprintf(paddr_str,"%08lx%08lx",
2394 (unsigned long)(addr>>thirty_two),(unsigned long)(addr&0xffffffff));
2397 sprintf(paddr_str,"%08lx",(unsigned long)addr);
2400 sprintf(paddr_str,"%04x",(unsigned short)(addr&0xffff));
2403 sprintf(paddr_str,"%x",addr);
2412 char *paddr_str=get_cell();
2413 sprintf(paddr_str,"%08lx%08lx",
2414 (unsigned long)(addr>>thirty_two),(unsigned long)(addr&0xffffffff));
2420 mips_core_signal (SIM_DESC sd,
2426 transfer_type transfer,
2427 sim_core_signals sig)
2429 const char *copy = (transfer == read_transfer ? "read" : "write");
2430 address_word ip = CIA_ADDR (cia);
2434 case sim_core_unmapped_signal:
2435 sim_io_eprintf (sd, "mips-core: %d byte %s to unmapped address 0x%lx at 0x%lx\n",
2437 (unsigned long) addr, (unsigned long) ip);
2438 COP0_BADVADDR = addr;
2439 SignalExceptionDataReference();
2442 case sim_core_unaligned_signal:
2443 sim_io_eprintf (sd, "mips-core: %d byte %s to unaligned address 0x%lx at 0x%lx\n",
2445 (unsigned long) addr, (unsigned long) ip);
2446 COP0_BADVADDR = addr;
2447 if(transfer == read_transfer)
2448 SignalExceptionAddressLoad();
2450 SignalExceptionAddressStore();
2454 sim_engine_abort (sd, cpu, cia,
2455 "mips_core_signal - internal error - bad switch");
2461 mips_cpu_exception_trigger(SIM_DESC sd, sim_cpu* cpu, address_word cia)
2463 ASSERT(cpu != NULL);
2465 if(cpu->exc_suspended > 0)
2466 sim_io_eprintf(sd, "Warning, nested exception triggered (%d)\n", cpu->exc_suspended);
2469 memcpy(cpu->exc_trigger_registers, cpu->registers, sizeof(cpu->exc_trigger_registers));
2470 cpu->exc_suspended = 0;
2474 mips_cpu_exception_suspend(SIM_DESC sd, sim_cpu* cpu, int exception)
2476 ASSERT(cpu != NULL);
2478 if(cpu->exc_suspended > 0)
2479 sim_io_eprintf(sd, "Warning, nested exception signal (%d then %d)\n",
2480 cpu->exc_suspended, exception);
2482 memcpy(cpu->exc_suspend_registers, cpu->registers, sizeof(cpu->exc_suspend_registers));
2483 memcpy(cpu->registers, cpu->exc_trigger_registers, sizeof(cpu->registers));
2484 cpu->exc_suspended = exception;
2488 mips_cpu_exception_resume(SIM_DESC sd, sim_cpu* cpu, int exception)
2490 ASSERT(cpu != NULL);
2492 if(exception == 0 && cpu->exc_suspended > 0)
2494 /* warn not for breakpoints */
2495 if(cpu->exc_suspended != sim_signal_to_host(sd, SIM_SIGTRAP))
2496 sim_io_eprintf(sd, "Warning, resuming but ignoring pending exception signal (%d)\n",
2497 cpu->exc_suspended);
2499 else if(exception != 0 && cpu->exc_suspended > 0)
2501 if(exception != cpu->exc_suspended)
2502 sim_io_eprintf(sd, "Warning, resuming with mismatched exception signal (%d vs %d)\n",
2503 cpu->exc_suspended, exception);
2505 memcpy(cpu->registers, cpu->exc_suspend_registers, sizeof(cpu->registers));
2507 else if(exception != 0 && cpu->exc_suspended == 0)
2509 sim_io_eprintf(sd, "Warning, ignoring spontanous exception signal (%d)\n", exception);
2511 cpu->exc_suspended = 0;
2515 /*---------------------------------------------------------------------------*/
2516 /*> EOF interp.c <*/