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.

Move tests to bottom of file.

This might be a sign to break this file down more as I did like having
the tests close to the functions they test, but I was getting lost.

+121 -121
+121 -121
src/f18.zig
··· 40 40 }; 41 41 }; 42 42 43 - test "ProgramCounter is reset" { 44 - var p: ProgramCounter = .reset; 45 - try expectEqual(false, p.extended_arithmetic); 46 - try expectEqual(Address.reset, p.address); 47 - p.address.io = true; 48 - p.address.rom = false; 49 - } 50 - 51 - test "I/O address is not incremented" { 52 - const io_address: Address = .{ .io = true, .rom = false, .local = 0x00 }; 53 - var p: ProgramCounter = .{ .address = io_address, .extended_arithmetic = false }; 54 - p.increment(); 55 - try expectEqual(0x00, p.address.local); 56 - } 57 - 58 - test "RAM address is incremented" { 59 - const ram_address: Address = .{ .io = false, .rom = false, .local = 0x0a }; 60 - var p: ProgramCounter = .{ .address = ram_address, .extended_arithmetic = false }; 61 - p.increment(); 62 - try expectEqual(0x0b, p.address.local); 63 - 64 - // RAM address wraps 65 - p.address.local = 0x7f; 66 - p.increment(); 67 - try expectEqual(0, p.address.local); 68 - } 69 - 70 - test "ROM address is incremented" { 71 - const rom_address: Address = .{ .io = false, .rom = true, .local = 0x0a }; 72 - var p: ProgramCounter = .{ .address = rom_address, .extended_arithmetic = false }; 73 - p.increment(); 74 - try expectEqual(0x0b, p.address.local); 75 - 76 - // ROM address wraps 77 - p.address.local = 0x7f; 78 - p.increment(); 79 - try expectEqual(0, p.address.local); 80 - } 81 - 82 - test "long jump" { 83 - var p: ProgramCounter = .reset; 84 - 85 - const instruction: Word = @bitCast(LongJump{ .destination = 0b1100000010 }); 86 - p.jump(0, instruction); 87 - 88 - try expectEqual(true, p.extended_arithmetic); 89 - try expectEqual(true, p.address.io); 90 - try expectEqual(false, p.address.rom); 91 - try expectEqual(0b10, p.address.local); 92 - } 93 - 94 - test "jump" { 95 - var p: ProgramCounter = .reset; 96 - 97 - const instruction: Word = @bitCast(Jump{ .destination = 0b0101010 }); 98 - p.jump(1, instruction); 99 - 100 - try expectEqual(false, p.extended_arithmetic); 101 - try expectEqual(false, p.address.io); 102 - try expectEqual(true, p.address.rom); 103 - try expectEqual(0b0101010, p.address.local); 104 - } 105 - 106 - test "short jump" { 107 - var p: ProgramCounter = .reset; 108 - 109 - const instruction: Word = @bitCast(ShortJump{ .destination = 0b101 }); 110 - p.jump(2, instruction); 111 - 112 - try expectEqual(false, p.extended_arithmetic); 113 - try expectEqual(false, p.address.io); 114 - try expectEqual(true, p.address.rom); 115 - try expectEqual(0b101, p.address.local); 116 - } 117 - 118 43 /// Address is a 9-bit value that points to a location in memory or 119 44 /// memory-mapped I/O. 120 45 pub const Address = packed struct(u9) { ··· 139 64 }; 140 65 }; 141 66 142 - test "address is set on reset" { 143 - // reset 144 - const reset_address: Address = .reset; 145 - try expectEqual(reset_address.io, false); 146 - try expectEqual(reset_address.rom, true); 147 - try expectEqual(reset_address.local, 0x42); 148 - } 149 - 150 - test "toWord" { 151 - const address: Address = .reset; 152 - 153 - try expectEqual(0xc2, address.toWord()); 154 - } 155 - 156 - test "fromWord" { 157 - const word: Word = 0b0000000011100010; 158 - const address: Address = Address.fromWord(word); 159 - 160 - try expectEqual(false, address.io); 161 - try expectEqual(true, address.rom); 162 - try expectEqual(0b1100010, address.local); 163 - } 164 - 165 67 pub const LongJump = packed struct(Word) { 166 68 destination: u10, 167 69 _: u8 = 0, 168 70 }; 169 71 170 - test LongJump { 171 - const word: Word = 0b000000001111111101; 172 - 173 - const long_jump: LongJump = .{ .destination = 0b1111111101 }; 174 - try expectEqual(word, @as(Word, @bitCast(long_jump))); 175 - } 176 - 177 72 pub const Jump = packed struct(Word) { 178 73 destination: u7, 179 74 p8: u1 = 0, 180 75 _: u10 = 0, 181 76 }; 182 77 183 - test Jump { 184 - const word: Word = 0b000000000001010101; 185 - 186 - const jump: Jump = .{ .destination = 0b1010101 }; 187 - try expectEqual(word, @as(Word, @bitCast(jump))); 188 - try expectEqual(word, jump.destination); 189 - try expectEqual(0, jump._); 190 - } 191 - 192 78 pub const ShortJump = packed struct(Word) { 193 79 destination: u3, 194 80 _: u15 = 0, 195 81 }; 196 - 197 - test ShortJump { 198 - const word: Word = 0b101; 199 - 200 - const short_jump: ShortJump = .{ .destination = 0b101 }; 201 - try expectEqual(word, @as(Word, @bitCast(short_jump))); 202 - } 203 82 204 83 pub const Instruction = packed struct(Word) { 205 84 slot_3: u3, ··· 369 248 .slot = 0, 370 249 }; 371 250 }; 251 + 252 + test "ProgramCounter is reset" { 253 + var p: ProgramCounter = .reset; 254 + try expectEqual(false, p.extended_arithmetic); 255 + try expectEqual(Address.reset, p.address); 256 + p.address.io = true; 257 + p.address.rom = false; 258 + } 259 + 260 + test "I/O address is not incremented" { 261 + const io_address: Address = .{ .io = true, .rom = false, .local = 0x00 }; 262 + var p: ProgramCounter = .{ .address = io_address, .extended_arithmetic = false }; 263 + p.increment(); 264 + try expectEqual(0x00, p.address.local); 265 + } 266 + 267 + test "RAM address is incremented" { 268 + const ram_address: Address = .{ .io = false, .rom = false, .local = 0x0a }; 269 + var p: ProgramCounter = .{ .address = ram_address, .extended_arithmetic = false }; 270 + p.increment(); 271 + try expectEqual(0x0b, p.address.local); 272 + 273 + // RAM address wraps 274 + p.address.local = 0x7f; 275 + p.increment(); 276 + try expectEqual(0, p.address.local); 277 + } 278 + 279 + test "ROM address is incremented" { 280 + const rom_address: Address = .{ .io = false, .rom = true, .local = 0x0a }; 281 + var p: ProgramCounter = .{ .address = rom_address, .extended_arithmetic = false }; 282 + p.increment(); 283 + try expectEqual(0x0b, p.address.local); 284 + 285 + // ROM address wraps 286 + p.address.local = 0x7f; 287 + p.increment(); 288 + try expectEqual(0, p.address.local); 289 + } 290 + 291 + test "long jump" { 292 + var p: ProgramCounter = .reset; 293 + 294 + const instruction: Word = @bitCast(LongJump{ .destination = 0b1100000010 }); 295 + p.jump(0, instruction); 296 + 297 + try expectEqual(true, p.extended_arithmetic); 298 + try expectEqual(true, p.address.io); 299 + try expectEqual(false, p.address.rom); 300 + try expectEqual(0b10, p.address.local); 301 + } 302 + 303 + test "jump" { 304 + var p: ProgramCounter = .reset; 305 + 306 + const instruction: Word = @bitCast(Jump{ .destination = 0b0101010 }); 307 + p.jump(1, instruction); 308 + 309 + try expectEqual(false, p.extended_arithmetic); 310 + try expectEqual(false, p.address.io); 311 + try expectEqual(true, p.address.rom); 312 + try expectEqual(0b0101010, p.address.local); 313 + } 314 + 315 + test "short jump" { 316 + var p: ProgramCounter = .reset; 317 + 318 + const instruction: Word = @bitCast(ShortJump{ .destination = 0b101 }); 319 + p.jump(2, instruction); 320 + 321 + try expectEqual(false, p.extended_arithmetic); 322 + try expectEqual(false, p.address.io); 323 + try expectEqual(true, p.address.rom); 324 + try expectEqual(0b101, p.address.local); 325 + } 326 + 327 + test "address is set on reset" { 328 + // reset 329 + const reset_address: Address = .reset; 330 + try expectEqual(reset_address.io, false); 331 + try expectEqual(reset_address.rom, true); 332 + try expectEqual(reset_address.local, 0x42); 333 + } 334 + 335 + test "toWord" { 336 + const address: Address = .reset; 337 + 338 + try expectEqual(0xc2, address.toWord()); 339 + } 340 + 341 + test "fromWord" { 342 + const word: Word = 0b0000000011100010; 343 + const address: Address = Address.fromWord(word); 344 + 345 + try expectEqual(false, address.io); 346 + try expectEqual(true, address.rom); 347 + try expectEqual(0b1100010, address.local); 348 + } 349 + 350 + test LongJump { 351 + const word: Word = 0b000000001111111101; 352 + 353 + const long_jump: LongJump = .{ .destination = 0b1111111101 }; 354 + try expectEqual(word, @as(Word, @bitCast(long_jump))); 355 + } 356 + 357 + test Jump { 358 + const word: Word = 0b000000000001010101; 359 + 360 + const jump: Jump = .{ .destination = 0b1010101 }; 361 + try expectEqual(word, @as(Word, @bitCast(jump))); 362 + try expectEqual(word, jump.destination); 363 + try expectEqual(0, jump._); 364 + } 365 + 366 + test ShortJump { 367 + const word: Word = 0b101; 368 + 369 + const short_jump: ShortJump = .{ .destination = 0b101 }; 370 + try expectEqual(word, @as(Word, @bitCast(short_jump))); 371 + } 372 372 373 373 test "computer is initialized" { 374 374 const computer: Computer = .reset;