| ... | ... | @@ -0,0 +1,159 @@ |
| 1 | const std = @import("std"); |
| 2 | usingnamespace @import("defines.zig"); |
| 3 | |
| 4 | pub fn Interpreter(comptime Bus: type) type { |
| 5 | return struct { |
| 6 | ip: u16 = 0, |
| 7 | sp: u16 = undefined, |
| 8 | bp: u16 = undefined, |
| 9 | fr: FlagRegister = @bitCast(FlagRegister, @as(u16, 0)), |
| 10 | /// This is set to true when we hit an undefined0 instruction, allowing it to |
| 11 | /// be used as a trap for testing purposes |
| 12 | undefined0: bool = false, |
| 13 | bus: Bus, |
| 14 | |
| 15 | pub fn ExecuteBlock(self: *@This(), comptime size: ?u32) !void { |
| 16 | var count: usize = 0; |
| 17 | while (size == null or count < size.?) { |
| 18 | count += 1; |
| 19 | var instruction = @bitCast(Instruction, self.bus.read16(self.ip)); |
| 20 | |
| 21 | std.log.debug(.SPU_2_Interpreter, "Executing {}\n", .{instruction}); |
| 22 | |
| 23 | self.ip +%= 2; |
| 24 | |
| 25 | const execute = switch (instruction.condition) { |
| 26 | .always => true, |
| 27 | .not_zero => !self.fr.zero, |
| 28 | .when_zero => self.fr.zero, |
| 29 | .overflow => self.fr.carry, |
| 30 | ExecutionCondition.greater_or_equal_zero => !self.fr.negative, |
| 31 | else => return error.unimplemented, |
| 32 | }; |
| 33 | |
| 34 | if (execute) { |
| 35 | const val0 = switch (instruction.input0) { |
| 36 | .zero => @as(u16, 0), |
| 37 | .immediate => i: { |
| 38 | const val = self.bus.read16(@intCast(u16, self.ip)); |
| 39 | self.ip +%= 2; |
| 40 | break :i val; |
| 41 | }, |
| 42 | else => |e| e: { |
| 43 | // peek or pop; show value at current SP, and if pop, increment sp |
| 44 | const val = self.bus.read16(self.sp); |
| 45 | if (e == .pop) { |
| 46 | self.sp +%= 2; |
| 47 | } |
| 48 | break :e val; |
| 49 | }, |
| 50 | }; |
| 51 | const val1 = switch (instruction.input1) { |
| 52 | .zero => @as(u16, 0), |
| 53 | .immediate => i: { |
| 54 | const val = self.bus.read16(@intCast(u16, self.ip)); |
| 55 | self.ip +%= 2; |
| 56 | break :i val; |
| 57 | }, |
| 58 | else => |e| e: { |
| 59 | // peek or pop; show value at current SP, and if pop, increment sp |
| 60 | const val = self.bus.read16(self.sp); |
| 61 | if (e == .pop) { |
| 62 | self.sp +%= 2; |
| 63 | } |
| 64 | break :e val; |
| 65 | }, |
| 66 | }; |
| 67 | |
| 68 | const output: u16 = switch (instruction.command) { |
| 69 | .get => self.bus.read16(self.bp +% (2 *% val0)), |
| 70 | .set => a: { |
| 71 | self.bus.write16(self.bp +% 2 *% val0, val1); |
| 72 | break :a val1; |
| 73 | }, |
| 74 | .load8 => self.bus.read8(val0), |
| 75 | .load16 => self.bus.read16(val0), |
| 76 | .store8 => a: { |
| 77 | const val = @truncate(u8, val1); |
| 78 | self.bus.write8(val0, val); |
| 79 | break :a val; |
| 80 | }, |
| 81 | .store16 => a: { |
| 82 | self.bus.write16(val0, val1); |
| 83 | break :a val1; |
| 84 | }, |
| 85 | .copy => val0, |
| 86 | .add => a: { |
| 87 | var val: u16 = undefined; |
| 88 | self.fr.carry = @addWithOverflow(u16, val0, val1, &val); |
| 89 | break :a val; |
| 90 | }, |
| 91 | .sub => a: { |
| 92 | var val: u16 = undefined; |
| 93 | self.fr.carry = @subWithOverflow(u16, val0, val1, &val); |
| 94 | break :a val; |
| 95 | }, |
| 96 | .spset => a: { |
| 97 | self.sp = val0; |
| 98 | break :a val0; |
| 99 | }, |
| 100 | .bpset => a: { |
| 101 | self.bp = val0; |
| 102 | break :a val0; |
| 103 | }, |
| 104 | .frset => a: { |
| 105 | const val = (@bitCast(u16, self.fr) & val1) | (val0 & ~val1); |
| 106 | self.fr = @bitCast(FlagRegister, val); |
| 107 | break :a val; |
| 108 | }, |
| 109 | .bswap => (val0 >> 8) | (val0 << 8), |
| 110 | .bpget => self.bp, |
| 111 | .spget => self.sp, |
| 112 | .ipget => self.ip +% (2 *% val0), |
| 113 | .lsl => val0 << 1, |
| 114 | .lsr => val0 >> 1, |
| 115 | .@"and" => val0 & val1, |
| 116 | .@"or" => val0 | val1, |
| 117 | .xor => val0 ^ val1, |
| 118 | .not => ~val0, |
| 119 | .undefined0 => { |
| 120 | self.undefined0 = true; |
| 121 | // Break out of the loop, and let the caller decide what to do |
| 122 | return; |
| 123 | }, |
| 124 | .undefined1 => return error.BadInstruction, |
| 125 | .signext => if ((val0 & 0x80) != 0) |
| 126 | (val0 & 0xFF) | 0xFF00 |
| 127 | else |
| 128 | (val0 & 0xFF), |
| 129 | else => return error.unimplemented, |
| 130 | }; |
| 131 | |
| 132 | switch (instruction.output) { |
| 133 | .discard => {}, |
| 134 | .push => { |
| 135 | self.sp -%= 2; |
| 136 | self.bus.write16(self.sp, output); |
| 137 | }, |
| 138 | .jump => { |
| 139 | self.ip = output; |
| 140 | if (!(instruction.command == .copy and instruction.input0 == .immediate)) { |
| 141 | // Not absolute. Break, for compatibility with JIT. |
| 142 | break; |
| 143 | } |
| 144 | }, |
| 145 | else => return error.unimplemented, |
| 146 | } |
| 147 | if (instruction.modify_flags) { |
| 148 | self.fr.negative = (output & 0x8000) != 0; |
| 149 | self.fr.zero = (output == 0x0000); |
| 150 | } |
| 151 | } else { |
| 152 | if (instruction.input0 == .immediate) self.ip +%= 2; |
| 153 | if (instruction.input1 == .immediate) self.ip +%= 2; |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | }; |
| 159 | } |