mknet.pl 2.61 KB
#!/usr/bin/perl

exit(0) if ($#ARGV != 0);
$dir = "Wir";
$dir = ".";

$name=$ARGV[0];
$verilog = "$dir/" . $name . ".v";
open V, $verilog or die "can't open verilog netlist: $verilog $!\n";

$nnet = 0;
$nsym = 0;
$ncomp = 0;

# read the verilog file
undef $/;
$file=<V>;
$/="\n";
# strip comments
$file =~ s|//.*\n|\n|g;
# strip newlines
$file =~ s/\n/ /g;
# split at ; separators
@file = split ';', $file;

# parse file
$inmodule=0;
for (@file) {
    split;
#print "$_[0]\n";
    if ($_[0] eq "module") {
    	$modname = $_[1];
	$inmodule = 1;
#print "module xx$modname\n";
	next;
    }
    $inmodule = 0 if ($_[0] eq "endmodule");

    if ($inmodule == 1 && $_[0] eq "wire") {
	if ($#_ == 2) {
	    $nname[$nnet] = $_[2];
	    $nwidth[$nnet] = $_[1];
	} else {
	    $nname[$nnet] = $_[1];
	}
	$nnet++;
	next;
    }
    if ($inmodule == 1) {
    	$compname[$ncomp] = $_[0];
	$instname[$ncomp] = $_[1];
	shift @_; shift @_;
	$sig[$ncomp] = join '',@_;
#	print "$sig[$ncomp]\n";
	$ncomp++;
    }
}

# read symbols
for($i = 0; $i < $ncomp; $i++) {
    $cname = $compname[$i];
    $sname = "$dir/" . $cname . ".pin";
    open S, $sname or die "can't open $sname: $!\n";
    $n=0;
    $sym{$cname} = [ () ];
    while(<S>) {
        split;
	$pin = $_[0];
	$type = $_[1];
	if ($pin =~ /(\w+)(\[.*\])/) {
	    $pin = $1;
	    $width = $2;
	} else {
	    $width = "";
	}
#print "$pin -- $width -- $type\n";
	push @{$sym{$cname}}, [($pin , $width, $type)];
    }
#    print "$cname\n";
#    for $x (@{$sym{$cname}}) {
#    	print "\t[ @$x ]\n";
#    }
#    print "@{$sym{$cname}} \n";
    close S;
}


print "DW $ARGV[0]\n";

# symbols
for($i = 0; $i < $ncomp; $i++) {
    print "AS $compname[$i] xx ";
    for $x (@{$sym{$compname[$i]}}) {
	($pin, $width, $type) = @$x;
	printf "%s ",$pin.$width;
    }
    print "\n";
    $j = 1;
    for $x (@{$sym{$compname[$i]}}) {
	($pin, $width, $type) = @$x;
	printf "AP %-10s %3d NAME    %s\n",$compname[$i],$j,$pin.$width;
	printf "AP %-10s %3d PINTYPE %s\n",$compname[$i],$j,$type;
	$j++;
    }
}

# nets
for($i = 0; $i < $nnet; $i++) {
    print "G $nname[$i]\n";
}

# instances
for($i = 0; $i < $ncomp; $i++) {
    printf "I %s %s  ", $instname[$i], $compname[$i];
    $x = $sig[$i];
    # remove outer ()
    $x =~ s/\((.*)\)/\1/;
    @x = split ',',$x;
    %px = ();
    for $s (@x) {
        $s =~ /\.([\w\[\]:]+)\(([\w\[\]:]+)?\)/;
	$p = $1;
	$n = $2;
	$n = "?" unless defined $n;
#	if ($n =~ /\w+(\[.*\])/) {
#	    $p = $p . $1;
#	}
	$px{$p} = $n;
#	print "$p, $n\n";
    }
    for $y (@{$sym{$compname[$i]}}) {
	($pin, $width, $type) = @$y;
	$n = $px{$pin};
	$n = "?" unless defined $n;
	print "$n ";
    }
    print "\n\n";
}