#!/bin/sh
#
#  @(#)timsim8	9.4  3/22/96
#
#  timsim8
#
###################################################################
#
# program: timsim8
# purpose: Prepare the Routed Xilinx Design for QuickSimII V8 Timing Simulation
# input: routed $design.lca file
# output: $design_tim eddm directory
# authors: Robert Maul, Rajesh Trivedi, Dale Walter, David Tseitlin
# company: Xilinx
#
# 10/13/94 djt  version changed to 5.1.0
# 10/17/94 djt  version changed to 6.1.0; Added XC5200 
# 11/02/94 djt  calls external script 'find_cpu_type.sh'
#
# 12/08/94 djt  disabled message about machine type (bita 20443); 
#		version changed to pre5.1.1a (bita 20426)
#
# 01/19/95 djt  Because in 8.4 $invoke_tool can send space as an argument
#               (see pld_timsim8.qual), this script has to deal with it.
#		This fixes bita 21137.
#		Also, calls 'version.sh'
#
# 02/08/95 djt  calls 'delete_object' with 'calc_tim' 
#		(instead of 'calc_tim/schematic') - to fix bita 21583.
#		Also, created function 'delete_object_run'
#
# 03/08/95 djt  created get_awk_cmd, mbapp_run - to fix bita 22111
#
# 03/15/95 djt  changed cmd in mbapp_run to make it work
#
# 03/16/95 djt  used 'sed' to modify the output of the Mentor DVE tool.
#		This fixes bita 20247
#
# 03/21/95 djt  corrected 'vopt' assignment in get_awk_cmd
#
# 04/24/95 djt  to fix bita 21206: added -s option; functions: mv_run, 
#		xnfba_run, lca2xnf_run, vmh2xnf_run, cp_run
#
# 04/24/95 djt  When invoked from DMGR, Mentor's 'delete_object' also deleted
#		$design_tim.xnf file. To work-around, added $design_tim_tmp2
#
# 05/02/95 djt  added from_design_manager to fix bita 20194
#
# 05/03/95 robertt converted _xilinx_ within the design_tim.mba to pound (#)
#                  as part of fix for bita 23507 
#
# 06/05/95 djt  Deleted "cp_run $design_timaka.xnf $design.xff" to fix bita 24429
#
# 07/31/95 djt  Modified find_part_number to handle JONATHAN (4000e)
#
# 03/22/96 robertt  Modified find_part_number to find PART statement anywher in the .xff file (BITA28005) 
#
###################################################################

  version=`version.sh`

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
this_cpu=`find_cpu_type.sh`

if [ "${this_cpu}" = "Unknown" ] ; then
   echo ""
   echo " Warning: cannot determine the type of this machine. "
#else 
   #echo " This machine was determined to be of type '${this_cpu}'. "
fi

if [ "${this_cpu}" = "apollo" ] ; then
   echo Changing path now...
   PATH=/bin:/usr/bin:/bsd4.3/usr/ucb:/sys5.3/usr/bin:$PATH
fi
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#################################################################
# function: usage
###################################################################

help() {
  #cat <<EOF
  $cat_or_more <<EOF

This script prepares a Xilinx routed Mentor V8 design for
QuickSim II timing simulation. (System V Bourne Shell)

Usage: timsim8 <design> { -o | -g | -s } [-q -verbose -help]

  where <design> is the name of your Mentor <design> directory
    -o          - Use original schematic for timing simulation.
                  Used for schematic only designs that do not have
                  XBLOX or CLB primitives.  Not allowed on XC7000 designs.

    -g          - Generate (using gen_sch8) new schematic for timing simulation.
                  Used on designs with XBLOX or CLB primitives.

    -s          - like '-g' option, but, in addition, skips XNFBA

                 You must use either the -o or -g or -s option.

    -q          - Invoke QuickSim

    -verbose    - Run the script in verbose mode.
                  All output messages will be  shown on monitor
                  as well as piped to timsim8.log file.

    -help       - Verbose help message 


Note:

    Your XACT environment variable must be set to the location
    of the installed Xilinx software.

    Also, your MGC_WD environment variable must be set to your
    Mentor design directory.


Examples: timsim8 mydesign -o
          timsim8 mydesign -g -verbose
          timsim8 mydesign -g -q
          timsim8 -help

EOF

help_ret=1
return_code
}


#################################################################
# function: return_code
###################################################################

return_code() {

  if [ $ret -ne 0 ]; then
    if [ $program = "none" ] ; then
        echo "Errors found."
	echo "Check timsim8.log and timsim8.sh files."
    else
        echo "Errors found while running $program."
	echo "Check timsim8.log and timsim8.sh files."
    fi
  fi

  if [ $tail_flag = "true" ]; then
  	KillTailProcess
  fi

  #if [ $help_ret = 1 ]; then
  #fi

  rm -f $mmgc_wd/.retcode
  echo $ret > $mmgc_wd/.retcode
  MGC_WD=$old_mgc_wd
  export MGC_WD
  echo "Timsim8 ended with return code " $ret
  echo "Done."
  exit $ret
}


#################################################################
# function: KillTailProcess
#################################################################

KillTailProcess() {
	echo "The following message about killing the tail process" 
	echo "is to be expected.  Do not be alarmed by it."
	echo "Killing tail -f timsim8.log process: $!"
	echo "Killing tail -f timsim8.log process: $!" >> $log_file 2>&1
	kill $!
}


#################################################################
# function: delete_object_run
#################################################################

delete_object_run() {

        program="delete_object"

        cmd2="delete_object $1"

        echo "$cmd2"
        echo "$cmd2" >> $sh_file 2>&1
        echo "$cmd2" >> $log_file 2>&1
         $cmd2 >> $log_file 2>&1

        ret=$?
        if [ $ret -ne 0 ]; then
          return_code
        fi
}

#################################################################
# function: get_awk_cmd
#################################################################

get_awk_cmd() {

    # get awk type

    if [ "$this_cpu" = "sparc"  ] ; then
      	awk_cmd=nawk
        case "`/bin/uname -r`" in
             4*) vopt=""   ;;
             5*) vopt="-v" ;;
             *)  vopt=""   ;;
        esac
    elif [ "$this_cpu" = "hppa" ] ; then
       awk_cmd=awk
       vopt="-v"
    elif [ "$this_cpu" = "apollo" ] ; then
       awk_cmd=nawk
       vopt="-v"
    else
       awk_cmd=awk
       vopt="-v"
    fi
}

#################################################################
# function: mbapp_run
#################################################################

mbapp_run() {

    program="mbapp"
    get_awk_cmd

    pgm=$LCA/com/$this_cpu/mbapp.nawk

    #cmd="$awk_cmd -f $pgm $vopt mbafile=$design_tim.mba > $design_tim.mbb"

     cmd="$awk_cmd -f $pgm $vopt mbafile=$design_tim.mba"

    echo "$cmd > $design_tim.mbb"
    echo "$cmd > $design_tim.mbb" >> $sh_file 2>&1
    echo "$cmd > $design_tim.mbb" >> $log_file 2>&1
    $cmd > $design_tim.mbb

    ret=$?
    if [ $ret -ne 0 ]; then
        return_code
    fi 
}

#################################################################
# function: check_files
#################################################################

check_files() {

if [ $skip_opt = "false" ]; then

	check_xff_pld
	check_xtf_clb
	check_xg_bmf
fi

check_lca_vmh_vmd

}

#################################################################
# function: check_xff_pld
#################################################################

check_xff_pld() {

if [ \( ! -f $design.xff \) -a \( ! -f $design.pld \) ]; then
	echo "Error: Unable to find $design.xff or $design.pld file."
	echo "       Please run xmake/xemake to generate $design.xff file,"
	echo "       or if equation file, make sure $design.pld file exists."

	echo "Error: Unable to find $design.xff or $design.pld file." \
		 >> $log_file 2>&1
	echo "       Please run xmake/xemake to generate $design.xff file," \
		 >> $log_file 2>&1
	echo "       or if equation file, make sure $design.pld file exists." \
		 >> $log_file 2>&1
	echo ""
        ret=1
	help
fi

}

#################################################################
# function: check_xtf_clb
#################################################################

check_xtf_clb() {

if [ \( ! -f $design.xtf \) -a \( -f $design.clb \) ]; then
	echo "Error: Unable to find $design.xtf file."
	echo "       Please run xmake to generate $design.xtf file."

	echo "Error: Unable to find $design.xtf file." \
		 >> $log_file 2>&1
	echo "       Please run xmake to generate $design.xtf file." \
		 >> $log_file 2>&1
	echo ""
        ret=1
	help
fi

}

#################################################################
# function: check_xg_bmf
#################################################################

check_xg_bmf() {

if [ \( ! -f $design.xg \) -a \( -f $design.bmf \) ]; then
	echo "Error: Unable to find $design.xg file."
	echo "       Please run xmake to generate $design.xg file."

	echo "Error: Unable to find $design.xg file." \
		 >> $log_file 2>&1
	echo "       Please run xmake to generate $design.xg file." \
		 >> $log_file 2>&1
	echo ""
        ret=1
	help
fi

}

#################################################################
# function: check_lca_vmh_vmd
#################################################################

check_lca_vmh_vmd() {

if [ \( ! -f $design.lca \) -a \( ! -f $design.vmh \) -a \( ! -f $design.vmd \) ]; then
	echo "Error: Unable to find $design.lca or $design.vmh/vmd file."
	echo "       Please run ppr or xmake to generate $design.lca file."
	echo "       Or run xemake to generate $design.vmh/vmd file."

	echo "Error: Unable to find $design.lca or $design.vmh/vmd file." \ 
                 >> $log_file 2>&1
	echo "       Please run ppr or xmake to generate $design.lca file." \
                 >> $log_file 2>&1
	echo "       Or run xemake to generate $design.vmh/vmd file." \
                 >> $log_file 2>&1
	echo ""
        ret=1
	help
fi

}

#################################################################
# function: find_part_and_family
#################################################################

find_part_and_family() {
	find_part_number
	check_family
}

#################################################################
# function: find_part_number
# get part type & family from $design.xff or $design.net file
# input: $design.xff or $design.net
# output: part & family
#################################################################

find_part_number() {

if [ -f $design.xff ] ; then
  part=`cat $design.xff | grep "PART," | cut -d, -f2`
elif [ -f $design.pld ] ; then
  part=7
fi
echo "Part Type = $part"

xc=`echo $part | cut -c-2 | tr "[a-z]" "[A-Z]"`
if [ $xc = "XC" ] ; then
  family=`echo $part | cut -c3`
  part=`echo $part | cut -c3-`
else 
  family=`echo $part | cut -c1`
fi 
echo "Family = $family"
echo Part = $part


#  OLD STUFF
#tech=xc$family\000
#if [ $tech = "xc5000" ] ; then
#  tech=xc5200
#fi


end=\000

if [ $family = "5" ]; then
  end=\200
elif [ $family = "4" ]; then
  suff=`echo $part | cut -c5 | tr "[a-z]" "[A-Z]"`
  if [ $suff = "E" ] ; then
     end=\000e  # JONATHAN
  fi
fi

tech=xc$family$end

echo "Technology = $tech"
}

#################################################################
# function: check_family
# do sanity check on family
# input: $family
# output:
###################################################################

check_family() {

if [ \( $family != "2" \) -a \( $family != "3" \) -a \( $family != "4" \) ] ; then
  if [ \( $family != "5" \) -a \( $family != "7" \) -a \( $family != "8" \) ] ; then
    echo "Error: part_type $part must start with 2, 3, 4, 5, 7, or 8"
    echo "Error: part_type $part must start with 2, 3, 4, 5, 7, or 8" >> $log_file 2>$1
    echo ""
    ret=1
    help
  fi
fi

if [ $orgsch_opt = "true" ]; then
        if [ $tech = "xc7000" ]; then
                echo "Error: -o option invalid with XC7000 designs."
                echo "       Please use the -g option."
                echo "Error: -o option invalid with XC7000 designs." >> $log_file 2>&1
                echo "       Please use the -g option." >> $log_file 2>&1
                ret=1
                help
        fi
fi

}

#################################################################
# function: mv_run
#################################################################

mv_run() {

  program="mv"
  cmd="mv -f $1 $2"

  echo "$cmd"
  echo "$cmd" >> $sh_file 2>&1
  echo "$cmd" >> $log_file 2>&1
  $cmd >> $log_file 2>&1

  ret=$?
  if [ $ret -ne 0 ]; then
    return_code
  fi

}

#################################################################
# function: cp_run
#################################################################

cp_run() {

  program="cp"
  echo "cp $1 $2"
  echo "cp $1 $2" >> $sh_file 2>&1
  echo "cp $1 $2" >> $log_file 2>&1
  cp $1 $2 >> $log_file 2>&1 
  ret=$?
  if [ $ret -ne 0 ]; then
    return_code
  fi

}

#################################################################
# function: lca2xnf_run
# run lca2xnf on lca file
# input: $design.lca
# output: $design_timaka.xnf
#################################################################

lca2xnf_run() {

# run lca2xnf with -g option

if [ -f $design.lca ] ; then

 program="lca2xnf"
 echo "lca2xnf -wg $design $design_timaka"
 echo "lca2xnf -wg $design $design_timaka" >> $sh_file 2>&1
 echo "lca2xnf -wg $design $design_timaka" >> $log_file 2>&1
 lca2xnf -wg $design $design_timaka >> $log_file 2>&1
 ret=$?
 if [ $ret -ne 0 ]; then
   return_code
 fi

fi

#if [ $skip_opt = "true" ]; then
#	cp_run $design_timaka.xnf $design.xff
#fi

}

#################################################################
# function: vmh2xnf_run
# run vmh2xnf on vmh file
# input: $design.vmh or $design.vmd for 7272 parts.
# output: $design_tim.xnf
#################################################################

vmh2xnf_run() {

# run vmh2xnf with -n option

if [ -f $design.vmh ] ; then
 
 cd $head
 program="vmh2xnf"
 echo "vmh2xnf -n $leaf.vmh -o $leaf_timaka"
 echo "vmh2xnf -n $leaf.vmh -o $leaf_timaka" >> $sh_file 2>&1
 echo "vmh2xnf -n $leaf.vmh -o $leaf_timaka" >> $log_file 2>&1
 vmh2xnf -n $leaf.vmh -o $leaf_timaka >> $log_file 2>&1
 ret=$?
 if [ $ret -ne 0 ]; then
   return_code
 fi

 if [ $skip_opt = "true" ]; then
	cp_run $leaf_timaka.xnf $design.xff
 fi

elif [ -f $design.vmd ] ; then
 
 cd $head
 program="vmh2xnf"
 echo "vmh2xnf -n $leaf.vmd -o $leaf_timaka"
 echo "vmh2xnf -n $leaf.vmd -o $leaf_timaka" >> $sh_file 2>&1
 echo "vmh2xnf -n $leaf.vmd -o $leaf_timaka" >> $log_file 2>&1
 vmh2xnf -n $leaf.vmd -o $leaf_timaka >> $log_file 2>&1
 ret=$?
 if [ $ret -ne 0 ]; then
   return_code
 fi

 if [ $skip_opt = "true" ]; then
	cp_run $leaf_timaka.xnf $design.xff
 fi

fi

}

#################################################################
# function: unakaxnf_run
# run unakaxnf on routed xnf file
# input: $design_timaka.xnf
# output: $design_tim.xnf
###################################################################
 
unakaxnf_run() {

# run unakaxnf with -o option for all families.
 
if [ $aka_flag = "true" ]; then
 
  program="unakaxnf"
  echo "unakaxnf -o $design_tim -a $design -w $design_timaka"
  echo "unakaxnf -o $design_tim -a $design -w $design_timaka" >> $sh_file 2>&1
  echo "unakaxnf -o $design_tim -a $design -w $design_timaka" >> $log_file 2>&1
  unakaxnf -o $design_tim -a $design -w $design_timaka >> $log_file 2>&1 
  ret=$?
  if [ $ret -ne 0 ]; then
    return_code
  fi

else
 
  mv_run $design_timaka.xnf $design_tim.xnf

fi

}

#################################################################
# function: xnfba_run
# If the user has specified -o option and it is a simple combinatorial
# design then run xnfba with -m # option.
# Otherwise run xnfba with -x option.
###################################################################

xnfba_run() {

if [ $orgsch_opt = "true" ]; then

  if [ $family != "7" ] ; then

    ###################################################################
    # function: run xnfba with -m option on $design.xff file
    # input: $design.xff
    # output: $design_tim.mba
    ###################################################################

    program="xnfba"
    echo "xnfba $design.xff $design_tim.xnf -m"
    echo "xnfba $design.xff $design_tim.xnf -m" >> $sh_file 2>&1
    echo "xnfba $design.xff $design_tim.xnf -m" >> $log_file 2>&1
    xnfba $design.xff $design_tim.xnf -m >> $log_file 2>&1

    # ROBERT
    cp $design_tim.mba tmp_file
    cat tmp_file | sed -e 's/_xilinx_/#/' > $design_tim.mba
    rm tmp_file
    # END ROBERT

    ret=$?
    if [ $ret -ne 0 ]; then
      return_code
    fi

    program="rm"
    rm -f $design_tim.mbb
    ret=$?
    if [ $ret -ne 0 ]; then
      return_code
    fi

    mbapp_run

    mv_run $design_tim.mbb $design_tim.mba

  fi

elif [ $gensch_opt = "true" ]; then

  if [ $family != "7" ] ; then

    if [ -f $design.clb ] ; then

      ###################################################################
      # function: for 2K/3K designs with CLB/IOB/EQN's run xnfba with -x option on
      #           $design_tim.xnf file
      # input: $design_tim.xnf
      # output: $design_tim.xnf
      ###################################################################

      program="xnfba"
      echo "xnfba $design.xtf $design_tim.xnf -o $design_tim.xbf"
      echo "xnfba $design.xtf $design_tim.xnf -o $design_tim.xbf" >> $sh_file 2>&1
      echo "xnfba $design.xtf $design_tim.xnf -o $design_tim.xbf" >> $log_file 2>&1
      xnfba $design.xtf $design_tim.xnf -o $design_tim.xbf >> $log_file 2>&1
      ret=$?
      if [ $ret -ne 0 ]; then
        return_code
      fi

    elif [ -f $design.bmf ] ; then

      program="xnfba"
      echo "xnfba $design.xg $design_tim.xnf -o $design_tim.xbf"
      echo "xnfba $design.xg $design_tim.xnf -o $design_tim.xbf" >> $sh_file 2>&1
      echo "xnfba $design.xg $design_tim.xnf -o $design_tim.xbf" >> $log_file 2>&1
      xnfba $design.xg $design_tim.xnf -o $design_tim.xbf >> $log_file 2>&1
      ret=$?
      if [ $ret -ne 0 ]; then
        return_code
      fi

    else

      program="xnfba"
      echo "xnfba $design.xff $design_tim.xnf -o $design_tim.xbf"
      echo "xnfba $design.xff $design_tim.xnf -o $design_tim.xbf" >> $sh_file 2>&1
      echo "xnfba $design.xff $design_tim.xnf -o $design_tim.xbf" >> $log_file 2>&1
      xnfba $design.xff $design_tim.xnf -o $design_tim.xbf >> $log_file 2>&1
      ret=$?
      if [ $ret -ne 0 ]; then
        return_code
      fi

    fi

    mv_run $design_tim.xbf $design_tim.xnf
 
  fi

fi

}


#################################################################
# start main shell
#################################################################

cat <<EOS

               TIMSIM8 Version $version
             Copyright (c) Xilinx 1994,1995. All Rights Reserved

                     UNPUBLISHED, LICENSED SOFTWARE.
          CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
                  PROPERTY OF XILINX OR ITS LICENSORS.


EOS


###################################################################
# init parameters
###################################################################

# Begin Variables

orgsch_opt="false"
gensch_opt="false"

skip_opt="false" # to skip XNFBA - see bita 21206

quick_opt="false"
verbose_opt="false"

from_design_manager="false" # to fix bita 20194

tail_flag="false"
help_ret=0
ret=0
aka_flag="false"
err_ret=0
err_opt=" "

# End Variables

if [ $# -lt 1 ] ; then
  help_ret=1
fi


#################################################################
# scan off command line arguments
#################################################################

while [ $# -gt 0 ]; do

  if [ null$1 = "null" ]; then
    	shift
  elif [ $1 = "-o" ]; then
	orgsch_opt="true"
    	shift
  elif [ $1 = "-O" ]; then
	orgsch_opt="true"
    	shift
  elif [ $1 = "-g" ]; then
	gensch_opt="true"
    	shift
  elif [ $1 = "-G" ]; then
	gensch_opt="true"
    	shift

  elif [ $1 = "-s" ]; then
	gensch_opt="true"
	skip_opt="true"
    	shift
  elif [ $1 = "-S" ]; then
	gensch_opt="true"
	skip_opt="true"
    	shift

  elif [ $1 = "-q" ]; then
	quick_opt="true"
    	shift
  elif [ $1 = "-Q" ]; then
	quick_opt="true"
    	shift
  elif [ $1 = "-verbose" ]; then
    	verbose_opt="true"
    	shift
  elif [ $1 = "-VERBOSE" ]; then
    	verbose_opt="true"
    	shift
  elif [ $1 = "FROM_DESIGN_MANAGER" ]; then
    	from_design_manager="true"
    	shift
  elif [ $1 = "-h" ]; then
    	help_ret=1
    	shift
  elif [ $1 = "-H" ]; then
    	help_ret=1
    	shift
  elif [ $1 = "-help" ]; then
    	help_ret=1
    	shift
  elif [ $1 = "-HELP" ]; then
    	help_ret=1
    	shift
  elif [ `echo $1 | cut -c1` = "-" ]; then
  	err_msg="Error: Illegal option specified on the command line:  "
	err_opt=$1
        err_ret=1
	#help
        shift
  else
    design=$1
    shift
  fi

done

if [ $from_design_manager = "true" ]; then
	cat_or_more="cat"
else
	cat_or_more="more"
fi

#################################################################
# strip off leading directory hierarchy for edifi2xnf -od argument
# use leaf for -of argument
#################################################################

tmp_design=`echo $design | sed -e 's|\(.*\)/\(.*\)|\1:\2|'`
head=`echo $tmp_design | cut -d: -f1`
leaf=`echo $tmp_design | cut -d: -f2`
if [ $head = $leaf ]; then
  # head=`pwd | sed -e "s./export.." | sed -e "s./tmp_mnt.."`
  head=`pwd | sed -e "s./tmp_mnt.."`
fi
if [ null$head = "null" ]; then
  head=`pwd | sed -e "s./tmp_mnt.."`
fi

###################################################################
# Reset MGC_WD to directory containing design...
###################################################################

old_mgc_wd=${MGC_WD}
MGC_WD=$head
export MGC_WD
mmgc_wd=`$MGC_HOME/bin/get_hard_name -s $MGC_WD`
echo "MGC_WD = " $MGC_WD
log_file=$mmgc_wd/timsim8.log
sh_file=$mmgc_wd/timsim8.sh


###################################################################
# create log file timsim8.log...
###################################################################

program="rm"
rm -f $log_file
ret=$?
if [ $ret -ne 0 ]; then
  return_code
fi

program="touch"
touch $log_file
ret=$?
if [ $ret -ne 0 ]; then
  return_code
fi


###################################################################
# create log file timsim8.sh...
###################################################################

program="rm"
rm -f $sh_file
ret=$?
if [ $ret -ne 0 ]; then
  return_code
fi

program="touch"
touch $sh_file
ret=$?
if [ $ret -ne 0 ]; then
  return_code
fi
program="none"

echo "#!/bin/sh" >> $sh_file 2>&1
echo "" >> $sh_file 2>&1

if [ $help_ret -ne 0 ] ; then
  ret=0
  help
fi

if [ $err_ret -ne 0 ] ; then
  echo $err_msg $err_opt
  echo $err_msg $err_opt >> $log_file 2>&1
  ret=1
  help
fi



#################################################################
# check env variables
#################################################################

if [ null$XACT = "null" ]; then
  echo "Error: XACT variable needs to be set to location of Xilinx software"
  echo "Error: XACT variable needs to be set to location of Xilinx software" \
	   >> $log_file 2>&1
  ret=1
  help
fi

if [ null$LCA = "null" ]; then
  echo "Error: LCA variable needs to be set to location of Xilinx/Mentor Kit"
  echo "Error: LCA variable needs to be set to location of Xilinx/Mentor Kit" \
	   >> $log_file 2>&1
  ret=1
  help
fi

if [ ! -d $LCA ]; then
  echo "Error: LCA = $LCA , directory not found."
  echo "Error: LCA = $LCA , directory not found." >> $log_file 2>&1
  ret=1
  help
fi

if [ null$MGC_WD = "null" ]; then
  echo "Error: MGC_WD variable needs to be set to design directory"
  echo "Error: MGC_WD variable needs to be set to design directory" \
       >> $log_file 2>&1
  ret=1
  help
fi

if [ ! -d $mmgc_wd ]; then
  echo "Error: MGC_WD = $mmgc_wd , directory not found."
  echo "Error: MGC_WD = $mmgc_wd , directory not found." >> $log_file 2>&1
  ret=1
  help
fi

if [ null$MGC_HOME = "null" ]; then
  echo "Error: MGC_HOME needs to be set to the MGC_HOME tree."
  echo "Error: MGC_HOME needs to be set to the MGC_HOME tree." >> $log_file 2>&1
  echo ""
  ret=1
  help
fi

if [ ! -d $MGC_HOME ]; then
  echo "Error: MGC_HOME = $MGC_HOME , directory not found!"
  echo "Error: MGC_HOME = $MGC_HOME , directory not found!" >> $log_file 2>&1
  echo ""
  ret=1
  help
fi


#################################################################
# check command line options and arguments
#################################################################

if [ \( $orgsch_opt = "false" \) -a \( $gensch_opt = "false" \) -a \( $skip_opt = "false" \) ]; then
  echo "Error: Either the -o or -g or -s option must be specified on command line"
  echo "Error: Either the -o or -g or -s option must be specified on command line" \
       >> $log_file 2>&1
  ret=1
  help
fi

if [ \( $orgsch_opt = "true" \) -a \( $gensch_opt = "true" \) -a \( $skip_opt = "true" \) ]; then
  echo "Error: Only the -o or -g or -s option should be specified"
  echo "Error: Only the -o or -g or -s option should be specified" \
       >> $log_file 2>&1
  ret=1
  help
fi

if [ null$design = "null" ]; then
  echo "Error: <design> not specified on command line"
  echo "Error: <design> not specified on command line" >> $log_file 2>&1
  ret=1
  help
fi

if [ $orgsch_opt = "true" ]; then

	if [ -f $design.bmf ]; then
      echo "Error: -o option can be used only with schematic only designs."
      echo "       $design.xnf contains DEF=BLOX properties."

      echo "Error: -o option can be used only with schematic only designs."\
           >> $log_file 2>&1
      echo "       $design.xnf contains DEF=BLOX properties." >> $log_file 2>&1

	  err="true"
	fi

    if [ null$err != "null" ]; then
      echo ""
      echo ""
          ret=1
	  help
    fi

fi

if [ -f $design.aka ]; then
  aka_flag="true"
fi

check_files #### djt: added 04/21/95 ####

###################################################################
# define variables
# design_tim: routed xnf file generated from lca2xnf.
###################################################################

design_tim=$design\_tim
design_tim_tmp2=$design_tim\_tmp2

design_timaka=$design\_timaka
leaf_timaka=$leaf\_timaka

#### djt: added 04/21/95 ####

if [ $skip_opt = "false" ]; then
	find_part_and_family
fi

###################################################################
# function: if verbose option is specified then start the tail process.
# input: timsim8.log
# output:
###################################################################

if [ $verbose_opt = "true" ]; then

	echo "tail -f " $log_file " &" >> $log_file 2>&1
	tail -f $log_file &

	trap return_code 2
	tail_flag="true"
fi


########################  Invoke Point Tools  #####################

#### djt: added 04/21/95 ####

lca2xnf_run
vmh2xnf_run

if [ $skip_opt = "true" ]; then
	find_part_and_family
fi

unakaxnf_run

if [ $skip_opt = "false" ]; then
	xnfba_run
fi


########################  pld_dve_ba  #############################


###################################################################
# If the user has specified -o option and it is a simple
# combinatorial design then run pld_dve_ba on $design_tim.mba file.
#
# Before running pld_dve_ba make sure that
# $design/default.Eddm_design_viewpoint.attr file exists.
# If it does not exists then create it by running pld_dve_sim on
# $design.
###################################################################

if [ $orgsch_opt = "true" ]; then

  if [ \( ! -f $design/default.Eddm_design_viewpoint.attr \) ]; then

    program="pld_dve_sim"
    echo "pld_dve_sim $design $tech"
    echo "pld_dve_sim $design $tech" >> $sh_file 2>&1
    echo "pld_dve_sim $design $tech" >> $log_file 2>&1
    pld_dve_sim $design $tech >> $log_file 2>&1
    ret=$?
    if [ $ret -ne 0 ]; then
      return_code
    fi

    grep "Error:" $log_file | grep "//" > /dev/null
    ret=$?
    if [ $ret -eq 0 ] ; then
      ret=1
      return_code
    fi

  fi

  ###################################################################
  # function: run pld_dve_ba script on $design_tim.mba file
  # input:  $design_tim.mba file
  # output:
  ###################################################################
  
  program="pld_dve_ba"
  echo "pld_dve_ba $design $design_tim.mba"
  echo "pld_dve_ba $design $design_tim.mba" >> $sh_file 2>&1
  echo "pld_dve_ba $design $design_tim.mba" >> $log_file 2>&1

  # 3/16/95 djt: 'sed' command in the following line fixes a Mentor DVE bug:
  # the last line of its output has too many tabs, so 'vi' can not display  
  # the log file (timsim8.log) and gives the message "Line too long" (see bita 20247)

  #pld_dve_ba $design $design_tim.mba >> $log_file 2>&1

   pld_dve_ba $design $design_tim.mba | sed -e 's/^		*//' >> $log_file 2>&1

  ret=$?
  if [ $ret -ne 0 ]; then
    return_code
  fi

  grep "Error:" $log_file | grep "//" > /dev/null
  ret=$?
  if [ $ret -eq 0 ] ; then
    ret=1
    return_code
  fi

fi


########################  gen_sch8  ###############################


###################################################################
# If the user has specified -g option then run gen_sch8.
###################################################################

if [ $gensch_opt = "true" ]; then
  
  # if $design_tim eddm dir already exists, then delete it
    
  if [ -d $design_tim ]; then
     mv_run $design_tim.xnf $design_tim_tmp2.xnf
     #delete_object_run $design_tim/schematic
     delete_object_run $design_tim
     mv_run $design_tim_tmp2.xnf $design_tim.xnf
  fi

  ###############################################################
  # function: run gen_sch8 on $design_tim.xnf file
  # input:  $design_tim.xnf
  # output: $design_tim eddm directory
  ###############################################################
  
  program="gen_sch8"
  echo "gen_sch8 -w $design_tim.xnf"
  echo "gen_sch8 -w $design_tim.xnf" >> $sh_file 2>&1
  echo "gen_sch8 -w $design_tim.xnf" >> $log_file 2>&1


  if [ ! -f $design_tim.xnf ] ; then
  	msg2="File $design_tim.xnf does not exist"
  	echo "$msg2"
  	echo "$msg2" >> $sh_file 2>&1
  	echo "$msg2" >> $log_file 2>&1
	exit 1
  fi

  gen_sch8 -w $design_tim.xnf >> $log_file 2>&1
  ret=$?
  if [ $ret -ne 0 ]; then
    return_code
  fi

fi


########################  pld_dve_sim  ############################


###################################################################
# If the user has specified -g option then run pld_dve_sim
# on $design_tim.
###################################################################

if [ $gensch_opt = "true" ]; then


  ###############################################################
  # function: run pld_dve_sim script on $design_tim design
  # input:  $design_tim
  # output:
  ###############################################################
  
  program="pld_dve_sim"
  echo "pld_dve_sim $design_tim $tech"
  echo "pld_dve_sim $design_tim $tech" >> $sh_file 2>&1
  echo "pld_dve_sim $design_tim $tech" >> $log_file 2>&1
  pld_dve_sim $design_tim $tech >> $log_file 2>&1
  ret=$?
  if [ $ret -ne 0 ]; then
    return_code
  fi
 
  grep "Error:" $log_file | grep "//" > /dev/null
  ret=$?
  if [ $ret -eq 0 ] ; then
    ret=1
    return_code
  fi

fi


########################  quicksim  ###############################


###################################################################
# If the user has specified -o option then run quicksim on $design.
# Otherwise, if the user has specified -g option then run quicksim
# on $design_tim.
###################################################################

if [ $orgsch_opt = "true" ]; then

  ###############################################################
  # function: if -q switch is present then run quicksim on $design
  # input: $design
  # output:
  ###############################################################
  
  if [ $quick_opt = "true" ]; then
  
  	program="quicksim"
  	echo "quicksim -tim typ -consm messages $design"
  	echo "quicksim -tim typ -consm messages $design" >> $sh_file 2>&1
  	echo "quicksim -tim typ -consm messages $design" >> $log_file 2>&1
  	quicksim -tim typ -consm messages $design >> $log_file 2>&1
  	ret=$?
  	if [ $ret -ne 0 ]; then
  	  return_code
  	fi
  
  else
  
	echo ""
  	echo "Typical timing model has been succesfully generated."
  	echo "Please run QuickSim on $design design for Timing Simulation."
  	echo "Please use the following command:"
  	echo "   quicksim -tim typ -consm messages $design"
  	
	echo "" >> $log_file 2>&1
  	echo "Typical timing model has been succesfully generated." >> $log_file 2>&1
  	echo "Please run QuickSim on $design design for Timing Simulation." \
		 >> $log_file 2>&1
  	echo "Please use the following command:" >> $log_file 2>&1
  	echo "   quicksim -tim typ -consm messages $design" >> $log_file 2>&1
  
  fi

elif [ $gensch_opt = "true" ]; then

  ###################################################################
  # function: if -q switch is present then run quicksim on $design_tim
  # input: $design_tim
  # output:
  ###################################################################
  
  if [ $quick_opt = "true" ]; then
  
  	program="quicksim"
  	echo "quicksim -tim typ -consm messages $design_tim"
  	echo "quicksim -tim typ -consm messages $design_tim" >> $sh_file 2>&1
  	echo "quicksim -tim typ -consm messages $design_tim" >> $log_file 2>&1
  	quicksim -tim typ -consm messages $design_tim >> $log_file 2>&1
  	ret=$?
  	if [ $ret -ne 0 ]; then
  	  return_code
  	fi
  
  else
  
	echo ""
  	echo "Typical timing model has been succesfully generated."
  	echo "Please run QuickSim on $design_tim design for Timing Simulation."
  	echo "Please use the following command:"
  	echo "   quicksim -tim typ -consm messages $design_tim"
  	
	echo "" >> $log_file 2>&1
  	echo "Typical timing model has been succesfully generated." >> $log_file 2>&1
  	echo "Please run QuickSim on $design_tim design for Timing Simulation." \
		 >> $log_file 2>&1
  	echo "Please use the following command:" >> $log_file 2>&1
  	echo "   quicksim -tim typ -consm messages $design_tim" >> $log_file 2>&1
  
  fi

fi


###################################################################
ret=0
return_code
###################################################################
