#!/bin/bash
#
# script to create all cell views. Using Alliance version 20060218 and
# Magic version 7.4.8 with Magic compiled with the TCL option
#
if test $# -eq 0
then
  echo "# Usage ./makeallviews cell_name (without AP extension). Please supply cell_name."
  exit 1
fi
if test -f "$1.ap"
then
dir=$PWD

echo "#"
echo "# MAKING ALL VIEWS FOR CELL $1" 1>&2
echo "#"
echo "# Write vxlib013_alliance CIF from Alliance"

source /usr/share/pharosc/alliance/bin/env_vx013
/usr/share/pharosc/alliance/bin/make_al $1
RDS_IN=cif
RDS_OUT=cif
rm -f $1.cif
s2r_vx013 $1 >/dev/null
s2r -p $1 >/dev/null
/usr/share/pharosc/alliance/bin/touch_alliance_cif $1
cp $1.cif /usr/share/pharosc/alliance/cif/vxlib013
/usr/share/pharosc/alliance/bin/grep_ref_boxes $1 ../../../magic/etc/vxlib/$1_vx013.ref
grep '^4A' $1.cif > /usr/share/pharosc/magic/etc/vxlib/$1_vx013.ab

#echo "# Write vxlib013_alliance GDS from Alliance"

#RDS_IN=gds
#RDS_OUT=gds
#rm -f $1.gds
#s2r_vx013 $1 >/dev/null
#cp $1.gds /usr/share/pharosc/alliance/gds/vxlib013

echo "# Write spice deck using vx013 values"

/usr/share/pharosc/alliance/bin/vx013_make_spice $1
grep subckt $1.spi > /usr/share/pharosc/magic/etc/vxlib/$1.subckt
cp $1.spi /usr/share/pharosc/alliance/subckt/vxlib013

echo "# Write vxlib100 CIF from Alliance"

source /usr/share/pharosc/alliance/bin/env_vx100
/usr/share/pharosc/alliance/bin/make_al $1
RDS_IN=cif
RDS_OUT=cif
rm -f $1.cif
s2r_vx100 $1 >/dev/null
s2r -p $1 >/dev/null
/usr/share/pharosc/alliance/bin/touch_alliance_cif $1
cp $1.cif /usr/share/pharosc/alliance/cif/vxlib100
/usr/share/pharosc/alliance/bin/grep_ref_boxes $1 ../../../magic/etc/vxlib/$1_vx100.ref
grep '^4A' $1.cif > /usr/share/pharosc/magic/etc/vxlib/$1_vx100.ab

echo "# Write vxlib200 CIF from Alliance for Magic"

source /usr/share/pharosc/alliance/bin/env_vx200
RDS_IN=cif
RDS_OUT=cif
rm -f $1.cif
s2r_vx200 $1 >/dev/null
s2r -p $1 >/dev/null
/usr/share/pharosc/alliance/bin/touch_alliance_cif $1
vx_add_ab $1
cp $1.cif /usr/share/pharosc/alliance/cif/vxlib200
/usr/share/pharosc/alliance/bin/grep_ref_boxes $1 ../../../magic/etc/vxlib/$1_vx200.ref
grep '^4A' $1.cif > /usr/share/pharosc/magic/etc/vxlib/$1_vx200.ab

/usr/share/pharosc/alliance/bin/make_magic_cif $1
cp $1.cif /usr/share/pharosc/magic/cells/vxlib

rm -f $1.al
rm -f $1.spi
rm -f $1.cif
rm -f $1.gds

cd /usr/share/pharosc/magic/cells/vxlib

#
# Script update 23-JUN-06 to run with Magic compiled with TCL. The
# actual version of Magic being used is the 7.4.8 release.
#
# xhost + command needed for -noconsole option to be accepted
#
xhost + 1>/dev/null

echo
echo "# Read vxlib200 CIF into Magic"
magic -Tvx200  -dnull -noconsole <<EOF  >/dev/null 2>&1
;cif read $1.cif
;load $1
;drc count total
;save
exit
EOF

echo
echo "# Extract 0.13um SPI file from Magic"
magic -Tvx013 -dnull -noconsole <<EOF >/dev/null 2>&1
;load $1
;box 0 0 1 1
;select more box pwell
;delete
;box 0 99 1 100
;select more box nwell
;delete
;select
;extract do capacitance
;extract do coupling
;extract do adjust
;extract do resistance
;extract all
;ext2spice -f spice3 -c 0.001 -r 1 -y 3 -d
;quit
EOF

echo
echo "# Write vxlib200 CIF file from Magic"
magic -Tvx200 -dnull -noconsole <<EOF >/dev/null 2>&1
;load $1
;cif write $1
;gds labels
;gds write $1
;quit
EOF

/usr/share/pharosc/alliance/bin/make_alliance_cif $1 ../../../alliance/cif/vxlib200/$1 ../../../magic/etc/vxlib/$1_vx200.ref
cp $1.cif /usr/share/pharosc/magic/cif/vxlib200
cp $1.gds /usr/share/pharosc/magic/gds/vxlib200

echo
echo "# Write vxlib013 CIF and GDS files from Magic"
magic -Tvx013 -dnull -noconsole <<EOF >/dev/null 2>&1
;load $1
;cif ostyle lambda=0.055um(vxlib013)
;cif write $1
;gds labels
;gds write $1
;quit
EOF

/usr/share/pharosc/alliance/bin/make_alliance_cif $1 ../../../alliance/cif/vxlib013/$1 ../../../magic/etc/vxlib/$1_vx013.ref
mv $1.cif /usr/share/pharosc/magic/cif/vxlib013
mv $1.gds /usr/share/pharosc/magic/gds/vxlib013

echo
echo "# Convert Magic SPICE output to SPI subckt"
/usr/share/pharosc/alliance/bin/spice2spi_013 $1 vxlib
mv $1.spi /usr/share/pharosc/magic/subckt/vxlib013
cd $dir
else
  echo "# Usage ./makeallviews cell_name (without AP extension). cell_name supplied does not exist."
  exit 1
fi
