aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/spu-mk2/interpreter.zig
blob: 1ec99546c6cbe1ee30d1473e455824d2f6dc9421 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const std = @import("std");
const log = std.log.scoped(.SPU_2_Interpreter);
const spu = @import("../spu-mk2.zig");
const FlagRegister = spu.FlagRegister;
const Instruction = spu.Instruction;
const ExecutionCondition = spu.ExecutionCondition;

pub fn Interpreter(comptime Bus: type) type {
    return struct {
        ip: u16 = 0,
        sp: u16 = undefined,
        bp: u16 = undefined,
        fr: FlagRegister = @bitCast(FlagRegister, @as(u16, 0)),
        /// This is set to true when we hit an undefined0 instruction, allowing it to
        /// be used as a trap for testing purposes
        undefined0: bool = false,
        /// This is set to true when we hit an undefined1 instruction, allowing it to
        /// be used as a trap for testing purposes. undefined1 is used as a breakpoint.
        undefined1: bool = false,
        bus: Bus,

        pub fn ExecuteBlock(self: *@This(), comptime size: ?u32) !void {
            var count: usize = 0;
            while (size == null or count < size.?) {
                count += 1;
                var instruction = @bitCast(Instruction, self.bus.read16(self.ip));

                log.debug("Executing {}\n", .{instruction});

                self.ip +%= 2;

                const execute = switch (instruction.condition) {
                    .always => true,
                    .not_zero => !self.fr.zero,
                    .when_zero => self.fr.zero,
                    .overflow => self.fr.carry,
                    ExecutionCondition.greater_or_equal_zero => !self.fr.negative,
                    else => return error.Unimplemented,
                };

                if (execute) {
                    const val0 = switch (instruction.input0) {
                        .zero => @as(u16, 0),
                        .immediate => i: {
                            const val = self.bus.read16(@intCast(u16, self.ip));
                            self.ip +%= 2;
                            break :i val;
                        },
                        else => |e| e: {
                            // peek or pop; show value at current SP, and if pop, increment sp
                            const val = self.bus.read16(self.sp);
                            if (e == .pop) {
                                self.sp +%= 2;
                            }
                            break :e val;
                        },
                    };
                    const val1 = switch (instruction.input1) {
                        .zero => @as(u16, 0),
                        .immediate => i: {
                            const val = self.bus.read16(@intCast(u16, self.ip));
                            self.ip +%= 2;
                            break :i val;
                        },
                        else => |e| e: {
                            // peek or pop; show value at current SP, and if pop, increment sp
                            const val = self.bus.read16(self.sp);
                            if (e == .pop) {
                                self.sp +%= 2;
                            }
                            break :e val;
                        },
                    };

                    const output: u16 = switch (instruction.command) {
                        .get => self.bus.read16(self.bp +% (2 *% val0)),
                        .set => a: {
                            self.bus.write16(self.bp +% 2 *% val0, val1);
                            break :a val1;
                        },
                        .load8 => self.bus.read8(val0),
                        .load16 => self.bus.read16(val0),
                        .store8 => a: {
                            const val = @truncate(u8, val1);
                            self.bus.write8(val0, val);
                            break :a val;
                        },
                        .store16 => a: {
                            self.bus.write16(val0, val1);
                            break :a val1;
                        },
                        .copy => val0,
                        .add => a: {
                            var val: u16 = undefined;
                            self.fr.carry = @addWithOverflow(u16, val0, val1, &val);
                            break :a val;
                        },
                        .sub => a: {
                            var val: u16 = undefined;
                            self.fr.carry = @subWithOverflow(u16, val0, val1, &val);
                            break :a val;
                        },
                        .spset => a: {
                            self.sp = val0;
                            break :a val0;
                        },
                        .bpset => a: {
                            self.bp = val0;
                            break :a val0;
                        },
                        .frset => a: {
                            const val = (@bitCast(u16, self.fr) & val1) | (val0 & ~val1);
                            self.fr = @bitCast(FlagRegister, val);
                            break :a val;
                        },
                        .bswap => (val0 >> 8) | (val0 << 8),
                        .bpget => self.bp,
                        .spget => self.sp,
                        .ipget => self.ip +% (2 *% val0),
                        .lsl => val0 << 1,
                        .lsr => val0 >> 1,
                        .@"and" => val0 & val1,
                        .@"or" => val0 | val1,
                        .xor => val0 ^ val1,
                        .not => ~val0,
                        .undefined0 => {
                            self.undefined0 = true;
                            // Break out of the loop, and let the caller decide what to do
                            return;
                        },
                        .undefined1 => {
                            self.undefined1 = true;
                            // Break out of the loop, and let the caller decide what to do
                            return;
                        },
                        .signext => if ((val0 & 0x80) != 0)
                            (val0 & 0xFF) | 0xFF00
                        else
                            (val0 & 0xFF),
                        else => return error.Unimplemented,
                    };

                    switch (instruction.output) {
                        .discard => {},
                        .push => {
                            self.sp -%= 2;
                            self.bus.write16(self.sp, output);
                        },
                        .jump => {
                            self.ip = output;
                        },
                        else => return error.Unimplemented,
                    }
                    if (instruction.modify_flags) {
                        self.fr.negative = (output & 0x8000) != 0;
                        self.fr.zero = (output == 0x0000);
                    }
                } else {
                    if (instruction.input0 == .immediate) self.ip +%= 2;
                    if (instruction.input1 == .immediate) self.ip +%= 2;
                    break;
                }
            }
        }
    };
}