#!/bin/csh -f
#
#****************************************************************
#  This program is the Confidential and Proprietary product of 
#  Cadence Design Systems.  Any unauthorized use, reproduction 
#  or transfer of this program is strictly prohibited. 
#  
#  Copyright (c) 1989 - 1994, Cadence Design Systems, Inc. 
#  All rights reserved. 
#  
#  Filename:  funcnetx 
#  Based on:  Cadence funcnet v5.4
#  Purpose:   
#  Author:    nasir
#  Date:      March 1994
#
#  
#  History:   
#  Nasir Junejo 10/19/94
#  5.1 if extension is xff then don't run x2vprep and xnfmerge.
#      run xblox with archopt=FALSE
#  Nasir 12/8/94
#  5.2  Changed PWD variable to cwd. PWD does not work on hp.   
#
#  funcnetx
#  5.4.1
#  4/4/95 Karlton Lau 
#         Modified to support Xilinx standalone Verilog-XL 
#         simulation only
#  8/9/95 Karlton Lau  (v5.4.1b)
#	  Modified:
#	  if ( $?partname ) then
#	     set cmd = "x2vprep -p $partname $design.$ext $design.xcd"
#                                                         ^^^^^^^^^^^
#                                                   added this argument
#  8/15/95 Karlton Lau
#         Modified to add ignore_timespec option to xnfprep (5.4.1c)
#
#  10/23/95 Karlton Lau
#          Modified to add support for 4000E and 5200 devices
#
#  01/03/96 Karlton Lau  v5.5a
#          Modified to match funcnet: copy .xcd from run directory
#	   out to working directory before running xnf2verilog
#
#  01/11/96 Karlton Lau  v5.5b
#          Modified to copy .xcd from run directory
#	   out to working directory before running xnf2verilog
#          only if run directory is different from working dir.
#          Also fixed bug 27832. 
#
#
#***************************************************************/

set debug = 1

###################################################################
# Define UNIX programs
###################################################################

set rm = /bin/rm
set mv = /bin/mv
set touch = /bin/touch
###################################################################
# Create log file
###################################################################

echo "*****************************************************************"
echo "*  Copyright (C) 1994                                           *"
echo "*  Cadence Design Systems Inc.  All Rights Reserved.            *"
echo "*  funcnetx 5.5b                                                *"
echo "*****************************************************************"
echo

set logfile = $cwd/funcnetx.log

set program = $rm
$rm -f $logfile
set ret = $status
if ( ! $ret == 0 ) then
  goto return_code
endif
set program = $touch
$touch $logfile
set ret = $status
if ( ! $ret == 0 ) then
  goto return_code
endif


echo "*****************************************************************" > $logfile
echo "*  Copyright (C) 1994                                           *" >> $logfile
echo "*  Cadence Design Systems Inc.  All Rights Reserved.            *" >> $logfile
echo "*  funcnetx 5.5b                                                 *" >> $logfile
echo "*****************************************************************" >> $logfile
echo >> $logfile

set program = funcnetx
if ( $1 == "-h"  || $1 == "-help" || $1 == "") then
  echo ""
  set ret = 0
  goto usage
endif
 
set interface_dir = ""

if (  ${?CDS_INTFC} ) then
    set interface_dir_set = 1
    set interface_dir = $CDS_INTFC
    echo ""
    echo "   The CDS_INTFC environment variable has been set to"
    echo "   $interface_dir"
    echo "" 

    echo "" >> $logfile
    echo "   CDS_INTFC environment variable has been set to" >> $logfile
    echo "   $interface_dir" >> $logfile
    echo "" >> $logfile
else
  if ( ${?debug} ) then
    echo ""
    echo "   The CDS_INTFC environment variable has not been defined,"
    echo "   so the pin file and Verilog library should be specified"
    echo "   on the command line."
    echo ""
    echo "" >> $logfile
    echo "   The CDS_INTFC environment variable has not been defined,"  >> $logfile
    echo "   so the pin file and Verilog library should be specified" >> $logfile
    echo "   on the command line." >> $logfile
    echo "" >> $logfile
    set interface_dir_set = 0
    goto usage
  endif
endif


if ( $#argv < 2 ) then
  echo ""
  echo "ERROR: funcnetx requires minimum of two arguments: design_name, and  architecture" 
  echo ""
  set ret = 1
  goto usage
endif


set x2coption = ""
set options = ""



set design = $1:r
set designf = ${design}f
set arch   = $2
set xrundir = "$cwd" 
set curdir  = $cwd
set x2vprep = 1
set merge = 1
set pinfile_specified = 0
set pinfile_exists = 1	  

if ( $1:e == "" ) then
  set ext = "xnf"
else
  set ext = $1:e
endif


if ( $arch == "4000E" ) then
   begin
      if ( $debug == 1 ) then
 	 echo "Converting 4000E to 4000e..."
      endif
      set arch = "4000e"
   end
endif

if ( "$arch" != "7000" && "$arch" != "2000" && "$arch" != "3000" && "$arch" != "4000" && "$arch" != "4000e" && "$arch" != "5200" ) then
  echo ""
  echo "ERROR: Invalid architecture $arch" >> $logfile
  echo ERROR: Invalid architecture $arch
  exit 1
endif
 
if ( $arch == "7000" ) then
   echo "ERROR: funcnetx can not be run on 7000 designs."
   echo "ERROR: funcnetx can not be run on 7000 designs." >> $logfile
   echo "       Run xemake and timenet to simulate your design."
   echo "       Run xemake and timenet to simulate your design." >> $logfile
   exit 1
endif

# Determine if the design is from Synopsys


if (  { grep -s "^PROG, [S,s][Y,y][N,n][2][X,x][N,n][F,f]" $design.$ext } | { grep -s "PROG, Synopsys" $design.$ext } ) then

   echo "** ERROR ** Post-synthesis functional simulation of Synopsys " 
   echo "            designs is not supported." 
   exit 1
endif

shift
shift
while ($#argv)
  switch ($1)
	case -pin:
		shift
		set pinfile = "$1"
		set pinfile_specified = 1
		set x2coption = "$x2coption -pin $1"
		breaksw
	case -vlibs:
		shift
		set vlibs = "$1"
		set x2coption = "$x2coption -vlibs $1"
		breaksw
	case -x:
	    	set startAtXnf = 1
		breaksw
        case -noio:
                set x2vprep = 0 
                breaksw
 	case -r:
 		shift
 		set xrundir = $1
 		if ( $xrundir == "." ) then
 		   set xrundir = "$cwd"
 		endif
 		breaksw
	case -save:
		set savefiles = 1
		breaksw
	default:
		echo "ERROR: option not recognized $1" >> $logfile
		echo "ERROR: option not recognized $1"
		set ret = 1
		goto usage
   	endsw
   	shift
end


if ( $arch != "7000" && $arch != "2000" && $arch != "3000" && $arch != "4000" && "$arch" != "4000e" && "$arch" != "5200" ) then
  echo "ERROR: Invalid architecture $arch" >> $logfile
  echo ERROR: Invalid architecture $arch
  goto usage
endif

# If the pinfile has not been specified, see if you can 
# find it based on environment variable


# Modify 5/17/95

   if (($pinfile_specified == 0 ) && ( $interface_dir == "" ) ) then 

    #   echo "line 196 goto pinfile_not_found"
       goto pinfile_not_found
    else
    if (($pinfile_specified == 1) && ! ( -e $CDS_INTFC/data/xc${arch}.pin ) )  then
       echo " goto pinfile_not_found"
       set pinfile_exists = 0
       goto pinfile_not_found
   else
      echo "CDS_INTFC = $CDS_INTFC"
      set interface_dir = $CDS_INTFC
      set x2coption = "$x2coption -pin $interface_dir/data/xc$arch.pin"
      set x2coption = "$x2coption -vlibs $interface_dir/verilog$arch"
   endif

set x2coption = "$x2coption  -arch $arch"

if ( ! -d $xrundir ) then
  echo "ERROR: directory $xrundir does not exist." >> $logfile
  echo ERROR: directory $xrundir does not exist.
  exit 1
endif
#

if ( ! -e $xrundir/$design.$ext ) then
  echo "ERROR: $design.$ext file does not exist." >> $logfile
  echo ERROR: $design.$ext file does not exist.
  exit 1
endif

###################################################################
# Startup messages
###################################################################

echo ""
echo "Design: 		$design"
echo "Design: 		$design" >> $logfile
echo "Output Design: 		$designf" 
echo "Output Design: 		$designf" >> $logfile
echo "Xilinx Run Directory:   $xrundir"
echo "Xilinx Run Directory:     $xrundir" >> $logfile

if ( $debug == 2 ) then
   echo options =  $options ...
endif

if ( "$options" != "" ) then
	echo "Options:      $options"
		echo "Options:      $options" >> $logfile
endif
echo "Log File:		$logfile:t"
echo "Log File:		$logfile:t" >> $logfile
echo ""
echo "" >> $logfile
cd $xrundir

###################################################################
# See if xnf file has  part type or else use default part type.
###################################################################
 if ( ! { grep -s '^PART' $design.$ext } ) then
    switch ($arch)
	case 2000:
          set partname = "2018PC44"
          breaksw 
	case 3000:
          set partname = "3020PC68"
          breaksw 
	case 4000:
          set partname = "4003PC84"
          breaksw 
	case 4000e:
	  set partname = "4003EPC84"
	  breaksw
	case 5200:
	  set partname = "5202PC84"
	  breaksw
	case 7000:
          set partname = "73108-10PC84"
          breaksw 
        default:
   endsw 
endif

###################################################################
# function: Run xnfmerge
# input: design
# output: design.xff
###################################################################

# Save original file.
set saveext = $ext
cp $design.$ext $design.$ext.save 

if ( $ext == "xff" ) then
   set merge = 0
endif

if ( $?partname ) then
   set cmd = "x2vprep -p $partname $design.$ext $design.xcd"
else 
   set cmd = "x2vprep $design.$ext $design.xcd"
endif

if ( $x2vprep == 1 ) then
   set program = "x2vprep"
   echo "Running x2vprep..."
   echo "Running x2vprep..." >> $logfile
   echo "$cmd" >>& $logfile
   $cmd >>& $logfile
   set ret = $status 
   if ( ! $ret == 0 ) then
      goto return_code
   endif
endif

if ( $merge == 1 ) then
   set program = "xnfmerge"
   echo "Running xnfmerge..."
   echo "Running xnfmerge..." >> $logfile
   if ( $?partname ) then
      set cmd = "xnfmerge -p $partname $design.xcd $design.xff"
   else 
      set cmd = "xnfmerge $design.xcd $design.xff"
   endif

   if($debug == 2) then
     echo "$cmd"
   endif

   echo "$cmd" >>& $logfile
   $cmd >>& $logfile
   set ret = $status

 #  if ( $debug == 1 ) then
 #    more $design.mrg
 #  endif

   # Can't rely on xnfmerge's return status code.
   if ( { grep -s ERROR $design.mrg } ) then
      set ret = 1 
   endif

   if ( $ret != 0 ) then
      goto return_code
   endif
endif
   
set ext = "xff"


if ( $debug == 2 ) then
  echo ext = "$ext" after xnfmerge
endif

###################################################################
# function: Run xnfprep and xblox
# input: design.xff
# output: design.xg
###################################################################
if ( { grep -s BLOX $design.$ext } ) then
   set xblox = 1
endif

if ( $?xblox ) then
   set program = "xnfprep"
   echo "Running xnfprep..."
   echo "Running xnfprep..." >> $logfile
   echo "xnfprep $design ignore_timespec=all" >>& $logfile
   xnfprep $design ignore_timespec=all >>& $logfile
   set ret = $status 
   if ( ! $ret == 0 ) then
      goto return_code
   endif

   set ext = "xtg"
endif

if ( $?xblox ) then

   set program = "xblox"
   echo "Running xblox..."
   echo "Running xblox..." >> $logfile
   xblox $design  >>& $logfile
   set ret = $status
   if ( ! $ret == 0 ) then
      goto return_code
   endif
 
   set ext = "xg"
endif

###################################################################
# function: x2vprep, fix any problems in the xnf file.
# input: design.xnf or design.xg
# output: designf.xcd
###################################################################
set program = "x2vprep"
echo "Running x2vprep..."
echo "Running x2vprep..." >> $logfile

if ( $debug == 2 ) then
  echo ext = "$ext" before running x2vprep after xnfmerge
endif

if ( $debug == 2 ) then
 echo "x2vprep ${design}.${ext} ${design}f.xcd" 
endif

 echo "x2vprep ${design}.${ext} ${design}f.xcd" >> $logfile
 x2vprep ${design}.${ext} ${design}f.xcd >> $logfile

set ret = $status
if ( ! $ret == 0 ) then
   goto return_code
endif

set ext = "xcd"

if($curdir != $xrundir) then
  cp ${design}f.$ext $curdir/$designf.$ext >& /dev/null
  cd $curdir
endif

if($debug == 2) then
   echo curdir = $curdir
endif

###################################################################
# function: convert XNF to Verilog simulation-
# input: design.xnf
# output: design.v, design.sdf, design.stim
###################################################################

set program = "xnf2verilog"
echo "Running xnf2verilog..."
echo "Running xnf2verilog..." >> $logfile
if ($debug == 2) then
  echo "xnf2verilog $designf.$ext  $designf  $x2coption" 
endif
echo "xnf2verilog $designf.$ext  $designf  $x2coption" >>& $logfile
xnf2verilog ${design}f.$ext  ${design}f  $x2coption |& tee -a $logfile
set ret = $status

if ( $ret != 0 ) then
  cat xnf2verilog.log >> $logfile
endif

goto return_code



pinfile_not_found:

	    
         echo ""
         echo "  Pin file not found in $interface_dir/data. " 
         echo ""
	 if ( $pinfile_exists == 0 ) then
	 exit 1
	 else
           echo "  Please do 1 of the following:"
           echo ""
           echo "  a. define an environment variable, \"CDS_INTFC\" 
           echo "     pointing to the directory containing the" 
           echo "     Verilog libraries, or "
           echo ""
           echo "  b. specify the explicit path to the appropriate" 
           echo "     pin file using the -pin option, and specify "
           echo "     the explicit path to the Verilog libraries using"
           echo "     the -vlibs option. " 
           echo ""
           echo "     Example:  -pin /tools/xact/data/xc4000.pin"
           echo "               -vlibs /tools/xact/verilog4000"
           echo ""
           exit 1
	 endif

###################################################################
usage:
set tmpFile = /tmp/funcnetx_$$
cat << EOFHELP  > $tmpFile
 
This program is used to create a Verilog-XL functional simulation model. 
It handles designs with FILE= properties as well as X-BLOX modules. 
Its input is usually an XNF file(s) that has not been run through 
XNFMERGE.  If the design has been through XNFMERGE, then the .XFF 
extension must also be specified in the input design name. 

FUNCNETX adds I/O records and ibufs/obufs to nets that do not have 
i/o buffers on them by default.  


Usage: funcnetx  XNF_file  <2000 | 3000 | 4000 | 4000e | 5200 | 7000> 
       [ -pin <path_to_pinfile> ] [ -vlibs <path_to_libraries> ]
       [ -noio ][ -save ] [ -r <run_directory ]


Example:  funcnetx foo 4000


XNF_file            - gate-level XNF|XFF file. 

2000|3000|4000|4000e|5200|7000 - is the target architecture family of your design.

-noio              - Do not add ibuf/obuf and EXT records. (Skip x2vprep)

-save              - Save intermediate files.

-pin		   - Use specified pin file.
		     This is not needed if the variable CDS_INTFC is 
		     declared in your environment.

-vlibs		   - Use specified Verilog libraries.
		     This is not needed if the variable CDS_INTFC is 
		     declared in your environment.

-r		   - Use specified run directory.  The default is the 
		     current working directory.

If the environment variable CDS_INTFC pointing to the install directory 
for the Xilinx interface to Verilog has been declared with a setenv 
statement (this is recommended), the location of the pin file will be set to 

	\$CDS_INTFC/data/xcXXXXX.pin, 
	
and the location of the appropriate library will be assumed to be 

	\$CDS_INTFC/verilogXXXX, 

where XXXX is either 2000, 3000, 4000, 4000e, 5200, or 70000.

EOFHELP

more $tmpFile
$rm $tmpFile
exit 0


return_code:

  cd $curdir

  if ( $ret == 0 && ! $?savefiles ) then
    $rm $xrundir/$design.{bxa,blx,xg,xcd,xff,xtg,mrg,prx,prp} >& /dev/null
    $rm $xrundir/xnfprep.log xnf2verilog.info $xrundir/xblox.log >& /dev/null
    $rm $designf.$ext >& /dev/null
  endif

  $mv ${xrundir}/${design}.${saveext}.save $xrundir/$design.${saveext}
  # Touch the top xnf file so that xemake will rerun xnfmerge and xnfprep.
  $touch $xrundir/$design.$saveext

  if ( $ret != 0 ) then
    echo "Errors found while running $program. Check the $logfile:t file."
  else
    echo ""
    echo "funcnetx finished. All output piped to $logfile:t file."
    endif
  endif
  exit $ret



