4 #include "remote-sim.h"
8 #define IMEM_SIZE 18 /* D10V instruction memory size is 18 bits */
9 #define DMEM_SIZE 16 /* Data memory is 64K (but only 32K internal RAM) */
10 #define UMEM_SIZE 17 /* each unified memory region is 17 bits */
12 enum _leftright
{ LEFT_FIRST
, RIGHT_FIRST
};
15 host_callback
*d10v_callback
;
16 unsigned long ins_type_counters
[ (int)INS_MAX
];
20 static int init_text_p
= 0;
25 static long hash
PARAMS ((long insn
, int format
));
26 static struct hash_entry
*lookup_hash
PARAMS ((uint32 ins
, int size
));
27 static void get_operands
PARAMS ((struct simops
*s
, uint32 ins
));
28 static void do_long
PARAMS ((uint32 ins
));
29 static void do_2_short
PARAMS ((uint16 ins1
, uint16 ins2
, enum _leftright leftright
));
30 static void do_parallel
PARAMS ((uint16 ins1
, uint16 ins2
));
31 static char *add_commas
PARAMS ((char *buf
, int sizeof_buf
, unsigned long value
));
32 extern void sim_size
PARAMS ((int power
));
33 static void init_system
PARAMS ((void));
34 extern int sim_write
PARAMS ((SIM_ADDR addr
, unsigned char *buffer
, int size
));
35 extern void sim_open
PARAMS ((char *args
));
36 extern void sim_close
PARAMS ((int quitting
));
37 extern void sim_set_profile
PARAMS ((int n
));
38 extern void sim_set_profile_size
PARAMS ((int n
));
39 extern void sim_resume
PARAMS ((int step
, int siggnal
));
40 extern void sim_info
PARAMS ((int verbose
));
41 extern void sim_create_inferior
PARAMS ((SIM_ADDR start_address
, char **argv
, char **env
));
42 extern void sim_kill
PARAMS ((void));
43 extern void sim_set_callbacks
PARAMS ((host_callback
*p
));
44 extern void sim_stop_reason
PARAMS ((enum sim_stop
*reason
, int *sigrc
));
45 extern void sim_fetch_register
PARAMS ((int rn
, unsigned char *memory
));
46 extern void sim_store_register
PARAMS ((int rn
, unsigned char *memory
));
47 extern int sim_read
PARAMS ((SIM_ADDR addr
, unsigned char *buffer
, int size
));
48 extern void sim_do_command
PARAMS ((char *cmd
));
51 #if defined(__GNUC__) && defined(__OPTIMIZE__)
52 #define INLINE __inline__
61 struct hash_entry
*next
;
68 struct hash_entry hash_table
[MAX_HASH
+1];
75 if (format
& LONG_OPCODE
)
76 return ((insn
& 0x3F000000) >> 24);
78 return((insn
& 0x7E00) >> 9);
81 INLINE
static struct hash_entry
*
82 lookup_hash (ins
, size
)
89 h
= &hash_table
[(ins
& 0x3F000000) >> 24];
91 h
= &hash_table
[(ins
& 0x7E00) >> 9];
93 while ((ins
& h
->mask
) != h
->opcode
|| h
->size
!= size
)
97 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR looking up hash for %x at PC %x\n",ins
, PC
);
106 get_operands (struct simops
*s
, uint32 ins
)
108 int i
, shift
, bits
, flags
;
110 for (i
=0; i
< s
->numops
; i
++)
112 shift
= s
->operands
[3*i
];
113 bits
= s
->operands
[3*i
+1];
114 flags
= s
->operands
[3*i
+2];
115 mask
= 0x7FFFFFFF >> (31 - bits
);
116 OP
[i
] = (ins
>> shift
) & mask
;
127 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
128 if (strcmp (bfd_get_section_name (exec_bfd
, s
), ".text") == 0)
131 text_start
= bfd_get_section_vma (exec_bfd
, s
);
132 text_end
= text_start
+ bfd_section_size (exec_bfd
, s
);
137 return (PC
<< 2) + text_start
;
144 struct hash_entry
*h
;
146 if ((d10v_debug
& DEBUG_INSTRUCTION
) != 0)
147 (*d10v_callback
->printf_filtered
) (d10v_callback
, "do_long 0x%x\n", ins
);
149 h
= lookup_hash (ins
, 1);
150 get_operands (h
->ops
, ins
);
151 State
.ins_type
= INS_LONG
;
152 ins_type_counters
[ (int)State
.ins_type
]++;
157 do_2_short (ins1
, ins2
, leftright
)
159 enum _leftright leftright
;
161 struct hash_entry
*h
;
163 enum _ins_type first
, second
;
166 if ((d10v_debug
& DEBUG_INSTRUCTION
) != 0)
167 (*d10v_callback
->printf_filtered
) (d10v_callback
, "do_2_short 0x%x (%s) -> 0x%x\n",
168 ins1
, (leftright
) ? "left" : "right", ins2
);
171 if (leftright
== LEFT_FIRST
)
175 ins_type_counters
[ (int)INS_LEFTRIGHT
]++;
181 ins_type_counters
[ (int)INS_RIGHTLEFT
]++;
184 h
= lookup_hash (ins1
, 0);
185 get_operands (h
->ops
, ins1
);
186 State
.ins_type
= first
;
187 ins_type_counters
[ (int)State
.ins_type
]++;
190 /* If the PC has changed (ie, a jump), don't do the second instruction */
191 if (orig_pc
== PC
&& !State
.exception
)
193 h
= lookup_hash (ins2
, 0);
194 get_operands (h
->ops
, ins2
);
195 State
.ins_type
= second
;
196 ins_type_counters
[ (int)State
.ins_type
]++;
197 ins_type_counters
[ (int)INS_CYCLES
]++;
200 else if (orig_pc
!= PC
&& !State
.exception
)
201 ins_type_counters
[ (int)INS_COND_JUMP
]++;
205 do_parallel (ins1
, ins2
)
208 struct hash_entry
*h1
, *h2
;
210 if ((d10v_debug
& DEBUG_INSTRUCTION
) != 0)
211 (*d10v_callback
->printf_filtered
) (d10v_callback
, "do_parallel 0x%x || 0x%x\n", ins1
, ins2
);
213 ins_type_counters
[ (int)INS_PARALLEL
]++;
214 h1
= lookup_hash (ins1
, 0);
215 h2
= lookup_hash (ins2
, 0);
217 if (h1
->ops
->exec_type
== PARONLY
)
219 get_operands (h1
->ops
, ins1
);
220 State
.ins_type
= INS_LEFT_COND_TEST
;
221 ins_type_counters
[ (int)State
.ins_type
]++;
225 ins_type_counters
[ (int)INS_COND_TRUE
]++;
226 get_operands (h2
->ops
, ins2
);
227 State
.ins_type
= INS_RIGHT_COND_EXE
;
228 ins_type_counters
[ (int)State
.ins_type
]++;
232 ins_type_counters
[ (int)INS_COND_FALSE
]++;
234 else if (h2
->ops
->exec_type
== PARONLY
)
236 get_operands (h2
->ops
, ins2
);
237 State
.ins_type
= INS_RIGHT_COND_TEST
;
238 ins_type_counters
[ (int)State
.ins_type
]++;
242 ins_type_counters
[ (int)INS_COND_TRUE
]++;
243 get_operands (h1
->ops
, ins1
);
244 State
.ins_type
= INS_LEFT_COND_EXE
;
245 ins_type_counters
[ (int)State
.ins_type
]++;
249 ins_type_counters
[ (int)INS_COND_FALSE
]++;
253 get_operands (h1
->ops
, ins1
);
254 State
.ins_type
= INS_LEFT_PARALLEL
;
255 ins_type_counters
[ (int)State
.ins_type
]++;
257 if (!State
.exception
)
259 get_operands (h2
->ops
, ins2
);
260 State
.ins_type
= INS_RIGHT_PARALLEL
;
261 ins_type_counters
[ (int)State
.ins_type
]++;
268 add_commas(buf
, sizeof_buf
, value
)
274 char *endbuf
= buf
+ sizeof_buf
- 1;
284 *--endbuf
= (value
% 10) + '0';
285 } while ((value
/= 10) != 0);
303 free (State
.umem
[i
]);
304 State
.umem
[i
] = NULL
;
311 State
.imem
= (uint8
*)calloc(1,1<<IMEM_SIZE
);
312 State
.dmem
= (uint8
*)calloc(1,1<<DMEM_SIZE
);
314 State
.umem
[i
] = NULL
;
315 State
.umem
[0] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
316 State
.umem
[1] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
317 State
.umem
[2] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
318 State
.umem
[127] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
319 if (!State
.imem
|| !State
.dmem
|| !State
.umem
[0] || !State
.umem
[1] || !State
.umem
[2] || !State
.umem
[127] )
321 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Memory allocation failed.\n");
330 if ((d10v_debug
& DEBUG_MEMSIZE
) != 0)
333 (*d10v_callback
->printf_filtered
) (d10v_callback
,
334 "Allocated %s bytes instruction memory and\n",
335 add_commas (buffer
, sizeof (buffer
), (1UL<<IMEM_SIZE
)));
337 (*d10v_callback
->printf_filtered
) (d10v_callback
, " %s bytes data memory.\n",
338 add_commas (buffer
, sizeof (buffer
), (1UL<<IMEM_SIZE
)));
351 xfer_mem (addr
, buffer
, size
, write
)
353 unsigned char *buffer
;
361 if ((d10v_debug
& DEBUG_INSTRUCTION
) != 0)
364 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_write %d bytes to 0x%x\n", size
, addr
);
366 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_read %d bytes from 0x%x\n", size
, addr
);
370 /* to access data, we use the following mapping */
371 /* 0x01000000 - 0x0103ffff : instruction memory */
372 /* 0x02000000 - 0x0200ffff : data memory */
373 /* 0x03000000 - 0x03ffffff : unified memory */
375 if ( (addr
& 0x03000000) == 0x03000000)
380 segment
= addr
>> UMEM_SIZE
;
382 if (!State
.umem
[segment
])
383 State
.umem
[segment
] = (uint8
*)calloc(1,1<<UMEM_SIZE
);
384 if (!State
.umem
[segment
])
386 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Memory allocation failed.\n");
390 (*d10v_callback
->printf_filtered
) (d10v_callback
,"Allocated %s bytes unified memory to region %d\n",
391 add_commas (buffer
, sizeof (buffer
), (1UL<<IMEM_SIZE
)), segment
);
393 /* FIXME: need to check size and read/write multiple segments if necessary */
395 memcpy (State
.umem
[segment
]+addr
, buffer
, size
);
397 memcpy (buffer
, State
.umem
[segment
]+addr
, size
);
399 else if ( (addr
& 0x03000000) == 0x02000000)
403 if (size
> (1<<(DMEM_SIZE
-1)))
405 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: data section is only %d bytes.\n",1<<(DMEM_SIZE
-1));
409 memcpy (State
.dmem
+addr
, buffer
, size
);
411 memcpy (buffer
, State
.dmem
+addr
, size
);
413 else if ( (addr
& 0x03000000) == 0x01000000)
415 /* INSTRUCTION MEMORY */
417 if (size
> (1<<IMEM_SIZE
))
419 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: inst section is only %d bytes.\n",1<<IMEM_SIZE
);
423 memcpy (State
.imem
+addr
, buffer
, size
);
425 memcpy (buffer
, State
.imem
+addr
, size
);
429 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: address 0x%x is not in valid range\n",addr
);
430 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Instruction addresses start at 0x01000000\n");
431 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Data addresses start at 0x02000000\n");
432 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Unified addresses start at 0x03000000\n");
443 sim_write (addr
, buffer
, size
)
445 unsigned char *buffer
;
448 return xfer_mem( addr
, buffer
, size
, 1);
452 sim_read (addr
, buffer
, size
)
454 unsigned char *buffer
;
457 return xfer_mem( addr
, buffer
, size
, 0);
466 struct hash_entry
*h
;
467 static int init_p
= 0;
472 if (strcmp (args
, "-t") == 0)
476 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: unsupported option(s): %s\n",args
);
479 /* put all the opcodes in the hash table */
482 for (s
= Simops
; s
->func
; s
++)
484 h
= &hash_table
[hash(s
->opcode
,s
->format
)];
486 /* go to the last entry in the chain */
492 h
->next
= calloc(1,sizeof(struct hash_entry
));
497 h
->opcode
= s
->opcode
;
498 h
->size
= s
->is_long
;
515 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_set_profile %d\n",n
);
519 sim_set_profile_size (n
)
522 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_set_profile_size %d\n",n
);
536 if ( (addr
& 0xfff0) != 0xff00)
537 (*d10v_callback
->printf_filtered
) (d10v_callback
, "Data address 0x%lx is in I/O space, pc = 0x%lx.\n",
538 (long)addr
, (long)decode_pc ());
539 return State
.dmem
+ addr
;
546 /* instruction memory */
547 return (DMAP
& 0xf) * 0x4000 + State
.imem
;
550 /* this is ugly because we allocate unified memory in 128K segments and */
551 /* dmap addresses 16k segments */
552 seg
= (DMAP
& 0x3ff) >> 3;
553 if (State
.umem
[seg
] == NULL
)
555 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: unified memory region %d unmapped, pc = 0x%lx\n",
556 seg
, (long)decode_pc ());
559 return State
.umem
[seg
] + (DMAP
& 7) * 0x4000;
562 return State
.dmem
+ addr
;
569 uint32 pc
= ((uint32
)PC
) << 2;
578 return State
.imem
+ pc
;
580 if (State
.umem
[imap
& 0xff] == NULL
)
582 (*d10v_callback
->printf_filtered
) (d10v_callback
, "ERROR: unified memory region %d unmapped, pc = 0x%lx\n",
583 imap
& 0xff, (long)PC
);
584 State
.exception
= SIGILL
;
588 return State
.umem
[imap
& 0xff] + pc
;
592 static int stop_simulator
;
601 /* Run (or resume) the program. */
603 sim_resume (step
, siggnal
)
609 /* (*d10v_callback->printf_filtered) (d10v_callback, "sim_resume (%d,%d) PC=0x%x\n",step,siggnal,PC); */
611 prev
= signal(SIGINT
, sim_ctrl_c
);
612 stop_simulator
= step
;
616 inst
= get_longword( pc_addr() );
617 State
.pc_changed
= 0;
618 ins_type_counters
[ (int)INS_CYCLES
]++;
619 switch (inst
& 0xC0000000)
622 /* long instruction */
623 do_long (inst
& 0x3FFFFFFF);
627 do_2_short ( inst
& 0x7FFF, (inst
& 0x3FFF8000) >> 15, 0);
631 do_2_short ((inst
& 0x3FFF8000) >> 15, inst
& 0x7FFF, 1);
634 do_parallel ((inst
& 0x3FFF8000) >> 15, inst
& 0x7FFF);
638 if (State
.RP
&& PC
== RPT_E
)
646 else if (!State
.pc_changed
)
649 while ( !State
.exception
&& !stop_simulator
);
651 if (step
&& !State
.exception
)
652 State
.exception
= SIGTRAP
;
654 signal(SIGINT
, prev
);
676 unsigned long left
= ins_type_counters
[ (int)INS_LEFT
] + ins_type_counters
[ (int)INS_LEFT_COND_EXE
];
677 unsigned long left_nops
= ins_type_counters
[ (int)INS_LEFT_NOPS
];
678 unsigned long left_parallel
= ins_type_counters
[ (int)INS_LEFT_PARALLEL
];
679 unsigned long left_cond
= ins_type_counters
[ (int)INS_LEFT_COND_TEST
];
680 unsigned long left_total
= left
+ left_parallel
+ left_cond
+ left_nops
;
682 unsigned long right
= ins_type_counters
[ (int)INS_RIGHT
] + ins_type_counters
[ (int)INS_RIGHT_COND_EXE
];
683 unsigned long right_nops
= ins_type_counters
[ (int)INS_RIGHT_NOPS
];
684 unsigned long right_parallel
= ins_type_counters
[ (int)INS_RIGHT_PARALLEL
];
685 unsigned long right_cond
= ins_type_counters
[ (int)INS_RIGHT_COND_TEST
];
686 unsigned long right_total
= right
+ right_parallel
+ right_cond
+ right_nops
;
688 unsigned long unknown
= ins_type_counters
[ (int)INS_UNKNOWN
];
689 unsigned long ins_long
= ins_type_counters
[ (int)INS_LONG
];
690 unsigned long parallel
= ins_type_counters
[ (int)INS_PARALLEL
];
691 unsigned long leftright
= ins_type_counters
[ (int)INS_LEFTRIGHT
];
692 unsigned long rightleft
= ins_type_counters
[ (int)INS_RIGHTLEFT
];
693 unsigned long cond_true
= ins_type_counters
[ (int)INS_COND_TRUE
];
694 unsigned long cond_false
= ins_type_counters
[ (int)INS_COND_FALSE
];
695 unsigned long cond_jump
= ins_type_counters
[ (int)INS_COND_JUMP
];
696 unsigned long cycles
= ins_type_counters
[ (int)INS_CYCLES
];
697 unsigned long total
= (unknown
+ left_total
+ right_total
+ ins_long
);
699 int size
= strlen (add_commas (buf1
, sizeof (buf1
), total
));
700 int parallel_size
= strlen (add_commas (buf1
, sizeof (buf1
),
701 (left_parallel
> right_parallel
) ? left_parallel
: right_parallel
));
702 int cond_size
= strlen (add_commas (buf1
, sizeof (buf1
), (left_cond
> right_cond
) ? left_cond
: right_cond
));
703 int nop_size
= strlen (add_commas (buf1
, sizeof (buf1
), (left_nops
> right_nops
) ? left_nops
: right_nops
));
704 int normal_size
= strlen (add_commas (buf1
, sizeof (buf1
), (left
> right
) ? left
: right
));
706 (*d10v_callback
->printf_filtered
) (d10v_callback
,
707 "executed %*s left instruction(s), %*s normal, %*s parallel, %*s EXExxx, %*s nops\n",
708 size
, add_commas (buf1
, sizeof (buf1
), left_total
),
709 normal_size
, add_commas (buf2
, sizeof (buf2
), left
),
710 parallel_size
, add_commas (buf3
, sizeof (buf3
), left_parallel
),
711 cond_size
, add_commas (buf4
, sizeof (buf4
), left_cond
),
712 nop_size
, add_commas (buf5
, sizeof (buf5
), left_nops
));
714 (*d10v_callback
->printf_filtered
) (d10v_callback
,
715 "executed %*s right instruction(s), %*s normal, %*s parallel, %*s EXExxx, %*s nops\n",
716 size
, add_commas (buf1
, sizeof (buf1
), right_total
),
717 normal_size
, add_commas (buf2
, sizeof (buf2
), right
),
718 parallel_size
, add_commas (buf3
, sizeof (buf3
), right_parallel
),
719 cond_size
, add_commas (buf4
, sizeof (buf4
), right_cond
),
720 nop_size
, add_commas (buf5
, sizeof (buf5
), right_nops
));
723 (*d10v_callback
->printf_filtered
) (d10v_callback
,
724 "executed %*s long instruction(s)\n",
725 size
, add_commas (buf1
, sizeof (buf1
), ins_long
));
728 (*d10v_callback
->printf_filtered
) (d10v_callback
,
729 "executed %*s parallel instruction(s)\n",
730 size
, add_commas (buf1
, sizeof (buf1
), parallel
));
733 (*d10v_callback
->printf_filtered
) (d10v_callback
,
734 "executed %*s instruction(s) encoded L->R\n",
735 size
, add_commas (buf1
, sizeof (buf1
), leftright
));
738 (*d10v_callback
->printf_filtered
) (d10v_callback
,
739 "executed %*s instruction(s) encoded R->L\n",
740 size
, add_commas (buf1
, sizeof (buf1
), rightleft
));
743 (*d10v_callback
->printf_filtered
) (d10v_callback
,
744 "executed %*s unknown instruction(s)\n",
745 size
, add_commas (buf1
, sizeof (buf1
), unknown
));
748 (*d10v_callback
->printf_filtered
) (d10v_callback
,
749 "executed %*s instruction(s) due to EXExxx condition being true\n",
750 size
, add_commas (buf1
, sizeof (buf1
), cond_true
));
753 (*d10v_callback
->printf_filtered
) (d10v_callback
,
754 "skipped %*s instruction(s) due to EXExxx condition being false\n",
755 size
, add_commas (buf1
, sizeof (buf1
), cond_false
));
758 (*d10v_callback
->printf_filtered
) (d10v_callback
,
759 "skipped %*s instruction(s) due to conditional branch succeeding\n",
760 size
, add_commas (buf1
, sizeof (buf1
), cond_jump
));
762 (*d10v_callback
->printf_filtered
) (d10v_callback
,
763 "executed %*s cycle(s)\n",
764 size
, add_commas (buf1
, sizeof (buf1
), cycles
));
766 (*d10v_callback
->printf_filtered
) (d10v_callback
,
767 "executed %*s total instructions\n",
768 size
, add_commas (buf1
, sizeof (buf1
), total
));
772 sim_create_inferior (start_address
, argv
, env
)
773 SIM_ADDR start_address
;
779 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_create_inferior: PC=0x%x\n", start_address
);
782 /* reset all state information */
783 memset (&State
.regs
, 0, (int)&State
.imem
- (int)&State
.regs
[0]);
786 PC
= start_address
>> 2;
788 /* cpu resets imap0 to 0 and imap1 to 0x7f, but D10V-EVA board */
789 /* resets imap0 and imap1 to 0x1000. */
811 sim_stop_reason (reason
, sigrc
)
812 enum sim_stop
*reason
;
815 /* (*d10v_callback->printf_filtered) (d10v_callback, "sim_stop_reason: PC=0x%x\n",PC<<2); */
817 switch (State
.exception
)
819 case SIG_D10V_STOP
: /* stop instruction */
820 *reason
= sim_exited
;
824 case SIG_D10V_EXIT
: /* exit trap */
825 *reason
= sim_exited
;
826 *sigrc
= State
.regs
[2];
829 default: /* some signal */
830 *reason
= sim_stopped
;
831 *sigrc
= State
.exception
;
837 sim_fetch_register (rn
, memory
)
839 unsigned char *memory
;
845 WRITE_64 (memory
, State
.a
[rn
-35]);
847 WRITE_16 (memory
, IMAP0
);
849 WRITE_16 (memory
, IMAP1
);
851 WRITE_16 (memory
, DMAP
);
853 WRITE_16 (memory
, State
.regs
[rn
]);
857 sim_store_register (rn
, memory
)
859 unsigned char *memory
;
865 State
.a
[rn
-35] = READ_64 (memory
) & MASK40
;
867 SET_DMAP( READ_16(memory
) );
869 SET_IMAP1( READ_16(memory
) );
871 SET_IMAP0( READ_16(memory
) );
873 State
.regs
[rn
]= READ_16 (memory
);
881 (*d10v_callback
->printf_filtered
) (d10v_callback
, "sim_do_command: %s\n",cmd
);
885 sim_load (prog
, from_tty
)
889 /* Return nonzero so GDB will handle it. */
This page took 0.062776 seconds and 4 git commands to generate.