summaryrefslogtreecommitdiff
path: root/simulator.c
diff options
context:
space:
mode:
Diffstat (limited to 'simulator.c')
-rw-r--r--simulator.c68
1 files changed, 51 insertions, 17 deletions
diff --git a/simulator.c b/simulator.c
index 1507f22..92c07b5 100644
--- a/simulator.c
+++ b/simulator.c
@@ -9,6 +9,7 @@
* for more details.
*/
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
@@ -23,10 +24,40 @@
static bool simulator_running;
static struct nios2 *this_cpu = NULL;
-static void sigint_handler(int sig __unused)
+static void simulator_pause(void)
{
- info("SIGINT caught\n");
- simulator_running = false;
+ do {
+ char input;
+
+ info("\n=== Simulator paused ===\n\n"
+ "Please choose an option:\n"
+ " d enter debug mode\n"
+ " r resume simulator\n"
+ " q quit simulator\n"
+ "> ");
+
+ input = tolower(getchar());
+ do {} while (getchar() != '\n');
+ switch (input) {
+ case 'd':
+ dbg("Entering debug mode\n");
+ return;
+ case 'r':
+ return;
+ case 'q':
+ simulator_running = false;
+ return;
+ default:
+ continue;
+ }
+ } while (true);
+}
+
+static void sigint_handler(int sig)
+{
+ signal(sig, SIG_IGN);
+ simulator_pause();
+ signal(SIGINT, sigint_handler);
}
static void sigsegv_handler(int sig __unused)
@@ -58,28 +89,36 @@ void simulator_run(struct nios2 *cpu)
memory_dump(cpu->mem, cpu->pc, 32);
simulator_running = true;
- while (simulator_running) {
+ while (true) {
instr = nios2_fetch_instr(cpu);
ret = nios2_execute_instr(cpu, instr);
-
- if (ret == PC_INC_NORMAL)
+ switch (ret) {
+ case PC_INC_NORMAL:
nios2_cpu_inc_pc(cpu);
- else if (ret == PC_INC_BY_INSTR)
- ;
- else if (IS_EXCEPTION(ret)) {
+ break;
+ case PC_INC_BY_INSTR:
+ /* do nothing */
+ break;
+ case INSTR_BREAK:
+ simulator_pause();
+ break;
+ case INSTR_EXCEPTION:
dbg("Exception\n");
- nios2_exception(cpu, EXCEPTION_CAUSE(ret));
- } else if (ret == INSTR_UNIMPL) {
+ nios2_handle_exception(cpu);
+ case INSTR_UNIMPL:
simulator_running = false;
break;
- } else {
+ default:
err("Invalid instruction 0x%08x at PC 0x%08x\n", instr, cpu->pc);
nios2_dump_registers(cpu);
simulator_running = false;
break;
}
+ if (!simulator_running)
+ break;
+
nios2_simulate(cpu);
device_simulate_all();
}
@@ -90,11 +129,6 @@ void simulator_run(struct nios2 *cpu)
#if 0
static int32_t simulator_mode = SIM_MODE;
-static void set_init_pc(struct NIOS_CPU * cpu, uint32_t pc)
-{
- cpu->pc = pc;
-}
-
static void quit_debug_mode(struct NIOS_CPU * cpu)
{
simulator_mode = SIM_MODE;