#!/usr/bin/perl
# Nelson Hu
# EE464H
# 08/07/03

#######################################################
# This program merges the timing table from Hspice into a matrix
# easily implement into Library compiler

#######################################################
# How to use this program:
# at the folder where "%CELLNAME%.mt0" ~ "%CELLNAME%.mt4" is found 
# type
#     perl /home/projects/abraham/vlsi464/spicetest/FinalTOOLS/fallrisetable.pl %CELLNAME%.mtX"

#######################################################
# Input:
#  %CELLNAME%.mt0, %CELLNAME%.mt1, %CELLNAME%.mt2, %CELLNAME%.mt3, or %CELLNAME%.mt4
#  depending on how many input there is for the cell

#######################################################
# Output:
#  each time the program is run, four output will be generated:
#   %CELLNAME%_rise_slew.mtX
#   %CELLNAME%_cell_rise.mtX
#   %CELLNAME%_fall_slew.mtX
#   %CELLNAME%_cell_fall.mtX
#  each with a matrix in it.

#######################################################
# get filename from prompt
#######################################################

$mtname=$ARGV[0];
die "no such file: $mtname /n" if !(<>);

@line = <>;
foreach (@line){
    if ($_ =~ /failed/){
	open LALA, ">>faildata";
        print LALA "$mtname has failed\n";
        die "$mtname has failed values\n";}}

#######################################################
# setup the four matrix table as array
#######################################################
$rise_slew = "";
$cell_rise = "";
$fall_slew = "";
$cell_fall = "";
foreach (@line){
	$_ =~ s/     / /g;
	$_ =~ s/    / /g;
	$_ =~ s/   / /g;
	$_ =~ s/  / /g;
	if ($_ =~ /.TITLE/){}
	elsif ($_ =~ /SOURCE='HSPICE'/) {}
	elsif ($_ =~ /index/) {}
	elsif ($_ =~ /27.0000 1.0000/){}
	elsif ($_ =~ /27.0000 2.0000/){}
	elsif ($_ =~ /27.0000 3.0000/){}
	elsif ($_ =~ /27.0000 4.0000/){}
	elsif ($_ =~ /27.0000 5.0000/){}
        elsif ($_ =~ /1.0000 \n/){}
        elsif ($_ =~ /2.0000 \n/){}
        elsif ($_ =~ /3.0000 \n/){}
        elsif ($_ =~ /4.0000 \n/){}
        elsif ($_ =~ /5.0000 \n/){}
	else {
		@temp2 = split / /,$_;
		$time .= "$temp2[2] ";
		$capa .= "$temp2[3] ";
		$rise_slew .= "$temp2[5] ";
		$cell_rise .= "$temp2[4] ";
		$fall_slew .= "$temp2[7] ";
		$cell_fall .= "$temp2[6] ";}}
#######################################################
# merge the array into matrix
#######################################################
$filename =~ s/\*//g;
$filename =~ s/.sp//g;
#####################################
@time = split / /,$time;
@list_time;
$c_count = 0;
foreach (@time){
	if ($time[0] eq $_){
		$c_count++;}
	if ($list_time[$#list_time] ne $_){
		$list_time[$#list_time+1] = $_;}}
foreach (@list_time){
	$_ .= "\t";}
#####################################
@capa = split / /,$capa;
@list_capa;
$t_count = 0;
foreach (@capa){
	if ($capa[0] eq $_){
		$t_count++;}}
for (1..$c_count){
	push (@list_capa,$capa[$_-1]);}
foreach (@list_capa){
	$_ .= "\t";}
#####################################
@rise_slew = split / /,$rise_slew;
@rise1=@rise_slew;
foreach (@rise_slew){
        $_ *= 1e12;}
for (1..$t_count){
	$k = ($c_count * $_)-1;
	$rise_slew[$k] .= "\n";}
$rise_slew = join ",\t",@rise_slew;
$rise_slew =~ s/\n,\t/,\n/g;
#####################################
@cell_rise = split / /,$cell_rise;
@rise2 = @cell_rise;
foreach (@cell_rise){
        $_ *= 1e12;}
for (1..$t_count){
	$k = ($c_count * $_)-1;
	$cell_rise[$k] .= "\n";}
$cell_rise = join ",\t",@cell_rise;
$cell_rise =~ s/\n,\t/,\n/g;
#####################################
@fall_slew = split / /,$fall_slew;
@fall1=@fall_slew;
foreach (@fall_slew){
        $_ *= 1e12;}
for (1..$t_count){
	$k = ($c_count * $_)-1;
	$fall_slew[$k] .= "\n";}
$fall_slew = join ",\t",@fall_slew;
$fall_slew =~ s/\n,\t/,\n/g;
#####################################
@cell_fall = split / /,$cell_fall;
@fall2=@cell_fall;
foreach (@cell_fall){
        $_ *= 1e12;}
for (1..$t_count){
	$k = ($c_count * $_)-1;
	$cell_fall[$k] .= "\n";}
$cell_fall = join ",\t",@cell_fall;
$cell_fall =~ s/\n,\t/,\n/g;
#####################################
$p=0;
foreach (@rise1){
        $rise[$p]=$rise1[$p]+$rise2[$p];
        $p++;}
$p=0;
foreach (@fall1){
        $fall[$p]=$fall1[$p]+$fall2[$p];
        $p++;}

foreach (@rise){
        $_ *=1e12;}
for (1..$t_count){
        $k = ($c_count * $_)-1;
        $rise[$k] .= "\n";}
$rise = join ",\t", @rise;
$rise =~ s/\n,\t/,\n/g;

foreach (@fall){
        $_ *=1e12;}
for (1..$t_count){
        $k = ($c_count * $_)-1;
        $fall[$k] .= "\n";}
$fall = join ",\t", @fall;
$fall =~ s/\n,\t/,\n/g;

#####################################
@name = qw ! rise_slew cell_rise fall_slew cell_fall rise fall!;
foreach (@name) {
	$tempfilename = "$mtname"."_"."$_";
	open FILE, ">$tempfilename";
	print FILE "$$_"."\n\ntime:\n"."@list_time"."\ncapacitors:\n"."@list_capa";
	close FILE;}







