aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/emu.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/emu.py b/tools/emu.py
new file mode 100644
index 0000000..d498f13
--- /dev/null
+++ b/tools/emu.py
@@ -0,0 +1,47 @@
+import sys
+from struct import unpack_from
+
+def main(argv):
+ with open(argv[0], "rb") as fh:
+ mem = list(unpack_from("64B", fh.read(64)))
+
+ try:
+ cycles = int(argv[1], 0)
+ except:
+ cycles = 20
+
+ pc = 0
+ acc = 0
+ carry = False
+ mnemonics = {0: "nor", 1: "add", 2: "sta", 3: "jcc"}
+
+ if cycles < 100:
+ print('\tPC\tIR\tOP\tARG\tACC\tC', file=sys.stderr)
+ for _ in range(cycles):
+ ir = mem[pc & 0x3f]
+ pc += 1
+ mnemo = mnemonics[ir >> 6]
+ arg = ir & 0x3f
+
+ if cycles < 100:
+ print('\t{:2x}\t{:2x}\t{}\t{:2x}\t{:2x}\t{}'.format(pc - 1, ir, mnemo, arg, acc, carry), file=sys.stderr)
+
+ if mnemo == "nor":
+ acc = (acc | mem[arg]) ^ 0xff
+ elif mnemo == "add":
+ acc += mem[arg]
+ carry = bool(acc & 0x100)
+ acc &= 0xff
+ elif mnemo == "sta":
+ mem[arg] = acc
+ elif mnemo == "jcc":
+ if ir == 0xff:
+ print(f"{acc}")
+ elif not carry:
+ pc = arg
+ carry = False
+ else:
+ assert False
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1:]))