summary refs log tree commit diff
path: root/eater.ts
diff options
context:
space:
mode:
Diffstat (limited to 'eater.ts')
-rw-r--r--eater.ts76
1 files changed, 76 insertions, 0 deletions
diff --git a/eater.ts b/eater.ts
new file mode 100644
index 0000000..4440635
--- /dev/null
+++ b/eater.ts
@@ -0,0 +1,76 @@
+// deno-lint-ignore-file no-process-globals
+import The65c02 from "./65c02.ts";
+// eater.ts
+// a runtime meant to mimic ben eater's 65c02 computer
+
+if (!process.stdin.isRaw)
+    process.stdin.setRawMode(true)
+
+// the thing used for ram
+const ram = new Uint8Array(2**16);
+
+// initiate the emulator
+const cpu = new The65c02(function (this: The65c02) {
+    // since this is controlled by the user it's easy
+    // to map things into address space
+    this.io.data.set(ram[this.io.address.num()] ?? 0)
+    if (this.io.address.num() == 0x5000
+        && ram[0x5001])
+        ram[0x5001] = 0;
+}, function (this: The65c02) {
+    if (this.io.address.num() == 0x5000) {
+        return Deno.stdout.write(new Uint8Array([this.io.data.num()]))
+    }
+    // write
+    ram[this.io.address.num()] = this.io.data.num()
+})
+
+const binStart = 0;
+
+await cpu.loadInstructions()
+
+const code = Deno.readFileSync('a.out')
+
+// mem address $0000
+ram[0xFFFC] = binStart & 0x00FF
+ram[0xFFFD] = (binStart & 0xFF00) >> 8
+
+// write code to ram before execution
+for (let offset = 0; offset < code.length; offset++) {
+    const byte = code[offset];
+    ram[binStart  + offset] = byte;
+}
+
+// pull RESB low to reset the 65c02
+cpu.io.reset.LO()
+cpu.cycle()
+
+//the cpu reset, pull RESB high and start execution!
+cpu.io.reset.HI()
+
+
+let running = true;
+
+process.stdin.on('data', (data) => {
+    if (data[0] == 3)
+        return running = false;
+    // console.log(`uh`, data[0], data[0].toString(16))
+    ram[0x5000] = data[0];
+    ram[0x5001] = 0x08;
+    // cpu.io.interruptRequest.HI();
+})
+
+// repeat until the cpu requests an interrupt
+const clock = setInterval(() => {
+    if (!running) {
+        Deno.writeFileSync('ram.bin', ram)
+        clearInterval(clock)
+        
+        process.stdin.setRawMode(false);
+        Deno.exit(0)
+    }
+    if(cpu.io.interruptRequest.high && !cpu.IRQBDisable)
+        cpu.io.interruptRequest.LO();
+    cpu.cycle();
+// 1MHz i think
+}, 1)