Bump version and update Changes
[app-scheme79asm.git] / lib / App / Scheme79asm.pm
CommitLineData
509643aa
MG
1package App::Scheme79asm;
2
3use 5.014000;
4use strict;
5use warnings;
6
4a9c3fa9 7use Data::Dumper qw/Dumper/;
509643aa
MG
8use Data::SExpression qw/consp scalarp/;
9use Scalar::Util qw/looks_like_number/;
10
3fff77b7 11our $VERSION = '0.004';
509643aa
MG
12
13our %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,
f0289d3d 35 'REVERSE-LIST' => 6,
4a9c3fa9 36 FUNCALL => 7,
509643aa
MG
37);
38
39*consp = *Data::SExpression::consp;
40*scalarp = *Data::SExpression::scalarp;
41
42sub 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
f0289d3d 80 $addr += (1 << $self->{addr_bits}) if $addr < 0;
509643aa
MG
81 die "Type too large: $type\n" unless $type < (1 << $self->{type_bits});
82 die "Addr too large: $addr\n" unless $addr < (1 << $self->{addr_bits});
83 my $result = ($type << $self->{addr_bits}) + $addr;
4a9c3fa9
MG
84 unless ($location) {
85 $self->{freeptr}++;
86 $location = $self->{freeptr}
87 }
88 $self->{memory}[$location] = $result;
34073a4a 89 $self->{comment}[$location] = "$comment_type $comment_addr";
4a9c3fa9 90 $location
509643aa
MG
91}
92
93sub parse {
94 my ($self, $string) = @_;
4a9c3fa9 95 my $ds = Data::SExpression->new({symbol_case => 'up', use_symbol_class => 1, fold_lists => 1});
509643aa
MG
96
97 my $sexp;
98 while () {
99 last if $string =~ /^\s*$/;
100 ($sexp, $string) = $ds->read($string);
101 $self->process($sexp)
102 }
103}
104
105sub finish {
106 my ($self) = @_;
107 $self->{memory}[5] = $self->{memory}[$self->{freeptr}];
34073a4a 108 $self->{comment}[5] = $self->{comment}[$self->{freeptr}];
509643aa
MG
109 $self->{memory}[4] = $self->{freeptr};
110 delete $self->{memory}[$self->{freeptr}]
111}
112
113sub new {
114 my ($class, %args) = @_;
115 $args{type_bits} //= 3;
116 $args{addr_bits} //= 8;
117 $args{freeptr} //= 6;
118 $args{memory} //= [0, 0, (1<<$args{addr_bits}), (1<<$args{addr_bits}), 0, 0, 0];
f0289d3d
MG
119 $args{symbols}{T} = 2;
120 $args{nsymbols} = 3;
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
f0289d3d
MG
125sub print_binary16 {
126 my ($self, $fh) = @_;
127 $fh //= \*STDOUT;
128
26bb1344
MG
129 die "addr_bits + type_bits >= 16\n"if $self->{addr_bits} + $self->{type_bits} > 16;
130
f0289d3d
MG
131 my $length = @{$self->{memory}};
132 print $fh pack('n', $length);
133 for (@{$self->{memory}}) {
134 print $fh pack('n', $_)
135 }
136}
137
138sub print_verilog {
509643aa
MG
139 my ($self, $fh) = @_;
140 $fh //= \*STDOUT;
141
142 my $bits = $self->{type_bits} + $self->{addr_bits};
34073a4a
MG
143 my $index_length = length $#{$self->{memory}};
144 my $index_format = '%' . $index_length . 'd';
509643aa
MG
145 for my $index (0 .. $#{$self->{memory}}) {
146 my $val = $self->{memory}[$index];
34073a4a 147 my $comment = $self->{comment}[$index];
509643aa
MG
148 if ($index == 4) {
149 $val = "${bits}'d$val"
150 } else {
151 $val = $val ? sprintf "%d'b%0${bits}b", $bits, $val : '0';
152 }
34073a4a
MG
153 my $spaces = ' ' x ($bits + 5 - (length $val));
154 $index = sprintf $index_format, $index;
155 say $fh "mem[$index] <= $val;$spaces // $comment"
509643aa 156 }
f0289d3d
MG
157
158}
159sub parse_and_print_binary16 {
160 my ($self, $string, $fh) = @_;
161 $self->parse($string);
162 $self->finish;
163 $self->print_binary16($fh);
509643aa
MG
164}
165
f0289d3d 166sub parse_and_print_verilog {
509643aa
MG
167 my ($self, $string, $fh) = @_;
168 $self->parse($string);
169 $self->finish;
f0289d3d 170 $self->print_verilog($fh);
509643aa
MG
171}
172
1731;
174__END__
175
176=encoding utf-8
177
178=head1 NAME
179
180App::Scheme79asm - assemble sexp to Verilog ROM for SIMPLE processor
181
182=head1 SYNOPSIS
183
184 use App::Scheme79asm;
185 my $asm = App::Scheme79asm->new(type_bits => 3, addr_bits => 5);
f0289d3d 186 $asm->parse_and_print_verilog('(number 70)');
509643aa
MG
187
188=head1 DESCRIPTION
189
509643aa
MG
190SIMPLE is a LISP processor defined in the 1979
191B<Design of LISP-Based Processors> paper by Steele and Sussman.
192
193The SIMPLE processor expects input in a particular tagged-pointer
194format. This module takes a string containing a sequence of
9924aa7b
MG
195S-expressions. Each S-expression is a list of one of three types:
196
197C<(tag value)>, for example C<(symbol nil)>, represents a value to be
198put in memory (for example a number, or a symbol, or a variable
199reference).
200
201C<(tag list)>, where C<list> is of one of these three types,
202represents a tagged pointer. In this case, C<list> is (recursively)
203laid out in memory as per these rules, and a pointer to that location
204(and tagged C<tag>) is put somewhere in memory.
205
206C<(tag list1 list2)>, where C<list1> and C<list2> are of one of these
207three types (not necessarily the same type). In this case, C<list1>
208and C<list2> are (recursively) laid out in memory such that C<list1>
209is at position X and C<list2> is at position X+1, and a pointer of
210type tag and value X is put somewhere in memory.
211
212After this process the very last pointer placed in memory is moved to
213the special location 5 (which is where SIMPLE expects to find the
214expression to be evaluated).
215
216In normal use a single S-expression will be supplied, representing an
217entire program.
218
68367057
MG
219The C<tag> is either a number, a type, or a primitive.
220The available types are:
509643aa 221
68367057
MG
222=over
223
224=item LIST
225
226=item SYMBOL (syn. NUMBER)
227
228=item VAR (syn. VARIABLE)
229
230=item CLOSURE
231
232=item PROC (syn. PROCEDURE)
233
234=item IF (syn. COND, CONDITIONAL)
235
236=item CALL
237
238=item QUOTE (syn. QUOTED)
239
240=back
241
242The available primitives are:
243
244=over
245
246=item MORE
247
248=item CAR
249
250=item CDR
251
252=item CONS
253
254=item ATOM
255
256=item PROGN
257
f0289d3d 258=item REVERSE-LIST
68367057
MG
259
260=item FUNCALL
261
262=back
263
264The following methods are available:
265
266=over
267
268=item App::Scheme79asm->B<new>([key => value, key => value, ...])
269
270Create a new assembler object. Takes a list of keys and values, here
271are the possible keys:
272
273=over
274
275=item type_bits
276
277=item address_bits
278
279A word is made of a type and an address, with the type occupying the
280most significant C<type_bits> (default 3) bits, and the address
281occupying the least significant C<address_bits> (default 8) bits.
282Therefore the word size is C<type_bits + address_bits> (default 13).
283
284=item freeptr
285
286A pointer to the last used byte in memory (default 6). The program
287will be laid out starting with location C<freeptr + 1>.
288
289=item memory
290
291The initial contents of the memory. Note that locations 4, 5, 6 will
292be overwritten, as will every location larger than the value of
293C<freeptr>.
294
295=item comment
296
297The initial comments for memory entries. C<< $comment->[$i] >> is the
298comment for C<< $memory->[$i] >>.
299
300=item symbols
301
302The initial symbol map, as a hashref from symbol name to the index of
f0289d3d 303that symbol. Defaults to C<< {T => 2} >>.
68367057
MG
304
305=item nsymbols
306
f0289d3d
MG
307The number to give to the "next" symbol (default 3, because T is
308defined to be 2).
68367057
MG
309
310=back
311
312=item $asm->B<parse>(I<$string>)
313
314Parse a sequence of S-expressions and lay it out in memory.
315Can be called multiple times to lay out multiple sequences of
316S-expressions one after another.
317
8ff4c670
MG
318=item $asm->B<process>(I<$sexp>)
319
320Given an already-parsed sexp (meaning a
321L<Data::SExpression> object), lay it out in memory.
322Can be called multiple times to lay out multiple sequences of
323S-expressions one after another.
324
68367057
MG
325=item $asm->B<finish>
326
327Move the last pointer to position 5, and put the free pointer at
328position 4. After all sequences of S-expressions have been given to
329B<parse>, this method should be called.
330
f0289d3d
MG
331=item $asm->B<print_binary16>([I<$fh>])
332
333Print the length of the memory (as a big-endian 16-bit value),
334followed by the memory contents as a sequence of big-endian 16-bit
335values to the given filehandle (default STDOUT). Dies if
336C<addr_bits + type_bits> is more than 16.
337
338Big-endian 16-bit values can be decoded with C<unpack 'n', $value>.
339
340=item $asm->B<print_verilog>([I<$fh>])
68367057
MG
341
342Print a block of Verilog code assigning the memory contents to an
343array named C<mem> to the given filehandle (default STDOUT).
344
f0289d3d
MG
345=item $asm->B<parse_and_print_binary16>(I<$string>[, I<$fh>])
346
347Convenience method that calls B<parse>($string), B<finish>, and then
348B<print_binary16>($fh).
349
350=item $asm->B<parse_and_print_verilog>(I<$string>[, I<$fh>])
68367057
MG
351
352Convenience method that calls B<parse>($string), B<finish>, and then
f0289d3d 353B<print_verilog>($fh).
68367057
MG
354
355=back
509643aa
MG
356
357=head1 SEE ALSO
358
359L<http://repository.readscheme.org/ftp/papers/ai-lab-pubs/AIM-514.pdf>
360
361=head1 AUTHOR
362
363Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
364
365=head1 COPYRIGHT AND LICENSE
366
367Copyright (C) 2018 by Marius Gavrilescu
368
369This library is free software; you can redistribute it and/or modify
370it under the same terms as Perl itself, either Perl version 5.24.3 or,
371at your option, any later version of Perl 5 you may have available.
372
373
374=cut
This page took 0.032647 seconds and 4 git commands to generate.