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