#!/bin/perl

open(FPR, $ARGV[0]) || die("open fail $ARGV[0]\n");
$o_name="n_" . $ARGV[0];
open(FPW, ">$o_name") || die("open fail $o_name\n");

$name_flg=1;
$sig_buf="";
$sig_offset=20; #first value starts here
$delta_flg=1;
$sig_prt_flg=1;
while(eof(FPR) != 1) {
  $line=<FPR>; chomp($line);
  if($name_flg == 1) {#get signal names
    if($line =~ /====/) {#separator
      $name_flg=0;
    }
    else {#this is a signal name
      $sig_buf=$line;
    }
  }
  else {#get time and signal value
    #2 ns +0 0 H H
    #10 ns +0 1 H H
    @a=split(/ /, $line);
    $ix=4;
    if($sig_prt_flg == 1) {
      $sig_prt_flg=0;
      @a_sig=split(/ /, $sig_buf);
      $sig_offset += length($a[3]); $sig_offset--;
      foreach $j ( @a_sig ) {
        $t=$j; while( length($t) < $sig_offset ) {$t=" " . $t;}
	print FPW ("$t\n");
	$sig_offset += 1;
	$sig_offset += length($a[$ix]);
	$ix++;
      }#foreach
    }
    $ix=0;
    if($delta_flg == 1) {
      print FPW ("          $a[1] delta\n");
      $delta_flg=0;
      $e_len=20; 
      foreach $j ( @a ) {if($ix > 2) {$e_len+=length($j);} $ix++;}
      for($i=0; $i <= $e_len; $i++) {print FPW ("=");} print FPW ("\n");
    }
    $ix=0;
    foreach $j ( @a ) {
      if($ix == 0) {#time
        #pad it
	$t=$j;
	while( length($t) < 12 ) {$t= " ". $t;}
	print FPW ("$t    +0");
      }
      elsif($ix == 1) {#time units
      }
      elsif($ix == 2) {#delta
      }
      else {#value
        print FPW (" $j");
      }
      $ix++;
    }#foreach
    print FPW ("\n");
  }
}
close(FPR);
close(FPW);
#~/bin/lst2vcd n_wave.lst n_wave.vcd
$cmd="/home/lxuser/bin/lst2vcd " . $o_name . " ";
$tmp=$o_name; $tmp =~ s/.lst//;
$cmd=$cmd . $tmp . ".vcd";
system($cmd);
$cmd="gzip " . $tmp . ".vcd";
system($cmd);
