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.
22 The IDT monitor (found on the VR4300 board), seems to lie about
23 register contents. It seems to treat the registers as sign-extended
24 32-bit values. This cause *REAL* problems when single-stepping 64-bit
29 /* The TRACE manifests enable the provision of extra features. If they
30 are not defined then a simpler (quicker) simulator is constructed
31 without the required run-time checks, etc. */
32 #if 1 /* 0 to allow user build selection, 1 to force inclusion */
38 #include "sim-utils.h"
39 #include "sim-options.h"
40 #include "sim-assert.h"
62 #include "libiberty.h"
64 #include "callback.h" /* GDB simulator callback interface */
65 #include "remote-sim.h" /* GDB simulator interface */
73 char* pr_addr
PARAMS ((SIM_ADDR addr
));
74 char* pr_uword64
PARAMS ((uword64 addr
));
77 /* Get the simulator engine description, without including the code: */
79 #define LOADDRMASK (WITH_TARGET_WORD_BITSIZE == 64 ? 0x7 : 0x3)
86 /* Within interp.c we refer to the sim_state and sim_cpu directly. */
91 /* The following reserved instruction value is used when a simulator
92 trap is required. NOTE: Care must be taken, since this value may be
93 used in later revisions of the MIPS ISA. */
94 #define RSVD_INSTRUCTION (0x00000005)
95 #define RSVD_INSTRUCTION_MASK (0xFC00003F)
97 #define RSVD_INSTRUCTION_ARG_SHIFT 6
98 #define RSVD_INSTRUCTION_ARG_MASK 0xFFFFF
101 /* Bits in the Debug register */
102 #define Debug_DBD 0x80000000 /* Debug Branch Delay */
103 #define Debug_DM 0x40000000 /* Debug Mode */
104 #define Debug_DBp 0x00000002 /* Debug Breakpoint indicator */
110 /*---------------------------------------------------------------------------*/
111 /*-- GDB simulator interface ------------------------------------------------*/
112 /*---------------------------------------------------------------------------*/
114 static void ColdReset
PARAMS((SIM_DESC sd
));
116 /*---------------------------------------------------------------------------*/
120 #define DELAYSLOT() {\
121 if (STATE & simDELAYSLOT)\
122 sim_io_eprintf(sd,"Delay slot already activated (branch in delay slot?)\n");\
123 STATE |= simDELAYSLOT;\
126 #define JALDELAYSLOT() {\
128 STATE |= simJALDELAYSLOT;\
132 STATE &= ~simDELAYSLOT;\
133 STATE |= simSKIPNEXT;\
136 #define CANCELDELAYSLOT() {\
138 STATE &= ~(simDELAYSLOT | simJALDELAYSLOT);\
141 #define INDELAYSLOT() ((STATE & simDELAYSLOT) != 0)
142 #define INJALDELAYSLOT() ((STATE & simJALDELAYSLOT) != 0)
144 #define K0BASE (0x80000000)
145 #define K0SIZE (0x20000000)
146 #define K1BASE (0xA0000000)
147 #define K1SIZE (0x20000000)
148 #define MONITOR_BASE (0xBFC00000)
149 #define MONITOR_SIZE (1 << 11)
150 #define MEM_SIZE (2 << 20)
153 static char *tracefile
= "trace.din"; /* default filename for trace log */
154 FILE *tracefh
= NULL
;
155 static void open_trace
PARAMS((SIM_DESC sd
));
158 #define OPTION_DINERO_TRACE 200
159 #define OPTION_DINERO_FILE 201
162 mips_option_handler (sd
, opt
, arg
)
170 case OPTION_DINERO_TRACE
: /* ??? */
172 /* Eventually the simTRACE flag could be treated as a toggle, to
173 allow external control of the program points being traced
174 (i.e. only from main onwards, excluding the run-time setup,
176 for (cpu_nr
= 0; cpu_nr
< sim_engine_nr_cpus (sd
); cpu_nr
++)
178 sim_cpu
*cpu
= STATE_CPU (sd
, cpu_nr
);
181 else if (strcmp (arg
, "yes") == 0)
183 else if (strcmp (arg
, "no") == 0)
185 else if (strcmp (arg
, "on") == 0)
187 else if (strcmp (arg
, "off") == 0)
191 fprintf (stderr
, "Unreconized dinero-trace option `%s'\n", arg
);
198 Simulator constructed without dinero tracing support (for performance).\n\
199 Re-compile simulator with \"-DTRACE\" to enable this option.\n");
203 case OPTION_DINERO_FILE
:
205 if (optarg
!= NULL
) {
207 tmp
= (char *)malloc(strlen(optarg
) + 1);
210 sim_io_printf(sd
,"Failed to allocate buffer for tracefile name \"%s\"\n",optarg
);
216 sim_io_printf(sd
,"Placing trace information into file \"%s\"\n",tracefile
);
227 static const OPTION mips_options
[] =
229 { {"dinero-trace", optional_argument
, NULL
, OPTION_DINERO_TRACE
},
230 '\0', "on|off", "Enable dinero tracing",
231 mips_option_handler
},
232 { {"dinero-file", required_argument
, NULL
, OPTION_DINERO_FILE
},
233 '\0', "FILE", "Write dinero trace to FILE",
234 mips_option_handler
},
235 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
239 int interrupt_pending
;
242 interrupt_event (SIM_DESC sd
, void *data
)
244 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
247 interrupt_pending
= 0;
248 SignalExceptionInterrupt ();
250 else if (!interrupt_pending
)
251 sim_events_schedule (sd
, 1, interrupt_event
, data
);
256 /*---------------------------------------------------------------------------*/
257 /*-- GDB simulator interface ------------------------------------------------*/
258 /*---------------------------------------------------------------------------*/
261 sim_open (kind
, cb
, abfd
, argv
)
267 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
268 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
270 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
272 /* FIXME: watchpoints code shouldn't need this */
273 STATE_WATCHPOINTS (sd
)->pc
= &(PC
);
274 STATE_WATCHPOINTS (sd
)->sizeof_pc
= sizeof (PC
);
275 STATE_WATCHPOINTS (sd
)->interrupt_handler
= interrupt_event
;
279 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
281 sim_add_option_table (sd
, mips_options
);
283 /* Allocate core managed memory */
286 sim_do_commandf (sd
, "memory region 0x%lx,0x%lx", MONITOR_BASE
, MONITOR_SIZE
);
287 /* For compatibility with the old code - under this (at level one)
288 are the kernel spaces K0 & K1. Both of these map to a single
289 smaller sub region */
290 sim_do_commandf (sd
, "memory alias 0x%lx@1,0x%lx%%0x%lx,0x%0x",
292 MEM_SIZE
, /* actual size */
295 /* getopt will print the error message so we just have to exit if this fails.
296 FIXME: Hmmm... in the case of gdb we need getopt to call
298 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
300 /* Uninstall the modules to avoid memory leaks,
301 file descriptor leaks, etc. */
302 sim_module_uninstall (sd
);
306 /* check for/establish the a reference program image */
307 if (sim_analyze_program (sd
,
308 (STATE_PROG_ARGV (sd
) != NULL
309 ? *STATE_PROG_ARGV (sd
)
313 sim_module_uninstall (sd
);
317 /* Configure/verify the target byte order and other runtime
318 configuration options */
319 if (sim_config (sd
) != SIM_RC_OK
)
321 sim_module_uninstall (sd
);
325 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
327 /* Uninstall the modules to avoid memory leaks,
328 file descriptor leaks, etc. */
329 sim_module_uninstall (sd
);
333 /* verify assumptions the simulator made about the host type system.
334 This macro does not return if there is a problem */
335 SIM_ASSERT (sizeof(int) == (4 * sizeof(char)));
336 SIM_ASSERT (sizeof(word64
) == (8 * sizeof(char)));
338 /* This is NASTY, in that we are assuming the size of specific
342 for (rn
= 0; (rn
< (LAST_EMBED_REGNUM
+ 1)); rn
++)
345 cpu
->register_widths
[rn
] = WITH_TARGET_WORD_BITSIZE
;
346 else if ((rn
>= FGRIDX
) && (rn
< (FGRIDX
+ NR_FGR
)))
347 cpu
->register_widths
[rn
] = WITH_TARGET_FLOATING_POINT_BITSIZE
;
348 else if ((rn
>= 33) && (rn
<= 37))
349 cpu
->register_widths
[rn
] = WITH_TARGET_WORD_BITSIZE
;
350 else if ((rn
== SRIDX
) || (rn
== FCR0IDX
) || (rn
== FCR31IDX
) || ((rn
>= 72) && (rn
<= 89)))
351 cpu
->register_widths
[rn
] = 32;
353 cpu
->register_widths
[rn
] = 0;
355 /* start-sanitize-r5900 */
357 /* set the 5900 "upper" registers to 64 bits */
358 for( rn
= LAST_EMBED_REGNUM
+1; rn
< NUM_REGS
; rn
++)
359 cpu
->register_widths
[rn
] = 64;
360 /* end-sanitize-r5900 */
364 if (STATE
& simTRACE
)
368 /* Write the monitor trap address handlers into the monitor (eeprom)
369 address space. This can only be done once the target endianness
370 has been determined. */
373 /* Entry into the IDT monitor is via fixed address vectors, and
374 not using machine instructions. To avoid clashing with use of
375 the MIPS TRAP system, we place our own (simulator specific)
376 "undefined" instructions into the relevant vector slots. */
377 for (loop
= 0; (loop
< MONITOR_SIZE
); loop
+= 4)
379 address_word vaddr
= (MONITOR_BASE
+ loop
);
380 unsigned32 insn
= (RSVD_INSTRUCTION
| (((loop
>> 2) & RSVD_INSTRUCTION_ARG_MASK
) << RSVD_INSTRUCTION_ARG_SHIFT
));
382 sim_write (sd
, vaddr
, (char *)&insn
, sizeof (insn
));
384 /* The PMON monitor uses the same address space, but rather than
385 branching into it the address of a routine is loaded. We can
386 cheat for the moment, and direct the PMON routine to IDT style
387 instructions within the monitor space. This relies on the IDT
388 monitor not using the locations from 0xBFC00500 onwards as its
390 for (loop
= 0; (loop
< 24); loop
++)
392 address_word vaddr
= (MONITOR_BASE
+ 0x500 + (loop
* 4));
393 unsigned32 value
= ((0x500 - 8) / 8); /* default UNDEFINED reason code */
409 value
= ((0x500 - 16) / 8); /* not an IDT reason code */
411 case 8: /* cliexit */
414 case 11: /* flush_cache */
418 /* FIXME - should monitor_base be SIM_ADDR?? */
419 value
= ((unsigned int)MONITOR_BASE
+ (value
* 8));
421 sim_write (sd
, vaddr
, (char *)&value
, sizeof (value
));
423 /* The LSI MiniRISC PMON has its vectors at 0x200, not 0x500. */
425 sim_write (sd
, vaddr
, (char *)&value
, sizeof (value
));
437 tracefh
= fopen(tracefile
,"wb+");
440 sim_io_eprintf(sd
,"Failed to create file \"%s\", writing trace information to stderr.\n",tracefile
);
447 sim_close (sd
, quitting
)
452 printf("DBG: sim_close: entered (quitting = %d)\n",quitting
);
455 /* "quitting" is non-zero if we cannot hang on errors */
457 /* Ensure that any resources allocated through the callback
458 mechanism are released: */
459 sim_io_shutdown (sd
);
462 if (tracefh
!= NULL
&& tracefh
!= stderr
)
467 /* FIXME - free SD */
474 sim_write (sd
,addr
,buffer
,size
)
477 unsigned char *buffer
;
481 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
483 /* Return the number of bytes written, or zero if error. */
485 sim_io_printf(sd
,"sim_write(0x%s,buffer,%d);\n",pr_addr(addr
),size
);
488 /* We use raw read and write routines, since we do not want to count
489 the GDB memory accesses in our statistics gathering. */
491 for (index
= 0; index
< size
; index
++)
493 address_word vaddr
= (address_word
)addr
+ index
;
496 if (!address_translation (SD
, CPU
, NULL_CIA
, vaddr
, isDATA
, isSTORE
, &paddr
, &cca
, isRAW
))
498 if (sim_core_write_buffer (SD
, CPU
, sim_core_read_map
, buffer
+ index
, paddr
, 1) != 1)
506 sim_read (sd
,addr
,buffer
,size
)
509 unsigned char *buffer
;
513 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
515 /* Return the number of bytes read, or zero if error. */
517 sim_io_printf(sd
,"sim_read(0x%s,buffer,%d);\n",pr_addr(addr
),size
);
520 for (index
= 0; (index
< size
); index
++)
522 address_word vaddr
= (address_word
)addr
+ index
;
525 if (!address_translation (SD
, CPU
, NULL_CIA
, vaddr
, isDATA
, isLOAD
, &paddr
, &cca
, isRAW
))
527 if (sim_core_read_buffer (SD
, CPU
, sim_core_read_map
, buffer
+ index
, paddr
, 1) != 1)
535 sim_store_register (sd
,rn
,memory
)
538 unsigned char *memory
;
540 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
541 /* NOTE: gdb (the client) stores registers in target byte order
542 while the simulator uses host byte order */
544 sim_io_printf(sd
,"sim_store_register(%d,*memory=0x%s);\n",rn
,pr_addr(*((SIM_ADDR
*)memory
)));
547 /* Unfortunately this suffers from the same problem as the register
548 numbering one. We need to know what the width of each logical
549 register number is for the architecture being simulated. */
551 if (cpu
->register_widths
[rn
] == 0)
552 sim_io_eprintf(sd
,"Invalid register width for %d (register store ignored)\n",rn
);
553 /* start-sanitize-r5900 */
554 else if (rn
== REGISTER_SA
)
555 SA
= T2H_8(*(unsigned64
*)memory
);
556 else if (rn
> LAST_EMBED_REGNUM
)
557 cpu
->registers1
[rn
- LAST_EMBED_REGNUM
- 1] = T2H_8(*(unsigned64
*)memory
);
558 /* end-sanitize-r5900 */
559 else if (rn
>= FGRIDX
&& rn
< FGRIDX
+ NR_FGR
)
561 if (cpu
->register_widths
[rn
] == 32)
562 cpu
->fgr
[rn
- FGRIDX
] = T2H_4 (*(unsigned32
*)memory
);
564 cpu
->fgr
[rn
- FGRIDX
] = T2H_8 (*(unsigned64
*)memory
);
566 else if (cpu
->register_widths
[rn
] == 32)
567 cpu
->registers
[rn
] = T2H_4 (*(unsigned32
*)memory
);
569 cpu
->registers
[rn
] = T2H_8 (*(unsigned64
*)memory
);
575 sim_fetch_register (sd
,rn
,memory
)
578 unsigned char *memory
;
580 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
581 /* NOTE: gdb (the client) stores registers in target byte order
582 while the simulator uses host byte order */
584 sim_io_printf(sd
,"sim_fetch_register(%d=0x%s,mem) : place simulator registers into memory\n",rn
,pr_addr(registers
[rn
]));
587 if (cpu
->register_widths
[rn
] == 0)
588 sim_io_eprintf(sd
,"Invalid register width for %d (register fetch ignored)\n",rn
);
589 /* start-sanitize-r5900 */
590 else if (rn
== REGISTER_SA
)
591 *((unsigned64
*)memory
) = H2T_8(SA
);
592 else if (rn
> LAST_EMBED_REGNUM
)
593 *((unsigned64
*)memory
) = H2T_8(cpu
->registers1
[rn
- LAST_EMBED_REGNUM
- 1]);
594 /* end-sanitize-r5900 */
595 else if (rn
>= FGRIDX
&& rn
< FGRIDX
+ NR_FGR
)
597 if (cpu
->register_widths
[rn
] == 32)
598 *(unsigned32
*)memory
= H2T_4 (cpu
->fgr
[rn
- FGRIDX
]);
600 *(unsigned64
*)memory
= H2T_8 (cpu
->fgr
[rn
- FGRIDX
]);
602 else if (cpu
->register_widths
[rn
] == 32)
603 *(unsigned32
*)memory
= H2T_4 ((unsigned32
)(cpu
->registers
[rn
]));
604 else /* 64bit register */
605 *(unsigned64
*)memory
= H2T_8 ((unsigned64
)(cpu
->registers
[rn
]));
612 sim_info (sd
,verbose
)
616 /* Accessed from the GDB "info files" command: */
617 if (STATE_VERBOSE_P (sd
) || verbose
)
620 sim_io_printf (sd
, "MIPS %d-bit %s endian simulator\n",
621 WITH_TARGET_WORD_BITSIZE
,
622 (CURRENT_TARGET_BYTE_ORDER
== BIG_ENDIAN
? "Big" : "Little"));
624 #if !defined(FASTSIM)
625 /* It would be a useful feature, if when performing multi-cycle
626 simulations (rather than single-stepping) we keep the start and
627 end times of the execution, so that we can give a performance
628 figure for the simulator. */
629 #endif /* !FASTSIM */
630 sim_io_printf (sd
, "Number of execution cycles = %ld\n",
631 (long) sim_events_time (sd
));
633 /* print information pertaining to MIPS ISA and architecture being simulated */
634 /* things that may be interesting */
635 /* instructions executed - if available */
636 /* cycles executed - if available */
637 /* pipeline stalls - if available */
638 /* virtual time taken */
640 /* profiling frequency */
644 profile_print (sd
, STATE_VERBOSE_P (sd
), NULL
, NULL
);
649 sim_create_inferior (sd
, abfd
, argv
,env
)
657 printf("DBG: sim_create_inferior entered: start_address = 0x%s\n",
665 /* override PC value set by ColdReset () */
667 for (cpu_nr
= 0; cpu_nr
< sim_engine_nr_cpus (sd
); cpu_nr
++)
669 sim_cpu
*cpu
= STATE_CPU (sd
, cpu_nr
);
670 CIA_SET (cpu
, (unsigned64
) bfd_get_start_address (abfd
));
674 #if 0 /* def DEBUG */
677 /* We should really place the argv slot values into the argument
678 registers, and onto the stack as required. However, this
679 assumes that we have a stack defined, which is not
680 necessarily true at the moment. */
682 sim_io_printf(sd
,"sim_create_inferior() : passed arguments ignored\n");
683 for (cptr
= argv
; (cptr
&& *cptr
); cptr
++)
684 printf("DBG: arg \"%s\"\n",*cptr
);
692 sim_do_command (sd
,cmd
)
696 if (sim_args_command (sd
, cmd
) != SIM_RC_OK
)
697 sim_io_printf (sd
, "Error: \"%s\" is not a valid MIPS simulator command.\n",
701 /*---------------------------------------------------------------------------*/
702 /*-- Private simulator support interface ------------------------------------*/
703 /*---------------------------------------------------------------------------*/
705 /* Read a null terminated string from memory, return in a buffer */
714 while (sim_read (sd
, addr
+ nr
, &null
, 1) == 1 && null
!= 0)
716 buf
= NZALLOC (char, nr
+ 1);
717 sim_read (sd
, addr
, buf
, nr
);
721 /* Simple monitor interface (currently setup for the IDT and PMON monitors) */
723 sim_monitor (SIM_DESC sd
,
729 printf("DBG: sim_monitor: entered (reason = %d)\n",reason
);
732 /* The IDT monitor actually allows two instructions per vector
733 slot. However, the simulator currently causes a trap on each
734 individual instruction. We cheat, and lose the bottom bit. */
737 /* The following callback functions are available, however the
738 monitor we are simulating does not make use of them: get_errno,
739 isatty, lseek, rename, system, time and unlink */
743 case 6: /* int open(char *path,int flags) */
745 char *path
= fetch_str (sd
, A0
);
746 V0
= sim_io_open (sd
, path
, (int)A1
);
751 case 7: /* int read(int file,char *ptr,int len) */
755 char *buf
= zalloc (nr
);
756 V0
= sim_io_read (sd
, fd
, buf
, nr
);
757 sim_write (sd
, A1
, buf
, nr
);
762 case 8: /* int write(int file,char *ptr,int len) */
766 char *buf
= zalloc (nr
);
767 sim_read (sd
, A1
, buf
, nr
);
768 V0
= sim_io_write (sd
, fd
, buf
, nr
);
773 case 10: /* int close(int file) */
775 V0
= sim_io_close (sd
, (int)A0
);
779 case 2: /* Densan monitor: char inbyte(int waitflag) */
781 if (A0
== 0) /* waitflag == NOWAIT */
782 V0
= (unsigned_word
)-1;
784 /* Drop through to case 11 */
786 case 11: /* char inbyte(void) */
789 if (sim_io_read_stdin (sd
, &tmp
, sizeof(char)) != sizeof(char))
791 sim_io_error(sd
,"Invalid return from character read");
792 V0
= (unsigned_word
)-1;
795 V0
= (unsigned_word
)tmp
;
799 case 3: /* Densan monitor: void co(char chr) */
800 case 12: /* void outbyte(char chr) : write a byte to "stdout" */
802 char tmp
= (char)(A0
& 0xFF);
803 sim_io_write_stdout (sd
, &tmp
, sizeof(char));
807 case 17: /* void _exit() */
809 sim_io_eprintf (sd
, "sim_monitor(17): _exit(int reason) to be coded\n");
810 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
, sim_exited
,
811 (unsigned int)(A0
& 0xFFFFFFFF));
815 case 28 : /* PMON flush_cache */
818 case 55: /* void get_mem_info(unsigned int *ptr) */
819 /* in: A0 = pointer to three word memory location */
820 /* out: [A0 + 0] = size */
821 /* [A0 + 4] = instruction cache size */
822 /* [A0 + 8] = data cache size */
824 address_word value
= MEM_SIZE
/* FIXME STATE_MEM_SIZE (sd) */;
826 sim_write (sd
, A0
, (char *)&value
, sizeof (value
));
827 /* sim_io_eprintf (sd, "sim: get_mem_info() depreciated\n"); */
831 case 158 : /* PMON printf */
832 /* in: A0 = pointer to format string */
833 /* A1 = optional argument 1 */
834 /* A2 = optional argument 2 */
835 /* A3 = optional argument 3 */
837 /* The following is based on the PMON printf source */
841 signed_word
*ap
= &A1
; /* 1st argument */
842 /* This isn't the quickest way, since we call the host print
843 routine for every character almost. But it does avoid
844 having to allocate and manage a temporary string buffer. */
845 /* TODO: Include check that we only use three arguments (A1,
847 while (sim_read (sd
, s
++, &c
, 1) && c
!= '\0')
852 enum {FMT_RJUST
, FMT_LJUST
, FMT_RJUST0
, FMT_CENTER
} fmt
= FMT_RJUST
;
853 int width
= 0, trunc
= 0, haddot
= 0, longlong
= 0;
854 while (sim_read (sd
, s
++, &c
, 1) && c
!= '\0')
856 if (strchr ("dobxXulscefg%", s
))
871 else if (c
>= '1' && c
<= '9')
875 while (sim_read (sd
, s
++, &c
, 1) == 1 && isdigit (c
))
878 n
= (unsigned int)strtol(tmp
,NULL
,10);
891 sim_io_printf (sd
, "%%");
896 address_word p
= *ap
++;
898 while (sim_read (sd
, p
++, &ch
, 1) == 1 && ch
!= '\0')
899 sim_io_printf(sd
, "%c", ch
);
902 sim_io_printf(sd
,"(null)");
905 sim_io_printf (sd
, "%c", (int)*ap
++);
910 sim_read (sd
, s
++, &c
, 1);
914 sim_read (sd
, s
++, &c
, 1);
917 if (strchr ("dobxXu", c
))
919 word64 lv
= (word64
) *ap
++;
921 sim_io_printf(sd
,"<binary not supported>");
924 sprintf (tmp
, "%%%s%c", longlong
? "ll" : "", c
);
926 sim_io_printf(sd
, tmp
, lv
);
928 sim_io_printf(sd
, tmp
, (int)lv
);
931 else if (strchr ("eEfgG", c
))
933 double dbl
= *(double*)(ap
++);
934 sprintf (tmp
, "%%%d.%d%c", width
, trunc
, c
);
935 sim_io_printf (sd
, tmp
, dbl
);
941 sim_io_printf(sd
, "%c", c
);
947 sim_io_error (sd
, "TODO: sim_monitor(%d) : PC = 0x%s\n",
948 reason
, pr_addr(cia
));
954 /* Store a word into memory. */
957 store_word (SIM_DESC sd
,
966 if ((vaddr
& 3) != 0)
967 SignalExceptionAddressStore ();
970 if (AddressTranslation (vaddr
, isDATA
, isSTORE
, &paddr
, &uncached
,
973 const uword64 mask
= 7;
977 paddr
= (paddr
& ~mask
) | ((paddr
& mask
) ^ (ReverseEndian
<< 2));
978 byte
= (vaddr
& mask
) ^ (BigEndianCPU
<< 2);
979 memval
= ((uword64
) val
) << (8 * byte
);
980 StoreMemory (uncached
, AccessLength_WORD
, memval
, 0, paddr
, vaddr
,
986 /* Load a word from memory. */
989 load_word (SIM_DESC sd
,
994 if ((vaddr
& 3) != 0)
995 SignalExceptionAddressLoad ();
1001 if (AddressTranslation (vaddr
, isDATA
, isLOAD
, &paddr
, &uncached
,
1004 const uword64 mask
= 0x7;
1005 const unsigned int reverse
= ReverseEndian
? 1 : 0;
1006 const unsigned int bigend
= BigEndianCPU
? 1 : 0;
1010 paddr
= (paddr
& ~mask
) | ((paddr
& mask
) ^ (reverse
<< 2));
1011 LoadMemory (&memval
,NULL
,uncached
, AccessLength_WORD
, paddr
, vaddr
,
1013 byte
= (vaddr
& mask
) ^ (bigend
<< 2);
1014 return SIGNEXTEND (((memval
>> (8 * byte
)) & 0xffffffff), 32);
1021 /* Simulate the mips16 entry and exit pseudo-instructions. These
1022 would normally be handled by the reserved instruction exception
1023 code, but for ease of simulation we just handle them directly. */
1026 mips16_entry (SIM_DESC sd
,
1031 int aregs
, sregs
, rreg
;
1034 printf("DBG: mips16_entry: entered (insn = 0x%08X)\n",insn
);
1037 aregs
= (insn
& 0x700) >> 8;
1038 sregs
= (insn
& 0x0c0) >> 6;
1039 rreg
= (insn
& 0x020) >> 5;
1041 /* This should be checked by the caller. */
1050 /* This is the entry pseudo-instruction. */
1052 for (i
= 0; i
< aregs
; i
++)
1053 store_word (SD
, CPU
, cia
, (uword64
) (SP
+ 4 * i
), GPR
[i
+ 4]);
1061 store_word (SD
, CPU
, cia
, (uword64
) tsp
, RA
);
1064 for (i
= 0; i
< sregs
; i
++)
1067 store_word (SD
, CPU
, cia
, (uword64
) tsp
, GPR
[16 + i
]);
1075 /* This is the exit pseudo-instruction. */
1082 RA
= load_word (SD
, CPU
, cia
, (uword64
) tsp
);
1085 for (i
= 0; i
< sregs
; i
++)
1088 GPR
[i
+ 16] = load_word (SD
, CPU
, cia
, (uword64
) tsp
);
1093 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
1097 FGR
[0] = WORD64LO (GPR
[4]);
1098 FPR_STATE
[0] = fmt_uninterpreted
;
1100 else if (aregs
== 6)
1102 FGR
[0] = WORD64LO (GPR
[5]);
1103 FGR
[1] = WORD64LO (GPR
[4]);
1104 FPR_STATE
[0] = fmt_uninterpreted
;
1105 FPR_STATE
[1] = fmt_uninterpreted
;
1114 /*-- trace support ----------------------------------------------------------*/
1116 /* The TRACE support is provided (if required) in the memory accessing
1117 routines. Since we are also providing the architecture specific
1118 features, the architecture simulation code can also deal with
1119 notifying the TRACE world of cache flushes, etc. Similarly we do
1120 not need to provide profiling support in the simulator engine,
1121 since we can sample in the instruction fetch control loop. By
1122 defining the TRACE manifest, we add tracing as a run-time
1126 /* Tracing by default produces "din" format (as required by
1127 dineroIII). Each line of such a trace file *MUST* have a din label
1128 and address field. The rest of the line is ignored, so comments can
1129 be included if desired. The first field is the label which must be
1130 one of the following values:
1135 3 escape record (treated as unknown access type)
1136 4 escape record (causes cache flush)
1138 The address field is a 32bit (lower-case) hexadecimal address
1139 value. The address should *NOT* be preceded by "0x".
1141 The size of the memory transfer is not important when dealing with
1142 cache lines (as long as no more than a cache line can be
1143 transferred in a single operation :-), however more information
1144 could be given following the dineroIII requirement to allow more
1145 complete memory and cache simulators to provide better
1146 results. i.e. the University of Pisa has a cache simulator that can
1147 also take bus size and speed as (variable) inputs to calculate
1148 complete system performance (a much more useful ability when trying
1149 to construct an end product, rather than a processor). They
1150 currently have an ARM version of their tool called ChARM. */
1154 dotrace (SIM_DESC sd
,
1162 if (STATE
& simTRACE
) {
1164 fprintf(tracefh
,"%d %s ; width %d ; ",
1168 va_start(ap
,comment
);
1169 vfprintf(tracefh
,comment
,ap
);
1171 fprintf(tracefh
,"\n");
1173 /* NOTE: Since the "din" format will only accept 32bit addresses, and
1174 we may be generating 64bit ones, we should put the hi-32bits of the
1175 address into the comment field. */
1177 /* TODO: Provide a buffer for the trace lines. We can then avoid
1178 performing writes until the buffer is filled, or the file is
1181 /* NOTE: We could consider adding a comment field to the "din" file
1182 produced using type 3 markers (unknown access). This would then
1183 allow information about the program that the "din" is for, and
1184 the MIPs world that was being simulated, to be placed into the
1191 /*---------------------------------------------------------------------------*/
1192 /*-- simulator engine -------------------------------------------------------*/
1193 /*---------------------------------------------------------------------------*/
1196 ColdReset (SIM_DESC sd
)
1199 for (cpu_nr
= 0; cpu_nr
< sim_engine_nr_cpus (sd
); cpu_nr
++)
1201 sim_cpu
*cpu
= STATE_CPU (sd
, cpu_nr
);
1202 /* RESET: Fixed PC address: */
1203 PC
= UNSIGNED64 (0xFFFFFFFFBFC00000);
1204 /* The reset vector address is in the unmapped, uncached memory space. */
1206 SR
&= ~(status_SR
| status_TS
| status_RP
);
1207 SR
|= (status_ERL
| status_BEV
);
1209 /* Cheat and allow access to the complete register set immediately */
1210 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
1211 && WITH_TARGET_WORD_BITSIZE
== 64)
1212 SR
|= status_FR
; /* 64bit registers */
1214 /* Ensure that any instructions with pending register updates are
1216 PENDING_INVALIDATE();
1218 /* Initialise the FPU registers to the unknown state */
1219 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
1222 for (rn
= 0; (rn
< 32); rn
++)
1223 FPR_STATE
[rn
] = fmt_uninterpreted
;
1229 /* Description from page A-22 of the "MIPS IV Instruction Set" manual
1231 /* Translate a virtual address to a physical address and cache
1232 coherence algorithm describing the mechanism used to resolve the
1233 memory reference. Given the virtual address vAddr, and whether the
1234 reference is to Instructions ot Data (IorD), find the corresponding
1235 physical address (pAddr) and the cache coherence algorithm (CCA)
1236 used to resolve the reference. If the virtual address is in one of
1237 the unmapped address spaces the physical address and the CCA are
1238 determined directly by the virtual address. If the virtual address
1239 is in one of the mapped address spaces then the TLB is used to
1240 determine the physical address and access type; if the required
1241 translation is not present in the TLB or the desired access is not
1242 permitted the function fails and an exception is taken.
1244 NOTE: Normally (RAW == 0), when address translation fails, this
1245 function raises an exception and does not return. */
1248 address_translation (SIM_DESC sd
,
1254 address_word
*pAddr
,
1258 int res
= -1; /* TRUE : Assume good return */
1261 sim_io_printf(sd
,"AddressTranslation(0x%s,%s,%s,...);\n",pr_addr(vAddr
),(IorD
? "isDATA" : "isINSTRUCTION"),(LorS
? "iSTORE" : "isLOAD"));
1264 /* Check that the address is valid for this memory model */
1266 /* For a simple (flat) memory model, we simply pass virtual
1267 addressess through (mostly) unchanged. */
1268 vAddr
&= 0xFFFFFFFF;
1270 *pAddr
= vAddr
; /* default for isTARGET */
1271 *CCA
= Uncached
; /* not used for isHOST */
1276 /* Description from page A-23 of the "MIPS IV Instruction Set" manual
1278 /* Prefetch data from memory. Prefetch is an advisory instruction for
1279 which an implementation specific action is taken. The action taken
1280 may increase performance, but must not change the meaning of the
1281 program, or alter architecturally-visible state. */
1284 prefetch (SIM_DESC sd
,
1294 sim_io_printf(sd
,"Prefetch(%d,0x%s,0x%s,%d,%d);\n",CCA
,pr_addr(pAddr
),pr_addr(vAddr
),DATA
,hint
);
1297 /* For our simple memory model we do nothing */
1301 /* Description from page A-22 of the "MIPS IV Instruction Set" manual
1303 /* Load a value from memory. Use the cache and main memory as
1304 specified in the Cache Coherence Algorithm (CCA) and the sort of
1305 access (IorD) to find the contents of AccessLength memory bytes
1306 starting at physical location pAddr. The data is returned in the
1307 fixed width naturally-aligned memory element (MemElem). The
1308 low-order two (or three) bits of the address and the AccessLength
1309 indicate which of the bytes within MemElem needs to be given to the
1310 processor. If the memory access type of the reference is uncached
1311 then only the referenced bytes are read from memory and valid
1312 within the memory element. If the access type is cached, and the
1313 data is not present in cache, an implementation specific size and
1314 alignment block of memory is read and loaded into the cache to
1315 satisfy a load reference. At a minimum, the block is the entire
1318 load_memory (SIM_DESC sd
,
1333 sim_io_printf(sd
,"DBG: LoadMemory(%p,%p,%d,%d,0x%s,0x%s,%s)\n",memvalp
,memval1p
,CCA
,AccessLength
,pr_addr(pAddr
),pr_addr(vAddr
),(IorD
? "isDATA" : "isINSTRUCTION"));
1336 #if defined(WARN_MEM)
1337 if (CCA
!= uncached
)
1338 sim_io_eprintf(sd
,"LoadMemory CCA (%d) is not uncached (currently all accesses treated as cached)\n",CCA
);
1339 #endif /* WARN_MEM */
1341 /* If instruction fetch then we need to check that the two lo-order
1342 bits are zero, otherwise raise a InstructionFetch exception: */
1343 if ((IorD
== isINSTRUCTION
)
1344 && ((pAddr
& 0x3) != 0)
1345 && (((pAddr
& 0x1) != 0) || ((vAddr
& 0x1) == 0)))
1346 SignalExceptionInstructionFetch ();
1348 if (((pAddr
& LOADDRMASK
) + AccessLength
) > LOADDRMASK
)
1350 /* In reality this should be a Bus Error */
1351 sim_io_error (sd
, "AccessLength of %d would extend over %dbit aligned boundary for physical address 0x%s\n",
1353 (LOADDRMASK
+ 1) << 2,
1358 dotrace (SD
, CPU
, tracefh
,((IorD
== isDATA
) ? 0 : 2),(unsigned int)(pAddr
&0xFFFFFFFF),(AccessLength
+ 1),"load%s",((IorD
== isDATA
) ? "" : " instruction"));
1361 /* Read the specified number of bytes from memory. Adjust for
1362 host/target byte ordering/ Align the least significant byte
1365 switch (AccessLength
)
1367 case AccessLength_QUADWORD
:
1369 unsigned_16 val
= sim_core_read_aligned_16 (cpu
, NULL_CIA
,
1370 sim_core_read_map
, pAddr
);
1371 value1
= VH8_16 (val
);
1372 value
= VL8_16 (val
);
1375 case AccessLength_DOUBLEWORD
:
1376 value
= sim_core_read_aligned_8 (cpu
, NULL_CIA
,
1377 sim_core_read_map
, pAddr
);
1379 case AccessLength_SEPTIBYTE
:
1380 value
= sim_core_read_misaligned_7 (cpu
, NULL_CIA
,
1381 sim_core_read_map
, pAddr
);
1382 case AccessLength_SEXTIBYTE
:
1383 value
= sim_core_read_misaligned_6 (cpu
, NULL_CIA
,
1384 sim_core_read_map
, pAddr
);
1385 case AccessLength_QUINTIBYTE
:
1386 value
= sim_core_read_misaligned_5 (cpu
, NULL_CIA
,
1387 sim_core_read_map
, pAddr
);
1388 case AccessLength_WORD
:
1389 value
= sim_core_read_aligned_4 (cpu
, NULL_CIA
,
1390 sim_core_read_map
, pAddr
);
1392 case AccessLength_TRIPLEBYTE
:
1393 value
= sim_core_read_misaligned_3 (cpu
, NULL_CIA
,
1394 sim_core_read_map
, pAddr
);
1395 case AccessLength_HALFWORD
:
1396 value
= sim_core_read_aligned_2 (cpu
, NULL_CIA
,
1397 sim_core_read_map
, pAddr
);
1399 case AccessLength_BYTE
:
1400 value
= sim_core_read_aligned_1 (cpu
, NULL_CIA
,
1401 sim_core_read_map
, pAddr
);
1408 printf("DBG: LoadMemory() : (offset %d) : value = 0x%s%s\n",
1409 (int)(pAddr
& LOADDRMASK
),pr_uword64(value1
),pr_uword64(value
));
1412 /* See also store_memory. */
1413 if (AccessLength
<= AccessLength_DOUBLEWORD
)
1416 /* for big endian target, byte (pAddr&LOADDRMASK == 0) is
1417 shifted to the most significant byte position. */
1418 value
<<= (((7 - (pAddr
& LOADDRMASK
)) - AccessLength
) * 8);
1420 /* For little endian target, byte (pAddr&LOADDRMASK == 0)
1421 is already in the correct postition. */
1422 value
<<= ((pAddr
& LOADDRMASK
) * 8);
1426 printf("DBG: LoadMemory() : shifted value = 0x%s%s\n",
1427 pr_uword64(value1
),pr_uword64(value
));
1431 if (memval1p
) *memval1p
= value1
;
1435 /* Description from page A-23 of the "MIPS IV Instruction Set" manual
1437 /* Store a value to memory. The specified data is stored into the
1438 physical location pAddr using the memory hierarchy (data caches and
1439 main memory) as specified by the Cache Coherence Algorithm
1440 (CCA). The MemElem contains the data for an aligned, fixed-width
1441 memory element (word for 32-bit processors, doubleword for 64-bit
1442 processors), though only the bytes that will actually be stored to
1443 memory need to be valid. The low-order two (or three) bits of pAddr
1444 and the AccessLength field indicates which of the bytes within the
1445 MemElem data should actually be stored; only these bytes in memory
1449 store_memory (SIM_DESC sd
,
1455 uword64 MemElem1
, /* High order 64 bits */
1460 sim_io_printf(sd
,"DBG: StoreMemory(%d,%d,0x%s,0x%s,0x%s,0x%s)\n",CCA
,AccessLength
,pr_uword64(MemElem
),pr_uword64(MemElem1
),pr_addr(pAddr
),pr_addr(vAddr
));
1463 #if defined(WARN_MEM)
1464 if (CCA
!= uncached
)
1465 sim_io_eprintf(sd
,"StoreMemory CCA (%d) is not uncached (currently all accesses treated as cached)\n",CCA
);
1466 #endif /* WARN_MEM */
1468 if (((pAddr
& LOADDRMASK
) + AccessLength
) > LOADDRMASK
)
1469 sim_io_error(sd
,"AccessLength of %d would extend over %dbit aligned boundary for physical address 0x%s\n",AccessLength
,(LOADDRMASK
+ 1)<<2,pr_addr(pAddr
));
1472 dotrace (SD
, CPU
, tracefh
,1,(unsigned int)(pAddr
&0xFFFFFFFF),(AccessLength
+ 1),"store");
1476 printf("DBG: StoreMemory: offset = %d MemElem = 0x%s%s\n",(unsigned int)(pAddr
& LOADDRMASK
),pr_uword64(MemElem1
),pr_uword64(MemElem
));
1479 /* See also load_memory */
1480 if (AccessLength
<= AccessLength_DOUBLEWORD
)
1483 /* for big endian target, byte (pAddr&LOADDRMASK == 0) is
1484 shifted to the most significant byte position. */
1485 MemElem
>>= (((7 - (pAddr
& LOADDRMASK
)) - AccessLength
) * 8);
1487 /* For little endian target, byte (pAddr&LOADDRMASK == 0)
1488 is already in the correct postition. */
1489 MemElem
>>= ((pAddr
& LOADDRMASK
) * 8);
1493 printf("DBG: StoreMemory: shift = %d MemElem = 0x%s%s\n",shift
,pr_uword64(MemElem1
),pr_uword64(MemElem
));
1496 switch (AccessLength
)
1498 case AccessLength_QUADWORD
:
1500 unsigned_16 val
= U16_8 (MemElem1
, MemElem
);
1501 sim_core_write_aligned_16 (cpu
, NULL_CIA
,
1502 sim_core_write_map
, pAddr
, val
);
1505 case AccessLength_DOUBLEWORD
:
1506 sim_core_write_aligned_8 (cpu
, NULL_CIA
,
1507 sim_core_write_map
, pAddr
, MemElem
);
1509 case AccessLength_SEPTIBYTE
:
1510 sim_core_write_misaligned_7 (cpu
, NULL_CIA
,
1511 sim_core_write_map
, pAddr
, MemElem
);
1513 case AccessLength_SEXTIBYTE
:
1514 sim_core_write_misaligned_6 (cpu
, NULL_CIA
,
1515 sim_core_write_map
, pAddr
, MemElem
);
1517 case AccessLength_QUINTIBYTE
:
1518 sim_core_write_misaligned_5 (cpu
, NULL_CIA
,
1519 sim_core_write_map
, pAddr
, MemElem
);
1521 case AccessLength_WORD
:
1522 sim_core_write_aligned_4 (cpu
, NULL_CIA
,
1523 sim_core_write_map
, pAddr
, MemElem
);
1525 case AccessLength_TRIPLEBYTE
:
1526 sim_core_write_misaligned_3 (cpu
, NULL_CIA
,
1527 sim_core_write_map
, pAddr
, MemElem
);
1529 case AccessLength_HALFWORD
:
1530 sim_core_write_aligned_2 (cpu
, NULL_CIA
,
1531 sim_core_write_map
, pAddr
, MemElem
);
1533 case AccessLength_BYTE
:
1534 sim_core_write_aligned_1 (cpu
, NULL_CIA
,
1535 sim_core_write_map
, pAddr
, MemElem
);
1546 ifetch32 (SIM_DESC sd
,
1551 /* Copy the action of the LW instruction */
1552 address_word reverse
= (ReverseEndian
? (LOADDRMASK
>> 2) : 0);
1553 address_word bigend
= (BigEndianCPU
? (LOADDRMASK
>> 2) : 0);
1556 unsigned32 instruction
;
1559 AddressTranslation (vaddr
, isINSTRUCTION
, isLOAD
, &paddr
, &cca
, isTARGET
, isREAL
);
1560 paddr
= ((paddr
& ~LOADDRMASK
) | ((paddr
& LOADDRMASK
) ^ (reverse
<< 2)));
1561 LoadMemory (&value
, NULL
, cca
, AccessLength_WORD
, paddr
, vaddr
, isINSTRUCTION
, isREAL
);
1562 byte
= ((vaddr
& LOADDRMASK
) ^ (bigend
<< 2));
1563 instruction
= ((value
>> (8 * byte
)) & 0xFFFFFFFF);
1569 ifetch16 (SIM_DESC sd
,
1574 /* Copy the action of the LW instruction */
1575 address_word reverse
= (ReverseEndian
? (LOADDRMASK
>> 2) : 0);
1576 address_word bigend
= (BigEndianCPU
? (LOADDRMASK
>> 2) : 0);
1579 unsigned16 instruction
;
1582 AddressTranslation (vaddr
, isINSTRUCTION
, isLOAD
, &paddr
, &cca
, isTARGET
, isREAL
);
1583 paddr
= ((paddr
& ~LOADDRMASK
) | ((paddr
& LOADDRMASK
) ^ (reverse
<< 2)));
1584 LoadMemory (&value
, NULL
, cca
, AccessLength_WORD
, paddr
, vaddr
, isINSTRUCTION
, isREAL
);
1585 byte
= ((vaddr
& LOADDRMASK
) ^ (bigend
<< 2));
1586 instruction
= ((value
>> (8 * byte
)) & 0xFFFFFFFF);
1591 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1592 /* Order loads and stores to synchronise shared memory. Perform the
1593 action necessary to make the effects of groups of synchronizable
1594 loads and stores indicated by stype occur in the same order for all
1597 sync_operation (SIM_DESC sd
,
1603 sim_io_printf(sd
,"SyncOperation(%d) : TODO\n",stype
);
1608 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1609 /* Signal an exception condition. This will result in an exception
1610 that aborts the instruction. The instruction operation pseudocode
1611 will never see a return from this function call. */
1614 signal_exception (SIM_DESC sd
,
1622 sim_io_printf(sd
,"DBG: SignalException(%d) PC = 0x%s\n",exception
,pr_addr(cia
));
1625 /* Ensure that any active atomic read/modify/write operation will fail: */
1628 switch (exception
) {
1629 /* TODO: For testing purposes I have been ignoring TRAPs. In
1630 reality we should either simulate them, or allow the user to
1631 ignore them at run-time.
1634 sim_io_eprintf(sd
,"Ignoring instruction TRAP (PC 0x%s)\n",pr_addr(cia
));
1640 unsigned int instruction
;
1643 va_start(ap
,exception
);
1644 instruction
= va_arg(ap
,unsigned int);
1647 code
= (instruction
>> 6) & 0xFFFFF;
1649 sim_io_eprintf(sd
,"Ignoring instruction `syscall %d' (PC 0x%s)\n",
1650 code
, pr_addr(cia
));
1654 case DebugBreakPoint
:
1655 if (! (Debug
& Debug_DM
))
1661 Debug
|= Debug_DBD
; /* signaled from within in delay slot */
1662 DEPC
= cia
- 4; /* reference the branch instruction */
1666 Debug
&= ~Debug_DBD
; /* not signaled from within a delay slot */
1670 Debug
|= Debug_DM
; /* in debugging mode */
1671 Debug
|= Debug_DBp
; /* raising a DBp exception */
1673 sim_engine_restart (SD
, CPU
, NULL
, NULL_CIA
);
1677 case ReservedInstruction
:
1680 unsigned int instruction
;
1681 va_start(ap
,exception
);
1682 instruction
= va_arg(ap
,unsigned int);
1684 /* Provide simple monitor support using ReservedInstruction
1685 exceptions. The following code simulates the fixed vector
1686 entry points into the IDT monitor by causing a simulator
1687 trap, performing the monitor operation, and returning to
1688 the address held in the $ra register (standard PCS return
1689 address). This means we only need to pre-load the vector
1690 space with suitable instruction values. For systems were
1691 actual trap instructions are used, we would not need to
1692 perform this magic. */
1693 if ((instruction
& RSVD_INSTRUCTION_MASK
) == RSVD_INSTRUCTION
)
1695 sim_monitor (SD
, CPU
, cia
, ((instruction
>> RSVD_INSTRUCTION_ARG_SHIFT
) & RSVD_INSTRUCTION_ARG_MASK
) );
1696 /* NOTE: This assumes that a branch-and-link style
1697 instruction was used to enter the vector (which is the
1698 case with the current IDT monitor). */
1699 sim_engine_restart (SD
, CPU
, NULL
, RA
);
1701 /* Look for the mips16 entry and exit instructions, and
1702 simulate a handler for them. */
1703 else if ((cia
& 1) != 0
1704 && (instruction
& 0xf81f) == 0xe809
1705 && (instruction
& 0x0c0) != 0x0c0)
1707 mips16_entry (SD
, CPU
, cia
, instruction
);
1708 sim_engine_restart (sd
, NULL
, NULL
, NULL_CIA
);
1710 /* else fall through to normal exception processing */
1711 sim_io_eprintf(sd
,"ReservedInstruction 0x%08X at PC = 0x%s\n",instruction
,pr_addr(cia
));
1716 sim_io_printf(sd
,"DBG: SignalException(%d) PC = 0x%s\n",exception
,pr_addr(cia
));
1718 /* Keep a copy of the current A0 in-case this is the program exit
1722 unsigned int instruction
;
1723 va_start(ap
,exception
);
1724 instruction
= va_arg(ap
,unsigned int);
1726 /* Check for our special terminating BREAK: */
1727 if ((instruction
& 0x03FFFFC0) == 0x03ff0000) {
1728 sim_engine_halt (SD
, CPU
, NULL
, cia
,
1729 sim_exited
, (unsigned int)(A0
& 0xFFFFFFFF));
1732 if (STATE
& simDELAYSLOT
)
1733 PC
= cia
- 4; /* reference the branch instruction */
1736 sim_engine_halt (SD
, CPU
, NULL
, cia
,
1737 sim_stopped
, SIM_SIGTRAP
);
1740 /* Store exception code into current exception id variable (used
1743 /* TODO: If not simulating exceptions then stop the simulator
1744 execution. At the moment we always stop the simulation. */
1746 /* See figure 5-17 for an outline of the code below */
1747 if (! (SR
& status_EXL
))
1749 CAUSE
= (exception
<< 2);
1750 if (STATE
& simDELAYSLOT
)
1752 STATE
&= ~simDELAYSLOT
;
1754 EPC
= (cia
- 4); /* reference the branch instruction */
1758 /* FIXME: TLB et.al. */
1763 CAUSE
= (exception
<< 2);
1767 /* Store exception code into current exception id variable (used
1769 if (SR
& status_BEV
)
1770 PC
= (signed)0xBFC00200 + 0x180;
1772 PC
= (signed)0x80000000 + 0x180;
1774 switch ((CAUSE
>> 2) & 0x1F)
1777 /* Interrupts arrive during event processing, no need to
1781 case TLBModification
:
1786 case InstructionFetch
:
1788 /* The following is so that the simulator will continue from the
1789 exception address on breakpoint operations. */
1791 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1792 sim_stopped
, SIM_SIGBUS
);
1794 case ReservedInstruction
:
1795 case CoProcessorUnusable
:
1797 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1798 sim_stopped
, SIM_SIGILL
);
1800 case IntegerOverflow
:
1802 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1803 sim_stopped
, SIM_SIGFPE
);
1809 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1810 sim_stopped
, SIM_SIGTRAP
);
1814 sim_engine_abort (SD
, CPU
, NULL_CIA
,
1815 "FATAL: Should not encounter a breakpoint\n");
1817 default : /* Unknown internal exception */
1819 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1820 sim_stopped
, SIM_SIGABRT
);
1824 case SimulatorFault
:
1828 va_start(ap
,exception
);
1829 msg
= va_arg(ap
,char *);
1831 sim_engine_abort (SD
, CPU
, NULL_CIA
,
1832 "FATAL: Simulator error \"%s\"\n",msg
);
1839 #if defined(WARN_RESULT)
1840 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1841 /* This function indicates that the result of the operation is
1842 undefined. However, this should not affect the instruction
1843 stream. All that is meant to happen is that the destination
1844 register is set to an undefined result. To keep the simulator
1845 simple, we just don't bother updating the destination register, so
1846 the overall result will be undefined. If desired we can stop the
1847 simulator by raising a pseudo-exception. */
1848 #define UndefinedResult() undefined_result (sd,cia)
1850 undefined_result(sd
,cia
)
1854 sim_io_eprintf(sd
,"UndefinedResult: PC = 0x%s\n",pr_addr(cia
));
1855 #if 0 /* Disabled for the moment, since it actually happens a lot at the moment. */
1860 #endif /* WARN_RESULT */
1863 cache_op (SIM_DESC sd
,
1869 unsigned int instruction
)
1871 #if 1 /* stop warning message being displayed (we should really just remove the code) */
1872 static int icache_warning
= 1;
1873 static int dcache_warning
= 1;
1875 static int icache_warning
= 0;
1876 static int dcache_warning
= 0;
1879 /* If CP0 is not useable (User or Supervisor mode) and the CP0
1880 enable bit in the Status Register is clear - a coprocessor
1881 unusable exception is taken. */
1883 sim_io_printf(sd
,"TODO: Cache availability checking (PC = 0x%s)\n",pr_addr(cia
));
1887 case 0: /* instruction cache */
1889 case 0: /* Index Invalidate */
1890 case 1: /* Index Load Tag */
1891 case 2: /* Index Store Tag */
1892 case 4: /* Hit Invalidate */
1894 case 6: /* Hit Writeback */
1895 if (!icache_warning
)
1897 sim_io_eprintf(sd
,"Instruction CACHE operation %d to be coded\n",(op
>> 2));
1903 SignalException(ReservedInstruction
,instruction
);
1908 case 1: /* data cache */
1910 case 0: /* Index Writeback Invalidate */
1911 case 1: /* Index Load Tag */
1912 case 2: /* Index Store Tag */
1913 case 3: /* Create Dirty */
1914 case 4: /* Hit Invalidate */
1915 case 5: /* Hit Writeback Invalidate */
1916 case 6: /* Hit Writeback */
1917 if (!dcache_warning
)
1919 sim_io_eprintf(sd
,"Data CACHE operation %d to be coded\n",(op
>> 2));
1925 SignalException(ReservedInstruction
,instruction
);
1930 default: /* unrecognised cache ID */
1931 SignalException(ReservedInstruction
,instruction
);
1938 /*-- FPU support routines ---------------------------------------------------*/
1940 /* Numbers are held in normalized form. The SINGLE and DOUBLE binary
1941 formats conform to ANSI/IEEE Std 754-1985. */
1942 /* SINGLE precision floating:
1943 * seeeeeeeefffffffffffffffffffffff
1945 * e = 8bits = exponent
1946 * f = 23bits = fraction
1948 /* SINGLE precision fixed:
1949 * siiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
1951 * i = 31bits = integer
1953 /* DOUBLE precision floating:
1954 * seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
1956 * e = 11bits = exponent
1957 * f = 52bits = fraction
1959 /* DOUBLE precision fixed:
1960 * siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
1962 * i = 63bits = integer
1965 /* Extract sign-bit: */
1966 #define FP_S_s(v) (((v) & ((unsigned)1 << 31)) ? 1 : 0)
1967 #define FP_D_s(v) (((v) & ((uword64)1 << 63)) ? 1 : 0)
1968 /* Extract biased exponent: */
1969 #define FP_S_be(v) (((v) >> 23) & 0xFF)
1970 #define FP_D_be(v) (((v) >> 52) & 0x7FF)
1971 /* Extract unbiased Exponent: */
1972 #define FP_S_e(v) (FP_S_be(v) - 0x7F)
1973 #define FP_D_e(v) (FP_D_be(v) - 0x3FF)
1974 /* Extract complete fraction field: */
1975 #define FP_S_f(v) ((v) & ~((unsigned)0x1FF << 23))
1976 #define FP_D_f(v) ((v) & ~((uword64)0xFFF << 52))
1977 /* Extract numbered fraction bit: */
1978 #define FP_S_fb(b,v) (((v) & (1 << (23 - (b)))) ? 1 : 0)
1979 #define FP_D_fb(b,v) (((v) & (1 << (52 - (b)))) ? 1 : 0)
1981 /* Explicit QNaN values used when value required: */
1982 #define FPQNaN_SINGLE (0x7FBFFFFF)
1983 #define FPQNaN_WORD (0x7FFFFFFF)
1984 #define FPQNaN_DOUBLE (((uword64)0x7FF7FFFF << 32) | 0xFFFFFFFF)
1985 #define FPQNaN_LONG (((uword64)0x7FFFFFFF << 32) | 0xFFFFFFFF)
1987 /* Explicit Infinity values used when required: */
1988 #define FPINF_SINGLE (0x7F800000)
1989 #define FPINF_DOUBLE (((uword64)0x7FF00000 << 32) | 0x00000000)
1991 #if 1 /* def DEBUG */
1992 #define RMMODE(v) (((v) == FP_RM_NEAREST) ? "Round" : (((v) == FP_RM_TOZERO) ? "Trunc" : (((v) == FP_RM_TOPINF) ? "Ceil" : "Floor")))
1993 #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>" : "<format error>"))))))
1997 value_fpr (SIM_DESC sd
,
2006 /* Treat unused register values, as fixed-point 64bit values: */
2007 if ((fmt
== fmt_uninterpreted
) || (fmt
== fmt_unknown
))
2009 /* If request to read data as "uninterpreted", then use the current
2011 fmt
= FPR_STATE
[fpr
];
2016 /* For values not yet accessed, set to the desired format: */
2017 if (FPR_STATE
[fpr
] == fmt_uninterpreted
) {
2018 FPR_STATE
[fpr
] = fmt
;
2020 printf("DBG: Register %d was fmt_uninterpreted. Now %s\n",fpr
,DOFMT(fmt
));
2023 if (fmt
!= FPR_STATE
[fpr
]) {
2024 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
));
2025 FPR_STATE
[fpr
] = fmt_unknown
;
2028 if (FPR_STATE
[fpr
] == fmt_unknown
) {
2029 /* Set QNaN value: */
2032 value
= FPQNaN_SINGLE
;
2036 value
= FPQNaN_DOUBLE
;
2040 value
= FPQNaN_WORD
;
2044 value
= FPQNaN_LONG
;
2051 } else if (SizeFGR() == 64) {
2055 value
= (FGR
[fpr
] & 0xFFFFFFFF);
2058 case fmt_uninterpreted
:
2072 value
= (FGR
[fpr
] & 0xFFFFFFFF);
2075 case fmt_uninterpreted
:
2078 if ((fpr
& 1) == 0) { /* even registers only */
2079 value
= ((((uword64
)FGR
[fpr
+1]) << 32) | (FGR
[fpr
] & 0xFFFFFFFF));
2081 SignalException(ReservedInstruction
,0);
2092 SignalExceptionSimulatorFault ("Unrecognised FP format in ValueFPR()");
2095 printf("DBG: ValueFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR() = %d\n",fpr
,DOFMT(fmt
),pr_addr(value
),pr_addr(cia
),SizeFGR());
2102 store_fpr (SIM_DESC sd
,
2112 printf("DBG: StoreFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR() = %d\n",fpr
,DOFMT(fmt
),pr_addr(value
),pr_addr(cia
),SizeFGR());
2115 if (SizeFGR() == 64) {
2117 case fmt_uninterpreted_32
:
2118 fmt
= fmt_uninterpreted
;
2121 FGR
[fpr
] = (((uword64
)0xDEADC0DE << 32) | (value
& 0xFFFFFFFF));
2122 FPR_STATE
[fpr
] = fmt
;
2125 case fmt_uninterpreted_64
:
2126 fmt
= fmt_uninterpreted
;
2127 case fmt_uninterpreted
:
2131 FPR_STATE
[fpr
] = fmt
;
2135 FPR_STATE
[fpr
] = fmt_unknown
;
2141 case fmt_uninterpreted_32
:
2142 fmt
= fmt_uninterpreted
;
2145 FGR
[fpr
] = (value
& 0xFFFFFFFF);
2146 FPR_STATE
[fpr
] = fmt
;
2149 case fmt_uninterpreted_64
:
2150 fmt
= fmt_uninterpreted
;
2151 case fmt_uninterpreted
:
2154 if ((fpr
& 1) == 0) { /* even register number only */
2155 FGR
[fpr
+1] = (value
>> 32);
2156 FGR
[fpr
] = (value
& 0xFFFFFFFF);
2157 FPR_STATE
[fpr
+ 1] = fmt
;
2158 FPR_STATE
[fpr
] = fmt
;
2160 FPR_STATE
[fpr
] = fmt_unknown
;
2161 FPR_STATE
[fpr
+ 1] = fmt_unknown
;
2162 SignalException(ReservedInstruction
,0);
2167 FPR_STATE
[fpr
] = fmt_unknown
;
2172 #if defined(WARN_RESULT)
2175 #endif /* WARN_RESULT */
2178 SignalExceptionSimulatorFault ("Unrecognised FP format in StoreFPR()");
2181 printf("DBG: StoreFPR: fpr[%d] = 0x%s (format %s)\n",fpr
,pr_addr(FGR
[fpr
]),DOFMT(fmt
));
2198 sim_fpu_32to (&wop
, op
);
2199 boolean
= sim_fpu_is_nan (&wop
);
2206 sim_fpu_64to (&wop
, op
);
2207 boolean
= sim_fpu_is_nan (&wop
);
2211 fprintf (stderr
, "Bad switch\n");
2216 printf("DBG: NaN: returning %d for 0x%s (format = %s)\n",boolean
,pr_addr(op
),DOFMT(fmt
));
2230 printf("DBG: Infinity: format %s 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2237 sim_fpu_32to (&wop
, op
);
2238 boolean
= sim_fpu_is_infinity (&wop
);
2244 sim_fpu_64to (&wop
, op
);
2245 boolean
= sim_fpu_is_infinity (&wop
);
2249 printf("DBG: TODO: unrecognised format (%s) for Infinity check\n",DOFMT(fmt
));
2254 printf("DBG: Infinity: returning %d for 0x%s (format = %s)\n",boolean
,pr_addr(op
),DOFMT(fmt
));
2268 /* Argument checking already performed by the FPCOMPARE code */
2271 printf("DBG: Less: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2274 /* The format type should already have been checked: */
2280 sim_fpu_32to (&wop1
, op1
);
2281 sim_fpu_32to (&wop2
, op2
);
2282 boolean
= sim_fpu_is_lt (&wop1
, &wop2
);
2289 sim_fpu_64to (&wop1
, op1
);
2290 sim_fpu_64to (&wop2
, op2
);
2291 boolean
= sim_fpu_is_lt (&wop1
, &wop2
);
2295 fprintf (stderr
, "Bad switch\n");
2300 printf("DBG: Less: returning %d (format = %s)\n",boolean
,DOFMT(fmt
));
2314 /* Argument checking already performed by the FPCOMPARE code */
2317 printf("DBG: Equal: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2320 /* The format type should already have been checked: */
2326 sim_fpu_32to (&wop1
, op1
);
2327 sim_fpu_32to (&wop2
, op2
);
2328 boolean
= sim_fpu_is_eq (&wop1
, &wop2
);
2335 sim_fpu_64to (&wop1
, op1
);
2336 sim_fpu_64to (&wop2
, op2
);
2337 boolean
= sim_fpu_is_eq (&wop1
, &wop2
);
2341 fprintf (stderr
, "Bad switch\n");
2346 printf("DBG: Equal: returning %d (format = %s)\n",boolean
,DOFMT(fmt
));
2353 AbsoluteValue(op
,fmt
)
2360 printf("DBG: AbsoluteValue: %s: op = 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2363 /* The format type should already have been checked: */
2369 sim_fpu_32to (&wop
, op
);
2370 sim_fpu_abs (&wop
, &wop
);
2371 sim_fpu_to32 (&ans
, &wop
);
2379 sim_fpu_64to (&wop
, op
);
2380 sim_fpu_abs (&wop
, &wop
);
2381 sim_fpu_to64 (&ans
, &wop
);
2386 fprintf (stderr
, "Bad switch\n");
2401 printf("DBG: Negate: %s: op = 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2404 /* The format type should already have been checked: */
2410 sim_fpu_32to (&wop
, op
);
2411 sim_fpu_neg (&wop
, &wop
);
2412 sim_fpu_to32 (&ans
, &wop
);
2420 sim_fpu_64to (&wop
, op
);
2421 sim_fpu_neg (&wop
, &wop
);
2422 sim_fpu_to64 (&ans
, &wop
);
2427 fprintf (stderr
, "Bad switch\n");
2443 printf("DBG: Add: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2446 /* The registers must specify FPRs valid for operands of type
2447 "fmt". If they are not valid, the result is undefined. */
2449 /* The format type should already have been checked: */
2457 sim_fpu_32to (&wop1
, op1
);
2458 sim_fpu_32to (&wop2
, op2
);
2459 sim_fpu_add (&ans
, &wop1
, &wop2
);
2460 sim_fpu_to32 (&res
, &ans
);
2470 sim_fpu_64to (&wop1
, op1
);
2471 sim_fpu_64to (&wop2
, op2
);
2472 sim_fpu_add (&ans
, &wop1
, &wop2
);
2473 sim_fpu_to64 (&res
, &ans
);
2478 fprintf (stderr
, "Bad switch\n");
2483 printf("DBG: Add: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2498 printf("DBG: Sub: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2501 /* The registers must specify FPRs valid for operands of type
2502 "fmt". If they are not valid, the result is undefined. */
2504 /* The format type should already have been checked: */
2512 sim_fpu_32to (&wop1
, op1
);
2513 sim_fpu_32to (&wop2
, op2
);
2514 sim_fpu_sub (&ans
, &wop1
, &wop2
);
2515 sim_fpu_to32 (&res
, &ans
);
2525 sim_fpu_64to (&wop1
, op1
);
2526 sim_fpu_64to (&wop2
, op2
);
2527 sim_fpu_sub (&ans
, &wop1
, &wop2
);
2528 sim_fpu_to64 (&res
, &ans
);
2533 fprintf (stderr
, "Bad switch\n");
2538 printf("DBG: Sub: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2545 Multiply(op1
,op2
,fmt
)
2553 printf("DBG: Multiply: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2556 /* The registers must specify FPRs valid for operands of type
2557 "fmt". If they are not valid, the result is undefined. */
2559 /* The format type should already have been checked: */
2567 sim_fpu_32to (&wop1
, op1
);
2568 sim_fpu_32to (&wop2
, op2
);
2569 sim_fpu_mul (&ans
, &wop1
, &wop2
);
2570 sim_fpu_to32 (&res
, &ans
);
2580 sim_fpu_64to (&wop1
, op1
);
2581 sim_fpu_64to (&wop2
, op2
);
2582 sim_fpu_mul (&ans
, &wop1
, &wop2
);
2583 sim_fpu_to64 (&res
, &ans
);
2588 fprintf (stderr
, "Bad switch\n");
2593 printf("DBG: Multiply: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2608 printf("DBG: Divide: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2611 /* The registers must specify FPRs valid for operands of type
2612 "fmt". If they are not valid, the result is undefined. */
2614 /* The format type should already have been checked: */
2622 sim_fpu_32to (&wop1
, op1
);
2623 sim_fpu_32to (&wop2
, op2
);
2624 sim_fpu_div (&ans
, &wop1
, &wop2
);
2625 sim_fpu_to32 (&res
, &ans
);
2635 sim_fpu_64to (&wop1
, op1
);
2636 sim_fpu_64to (&wop2
, op2
);
2637 sim_fpu_div (&ans
, &wop1
, &wop2
);
2638 sim_fpu_to64 (&res
, &ans
);
2643 fprintf (stderr
, "Bad switch\n");
2648 printf("DBG: Divide: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2662 printf("DBG: Recip: %s: op = 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2665 /* The registers must specify FPRs valid for operands of type
2666 "fmt". If they are not valid, the result is undefined. */
2668 /* The format type should already have been checked: */
2675 sim_fpu_32to (&wop
, op
);
2676 sim_fpu_inv (&ans
, &wop
);
2677 sim_fpu_to32 (&res
, &ans
);
2686 sim_fpu_64to (&wop
, op
);
2687 sim_fpu_inv (&ans
, &wop
);
2688 sim_fpu_to64 (&res
, &ans
);
2693 fprintf (stderr
, "Bad switch\n");
2698 printf("DBG: Recip: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2712 printf("DBG: SquareRoot: %s: op = 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2715 /* The registers must specify FPRs valid for operands of type
2716 "fmt". If they are not valid, the result is undefined. */
2718 /* The format type should already have been checked: */
2725 sim_fpu_32to (&wop
, op
);
2726 sim_fpu_sqrt (&ans
, &wop
);
2727 sim_fpu_to32 (&res
, &ans
);
2736 sim_fpu_64to (&wop
, op
);
2737 sim_fpu_sqrt (&ans
, &wop
);
2738 sim_fpu_to64 (&res
, &ans
);
2743 fprintf (stderr
, "Bad switch\n");
2748 printf("DBG: SquareRoot: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2755 convert (SIM_DESC sd
,
2764 sim_fpu_round round
;
2765 unsigned32 result32
;
2766 unsigned64 result64
;
2769 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
));
2775 /* Round result to nearest representable value. When two
2776 representable values are equally near, round to the value
2777 that has a least significant bit of zero (i.e. is even). */
2778 round
= sim_fpu_round_near
;
2781 /* Round result to the value closest to, and not greater in
2782 magnitude than, the result. */
2783 round
= sim_fpu_round_zero
;
2786 /* Round result to the value closest to, and not less than,
2788 round
= sim_fpu_round_up
;
2792 /* Round result to the value closest to, and not greater than,
2794 round
= sim_fpu_round_down
;
2798 fprintf (stderr
, "Bad switch\n");
2802 /* Convert the input to sim_fpu internal format */
2806 sim_fpu_64to (&wop
, op
);
2809 sim_fpu_32to (&wop
, op
);
2812 sim_fpu_i32to (&wop
, op
, round
);
2815 sim_fpu_i64to (&wop
, op
, round
);
2818 fprintf (stderr
, "Bad switch\n");
2822 /* Convert sim_fpu format into the output */
2823 /* The value WOP is converted to the destination format, rounding
2824 using mode RM. When the destination is a fixed-point format, then
2825 a source value of Infinity, NaN or one which would round to an
2826 integer outside the fixed point range then an IEEE Invalid
2827 Operation condition is raised. */
2831 sim_fpu_round_32 (&wop
, round
, 0);
2832 sim_fpu_to32 (&result32
, &wop
);
2833 result64
= result32
;
2836 sim_fpu_round_64 (&wop
, round
, 0);
2837 sim_fpu_to64 (&result64
, &wop
);
2840 sim_fpu_to32i (&result32
, &wop
, round
);
2841 result64
= result32
;
2844 sim_fpu_to64i (&result64
, &wop
, round
);
2848 fprintf (stderr
, "Bad switch\n");
2853 printf("DBG: Convert: returning 0x%s (to format = %s)\n",pr_addr(result64
),DOFMT(to
));
2860 /*-- co-processor support routines ------------------------------------------*/
2863 CoProcPresent(coproc_number
)
2864 unsigned int coproc_number
;
2866 /* Return TRUE if simulator provides a model for the given co-processor number */
2871 cop_lw (SIM_DESC sd
,
2876 unsigned int memword
)
2881 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
2884 printf("DBG: COP_LW: memword = 0x%08X (uword64)memword = 0x%s\n",memword
,pr_addr(memword
));
2886 StoreFPR(coproc_reg
,fmt_word
,(uword64
)memword
);
2887 FPR_STATE
[coproc_reg
] = fmt_uninterpreted
;
2892 #if 0 /* this should be controlled by a configuration option */
2893 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
));
2902 cop_ld (SIM_DESC sd
,
2909 switch (coproc_num
) {
2911 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
2913 StoreFPR(coproc_reg
,fmt_uninterpreted
,memword
);
2918 #if 0 /* this message should be controlled by a configuration option */
2919 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
));
2928 cop_sw (SIM_DESC sd
,
2934 unsigned int value
= 0;
2939 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
2942 hold
= FPR_STATE
[coproc_reg
];
2943 FPR_STATE
[coproc_reg
] = fmt_word
;
2944 value
= (unsigned int)ValueFPR(coproc_reg
,fmt_uninterpreted
);
2945 FPR_STATE
[coproc_reg
] = hold
;
2950 #if 0 /* should be controlled by configuration option */
2951 sim_io_printf(sd
,"COP_SW(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num
,coproc_reg
,pr_addr(cia
));
2960 cop_sd (SIM_DESC sd
,
2970 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
2972 value
= ValueFPR(coproc_reg
,fmt_uninterpreted
);
2977 #if 0 /* should be controlled by configuration option */
2978 sim_io_printf(sd
,"COP_SD(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num
,coproc_reg
,pr_addr(cia
));
2987 decode_coproc (SIM_DESC sd
,
2990 unsigned int instruction
)
2992 int coprocnum
= ((instruction
>> 26) & 3);
2996 case 0: /* standard CPU control and cache registers */
2998 int code
= ((instruction
>> 21) & 0x1F);
2999 /* R4000 Users Manual (second edition) lists the following CP0
3001 DMFC0 Doubleword Move From CP0 (VR4100 = 01000000001tttttddddd00000000000)
3002 DMTC0 Doubleword Move To CP0 (VR4100 = 01000000101tttttddddd00000000000)
3003 MFC0 word Move From CP0 (VR4100 = 01000000000tttttddddd00000000000)
3004 MTC0 word Move To CP0 (VR4100 = 01000000100tttttddddd00000000000)
3005 TLBR Read Indexed TLB Entry (VR4100 = 01000010000000000000000000000001)
3006 TLBWI Write Indexed TLB Entry (VR4100 = 01000010000000000000000000000010)
3007 TLBWR Write Random TLB Entry (VR4100 = 01000010000000000000000000000110)
3008 TLBP Probe TLB for Matching Entry (VR4100 = 01000010000000000000000000001000)
3009 CACHE Cache operation (VR4100 = 101111bbbbbpppppiiiiiiiiiiiiiiii)
3010 ERET Exception return (VR4100 = 01000010000000000000000000011000)
3012 if (((code
== 0x00) || (code
== 0x04)) && ((instruction
& 0x7FF) == 0))
3014 int rt
= ((instruction
>> 16) & 0x1F);
3015 int rd
= ((instruction
>> 11) & 0x1F);
3017 switch (rd
) /* NOTEs: Standard CP0 registers */
3019 /* 0 = Index R4000 VR4100 VR4300 */
3020 /* 1 = Random R4000 VR4100 VR4300 */
3021 /* 2 = EntryLo0 R4000 VR4100 VR4300 */
3022 /* 3 = EntryLo1 R4000 VR4100 VR4300 */
3023 /* 4 = Context R4000 VR4100 VR4300 */
3024 /* 5 = PageMask R4000 VR4100 VR4300 */
3025 /* 6 = Wired R4000 VR4100 VR4300 */
3026 /* 8 = BadVAddr R4000 VR4100 VR4300 */
3027 /* 9 = Count R4000 VR4100 VR4300 */
3028 /* 10 = EntryHi R4000 VR4100 VR4300 */
3029 /* 11 = Compare R4000 VR4100 VR4300 */
3030 /* 12 = SR R4000 VR4100 VR4300 */
3037 /* 13 = Cause R4000 VR4100 VR4300 */
3044 /* 14 = EPC R4000 VR4100 VR4300 */
3045 /* 15 = PRId R4000 VR4100 VR4300 */
3046 #ifdef SUBTARGET_R3900
3055 /* 16 = Config R4000 VR4100 VR4300 */
3058 GPR
[rt
] = C0_CONFIG
;
3060 C0_CONFIG
= GPR
[rt
];
3063 #ifdef SUBTARGET_R3900
3072 /* 17 = LLAddr R4000 VR4100 VR4300 */
3074 /* 18 = WatchLo R4000 VR4100 VR4300 */
3075 /* 19 = WatchHi R4000 VR4100 VR4300 */
3076 /* 20 = XContext R4000 VR4100 VR4300 */
3077 /* 26 = PErr or ECC R4000 VR4100 VR4300 */
3078 /* 27 = CacheErr R4000 VR4100 */
3079 /* 28 = TagLo R4000 VR4100 VR4300 */
3080 /* 29 = TagHi R4000 VR4100 VR4300 */
3081 /* 30 = ErrorEPC R4000 VR4100 VR4300 */
3082 GPR
[rt
] = 0xDEADC0DE; /* CPR[0,rd] */
3083 /* CPR[0,rd] = GPR[rt]; */
3086 sim_io_printf(sd
,"Warning: MFC0 %d,%d ignored (architecture specific)\n",rt
,rd
);
3088 sim_io_printf(sd
,"Warning: MTC0 %d,%d ignored (architecture specific)\n",rt
,rd
);
3091 else if (code
== 0x10 && (instruction
& 0x3f) == 0x18)
3094 if (SR
& status_ERL
)
3096 /* Oops, not yet available */
3097 sim_io_printf(sd
,"Warning: ERET when SR[ERL] set not handled yet");
3107 else if (code
== 0x10 && (instruction
& 0x3f) == 0x10)
3111 else if (code
== 0x10 && (instruction
& 0x3f) == 0x1F)
3119 sim_io_eprintf(sd
,"Unrecognised COP0 instruction 0x%08X at PC = 0x%s : No handler present\n",instruction
,pr_addr(cia
));
3120 /* TODO: When executing an ERET or RFE instruction we should
3121 clear LLBIT, to ensure that any out-standing atomic
3122 read/modify/write sequence fails. */
3126 case 2: /* undefined co-processor */
3127 sim_io_eprintf(sd
,"COP2 instruction 0x%08X at PC = 0x%s : No handler present\n",instruction
,pr_addr(cia
));
3130 case 1: /* should not occur (FPU co-processor) */
3131 case 3: /* should not occur (FPU co-processor) */
3132 SignalException(ReservedInstruction
,instruction
);
3139 /*-- instruction simulation -------------------------------------------------*/
3141 /* When the IGEN simulator is being built, the function below is be
3142 replaced by a generated version. However, WITH_IGEN == 2 indicates
3143 that the fubction below should be compiled but under a different
3144 name (to allow backward compatibility) */
3146 #if (WITH_IGEN != 1)
3148 void old_engine_run
PARAMS ((SIM_DESC sd
, int next_cpu_nr
, int siggnal
));
3150 old_engine_run (sd
, next_cpu_nr
, nr_cpus
, siggnal
)
3153 sim_engine_run (sd
, next_cpu_nr
, nr_cpus
, siggnal
)
3156 int next_cpu_nr
; /* ignore */
3157 int nr_cpus
; /* ignore */
3158 int siggnal
; /* ignore */
3160 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* hardwire to cpu 0 */
3161 #if !defined(FASTSIM)
3162 unsigned int pipeline_count
= 1;
3166 if (STATE_MEMORY (sd
) == NULL
) {
3167 printf("DBG: simulate() entered with no memory\n");
3172 #if 0 /* Disabled to check that everything works OK */
3173 /* The VR4300 seems to sign-extend the PC on its first
3174 access. However, this may just be because it is currently
3175 configured in 32bit mode. However... */
3176 PC
= SIGNEXTEND(PC
,32);
3179 /* main controlling loop */
3181 /* vaddr is slowly being replaced with cia - current instruction
3183 address_word cia
= (uword64
)PC
;
3184 address_word vaddr
= cia
;
3187 unsigned int instruction
; /* uword64? what's this used for? FIXME! */
3191 printf("DBG: state = 0x%08X :",state
);
3192 if (state
& simHALTEX
) printf(" simHALTEX");
3193 if (state
& simHALTIN
) printf(" simHALTIN");
3198 DSSTATE
= (STATE
& simDELAYSLOT
);
3201 sim_io_printf(sd
,"DBG: DSPC = 0x%s\n",pr_addr(DSPC
));
3204 /* Fetch the next instruction from the simulator memory: */
3205 if (AddressTranslation(cia
,isINSTRUCTION
,isLOAD
,&paddr
,&cca
,isTARGET
,isREAL
)) {
3206 if ((vaddr
& 1) == 0) {
3207 /* Copy the action of the LW instruction */
3208 unsigned int reverse
= (ReverseEndian
? (LOADDRMASK
>> 2) : 0);
3209 unsigned int bigend
= (BigEndianCPU
? (LOADDRMASK
>> 2) : 0);
3212 paddr
= ((paddr
& ~LOADDRMASK
) | ((paddr
& LOADDRMASK
) ^ (reverse
<< 2)));
3213 LoadMemory(&value
,NULL
,cca
,AccessLength_WORD
,paddr
,vaddr
,isINSTRUCTION
,isREAL
);
3214 byte
= ((vaddr
& LOADDRMASK
) ^ (bigend
<< 2));
3215 instruction
= ((value
>> (8 * byte
)) & 0xFFFFFFFF);
3217 /* Copy the action of the LH instruction */
3218 unsigned int reverse
= (ReverseEndian
? (LOADDRMASK
>> 1) : 0);
3219 unsigned int bigend
= (BigEndianCPU
? (LOADDRMASK
>> 1) : 0);
3222 paddr
= (((paddr
& ~ (uword64
) 1) & ~LOADDRMASK
)
3223 | (((paddr
& ~ (uword64
) 1) & LOADDRMASK
) ^ (reverse
<< 1)));
3224 LoadMemory(&value
,NULL
,cca
, AccessLength_HALFWORD
,
3225 paddr
& ~ (uword64
) 1,
3226 vaddr
, isINSTRUCTION
, isREAL
);
3227 byte
= (((vaddr
&~ (uword64
) 1) & LOADDRMASK
) ^ (bigend
<< 1));
3228 instruction
= ((value
>> (8 * byte
)) & 0xFFFF);
3231 fprintf(stderr
,"Cannot translate address for PC = 0x%s failed\n",pr_addr(PC
));
3236 sim_io_printf(sd
,"DBG: fetched 0x%08X from PC = 0x%s\n",instruction
,pr_addr(PC
));
3239 /* This is required by exception processing, to ensure that we can
3240 cope with exceptions in the delay slots of branches that may
3241 already have changed the PC. */
3242 if ((vaddr
& 1) == 0)
3243 PC
+= 4; /* increment ready for the next fetch */
3246 /* NOTE: If we perform a delay slot change to the PC, this
3247 increment is not requuired. However, it would make the
3248 simulator more complicated to try and avoid this small hit. */
3250 /* Currently this code provides a simple model. For more
3251 complicated models we could perform exception status checks at
3252 this point, and set the simSTOP state as required. This could
3253 also include processing any hardware interrupts raised by any
3254 I/O model attached to the simulator context.
3256 Support for "asynchronous" I/O events within the simulated world
3257 could be providing by managing a counter, and calling a I/O
3258 specific handler when a particular threshold is reached. On most
3259 architectures a decrement and check for zero operation is
3260 usually quicker than an increment and compare. However, the
3261 process of managing a known value decrement to zero, is higher
3262 than the cost of using an explicit value UINT_MAX into the
3263 future. Which system is used will depend on how complicated the
3264 I/O model is, and how much it is likely to affect the simulator
3267 If events need to be scheduled further in the future than
3268 UINT_MAX event ticks, then the I/O model should just provide its
3269 own counter, triggered from the event system. */
3271 /* MIPS pipeline ticks. To allow for future support where the
3272 pipeline hit of individual instructions is known, this control
3273 loop manages a "pipeline_count" variable. It is initialised to
3274 1 (one), and will only be changed by the simulator engine when
3275 executing an instruction. If the engine does not have access to
3276 pipeline cycle count information then all instructions will be
3277 treated as using a single cycle. NOTE: A standard system is not
3278 provided by the default simulator because different MIPS
3279 architectures have different cycle counts for the same
3282 [NOTE: pipeline_count has been replaced the event queue] */
3284 /* shuffle the floating point status pipeline state */
3285 ENGINE_ISSUE_PREFIX_HOOK();
3287 /* NOTE: For multi-context simulation environments the "instruction"
3288 variable should be local to this routine. */
3290 /* Shorthand accesses for engine. Note: If we wanted to use global
3291 variables (and a single-threaded simulator engine), then we can
3292 create the actual variables with these names. */
3294 if (!(STATE
& simSKIPNEXT
)) {
3295 /* Include the simulator engine */
3296 #include "oengine.c"
3297 #if ((GPRLEN == 64) && !PROCESSOR_64BIT) || ((GPRLEN == 32) && PROCESSOR_64BIT)
3298 #error "Mismatch between run-time simulator code and simulation engine"
3300 #if (WITH_TARGET_WORD_BITSIZE != GPRLEN)
3301 #error "Mismatch between configure WITH_TARGET_WORD_BITSIZE and gencode GPRLEN"
3303 #if ((WITH_FLOATING_POINT == HARD_FLOATING_POINT) != defined (HASFPU))
3304 #error "Mismatch between configure WITH_FLOATING_POINT and gencode HASFPU"
3307 #if defined(WARN_LOHI)
3308 /* Decrement the HI/LO validity ticks */
3313 /* start-sanitize-r5900 */
3318 /* end-sanitize-r5900 */
3319 #endif /* WARN_LOHI */
3321 /* For certain MIPS architectures, GPR[0] is hardwired to zero. We
3322 should check for it being changed. It is better doing it here,
3323 than within the simulator, since it will help keep the simulator
3326 #if defined(WARN_ZERO)
3327 sim_io_eprintf(sd
,"The ZERO register has been updated with 0x%s (PC = 0x%s) (reset back to zero)\n",pr_addr(ZERO
),pr_addr(cia
));
3328 #endif /* WARN_ZERO */
3329 ZERO
= 0; /* reset back to zero before next instruction */
3331 } else /* simSKIPNEXT check */
3332 STATE
&= ~simSKIPNEXT
;
3334 /* If the delay slot was active before the instruction is
3335 executed, then update the PC to its new value: */
3338 printf("DBG: dsstate set before instruction execution - updating PC to 0x%s\n",pr_addr(DSPC
));
3347 #if !defined(FASTSIM)
3348 if (sim_events_tickn (sd
, pipeline_count
))
3350 /* cpu->cia = cia; */
3351 sim_events_process (sd
);
3354 if (sim_events_tick (sd
))
3356 /* cpu->cia = cia; */
3357 sim_events_process (sd
);
3359 #endif /* FASTSIM */
3365 /* This code copied from gdb's utils.c. Would like to share this code,
3366 but don't know of a common place where both could get to it. */
3368 /* Temporary storage using circular buffer */
3374 static char buf
[NUMCELLS
][CELLSIZE
];
3376 if (++cell
>=NUMCELLS
) cell
=0;
3380 /* Print routines to handle variable size regs, etc */
3382 /* Eliminate warning from compiler on 32-bit systems */
3383 static int thirty_two
= 32;
3389 char *paddr_str
=get_cell();
3390 switch (sizeof(addr
))
3393 sprintf(paddr_str
,"%08lx%08lx",
3394 (unsigned long)(addr
>>thirty_two
),(unsigned long)(addr
&0xffffffff));
3397 sprintf(paddr_str
,"%08lx",(unsigned long)addr
);
3400 sprintf(paddr_str
,"%04x",(unsigned short)(addr
&0xffff));
3403 sprintf(paddr_str
,"%x",addr
);
3412 char *paddr_str
=get_cell();
3413 sprintf(paddr_str
,"%08lx%08lx",
3414 (unsigned long)(addr
>>thirty_two
),(unsigned long)(addr
&0xffffffff));
3420 pending_tick (SIM_DESC sd
,
3425 sim_io_printf (sd
, "PENDING_DRAIN - pending_in = %d, pending_out = %d, pending_total = %d\n", PENDING_IN
, PENDING_OUT
, PENDING_TOTAL
);
3426 if (PENDING_OUT
!= PENDING_IN
)
3429 int index
= PENDING_OUT
;
3430 int total
= PENDING_TOTAL
;
3431 if (PENDING_TOTAL
== 0)
3432 sim_engine_abort (SD
, CPU
, cia
, "PENDING_DRAIN - Mis-match on pending update pointers\n");
3433 for (loop
= 0; (loop
< total
); loop
++)
3435 if (PENDING_SLOT_DEST
[index
] != NULL
)
3437 PENDING_SLOT_DELAY
[index
] -= 1;
3438 if (PENDING_SLOT_DELAY
[index
] == 0)
3440 if (PENDING_SLOT_BIT
[index
] >= 0)
3441 switch (PENDING_SLOT_SIZE
[index
])
3444 if (PENDING_SLOT_VALUE
[index
])
3445 *(unsigned32
*)PENDING_SLOT_DEST
[index
] |=
3446 BIT32 (PENDING_SLOT_BIT
[index
]);
3448 *(unsigned32
*)PENDING_SLOT_DEST
[index
] &=
3449 BIT32 (PENDING_SLOT_BIT
[index
]);
3452 if (PENDING_SLOT_VALUE
[index
])
3453 *(unsigned64
*)PENDING_SLOT_DEST
[index
] |=
3454 BIT64 (PENDING_SLOT_BIT
[index
]);
3456 *(unsigned64
*)PENDING_SLOT_DEST
[index
] &=
3457 BIT64 (PENDING_SLOT_BIT
[index
]);
3462 switch (PENDING_SLOT_SIZE
[index
])
3465 *(unsigned32
*)PENDING_SLOT_DEST
[index
] =
3466 PENDING_SLOT_VALUE
[index
];
3469 *(unsigned64
*)PENDING_SLOT_DEST
[index
] =
3470 PENDING_SLOT_VALUE
[index
];
3474 if (PENDING_OUT
== index
)
3476 PENDING_SLOT_DEST
[index
] = NULL
;
3477 PENDING_OUT
= (PENDING_OUT
+ 1) % PSLOTS
;
3482 index
= (index
+ 1) % PSLOTS
;
3486 /*---------------------------------------------------------------------------*/
3487 /*> EOF interp.c <*/