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.

add a type for slot numbers

+43 -33
+33 -26
src/f18.zig
··· 2 2 3 3 pub const Word = i18; 4 4 5 + const Slot = enum(u2) { 6 + _, 7 + 8 + pub fn slot(num: u2) Slot { 9 + return @enumFromInt(num); 10 + } 11 + }; 12 + 5 13 /// type used by the P register 6 14 const ProgramCounter = packed struct(u10) { 7 15 /// holds the address of the next word in the instruction stream ··· 16 24 if (!self.address.io) self.address.local +%= 1; 17 25 } 18 26 19 - pub fn jump(self: *ProgramCounter, slot: u2, instruction: Word) void { 27 + pub fn jump(self: *ProgramCounter, slot: Slot, instruction: Word) void { 20 28 return switch (slot) { 21 - 0 => { 29 + .slot(0) => { 22 30 const long_jump: LongJump = @bitCast(instruction); 23 31 self.* = @bitCast(@as(u10, @intCast(long_jump.destination))); 24 32 }, 25 - 1 => { 33 + .slot(1) => { 26 34 const med_jump: Jump = @bitCast(instruction); 27 35 self.address.local = (self.address.local & ~@as(u7, 0b1111111)) | (med_jump.destination & 0b1111111); 28 36 }, 29 - 2 => { 37 + .slot(2) => { 30 38 const short_jump: ShortJump = @bitCast(instruction); 31 39 self.address.local = (self.address.local & ~@as(u3, 0b111)) | (short_jump.destination & 0b111); 32 40 }, ··· 86 94 slot_1: Opcode, 87 95 slot_0: Opcode, 88 96 89 - pub fn getSlot(self: Instruction, slot: u2) Opcode { 97 + pub fn getSlot(self: Instruction, slot: Slot) Opcode { 90 98 return switch (slot) { 91 - 0 => self.slot_0, 92 - 1 => self.slot_1, 93 - 2 => self.slot_2, 94 - 3 => Opcode.fromInt(self.slot_3), 99 + .slot(0) => self.slot_0, 100 + .slot(1) => self.slot_1, 101 + .slot(2) => self.slot_2, 102 + .slot(3) => Opcode.fromInt(self.slot_3), 103 + else => unreachable, 95 104 }; 96 105 } 97 106 }; ··· 125 134 126 135 state: State = .fetch, 127 136 // keeps track of current slot to help address decoding 128 - slot: u2, 137 + slot: Slot, 129 138 130 139 execution_time: f32, 131 140 ··· 141 150 continue :current_state .execution; 142 151 }, 143 152 .execution => { 144 - self.slot = 0; 153 + self.slot = .slot(0); 145 154 self.state = .execution; 146 - current_slot: switch (self.slot) { 155 + current_slot: switch (@intFromEnum(self.slot)) { 147 156 0...2 => { 148 157 self.execute(instruction.getSlot(self.slot)); 149 158 if (self.state != .execution) continue :current_state self.state; 150 - continue :current_slot self.slot; 159 + continue :current_slot @intFromEnum(self.slot); 151 160 }, 152 161 3 => { 153 162 self.execute(instruction.getSlot(self.slot)); ··· 163 172 } 164 173 continue :current_state .execution; 165 174 }, 166 - .next => { 167 - return; 168 - }, 175 + .next => return, 169 176 } 170 177 } 171 178 ··· 186 193 }, 187 194 else => { 188 195 self.state = .execution; 189 - self.slot +%= 1; 196 + self.slot = @enumFromInt(@intFromEnum(self.slot) +% 1); 190 197 opcodes.opcodes[code](self); 191 198 192 199 break :time 1.5; ··· 227 234 228 235 .carry = 0, 229 236 230 - .mem = [_]Word{0} ** 128, 237 + .mem = @splat(0), 231 238 232 239 .state = .fetch, 233 - .slot = 0, 240 + .slot = .slot(0), 234 241 .execution_time = 0.0, 235 242 }; 236 243 }; ··· 278 285 var p: ProgramCounter = .reset; 279 286 280 287 const instruction: Word = @bitCast(LongJump{ .destination = 0b1100000010 }); 281 - p.jump(0, instruction); 288 + p.jump(.slot(0), instruction); 282 289 283 290 try expectEqual(true, p.extended_arithmetic); 284 291 try expectEqual(true, p.address.io); ··· 290 297 var p: ProgramCounter = .reset; 291 298 292 299 const instruction: Word = @bitCast(Jump{ .destination = 0b0101010 }); 293 - p.jump(1, instruction); 300 + p.jump(.slot(1), instruction); 294 301 295 302 try expectEqual(false, p.extended_arithmetic); 296 303 try expectEqual(false, p.address.io); ··· 302 309 var p: ProgramCounter = .reset; 303 310 304 311 const instruction: Word = @bitCast(ShortJump{ .destination = 0b101 }); 305 - p.jump(2, instruction); 312 + p.jump(.slot(2), instruction); 306 313 307 314 try expectEqual(false, p.extended_arithmetic); 308 315 try expectEqual(false, p.address.io); ··· 365 372 }; 366 373 367 374 try expectEqual(0b010000000000000001, @as(Word, @bitCast(instruction))); 368 - try expectEqual(instruction.getSlot(0), instruction.slot_0); 369 - try expectEqual(instruction.getSlot(1), instruction.slot_1); 370 - try expectEqual(instruction.getSlot(2), instruction.slot_2); 371 - try expectEqual(instruction.getSlot(3), .unext); 375 + try expectEqual(instruction.getSlot(.slot(0)), instruction.slot_0); 376 + try expectEqual(instruction.getSlot(.slot(1)), instruction.slot_1); 377 + try expectEqual(instruction.getSlot(.slot(2)), instruction.slot_2); 378 + try expectEqual(instruction.getSlot(.slot(3)), .unext); 372 379 } 373 380 374 381 test "computer is initialized" {
+10 -7
src/opcodes.zig
··· 149 149 150 150 test "slot 1 jump" { 151 151 var computer: Computer = .reset; 152 - computer.slot = 1; 152 + computer.slot = .slot(1); 153 153 const expected_dest: u7 = 0b1111101; 154 154 computer.i = @bitCast(f18.Jump{ .destination = expected_dest }); 155 155 ··· 159 159 160 160 test "slot 2 jump" { 161 161 var computer: Computer = .reset; 162 - computer.slot = 2; 162 + computer.slot = .slot(2); 163 163 const expected_dest: u3 = 0b101; 164 164 computer.i = @bitCast(f18.ShortJump{ .destination = expected_dest }); 165 165 ··· 219 219 test next { 220 220 var computer: Computer = .reset; 221 221 computer.i = @bitCast(f18.Jump{ .destination = 0x44 }); 222 - computer.slot = 1; 222 + computer.slot = .slot(1); 223 223 computer.return_stack.push(0x123); 224 224 225 225 next(&computer); ··· 240 240 test _if { 241 241 var computer: Computer = .reset; 242 242 computer.i = @bitCast(f18.Jump{ .destination = 0x44 }); 243 - computer.slot = 1; 243 + computer.slot = .slot(1); 244 244 computer.data_stack.t = 0; 245 245 246 246 _if(&computer); ··· 262 262 test minus_if { 263 263 var computer: Computer = .reset; 264 264 computer.i = @bitCast(f18.Jump{ .destination = 0x44 }); 265 - computer.slot = 1; 265 + computer.slot = .slot(1); 266 266 computer.data_stack.t = 1; 267 267 268 268 minus_if(&computer); ··· 676 676 try expectEqual(0, computer.data_stack.t); 677 677 } 678 678 679 + const gasim = @import("root.zig"); 679 680 const f18 = @import("f18.zig"); 680 - const Address = f18.Address; 681 - const Computer = f18.Computer; 681 + 682 + const Address = gasim.Address; 683 + const Computer = gasim.Computer; 684 + const Word = gasim.Word; 682 685 683 686 const std = @import("std"); 684 687 const expectEqual = std.testing.expectEqual;