#!/bin/sh
# Fires up magic to stream in gds and save as .mag
# also copies standard cells into directory
#
# Johannes Grad, OSU

# Customize this script here:

osucells_dir=$OSUcells/lib

##########################################################

sourceFile=final.gds2
/bin/echo "Checking if $sourceFile exists........\c"
if (test ! -f $sourceFile ) then
    echo "FAILED"
    echo $sourceFile does not exist!
    echo Please run Encounter first
    exit 127
fi
echo "OK"

sourceFile=encounter.conf
/bin/echo "Checking if $sourceFile exists....\c"
if (test ! -f $sourceFile ) then
    echo "FAILED"
    echo $sourceFile does not exist!
    echo Sorry, file is necessary to determine top-level name 
    exit 127
fi
echo "OK"

/bin/echo "Determining top-level name...........\c"
toplevel=`grep "set my_toplevel" encounter.conf | awk '{print $3;}'`
echo "OK ($toplevel)"

/bin/echo "Determining Technology...............\c"
leffile=`grep "leffile" encounter.conf | awk '-F"' '{print $2;}' | awk -F/ '{print $NF}'`
if (test $leffile = "osu025_stdcells.lef") then
    echo "OK (TSMC 0.25um)"
    techfile=SCN5M_SUBM.15
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="noPads"
    sourceDirSue="$osucells_dir/tsmc025/sue"
elif (test $leffile = "osu018_stdcells.lef") then
    echo "OK (TSMC 0.18um)"
    techfile=SCN6M_SUBM.10
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="noPads"
    sourceDirSue="$osucells_dir/tsmc018/sue"
elif (test $leffile = "osu035_stdcells.lef") then
    echo "OK (AMI 0.35um)"
    techfile=SCN4M_SUBM.20.TSMC
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="$osucells_dir/source/pads_ami035"
    sourceDirSue="$osucells_dir/ami035/sue"
elif (test $leffile = "osu05_stdcells.lef") then
    echo "OK (AMI 0.5um)"
    techfile=SCN3ME_SUBM.30
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="$osucells_dir/source/pads_ami05"
    sourceDirSue="$osucells_dir/ami05/sue"
elif (test $leffile = "osu05_stdcells.stacks.lef") then
    echo "OK (AMI 0.5um stacks)"
    techfile=SCN3ME_SUBM.30
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="$osucells_dir/source/pads_ami05"
    sourceDirSue="$osucells_dir/ami05/sue"
else
    echo "FAILED"
    echo "Unknown .lef file used for P&R. Sorry."
fi

/bin/rm -rf magic
mkdir magic
cp final.gds2  magic/final.strm 
cd magic

/bin/echo "Importing standard cells.............\c"
/bin/cp $sourceDirMagic/*.mag .
echo "OK"

if (test $sourceDirPads != "noPads") then 
  /bin/echo "Importing Pads.......................\c"
  /bin/cp $sourceDirPads/*.mag .
  echo "OK"
fi

/bin/echo "Converting from gds to mag...........\c"
(
ls -1 *.mag | awk -F. '{printf ":load %s\n",$1}';
echo :calma read final; 
echo :writeall force; 
echo :q; 
echo yes) | magic -T $techfile -d NULL > NULL 2> magic.log
echo "OK"

if (test -s magic.log ) then
echo "Scaling Coordinates.................."
 for coord in `cat magic.log | tr '(,)/=' '.....' | grep -v "SREF" | awk -F. '{printf("%d,%d,%d,%d\n",$7,$8,$2/$5,$3/$5);}'` 
 do
   o1=`echo $coord | awk -F, '{print $1}'`
   o2=`echo $coord | awk -F, '{print $2}'`
   n1=`echo $coord | awk -F, '{print $3}'`
   n2=`echo $coord | awk -F, '{print $4}'`
   echo "($o1,$o2)->($n1,$n2)"
   /bin/mv -f $toplevel.mag scale.temp
   awk '{if($1=="transform" && $4==o1 && $7==o2)\
         printf("transform %d %d %d %d %d %d\n",$2,$3,n1,$5,$6,n2);\
         else print $0;}' o1=$o1 o2=$o2 n1=$n1 n2=$n2 scale.temp > $toplevel.mag
   /bin/rm -rf scale.temp
 done
echo ".....................................OK"
fi

/bin/echo "Checking vdd label...................\c"
layer=`grep "label" $toplevel.mag | grep "vdd" |awk '{print $2}'`
if (test $layer = "space") then
    mv $toplevel.mag $toplevel.tmp
    awk '{if($1=="rlabel" && $8=="vdd") printf("%s metal1 %s %s %s %s %s %s\n",$1,$3,$4,$5,$6,$7,$8); else print $0;}' $toplevel.tmp > $toplevel.mag
    rm $toplevel.tmp
fi
echo "OK"


/bin/echo "Checking gnd label...................\c"
layer=`grep "label" $toplevel.mag | grep "gnd" |awk '{print $2}'`
if (test $layer = "space") then
    mv $toplevel.mag $toplevel.tmp
    awk '{if($1=="rlabel" && $8=="gnd") printf("%s metal1 %s %s %s %s %s %s\n",$1,$3,$4,$5,$6,$7,$8); else print $0;}' $toplevel.tmp > $toplevel.mag
    rm $toplevel.tmp
fi
echo "OK"


 
/bin/rm -f NULL
/bin/rm -f final.strm
cd ..

if (test $sourceDirSue != "noSue") then 
  /bin/rm -rf sue
  mkdir sue
  grep -v "PADFC" final.v | grep -v "PADNC" | grep -v "PADGND" | grep -v "PADVDD" | sed "s/1'b1/vdd/g" | sed "s/1'b0/gnd/g" > sue/final.v
  cd sue

  /bin/echo "Importing standard cell schematics...\c"
  cp $sourceDirSue/*.sue .
  echo "OK"

  /bin/echo "Creating Sue schematic...............\c"
  v2sue -y 300 final.v
  cd ..
fi

echo "Good by."

