| 1 | #!/usr/bin/perl |
| 2 | use v5.14; |
| 3 | use warnings; |
| 4 | |
| 5 | use Device::SerialPort; |
| 6 | use IPC::Open3; |
| 7 | use Term::ReadLine; |
| 8 | use File::Slurp::Tiny qw/read_file/; |
| 9 | |
| 10 | my $term; |
| 11 | |
| 12 | if (-t) { |
| 13 | $term = Term::ReadLine->new('YULE REPL'); |
| 14 | say "YULE REPL\n"; |
| 15 | } |
| 16 | |
| 17 | my $port = Device::SerialPort->new($ARGV[0] // '/dev/ttyUSB1') or die "$!"; |
| 18 | $port->baudrate(300); |
| 19 | #$port->baudrate(4000000); |
| 20 | $port->parity('none'); |
| 21 | $port->databits(8); |
| 22 | $port->stopbits(1); |
| 23 | $port->handshake('none'); |
| 24 | $port->read_const_time(5000); |
| 25 | |
| 26 | $port->write_settings or die "$!"; |
| 27 | |
| 28 | my $asm = read_file 'cells.clump'; |
| 29 | |
| 30 | $port->write($asm); |
| 31 | my ($count_in, $string_in) = $port->read(5000); |
| 32 | my @memory = unpack 'v*', $string_in; |
| 33 | for (@memory) { |
| 34 | printf "%X ", $_ |
| 35 | } |
| 36 | |
| 37 | __END__ |
| 38 | |
| 39 | while () { |
| 40 | my $sexp; |
| 41 | if (-t) { |
| 42 | $sexp = $term->readline('* '); |
| 43 | } else { |
| 44 | $sexp = <>; |
| 45 | exit unless defined $sexp; |
| 46 | chomp $sexp; |
| 47 | next unless $sexp; |
| 48 | say "* $sexp"; |
| 49 | } |
| 50 | exit unless defined $sexp; |
| 51 | next unless $sexp; |
| 52 | my $compiler = App::Scheme79asm::Compiler->new; |
| 53 | my $compiler_out = $compiler->compile_string($sexp); |
| 54 | |
| 55 | # say 'Compiler says: ', dump_sexp($compiler_out); |
| 56 | |
| 57 | my $asm = App::Scheme79asm->new(addr_bits => 13); |
| 58 | my $asm_output; |
| 59 | open my $asm_fh, '>', \$asm_output; |
| 60 | $asm->process($compiler_out); |
| 61 | $asm->finish; |
| 62 | $asm->print_binary16($asm_fh); |
| 63 | close $asm_fh; |
| 64 | |
| 65 | # say "Writing: ", join ' ', uc join ' ', unpack '(H2)*', $asm_output; |
| 66 | my $bytes_written = $port->write($asm_output); |
| 67 | my $bytes_to_write = length $asm_output; |
| 68 | die "Only wrote $bytes_written instead of $bytes_to_write" unless $bytes_written == $bytes_to_write; |
| 69 | |
| 70 | my ($count_in, $string_in) = $port->read(5000); |
| 71 | my @memory = unpack 'n*', $string_in; |
| 72 | # say 'Received: ', uc join ' ', unpack '(H2)*', $string_in; |
| 73 | unshift @memory, 0, 0, (1<<13), (1<<13); |
| 74 | princ $compiler, \@memory, 6; |
| 75 | say ''; |
| 76 | } |