From ef4e8c20719822eebd6318a878cc37902c2b85a5 Mon Sep 17 00:00:00 2001 From: WlodekM Date: Mon, 31 Mar 2025 19:27:55 +0300 Subject: pc thing --- instructions/add.ts | 8 ++++++++ instructions/and.ts | 8 ++++++++ instructions/clr.ts | 8 ++++++++ instructions/cmp.ts | 9 +++++++++ instructions/cmr.ts | 10 ++++++++++ instructions/crp.ts | 10 ++++++++++ instructions/crr.ts | 11 +++++++++++ instructions/dbg.ts | 13 +++++++++++++ instructions/dec.ts | 9 +++++++++ instructions/decr.ts | 10 ++++++++++ instructions/div.ts | 8 ++++++++ instructions/halt.ts | 9 +++++++++ instructions/inc.ts | 9 +++++++++ instructions/incr.ts | 10 ++++++++++ instructions/jmp.ts | 8 ++++++++ instructions/jmr.ts | 9 +++++++++ instructions/jnz.ts | 9 +++++++++ instructions/jnzr.ts | 11 +++++++++++ instructions/jz.ts | 9 +++++++++ instructions/ld.ts | 9 +++++++++ instructions/ldm.ts | 8 ++++++++ instructions/ldr.ts | 8 ++++++++ instructions/mod.ts | 8 ++++++++ instructions/mul.ts | 8 ++++++++ instructions/not.ts | 8 ++++++++ instructions/or.ts | 8 ++++++++ instructions/put.ts | 9 +++++++++ instructions/ret.ts | 10 ++++++++++ instructions/sbc.ts | 10 ++++++++++ instructions/shl.ts | 9 +++++++++ instructions/shr.ts | 9 +++++++++ instructions/srm.ts | 8 ++++++++ instructions/srr.ts | 10 ++++++++++ instructions/str.ts | 9 +++++++++ instructions/sub.ts | 8 ++++++++ instructions/swp.ts | 13 +++++++++++++ instructions/swpm.ts | 13 +++++++++++++ instructions/sys.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ instructions/xor.ts | 8 ++++++++ 39 files changed, 406 insertions(+) create mode 100644 instructions/add.ts create mode 100644 instructions/and.ts create mode 100644 instructions/clr.ts create mode 100644 instructions/cmp.ts create mode 100644 instructions/cmr.ts create mode 100644 instructions/crp.ts create mode 100644 instructions/crr.ts create mode 100644 instructions/dbg.ts create mode 100644 instructions/dec.ts create mode 100644 instructions/decr.ts create mode 100644 instructions/div.ts create mode 100644 instructions/halt.ts create mode 100644 instructions/inc.ts create mode 100644 instructions/incr.ts create mode 100644 instructions/jmp.ts create mode 100644 instructions/jmr.ts create mode 100644 instructions/jnz.ts create mode 100644 instructions/jnzr.ts create mode 100644 instructions/jz.ts create mode 100644 instructions/ld.ts create mode 100644 instructions/ldm.ts create mode 100644 instructions/ldr.ts create mode 100644 instructions/mod.ts create mode 100644 instructions/mul.ts create mode 100644 instructions/not.ts create mode 100644 instructions/or.ts create mode 100644 instructions/put.ts create mode 100644 instructions/ret.ts create mode 100644 instructions/sbc.ts create mode 100644 instructions/shl.ts create mode 100644 instructions/shr.ts create mode 100644 instructions/srm.ts create mode 100644 instructions/srr.ts create mode 100644 instructions/str.ts create mode 100644 instructions/sub.ts create mode 100644 instructions/swp.ts create mode 100644 instructions/swpm.ts create mode 100644 instructions/sys.ts create mode 100644 instructions/xor.ts (limited to 'instructions') 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 -- cgit 1.4.1-2-gfad0