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.
21 The IDT monitor (found on the VR4300 board), seems to lie about
22 register contents. It seems to treat the registers as sign-extended
23 32-bit values. This cause *REAL* problems when single-stepping 64-bit
28 /* The TRACE manifests enable the provision of extra features. If they
29 are not defined then a simpler (quicker) simulator is constructed
30 without the required run-time checks, etc. */
31 #if 1 /* 0 to allow user build selection, 1 to force inclusion */
37 #include "sim-utils.h"
38 #include "sim-options.h"
39 #include "sim-assert.h"
65 #include "libiberty.h"
67 #include "callback.h" /* GDB simulator callback interface */
68 #include "remote-sim.h" /* GDB simulator interface */
76 char* pr_addr PARAMS ((SIM_ADDR addr));
77 char* pr_uword64 PARAMS ((uword64 addr));
80 /* Within interp.c we refer to the sim_state and sim_cpu directly. */
85 /* The following reserved instruction value is used when a simulator
86 trap is required. NOTE: Care must be taken, since this value may be
87 used in later revisions of the MIPS ISA. */
89 #define RSVD_INSTRUCTION (0x00000005)
90 #define RSVD_INSTRUCTION_MASK (0xFC00003F)
92 #define RSVD_INSTRUCTION_ARG_SHIFT 6
93 #define RSVD_INSTRUCTION_ARG_MASK 0xFFFFF
96 /* Bits in the Debug register */
97 #define Debug_DBD 0x80000000 /* Debug Branch Delay */
98 #define Debug_DM 0x40000000 /* Debug Mode */
99 #define Debug_DBp 0x00000002 /* Debug Breakpoint indicator */
101 /*---------------------------------------------------------------------------*/
102 /*-- GDB simulator interface ------------------------------------------------*/
103 /*---------------------------------------------------------------------------*/
105 static void ColdReset PARAMS((SIM_DESC sd));
107 /*---------------------------------------------------------------------------*/
111 #define DELAYSLOT() {\
112 if (STATE & simDELAYSLOT)\
113 sim_io_eprintf(sd,"Delay slot already activated (branch in delay slot?)\n");\
114 STATE |= simDELAYSLOT;\
117 #define JALDELAYSLOT() {\
119 STATE |= simJALDELAYSLOT;\
123 STATE &= ~simDELAYSLOT;\
124 STATE |= simSKIPNEXT;\
127 #define CANCELDELAYSLOT() {\
129 STATE &= ~(simDELAYSLOT | simJALDELAYSLOT);\
132 #define INDELAYSLOT() ((STATE & simDELAYSLOT) != 0)
133 #define INJALDELAYSLOT() ((STATE & simJALDELAYSLOT) != 0)
135 #define K0BASE (0x80000000)
136 #define K0SIZE (0x20000000)
137 #define K1BASE (0xA0000000)
138 #define K1SIZE (0x20000000)
139 #define MONITOR_BASE (0xBFC00000)
140 #define MONITOR_SIZE (1 << 11)
141 #define MEM_SIZE (2 << 20)
145 static char *tracefile = "trace.din"; /* default filename for trace log */
146 FILE *tracefh = NULL;
147 static void open_trace PARAMS((SIM_DESC sd));
150 static const char * get_insn_name (sim_cpu *, int);
152 /* simulation target board. NULL=canonical */
153 static char* board = NULL;
156 static DECLARE_OPTION_HANDLER (mips_option_handler);
159 OPTION_DINERO_TRACE = OPTION_START,
166 mips_option_handler (sd, cpu, opt, arg, is_command)
176 case OPTION_DINERO_TRACE: /* ??? */
178 /* Eventually the simTRACE flag could be treated as a toggle, to
179 allow external control of the program points being traced
180 (i.e. only from main onwards, excluding the run-time setup,
182 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
184 sim_cpu *cpu = STATE_CPU (sd, cpu_nr);
187 else if (strcmp (arg, "yes") == 0)
189 else if (strcmp (arg, "no") == 0)
191 else if (strcmp (arg, "on") == 0)
193 else if (strcmp (arg, "off") == 0)
197 fprintf (stderr, "Unrecognized dinero-trace option `%s'\n", arg);
204 Simulator constructed without dinero tracing support (for performance).\n\
205 Re-compile simulator with \"-DTRACE\" to enable this option.\n");
209 case OPTION_DINERO_FILE:
211 if (optarg != NULL) {
213 tmp = (char *)malloc(strlen(optarg) + 1);
216 sim_io_printf(sd,"Failed to allocate buffer for tracefile name \"%s\"\n",optarg);
222 sim_io_printf(sd,"Placing trace information into file \"%s\"\n",tracefile);
232 board = zalloc(strlen(arg) + 1);
243 static const OPTION mips_options[] =
245 { {"dinero-trace", optional_argument, NULL, OPTION_DINERO_TRACE},
246 '\0', "on|off", "Enable dinero tracing",
247 mips_option_handler },
248 { {"dinero-file", required_argument, NULL, OPTION_DINERO_FILE},
249 '\0', "FILE", "Write dinero trace to FILE",
250 mips_option_handler },
251 { {"board", required_argument, NULL, OPTION_BOARD},
252 '\0', "none" /* rely on compile-time string concatenation for other options */
254 #define BOARD_JMR3904 "jmr3904"
256 #define BOARD_JMR3904_PAL "jmr3904pal"
257 "|" BOARD_JMR3904_PAL
258 #define BOARD_JMR3904_DEBUG "jmr3904debug"
259 "|" BOARD_JMR3904_DEBUG
261 , "Customize simulation for a particular board.", mips_option_handler },
263 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
267 int interrupt_pending;
270 interrupt_event (SIM_DESC sd, void *data)
272 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
273 address_word cia = CIA_GET (cpu);
276 interrupt_pending = 0;
277 SignalExceptionInterrupt (1); /* interrupt "1" */
279 else if (!interrupt_pending)
280 sim_events_schedule (sd, 1, interrupt_event, data);
284 /*---------------------------------------------------------------------------*/
285 /*-- Device registration hook -----------------------------------------------*/
286 /*---------------------------------------------------------------------------*/
287 static void device_init(SIM_DESC sd) {
289 extern void register_devices(SIM_DESC);
290 register_devices(sd);
294 /*---------------------------------------------------------------------------*/
295 /*-- GDB simulator interface ------------------------------------------------*/
296 /*---------------------------------------------------------------------------*/
299 sim_open (kind, cb, abfd, argv)
305 SIM_DESC sd = sim_state_alloc (kind, cb);
306 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
308 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
310 /* FIXME: watchpoints code shouldn't need this */
311 STATE_WATCHPOINTS (sd)->pc = &(PC);
312 STATE_WATCHPOINTS (sd)->sizeof_pc = sizeof (PC);
313 STATE_WATCHPOINTS (sd)->interrupt_handler = interrupt_event;
315 /* Initialize the mechanism for doing insn profiling. */
316 CPU_INSN_NAME (cpu) = get_insn_name;
317 CPU_MAX_INSNS (cpu) = nr_itable_entries;
321 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
323 sim_add_option_table (sd, NULL, mips_options);
326 /* getopt will print the error message so we just have to exit if this fails.
327 FIXME: Hmmm... in the case of gdb we need getopt to call
329 if (sim_parse_args (sd, argv) != SIM_RC_OK)
331 /* Uninstall the modules to avoid memory leaks,
332 file descriptor leaks, etc. */
333 sim_module_uninstall (sd);
337 /* handle board-specific memory maps */
340 /* Allocate core managed memory */
343 sim_do_commandf (sd, "memory region 0x%lx,0x%lx", MONITOR_BASE, MONITOR_SIZE);
344 /* For compatibility with the old code - under this (at level one)
345 are the kernel spaces K0 & K1. Both of these map to a single
346 smaller sub region */
347 sim_do_command(sd," memory region 0x7fff8000,0x8000") ; /* MTZ- 32 k stack */
348 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx%%0x%lx,0x%0x",
350 MEM_SIZE, /* actual size */
358 && (strcmp(board, BOARD_JMR3904) == 0 ||
359 strcmp(board, BOARD_JMR3904_PAL) == 0 ||
360 strcmp(board, BOARD_JMR3904_DEBUG) == 0))
362 /* match VIRTUAL memory layout of JMR-TX3904 board */
365 /* --- environment --- */
367 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
371 /* ROM: 0x9FC0_0000 - 0x9FFF_FFFF and 0xBFC0_0000 - 0xBFFF_FFFF */
372 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx,0x%0x",
374 4 * 1024 * 1024, /* 4 MB */
377 /* SRAM: 0x8000_0000 - 0x803F_FFFF and 0xA000_0000 - 0xA03F_FFFF */
378 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx,0x%0x",
380 4 * 1024 * 1024, /* 4 MB */
383 /* DRAM: 0x8800_0000 - 0x89FF_FFFF and 0xA800_0000 - 0xA9FF_FFFF */
384 for (i=0; i<8; i++) /* 32 MB total */
386 unsigned size = 4 * 1024 * 1024; /* 4 MB */
387 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx,0x%0x",
388 0x88000000 + (i * size),
390 0xA8000000 + (i * size));
393 /* Dummy memory regions for unsimulated devices */
395 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xFFFFE010, 0x00c); /* EBIF */
396 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xFFFF9000, 0x200); /* EBIF */
397 sim_do_commandf (sd, "memory alias 0x%lx@1,0x%lx", 0xFFFFF500, 0x300); /* PIO */
399 /* --- simulated devices --- */
400 sim_hw_parse (sd, "/tx3904irc@0xffffc000/reg 0xffffc000 0x20");
401 sim_hw_parse (sd, "/tx3904cpu");
402 sim_hw_parse (sd, "/tx3904tmr@0xfffff000/reg 0xfffff000 0x100");
403 sim_hw_parse (sd, "/tx3904tmr@0xfffff100/reg 0xfffff100 0x100");
404 sim_hw_parse (sd, "/tx3904tmr@0xfffff200/reg 0xfffff200 0x100");
405 sim_hw_parse (sd, "/tx3904sio@0xfffff300/reg 0xfffff300 0x100");
407 /* FIXME: poking at dv-sockser internals, use tcp backend if
408 --sockser_addr option was given.*/
409 extern char* sockser_addr;
410 if(sockser_addr == NULL)
411 sim_hw_parse (sd, "/tx3904sio@0xfffff300/backend stdio");
413 sim_hw_parse (sd, "/tx3904sio@0xfffff300/backend tcp");
415 sim_hw_parse (sd, "/tx3904sio@0xfffff400/reg 0xfffff400 0x100");
416 sim_hw_parse (sd, "/tx3904sio@0xfffff400/backend stdio");
418 /* -- device connections --- */
419 sim_hw_parse (sd, "/tx3904irc > ip level /tx3904cpu");
420 sim_hw_parse (sd, "/tx3904tmr@0xfffff000 > int tmr0 /tx3904irc");
421 sim_hw_parse (sd, "/tx3904tmr@0xfffff100 > int tmr1 /tx3904irc");
422 sim_hw_parse (sd, "/tx3904tmr@0xfffff200 > int tmr2 /tx3904irc");
423 sim_hw_parse (sd, "/tx3904sio@0xfffff300 > int sio0 /tx3904irc");
424 sim_hw_parse (sd, "/tx3904sio@0xfffff400 > int sio1 /tx3904irc");
426 /* add PAL timer & I/O module */
427 if(! strcmp(board, BOARD_JMR3904_PAL))
430 sim_hw_parse (sd, "/pal@0xffff0000");
431 sim_hw_parse (sd, "/pal@0xffff0000/reg 0xffff0000 64");
433 /* wire up interrupt ports to irc */
434 sim_hw_parse (sd, "/pal@0x31000000 > countdown tmr0 /tx3904irc");
435 sim_hw_parse (sd, "/pal@0x31000000 > timer tmr1 /tx3904irc");
436 sim_hw_parse (sd, "/pal@0x31000000 > int int0 /tx3904irc");
439 if(! strcmp(board, BOARD_JMR3904_DEBUG))
441 /* -- DEBUG: glue interrupt generators --- */
442 sim_hw_parse (sd, "/glue@0xffff0000/reg 0xffff0000 0x50");
443 sim_hw_parse (sd, "/glue@0xffff0000 > int0 int0 /tx3904irc");
444 sim_hw_parse (sd, "/glue@0xffff0000 > int1 int1 /tx3904irc");
445 sim_hw_parse (sd, "/glue@0xffff0000 > int2 int2 /tx3904irc");
446 sim_hw_parse (sd, "/glue@0xffff0000 > int3 int3 /tx3904irc");
447 sim_hw_parse (sd, "/glue@0xffff0000 > int4 int4 /tx3904irc");
448 sim_hw_parse (sd, "/glue@0xffff0000 > int5 int5 /tx3904irc");
449 sim_hw_parse (sd, "/glue@0xffff0000 > int6 int6 /tx3904irc");
450 sim_hw_parse (sd, "/glue@0xffff0000 > int7 int7 /tx3904irc");
451 sim_hw_parse (sd, "/glue@0xffff0000 > int8 dmac0 /tx3904irc");
452 sim_hw_parse (sd, "/glue@0xffff0000 > int9 dmac1 /tx3904irc");
453 sim_hw_parse (sd, "/glue@0xffff0000 > int10 dmac2 /tx3904irc");
454 sim_hw_parse (sd, "/glue@0xffff0000 > int11 dmac3 /tx3904irc");
455 sim_hw_parse (sd, "/glue@0xffff0000 > int12 sio0 /tx3904irc");
456 sim_hw_parse (sd, "/glue@0xffff0000 > int13 sio1 /tx3904irc");
457 sim_hw_parse (sd, "/glue@0xffff0000 > int14 tmr0 /tx3904irc");
458 sim_hw_parse (sd, "/glue@0xffff0000 > int15 tmr1 /tx3904irc");
459 sim_hw_parse (sd, "/glue@0xffff0000 > int16 tmr2 /tx3904irc");
460 sim_hw_parse (sd, "/glue@0xffff0000 > int17 nmi /tx3904cpu");
468 /* check for/establish the a reference program image */
469 if (sim_analyze_program (sd,
470 (STATE_PROG_ARGV (sd) != NULL
471 ? *STATE_PROG_ARGV (sd)
475 sim_module_uninstall (sd);
479 /* Configure/verify the target byte order and other runtime
480 configuration options */
481 if (sim_config (sd) != SIM_RC_OK)
483 sim_module_uninstall (sd);
487 if (sim_post_argv_init (sd) != SIM_RC_OK)
489 /* Uninstall the modules to avoid memory leaks,
490 file descriptor leaks, etc. */
491 sim_module_uninstall (sd);
495 /* verify assumptions the simulator made about the host type system.
496 This macro does not return if there is a problem */
497 SIM_ASSERT (sizeof(int) == (4 * sizeof(char)));
498 SIM_ASSERT (sizeof(word64) == (8 * sizeof(char)));
500 /* This is NASTY, in that we are assuming the size of specific
504 for (rn = 0; (rn < (LAST_EMBED_REGNUM + 1)); rn++)
507 cpu->register_widths[rn] = WITH_TARGET_WORD_BITSIZE;
508 else if ((rn >= FGRIDX) && (rn < (FGRIDX + NR_FGR)))
509 cpu->register_widths[rn] = WITH_TARGET_FLOATING_POINT_BITSIZE;
510 else if ((rn >= 33) && (rn <= 37))
511 cpu->register_widths[rn] = WITH_TARGET_WORD_BITSIZE;
512 else if ((rn == SRIDX)
515 || ((rn >= 72) && (rn <= 89)))
516 cpu->register_widths[rn] = 32;
518 cpu->register_widths[rn] = 0;
525 if (STATE & simTRACE)
529 /* Write an abort sequence into the TRAP (common) exception vector
530 addresses. This is to catch code executing a TRAP (et.al.)
531 instruction without installing a trap handler. */
533 unsigned32 halt[2] = { 0x2404002f /* addiu r4, r0, 47 */,
534 HALT_INSTRUCTION /* BREAK */ };
537 sim_write (sd, 0x80000000, (char *) halt, sizeof (halt));
538 sim_write (sd, 0x80000180, (char *) halt, sizeof (halt));
539 sim_write (sd, 0x80000200, (char *) halt, sizeof (halt));
540 sim_write (sd, 0xBFC00200, (char *) halt, sizeof (halt));
541 sim_write (sd, 0xBFC00380, (char *) halt, sizeof (halt));
542 sim_write (sd, 0xBFC00400, (char *) halt, sizeof (halt));
546 /* Write the monitor trap address handlers into the monitor (eeprom)
547 address space. This can only be done once the target endianness
548 has been determined. */
551 /* Entry into the IDT monitor is via fixed address vectors, and
552 not using machine instructions. To avoid clashing with use of
553 the MIPS TRAP system, we place our own (simulator specific)
554 "undefined" instructions into the relevant vector slots. */
555 for (loop = 0; (loop < MONITOR_SIZE); loop += 4)
557 address_word vaddr = (MONITOR_BASE + loop);
558 unsigned32 insn = (RSVD_INSTRUCTION | (((loop >> 2) & RSVD_INSTRUCTION_ARG_MASK) << RSVD_INSTRUCTION_ARG_SHIFT));
560 sim_write (sd, vaddr, (char *)&insn, sizeof (insn));
562 /* The PMON monitor uses the same address space, but rather than
563 branching into it the address of a routine is loaded. We can
564 cheat for the moment, and direct the PMON routine to IDT style
565 instructions within the monitor space. This relies on the IDT
566 monitor not using the locations from 0xBFC00500 onwards as its
568 for (loop = 0; (loop < 24); loop++)
570 address_word vaddr = (MONITOR_BASE + 0x500 + (loop * 4));
571 unsigned32 value = ((0x500 - 8) / 8); /* default UNDEFINED reason code */
587 value = ((0x500 - 16) / 8); /* not an IDT reason code */
589 case 8: /* cliexit */
592 case 11: /* flush_cache */
596 /* FIXME - should monitor_base be SIM_ADDR?? */
597 value = ((unsigned int)MONITOR_BASE + (value * 8));
599 sim_write (sd, vaddr, (char *)&value, sizeof (value));
601 /* The LSI MiniRISC PMON has its vectors at 0x200, not 0x500. */
603 sim_write (sd, vaddr, (char *)&value, sizeof (value));
617 tracefh = fopen(tracefile,"wb+");
620 sim_io_eprintf(sd,"Failed to create file \"%s\", writing trace information to stderr.\n",tracefile);
626 /* Return name of an insn, used by insn profiling. */
628 get_insn_name (sim_cpu *cpu, int i)
630 return itable[i].name;
634 sim_close (sd, quitting)
639 printf("DBG: sim_close: entered (quitting = %d)\n",quitting);
643 /* "quitting" is non-zero if we cannot hang on errors */
645 /* shut down modules */
646 sim_module_uninstall (sd);
648 /* Ensure that any resources allocated through the callback
649 mechanism are released: */
650 sim_io_shutdown (sd);
653 if (tracefh != NULL && tracefh != stderr)
658 /* FIXME - free SD */
665 sim_write (sd,addr,buffer,size)
668 unsigned char *buffer;
672 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
674 /* Return the number of bytes written, or zero if error. */
676 sim_io_printf(sd,"sim_write(0x%s,buffer,%d);\n",pr_addr(addr),size);
679 /* We use raw read and write routines, since we do not want to count
680 the GDB memory accesses in our statistics gathering. */
682 for (index = 0; index < size; index++)
684 address_word vaddr = (address_word)addr + index;
687 if (!address_translation (SD, CPU, NULL_CIA, vaddr, isDATA, isSTORE, &paddr, &cca, isRAW))
689 if (sim_core_write_buffer (SD, CPU, read_map, buffer + index, paddr, 1) != 1)
697 sim_read (sd,addr,buffer,size)
700 unsigned char *buffer;
704 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
706 /* Return the number of bytes read, or zero if error. */
708 sim_io_printf(sd,"sim_read(0x%s,buffer,%d);\n",pr_addr(addr),size);
711 for (index = 0; (index < size); index++)
713 address_word vaddr = (address_word)addr + index;
716 if (!address_translation (SD, CPU, NULL_CIA, vaddr, isDATA, isLOAD, &paddr, &cca, isRAW))
718 if (sim_core_read_buffer (SD, CPU, read_map, buffer + index, paddr, 1) != 1)
726 sim_store_register (sd,rn,memory,length)
729 unsigned char *memory;
732 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
733 /* NOTE: gdb (the client) stores registers in target byte order
734 while the simulator uses host byte order */
736 sim_io_printf(sd,"sim_store_register(%d,*memory=0x%s);\n",rn,pr_addr(*((SIM_ADDR *)memory)));
739 /* Unfortunately this suffers from the same problem as the register
740 numbering one. We need to know what the width of each logical
741 register number is for the architecture being simulated. */
743 if (cpu->register_widths[rn] == 0)
745 sim_io_eprintf(sd,"Invalid register width for %d (register store ignored)\n",rn);
751 if (rn >= FGRIDX && rn < FGRIDX + NR_FGR)
753 if (cpu->register_widths[rn] == 32)
755 cpu->fgr[rn - FGRIDX] = T2H_4 (*(unsigned32*)memory);
760 cpu->fgr[rn - FGRIDX] = T2H_8 (*(unsigned64*)memory);
765 if (cpu->register_widths[rn] == 32)
767 cpu->registers[rn] = T2H_4 (*(unsigned32*)memory);
772 cpu->registers[rn] = T2H_8 (*(unsigned64*)memory);
780 sim_fetch_register (sd,rn,memory,length)
783 unsigned char *memory;
786 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
787 /* NOTE: gdb (the client) stores registers in target byte order
788 while the simulator uses host byte order */
790 #if 0 /* FIXME: doesn't compile */
791 sim_io_printf(sd,"sim_fetch_register(%d=0x%s,mem) : place simulator registers into memory\n",rn,pr_addr(registers[rn]));
795 if (cpu->register_widths[rn] == 0)
797 sim_io_eprintf (sd, "Invalid register width for %d (register fetch ignored)\n",rn);
803 /* Any floating point register */
804 if (rn >= FGRIDX && rn < FGRIDX + NR_FGR)
806 if (cpu->register_widths[rn] == 32)
808 *(unsigned32*)memory = H2T_4 (cpu->fgr[rn - FGRIDX]);
813 *(unsigned64*)memory = H2T_8 (cpu->fgr[rn - FGRIDX]);
818 if (cpu->register_widths[rn] == 32)
820 *(unsigned32*)memory = H2T_4 ((unsigned32)(cpu->registers[rn]));
825 *(unsigned64*)memory = H2T_8 ((unsigned64)(cpu->registers[rn]));
834 sim_create_inferior (sd, abfd, argv,env)
842 #if 0 /* FIXME: doesn't compile */
843 printf("DBG: sim_create_inferior entered: start_address = 0x%s\n",
852 /* override PC value set by ColdReset () */
854 for (cpu_nr = 0; cpu_nr < sim_engine_nr_cpus (sd); cpu_nr++)
856 sim_cpu *cpu = STATE_CPU (sd, cpu_nr);
857 CIA_SET (cpu, (unsigned64) bfd_get_start_address (abfd));
861 #if 0 /* def DEBUG */
864 /* We should really place the argv slot values into the argument
865 registers, and onto the stack as required. However, this
866 assumes that we have a stack defined, which is not
867 necessarily true at the moment. */
869 sim_io_printf(sd,"sim_create_inferior() : passed arguments ignored\n");
870 for (cptr = argv; (cptr && *cptr); cptr++)
871 printf("DBG: arg \"%s\"\n",*cptr);
879 sim_do_command (sd,cmd)
883 if (sim_args_command (sd, cmd) != SIM_RC_OK)
884 sim_io_printf (sd, "Error: \"%s\" is not a valid MIPS simulator command.\n",
888 /*---------------------------------------------------------------------------*/
889 /*-- Private simulator support interface ------------------------------------*/
890 /*---------------------------------------------------------------------------*/
892 /* Read a null terminated string from memory, return in a buffer */
894 fetch_str (SIM_DESC sd,
900 while (sim_read (sd, addr + nr, &null, 1) == 1 && null != 0)
902 buf = NZALLOC (char, nr + 1);
903 sim_read (sd, addr, buf, nr);
907 /* Simple monitor interface (currently setup for the IDT and PMON monitors) */
909 sim_monitor (SIM_DESC sd,
915 printf("DBG: sim_monitor: entered (reason = %d)\n",reason);
918 /* The IDT monitor actually allows two instructions per vector
919 slot. However, the simulator currently causes a trap on each
920 individual instruction. We cheat, and lose the bottom bit. */
923 /* The following callback functions are available, however the
924 monitor we are simulating does not make use of them: get_errno,
925 isatty, lseek, rename, system, time and unlink */
929 case 6: /* int open(char *path,int flags) */
931 char *path = fetch_str (sd, A0);
932 V0 = sim_io_open (sd, path, (int)A1);
937 case 7: /* int read(int file,char *ptr,int len) */
941 char *buf = zalloc (nr);
942 V0 = sim_io_read (sd, fd, buf, nr);
943 sim_write (sd, A1, buf, nr);
948 case 8: /* int write(int file,char *ptr,int len) */
952 char *buf = zalloc (nr);
953 sim_read (sd, A1, buf, nr);
954 V0 = sim_io_write (sd, fd, buf, nr);
959 case 10: /* int close(int file) */
961 V0 = sim_io_close (sd, (int)A0);
965 case 2: /* Densan monitor: char inbyte(int waitflag) */
967 if (A0 == 0) /* waitflag == NOWAIT */
968 V0 = (unsigned_word)-1;
970 /* Drop through to case 11 */
972 case 11: /* char inbyte(void) */
975 if (sim_io_read_stdin (sd, &tmp, sizeof(char)) != sizeof(char))
977 sim_io_error(sd,"Invalid return from character read");
978 V0 = (unsigned_word)-1;
981 V0 = (unsigned_word)tmp;
985 case 3: /* Densan monitor: void co(char chr) */
986 case 12: /* void outbyte(char chr) : write a byte to "stdout" */
988 char tmp = (char)(A0 & 0xFF);
989 sim_io_write_stdout (sd, &tmp, sizeof(char));
993 case 17: /* void _exit() */
995 sim_io_eprintf (sd, "sim_monitor(17): _exit(int reason) to be coded\n");
996 sim_engine_halt (SD, CPU, NULL, NULL_CIA, sim_exited,
997 (unsigned int)(A0 & 0xFFFFFFFF));
1001 case 28 : /* PMON flush_cache */
1004 case 55: /* void get_mem_info(unsigned int *ptr) */
1005 /* in: A0 = pointer to three word memory location */
1006 /* out: [A0 + 0] = size */
1007 /* [A0 + 4] = instruction cache size */
1008 /* [A0 + 8] = data cache size */
1010 unsigned_4 value = MEM_SIZE /* FIXME STATE_MEM_SIZE (sd) */;
1011 unsigned_4 zero = 0;
1013 sim_write (sd, A0 + 0, (char *)&value, 4);
1014 sim_write (sd, A0 + 4, (char *)&zero, 4);
1015 sim_write (sd, A0 + 8, (char *)&zero, 4);
1016 /* sim_io_eprintf (sd, "sim: get_mem_info() depreciated\n"); */
1020 case 158 : /* PMON printf */
1021 /* in: A0 = pointer to format string */
1022 /* A1 = optional argument 1 */
1023 /* A2 = optional argument 2 */
1024 /* A3 = optional argument 3 */
1026 /* The following is based on the PMON printf source */
1028 address_word s = A0;
1030 signed_word *ap = &A1; /* 1st argument */
1031 /* This isn't the quickest way, since we call the host print
1032 routine for every character almost. But it does avoid
1033 having to allocate and manage a temporary string buffer. */
1034 /* TODO: Include check that we only use three arguments (A1,
1036 while (sim_read (sd, s++, &c, 1) && c != '\0')
1041 enum {FMT_RJUST, FMT_LJUST, FMT_RJUST0, FMT_CENTER} fmt = FMT_RJUST;
1042 int width = 0, trunc = 0, haddot = 0, longlong = 0;
1043 while (sim_read (sd, s++, &c, 1) && c != '\0')
1045 if (strchr ("dobxXulscefg%", c))
1060 else if (c >= '1' && c <= '9')
1064 while (sim_read (sd, s++, &c, 1) == 1 && isdigit (c))
1067 n = (unsigned int)strtol(tmp,NULL,10);
1080 sim_io_printf (sd, "%%");
1085 address_word p = *ap++;
1087 while (sim_read (sd, p++, &ch, 1) == 1 && ch != '\0')
1088 sim_io_printf(sd, "%c", ch);
1091 sim_io_printf(sd,"(null)");
1094 sim_io_printf (sd, "%c", (int)*ap++);
1099 sim_read (sd, s++, &c, 1);
1103 sim_read (sd, s++, &c, 1);
1106 if (strchr ("dobxXu", c))
1108 word64 lv = (word64) *ap++;
1110 sim_io_printf(sd,"<binary not supported>");
1113 sprintf (tmp, "%%%s%c", longlong ? "ll" : "", c);
1115 sim_io_printf(sd, tmp, lv);
1117 sim_io_printf(sd, tmp, (int)lv);
1120 else if (strchr ("eEfgG", c))
1122 double dbl = *(double*)(ap++);
1123 sprintf (tmp, "%%%d.%d%c", width, trunc, c);
1124 sim_io_printf (sd, tmp, dbl);
1130 sim_io_printf(sd, "%c", c);
1136 sim_io_error (sd, "TODO: sim_monitor(%d) : PC = 0x%s\n",
1137 reason, pr_addr(cia));
1143 /* Store a word into memory. */
1146 store_word (SIM_DESC sd,
1155 if ((vaddr & 3) != 0)
1156 SignalExceptionAddressStore ();
1159 if (AddressTranslation (vaddr, isDATA, isSTORE, &paddr, &uncached,
1162 const uword64 mask = 7;
1166 paddr = (paddr & ~mask) | ((paddr & mask) ^ (ReverseEndian << 2));
1167 byte = (vaddr & mask) ^ (BigEndianCPU << 2);
1168 memval = ((uword64) val) << (8 * byte);
1169 StoreMemory (uncached, AccessLength_WORD, memval, 0, paddr, vaddr,
1175 /* Load a word from memory. */
1178 load_word (SIM_DESC sd,
1183 if ((vaddr & 3) != 0)
1185 SIM_CORE_SIGNAL (SD, cpu, cia, read_map, AccessLength_WORD+1, vaddr, read_transfer, sim_core_unaligned_signal);
1192 if (AddressTranslation (vaddr, isDATA, isLOAD, &paddr, &uncached,
1195 const uword64 mask = 0x7;
1196 const unsigned int reverse = ReverseEndian ? 1 : 0;
1197 const unsigned int bigend = BigEndianCPU ? 1 : 0;
1201 paddr = (paddr & ~mask) | ((paddr & mask) ^ (reverse << 2));
1202 LoadMemory (&memval,NULL,uncached, AccessLength_WORD, paddr, vaddr,
1204 byte = (vaddr & mask) ^ (bigend << 2);
1205 return SIGNEXTEND (((memval >> (8 * byte)) & 0xffffffff), 32);
1212 /* Simulate the mips16 entry and exit pseudo-instructions. These
1213 would normally be handled by the reserved instruction exception
1214 code, but for ease of simulation we just handle them directly. */
1217 mips16_entry (SIM_DESC sd,
1222 int aregs, sregs, rreg;
1225 printf("DBG: mips16_entry: entered (insn = 0x%08X)\n",insn);
1228 aregs = (insn & 0x700) >> 8;
1229 sregs = (insn & 0x0c0) >> 6;
1230 rreg = (insn & 0x020) >> 5;
1232 /* This should be checked by the caller. */
1241 /* This is the entry pseudo-instruction. */
1243 for (i = 0; i < aregs; i++)
1244 store_word (SD, CPU, cia, (uword64) (SP + 4 * i), GPR[i + 4]);
1252 store_word (SD, CPU, cia, (uword64) tsp, RA);
1255 for (i = 0; i < sregs; i++)
1258 store_word (SD, CPU, cia, (uword64) tsp, GPR[16 + i]);
1266 /* This is the exit pseudo-instruction. */
1273 RA = load_word (SD, CPU, cia, (uword64) tsp);
1276 for (i = 0; i < sregs; i++)
1279 GPR[i + 16] = load_word (SD, CPU, cia, (uword64) tsp);
1284 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
1288 FGR[0] = WORD64LO (GPR[4]);
1289 FPR_STATE[0] = fmt_uninterpreted;
1291 else if (aregs == 6)
1293 FGR[0] = WORD64LO (GPR[5]);
1294 FGR[1] = WORD64LO (GPR[4]);
1295 FPR_STATE[0] = fmt_uninterpreted;
1296 FPR_STATE[1] = fmt_uninterpreted;
1305 /*-- trace support ----------------------------------------------------------*/
1307 /* The TRACE support is provided (if required) in the memory accessing
1308 routines. Since we are also providing the architecture specific
1309 features, the architecture simulation code can also deal with
1310 notifying the TRACE world of cache flushes, etc. Similarly we do
1311 not need to provide profiling support in the simulator engine,
1312 since we can sample in the instruction fetch control loop. By
1313 defining the TRACE manifest, we add tracing as a run-time
1317 /* Tracing by default produces "din" format (as required by
1318 dineroIII). Each line of such a trace file *MUST* have a din label
1319 and address field. The rest of the line is ignored, so comments can
1320 be included if desired. The first field is the label which must be
1321 one of the following values:
1326 3 escape record (treated as unknown access type)
1327 4 escape record (causes cache flush)
1329 The address field is a 32bit (lower-case) hexadecimal address
1330 value. The address should *NOT* be preceded by "0x".
1332 The size of the memory transfer is not important when dealing with
1333 cache lines (as long as no more than a cache line can be
1334 transferred in a single operation :-), however more information
1335 could be given following the dineroIII requirement to allow more
1336 complete memory and cache simulators to provide better
1337 results. i.e. the University of Pisa has a cache simulator that can
1338 also take bus size and speed as (variable) inputs to calculate
1339 complete system performance (a much more useful ability when trying
1340 to construct an end product, rather than a processor). They
1341 currently have an ARM version of their tool called ChARM. */
1345 dotrace (SIM_DESC sd,
1353 if (STATE & simTRACE) {
1355 fprintf(tracefh,"%d %s ; width %d ; ",
1359 va_start(ap,comment);
1360 vfprintf(tracefh,comment,ap);
1362 fprintf(tracefh,"\n");
1364 /* NOTE: Since the "din" format will only accept 32bit addresses, and
1365 we may be generating 64bit ones, we should put the hi-32bits of the
1366 address into the comment field. */
1368 /* TODO: Provide a buffer for the trace lines. We can then avoid
1369 performing writes until the buffer is filled, or the file is
1372 /* NOTE: We could consider adding a comment field to the "din" file
1373 produced using type 3 markers (unknown access). This would then
1374 allow information about the program that the "din" is for, and
1375 the MIPs world that was being simulated, to be placed into the
1382 /*---------------------------------------------------------------------------*/
1383 /*-- simulator engine -------------------------------------------------------*/
1384 /*---------------------------------------------------------------------------*/
1387 ColdReset (SIM_DESC sd)
1390 for (cpu_nr = 0; cpu_nr < sim_engine_nr_cpus (sd); cpu_nr++)
1392 sim_cpu *cpu = STATE_CPU (sd, cpu_nr);
1393 /* RESET: Fixed PC address: */
1394 PC = (unsigned_word) UNSIGNED64 (0xFFFFFFFFBFC00000);
1395 /* The reset vector address is in the unmapped, uncached memory space. */
1397 SR &= ~(status_SR | status_TS | status_RP);
1398 SR |= (status_ERL | status_BEV);
1400 /* Cheat and allow access to the complete register set immediately */
1401 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT
1402 && WITH_TARGET_WORD_BITSIZE == 64)
1403 SR |= status_FR; /* 64bit registers */
1405 /* Ensure that any instructions with pending register updates are
1407 PENDING_INVALIDATE();
1409 /* Initialise the FPU registers to the unknown state */
1410 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
1413 for (rn = 0; (rn < 32); rn++)
1414 FPR_STATE[rn] = fmt_uninterpreted;
1423 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1424 /* Signal an exception condition. This will result in an exception
1425 that aborts the instruction. The instruction operation pseudocode
1426 will never see a return from this function call. */
1429 signal_exception (SIM_DESC sd,
1437 sim_io_printf(sd,"DBG: SignalException(%d) PC = 0x%s\n",exception,pr_addr(cia));
1440 /* Ensure that any active atomic read/modify/write operation will fail: */
1443 /* Save registers before interrupt dispatching */
1444 #ifdef SIM_CPU_EXCEPTION_TRIGGER
1445 SIM_CPU_EXCEPTION_TRIGGER(sd, cpu, cia);
1448 switch (exception) {
1450 case DebugBreakPoint :
1451 if (! (Debug & Debug_DM))
1457 Debug |= Debug_DBD; /* signaled from within in delay slot */
1458 DEPC = cia - 4; /* reference the branch instruction */
1462 Debug &= ~Debug_DBD; /* not signaled from within a delay slot */
1466 Debug |= Debug_DM; /* in debugging mode */
1467 Debug |= Debug_DBp; /* raising a DBp exception */
1469 sim_engine_restart (SD, CPU, NULL, NULL_CIA);
1473 case ReservedInstruction :
1476 unsigned int instruction;
1477 va_start(ap,exception);
1478 instruction = va_arg(ap,unsigned int);
1480 /* Provide simple monitor support using ReservedInstruction
1481 exceptions. The following code simulates the fixed vector
1482 entry points into the IDT monitor by causing a simulator
1483 trap, performing the monitor operation, and returning to
1484 the address held in the $ra register (standard PCS return
1485 address). This means we only need to pre-load the vector
1486 space with suitable instruction values. For systems were
1487 actual trap instructions are used, we would not need to
1488 perform this magic. */
1489 if ((instruction & RSVD_INSTRUCTION_MASK) == RSVD_INSTRUCTION)
1491 sim_monitor (SD, CPU, cia, ((instruction >> RSVD_INSTRUCTION_ARG_SHIFT) & RSVD_INSTRUCTION_ARG_MASK) );
1492 /* NOTE: This assumes that a branch-and-link style
1493 instruction was used to enter the vector (which is the
1494 case with the current IDT monitor). */
1495 sim_engine_restart (SD, CPU, NULL, RA);
1497 /* Look for the mips16 entry and exit instructions, and
1498 simulate a handler for them. */
1499 else if ((cia & 1) != 0
1500 && (instruction & 0xf81f) == 0xe809
1501 && (instruction & 0x0c0) != 0x0c0)
1503 mips16_entry (SD, CPU, cia, instruction);
1504 sim_engine_restart (sd, NULL, NULL, NULL_CIA);
1506 /* else fall through to normal exception processing */
1507 sim_io_eprintf(sd,"ReservedInstruction at PC = 0x%s\n", pr_addr (cia));
1511 /* Store exception code into current exception id variable (used
1514 /* TODO: If not simulating exceptions then stop the simulator
1515 execution. At the moment we always stop the simulation. */
1517 #ifdef SUBTARGET_R3900
1518 /* update interrupt-related registers */
1520 /* insert exception code in bits 6:2 */
1521 CAUSE = LSMASKED32(CAUSE, 31, 7) | LSINSERTED32(exception, 6, 2);
1522 /* shift IE/KU history bits left */
1523 SR = LSMASKED32(SR, 31, 4) | LSINSERTED32(LSEXTRACTED32(SR, 3, 0), 5, 2);
1525 if (STATE & simDELAYSLOT)
1527 STATE &= ~simDELAYSLOT;
1529 EPC = (cia - 4); /* reference the branch instruction */
1534 if (SR & status_BEV)
1535 PC = (signed)0xBFC00000 + 0x180;
1537 PC = (signed)0x80000000 + 0x080;
1539 /* See figure 5-17 for an outline of the code below */
1540 if (! (SR & status_EXL))
1542 CAUSE = (exception << 2);
1543 if (STATE & simDELAYSLOT)
1545 STATE &= ~simDELAYSLOT;
1547 EPC = (cia - 4); /* reference the branch instruction */
1551 /* FIXME: TLB et.al. */
1552 /* vector = 0x180; */
1556 CAUSE = (exception << 2);
1557 /* vector = 0x180; */
1560 /* Store exception code into current exception id variable (used
1563 if (SR & status_BEV)
1564 PC = (signed)0xBFC00200 + 0x180;
1566 PC = (signed)0x80000000 + 0x180;
1569 switch ((CAUSE >> 2) & 0x1F)
1572 /* Interrupts arrive during event processing, no need to
1578 #ifdef SUBTARGET_3900
1579 /* Exception vector: BEV=0 BFC00000 / BEF=1 BFC00000 */
1580 PC = (signed)0xBFC00000;
1581 #endif SUBTARGET_3900
1584 case TLBModification:
1589 case InstructionFetch:
1591 /* The following is so that the simulator will continue from the
1592 exception handler address. */
1593 sim_engine_halt (SD, CPU, NULL, PC,
1594 sim_stopped, SIM_SIGBUS);
1596 case ReservedInstruction:
1597 case CoProcessorUnusable:
1599 sim_engine_halt (SD, CPU, NULL, PC,
1600 sim_stopped, SIM_SIGILL);
1602 case IntegerOverflow:
1604 sim_engine_halt (SD, CPU, NULL, PC,
1605 sim_stopped, SIM_SIGFPE);
1608 sim_engine_halt (SD, CPU, NULL, PC, sim_stopped, SIM_SIGTRAP);
1613 sim_engine_restart (SD, CPU, NULL, PC);
1618 sim_engine_halt (SD, CPU, NULL, PC,
1619 sim_stopped, SIM_SIGTRAP);
1621 default : /* Unknown internal exception */
1623 sim_engine_halt (SD, CPU, NULL, PC,
1624 sim_stopped, SIM_SIGABRT);
1628 case SimulatorFault:
1632 va_start(ap,exception);
1633 msg = va_arg(ap,char *);
1635 sim_engine_abort (SD, CPU, NULL_CIA,
1636 "FATAL: Simulator error \"%s\"\n",msg);
1645 #if defined(WARN_RESULT)
1646 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1647 /* This function indicates that the result of the operation is
1648 undefined. However, this should not affect the instruction
1649 stream. All that is meant to happen is that the destination
1650 register is set to an undefined result. To keep the simulator
1651 simple, we just don't bother updating the destination register, so
1652 the overall result will be undefined. If desired we can stop the
1653 simulator by raising a pseudo-exception. */
1654 #define UndefinedResult() undefined_result (sd,cia)
1656 undefined_result(sd,cia)
1660 sim_io_eprintf(sd,"UndefinedResult: PC = 0x%s\n",pr_addr(cia));
1661 #if 0 /* Disabled for the moment, since it actually happens a lot at the moment. */
1666 #endif /* WARN_RESULT */
1668 /*-- FPU support routines ---------------------------------------------------*/
1670 /* Numbers are held in normalized form. The SINGLE and DOUBLE binary
1671 formats conform to ANSI/IEEE Std 754-1985. */
1672 /* SINGLE precision floating:
1673 * seeeeeeeefffffffffffffffffffffff
1675 * e = 8bits = exponent
1676 * f = 23bits = fraction
1678 /* SINGLE precision fixed:
1679 * siiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
1681 * i = 31bits = integer
1683 /* DOUBLE precision floating:
1684 * seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
1686 * e = 11bits = exponent
1687 * f = 52bits = fraction
1689 /* DOUBLE precision fixed:
1690 * siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
1692 * i = 63bits = integer
1695 /* Extract sign-bit: */
1696 #define FP_S_s(v) (((v) & ((unsigned)1 << 31)) ? 1 : 0)
1697 #define FP_D_s(v) (((v) & ((uword64)1 << 63)) ? 1 : 0)
1698 /* Extract biased exponent: */
1699 #define FP_S_be(v) (((v) >> 23) & 0xFF)
1700 #define FP_D_be(v) (((v) >> 52) & 0x7FF)
1701 /* Extract unbiased Exponent: */
1702 #define FP_S_e(v) (FP_S_be(v) - 0x7F)
1703 #define FP_D_e(v) (FP_D_be(v) - 0x3FF)
1704 /* Extract complete fraction field: */
1705 #define FP_S_f(v) ((v) & ~((unsigned)0x1FF << 23))
1706 #define FP_D_f(v) ((v) & ~((uword64)0xFFF << 52))
1707 /* Extract numbered fraction bit: */
1708 #define FP_S_fb(b,v) (((v) & (1 << (23 - (b)))) ? 1 : 0)
1709 #define FP_D_fb(b,v) (((v) & (1 << (52 - (b)))) ? 1 : 0)
1711 /* Explicit QNaN values used when value required: */
1712 #define FPQNaN_SINGLE (0x7FBFFFFF)
1713 #define FPQNaN_WORD (0x7FFFFFFF)
1714 #define FPQNaN_DOUBLE (((uword64)0x7FF7FFFF << 32) | 0xFFFFFFFF)
1715 #define FPQNaN_LONG (((uword64)0x7FFFFFFF << 32) | 0xFFFFFFFF)
1717 /* Explicit Infinity values used when required: */
1718 #define FPINF_SINGLE (0x7F800000)
1719 #define FPINF_DOUBLE (((uword64)0x7FF00000 << 32) | 0x00000000)
1721 #define RMMODE(v) (((v) == FP_RM_NEAREST) ? "Round" : (((v) == FP_RM_TOZERO) ? "Trunc" : (((v) == FP_RM_TOPINF) ? "Ceil" : "Floor")))
1722 #define DOFMT(v) (((v) == fmt_single) ? "single" : (((v) == fmt_double) ? "double" : (((v) == fmt_word) ? "word" : (((v) == fmt_long) ? "long" : (((v) == fmt_unknown) ? "<unknown>" : (((v) == fmt_uninterpreted) ? "<uninterpreted>" : (((v) == fmt_uninterpreted_32) ? "<uninterpreted_32>" : (((v) == fmt_uninterpreted_64) ? "<uninterpreted_64>" : "<format error>"))))))))
1725 value_fpr (SIM_DESC sd,
1734 /* Treat unused register values, as fixed-point 64bit values: */
1735 if ((fmt == fmt_uninterpreted) || (fmt == fmt_unknown))
1737 /* If request to read data as "uninterpreted", then use the current
1739 fmt = FPR_STATE[fpr];
1744 /* For values not yet accessed, set to the desired format: */
1745 if (FPR_STATE[fpr] == fmt_uninterpreted) {
1746 FPR_STATE[fpr] = fmt;
1748 printf("DBG: Register %d was fmt_uninterpreted. Now %s\n",fpr,DOFMT(fmt));
1751 if (fmt != FPR_STATE[fpr]) {
1752 sim_io_eprintf(sd,"FPR %d (format %s) being accessed with format %s - setting to unknown (PC = 0x%s)\n",fpr,DOFMT(FPR_STATE[fpr]),DOFMT(fmt),pr_addr(cia));
1753 FPR_STATE[fpr] = fmt_unknown;
1756 if (FPR_STATE[fpr] == fmt_unknown) {
1757 /* Set QNaN value: */
1760 value = FPQNaN_SINGLE;
1764 value = FPQNaN_DOUBLE;
1768 value = FPQNaN_WORD;
1772 value = FPQNaN_LONG;
1779 } else if (SizeFGR() == 64) {
1783 value = (FGR[fpr] & 0xFFFFFFFF);
1786 case fmt_uninterpreted:
1800 value = (FGR[fpr] & 0xFFFFFFFF);
1803 case fmt_uninterpreted:
1806 if ((fpr & 1) == 0) { /* even registers only */
1808 printf("DBG: ValueFPR: FGR[%d] = %s, FGR[%d] = %s\n",
1809 fpr+1, pr_uword64( (uword64) FGR[fpr+1] ),
1810 fpr, pr_uword64( (uword64) FGR[fpr] ));
1812 value = ((((uword64)FGR[fpr+1]) << 32) | (FGR[fpr] & 0xFFFFFFFF));
1814 SignalException(ReservedInstruction,0);
1825 SignalExceptionSimulatorFault ("Unrecognised FP format in ValueFPR()");
1828 printf("DBG: ValueFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR() = %d\n",fpr,DOFMT(fmt),pr_uword64(value),pr_addr(cia),SizeFGR());
1835 store_fpr (SIM_DESC sd,
1845 printf("DBG: StoreFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR() = %d,\n",fpr,DOFMT(fmt),pr_uword64(value),pr_addr(cia),SizeFGR());
1848 if (SizeFGR() == 64) {
1850 case fmt_uninterpreted_32:
1851 fmt = fmt_uninterpreted;
1854 FGR[fpr] = (((uword64)0xDEADC0DE << 32) | (value & 0xFFFFFFFF));
1855 FPR_STATE[fpr] = fmt;
1858 case fmt_uninterpreted_64:
1859 fmt = fmt_uninterpreted;
1860 case fmt_uninterpreted:
1864 FPR_STATE[fpr] = fmt;
1868 FPR_STATE[fpr] = fmt_unknown;
1874 case fmt_uninterpreted_32:
1875 fmt = fmt_uninterpreted;
1878 FGR[fpr] = (value & 0xFFFFFFFF);
1879 FPR_STATE[fpr] = fmt;
1882 case fmt_uninterpreted_64:
1883 fmt = fmt_uninterpreted;
1884 case fmt_uninterpreted:
1887 if ((fpr & 1) == 0) { /* even register number only */
1888 FGR[fpr+1] = (value >> 32);
1889 FGR[fpr] = (value & 0xFFFFFFFF);
1890 FPR_STATE[fpr + 1] = fmt;
1891 FPR_STATE[fpr] = fmt;
1893 FPR_STATE[fpr] = fmt_unknown;
1894 FPR_STATE[fpr + 1] = fmt_unknown;
1895 SignalException(ReservedInstruction,0);
1900 FPR_STATE[fpr] = fmt_unknown;
1905 #if defined(WARN_RESULT)
1908 #endif /* WARN_RESULT */
1911 SignalExceptionSimulatorFault ("Unrecognised FP format in StoreFPR()");
1914 printf("DBG: StoreFPR: fpr[%d] = 0x%s (format %s)\n",fpr,pr_uword64(FGR[fpr]),DOFMT(fmt));
1931 sim_fpu_32to (&wop, op);
1932 boolean = sim_fpu_is_nan (&wop);
1939 sim_fpu_64to (&wop, op);
1940 boolean = sim_fpu_is_nan (&wop);
1944 fprintf (stderr, "Bad switch\n");
1949 printf("DBG: NaN: returning %d for 0x%s (format = %s)\n",boolean,pr_addr(op),DOFMT(fmt));
1963 printf("DBG: Infinity: format %s 0x%s\n",DOFMT(fmt),pr_addr(op));
1970 sim_fpu_32to (&wop, op);
1971 boolean = sim_fpu_is_infinity (&wop);
1977 sim_fpu_64to (&wop, op);
1978 boolean = sim_fpu_is_infinity (&wop);
1982 printf("DBG: TODO: unrecognised format (%s) for Infinity check\n",DOFMT(fmt));
1987 printf("DBG: Infinity: returning %d for 0x%s (format = %s)\n",boolean,pr_addr(op),DOFMT(fmt));
2001 /* Argument checking already performed by the FPCOMPARE code */
2004 printf("DBG: Less: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2007 /* The format type should already have been checked: */
2013 sim_fpu_32to (&wop1, op1);
2014 sim_fpu_32to (&wop2, op2);
2015 boolean = sim_fpu_is_lt (&wop1, &wop2);
2022 sim_fpu_64to (&wop1, op1);
2023 sim_fpu_64to (&wop2, op2);
2024 boolean = sim_fpu_is_lt (&wop1, &wop2);
2028 fprintf (stderr, "Bad switch\n");
2033 printf("DBG: Less: returning %d (format = %s)\n",boolean,DOFMT(fmt));
2047 /* Argument checking already performed by the FPCOMPARE code */
2050 printf("DBG: Equal: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2053 /* The format type should already have been checked: */
2059 sim_fpu_32to (&wop1, op1);
2060 sim_fpu_32to (&wop2, op2);
2061 boolean = sim_fpu_is_eq (&wop1, &wop2);
2068 sim_fpu_64to (&wop1, op1);
2069 sim_fpu_64to (&wop2, op2);
2070 boolean = sim_fpu_is_eq (&wop1, &wop2);
2074 fprintf (stderr, "Bad switch\n");
2079 printf("DBG: Equal: returning %d (format = %s)\n",boolean,DOFMT(fmt));
2086 AbsoluteValue(op,fmt)
2093 printf("DBG: AbsoluteValue: %s: op = 0x%s\n",DOFMT(fmt),pr_addr(op));
2096 /* The format type should already have been checked: */
2102 sim_fpu_32to (&wop, op);
2103 sim_fpu_abs (&wop, &wop);
2104 sim_fpu_to32 (&ans, &wop);
2112 sim_fpu_64to (&wop, op);
2113 sim_fpu_abs (&wop, &wop);
2114 sim_fpu_to64 (&ans, &wop);
2119 fprintf (stderr, "Bad switch\n");
2134 printf("DBG: Negate: %s: op = 0x%s\n",DOFMT(fmt),pr_addr(op));
2137 /* The format type should already have been checked: */
2143 sim_fpu_32to (&wop, op);
2144 sim_fpu_neg (&wop, &wop);
2145 sim_fpu_to32 (&ans, &wop);
2153 sim_fpu_64to (&wop, op);
2154 sim_fpu_neg (&wop, &wop);
2155 sim_fpu_to64 (&ans, &wop);
2160 fprintf (stderr, "Bad switch\n");
2176 printf("DBG: Add: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2179 /* The registers must specify FPRs valid for operands of type
2180 "fmt". If they are not valid, the result is undefined. */
2182 /* The format type should already have been checked: */
2190 sim_fpu_32to (&wop1, op1);
2191 sim_fpu_32to (&wop2, op2);
2192 sim_fpu_add (&ans, &wop1, &wop2);
2193 sim_fpu_to32 (&res, &ans);
2203 sim_fpu_64to (&wop1, op1);
2204 sim_fpu_64to (&wop2, op2);
2205 sim_fpu_add (&ans, &wop1, &wop2);
2206 sim_fpu_to64 (&res, &ans);
2211 fprintf (stderr, "Bad switch\n");
2216 printf("DBG: Add: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2231 printf("DBG: Sub: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2234 /* The registers must specify FPRs valid for operands of type
2235 "fmt". If they are not valid, the result is undefined. */
2237 /* The format type should already have been checked: */
2245 sim_fpu_32to (&wop1, op1);
2246 sim_fpu_32to (&wop2, op2);
2247 sim_fpu_sub (&ans, &wop1, &wop2);
2248 sim_fpu_to32 (&res, &ans);
2258 sim_fpu_64to (&wop1, op1);
2259 sim_fpu_64to (&wop2, op2);
2260 sim_fpu_sub (&ans, &wop1, &wop2);
2261 sim_fpu_to64 (&res, &ans);
2266 fprintf (stderr, "Bad switch\n");
2271 printf("DBG: Sub: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2278 Multiply(op1,op2,fmt)
2286 printf("DBG: Multiply: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2289 /* The registers must specify FPRs valid for operands of type
2290 "fmt". If they are not valid, the result is undefined. */
2292 /* The format type should already have been checked: */
2300 sim_fpu_32to (&wop1, op1);
2301 sim_fpu_32to (&wop2, op2);
2302 sim_fpu_mul (&ans, &wop1, &wop2);
2303 sim_fpu_to32 (&res, &ans);
2313 sim_fpu_64to (&wop1, op1);
2314 sim_fpu_64to (&wop2, op2);
2315 sim_fpu_mul (&ans, &wop1, &wop2);
2316 sim_fpu_to64 (&res, &ans);
2321 fprintf (stderr, "Bad switch\n");
2326 printf("DBG: Multiply: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2341 printf("DBG: Divide: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2344 /* The registers must specify FPRs valid for operands of type
2345 "fmt". If they are not valid, the result is undefined. */
2347 /* The format type should already have been checked: */
2355 sim_fpu_32to (&wop1, op1);
2356 sim_fpu_32to (&wop2, op2);
2357 sim_fpu_div (&ans, &wop1, &wop2);
2358 sim_fpu_to32 (&res, &ans);
2368 sim_fpu_64to (&wop1, op1);
2369 sim_fpu_64to (&wop2, op2);
2370 sim_fpu_div (&ans, &wop1, &wop2);
2371 sim_fpu_to64 (&res, &ans);
2376 fprintf (stderr, "Bad switch\n");
2381 printf("DBG: Divide: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2395 printf("DBG: Recip: %s: op = 0x%s\n",DOFMT(fmt),pr_addr(op));
2398 /* The registers must specify FPRs valid for operands of type
2399 "fmt". If they are not valid, the result is undefined. */
2401 /* The format type should already have been checked: */
2408 sim_fpu_32to (&wop, op);
2409 sim_fpu_inv (&ans, &wop);
2410 sim_fpu_to32 (&res, &ans);
2419 sim_fpu_64to (&wop, op);
2420 sim_fpu_inv (&ans, &wop);
2421 sim_fpu_to64 (&res, &ans);
2426 fprintf (stderr, "Bad switch\n");
2431 printf("DBG: Recip: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2445 printf("DBG: SquareRoot: %s: op = 0x%s\n",DOFMT(fmt),pr_addr(op));
2448 /* The registers must specify FPRs valid for operands of type
2449 "fmt". If they are not valid, the result is undefined. */
2451 /* The format type should already have been checked: */
2458 sim_fpu_32to (&wop, op);
2459 sim_fpu_sqrt (&ans, &wop);
2460 sim_fpu_to32 (&res, &ans);
2469 sim_fpu_64to (&wop, op);
2470 sim_fpu_sqrt (&ans, &wop);
2471 sim_fpu_to64 (&res, &ans);
2476 fprintf (stderr, "Bad switch\n");
2481 printf("DBG: SquareRoot: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2497 printf("DBG: Max: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2500 /* The registers must specify FPRs valid for operands of type
2501 "fmt". If they are not valid, the result is undefined. */
2503 /* The format type should already have been checked: */
2510 sim_fpu_32to (&wop1, op1);
2511 sim_fpu_32to (&wop2, op2);
2512 cmp = sim_fpu_cmp (&wop1, &wop2);
2519 sim_fpu_64to (&wop1, op1);
2520 sim_fpu_64to (&wop2, op2);
2521 cmp = sim_fpu_cmp (&wop1, &wop2);
2525 fprintf (stderr, "Bad switch\n");
2531 case SIM_FPU_IS_SNAN:
2532 case SIM_FPU_IS_QNAN:
2534 case SIM_FPU_IS_NINF:
2535 case SIM_FPU_IS_NNUMBER:
2536 case SIM_FPU_IS_NDENORM:
2537 case SIM_FPU_IS_NZERO:
2538 result = op2; /* op1 - op2 < 0 */
2539 case SIM_FPU_IS_PINF:
2540 case SIM_FPU_IS_PNUMBER:
2541 case SIM_FPU_IS_PDENORM:
2542 case SIM_FPU_IS_PZERO:
2543 result = op1; /* op1 - op2 > 0 */
2545 fprintf (stderr, "Bad switch\n");
2550 printf("DBG: Max: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2567 printf("DBG: Min: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2570 /* The registers must specify FPRs valid for operands of type
2571 "fmt". If they are not valid, the result is undefined. */
2573 /* The format type should already have been checked: */
2580 sim_fpu_32to (&wop1, op1);
2581 sim_fpu_32to (&wop2, op2);
2582 cmp = sim_fpu_cmp (&wop1, &wop2);
2589 sim_fpu_64to (&wop1, op1);
2590 sim_fpu_64to (&wop2, op2);
2591 cmp = sim_fpu_cmp (&wop1, &wop2);
2595 fprintf (stderr, "Bad switch\n");
2601 case SIM_FPU_IS_SNAN:
2602 case SIM_FPU_IS_QNAN:
2604 case SIM_FPU_IS_NINF:
2605 case SIM_FPU_IS_NNUMBER:
2606 case SIM_FPU_IS_NDENORM:
2607 case SIM_FPU_IS_NZERO:
2608 result = op1; /* op1 - op2 < 0 */
2609 case SIM_FPU_IS_PINF:
2610 case SIM_FPU_IS_PNUMBER:
2611 case SIM_FPU_IS_PDENORM:
2612 case SIM_FPU_IS_PZERO:
2613 result = op2; /* op1 - op2 > 0 */
2615 fprintf (stderr, "Bad switch\n");
2620 printf("DBG: Min: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2628 convert (SIM_DESC sd,
2637 sim_fpu_round round;
2638 unsigned32 result32;
2639 unsigned64 result64;
2642 #if 0 /* FIXME: doesn't compile */
2643 printf("DBG: Convert: mode %s : op 0x%s : from %s : to %s : (PC = 0x%s)\n",RMMODE(rm),pr_addr(op),DOFMT(from),DOFMT(to),pr_addr(IPC));
2650 /* Round result to nearest representable value. When two
2651 representable values are equally near, round to the value
2652 that has a least significant bit of zero (i.e. is even). */
2653 round = sim_fpu_round_near;
2656 /* Round result to the value closest to, and not greater in
2657 magnitude than, the result. */
2658 round = sim_fpu_round_zero;
2661 /* Round result to the value closest to, and not less than,
2663 round = sim_fpu_round_up;
2667 /* Round result to the value closest to, and not greater than,
2669 round = sim_fpu_round_down;
2673 fprintf (stderr, "Bad switch\n");
2677 /* Convert the input to sim_fpu internal format */
2681 sim_fpu_64to (&wop, op);
2684 sim_fpu_32to (&wop, op);
2687 sim_fpu_i32to (&wop, op, round);
2690 sim_fpu_i64to (&wop, op, round);
2693 fprintf (stderr, "Bad switch\n");
2697 /* Convert sim_fpu format into the output */
2698 /* The value WOP is converted to the destination format, rounding
2699 using mode RM. When the destination is a fixed-point format, then
2700 a source value of Infinity, NaN or one which would round to an
2701 integer outside the fixed point range then an IEEE Invalid
2702 Operation condition is raised. */
2706 sim_fpu_round_32 (&wop, round, 0);
2707 sim_fpu_to32 (&result32, &wop);
2708 result64 = result32;
2711 sim_fpu_round_64 (&wop, round, 0);
2712 sim_fpu_to64 (&result64, &wop);
2715 sim_fpu_to32i (&result32, &wop, round);
2716 result64 = result32;
2719 sim_fpu_to64i (&result64, &wop, round);
2723 fprintf (stderr, "Bad switch\n");
2728 printf("DBG: Convert: returning 0x%s (to format = %s)\n",pr_addr(result64),DOFMT(to));
2735 /*-- co-processor support routines ------------------------------------------*/
2738 CoProcPresent(unsigned int coproc_number)
2740 /* Return TRUE if simulator provides a model for the given co-processor number */
2745 cop_lw (SIM_DESC sd,
2750 unsigned int memword)
2755 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2758 printf("DBG: COP_LW: memword = 0x%08X (uword64)memword = 0x%s\n",memword,pr_addr(memword));
2760 StoreFPR(coproc_reg,fmt_word,(uword64)memword);
2761 FPR_STATE[coproc_reg] = fmt_uninterpreted;
2766 #if 0 /* this should be controlled by a configuration option */
2767 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));
2776 cop_ld (SIM_DESC sd,
2785 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) );
2788 switch (coproc_num) {
2790 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2792 StoreFPR(coproc_reg,fmt_uninterpreted,memword);
2797 #if 0 /* this message should be controlled by a configuration option */
2798 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));
2810 cop_sw (SIM_DESC sd,
2816 unsigned int value = 0;
2821 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2824 hold = FPR_STATE[coproc_reg];
2825 FPR_STATE[coproc_reg] = fmt_word;
2826 value = (unsigned int)ValueFPR(coproc_reg,fmt_uninterpreted);
2827 FPR_STATE[coproc_reg] = hold;
2832 #if 0 /* should be controlled by configuration option */
2833 sim_io_printf(sd,"COP_SW(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(cia));
2842 cop_sd (SIM_DESC sd,
2852 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2854 value = ValueFPR(coproc_reg,fmt_uninterpreted);
2859 #if 0 /* should be controlled by configuration option */
2860 sim_io_printf(sd,"COP_SD(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(cia));
2872 decode_coproc (SIM_DESC sd,
2875 unsigned int instruction)
2877 int coprocnum = ((instruction >> 26) & 3);
2881 case 0: /* standard CPU control and cache registers */
2883 int code = ((instruction >> 21) & 0x1F);
2884 int rt = ((instruction >> 16) & 0x1F);
2885 int rd = ((instruction >> 11) & 0x1F);
2886 int tail = instruction & 0x3ff;
2887 /* R4000 Users Manual (second edition) lists the following CP0
2889 CODE><-RT><RD-><--TAIL--->
2890 DMFC0 Doubleword Move From CP0 (VR4100 = 01000000001tttttddddd00000000000)
2891 DMTC0 Doubleword Move To CP0 (VR4100 = 01000000101tttttddddd00000000000)
2892 MFC0 word Move From CP0 (VR4100 = 01000000000tttttddddd00000000000)
2893 MTC0 word Move To CP0 (VR4100 = 01000000100tttttddddd00000000000)
2894 TLBR Read Indexed TLB Entry (VR4100 = 01000010000000000000000000000001)
2895 TLBWI Write Indexed TLB Entry (VR4100 = 01000010000000000000000000000010)
2896 TLBWR Write Random TLB Entry (VR4100 = 01000010000000000000000000000110)
2897 TLBP Probe TLB for Matching Entry (VR4100 = 01000010000000000000000000001000)
2898 CACHE Cache operation (VR4100 = 101111bbbbbpppppiiiiiiiiiiiiiiii)
2899 ERET Exception return (VR4100 = 01000010000000000000000000011000)
2901 if (((code == 0x00) || (code == 0x04)) && tail == 0)
2903 /* M[TF]C0 - 32 bit word */
2905 switch (rd) /* NOTEs: Standard CP0 registers */
2907 /* 0 = Index R4000 VR4100 VR4300 */
2908 /* 1 = Random R4000 VR4100 VR4300 */
2909 /* 2 = EntryLo0 R4000 VR4100 VR4300 */
2910 /* 3 = EntryLo1 R4000 VR4100 VR4300 */
2911 /* 4 = Context R4000 VR4100 VR4300 */
2912 /* 5 = PageMask R4000 VR4100 VR4300 */
2913 /* 6 = Wired R4000 VR4100 VR4300 */
2914 /* 8 = BadVAddr R4000 VR4100 VR4300 */
2915 /* 9 = Count R4000 VR4100 VR4300 */
2916 /* 10 = EntryHi R4000 VR4100 VR4300 */
2917 /* 11 = Compare R4000 VR4100 VR4300 */
2918 /* 12 = SR R4000 VR4100 VR4300 */
2919 #ifdef SUBTARGET_R3900
2921 /* 3 = Config R3900 */
2923 /* 7 = Cache R3900 */
2925 /* 15 = PRID R3900 */
2931 /* 8 = BadVAddr R4000 VR4100 VR4300 */
2933 GPR[rt] = COP0_BADVADDR;
2935 COP0_BADVADDR = GPR[rt];
2938 #endif /* SUBTARGET_R3900 */
2945 /* 13 = Cause R4000 VR4100 VR4300 */
2952 /* 14 = EPC R4000 VR4100 VR4300 */
2955 GPR[rt] = (signed_word) (signed_address) EPC;
2959 /* 15 = PRId R4000 VR4100 VR4300 */
2960 #ifdef SUBTARGET_R3900
2969 /* 16 = Config R4000 VR4100 VR4300 */
2972 GPR[rt] = C0_CONFIG;
2974 C0_CONFIG = GPR[rt];
2977 #ifdef SUBTARGET_R3900
2986 /* 17 = LLAddr R4000 VR4100 VR4300 */
2988 /* 18 = WatchLo R4000 VR4100 VR4300 */
2989 /* 19 = WatchHi R4000 VR4100 VR4300 */
2990 /* 20 = XContext R4000 VR4100 VR4300 */
2991 /* 26 = PErr or ECC R4000 VR4100 VR4300 */
2992 /* 27 = CacheErr R4000 VR4100 */
2993 /* 28 = TagLo R4000 VR4100 VR4300 */
2994 /* 29 = TagHi R4000 VR4100 VR4300 */
2995 /* 30 = ErrorEPC R4000 VR4100 VR4300 */
2996 GPR[rt] = 0xDEADC0DE; /* CPR[0,rd] */
2997 /* CPR[0,rd] = GPR[rt]; */
3000 GPR[rt] = (signed_word) (signed32) COP0_GPR[rd];
3002 COP0_GPR[rd] = GPR[rt];
3005 sim_io_printf(sd,"Warning: MFC0 %d,%d ignored, PC=%08x (architecture specific)\n",rt,rd, (unsigned)cia);
3007 sim_io_printf(sd,"Warning: MTC0 %d,%d ignored, PC=%08x (architecture specific)\n",rt,rd, (unsigned)cia);
3011 else if (code == 0x10 && (tail & 0x3f) == 0x18)
3014 if (SR & status_ERL)
3016 /* Oops, not yet available */
3017 sim_io_printf(sd,"Warning: ERET when SR[ERL] set not handled yet");
3027 else if (code == 0x10 && (tail & 0x3f) == 0x10)
3030 #ifdef SUBTARGET_R3900
3031 /* TX39: Copy IEp/KUp -> IEc/KUc, and IEo/KUo -> IEp/KUp */
3033 /* shift IE/KU history bits right */
3034 SR = LSMASKED32(SR, 31, 4) | LSINSERTED32(LSEXTRACTED32(SR, 5, 2), 3, 0);
3036 /* TODO: CACHE register */
3037 #endif /* SUBTARGET_R3900 */
3039 else if (code == 0x10 && (tail & 0x3f) == 0x1F)
3047 sim_io_eprintf(sd,"Unrecognised COP0 instruction 0x%08X at PC = 0x%s : No handler present\n",instruction,pr_addr(cia));
3048 /* TODO: When executing an ERET or RFE instruction we should
3049 clear LLBIT, to ensure that any out-standing atomic
3050 read/modify/write sequence fails. */
3054 case 2: /* co-processor 2 */
3061 sim_io_eprintf(sd, "COP2 instruction 0x%08X at PC = 0x%s : No handler present\n",
3062 instruction,pr_addr(cia));
3067 case 1: /* should not occur (FPU co-processor) */
3068 case 3: /* should not occur (FPU co-processor) */
3069 SignalException(ReservedInstruction,instruction);
3077 /* This code copied from gdb's utils.c. Would like to share this code,
3078 but don't know of a common place where both could get to it. */
3080 /* Temporary storage using circular buffer */
3086 static char buf[NUMCELLS][CELLSIZE];
3088 if (++cell>=NUMCELLS) cell=0;
3092 /* Print routines to handle variable size regs, etc */
3094 /* Eliminate warning from compiler on 32-bit systems */
3095 static int thirty_two = 32;
3101 char *paddr_str=get_cell();
3102 switch (sizeof(addr))
3105 sprintf(paddr_str,"%08lx%08lx",
3106 (unsigned long)(addr>>thirty_two),(unsigned long)(addr&0xffffffff));
3109 sprintf(paddr_str,"%08lx",(unsigned long)addr);
3112 sprintf(paddr_str,"%04x",(unsigned short)(addr&0xffff));
3115 sprintf(paddr_str,"%x",addr);
3124 char *paddr_str=get_cell();
3125 sprintf(paddr_str,"%08lx%08lx",
3126 (unsigned long)(addr>>thirty_two),(unsigned long)(addr&0xffffffff));
3132 mips_core_signal (SIM_DESC sd,
3138 transfer_type transfer,
3139 sim_core_signals sig)
3141 const char *copy = (transfer == read_transfer ? "read" : "write");
3142 address_word ip = CIA_ADDR (cia);
3146 case sim_core_unmapped_signal:
3147 sim_io_eprintf (sd, "mips-core: %d byte %s to unmapped address 0x%lx at 0x%lx\n",
3149 (unsigned long) addr, (unsigned long) ip);
3150 COP0_BADVADDR = addr;
3151 SignalExceptionDataReference();
3154 case sim_core_unaligned_signal:
3155 sim_io_eprintf (sd, "mips-core: %d byte %s to unaligned address 0x%lx at 0x%lx\n",
3157 (unsigned long) addr, (unsigned long) ip);
3158 COP0_BADVADDR = addr;
3159 if(transfer == read_transfer)
3160 SignalExceptionAddressLoad();
3162 SignalExceptionAddressStore();
3166 sim_engine_abort (sd, cpu, cia,
3167 "mips_core_signal - internal error - bad switch");
3173 mips_cpu_exception_trigger(SIM_DESC sd, sim_cpu* cpu, address_word cia)
3175 ASSERT(cpu != NULL);
3177 if(cpu->exc_suspended > 0)
3178 sim_io_eprintf(sd, "Warning, nested exception triggered (%d)\n", cpu->exc_suspended);
3181 memcpy(cpu->exc_trigger_registers, cpu->registers, sizeof(cpu->exc_trigger_registers));
3182 cpu->exc_suspended = 0;
3186 mips_cpu_exception_suspend(SIM_DESC sd, sim_cpu* cpu, int exception)
3188 ASSERT(cpu != NULL);
3190 if(cpu->exc_suspended > 0)
3191 sim_io_eprintf(sd, "Warning, nested exception signal (%d then %d)\n",
3192 cpu->exc_suspended, exception);
3194 memcpy(cpu->exc_suspend_registers, cpu->registers, sizeof(cpu->exc_suspend_registers));
3195 memcpy(cpu->registers, cpu->exc_trigger_registers, sizeof(cpu->registers));
3196 cpu->exc_suspended = exception;
3200 mips_cpu_exception_resume(SIM_DESC sd, sim_cpu* cpu, int exception)
3202 ASSERT(cpu != NULL);
3204 if(exception == 0 && cpu->exc_suspended > 0)
3206 /* warn not for breakpoints */
3207 if(cpu->exc_suspended != sim_signal_to_host(sd, SIM_SIGTRAP))
3208 sim_io_eprintf(sd, "Warning, resuming but ignoring pending exception signal (%d)\n",
3209 cpu->exc_suspended);
3211 else if(exception != 0 && cpu->exc_suspended > 0)
3213 if(exception != cpu->exc_suspended)
3214 sim_io_eprintf(sd, "Warning, resuming with mismatched exception signal (%d vs %d)\n",
3215 cpu->exc_suspended, exception);
3217 memcpy(cpu->registers, cpu->exc_suspend_registers, sizeof(cpu->registers));
3219 else if(exception != 0 && cpu->exc_suspended == 0)
3221 sim_io_eprintf(sd, "Warning, ignoring spontanous exception signal (%d)\n", exception);
3223 cpu->exc_suspended = 0;
3227 /*---------------------------------------------------------------------------*/
3228 /*> EOF interp.c <*/