Delete profile support from MIPS simulator, use sim/common/sim-profile
[deliverable/binutils-gdb.git] / sim / mips / interp.c
1 /*> interp.c <*/
2 /* Simulator for the MIPS architecture.
3
4 This file is part of the MIPS sim
5
6 THIS SOFTWARE IS NOT COPYRIGHTED
7
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.
11
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.
15
16 $Revision$
17 $Author$
18 $Date$
19
20 NOTEs:
21
22 We only need to take account of the target endianness when moving data
23 between the simulator and the host. We do not need to worry about the
24 endianness of the host, since this sim code and GDB are executing in
25 the same process.
26
27 The IDT monitor (found on the VR4300 board), seems to lie about
28 register contents. It seems to treat the registers as sign-extended
29 32-bit values. This cause *REAL* problems when single-stepping 64-bit
30 code on the hardware.
31
32 */
33
34 /* The TRACE manifests enable the provision of extra features. If they
35 are not defined then a simpler (quicker) simulator is constructed
36 without the required run-time checks, etc. */
37 #if 1 /* 0 to allow user build selection, 1 to force inclusion */
38 #define TRACE (1)
39 #endif
40
41 #include "bfd.h"
42 #include "sim-main.h"
43 #include "sim-utils.h"
44 #include "sim-options.h"
45 #include "sim-assert.h"
46
47 #include "config.h"
48
49 #include <stdio.h>
50 #include <stdarg.h>
51 #include <ansidecl.h>
52 #include <ctype.h>
53 #include <limits.h>
54 #include <math.h>
55 #ifdef HAVE_STDLIB_H
56 #include <stdlib.h>
57 #endif
58 #ifdef HAVE_STRING_H
59 #include <string.h>
60 #else
61 #ifdef HAVE_STRINGS_H
62 #include <strings.h>
63 #endif
64 #endif
65
66 #include "getopt.h"
67 #include "libiberty.h"
68 #include "bfd.h"
69 #include "callback.h" /* GDB simulator callback interface */
70 #include "remote-sim.h" /* GDB simulator interface */
71
72 #include "sysdep.h"
73
74 #ifndef PARAMS
75 #define PARAMS(x)
76 #endif
77
78 char* pr_addr PARAMS ((SIM_ADDR addr));
79 char* pr_uword64 PARAMS ((uword64 addr));
80
81
82 /* Get the simulator engine description, without including the code: */
83 #define SIM_MANIFESTS
84 #include "oengine.c"
85 #undef SIM_MANIFESTS
86
87
88 /* The following reserved instruction value is used when a simulator
89 trap is required. NOTE: Care must be taken, since this value may be
90 used in later revisions of the MIPS ISA. */
91 #define RSVD_INSTRUCTION (0x00000005)
92 #define RSVD_INSTRUCTION_MASK (0xFC00003F)
93
94 #define RSVD_INSTRUCTION_ARG_SHIFT 6
95 #define RSVD_INSTRUCTION_ARG_MASK 0xFFFFF
96
97
98 /* Bits in the Debug register */
99 #define Debug_DBD 0x80000000 /* Debug Branch Delay */
100 #define Debug_DM 0x40000000 /* Debug Mode */
101 #define Debug_DBp 0x00000002 /* Debug Breakpoint indicator */
102
103
104
105
106
107 /*---------------------------------------------------------------------------*/
108 /*-- GDB simulator interface ------------------------------------------------*/
109 /*---------------------------------------------------------------------------*/
110
111 static void dotrace PARAMS((SIM_DESC sd,FILE *tracefh,int type,SIM_ADDR address,int width,char *comment,...));
112 static void ColdReset PARAMS((SIM_DESC sd));
113 static long getnum PARAMS((SIM_DESC sd, char *value));
114 static unsigned int power2 PARAMS((unsigned int value));
115 static void mips_size PARAMS((SIM_DESC sd, int n));
116
117 /*---------------------------------------------------------------------------*/
118
119
120
121 #define DELAYSLOT() {\
122 if (STATE & simDELAYSLOT)\
123 sim_io_eprintf(sd,"Delay slot already activated (branch in delay slot?)\n");\
124 STATE |= simDELAYSLOT;\
125 }
126
127 #define JALDELAYSLOT() {\
128 DELAYSLOT ();\
129 STATE |= simJALDELAYSLOT;\
130 }
131
132 #define NULLIFY() {\
133 STATE &= ~simDELAYSLOT;\
134 STATE |= simSKIPNEXT;\
135 }
136
137 #define CANCELDELAYSLOT() {\
138 DSSTATE = 0;\
139 STATE &= ~(simDELAYSLOT | simJALDELAYSLOT);\
140 }
141
142 #define INDELAYSLOT() ((STATE & simDELAYSLOT) != 0)
143 #define INJALDELAYSLOT() ((STATE & simJALDELAYSLOT) != 0)
144
145 #define K0BASE (0x80000000)
146 #define K0SIZE (0x20000000)
147 #define K1BASE (0xA0000000)
148 #define K1SIZE (0x20000000)
149
150 /* Simple run-time monitor support */
151 static unsigned char *monitor = NULL;
152 static ut_reg monitor_base = 0xBFC00000;
153 static unsigned monitor_size = (1 << 11); /* power-of-2 */
154
155 static char *logfile = NULL; /* logging disabled by default */
156 static FILE *logfh = NULL;
157
158 #if defined(TRACE)
159 static char *tracefile = "trace.din"; /* default filename for trace log */
160 static FILE *tracefh = NULL;
161 static void open_trace PARAMS((SIM_DESC sd));
162 #endif /* TRACE */
163
164 static SIM_RC
165 mips_option_handler (sd, opt, arg)
166 SIM_DESC sd;
167 int opt;
168 char *arg;
169 {
170 switch (opt)
171 {
172 case 'l':
173 if (arg != NULL) {
174 char *tmp;
175 tmp = (char *)malloc(strlen(arg) + 1);
176 if (tmp == NULL)
177 sim_io_printf(sd,"Failed to allocate buffer for logfile name \"%s\"\n",optarg);
178 else {
179 strcpy(tmp,optarg);
180 logfile = tmp;
181 }
182 }
183 return SIM_RC_OK;
184
185 case 'n': /* OK */
186 sim_io_printf(sd,"Explicit model selection not yet available (Ignoring \"%s\")\n",optarg);
187 return SIM_RC_FAIL;
188
189 case 't': /* ??? */
190 #if defined(TRACE)
191 /* Eventually the simTRACE flag could be treated as a toggle, to
192 allow external control of the program points being traced
193 (i.e. only from main onwards, excluding the run-time setup,
194 etc.). */
195 if (arg == NULL)
196 STATE |= simTRACE;
197 else if (strcmp (arg, "yes") == 0)
198 STATE |= simTRACE;
199 else if (strcmp (arg, "no") == 0)
200 STATE &= ~simTRACE;
201 else
202 {
203 fprintf (stderr, "Unreconized trace option `%s'\n", arg);
204 return SIM_RC_FAIL;
205 }
206 return SIM_RC_OK;
207 #else /* !TRACE */
208 fprintf(stderr,"\
209 Simulator constructed without tracing support (for performance).\n\
210 Re-compile simulator with \"-DTRACE\" to enable this option.\n");
211 return SIM_RC_FAIL;
212 #endif /* !TRACE */
213
214 case 'z':
215 #if defined(TRACE)
216 if (optarg != NULL) {
217 char *tmp;
218 tmp = (char *)malloc(strlen(optarg) + 1);
219 if (tmp == NULL)
220 {
221 sim_io_printf(sd,"Failed to allocate buffer for tracefile name \"%s\"\n",optarg);
222 return SIM_RC_FAIL;
223 }
224 else {
225 strcpy(tmp,optarg);
226 tracefile = tmp;
227 sim_io_printf(sd,"Placing trace information into file \"%s\"\n",tracefile);
228 }
229 }
230 #endif /* TRACE */
231 return SIM_RC_OK;
232
233 }
234
235 return SIM_RC_OK;
236 }
237
238 static const OPTION mips_options[] =
239 {
240 { {"log", required_argument, NULL,'l'},
241 'l', "FILE", "Log file",
242 mips_option_handler },
243 { {"name", required_argument, NULL,'n'},
244 'n', "MODEL", "Select arch model",
245 mips_option_handler },
246 { {"trace", optional_argument, NULL,'t'},
247 't', "on|off", "Enable tracing",
248 mips_option_handler },
249 { {"tracefile",required_argument, NULL,'z'},
250 'z', "FILE", "Write trace to file",
251 mips_option_handler },
252 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
253 };
254
255
256 int interrupt_pending;
257
258 static void
259 interrupt_event (SIM_DESC sd, void *data)
260 {
261 if (SR & status_IE)
262 {
263 interrupt_pending = 0;
264 SignalExceptionInterrupt ();
265 }
266 else if (!interrupt_pending)
267 sim_events_schedule (sd, 1, interrupt_event, data);
268 }
269
270
271
272 /*---------------------------------------------------------------------------*/
273 /*-- GDB simulator interface ------------------------------------------------*/
274 /*---------------------------------------------------------------------------*/
275
276 SIM_DESC
277 sim_open (kind, cb, abfd, argv)
278 SIM_OPEN_KIND kind;
279 host_callback *cb;
280 struct _bfd *abfd;
281 char **argv;
282 {
283 SIM_DESC sd = sim_state_alloc (kind, cb);
284 sim_cpu *cpu = STATE_CPU (sd, 0);
285
286 /* FIXME: watchpoints code shouldn't need this */
287 STATE_WATCHPOINTS (sd)->pc = &(PC);
288 STATE_WATCHPOINTS (sd)->sizeof_pc = sizeof (PC);
289 STATE_WATCHPOINTS (sd)->interrupt_handler = interrupt_event;
290
291 /* memory defaults (unless sim_size was here first) */
292 if (STATE_MEM_SIZE (sd) == 0)
293 STATE_MEM_SIZE (sd) = (2 << 20);
294 STATE_MEM_BASE (sd) = K1BASE;
295
296 STATE = 0;
297
298 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
299 return 0;
300 sim_add_option_table (sd, mips_options);
301
302 /* getopt will print the error message so we just have to exit if this fails.
303 FIXME: Hmmm... in the case of gdb we need getopt to call
304 print_filtered. */
305 if (sim_parse_args (sd, argv) != SIM_RC_OK)
306 {
307 /* Uninstall the modules to avoid memory leaks,
308 file descriptor leaks, etc. */
309 sim_module_uninstall (sd);
310 return 0;
311 }
312
313 /* check for/establish the a reference program image */
314 if (sim_analyze_program (sd,
315 (STATE_PROG_ARGV (sd) != NULL
316 ? *STATE_PROG_ARGV (sd)
317 : NULL),
318 abfd) != SIM_RC_OK)
319 {
320 sim_module_uninstall (sd);
321 return 0;
322 }
323
324 /* Configure/verify the target byte order and other runtime
325 configuration options */
326 if (sim_config (sd) != SIM_RC_OK)
327 {
328 sim_module_uninstall (sd);
329 return 0;
330 }
331
332 if (sim_post_argv_init (sd) != SIM_RC_OK)
333 {
334 /* Uninstall the modules to avoid memory leaks,
335 file descriptor leaks, etc. */
336 sim_module_uninstall (sd);
337 return 0;
338 }
339
340 /* verify assumptions the simulator made about the host type system.
341 This macro does not return if there is a problem */
342 if (sizeof(int) != (4 * sizeof(char)))
343 SignalExceptionSimulatorFault ("sizeof(int) != 4");
344 if (sizeof(word64) != (8 * sizeof(char)))
345 SignalExceptionSimulatorFault ("sizeof(word64) != 8");
346
347 #if defined(HASFPU)
348 /* Check that the host FPU conforms to IEEE 754-1985 for the SINGLE
349 and DOUBLE binary formats. This is a bit nasty, requiring that we
350 trust the explicit manifests held in the source: */
351 /* TODO: We need to cope with the simulated target and the host not
352 having the same endianness. This will require the high and low
353 words of a (double) to be swapped when converting between the
354 host and the simulated target. */
355 {
356 union {
357 unsigned int i[2];
358 double d;
359 float f[2];
360 } s;
361
362 s.d = (double)523.2939453125;
363
364 if ((s.i[0] == 0 && (s.f[1] != (float)4.01102924346923828125
365 || s.i[1] != 0x40805A5A))
366 || (s.i[1] == 0 && (s.f[0] != (float)4.01102924346923828125
367 || s.i[0] != 0x40805A5A)))
368 {
369 fprintf(stderr,"The host executing the simulator does not seem to have IEEE 754-1985 std FP\n");
370 return 0;
371 }
372 }
373 #endif /* HASFPU */
374
375 /* This is NASTY, in that we are assuming the size of specific
376 registers: */
377 {
378 int rn;
379 for (rn = 0; (rn < (LAST_EMBED_REGNUM + 1)); rn++) {
380 if (rn < 32)
381 cpu->register_widths[rn] = GPRLEN;
382 else if ((rn >= FGRIDX) && (rn < (FGRIDX + 32)))
383 cpu->register_widths[rn] = GPRLEN;
384 else if ((rn >= 33) && (rn <= 37))
385 cpu->register_widths[rn] = GPRLEN;
386 else if ((rn == SRIDX) || (rn == FCR0IDX) || (rn == FCR31IDX) || ((rn >= 72) && (rn <= 89)))
387 cpu->register_widths[rn] = 32;
388 else
389 cpu->register_widths[rn] = 0;
390 }
391 /* start-sanitize-r5900 */
392
393 /* set the 5900 "upper" registers to 64 bits */
394 for( rn = LAST_EMBED_REGNUM+1; rn < NUM_REGS; rn++)
395 cpu->register_widths[rn] = 64;
396 /* end-sanitize-r5900 */
397 }
398
399
400 if (logfile != NULL) {
401 if (strcmp(logfile,"-") == 0)
402 logfh = stdout;
403 else {
404 logfh = fopen(logfile,"wb+");
405 if (logfh == NULL) {
406 sim_io_printf(sd,"Failed to create file \"%s\", writing log information to stderr.\n",tracefile);
407 logfh = stderr;
408 }
409 }
410 }
411
412 /* FIXME: In the future both of these malloc's can be replaced by
413 calls to sim-core. */
414
415 /* If the host has "mmap" available we could use it to provide a
416 very large virtual address space for the simulator, since memory
417 would only be allocated within the "mmap" space as it is
418 accessed. This can also be linked to the architecture specific
419 support, required to simulate the MMU. */
420 mips_size(sd, STATE_MEM_SIZE (sd));
421 /* NOTE: The above will also have enabled any profiling state! */
422
423 /* Create the monitor address space as well */
424 monitor = (unsigned char *)calloc(1,monitor_size);
425 if (!monitor)
426 fprintf(stderr,"Not enough VM for monitor simulation (%d bytes)\n",
427 monitor_size);
428
429 #if defined(TRACE)
430 if (STATE & simTRACE)
431 open_trace(sd);
432 #endif /* TRACE */
433
434 /* Write the monitor trap address handlers into the monitor (eeprom)
435 address space. This can only be done once the target endianness
436 has been determined. */
437 {
438 unsigned loop;
439 /* Entry into the IDT monitor is via fixed address vectors, and
440 not using machine instructions. To avoid clashing with use of
441 the MIPS TRAP system, we place our own (simulator specific)
442 "undefined" instructions into the relevant vector slots. */
443 for (loop = 0; (loop < monitor_size); loop += 4) {
444 uword64 vaddr = (monitor_base + loop);
445 uword64 paddr;
446 int cca;
447 if (AddressTranslation(vaddr, isDATA, isSTORE, &paddr, &cca, isTARGET, isRAW))
448 StoreMemory(cca, AccessLength_WORD,
449 (RSVD_INSTRUCTION | (((loop >> 2) & RSVD_INSTRUCTION_ARG_MASK) << RSVD_INSTRUCTION_ARG_SHIFT)),
450 0, paddr, vaddr, isRAW);
451 }
452 /* The PMON monitor uses the same address space, but rather than
453 branching into it the address of a routine is loaded. We can
454 cheat for the moment, and direct the PMON routine to IDT style
455 instructions within the monitor space. This relies on the IDT
456 monitor not using the locations from 0xBFC00500 onwards as its
457 entry points.*/
458 for (loop = 0; (loop < 24); loop++)
459 {
460 uword64 vaddr = (monitor_base + 0x500 + (loop * 4));
461 uword64 paddr;
462 int cca;
463 unsigned int value = ((0x500 - 8) / 8); /* default UNDEFINED reason code */
464 switch (loop)
465 {
466 case 0: /* read */
467 value = 7;
468 break;
469
470 case 1: /* write */
471 value = 8;
472 break;
473
474 case 2: /* open */
475 value = 6;
476 break;
477
478 case 3: /* close */
479 value = 10;
480 break;
481
482 case 5: /* printf */
483 value = ((0x500 - 16) / 8); /* not an IDT reason code */
484 break;
485
486 case 8: /* cliexit */
487 value = 17;
488 break;
489
490 case 11: /* flush_cache */
491 value = 28;
492 break;
493 }
494 /* FIXME - should monitor_base be SIM_ADDR?? */
495 value = ((unsigned int)monitor_base + (value * 8));
496 if (AddressTranslation(vaddr,isDATA,isSTORE,&paddr,&cca,isTARGET,isRAW))
497 StoreMemory(cca,AccessLength_WORD,value,0,paddr,vaddr,isRAW);
498 else
499 sim_io_error(sd,"Failed to write to monitor space 0x%s",pr_addr(vaddr));
500
501 /* The LSI MiniRISC PMON has its vectors at 0x200, not 0x500. */
502 vaddr -= 0x300;
503 if (AddressTranslation(vaddr,isDATA,isSTORE,&paddr,&cca,isTARGET,isRAW))
504 StoreMemory(cca,AccessLength_WORD,value,0,paddr,vaddr,isRAW);
505 else
506 sim_io_error(sd,"Failed to write to monitor space 0x%s",pr_addr(vaddr));
507 }
508 }
509
510 return sd;
511 }
512
513 #if defined(TRACE)
514 static void
515 open_trace(sd)
516 SIM_DESC sd;
517 {
518 tracefh = fopen(tracefile,"wb+");
519 if (tracefh == NULL)
520 {
521 sim_io_eprintf(sd,"Failed to create file \"%s\", writing trace information to stderr.\n",tracefile);
522 tracefh = stderr;
523 }
524 }
525 #endif /* TRACE */
526
527 void
528 sim_close (sd, quitting)
529 SIM_DESC sd;
530 int quitting;
531 {
532 #ifdef DEBUG
533 printf("DBG: sim_close: entered (quitting = %d)\n",quitting);
534 #endif
535
536 /* "quitting" is non-zero if we cannot hang on errors */
537
538 /* Ensure that any resources allocated through the callback
539 mechanism are released: */
540 sim_io_shutdown (sd);
541
542 #if defined(TRACE)
543 if (tracefh != NULL && tracefh != stderr)
544 fclose(tracefh);
545 tracefh = NULL;
546 STATE &= ~simTRACE;
547 #endif /* TRACE */
548
549 if (logfh != NULL && logfh != stdout && logfh != stderr)
550 fclose(logfh);
551 logfh = NULL;
552
553 if (STATE_MEMORY (sd) != NULL)
554 free(STATE_MEMORY (sd)); /* cfree not available on all hosts */
555 STATE_MEMORY (sd) = NULL;
556
557 return;
558 }
559
560
561 int
562 sim_write (sd,addr,buffer,size)
563 SIM_DESC sd;
564 SIM_ADDR addr;
565 unsigned char *buffer;
566 int size;
567 {
568 int index = size;
569 uword64 vaddr = (uword64)addr;
570
571 /* Return the number of bytes written, or zero if error. */
572 #ifdef DEBUG
573 sim_io_printf(sd,"sim_write(0x%s,buffer,%d);\n",pr_addr(addr),size);
574 #endif
575
576 /* We provide raw read and write routines, since we do not want to
577 count the GDB memory accesses in our statistics gathering. */
578
579 /* There is a lot of code duplication in the individual blocks
580 below, but the variables are declared locally to a block to give
581 the optimiser the best chance of improving the code. We have to
582 perform slow byte reads from the host memory, to ensure that we
583 get the data into the correct endianness for the (simulated)
584 target memory world. */
585
586 /* Mask count to get odd byte, odd halfword, and odd word out of the
587 way. We can then perform doubleword transfers to and from the
588 simulator memory for optimum performance. */
589 if (index && (index & 1)) {
590 uword64 paddr;
591 int cca;
592 if (AddressTranslation(vaddr,isDATA,isSTORE,&paddr,&cca,isTARGET,isRAW)) {
593 uword64 value = ((uword64)(*buffer++));
594 StoreMemory(cca,AccessLength_BYTE,value,0,paddr,vaddr,isRAW);
595 }
596 vaddr++;
597 index &= ~1; /* logical operations usually quicker than arithmetic on RISC systems */
598 }
599 if (index && (index & 2)) {
600 uword64 paddr;
601 int cca;
602 if (AddressTranslation(vaddr,isDATA,isSTORE,&paddr,&cca,isTARGET,isRAW)) {
603 uword64 value;
604 /* We need to perform the following magic to ensure that that
605 bytes are written into same byte positions in the target memory
606 world, regardless of the endianness of the host. */
607 if (BigEndianMem) {
608 value = ((uword64)(*buffer++) << 8);
609 value |= ((uword64)(*buffer++) << 0);
610 } else {
611 value = ((uword64)(*buffer++) << 0);
612 value |= ((uword64)(*buffer++) << 8);
613 }
614 StoreMemory(cca,AccessLength_HALFWORD,value,0,paddr,vaddr,isRAW);
615 }
616 vaddr += 2;
617 index &= ~2;
618 }
619 if (index && (index & 4)) {
620 uword64 paddr;
621 int cca;
622 if (AddressTranslation(vaddr,isDATA,isSTORE,&paddr,&cca,isTARGET,isRAW)) {
623 uword64 value;
624 if (BigEndianMem) {
625 value = ((uword64)(*buffer++) << 24);
626 value |= ((uword64)(*buffer++) << 16);
627 value |= ((uword64)(*buffer++) << 8);
628 value |= ((uword64)(*buffer++) << 0);
629 } else {
630 value = ((uword64)(*buffer++) << 0);
631 value |= ((uword64)(*buffer++) << 8);
632 value |= ((uword64)(*buffer++) << 16);
633 value |= ((uword64)(*buffer++) << 24);
634 }
635 StoreMemory(cca,AccessLength_WORD,value,0,paddr,vaddr,isRAW);
636 }
637 vaddr += 4;
638 index &= ~4;
639 }
640 for (;index; index -= 8) {
641 uword64 paddr;
642 int cca;
643 if (AddressTranslation(vaddr,isDATA,isSTORE,&paddr,&cca,isTARGET,isRAW)) {
644 uword64 value;
645 if (BigEndianMem) {
646 value = ((uword64)(*buffer++) << 56);
647 value |= ((uword64)(*buffer++) << 48);
648 value |= ((uword64)(*buffer++) << 40);
649 value |= ((uword64)(*buffer++) << 32);
650 value |= ((uword64)(*buffer++) << 24);
651 value |= ((uword64)(*buffer++) << 16);
652 value |= ((uword64)(*buffer++) << 8);
653 value |= ((uword64)(*buffer++) << 0);
654 } else {
655 value = ((uword64)(*buffer++) << 0);
656 value |= ((uword64)(*buffer++) << 8);
657 value |= ((uword64)(*buffer++) << 16);
658 value |= ((uword64)(*buffer++) << 24);
659 value |= ((uword64)(*buffer++) << 32);
660 value |= ((uword64)(*buffer++) << 40);
661 value |= ((uword64)(*buffer++) << 48);
662 value |= ((uword64)(*buffer++) << 56);
663 }
664 StoreMemory(cca,AccessLength_DOUBLEWORD,value,0,paddr,vaddr,isRAW);
665 }
666 vaddr += 8;
667 }
668
669 return(size);
670 }
671
672 int
673 sim_read (sd,addr,buffer,size)
674 SIM_DESC sd;
675 SIM_ADDR addr;
676 unsigned char *buffer;
677 int size;
678 {
679 int index;
680
681 /* Return the number of bytes read, or zero if error. */
682 #ifdef DEBUG
683 sim_io_printf(sd,"sim_read(0x%s,buffer,%d);\n",pr_addr(addr),size);
684 #endif /* DEBUG */
685
686 /* TODO: Perform same optimisation as the sim_write() code
687 above. NOTE: This will require a bit more work since we will need
688 to ensure that the source physical address is doubleword aligned
689 before, and then deal with trailing bytes. */
690 for (index = 0; (index < size); index++) {
691 uword64 vaddr,paddr,value;
692 int cca;
693 vaddr = (uword64)addr + index;
694 if (AddressTranslation(vaddr,isDATA,isLOAD,&paddr,&cca,isTARGET,isRAW)) {
695 LoadMemory(&value,NULL,cca,AccessLength_BYTE,paddr,vaddr,isDATA,isRAW);
696 buffer[index] = (unsigned char)(value&0xFF);
697 } else
698 break;
699 }
700
701 return(index);
702 }
703
704 void
705 sim_store_register (sd,rn,memory)
706 SIM_DESC sd;
707 int rn;
708 unsigned char *memory;
709 {
710 sim_cpu *cpu = STATE_CPU (sd, 0);
711 /* NOTE: gdb (the client) stores registers in target byte order
712 while the simulator uses host byte order */
713 #ifdef DEBUG
714 sim_io_printf(sd,"sim_store_register(%d,*memory=0x%s);\n",rn,pr_addr(*((SIM_ADDR *)memory)));
715 #endif /* DEBUG */
716
717 /* Unfortunately this suffers from the same problem as the register
718 numbering one. We need to know what the width of each logical
719 register number is for the architecture being simulated. */
720
721 if (cpu->register_widths[rn] == 0)
722 sim_io_eprintf(sd,"Invalid register width for %d (register store ignored)\n",rn);
723 /* start-sanitize-r5900 */
724 else if (rn == REGISTER_SA)
725 SA = T2H_8(*(uword64*)memory);
726 else if (rn > LAST_EMBED_REGNUM)
727 cpu->registers1[rn - LAST_EMBED_REGNUM - 1] = T2H_8(*(uword64*)memory);
728 /* end-sanitize-r5900 */
729 else if (cpu->register_widths[rn] == 32)
730 cpu->registers[rn] = T2H_4 (*(unsigned int*)memory);
731 else
732 cpu->registers[rn] = T2H_8 (*(uword64*)memory);
733
734 return;
735 }
736
737 void
738 sim_fetch_register (sd,rn,memory)
739 SIM_DESC sd;
740 int rn;
741 unsigned char *memory;
742 {
743 sim_cpu *cpu = STATE_CPU (sd, 0);
744 /* NOTE: gdb (the client) stores registers in target byte order
745 while the simulator uses host byte order */
746 #ifdef DEBUG
747 sim_io_printf(sd,"sim_fetch_register(%d=0x%s,mem) : place simulator registers into memory\n",rn,pr_addr(registers[rn]));
748 #endif /* DEBUG */
749
750 if (cpu->register_widths[rn] == 0)
751 sim_io_eprintf(sd,"Invalid register width for %d (register fetch ignored)\n",rn);
752 /* start-sanitize-r5900 */
753 else if (rn == REGISTER_SA)
754 *((uword64 *)memory) = H2T_8(SA);
755 else if (rn > LAST_EMBED_REGNUM)
756 *((uword64 *)memory) = H2T_8(cpu->registers1[rn - LAST_EMBED_REGNUM - 1]);
757 /* end-sanitize-r5900 */
758 else if (cpu->register_widths[rn] == 32)
759 *((unsigned int *)memory) = H2T_4 ((unsigned int)(cpu->registers[rn] & 0xFFFFFFFF));
760 else /* 64bit register */
761 *((uword64 *)memory) = H2T_8 (cpu->registers[rn]);
762
763 return;
764 }
765
766
767 void
768 sim_info (sd,verbose)
769 SIM_DESC sd;
770 int verbose;
771 {
772 /* Accessed from the GDB "info files" command: */
773 if (STATE_VERBOSE_P (sd) || verbose)
774 {
775
776 sim_io_printf (sd, "MIPS %d-bit %s endian simulator\n",
777 (PROCESSOR_64BIT ? 64 : 32),
778 (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN ? "Big" : "Little"));
779
780 sim_io_printf (sd, "0x%08X bytes of memory at 0x%s\n",
781 STATE_MEM_SIZE (sd),
782 pr_addr (STATE_MEM_BASE (sd)));
783
784 #if !defined(FASTSIM)
785 #if 0
786 /* at present this simulator executes one instruction per
787 simulator cycle. Consequently this data never changes */
788 if (instruction_fetch_overflow != 0)
789 sim_io_printf (sd, "Instruction fetches = 0x%08X%08X\n",
790 instruction_fetch_overflow, instruction_fetches);
791 else
792 sim_io_printf (sd, "Instruction fetches = %d\n", instruction_fetches);
793 #endif
794 /* It would be a useful feature, if when performing multi-cycle
795 simulations (rather than single-stepping) we keep the start and
796 end times of the execution, so that we can give a performance
797 figure for the simulator. */
798 #endif /* !FASTSIM */
799 sim_io_printf (sd, "Number of execution cycles = %ld\n",
800 (long) sim_events_time (sd));
801
802 /* print information pertaining to MIPS ISA and architecture being simulated */
803 /* things that may be interesting */
804 /* instructions executed - if available */
805 /* cycles executed - if available */
806 /* pipeline stalls - if available */
807 /* virtual time taken */
808 /* profiling size */
809 /* profiling frequency */
810 /* profile minpc */
811 /* profile maxpc */
812 }
813 }
814
815
816 SIM_RC
817 sim_create_inferior (sd, abfd, argv,env)
818 SIM_DESC sd;
819 struct _bfd *abfd;
820 char **argv;
821 char **env;
822 {
823
824 #ifdef DEBUG
825 printf("DBG: sim_create_inferior entered: start_address = 0x%s\n",
826 pr_addr(PC));
827 #endif /* DEBUG */
828
829 ColdReset(sd);
830 /* If we were providing a more complete I/O, co-processor or memory
831 simulation, we should perform any "device" initialisation at this
832 point. This can include pre-loading memory areas with particular
833 patterns (e.g. simulating ROM monitors). */
834
835 #if 1
836 if (abfd != NULL)
837 PC = (unsigned64) bfd_get_start_address(abfd);
838 else
839 PC = 0; /* ???? */
840 #else
841 /* TODO: Sort this properly. SIM_ADDR may already be a 64bit value: */
842 PC = SIGNEXTEND(bfd_get_start_address(abfd),32);
843 #endif
844
845 /* Prepare to execute the program to be simulated */
846 /* argv and env are NULL terminated lists of pointers */
847
848 if (argv || env) {
849 #if 0 /* def DEBUG */
850 sim_io_printf(sd,"sim_create_inferior() : passed arguments ignored\n");
851 {
852 char **cptr;
853 for (cptr = argv; (cptr && *cptr); cptr++)
854 printf("DBG: arg \"%s\"\n",*cptr);
855 }
856 #endif /* DEBUG */
857 /* We should really place the argv slot values into the argument
858 registers, and onto the stack as required. However, this
859 assumes that we have a stack defined, which is not necessarily
860 true at the moment. */
861 }
862
863 return SIM_RC_OK;
864 }
865
866 typedef enum {e_terminate,e_help,e_setmemsize,e_reset} e_cmds;
867
868 static struct t_sim_command {
869 e_cmds id;
870 const char *name;
871 const char *help;
872 } sim_commands[] = {
873 {e_help, "help", ": Show MIPS simulator private commands"},
874 {e_setmemsize,"set-memory-size","<n> : Specify amount of memory simulated"},
875 {e_reset, "reset-system", ": Reset the simulated processor"},
876 {e_terminate, NULL}
877 };
878
879 void
880 sim_do_command (sd,cmd)
881 SIM_DESC sd;
882 char *cmd;
883 {
884 struct t_sim_command *cptr;
885
886 if (!(cmd && *cmd != '\0'))
887 cmd = "help";
888
889 /* NOTE: Accessed from the GDB "sim" commmand: */
890 for (cptr = sim_commands; cptr && cptr->name; cptr++)
891 if (strncmp (cmd, cptr->name, strlen(cptr->name)) == 0)
892 {
893 cmd += strlen(cptr->name);
894 switch (cptr->id) {
895 case e_help: /* no arguments */
896 { /* no arguments */
897 struct t_sim_command *lptr;
898 sim_io_printf(sd,"List of MIPS simulator commands:\n");
899 for (lptr = sim_commands; lptr->name; lptr++)
900 sim_io_printf(sd,"%s %s\n",lptr->name,lptr->help);
901 sim_args_command (sd, "help");
902 }
903 break;
904
905 case e_setmemsize: /* memory size argument */
906 {
907 unsigned int newsize = (unsigned int)getnum(sd, cmd);
908 mips_size(sd, newsize);
909 }
910 break;
911
912 case e_reset: /* no arguments */
913 ColdReset(sd);
914 /* NOTE: See the comments in sim_open() relating to device
915 initialisation. */
916 break;
917
918 default:
919 sim_io_printf(sd,"FATAL: Matched \"%s\", but failed to match command id %d.\n",cmd,cptr->id);
920 break;
921 }
922 break;
923 }
924
925 if (!(cptr->name))
926 {
927 /* try for a common command when the sim specific lookup fails */
928 if (sim_args_command (sd, cmd) != SIM_RC_OK)
929 sim_io_printf(sd,"Error: \"%s\" is not a valid MIPS simulator command.\n",cmd);
930 }
931
932 return;
933 }
934
935 /*---------------------------------------------------------------------------*/
936 /* NOTE: The following routines do not seem to be used by GDB at the
937 moment. However, they may be useful to the standalone simulator
938 world. */
939
940
941 static void
942 mips_size(sd, newsize)
943 SIM_DESC sd;
944 int newsize;
945 {
946 char *new;
947 /* Used by "run", and internally, to set the simulated memory size */
948 if (newsize == 0) {
949 sim_io_printf(sd,"Zero not valid: Memory size still 0x%08X bytes\n",STATE_MEM_SIZE (sd));
950 return;
951 }
952 newsize = power2(newsize);
953 if (STATE_MEMORY (sd) == NULL)
954 new = (char *)calloc(64,(STATE_MEM_SIZE (sd) / 64));
955 else
956 new = (char *)realloc(STATE_MEMORY (sd),newsize);
957 if (new == NULL) {
958 if (STATE_MEMORY (sd) == NULL)
959 sim_io_error(sd,"Not enough VM for simulation memory of 0x%08X bytes",STATE_MEM_SIZE (sd));
960 else
961 sim_io_eprintf(sd,"Failed to resize memory (still 0x%08X bytes)\n",STATE_MEM_SIZE (sd));
962 } else {
963 STATE_MEM_SIZE (sd) = (unsigned)newsize;
964 STATE_MEMORY (sd) = new;
965 }
966 return;
967 }
968
969
970 /*---------------------------------------------------------------------------*/
971 /*-- Private simulator support interface ------------------------------------*/
972 /*---------------------------------------------------------------------------*/
973
974 /* Simple monitor interface (currently setup for the IDT and PMON monitors) */
975 static void
976 sim_monitor(sd,reason)
977 SIM_DESC sd;
978 unsigned int reason;
979 {
980 #ifdef DEBUG
981 printf("DBG: sim_monitor: entered (reason = %d)\n",reason);
982 #endif /* DEBUG */
983
984 /* The IDT monitor actually allows two instructions per vector
985 slot. However, the simulator currently causes a trap on each
986 individual instruction. We cheat, and lose the bottom bit. */
987 reason >>= 1;
988
989 /* The following callback functions are available, however the
990 monitor we are simulating does not make use of them: get_errno,
991 isatty, lseek, rename, system, time and unlink */
992 switch (reason) {
993 case 6: /* int open(char *path,int flags) */
994 {
995 uword64 paddr;
996 int cca;
997 if (AddressTranslation(A0,isDATA,isLOAD,&paddr,&cca,isHOST,isREAL))
998 V0 = sim_io_open(sd,(char *)((int)paddr),(int)A1);
999 else
1000 sim_io_error(sd,"Attempt to pass pointer that does not reference simulated memory");
1001 }
1002 break;
1003
1004 case 7: /* int read(int file,char *ptr,int len) */
1005 {
1006 uword64 paddr;
1007 int cca;
1008 if (AddressTranslation(A1,isDATA,isLOAD,&paddr,&cca,isHOST,isREAL))
1009 V0 = sim_io_read(sd,(int)A0,(char *)((int)paddr),(int)A2);
1010 else
1011 sim_io_error(sd,"Attempt to pass pointer that does not reference simulated memory");
1012 }
1013 break;
1014
1015 case 8: /* int write(int file,char *ptr,int len) */
1016 {
1017 uword64 paddr;
1018 int cca;
1019 if (AddressTranslation(A1,isDATA,isLOAD,&paddr,&cca,isHOST,isREAL))
1020 V0 = sim_io_write(sd,(int)A0,(const char *)((int)paddr),(int)A2);
1021 else
1022 sim_io_error(sd,"Attempt to pass pointer that does not reference simulated memory");
1023 }
1024 break;
1025
1026 case 10: /* int close(int file) */
1027 V0 = sim_io_close(sd,(int)A0);
1028 break;
1029
1030 case 11: /* char inbyte(void) */
1031 {
1032 char tmp;
1033 if (sim_io_read_stdin(sd,&tmp,sizeof(char)) != sizeof(char)) {
1034 sim_io_error(sd,"Invalid return from character read");
1035 V0 = (ut_reg)-1;
1036 }
1037 else
1038 V0 = (ut_reg)tmp;
1039 }
1040 break;
1041
1042 case 12: /* void outbyte(char chr) : write a byte to "stdout" */
1043 {
1044 char tmp = (char)(A0 & 0xFF);
1045 sim_io_write_stdout(sd,&tmp,sizeof(char));
1046 }
1047 break;
1048
1049 case 17: /* void _exit() */
1050 sim_io_eprintf(sd,"sim_monitor(17): _exit(int reason) to be coded\n");
1051 sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, NULL_CIA, sim_exited,
1052 (unsigned int)(A0 & 0xFFFFFFFF));
1053 break;
1054
1055 case 28 : /* PMON flush_cache */
1056 break;
1057
1058 case 55: /* void get_mem_info(unsigned int *ptr) */
1059 /* in: A0 = pointer to three word memory location */
1060 /* out: [A0 + 0] = size */
1061 /* [A0 + 4] = instruction cache size */
1062 /* [A0 + 8] = data cache size */
1063 {
1064 uword64 vaddr = A0;
1065 uword64 paddr, value;
1066 int cca;
1067 int failed = 0;
1068
1069 /* NOTE: We use RAW memory writes here, but since we are not
1070 gathering statistics for the monitor calls we are simulating,
1071 it is not an issue. */
1072
1073 /* Memory size */
1074 if (AddressTranslation(vaddr,isDATA,isSTORE,&paddr,&cca,isTARGET,isREAL)) {
1075 value = (uword64)STATE_MEM_SIZE (sd);
1076 StoreMemory(cca,AccessLength_WORD,value,0,paddr,vaddr,isRAW);
1077 /* We re-do the address translations, in-case the block
1078 overlaps a memory boundary: */
1079 value = 0;
1080 vaddr += (AccessLength_WORD + 1);
1081 if (AddressTranslation(vaddr,isDATA,isSTORE,&paddr,&cca,isTARGET,isREAL)) {
1082 StoreMemory(cca,AccessLength_WORD,0,value,paddr,vaddr,isRAW);
1083 vaddr += (AccessLength_WORD + 1);
1084 if (AddressTranslation(vaddr,isDATA,isSTORE,&paddr,&cca,isTARGET,isREAL))
1085 StoreMemory(cca,AccessLength_WORD,value,0,paddr,vaddr,isRAW);
1086 else
1087 failed = -1;
1088 } else
1089 failed = -1;
1090 } else
1091 failed = -1;
1092
1093 if (failed)
1094 sim_io_error(sd,"Invalid pointer passed into monitor call");
1095 }
1096 break;
1097
1098 case 158 : /* PMON printf */
1099 /* in: A0 = pointer to format string */
1100 /* A1 = optional argument 1 */
1101 /* A2 = optional argument 2 */
1102 /* A3 = optional argument 3 */
1103 /* out: void */
1104 /* The following is based on the PMON printf source */
1105 {
1106 uword64 paddr;
1107 int cca;
1108 /* This isn't the quickest way, since we call the host print
1109 routine for every character almost. But it does avoid
1110 having to allocate and manage a temporary string buffer. */
1111 if (AddressTranslation(A0,isDATA,isLOAD,&paddr,&cca,isHOST,isREAL)) {
1112 char *s = (char *)((int)paddr);
1113 signed_word *ap = &A1; /* 1st argument */
1114 /* TODO: Include check that we only use three arguments (A1, A2 and A3) */
1115 for (; *s;) {
1116 if (*s == '%') {
1117 char tmp[40];
1118 enum {FMT_RJUST, FMT_LJUST, FMT_RJUST0, FMT_CENTER} fmt = FMT_RJUST;
1119 int width = 0, trunc = 0, haddot = 0, longlong = 0;
1120 s++;
1121 for (; *s; s++) {
1122 if (strchr ("dobxXulscefg%", *s))
1123 break;
1124 else if (*s == '-')
1125 fmt = FMT_LJUST;
1126 else if (*s == '0')
1127 fmt = FMT_RJUST0;
1128 else if (*s == '~')
1129 fmt = FMT_CENTER;
1130 else if (*s == '*') {
1131 if (haddot)
1132 trunc = (int)*ap++;
1133 else
1134 width = (int)*ap++;
1135 } else if (*s >= '1' && *s <= '9') {
1136 char *t;
1137 unsigned int n;
1138 for (t = s; isdigit (*s); s++);
1139 strncpy (tmp, t, s - t);
1140 tmp[s - t] = '\0';
1141 n = (unsigned int)strtol(tmp,NULL,10);
1142 if (haddot)
1143 trunc = n;
1144 else
1145 width = n;
1146 s--;
1147 } else if (*s == '.')
1148 haddot = 1;
1149 }
1150 if (*s == '%') {
1151 sim_io_printf(sd,"%%");
1152 } else if (*s == 's') {
1153 if ((int)*ap != 0) {
1154 if (AddressTranslation(*ap++,isDATA,isLOAD,&paddr,&cca,isHOST,isREAL)) {
1155 char *p = (char *)((int)paddr);;
1156 sim_io_printf(sd,p);
1157 } else {
1158 ap++;
1159 sim_io_error(sd,"Attempt to pass pointer that does not reference simulated memory");
1160 }
1161 }
1162 else
1163 sim_io_printf(sd,"(null)");
1164 } else if (*s == 'c') {
1165 int n = (int)*ap++;
1166 sim_io_printf(sd,"%c",n);
1167 } else {
1168 if (*s == 'l') {
1169 if (*++s == 'l') {
1170 longlong = 1;
1171 ++s;
1172 }
1173 }
1174 if (strchr ("dobxXu", *s)) {
1175 word64 lv = (word64) *ap++;
1176 if (*s == 'b')
1177 sim_io_printf(sd,"<binary not supported>");
1178 else {
1179 sprintf(tmp,"%%%s%c",longlong ? "ll" : "",*s);
1180 if (longlong)
1181 sim_io_printf(sd,tmp,lv);
1182 else
1183 sim_io_printf(sd,tmp,(int)lv);
1184 }
1185 } else if (strchr ("eEfgG", *s)) {
1186 #ifdef _MSC_VER /* MSVC version 2.x can't convert from uword64 directly */
1187 double dbl = (double)((word64)*ap++);
1188 #else
1189 double dbl = (double)*ap++;
1190 #endif
1191 sprintf(tmp,"%%%d.%d%c",width,trunc,*s);
1192 sim_io_printf(sd,tmp,dbl);
1193 trunc = 0;
1194 }
1195 }
1196 s++;
1197 } else
1198 sim_io_printf(sd,"%c",*s++);
1199 }
1200 } else
1201 sim_io_error(sd,"Attempt to pass pointer that does not reference simulated memory");
1202 }
1203 break;
1204
1205 default:
1206 sim_io_eprintf(sd,"TODO: sim_monitor(%d) : PC = 0x%s\n",reason,pr_addr(IPC));
1207 sim_io_eprintf(sd,"(Arguments : A0 = 0x%s : A1 = 0x%s : A2 = 0x%s : A3 = 0x%s)\n",pr_addr(A0),pr_addr(A1),pr_addr(A2),pr_addr(A3));
1208 break;
1209 }
1210 return;
1211 }
1212
1213 /* Store a word into memory. */
1214
1215 static void
1216 store_word (sd, vaddr, val)
1217 SIM_DESC sd;
1218 uword64 vaddr;
1219 t_reg val;
1220 {
1221 uword64 paddr;
1222 int uncached;
1223
1224 if ((vaddr & 3) != 0)
1225 SignalExceptionAddressStore ();
1226 else
1227 {
1228 if (AddressTranslation (vaddr, isDATA, isSTORE, &paddr, &uncached,
1229 isTARGET, isREAL))
1230 {
1231 const uword64 mask = 7;
1232 uword64 memval;
1233 unsigned int byte;
1234
1235 paddr = (paddr & ~mask) | ((paddr & mask) ^ (ReverseEndian << 2));
1236 byte = (vaddr & mask) ^ (BigEndianCPU << 2);
1237 memval = ((uword64) val) << (8 * byte);
1238 StoreMemory (uncached, AccessLength_WORD, memval, 0, paddr, vaddr,
1239 isREAL);
1240 }
1241 }
1242 }
1243
1244 /* Load a word from memory. */
1245
1246 static t_reg
1247 load_word (sd, vaddr)
1248 SIM_DESC sd;
1249 uword64 vaddr;
1250 {
1251 if ((vaddr & 3) != 0)
1252 SignalExceptionAddressLoad ();
1253 else
1254 {
1255 uword64 paddr;
1256 int uncached;
1257
1258 if (AddressTranslation (vaddr, isDATA, isLOAD, &paddr, &uncached,
1259 isTARGET, isREAL))
1260 {
1261 const uword64 mask = 0x7;
1262 const unsigned int reverse = ReverseEndian ? 1 : 0;
1263 const unsigned int bigend = BigEndianCPU ? 1 : 0;
1264 uword64 memval;
1265 unsigned int byte;
1266
1267 paddr = (paddr & ~mask) | ((paddr & mask) ^ (reverse << 2));
1268 LoadMemory (&memval,NULL,uncached, AccessLength_WORD, paddr, vaddr,
1269 isDATA, isREAL);
1270 byte = (vaddr & mask) ^ (bigend << 2);
1271 return SIGNEXTEND (((memval >> (8 * byte)) & 0xffffffff), 32);
1272 }
1273 }
1274
1275 return 0;
1276 }
1277
1278 /* Simulate the mips16 entry and exit pseudo-instructions. These
1279 would normally be handled by the reserved instruction exception
1280 code, but for ease of simulation we just handle them directly. */
1281
1282 static void
1283 mips16_entry (sd,insn)
1284 SIM_DESC sd;
1285 unsigned int insn;
1286 {
1287 int aregs, sregs, rreg;
1288
1289 #ifdef DEBUG
1290 printf("DBG: mips16_entry: entered (insn = 0x%08X)\n",insn);
1291 #endif /* DEBUG */
1292
1293 aregs = (insn & 0x700) >> 8;
1294 sregs = (insn & 0x0c0) >> 6;
1295 rreg = (insn & 0x020) >> 5;
1296
1297 /* This should be checked by the caller. */
1298 if (sregs == 3)
1299 abort ();
1300
1301 if (aregs < 5)
1302 {
1303 int i;
1304 t_reg tsp;
1305
1306 /* This is the entry pseudo-instruction. */
1307
1308 for (i = 0; i < aregs; i++)
1309 store_word ((uword64) (SP + 4 * i), GPR[i + 4]);
1310
1311 tsp = SP;
1312 SP -= 32;
1313
1314 if (rreg)
1315 {
1316 tsp -= 4;
1317 store_word ((uword64) tsp, RA);
1318 }
1319
1320 for (i = 0; i < sregs; i++)
1321 {
1322 tsp -= 4;
1323 store_word ((uword64) tsp, GPR[16 + i]);
1324 }
1325 }
1326 else
1327 {
1328 int i;
1329 t_reg tsp;
1330
1331 /* This is the exit pseudo-instruction. */
1332
1333 tsp = SP + 32;
1334
1335 if (rreg)
1336 {
1337 tsp -= 4;
1338 RA = load_word ((uword64) tsp);
1339 }
1340
1341 for (i = 0; i < sregs; i++)
1342 {
1343 tsp -= 4;
1344 GPR[i + 16] = load_word ((uword64) tsp);
1345 }
1346
1347 SP += 32;
1348
1349 #if defined(HASFPU)
1350 if (aregs == 5)
1351 {
1352 FGR[0] = WORD64LO (GPR[4]);
1353 FPR_STATE[0] = fmt_uninterpreted;
1354 }
1355 else if (aregs == 6)
1356 {
1357 FGR[0] = WORD64LO (GPR[5]);
1358 FGR[1] = WORD64LO (GPR[4]);
1359 FPR_STATE[0] = fmt_uninterpreted;
1360 FPR_STATE[1] = fmt_uninterpreted;
1361 }
1362 #endif /* defined(HASFPU) */
1363
1364 PC = RA;
1365 }
1366 }
1367
1368 static unsigned int
1369 power2(value)
1370 unsigned int value;
1371 {
1372 int loop,tmp;
1373
1374 /* Round *UP* to the nearest power-of-2 if not already one */
1375 if (value != (value & ~(value - 1))) {
1376 for (tmp = value, loop = 0; (tmp != 0); loop++)
1377 tmp >>= 1;
1378 value = (1 << loop);
1379 }
1380
1381 return(value);
1382 }
1383
1384 static long
1385 getnum(sd,value)
1386 SIM_DESC sd;
1387 char *value;
1388 {
1389 long num;
1390 char *end;
1391
1392 num = strtol(value,&end,10);
1393 if (end == value)
1394 sim_io_printf(sd,"Warning: Invalid number \"%s\" ignored, using zero\n",value);
1395 else {
1396 if (*end && ((tolower(*end) == 'k') || (tolower(*end) == 'm'))) {
1397 if (tolower(*end) == 'k')
1398 num *= (1 << 10);
1399 else
1400 num *= (1 << 20);
1401 end++;
1402 }
1403 if (*end)
1404 sim_io_printf(sd,"Warning: Spurious characters \"%s\" at end of number ignored\n",end);
1405 }
1406
1407 return(num);
1408 }
1409
1410 /*-- trace support ----------------------------------------------------------*/
1411
1412 /* The TRACE support is provided (if required) in the memory accessing
1413 routines. Since we are also providing the architecture specific
1414 features, the architecture simulation code can also deal with
1415 notifying the TRACE world of cache flushes, etc. Similarly we do
1416 not need to provide profiling support in the simulator engine,
1417 since we can sample in the instruction fetch control loop. By
1418 defining the TRACE manifest, we add tracing as a run-time
1419 option. */
1420
1421 #if defined(TRACE)
1422 /* Tracing by default produces "din" format (as required by
1423 dineroIII). Each line of such a trace file *MUST* have a din label
1424 and address field. The rest of the line is ignored, so comments can
1425 be included if desired. The first field is the label which must be
1426 one of the following values:
1427
1428 0 read data
1429 1 write data
1430 2 instruction fetch
1431 3 escape record (treated as unknown access type)
1432 4 escape record (causes cache flush)
1433
1434 The address field is a 32bit (lower-case) hexadecimal address
1435 value. The address should *NOT* be preceded by "0x".
1436
1437 The size of the memory transfer is not important when dealing with
1438 cache lines (as long as no more than a cache line can be
1439 transferred in a single operation :-), however more information
1440 could be given following the dineroIII requirement to allow more
1441 complete memory and cache simulators to provide better
1442 results. i.e. the University of Pisa has a cache simulator that can
1443 also take bus size and speed as (variable) inputs to calculate
1444 complete system performance (a much more useful ability when trying
1445 to construct an end product, rather than a processor). They
1446 currently have an ARM version of their tool called ChARM. */
1447
1448
1449 static
1450 void dotrace(SIM_DESC sd,FILE *tracefh,int type,SIM_ADDR address,int width,char *comment,...)
1451 {
1452 if (STATE & simTRACE) {
1453 va_list ap;
1454 fprintf(tracefh,"%d %s ; width %d ; ",
1455 type,
1456 pr_addr(address),
1457 width);
1458 va_start(ap,comment);
1459 vfprintf(tracefh,comment,ap);
1460 va_end(ap);
1461 fprintf(tracefh,"\n");
1462 }
1463 /* NOTE: Since the "din" format will only accept 32bit addresses, and
1464 we may be generating 64bit ones, we should put the hi-32bits of the
1465 address into the comment field. */
1466
1467 /* TODO: Provide a buffer for the trace lines. We can then avoid
1468 performing writes until the buffer is filled, or the file is
1469 being closed. */
1470
1471 /* NOTE: We could consider adding a comment field to the "din" file
1472 produced using type 3 markers (unknown access). This would then
1473 allow information about the program that the "din" is for, and
1474 the MIPs world that was being simulated, to be placed into the
1475 trace file. */
1476
1477 return;
1478 }
1479 #endif /* TRACE */
1480
1481 /*---------------------------------------------------------------------------*/
1482 /*-- simulator engine -------------------------------------------------------*/
1483 /*---------------------------------------------------------------------------*/
1484
1485 static void
1486 ColdReset(sd)
1487 SIM_DESC sd;
1488 {
1489 /* RESET: Fixed PC address: */
1490 PC = (((uword64)0xFFFFFFFF<<32) | 0xBFC00000);
1491 /* The reset vector address is in the unmapped, uncached memory space. */
1492
1493 SR &= ~(status_SR | status_TS | status_RP);
1494 SR |= (status_ERL | status_BEV);
1495
1496 #if defined(HASFPU) && (GPRLEN == (64))
1497 /* Cheat and allow access to the complete register set immediately: */
1498 SR |= status_FR; /* 64bit registers */
1499 #endif /* HASFPU and 64bit FP registers */
1500
1501 /* Ensure that any instructions with pending register updates are
1502 cleared: */
1503 {
1504 int loop;
1505 for (loop = 0; (loop < PSLOTS); loop++)
1506 PENDING_SLOT_REG[loop] = (LAST_EMBED_REGNUM + 1);
1507 PENDING_IN = PENDING_OUT = PENDING_TOTAL = 0;
1508 }
1509
1510 #if defined(HASFPU)
1511 /* Initialise the FPU registers to the unknown state */
1512 {
1513 int rn;
1514 for (rn = 0; (rn < 32); rn++)
1515 FPR_STATE[rn] = fmt_uninterpreted;
1516 }
1517 #endif /* HASFPU */
1518
1519 return;
1520 }
1521
1522 /* Description from page A-22 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1523 /* Translate a virtual address to a physical address and cache
1524 coherence algorithm describing the mechanism used to resolve the
1525 memory reference. Given the virtual address vAddr, and whether the
1526 reference is to Instructions ot Data (IorD), find the corresponding
1527 physical address (pAddr) and the cache coherence algorithm (CCA)
1528 used to resolve the reference. If the virtual address is in one of
1529 the unmapped address spaces the physical address and the CCA are
1530 determined directly by the virtual address. If the virtual address
1531 is in one of the mapped address spaces then the TLB is used to
1532 determine the physical address and access type; if the required
1533 translation is not present in the TLB or the desired access is not
1534 permitted the function fails and an exception is taken.
1535
1536 NOTE: This function is extended to return an exception state. This,
1537 along with the exception generation is used to notify whether a
1538 valid address translation occured */
1539
1540 int
1541 address_translation(sd,vAddr,IorD,LorS,pAddr,CCA,host,raw)
1542 SIM_DESC sd;
1543 uword64 vAddr;
1544 int IorD;
1545 int LorS;
1546 uword64 *pAddr;
1547 int *CCA;
1548 int host;
1549 int raw;
1550 {
1551 int res = -1; /* TRUE : Assume good return */
1552
1553 #ifdef DEBUG
1554 sim_io_printf(sd,"AddressTranslation(0x%s,%s,%s,...);\n",pr_addr(vAddr),(IorD ? "isDATA" : "isINSTRUCTION"),(LorS ? "iSTORE" : "isLOAD"));
1555 #endif
1556
1557 /* Check that the address is valid for this memory model */
1558
1559 /* For a simple (flat) memory model, we simply pass virtual
1560 addressess through (mostly) unchanged. */
1561 vAddr &= 0xFFFFFFFF;
1562
1563 /* Treat the kernel memory spaces identically for the moment: */
1564 if ((STATE_MEM_BASE (sd) == K1BASE) && (vAddr >= K0BASE) && (vAddr < (K0BASE + K0SIZE)))
1565 vAddr += (K1BASE - K0BASE);
1566
1567 /* Also assume that the K1BASE memory wraps. This is required to
1568 allow the PMON run-time __sizemem() routine to function (without
1569 having to provide exception simulation). NOTE: A kludge to work
1570 around the fact that the monitor memory is currently held in the
1571 K1BASE space. */
1572 if (((vAddr < monitor_base) || (vAddr >= (monitor_base + monitor_size))) && (vAddr >= K1BASE && vAddr < (K1BASE + K1SIZE)))
1573 vAddr = (K1BASE | (vAddr & (STATE_MEM_SIZE (sd) - 1)));
1574
1575 *pAddr = vAddr; /* default for isTARGET */
1576 *CCA = Uncached; /* not used for isHOST */
1577
1578 /* NOTE: This is a duplicate of the code that appears in the
1579 LoadMemory and StoreMemory functions. They should be merged into
1580 a single function (that can be in-lined if required). */
1581 if ((vAddr >= STATE_MEM_BASE (sd)) && (vAddr < (STATE_MEM_BASE (sd) + STATE_MEM_SIZE (sd)))) {
1582 if (host)
1583 *pAddr = (int)&STATE_MEMORY (sd)[((unsigned int)(vAddr - STATE_MEM_BASE (sd)) & (STATE_MEM_SIZE (sd) - 1))];
1584 } else if ((vAddr >= monitor_base) && (vAddr < (monitor_base + monitor_size))) {
1585 if (host)
1586 *pAddr = (int)&monitor[((unsigned int)(vAddr - monitor_base) & (monitor_size - 1))];
1587 } else {
1588 #ifdef DEBUG
1589 sim_io_eprintf(sd,"Failed: AddressTranslation(0x%s,%s,%s,...) IPC = 0x%s\n",pr_addr(vAddr),(IorD ? "isDATA" : "isINSTRUCTION"),(LorS ? "isSTORE" : "isLOAD"),pr_addr(IPC));
1590 #endif /* DEBUG */
1591 res = 0; /* AddressTranslation has failed */
1592 *pAddr = (SIM_ADDR)-1;
1593 if (!raw) /* only generate exceptions on real memory transfers */
1594 if (LorS == isSTORE)
1595 SignalExceptionAddressStore ();
1596 else
1597 SignalExceptionAddressLoad ();
1598 #ifdef DEBUG
1599 else
1600 /* This is a normal occurance during gdb operation, for instance trying
1601 to print parameters at function start before they have been setup,
1602 and hence we should not print a warning except when debugging the
1603 simulator. */
1604 sim_io_eprintf(sd,"AddressTranslation for %s %s from 0x%s failed\n",(IorD ? "data" : "instruction"),(LorS ? "store" : "load"),pr_addr(vAddr));
1605 #endif
1606 }
1607
1608 return(res);
1609 }
1610
1611 /* Description from page A-23 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1612 /* Prefetch data from memory. Prefetch is an advisory instruction for
1613 which an implementation specific action is taken. The action taken
1614 may increase performance, but must not change the meaning of the
1615 program, or alter architecturally-visible state. */
1616
1617 void
1618 prefetch(sd,CCA,pAddr,vAddr,DATA,hint)
1619 SIM_DESC sd;
1620 int CCA;
1621 uword64 pAddr;
1622 uword64 vAddr;
1623 int DATA;
1624 int hint;
1625 {
1626 #ifdef DEBUG
1627 sim_io_printf(sd,"Prefetch(%d,0x%s,0x%s,%d,%d);\n",CCA,pr_addr(pAddr),pr_addr(vAddr),DATA,hint);
1628 #endif /* DEBUG */
1629
1630 /* For our simple memory model we do nothing */
1631 return;
1632 }
1633
1634 /* Description from page A-22 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1635 /* Load a value from memory. Use the cache and main memory as
1636 specified in the Cache Coherence Algorithm (CCA) and the sort of
1637 access (IorD) to find the contents of AccessLength memory bytes
1638 starting at physical location pAddr. The data is returned in the
1639 fixed width naturally-aligned memory element (MemElem). The
1640 low-order two (or three) bits of the address and the AccessLength
1641 indicate which of the bytes within MemElem needs to be given to the
1642 processor. If the memory access type of the reference is uncached
1643 then only the referenced bytes are read from memory and valid
1644 within the memory element. If the access type is cached, and the
1645 data is not present in cache, an implementation specific size and
1646 alignment block of memory is read and loaded into the cache to
1647 satisfy a load reference. At a minimum, the block is the entire
1648 memory element. */
1649 void
1650 load_memory(sd,memvalp,memval1p,CCA,AccessLength,pAddr,vAddr,IorD,raw)
1651 SIM_DESC sd;
1652 uword64* memvalp;
1653 uword64* memval1p;
1654 int CCA;
1655 int AccessLength;
1656 uword64 pAddr;
1657 uword64 vAddr;
1658 int IorD;
1659 int raw;
1660 {
1661 uword64 value = 0;
1662 uword64 value1 = 0;
1663
1664 #ifdef DEBUG
1665 if (STATE_MEMORY (sd) == NULL)
1666 sim_io_printf(sd,"DBG: LoadMemory(%p,%p,%d,%d,0x%s,0x%s,%s,%s)\n",memvalp,memval1p,CCA,AccessLength,pr_addr(pAddr),pr_addr(vAddr),(IorD ? "isDATA" : "isINSTRUCTION"),(raw ? "isRAW" : "isREAL"));
1667 #endif /* DEBUG */
1668
1669 #if defined(WARN_MEM)
1670 if (CCA != uncached)
1671 sim_io_eprintf(sd,"LoadMemory CCA (%d) is not uncached (currently all accesses treated as cached)\n",CCA);
1672
1673 if (((pAddr & LOADDRMASK) + AccessLength) > LOADDRMASK) {
1674 /* In reality this should be a Bus Error */
1675 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));
1676 }
1677 #endif /* WARN_MEM */
1678
1679 /* Decide which physical memory locations are being dealt with. At
1680 this point we should be able to split the pAddr bits into the
1681 relevant address map being simulated. If the "raw" variable is
1682 set, the memory read being performed should *NOT* update any I/O
1683 state or affect the CPU state. This also includes avoiding
1684 affecting statistics gathering. */
1685
1686 /* If instruction fetch then we need to check that the two lo-order
1687 bits are zero, otherwise raise a InstructionFetch exception: */
1688 if ((IorD == isINSTRUCTION)
1689 && ((pAddr & 0x3) != 0)
1690 && (((pAddr & 0x1) != 0) || ((vAddr & 0x1) == 0)))
1691 SignalExceptionInstructionFetch ();
1692 else {
1693 unsigned int index = 0;
1694 unsigned char *mem = NULL;
1695
1696 #if defined(TRACE)
1697 if (!raw)
1698 dotrace(sd,tracefh,((IorD == isDATA) ? 0 : 2),(unsigned int)(pAddr&0xFFFFFFFF),(AccessLength + 1),"load%s",((IorD == isDATA) ? "" : " instruction"));
1699 #endif /* TRACE */
1700
1701 /* NOTE: Quicker methods of decoding the address space can be used
1702 when a real memory map is being simulated (i.e. using hi-order
1703 address bits to select device). */
1704 if ((pAddr >= STATE_MEM_BASE (sd)) && (pAddr < (STATE_MEM_BASE (sd) + STATE_MEM_SIZE (sd)))) {
1705 index = ((unsigned int)(pAddr - STATE_MEM_BASE (sd)) & (STATE_MEM_SIZE (sd) - 1));
1706 mem = STATE_MEMORY (sd);
1707 } else if ((pAddr >= monitor_base) && (pAddr < (monitor_base + monitor_size))) {
1708 index = ((unsigned int)(pAddr - monitor_base) & (monitor_size - 1));
1709 mem = monitor;
1710 }
1711 if (mem == NULL)
1712 sim_io_error(sd,"Simulator memory not found for physical address 0x%s\n",pr_addr(pAddr));
1713 else {
1714 /* If we obtained the endianness of the host, and it is the same
1715 as the target memory system we can optimise the memory
1716 accesses. However, without that information we must perform
1717 slow transfer, and hope that the compiler optimisation will
1718 merge successive loads. */
1719
1720 /* In reality we should always be loading a doubleword value (or
1721 word value in 32bit memory worlds). The external code then
1722 extracts the required bytes. However, to keep performance
1723 high we only load the required bytes into the relevant
1724 slots. */
1725 if (BigEndianMem)
1726 switch (AccessLength) { /* big-endian memory */
1727 case AccessLength_QUADWORD :
1728 value1 |= ((uword64)mem[index++] << 56);
1729 case 14: /* AccessLength is one less than datalen */
1730 value1 |= ((uword64)mem[index++] << 48);
1731 case 13:
1732 value1 |= ((uword64)mem[index++] << 40);
1733 case 12:
1734 value1 |= ((uword64)mem[index++] << 32);
1735 case 11:
1736 value1 |= ((unsigned int)mem[index++] << 24);
1737 case 10:
1738 value1 |= ((unsigned int)mem[index++] << 16);
1739 case 9:
1740 value1 |= ((unsigned int)mem[index++] << 8);
1741 case 8:
1742 value1 |= mem[index];
1743
1744 case AccessLength_DOUBLEWORD :
1745 value |= ((uword64)mem[index++] << 56);
1746 case AccessLength_SEPTIBYTE :
1747 value |= ((uword64)mem[index++] << 48);
1748 case AccessLength_SEXTIBYTE :
1749 value |= ((uword64)mem[index++] << 40);
1750 case AccessLength_QUINTIBYTE :
1751 value |= ((uword64)mem[index++] << 32);
1752 case AccessLength_WORD :
1753 value |= ((unsigned int)mem[index++] << 24);
1754 case AccessLength_TRIPLEBYTE :
1755 value |= ((unsigned int)mem[index++] << 16);
1756 case AccessLength_HALFWORD :
1757 value |= ((unsigned int)mem[index++] << 8);
1758 case AccessLength_BYTE :
1759 value |= mem[index];
1760 break;
1761 }
1762 else {
1763 index += (AccessLength + 1);
1764 switch (AccessLength) { /* little-endian memory */
1765 case AccessLength_QUADWORD :
1766 value1 |= ((uword64)mem[--index] << 56);
1767 case 14: /* AccessLength is one less than datalen */
1768 value1 |= ((uword64)mem[--index] << 48);
1769 case 13:
1770 value1 |= ((uword64)mem[--index] << 40);
1771 case 12:
1772 value1 |= ((uword64)mem[--index] << 32);
1773 case 11:
1774 value1 |= ((uword64)mem[--index] << 24);
1775 case 10:
1776 value1 |= ((uword64)mem[--index] << 16);
1777 case 9:
1778 value1 |= ((uword64)mem[--index] << 8);
1779 case 8:
1780 value1 |= ((uword64)mem[--index] << 0);
1781
1782 case AccessLength_DOUBLEWORD :
1783 value |= ((uword64)mem[--index] << 56);
1784 case AccessLength_SEPTIBYTE :
1785 value |= ((uword64)mem[--index] << 48);
1786 case AccessLength_SEXTIBYTE :
1787 value |= ((uword64)mem[--index] << 40);
1788 case AccessLength_QUINTIBYTE :
1789 value |= ((uword64)mem[--index] << 32);
1790 case AccessLength_WORD :
1791 value |= ((uword64)mem[--index] << 24);
1792 case AccessLength_TRIPLEBYTE :
1793 value |= ((uword64)mem[--index] << 16);
1794 case AccessLength_HALFWORD :
1795 value |= ((uword64)mem[--index] << 8);
1796 case AccessLength_BYTE :
1797 value |= ((uword64)mem[--index] << 0);
1798 break;
1799 }
1800 }
1801
1802 #ifdef DEBUG
1803 printf("DBG: LoadMemory() : (offset %d) : value = 0x%s%s\n",
1804 (int)(pAddr & LOADDRMASK),pr_uword64(value1),pr_uword64(value));
1805 #endif /* DEBUG */
1806
1807 /* TODO: We could try and avoid the shifts when dealing with raw
1808 memory accesses. This would mean updating the LoadMemory and
1809 StoreMemory routines to avoid shifting the data before
1810 returning or using it. */
1811 if (AccessLength <= AccessLength_DOUBLEWORD) {
1812 if (!raw) { /* do nothing for raw accessess */
1813 if (BigEndianMem)
1814 value <<= (((7 - (pAddr & LOADDRMASK)) - AccessLength) * 8);
1815 else /* little-endian only needs to be shifted up to the correct byte offset */
1816 value <<= ((pAddr & LOADDRMASK) * 8);
1817 }
1818 }
1819
1820 #ifdef DEBUG
1821 printf("DBG: LoadMemory() : shifted value = 0x%s%s\n",
1822 pr_uword64(value1),pr_uword64(value));
1823 #endif /* DEBUG */
1824 }
1825 }
1826
1827 *memvalp = value;
1828 if (memval1p) *memval1p = value1;
1829 }
1830
1831
1832 /* Description from page A-23 of the "MIPS IV Instruction Set" manual
1833 (revision 3.1) */
1834 /* Store a value to memory. The specified data is stored into the
1835 physical location pAddr using the memory hierarchy (data caches and
1836 main memory) as specified by the Cache Coherence Algorithm
1837 (CCA). The MemElem contains the data for an aligned, fixed-width
1838 memory element (word for 32-bit processors, doubleword for 64-bit
1839 processors), though only the bytes that will actually be stored to
1840 memory need to be valid. The low-order two (or three) bits of pAddr
1841 and the AccessLength field indicates which of the bytes within the
1842 MemElem data should actually be stored; only these bytes in memory
1843 will be changed. */
1844
1845 void
1846 store_memory(sd,CCA,AccessLength,MemElem,MemElem1,pAddr,vAddr,raw)
1847 SIM_DESC sd;
1848 int CCA;
1849 int AccessLength;
1850 uword64 MemElem;
1851 uword64 MemElem1; /* High order 64 bits */
1852 uword64 pAddr;
1853 uword64 vAddr;
1854 int raw;
1855 {
1856 #ifdef DEBUG
1857 sim_io_printf(sd,"DBG: StoreMemory(%d,%d,0x%s,0x%s,0x%s,0x%s,%s)\n",CCA,AccessLength,pr_uword64(MemElem),pr_uword64(MemElem1),pr_addr(pAddr),pr_addr(vAddr),(raw ? "isRAW" : "isREAL"));
1858 #endif /* DEBUG */
1859
1860 #if defined(WARN_MEM)
1861 if (CCA != uncached)
1862 sim_io_eprintf(sd,"StoreMemory CCA (%d) is not uncached (currently all accesses treated as cached)\n",CCA);
1863
1864 if (((pAddr & LOADDRMASK) + AccessLength) > LOADDRMASK)
1865 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));
1866 #endif /* WARN_MEM */
1867
1868 #if defined(TRACE)
1869 if (!raw)
1870 dotrace(sd,tracefh,1,(unsigned int)(pAddr&0xFFFFFFFF),(AccessLength + 1),"store");
1871 #endif /* TRACE */
1872
1873 /* See the comments in the LoadMemory routine about optimising
1874 memory accesses. Also if we wanted to make the simulator smaller,
1875 we could merge a lot of this code with the LoadMemory
1876 routine. However, this would slow the simulator down with
1877 run-time conditionals. */
1878 {
1879 unsigned int index = 0;
1880 unsigned char *mem = NULL;
1881
1882 if ((pAddr >= STATE_MEM_BASE (sd)) && (pAddr < (STATE_MEM_BASE (sd) + STATE_MEM_SIZE (sd)))) {
1883 index = ((unsigned int)(pAddr - STATE_MEM_BASE (sd)) & (STATE_MEM_SIZE (sd) - 1));
1884 mem = STATE_MEMORY (sd);
1885 } else if ((pAddr >= monitor_base) && (pAddr < (monitor_base + monitor_size))) {
1886 index = ((unsigned int)(pAddr - monitor_base) & (monitor_size - 1));
1887 mem = monitor;
1888 }
1889
1890 if (mem == NULL)
1891 sim_io_error(sd,"Simulator memory not found for physical address 0x%s\n",pr_addr(pAddr));
1892 else {
1893 int shift = 0;
1894
1895 #ifdef DEBUG
1896 printf("DBG: StoreMemory: offset = %d MemElem = 0x%s%s\n",(unsigned int)(pAddr & LOADDRMASK),pr_uword64(MemElem1),pr_uword64(MemElem));
1897 #endif /* DEBUG */
1898
1899 if (AccessLength <= AccessLength_DOUBLEWORD) {
1900 if (BigEndianMem) {
1901 if (raw)
1902 shift = ((7 - AccessLength) * 8);
1903 else /* real memory access */
1904 shift = ((pAddr & LOADDRMASK) * 8);
1905 MemElem <<= shift;
1906 } else {
1907 /* no need to shift raw little-endian data */
1908 if (!raw)
1909 MemElem >>= ((pAddr & LOADDRMASK) * 8);
1910 }
1911 }
1912
1913 #ifdef DEBUG
1914 printf("DBG: StoreMemory: shift = %d MemElem = 0x%s%s\n",shift,pr_uword64(MemElem1),pr_uword64(MemElem));
1915 #endif /* DEBUG */
1916
1917 if (BigEndianMem) {
1918 switch (AccessLength) { /* big-endian memory */
1919 case AccessLength_QUADWORD :
1920 mem[index++] = (unsigned char)(MemElem1 >> 56);
1921 MemElem1 <<= 8;
1922 case 14 :
1923 mem[index++] = (unsigned char)(MemElem1 >> 56);
1924 MemElem1 <<= 8;
1925 case 13 :
1926 mem[index++] = (unsigned char)(MemElem1 >> 56);
1927 MemElem1 <<= 8;
1928 case 12 :
1929 mem[index++] = (unsigned char)(MemElem1 >> 56);
1930 MemElem1 <<= 8;
1931 case 11 :
1932 mem[index++] = (unsigned char)(MemElem1 >> 56);
1933 MemElem1 <<= 8;
1934 case 10 :
1935 mem[index++] = (unsigned char)(MemElem1 >> 56);
1936 MemElem1 <<= 8;
1937 case 9 :
1938 mem[index++] = (unsigned char)(MemElem1 >> 56);
1939 MemElem1 <<= 8;
1940 case 8 :
1941 mem[index++] = (unsigned char)(MemElem1 >> 56);
1942
1943 case AccessLength_DOUBLEWORD :
1944 mem[index++] = (unsigned char)(MemElem >> 56);
1945 MemElem <<= 8;
1946 case AccessLength_SEPTIBYTE :
1947 mem[index++] = (unsigned char)(MemElem >> 56);
1948 MemElem <<= 8;
1949 case AccessLength_SEXTIBYTE :
1950 mem[index++] = (unsigned char)(MemElem >> 56);
1951 MemElem <<= 8;
1952 case AccessLength_QUINTIBYTE :
1953 mem[index++] = (unsigned char)(MemElem >> 56);
1954 MemElem <<= 8;
1955 case AccessLength_WORD :
1956 mem[index++] = (unsigned char)(MemElem >> 56);
1957 MemElem <<= 8;
1958 case AccessLength_TRIPLEBYTE :
1959 mem[index++] = (unsigned char)(MemElem >> 56);
1960 MemElem <<= 8;
1961 case AccessLength_HALFWORD :
1962 mem[index++] = (unsigned char)(MemElem >> 56);
1963 MemElem <<= 8;
1964 case AccessLength_BYTE :
1965 mem[index++] = (unsigned char)(MemElem >> 56);
1966 break;
1967 }
1968 } else {
1969 index += (AccessLength + 1);
1970 switch (AccessLength) { /* little-endian memory */
1971 case AccessLength_QUADWORD :
1972 mem[--index] = (unsigned char)(MemElem1 >> 56);
1973 case 14 :
1974 mem[--index] = (unsigned char)(MemElem1 >> 48);
1975 case 13 :
1976 mem[--index] = (unsigned char)(MemElem1 >> 40);
1977 case 12 :
1978 mem[--index] = (unsigned char)(MemElem1 >> 32);
1979 case 11 :
1980 mem[--index] = (unsigned char)(MemElem1 >> 24);
1981 case 10 :
1982 mem[--index] = (unsigned char)(MemElem1 >> 16);
1983 case 9 :
1984 mem[--index] = (unsigned char)(MemElem1 >> 8);
1985 case 8 :
1986 mem[--index] = (unsigned char)(MemElem1 >> 0);
1987
1988 case AccessLength_DOUBLEWORD :
1989 mem[--index] = (unsigned char)(MemElem >> 56);
1990 case AccessLength_SEPTIBYTE :
1991 mem[--index] = (unsigned char)(MemElem >> 48);
1992 case AccessLength_SEXTIBYTE :
1993 mem[--index] = (unsigned char)(MemElem >> 40);
1994 case AccessLength_QUINTIBYTE :
1995 mem[--index] = (unsigned char)(MemElem >> 32);
1996 case AccessLength_WORD :
1997 mem[--index] = (unsigned char)(MemElem >> 24);
1998 case AccessLength_TRIPLEBYTE :
1999 mem[--index] = (unsigned char)(MemElem >> 16);
2000 case AccessLength_HALFWORD :
2001 mem[--index] = (unsigned char)(MemElem >> 8);
2002 case AccessLength_BYTE :
2003 mem[--index] = (unsigned char)(MemElem >> 0);
2004 break;
2005 }
2006 }
2007 }
2008 }
2009
2010 return;
2011 }
2012
2013
2014 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
2015 /* Order loads and stores to synchronise shared memory. Perform the
2016 action necessary to make the effects of groups of synchronizable
2017 loads and stores indicated by stype occur in the same order for all
2018 processors. */
2019 void
2020 sync_operation(sd,stype)
2021 SIM_DESC sd;
2022 int stype;
2023 {
2024 #ifdef DEBUG
2025 sim_io_printf(sd,"SyncOperation(%d) : TODO\n",stype);
2026 #endif /* DEBUG */
2027 return;
2028 }
2029
2030 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
2031 /* Signal an exception condition. This will result in an exception
2032 that aborts the instruction. The instruction operation pseudocode
2033 will never see a return from this function call. */
2034
2035 void
2036 signal_exception (SIM_DESC sd, int exception,...)
2037 {
2038 int vector;
2039
2040 #ifdef DEBUG
2041 sim_io_printf(sd,"DBG: SignalException(%d) IPC = 0x%s\n",exception,pr_addr(IPC));
2042 #endif /* DEBUG */
2043
2044 /* Ensure that any active atomic read/modify/write operation will fail: */
2045 LLBIT = 0;
2046
2047 switch (exception) {
2048 /* TODO: For testing purposes I have been ignoring TRAPs. In
2049 reality we should either simulate them, or allow the user to
2050 ignore them at run-time.
2051 Same for SYSCALL */
2052 case Trap :
2053 sim_io_eprintf(sd,"Ignoring instruction TRAP (PC 0x%s)\n",pr_addr(IPC));
2054 break;
2055
2056 case SystemCall :
2057 {
2058 va_list ap;
2059 unsigned int instruction;
2060 unsigned int code;
2061
2062 va_start(ap,exception);
2063 instruction = va_arg(ap,unsigned int);
2064 va_end(ap);
2065
2066 code = (instruction >> 6) & 0xFFFFF;
2067
2068 sim_io_eprintf(sd,"Ignoring instruction `syscall %d' (PC 0x%s)\n",
2069 code, pr_addr(IPC));
2070 }
2071 break;
2072
2073 case DebugBreakPoint :
2074 if (! (Debug & Debug_DM))
2075 {
2076 if (INDELAYSLOT())
2077 {
2078 CANCELDELAYSLOT();
2079
2080 Debug |= Debug_DBD; /* signaled from within in delay slot */
2081 DEPC = IPC - 4; /* reference the branch instruction */
2082 }
2083 else
2084 {
2085 Debug &= ~Debug_DBD; /* not signaled from within a delay slot */
2086 DEPC = IPC;
2087 }
2088
2089 Debug |= Debug_DM; /* in debugging mode */
2090 Debug |= Debug_DBp; /* raising a DBp exception */
2091 PC = 0xBFC00200;
2092 sim_engine_restart (sd, STATE_CPU (sd, 0), NULL, NULL_CIA);
2093 }
2094 break;
2095
2096 case ReservedInstruction :
2097 {
2098 va_list ap;
2099 unsigned int instruction;
2100 va_start(ap,exception);
2101 instruction = va_arg(ap,unsigned int);
2102 va_end(ap);
2103 /* Provide simple monitor support using ReservedInstruction
2104 exceptions. The following code simulates the fixed vector
2105 entry points into the IDT monitor by causing a simulator
2106 trap, performing the monitor operation, and returning to
2107 the address held in the $ra register (standard PCS return
2108 address). This means we only need to pre-load the vector
2109 space with suitable instruction values. For systems were
2110 actual trap instructions are used, we would not need to
2111 perform this magic. */
2112 if ((instruction & RSVD_INSTRUCTION_MASK) == RSVD_INSTRUCTION) {
2113 sim_monitor(sd, ((instruction >> RSVD_INSTRUCTION_ARG_SHIFT) & RSVD_INSTRUCTION_ARG_MASK) );
2114 PC = RA; /* simulate the return from the vector entry */
2115 /* NOTE: This assumes that a branch-and-link style
2116 instruction was used to enter the vector (which is the
2117 case with the current IDT monitor). */
2118 sim_engine_restart (sd, STATE_CPU (sd, 0), NULL, NULL_CIA);
2119 }
2120 /* Look for the mips16 entry and exit instructions, and
2121 simulate a handler for them. */
2122 else if ((IPC & 1) != 0
2123 && (instruction & 0xf81f) == 0xe809
2124 && (instruction & 0x0c0) != 0x0c0) {
2125 mips16_entry (instruction);
2126 sim_engine_restart (sd, STATE_CPU (sd, 0), NULL, NULL_CIA);
2127 } /* else fall through to normal exception processing */
2128 sim_io_eprintf(sd,"ReservedInstruction 0x%08X at IPC = 0x%s\n",instruction,pr_addr(IPC));
2129 }
2130
2131 case BreakPoint:
2132 #ifdef DEBUG
2133 sim_io_printf(sd,"DBG: SignalException(%d) IPC = 0x%s\n",exception,pr_addr(IPC));
2134 #endif /* DEBUG */
2135 /* Keep a copy of the current A0 in-case this is the program exit
2136 breakpoint: */
2137 {
2138 va_list ap;
2139 unsigned int instruction;
2140 va_start(ap,exception);
2141 instruction = va_arg(ap,unsigned int);
2142 va_end(ap);
2143 /* Check for our special terminating BREAK: */
2144 if ((instruction & 0x03FFFFC0) == 0x03ff0000) {
2145 sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, NULL_CIA,
2146 sim_exited, (unsigned int)(A0 & 0xFFFFFFFF));
2147 }
2148 }
2149 if (STATE & simDELAYSLOT)
2150 PC = IPC - 4; /* reference the branch instruction */
2151 else
2152 PC = IPC;
2153 sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, NULL_CIA,
2154 sim_stopped, SIGTRAP);
2155
2156 default:
2157 /* Store exception code into current exception id variable (used
2158 by exit code): */
2159
2160 /* TODO: If not simulating exceptions then stop the simulator
2161 execution. At the moment we always stop the simulation. */
2162
2163 /* See figure 5-17 for an outline of the code below */
2164 if (! (SR & status_EXL))
2165 {
2166 CAUSE = (exception << 2);
2167 if (STATE & simDELAYSLOT)
2168 {
2169 STATE &= ~simDELAYSLOT;
2170 CAUSE |= cause_BD;
2171 EPC = (IPC - 4); /* reference the branch instruction */
2172 }
2173 else
2174 EPC = IPC;
2175 /* FIXME: TLB et.al. */
2176 vector = 0x180;
2177 }
2178 else
2179 {
2180 CAUSE = (exception << 2);
2181 vector = 0x180;
2182 }
2183 SR |= status_EXL;
2184 /* Store exception code into current exception id variable (used
2185 by exit code): */
2186 if (SR & status_BEV)
2187 PC = (signed)0xBFC00200 + 0x180;
2188 else
2189 PC = (signed)0x80000000 + 0x180;
2190
2191 switch ((CAUSE >> 2) & 0x1F)
2192 {
2193 case Interrupt:
2194 /* Interrupts arrive during event processing, no need to
2195 restart */
2196 return;
2197
2198 case TLBModification:
2199 case TLBLoad:
2200 case TLBStore:
2201 case AddressLoad:
2202 case AddressStore:
2203 case InstructionFetch:
2204 case DataReference:
2205 /* The following is so that the simulator will continue from the
2206 exception address on breakpoint operations. */
2207 PC = EPC;
2208 sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, NULL_CIA,
2209 sim_stopped, SIGBUS);
2210
2211 case ReservedInstruction:
2212 case CoProcessorUnusable:
2213 PC = EPC;
2214 sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, NULL_CIA,
2215 sim_stopped, SIGILL);
2216
2217 case IntegerOverflow:
2218 case FPE:
2219 sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, NULL_CIA,
2220 sim_stopped, SIGFPE);
2221
2222 case Trap:
2223 case Watch:
2224 case SystemCall:
2225 PC = EPC;
2226 sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, NULL_CIA,
2227 sim_stopped, SIGTRAP);
2228
2229 case BreakPoint:
2230 PC = EPC;
2231 sim_engine_abort (sd, STATE_CPU (sd, 0), NULL_CIA,
2232 "FATAL: Should not encounter a breakpoint\n");
2233
2234 default : /* Unknown internal exception */
2235 PC = EPC;
2236 sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, NULL_CIA,
2237 sim_stopped, SIGQUIT);
2238
2239 }
2240
2241 case SimulatorFault:
2242 {
2243 va_list ap;
2244 char *msg;
2245 va_start(ap,exception);
2246 msg = va_arg(ap,char *);
2247 va_end(ap);
2248 sim_engine_abort (sd, STATE_CPU (sd, 0), NULL_CIA,
2249 "FATAL: Simulator error \"%s\"\n",msg);
2250 }
2251 }
2252
2253 return;
2254 }
2255
2256 #if defined(WARN_RESULT)
2257 /* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
2258 /* This function indicates that the result of the operation is
2259 undefined. However, this should not affect the instruction
2260 stream. All that is meant to happen is that the destination
2261 register is set to an undefined result. To keep the simulator
2262 simple, we just don't bother updating the destination register, so
2263 the overall result will be undefined. If desired we can stop the
2264 simulator by raising a pseudo-exception. */
2265 static void
2266 UndefinedResult()
2267 {
2268 sim_io_eprintf(sd,"UndefinedResult: IPC = 0x%s\n",pr_addr(IPC));
2269 #if 0 /* Disabled for the moment, since it actually happens a lot at the moment. */
2270 state |= simSTOP;
2271 #endif
2272 return;
2273 }
2274 #endif /* WARN_RESULT */
2275
2276 void
2277 cache_op(sd,op,pAddr,vAddr,instruction)
2278 SIM_DESC sd;
2279 int op;
2280 uword64 pAddr;
2281 uword64 vAddr;
2282 unsigned int instruction;
2283 {
2284 #if 1 /* stop warning message being displayed (we should really just remove the code) */
2285 static int icache_warning = 1;
2286 static int dcache_warning = 1;
2287 #else
2288 static int icache_warning = 0;
2289 static int dcache_warning = 0;
2290 #endif
2291
2292 /* If CP0 is not useable (User or Supervisor mode) and the CP0
2293 enable bit in the Status Register is clear - a coprocessor
2294 unusable exception is taken. */
2295 #if 0
2296 sim_io_printf(sd,"TODO: Cache availability checking (PC = 0x%s)\n",pr_addr(IPC));
2297 #endif
2298
2299 switch (op & 0x3) {
2300 case 0: /* instruction cache */
2301 switch (op >> 2) {
2302 case 0: /* Index Invalidate */
2303 case 1: /* Index Load Tag */
2304 case 2: /* Index Store Tag */
2305 case 4: /* Hit Invalidate */
2306 case 5: /* Fill */
2307 case 6: /* Hit Writeback */
2308 if (!icache_warning)
2309 {
2310 sim_io_eprintf(sd,"Instruction CACHE operation %d to be coded\n",(op >> 2));
2311 icache_warning = 1;
2312 }
2313 break;
2314
2315 default:
2316 SignalException(ReservedInstruction,instruction);
2317 break;
2318 }
2319 break;
2320
2321 case 1: /* data cache */
2322 switch (op >> 2) {
2323 case 0: /* Index Writeback Invalidate */
2324 case 1: /* Index Load Tag */
2325 case 2: /* Index Store Tag */
2326 case 3: /* Create Dirty */
2327 case 4: /* Hit Invalidate */
2328 case 5: /* Hit Writeback Invalidate */
2329 case 6: /* Hit Writeback */
2330 if (!dcache_warning)
2331 {
2332 sim_io_eprintf(sd,"Data CACHE operation %d to be coded\n",(op >> 2));
2333 dcache_warning = 1;
2334 }
2335 break;
2336
2337 default:
2338 SignalException(ReservedInstruction,instruction);
2339 break;
2340 }
2341 break;
2342
2343 default: /* unrecognised cache ID */
2344 SignalException(ReservedInstruction,instruction);
2345 break;
2346 }
2347
2348 return;
2349 }
2350
2351 /*-- FPU support routines ---------------------------------------------------*/
2352
2353 #if defined(HASFPU) /* Only needed when building FPU aware simulators */
2354
2355 /* Numbers are held in normalized form. The SINGLE and DOUBLE binary
2356 formats conform to ANSI/IEEE Std 754-1985. */
2357 /* SINGLE precision floating:
2358 * seeeeeeeefffffffffffffffffffffff
2359 * s = 1bit = sign
2360 * e = 8bits = exponent
2361 * f = 23bits = fraction
2362 */
2363 /* SINGLE precision fixed:
2364 * siiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
2365 * s = 1bit = sign
2366 * i = 31bits = integer
2367 */
2368 /* DOUBLE precision floating:
2369 * seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
2370 * s = 1bit = sign
2371 * e = 11bits = exponent
2372 * f = 52bits = fraction
2373 */
2374 /* DOUBLE precision fixed:
2375 * siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
2376 * s = 1bit = sign
2377 * i = 63bits = integer
2378 */
2379
2380 /* Extract sign-bit: */
2381 #define FP_S_s(v) (((v) & ((unsigned)1 << 31)) ? 1 : 0)
2382 #define FP_D_s(v) (((v) & ((uword64)1 << 63)) ? 1 : 0)
2383 /* Extract biased exponent: */
2384 #define FP_S_be(v) (((v) >> 23) & 0xFF)
2385 #define FP_D_be(v) (((v) >> 52) & 0x7FF)
2386 /* Extract unbiased Exponent: */
2387 #define FP_S_e(v) (FP_S_be(v) - 0x7F)
2388 #define FP_D_e(v) (FP_D_be(v) - 0x3FF)
2389 /* Extract complete fraction field: */
2390 #define FP_S_f(v) ((v) & ~((unsigned)0x1FF << 23))
2391 #define FP_D_f(v) ((v) & ~((uword64)0xFFF << 52))
2392 /* Extract numbered fraction bit: */
2393 #define FP_S_fb(b,v) (((v) & (1 << (23 - (b)))) ? 1 : 0)
2394 #define FP_D_fb(b,v) (((v) & (1 << (52 - (b)))) ? 1 : 0)
2395
2396 /* Explicit QNaN values used when value required: */
2397 #define FPQNaN_SINGLE (0x7FBFFFFF)
2398 #define FPQNaN_WORD (0x7FFFFFFF)
2399 #define FPQNaN_DOUBLE (((uword64)0x7FF7FFFF << 32) | 0xFFFFFFFF)
2400 #define FPQNaN_LONG (((uword64)0x7FFFFFFF << 32) | 0xFFFFFFFF)
2401
2402 /* Explicit Infinity values used when required: */
2403 #define FPINF_SINGLE (0x7F800000)
2404 #define FPINF_DOUBLE (((uword64)0x7FF00000 << 32) | 0x00000000)
2405
2406 #if 1 /* def DEBUG */
2407 #define RMMODE(v) (((v) == FP_RM_NEAREST) ? "Round" : (((v) == FP_RM_TOZERO) ? "Trunc" : (((v) == FP_RM_TOPINF) ? "Ceil" : "Floor")))
2408 #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>"))))))
2409 #endif /* DEBUG */
2410
2411 uword64
2412 value_fpr(sd,fpr,fmt)
2413 SIM_DESC sd;
2414 int fpr;
2415 FP_formats fmt;
2416 {
2417 uword64 value = 0;
2418 int err = 0;
2419
2420 /* Treat unused register values, as fixed-point 64bit values: */
2421 if ((fmt == fmt_uninterpreted) || (fmt == fmt_unknown))
2422 #if 1
2423 /* If request to read data as "uninterpreted", then use the current
2424 encoding: */
2425 fmt = FPR_STATE[fpr];
2426 #else
2427 fmt = fmt_long;
2428 #endif
2429
2430 /* For values not yet accessed, set to the desired format: */
2431 if (FPR_STATE[fpr] == fmt_uninterpreted) {
2432 FPR_STATE[fpr] = fmt;
2433 #ifdef DEBUG
2434 printf("DBG: Register %d was fmt_uninterpreted. Now %s\n",fpr,DOFMT(fmt));
2435 #endif /* DEBUG */
2436 }
2437 if (fmt != FPR_STATE[fpr]) {
2438 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(IPC));
2439 FPR_STATE[fpr] = fmt_unknown;
2440 }
2441
2442 if (FPR_STATE[fpr] == fmt_unknown) {
2443 /* Set QNaN value: */
2444 switch (fmt) {
2445 case fmt_single:
2446 value = FPQNaN_SINGLE;
2447 break;
2448
2449 case fmt_double:
2450 value = FPQNaN_DOUBLE;
2451 break;
2452
2453 case fmt_word:
2454 value = FPQNaN_WORD;
2455 break;
2456
2457 case fmt_long:
2458 value = FPQNaN_LONG;
2459 break;
2460
2461 default:
2462 err = -1;
2463 break;
2464 }
2465 } else if (SizeFGR() == 64) {
2466 switch (fmt) {
2467 case fmt_single:
2468 case fmt_word:
2469 value = (FGR[fpr] & 0xFFFFFFFF);
2470 break;
2471
2472 case fmt_uninterpreted:
2473 case fmt_double:
2474 case fmt_long:
2475 value = FGR[fpr];
2476 break;
2477
2478 default :
2479 err = -1;
2480 break;
2481 }
2482 } else {
2483 switch (fmt) {
2484 case fmt_single:
2485 case fmt_word:
2486 value = (FGR[fpr] & 0xFFFFFFFF);
2487 break;
2488
2489 case fmt_uninterpreted:
2490 case fmt_double:
2491 case fmt_long:
2492 if ((fpr & 1) == 0) { /* even registers only */
2493 value = ((((uword64)FGR[fpr+1]) << 32) | (FGR[fpr] & 0xFFFFFFFF));
2494 } else {
2495 SignalException(ReservedInstruction,0);
2496 }
2497 break;
2498
2499 default :
2500 err = -1;
2501 break;
2502 }
2503 }
2504
2505 if (err)
2506 SignalExceptionSimulatorFault ("Unrecognised FP format in ValueFPR()");
2507
2508 #ifdef DEBUG
2509 printf("DBG: ValueFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR() = %d\n",fpr,DOFMT(fmt),pr_addr(value),pr_addr(IPC),SizeFGR());
2510 #endif /* DEBUG */
2511
2512 return(value);
2513 }
2514
2515 void
2516 store_fpr(sd,fpr,fmt,value)
2517 SIM_DESC sd;
2518 int fpr;
2519 FP_formats fmt;
2520 uword64 value;
2521 {
2522 int err = 0;
2523
2524 #ifdef DEBUG
2525 printf("DBG: StoreFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR() = %d\n",fpr,DOFMT(fmt),pr_addr(value),pr_addr(IPC),SizeFGR());
2526 #endif /* DEBUG */
2527
2528 if (SizeFGR() == 64) {
2529 switch (fmt) {
2530 case fmt_single :
2531 case fmt_word :
2532 FGR[fpr] = (((uword64)0xDEADC0DE << 32) | (value & 0xFFFFFFFF));
2533 FPR_STATE[fpr] = fmt;
2534 break;
2535
2536 case fmt_uninterpreted:
2537 case fmt_double :
2538 case fmt_long :
2539 FGR[fpr] = value;
2540 FPR_STATE[fpr] = fmt;
2541 break;
2542
2543 default :
2544 FPR_STATE[fpr] = fmt_unknown;
2545 err = -1;
2546 break;
2547 }
2548 } else {
2549 switch (fmt) {
2550 case fmt_single :
2551 case fmt_word :
2552 FGR[fpr] = (value & 0xFFFFFFFF);
2553 FPR_STATE[fpr] = fmt;
2554 break;
2555
2556 case fmt_uninterpreted:
2557 case fmt_double :
2558 case fmt_long :
2559 if ((fpr & 1) == 0) { /* even register number only */
2560 FGR[fpr+1] = (value >> 32);
2561 FGR[fpr] = (value & 0xFFFFFFFF);
2562 FPR_STATE[fpr + 1] = fmt;
2563 FPR_STATE[fpr] = fmt;
2564 } else {
2565 FPR_STATE[fpr] = fmt_unknown;
2566 FPR_STATE[fpr + 1] = fmt_unknown;
2567 SignalException(ReservedInstruction,0);
2568 }
2569 break;
2570
2571 default :
2572 FPR_STATE[fpr] = fmt_unknown;
2573 err = -1;
2574 break;
2575 }
2576 }
2577 #if defined(WARN_RESULT)
2578 else
2579 UndefinedResult();
2580 #endif /* WARN_RESULT */
2581
2582 if (err)
2583 SignalExceptionSimulatorFault ("Unrecognised FP format in StoreFPR()");
2584
2585 #ifdef DEBUG
2586 printf("DBG: StoreFPR: fpr[%d] = 0x%s (format %s)\n",fpr,pr_addr(FGR[fpr]),DOFMT(fmt));
2587 #endif /* DEBUG */
2588
2589 return;
2590 }
2591
2592 int
2593 NaN(op,fmt)
2594 uword64 op;
2595 FP_formats fmt;
2596 {
2597 int boolean = 0;
2598
2599 /* Check if (((E - bias) == (E_max + 1)) && (fraction != 0)). We
2600 know that the exponent field is biased... we we cheat and avoid
2601 removing the bias value. */
2602 switch (fmt) {
2603 case fmt_single:
2604 boolean = ((FP_S_be(op) == 0xFF) && (FP_S_f(op) != 0));
2605 /* We could use "FP_S_fb(1,op)" to ascertain whether we are
2606 dealing with a SNaN or QNaN */
2607 break;
2608 case fmt_double:
2609 boolean = ((FP_D_be(op) == 0x7FF) && (FP_D_f(op) != 0));
2610 /* We could use "FP_S_fb(1,op)" to ascertain whether we are
2611 dealing with a SNaN or QNaN */
2612 break;
2613 case fmt_word:
2614 boolean = (op == FPQNaN_WORD);
2615 break;
2616 case fmt_long:
2617 boolean = (op == FPQNaN_LONG);
2618 break;
2619 default:
2620 fprintf (stderr, "Bad switch\n");
2621 abort ();
2622 }
2623
2624 #ifdef DEBUG
2625 printf("DBG: NaN: returning %d for 0x%s (format = %s)\n",boolean,pr_addr(op),DOFMT(fmt));
2626 #endif /* DEBUG */
2627
2628 return(boolean);
2629 }
2630
2631 int
2632 Infinity(op,fmt)
2633 uword64 op;
2634 FP_formats fmt;
2635 {
2636 int boolean = 0;
2637
2638 #ifdef DEBUG
2639 printf("DBG: Infinity: format %s 0x%s (PC = 0x%s)\n",DOFMT(fmt),pr_addr(op),pr_addr(IPC));
2640 #endif /* DEBUG */
2641
2642 /* Check if (((E - bias) == (E_max + 1)) && (fraction == 0)). We
2643 know that the exponent field is biased... we we cheat and avoid
2644 removing the bias value. */
2645 switch (fmt) {
2646 case fmt_single:
2647 boolean = ((FP_S_be(op) == 0xFF) && (FP_S_f(op) == 0));
2648 break;
2649 case fmt_double:
2650 boolean = ((FP_D_be(op) == 0x7FF) && (FP_D_f(op) == 0));
2651 break;
2652 default:
2653 printf("DBG: TODO: unrecognised format (%s) for Infinity check\n",DOFMT(fmt));
2654 break;
2655 }
2656
2657 #ifdef DEBUG
2658 printf("DBG: Infinity: returning %d for 0x%s (format = %s)\n",boolean,pr_addr(op),DOFMT(fmt));
2659 #endif /* DEBUG */
2660
2661 return(boolean);
2662 }
2663
2664 int
2665 Less(op1,op2,fmt)
2666 uword64 op1;
2667 uword64 op2;
2668 FP_formats fmt;
2669 {
2670 int boolean = 0;
2671
2672 /* Argument checking already performed by the FPCOMPARE code */
2673
2674 #ifdef DEBUG
2675 printf("DBG: Less: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2676 #endif /* DEBUG */
2677
2678 /* The format type should already have been checked: */
2679 switch (fmt) {
2680 case fmt_single:
2681 {
2682 unsigned int wop1 = (unsigned int)op1;
2683 unsigned int wop2 = (unsigned int)op2;
2684 boolean = (*(float *)&wop1 < *(float *)&wop2);
2685 }
2686 break;
2687 case fmt_double:
2688 boolean = (*(double *)&op1 < *(double *)&op2);
2689 break;
2690 default:
2691 fprintf (stderr, "Bad switch\n");
2692 abort ();
2693 }
2694
2695 #ifdef DEBUG
2696 printf("DBG: Less: returning %d (format = %s)\n",boolean,DOFMT(fmt));
2697 #endif /* DEBUG */
2698
2699 return(boolean);
2700 }
2701
2702 int
2703 Equal(op1,op2,fmt)
2704 uword64 op1;
2705 uword64 op2;
2706 FP_formats fmt;
2707 {
2708 int boolean = 0;
2709
2710 /* Argument checking already performed by the FPCOMPARE code */
2711
2712 #ifdef DEBUG
2713 printf("DBG: Equal: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2714 #endif /* DEBUG */
2715
2716 /* The format type should already have been checked: */
2717 switch (fmt) {
2718 case fmt_single:
2719 boolean = ((op1 & 0xFFFFFFFF) == (op2 & 0xFFFFFFFF));
2720 break;
2721 case fmt_double:
2722 boolean = (op1 == op2);
2723 break;
2724 default:
2725 fprintf (stderr, "Bad switch\n");
2726 abort ();
2727 }
2728
2729 #ifdef DEBUG
2730 printf("DBG: Equal: returning %d (format = %s)\n",boolean,DOFMT(fmt));
2731 #endif /* DEBUG */
2732
2733 return(boolean);
2734 }
2735
2736 uword64
2737 AbsoluteValue(op,fmt)
2738 uword64 op;
2739 FP_formats fmt;
2740 {
2741 uword64 result = 0;
2742
2743 #ifdef DEBUG
2744 printf("DBG: AbsoluteValue: %s: op = 0x%s\n",DOFMT(fmt),pr_addr(op));
2745 #endif /* DEBUG */
2746
2747 /* The format type should already have been checked: */
2748 switch (fmt) {
2749 case fmt_single:
2750 {
2751 unsigned int wop = (unsigned int)op;
2752 float tmp = ((float)fabs((double)*(float *)&wop));
2753 result = (uword64)*(unsigned int *)&tmp;
2754 }
2755 break;
2756 case fmt_double:
2757 {
2758 double tmp = (fabs(*(double *)&op));
2759 result = *(uword64 *)&tmp;
2760 }
2761 default:
2762 fprintf (stderr, "Bad switch\n");
2763 abort ();
2764 }
2765
2766 return(result);
2767 }
2768
2769 uword64
2770 Negate(op,fmt)
2771 uword64 op;
2772 FP_formats fmt;
2773 {
2774 uword64 result = 0;
2775
2776 #ifdef DEBUG
2777 printf("DBG: Negate: %s: op = 0x%s\n",DOFMT(fmt),pr_addr(op));
2778 #endif /* DEBUG */
2779
2780 /* The format type should already have been checked: */
2781 switch (fmt) {
2782 case fmt_single:
2783 {
2784 unsigned int wop = (unsigned int)op;
2785 float tmp = ((float)0.0 - *(float *)&wop);
2786 result = (uword64)*(unsigned int *)&tmp;
2787 }
2788 break;
2789 case fmt_double:
2790 {
2791 double tmp = ((double)0.0 - *(double *)&op);
2792 result = *(uword64 *)&tmp;
2793 }
2794 break;
2795 default:
2796 fprintf (stderr, "Bad switch\n");
2797 abort ();
2798 }
2799
2800 return(result);
2801 }
2802
2803 uword64
2804 Add(op1,op2,fmt)
2805 uword64 op1;
2806 uword64 op2;
2807 FP_formats fmt;
2808 {
2809 uword64 result = 0;
2810
2811 #ifdef DEBUG
2812 printf("DBG: Add: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2813 #endif /* DEBUG */
2814
2815 /* The registers must specify FPRs valid for operands of type
2816 "fmt". If they are not valid, the result is undefined. */
2817
2818 /* The format type should already have been checked: */
2819 switch (fmt) {
2820 case fmt_single:
2821 {
2822 unsigned int wop1 = (unsigned int)op1;
2823 unsigned int wop2 = (unsigned int)op2;
2824 float tmp = (*(float *)&wop1 + *(float *)&wop2);
2825 result = (uword64)*(unsigned int *)&tmp;
2826 }
2827 break;
2828 case fmt_double:
2829 {
2830 double tmp = (*(double *)&op1 + *(double *)&op2);
2831 result = *(uword64 *)&tmp;
2832 }
2833 break;
2834 default:
2835 fprintf (stderr, "Bad switch\n");
2836 abort ();
2837 }
2838
2839 #ifdef DEBUG
2840 printf("DBG: Add: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2841 #endif /* DEBUG */
2842
2843 return(result);
2844 }
2845
2846 uword64
2847 Sub(op1,op2,fmt)
2848 uword64 op1;
2849 uword64 op2;
2850 FP_formats fmt;
2851 {
2852 uword64 result = 0;
2853
2854 #ifdef DEBUG
2855 printf("DBG: Sub: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2856 #endif /* DEBUG */
2857
2858 /* The registers must specify FPRs valid for operands of type
2859 "fmt". If they are not valid, the result is undefined. */
2860
2861 /* The format type should already have been checked: */
2862 switch (fmt) {
2863 case fmt_single:
2864 {
2865 unsigned int wop1 = (unsigned int)op1;
2866 unsigned int wop2 = (unsigned int)op2;
2867 float tmp = (*(float *)&wop1 - *(float *)&wop2);
2868 result = (uword64)*(unsigned int *)&tmp;
2869 }
2870 break;
2871 case fmt_double:
2872 {
2873 double tmp = (*(double *)&op1 - *(double *)&op2);
2874 result = *(uword64 *)&tmp;
2875 }
2876 break;
2877 default:
2878 fprintf (stderr, "Bad switch\n");
2879 abort ();
2880 }
2881
2882 #ifdef DEBUG
2883 printf("DBG: Sub: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2884 #endif /* DEBUG */
2885
2886 return(result);
2887 }
2888
2889 uword64
2890 Multiply(op1,op2,fmt)
2891 uword64 op1;
2892 uword64 op2;
2893 FP_formats fmt;
2894 {
2895 uword64 result = 0;
2896
2897 #ifdef DEBUG
2898 printf("DBG: Multiply: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2899 #endif /* DEBUG */
2900
2901 /* The registers must specify FPRs valid for operands of type
2902 "fmt". If they are not valid, the result is undefined. */
2903
2904 /* The format type should already have been checked: */
2905 switch (fmt) {
2906 case fmt_single:
2907 {
2908 unsigned int wop1 = (unsigned int)op1;
2909 unsigned int wop2 = (unsigned int)op2;
2910 float tmp = (*(float *)&wop1 * *(float *)&wop2);
2911 result = (uword64)*(unsigned int *)&tmp;
2912 }
2913 break;
2914 case fmt_double:
2915 {
2916 double tmp = (*(double *)&op1 * *(double *)&op2);
2917 result = *(uword64 *)&tmp;
2918 }
2919 break;
2920 default:
2921 fprintf (stderr, "Bad switch\n");
2922 abort ();
2923 }
2924
2925 #ifdef DEBUG
2926 printf("DBG: Multiply: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2927 #endif /* DEBUG */
2928
2929 return(result);
2930 }
2931
2932 uword64
2933 Divide(op1,op2,fmt)
2934 uword64 op1;
2935 uword64 op2;
2936 FP_formats fmt;
2937 {
2938 uword64 result = 0;
2939
2940 #ifdef DEBUG
2941 printf("DBG: Divide: %s: op1 = 0x%s : op2 = 0x%s\n",DOFMT(fmt),pr_addr(op1),pr_addr(op2));
2942 #endif /* DEBUG */
2943
2944 /* The registers must specify FPRs valid for operands of type
2945 "fmt". If they are not valid, the result is undefined. */
2946
2947 /* The format type should already have been checked: */
2948 switch (fmt) {
2949 case fmt_single:
2950 {
2951 unsigned int wop1 = (unsigned int)op1;
2952 unsigned int wop2 = (unsigned int)op2;
2953 float tmp = (*(float *)&wop1 / *(float *)&wop2);
2954 result = (uword64)*(unsigned int *)&tmp;
2955 }
2956 break;
2957 case fmt_double:
2958 {
2959 double tmp = (*(double *)&op1 / *(double *)&op2);
2960 result = *(uword64 *)&tmp;
2961 }
2962 break;
2963 default:
2964 fprintf (stderr, "Bad switch\n");
2965 abort ();
2966 }
2967
2968 #ifdef DEBUG
2969 printf("DBG: Divide: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
2970 #endif /* DEBUG */
2971
2972 return(result);
2973 }
2974
2975 uword64 UNUSED
2976 Recip(op,fmt)
2977 uword64 op;
2978 FP_formats fmt;
2979 {
2980 uword64 result = 0;
2981
2982 #ifdef DEBUG
2983 printf("DBG: Recip: %s: op = 0x%s\n",DOFMT(fmt),pr_addr(op));
2984 #endif /* DEBUG */
2985
2986 /* The registers must specify FPRs valid for operands of type
2987 "fmt". If they are not valid, the result is undefined. */
2988
2989 /* The format type should already have been checked: */
2990 switch (fmt) {
2991 case fmt_single:
2992 {
2993 unsigned int wop = (unsigned int)op;
2994 float tmp = ((float)1.0 / *(float *)&wop);
2995 result = (uword64)*(unsigned int *)&tmp;
2996 }
2997 break;
2998 case fmt_double:
2999 {
3000 double tmp = ((double)1.0 / *(double *)&op);
3001 result = *(uword64 *)&tmp;
3002 }
3003 break;
3004 default:
3005 fprintf (stderr, "Bad switch\n");
3006 abort ();
3007 }
3008
3009 #ifdef DEBUG
3010 printf("DBG: Recip: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
3011 #endif /* DEBUG */
3012
3013 return(result);
3014 }
3015
3016 uword64
3017 SquareRoot(op,fmt)
3018 uword64 op;
3019 FP_formats fmt;
3020 {
3021 uword64 result = 0;
3022
3023 #ifdef DEBUG
3024 printf("DBG: SquareRoot: %s: op = 0x%s\n",DOFMT(fmt),pr_addr(op));
3025 #endif /* DEBUG */
3026
3027 /* The registers must specify FPRs valid for operands of type
3028 "fmt". If they are not valid, the result is undefined. */
3029
3030 /* The format type should already have been checked: */
3031 switch (fmt) {
3032 case fmt_single:
3033 {
3034 unsigned int wop = (unsigned int)op;
3035 #ifdef HAVE_SQRT
3036 float tmp = ((float)sqrt((double)*(float *)&wop));
3037 result = (uword64)*(unsigned int *)&tmp;
3038 #else
3039 /* TODO: Provide square-root */
3040 result = (uword64)0;
3041 #endif
3042 }
3043 break;
3044 case fmt_double:
3045 {
3046 #ifdef HAVE_SQRT
3047 double tmp = (sqrt(*(double *)&op));
3048 result = *(uword64 *)&tmp;
3049 #else
3050 /* TODO: Provide square-root */
3051 result = (uword64)0;
3052 #endif
3053 }
3054 break;
3055 default:
3056 fprintf (stderr, "Bad switch\n");
3057 abort ();
3058 }
3059
3060 #ifdef DEBUG
3061 printf("DBG: SquareRoot: returning 0x%s (format = %s)\n",pr_addr(result),DOFMT(fmt));
3062 #endif /* DEBUG */
3063
3064 return(result);
3065 }
3066
3067 uword64
3068 convert(sd,rm,op,from,to)
3069 SIM_DESC sd;
3070 int rm;
3071 uword64 op;
3072 FP_formats from;
3073 FP_formats to;
3074 {
3075 uword64 result = 0;
3076
3077 #ifdef DEBUG
3078 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));
3079 #endif /* DEBUG */
3080
3081 /* The value "op" is converted to the destination format, rounding
3082 using mode "rm". When the destination is a fixed-point format,
3083 then a source value of Infinity, NaN or one which would round to
3084 an integer outside the fixed point range then an IEEE Invalid
3085 Operation condition is raised. */
3086 switch (to) {
3087 case fmt_single:
3088 {
3089 float tmp;
3090 switch (from) {
3091 case fmt_double:
3092 tmp = (float)(*(double *)&op);
3093 break;
3094
3095 case fmt_word:
3096 tmp = (float)((int)(op & 0xFFFFFFFF));
3097 break;
3098
3099 case fmt_long:
3100 tmp = (float)((word64)op);
3101 break;
3102 default:
3103 fprintf (stderr, "Bad switch\n");
3104 abort ();
3105 }
3106
3107 #if 0
3108 /* FIXME: This code is incorrect. The rounding mode does not
3109 round to integral values; it rounds to the nearest
3110 representable value in the format. */
3111
3112 switch (rm) {
3113 case FP_RM_NEAREST:
3114 /* Round result to nearest representable value. When two
3115 representable values are equally near, round to the value
3116 that has a least significant bit of zero (i.e. is even). */
3117 #ifdef HAVE_ANINT
3118 tmp = (float)anint((double)tmp);
3119 #else
3120 /* TODO: Provide round-to-nearest */
3121 #endif
3122 break;
3123
3124 case FP_RM_TOZERO:
3125 /* Round result to the value closest to, and not greater in
3126 magnitude than, the result. */
3127 #ifdef HAVE_AINT
3128 tmp = (float)aint((double)tmp);
3129 #else
3130 /* TODO: Provide round-to-zero */
3131 #endif
3132 break;
3133
3134 case FP_RM_TOPINF:
3135 /* Round result to the value closest to, and not less than,
3136 the result. */
3137 tmp = (float)ceil((double)tmp);
3138 break;
3139
3140 case FP_RM_TOMINF:
3141 /* Round result to the value closest to, and not greater than,
3142 the result. */
3143 tmp = (float)floor((double)tmp);
3144 break;
3145 }
3146 #endif /* 0 */
3147
3148 result = (uword64)*(unsigned int *)&tmp;
3149 }
3150 break;
3151
3152 case fmt_double:
3153 {
3154 double tmp;
3155 word64 xxx;
3156
3157 switch (from) {
3158 case fmt_single:
3159 {
3160 unsigned int wop = (unsigned int)op;
3161 tmp = (double)(*(float *)&wop);
3162 }
3163 break;
3164
3165 case fmt_word:
3166 xxx = SIGNEXTEND((op & 0xFFFFFFFF),32);
3167 tmp = (double)xxx;
3168 break;
3169
3170 case fmt_long:
3171 tmp = (double)((word64)op);
3172 break;
3173
3174 default:
3175 fprintf (stderr, "Bad switch\n");
3176 abort ();
3177 }
3178
3179 #if 0
3180 /* FIXME: This code is incorrect. The rounding mode does not
3181 round to integral values; it rounds to the nearest
3182 representable value in the format. */
3183
3184 switch (rm) {
3185 case FP_RM_NEAREST:
3186 #ifdef HAVE_ANINT
3187 tmp = anint(*(double *)&tmp);
3188 #else
3189 /* TODO: Provide round-to-nearest */
3190 #endif
3191 break;
3192
3193 case FP_RM_TOZERO:
3194 #ifdef HAVE_AINT
3195 tmp = aint(*(double *)&tmp);
3196 #else
3197 /* TODO: Provide round-to-zero */
3198 #endif
3199 break;
3200
3201 case FP_RM_TOPINF:
3202 tmp = ceil(*(double *)&tmp);
3203 break;
3204
3205 case FP_RM_TOMINF:
3206 tmp = floor(*(double *)&tmp);
3207 break;
3208 }
3209 #endif /* 0 */
3210
3211 result = *(uword64 *)&tmp;
3212 }
3213 break;
3214
3215 case fmt_word:
3216 case fmt_long:
3217 if (Infinity(op,from) || NaN(op,from) || (1 == 0/*TODO: check range */)) {
3218 printf("DBG: TODO: update FCSR\n");
3219 SignalExceptionFPE ();
3220 } else {
3221 if (to == fmt_word) {
3222 int tmp = 0;
3223 switch (from) {
3224 case fmt_single:
3225 {
3226 unsigned int wop = (unsigned int)op;
3227 tmp = (int)*((float *)&wop);
3228 }
3229 break;
3230 case fmt_double:
3231 tmp = (int)*((double *)&op);
3232 #ifdef DEBUG
3233 printf("DBG: from double %.30f (0x%s) to word: 0x%08X\n",*((double *)&op),pr_addr(op),tmp);
3234 #endif /* DEBUG */
3235 break;
3236 default:
3237 fprintf (stderr, "Bad switch\n");
3238 abort ();
3239 }
3240 result = (uword64)tmp;
3241 } else { /* fmt_long */
3242 word64 tmp = 0;
3243 switch (from) {
3244 case fmt_single:
3245 {
3246 unsigned int wop = (unsigned int)op;
3247 tmp = (word64)*((float *)&wop);
3248 }
3249 break;
3250 case fmt_double:
3251 tmp = (word64)*((double *)&op);
3252 break;
3253 default:
3254 fprintf (stderr, "Bad switch\n");
3255 abort ();
3256 }
3257 result = (uword64)tmp;
3258 }
3259 }
3260 break;
3261 default:
3262 fprintf (stderr, "Bad switch\n");
3263 abort ();
3264 }
3265
3266 #ifdef DEBUG
3267 printf("DBG: Convert: returning 0x%s (to format = %s)\n",pr_addr(result),DOFMT(to));
3268 #endif /* DEBUG */
3269
3270 return(result);
3271 }
3272 #endif /* HASFPU */
3273
3274 /*-- co-processor support routines ------------------------------------------*/
3275
3276 static int UNUSED
3277 CoProcPresent(coproc_number)
3278 unsigned int coproc_number;
3279 {
3280 /* Return TRUE if simulator provides a model for the given co-processor number */
3281 return(0);
3282 }
3283
3284 void
3285 cop_lw(sd,coproc_num,coproc_reg,memword)
3286 SIM_DESC sd;
3287 int coproc_num, coproc_reg;
3288 unsigned int memword;
3289 {
3290 switch (coproc_num) {
3291 #if defined(HASFPU)
3292 case 1:
3293 #ifdef DEBUG
3294 printf("DBG: COP_LW: memword = 0x%08X (uword64)memword = 0x%s\n",memword,pr_addr(memword));
3295 #endif
3296 StoreFPR(coproc_reg,fmt_word,(uword64)memword);
3297 FPR_STATE[coproc_reg] = fmt_uninterpreted;
3298 break;
3299 #endif /* HASFPU */
3300
3301 default:
3302 #if 0 /* this should be controlled by a configuration option */
3303 sim_io_printf(sd,"COP_LW(%d,%d,0x%08X) at IPC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,memword,pr_addr(IPC));
3304 #endif
3305 break;
3306 }
3307
3308 return;
3309 }
3310
3311 void
3312 cop_ld(sd,coproc_num,coproc_reg,memword)
3313 SIM_DESC sd;
3314 int coproc_num, coproc_reg;
3315 uword64 memword;
3316 {
3317 switch (coproc_num) {
3318 #if defined(HASFPU)
3319 case 1:
3320 StoreFPR(coproc_reg,fmt_uninterpreted,memword);
3321 break;
3322 #endif /* HASFPU */
3323
3324 default:
3325 #if 0 /* this message should be controlled by a configuration option */
3326 sim_io_printf(sd,"COP_LD(%d,%d,0x%s) at IPC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(memword),pr_addr(IPC));
3327 #endif
3328 break;
3329 }
3330
3331 return;
3332 }
3333
3334 unsigned int
3335 cop_sw(sd,coproc_num,coproc_reg)
3336 SIM_DESC sd;
3337 int coproc_num, coproc_reg;
3338 {
3339 unsigned int value = 0;
3340
3341 switch (coproc_num) {
3342 #if defined(HASFPU)
3343 case 1:
3344 #if 1
3345 {
3346 FP_formats hold;
3347 hold = FPR_STATE[coproc_reg];
3348 FPR_STATE[coproc_reg] = fmt_word;
3349 value = (unsigned int)ValueFPR(coproc_reg,fmt_uninterpreted);
3350 FPR_STATE[coproc_reg] = hold;
3351 }
3352 #else
3353 #if 1
3354 value = (unsigned int)ValueFPR(coproc_reg,FPR_STATE[coproc_reg]);
3355 #else
3356 #ifdef DEBUG
3357 printf("DBG: COP_SW: reg in format %s (will be accessing as single)\n",DOFMT(FPR_STATE[coproc_reg]));
3358 #endif /* DEBUG */
3359 value = (unsigned int)ValueFPR(coproc_reg,fmt_single);
3360 #endif
3361 #endif
3362 break;
3363 #endif /* HASFPU */
3364
3365 default:
3366 #if 0 /* should be controlled by configuration option */
3367 sim_io_printf(sd,"COP_SW(%d,%d) at IPC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(IPC));
3368 #endif
3369 break;
3370 }
3371
3372 return(value);
3373 }
3374
3375 uword64
3376 cop_sd(sd,coproc_num,coproc_reg)
3377 SIM_DESC sd;
3378 int coproc_num, coproc_reg;
3379 {
3380 uword64 value = 0;
3381 switch (coproc_num) {
3382 #if defined(HASFPU)
3383 case 1:
3384 #if 1
3385 value = ValueFPR(coproc_reg,fmt_uninterpreted);
3386 #else
3387 #if 1
3388 value = ValueFPR(coproc_reg,FPR_STATE[coproc_reg]);
3389 #else
3390 #ifdef DEBUG
3391 printf("DBG: COP_SD: reg in format %s (will be accessing as double)\n",DOFMT(FPR_STATE[coproc_reg]));
3392 #endif /* DEBUG */
3393 value = ValueFPR(coproc_reg,fmt_double);
3394 #endif
3395 #endif
3396 break;
3397 #endif /* HASFPU */
3398
3399 default:
3400 #if 0 /* should be controlled by configuration option */
3401 sim_io_printf(sd,"COP_SD(%d,%d) at IPC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(IPC));
3402 #endif
3403 break;
3404 }
3405
3406 return(value);
3407 }
3408
3409 void
3410 decode_coproc(sd,instruction)
3411 SIM_DESC sd;
3412 unsigned int instruction;
3413 {
3414 int coprocnum = ((instruction >> 26) & 3);
3415
3416 switch (coprocnum)
3417 {
3418 case 0: /* standard CPU control and cache registers */
3419 {
3420 int code = ((instruction >> 21) & 0x1F);
3421 /* R4000 Users Manual (second edition) lists the following CP0
3422 instructions:
3423 DMFC0 Doubleword Move From CP0 (VR4100 = 01000000001tttttddddd00000000000)
3424 DMTC0 Doubleword Move To CP0 (VR4100 = 01000000101tttttddddd00000000000)
3425 MFC0 word Move From CP0 (VR4100 = 01000000000tttttddddd00000000000)
3426 MTC0 word Move To CP0 (VR4100 = 01000000100tttttddddd00000000000)
3427 TLBR Read Indexed TLB Entry (VR4100 = 01000010000000000000000000000001)
3428 TLBWI Write Indexed TLB Entry (VR4100 = 01000010000000000000000000000010)
3429 TLBWR Write Random TLB Entry (VR4100 = 01000010000000000000000000000110)
3430 TLBP Probe TLB for Matching Entry (VR4100 = 01000010000000000000000000001000)
3431 CACHE Cache operation (VR4100 = 101111bbbbbpppppiiiiiiiiiiiiiiii)
3432 ERET Exception return (VR4100 = 01000010000000000000000000011000)
3433 */
3434 if (((code == 0x00) || (code == 0x04)) && ((instruction & 0x7FF) == 0))
3435 {
3436 int rt = ((instruction >> 16) & 0x1F);
3437 int rd = ((instruction >> 11) & 0x1F);
3438
3439 switch (rd) /* NOTEs: Standard CP0 registers */
3440 {
3441 /* 0 = Index R4000 VR4100 VR4300 */
3442 /* 1 = Random R4000 VR4100 VR4300 */
3443 /* 2 = EntryLo0 R4000 VR4100 VR4300 */
3444 /* 3 = EntryLo1 R4000 VR4100 VR4300 */
3445 /* 4 = Context R4000 VR4100 VR4300 */
3446 /* 5 = PageMask R4000 VR4100 VR4300 */
3447 /* 6 = Wired R4000 VR4100 VR4300 */
3448 /* 8 = BadVAddr R4000 VR4100 VR4300 */
3449 /* 9 = Count R4000 VR4100 VR4300 */
3450 /* 10 = EntryHi R4000 VR4100 VR4300 */
3451 /* 11 = Compare R4000 VR4100 VR4300 */
3452 /* 12 = SR R4000 VR4100 VR4300 */
3453 case 12:
3454 if (code == 0x00)
3455 GPR[rt] = SR;
3456 else
3457 SR = GPR[rt];
3458 break;
3459 /* 13 = Cause R4000 VR4100 VR4300 */
3460 case 13:
3461 if (code == 0x00)
3462 GPR[rt] = CAUSE;
3463 else
3464 CAUSE = GPR[rt];
3465 break;
3466 /* 14 = EPC R4000 VR4100 VR4300 */
3467 /* 15 = PRId R4000 VR4100 VR4300 */
3468 #ifdef SUBTARGET_R3900
3469 /* 16 = Debug */
3470 case 16:
3471 if (code == 0x00)
3472 GPR[rt] = Debug;
3473 else
3474 Debug = GPR[rt];
3475 break;
3476 #else
3477 /* 16 = Config R4000 VR4100 VR4300 */
3478 #endif
3479 #ifdef SUBTARGET_R3900
3480 /* 17 = Debug */
3481 case 17:
3482 if (code == 0x00)
3483 GPR[rt] = DEPC;
3484 else
3485 DEPC = GPR[rt];
3486 break;
3487 #else
3488 /* 17 = LLAddr R4000 VR4100 VR4300 */
3489 #endif
3490 /* 18 = WatchLo R4000 VR4100 VR4300 */
3491 /* 19 = WatchHi R4000 VR4100 VR4300 */
3492 /* 20 = XContext R4000 VR4100 VR4300 */
3493 /* 26 = PErr or ECC R4000 VR4100 VR4300 */
3494 /* 27 = CacheErr R4000 VR4100 */
3495 /* 28 = TagLo R4000 VR4100 VR4300 */
3496 /* 29 = TagHi R4000 VR4100 VR4300 */
3497 /* 30 = ErrorEPC R4000 VR4100 VR4300 */
3498 GPR[rt] = 0xDEADC0DE; /* CPR[0,rd] */
3499 /* CPR[0,rd] = GPR[rt]; */
3500 default:
3501 if (code == 0x00)
3502 sim_io_printf(sd,"Warning: MFC0 %d,%d ignored (architecture specific)\n",rt,rd);
3503 else
3504 sim_io_printf(sd,"Warning: MTC0 %d,%d ignored (architecture specific)\n",rt,rd);
3505 }
3506 }
3507 else if (code == 0x10 && (instruction & 0x3f) == 0x18)
3508 {
3509 /* ERET */
3510 if (SR & status_ERL)
3511 {
3512 /* Oops, not yet available */
3513 sim_io_printf(sd,"Warning: ERET when SR[ERL] set not handled yet");
3514 PC = EPC;
3515 SR &= ~status_ERL;
3516 }
3517 else
3518 {
3519 PC = EPC;
3520 SR &= ~status_EXL;
3521 }
3522 }
3523 else if (code == 0x10 && (instruction & 0x3f) == 0x10)
3524 {
3525 /* RFE */
3526 }
3527 else if (code == 0x10 && (instruction & 0x3f) == 0x1F)
3528 {
3529 /* DERET */
3530 Debug &= ~Debug_DM;
3531 DELAYSLOT();
3532 DSPC = DEPC;
3533 }
3534 else
3535 sim_io_eprintf(sd,"Unrecognised COP0 instruction 0x%08X at IPC = 0x%s : No handler present\n",instruction,pr_addr(IPC));
3536 /* TODO: When executing an ERET or RFE instruction we should
3537 clear LLBIT, to ensure that any out-standing atomic
3538 read/modify/write sequence fails. */
3539 }
3540 break;
3541
3542 case 2: /* undefined co-processor */
3543 sim_io_eprintf(sd,"COP2 instruction 0x%08X at IPC = 0x%s : No handler present\n",instruction,pr_addr(IPC));
3544 break;
3545
3546 case 1: /* should not occur (FPU co-processor) */
3547 case 3: /* should not occur (FPU co-processor) */
3548 SignalException(ReservedInstruction,instruction);
3549 break;
3550 }
3551
3552 return;
3553 }
3554
3555 /*-- instruction simulation -------------------------------------------------*/
3556
3557 void
3558 sim_engine_run (sd, next_cpu_nr, siggnal)
3559 SIM_DESC sd;
3560 int next_cpu_nr; /* ignore */
3561 int siggnal; /* ignore */
3562 {
3563 #if !defined(FASTSIM)
3564 unsigned int pipeline_count = 1;
3565 #endif
3566
3567 #ifdef DEBUG
3568 if (STATE_MEMORY (sd) == NULL) {
3569 printf("DBG: simulate() entered with no memory\n");
3570 exit(1);
3571 }
3572 #endif /* DEBUG */
3573
3574 #if 0 /* Disabled to check that everything works OK */
3575 /* The VR4300 seems to sign-extend the PC on its first
3576 access. However, this may just be because it is currently
3577 configured in 32bit mode. However... */
3578 PC = SIGNEXTEND(PC,32);
3579 #endif
3580
3581 /* main controlling loop */
3582 while (1) {
3583 /* Fetch the next instruction from the simulator memory: */
3584 uword64 vaddr = (uword64)PC;
3585 uword64 paddr;
3586 int cca;
3587 unsigned int instruction; /* uword64? what's this used for? FIXME! */
3588
3589 #ifdef DEBUG
3590 {
3591 printf("DBG: state = 0x%08X :",state);
3592 #if 0
3593 if (state & simSTOP) printf(" simSTOP");
3594 if (state & simSTEP) printf(" simSTEP");
3595 #endif
3596 if (state & simHALTEX) printf(" simHALTEX");
3597 if (state & simHALTIN) printf(" simHALTIN");
3598 #if 0
3599 if (state & simBE) printf(" simBE");
3600 #endif
3601 printf("\n");
3602 }
3603 #endif /* DEBUG */
3604
3605 DSSTATE = (STATE & simDELAYSLOT);
3606 #ifdef DEBUG
3607 if (dsstate)
3608 sim_io_printf(sd,"DBG: DSPC = 0x%s\n",pr_addr(DSPC));
3609 #endif /* DEBUG */
3610
3611 if (AddressTranslation(PC,isINSTRUCTION,isLOAD,&paddr,&cca,isTARGET,isREAL)) {
3612 if ((vaddr & 1) == 0) {
3613 /* Copy the action of the LW instruction */
3614 unsigned int reverse = (ReverseEndian ? (LOADDRMASK >> 2) : 0);
3615 unsigned int bigend = (BigEndianCPU ? (LOADDRMASK >> 2) : 0);
3616 uword64 value;
3617 unsigned int byte;
3618 paddr = ((paddr & ~LOADDRMASK) | ((paddr & LOADDRMASK) ^ (reverse << 2)));
3619 LoadMemory(&value,NULL,cca,AccessLength_WORD,paddr,vaddr,isINSTRUCTION,isREAL);
3620 byte = ((vaddr & LOADDRMASK) ^ (bigend << 2));
3621 instruction = ((value >> (8 * byte)) & 0xFFFFFFFF);
3622 } else {
3623 /* Copy the action of the LH instruction */
3624 unsigned int reverse = (ReverseEndian ? (LOADDRMASK >> 1) : 0);
3625 unsigned int bigend = (BigEndianCPU ? (LOADDRMASK >> 1) : 0);
3626 uword64 value;
3627 unsigned int byte;
3628 paddr = (((paddr & ~ (uword64) 1) & ~LOADDRMASK)
3629 | (((paddr & ~ (uword64) 1) & LOADDRMASK) ^ (reverse << 1)));
3630 LoadMemory(&value,NULL,cca, AccessLength_HALFWORD,
3631 paddr & ~ (uword64) 1,
3632 vaddr, isINSTRUCTION, isREAL);
3633 byte = (((vaddr &~ (uword64) 1) & LOADDRMASK) ^ (bigend << 1));
3634 instruction = ((value >> (8 * byte)) & 0xFFFF);
3635 }
3636 } else {
3637 fprintf(stderr,"Cannot translate address for PC = 0x%s failed\n",pr_addr(PC));
3638 exit(1);
3639 }
3640
3641 #ifdef DEBUG
3642 sim_io_printf(sd,"DBG: fetched 0x%08X from PC = 0x%s\n",instruction,pr_addr(PC));
3643 #endif /* DEBUG */
3644
3645 IPC = PC; /* copy PC for this instruction */
3646 /* This is required by exception processing, to ensure that we can
3647 cope with exceptions in the delay slots of branches that may
3648 already have changed the PC. */
3649 if ((vaddr & 1) == 0)
3650 PC += 4; /* increment ready for the next fetch */
3651 else
3652 PC += 2;
3653 /* NOTE: If we perform a delay slot change to the PC, this
3654 increment is not requuired. However, it would make the
3655 simulator more complicated to try and avoid this small hit. */
3656
3657 /* Currently this code provides a simple model. For more
3658 complicated models we could perform exception status checks at
3659 this point, and set the simSTOP state as required. This could
3660 also include processing any hardware interrupts raised by any
3661 I/O model attached to the simulator context.
3662
3663 Support for "asynchronous" I/O events within the simulated world
3664 could be providing by managing a counter, and calling a I/O
3665 specific handler when a particular threshold is reached. On most
3666 architectures a decrement and check for zero operation is
3667 usually quicker than an increment and compare. However, the
3668 process of managing a known value decrement to zero, is higher
3669 than the cost of using an explicit value UINT_MAX into the
3670 future. Which system is used will depend on how complicated the
3671 I/O model is, and how much it is likely to affect the simulator
3672 bandwidth.
3673
3674 If events need to be scheduled further in the future than
3675 UINT_MAX event ticks, then the I/O model should just provide its
3676 own counter, triggered from the event system. */
3677
3678 /* MIPS pipeline ticks. To allow for future support where the
3679 pipeline hit of individual instructions is known, this control
3680 loop manages a "pipeline_count" variable. It is initialised to
3681 1 (one), and will only be changed by the simulator engine when
3682 executing an instruction. If the engine does not have access to
3683 pipeline cycle count information then all instructions will be
3684 treated as using a single cycle. NOTE: A standard system is not
3685 provided by the default simulator because different MIPS
3686 architectures have different cycle counts for the same
3687 instructions.
3688
3689 [NOTE: pipeline_count has been replaced the event queue] */
3690
3691 #if defined(HASFPU)
3692 /* Set previous flag, depending on current: */
3693 if (STATE & simPCOC0)
3694 STATE |= simPCOC1;
3695 else
3696 STATE &= ~simPCOC1;
3697 /* and update the current value: */
3698 if (GETFCC(0))
3699 STATE |= simPCOC0;
3700 else
3701 STATE &= ~simPCOC0;
3702 #endif /* HASFPU */
3703
3704 /* NOTE: For multi-context simulation environments the "instruction"
3705 variable should be local to this routine. */
3706
3707 /* Shorthand accesses for engine. Note: If we wanted to use global
3708 variables (and a single-threaded simulator engine), then we can
3709 create the actual variables with these names. */
3710
3711 if (!(STATE & simSKIPNEXT)) {
3712 /* Include the simulator engine */
3713 #include "oengine.c"
3714 #if ((GPRLEN == 64) && !PROCESSOR_64BIT) || ((GPRLEN == 32) && PROCESSOR_64BIT)
3715 #error "Mismatch between run-time simulator code and simulation engine"
3716 #endif
3717 #if (WITH_TARGET_WORD_BITSIZE != GPRLEN)
3718 #error "Mismatch between configure WITH_TARGET_WORD_BITSIZE and gencode GPRLEN"
3719 #endif
3720 #if (WITH_FLOATING_POINT == HARD_FLOATING_POINT != defined (HASFPU))
3721 #error "Mismatch between configure WITH_FLOATING_POINT and gencode HASFPU"
3722 #endif
3723
3724 #if defined(WARN_LOHI)
3725 /* Decrement the HI/LO validity ticks */
3726 if (HIACCESS > 0)
3727 HIACCESS--;
3728 if (LOACCESS > 0)
3729 LOACCESS--;
3730 if (HI1ACCESS > 0)
3731 HI1ACCESS--;
3732 if (LO1ACCESS > 0)
3733 LO1ACCESS--;
3734 #endif /* WARN_LOHI */
3735
3736 /* For certain MIPS architectures, GPR[0] is hardwired to zero. We
3737 should check for it being changed. It is better doing it here,
3738 than within the simulator, since it will help keep the simulator
3739 small. */
3740 if (ZERO != 0) {
3741 #if defined(WARN_ZERO)
3742 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(IPC));
3743 #endif /* WARN_ZERO */
3744 ZERO = 0; /* reset back to zero before next instruction */
3745 }
3746 } else /* simSKIPNEXT check */
3747 STATE &= ~simSKIPNEXT;
3748
3749 /* If the delay slot was active before the instruction is
3750 executed, then update the PC to its new value: */
3751 if (DSSTATE) {
3752 #ifdef DEBUG
3753 printf("DBG: dsstate set before instruction execution - updating PC to 0x%s\n",pr_addr(DSPC));
3754 #endif /* DEBUG */
3755 PC = DSPC;
3756 CANCELDELAYSLOT();
3757 }
3758
3759 if (MIPSISA < 4) { /* The following is only required on pre MIPS IV processors: */
3760 /* Deal with pending register updates: */
3761 #ifdef DEBUG
3762 printf("DBG: EMPTY BEFORE pending_in = %d, pending_out = %d, pending_total = %d\n",pending_in,pending_out,pending_total);
3763 #endif /* DEBUG */
3764 if (PENDING_OUT != PENDING_IN) {
3765 int loop;
3766 int index = PENDING_OUT;
3767 int total = PENDING_TOTAL;
3768 if (PENDING_TOTAL == 0) {
3769 fprintf(stderr,"FATAL: Mis-match on pending update pointers\n");
3770 exit(1);
3771 }
3772 for (loop = 0; (loop < total); loop++) {
3773 #ifdef DEBUG
3774 printf("DBG: BEFORE index = %d, loop = %d\n",index,loop);
3775 #endif /* DEBUG */
3776 if (PENDING_SLOT_REG[index] != (LAST_EMBED_REGNUM + 1)) {
3777 #ifdef DEBUG
3778 printf("pending_slot_count[%d] = %d\n",index,PENDING_SLOT_COUNT[index]);
3779 #endif /* DEBUG */
3780 if (--(PENDING_SLOT_COUNT[index]) == 0) {
3781 #ifdef DEBUG
3782 printf("pending_slot_reg[%d] = %d\n",index,PENDING_SLOT_REG[index]);
3783 printf("pending_slot_value[%d] = 0x%s\n",index,pr_addr(PENDING_SLOT_VALUE[index]));
3784 #endif /* DEBUG */
3785 if (PENDING_SLOT_REG[index] == COCIDX) {
3786 #if defined(HASFPU)
3787 SETFCC(0,((FCR31 & (1 << 23)) ? 1 : 0));
3788 #else
3789 ;
3790 #endif
3791 } else {
3792 REGISTERS[PENDING_SLOT_REG[index]] = PENDING_SLOT_VALUE[index];
3793 #if defined(HASFPU)
3794 /* The only time we have PENDING updates to FPU
3795 registers, is when performing binary transfers. This
3796 means we should update the register type field. */
3797 if ((PENDING_SLOT_REG[index] >= FGRIDX) && (PENDING_SLOT_REG[index] < (FGRIDX + 32)))
3798 FPR_STATE[PENDING_SLOT_REG[index] - FGRIDX] = fmt_uninterpreted;
3799 #endif /* HASFPU */
3800 }
3801 #ifdef DEBUG
3802 printf("registers[%d] = 0x%s\n",PENDING_SLOT_REG[index],pr_addr(REGISTERS[PENDING_SLOT_REG[index]]));
3803 #endif /* DEBUG */
3804 PENDING_SLOT_REG[index] = (LAST_EMBED_REGNUM + 1);
3805 PENDING_OUT++;
3806 if (PENDING_OUT == PSLOTS)
3807 PENDING_OUT = 0;
3808 PENDING_TOTAL--;
3809 }
3810 }
3811 #ifdef DEBUG
3812 printf("DBG: AFTER index = %d, loop = %d\n",index,loop);
3813 #endif /* DEBUG */
3814 index++;
3815 if (index == PSLOTS)
3816 index = 0;
3817 }
3818 }
3819 #ifdef DEBUG
3820 printf("DBG: EMPTY AFTER pending_in = %d, pending_out = %d, pending_total = %d\n",PENDING_IN,PENDING_OUT,PENDING_TOTAL);
3821 #endif /* DEBUG */
3822 }
3823
3824 #if !defined(FASTSIM)
3825 if (sim_events_tickn (sd, pipeline_count))
3826 {
3827 /* cpu->cia = cia; */
3828 sim_events_process (sd);
3829 }
3830 #else
3831 if (sim_events_tick (sd))
3832 {
3833 /* cpu->cia = cia; */
3834 sim_events_process (sd);
3835 }
3836 #endif /* FASTSIM */
3837 }
3838 }
3839
3840 /* This code copied from gdb's utils.c. Would like to share this code,
3841 but don't know of a common place where both could get to it. */
3842
3843 /* Temporary storage using circular buffer */
3844 #define NUMCELLS 16
3845 #define CELLSIZE 32
3846 static char*
3847 get_cell()
3848 {
3849 static char buf[NUMCELLS][CELLSIZE];
3850 static int cell=0;
3851 if (++cell>=NUMCELLS) cell=0;
3852 return buf[cell];
3853 }
3854
3855 /* Print routines to handle variable size regs, etc */
3856
3857 /* Eliminate warning from compiler on 32-bit systems */
3858 static int thirty_two = 32;
3859
3860 char*
3861 pr_addr(addr)
3862 SIM_ADDR addr;
3863 {
3864 char *paddr_str=get_cell();
3865 switch (sizeof(addr))
3866 {
3867 case 8:
3868 sprintf(paddr_str,"%08lx%08lx",
3869 (unsigned long)(addr>>thirty_two),(unsigned long)(addr&0xffffffff));
3870 break;
3871 case 4:
3872 sprintf(paddr_str,"%08lx",(unsigned long)addr);
3873 break;
3874 case 2:
3875 sprintf(paddr_str,"%04x",(unsigned short)(addr&0xffff));
3876 break;
3877 default:
3878 sprintf(paddr_str,"%x",addr);
3879 }
3880 return paddr_str;
3881 }
3882
3883 char*
3884 pr_uword64(addr)
3885 uword64 addr;
3886 {
3887 char *paddr_str=get_cell();
3888 sprintf(paddr_str,"%08lx%08lx",
3889 (unsigned long)(addr>>thirty_two),(unsigned long)(addr&0xffffffff));
3890 return paddr_str;
3891 }
3892
3893
3894 /*---------------------------------------------------------------------------*/
3895 /*> EOF interp.c <*/
This page took 0.109744 seconds and 5 git commands to generate.