1 package App
::Scheme79asm
;
7 use Data
::Dumper qw
/Dumper/;
8 use Data
::SExpression qw
/consp scalarp/;
9 use Scalar
::Util qw
/looks_like_number/;
11 our $VERSION = '0.004';
39 *consp
= *Data
::SExpression
::consp
;
40 *scalarp
= *Data
::SExpression
::scalarp
;
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;
48 die 'Type of toplevel is not atom: '. Dumper
($type), "\n" unless scalarp
($type);
51 $addr = $self->{freeptr
} + 1;
52 $self->{freeptr
} += @addrs;
53 $self->process($addrs[$_], $addr + $_) for 0 .. $#addrs;
58 $addr = $self->process($addr) if ref $addr eq 'ARRAY';
59 die 'Addr of toplevel is not atom: ', Dumper
($addr), "\n" unless scalarp
($addr);
61 my ($comment_type, $comment_addr) = ($type, $addr);
63 unless (looks_like_number
$addr) { # is symbol
64 unless (exists $self->{symbols
}{$addr}) {
65 $self->{symbols
}{$addr} = $self->{nsymbols
};
68 $addr = $self->{symbols
}{$addr}
71 die 'Computed addr is not a number: ', Dumper
($addr), "\n" unless looks_like_number
$addr;
73 if (!looks_like_number
$type) {
74 die "No such type: $type\n" unless exists $TYPES{$type};
75 $type = $TYPES{$type};
78 $addr += (1 << $self->{addr_bits
}) if $addr < 0;
79 die "Type too large: $type\n" unless $type < (1 << $self->{type_bits
});
80 die "Addr too large: $addr\n" unless $addr < (1 << $self->{addr_bits
});
81 my $result = ($type << $self->{addr_bits
}) + $addr;
84 $location = $self->{freeptr
}
86 $self->{memory
}[$location] = $result;
87 $self->{comment
}[$location] = "$comment_type $comment_addr";
92 my ($self, $string) = @_;
93 my $ds = Data
::SExpression
->new({symbol_case
=> 'up', use_symbol_class
=> 1, fold_lists
=> 1});
97 last if $string =~ /^\s*$/;
98 ($sexp, $string) = $ds->read($string);
105 $self->{memory
}[5] = $self->{memory
}[$self->{freeptr
}];
106 $self->{comment
}[5] = $self->{comment
}[$self->{freeptr
}];
107 $self->{memory
}[4] = $self->{freeptr
};
108 delete $self->{memory
}[$self->{freeptr
}]
112 my ($class, %args) = @_;
113 $args{type_bits
} //= 3;
114 $args{addr_bits
} //= 8;
115 $args{freeptr
} //= 6;
116 $args{memory
} //= [0, 0, (1<<$args{addr_bits
}), (1<<$args{addr_bits
}), 0, 0, 0];
117 $args{symbols
}{T
} = 2;
119 $args{comment
} = ['(cdr part of NIL)', '(car part of NIL)', '(cdr part of T)', '(car part of T)', '(free storage pointer)', '', '(result of computation)'];
124 my ($self, $fh) = @_;
127 die "addr_bits + type_bits >= 16\n"if $self->{addr_bits
} + $self->{type_bits
} > 16;
129 my $length = @
{$self->{memory
}};
130 print $fh pack('n', $length);
131 for (@
{$self->{memory
}}) {
132 print $fh pack('n', $_)
137 my ($self, $fh) = @_;
140 my $bits = $self->{type_bits
} + $self->{addr_bits
};
141 my $index_length = length $#{$self->{memory}};
142 my $index_format = '%' . $index_length . 'd';
143 for my $index (0 .. $#{$self->{memory}}) {
144 my $val = $self->{memory
}[$index];
145 my $comment = $self->{comment
}[$index];
147 $val = "${bits}'d$val"
149 $val = $val ?
sprintf "%d'b%0${bits}b", $bits, $val : '0';
151 my $spaces = ' ' x
($bits + 5 - (length $val));
152 $index = sprintf $index_format, $index;
153 say $fh "mem[$index] <= $val;$spaces // $comment"
157 sub parse_and_print_binary16
{
158 my ($self, $string, $fh) = @_;
159 $self->parse($string);
161 $self->print_binary16($fh);
164 sub parse_and_print_verilog
{
165 my ($self, $string, $fh) = @_;
166 $self->parse($string);
168 $self->print_verilog($fh);
178 App::Scheme79asm - assemble sexp to Verilog ROM for SIMPLE processor
182 use App::Scheme79asm;
183 my $asm = App::Scheme79asm->new(type_bits => 3, addr_bits => 5);
184 $asm->parse_and_print_verilog('(number 70)');
188 SIMPLE is a LISP processor defined in the 1979
189 B<Design of LISP-Based Processors> paper by Steele and Sussman.
191 The SIMPLE processor expects input in a particular tagged-pointer
192 format. This module takes a string containing a sequence of
193 S-expressions. Each S-expression is a list of one of three types:
195 C<(tag value)>, for example C<(symbol nil)>, represents a value to be
196 put in memory (for example a number, or a symbol, or a variable
199 C<(tag list)>, where C<list> is of one of these three types,
200 represents a tagged pointer. In this case, C<list> is (recursively)
201 laid out in memory as per these rules, and a pointer to that location
202 (and tagged C<tag>) is put somewhere in memory.
204 C<(tag list1 list2)>, where C<list1> and C<list2> are of one of these
205 three types (not necessarily the same type). In this case, C<list1>
206 and C<list2> are (recursively) laid out in memory such that C<list1>
207 is at position X and C<list2> is at position X+1, and a pointer of
208 type tag and value X is put somewhere in memory.
210 After this process the very last pointer placed in memory is moved to
211 the special location 5 (which is where SIMPLE expects to find the
212 expression to be evaluated).
214 In normal use a single S-expression will be supplied, representing an
217 The C<tag> is either a number, a type, or a primitive.
218 The available types are:
224 =item SYMBOL (syn. NUMBER)
226 =item VAR (syn. VARIABLE)
230 =item PROC (syn. PROCEDURE)
232 =item IF (syn. COND, CONDITIONAL)
236 =item QUOTE (syn. QUOTED)
240 The available primitives are:
262 The following methods are available:
266 =item App::Scheme79asm->B<new>([key => value, key => value, ...])
268 Create a new assembler object. Takes a list of keys and values, here
269 are the possible keys:
277 A word is made of a type and an address, with the type occupying the
278 most significant C<type_bits> (default 3) bits, and the address
279 occupying the least significant C<address_bits> (default 8) bits.
280 Therefore the word size is C<type_bits + address_bits> (default 13).
284 A pointer to the last used byte in memory (default 6). The program
285 will be laid out starting with location C<freeptr + 1>.
289 The initial contents of the memory. Note that locations 4, 5, 6 will
290 be overwritten, as will every location larger than the value of
295 The initial comments for memory entries. C<< $comment->[$i] >> is the
296 comment for C<< $memory->[$i] >>.
300 The initial symbol map, as a hashref from symbol name to the index of
301 that symbol. Defaults to C<< {T => 2} >>.
305 The number to give to the "next" symbol (default 3, because T is
310 =item $asm->B<parse>(I<$string>)
312 Parse a sequence of S-expressions and lay it out in memory.
313 Can be called multiple times to lay out multiple sequences of
314 S-expressions one after another.
316 =item $asm->B<process>(I<$sexp>)
318 Given an already-parsed sexp (meaning a
319 L<Data::SExpression> object), lay it out in memory.
320 Can be called multiple times to lay out multiple sequences of
321 S-expressions one after another.
323 =item $asm->B<finish>
325 Move the last pointer to position 5, and put the free pointer at
326 position 4. After all sequences of S-expressions have been given to
327 B<parse>, this method should be called.
329 =item $asm->B<print_binary16>([I<$fh>])
331 Print the length of the memory (as a big-endian 16-bit value),
332 followed by the memory contents as a sequence of big-endian 16-bit
333 values to the given filehandle (default STDOUT). Dies if
334 C<addr_bits + type_bits> is more than 16.
336 Big-endian 16-bit values can be decoded with C<unpack 'n', $value>.
338 =item $asm->B<print_verilog>([I<$fh>])
340 Print a block of Verilog code assigning the memory contents to an
341 array named C<mem> to the given filehandle (default STDOUT).
343 =item $asm->B<parse_and_print_binary16>(I<$string>[, I<$fh>])
345 Convenience method that calls B<parse>($string), B<finish>, and then
346 B<print_binary16>($fh).
348 =item $asm->B<parse_and_print_verilog>(I<$string>[, I<$fh>])
350 Convenience method that calls B<parse>($string), B<finish>, and then
351 B<print_verilog>($fh).
357 L<http://repository.readscheme.org/ftp/papers/ai-lab-pubs/AIM-514.pdf>
361 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
363 =head1 COPYRIGHT AND LICENSE
365 Copyright (C) 2018 by Marius Gavrilescu
367 This library is free software; you can redistribute it and/or modify
368 it under the same terms as Perl itself, either Perl version 5.24.3 or,
369 at your option, any later version of Perl 5 you may have available.
This page took 0.03894 seconds and 4 git commands to generate.