#!/bin/sh
#
#@(#)fncsim8	9.4  3/19/96
#
###################################################################
#
# program: fncsim8
# purpose: Perform QuickSimII V8 Functional Simulation
# input: flat design.xnf
# output:
# 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'
# 11/08/94 djt  suppressed error checking in 'mv leaf_fnc.lca' - fixes bug 20001
#
# 12/08/94 djt  disabled message about machine type (bita 20443); 
#		version changed to pre5.1.1a (bita 20426)
# 12/20/94 djt  modularized: more functions
# 12/20/94 djt  fixed bita 20036
#
# 01/19/95 djt  Because in 8.4 $invoke_tool can send space as an argument
#               (see pld_fncsim8.qual), this script has to deal with it.
#		This fixes bita 21137.
#		Also, calls 'version.sh'
#
# 01/27/95 djt  gen_sch8_run argument changed to 'simdir/$xnf_file'
#		(was 'simdir/$xnf'). This fixes bita 21229.
#
# 02/06/95 djt  changed 'option_g_4k_no_place' to fix bita 21527
#
# 02/07/95 djt  print_header, init_variables un-function'ed to fix bita 21569
#		(because HP loses value of '$#' after any function invocation)
#
# 02/08/95 djt  calls 'delete_object' with 'calc_fnc' 
#		(instead of 'calc_fnc/schematic') - to fix bita 21585
#
# 02/13/95 djt  (1) place_3k: apr_run replaced by mv_run - fixes bug 21713
#		(2) option_g_3k_no_place: mv_run is under 'else' - 
#		    consistent with option_g_4k_no_place (bita 21527 - see above)
#		(3) mv_run accepts arg's
#
# 02/15/95 djt  find_need_place, process_FILE_and_PLD_prop changed to fix bita 21778
#
# 02/16/95 djt  added process_PLD, process_ABEL, ABEL_gensch_1, ABEL_gensch_2 
#		- to modularise and fix bita 21833
#
# 03/08/95 djt  get_platform replaced by get_awk_cmd, platform replaced by this_cpu
#		- to fix bita 22095
#
# 07/25/95 djt  Modified get_part to handle JONATHAN (4000e)
#
# 03/19/96 robertt Modified option_g_4k_place to fix BITA 28020
#
####################################################################

  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

#exit 1

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

#################################################################
# function: help
###################################################################

help() {
  cat <<EOF

This program prepares a Mentor design for QuickSimII functional
simulation. It invokes gen_sch8, pld_dve_sim, and QuickSim as needed.
Men2xnf8 must be run on design prior to invoking fncsim8.

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

       where <design> is the name of your Mentor <design> directory.

    -o             - Use original schematic for functional simulation.
                     Used for schematic-only designs that do not use
                     CLB primitives.

    -g             - Generate (using gen_sch8) new schematic for functional simulation.
                     Used on designs with CLB primitives.  Not allowed on XC7000
                     designs.

                     Either the -o or -g option must be used, but not both.

    -q             - Invoke QuickSim for functional simulation.

    -r             - Force regeneration of simdir and all files contained in it
		     (for X-BLOX designs only).

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

    -help          - Verbose help message


Note:

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


Examples: fncsim8 mydesign -o 
          fncsim8 mydesign -o -verbose
          fncsim8 mydesign -g -q -verbose
          fncsim8 mydesign -g -q -r -verbose
          fncsim8 -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 fncsim8.log and fncsim8.sh files."
    else
        echo "Errors found while running $program."
        echo "Check fncsim8.log and fncsim8.sh files."
    fi
  fi

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

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

  if [ $set_return_code = "true" ] ; then

     rm -f $mmgc_wd/.retcode
     echo $ret > $mmgc_wd/.retcode
     MGC_WD=$old_mgc_wd
     export MGC_WD
     echo "Fncsim8 ended with return code " $ret
  fi

  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 fncsim8.log process: $!"
	echo "Killing tail -f fncsim8.log process: $!" >> $log_file 2>&1
	kill $!
}

#################################################################
# function: strip_hierarchy
#################################################################

strip_hierarchy() {

# 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

}

#################################################################
# function: reset_MGC_WD
#################################################################

reset_MGC_WD() {

# 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/fncsim8.log
sh_file=$mmgc_wd/fncsim8.sh
}

#################################################################
# function: create_log 
#################################################################

create_log () {

# create log file fncsim8.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
}

#################################################################
# function: create_log_2 
#################################################################

create_log_2 () {

# create log file fncsim8.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

}

#################################################################
# function: check_env_vars
#################################################################

check_env_vars() {

# 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
  echo ""
  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
  echo ""
  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
  echo ""
  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
  echo ""
  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
  echo ""
  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

}

#################################################################
# function: check_options
#################################################################

check_options() {

# check command line options and arguments

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

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

if [ ! -f $design.xnf ]; then

	echo "Error: Unable to find $design.xnf file."
	echo "       Please run men2xnf8 to generate $design.xnf file."

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

if [ $orgsch_opt = "true" ]; then

	if [ -f $design.clb ]; then

	  echo "Error: Unable to process design with -o option."
	  echo "       Design contains CLB/IOB/EQN primitives."
	  echo "Error: Unable to process design with -o option." >> $log_file 2>&1
	  echo "       Design contains CLB/IOB/EQN primitives." >> $log_file 2>&1
          echo ""
          ret=1
	  help

        fi

# The following section is obsolete in Virgil
#
#	if [ -f $design.hmf ]; then
#      echo "Error: -o option can be used only with schematic only designs."
#      echo "       $design.xnf contains DEF=HM properties."
#
#      echo "Error: -o option can be used only with schematic only designs."\
#           >> $log_file 2>&1
#      echo "       $design.xnf contains DEF=HM properties."\
#           >> $log_file 2>&1
#
#	  err="true"
#	fi

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

fi
}

#################################################################
# function: get_part
#################################################################

get_part() {

# function: Obtain part type from $design.xnf file.
# input: $design.xnf
# output: part, family, tech


part=`head $design.xnf | grep "PART," | cut -d, -f2`
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" \) -o \( $suff = "L" \) ] ; then
     end=\000e  # JONATHAN
  fi
fi

tech=xc$family$end

echo "Technology = $tech"


abl_fam=$tech
# a_part = TRUE if part is type A or L, otherwise a_part = FALSE
a_part=FALSE
echo $part | grep -i "A"  > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
  a_part=TRUE
  abl_fam=$tech\a
fi
echo $part | grep -i "L"  > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
  a_part=TRUE
  abl_fam=$tech\l
fi
}

#################################################################
# function: check_options_2
#################################################################

check_options_2() {

if [ $gensch_opt = "true" ]; then

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

	if [ -f $design.pld ]; then

	  echo "Error: Unable to process design with -g option."
	  echo "       Design contains DEF=PLD primitives."
	  echo "Error: Unable to process design with -g option." >> $log_file 2>&1
	  echo "       Design contains DEF=PLD primitives." >> $log_file 2>&1
          echo ""
          ret=1
	  help

        fi

fi
}

#################################################################
# function: abl2xnf_run
#################################################################

abl2xnf_run() {

   program="abl2xnf"
   if [ -f $design.ulf ] ; then
            echo "abl2xnf $out_fn.abl parttype=$part family=$abl_fam"
            echo "abl2xnf $out_fn.abl parttype=$part family=$abl_fam" >> $sh_file 2>&1
            echo "abl2xnf $out_fn.abl parttype=$part family=$abl_fam" >> $log_file 2>&1
            abl2xnf $out_fn.abl parttype=$part family=$abl_fam >> $log_file 2>&1
   else
            echo "abl2xnf $out_fn.abl parttype=$part family=$abl_fam old_library=true"
            echo "abl2xnf $out_fn.abl parttype=$part family=$abl_fam old_library=true" >> $sh_file 2>&1
            echo "abl2xnf $out_fn.abl parttype=$part family=$abl_fam old_library=true" >> $log_file 2>&1
            abl2xnf $out_fn.abl parttype=$part family=$abl_fam old_library=true >> $log_file 2>&1
   fi
   ret=$?
   if [ $ret -ne 0 ]; then
           return_code
   fi
}

#################################################################
# function: gen_sch8_run
#################################################################

gen_sch8_run() {
	program="gen_sch8"
        echo "gen_sch8 $1 -w -o $2"
        echo "gen_sch8 $1 -w -o $2" >> $sh_file 2>&1
        echo "gen_sch8 $1 -w -o $2" >> $log_file 2>&1
        gen_sch8 $1 -w  -o $2 >> $log_file 2>&1
        ret=$?
        if [ $ret -ne 0 ]; then
                return_code
        fi
}

#################################################################
# function: gen_sch8_run_2
#################################################################

gen_sch8_run_2() {

	program="gen_sch8"
	echo "gen_sch8 -w $design_fnc.xnf"
	echo "gen_sch8 -w $design_fnc.xnf" >> $sh_file 2>&1
	echo "gen_sch8 -w $design_fnc.xnf" >> $log_file 2>&1
	gen_sch8 -w $design_fnc.xnf >> $log_file 2>&1
	ret=$?
	if [ $ret -ne 0 ]; then
	  return_code
	fi
}

#################################################################
# function: find_need_place
#################################################################

find_need_place() {

	if [ -f $design.clb ]; then
		need_place="true"
	elif [ $file_prop_exists = "true" ]; then
		need_place="true"
	else
		need_place="false"
	fi
}

#################################################################
# function: process_PLD
#################################################################

process_PLD() {

	for pld_file in `grep 'property pld' $head/$leaf.edif | cut -d'"' -f2 | sort -u`
            do
              	out_fn=`basename $pld_file .xnf`
	      	gen_sch8_run $pld_file $out_fn 
            done
}

#################################################################
# function: process_ABEL
#################################################################

process_ABEL() {


       # .abl file exists; see if newer than .xas file

        run_abl2xnf="false"

        if [ -f $out_fn.xas ] ; then
  		both_files=`ls -c $out_fn.abl $out_fn.xas`
		young_one=`echo $both_files | cut -f1 -d" "`

		if [ $young_one = $out_fn.abl ]; then
                    	run_abl2xnf="true"
                fi
        else
		run_abl2xnf="true"
        fi
  
        if [ $run_abl2xnf = "true" ]; then
		abl2xnf_run
        fi
}

#################################################################
# function: ABEL_gensch_1
#################################################################

ABEL_gensch_1() {

	run_gen_sch8="false"

        if [ -f $out_fn/schematic.mgc_schematic.attr ] ; then
        	both_files=`ls -c $out_fn.xas $out_fn/schematic.mgc_schematic.attr`
                young_one=`echo $both_files | cut -f1 -d" "`
                if [ $young_one = $out_fn.xas ]; then
                    run_gen_sch8="true"
                fi
        else
                run_gen_sch8="true"
        fi

        if [ $run_gen_sch8 = "true" ]; then
		if [ $orgsch_opt = "true" ]; then
			gen_sch8_run $out_fn.xas $out_fn
		fi
	fi

}

#################################################################
# function: ABEL_gensch_2
#################################################################

ABEL_gensch_2() {

	run_gen_sch8="false"

        if [ -f $out_fn/schematic.mgc_schematic.attr ] ; then
		both_files=`ls -c $out_fn.xnf $out_fn/schematic.mgc_schematic.attr`
                young_one=`echo $both_files | cut -f1 -d" "`
                if [ $young_one = $out_fn.xnf ]; then
                    run_gen_sch8="true"
                fi
        else
                run_gen_sch8="true"
        fi

        if [ $run_gen_sch8 = "true" ]; then
		if [ $orgsch_opt = "true" ]; then
			gen_sch8_run $out_fn $out_fn
		fi
	fi
}

#################################################################
# function: process_FILE_and_PLD_prop
#################################################################

process_FILE_and_PLD_prop() {

  # See if there's any FILE= properties on any symbols.

  file_prop_exists="false"

  if [ -f $design.edif ] ; then

        for xnf_file in `grep 'property file' $head/$leaf.edif | cut -d'"' -f2 | sort -u`
            do
	      file_prop_exists="true"
              #out_fn=`basename $xnf_file .xnf`
              out_fn=`echo $xnf_file | cut -f1 -d.`
              ext=`echo $xnf_file | cut -f2 -d.`

              # If no extension appears in $xnf_file, then $out_fn = $ext

	      if [ -f $out_fn.abl ] ; then
	      	process_ABEL
	      fi

              if [ -f $out_fn.xas ] ; then
		ABEL_gensch_1
              elif [ -f $out_fn.xnf ] ; then
		ABEL_gensch_2
              fi
 
            done

	if [ $orgsch_opt = "true" ]; then
		process_PLD
	fi

  fi # if edif file exists

}

#################################################################
# function: check_family
#################################################################

check_family() {


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


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
}

#################################################################
# function: start_tail_process
#################################################################

start_tail_process() {

#
# function: if verbose option is specified then start the tail process.
# input: fncsim8.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

}

#################################################################
# function: pld_dve_sim_run
#################################################################

pld_dve_sim_run() {

      program="pld_dve_sim"
      echo "pld_dve_sim $1 $2"
      echo "pld_dve_sim $1 $2" >> $sh_file 2>&1
      echo "pld_dve_sim $1 $2" >> $log_file 2>&1
      pld_dve_sim $1 $2 >> $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
}

#################################################################
# function: xnfmerge_run
#################################################################

xnfmerge_run() {

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

#################################################################
# function: xnfprep_run
#################################################################

xnfprep_run() {

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

#################################################################
# function: xnfmap_run
#################################################################

xnfmap_run() {

     program="xnfmap"
     echo "xnfmap $design.xtf $design.map"
     echo "xnfmap $design.xtf $design.map" >> $sh_file 2>&1
     echo "xnfmap $design.xtf $design.map" >> $log_file 2>&1
     xnfmap $design.xtf $design.map >> $log_file 2>&1
     ret=$?
     if [ $ret -ne 0 ]; then
            return_code
     fi
}

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

cp_run() {

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

#################################################################
# function: map2lca_run
#################################################################

map2lca_run() {

      program="map2lca"
      echo "map2lca $design.map $design.lca"
      echo "map2lca $design.map $design.lca" >> $sh_file 2>&1
      echo "map2lca $design.map $design.lca" >> $log_file 2>&1
      map2lca $design.map $design.lca >> $log_file 2>&1
      ret=$?
      if [ $ret -ne 0 ] ; then
              return_code
      fi
}

#################################################################
# function: apr_run
#################################################################

apr_run() {

      program="apr"
      echo "apr -w $design.lca $design_fnc.lca"
      echo "apr -w $design.lca $design_fnc.lca" >> $sh_file 2>&1
      echo "apr -w $design.lca $design_fnc.lca" >> $log_file 2>&1
      apr -w $design.lca $design_fnc.lca >> $log_file 2>&1
      ret=$?
      if [ $ret -ne 0 ]; then
              return_code
      fi
}

#################################################################
# function: ppr_run
#################################################################

ppr_run() {

       program="ppr"
       echo "ppr $1 outfile=$leaf_fnc placer_effort=1 route=FALSE"
       echo "ppr $1 outfile=$leaf_fnc placer_effort=1 route=FALSE" >> $sh_file 2>&1
       echo "ppr $1 outfile=$leaf_fnc placer_effort=1 route=FALSE" >> $log_file 2>&1
       ppr $1 outfile=$leaf_fnc placer_effort=1 route=FALSE >> $log_file 2>&1
       ret=$?
       if [ $ret -ne 0 ]; then
              return_code
       fi
}

#################################################################
# function: lca2xnf_run
#################################################################

lca2xnf_run() {

        program="lca2xnf"
        echo "lca2xnf -g -u $design_fnc.lca $design_fnc.xnf"
        echo "lca2xnf -g -u $design_fnc.lca $design_fnc.xnf" >> $sh_file 2>&1
        echo "lca2xnf -g -u $design_fnc.lca $design_fnc.xnf" >> $log_file 2>&1
        lca2xnf -g -u $design_fnc.lca $design_fnc.xnf >> $log_file 2>&1
        ret=$?
        if [ $ret -ne 0 ]; then
              return_code
        fi
}


#################################################################
# function: lca2xnf_run_2
#################################################################

lca2xnf_run_2() {

         program="lca2xnf"
         echo "lca2xnf -v -g -u $design_fnc.lca $design_fnc.xnf"
         echo "lca2xnf -v -g -u $design_fnc.lca $design_fnc.xnf" >> $sh_file 2>&1
         echo "lca2xnf -v -g -u $design_fnc.lca $design_fnc.xnf" >> $log_file 2>&1
         lca2xnf -v -g -u $design_fnc.lca $design_fnc.xnf >> $log_file 2>&1
         ret=$?
         if [ $ret -ne 0 ]; then
              return_code
         fi
}

#################################################################
# function: quicksim_run
#################################################################

quicksim_run() {

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

#################################################################
# function: quicksim_run
#################################################################

quicksim_display() {

	echo ""
	echo "Unit delay model has been succesfully generated."
	echo "Please run QuickSim on $1 design for Functional Simulation."
	echo "Please use the following command:"
	echo "   quicksim $1"
		
	echo "" >> $log_file 2>&1
	echo "Unit delay model has been succesfully generated." >> $log_file 2>&1
	echo "Please run QuickSim on $1 design for Functional Simulation." \
		     >> $log_file 2>&1
	echo "Please use the following command:" >> $log_file 2>&1
	echo "   quicksim $1" >> $log_file 2>&1
}

#################################################################
# function: rm_run
#################################################################

rm_run() {

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

#################################################################
# function: mkdir_run
#################################################################

mkdir_run() {

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

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

mv_run() {

        program="mv"
	echo "mv -f $1 $2"
	echo "mv -f $1 $2" >> $sh_file 2>&1
	echo "mv -f $1 $2" >> $log_file 2>&1
	mv -f $1 $2 >> $log_file 2>&1
	ret=$?
	if [ $ret -ne 0 ]; then
	      return_code
	fi
}
#################################################################
# function: mv_run_2
#################################################################

mv_run_2() {

	if [ ! $leaf_fnc.lca = $design_fnc.lca ] ; then
	      program="mv"
	      echo "mv -f $leaf_fnc.lca $design_fnc.lca"
	      echo "mv -f $leaf_fnc.lca $design_fnc.lca" >> $sh_file 2>&1
	      echo "mv -f $leaf_fnc.lca $design_fnc.lca" >> $log_file 2>&1
	      mv -f $leaf_fnc.lca $design_fnc.lca >> $log_file 2>&1
	      ret=$?
	      #if [ $ret -ne 0 ]; then
	      #  return_code
	      #fi
	fi
}

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

delete_object_run() {

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

#################################################################
# function: xblox_run
#################################################################

xblox_run() {

    program="xblox"
    echo "xblox $design.xtg simdir=simdir sim=xnf"
    echo "xblox $design.xtg simdir=simdir sim=xnf" >> $sh_file 2>&1
    echo "xblox $design.xtg simdir=simdir sim=xnf" >> $log_file 2>&1
    xblox $design.xtg simdir=simdir sim=xnf >> $log_file 2>&1
    ret=$?
    if [ $ret -ne 0 ]; then
      return_code
    fi
}
 
#################################################################
# function: xblox_run_2
#################################################################

xblox_run_2() {

     program="xblox"
     echo "xblox $design.xtg mergeio=false archopt=false outfile=$design_fnc.xnf"
     echo "xblox $design.xtg mergeio=false archopt=false outfile=$design_fnc.xnf" >> $sh_file 2>&1
     echo "xblox $design.xtg mergeio=false archopt=false outfile=$design_fnc.xnf" >> $log_file 2>&1
     xblox $design.xtg mergeio=false archopt=false outfile=$design_fnc.xnf >> $log_file 2>&1
     ret=$?
     if [ $ret -ne 0 ]; then
         return_code
     fi
}

#################################################################
# function: xblox_run_3
#################################################################

xblox_run_3() {

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

#################################################################
# function: xbloxgs_run
#################################################################

xbloxgs_run() {

    program="xblxgs"
    echo "xblxgs $leaf $head/simdir/$leaf.xgs -w -d simdir"
    echo "xblxgs $leaf $head/simdir/$leaf.xgs -w -d simdir" >> $sh_file 2>&1
    echo "xblxgs $leaf $head/simdir/$leaf.xgs -w -d simdir" >> $log_file 2>&1
    xblxgs $leaf $head/simdir/$leaf.xgs -w -d simdir >> $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
    elif [ "$this_cpu" = "hppa" ] ; then
       awk_cmd=awk
    elif [ "$this_cpu" = "apollo" ] ; then
       awk_cmd=nawk
    else
      awk_cmd=awk
    fi
}

#################################################################
# function: process_option_o
#################################################################

process_option_o() {

#
# If the user has specified -o option then it is a schematic only
# design.
#
# 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 [ ! -f $design.bmf ]; then

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

  else

    xnfmerge_run
    xnfprep_run $design.xff $design.xtg

    if [ $regen_opt = "true" ]; then
	rm_run
    fi 

    if [ ! -d simdir ]; then
	mkdir_run
    else

      if [ -d simdir/$leaf ] ; then
	delete_object_run simdir/$leaf
      fi

      for xsys_file in `ls simdir | grep -v mgc_component.attr | grep xsysc`
	do
          if [ -d simdir/$xsys_file ] ; then
		delete_object_run simdir/$xsys_file

	  fi
	done

      for bsm_file in `ls simdir | grep -v mgc_component.attr | grep bsm | grep -v xnf`
	do
	  if [ -d simdir/$bsm_file/$bsm_file ] ; then
		delete_object_run simdir/$bsm_file/$bsm_file
	  fi
	done
    fi

    xblox_run
    xbloxgs_run
    get_awk_cmd
 
    for xnf_file in `$awk_cmd -f $LCA/com/$this_cpu/get_file.nawk < $head/simdir/$leaf.xgs | sort -u`

      do

        out_fn=`basename $xnf_file .xnf`
	if [ -f simdir/$xnf_file ] ; then
	  run_gen_sch8="false"
	  if [ -f simdir/$out_fn/schematic.mgc_schematic.attr ] ; then
            both_files=`ls -c simdir/$xnf_file simdir/$out_fn/schematic.mgc_schematic.attr`
            young_one=`echo $both_files | cut -f1 -d" "`
            if [ $young_one = $xnf_file ]; then
              run_gen_sch8="true"
            fi
          else
            run_gen_sch8="true"
          fi  

          if [ $run_gen_sch8 = "true" ]; then
		gen_sch8_run simdir/$xnf_file simdir/$out_fn
	  fi
	fi
 
      done

    for bsm_file in `ls simdir | grep -v mgc_component.attr | grep bsm | grep -v xnf`
      do
	if [ -f simdir/$bsm_file.mgc_component.attr ] ; then
	  if [ ! -f simdir/$bsm_file.xnf ] ; then
		delete_object_run simdir/$bsm_file
	  fi
	fi
      done

    pld_dve_sim_run $head/simdir/$leaf $tech

  fi
}

#################################################################
# function: option_g_4k
#################################################################

option_g_4k() {

	if [ $need_place = "true" ]; then
		option_g_4k_place
	else
		option_g_4k_no_place
	fi
}

#################################################################
# function: option_g_4k_no_place
#################################################################

option_g_4k_no_place() {

	if [ -f $design.bmf ]; then
	    # blox macros found
	    xnfprep_run $design.xff $design.xtg
	    xblox_run_2
	else
	    mv_run $design.xff $design_fnc.xnf
	fi 
}

#################################################################
# function: option_g_4k_place
#################################################################

option_g_4k_place() {

	if [ -f $design.bmf ]; then
	    # blox macros found
	    xnfprep_run $design.xff $design.xtg
	    xblox_run_2
	    xnfprep_run $design_fnc.xnf $design.xtf
	else
	    xnfprep_run $design.xff $design.xtf
	fi 

	place_4k
}

#################################################################
# function: lca2xnf_run_gen
#################################################################

lca2xnf_run_gen() {

	if [ -f $design.ulf ] ; then
	    lca2xnf_run
	else
	    lca2xnf_run_2
	fi
}

#################################################################
# function: place_4k
#################################################################

place_4k() {

	ppr_run $design.xtf
	mv_run_2
	lca2xnf_run_gen
}

#################################################################
# function: place_3k
#################################################################

place_3k() {

	xnfmap_run

        if [ $a_part = FALSE ] ; then
	    map2lca_run
	    #apr_run
	    mv_run $design.lca $design_fnc.lca
        else
	    # A/L part type
	    ppr_run $design.map
	    mv_run_2
        fi 

	lca2xnf_run_gen
}

#################################################################
# function: option_g_3k
#################################################################

option_g_3k() {

	if [ $need_place = "true" ]; then
		option_g_3k_place
	else
		option_g_3k_no_place
	fi
}

#################################################################
# function: option_g_3k_place
#################################################################

option_g_3k_place() {

	if [ -f $design.bmf ]; then
	    # blox macros found
	    xnfprep_run $design.xff $design.xtg
	    xblox_run_3
	    xnfprep_run $design.xg $design.xtf
	else
	    xnfprep_run $design.xff $design.xtf
	fi 

	place_3k
}

#################################################################
# function: option_g_3k_no_place
#################################################################

option_g_3k_no_place() {

	if [ -f $design.bmf ]; then
	    # blox macros found
	    xnfprep_run $design.xff $design.xtg
	    xblox_run_3
	else 
	    mv_run $design.xff $design_fnc.xnf
	fi
}

#################################################################
# function: process_option_g
#################################################################

process_option_g() {

  	xnfmerge_run
	find_need_place

        # If it is a 4K design

        if [ \( $family -eq 4 \) -o \( $family -eq 5 \) ] ; then
          # it is a 4K design
	  option_g_4k	
	else 	
	  # For 2K & 3K
	  option_g_3k	
 	fi 
		
	# if $design_fnc eddm dir already exists, then delete it	
	if [ -d $design_fnc ]; then
	   delete_object_run $design_fnc
	fi
		
	# run gen_sch8 on $design_fnc.xnf file
	gen_sch8_run_2
		
	# run pld_dve_sim script on $design_fnc
	pld_dve_sim_run $design_fnc $tech	
}

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

#################################################################
#  print_header
#################################################################

cat <<EOS

               FNCSIM8 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_variables
###################################################################

orgsch_opt="false"
gensch_opt="false"
quick_opt="false"
regen_opt="false"
verbose_opt="false"
tail_flag="false"
help_ret=0
ret=0
err_ret=0
err_opt=" "
set_return_code="true"

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

if [ $# -lt 1 ] ; then
  set_return_code="false"
  help
  #help_ret=1
fi

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 = "-q" ]; then
	quick_opt="true"
    shift
  elif [ $1 = "-Q" ]; then
	quick_opt="true"
    shift
  elif [ $1 = "-r" ]; then
	regen_opt="true"
    shift
  elif [ $1 = "-R" ]; then
	regen_opt="true"
    shift
  elif [ $1 = "-verbose" ]; then
    verbose_opt="true"
    shift
  elif [ $1 = "-VERBOSE" ]; then
    verbose_opt="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
        shift
  else
    design=$1
    shift
  fi

done  # END of scan off command line arguments

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

strip_hierarchy
reset_MGC_WD
create_log
create_log_2

check_env_vars
check_options

get_part
check_options_2

process_FILE_and_PLD_prop

##################################
# define variables
# design_fnc: Unit delay design model. Used for functional simulation.

design_fnc=$design\_fnc
leaf_fnc=$leaf\_fnc
##################################

check_family
start_tail_process

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

if [ $orgsch_opt = "true" ]; then
	process_option_o
fi

if [ $gensch_opt = "true" ]; then
	process_option_g
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_fnc.
###################################################################

if [ $orgsch_opt = "true" ]; then

	# if -q switch is present then run quicksim on $design
	
	if [ $quick_opt = "true" ]; then

          if [ -f $design.bmf ]; then 
		quicksim_run $head/simdir/$leaf
          else
		quicksim_run $design	
          fi
	
	else
	
          if [ -f $design.bmf ]; then
		quicksim_display simdir/$leaf
          else
		quicksim_display $design
          fi
	fi

elif [ $gensch_opt = "true" ]; then

	# if -q switch is present then run quicksim on $design_fnc
	
	if [ $quick_opt = "true" ]; then

		quicksim_run $design_fnc		
	else
		quicksim_display $design_fnc		
	fi
fi

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

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