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.

Rename function argument to be more accurate

and more brief. don't know how I feel about that. I think that these
opcode functions maybe belong in the Computer container

+70 -70
+70 -70
src/opcodes.zig
··· 101 101 /// 102 102 /// **Return**. Moves R into P, popping the return stack. Skips any remaining slots 103 103 /// and fetches next instruction word. 104 - fn ret(self: *Computer) void { 105 - self.p.address = @bitCast(@as(u9, @intCast(self.return_stack.pop() & 0x1ff))); 104 + fn ret(c: *Computer) void { 105 + c.p.address = @bitCast(@as(u9, @intCast(c.return_stack.pop() & 0x1ff))); 106 106 } 107 107 108 108 test ret { ··· 116 116 /// ex 117 117 /// 118 118 /// **Execute**. Exchanges R and P, skips any remaining slots and fetches next instruction word. 119 - fn ex(self: *Computer) void { 120 - const current_address = self.p.address; 121 - self.p.address = Address.fromWord(self.return_stack.t); 122 - self.return_stack.t = current_address.toWord(); 119 + fn ex(c: *Computer) void { 120 + const current_address = c.p.address; 121 + c.p.address = Address.fromWord(c.return_stack.t); 122 + c.return_stack.t = current_address.toWord(); 123 123 } 124 124 125 125 test ex { ··· 134 134 /// name ; 135 135 /// 136 136 /// **Jump**. Sets P to destination address and fetches next instruction word. 137 - fn jump(self: *Computer) void { 138 - self.p.jump(self.slot, self.i); 137 + fn jump(c: *Computer) void { 138 + c.p.jump(c.slot, c.i); 139 139 } 140 140 141 141 test "slot 0 jump" { ··· 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(self: *Computer) void { 194 - self.return_stack.t -= 1; 193 + pub fn unext(c: *Computer) void { 194 + c.return_stack.t -= 1; 195 195 } 196 196 197 197 test unext { ··· 207 207 /// **Next**. If R is zero, pops the return stack and continues with the next 208 208 /// instruction word addressed by P. If R is nonzero, decrements R by 1 and 209 209 /// jumps 210 - pub fn next(self: *Computer) void { 211 - if (self.return_stack.t == 0) { 212 - _ = self.return_stack.pop(); 210 + pub fn next(c: *Computer) void { 211 + if (c.return_stack.t == 0) { 212 + _ = c.return_stack.pop(); 213 213 } else { 214 - self.return_stack.t -= 1; 215 - self.p.jump(self.slot, self.i); 214 + c.return_stack.t -= 1; 215 + c.p.jump(c.slot, c.i); 216 216 } 217 217 } 218 218 ··· 231 231 /// if 232 232 /// 233 233 /// **If**. If T is nonzero, continues with the next instruction word addressed by P. If T is zero, jumps 234 - pub fn _if(self: *Computer) void { 235 - if (self.data_stack.t == 0) { 236 - self.p.jump(self.slot, self.i); 234 + pub fn _if(c: *Computer) void { 235 + if (c.data_stack.t == 0) { 236 + c.p.jump(c.slot, c.i); 237 237 } 238 238 } 239 239 ··· 253 253 /// 254 254 /// Minus-if. If T is negative (T17 set), continues with the next instruction 255 255 /// word addressed by P. If T is positive, jumps 256 - pub fn minus_if(self: *Computer) void { 257 - if (self.data_stack.t >= 0) { 258 - self.p.jump(self.slot, self.i); 256 + pub fn minus_if(c: *Computer) void { 257 + if (c.data_stack.t >= 0) { 258 + c.p.jump(c.slot, c.i); 259 259 } 260 260 } 261 261 ··· 274 274 /// @p 275 275 /// 276 276 /// Pushes data stack, reads [P] into T, and increments P 277 - pub fn fetchP(self: *Computer) void { 278 - self.data_stack.push(self.fetch(self.p.address)); 279 - self.p.increment(); 277 + pub fn fetchP(c: *Computer) void { 278 + c.data_stack.push(c.fetch(c.p.address)); 279 + c.p.increment(); 280 280 } 281 281 282 282 test fetchP { ··· 292 292 /// @+ 293 293 /// 294 294 /// **Fetch-plus**. Pushes data stack, reads [A] into T, and increments A 295 - pub fn fetchPlus(self: *Computer) void { 296 - var address = Address.fromWord(self.a); 297 - self.data_stack.push(self.fetch(address)); 295 + pub fn fetchPlus(c: *Computer) void { 296 + var address = Address.fromWord(c.a); 297 + c.data_stack.push(c.fetch(address)); 298 298 address.local +%= 1; 299 - self.a = address.toWord(); 299 + c.a = address.toWord(); 300 300 } 301 301 302 302 test fetchPlus { ··· 344 344 /// !p 345 345 /// 346 346 /// **Store-P**. Writes T into [P], pops the data stack, and increments P 347 - pub fn storeP(self: *Computer) void { 348 - self.store(self.p.address, self.data_stack.pop()); 349 - self.p.increment(); 347 + pub fn storeP(c: *Computer) void { 348 + c.store(c.p.address, c.data_stack.pop()); 349 + c.p.increment(); 350 350 } 351 351 352 352 test storeP { ··· 363 363 /// !+ 364 364 /// 365 365 /// **Store-plus**. Writes T into [A], pops the data stack, and increments A 366 - pub fn storePlus(self: *Computer) void { 367 - var address = Address.fromWord(self.a); 368 - self.store(address, self.data_stack.pop()); 366 + pub fn storePlus(c: *Computer) void { 367 + var address = Address.fromWord(c.a); 368 + c.store(address, c.data_stack.pop()); 369 369 370 370 address.local +%= 1; 371 - self.a = address.toWord(); 371 + c.a = address.toWord(); 372 372 } 373 373 374 374 test storePlus { ··· 385 385 /// !b 386 386 /// 387 387 /// **Store-B**. Writes T into [B] and pops the data stack. 388 - pub fn storeB(self: *Computer) void { 389 - self.store(Address.fromWord(self.b), self.data_stack.pop()); 388 + pub fn storeB(c: *Computer) void { 389 + c.store(Address.fromWord(c.b), c.data_stack.pop()); 390 390 } 391 391 392 392 test storeB { ··· 402 402 /// ! 403 403 /// 404 404 /// **Store**. Writes T into [A] and pops the data stack. 405 - pub fn store(self: *Computer) void { 406 - self.store(Address.fromWord(self.a), self.data_stack.pop()); 405 + pub fn store(c: *Computer) void { 406 + c.store(Address.fromWord(c.a), c.data_stack.pop()); 407 407 } 408 408 409 409 test store { ··· 419 419 /// 2* 420 420 /// 421 421 /// **Two-Star**. Shifts T left one bit logically (shifts zero into T0, discards T17) thus multiplying a signed or unsigned value by two. 422 - pub fn shl(self: *Computer) void { 423 - self.data_stack.t <<= 1; 422 + pub fn shl(c: *Computer) void { 423 + c.data_stack.t <<= 1; 424 424 } 425 425 426 426 test shl { ··· 434 434 /// 2/ 435 435 /// 436 436 /// **Two-Slash**. Shifts T right one bit arithmetically (propagates T17 by leaving it unchanged; discards T0) thus dividing a signed value by two and discarding the positive remainder. 437 - pub fn shr(self: *Computer) void { 438 - self.data_stack.t >>= 1; 437 + pub fn shr(c: *Computer) void { 438 + c.data_stack.t >>= 1; 439 439 } 440 440 441 441 test shr { ··· 449 449 /// inv 450 450 /// 451 451 /// **Invert**. Inverts each bit of T, replacing T with its ones complement. 452 - pub fn inv(self: *Computer) void { 453 - self.data_stack.t = ~self.data_stack.t; 452 + pub fn inv(c: *Computer) void { 453 + c.data_stack.t = ~c.data_stack.t; 454 454 } 455 455 456 456 test inv { ··· 468 468 /// + 469 469 /// 470 470 /// **Plus**. Replaces T with the twos complement sum of S and T. Pops data stack into S. This instruction is affected in Extended Arithmetic Mode, becoming **Add with carry**. Includes the latched carry in the sum, and latches the carry out from bit 17. 471 - pub fn add(self: *Computer) void { 471 + pub fn add(c: *Computer) void { 472 472 var result: f18.Word = 0; 473 - if (self.p.extended_arithmetic) { 474 - result, self.carry = @addWithOverflow(self.data_stack.s + self.carry, self.data_stack.pop()); 473 + if (c.p.extended_arithmetic) { 474 + result, c.carry = @addWithOverflow(c.data_stack.s + c.carry, c.data_stack.pop()); 475 475 } else { 476 - result = self.data_stack.s + self.data_stack.pop(); 476 + result = c.data_stack.s + c.data_stack.pop(); 477 477 } 478 - self.data_stack.push(result); 478 + c.data_stack.push(result); 479 479 } 480 480 481 481 test add { ··· 508 508 /// and 509 509 /// 510 510 /// Replaces T with the Boolean AND of S and T. Pops data stack. 511 - pub fn _and(self: *Computer) void { 512 - self.data_stack.push(self.data_stack.s & self.data_stack.pop()); 511 + pub fn _and(c: *Computer) void { 512 + c.data_stack.push(c.data_stack.s & c.data_stack.pop()); 513 513 } 514 514 515 515 test _and { ··· 524 524 /// xor 525 525 /// 526 526 /// **Exclusive Or**. Replaces T with the Boolean XOR of S and T. Pops data stack. 527 - pub fn xor(self: *Computer) void { 528 - self.data_stack.push(self.data_stack.s ^ self.data_stack.pop()); 527 + pub fn xor(c: *Computer) void { 528 + c.data_stack.push(c.data_stack.s ^ c.data_stack.pop()); 529 529 } 530 530 531 531 test xor { ··· 540 540 /// drop 541 541 /// 542 542 /// Drops the top item from the data stack by copying S into T and popping the data stack. 543 - pub fn drop(self: *Computer) void { 544 - _ = self.data_stack.pop(); 543 + pub fn drop(c: *Computer) void { 544 + _ = c.data_stack.pop(); 545 545 } 546 546 547 547 test drop { ··· 557 557 /// dup 558 558 /// 559 559 /// Duplicates the top item on the data stack by pushing the data stack and copying T into S. 560 - pub fn dup(self: *Computer) void { 561 - self.data_stack.push(self.data_stack.t); 560 + pub fn dup(c: *Computer) void { 561 + c.data_stack.push(c.data_stack.t); 562 562 } 563 563 564 564 test dup { ··· 573 573 /// r> 574 574 /// 575 575 /// Moves R into T, popping the return stack and pushing the data stack. 576 - pub fn pop(self: *Computer) void { 577 - self.data_stack.push(self.return_stack.pop()); 576 + pub fn pop(c: *Computer) void { 577 + c.data_stack.push(c.return_stack.pop()); 578 578 } 579 579 580 580 test pop { ··· 590 590 /// 591 591 /// Makes a copy of S on top of the data stack by pushing S onto the stack, 592 592 /// moving T into S, and replacing T by the previous value of S. 593 - pub fn over(self: *Computer) void { 594 - self.data_stack.push(self.data_stack.s); 593 + pub fn over(c: *Computer) void { 594 + c.data_stack.push(c.data_stack.s); 595 595 } 596 596 597 597 test over { ··· 611 611 /// a 612 612 /// 613 613 /// Fetches the contents of register A into T, pushing the data stack. 614 - pub fn a(self: *Computer) void { 615 - self.data_stack.push(self.a); 614 + pub fn a(c: *Computer) void { 615 + c.data_stack.push(c.a); 616 616 } 617 617 618 618 test a { ··· 631 631 /// >r 632 632 /// 633 633 /// Moves T into R, pushing the return stack and popping the data stack. 634 - pub fn push(self: *Computer) void { 635 - self.return_stack.push(self.data_stack.pop()); 634 + pub fn push(c: *Computer) void { 635 + c.return_stack.push(c.data_stack.pop()); 636 636 } 637 637 638 638 test push { ··· 647 647 /// b! 648 648 /// 649 649 /// **B-Store**. Stores T into register B, popping the data stack. 650 - pub fn bStore(self: *Computer) void { 651 - self.b = @intCast(self.data_stack.pop() & 0x1ff); 650 + pub fn bStore(c: *Computer) void { 651 + c.b = @intCast(c.data_stack.pop() & 0x1ff); 652 652 } 653 653 654 654 test bStore { ··· 663 663 /// a! 664 664 /// 665 665 /// **A-Store**. Stores T into register A, popping the data stack. 666 - pub fn aStore(self: *Computer) void { 667 - self.a = self.data_stack.pop(); 666 + pub fn aStore(c: *Computer) void { 667 + c.a = c.data_stack.pop(); 668 668 } 669 669 670 670 test aStore {