summary refs log tree commit diff
path: root/pc-thing/instructions_new/sys.ts
diff options
context:
space:
mode:
authorWlodekM <[email protected]>2025-03-31 19:47:54 +0300
committerWlodekM <[email protected]>2025-03-31 19:47:54 +0300
commitcccb99226d3951fd9dfe1c4cf1c43126a1309d51 (patch)
tree518d3e965558ba313f103cee6161cd2b6aedb3b9 /pc-thing/instructions_new/sys.ts
parentef4e8c20719822eebd6318a878cc37902c2b85a5 (diff)
move to pc-thing/
Diffstat (limited to 'pc-thing/instructions_new/sys.ts')
-rw-r--r--pc-thing/instructions_new/sys.ts60
1 files changed, 60 insertions, 0 deletions
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