1 /* Simulator for Xilinx MicroBlaze processor
2 Copyright 2009-2023 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/>. */
19 /* This must come before any other includes. */
26 #include "sim/callback.h"
27 #include "libiberty.h"
31 #include "sim-options.h"
32 #include "sim-signal.h"
33 #include "sim-syscall.h"
35 #include "microblaze-sim.h"
36 #include "opcodes/microblaze-dis.h"
38 #define target_big_endian (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
41 microblaze_extract_unsigned_integer (const unsigned char *addr
, int len
)
45 unsigned char *startaddr
= (unsigned char *)addr
;
46 unsigned char *endaddr
= startaddr
+ len
;
48 if (len
> (int) sizeof (unsigned long))
49 printf ("That operation is not available on integers of more than "
50 "%zu bytes.", sizeof (unsigned long));
52 /* Start at the most significant end of the integer, and work towards
53 the least significant. */
56 if (!target_big_endian
)
58 for (p
= endaddr
; p
> startaddr
;)
59 retval
= (retval
<< 8) | * -- p
;
63 for (p
= startaddr
; p
< endaddr
;)
64 retval
= (retval
<< 8) | * p
++;
71 microblaze_store_unsigned_integer (unsigned char *addr
, int len
,
75 unsigned char *startaddr
= (unsigned char *)addr
;
76 unsigned char *endaddr
= startaddr
+ len
;
78 if (!target_big_endian
)
80 for (p
= startaddr
; p
< endaddr
;)
88 for (p
= endaddr
; p
> startaddr
;)
97 set_initial_gprs (SIM_CPU
*cpu
)
102 /* Set up machine just out of reset. */
106 /* Clean out the GPRs */
107 for (i
= 0; i
< 32; i
++)
114 static int tracing
= 0;
117 sim_engine_run (SIM_DESC sd
,
118 int next_cpu_nr
, /* ignore */
119 int nr_cpus
, /* ignore */
120 int siggnal
) /* ignore */
122 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
125 enum microblaze_instr op
;
136 unsigned_4 oldpc
, newpc
;
137 short delay_slot_enable
;
139 short num_delay_slot
; /* UNUSED except as reqd parameter */
140 enum microblaze_instr_type insn_type
;
148 /* Fetch the initial instructions that we'll decode. */
149 inst
= MEM_RD_WORD (PC
& 0xFFFFFFFC);
151 op
= get_insn_microblaze (inst
, &imm_unsigned
, &insn_type
,
154 if (op
== invalid_inst
)
155 fprintf (stderr
, "Unknown instruction 0x%04x", inst
);
158 fprintf (stderr
, "%.4x: inst = %.4x ", PC
, inst
);
163 /* immword = IMM_W; */
166 delay_slot_enable
= 0;
168 if (op
== microblaze_brk
)
169 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_stopped
, SIM_SIGTRAP
);
170 else if (inst
== MICROBLAZE_HALT_INST
)
174 TRACE_INSN (cpu
, "HALT (%i)", RETREG
);
175 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_exited
, RETREG
);
181 #define INSTRUCTION(NAME, OPCODE, TYPE, ACTION) \
183 TRACE_INSN (cpu, #NAME); \
186 #include "microblaze.isa"
190 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_signalled
,
192 fprintf (stderr
, "ERROR: Unknown opcode\n");
194 /* Make R0 consistent */
197 /* Check for imm instr */
203 /* Update cycle counts */
205 if (insn_type
== memory_store_inst
|| insn_type
== memory_load_inst
)
207 if (insn_type
== mult_inst
)
209 if (insn_type
== barrel_shift_inst
)
211 if (insn_type
== anyware_inst
)
213 if (insn_type
== div_inst
)
216 if ((insn_type
== branch_inst
|| insn_type
== return_inst
)
219 /* Add an extra cycle for taken branches */
221 /* For branch instructions handle the instruction in the delay slot */
222 if (delay_slot_enable
)
225 PC
= oldpc
+ INST_SIZE
;
226 inst
= MEM_RD_WORD (PC
& 0xFFFFFFFC);
227 op
= get_insn_microblaze (inst
, &imm_unsigned
, &insn_type
,
229 if (op
== invalid_inst
)
230 fprintf (stderr
, "Unknown instruction 0x%04x", inst
);
232 fprintf (stderr
, "%.4x: inst = %.4x ", PC
, inst
);
236 /* immword = IMM_W; */
237 if (op
== microblaze_brk
)
239 if (STATE_VERBOSE_P (sd
))
240 fprintf (stderr
, "Breakpoint set in delay slot "
241 "(at address 0x%x) will not be honored\n", PC
);
242 /* ignore the breakpoint */
244 else if (insn_type
== branch_inst
|| insn_type
== return_inst
)
246 if (STATE_VERBOSE_P (sd
))
247 fprintf (stderr
, "Cannot have branch or return instructions "
248 "in delay slot (at address 0x%x)\n", PC
);
249 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
, sim_signalled
,
256 #define INSTRUCTION(NAME, OPCODE, TYPE, ACTION) \
260 #include "microblaze.isa"
264 sim_engine_halt (sd
, NULL
, NULL
, NULL_CIA
,
265 sim_signalled
, SIM_SIGILL
);
266 fprintf (stderr
, "ERROR: Unknown opcode at 0x%x\n", PC
);
268 /* Update cycle counts */
270 if (insn_type
== memory_store_inst
271 || insn_type
== memory_load_inst
)
273 if (insn_type
== mult_inst
)
275 if (insn_type
== barrel_shift_inst
)
277 if (insn_type
== anyware_inst
)
279 if (insn_type
== div_inst
)
284 /* Make R0 consistent */
286 /* Check for imm instr */
294 if (op
== brki
&& IMM
== 8)
296 RETREG
= sim_syscall (cpu
, CPU
.regs
[12], CPU
.regs
[5],
297 CPU
.regs
[6], CPU
.regs
[7],
302 /* no delay slot: increment cycle count */
309 fprintf (stderr
, "\n");
311 if (sim_events_tick (sd
))
312 sim_events_process (sd
);
315 /* Hide away the things we've cached while executing. */
317 CPU
.insts
+= insts
; /* instructions done ... */
318 CPU
.cycles
+= insts
; /* and each takes a cycle */
319 CPU
.cycles
+= bonus_cycles
; /* and extra cycles for branches */
320 CPU
.cycles
+= memops
; /* and memop cycle delays */
324 microblaze_reg_store (SIM_CPU
*cpu
, int rn
, const void *memory
, int length
)
326 if (rn
< NUM_REGS
+ NUM_SPECIAL
&& rn
>= 0)
330 /* misalignment safe */
331 long ival
= microblaze_extract_unsigned_integer (memory
, 4);
335 CPU
.spregs
[rn
-NUM_REGS
] = ival
;
346 microblaze_reg_fetch (SIM_CPU
*cpu
, int rn
, void *memory
, int length
)
350 if (rn
< NUM_REGS
+ NUM_SPECIAL
&& rn
>= 0)
357 ival
= CPU
.spregs
[rn
-NUM_REGS
];
359 /* misalignment-safe */
360 microblaze_store_unsigned_integer (memory
, 4, ival
);
371 sim_info (SIM_DESC sd
, bool verbose
)
373 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
374 host_callback
*callback
= STATE_CALLBACK (sd
);
376 callback
->printf_filtered (callback
, "\n\n# instructions executed %10d\n",
378 callback
->printf_filtered (callback
, "# cycles %10d\n",
379 (CPU
.cycles
) ? CPU
.cycles
+2 : 0);
383 microblaze_pc_get (sim_cpu
*cpu
)
385 return MICROBLAZE_SIM_CPU (cpu
)->spregs
[0];
389 microblaze_pc_set (sim_cpu
*cpu
, sim_cia pc
)
391 MICROBLAZE_SIM_CPU (cpu
)->spregs
[0] = pc
;
395 free_state (SIM_DESC sd
)
397 if (STATE_MODULES (sd
) != NULL
)
398 sim_module_uninstall (sd
);
399 sim_cpu_free_all (sd
);
404 sim_open (SIM_OPEN_KIND kind
, host_callback
*cb
,
405 struct bfd
*abfd
, char * const *argv
)
408 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
409 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
411 /* The cpu data is kept in a separately allocated chunk of memory. */
412 if (sim_cpu_alloc_all_extra (sd
, 0, sizeof (struct microblaze_regset
))
419 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
425 /* The parser will print an error message for us, so we silently return. */
426 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
432 /* Check for/establish the a reference program image. */
433 if (sim_analyze_program (sd
, STATE_PROG_FILE (sd
), abfd
) != SIM_RC_OK
)
439 /* Configure/verify the target byte order and other runtime
440 configuration options. */
441 if (sim_config (sd
) != SIM_RC_OK
)
443 sim_module_uninstall (sd
);
447 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
449 /* Uninstall the modules to avoid memory leaks,
450 file descriptor leaks, etc. */
451 sim_module_uninstall (sd
);
455 /* CPU specific initialization. */
456 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
458 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
460 CPU_REG_FETCH (cpu
) = microblaze_reg_fetch
;
461 CPU_REG_STORE (cpu
) = microblaze_reg_store
;
462 CPU_PC_FETCH (cpu
) = microblaze_pc_get
;
463 CPU_PC_STORE (cpu
) = microblaze_pc_set
;
465 set_initial_gprs (cpu
);
468 /* Default to a 8 Mbyte (== 2^23) memory space. */
469 sim_do_commandf (sd
, "memory-size 0x800000");
475 sim_create_inferior (SIM_DESC sd
, struct bfd
*prog_bfd
,
476 char * const *argv
, char * const *env
)
478 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
480 PC
= bfd_get_start_address (prog_bfd
);