1 /* Simulator for Xilinx MicroBlaze processor
2 Copyright 2009-2021 Free Software Foundation, Inc.
4 This file is part of GDB, the GNU debugger.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>. */
25 #include "gdb/callback.h"
26 #include "libiberty.h"
27 #include "gdb/remote-sim.h"
30 #include "sim-options.h"
31 #include "sim-syscall.h"
33 #include "microblaze-dis.h"
35 #define target_big_endian (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
38 microblaze_extract_unsigned_integer (unsigned char *addr
, int len
)
42 unsigned char *startaddr
= (unsigned char *)addr
;
43 unsigned char *endaddr
= startaddr
+ len
;
45 if (len
> (int) sizeof (unsigned long))
46 printf ("That operation is not available on integers of more than "
47 "%zu bytes.", sizeof (unsigned long));
49 /* Start at the most significant end of the integer, and work towards
50 the least significant. */
53 if (!target_big_endian
)
55 for (p
= endaddr
; p
> startaddr
;)
56 retval
= (retval
<< 8) | * -- p
;
60 for (p
= startaddr
; p
< endaddr
;)
61 retval
= (retval
<< 8) | * p
++;
68 microblaze_store_unsigned_integer (unsigned char *addr
, int len
,
72 unsigned char *startaddr
= (unsigned char *)addr
;
73 unsigned char *endaddr
= startaddr
+ len
;
75 if (!target_big_endian
)
77 for (p
= startaddr
; p
< endaddr
;)
85 for (p
= endaddr
; p
> startaddr
;)
94 set_initial_gprs (SIM_CPU
*cpu
)
99 /* Set up machine just out of reset. */
103 /* Clean out the GPRs */
104 for (i
= 0; i
< 32; i
++)
111 static int tracing
= 0;
114 sim_engine_run (SIM_DESC sd
,
115 int next_cpu_nr
, /* ignore */
116 int nr_cpus
, /* ignore */
117 int siggnal
) /* ignore */
119 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
122 enum microblaze_instr op
;
134 short delay_slot_enable
;
136 short num_delay_slot
; /* UNUSED except as reqd parameter */
137 enum microblaze_instr_type insn_type
;
145 /* Fetch the initial instructions that we'll decode. */
146 inst
= MEM_RD_WORD (PC
& 0xFFFFFFFC);
148 op
= get_insn_microblaze (inst
, &imm_unsigned
, &insn_type
,
151 if (op
== invalid_inst
)
152 fprintf (stderr
, "Unknown instruction 0x%04x", inst
);
155 fprintf (stderr
, "%.4x: inst = %.4x ", PC
, inst
);
160 /* immword = IMM_W; */
163 delay_slot_enable
= 0;
165 if (op
== microblaze_brk
)
166 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGTRAP
);
167 else if (inst
== MICROBLAZE_HALT_INST
)
171 TRACE_INSN (cpu
, "HALT (%i)", RETREG
);
172 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_exited
, RETREG
);
178 #define INSTRUCTION(NAME, OPCODE, TYPE, ACTION) \
180 TRACE_INSN (cpu, #NAME); \
183 #include "microblaze.isa"
187 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_signalled
,
189 fprintf (stderr
, "ERROR: Unknown opcode\n");
191 /* Make R0 consistent */
194 /* Check for imm instr */
200 /* Update cycle counts */
202 if (insn_type
== memory_store_inst
|| insn_type
== memory_load_inst
)
204 if (insn_type
== mult_inst
)
206 if (insn_type
== barrel_shift_inst
)
208 if (insn_type
== anyware_inst
)
210 if (insn_type
== div_inst
)
213 if ((insn_type
== branch_inst
|| insn_type
== return_inst
)
216 /* Add an extra cycle for taken branches */
218 /* For branch instructions handle the instruction in the delay slot */
219 if (delay_slot_enable
)
222 PC
= oldpc
+ INST_SIZE
;
223 inst
= MEM_RD_WORD (PC
& 0xFFFFFFFC);
224 op
= get_insn_microblaze (inst
, &imm_unsigned
, &insn_type
,
226 if (op
== invalid_inst
)
227 fprintf (stderr
, "Unknown instruction 0x%04x", inst
);
229 fprintf (stderr
, "%.4x: inst = %.4x ", PC
, inst
);
233 /* immword = IMM_W; */
234 if (op
== microblaze_brk
)
236 if (STATE_VERBOSE_P (sd
))
237 fprintf (stderr
, "Breakpoint set in delay slot "
238 "(at address 0x%x) will not be honored\n", PC
);
239 /* ignore the breakpoint */
241 else if (insn_type
== branch_inst
|| insn_type
== return_inst
)
243 if (STATE_VERBOSE_P (sd
))
244 fprintf (stderr
, "Cannot have branch or return instructions "
245 "in delay slot (at address 0x%x)\n", PC
);
246 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_signalled
,
253 #define INSTRUCTION(NAME, OPCODE, TYPE, ACTION) \
257 #include "microblaze.isa"
261 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
,
262 sim_signalled
, SIM_SIGILL
);
263 fprintf (stderr
, "ERROR: Unknown opcode at 0x%x\n", PC
);
265 /* Update cycle counts */
267 if (insn_type
== memory_store_inst
268 || insn_type
== memory_load_inst
)
270 if (insn_type
== mult_inst
)
272 if (insn_type
== barrel_shift_inst
)
274 if (insn_type
== anyware_inst
)
276 if (insn_type
== div_inst
)
281 /* Make R0 consistent */
283 /* Check for imm instr */
291 if (op
== brki
&& IMM
== 8)
293 RETREG
= sim_syscall (cpu
, CPU
.regs
[12], CPU
.regs
[5],
294 CPU
.regs
[6], CPU
.regs
[7],
299 /* no delay slot: increment cycle count */
306 fprintf (stderr
, "\n");
308 if (sim_events_tick (sd
))
309 sim_events_process (sd
);
312 /* Hide away the things we've cached while executing. */
314 CPU
.insts
+= insts
; /* instructions done ... */
315 CPU
.cycles
+= insts
; /* and each takes a cycle */
316 CPU
.cycles
+= bonus_cycles
; /* and extra cycles for branches */
317 CPU
.cycles
+= memops
; /* and memop cycle delays */
321 microblaze_reg_store (SIM_CPU
*cpu
, int rn
, unsigned char *memory
, int length
)
323 if (rn
< NUM_REGS
+ NUM_SPECIAL
&& rn
>= 0)
327 /* misalignment safe */
328 long ival
= microblaze_extract_unsigned_integer (memory
, 4);
332 CPU
.spregs
[rn
-NUM_REGS
] = ival
;
343 microblaze_reg_fetch (SIM_CPU
*cpu
, int rn
, unsigned char *memory
, int length
)
347 if (rn
< NUM_REGS
+ NUM_SPECIAL
&& rn
>= 0)
354 ival
= CPU
.spregs
[rn
-NUM_REGS
];
356 /* misalignment-safe */
357 microblaze_store_unsigned_integer (memory
, 4, ival
);
368 sim_info (SIM_DESC sd
, int verbose
)
370 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
371 host_callback
*callback
= STATE_CALLBACK (sd
);
373 callback
->printf_filtered (callback
, "\n\n# instructions executed %10d\n",
375 callback
->printf_filtered (callback
, "# cycles %10d\n",
376 (CPU
.cycles
) ? CPU
.cycles
+2 : 0);
380 microblaze_pc_get (sim_cpu
*cpu
)
382 return cpu
->microblaze_cpu
.spregs
[0];
386 microblaze_pc_set (sim_cpu
*cpu
, sim_cia pc
)
388 cpu
->microblaze_cpu
.spregs
[0] = pc
;
392 free_state (SIM_DESC sd
)
394 if (STATE_MODULES (sd
) != NULL
)
395 sim_module_uninstall (sd
);
396 sim_cpu_free_all (sd
);
401 sim_open (SIM_OPEN_KIND kind
, host_callback
*cb
,
402 struct bfd
*abfd
, char * const *argv
)
405 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
406 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
408 /* The cpu data is kept in a separately allocated chunk of memory. */
409 if (sim_cpu_alloc_all (sd
, 1) != SIM_RC_OK
)
415 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
421 /* The parser will print an error message for us, so we silently return. */
422 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
428 /* Check for/establish the a reference program image. */
429 if (sim_analyze_program (sd
,
430 (STATE_PROG_ARGV (sd
) != NULL
431 ? *STATE_PROG_ARGV (sd
)
432 : NULL
), abfd
) != SIM_RC_OK
)
438 /* Configure/verify the target byte order and other runtime
439 configuration options. */
440 if (sim_config (sd
) != SIM_RC_OK
)
442 sim_module_uninstall (sd
);
446 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
448 /* Uninstall the modules to avoid memory leaks,
449 file descriptor leaks, etc. */
450 sim_module_uninstall (sd
);
454 /* CPU specific initialization. */
455 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
457 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
459 CPU_REG_FETCH (cpu
) = microblaze_reg_fetch
;
460 CPU_REG_STORE (cpu
) = microblaze_reg_store
;
461 CPU_PC_FETCH (cpu
) = microblaze_pc_get
;
462 CPU_PC_STORE (cpu
) = microblaze_pc_set
;
464 set_initial_gprs (cpu
);
467 /* Default to a 8 Mbyte (== 2^23) memory space. */
468 sim_do_commandf (sd
, "memory-size 0x800000");
474 sim_create_inferior (SIM_DESC sd
, struct bfd
*prog_bfd
,
475 char * const *argv
, char * const *env
)
477 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
479 PC
= bfd_get_start_address (prog_bfd
);