#!/bin/sh
#
# Creates GDS files from the Magic cell layouts
# 
# The master cell views are located in lib/source/magic.abstract
# 
# Two things to note: 
#   1) We move all labels to "fa" (no. 109) 
#      so that we can later stream them into "text drawing"
#   2) We don't output pwell
#
# Run this script in lib/source
#
# Johannes Grad, OSU
#

#################################################
# Step: Make a local copy of the magic views
################################################
/usr/bin/rm -rf gds2
mkdir gds2
/usr/bin/rm -rf strmout
cp -R magic.abstract strmout
cd strmout

#################################################
# Step:  Move all pins to "filla" (layer 109)
#################################################
echo Moving pins to filla...
for i in `ls -1 *.mag`
do
mv $i $i.tmp
awk '{if($1=="rlabel") printf("rlabel filla %s %s %s %s %s %s\n",$3,$4,$5,$6,$7,$8); else print $0}' $i.tmp > $i
rm $i.tmp
done

#################################################
# Step: Create GDS files
#################################################
echo Streaming out AMI 0.5um for ICFB...
(
echo ":cif ostyle lambda=0.30()"
echo :calma; 
echo :q; 
echo yes) | magic -T SCN3ME_SUBM.30 -d NULL osu_stdcells > NULL
rm NULL
cp osu_stdcells.strm ../gds2/osu05_stdcells.gds2


echo Streaming out AMI 0.35um for ICFB...
(
echo ":cif ostyle lambda=0.20()"
echo :calma; 
echo :q; 
echo yes) | magic -T SCN4M_SUBM.20.TSMC -d NULL osu_stdcells > NULL
rm NULL
cp osu_stdcells.strm ../gds2/osu035_stdcells.gds2


echo Streaming out TSMC 0.25um for ICFB...
(
echo ":cif ostyle lambda=0.15()"
echo :calma; 
echo :q; 
echo yes) | magic -T SCN5M_SUBM.15 -d NULL osu_stdcells > NULL
rm NULL
cp osu_stdcells.strm ../gds2/osu025_stdcells.gds2


echo Streaming out TSMC 0.18um for ICFB...
(
echo ":cif ostyle lambda=0.10()"
echo :calma; 
echo :q; 
echo yes) | magic -T SCN6M_SUBM.10 -d NULL osu_stdcells > NULL
rm NULL
cp osu_stdcells.strm ../gds2/osu018_stdcells.gds2

cd ..

#################################################
# Step: Output AMI 05 pads
#################################################
/usr/bin/rm -rf strmout
cp -R pads_ami05 strmout
cd strmout

echo Streaming out AMI 0.5um Pads for ICFB...
(
echo ":cif ostyle lambda=0.30()"
echo :calma; 
echo :q; 
echo yes) | magic -T SCN3ME_SUBM.30 -d NULL osu_stdcells_pads > NULL
rm NULL
cp osu_stdcells_pads.strm ../gds2/osu05_pads.gds2

cd ..

#################################################
# Step: Output AMI 035 pads
#################################################
/usr/bin/rm -rf strmout
cp -R pads_ami035 strmout
cd strmout

echo Streaming out AMI 0.35um Pads for ICFB...
(
echo ":cif ostyle lambda=0.20()"
echo :calma; 
echo :q; 
echo yes) | magic -T SCN4M_SUBM.20.TSMC -d NULL osu_stdcells_pads > NULL
rm NULL
cp osu_stdcells_pads.strm ../gds2/osu035_pads.gds2

cd ..
/usr/bin/rm -rf strmout

#################################################
# Step: Create map file
#################################################
echo Writing out DFII layer map file...

cd gds2

echo "">streamin.map
echo "# Stream In Map file for GDS import into DF-II"                  >>streamin.map  
echo "# Layer Name           Layer purpose            GDSII layer"     >>streamin.map
echo "# -------------     ---------------------       -----------"     >>streamin.map
echo "# Next line maps fake filla to text"                             >>streamin.map
echo "text                drawing                     109 0"           >>streamin.map
echo "nwell               drawing                     42  0"           >>streamin.map
echo "active              drawing                     43  0"           >>streamin.map
echo "nactive             drawing                     103 0"           >>streamin.map
echo "pactive             drawing                     113 0"           >>streamin.map
echo "tactive             drawing                     60  0"           >>streamin.map
echo "poly                drawing                     46  0"           >>streamin.map
echo "sblock              drawing                     29  0"           >>streamin.map
echo "nselect             drawing                     45  0"           >>streamin.map
echo "pselect             drawing                     44  0"           >>streamin.map
echo "cc                  drawing                     25  0"           >>streamin.map
echo "cc                  drawing                     47  0"           >>streamin.map
echo "cc                  drawing                     48  0"           >>streamin.map
echo "metal1              drawing                     49  0"           >>streamin.map
echo "via                 drawing                     50  0"           >>streamin.map
echo "metal2              drawing                     51  0"           >>streamin.map
echo "via2                drawing                     61  0"           >>streamin.map
echo "metal3              drawing                     62  0"           >>streamin.map
echo "via3                drawing                     30  0"           >>streamin.map
echo "metal4              drawing                     31  0"           >>streamin.map
echo "via4                drawing                     32  0"           >>streamin.map
echo "metal5              drawing                     33  0"           >>streamin.map
echo "via5                drawing                     36  0"           >>streamin.map
echo "metal6              drawing                     37  0"           >>streamin.map
echo "metalcap            drawing                     35  0"           >>streamin.map
echo "glass               drawing                     52  0"           >>streamin.map
echo "pad                 drawing                     26  0"           >>streamin.map
echo "text                drawing                     63  0"           >>streamin.map
echo "res_id              drawing                     125 0"           >>streamin.map
echo "cap_id              drawing                     124 0"           >>streamin.map
echo "nodrc               drawing                     123 0"           >>streamin.map

cd ..

echo Done!
