A cross-platform simulator for the GreenArrays GA144 multi-computer chip.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Clean up next/unext logic

Move some of the concern to the opcode functions

+41 -26
+3 -24
src/f18.zig
··· 119 119 execution, 120 120 unext, 121 121 next, 122 - skip, 123 122 }, 124 123 125 124 slot: u2, ··· 153 152 return; 154 153 }, 155 154 .unext => { 156 - const r = self.return_stack.t; 157 - if (r == 0) { 155 + if (self.return_stack.t == 0) { 158 156 _ = self.return_stack.pop(); 159 157 return; 160 - } else { 161 - self.return_stack.t -= 1; 162 158 } 163 159 continue :current_state .execution; 164 160 }, 165 161 .next => { 166 - const r = self.return_stack.t; 167 - if (r == 0) { 168 - _ = self.return_stack.pop(); 169 - } else { 170 - self.return_stack.t -= 1; 171 - self.p.jump(self.slot, self.i); 172 - } 173 - 174 - return; 175 - }, 176 - .skip => { 177 162 return; 178 163 }, 179 164 } ··· 182 167 pub fn execute(self: *Computer, opcode: Opcode) f64 { 183 168 const code: u5 = @intCast(@intFromEnum(opcode)); 184 169 switch (code) { 185 - 0x00...0x03 => { 186 - self.state = .skip; 170 + 0x00...0x03, 0x05...0x07 => { 171 + self.state = .next; 187 172 opcodes.opcodes[code](self); 188 173 189 174 return 5.1; ··· 193 178 opcodes.opcodes[code](self); 194 179 195 180 return 2.0; 196 - }, 197 - 0x05...0x07 => { 198 - self.state = .next; 199 - opcodes.opcodes[code](self); 200 - 201 - return 5.1; 202 181 }, 203 182 else => { 204 183 self.state = .execution;
+38 -2
src/opcodes.zig
··· 68 68 jump, 69 69 call, 70 70 unext, 71 - nop, 71 + next, 72 72 nop, 73 73 nop, 74 74 fetchP, ··· 190 190 /// Micronext. If R is zero, pops the return stack and continues with the next 191 191 /// opcode. If R is nonzero, decrements R by 1 and causes execution to continue 192 192 /// with slot 0 of the current instruction word without re-fetching the word. 193 - pub fn unext(_: *Computer) void {} 193 + pub fn unext(self: *Computer) void { 194 + self.return_stack.t -= 1; 195 + } 196 + 197 + test unext { 198 + var computer: Computer = .reset; 199 + computer.return_stack.push(0x123); 200 + 201 + unext(&computer); 202 + try expectEqual(0x122, computer.return_stack.t); 203 + } 204 + 205 + /// next 206 + /// 207 + /// **Next**. If R is zero, pops the return stack and continues with the next 208 + /// instruction word addressed by P. If R is nonzero, decrements R by 1 and 209 + /// jumps 210 + pub fn next(self: *Computer) void { 211 + if (self.return_stack.t == 0) { 212 + _ = self.return_stack.pop(); 213 + } else { 214 + self.return_stack.t -= 1; 215 + self.p.jump(self.slot, self.i); 216 + } 217 + } 218 + 219 + test next { 220 + var computer: Computer = .reset; 221 + computer.i = @bitCast(f18.Jump{ .destination = 0x44 }); 222 + computer.slot = 1; 223 + computer.return_stack.push(0x123); 224 + 225 + next(&computer); 226 + try expectEqual(0x122, computer.return_stack.t); 227 + computer.step(); 228 + try expectEqual(0x22, computer.p.address.local); 229 + } 194 230 195 231 /// @p 196 232 ///