]> iEval git - clump.git/blob - asm.pm
Conway works (with sleeps)
[clump.git] / asm.pm
1 #!/usr/bin/perl
2 use v5.14;
3 use warnings;
4
5 use constant +{
6 OP_NOP => 0,
7 OP_LOADA => 1,
8 OP_LOADB => 2,
9 OP_STORE => 3,
10 OP_READ => 4,
11 OP_LOADI => 5,
12 OP_ROUTE => 6,
13 OP_RUG => 7,
14 };
15
16 q,
17 use Device::SerialPort;
18
19 my $port = Device::SerialPort->new($ARGV[0] // '/dev/ttyUSB1') or die "$!";
20 $port->baudrate(300);
21 #$port->baudrate(4000000);
22 $port->parity('none');
23 $port->databits(8);
24 $port->stopbits(2);
25 $port->handshake('none');
26 $port->read_const_time(2000);
27
28 $port->write_settings or die "$!";
29 ,;
30
31 use Fcntl;
32 use Time::HiRes qw/sleep/;
33
34 sysopen my $port, '/dev/ttyUSB1', O_SYNC | O_RDWR or die "$!";
35
36
37 use parent qw/Exporter/;
38 our @EXPORT = qw/loada loadb store write_verilog alu_select_a alu_select_b alu_select_f alu_zero flag_zero flag_news alu_xor alu_xnor alu_or alu_of_function aluc_add alus_add aluc_addAF alus_addAF alu2 alu3 add addC/;
39 our @EXPORT_OK = qw/loada loadb store write_verilog alu_select_a alu_select_b alu_select_f alu_zero flag_zero flag_news alu_xor alu_xnor alu_or alu_of_function aluc_add alus_add aluc_addAF alus_addAF alu2 alu3 add addC/;
40
41 use File::Slurp::Tiny 'write_file';
42
43 sub send_ {
44 my ($cmd) = @_;
45 my %cmd = %$cmd;
46
47 my $binary = pack 'vCC', @cmd{qw/I mem_addr op/}; # we ignore CS for now
48 my $length = length $binary;
49 my $wrote = syswrite $port, $binary, $length;
50 sleep 0.2;
51 # say "Wrote $wrote of $length bytes";
52 if ($cmd{op} == OP_READ) {
53 my $string_in;
54 my $count_in = sysread $port, $string_in, 2;
55 my @memory = unpack 'v*', $string_in;
56 for (@memory) {
57 printf "%X", $_
58 }
59 print " "
60 }
61 }
62
63 sub loada {
64 my ($addr, $flagr, $bsel, $aluc) = @_;
65 my $I = 0;
66 $I |= $flagr;
67 $I |= $bsel << 4;
68 $I |= $aluc << 5;
69 send_
70 { I => $I, mem_addr => $addr, op => OP_LOADA, CS => 0 }
71 }
72
73 sub loadb {
74 my ($addr, $cond, $inv, $alus) = @_;
75 my $I = 0;
76 $I |= $cond;
77 $I |= $inv << 4;
78 $I |= $alus << 5;
79 send_
80 { I => $I, mem_addr => $addr, op => OP_LOADB, CS => 0 }
81 }
82
83 sub read_ {
84 my ($addr) = @_;
85 send_ { I => 0, mem_addr => $addr, op => OP_READ, CS => 1 }
86 }
87
88 sub store {
89 my ($addr, $flagw, $edge_, $cube) = @_;
90 my $I = 0;
91 $I |= $flagw;
92 $I |= $edge_ << 7;
93 $I |= $cube << 8;
94 send_
95 { I => $I, mem_addr => $addr, op => OP_STORE, CS => 0 }
96 }
97
98 sub loadi {
99 my ($addr, $I) = @_;
100 send_
101 { I => $I, mem_addr => $addr, op => OP_LOADI, CS => 0 }
102 }
103
104 sub flag_zero { 0 }
105
106 sub flag_news { 8 + $_[0] }
107
108 sub alu_select_a {
109 0b11110000
110 }
111
112 sub alu_select_b {
113 0b11001100
114 }
115
116 sub alu_select_f {
117 0b10101010
118 }
119
120 sub alu_zero {
121 0
122 }
123
124 sub alu_xor {
125 0b10010110
126 }
127
128 sub alu_xnor {
129 0b01101001
130 }
131
132 sub alu_of_function (&) {
133 my ($fun) = @_;
134 my $alu = 0;
135 for my $i (0 .. 7) {
136 local $a = ($i & 4) >> 2;
137 local $b = ($i & 2) >> 1;
138 local $_ = $i & 1;
139 $alu += ($fun->() ? 1 : 0) << $i;
140 }
141 $alu
142 }
143
144 sub aluc_add { alu_of_function { ($a + $b + $_) & 2 } }
145 sub alus_add { alu_of_function { ($a + $b + $_) & 1 } }
146
147 sub aluc_addAF { alu_of_function { ($a + $_) & 2 } }
148 sub alus_addAF { alu_of_function { ($a + $_) & 1 } }
149
150 sub alu_or { alu_of_function { $a | $b | $_ } }
151
152 sub alu2 {
153 my ($aluc, $alus, $addrA, $addrB, $flagr, $flagw, $cond, $inv) = @_;
154 loada $addrA, $flagr, 0, $aluc;
155 loadb $addrB, $cond, $inv, $alus;
156 store $addrA, $flagw, 0, 0;
157 }
158
159 sub alu3 {
160 my ($aluc, $alus, $addrA, $addrB, $addrC, $flagr, $flagw) = @_;
161 loada $addrA, $flagr, 0, $aluc;
162 loadb $addrB, 0, 1, $alus;
163 store $addrC, $flagw, 0, 0;
164 }
165
166 sub add {
167 my ($addrA, $addrB, $addrC, $flag_carry) = @_;
168 alu3 aluc_add, alus_add, $addrA, $addrB, $addrC, flag_zero, $flag_carry;
169 }
170
171 sub addC {
172 my ($addrA, $addrB, $addrC, $flag_carry) = @_;
173 alu3 aluc_add, alus_add, $addrA, $addrB, $addrC, $flag_carry, $flag_carry;
174 }
This page took 0.059078 seconds and 5 git commands to generate.