summary refs log tree commit diff
path: root/pc-thing/instructions/sys.ts
blob: 5131bf2220bc0d3ba4532060ecb324066ae15202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
}