summary refs log tree commit diff
path: root/instructions
diff options
context:
space:
mode:
authorWlodekM <[email protected]>2025-03-31 19:27:55 +0300
committerWlodekM <[email protected]>2025-03-31 19:27:55 +0300
commitef4e8c20719822eebd6318a878cc37902c2b85a5 (patch)
treec80cc67921c8b511f5a50ec68834b5b28deb05a1 /instructions
pc thing
Diffstat (limited to 'instructions')
-rw-r--r--instructions/add.ts8
-rw-r--r--instructions/and.ts8
-rw-r--r--instructions/clr.ts8
-rw-r--r--instructions/cmp.ts9
-rw-r--r--instructions/cmr.ts10
-rw-r--r--instructions/crp.ts10
-rw-r--r--instructions/crr.ts11
-rw-r--r--instructions/dbg.ts13
-rw-r--r--instructions/dec.ts9
-rw-r--r--instructions/decr.ts10
-rw-r--r--instructions/div.ts8
-rw-r--r--instructions/halt.ts9
-rw-r--r--instructions/inc.ts9
-rw-r--r--instructions/incr.ts10
-rw-r--r--instructions/jmp.ts8
-rw-r--r--instructions/jmr.ts9
-rw-r--r--instructions/jnz.ts9
-rw-r--r--instructions/jnzr.ts11
-rw-r--r--instructions/jz.ts9
-rw-r--r--instructions/ld.ts9
-rw-r--r--instructions/ldm.ts8
-rw-r--r--instructions/ldr.ts8
-rw-r--r--instructions/mod.ts8
-rw-r--r--instructions/mul.ts8
-rw-r--r--instructions/not.ts8
-rw-r--r--instructions/or.ts8
-rw-r--r--instructions/put.ts9
-rw-r--r--instructions/ret.ts10
-rw-r--r--instructions/sbc.ts10
-rw-r--r--instructions/shl.ts9
-rw-r--r--instructions/shr.ts9
-rw-r--r--instructions/srm.ts8
-rw-r--r--instructions/srr.ts10
-rw-r--r--instructions/str.ts9
-rw-r--r--instructions/sub.ts8
-rw-r--r--instructions/swp.ts13
-rw-r--r--instructions/swpm.ts13
-rw-r--r--instructions/sys.ts55
-rw-r--r--instructions/xor.ts8
39 files changed, 406 insertions, 0 deletions
diff --git a/instructions/add.ts b/instructions/add.ts
new file mode 100644
index 0000000..93a5018
--- /dev/null
+++ b/instructions/add.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.registers[2] = this.registers[0] + this.registers[1]
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/and.ts b/instructions/and.ts
new file mode 100644
index 0000000..77a28e0
--- /dev/null
+++ b/instructions/and.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.registers[2] = this.registers[0] & this.registers[1]
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/clr.ts b/instructions/clr.ts
new file mode 100644
index 0000000..bbbe422
--- /dev/null
+++ b/instructions/clr.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.returnStack.pop();
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/cmp.ts b/instructions/cmp.ts
new file mode 100644
index 0000000..1b2db94
--- /dev/null
+++ b/instructions/cmp.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, val]: number[]) {
+        const r = this.lib.parseReg(reg)
+        this.returnFlag = +(this.registers[r] == +val)
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/cmr.ts b/instructions/cmr.ts
new file mode 100644
index 0000000..c92d939
--- /dev/null
+++ b/instructions/cmr.ts
@@ -0,0 +1,10 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2]: number[]) {
+        const r1 = this.lib.parseReg(reg1)
+        const r2 = this.lib.parseReg(reg2)
+        this.returnFlag = +(this.registers[r1] == this.registers[r2])
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/crp.ts b/instructions/crp.ts
new file mode 100644
index 0000000..c334ed5
--- /dev/null
+++ b/instructions/crp.ts
@@ -0,0 +1,10 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2, val]: number[]) {
+        const r1 = this.lib.parseReg(reg1)
+        const r2 = this.lib.parseReg(reg2)
+        this.registers[r1] = +(this.registers[r2] == val)
+    },
+    args: 3
+}
\ No newline at end of file
diff --git a/instructions/crr.ts b/instructions/crr.ts
new file mode 100644
index 0000000..cea6995
--- /dev/null
+++ b/instructions/crr.ts
@@ -0,0 +1,11 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2, reg3]: number[]) {
+        const r1 = this.lib.parseReg(reg1)
+        const r2 = this.lib.parseReg(reg2)
+        const r3 = this.lib.parseReg(reg3)
+        this.registers[r1] = +(this.registers[r2] == this.registers[r3])
+    },
+    args: 3
+}
\ No newline at end of file
diff --git a/instructions/dbg.ts b/instructions/dbg.ts
new file mode 100644
index 0000000..6781fbf
--- /dev/null
+++ b/instructions/dbg.ts
@@ -0,0 +1,13 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [msg]: string[]) {
+        console.log(msg, this.mem, this.registers, this.returnFlag, this.returnStack);
+        const b = new Uint8Array(1);
+        Deno.stdin.readSync(b)
+        if (b[0] == 'c'.charCodeAt(0)) {
+            this.programPointer = 0xFFFF - 1
+        }
+    },
+    args: 1
+}
\ No newline at end of file
diff --git a/instructions/dec.ts b/instructions/dec.ts
new file mode 100644
index 0000000..df46769
--- /dev/null
+++ b/instructions/dec.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, by]: number[]) {
+        const r = this.lib.parseReg(reg);
+        this.registers[r] -= by
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/decr.ts b/instructions/decr.ts
new file mode 100644
index 0000000..12e7fd9
--- /dev/null
+++ b/instructions/decr.ts
@@ -0,0 +1,10 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2]: number[]) {
+        const r1 = this.lib.parseReg(reg1);
+        const r2 = this.lib.parseReg(reg2);
+        this.registers[r1] -= this.registers[r2]
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/div.ts b/instructions/div.ts
new file mode 100644
index 0000000..dbad7e1
--- /dev/null
+++ b/instructions/div.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.registers[2] = Math.floor(this.registers[0] / this.registers[1])
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/halt.ts b/instructions/halt.ts
new file mode 100644
index 0000000..4685f2b
--- /dev/null
+++ b/instructions/halt.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        // this.programPointer = 0xFFFF - 1
+        this.halted = true
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/inc.ts b/instructions/inc.ts
new file mode 100644
index 0000000..180546e
--- /dev/null
+++ b/instructions/inc.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, by]: number[]) {
+        const r = this.lib.parseReg(reg);
+        this.registers[r] += by
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/incr.ts b/instructions/incr.ts
new file mode 100644
index 0000000..5f1d529
--- /dev/null
+++ b/instructions/incr.ts
@@ -0,0 +1,10 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2]: number[]) {
+        const r1 = this.lib.parseReg(reg1);
+        const r2 = this.lib.parseReg(reg2);
+        this.registers[r1] += this.registers[r2]
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/jmp.ts b/instructions/jmp.ts
new file mode 100644
index 0000000..00db338
--- /dev/null
+++ b/instructions/jmp.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [addr]: string[]) {
+        this.programPointer = Number(addr) - 1
+    },
+    args: 1
+}
\ No newline at end of file
diff --git a/instructions/jmr.ts b/instructions/jmr.ts
new file mode 100644
index 0000000..25f5334
--- /dev/null
+++ b/instructions/jmr.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [addr]: string[]) {
+        this.returnStack.push(this.programPointer)
+        this.programPointer = Number(addr) - 1
+    },
+    args: 1
+}
\ No newline at end of file
diff --git a/instructions/jnz.ts b/instructions/jnz.ts
new file mode 100644
index 0000000..e5b539f
--- /dev/null
+++ b/instructions/jnz.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [addr]: string[]) {
+        if (this.returnFlag == 0) return;
+        this.programPointer = +addr - 1
+    },
+    args: 1
+}
\ No newline at end of file
diff --git a/instructions/jnzr.ts b/instructions/jnzr.ts
new file mode 100644
index 0000000..00f1569
--- /dev/null
+++ b/instructions/jnzr.ts
@@ -0,0 +1,11 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2]: number[]) {
+        const r1 = this.lib.parseReg(reg1)
+        const r2 = this.lib.parseReg(reg2)
+        if (this.registers[r1] == 0) return;
+        this.programPointer = this.registers[r2] - 1
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/jz.ts b/instructions/jz.ts
new file mode 100644
index 0000000..0e105b3
--- /dev/null
+++ b/instructions/jz.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [addr]: string[]) {
+        if (this.returnFlag != 0) return;
+        this.programPointer = +addr - 1
+    },
+    args: 1
+}
\ No newline at end of file
diff --git a/instructions/ld.ts b/instructions/ld.ts
new file mode 100644
index 0000000..977f343
--- /dev/null
+++ b/instructions/ld.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, addr]: number[]) {
+        const r = this.lib.parseReg(reg)
+        this.registers[r] = this.getMem(Number(addr))
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/ldm.ts b/instructions/ldm.ts
new file mode 100644
index 0000000..c82425a
--- /dev/null
+++ b/instructions/ldm.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, addr]: number[]) {
+        this.registers[this.lib.parseReg(reg)] = this.getMem(this.getMem(Number(addr)))
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/ldr.ts b/instructions/ldr.ts
new file mode 100644
index 0000000..724b94c
--- /dev/null
+++ b/instructions/ldr.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2]: number[]) {
+        this.registers[this.lib.parseReg(reg1)] = this.getMem(this.registers[this.lib.parseReg(reg2)])
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/mod.ts b/instructions/mod.ts
new file mode 100644
index 0000000..24b96d5
--- /dev/null
+++ b/instructions/mod.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.registers[2] = this.registers[0] % this.registers[1]
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/mul.ts b/instructions/mul.ts
new file mode 100644
index 0000000..b625a07
--- /dev/null
+++ b/instructions/mul.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.registers[2] = this.registers[0] * this.registers[1]
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/not.ts b/instructions/not.ts
new file mode 100644
index 0000000..6416a5c
--- /dev/null
+++ b/instructions/not.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.registers[2] = ~this.registers[0]
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/or.ts b/instructions/or.ts
new file mode 100644
index 0000000..5a8b593
--- /dev/null
+++ b/instructions/or.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.registers[2] = this.registers[0] | this.registers[1]
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/put.ts b/instructions/put.ts
new file mode 100644
index 0000000..a35806e
--- /dev/null
+++ b/instructions/put.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, data]: number[]) {
+        const r = this.lib.parseReg(reg)
+        this.registers[r] = Number(data) ?? 0
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/ret.ts b/instructions/ret.ts
new file mode 100644
index 0000000..6a42313
--- /dev/null
+++ b/instructions/ret.ts
@@ -0,0 +1,10 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        const ret = this.returnStack.pop();
+        if (ret == undefined) throw 'whar'
+        this.programPointer = ret
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/sbc.ts b/instructions/sbc.ts
new file mode 100644
index 0000000..928aa61
--- /dev/null
+++ b/instructions/sbc.ts
@@ -0,0 +1,10 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2]: number[]) {
+        const r1 = this.lib.parseReg(reg1);
+        const r2 = this.lib.parseReg(reg2);
+        this.registers[r1] -= this.registers[r2] - this.returnFlag
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/shl.ts b/instructions/shl.ts
new file mode 100644
index 0000000..68345bb
--- /dev/null
+++ b/instructions/shl.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, by]: number[]) {
+        const r = this.lib.parseReg(reg);
+        this.registers[r] = this.registers[r] << by
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/shr.ts b/instructions/shr.ts
new file mode 100644
index 0000000..15e9345
--- /dev/null
+++ b/instructions/shr.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, by]: number[]) {
+        const r = this.lib.parseReg(reg);
+        this.registers[r] = this.registers[r] >> by
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/srm.ts b/instructions/srm.ts
new file mode 100644
index 0000000..b3c71fa
--- /dev/null
+++ b/instructions/srm.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, addr]: number[]) {
+        this.setMem(this.getMem(Number(addr)), this.registers[this.lib.parseReg(reg)])
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/srr.ts b/instructions/srr.ts
new file mode 100644
index 0000000..0a7e5c6
--- /dev/null
+++ b/instructions/srr.ts
@@ -0,0 +1,10 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2]: number[]) {
+        const r1 = this.lib.parseReg(reg1)
+        const r2 = this.registers[this.lib.parseReg(reg2)]
+        this.setMem(Number(r2), this.registers[r1] & 0xFFFF)
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/str.ts b/instructions/str.ts
new file mode 100644
index 0000000..bdcae63
--- /dev/null
+++ b/instructions/str.ts
@@ -0,0 +1,9 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg, addr]: number[]) {
+        const r = this.lib.parseReg(reg)
+        this.setMem(Number(addr), this.registers[r] & 0xFFFF)
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/sub.ts b/instructions/sub.ts
new file mode 100644
index 0000000..8e4d894
--- /dev/null
+++ b/instructions/sub.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.registers[2] = this.registers[0] - this.registers[1]
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/swp.ts b/instructions/swp.ts
new file mode 100644
index 0000000..48ee9b6
--- /dev/null
+++ b/instructions/swp.ts
@@ -0,0 +1,13 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2]: number[]) {
+        const r1 = this.lib.parseReg(reg1)
+        const r2 = this.lib.parseReg(reg2)
+        const data1 = Number(this.registers[r1])
+        const data2 = Number(this.registers[r2])
+        this.registers[r1] = data2
+        this.registers[r2] = data1
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/swpm.ts b/instructions/swpm.ts
new file mode 100644
index 0000000..f4856a2
--- /dev/null
+++ b/instructions/swpm.ts
@@ -0,0 +1,13 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC, [reg1, reg2]: number[]) {
+        const addr1 = this.registers[this.lib.parseReg(reg1)]
+        const addr2 = this.registers[this.lib.parseReg(reg2)]
+        const data1 = 0 + this.getMem(+addr1)
+        const data2 = 0 + this.getMem(+addr2)
+        this.setMem(+addr1, data2)
+        this.setMem(+addr2, data1)
+    },
+    args: 2
+}
\ No newline at end of file
diff --git a/instructions/sys.ts b/instructions/sys.ts
new file mode 100644
index 0000000..5131bf2
--- /dev/null
+++ b/instructions/sys.ts
@@ -0,0 +1,55 @@
+// deno-lint-ignore-file no-case-declarations no-process-globals
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        switch (this.registers[0]) {
+            case 0:
+                switch (this.registers[1]) {
+                    case 0:
+                        const data = new Uint8Array(1024)
+                        const len = Deno.stdin.readSync(data) ?? 0
+                        let i = 0;
+                        while (i < len) {
+                            this.mem[this.registers[2] + i] = data[i]
+                            i++
+                        }
+                        this.registers[0] = len
+                        this.mem[this.registers[2] + i] = 0
+                        break;
+
+                    default:
+                        throw 'unknown fd'
+                }
+                break;
+            case 1:
+                const writeBuff = [];
+                let i = this.registers[2];
+                while (this.mem[i] != 0 && i <= this.mem.length) {
+                    writeBuff.push(this.mem[i]);
+                    i++
+                }
+                switch (this.registers[1]) {
+                    case 1:
+                        process.stdout.write(writeBuff.map(a => String.fromCharCode(a)).join(''))
+                        break;
+
+                    case 2:
+                        process.stderr.write(writeBuff.map(a => String.fromCharCode(a)).join(''))
+                        break;
+
+                    default:
+                        throw 'unknown fd'
+                }
+                break;
+            
+            case 2:
+                Deno.stdin.setRaw(this.registers[1] ? true : false);
+                break;
+
+            default:
+                throw 'unknown syscall id ' + this.registers[0]
+        }
+    },
+    args: 0
+}
\ No newline at end of file
diff --git a/instructions/xor.ts b/instructions/xor.ts
new file mode 100644
index 0000000..4cccecd
--- /dev/null
+++ b/instructions/xor.ts
@@ -0,0 +1,8 @@
+import { PC } from "../pc.ts";
+
+export default {
+    function(this: PC) {
+        this.registers[2] = this.registers[0] ^ this.registers[1]
+    },
+    args: 0
+}
\ No newline at end of file