#!/usr/bin/bash

EXT2SPICE="/import/cad1/bin/ext2spice"

# default simulation temperature:
temp="20"

NO="\x1b[0;0m"
RD="\x1b[31;01m"
GR="\x1b[32;01m"

show_usage() {
	echo "This should be ran from the magic directory."
	echo
	echo "Usage:"
	echo
	echo "Force the top level name:"
	echo " `basename ${0}` -f <top level name>"
	echo
	echo "Keep the current extraction:"
	echo " `basename ${0}` -k"
	echo
	echo "Change operation temperature (default is ${temp}):"
	echo " `basename ${0}` -t <temperature in celcius>"
}

do_bad() {
	echo -e "${RD}!!!${NO}"
}

do_good() {
	if [ "${1}" ]; then
		echo -e "${GR}${1}${NO}"
	else
		echo -e "${GR}done${NO}"
	fi
}

working_on() {
	if [ "${1}" ]; then
		echo -n "${1}: "
	fi
}

while [ ${#} -gt 0 ]; do
	a=${1}
	shift

	case "${a}" in

	-f)
		toplevel="${1}"
		;;
	-k)
		keep=y
		;;
	-t)
		temp="${1}"
		;;
	-h)
		show_usage
		exit 0
		;;
	esac
done

working_on "Top level name   "

if [ -z "${toplevel}" ]; then
	# see if we can figure out the top level name
	if [ -f ../seultra.scr ]; then
	    toplevel=`grep "DESIGN" ../seultra.scr | grep hdl | awk -F\" '{print $2}' | awk -F. '{print $2}' | awk -F: '{print $1}'`
	else
		do_bad
		echo
		echo "Could not determine top level name."
		show_usage
		exit 127;
	fi
fi

if [ -f ${toplevel}.mag ]; then
	do_good "${toplevel}"
else
	do_bad
	echo
	echo "Top level file (${toplevel}.mag) is missing!"
	echo
	exit 127
fi

working_on "Technology       "

leffile=`grep "INPUT LEF" ../seultra.scr | awk '-F"' '{print $2;}' | awk -F/ '{print $NF}'`
if [ $leffile = "osu025_stdcells.lef" ]; then
        do_good "TSMC 0.25um"
        techfile="-T SCN5M_SUBM.15"
	models="/import/cad2/osu_stdcells/devel/models/tsmc025.spice"
        ampl="2.5"
        tsmc25=y
elif [ $leffile =  "osu018_stdcells.lef" ]; then
        do_good "TSMC 0.18um"
        techfile="-T SCN6M_SUBM.10"
	models="/import/cad2/osu_stdcells/devel/models/tsmc018.spice"
        ampl="1.8"
        tsmc18=y
elif [ $leffile = "osu05_stdcells.lef" ]; then
	do_good "AMI 0.5um"
	models="/import/cad2/osu_stdcells/devel/models/ami06.spice"
	techfile="-T SCN3ME_SUBM.30"
	ampl="5"
	ami06=y
elif [ $leffile = "osu05_stdcells.stacks.lef" ]; then
	do_good "AMI 0.5um"
	models="/import/cad2/osu_stdcells/devel/models/ami06.spice"
	techfile="-T SCN3ME_SUBM.30"
	ampl="5"
	ami06=y
else
	do_bad
	echo
        echo "Unknown Technology."
	echo
	echo "Are you running `basename ${0}` from the magic directory?"
        exit 127
fi

working_on "Extracting Layout"

# generate a magic command file
cat << EOF > .magic
:load ${toplevel}
:drc off
:extract all
:quit
yes
EOF

if [ -z "${keep}" ]; then
	# remove *.ext to make sure the extraction is clean
	rm -f *.ext

	# extract the magic layout
	magic ${techfile} -dNULL &> magic.log
else
	# try to keep the current extraction
	if [ ! -f "${toplevel}.ext" ]; then
		echo -e "${toplevel}.ext missing.  Extraction forced. "

		# remove *.ext to make sure the extraction is clean
		rm -f *.ext

		# extract the magic layout
		magic ${techfile} -dNULL &> magic.log

		# force the .spice file to be regenerated
		rm -f ${toplevel}.spice
	fi
fi

rm -f .magic

# better error checking could possibly be done
if [ ! -f "${toplevel}.ext" ]; then
	do_bad
	echo
	echo "Extraction failed.  See magic.log for details."
	exit 127
else
	do_good "ok"
fi

working_on "Creating netlist "

if [ -z "${keep}" ]; then
	${EXT2SPICE} -F -y 6 -f spice3 ${toplevel}.ext &> /dev/null
else
	if [ ! -f ${toplevel}.spice ]; then
		echo -e "${toplevel}.spice missing.  ext2spice forced. "
		${EXT2SPICE} -F -y 6 -f spice3 ${toplevel}.ext &> /dev/null
	fi
fi

if [ ! -f ${toplevel}.spice ]; then
	do_bad
	echo
	echo "ext2spice failed."
	exit 127
fi

[ -d pathmill ] || mkdir pathmill

# put together the hspice netlist
if [ -f ${models} ]; then
	cat ${toplevel}.spice ${models} > pathmill/${toplevel}.sp
else
	do_bad
	echo
	echo "The transistor models are missing!  Please report this problem."
	exit 127
fi

cat << EOF >> pathmill/${toplevel}.sp
.temp ${temp}
.end
EOF

# create a powermill config file
cat << EOF > pathmill/pathmill.conf
; PathMill simulation configuration options
;
; To run PathMill: pathmill -n myNetlist.sp -c pathmill.conf

;*******************
; define gnd and vdd nodes
set_gnd gnd
set_vdd vdd
set_voltage ${ampl}

;*******************
; set input pins
source_node A B Cin

;*******************
; set output pins
sink_node Sum Cout

;*******************
; Report critical path
; and create a spice file
; to analyze it
pwl *
report_paths critical max 1
print_spice_paths critical max 1 spice_type=hspice spice_model_file=${models}
EOF

do_good "ok"
