5 #include "remote-sim.h"
9 #define IMEM_SIZE 18 /* D10V instruction memory size is 18 bits */
10 #define DMEM_SIZE 16 /* Data memory is 64K (but only 32K internal RAM) */
11 #define UMEM_SIZE 17 /* each unified memory region is 17 bits */
13 enum _leftright
{ LEFT_FIRST
, RIGHT_FIRST
};
16 static SIM_OPEN_KIND sim_kind
;
17 static bfd_vma start_address
;
19 host_callback
*d10v_callback
;
20 unsigned long ins_type_counters
[ (int)INS_MAX
];
24 static int init_text_p
= 0;
25 /* non-zero if we opened prog_bfd */
26 static int prog_bfd_was_opened_p
;
32 static long hash
PARAMS ((long insn
, int format
));
33 static struct hash_entry
*lookup_hash
PARAMS ((uint32 ins
, int size
));
34 static void get_operands
PARAMS ((struct simops
*s
, uint32 ins
));
35 static void do_long
PARAMS ((uint32 ins
));
36 static void do_2_short
PARAMS ((uint16 ins1
, uint16 ins2
, enum _leftright leftright
));
37 static void do_parallel
PARAMS ((uint16 ins1
, uint16 ins2
));
38 static char *add_commas
PARAMS ((char *buf
, int sizeof_buf
, unsigned long value
));
39 static void init_system
PARAMS ((void));
40 extern void sim_set_profile
PARAMS ((int n
));
41 extern void sim_set_profile_size
PARAMS ((int n
));
44 #if defined(__GNUC__) && defined(__OPTIMIZE__)
45 #define INLINE __inline__
54 struct hash_entry
*next
;
61 struct hash_entry hash_table
[MAX_HASH
+1];
68 if (format
& LONG_OPCODE
)
69 return ((insn
& 0x3F000000) >> 24);
71 return((insn
& 0x7E00) >> 9);
74 INLINE
static struct hash_entry
*
75 lookup_hash (ins
, size
)
82 h
= &hash_table
[(ins
& 0x3F000000) >> 24];
84 h
= &hash_table
[(ins
& 0x7E00) >> 9];
86 while ((ins
& h
->mask
) != h
->opcode
|| h
->size
!= size
)
90 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR looking up hash for %x at PC %x\n",ins
, PC
);
99 get_operands (struct simops
*s
, uint32 ins
)
101 int i
, shift
, bits
, flags
;
103 for (i
=0; i
< s
->numops
; i
++)
105 shift
= s
->operands
[3*i
];
106 bits
= s
->operands
[3*i
+1];
107 flags
= s
->operands
[3*i
+2];
108 mask
= 0x7FFFFFFF >> (31 - bits
);
109 OP
[i
] = (ins
>> shift
) & mask
;
120 for (s
= prog_bfd
->sections
; s
; s
= s
->next
)
121 if (strcmp (bfd_get_section_name (prog_bfd
, s
), ".text") == 0)
124 text_start
= bfd_get_section_vma (prog_bfd
, s
);
125 text_end
= text_start
+ bfd_section_size (prog_bfd
, s
);
130 return (PC
<< 2) + text_start
;
137 struct hash_entry
*h
;
139 if ((d10v_debug
& DEBUG_INSTRUCTION
) != 0)
140 (*d10v_callback
->printf_filtered
) (d10v_callback
, "do_long 0x%x\n", ins
);
142 h
= lookup_hash (ins
, 1);
143 get_operands (h
->ops
, ins
);
144 State
.ins_type
= INS_LONG
;
145 ins_type_counters
[ (int)State
.ins_type
]++;
150 do_2_short (ins1
, ins2
, leftright
)
152 enum _leftright leftright
;
154 struct hash_entry
*h
;
156 enum _ins_type first
, second
;
159 if ((d10v_debug
& DEBUG_INSTRUCTION
) != 0)
160 (*d10v_callback
->printf_filtered
) (d10v_callback
, "do_2_short 0x%x (%s) -> 0x%x\n",
161 ins1
, (leftright
) ? "left" : "right", ins2
);
164 if (leftright
== LEFT_FIRST
)
168 ins_type_counters
[ (int)INS_LEFTRIGHT
]++;
174 ins_type_counters
[ (int)INS_RIGHTLEFT
]++;
177 h
= lookup_hash (ins1
, 0);
178 get_operands (h
->ops
, ins1
);
179 State
.ins_type
= first
;
180 ins_type_counters
[ (int)State
.ins_type
]++;
183 /* If the PC has changed (ie, a jump), don't do the second instruction */
184 if (orig_pc
== PC
&& !State
.exception
)
186 h
= lookup_hash (ins2
, 0);
187 get_operands (h
->ops
, ins2
);
188 State
.ins_type
= second
;
189 ins_type_counters
[ (int)State
.ins_type
]++;
190 ins_type_counters
[ (int)INS_CYCLES
]++;
193 else if (orig_pc
!= PC
&& !State
.exception
)
194 ins_type_counters
[ (int)INS_COND_JUMP
]++;
198 do_parallel (ins1
, ins2
)
201 struct hash_entry
*h1
, *h2
;
203 if ((d10v_debug
& DEBUG_INSTRUCTION
) != 0)
204 (*d10v_callback
->printf_filtered
) (d10v_callback
, "do_parallel 0x%x || 0x%x\n", ins1
, ins2
);
206 ins_type_counters
[ (int)INS_PARALLEL
]++;
207 h1
= lookup_hash (ins1
, 0);
208 h2
= lookup_hash (ins2
, 0);
210 if (h1
->ops
->exec_type
== PARONLY
)
212 get_operands (h1
->ops
, ins1
);
213 State
.ins_type
= INS_LEFT_COND_TEST
;
214 ins_type_counters
[ (int)State
.ins_type
]++;
218 ins_type_counters
[ (int)INS_COND_TRUE
]++;
219 get_operands (h2
->ops
, ins2
);
220 State
.ins_type
= INS_RIGHT_COND_EXE
;
221 ins_type_counters
[ (int)State
.ins_type
]++;
225 ins_type_counters
[ (int)INS_COND_FALSE
]++;
227 else if (h2
->ops
->exec_type
== PARONLY
)
229 get_operands (h2
->ops
, ins2
);
230 State
.ins_type
= INS_RIGHT_COND_TEST
;
231 ins_type_counters
[ (int)State
.ins_type
]++;
235 ins_type_counters
[ (int)INS_COND_TRUE
]++;
236 get_operands (h1
->ops
, ins1
);
237 State
.ins_type
= INS_LEFT_COND_EXE
;
238 ins_type_counters
[ (int)State
.ins_type
]++;
242 ins_type_counters
[ (int)INS_COND_FALSE
]++;
246 get_operands (h1
->ops
, ins1
);
247 State
.ins_type
= INS_LEFT_PARALLEL
;
248 ins_type_counters
[ (int)State
.ins_type
]++;
250 if (!State
.exception
)
252 get_operands (h2
->ops
, ins2
);
253 State
.ins_type
= INS_RIGHT_PARALLEL
;
254 ins_type_counters
[ (int)State
.ins_type
]++;
261 add_commas(buf
, sizeof_buf
, value
)
267 char *endbuf
= buf
+ sizeof_buf
- 1;
277 *--endbuf
= (value
% 10) + '0';
278 } while ((value
/= 10) != 0);
296 free (State
.umem
[i
]);
297 State
.umem
[i
] = NULL
;
304 State
.imem
= (uint8
*)calloc(1,1<<IMEM_SIZE
);
305 State
.dmem
= (uint8
*)calloc(1,1<<DMEM_SIZE
);
307 State
.umem
[i
] = NULL
;
308 State
.umem
[0] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
309 State
.umem
[1] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
310 State
.umem
[2] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
311 State
.umem
[127] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
312 if (!State
.imem
|| !State
.dmem
|| !State
.umem
[0] || !State
.umem
[1] || !State
.umem
[2] || !State
.umem
[127] )
314 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Memory allocation failed.\n");
323 if ((d10v_debug
& DEBUG_MEMSIZE
) != 0)
326 (*d10v_callback
->printf_filtered
) (d10v_callback
,
327 "Allocated %s bytes instruction memory and\n",
328 add_commas (buffer
, sizeof (buffer
), (1UL<<IMEM_SIZE
)));
330 (*d10v_callback
->printf_filtered
) (d10v_callback
, " %s bytes data memory.\n",
331 add_commas (buffer
, sizeof (buffer
), (1UL<<IMEM_SIZE
)));
344 xfer_mem (addr
, buffer
, size
, write
)
346 unsigned char *buffer
;
354 if ((d10v_debug
& DEBUG_INSTRUCTION
) != 0)
357 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_write %d bytes to 0x%x\n", size
, addr
);
359 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_read %d bytes from 0x%x\n", size
, addr
);
363 /* to access data, we use the following mapping */
364 /* 0x01000000 - 0x0103ffff : instruction memory */
365 /* 0x02000000 - 0x0200ffff : data memory */
366 /* 0x00000000 - 0x00ffffff : unified memory */
368 if ( (addr
& 0x03000000) == 0)
372 segment
= addr
>> UMEM_SIZE
;
374 if (!State
.umem
[segment
])
377 (*d10v_callback
->printf_filtered
) (d10v_callback
,"Allocating %s bytes unified memory to region %d\n",
378 add_commas (buffer
, sizeof (buffer
), (1UL<<IMEM_SIZE
)), segment
);
380 State
.umem
[segment
] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
382 if (!State
.umem
[segment
])
384 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Memory allocation failed.\n");
387 /* FIXME: need to check size and read/write multiple segments if necessary */
389 memcpy (State
.umem
[segment
]+addr
, buffer
, size
) ;
391 memcpy (buffer
, State
.umem
[segment
]+addr
, size
);
393 else if ( (addr
& 0x03000000) == 0x02000000)
397 if (size
> (1<<(DMEM_SIZE
-1)))
399 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: data section is only %d bytes.\n",1<<(DMEM_SIZE
-1));
403 memcpy (State
.dmem
+addr
, buffer
, size
);
405 memcpy (buffer
, State
.dmem
+addr
, size
);
407 else if ( (addr
& 0x03000000) == 0x01000000)
409 /* INSTRUCTION MEMORY */
411 if (size
> (1<<IMEM_SIZE
))
413 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: inst section is only %d bytes.\n",1<<IMEM_SIZE
);
417 memcpy (State
.imem
+addr
, buffer
, size
);
419 memcpy (buffer
, State
.imem
+addr
, size
);
423 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: address 0x%x is not in valid range\n",addr
);
424 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Instruction addresses start at 0x01000000\n");
425 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Data addresses start at 0x02000000\n");
426 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Unified addresses start at 0x00000000\n");
437 sim_write (sd
, addr
, buffer
, size
)
440 unsigned char *buffer
;
443 return xfer_mem( addr
, buffer
, size
, 1);
447 sim_read (sd
, addr
, buffer
, size
)
450 unsigned char *buffer
;
453 return xfer_mem( addr
, buffer
, size
, 0);
458 sim_open (kind
, callback
, abfd
, argv
)
460 host_callback
*callback
;
465 struct hash_entry
*h
;
466 static int init_p
= 0;
470 d10v_callback
= callback
;
473 for (p
= argv
+ 1; *p
; ++p
)
476 if (strcmp (*p
, "-t") == 0)
480 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: unsupported option(s): %s\n",*p
);
483 /* put all the opcodes in the hash table */
486 for (s
= Simops
; s
->func
; s
++)
488 h
= &hash_table
[hash(s
->opcode
,s
->format
)];
490 /* go to the last entry in the chain */
496 h
->next
= (struct hash_entry
*) calloc(1,sizeof(struct hash_entry
));
498 perror ("malloc failure");
504 h
->opcode
= s
->opcode
;
505 h
->size
= s
->is_long
;
509 /* Fudge our descriptor. */
515 sim_close (sd
, quitting
)
519 if (prog_bfd
!= NULL
&& prog_bfd_was_opened_p
)
520 bfd_close (prog_bfd
);
527 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_set_profile %d\n",n
);
531 sim_set_profile_size (n
)
534 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_set_profile_size %d\n",n
);
548 if ( (addr
& 0xfff0) != 0xff00)
550 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Data address 0x%lx is in I/O space, pc = 0x%lx.\n",
551 (long)addr
, (long)decode_pc ());
552 State
.exception
= SIGBUS
;
555 return State
.dmem
+ addr
;
562 /* instruction memory */
563 return (DMAP
& 0xf) * 0x4000 + State
.imem
;
566 /* this is ugly because we allocate unified memory in 128K segments and */
567 /* dmap addresses 16k segments */
568 seg
= (DMAP
& 0x3ff) >> 3;
569 if (State
.umem
[seg
] == NULL
)
571 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: unified memory region %d unmapped, pc = 0x%lx\n",
572 seg
, (long)decode_pc ());
573 State
.exception
= SIGBUS
;
575 return State
.umem
[seg
] + (DMAP
& 7) * 0x4000;
578 return State
.dmem
+ addr
;
585 uint32 pc
= ((uint32
)PC
) << 2;
594 return State
.imem
+ pc
;
596 if (State
.umem
[imap
& 0xff] == NULL
)
598 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: unified memory region %d unmapped, pc = 0x%lx\n",
599 imap
& 0xff, (long)PC
);
600 State
.exception
= SIGBUS
;
604 return State
.umem
[imap
& 0xff] + pc
;
608 static int stop_simulator
;
626 /* Run (or resume) the program. */
628 sim_resume (sd
, step
, siggnal
)
635 /* (*d10v_callback->printf_filtered) (d10v_callback, "sim_resume (%d,%d) PC=0x%x\n",step,siggnal,PC); */
637 prev
= signal(SIGINT
, sim_ctrl_c
);
638 stop_simulator
= step
;
642 inst
= get_longword( pc_addr() );
643 State
.pc_changed
= 0;
644 ins_type_counters
[ (int)INS_CYCLES
]++;
645 switch (inst
& 0xC0000000)
648 /* long instruction */
649 do_long (inst
& 0x3FFFFFFF);
653 do_2_short ( inst
& 0x7FFF, (inst
& 0x3FFF8000) >> 15, 0);
657 do_2_short ((inst
& 0x3FFF8000) >> 15, inst
& 0x7FFF, 1);
660 do_parallel ((inst
& 0x3FFF8000) >> 15, inst
& 0x7FFF);
664 if (State
.RP
&& PC
== RPT_E
)
672 else if (!State
.pc_changed
)
675 while ( !State
.exception
&& !stop_simulator
);
677 if (step
&& !State
.exception
)
678 State
.exception
= SIGTRAP
;
680 signal(SIGINT
, prev
);
690 sim_resume (sd
, 0, 0);
695 sim_info (sd
, verbose
)
704 unsigned long left
= ins_type_counters
[ (int)INS_LEFT
] + ins_type_counters
[ (int)INS_LEFT_COND_EXE
];
705 unsigned long left_nops
= ins_type_counters
[ (int)INS_LEFT_NOPS
];
706 unsigned long left_parallel
= ins_type_counters
[ (int)INS_LEFT_PARALLEL
];
707 unsigned long left_cond
= ins_type_counters
[ (int)INS_LEFT_COND_TEST
];
708 unsigned long left_total
= left
+ left_parallel
+ left_cond
+ left_nops
;
710 unsigned long right
= ins_type_counters
[ (int)INS_RIGHT
] + ins_type_counters
[ (int)INS_RIGHT_COND_EXE
];
711 unsigned long right_nops
= ins_type_counters
[ (int)INS_RIGHT_NOPS
];
712 unsigned long right_parallel
= ins_type_counters
[ (int)INS_RIGHT_PARALLEL
];
713 unsigned long right_cond
= ins_type_counters
[ (int)INS_RIGHT_COND_TEST
];
714 unsigned long right_total
= right
+ right_parallel
+ right_cond
+ right_nops
;
716 unsigned long unknown
= ins_type_counters
[ (int)INS_UNKNOWN
];
717 unsigned long ins_long
= ins_type_counters
[ (int)INS_LONG
];
718 unsigned long parallel
= ins_type_counters
[ (int)INS_PARALLEL
];
719 unsigned long leftright
= ins_type_counters
[ (int)INS_LEFTRIGHT
];
720 unsigned long rightleft
= ins_type_counters
[ (int)INS_RIGHTLEFT
];
721 unsigned long cond_true
= ins_type_counters
[ (int)INS_COND_TRUE
];
722 unsigned long cond_false
= ins_type_counters
[ (int)INS_COND_FALSE
];
723 unsigned long cond_jump
= ins_type_counters
[ (int)INS_COND_JUMP
];
724 unsigned long cycles
= ins_type_counters
[ (int)INS_CYCLES
];
725 unsigned long total
= (unknown
+ left_total
+ right_total
+ ins_long
);
727 int size
= strlen (add_commas (buf1
, sizeof (buf1
), total
));
728 int parallel_size
= strlen (add_commas (buf1
, sizeof (buf1
),
729 (left_parallel
> right_parallel
) ? left_parallel
: right_parallel
));
730 int cond_size
= strlen (add_commas (buf1
, sizeof (buf1
), (left_cond
> right_cond
) ? left_cond
: right_cond
));
731 int nop_size
= strlen (add_commas (buf1
, sizeof (buf1
), (left_nops
> right_nops
) ? left_nops
: right_nops
));
732 int normal_size
= strlen (add_commas (buf1
, sizeof (buf1
), (left
> right
) ? left
: right
));
734 (*d10v_callback
->printf_filtered
) (d10v_callback
,
735 "executed %*s left instruction(s), %*s normal, %*s parallel, %*s EXExxx, %*s nops\n",
736 size
, add_commas (buf1
, sizeof (buf1
), left_total
),
737 normal_size
, add_commas (buf2
, sizeof (buf2
), left
),
738 parallel_size
, add_commas (buf3
, sizeof (buf3
), left_parallel
),
739 cond_size
, add_commas (buf4
, sizeof (buf4
), left_cond
),
740 nop_size
, add_commas (buf5
, sizeof (buf5
), left_nops
));
742 (*d10v_callback
->printf_filtered
) (d10v_callback
,
743 "executed %*s right instruction(s), %*s normal, %*s parallel, %*s EXExxx, %*s nops\n",
744 size
, add_commas (buf1
, sizeof (buf1
), right_total
),
745 normal_size
, add_commas (buf2
, sizeof (buf2
), right
),
746 parallel_size
, add_commas (buf3
, sizeof (buf3
), right_parallel
),
747 cond_size
, add_commas (buf4
, sizeof (buf4
), right_cond
),
748 nop_size
, add_commas (buf5
, sizeof (buf5
), right_nops
));
751 (*d10v_callback
->printf_filtered
) (d10v_callback
,
752 "executed %*s long instruction(s)\n",
753 size
, add_commas (buf1
, sizeof (buf1
), ins_long
));
756 (*d10v_callback
->printf_filtered
) (d10v_callback
,
757 "executed %*s parallel instruction(s)\n",
758 size
, add_commas (buf1
, sizeof (buf1
), parallel
));
761 (*d10v_callback
->printf_filtered
) (d10v_callback
,
762 "executed %*s instruction(s) encoded L->R\n",
763 size
, add_commas (buf1
, sizeof (buf1
), leftright
));
766 (*d10v_callback
->printf_filtered
) (d10v_callback
,
767 "executed %*s instruction(s) encoded R->L\n",
768 size
, add_commas (buf1
, sizeof (buf1
), rightleft
));
771 (*d10v_callback
->printf_filtered
) (d10v_callback
,
772 "executed %*s unknown instruction(s)\n",
773 size
, add_commas (buf1
, sizeof (buf1
), unknown
));
776 (*d10v_callback
->printf_filtered
) (d10v_callback
,
777 "executed %*s instruction(s) due to EXExxx condition being true\n",
778 size
, add_commas (buf1
, sizeof (buf1
), cond_true
));
781 (*d10v_callback
->printf_filtered
) (d10v_callback
,
782 "skipped %*s instruction(s) due to EXExxx condition being false\n",
783 size
, add_commas (buf1
, sizeof (buf1
), cond_false
));
786 (*d10v_callback
->printf_filtered
) (d10v_callback
,
787 "skipped %*s instruction(s) due to conditional branch succeeding\n",
788 size
, add_commas (buf1
, sizeof (buf1
), cond_jump
));
790 (*d10v_callback
->printf_filtered
) (d10v_callback
,
791 "executed %*s cycle(s)\n",
792 size
, add_commas (buf1
, sizeof (buf1
), cycles
));
794 (*d10v_callback
->printf_filtered
) (d10v_callback
,
795 "executed %*s total instructions\n",
796 size
, add_commas (buf1
, sizeof (buf1
), total
));
800 sim_create_inferior (sd
, argv
, env
)
807 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_create_inferior: PC=0x%x\n", start_address
);
810 /* reset all state information */
811 memset (&State
.regs
, 0, (int)&State
.imem
- (int)&State
.regs
[0]);
814 PC
= start_address
>> 2;
816 /* cpu resets imap0 to 0 and imap1 to 0x7f, but D10V-EVA board */
817 /* resets imap0 and imap1 to 0x1000. */
835 sim_set_callbacks (p
)
842 sim_stop_reason (sd
, reason
, sigrc
)
844 enum sim_stop
*reason
;
847 /* (*d10v_callback->printf_filtered) (d10v_callback, "sim_stop_reason: PC=0x%x\n",PC<<2); */
849 switch (State
.exception
)
851 case SIG_D10V_STOP
: /* stop instruction */
852 *reason
= sim_exited
;
856 case SIG_D10V_EXIT
: /* exit trap */
857 *reason
= sim_exited
;
858 *sigrc
= State
.regs
[2];
861 default: /* some signal */
862 *reason
= sim_stopped
;
863 *sigrc
= State
.exception
;
869 sim_fetch_register (sd
, rn
, memory
)
872 unsigned char *memory
;
878 WRITE_64 (memory
, State
.a
[rn
-35]);
880 WRITE_16 (memory
, IMAP0
);
882 WRITE_16 (memory
, IMAP1
);
884 WRITE_16 (memory
, DMAP
);
886 WRITE_16 (memory
, State
.regs
[rn
]);
890 sim_store_register (sd
, rn
, memory
)
893 unsigned char *memory
;
899 State
.a
[rn
-35] = READ_64 (memory
) & MASK40
;
901 SET_DMAP( READ_16(memory
) );
903 SET_IMAP1( READ_16(memory
) );
905 SET_IMAP0( READ_16(memory
) );
907 State
.regs
[rn
]= READ_16 (memory
);
912 sim_do_command (sd
, cmd
)
916 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_do_command: %s\n",cmd
);
920 sim_load (sd
, prog
, abfd
, from_tty
)
926 extern bfd
*sim_load_file (); /* ??? Don't know where this should live. */
928 if (prog_bfd
!= NULL
&& prog_bfd_was_opened_p
)
929 bfd_close (prog_bfd
);
930 prog_bfd
= sim_load_file (sd
, myname
, d10v_callback
, prog
, abfd
,
931 sim_kind
== SIM_OPEN_DEBUG
);
932 if (prog_bfd
== NULL
)
934 start_address
= bfd_get_start_address (prog_bfd
);
935 prog_bfd_was_opened_p
= abfd
== NULL
;
This page took 0.083034 seconds and 4 git commands to generate.