#! /usr/bin/perl -w

############################################################
## Copyright: 2011 Integrated Sytems Laboratory, ETH Zurich
##            http://www.iis.ee.ethz.ch/~sha3
############################################################
use strict;

# v0.5 - kgf@ee.ethz.ch - Mon Sep 19 12:52:33 CEST 2011
#  - blake has saved another cycle + added a small note to
#    the HTML file about the most recent run.. 
# v0.4 - kgf@ee.ethz.ch - Fri Sep 16 12:50:35 CEST 2011
#  - changed the latency for blake
# v0.3 - kgf@ee.ethz.ch - Fri Sep  9 18:27:28 CEST 2011
#  - added a sort to the ls, so that we have numerical sort.. 
#    well sort of.. 
# v0.2 - kgf@ee.ethz.ch - Tue Sep  6 07:46:30 CEST 2011
#  - improvements 
# v0.1 - kgf@ee.ethz.ch - Mon Sep  5 08:42:52 CEST 2011
#  - read all the reports and print some summaries

my $GATE=1.44;
my $SCALE=1.05;   # scaling for the GNUPLOTS

my $cond = "wc";
my @TIMINGS=`ls reports/*_${cond}_timing.rep|sort -n -t_ -k3 `;
my %TIME;
my %AREA;
my %THROUGHPUT;
my $DEBUG= ($#ARGV > -1)? 1 : 0;
my %LATENCY = (
                  "ethz_sha2"    =>  67, 
                  "ethz_blake"   =>  57,
                  "ethz_groestl" =>  81, 
                  "ethz_jh"      =>  42, 
                  "ethz_keccak"  =>  24, 
                  "ethz_skein"   =>  92, 
                  "gmu_sha2"     =>  65, 
                  "gmu_blake"    =>  29, 
                  "gmu_groestl"  =>  21,
                  "gmu_jh"       =>  43,
                  "gmu_keccak"   =>  24,
                  "gmu_skein"    =>  19
              );
# where to generate the files
my $GNUPLOT="./reports/gnuplot";
my $PNG="../../results/atplot";
my $HTML="../../results/";

my %CONDITIONS = (
                     "wc" => "Worst case - 1.08V 125C",
                     "bc" => "Best case - 1.32V 0C",
                     "tc" => "Typical case - 1.20V 25C"
                 );

unless ($ENV{PWD}=~/(ethz|gmu)_(blake|skein|sha2|jh|keccak|groestl)\/synopsys$/){
   die "Current directory is [$ENV{PWD}] not in synopsys\n";
}

## get the last constraint that has been used
my $current_cons=0;
open (S, "< scripts/design_settings.tcl") or die "Can not open scripts/design_settings.tcl\n";
while(S){
next unless (/^\s*set CLOCKLIST\s*\{([^}]+)\}/);
 my @cons=split(' ',$1);
 $current_cons = "$cons[$#cons]\n";
}
close(S);



my $flavor= $1;
my  $alg = $2;

my $date=`date`;
chomp($date);

## create the html file
my $htmlfile=$HTML."${flavor}_${alg}_results.html";
open (H, "> $htmlfile") or die "Can not open HTML file $htmlfile\n";
print H <<"BUGU";
HTML
TITLESynthesis results for $alg ($flavor version)/TITLE
BODY
CENTER
H3
Synthesis results for
/H3
H1
$flavor $alg
/H1
ILast run for $current_cons ns clock constraint/IBR
IMG SRC="atplot_${flavor}_${alg}.png"
/CENTER
HR
BUGU

foreach my $tim (@TIMINGS){
 chomp ($tim);
 $tim=~/${flavor}_${alg}_(\d+(?:\.\d+)?)_(wc|tc|bc)_timing/;
 my $target= $1;
 $cond = $2;

 print H "H3Clock Constraint $target ns/H3\nBArea:/B\nUL\n";
 my $area=0;
 my $ffs=0;
 my $afile="./reports/${flavor}_${alg}_${target}_${cond}_area.rpt";
 open (A, "<$afile") or die "Can not open $afile for reading\n";
   while (A){
     next unless (/(Noncombinational|Total cell) area/);
     my @sp = split;
     if (/^Total/){
       $area = $sp[3];
     } 
     else         {
       $ffs  = $sp[2];
     }
     print if $DEBUG;
   }
   printf(H " LI Total area : %6.3f kGate equivalents\n", $area/ (1000*$GATE));
   printf(H " LI Total FF area : %6.3f  kGate equivalents (%4.1f%%)\n", $ffs/ (1000*$GATE), 100*($ffs/$area));

   $AREA{$target}=$area;
 close (A);

 print H "/UL\nBTiming:/B (I$CONDITIONS{$cond}/I)\nUL\n";
 
 open (T, "<$tim") or die "Can not open $tim for reading\n";
 my %R;
 my $type ="none";
 my $maxtime = 0;
   while (T){
       next unless (/(TIMING|slack)/);
       if (/^(IN2REG|IN2OUT|REG2REG|REG2OUT)/){
         $type = $1;
         printf(H "LI %s timing : ",lc($type));
       }
       else{
         my @sp=split;
         my $time= $target - ($sp[2]);

         printf(H "%6.4f ns\n",$time);

         $maxtime=$time if ($maxtime lt $time);
         $R{$type} = $time; 
       }
   }
   printf("%s(%s) - %s - crit.path %6.3f ns - area %6.3f kGE ( %4.1f%% FFs)\n",
           $alg,$flavor, $target, $maxtime, $area/(1000*$GATE), 100* ($ffs/$area));
   foreach my $r (keys %R){
     print " $r timing ($R{$r}) ns \n" if $DEBUG;
   }
#   print "max timing = $maxtime\n";
   $TIME{$target} = $maxtime;
   my $bpc = ($alg eq "keccak") ? 1088 : 512 ; # bits per block
   my $throughput = $bpc / ($maxtime * $LATENCY{"${flavor}_${alg}"});
   $THROUGHPUT{$target} = $throughput;

   printf(H "/UL\nBThroughput : %6.3f Gbps/B (I%d bits per %d cycles a %6.3f ns/I)BR\n", 
             $throughput, $bpc, $LATENCY{"${flavor}_${alg}"}, $maxtime);

   printf(H "/UL\nThroughput per area: %6.3f kbits/s/gate equivalentBRHR\n", 
             (1000*$throughput)/ ($area/(1000*$GATE)));
   
   print '-' x 75, "\n" if $DEBUG;
 close(T);
}
chomp($current_cons);
print "Last synthesis run for $current_cons ns\n";

# now let us generate a GNUPLOT picture
my $g1= $GNUPLOT."_${flavor}_${alg}_at.dat";
open (G, "> $g1") or die "can not open $g1\n";

# defaults so that the min max comparison works 
my $mina=99999.0;
my $mint=99.0;
my $maxa=0.0;
my $maxt=0.0;

foreach my $t (sort {$a =$b} keys %TIME){
 my $gates= $AREA{$t}/(1000*$GATE); # calculate in gates 
 printf(G "%f %6.3f %6.3f\n",$gates, $TIME{$t}, $THROUGHPUT{$t});
 $mina = $gates    if ($mina > $gates);
 $maxa = $gates    if ($maxa < $gates);
 $mint = $TIME{$t} if ($mint > $TIME{$t});
 $maxt = $TIME{$t} if ($maxt < $TIME{$t});
# print "$AREA{$t} $gates : $mina - $maxa , $TIME{$t} : $mint - $maxt\n" ;
}
close (G);

$mina = int($mina / $SCALE);
$maxa = int($maxa * $SCALE);
$mint = int($mint);
$maxt = int(($maxt+1) );


my $g2 = $GNUPLOT."_${flavor}_${alg}_at.gnuplot";
open (G, "> $g2") or die "can not open $g2\n";
print G<<"BUGU";
set title "$alg ($flavor) - $CONDITIONS{$cond} Synthesis Results - UMC65LL"
set ylabel "Area [kGate eq]"
set xlabel "Critical Path [ns]"
set grid
set terminal png
set out "${PNG}_${flavor}_${alg}.png"
plot [$mint:$maxt][$mina:$maxa] "${GNUPLOT}_${flavor}_${alg}_at.dat" using 2:1 with steps notitle lt 0 lw 2, "${GNUPLOT}_${flavor}_${alg}_at.dat" using 2:1 with points notitle lt 1 pointtype 3 pointsize 2
set terminal post eps color
set out "${PNG}_${flavor}_${alg}.eps"
replot
BUGU
close(G);

# execute gnuplot
my $result=`gnuplot $g2`;
#unlink ($g1,$g2);
print H<<"BUGU";
FONT SIZE=-2
CENTER
Shabziger ResultsBR
BGMU:/B
[A HREF="gmu_sha2_results.html"SHA2/A]
[A HREF="gmu_blake_results.html"Blake/A]
[A HREF="gmu_groestl_results.html"Groestl/A]
[A HREF="gmu_jh_results.html"JH/A]
[A HREF="gmu_keccak_results.html"Keccak/A]
[A HREF="gmu_skein_results.html"Skein/A]BR
BETHZ:/B
[A HREF="ethz_sha2_results.html"SHA2/A]
[A HREF="ethz_blake_results.html"Blake/A]
[A HREF="ethz_groestl_results.html"Groestl/A]
[A HREF="ethz_jh_results.html"JH/A]
[A HREF="ethz_keccak_results.html"Keccak/A]
[A HREF="ethz_skein_results.html"Skein/A]BR
/CENTER
BR
Generated automatically on $date by eval_syn_results.pl/FONT
/HTML
BUGU
close(H);

Generated on Tue Nov 22 15:16:34 CET 2011
Home