#!/bin/tcsh -f
#----------------------------------------------------------
# Place and Route script using TimberWolf and LithoRoute
#
# This script assumes the existence of the pre-TimberWolf
# ".cel" and ".par" files.  It will run TimberWolf for the
# placement, and beQroute for the detailed route.
#----------------------------------------------------------
# Tim Edwards, 5/16/11, for Open Circuit Design
#----------------------------------------------------------

set noplace=0
set retain=0
set tech=OSU035

set temp=(`getopt -s tcsh -o nrt: --long noplace,retain,tech: -- $argv:q`)

eval set argv=\($temp:q\)

while (1)
   switch($1:q)
      case -n:
      case --noplace:
	 set noplace=1
	 shift
	 breaksw
      case -r:
      case --retain:
	 set retain=1
	 shift
	 breaksw
      case -t:
      case --tech:
	 shift
	 set tech=$1
	 shift
	 breaksw
      case --:
	 shift
	 break
      endsw
end	

if ($#argv != 1) then
   echo "Usage:  place_and_route.sh [options] <cellname>"
   echo "Options: -n, --noplace		Don't run the placement"
   echo "         -r, --retain		Keep the primary files used"
   echo "         -t, --tech <name>	Use technology <name>"
   exit 1
endif

#----------------------------------------------------------
# Find out where we are, and where the files are
#----------------------------------------------------------

set curdir=`pwd`
set layoutdir=$1:h
set layoutname=$1:t
set topdir="."

if ( $layoutdir == $layoutname ) then
   set fullpath=`find . -name ${rootname}.cel -print`
   set found=`echo $fullpath | wc -w`
   set layoutdir=${fullpath[1]:h}
   set topdir="${layoutdir}/.."
endif

if ( -d ${curdir}/scripts ) then
   set scriptdir=${curdir}/scripts
else
   if ( -d ${topdir}/scripts ) then
      set scriptdir=${topdir}/scripts
   else
      set scriptdir=${curdir}
   endif
endif

if ( -d ${curdir}/lib ) then
   set libdir=${curdir}/lib
else
   if ( -d ${topdir}/lib ) then
      set libdir=${topdir}/lib
   else
      set libdir=${curdir}
   endif
endif

switch ($tech)
   case OSU035:
      set lef1name=${libdir}/osu035_stdcells.lef
      set lef2name=""
      breaksw
   case XH035:
      set lef1name=${libdir}/xh035i_m3.lef
      set lef2name=${libdir}/D_CELLS.lef
      breaksw
   default:
      echo "Unknown technology ${tech}; use OSU035 or XH035"
      breaksw
endsw

pushd ${layoutdir}

#----------------------------------------------------------
# Done with initialization
#----------------------------------------------------------

set rootname=$layoutname:r

# Check if rootname needs a "_buf" suffix

if ( ! -f ${rootname}.cel && -f ${rootname}_buf.cel ) then
   set rootname=${rootname}_buf
endif

# Check if a .cel2 file exists and needs to be appended to .cel
# If the .cel2 file is newer than .cel, then truncate .cel and
# re-append.

if ( -f ${rootname}.cel2 ) then
   if ( `grep -c padgroup ${rootname}.cel` == "0" ) then
      cat ${rootname}.cel2 >> ${rootname}.cel
   else if ( -M ${rootname}.cel2 > -M ${rootname}.cel ) then
      # Truncate .cel file to first line containing "padgroup"
      cat ${rootname}.cel | sed -e "/padgroup/Q" > ${rootname}_tmp.cel
      cat ${rootname}_tmp.cel ${rootname}.cel2 > ${rootname}.cel
      rm -f ${rootname}_tmp.cel
   endif
endif

#-----------------------------------------------
# 1) Run TimberWolf
#-----------------------------------------------

if ($noplace == 0) then

( pushd /pub/src/timberwolf-6.3 ;\
  source .twrc ;\
  popd ;\
  TimberWolf $rootname )

endif

#------------------------------------------------
# 3) Prepare DEF file for qrouter
#------------------------------------------------

${scriptdir}/place2def2.tcl $rootname $lef1name $lef2name

#-----------------------------------------------
# 4) Create the detailed route.  Do this in a
# subshell so we don't mess up the environment
# for Magic.
#-----------------------------------------------

qrouter -c ${rootname}.cfg ${rootname}

# Not ready to continue yet. . .
# exit

set origname=${rootname:s/_buf//}
mv ${rootname}_route.def ${origname}.def

#---------------------------------------------------
# 5) Add spacer cells to create a straight border on
#    the right side
#---------------------------------------------------

# ${scriptdir}/addspacers.tcl ${origname} -p

#---------------------------------------------------
# 5) Create magic layout (.mag file) using the
#    technology LEF file to determine route widths
#    and other parameters.
#---------------------------------------------------

if ($lef2name == "") then
   set lefcmd="lef read ${lef1name}"
else
   set lefcmd="lef read ${lef1name}\nlef read ${lef2name}"
endif

/home/tim/cad/bin/magic -dnull -noconsole <<EOF
drc off
box 0 0 0 0
snap int
${lefcmd}
def read ${origname}
select top cell
select area labels
setlabel font FreeSans
setlabel size 0.3um
box grow s -[box height]
box grow s 100
select area labels
setlabel rotate 90
setlabel just e
select top cell
box height 100
select area labels
setlabel rotate 270
setlabel just w
select top cell
box width 100
select area labels
setlabel just w
select top cell
box grow w -[box width]
box grow w 100
select area labels
setlabel just e
save ${origname}
quit -noprompt
EOF

#---------------------------------------------------
# 5) Generate GDS from the magic file.  This time,
#    we do not read the LEF file, so that we pick
#    up the vendor GDS database for the standard
#    cells.
#
#    Use magic version 8.0 to make nice labels
#---------------------------------------------------

# /home/tim/cad/bin/magic -dnull -noconsole <<EOF
# drc off
# box 0 0 0 0
# snap int
# read ${origname}
# gds write ${origname}
# quit -noprompt
# EOF

#----------------------------------------------------------
# 6) Cleanup the (excessively numerous) Timberwolf files
#----------------------------------------------------------

rm -f ${rootname}.blk ${rootname}.gen ${rootname}.gsav ${rootname}.history
rm -f ${rootname}.log ${rootname}.mcel ${rootname}.mdat ${rootname}.mgeo
rm -f ${rootname}.mout ${rootname}.mpin ${rootname}.mpth ${rootname}.msav
rm -f ${rootname}.mver ${rootname}.mvio ${rootname}.stat ${rootname}.out
rm -f ${rootname}.pth ${rootname}.sav ${rootname}.scel
rm -f ${rootname}.txt ${rootname}.info

# Keep the files that we make use of, at least for now. . .
# rm -f ${rootname}.pin ${rootname}.pl1 ${rootname}.pl2
# rm -f ${rootname}.cfg

# If the "retain" option is set, keep both the unrouted and routed DEF files
# with their original filenames.

if ($retain != 1) then
   rm -f ${rootname}_route.def
endif

#------------------------------------------------------------
# Done!
#------------------------------------------------------------

popd

