diff options
Diffstat (limited to 'pc-thing/instructions_new')
26 files changed, 336 insertions, 0 deletions
diff --git a/pc-thing/instructions_new/add.ts b/pc-thing/instructions_new/add.ts new file mode 100644 index 0000000..5d211d3 --- /dev/null +++ b/pc-thing/instructions_new/add.ts @@ -0,0 +1,12 @@ +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.returnFlag = +((this.registers[r2] + this.registers[r3]) != 0); + this.registers[r1] = this.registers[r2] + this.registers[r3] + }, + args: 3 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/and.ts b/pc-thing/instructions_new/and.ts new file mode 100644 index 0000000..12db601 --- /dev/null +++ b/pc-thing/instructions_new/and.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/pc-thing/instructions_new/cmp.ts b/pc-thing/instructions_new/cmp.ts new file mode 100644 index 0000000..f97322a --- /dev/null +++ b/pc-thing/instructions_new/cmp.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/pc-thing/instructions_new/cmr.ts b/pc-thing/instructions_new/cmr.ts new file mode 100644 index 0000000..a83b46c --- /dev/null +++ b/pc-thing/instructions_new/cmr.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/pc-thing/instructions_new/div.ts b/pc-thing/instructions_new/div.ts new file mode 100644 index 0000000..c78cd93 --- /dev/null +++ b/pc-thing/instructions_new/div.ts @@ -0,0 +1,12 @@ +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.returnFlag = +((this.registers[r2] / this.registers[r3]) != 0); + this.registers[r1] = this.registers[r2] / this.registers[r3] + }, + args: 3 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/halt.ts b/pc-thing/instructions_new/halt.ts new file mode 100644 index 0000000..4685f2b --- /dev/null +++ b/pc-thing/instructions_new/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/pc-thing/instructions_new/jmp.ts b/pc-thing/instructions_new/jmp.ts new file mode 100644 index 0000000..f3bbc79 --- /dev/null +++ b/pc-thing/instructions_new/jmp.ts @@ -0,0 +1,9 @@ +import { PC } from "../pc.ts"; + +export default { + function(this: PC, [reg1]: number[]) { + const r1 = this.lib.parseReg(reg1); + this.programPointer = this.registers[r1] + }, + args: 1 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/jmr.ts b/pc-thing/instructions_new/jmr.ts new file mode 100644 index 0000000..f7b0c00 --- /dev/null +++ b/pc-thing/instructions_new/jmr.ts @@ -0,0 +1,10 @@ +import { PC } from "../pc.ts"; + +export default { + function(this: PC, [reg1]: number[]) { + const r1 = this.lib.parseReg(reg1); + this.returnStack.push(this.programPointer); + this.programPointer = this.registers[r1]; + }, + args: 1 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/jnz.ts b/pc-thing/instructions_new/jnz.ts new file mode 100644 index 0000000..d600f59 --- /dev/null +++ b/pc-thing/instructions_new/jnz.ts @@ -0,0 +1,10 @@ +import { PC } from "../pc.ts"; + +export default { + function(this: PC, [reg1]: number[]) { + if (this.returnFlag == 0) return; + const r1 = this.lib.parseReg(reg1); + this.programPointer = this.registers[r1] + }, + args: 1 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/jz.ts b/pc-thing/instructions_new/jz.ts new file mode 100644 index 0000000..0d4f64c --- /dev/null +++ b/pc-thing/instructions_new/jz.ts @@ -0,0 +1,10 @@ +import { PC } from "../pc.ts"; + +export default { + function(this: PC, [reg1]: number[]) { + if (this.returnFlag != 0) return; + const r1 = this.lib.parseReg(reg1); + this.programPointer = this.registers[r1] + }, + args: 1 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/ld.ts b/pc-thing/instructions_new/ld.ts new file mode 100644 index 0000000..8d6c30d --- /dev/null +++ b/pc-thing/instructions_new/ld.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); + this.returnFlag = +(this.getMem(this.registers[r2]) != 0); + this.registers[r1] = this.getMem(this.registers[r2]) + }, + args: 2 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/mod.ts b/pc-thing/instructions_new/mod.ts new file mode 100644 index 0000000..8f1a343 --- /dev/null +++ b/pc-thing/instructions_new/mod.ts @@ -0,0 +1,12 @@ +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.returnFlag = +((this.registers[r2] % this.registers[r3]) != 0); + this.registers[r1] = this.registers[r2] % this.registers[r3] + }, + args: 3 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/mov.ts b/pc-thing/instructions_new/mov.ts new file mode 100644 index 0000000..a35806e --- /dev/null +++ b/pc-thing/instructions_new/mov.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/pc-thing/instructions_new/mul.ts b/pc-thing/instructions_new/mul.ts new file mode 100644 index 0000000..bf3f12c --- /dev/null +++ b/pc-thing/instructions_new/mul.ts @@ -0,0 +1,12 @@ +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.returnFlag = +((this.registers[r2] * this.registers[r3]) != 0); + this.registers[r1] = this.registers[r2] * this.registers[r3] + }, + args: 3 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/not.ts b/pc-thing/instructions_new/not.ts new file mode 100644 index 0000000..241897a --- /dev/null +++ b/pc-thing/instructions_new/not.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); + this.returnFlag = +((~this.registers[r1]) != 0); + this.registers[r1] = ~this.registers[r2]; + }, + args: 2 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/or.ts b/pc-thing/instructions_new/or.ts new file mode 100644 index 0000000..bd7cc7e --- /dev/null +++ b/pc-thing/instructions_new/or.ts @@ -0,0 +1,12 @@ +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.returnFlag = +((this.registers[r2] | this.registers[r3]) != 0); + this.registers[r1] = this.registers[r2] | this.registers[r3]; + }, + args: 3 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/pop.ts b/pc-thing/instructions_new/pop.ts new file mode 100644 index 0000000..83ece88 --- /dev/null +++ b/pc-thing/instructions_new/pop.ts @@ -0,0 +1,12 @@ +import { PC } from "../pc.ts"; + +export default { + function(this: PC, [reg1]: number[]) { + if (!this.getMem(0x7000)) throw 'no stack pointer'; + if (this.getMem(0x7001) == 0) throw 'stack underflow'; + const r1 = this.lib.parseReg(reg1); + this.registers[r1] = this.getMem(this.getMem(0x7001) + 1); + this.setMem(0x7001, this.getMem(0x7001) - 1) + }, + args: 1 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/push.ts b/pc-thing/instructions_new/push.ts new file mode 100644 index 0000000..b9dd718 --- /dev/null +++ b/pc-thing/instructions_new/push.ts @@ -0,0 +1,12 @@ +import { PC } from "../pc.ts"; + +export default { + function(this: PC, [reg1]: number[]) { + if (!this.getMem(0x7000)) throw 'no stack pointer'; + if (this.getMem(0x7001) == 256) throw 'stack overflow'; + const r1 = this.lib.parseReg(reg1); + this.setMem(this.getMem(0x7000) + this.getMem(0x7001), this.registers[r1]) + this.setMem(0x7001, this.getMem(0x7001) + 1) + }, + args: 1 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/ret.ts b/pc-thing/instructions_new/ret.ts new file mode 100644 index 0000000..04b5cc3 --- /dev/null +++ b/pc-thing/instructions_new/ret.ts @@ -0,0 +1,10 @@ +import { PC } from "../pc.ts"; + +export default { + function(this: PC) { + const returnAddr = this.returnStack.pop(); + if (!returnAddr) throw 'return stack empty'; + this.programPointer = returnAddr; + }, + args: 1 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/shl.ts b/pc-thing/instructions_new/shl.ts new file mode 100644 index 0000000..16bb382 --- /dev/null +++ b/pc-thing/instructions_new/shl.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); + this.returnFlag = +((this.registers[r1] << this.registers[r2]) != 0); + this.registers[r1] <<= this.registers[r2] + }, + args: 2 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/shr.ts b/pc-thing/instructions_new/shr.ts new file mode 100644 index 0000000..09bf005 --- /dev/null +++ b/pc-thing/instructions_new/shr.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); + this.returnFlag = +((this.registers[r1] >> this.registers[r2]) != 0); + this.registers[r1] >>= this.registers[r2] + }, + args: 2 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/str.ts b/pc-thing/instructions_new/str.ts new file mode 100644 index 0000000..aa4c2d5 --- /dev/null +++ b/pc-thing/instructions_new/str.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.setMem(this.registers[r1], this.registers[r2]); + }, + args: 2 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/sub.ts b/pc-thing/instructions_new/sub.ts new file mode 100644 index 0000000..c7b4e94 --- /dev/null +++ b/pc-thing/instructions_new/sub.ts @@ -0,0 +1,12 @@ +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.returnFlag = +((this.registers[r2] - this.registers[r3]) != 0); + this.registers[r1] = this.registers[r2] - this.registers[r3] + }, + args: 3 +} \ No newline at end of file diff --git a/pc-thing/instructions_new/swp.ts b/pc-thing/instructions_new/swp.ts new file mode 100644 index 0000000..7d07aea --- /dev/null +++ b/pc-thing/instructions_new/swp.ts @@ -0,0 +1,15 @@ +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.registers[r2], this.registers[r1]] + // 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/pc-thing/instructions_new/sys.ts b/pc-thing/instructions_new/sys.ts new file mode 100644 index 0000000..fc27af9 --- /dev/null +++ b/pc-thing/instructions_new/sys.ts @@ -0,0 +1,60 @@ +// deno-lint-ignore-file no-case-declarations no-process-globals +import { PC } from "../pc.ts"; + +declare global { + const process: any; +} + +export default { + function(this: PC) { + if (typeof process == 'undefined') throw 'process global is undefined'; + 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/pc-thing/instructions_new/xor.ts b/pc-thing/instructions_new/xor.ts new file mode 100644 index 0000000..135e1cf --- /dev/null +++ b/pc-thing/instructions_new/xor.ts @@ -0,0 +1,12 @@ +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.returnFlag = +((this.registers[r2] ^ this.registers[r3]) != 0); + this.registers[r1] = this.registers[r2] ^ this.registers[r3]; + }, + args: 3 +} \ No newline at end of file |