|
楼主 |
发表于 2012-4-3 17:27:31
|
显示全部楼层
#!/usr/bin/perl
if(@ARGV == 0)
{
printf ("********************************\n");
printf ("* Usage: ./vcd2vec.pl VCD_NAME *\n");
printf ("********************************\n");
exit;
}
$vcd_name = $ARGV[0];
open(VCD, $vcd_name);
$vec_name = $vcd_name.".vec";
open(VEC, ">$vec_name");
my %signal_width;
my %signal_fname;
my %signal_value;
my @sarray;
my @array;
my @sub_array;
my $item;
my $sub_item;
my $line;
my $full_name;
my $dump_beg;
my $unit = 1;
my $ts_en;
while($line = <VCD>)
{
if($line =~ /^\$timescale$/)
{
$ts_en = 1;
}
if($line =~ /^\$end$/)
{
$ts_en = 0;
}
if($ts_en == 1 && $line =~ /^\s*(\d+)ps$/)
{
$unit = 1000/$1;
}
if($line =~ /^\$scope\s+(\S+)\s+(\S+)\s+\$end\s*$/)
{
push(@array, $2);
#printf("@array\n");
}
if($line =~ /^\$upscope\s+\$end\s*$/)
{
pop(@array);
#printf("@array\n");
}
if($line =~ /^^\$var\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+\$end$/)
{
$full_name = &array2hir(@array)."$4";
#printf ("$full_name\n");
$signal_fname{$3} = $full_name;
$signal_width{$3} = $2;
}
if($line =~ /^\$dumpvars$/)
{
$dump_beg = 1;
@sarray = &Sig2Arr(%signal_width);
&prt_hdr_vec(VEC, %signal_fname);
$str = "0 ";
&myprt(VEC, $str, 16);
}
if($dump_beg==1)
{
if ($line =~ /^#(\d+)$/)
{
foreach $item (@sarray)
{
if(!exists($signal_value{$item}))
{
printf ("Signal names Wrong\n");
exit;
}
if($signal_width{$item} == 1)
{
printf VEC (" $signal_value{$item}");
}else
{
@sub_array = split("", $signal_value{$item});
foreach $sub_item (@sub_array)
{
printf VEC (" $sub_item");
}
}
}
printf VEC ("\n");
$str = $1/$unit." " ;
&myprt(VEC, $str, 16);
}
if ($line =~ /^([01xz])(\S+)$/)
{
$signal_value{$2} = $1;
}
if ($line =~ /^b(\S+)\s+(\S+)$/)
{
$signal_value{$2} = $1
}
}
}
foreach $item (@sarray)
{
if(!exists($signal_value{$item}))
{
printf ("Signal names Wrong\n");
exit;
}
if($signal_width{$item} == 1)
{
printf VEC (" $signal_value{$item}");
}else
{
@sub_array = split("", $signal_value{$item});
foreach $sub_item (@sub_array)
{
printf VEC (" $sub_item");
}
}
}
close(VCD);
close(VEC);
printf ("Output File: $vec_name\n");
sub array2hir
{
my @array = @_;
my $hir;
my $len = @array;
for $i(0..$len-1)
{
$hir .= "$array[$i]/";
}
return $hir;
}
sub get_name
{
my $src_name = @_;
my @array = split(".", $src_name);
my $len = @array;
my $name;
foreach my $i (0 .. $len-2)
{
$name .= "$array[$i]";
}
return $name;
}
sub myprt
{
my ($FHANDLE, $str, $len) = @_;
$len = $len-1-length($str);
foreach $i (0..$len)
{
printf $FHANDLE (" ");
}
printf $FHANDLE ("$str");
}
sub Sig2Arr
{
my (%signal_width) = @_;
my @sarray;
foreach my $item (keys(%signal_width))
{
push(@sarray, $item);
}
return @sarray;
}
sub prt_hdr_vec
{
my ($FHANDLE, %signal_fname) = @_;
printf $FHANDLE ("logichv 1\n");
printf $FHANDLE ("logichv 0\n");
my $str;
my $len;
$str = "signal ";
printf $FHANDLE ("$str");
foreach $item(keys(%signal_fname))
{
$str = $signal_fname{$item};
$len = length($str) + 1;
&myprt($FHANDLE, $str, $len);
}
$str = "\nradix ";
printf $FHANDLE ("$str");
foreach $item(keys(%signal_fname))
{
$str = $signal_fname{$item};
$len = length($str) + 1;
$str = "1";
&myprt($FHANDLE, $str, $len);
}
$str = "\nio ";
printf $FHANDLE ("$str");
foreach $item(keys(%signal_fname))
{
$str = $signal_fname{$item};
$len = length($str) + 1;
$str = "b";
&myprt($FHANDLE, $str, $len);
}
printf $FHANDLE ("\n");
} |
|