Commit | Line | Data |
---|---|---|
509643aa MG |
1 | package App::Scheme79asm; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
4a9c3fa9 | 7 | use Data::Dumper qw/Dumper/; |
509643aa MG |
8 | use Data::SExpression qw/consp scalarp/; |
9 | use Scalar::Util qw/looks_like_number/; | |
10 | ||
0edb4b34 | 11 | our $VERSION = '0.002'; |
509643aa MG |
12 | |
13 | our %TYPES = ( | |
14 | LIST => 0, | |
15 | SYMBOL => 1, | |
4a9c3fa9 | 16 | NUMBER => 1, |
509643aa MG |
17 | VAR => 2, |
18 | VARIABLE => 2, | |
19 | CLOSURE => 3, | |
20 | PROC => 4, | |
21 | PROCEDURE => 4, | |
4a9c3fa9 | 22 | IF => 5, |
509643aa MG |
23 | COND => 5, |
24 | CONDITIONAL => 5, | |
25 | CALL => 6, | |
509643aa MG |
26 | QUOTE => 7, |
27 | QUOTED => 7, | |
4a9c3fa9 MG |
28 | |
29 | MORE => 0, | |
30 | CAR => 1, | |
31 | CDR => 2, | |
32 | CONS => 3, | |
33 | ATOM => 4, | |
34 | PROGN => 5, | |
35 | MAKELIST => 6, | |
36 | FUNCALL => 7, | |
509643aa MG |
37 | ); |
38 | ||
39 | *consp = *Data::SExpression::consp; | |
40 | *scalarp = *Data::SExpression::scalarp; | |
41 | ||
42 | sub process { | |
4a9c3fa9 MG |
43 | my ($self, $sexp, $location) = @_; |
44 | die 'Toplevel is not a list: ', Dumper($sexp), "\n" unless ref $sexp eq 'ARRAY'; | |
45 | my ($type, @addrs) = @$sexp; | |
46 | my $addr; | |
47 | ||
48 | die 'Type of toplevel is not atom: '. Dumper($type), "\n" unless scalarp($type); | |
49 | ||
50 | if (@addrs > 1) { | |
51 | $addr = $self->{freeptr} + 1; | |
52 | $self->{freeptr} += @addrs; | |
53 | $self->process($addrs[$_], $addr + $_) for 0 .. $#addrs; | |
54 | } else { | |
55 | $addr = $addrs[0]; | |
56 | } | |
57 | ||
58 | $addr = $self->process($addr) if ref $addr eq 'ARRAY'; | |
59 | die 'Addr of toplevel is not atom: ', Dumper($addr), "\n" unless scalarp($addr); | |
509643aa | 60 | |
34073a4a MG |
61 | my ($comment_type, $comment_addr) = ($type, $addr); |
62 | ||
4a9c3fa9 MG |
63 | unless (looks_like_number $addr) { # is symbol |
64 | unless (exists $self->{symbols}{$addr}) { | |
65 | $self->{symbols}{$addr} = $self->{nsymbols}; | |
66 | $self->{nsymbols}++; | |
67 | } | |
68 | $addr = $self->{symbols}{$addr} | |
69 | } | |
509643aa | 70 | |
4a9c3fa9 | 71 | die 'Computed addr is not a number: ', Dumper($addr), "\n" unless looks_like_number $addr; |
509643aa MG |
72 | |
73 | if (ref $type eq 'Data::SExpression::Symbol') { | |
74 | die "No such type: $type\n" unless exists $TYPES{$type}; | |
75 | $type = $TYPES{$type}; | |
76 | } elsif (!looks_like_number $type) { | |
77 | die "Type is not a number or symbol: $type\n" | |
78 | } | |
79 | ||
80 | die "Type too large: $type\n" unless $type < (1 << $self->{type_bits}); | |
81 | die "Addr too large: $addr\n" unless $addr < (1 << $self->{addr_bits}); | |
82 | my $result = ($type << $self->{addr_bits}) + $addr; | |
4a9c3fa9 MG |
83 | unless ($location) { |
84 | $self->{freeptr}++; | |
85 | $location = $self->{freeptr} | |
86 | } | |
87 | $self->{memory}[$location] = $result; | |
34073a4a | 88 | $self->{comment}[$location] = "$comment_type $comment_addr"; |
4a9c3fa9 | 89 | $location |
509643aa MG |
90 | } |
91 | ||
92 | sub parse { | |
93 | my ($self, $string) = @_; | |
4a9c3fa9 | 94 | my $ds = Data::SExpression->new({symbol_case => 'up', use_symbol_class => 1, fold_lists => 1}); |
509643aa MG |
95 | |
96 | my $sexp; | |
97 | while () { | |
98 | last if $string =~ /^\s*$/; | |
99 | ($sexp, $string) = $ds->read($string); | |
100 | $self->process($sexp) | |
101 | } | |
102 | } | |
103 | ||
104 | sub finish { | |
105 | my ($self) = @_; | |
106 | $self->{memory}[5] = $self->{memory}[$self->{freeptr}]; | |
34073a4a | 107 | $self->{comment}[5] = $self->{comment}[$self->{freeptr}]; |
509643aa MG |
108 | $self->{memory}[4] = $self->{freeptr}; |
109 | delete $self->{memory}[$self->{freeptr}] | |
110 | } | |
111 | ||
112 | sub new { | |
113 | my ($class, %args) = @_; | |
114 | $args{type_bits} //= 3; | |
115 | $args{addr_bits} //= 8; | |
116 | $args{freeptr} //= 6; | |
117 | $args{memory} //= [0, 0, (1<<$args{addr_bits}), (1<<$args{addr_bits}), 0, 0, 0]; | |
4a9c3fa9 MG |
118 | $args{symbols}{NIL} = 0; |
119 | $args{symbols}{T} = 1; | |
120 | $args{nsymbols} = 2; | |
34073a4a | 121 | $args{comment} = ['(cdr part of NIL)', '(car part of NIL)', '(cdr part of T)', '(car part of T)', '(free storage pointer)', '', '(result of computation)']; |
509643aa MG |
122 | bless \%args, $class |
123 | } | |
124 | ||
125 | sub print { | |
126 | my ($self, $fh) = @_; | |
127 | $fh //= \*STDOUT; | |
128 | ||
129 | my $bits = $self->{type_bits} + $self->{addr_bits}; | |
34073a4a MG |
130 | my $index_length = length $#{$self->{memory}}; |
131 | my $index_format = '%' . $index_length . 'd'; | |
509643aa MG |
132 | for my $index (0 .. $#{$self->{memory}}) { |
133 | my $val = $self->{memory}[$index]; | |
34073a4a | 134 | my $comment = $self->{comment}[$index]; |
509643aa MG |
135 | if ($index == 4) { |
136 | $val = "${bits}'d$val" | |
137 | } else { | |
138 | $val = $val ? sprintf "%d'b%0${bits}b", $bits, $val : '0'; | |
139 | } | |
34073a4a MG |
140 | my $spaces = ' ' x ($bits + 5 - (length $val)); |
141 | $index = sprintf $index_format, $index; | |
142 | say $fh "mem[$index] <= $val;$spaces // $comment" | |
509643aa MG |
143 | } |
144 | } | |
145 | ||
146 | sub parse_and_print { | |
147 | my ($self, $string, $fh) = @_; | |
148 | $self->parse($string); | |
149 | $self->finish; | |
150 | $self->print($fh); | |
151 | } | |
152 | ||
153 | 1; | |
154 | __END__ | |
155 | ||
156 | =encoding utf-8 | |
157 | ||
158 | =head1 NAME | |
159 | ||
160 | App::Scheme79asm - assemble sexp to Verilog ROM for SIMPLE processor | |
161 | ||
162 | =head1 SYNOPSIS | |
163 | ||
164 | use App::Scheme79asm; | |
165 | my $asm = App::Scheme79asm->new(type_bits => 3, addr_bits => 5); | |
9924aa7b | 166 | $asm->parse_and_print('(number 70)'); |
509643aa MG |
167 | |
168 | =head1 DESCRIPTION | |
169 | ||
509643aa MG |
170 | SIMPLE is a LISP processor defined in the 1979 |
171 | B<Design of LISP-Based Processors> paper by Steele and Sussman. | |
172 | ||
173 | The SIMPLE processor expects input in a particular tagged-pointer | |
174 | format. This module takes a string containing a sequence of | |
9924aa7b MG |
175 | S-expressions. Each S-expression is a list of one of three types: |
176 | ||
177 | C<(tag value)>, for example C<(symbol nil)>, represents a value to be | |
178 | put in memory (for example a number, or a symbol, or a variable | |
179 | reference). | |
180 | ||
181 | C<(tag list)>, where C<list> is of one of these three types, | |
182 | represents a tagged pointer. In this case, C<list> is (recursively) | |
183 | laid out in memory as per these rules, and a pointer to that location | |
184 | (and tagged C<tag>) is put somewhere in memory. | |
185 | ||
186 | C<(tag list1 list2)>, where C<list1> and C<list2> are of one of these | |
187 | three types (not necessarily the same type). In this case, C<list1> | |
188 | and C<list2> are (recursively) laid out in memory such that C<list1> | |
189 | is at position X and C<list2> is at position X+1, and a pointer of | |
190 | type tag and value X is put somewhere in memory. | |
191 | ||
192 | After this process the very last pointer placed in memory is moved to | |
193 | the special location 5 (which is where SIMPLE expects to find the | |
194 | expression to be evaluated). | |
195 | ||
196 | In normal use a single S-expression will be supplied, representing an | |
197 | entire program. | |
198 | ||
68367057 MG |
199 | The C<tag> is either a number, a type, or a primitive. |
200 | The available types are: | |
509643aa | 201 | |
68367057 MG |
202 | =over |
203 | ||
204 | =item LIST | |
205 | ||
206 | =item SYMBOL (syn. NUMBER) | |
207 | ||
208 | =item VAR (syn. VARIABLE) | |
209 | ||
210 | =item CLOSURE | |
211 | ||
212 | =item PROC (syn. PROCEDURE) | |
213 | ||
214 | =item IF (syn. COND, CONDITIONAL) | |
215 | ||
216 | =item CALL | |
217 | ||
218 | =item QUOTE (syn. QUOTED) | |
219 | ||
220 | =back | |
221 | ||
222 | The available primitives are: | |
223 | ||
224 | =over | |
225 | ||
226 | =item MORE | |
227 | ||
228 | =item CAR | |
229 | ||
230 | =item CDR | |
231 | ||
232 | =item CONS | |
233 | ||
234 | =item ATOM | |
235 | ||
236 | =item PROGN | |
237 | ||
238 | =item MAKELIST | |
239 | ||
240 | =item FUNCALL | |
241 | ||
242 | =back | |
243 | ||
244 | The following methods are available: | |
245 | ||
246 | =over | |
247 | ||
248 | =item App::Scheme79asm->B<new>([key => value, key => value, ...]) | |
249 | ||
250 | Create a new assembler object. Takes a list of keys and values, here | |
251 | are the possible keys: | |
252 | ||
253 | =over | |
254 | ||
255 | =item type_bits | |
256 | ||
257 | =item address_bits | |
258 | ||
259 | A word is made of a type and an address, with the type occupying the | |
260 | most significant C<type_bits> (default 3) bits, and the address | |
261 | occupying the least significant C<address_bits> (default 8) bits. | |
262 | Therefore the word size is C<type_bits + address_bits> (default 13). | |
263 | ||
264 | =item freeptr | |
265 | ||
266 | A pointer to the last used byte in memory (default 6). The program | |
267 | will be laid out starting with location C<freeptr + 1>. | |
268 | ||
269 | =item memory | |
270 | ||
271 | The initial contents of the memory. Note that locations 4, 5, 6 will | |
272 | be overwritten, as will every location larger than the value of | |
273 | C<freeptr>. | |
274 | ||
275 | =item comment | |
276 | ||
277 | The initial comments for memory entries. C<< $comment->[$i] >> is the | |
278 | comment for C<< $memory->[$i] >>. | |
279 | ||
280 | =item symbols | |
281 | ||
282 | The initial symbol map, as a hashref from symbol name to the index of | |
283 | that symbol. Defaults to C<< {NIL => 0, T => 1} >>. | |
284 | ||
285 | =item nsymbols | |
286 | ||
287 | The number of distinct symbols in the initial symbols map (default 2). | |
288 | ||
289 | =back | |
290 | ||
291 | =item $asm->B<parse>(I<$string>) | |
292 | ||
293 | Parse a sequence of S-expressions and lay it out in memory. | |
294 | Can be called multiple times to lay out multiple sequences of | |
295 | S-expressions one after another. | |
296 | ||
297 | =item $asm->B<finish> | |
298 | ||
299 | Move the last pointer to position 5, and put the free pointer at | |
300 | position 4. After all sequences of S-expressions have been given to | |
301 | B<parse>, this method should be called. | |
302 | ||
303 | =item $asm->B<print>([I<$fh>]) | |
304 | ||
305 | Print a block of Verilog code assigning the memory contents to an | |
306 | array named C<mem> to the given filehandle (default STDOUT). | |
307 | ||
308 | =item $asm->B<parse_and_print>(I<$string>[, I<$fh>]) | |
309 | ||
310 | Convenience method that calls B<parse>($string), B<finish>, and then | |
311 | B<print>($fh). | |
312 | ||
313 | =back | |
509643aa MG |
314 | |
315 | =head1 SEE ALSO | |
316 | ||
317 | L<http://repository.readscheme.org/ftp/papers/ai-lab-pubs/AIM-514.pdf> | |
318 | ||
319 | =head1 AUTHOR | |
320 | ||
321 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> | |
322 | ||
323 | =head1 COPYRIGHT AND LICENSE | |
324 | ||
325 | Copyright (C) 2018 by Marius Gavrilescu | |
326 | ||
327 | This library is free software; you can redistribute it and/or modify | |
328 | it under the same terms as Perl itself, either Perl version 5.24.3 or, | |
329 | at your option, any later version of Perl 5 you may have available. | |
330 | ||
331 | ||
332 | =cut |