use autodie;
use Data::Dump qw(dump);
use IO::Socket::INET;
+use Storable;
+
+my $openocd_remote = shift @ARGV || 'picam:4444';
+my $bsdl_file = $ENV{BSDL} ||
+ 'EP4CE6E22.bsd';
+# '_3128at100.bsd';
+# '3064at44.bsd'; # XXX
+my $tap = $ENV{TAP} || 'tb276.tap';
+my $project_file = 'Altera/epm3064_dac/3064at44.qsf';
+$project_file = 'tb276/f32c.qsf';
+
+my $BOUNDARY_LENGTH = -1;
+my $entity;
my $bit2pin;
my $io2bit;
my $pin2io;
-my $in_pin_map = 0;
-my $pin_map = '';
+my $parse;
+my $parse_in = 0;
+my $parse_regex = join('|', qw(
+ PIN_MAP_STRING
+ INSTRUCTION_OPCODE
+));
+
+$|=1; # flush stdout
+my $have_gpio = `which gpio`;
+warn "# ", ! $have_gpio ? "DOESN'T " : '', "have rpi gpio\n";
+require 'gpio.pl' if $have_gpio;
-open(my $bsdl, '<', '_3128at100.bsd');
+open(my $bsdl, '<', $bsdl_file);
while(<$bsdl>) {
- if ( m/PIN_MAP_STRING/ ) {
- $pin_map .= $_;
- $in_pin_map = 1;
- } elsif ( $in_pin_map ) {
+ if ( m/($parse_regex)/ ) {
+ $parse_in = $1;
+ $parse->{$1} .= $_;
+ } elsif ( $parse_in ) {
next if m/^\s*--/;
- $pin_map .= $_;
- $in_pin_map = 0 if m/;/;
+ $parse->{$parse_in} .= $_;
+ $parse_in = 0 if m/;/;
} elsif ( m/"(\d+)\s+\(BC_\d+,\s+(\S+),\s+(\S+)/ ) {
$bit2pin->{$1} = [ $2, $3 ];
push @{ $io2bit->{$2} }, $1;
+ } elsif ( m/attribute\s+BOUNDARY_LENGTH\s+of\s+\S+\s*:\s*entity\s+is\s+(\d+);/ ) {
+ $BOUNDARY_LENGTH = $1;
+ warn "# BSDL $bsdl_file BOUNDARY_LENGTH = $BOUNDARY_LENGTH\n";
+ } elsif ( ! $entity && m/^entity\s(\S+)\s+is/ ) {
+ $entity = $1;
+ warn "# entity = $entity\n";
}
+
}
close($bsdl);
warn "# bit2pin = ",dump($bit2pin);
warn "# io2bit = ",dump($io2bit);
-$pin_map =~ s/"\s*\&\s*"/ /gs;
-$pin_map =~ s/^.*?:=\s*"//;
-$pin_map =~ s/"\s*;\s*$//s;
+foreach my $chunk ( keys %$parse ) {
+ $parse->{$chunk} =~ s/"\s*\&\s*"/ /gs; # concat lines back to single line
+ $parse->{$chunk} =~ s/^.+?"//s; # strip upto first quote
+ $parse->{$chunk} =~ s/"\s*;.*?$//s; # strip everything after closing
+ $parse->{$chunk} =~ s/\s+/ /gs;
+}
+
+warn "# parse = ",dump($parse);
+
+my $pin_map = $parse->{PIN_MAP_STRING} || die "no PIN_MAP_STRING in ",dump($parse);
sub str_comma {
my $t = shift;
}
warn "# pin2io = ",dump( $pin2io );
+#store \$pin2io, $bsdl_file . '.pin2io.storable';
+store \$pin2io, $entity . '.pin2io.storable';
+
+
+my $opcode;
+my $o = $parse->{INSTRUCTION_OPCODE} || die "no INSTRUCTION_OPCODE in ",dump($parse);
+
+foreach ( split(/\)\s*,\s*/, $o) ) {
+ s/\s+\(/ /g;
+ s/\s*\)\s*$//g;
+ warn "### [$_]\n";
+ my ( $inst_opcode, $bin ) = split(/\s+/, $_);
+ $opcode->{$inst_opcode} = sprintf "0x%02x", eval '0b' . $bin;
+}
+warn "# opcode = ",dump($opcode);
+
+foreach (qw(SAMPLE EXTEST)) {
+ die "missing INSTRUCTION_OPCODE $_ in ",dump($opcode) unless $opcode->{$_};
+}
+
+my $pin_desc;
+foreach my $pin_file ( glob "pins/$entity*.tsv" ) {
+ open(my $fh, '<', $pin_file);
+ while(<$fh>) {
+ chomp;
+ my ($pin,$desc, $bcm) = split(/\t+/,$_,3);
+ $pin_desc->{$pin} = $desc;
+ gpio::add_bcm_desc( $bcm => $desc ) if $have_gpio;
+ }
+ close($fh);
+ warn "# $pin_file ",dump($pin_desc);
+}
+
+{
+ open(my $fh, '<', $project_file);
+ while(<$fh>) {
+ chomp;
+ s/[\r\n]+$//;
+ my ($set,$pin, $op, $desc) = split(/\s+/,$_,4);
+ if ( $set =~ m/set_location_assignment/i && $op =~ m/-to/i && $pin =~ s/^PIN_//ig ) {
+ $pin_desc->{$pin} = $desc; # overwrite pin description
+ } else {
+ warn "# ignored $_\n";
+ }
+ }
+ close($fh);
+ warn "# $project_file ",dump($pin_desc);
+}
+=for remove
my @sort = sort {
my $aa = $a; $aa =~ s/\D+//g;
my $bb = $b; $bb =~ s/\D+//g;
$aa <=> $bb } keys %$io2bit;
warn "# sort = ",dump(@sort);
+=cut
+sub bits {
+ my $bits = shift;
+ return $bits; # FIXME disable for now
+ $bits =~ s/0/ /;
+ $bits =~ s/^(.)0/$1 /;
+ $bits =~ s/^1/I/;
+ $bits =~ s/^(.)1/$1./;
+ return $bits;
+}
+
+my $last_bits = '';
sub print_io {
my $bits = shift;
- my $i = 0;
-# foreach my $io ( @sort ) {
- print "pin off io___ I C O | " x 4, "\n";
+ my $o_bits = $bits;
+ $bits = reverse $bits; # make substr work as expected
+ my $last_bits_r = reverse $last_bits;
+
+ print "pin off io___ ICO | " x 4, $openocd_remote,"\n";
foreach my $i ( 0 .. ($#$pin2io/4)-1 ) {
foreach my $j ( 0 .. 3 ) {
#my $pin = ($i*4) + $j + 1; # rows
my $pin = ($#$pin2io/4) * $j + $i + 1; # columns
my $io = $pin2io->[$pin];
my $o = $io2bit->{$io}->[0];
+
if ( defined $o ) {
- printf "%-3d %-3d %-5s %d %d %d | ", $pin, $o, $io, split(//, substr($bits, $o, 3));
+ my $l = $b = substr($bits, $o, 3);
+ $l = substr($last_bits_r,$o,3) if $last_bits;
+
+ my $desc = $pin_desc->{$pin} || '';
+ my $bits = bits($b);
+ if ( $b ne $l ) {
+ my @b = split(//, $bits);
+ my @l = split(//, bits($l));
+ $bits = '';
+ foreach ( 0 .. $#b ) {
+ $bits .= $b[$_] eq $l[$_] ? $b[$_] : "\e[33;7;1m$b[$_]\e[0m";
+ }
+ }
+
+ $desc = ' ' . $desc if length($desc) < 10;
+ $desc = substr($desc,0,10);
+
+ printf "%-3d %-3d %-5s %3s%-10s| ", $pin, $o, $io, $bits, $desc;
} else {
- printf "%-3d ... %-5s | ", $pin, $io;
+ printf "%-3d ... %-18s | ", $pin, $io;
}
}
print "\n";
}
print "\n";
+ $last_bits = $o_bits;
}
#foreach my $h (qw(0 1 2 e f 10 11 12 1e 1f 20 30 40 50 100 200 1000)) {
# printf "hex = %4s binary = %16s\n", $h, hex2bin($h);
#}
-my $sock = IO::Socket::INET->new('picam:4444');
+my $sock = IO::Socket::INET->new($openocd_remote);
sub hex2bin {
my $h = shift;
#my $cmd;
+
+
my $BSR;
# first sample
-print $sock "irscan x300.tap 0x55\n"; # SAMPLE/PRELOAD
-print $sock "drscan x300.tap 288 0\n";
+print $sock "irscan $tap $opcode->{SAMPLE}\n"; # SAMPLE/PRELOAD
+print $sock "drscan $tap $BOUNDARY_LENGTH 0\n";
while(1) {
if ( /^\s*([A-F0-9]+)/ ) {
my $hex = $1;
my $bin = hex2bin($hex);
+ diff_bits($BSR, $bin);
$BSR = $bin;
- print_io $bin;
+ print_io $bin if $bin ne $last_bits;
+ gpio::pins() if $have_gpio;
last;
} else {
warn "# in ",dump($_);
sub diff_bits {
my ($old, $new) = @_;
+ $old = reverse $old; # extra bits must be on the end
+ $new = reverse $new;
$old =~ s/(...)/$1 /g;
$new =~ s/(...)/$1 /g;
my @o = split(/ /, $old);
my @n = split(/ /, $new);
- foreach my $i ( 0 .. $#o ) {
+ my $bsr_max_bit = $BOUNDARY_LENGTH / 3 - 1;
+ foreach my $i ( 0 .. $bsr_max_bit ) {
if ( $o[$i] eq $n[$i] ) {
$o[$i]='.';
} else {
- $o[$i].='>'.$n[$i];
+ my $pin = $bit2pin->{ $i * 3 }->[0];
+ $o[$i] = " $pin@" . ( $i * 3 ) . ":" . $o[$i] . '>' . $n[$i];
}
}
- return join('', @o);
+ my $diff = join('', @o[ 0 .. $bsr_max_bit ]);
+ print "# diff_bits $diff\n";
}
-print "[press enter]\n";
+print "> ";
my $cmd = <STDIN>;
chomp $cmd;
-if ( $cmd =~ /(\d+)=(\d)/ ) {
- my ( $pin, $v ) = ( $1, $2 );
- warn "# pin $pin = $v\n";
+if ( $cmd =~ /(p|o)(\d+)=(\d+)/ ) {
+ my ( $what, $p, $v ) = ( $1, $2, $3 );
+
+ my $old_bsr = reverse $BSR;
+ my $b = $old_bsr;
+
+ if ( $what eq 'p' && length($v) == 1 ) {
+
+ my $pin = $p;
+ my $io = $pin2io->[$pin];
+ my $bit = $io2bit->{$io}->[0];
+
+ warn "# pin $pin = $v io=$io bit=$bit\n";
+
+ $b =
+ substr($old_bsr,0,$bit+1) # leave input as-is
+ . ( $v eq 'Z' ? 1 : 0 ) # control
+ . ( $v & 1 ) # output
+ . substr($old_bsr,$bit+3)
+ ;
+
+
+ } elsif ( $what eq 'o' && $p < $BOUNDARY_LENGTH ) {
+ my $o = $p;
+ #$b = substr($old_bsr,0,$o) . reverse $v . substr($old_bsr,$o+length($v));
+ $b = ( "0" x $o ) . reverse $v . ( "0" x ( length($old_bsr) - $o - length($v) ) );
+warn "XXX",length($old_bsr), " == ",length($b);
+ $old_bsr = "0" x length($old_bsr);
+ } else {
+ print "IGNORED ",dump( $what, $p, $v );
+ }
- my $io = $pin2io->[$pin];
- my $bit = $io2bit->{$io}->[0];
+ $b = reverse $b;
- my $b = substr($BSR,0,$bit+1)
- . ( substr($BSR,$bit+1,1) ^ 1 )
- . ( $v & 1 )
- . substr($BSR,$bit+3)
- ;
+ diff_bits( $BSR, $b );
- print diff_bits( $BSR, $b ), $/;
+# print $sock "irscan $tap $opcode->{SAMPLE}\n"; # SAMPLE/PRELOAD
+ print $sock "drscan $tap $BOUNDARY_LENGTH 0x", bin2hex($b), "\n";
+ print $sock "irscan $tap $opcode->{EXTEST}\n"; # EXTEST
+# print $sock "drscan $tap $BOUNDARY_LENGTH 0x", bin2hex($b), "\n";
- print $sock "irscan x300.tap 0x00\n"; # EXTEST
- print $sock "drscan x300.tap 288 0x", bin2hex($b), "\n";
+} elsif ( $cmd =~ m/\?/ ) {
+ $last_bits = '';
+ print $sock "irscan $tap $opcode->{SAMPLE}\n"; # SAMPLE/PRELOAD
+ print $sock "drscan $tap $BOUNDARY_LENGTH 0\n";
} else {
- print $sock "irscan x300.tap 0x55\n"; # SAMPLE/PRELOAD
- print $sock "drscan x300.tap 288 0\n";
+ gpio::cmd( $cmd ) if $have_gpio;
+ print $sock "irscan $tap $opcode->{SAMPLE}\n"; # SAMPLE/PRELOAD
+ print $sock "drscan $tap $BOUNDARY_LENGTH 0\n";
}