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.

Simplify slot execution logic

I can probably just do a for (0..3) loop here rather than a labeled
switch.

+29 -15
+29 -15
src/f18.zig
··· 11 11 12 12 /// Within RAM or ROM, an address is incremented after each word is fetched, 13 13 /// circularly within whichever storage class it currently points to. Within I/O 14 - /// space, it is not incremented. 14 + /// space, it is not incremented. Incrementing never affects bits P8 or P9 15 15 pub fn increment(self: *ProgramCounter) void { 16 16 if (!self.address.io) self.address.local +%= 1; 17 17 } ··· 85 85 slot_2: Opcode, 86 86 slot_1: Opcode, 87 87 slot_0: Opcode, 88 + 89 + pub fn getSlot(self: Instruction, slot: u2) Opcode { 90 + 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), 95 + }; 96 + } 88 97 }; 89 98 90 99 /// An F18A computer ··· 130 139 self.slot = 0; 131 140 self.state = .execution; 132 141 current_slot: switch (self.slot) { 133 - 0 => { 134 - _ = self.execute(instruction.slot_0); 135 - if (self.state != .execution) continue :current_state self.state; 136 - continue :current_slot self.slot; 137 - }, 138 - 1 => { 139 - _ = self.execute(instruction.slot_1); 140 - if (self.state != .execution) continue :current_state self.state; 141 - continue :current_slot self.slot; 142 - }, 143 - 2 => { 144 - _ = self.execute(instruction.slot_2); 142 + 0...2 => { 143 + _ = self.execute(instruction.getSlot(self.slot)); 145 144 if (self.state != .execution) continue :current_state self.state; 146 145 continue :current_slot self.slot; 147 146 }, 148 147 3 => { 149 148 _ = self.execute(Opcode.fromInt(instruction.slot_3)); 150 149 if (self.state != .execution) continue :current_state self.state; 151 - return; 150 + break :current_state; 152 151 }, 153 152 } 154 - continue :current_state self.state; 153 + return; 155 154 }, 156 155 .unext => { 157 156 const r = self.return_stack.t; ··· 368 367 369 368 const short_jump: ShortJump = .{ .destination = 0b101 }; 370 369 try expectEqual(word, @as(Word, @bitCast(short_jump))); 370 + } 371 + 372 + test Instruction { 373 + const instruction: Instruction = .{ 374 + .slot_0 = .@"@p", 375 + .slot_1 = .@";", 376 + .slot_2 = .@";", 377 + .slot_3 = 1, 378 + }; 379 + 380 + try expectEqual(0b010000000000000001, @as(Word, @bitCast(instruction))); 381 + try expectEqual(instruction.getSlot(0), instruction.slot_0); 382 + try expectEqual(instruction.getSlot(1), instruction.slot_1); 383 + try expectEqual(instruction.getSlot(2), instruction.slot_2); 384 + try expectEqual(instruction.getSlot(3), .unext); 371 385 } 372 386 373 387 test "computer is initialized" {