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"
42 /* start-sanitize-sky */
46 #include "sky-libvpe.h"
48 #include "sky-gpuif.h"
53 /* end-sanitize-sky */
75 #include "libiberty.h"
77 #include "callback.h" /* GDB simulator callback interface */
78 #include "remote-sim.h" /* GDB simulator interface */
86 char* pr_addr
PARAMS ((SIM_ADDR addr
));
87 char* pr_uword64
PARAMS ((uword64 addr
));
90 /* Get the simulator engine description, without including the code: */
97 /* Within interp.c we refer to the sim_state and sim_cpu directly. */
102 /* The following reserved instruction value is used when a simulator
103 trap is required. NOTE: Care must be taken, since this value may be
104 used in later revisions of the MIPS ISA. */
106 #define RSVD_INSTRUCTION (0x00000005)
107 #define RSVD_INSTRUCTION_MASK (0xFC00003F)
109 #define RSVD_INSTRUCTION_ARG_SHIFT 6
110 #define RSVD_INSTRUCTION_ARG_MASK 0xFFFFF
113 /* Bits in the Debug register */
114 #define Debug_DBD 0x80000000 /* Debug Branch Delay */
115 #define Debug_DM 0x40000000 /* Debug Mode */
116 #define Debug_DBp 0x00000002 /* Debug Breakpoint indicator */
118 /*---------------------------------------------------------------------------*/
119 /*-- GDB simulator interface ------------------------------------------------*/
120 /*---------------------------------------------------------------------------*/
122 static void ColdReset
PARAMS((SIM_DESC sd
));
124 /*---------------------------------------------------------------------------*/
128 #define DELAYSLOT() {\
129 if (STATE & simDELAYSLOT)\
130 sim_io_eprintf(sd,"Delay slot already activated (branch in delay slot?)\n");\
131 STATE |= simDELAYSLOT;\
134 #define JALDELAYSLOT() {\
136 STATE |= simJALDELAYSLOT;\
140 STATE &= ~simDELAYSLOT;\
141 STATE |= simSKIPNEXT;\
144 #define CANCELDELAYSLOT() {\
146 STATE &= ~(simDELAYSLOT | simJALDELAYSLOT);\
149 #define INDELAYSLOT() ((STATE & simDELAYSLOT) != 0)
150 #define INJALDELAYSLOT() ((STATE & simJALDELAYSLOT) != 0)
152 #define K0BASE (0x80000000)
153 #define K0SIZE (0x20000000)
154 #define K1BASE (0xA0000000)
155 #define K1SIZE (0x20000000)
156 #define MONITOR_BASE (0xBFC00000)
157 #define MONITOR_SIZE (1 << 11)
158 #define MEM_SIZE (2 << 20)
160 /* start-sanitize-sky */
163 #define MEM_SIZE (16 << 20) /* 16 MB */
165 /* end-sanitize-sky */
168 static char *tracefile
= "trace.din"; /* default filename for trace log */
169 FILE *tracefh
= NULL
;
170 static void open_trace
PARAMS((SIM_DESC sd
));
173 /* simulation target board. NULL=canonical */
174 static char* board
= NULL
;
177 static DECLARE_OPTION_HANDLER (mips_option_handler
);
180 OPTION_DINERO_TRACE
= OPTION_START
,
182 /* start-sanitize-sky */
191 /* end-sanitize-sky */
197 mips_option_handler (sd
, cpu
, opt
, arg
, is_command
)
207 case OPTION_DINERO_TRACE
: /* ??? */
209 /* Eventually the simTRACE flag could be treated as a toggle, to
210 allow external control of the program points being traced
211 (i.e. only from main onwards, excluding the run-time setup,
213 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
215 sim_cpu
*cpu
= STATE_CPU (sd
, cpu_nr
);
218 else if (strcmp (arg
, "yes") == 0)
220 else if (strcmp (arg
, "no") == 0)
222 else if (strcmp (arg
, "on") == 0)
224 else if (strcmp (arg
, "off") == 0)
228 fprintf (stderr
, "Unrecognized dinero-trace option `%s'\n", arg
);
235 Simulator constructed without dinero tracing support (for performance).\n\
236 Re-compile simulator with \"-DTRACE\" to enable this option.\n");
240 case OPTION_DINERO_FILE
:
242 if (optarg
!= NULL
) {
244 tmp
= (char *)malloc(strlen(optarg
) + 1);
247 sim_io_printf(sd
,"Failed to allocate buffer for tracefile name \"%s\"\n",optarg
);
253 sim_io_printf(sd
,"Placing trace information into file \"%s\"\n",tracefile
);
259 /* start-sanitize-sky */
262 case OPTION_FLOAT_TYPE
:
263 /* Use host (fast) or target (accurate) floating point implementation. */
264 if (arg
&& strcmp (arg
, "fast") == 0)
265 STATE_FP_TYPE_OPT (sd
) &= ~STATE_FP_TYPE_OPT_ACCURATE
;
266 else if (arg
&& strcmp (arg
, "accurate") == 0)
267 STATE_FP_TYPE_OPT (sd
) |= STATE_FP_TYPE_OPT_ACCURATE
;
270 fprintf (stderr
, "Unrecognized float-type option `%s'\n", arg
);
273 /*printf ("float-type=0x%08x\n", STATE_FP_TYPE_OPT (sd));*/
277 case OPTION_GS_ENABLE
:
278 /* Enable GS libraries. */
279 if ( arg
&& strcmp (arg
, "on") == 0 )
280 gif_options (&GIF_full
,GIF_OPT_GS_ENABLE
,1,0,0);
281 else if ( arg
&& strcmp (arg
, "off") == 0 )
282 gif_options (&GIF_full
,GIF_OPT_GS_ENABLE
,0,0,0);
285 fprintf (stderr
, "Unrecognized enable-gs option `%s'\n", arg
);
290 case OPTION_GS_REFRESH1
:
291 case OPTION_GS_REFRESH2
:
293 /* The GS has defineable register and register values. */
294 unsigned_4 address
[2];
298 if ( arg
&& strlen (arg
) == 59 && arg
[10] == '=' &&
299 arg
[29] == ':' && arg
[40] == '=' &&
300 ( sscanf (arg
,"%lx%c%Lx%c%lx%c%Lx", &address
[0],&c
[0],&value
[0],
301 &c
[1],&address
[1],&c
[2],&value
[1]) == 7 ))
303 gif_options (&GIF_full
, ( opt
== OPTION_GS_REFRESH1
) ?
304 GIF_OPT_GS_REFRESH1
:GIF_OPT_GS_REFRESH2
,
305 0,&address
[0],&value
[0]);
309 fprintf (stderr
, "Unrecognized gs-refresh option `%s'\n", arg
);
316 /* end-sanitize-sky */
322 board
= zalloc(strlen(arg
) + 1);
333 static const OPTION mips_options
[] =
335 { {"dinero-trace", optional_argument
, NULL
, OPTION_DINERO_TRACE
},
336 '\0', "on|off", "Enable dinero tracing",
337 mips_option_handler
},
338 { {"dinero-file", required_argument
, NULL
, OPTION_DINERO_FILE
},
339 '\0', "FILE", "Write dinero trace to FILE",
340 mips_option_handler
},
341 /* start-sanitize-sky */
344 { {"float-type", required_argument
, NULL
, OPTION_FLOAT_TYPE
},
345 '\0', "fast|accurate", "Use fast (host) or accurate (target) floating point",
346 mips_option_handler
},
348 { {"enable-gs", required_argument
, NULL
, OPTION_GS_ENABLE
},
349 '\0', "on|off", "Enable GS library routines",
350 mips_option_handler
},
351 { {"gs-refresh1", required_argument
, NULL
, OPTION_GS_REFRESH1
},
352 '\0', "0xaddress0=0xvalue0:0xaddress1=0xvalue1", "GS refresh buffer 1 addresses and values",
353 mips_option_handler
},
354 { {"gs-refresh2", required_argument
, NULL
, OPTION_GS_REFRESH2
},
355 '\0', "0xaddress0=0xvalue0:0xaddress1=0xvalue1", "GS refresh buffer 2 addresses and values",
356 mips_option_handler
},
358 /* end-sanitize-sky */
360 { {"board", required_argument
, NULL
, OPTION_BOARD
},
361 '\0', "none" /* rely on compile-time string concatenation for other options */
363 /* start-sanitize-tx3904 */
364 #define BOARD_JMR3904 "jmr3904"
366 #define BOARD_JMR3904_PAL "jmr3904pal"
367 "|" BOARD_JMR3904_PAL
368 #define BOARD_JMR3904_DEBUG "jmr3904debug"
369 "|" BOARD_JMR3904_DEBUG
370 /* end-sanitize-tx3904 */
372 , "Customize simulation for a particular board.", mips_option_handler
},
374 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
378 int interrupt_pending
;
381 interrupt_event (SIM_DESC sd
, void *data
)
383 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
384 address_word cia
= CIA_GET (cpu
);
387 interrupt_pending
= 0;
388 SignalExceptionInterrupt ();
390 else if (!interrupt_pending
)
391 sim_events_schedule (sd
, 1, interrupt_event
, data
);
395 /*---------------------------------------------------------------------------*/
396 /*-- Device registration hook -----------------------------------------------*/
397 /*---------------------------------------------------------------------------*/
398 static void device_init(SIM_DESC sd
) {
400 extern void register_devices(SIM_DESC
);
401 register_devices(sd
);
405 /*---------------------------------------------------------------------------*/
406 /*-- GDB simulator interface ------------------------------------------------*/
407 /*---------------------------------------------------------------------------*/
410 sim_open (kind
, cb
, abfd
, argv
)
416 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
417 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
419 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
420 /* start-sanitize-sky */
422 #if defined(TARGET_SKY) && defined(SKY_FUNIT)
423 /* Set "--float-type fast" as the default. */
424 STATE_FP_TYPE_OPT (sd
) &= ~STATE_FP_TYPE_OPT_ACCURATE
;
426 /* end-sanitize-sky */
428 /* FIXME: watchpoints code shouldn't need this */
429 STATE_WATCHPOINTS (sd
)->pc
= &(PC
);
430 STATE_WATCHPOINTS (sd
)->sizeof_pc
= sizeof (PC
);
431 STATE_WATCHPOINTS (sd
)->interrupt_handler
= interrupt_event
;
435 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
437 sim_add_option_table (sd
, NULL
, mips_options
);
439 /* getopt will print the error message so we just have to exit if this fails.
440 FIXME: Hmmm... in the case of gdb we need getopt to call
442 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
444 /* Uninstall the modules to avoid memory leaks,
445 file descriptor leaks, etc. */
446 sim_module_uninstall (sd
);
450 /* handle board-specific memory maps */
453 /* Allocate core managed memory */
455 /* start-sanitize-sky */
457 /* end-sanitize-sky */
459 sim_do_commandf (sd
, "memory region 0x%lx,0x%lx", MONITOR_BASE
, MONITOR_SIZE
);
460 /* For compatibility with the old code - under this (at level one)
461 are the kernel spaces K0 & K1. Both of these map to a single
462 smaller sub region */
463 sim_do_command(sd
," memory region 0x7fff8000,0x8000") ; /* MTZ- 32 k stack */
464 sim_do_commandf (sd
, "memory alias 0x%lx@1,0x%lx%%0x%lx,0x%0x",
466 MEM_SIZE
, /* actual size */
468 /* start-sanitize-sky */
471 sim_do_commandf (sd
, "memory region 0x%lx,0x%lx", MONITOR_BASE
- K1BASE
, MONITOR_SIZE
);
472 sim_do_command (sd
," memory region 0x7fff8000,0x8000") ; /* MTZ- 32 k stack */
473 /* 16M @ 0x0. Aliases at 0x80000000 and 0xA0000000 are handled by
474 address_translation() */
475 sim_do_commandf (sd
, "memory size 0x%lx", MEM_SIZE
);
477 /* end-sanitize-sky */
482 /* start-sanitize-tx3904 */
485 && (strcmp(board
, BOARD_JMR3904
) == 0 ||
486 strcmp(board
, BOARD_JMR3904_PAL
) == 0 ||
487 strcmp(board
, BOARD_JMR3904_DEBUG
) == 0))
489 /* match VIRTUAL memory layout of JMR-TX3904 board */
493 /* ROM: 0x9FC0_0000 - 0x9FFF_FFFF and 0xBFC0_0000 - 0xBFFF_FFFF */
494 sim_do_commandf (sd
, "memory alias 0x%lx@1,0x%lx,0x%0x",
496 4 * 1024 * 1024, /* 4 MB */
499 /* SRAM: 0x8000_0000 - 0x803F_FFFF and 0xA000_0000 - 0xA03F_FFFF */
500 sim_do_commandf (sd
, "memory alias 0x%lx@1,0x%lx,0x%0x",
502 4 * 1024 * 1024, /* 4 MB */
505 /* DRAM: 0x8800_0000 - 0x89FF_FFFF and 0xA800_0000 - 0xA9FF_FFFF */
506 sim_do_commandf (sd
, "memory alias 0x%lx@1,0x%lx,0x%0x",
508 32 * 1024 * 1024, /* 32 MB */
511 /* --- simulated devices --- */
512 sim_hw_parse (sd
, "/tx3904irc@0xffffc000/reg 0xffffc000 0x20");
513 sim_hw_parse (sd
, "/tx3904cpu");
514 sim_hw_parse (sd
, "/tx3904tmr@0xfffff000/reg 0xfffff000 0x100");
515 sim_hw_parse (sd
, "/tx3904tmr@0xfffff100/reg 0xfffff100 0x100");
516 sim_hw_parse (sd
, "/tx3904tmr@0xfffff200/reg 0xfffff200 0x100");
518 /* -- device connections --- */
519 sim_hw_parse (sd
, "/tx3904irc > ip level /tx3904cpu");
520 sim_hw_parse (sd
, "/tx3904tmr@0xfffff000 > int tmr0 /tx3904irc");
521 sim_hw_parse (sd
, "/tx3904tmr@0xfffff100 > int tmr1 /tx3904irc");
522 sim_hw_parse (sd
, "/tx3904tmr@0xfffff200 > int tmr2 /tx3904irc");
524 /* add PAL timer & I/O module */
525 if(! strcmp(board
, BOARD_JMR3904_PAL
))
528 sim_hw_parse (sd
, "/pal@0xffff0000");
529 sim_hw_parse (sd
, "/pal@0xffff0000/reg 0xffff0000 64");
531 /* wire up interrupt ports to irc */
532 sim_hw_parse (sd
, "/pal@0x31000000 > countdown tmr0 /tx3904irc");
533 sim_hw_parse (sd
, "/pal@0x31000000 > timer tmr1 /tx3904irc");
534 sim_hw_parse (sd
, "/pal@0x31000000 > int int0 /tx3904irc");
537 if(! strcmp(board
, BOARD_JMR3904_DEBUG
))
539 /* -- DEBUG: glue interrupt generators --- */
540 sim_hw_parse (sd
, "/glue@0xffff0000/reg 0xffff0000 0x50");
541 sim_hw_parse (sd
, "/glue@0xffff0000 > int0 int0 /tx3904irc");
542 sim_hw_parse (sd
, "/glue@0xffff0000 > int1 int1 /tx3904irc");
543 sim_hw_parse (sd
, "/glue@0xffff0000 > int2 int2 /tx3904irc");
544 sim_hw_parse (sd
, "/glue@0xffff0000 > int3 int3 /tx3904irc");
545 sim_hw_parse (sd
, "/glue@0xffff0000 > int4 int4 /tx3904irc");
546 sim_hw_parse (sd
, "/glue@0xffff0000 > int5 int5 /tx3904irc");
547 sim_hw_parse (sd
, "/glue@0xffff0000 > int6 int6 /tx3904irc");
548 sim_hw_parse (sd
, "/glue@0xffff0000 > int7 int7 /tx3904irc");
549 sim_hw_parse (sd
, "/glue@0xffff0000 > int8 dmac0 /tx3904irc");
550 sim_hw_parse (sd
, "/glue@0xffff0000 > int9 dmac1 /tx3904irc");
551 sim_hw_parse (sd
, "/glue@0xffff0000 > int10 dmac2 /tx3904irc");
552 sim_hw_parse (sd
, "/glue@0xffff0000 > int11 dmac3 /tx3904irc");
553 sim_hw_parse (sd
, "/glue@0xffff0000 > int12 sio0 /tx3904irc");
554 sim_hw_parse (sd
, "/glue@0xffff0000 > int13 sio1 /tx3904irc");
555 sim_hw_parse (sd
, "/glue@0xffff0000 > int14 tmr0 /tx3904irc");
556 sim_hw_parse (sd
, "/glue@0xffff0000 > int15 tmr1 /tx3904irc");
557 sim_hw_parse (sd
, "/glue@0xffff0000 > int16 tmr2 /tx3904irc");
558 sim_hw_parse (sd
, "/glue@0xffff0000 > int17 nmi /tx3904cpu");
564 /* end-sanitize-tx3904 */
567 /* check for/establish the a reference program image */
568 if (sim_analyze_program (sd
,
569 (STATE_PROG_ARGV (sd
) != NULL
570 ? *STATE_PROG_ARGV (sd
)
574 sim_module_uninstall (sd
);
578 /* Configure/verify the target byte order and other runtime
579 configuration options */
580 if (sim_config (sd
) != SIM_RC_OK
)
582 sim_module_uninstall (sd
);
586 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
588 /* Uninstall the modules to avoid memory leaks,
589 file descriptor leaks, etc. */
590 sim_module_uninstall (sd
);
594 /* verify assumptions the simulator made about the host type system.
595 This macro does not return if there is a problem */
596 SIM_ASSERT (sizeof(int) == (4 * sizeof(char)));
597 SIM_ASSERT (sizeof(word64
) == (8 * sizeof(char)));
599 /* This is NASTY, in that we are assuming the size of specific
603 for (rn
= 0; (rn
< (LAST_EMBED_REGNUM
+ 1)); rn
++)
606 cpu
->register_widths
[rn
] = WITH_TARGET_WORD_BITSIZE
;
607 else if ((rn
>= FGRIDX
) && (rn
< (FGRIDX
+ NR_FGR
)))
608 cpu
->register_widths
[rn
] = WITH_TARGET_FLOATING_POINT_BITSIZE
;
609 else if ((rn
>= 33) && (rn
<= 37))
610 cpu
->register_widths
[rn
] = WITH_TARGET_WORD_BITSIZE
;
611 else if ((rn
== SRIDX
)
614 || ((rn
>= 72) && (rn
<= 89)))
615 cpu
->register_widths
[rn
] = 32;
617 cpu
->register_widths
[rn
] = 0;
619 /* start-sanitize-r5900 */
621 /* set the 5900 "upper" registers to 64 bits */
622 for( rn
= LAST_EMBED_REGNUM
+1; rn
< NUM_REGS
; rn
++)
623 cpu
->register_widths
[rn
] = 64;
624 /* end-sanitize-r5900 */
626 /* start-sanitize-sky */
628 /* Now the VU registers */
629 for( rn
= 0; rn
< NUM_VU_INTEGER_REGS
; rn
++ ) {
630 cpu
->register_widths
[rn
+ NUM_R5900_REGS
] = 16;
631 cpu
->register_widths
[rn
+ NUM_R5900_REGS
+ NUM_VU_REGS
] = 16;
634 for( rn
= NUM_VU_INTEGER_REGS
; rn
< NUM_VU_REGS
; rn
++ ) {
635 cpu
->register_widths
[rn
+ NUM_R5900_REGS
] = 32;
636 cpu
->register_widths
[rn
+ NUM_R5900_REGS
+ NUM_VU_REGS
] = 32;
639 /* Finally the VIF registers */
640 for( rn
= 2*NUM_VU_REGS
; rn
< 2*NUM_VU_REGS
+ 2*NUM_VIF_REGS
; rn
++ )
641 cpu
->register_widths
[rn
+ NUM_R5900_REGS
] = 32;
645 /* end-sanitize-sky */
649 if (STATE
& simTRACE
)
653 /* Write an abort sequence into the TRAP (common) exception vector
654 addresses. This is to catch code executing a TRAP (et.al.)
655 instruction without installing a trap handler. */
657 unsigned32 halt
[2] = { 0x2404002f /* addiu r4, r0, 47 */,
658 HALT_INSTRUCTION
/* BREAK */ };
661 sim_write (sd
, 0x80000180, (char *) halt
, sizeof (halt
));
662 sim_write (sd
, 0xBFC00380, (char *) halt
, sizeof (halt
));
666 /* Write the monitor trap address handlers into the monitor (eeprom)
667 address space. This can only be done once the target endianness
668 has been determined. */
671 /* Entry into the IDT monitor is via fixed address vectors, and
672 not using machine instructions. To avoid clashing with use of
673 the MIPS TRAP system, we place our own (simulator specific)
674 "undefined" instructions into the relevant vector slots. */
675 for (loop
= 0; (loop
< MONITOR_SIZE
); loop
+= 4)
677 address_word vaddr
= (MONITOR_BASE
+ loop
);
678 unsigned32 insn
= (RSVD_INSTRUCTION
| (((loop
>> 2) & RSVD_INSTRUCTION_ARG_MASK
) << RSVD_INSTRUCTION_ARG_SHIFT
));
680 sim_write (sd
, vaddr
, (char *)&insn
, sizeof (insn
));
682 /* The PMON monitor uses the same address space, but rather than
683 branching into it the address of a routine is loaded. We can
684 cheat for the moment, and direct the PMON routine to IDT style
685 instructions within the monitor space. This relies on the IDT
686 monitor not using the locations from 0xBFC00500 onwards as its
688 for (loop
= 0; (loop
< 24); loop
++)
690 address_word vaddr
= (MONITOR_BASE
+ 0x500 + (loop
* 4));
691 unsigned32 value
= ((0x500 - 8) / 8); /* default UNDEFINED reason code */
707 value
= ((0x500 - 16) / 8); /* not an IDT reason code */
709 case 8: /* cliexit */
712 case 11: /* flush_cache */
716 /* FIXME - should monitor_base be SIM_ADDR?? */
717 value
= ((unsigned int)MONITOR_BASE
+ (value
* 8));
719 sim_write (sd
, vaddr
, (char *)&value
, sizeof (value
));
721 /* The LSI MiniRISC PMON has its vectors at 0x200, not 0x500. */
723 sim_write (sd
, vaddr
, (char *)&value
, sizeof (value
));
735 tracefh
= fopen(tracefile
,"wb+");
738 sim_io_eprintf(sd
,"Failed to create file \"%s\", writing trace information to stderr.\n",tracefile
);
745 sim_close (sd
, quitting
)
750 printf("DBG: sim_close: entered (quitting = %d)\n",quitting
);
753 /* "quitting" is non-zero if we cannot hang on errors */
755 /* Ensure that any resources allocated through the callback
756 mechanism are released: */
757 sim_io_shutdown (sd
);
760 if (tracefh
!= NULL
&& tracefh
!= stderr
)
765 /* FIXME - free SD */
772 sim_write (sd
,addr
,buffer
,size
)
775 unsigned char *buffer
;
779 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
781 /* Return the number of bytes written, or zero if error. */
783 sim_io_printf(sd
,"sim_write(0x%s,buffer,%d);\n",pr_addr(addr
),size
);
786 /* We use raw read and write routines, since we do not want to count
787 the GDB memory accesses in our statistics gathering. */
789 for (index
= 0; index
< size
; index
++)
791 address_word vaddr
= (address_word
)addr
+ index
;
794 if (!address_translation (SD
, CPU
, NULL_CIA
, vaddr
, isDATA
, isSTORE
, &paddr
, &cca
, isRAW
))
796 if (sim_core_write_buffer (SD
, CPU
, read_map
, buffer
+ index
, paddr
, 1) != 1)
804 sim_read (sd
,addr
,buffer
,size
)
807 unsigned char *buffer
;
811 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
813 /* Return the number of bytes read, or zero if error. */
815 sim_io_printf(sd
,"sim_read(0x%s,buffer,%d);\n",pr_addr(addr
),size
);
818 for (index
= 0; (index
< size
); index
++)
820 address_word vaddr
= (address_word
)addr
+ index
;
823 if (!address_translation (SD
, CPU
, NULL_CIA
, vaddr
, isDATA
, isLOAD
, &paddr
, &cca
, isRAW
))
825 if (sim_core_read_buffer (SD
, CPU
, read_map
, buffer
+ index
, paddr
, 1) != 1)
833 sim_store_register (sd
,rn
,memory
,length
)
836 unsigned char *memory
;
839 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
840 /* NOTE: gdb (the client) stores registers in target byte order
841 while the simulator uses host byte order */
843 sim_io_printf(sd
,"sim_store_register(%d,*memory=0x%s);\n",rn
,pr_addr(*((SIM_ADDR
*)memory
)));
846 /* Unfortunately this suffers from the same problem as the register
847 numbering one. We need to know what the width of each logical
848 register number is for the architecture being simulated. */
850 if (cpu
->register_widths
[rn
] == 0)
852 sim_io_eprintf(sd
,"Invalid register width for %d (register store ignored)\n",rn
);
856 /* start-sanitize-r5900 */
857 if (rn
>= 90 && rn
< 90 + 32)
859 GPR1
[rn
- 90] = T2H_8 (*(unsigned64
*)memory
);
865 SA
= T2H_8(*(unsigned64
*)memory
);
867 case 122: /* FIXME */
868 LO1
= T2H_8(*(unsigned64
*)memory
);
870 case 123: /* FIXME */
871 HI1
= T2H_8(*(unsigned64
*)memory
);
874 /* end-sanitize-r5900 */
876 /* start-sanitize-sky */
878 if (rn
>= NUM_R5900_REGS
)
880 rn
= rn
- NUM_R5900_REGS
;
882 if( rn
< NUM_VU_REGS
)
884 if (rn
< NUM_VU_INTEGER_REGS
)
885 return write_vu_int_reg (&(vu0_device
.regs
), rn
, memory
);
886 else if (rn
>= FIRST_VEC_REG
)
889 return write_vu_vec_reg (&(vu0_device
.regs
), rn
>>2, rn
&3,
892 else switch (rn
- NUM_VU_INTEGER_REGS
)
895 return write_vu_special_reg (&vu0_device
, VU_REG_CIA
,
898 return write_vu_misc_reg (&(vu0_device
.regs
), VU_REG_MR
,
900 case 2: /* VU0 has no P register */
903 return write_vu_misc_reg (&(vu0_device
.regs
), VU_REG_MI
,
906 return write_vu_misc_reg (&(vu0_device
.regs
), VU_REG_MQ
,
909 return write_vu_acc_reg (&(vu0_device
.regs
),
910 rn
- (NUM_VU_INTEGER_REGS
+ 5),
915 rn
= rn
- NUM_VU_REGS
;
917 if (rn
< NUM_VU_REGS
)
919 if (rn
< NUM_VU_INTEGER_REGS
)
920 return write_vu_int_reg (&(vu1_device
.regs
), rn
, memory
);
921 else if (rn
>= FIRST_VEC_REG
)
924 return write_vu_vec_reg (&(vu1_device
.regs
),
925 rn
>> 2, rn
& 3, memory
);
927 else switch (rn
- NUM_VU_INTEGER_REGS
)
930 return write_vu_special_reg (&vu1_device
, VU_REG_CIA
,
933 return write_vu_misc_reg (&(vu1_device
.regs
), VU_REG_MR
,
936 return write_vu_misc_reg (&(vu1_device
.regs
), VU_REG_MP
,
939 return write_vu_misc_reg (&(vu1_device
.regs
), VU_REG_MI
,
942 return write_vu_misc_reg (&(vu1_device
.regs
), VU_REG_MQ
,
945 return write_vu_acc_reg (&(vu1_device
.regs
),
946 rn
- (NUM_VU_INTEGER_REGS
+ 5),
951 rn
-= NUM_VU_REGS
; /* VIF0 registers are next */
953 if (rn
< NUM_VIF_REGS
)
955 if (rn
< NUM_VIF_REGS
-1)
956 return write_pke_reg (&pke0_device
, rn
, memory
);
959 sim_io_eprintf( sd
, "Can't write vif0_pc (store ignored)\n" );
964 rn
-= NUM_VIF_REGS
; /* VIF1 registers are last */
966 if (rn
< NUM_VIF_REGS
)
968 if (rn
< NUM_VIF_REGS
-1)
969 return write_pke_reg (&pke1_device
, rn
, memory
);
972 sim_io_eprintf( sd
, "Can't write vif1_pc (store ignored)\n" );
977 sim_io_eprintf( sd
, "Invalid VU register (register store ignored)\n" );
981 /* end-sanitize-sky */
983 if (rn
>= FGRIDX
&& rn
< FGRIDX
+ NR_FGR
)
985 if (cpu
->register_widths
[rn
] == 32)
987 cpu
->fgr
[rn
- FGRIDX
] = T2H_4 (*(unsigned32
*)memory
);
992 cpu
->fgr
[rn
- FGRIDX
] = T2H_8 (*(unsigned64
*)memory
);
997 if (cpu
->register_widths
[rn
] == 32)
999 cpu
->registers
[rn
] = T2H_4 (*(unsigned32
*)memory
);
1004 cpu
->registers
[rn
] = T2H_8 (*(unsigned64
*)memory
);
1012 sim_fetch_register (sd
,rn
,memory
,length
)
1015 unsigned char *memory
;
1018 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* FIXME */
1019 /* NOTE: gdb (the client) stores registers in target byte order
1020 while the simulator uses host byte order */
1022 sim_io_printf(sd
,"sim_fetch_register(%d=0x%s,mem) : place simulator registers into memory\n",rn
,pr_addr(registers
[rn
]));
1025 if (cpu
->register_widths
[rn
] == 0)
1027 sim_io_eprintf (sd
, "Invalid register width for %d (register fetch ignored)\n",rn
);
1031 /* start-sanitize-r5900 */
1032 if (rn
>= 90 && rn
< 90 + 32)
1034 *((unsigned64
*)memory
) = H2T_8 (GPR1
[rn
- 90]);
1040 *((unsigned64
*)memory
) = H2T_8(SA
);
1042 case 122: /* FIXME */
1043 *((unsigned64
*)memory
) = H2T_8(LO1
);
1045 case 123: /* FIXME */
1046 *((unsigned64
*)memory
) = H2T_8(HI1
);
1049 /* end-sanitize-r5900 */
1051 /* start-sanitize-sky */
1053 if (rn
>= NUM_R5900_REGS
)
1055 rn
= rn
- NUM_R5900_REGS
;
1057 if (rn
< NUM_VU_REGS
)
1059 if (rn
< NUM_VU_INTEGER_REGS
)
1060 return read_vu_int_reg (&(vu0_device
.regs
), rn
, memory
);
1061 else if (rn
>= FIRST_VEC_REG
)
1063 rn
-= FIRST_VEC_REG
;
1064 return read_vu_vec_reg (&(vu0_device
.regs
), rn
>>2, rn
& 3,
1067 else switch (rn
- NUM_VU_INTEGER_REGS
)
1070 return read_vu_special_reg(&vu0_device
, VU_REG_CIA
, memory
);
1072 return read_vu_misc_reg (&(vu0_device
.regs
), VU_REG_MR
,
1074 case 2: /* VU0 has no P register */
1075 *((int *) memory
) = 0;
1078 return read_vu_misc_reg (&(vu0_device
.regs
), VU_REG_MI
,
1081 return read_vu_misc_reg (&(vu0_device
.regs
), VU_REG_MQ
,
1084 return read_vu_acc_reg (&(vu0_device
.regs
),
1085 rn
- (NUM_VU_INTEGER_REGS
+ 5),
1090 rn
-= NUM_VU_REGS
; /* VU1 registers are next */
1092 if (rn
< NUM_VU_REGS
)
1094 if (rn
< NUM_VU_INTEGER_REGS
)
1095 return read_vu_int_reg (&(vu1_device
.regs
), rn
, memory
);
1096 else if (rn
>= FIRST_VEC_REG
)
1098 rn
-= FIRST_VEC_REG
;
1099 return read_vu_vec_reg (&(vu1_device
.regs
),
1100 rn
>> 2, rn
& 3, memory
);
1102 else switch (rn
- NUM_VU_INTEGER_REGS
)
1105 return read_vu_special_reg(&vu1_device
, VU_REG_CIA
, memory
);
1107 return read_vu_misc_reg (&(vu1_device
.regs
),
1110 return read_vu_misc_reg (&(vu1_device
.regs
),
1113 return read_vu_misc_reg (&(vu1_device
.regs
),
1116 return read_vu_misc_reg (&(vu1_device
.regs
),
1119 return read_vu_acc_reg (&(vu1_device
.regs
),
1120 rn
- (NUM_VU_INTEGER_REGS
+ 5),
1125 rn
-= NUM_VU_REGS
; /* VIF0 registers are next */
1127 if (rn
< NUM_VIF_REGS
)
1129 if (rn
< NUM_VIF_REGS
-1)
1130 return read_pke_reg (&pke0_device
, rn
, memory
);
1132 return read_pke_pc (&pke0_device
, memory
);
1135 rn
-= NUM_VIF_REGS
; /* VIF1 registers are last */
1137 if (rn
< NUM_VIF_REGS
)
1139 if (rn
< NUM_VIF_REGS
-1)
1140 return read_pke_reg (&pke1_device
, rn
, memory
);
1142 return read_pke_pc (&pke1_device
, memory
);
1145 sim_io_eprintf( sd
, "Invalid VU register (register fetch ignored)\n" );
1148 /* end-sanitize-sky */
1150 /* Any floating point register */
1151 if (rn
>= FGRIDX
&& rn
< FGRIDX
+ NR_FGR
)
1153 if (cpu
->register_widths
[rn
] == 32)
1155 *(unsigned32
*)memory
= H2T_4 (cpu
->fgr
[rn
- FGRIDX
]);
1160 *(unsigned64
*)memory
= H2T_8 (cpu
->fgr
[rn
- FGRIDX
]);
1165 if (cpu
->register_widths
[rn
] == 32)
1167 *(unsigned32
*)memory
= H2T_4 ((unsigned32
)(cpu
->registers
[rn
]));
1172 *(unsigned64
*)memory
= H2T_8 ((unsigned64
)(cpu
->registers
[rn
]));
1181 sim_create_inferior (sd
, abfd
, argv
,env
)
1189 printf("DBG: sim_create_inferior entered: start_address = 0x%s\n",
1197 /* override PC value set by ColdReset () */
1199 for (cpu_nr
= 0; cpu_nr
< sim_engine_nr_cpus (sd
); cpu_nr
++)
1201 sim_cpu
*cpu
= STATE_CPU (sd
, cpu_nr
);
1202 CIA_SET (cpu
, (unsigned64
) bfd_get_start_address (abfd
));
1206 #if 0 /* def DEBUG */
1209 /* We should really place the argv slot values into the argument
1210 registers, and onto the stack as required. However, this
1211 assumes that we have a stack defined, which is not
1212 necessarily true at the moment. */
1214 sim_io_printf(sd
,"sim_create_inferior() : passed arguments ignored\n");
1215 for (cptr
= argv
; (cptr
&& *cptr
); cptr
++)
1216 printf("DBG: arg \"%s\"\n",*cptr
);
1224 sim_do_command (sd
,cmd
)
1228 if (sim_args_command (sd
, cmd
) != SIM_RC_OK
)
1229 sim_io_printf (sd
, "Error: \"%s\" is not a valid MIPS simulator command.\n",
1233 /*---------------------------------------------------------------------------*/
1234 /*-- Private simulator support interface ------------------------------------*/
1235 /*---------------------------------------------------------------------------*/
1237 /* Read a null terminated string from memory, return in a buffer */
1239 fetch_str (sd
, addr
)
1246 while (sim_read (sd
, addr
+ nr
, &null
, 1) == 1 && null
!= 0)
1248 buf
= NZALLOC (char, nr
+ 1);
1249 sim_read (sd
, addr
, buf
, nr
);
1253 /* Simple monitor interface (currently setup for the IDT and PMON monitors) */
1255 sim_monitor (SIM_DESC sd
,
1258 unsigned int reason
)
1261 printf("DBG: sim_monitor: entered (reason = %d)\n",reason
);
1264 /* The IDT monitor actually allows two instructions per vector
1265 slot. However, the simulator currently causes a trap on each
1266 individual instruction. We cheat, and lose the bottom bit. */
1269 /* The following callback functions are available, however the
1270 monitor we are simulating does not make use of them: get_errno,
1271 isatty, lseek, rename, system, time and unlink */
1275 case 6: /* int open(char *path,int flags) */
1277 char *path
= fetch_str (sd
, A0
);
1278 V0
= sim_io_open (sd
, path
, (int)A1
);
1283 case 7: /* int read(int file,char *ptr,int len) */
1287 char *buf
= zalloc (nr
);
1288 V0
= sim_io_read (sd
, fd
, buf
, nr
);
1289 sim_write (sd
, A1
, buf
, nr
);
1294 case 8: /* int write(int file,char *ptr,int len) */
1298 char *buf
= zalloc (nr
);
1299 sim_read (sd
, A1
, buf
, nr
);
1300 V0
= sim_io_write (sd
, fd
, buf
, nr
);
1305 case 10: /* int close(int file) */
1307 V0
= sim_io_close (sd
, (int)A0
);
1311 case 2: /* Densan monitor: char inbyte(int waitflag) */
1313 if (A0
== 0) /* waitflag == NOWAIT */
1314 V0
= (unsigned_word
)-1;
1316 /* Drop through to case 11 */
1318 case 11: /* char inbyte(void) */
1321 if (sim_io_read_stdin (sd
, &tmp
, sizeof(char)) != sizeof(char))
1323 sim_io_error(sd
,"Invalid return from character read");
1324 V0
= (unsigned_word
)-1;
1327 V0
= (unsigned_word
)tmp
;
1331 case 3: /* Densan monitor: void co(char chr) */
1332 case 12: /* void outbyte(char chr) : write a byte to "stdout" */
1334 char tmp
= (char)(A0
& 0xFF);
1335 sim_io_write_stdout (sd
, &tmp
, sizeof(char));
1339 case 17: /* void _exit() */
1341 sim_io_eprintf (sd
, "sim_monitor(17): _exit(int reason) to be coded\n");
1342 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
, sim_exited
,
1343 (unsigned int)(A0
& 0xFFFFFFFF));
1347 case 28 : /* PMON flush_cache */
1350 case 55: /* void get_mem_info(unsigned int *ptr) */
1351 /* in: A0 = pointer to three word memory location */
1352 /* out: [A0 + 0] = size */
1353 /* [A0 + 4] = instruction cache size */
1354 /* [A0 + 8] = data cache size */
1356 unsigned_4 value
= MEM_SIZE
/* FIXME STATE_MEM_SIZE (sd) */;
1357 unsigned_4 zero
= 0;
1359 sim_write (sd
, A0
+ 0, (char *)&value
, 4);
1360 sim_write (sd
, A0
+ 4, (char *)&zero
, 4);
1361 sim_write (sd
, A0
+ 8, (char *)&zero
, 4);
1362 /* sim_io_eprintf (sd, "sim: get_mem_info() depreciated\n"); */
1366 case 158 : /* PMON printf */
1367 /* in: A0 = pointer to format string */
1368 /* A1 = optional argument 1 */
1369 /* A2 = optional argument 2 */
1370 /* A3 = optional argument 3 */
1372 /* The following is based on the PMON printf source */
1374 address_word s
= A0
;
1376 signed_word
*ap
= &A1
; /* 1st argument */
1377 /* This isn't the quickest way, since we call the host print
1378 routine for every character almost. But it does avoid
1379 having to allocate and manage a temporary string buffer. */
1380 /* TODO: Include check that we only use three arguments (A1,
1382 while (sim_read (sd
, s
++, &c
, 1) && c
!= '\0')
1387 enum {FMT_RJUST
, FMT_LJUST
, FMT_RJUST0
, FMT_CENTER
} fmt
= FMT_RJUST
;
1388 int width
= 0, trunc
= 0, haddot
= 0, longlong
= 0;
1389 while (sim_read (sd
, s
++, &c
, 1) && c
!= '\0')
1391 if (strchr ("dobxXulscefg%", c
))
1406 else if (c
>= '1' && c
<= '9')
1410 while (sim_read (sd
, s
++, &c
, 1) == 1 && isdigit (c
))
1413 n
= (unsigned int)strtol(tmp
,NULL
,10);
1426 sim_io_printf (sd
, "%%");
1431 address_word p
= *ap
++;
1433 while (sim_read (sd
, p
++, &ch
, 1) == 1 && ch
!= '\0')
1434 sim_io_printf(sd
, "%c", ch
);
1437 sim_io_printf(sd
,"(null)");
1440 sim_io_printf (sd
, "%c", (int)*ap
++);
1445 sim_read (sd
, s
++, &c
, 1);
1449 sim_read (sd
, s
++, &c
, 1);
1452 if (strchr ("dobxXu", c
))
1454 word64 lv
= (word64
) *ap
++;
1456 sim_io_printf(sd
,"<binary not supported>");
1459 sprintf (tmp
, "%%%s%c", longlong
? "ll" : "", c
);
1461 sim_io_printf(sd
, tmp
, lv
);
1463 sim_io_printf(sd
, tmp
, (int)lv
);
1466 else if (strchr ("eEfgG", c
))
1468 double dbl
= *(double*)(ap
++);
1469 sprintf (tmp
, "%%%d.%d%c", width
, trunc
, c
);
1470 sim_io_printf (sd
, tmp
, dbl
);
1476 sim_io_printf(sd
, "%c", c
);
1482 sim_io_error (sd
, "TODO: sim_monitor(%d) : PC = 0x%s\n",
1483 reason
, pr_addr(cia
));
1489 /* Store a word into memory. */
1492 store_word (SIM_DESC sd
,
1501 if ((vaddr
& 3) != 0)
1502 SignalExceptionAddressStore ();
1505 if (AddressTranslation (vaddr
, isDATA
, isSTORE
, &paddr
, &uncached
,
1508 const uword64 mask
= 7;
1512 paddr
= (paddr
& ~mask
) | ((paddr
& mask
) ^ (ReverseEndian
<< 2));
1513 byte
= (vaddr
& mask
) ^ (BigEndianCPU
<< 2);
1514 memval
= ((uword64
) val
) << (8 * byte
);
1515 StoreMemory (uncached
, AccessLength_WORD
, memval
, 0, paddr
, vaddr
,
1521 /* Load a word from memory. */
1524 load_word (SIM_DESC sd
,
1529 if ((vaddr
& 3) != 0)
1530 SignalExceptionAddressLoad ();
1536 if (AddressTranslation (vaddr
, isDATA
, isLOAD
, &paddr
, &uncached
,
1539 const uword64 mask
= 0x7;
1540 const unsigned int reverse
= ReverseEndian
? 1 : 0;
1541 const unsigned int bigend
= BigEndianCPU
? 1 : 0;
1545 paddr
= (paddr
& ~mask
) | ((paddr
& mask
) ^ (reverse
<< 2));
1546 LoadMemory (&memval
,NULL
,uncached
, AccessLength_WORD
, paddr
, vaddr
,
1548 byte
= (vaddr
& mask
) ^ (bigend
<< 2);
1549 return SIGNEXTEND (((memval
>> (8 * byte
)) & 0xffffffff), 32);
1556 /* Simulate the mips16 entry and exit pseudo-instructions. These
1557 would normally be handled by the reserved instruction exception
1558 code, but for ease of simulation we just handle them directly. */
1561 mips16_entry (SIM_DESC sd
,
1566 int aregs
, sregs
, rreg
;
1569 printf("DBG: mips16_entry: entered (insn = 0x%08X)\n",insn
);
1572 aregs
= (insn
& 0x700) >> 8;
1573 sregs
= (insn
& 0x0c0) >> 6;
1574 rreg
= (insn
& 0x020) >> 5;
1576 /* This should be checked by the caller. */
1585 /* This is the entry pseudo-instruction. */
1587 for (i
= 0; i
< aregs
; i
++)
1588 store_word (SD
, CPU
, cia
, (uword64
) (SP
+ 4 * i
), GPR
[i
+ 4]);
1596 store_word (SD
, CPU
, cia
, (uword64
) tsp
, RA
);
1599 for (i
= 0; i
< sregs
; i
++)
1602 store_word (SD
, CPU
, cia
, (uword64
) tsp
, GPR
[16 + i
]);
1610 /* This is the exit pseudo-instruction. */
1617 RA
= load_word (SD
, CPU
, cia
, (uword64
) tsp
);
1620 for (i
= 0; i
< sregs
; i
++)
1623 GPR
[i
+ 16] = load_word (SD
, CPU
, cia
, (uword64
) tsp
);
1628 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
1632 FGR
[0] = WORD64LO (GPR
[4]);
1633 FPR_STATE
[0] = fmt_uninterpreted
;
1635 else if (aregs
== 6)
1637 FGR
[0] = WORD64LO (GPR
[5]);
1638 FGR
[1] = WORD64LO (GPR
[4]);
1639 FPR_STATE
[0] = fmt_uninterpreted
;
1640 FPR_STATE
[1] = fmt_uninterpreted
;
1649 /*-- trace support ----------------------------------------------------------*/
1651 /* The TRACE support is provided (if required) in the memory accessing
1652 routines. Since we are also providing the architecture specific
1653 features, the architecture simulation code can also deal with
1654 notifying the TRACE world of cache flushes, etc. Similarly we do
1655 not need to provide profiling support in the simulator engine,
1656 since we can sample in the instruction fetch control loop. By
1657 defining the TRACE manifest, we add tracing as a run-time
1661 /* Tracing by default produces "din" format (as required by
1662 dineroIII). Each line of such a trace file *MUST* have a din label
1663 and address field. The rest of the line is ignored, so comments can
1664 be included if desired. The first field is the label which must be
1665 one of the following values:
1670 3 escape record (treated as unknown access type)
1671 4 escape record (causes cache flush)
1673 The address field is a 32bit (lower-case) hexadecimal address
1674 value. The address should *NOT* be preceded by "0x".
1676 The size of the memory transfer is not important when dealing with
1677 cache lines (as long as no more than a cache line can be
1678 transferred in a single operation :-), however more information
1679 could be given following the dineroIII requirement to allow more
1680 complete memory and cache simulators to provide better
1681 results. i.e. the University of Pisa has a cache simulator that can
1682 also take bus size and speed as (variable) inputs to calculate
1683 complete system performance (a much more useful ability when trying
1684 to construct an end product, rather than a processor). They
1685 currently have an ARM version of their tool called ChARM. */
1689 dotrace (SIM_DESC sd
,
1697 if (STATE
& simTRACE
) {
1699 fprintf(tracefh
,"%d %s ; width %d ; ",
1703 va_start(ap
,comment
);
1704 vfprintf(tracefh
,comment
,ap
);
1706 fprintf(tracefh
,"\n");
1708 /* NOTE: Since the "din" format will only accept 32bit addresses, and
1709 we may be generating 64bit ones, we should put the hi-32bits of the
1710 address into the comment field. */
1712 /* TODO: Provide a buffer for the trace lines. We can then avoid
1713 performing writes until the buffer is filled, or the file is
1716 /* NOTE: We could consider adding a comment field to the "din" file
1717 produced using type 3 markers (unknown access). This would then
1718 allow information about the program that the "din" is for, and
1719 the MIPs world that was being simulated, to be placed into the
1726 /*---------------------------------------------------------------------------*/
1727 /*-- simulator engine -------------------------------------------------------*/
1728 /*---------------------------------------------------------------------------*/
1731 ColdReset (SIM_DESC sd
)
1734 for (cpu_nr
= 0; cpu_nr
< sim_engine_nr_cpus (sd
); cpu_nr
++)
1736 sim_cpu
*cpu
= STATE_CPU (sd
, cpu_nr
);
1737 /* RESET: Fixed PC address: */
1738 PC
= (unsigned_word
) UNSIGNED64 (0xFFFFFFFFBFC00000);
1739 /* The reset vector address is in the unmapped, uncached memory space. */
1741 SR
&= ~(status_SR
| status_TS
| status_RP
);
1742 SR
|= (status_ERL
| status_BEV
);
1744 /* Cheat and allow access to the complete register set immediately */
1745 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
1746 && WITH_TARGET_WORD_BITSIZE
== 64)
1747 SR
|= status_FR
; /* 64bit registers */
1749 /* Ensure that any instructions with pending register updates are
1751 PENDING_INVALIDATE();
1753 /* Initialise the FPU registers to the unknown state */
1754 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
1757 for (rn
= 0; (rn
< 32); rn
++)
1758 FPR_STATE
[rn
] = fmt_uninterpreted
;
1764 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1765 /* Signal an exception condition. This will result in an exception
1766 that aborts the instruction. The instruction operation pseudocode
1767 will never see a return from this function call. */
1770 signal_exception (SIM_DESC sd
,
1778 sim_io_printf(sd
,"DBG: SignalException(%d) PC = 0x%s\n",exception
,pr_addr(cia
));
1781 /* Ensure that any active atomic read/modify/write operation will fail: */
1784 switch (exception
) {
1786 case DebugBreakPoint
:
1787 if (! (Debug
& Debug_DM
))
1793 Debug
|= Debug_DBD
; /* signaled from within in delay slot */
1794 DEPC
= cia
- 4; /* reference the branch instruction */
1798 Debug
&= ~Debug_DBD
; /* not signaled from within a delay slot */
1802 Debug
|= Debug_DM
; /* in debugging mode */
1803 Debug
|= Debug_DBp
; /* raising a DBp exception */
1805 sim_engine_restart (SD
, CPU
, NULL
, NULL_CIA
);
1809 case ReservedInstruction
:
1812 unsigned int instruction
;
1813 va_start(ap
,exception
);
1814 instruction
= va_arg(ap
,unsigned int);
1816 /* Provide simple monitor support using ReservedInstruction
1817 exceptions. The following code simulates the fixed vector
1818 entry points into the IDT monitor by causing a simulator
1819 trap, performing the monitor operation, and returning to
1820 the address held in the $ra register (standard PCS return
1821 address). This means we only need to pre-load the vector
1822 space with suitable instruction values. For systems were
1823 actual trap instructions are used, we would not need to
1824 perform this magic. */
1825 if ((instruction
& RSVD_INSTRUCTION_MASK
) == RSVD_INSTRUCTION
)
1827 sim_monitor (SD
, CPU
, cia
, ((instruction
>> RSVD_INSTRUCTION_ARG_SHIFT
) & RSVD_INSTRUCTION_ARG_MASK
) );
1828 /* NOTE: This assumes that a branch-and-link style
1829 instruction was used to enter the vector (which is the
1830 case with the current IDT monitor). */
1831 sim_engine_restart (SD
, CPU
, NULL
, RA
);
1833 /* Look for the mips16 entry and exit instructions, and
1834 simulate a handler for them. */
1835 else if ((cia
& 1) != 0
1836 && (instruction
& 0xf81f) == 0xe809
1837 && (instruction
& 0x0c0) != 0x0c0)
1839 mips16_entry (SD
, CPU
, cia
, instruction
);
1840 sim_engine_restart (sd
, NULL
, NULL
, NULL_CIA
);
1842 /* else fall through to normal exception processing */
1843 sim_io_eprintf(sd
,"ReservedInstruction at PC = 0x%s\n", pr_addr (cia
));
1847 /* Store exception code into current exception id variable (used
1850 /* TODO: If not simulating exceptions then stop the simulator
1851 execution. At the moment we always stop the simulation. */
1853 #ifdef SUBTARGET_R3900
1854 /* update interrupt-related registers */
1856 /* insert exception code in bits 6:2 */
1857 CAUSE
= LSMASKED32(CAUSE
, 31, 7) | LSINSERTED32(exception
, 6, 2);
1858 /* shift IE/KU history bits left */
1859 SR
= LSMASKED32(SR
, 31, 4) | LSINSERTED32(LSEXTRACTED32(SR
, 3, 0), 5, 2);
1861 if (STATE
& simDELAYSLOT
)
1863 STATE
&= ~simDELAYSLOT
;
1865 EPC
= (cia
- 4); /* reference the branch instruction */
1870 if (SR
& status_BEV
)
1871 PC
= (signed)0xBFC00000 + 0x180;
1873 PC
= (signed)0x80000000 + 0x080;
1875 /* See figure 5-17 for an outline of the code below */
1876 if (! (SR
& status_EXL
))
1878 CAUSE
= (exception
<< 2);
1879 if (STATE
& simDELAYSLOT
)
1881 STATE
&= ~simDELAYSLOT
;
1883 EPC
= (cia
- 4); /* reference the branch instruction */
1887 /* FIXME: TLB et.al. */
1888 /* vector = 0x180; */
1892 CAUSE
= (exception
<< 2);
1893 /* vector = 0x180; */
1896 /* Store exception code into current exception id variable (used
1899 if (SR
& status_BEV
)
1900 PC
= (signed)0xBFC00200 + 0x180;
1902 PC
= (signed)0x80000000 + 0x180;
1905 switch ((CAUSE
>> 2) & 0x1F)
1908 /* Interrupts arrive during event processing, no need to
1914 #ifdef SUBTARGET_3900
1915 /* Exception vector: BEV=0 BFC00000 / BEF=1 BFC00000 */
1916 PC
= (signed)0xBFC00000;
1917 #endif SUBTARGET_3900
1920 case TLBModification
:
1925 case InstructionFetch
:
1927 /* The following is so that the simulator will continue from the
1928 exception address on breakpoint operations. */
1930 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1931 sim_stopped
, SIM_SIGBUS
);
1933 case ReservedInstruction
:
1934 case CoProcessorUnusable
:
1936 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1937 sim_stopped
, SIM_SIGILL
);
1939 case IntegerOverflow
:
1941 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1942 sim_stopped
, SIM_SIGFPE
);
1947 sim_engine_restart (SD
, CPU
, NULL
, PC
);
1952 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1953 sim_stopped
, SIM_SIGTRAP
);
1955 default : /* Unknown internal exception */
1957 sim_engine_halt (SD
, CPU
, NULL
, NULL_CIA
,
1958 sim_stopped
, SIM_SIGABRT
);
1962 case SimulatorFault
:
1966 va_start(ap
,exception
);
1967 msg
= va_arg(ap
,char *);
1969 sim_engine_abort (SD
, CPU
, NULL_CIA
,
1970 "FATAL: Simulator error \"%s\"\n",msg
);
1977 #if defined(WARN_RESULT)
1978 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1979 /* This function indicates that the result of the operation is
1980 undefined. However, this should not affect the instruction
1981 stream. All that is meant to happen is that the destination
1982 register is set to an undefined result. To keep the simulator
1983 simple, we just don't bother updating the destination register, so
1984 the overall result will be undefined. If desired we can stop the
1985 simulator by raising a pseudo-exception. */
1986 #define UndefinedResult() undefined_result (sd,cia)
1988 undefined_result(sd
,cia
)
1992 sim_io_eprintf(sd
,"UndefinedResult: PC = 0x%s\n",pr_addr(cia
));
1993 #if 0 /* Disabled for the moment, since it actually happens a lot at the moment. */
1998 #endif /* WARN_RESULT */
2000 /*-- FPU support routines ---------------------------------------------------*/
2002 /* Numbers are held in normalized form. The SINGLE and DOUBLE binary
2003 formats conform to ANSI/IEEE Std 754-1985. */
2004 /* SINGLE precision floating:
2005 * seeeeeeeefffffffffffffffffffffff
2007 * e = 8bits = exponent
2008 * f = 23bits = fraction
2010 /* SINGLE precision fixed:
2011 * siiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
2013 * i = 31bits = integer
2015 /* DOUBLE precision floating:
2016 * seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
2018 * e = 11bits = exponent
2019 * f = 52bits = fraction
2021 /* DOUBLE precision fixed:
2022 * siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
2024 * i = 63bits = integer
2027 /* Extract sign-bit: */
2028 #define FP_S_s(v) (((v) & ((unsigned)1 << 31)) ? 1 : 0)
2029 #define FP_D_s(v) (((v) & ((uword64)1 << 63)) ? 1 : 0)
2030 /* Extract biased exponent: */
2031 #define FP_S_be(v) (((v) >> 23) & 0xFF)
2032 #define FP_D_be(v) (((v) >> 52) & 0x7FF)
2033 /* Extract unbiased Exponent: */
2034 #define FP_S_e(v) (FP_S_be(v) - 0x7F)
2035 #define FP_D_e(v) (FP_D_be(v) - 0x3FF)
2036 /* Extract complete fraction field: */
2037 #define FP_S_f(v) ((v) & ~((unsigned)0x1FF << 23))
2038 #define FP_D_f(v) ((v) & ~((uword64)0xFFF << 52))
2039 /* Extract numbered fraction bit: */
2040 #define FP_S_fb(b,v) (((v) & (1 << (23 - (b)))) ? 1 : 0)
2041 #define FP_D_fb(b,v) (((v) & (1 << (52 - (b)))) ? 1 : 0)
2043 /* Explicit QNaN values used when value required: */
2044 #define FPQNaN_SINGLE (0x7FBFFFFF)
2045 #define FPQNaN_WORD (0x7FFFFFFF)
2046 #define FPQNaN_DOUBLE (((uword64)0x7FF7FFFF << 32) | 0xFFFFFFFF)
2047 #define FPQNaN_LONG (((uword64)0x7FFFFFFF << 32) | 0xFFFFFFFF)
2049 /* Explicit Infinity values used when required: */
2050 #define FPINF_SINGLE (0x7F800000)
2051 #define FPINF_DOUBLE (((uword64)0x7FF00000 << 32) | 0x00000000)
2053 #if 1 /* def DEBUG */
2054 #define RMMODE(v) (((v) == FP_RM_NEAREST) ? "Round" : (((v) == FP_RM_TOZERO) ? "Trunc" : (((v) == FP_RM_TOPINF) ? "Ceil" : "Floor")))
2055 #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>"))))))
2059 value_fpr (SIM_DESC sd
,
2068 /* Treat unused register values, as fixed-point 64bit values: */
2069 if ((fmt
== fmt_uninterpreted
) || (fmt
== fmt_unknown
))
2071 /* If request to read data as "uninterpreted", then use the current
2073 fmt
= FPR_STATE
[fpr
];
2078 /* For values not yet accessed, set to the desired format: */
2079 if (FPR_STATE
[fpr
] == fmt_uninterpreted
) {
2080 FPR_STATE
[fpr
] = fmt
;
2082 printf("DBG: Register %d was fmt_uninterpreted. Now %s\n",fpr
,DOFMT(fmt
));
2085 if (fmt
!= FPR_STATE
[fpr
]) {
2086 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
));
2087 FPR_STATE
[fpr
] = fmt_unknown
;
2090 if (FPR_STATE
[fpr
] == fmt_unknown
) {
2091 /* Set QNaN value: */
2094 value
= FPQNaN_SINGLE
;
2098 value
= FPQNaN_DOUBLE
;
2102 value
= FPQNaN_WORD
;
2106 value
= FPQNaN_LONG
;
2113 } else if (SizeFGR() == 64) {
2117 value
= (FGR
[fpr
] & 0xFFFFFFFF);
2120 case fmt_uninterpreted
:
2134 value
= (FGR
[fpr
] & 0xFFFFFFFF);
2137 case fmt_uninterpreted
:
2140 if ((fpr
& 1) == 0) { /* even registers only */
2141 value
= ((((uword64
)FGR
[fpr
+1]) << 32) | (FGR
[fpr
] & 0xFFFFFFFF));
2143 SignalException(ReservedInstruction
,0);
2154 SignalExceptionSimulatorFault ("Unrecognised FP format in ValueFPR()");
2157 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());
2164 store_fpr (SIM_DESC sd
,
2174 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());
2177 if (SizeFGR() == 64) {
2179 case fmt_uninterpreted_32
:
2180 fmt
= fmt_uninterpreted
;
2183 FGR
[fpr
] = (((uword64
)0xDEADC0DE << 32) | (value
& 0xFFFFFFFF));
2184 FPR_STATE
[fpr
] = fmt
;
2187 case fmt_uninterpreted_64
:
2188 fmt
= fmt_uninterpreted
;
2189 case fmt_uninterpreted
:
2193 FPR_STATE
[fpr
] = fmt
;
2197 FPR_STATE
[fpr
] = fmt_unknown
;
2203 case fmt_uninterpreted_32
:
2204 fmt
= fmt_uninterpreted
;
2207 FGR
[fpr
] = (value
& 0xFFFFFFFF);
2208 FPR_STATE
[fpr
] = fmt
;
2211 case fmt_uninterpreted_64
:
2212 fmt
= fmt_uninterpreted
;
2213 case fmt_uninterpreted
:
2216 if ((fpr
& 1) == 0) { /* even register number only */
2217 FGR
[fpr
+1] = (value
>> 32);
2218 FGR
[fpr
] = (value
& 0xFFFFFFFF);
2219 FPR_STATE
[fpr
+ 1] = fmt
;
2220 FPR_STATE
[fpr
] = fmt
;
2222 FPR_STATE
[fpr
] = fmt_unknown
;
2223 FPR_STATE
[fpr
+ 1] = fmt_unknown
;
2224 SignalException(ReservedInstruction
,0);
2229 FPR_STATE
[fpr
] = fmt_unknown
;
2234 #if defined(WARN_RESULT)
2237 #endif /* WARN_RESULT */
2240 SignalExceptionSimulatorFault ("Unrecognised FP format in StoreFPR()");
2243 printf("DBG: StoreFPR: fpr[%d] = 0x%s (format %s)\n",fpr
,pr_addr(FGR
[fpr
]),DOFMT(fmt
));
2260 sim_fpu_32to (&wop
, op
);
2261 boolean
= sim_fpu_is_nan (&wop
);
2268 sim_fpu_64to (&wop
, op
);
2269 boolean
= sim_fpu_is_nan (&wop
);
2273 fprintf (stderr
, "Bad switch\n");
2278 printf("DBG: NaN: returning %d for 0x%s (format = %s)\n",boolean
,pr_addr(op
),DOFMT(fmt
));
2292 printf("DBG: Infinity: format %s 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2299 sim_fpu_32to (&wop
, op
);
2300 boolean
= sim_fpu_is_infinity (&wop
);
2306 sim_fpu_64to (&wop
, op
);
2307 boolean
= sim_fpu_is_infinity (&wop
);
2311 printf("DBG: TODO: unrecognised format (%s) for Infinity check\n",DOFMT(fmt
));
2316 printf("DBG: Infinity: returning %d for 0x%s (format = %s)\n",boolean
,pr_addr(op
),DOFMT(fmt
));
2330 /* Argument checking already performed by the FPCOMPARE code */
2333 printf("DBG: Less: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2336 /* The format type should already have been checked: */
2342 sim_fpu_32to (&wop1
, op1
);
2343 sim_fpu_32to (&wop2
, op2
);
2344 boolean
= sim_fpu_is_lt (&wop1
, &wop2
);
2351 sim_fpu_64to (&wop1
, op1
);
2352 sim_fpu_64to (&wop2
, op2
);
2353 boolean
= sim_fpu_is_lt (&wop1
, &wop2
);
2357 fprintf (stderr
, "Bad switch\n");
2362 printf("DBG: Less: returning %d (format = %s)\n",boolean
,DOFMT(fmt
));
2376 /* Argument checking already performed by the FPCOMPARE code */
2379 printf("DBG: Equal: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2382 /* The format type should already have been checked: */
2388 sim_fpu_32to (&wop1
, op1
);
2389 sim_fpu_32to (&wop2
, op2
);
2390 boolean
= sim_fpu_is_eq (&wop1
, &wop2
);
2397 sim_fpu_64to (&wop1
, op1
);
2398 sim_fpu_64to (&wop2
, op2
);
2399 boolean
= sim_fpu_is_eq (&wop1
, &wop2
);
2403 fprintf (stderr
, "Bad switch\n");
2408 printf("DBG: Equal: returning %d (format = %s)\n",boolean
,DOFMT(fmt
));
2415 AbsoluteValue(op
,fmt
)
2422 printf("DBG: AbsoluteValue: %s: op = 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2425 /* The format type should already have been checked: */
2431 sim_fpu_32to (&wop
, op
);
2432 sim_fpu_abs (&wop
, &wop
);
2433 sim_fpu_to32 (&ans
, &wop
);
2441 sim_fpu_64to (&wop
, op
);
2442 sim_fpu_abs (&wop
, &wop
);
2443 sim_fpu_to64 (&ans
, &wop
);
2448 fprintf (stderr
, "Bad switch\n");
2463 printf("DBG: Negate: %s: op = 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2466 /* The format type should already have been checked: */
2472 sim_fpu_32to (&wop
, op
);
2473 sim_fpu_neg (&wop
, &wop
);
2474 sim_fpu_to32 (&ans
, &wop
);
2482 sim_fpu_64to (&wop
, op
);
2483 sim_fpu_neg (&wop
, &wop
);
2484 sim_fpu_to64 (&ans
, &wop
);
2489 fprintf (stderr
, "Bad switch\n");
2505 printf("DBG: Add: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2508 /* The registers must specify FPRs valid for operands of type
2509 "fmt". If they are not valid, the result is undefined. */
2511 /* The format type should already have been checked: */
2519 sim_fpu_32to (&wop1
, op1
);
2520 sim_fpu_32to (&wop2
, op2
);
2521 sim_fpu_add (&ans
, &wop1
, &wop2
);
2522 sim_fpu_to32 (&res
, &ans
);
2532 sim_fpu_64to (&wop1
, op1
);
2533 sim_fpu_64to (&wop2
, op2
);
2534 sim_fpu_add (&ans
, &wop1
, &wop2
);
2535 sim_fpu_to64 (&res
, &ans
);
2540 fprintf (stderr
, "Bad switch\n");
2545 printf("DBG: Add: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2560 printf("DBG: Sub: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2563 /* The registers must specify FPRs valid for operands of type
2564 "fmt". If they are not valid, the result is undefined. */
2566 /* The format type should already have been checked: */
2574 sim_fpu_32to (&wop1
, op1
);
2575 sim_fpu_32to (&wop2
, op2
);
2576 sim_fpu_sub (&ans
, &wop1
, &wop2
);
2577 sim_fpu_to32 (&res
, &ans
);
2587 sim_fpu_64to (&wop1
, op1
);
2588 sim_fpu_64to (&wop2
, op2
);
2589 sim_fpu_sub (&ans
, &wop1
, &wop2
);
2590 sim_fpu_to64 (&res
, &ans
);
2595 fprintf (stderr
, "Bad switch\n");
2600 printf("DBG: Sub: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2607 Multiply(op1
,op2
,fmt
)
2615 printf("DBG: Multiply: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2618 /* The registers must specify FPRs valid for operands of type
2619 "fmt". If they are not valid, the result is undefined. */
2621 /* The format type should already have been checked: */
2629 sim_fpu_32to (&wop1
, op1
);
2630 sim_fpu_32to (&wop2
, op2
);
2631 sim_fpu_mul (&ans
, &wop1
, &wop2
);
2632 sim_fpu_to32 (&res
, &ans
);
2642 sim_fpu_64to (&wop1
, op1
);
2643 sim_fpu_64to (&wop2
, op2
);
2644 sim_fpu_mul (&ans
, &wop1
, &wop2
);
2645 sim_fpu_to64 (&res
, &ans
);
2650 fprintf (stderr
, "Bad switch\n");
2655 printf("DBG: Multiply: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2670 printf("DBG: Divide: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2673 /* The registers must specify FPRs valid for operands of type
2674 "fmt". If they are not valid, the result is undefined. */
2676 /* The format type should already have been checked: */
2684 sim_fpu_32to (&wop1
, op1
);
2685 sim_fpu_32to (&wop2
, op2
);
2686 sim_fpu_div (&ans
, &wop1
, &wop2
);
2687 sim_fpu_to32 (&res
, &ans
);
2697 sim_fpu_64to (&wop1
, op1
);
2698 sim_fpu_64to (&wop2
, op2
);
2699 sim_fpu_div (&ans
, &wop1
, &wop2
);
2700 sim_fpu_to64 (&res
, &ans
);
2705 fprintf (stderr
, "Bad switch\n");
2710 printf("DBG: Divide: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2724 printf("DBG: Recip: %s: op = 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2727 /* The registers must specify FPRs valid for operands of type
2728 "fmt". If they are not valid, the result is undefined. */
2730 /* The format type should already have been checked: */
2737 sim_fpu_32to (&wop
, op
);
2738 sim_fpu_inv (&ans
, &wop
);
2739 sim_fpu_to32 (&res
, &ans
);
2748 sim_fpu_64to (&wop
, op
);
2749 sim_fpu_inv (&ans
, &wop
);
2750 sim_fpu_to64 (&res
, &ans
);
2755 fprintf (stderr
, "Bad switch\n");
2760 printf("DBG: Recip: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2774 printf("DBG: SquareRoot: %s: op = 0x%s\n",DOFMT(fmt
),pr_addr(op
));
2777 /* The registers must specify FPRs valid for operands of type
2778 "fmt". If they are not valid, the result is undefined. */
2780 /* The format type should already have been checked: */
2787 sim_fpu_32to (&wop
, op
);
2788 sim_fpu_sqrt (&ans
, &wop
);
2789 sim_fpu_to32 (&res
, &ans
);
2798 sim_fpu_64to (&wop
, op
);
2799 sim_fpu_sqrt (&ans
, &wop
);
2800 sim_fpu_to64 (&res
, &ans
);
2805 fprintf (stderr
, "Bad switch\n");
2810 printf("DBG: SquareRoot: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2826 printf("DBG: Max: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2829 /* The registers must specify FPRs valid for operands of type
2830 "fmt". If they are not valid, the result is undefined. */
2832 /* The format type should already have been checked: */
2839 sim_fpu_32to (&wop1
, op1
);
2840 sim_fpu_32to (&wop2
, op2
);
2841 cmp
= sim_fpu_cmp (&wop1
, &wop2
);
2848 sim_fpu_64to (&wop1
, op1
);
2849 sim_fpu_64to (&wop2
, op2
);
2850 cmp
= sim_fpu_cmp (&wop1
, &wop2
);
2854 fprintf (stderr
, "Bad switch\n");
2860 case SIM_FPU_IS_SNAN
:
2861 case SIM_FPU_IS_QNAN
:
2863 case SIM_FPU_IS_NINF
:
2864 case SIM_FPU_IS_NNUMBER
:
2865 case SIM_FPU_IS_NDENORM
:
2866 case SIM_FPU_IS_NZERO
:
2867 result
= op2
; /* op1 - op2 < 0 */
2868 case SIM_FPU_IS_PINF
:
2869 case SIM_FPU_IS_PNUMBER
:
2870 case SIM_FPU_IS_PDENORM
:
2871 case SIM_FPU_IS_PZERO
:
2872 result
= op1
; /* op1 - op2 > 0 */
2874 fprintf (stderr
, "Bad switch\n");
2879 printf("DBG: Max: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2896 printf("DBG: Min: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt
),pr_addr(op1
),pr_addr(op2
));
2899 /* The registers must specify FPRs valid for operands of type
2900 "fmt". If they are not valid, the result is undefined. */
2902 /* The format type should already have been checked: */
2909 sim_fpu_32to (&wop1
, op1
);
2910 sim_fpu_32to (&wop2
, op2
);
2911 cmp
= sim_fpu_cmp (&wop1
, &wop2
);
2918 sim_fpu_64to (&wop1
, op1
);
2919 sim_fpu_64to (&wop2
, op2
);
2920 cmp
= sim_fpu_cmp (&wop1
, &wop2
);
2924 fprintf (stderr
, "Bad switch\n");
2930 case SIM_FPU_IS_SNAN
:
2931 case SIM_FPU_IS_QNAN
:
2933 case SIM_FPU_IS_NINF
:
2934 case SIM_FPU_IS_NNUMBER
:
2935 case SIM_FPU_IS_NDENORM
:
2936 case SIM_FPU_IS_NZERO
:
2937 result
= op1
; /* op1 - op2 < 0 */
2938 case SIM_FPU_IS_PINF
:
2939 case SIM_FPU_IS_PNUMBER
:
2940 case SIM_FPU_IS_PDENORM
:
2941 case SIM_FPU_IS_PZERO
:
2942 result
= op2
; /* op1 - op2 > 0 */
2944 fprintf (stderr
, "Bad switch\n");
2949 printf("DBG: Min: returning 0x%s (format = %s)\n",pr_addr(result
),DOFMT(fmt
));
2957 convert (SIM_DESC sd
,
2966 sim_fpu_round round
;
2967 unsigned32 result32
;
2968 unsigned64 result64
;
2971 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
));
2977 /* Round result to nearest representable value. When two
2978 representable values are equally near, round to the value
2979 that has a least significant bit of zero (i.e. is even). */
2980 round
= sim_fpu_round_near
;
2983 /* Round result to the value closest to, and not greater in
2984 magnitude than, the result. */
2985 round
= sim_fpu_round_zero
;
2988 /* Round result to the value closest to, and not less than,
2990 round
= sim_fpu_round_up
;
2994 /* Round result to the value closest to, and not greater than,
2996 round
= sim_fpu_round_down
;
3000 fprintf (stderr
, "Bad switch\n");
3004 /* Convert the input to sim_fpu internal format */
3008 sim_fpu_64to (&wop
, op
);
3011 sim_fpu_32to (&wop
, op
);
3014 sim_fpu_i32to (&wop
, op
, round
);
3017 sim_fpu_i64to (&wop
, op
, round
);
3020 fprintf (stderr
, "Bad switch\n");
3024 /* Convert sim_fpu format into the output */
3025 /* The value WOP is converted to the destination format, rounding
3026 using mode RM. When the destination is a fixed-point format, then
3027 a source value of Infinity, NaN or one which would round to an
3028 integer outside the fixed point range then an IEEE Invalid
3029 Operation condition is raised. */
3033 sim_fpu_round_32 (&wop
, round
, 0);
3034 sim_fpu_to32 (&result32
, &wop
);
3035 result64
= result32
;
3038 sim_fpu_round_64 (&wop
, round
, 0);
3039 sim_fpu_to64 (&result64
, &wop
);
3042 sim_fpu_to32i (&result32
, &wop
, round
);
3043 result64
= result32
;
3046 sim_fpu_to64i (&result64
, &wop
, round
);
3050 fprintf (stderr
, "Bad switch\n");
3055 printf("DBG: Convert: returning 0x%s (to format = %s)\n",pr_addr(result64
),DOFMT(to
));
3062 /*-- co-processor support routines ------------------------------------------*/
3065 CoProcPresent(coproc_number
)
3066 unsigned int coproc_number
;
3068 /* Return TRUE if simulator provides a model for the given co-processor number */
3073 cop_lw (SIM_DESC sd
,
3078 unsigned int memword
)
3083 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
3086 printf("DBG: COP_LW: memword = 0x%08X (uword64)memword = 0x%s\n",memword
,pr_addr(memword
));
3088 StoreFPR(coproc_reg
,fmt_word
,(uword64
)memword
);
3089 FPR_STATE
[coproc_reg
] = fmt_uninterpreted
;
3094 #if 0 /* this should be controlled by a configuration option */
3095 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
));
3104 cop_ld (SIM_DESC sd
,
3111 switch (coproc_num
) {
3113 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
3115 StoreFPR(coproc_reg
,fmt_uninterpreted
,memword
);
3120 #if 0 /* this message should be controlled by a configuration option */
3121 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
));
3130 /* start-sanitize-sky */
3133 cop_lq (SIM_DESC sd
,
3138 unsigned128 memword
)
3149 /* one word at a time, argh! */
3153 value
= H2T_4(*A4_16(& memword
, 3-i
));
3154 write_vu_vec_reg(&(vu0_device
.regs
), coproc_reg
, i
, & value
);
3160 sim_io_printf(sd
,"COP_LQ(%d,%d,??) at PC = 0x%s : TODO (architecture specific)\n",
3161 coproc_num
,coproc_reg
,pr_addr(cia
));
3167 #endif /* TARGET_SKY */
3168 /* end-sanitize-sky */
3172 cop_sw (SIM_DESC sd
,
3178 unsigned int value
= 0;
3183 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
3186 hold
= FPR_STATE
[coproc_reg
];
3187 FPR_STATE
[coproc_reg
] = fmt_word
;
3188 value
= (unsigned int)ValueFPR(coproc_reg
,fmt_uninterpreted
);
3189 FPR_STATE
[coproc_reg
] = hold
;
3194 #if 0 /* should be controlled by configuration option */
3195 sim_io_printf(sd
,"COP_SW(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num
,coproc_reg
,pr_addr(cia
));
3204 cop_sd (SIM_DESC sd
,
3214 if (CURRENT_FLOATING_POINT
== HARD_FLOATING_POINT
)
3216 value
= ValueFPR(coproc_reg
,fmt_uninterpreted
);
3221 #if 0 /* should be controlled by configuration option */
3222 sim_io_printf(sd
,"COP_SD(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num
,coproc_reg
,pr_addr(cia
));
3231 /* start-sanitize-sky */
3234 cop_sq (SIM_DESC sd
,
3240 unsigned128 value
= U16_8(0, 0);
3251 /* one word at a time, argh! */
3255 read_vu_vec_reg(&(vu0_device
.regs
), coproc_reg
, i
, & value
);
3256 *A4_16(& xyzw
, 3-i
) = T2H_4(value
);
3263 sim_io_printf(sd
,"COP_SQ(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",
3264 coproc_num
,coproc_reg
,pr_addr(cia
));
3270 #endif /* TARGET_SKY */
3271 /* end-sanitize-sky */
3275 decode_coproc (SIM_DESC sd
,
3278 unsigned int instruction
)
3280 int coprocnum
= ((instruction
>> 26) & 3);
3284 case 0: /* standard CPU control and cache registers */
3286 int code
= ((instruction
>> 21) & 0x1F);
3287 /* R4000 Users Manual (second edition) lists the following CP0
3289 DMFC0 Doubleword Move From CP0 (VR4100 = 01000000001tttttddddd00000000000)
3290 DMTC0 Doubleword Move To CP0 (VR4100 = 01000000101tttttddddd00000000000)
3291 MFC0 word Move From CP0 (VR4100 = 01000000000tttttddddd00000000000)
3292 MTC0 word Move To CP0 (VR4100 = 01000000100tttttddddd00000000000)
3293 TLBR Read Indexed TLB Entry (VR4100 = 01000010000000000000000000000001)
3294 TLBWI Write Indexed TLB Entry (VR4100 = 01000010000000000000000000000010)
3295 TLBWR Write Random TLB Entry (VR4100 = 01000010000000000000000000000110)
3296 TLBP Probe TLB for Matching Entry (VR4100 = 01000010000000000000000000001000)
3297 CACHE Cache operation (VR4100 = 101111bbbbbpppppiiiiiiiiiiiiiiii)
3298 ERET Exception return (VR4100 = 01000010000000000000000000011000)
3300 if (((code
== 0x00) || (code
== 0x04)) && ((instruction
& 0x7FF) == 0))
3302 int rt
= ((instruction
>> 16) & 0x1F);
3303 int rd
= ((instruction
>> 11) & 0x1F);
3305 switch (rd
) /* NOTEs: Standard CP0 registers */
3307 /* 0 = Index R4000 VR4100 VR4300 */
3308 /* 1 = Random R4000 VR4100 VR4300 */
3309 /* 2 = EntryLo0 R4000 VR4100 VR4300 */
3310 /* 3 = EntryLo1 R4000 VR4100 VR4300 */
3311 /* 4 = Context R4000 VR4100 VR4300 */
3312 /* 5 = PageMask R4000 VR4100 VR4300 */
3313 /* 6 = Wired R4000 VR4100 VR4300 */
3314 /* 8 = BadVAddr R4000 VR4100 VR4300 */
3315 /* 9 = Count R4000 VR4100 VR4300 */
3316 /* 10 = EntryHi R4000 VR4100 VR4300 */
3317 /* 11 = Compare R4000 VR4100 VR4300 */
3318 /* 12 = SR R4000 VR4100 VR4300 */
3319 #ifdef SUBTARGET_R3900
3323 /* 3 = Config R3900 */
3324 #endif /* SUBTARGET_R3900 */
3331 /* 13 = Cause R4000 VR4100 VR4300 */
3338 /* 14 = EPC R4000 VR4100 VR4300 */
3341 GPR
[rt
] = (signed_word
) (signed_address
) EPC
;
3345 /* 15 = PRId R4000 VR4100 VR4300 */
3346 #ifdef SUBTARGET_R3900
3355 /* 16 = Config R4000 VR4100 VR4300 */
3358 GPR
[rt
] = C0_CONFIG
;
3360 C0_CONFIG
= GPR
[rt
];
3363 #ifdef SUBTARGET_R3900
3372 /* 17 = LLAddr R4000 VR4100 VR4300 */
3374 /* 18 = WatchLo R4000 VR4100 VR4300 */
3375 /* 19 = WatchHi R4000 VR4100 VR4300 */
3376 /* 20 = XContext R4000 VR4100 VR4300 */
3377 /* 26 = PErr or ECC R4000 VR4100 VR4300 */
3378 /* 27 = CacheErr R4000 VR4100 */
3379 /* 28 = TagLo R4000 VR4100 VR4300 */
3380 /* 29 = TagHi R4000 VR4100 VR4300 */
3381 /* 30 = ErrorEPC R4000 VR4100 VR4300 */
3382 GPR
[rt
] = 0xDEADC0DE; /* CPR[0,rd] */
3383 /* CPR[0,rd] = GPR[rt]; */
3386 sim_io_printf(sd
,"Warning: MFC0 %d,%d ignored (architecture specific)\n",rt
,rd
);
3388 sim_io_printf(sd
,"Warning: MTC0 %d,%d ignored (architecture specific)\n",rt
,rd
);
3391 else if (code
== 0x10 && (instruction
& 0x3f) == 0x18)
3394 if (SR
& status_ERL
)
3396 /* Oops, not yet available */
3397 sim_io_printf(sd
,"Warning: ERET when SR[ERL] set not handled yet");
3407 else if (code
== 0x10 && (instruction
& 0x3f) == 0x10)
3410 #ifdef SUBTARGET_R3900
3411 /* TX39: Copy IEp/KUp -> IEc/KUc, and IEo/KUo -> IEp/KUp */
3413 /* shift IE/KU history bits right */
3414 SR
= LSMASKED32(SR
, 31, 4) | LSINSERTED32(LSEXTRACTED32(SR
, 5, 2), 3, 0);
3416 /* TODO: CACHE register */
3417 #endif /* SUBTARGET_R3900 */
3419 else if (code
== 0x10 && (instruction
& 0x3f) == 0x1F)
3427 sim_io_eprintf(sd
,"Unrecognised COP0 instruction 0x%08X at PC = 0x%s : No handler present\n",instruction
,pr_addr(cia
));
3428 /* TODO: When executing an ERET or RFE instruction we should
3429 clear LLBIT, to ensure that any out-standing atomic
3430 read/modify/write sequence fails. */
3434 case 2: /* co-processor 2 */
3438 /* start-sanitize-sky */
3440 /* On the R5900, this refers to a "VU" vector co-processor. */
3442 int i_25_21
= (instruction
>> 21) & 0x1f;
3443 int i_20_16
= (instruction
>> 16) & 0x1f;
3444 int i_20_6
= (instruction
>> 6) & 0x7fff;
3445 int i_15_11
= (instruction
>> 11) & 0x1f;
3446 int i_15_0
= instruction
& 0xffff;
3447 int i_10_1
= (instruction
>> 1) & 0x3ff;
3448 int i_10_0
= instruction
& 0x7ff;
3449 int i_10_6
= (instruction
>> 6) & 0x1f;
3450 int i_5_0
= instruction
& 0x03f;
3451 int interlock
= instruction
& 0x01;
3452 /* setup for semantic.c-like actions below */
3453 typedef unsigned_4 instruction_word
;
3459 /* test COP2 usability */
3460 if(! (SR
& status_CU2
))
3462 SignalException(CoProcessorUnusable
,instruction
);
3466 #define MY_INDEX itable_COPz_NORMAL
3467 #define MY_PREFIX COPz_NORMAL
3468 #define MY_NAME "COPz_NORMAL"
3470 /* classify & execute basic COP2 instructions */
3471 if(i_25_21
== 0x08 && i_20_16
== 0x00) /* BC2F */
3473 address_word offset
= EXTEND16(i_15_0
) << 2;
3474 if(! vu0_busy()) DELAY_SLOT(cia
+ 4 + offset
);
3476 else if(i_25_21
== 0x08 && i_20_16
==0x02) /* BC2FL */
3478 address_word offset
= EXTEND16(i_15_0
) << 2;
3479 if(! vu0_busy()) DELAY_SLOT(cia
+ 4 + offset
);
3480 else NULLIFY_NEXT_INSTRUCTION();
3482 else if(i_25_21
== 0x08 && i_20_16
== 0x01) /* BC2T */
3484 address_word offset
= EXTEND16(i_15_0
) << 2;
3485 if(vu0_busy()) DELAY_SLOT(cia
+ 4 + offset
);
3487 else if(i_25_21
== 0x08 && i_20_16
== 0x03) /* BC2TL */
3489 address_word offset
= EXTEND16(i_15_0
) << 2;
3490 if(vu0_busy()) DELAY_SLOT(cia
+ 4 + offset
);
3491 else NULLIFY_NEXT_INSTRUCTION();
3493 else if((i_25_21
== 0x02 && i_10_1
== 0x000) || /* CFC2 */
3494 (i_25_21
== 0x01)) /* QMFC2 */
3499 /* interlock checking */
3500 /* POLICY: never busy in macro mode */
3501 while(vu0_busy() && interlock
)
3504 /* perform VU register address */
3505 if(i_25_21
== 0x01) /* QMFC2 */
3508 /* one word at a time, argh! */
3509 read_vu_vec_reg(&(vu0_device
.regs
), id
, 0, A4_16(& xyzw
, 3));
3510 read_vu_vec_reg(&(vu0_device
.regs
), id
, 1, A4_16(& xyzw
, 2));
3511 read_vu_vec_reg(&(vu0_device
.regs
), id
, 2, A4_16(& xyzw
, 1));
3512 read_vu_vec_reg(&(vu0_device
.regs
), id
, 3, A4_16(& xyzw
, 0));
3513 GPR
[rt
] = T2H_8(* A8_16(& xyzw
, 1));
3514 GPR1
[rt
] = T2H_8(* A8_16(& xyzw
, 0));
3519 /* enum + int calculation, argh! */
3520 id
= VU_REG_MST
+ 16 * id
;
3521 if (id
>= VU_REG_CMSAR0
)
3522 read_vu_special_reg(&vu0_device
, id
, & data
);
3524 read_vu_misc_reg(&(vu0_device
.regs
), id
, & data
);
3525 GPR
[rt
] = EXTEND32(T2H_4(data
));
3528 else if((i_25_21
== 0x06 && i_10_1
== 0x000) || /* CTC2 */
3529 (i_25_21
== 0x05)) /* QMTC2 */
3534 /* interlock checking: wait until M or E bits set */
3535 /* POLICY: never busy in macro mode */
3536 while(vu0_busy() && interlock
)
3538 if(vu0_micro_interlock_released())
3540 vu0_micro_interlock_clear();
3547 /* perform VU register address */
3548 if(i_25_21
== 0x05) /* QMTC2 */
3550 unsigned_16 xyzw
= U16_8(GPR1
[rt
], GPR
[rt
]);
3552 xyzw
= H2T_16(xyzw
);
3553 /* one word at a time, argh! */
3554 write_vu_vec_reg(&(vu0_device
.regs
), id
, 0, A4_16(& xyzw
, 3));
3555 write_vu_vec_reg(&(vu0_device
.regs
), id
, 1, A4_16(& xyzw
, 2));
3556 write_vu_vec_reg(&(vu0_device
.regs
), id
, 2, A4_16(& xyzw
, 1));
3557 write_vu_vec_reg(&(vu0_device
.regs
), id
, 3, A4_16(& xyzw
, 0));
3561 unsigned_4 data
= H2T_4(GPR
[rt
]);
3562 /* enum + int calculation, argh! */
3563 id
= VU_REG_VI
+ 16 * id
;
3564 if (id
>= VU_REG_CMSAR0
)
3565 write_vu_special_reg(&vu0_device
, id
, & data
);
3567 write_vu_misc_reg(&(vu0_device
.regs
), id
, & data
);
3570 else if(i_10_0
== 0x3bf) /* VWAITQ */
3575 else if(i_5_0
== 0x38) /* VCALLMS */
3577 unsigned_4 data
= H2T_2(i_20_6
);
3582 /* write to reserved CIA register to get VU0 moving */
3583 write_vu_special_reg(& vu0_device
, VU_REG_CIA
, & data
);
3587 else if(i_5_0
== 0x39) /* VCALLMSR */
3594 read_vu_special_reg(& vu0_device
, VU_REG_CMSAR0
, & data
);
3595 /* write to reserved CIA register to get VU0 moving */
3596 write_vu_special_reg(& vu0_device
, VU_REG_CIA
, & data
);
3600 /* handle all remaining UPPER VU instructions in one block */
3601 else if((i_5_0
< 0x30) || /* VADDx .. VMINI */
3602 (i_5_0
>= 0x3c && i_10_6
< 0x0c)) /* VADDAx .. VNOP */
3604 unsigned_4 vu_upper
, vu_lower
;
3606 0x00000000 | /* bits 31 .. 25 */
3607 (instruction
& 0x01ffffff); /* bits 24 .. 0 */
3608 vu_lower
= 0x8000033c; /* NOP */
3610 /* POLICY: never busy in macro mode */
3614 vu0_macro_issue(vu_upper
, vu_lower
);
3616 /* POLICY: wait for completion of macro-instruction */
3620 /* handle all remaining LOWER VU instructions in one block */
3621 else if((i_5_0
>= 0x30 && i_5_0
<= 0x35) || /* VIADD .. VIOR */
3622 (i_5_0
>= 0x3c && i_10_6
>= 0x0c)) /* VMOVE .. VRXOR */
3623 { /* N.B.: VWAITQ already covered by prior case */
3624 unsigned_4 vu_upper
, vu_lower
;
3625 vu_upper
= 0x000002ff; /* NOP/NOP */
3627 0x80000000 | /* bits 31 .. 25 */
3628 (instruction
& 0x01ffffff); /* bits 24 .. 0 */
3630 /* POLICY: never busy in macro mode */
3634 vu0_macro_issue(vu_upper
, vu_lower
);
3636 /* POLICY: wait for completion of macro-instruction */
3640 /* ... no other COP2 instructions ... */
3643 SignalException(ReservedInstruction
, instruction
);
3647 /* cleanup for semantic.c-like actions above */
3654 #endif /* TARGET_SKY */
3655 /* end-sanitize-sky */
3659 sim_io_eprintf(sd
, "COP2 instruction 0x%08X at PC = 0x%s : No handler present\n",
3660 instruction
,pr_addr(cia
));
3665 case 1: /* should not occur (FPU co-processor) */
3666 case 3: /* should not occur (FPU co-processor) */
3667 SignalException(ReservedInstruction
,instruction
);
3675 /*-- instruction simulation -------------------------------------------------*/
3677 /* When the IGEN simulator is being built, the function below is be
3678 replaced by a generated version. However, WITH_IGEN == 2 indicates
3679 that the fubction below should be compiled but under a different
3680 name (to allow backward compatibility) */
3682 #if (WITH_IGEN != 1)
3684 void old_engine_run
PARAMS ((SIM_DESC sd
, int next_cpu_nr
, int siggnal
));
3686 old_engine_run (sd
, next_cpu_nr
, nr_cpus
, siggnal
)
3689 sim_engine_run (sd
, next_cpu_nr
, nr_cpus
, siggnal
)
3692 int next_cpu_nr
; /* ignore */
3693 int nr_cpus
; /* ignore */
3694 int siggnal
; /* ignore */
3696 sim_cpu
*cpu
= STATE_CPU (sd
, 0); /* hardwire to cpu 0 */
3697 #if !defined(FASTSIM)
3698 unsigned int pipeline_count
= 1;
3702 if (STATE_MEMORY (sd
) == NULL
) {
3703 printf("DBG: simulate() entered with no memory\n");
3708 #if 0 /* Disabled to check that everything works OK */
3709 /* The VR4300 seems to sign-extend the PC on its first
3710 access. However, this may just be because it is currently
3711 configured in 32bit mode. However... */
3712 PC
= SIGNEXTEND(PC
,32);
3715 /* main controlling loop */
3717 /* vaddr is slowly being replaced with cia - current instruction
3719 address_word cia
= (uword64
)PC
;
3720 address_word vaddr
= cia
;
3723 unsigned int instruction
; /* uword64? what's this used for? FIXME! */
3727 printf("DBG: state = 0x%08X :",state
);
3728 if (state
& simHALTEX
) printf(" simHALTEX");
3729 if (state
& simHALTIN
) printf(" simHALTIN");
3734 DSSTATE
= (STATE
& simDELAYSLOT
);
3737 sim_io_printf(sd
,"DBG: DSPC = 0x%s\n",pr_addr(DSPC
));
3740 /* Fetch the next instruction from the simulator memory: */
3741 if (AddressTranslation(cia
,isINSTRUCTION
,isLOAD
,&paddr
,&cca
,isTARGET
,isREAL
)) {
3742 if ((vaddr
& 1) == 0) {
3743 /* Copy the action of the LW instruction */
3744 unsigned int reverse
= (ReverseEndian
? (LOADDRMASK
>> 2) : 0);
3745 unsigned int bigend
= (BigEndianCPU
? (LOADDRMASK
>> 2) : 0);
3748 paddr
= ((paddr
& ~LOADDRMASK
) | ((paddr
& LOADDRMASK
) ^ (reverse
<< 2)));
3749 LoadMemory(&value
,NULL
,cca
,AccessLength_WORD
,paddr
,vaddr
,isINSTRUCTION
,isREAL
);
3750 byte
= ((vaddr
& LOADDRMASK
) ^ (bigend
<< 2));
3751 instruction
= ((value
>> (8 * byte
)) & 0xFFFFFFFF);
3753 /* Copy the action of the LH instruction */
3754 unsigned int reverse
= (ReverseEndian
? (LOADDRMASK
>> 1) : 0);
3755 unsigned int bigend
= (BigEndianCPU
? (LOADDRMASK
>> 1) : 0);
3758 paddr
= (((paddr
& ~ (uword64
) 1) & ~LOADDRMASK
)
3759 | (((paddr
& ~ (uword64
) 1) & LOADDRMASK
) ^ (reverse
<< 1)));
3760 LoadMemory(&value
,NULL
,cca
, AccessLength_HALFWORD
,
3761 paddr
& ~ (uword64
) 1,
3762 vaddr
, isINSTRUCTION
, isREAL
);
3763 byte
= (((vaddr
&~ (uword64
) 1) & LOADDRMASK
) ^ (bigend
<< 1));
3764 instruction
= ((value
>> (8 * byte
)) & 0xFFFF);
3767 fprintf(stderr
,"Cannot translate address for PC = 0x%s failed\n",pr_addr(PC
));
3772 sim_io_printf(sd
,"DBG: fetched 0x%08X from PC = 0x%s\n",instruction
,pr_addr(PC
));
3775 /* This is required by exception processing, to ensure that we can
3776 cope with exceptions in the delay slots of branches that may
3777 already have changed the PC. */
3778 if ((vaddr
& 1) == 0)
3779 PC
+= 4; /* increment ready for the next fetch */
3782 /* NOTE: If we perform a delay slot change to the PC, this
3783 increment is not requuired. However, it would make the
3784 simulator more complicated to try and avoid this small hit. */
3786 /* Currently this code provides a simple model. For more
3787 complicated models we could perform exception status checks at
3788 this point, and set the simSTOP state as required. This could
3789 also include processing any hardware interrupts raised by any
3790 I/O model attached to the simulator context.
3792 Support for "asynchronous" I/O events within the simulated world
3793 could be providing by managing a counter, and calling a I/O
3794 specific handler when a particular threshold is reached. On most
3795 architectures a decrement and check for zero operation is
3796 usually quicker than an increment and compare. However, the
3797 process of managing a known value decrement to zero, is higher
3798 than the cost of using an explicit value UINT_MAX into the
3799 future. Which system is used will depend on how complicated the
3800 I/O model is, and how much it is likely to affect the simulator
3803 If events need to be scheduled further in the future than
3804 UINT_MAX event ticks, then the I/O model should just provide its
3805 own counter, triggered from the event system. */
3807 /* MIPS pipeline ticks. To allow for future support where the
3808 pipeline hit of individual instructions is known, this control
3809 loop manages a "pipeline_count" variable. It is initialised to
3810 1 (one), and will only be changed by the simulator engine when
3811 executing an instruction. If the engine does not have access to
3812 pipeline cycle count information then all instructions will be
3813 treated as using a single cycle. NOTE: A standard system is not
3814 provided by the default simulator because different MIPS
3815 architectures have different cycle counts for the same
3818 [NOTE: pipeline_count has been replaced the event queue] */
3820 /* shuffle the floating point status pipeline state */
3821 ENGINE_ISSUE_PREFIX_HOOK();
3823 /* NOTE: For multi-context simulation environments the "instruction"
3824 variable should be local to this routine. */
3826 /* Shorthand accesses for engine. Note: If we wanted to use global
3827 variables (and a single-threaded simulator engine), then we can
3828 create the actual variables with these names. */
3830 if (!(STATE
& simSKIPNEXT
)) {
3831 /* Include the simulator engine */
3832 #include "oengine.c"
3833 #if ((GPRLEN == 64) && !PROCESSOR_64BIT) || ((GPRLEN == 32) && PROCESSOR_64BIT)
3834 #error "Mismatch between run-time simulator code and simulation engine"
3836 #if (WITH_TARGET_WORD_BITSIZE != GPRLEN)
3837 #error "Mismatch between configure WITH_TARGET_WORD_BITSIZE and gencode GPRLEN"
3839 #if ((WITH_FLOATING_POINT == HARD_FLOATING_POINT) != defined (HASFPU))
3840 #error "Mismatch between configure WITH_FLOATING_POINT and gencode HASFPU"
3843 /* For certain MIPS architectures, GPR[0] is hardwired to zero. We
3844 should check for it being changed. It is better doing it here,
3845 than within the simulator, since it will help keep the simulator
3848 #if defined(WARN_ZERO)
3849 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
));
3850 #endif /* WARN_ZERO */
3851 ZERO
= 0; /* reset back to zero before next instruction */
3853 } else /* simSKIPNEXT check */
3854 STATE
&= ~simSKIPNEXT
;
3856 /* If the delay slot was active before the instruction is
3857 executed, then update the PC to its new value: */
3860 printf("DBG: dsstate set before instruction execution - updating PC to 0x%s\n",pr_addr(DSPC
));
3869 #if !defined(FASTSIM)
3870 if (sim_events_tickn (sd
, pipeline_count
))
3872 /* cpu->cia = cia; */
3873 sim_events_process (sd
);
3876 if (sim_events_tick (sd
))
3878 /* cpu->cia = cia; */
3879 sim_events_process (sd
);
3881 #endif /* FASTSIM */
3887 /* This code copied from gdb's utils.c. Would like to share this code,
3888 but don't know of a common place where both could get to it. */
3890 /* Temporary storage using circular buffer */
3896 static char buf
[NUMCELLS
][CELLSIZE
];
3898 if (++cell
>=NUMCELLS
) cell
=0;
3902 /* Print routines to handle variable size regs, etc */
3904 /* Eliminate warning from compiler on 32-bit systems */
3905 static int thirty_two
= 32;
3911 char *paddr_str
=get_cell();
3912 switch (sizeof(addr
))
3915 sprintf(paddr_str
,"%08lx%08lx",
3916 (unsigned long)(addr
>>thirty_two
),(unsigned long)(addr
&0xffffffff));
3919 sprintf(paddr_str
,"%08lx",(unsigned long)addr
);
3922 sprintf(paddr_str
,"%04x",(unsigned short)(addr
&0xffff));
3925 sprintf(paddr_str
,"%x",addr
);
3934 char *paddr_str
=get_cell();
3935 sprintf(paddr_str
,"%08lx%08lx",
3936 (unsigned long)(addr
>>thirty_two
),(unsigned long)(addr
&0xffffffff));
3942 /*---------------------------------------------------------------------------*/
3943 /*> EOF interp.c <*/