summaryrefslogtreecommitdiff
path: root/simulator.c
diff options
context:
space:
mode:
Diffstat (limited to 'simulator.c')
-rw-r--r--simulator.c108
1 files changed, 81 insertions, 27 deletions
diff --git a/simulator.c b/simulator.c
index fb41893..1507f22 100644
--- a/simulator.c
+++ b/simulator.c
@@ -1,39 +1,95 @@
/*
- Nios-sim - one simple NIOSII simulator only for personal interest and fun.
- Copyright (C) 2010 chysun2000@gmail.com
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
+ * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
+ * Copyright (C) 2010 chysun2000@gmail.com
+ *
+ * This file is part of nios2sim-ng.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
#include <stdio.h>
-#include <string.h>
+#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
-#include "public.h"
-#include "niosii.h"
-#include "io_device.h"
+#include "nios2sim-ng.h"
+#include "nios2.h"
+#include "memory.h"
+#include "device.h"
#include "simulator.h"
-static int32_t simulator_mode = SIM_MODE;
+static bool simulator_running;
+static struct nios2 *this_cpu = NULL;
-static void modify_pc(struct NIOS_CPU * cpu)
+static void sigint_handler(int sig __unused)
{
- cpu->pc += 4;
+ info("SIGINT caught\n");
+ simulator_running = false;
+}
+
+static void sigsegv_handler(int sig __unused)
+{
+ info("SIGSEGV caught\n");
+ /* TODO: Dump registers, memory etc */
+ if (this_cpu != NULL)
+ nios2_dump_registers(this_cpu);
+ _exit(EXIT_FAILURE);
+}
+
+void simulator_run(struct nios2 *cpu)
+{
+ uint32_t instr = 0;
+ int ret;
+
+ this_cpu = cpu;
+
+ /* Register signal handlers */
+ signal(SIGINT, sigint_handler);
+ signal(SIGSEGV, sigsegv_handler);
+
+ nios2_cpu_reset(cpu);
+ nios2_cpu_init(cpu);
+
+ vinfo("Starting nios2sim-ng simulator...\n");
+
+ dbg("Dumping first 32 words of memory:\n");
+ memory_dump(cpu->mem, cpu->pc, 32);
+
+ simulator_running = true;
+ while (simulator_running) {
+ instr = nios2_fetch_instr(cpu);
+
+ ret = nios2_execute_instr(cpu, instr);
+
+ if (ret == PC_INC_NORMAL)
+ nios2_cpu_inc_pc(cpu);
+ else if (ret == PC_INC_BY_INSTR)
+ ;
+ else if (IS_EXCEPTION(ret)) {
+ dbg("Exception\n");
+ nios2_exception(cpu, EXCEPTION_CAUSE(ret));
+ } else if (ret == INSTR_UNIMPL) {
+ simulator_running = false;
+ break;
+ } else {
+ err("Invalid instruction 0x%08x at PC 0x%08x\n", instr, cpu->pc);
+ nios2_dump_registers(cpu);
+ simulator_running = false;
+ break;
+ }
+
+ nios2_simulate(cpu);
+ device_simulate_all();
+ }
+
+ vinfo("Exiting nios2sim-ng simulator...\n");
}
+#if 0
+static int32_t simulator_mode = SIM_MODE;
+
static void set_init_pc(struct NIOS_CPU * cpu, uint32_t pc)
{
cpu->pc = pc;
@@ -415,6 +471,4 @@ void simulator_run(void)
}
}
-/*----------------------------------------------------------------------------*/
-
-
+#endif