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.001';
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 unless (looks_like_number
$addr) { # is symbol
62 unless (exists $self->{symbols
}{$addr}) {
63 $self->{symbols
}{$addr} = $self->{nsymbols
};
66 $addr = $self->{symbols
}{$addr}
69 die 'Computed addr is not a number: ', Dumper
($addr), "\n" unless looks_like_number
$addr;
71 if (ref $type eq 'Data::SExpression::Symbol') {
72 die "No such type: $type\n" unless exists $TYPES{$type};
73 $type = $TYPES{$type};
74 } elsif (!looks_like_number
$type) {
75 die "Type is not a number or symbol: $type\n"
78 die "Type too large: $type\n" unless $type < (1 << $self->{type_bits
});
79 die "Addr too large: $addr\n" unless $addr < (1 << $self->{addr_bits
});
80 my $result = ($type << $self->{addr_bits
}) + $addr;
83 $location = $self->{freeptr
}
85 $self->{memory
}[$location] = $result;
90 my ($self, $string) = @_;
91 my $ds = Data
::SExpression
->new({symbol_case
=> 'up', use_symbol_class
=> 1, fold_lists
=> 1});
95 last if $string =~ /^\s*$/;
96 ($sexp, $string) = $ds->read($string);
103 $self->{memory
}[5] = $self->{memory
}[$self->{freeptr
}];
104 $self->{memory
}[4] = $self->{freeptr
};
105 delete $self->{memory
}[$self->{freeptr
}]
109 my ($class, %args) = @_;
110 $args{type_bits
} //= 3;
111 $args{addr_bits
} //= 8;
112 $args{freeptr
} //= 6;
113 $args{memory
} //= [0, 0, (1<<$args{addr_bits
}), (1<<$args{addr_bits
}), 0, 0, 0];
114 $args{symbols
}{NIL
} = 0;
115 $args{symbols
}{T
} = 1;
121 my ($self, $fh) = @_;
124 my $bits = $self->{type_bits
} + $self->{addr_bits
};
125 for my $index (0 .. $#{$self->{memory}}) {
126 my $val = $self->{memory
}[$index];
128 $val = "${bits}'d$val"
130 $val = $val ?
sprintf "%d'b%0${bits}b", $bits, $val : '0';
132 say $fh "mem[$index] <= $val;"
136 sub parse_and_print
{
137 my ($self, $string, $fh) = @_;
138 $self->parse($string);
150 App::Scheme79asm - assemble sexp to Verilog ROM for SIMPLE processor
154 use App::Scheme79asm;
155 my $asm = App::Scheme79asm->new(type_bits => 3, addr_bits => 5);
156 $asm->parse_and_print('(number . 70)');
160 B<NOTE:> this module does not do much at the moment.
162 SIMPLE is a LISP processor defined in the 1979
163 B<Design of LISP-Based Processors> paper by Steele and Sussman.
165 The SIMPLE processor expects input in a particular tagged-pointer
166 format. This module takes a string containing a sequence of
167 S-expressions of the form C<(tag . value)> representing a tagged
168 pointer. Here the tag is either a number or one of several predefined
169 values (see the source for a full list), and the value is either a
170 number or another tagged pointer. These values are laid out in memory
171 and a block of verilog code assigning the memory contents to an array
172 named C<mem> is printed.
174 More documentation and features to follow.
178 L<http://repository.readscheme.org/ftp/papers/ai-lab-pubs/AIM-514.pdf>
182 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
184 =head1 COPYRIGHT AND LICENSE
186 Copyright (C) 2018 by Marius Gavrilescu
188 This library is free software; you can redistribute it and/or modify
189 it under the same terms as Perl itself, either Perl version 5.24.3 or,
190 at your option, any later version of Perl 5 you may have available.
This page took 0.030465 seconds and 4 git commands to generate.