#!/usr/bin/perl
# Nelson Hu
# EE464H
# July 10, 2003
#

#######################################################
# This program "librarymaker.pl" takes the list in librarylist.nh
# and matrix in each cell and make into generatedlibrary.lib to be compiled
# by lc_shell into PRIMETIME library

#######################################################
# how to use this program:
# at a place where you want the library to be at, type
#    perl /home/projects/abraham/vlsi464/spicetest/FinalTOOLS/librarymaker.pl /home/projects/abraham/vlsi464/spicetest/FinalTOOLS/librarylist.nh

#######################################################
# Input:
#  librarylist.nh

#######################################################
# Output:
#  generatedlibrary.lib

#######################################################
# open a file to be written
#######################################################
open LIB, ">generatedlibrary.lib";

#########################################################
# @data contains all the information of the specific cell
#########################################################

@data = <>;
shift(@data);

foreach (@data){
    $_ =~ s/ +/ /g;
    @cell = split /[\t ]+/,$_;
    shift(@cell);
    print "now working on $cell[0]\n";
    $code = "cell ( $cell[0] ) {\n";
    $code .= "\tarea : $cell[1];\n";
    $letter="\x41";
    $m=4;
    for (1..$cell[2]){
	$code .= "\tpin ( $letter ) {\n";
	$letter++;
	$code .= "\t\tdirection : input;\n";
	$code .= "\t\tcapacitance : $cell[$m];\n";
	$m++;
	$code .= "\t\t}\n";}
    $code .= "\tpin ( Z ) {\n";
    $code .= "\t\tdirection : output;\n";
    $code .= "\t\tfunction : \" $cell[3] \";\n\n";
    $letter="\x41";
    $mt = "0";
    for (1..$cell[2]){
	$code .= "\t\ttiming ($letter\_Z) {\n\n";
#############################cell_rise
	$code .= "\t\t\tcell_rise(cell_template){\n";
	open TIME, "/home/projects/abraham/vlsi464/spicetest/$cell[0]/$cell[0].mt$mt\_cell_rise";
	@timing= <TIME>;
	for (1..6){
	    pop @timing;}
	foreach (@timing){
	    chomp ($_);}
	for (0..5){
	    chop ($timing[$_]);}
	$code .= "\t\t\t\tvalues (\"$timing[0]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[1]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[2]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[3]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[4]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[5]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[6]\");\n";
	$code .= "\t\t\t\t}\n";
	close TIME;
#############################cell_fall
	$code .= "\t\t\tcell_fall(cell_template){\n";
	open TIME, "/home/projects/abraham/vlsi464/spicetest/$cell[0]/$cell[0].mt$mt\_cell_fall";
	@timing= <TIME>;
	for (1..6){
	    pop @timing;}
	foreach (@timing){
	    chomp ($_);}
	for (0..5){
	    chop ($timing[$_]);}
	$code .= "\t\t\t\tvalues (\"$timing[0]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[1]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[2]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[3]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[4]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[5]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[6]\");\n";
	$code .= "\t\t\t\t}\n";
	close TIME;
#############################rise_transition
	$code .= "\t\t\trise_transition(cell_template){\n";
	open TIME, "/home/projects/abraham/vlsi464/spicetest/$cell[0]/$cell[0].mt$mt\_rise_slew";
	@timing= <TIME>;
	for (1..6){
	    pop @timing;}
	foreach (@timing){
	    chomp ($_);}
	for (0..5){
	    chop ($timing[$_]);}
	$code .= "\t\t\t\tvalues (\"$timing[0]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[1]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[2]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[3]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[4]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[5]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[6]\");\n";
	$code .= "\t\t\t\t}\n";
	close TIME;
#############################fall_transition
	$code .= "\t\t\tfall_transition(cell_template){\n";
	open TIME, "/home/projects/abraham/vlsi464/spicetest/$cell[0]/$cell[0].mt$mt\_fall_slew";
	@timing= <TIME>;
	for (1..6){
	    pop @timing;}
	foreach (@timing){
	    chomp ($_);}
	for (0..5){
	    chop ($timing[$_]);}
	$code .= "\t\t\t\tvalues (\"$timing[0]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[1]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[2]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[3]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[4]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[5]\",\\\n";
	$code .= "\t\t\t\t\t\"$timing[6]\");\n";
	$code .= "\t\t\t\t}\n";
	close TIME;
#############################
	$code .= "\t\t\trelated_pin : \" $letter \";\n";
	$code .= "\t\t\t}\n";
	$letter++;
	$mt++;}
    $code .= "\t\t}\n";
    $code .= "\t}\n";
    $code .= "\n\n\n";
    print LIB $code;
}

