| 1 | #!/usr/bin/perl |
| 2 | use v5.14; |
| 3 | use warnings; |
| 4 | use lib '.'; |
| 5 | |
| 6 | use asm; |
| 7 | |
| 8 | sub send_memory_to_news { |
| 9 | my ($address) = @_; |
| 10 | alu3 alu_select_a, alu_select_a, $address, $address, $address, flag_zero, flag_zero; |
| 11 | } |
| 12 | |
| 13 | sub flag_to_memory { |
| 14 | my ($address, $flag) = @_; |
| 15 | alu3 alu_zero, alu_select_f, 0, 0, $address, $flag, flag_zero; |
| 16 | } |
| 17 | |
| 18 | my $flag_F = 1; |
| 19 | |
| 20 | sub read_write_two_ways { |
| 21 | my ($news) = @_; |
| 22 | my $memory = $news + 1; |
| 23 | send_memory_to_news 0; |
| 24 | alu3 alu_zero, alu_select_f, 0, 0, $memory, flag_news($news), flag_zero; |
| 25 | send_memory_to_news 0; |
| 26 | alu3 aluc_addAF, alus_addAF, $memory, 0, $memory, flag_news($news + 1), $flag_F; |
| 27 | flag_to_memory $memory + 1, $flag_F; |
| 28 | } |
| 29 | |
| 30 | sub conway_step { |
| 31 | #say "CONWAY STEP"; |
| 32 | # write memory 0 to news |
| 33 | # read from news direction 0 into memory 1 |
| 34 | # write memory 0 to news |
| 35 | # read from news direction 1, add to memory 1, write into memory 1 and flag F |
| 36 | # write flag F to memory 2 |
| 37 | read_write_two_ways(0); |
| 38 | |
| 39 | # write memory 0 to news |
| 40 | # read from news direction 2 into memory 3 |
| 41 | # write memory 0 to news |
| 42 | # read from news direction 3, add to memory 3, write into memory 3 and flag F |
| 43 | # write flag F to memory 4 |
| 44 | read_write_two_ways(2); |
| 45 | |
| 46 | # write memory 0 to news |
| 47 | # read from news direction 4 into memory 5 |
| 48 | # write memory 0 to news |
| 49 | # read from news direction 5, add to memory 5, write into memory 5 and flag F |
| 50 | # write flag F to memory 6 |
| 51 | read_write_two_ways(4); |
| 52 | |
| 53 | # write memory 0 to news |
| 54 | # read from news direction 6 into memory 7 |
| 55 | # write memory 0 to news |
| 56 | # read from news direction 7, add to memory 7, write into memory 7 and flag F |
| 57 | # write flag F to memory 8 |
| 58 | read_write_two_ways(6); |
| 59 | |
| 60 | # read from memory 1, add to memory 3, write into memory 1 and flag F |
| 61 | add 1, 3, 1, $flag_F; |
| 62 | # read from memory 2, add to memory 4 and flag F, write into memory 2 and flag F |
| 63 | addC 2, 4, 2, $flag_F; |
| 64 | # write flag F to memory 3 |
| 65 | flag_to_memory 3, $flag_F; |
| 66 | |
| 67 | # read from memory 5, add to memory 7, write into memory 5 and flag F |
| 68 | add 5, 7, 5, $flag_F; |
| 69 | # read from memory 6, add to memory 8 and flag F, write into memory 6 and flag F |
| 70 | addC 6, 8, 6, $flag_F; |
| 71 | # write flag F to memory 7 |
| 72 | flag_to_memory 7, $flag_F; |
| 73 | |
| 74 | # read from memory 1, add to memory 5, write into memory 1 and flag F |
| 75 | add 1, 5, 1, $flag_F; |
| 76 | # read from memory 2, add to memory 6 and flag F, write into memory 2 and flag F |
| 77 | addC 2, 6, 2, $flag_F; |
| 78 | # read from memory 3, or with memory 7 and flag F, write into flag F |
| 79 | alu3 alu_or, alu_select_a, 3, 7, 3, $flag_F, $flag_F; |
| 80 | |
| 81 | # should a new cell be born here? (do we have 3 neighbours?) |
| 82 | sub alu_birth { alu_of_function {; $a && $b && !$_ } } |
| 83 | |
| 84 | # should a living cell survive here? (do we have 2 or 3 neighbours?) |
| 85 | sub alu_survival { alu_of_function {; $a && !$_ } } |
| 86 | |
| 87 | # compute the state of this cell at the next tick |
| 88 | sub alu_step { alu_of_function {; $_ & ($a | $b) } } |
| 89 | |
| 90 | # read from memory 1, and with memory 2 and not F, write into memory 1 (= birth-p) |
| 91 | alu3 alu_zero, alu_birth, 1, 2, 1, $flag_F, flag_zero; |
| 92 | # read from memory 2, and with flag not F, write into flag F (= survive-p) |
| 93 | alu3 alu_survival, alu_select_a, 2, 0, 2, $flag_F, $flag_F; |
| 94 | # read from memory 0, memory 1, and flag F, write F and (mem0 or mem1) into memory 0 |
| 95 | alu3 alu_zero, alu_step, 0, 1, 0, $flag_F, $flag_F; # also zeroes out flag F |
| 96 | |
| 97 | read_ 0; |
| 98 | # say 'Press enter to continue:'; |
| 99 | # my $nope = <>; |
| 100 | # say 'Pressed, continuing'; |
| 101 | read_ 0; |
| 102 | # read_ 1; |
| 103 | # read_ 2; |
| 104 | # read_ 3; |
| 105 | # read_ 4; |
| 106 | # read_ 5; |
| 107 | # read_ 6; |
| 108 | |
| 109 | #sleep 10; |
| 110 | } |
| 111 | |
| 112 | $| = 1; |
| 113 | |
| 114 | # start with a blinker |
| 115 | read_ 0; |
| 116 | loadi 0, 0x2022; |
| 117 | #sleep 100; |
| 118 | |
| 119 | store 0, flag_zero, 0, 0; |
| 120 | read_ 0; |
| 121 | read_ 0; |
| 122 | #sleep 10; |
| 123 | |
| 124 | conway_step; |
| 125 | conway_step; |
| 126 | conway_step; |
| 127 | conway_step; |
| 128 | conway_step; |
| 129 | conway_step; |