summaryrefslogtreecommitdiff
path: root/msim/sim.h
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-09-30 18:52:25 -0400
committerFreya Murphy <freya@freyacat.org>2024-09-30 18:52:25 -0400
commit4af200b00188e02b2c6207dfe494a3dd12556c5f (patch)
tree25d993117e6306907d1d3821ef4fca729390d221 /msim/sim.h
parentupdate runtime to return exit code from main (diff)
downloadmips-4af200b00188e02b2c6207dfe494a3dd12556c5f.tar.gz
mips-4af200b00188e02b2c6207dfe494a3dd12556c5f.tar.bz2
mips-4af200b00188e02b2c6207dfe494a3dd12556c5f.zip
msim done (ish)
Diffstat (limited to '')
-rw-r--r--msim/sim.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/msim/sim.h b/msim/sim.h
new file mode 100644
index 0000000..517babd
--- /dev/null
+++ b/msim/sim.h
@@ -0,0 +1,87 @@
+/* Copyright (c) 2024 Freya Murphy */
+
+#ifndef __SIM_H__
+#define __SIM_H__
+
+#include <elf.h>
+#include <stdint.h>
+
+/// arguments
+struct simulator_args {
+ char *executable;
+ bool memchk;
+ bool debug;
+};
+
+
+// when loading the executable, we need to bounds check to
+// make sure its within the file size
+//
+// this checks that
+// 1. the end is in the file
+// 2. the off and len doesnt integer overflow
+#define BOUND_CHK(size, len, off) \
+ ((off) > UINT32_MAX - (len) || (off) + (len) > (size))
+
+///
+/// data must start at these
+/// addresses or the executable is
+/// invalid
+///
+#define MEM_TEXT_ADDR 0x00400000
+#define MEM_DATA_ADDR 0x10000000
+
+struct simulator {
+ struct simulator_args *args;
+
+
+ /// the registers
+ uint32_t reg[32];
+
+ /// the program counter
+ uint32_t pc;
+
+ /// the current executing pc
+ uint32_t current_pc;
+
+ /// low and hi
+ uint32_t low;
+ uint32_t hi;
+
+ /// memory address bounds
+ uint32_t data_min;
+ uint32_t data_max;
+ uint32_t text_min;
+ uint32_t text_max;
+
+ // entrypoint
+ uint32_t entry;
+};
+
+/* initalize a simulator */
+int sim_init(struct simulator *sim, struct simulator_args *args);
+
+/* load a file into a simulator */
+int sim_load_file(struct simulator *sim);
+
+/* execute and instruction */
+void sim_ins(struct simulator *sim, uint32_t ins);
+
+/* execute the next instruction in the simulator */
+void sim_step(struct simulator *sim);
+
+/* run the simulator with the loaded state */
+_Noreturn void sim_run(struct simulator *sim);
+
+/* run the debugger for a simulator */
+_Noreturn void sim_debug(struct simulator *sim);
+
+/* panics the simulator */
+__attribute__((format(printf, 2, 3)))
+_Noreturn void sim_dump(struct simulator *sim, const char *format, ...);
+
+/* dumps registers */
+void sim_dump_reg(struct simulator *sim);
+
+#endif /* __SIM_H__ */
+