1 package App
::Scheme79asm
;
7 use Data
::SExpression qw
/consp scalarp/;
8 use Scalar
::Util qw
/looks_like_number/;
10 our $VERSION = '0.001';
28 *consp
= *Data
::SExpression
::consp
;
29 *scalarp
= *Data
::SExpression
::scalarp
;
32 my ($self, $sexp) = @_;
33 die "Toplevel is not a cons: $sexp\n " unless consp
($sexp);
34 my $type = $sexp->car;
35 my $addr = $sexp->cdr;
37 die "Type of toplevel is not atom: $type\n" unless scalarp
($type);
38 $addr = $self->process($addr) if consp
($addr);
39 die "Addr of toplevel is not atom: $addr\n" unless scalarp
($addr);
41 die "Computed addr is not a number: $addr\n" unless looks_like_number
$addr;
43 if (ref $type eq 'Data::SExpression::Symbol') {
44 die "No such type: $type\n" unless exists $TYPES{$type};
45 $type = $TYPES{$type};
46 } elsif (!looks_like_number
$type) {
47 die "Type is not a number or symbol: $type\n"
50 die "Type too large: $type\n" unless $type < (1 << $self->{type_bits
});
51 die "Addr too large: $addr\n" unless $addr < (1 << $self->{addr_bits
});
52 my $result = ($type << $self->{addr_bits
}) + $addr;
54 $self->{memory
}[$self->{freeptr
}] = $result;
59 my ($self, $string) = @_;
60 my $ds = Data
::SExpression
->new({symbol_case
=> 'up', use_symbol_class
=> 1, fold_lists
=> 0});
64 last if $string =~ /^\s*$/;
65 ($sexp, $string) = $ds->read($string);
72 $self->{memory
}[5] = $self->{memory
}[$self->{freeptr
}];
73 $self->{memory
}[4] = $self->{freeptr
};
74 delete $self->{memory
}[$self->{freeptr
}]
78 my ($class, %args) = @_;
79 $args{type_bits
} //= 3;
80 $args{addr_bits
} //= 8;
82 $args{memory
} //= [0, 0, (1<<$args{addr_bits
}), (1<<$args{addr_bits
}), 0, 0, 0];
90 my $bits = $self->{type_bits
} + $self->{addr_bits
};
91 for my $index (0 .. $#{$self->{memory}}) {
92 my $val = $self->{memory
}[$index];
94 $val = "${bits}'d$val"
96 $val = $val ?
sprintf "%d'b%0${bits}b", $bits, $val : '0';
98 say $fh "mem[$index] <= $val;"
102 sub parse_and_print
{
103 my ($self, $string, $fh) = @_;
104 $self->parse($string);
116 App::Scheme79asm - assemble sexp to Verilog ROM for SIMPLE processor
120 use App::Scheme79asm;
121 my $asm = App::Scheme79asm->new(type_bits => 3, addr_bits => 5);
122 $asm->parse_and_print('(number . 70)');
126 B<NOTE:> this module does not do much at the moment.
128 SIMPLE is a LISP processor defined in the 1979
129 B<Design of LISP-Based Processors> paper by Steele and Sussman.
131 The SIMPLE processor expects input in a particular tagged-pointer
132 format. This module takes a string containing a sequence of
133 S-expressions of the form C<(tag . value)> representing a tagged
134 pointer. Here the tag is either a number or one of several predefined
135 values (see the source for a full list), and the value is either a
136 number or another tagged pointer. These values are laid out in memory
137 and a block of verilog code assigning the memory contents to an array
138 named C<mem> is printed.
140 More documentation and features to follow.
144 L<http://repository.readscheme.org/ftp/papers/ai-lab-pubs/AIM-514.pdf>
148 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
150 =head1 COPYRIGHT AND LICENSE
152 Copyright (C) 2018 by Marius Gavrilescu
154 This library is free software; you can redistribute it and/or modify
155 it under the same terms as Perl itself, either Perl version 5.24.3 or,
156 at your option, any later version of Perl 5 you may have available.
This page took 0.032677 seconds and 5 git commands to generate.