#!/bin/csh -f

# %W% %H% %T%
echo "Xilinx:xepldsim:XACT-CPLD-V6.0.0b" | tee xepldsim.log
echo '(c)' Copyright 1995 Xilinx Inc.  All rights reserved.  | tee -a xepldsim.log

set DESIGNNAME=""
set EXITSTATUS=0
set SIMULATOR=""

if (( $#argv == 0 ) || ("$1" == "-h") || ("$1" == "-help")) then
HELP:
    set Program_Name=$0
    set Program_Name=$Program_Name:t
cat << EOF  | tee -a xepldsim.log

The xepldsim command generates the simulation models for various simulators.

Usage:  xepldsim [options] <designname>

Options:
   -mentor		Mentor timing simulation
   -vlfunc		Viewlogic functional simulation
   -vltime		Viewlogic timing simulation
   -verilog		Verilog simulation
   -vss			VSS simulation

EOF
    goto DONE
endif

while ( $#argv )
    switch ( "$argv[1]" )
 
        case -mentor:
            set SIMULATOR="MENTOR"
        breaksw

        case -vlfunc:
            set SIMULATOR="VLFUNC"
        breaksw

        case -vltime:
            set SIMULATOR="VLTIME"
        breaksw

        case -verilog:
            set SIMULATOR="VERILOG"
        breaksw

        case -vss:
            set SIMULATOR="VSS"
        breaksw

        case -*:
            echo "Warning: Unrecognized option "$argv[1]" ignored." | tee -a xepldsim.log
        default:
            set DESIGNNAME=$argv[1]
        breaksw
    endsw
    shift
end

CHECK_SIMULATOR:
if ( $SIMULATOR == "" ) then
    echo "Error: Missing Simulator Option." | tee -a xepldsim.log
    set EXITSTATUS=2
    goto HELP
endif

CHECK_DESIGNNAME:
if ( $DESIGNNAME == "" ) then
    echo "Error: Missing Design Name." | tee -a xepldsim.log
    set EXITSTATUS=2
    goto HELP
endif
if ( $DESIGNNAME != `basename $DESIGNNAME` ) then
    echo "Error: The specified design name "$DESIGNNAME" contains a directory path" | tee -a xepldsim.log
    echo "      The xepldsim command can only run from the current directory." | tee -a xepldsim.log
    set EXITSTATUS=2
    goto DONE
endif

BACKUP:
if ( -e $DESIGNNAME.xnf ) then
    echo "Saving file $DESIGNNAME.xnf to backup directory" | tee -a xepldsim.log
    /bin/cp -p $DESIGNNAME.xnf backup
endif

EXECUTE:
switch ( "$SIMULATOR" )
    case MENTOR:
	echo "cat ${DESIGNNAME}_tim.xnf | sed -e 's/PART,.*/PART, 7/' | sed -e 's/OBUF/BUF/' > xsimtemp.xnf" | tee -a xepldsim.log
	cat ${DESIGNNAME}_tim.xnf | sed -e 's/PART,.*/PART, 7/' | sed -e 's/OBUF/BUF/' > xsimtemp.xnf
	echo "mv xsimtemp.xnf ${DESIGNNAME}_tim.xnf" | tee -a xepldsim.log
	mv xsimtemp.xnf ${DESIGNNAME}_tim.xnf
	echo "gen_sch8 -w ${DESIGNNAME}_tim.xnf" | tee -a xepldsim.log
	gen_sch8 -w ${DESIGNNAME}_tim.xnf
	echo "pld_dve_sim ${DESIGNNAME}_tim xc7000" | tee -a xepldsim.log
	pld_dve_sim ${DESIGNNAME}_tim xc7000
    breaksw
    case VLFUNC:
        echo "xsimmake -f vef6 $DESIGNNAME" | tee -a xepldsim.log
        xsimmake -f vef6 $DESIGNNAME
    breaksw
    case VLTIME:
        echo "/bin/cp -p ${DESIGNNAME}_tim.xnf xsimtime.xnf" | tee -a xepldsim.log
        /bin/cp -p ${DESIGNNAME}_tim.xnf xsimtime.xnf
        echo "xsimmake -f vet6 $DESIGNNAME" | tee -a xepldsim.log
        xsimmake -f vet6 $DESIGNNAME
    breaksw
    case VERILOG:
        set XACT_PATH=(`echo $XACT | tr : ' '`)
        set LIB_PATHS=()
        foreach xact ( ${XACT_PATH} )
            if ( -d $xact/data/verilog7000 ) then
                set LIB_PATHS=$xact
            endif
        end
        echo "cat ${DESIGNNAME}_tim.xnf | sed -e 's/PART,.*/PART, 7/' | sed -e 's/OBUF/BUF/' | sed -e 's/\.\([^0-9]\)/_X\1/g' > xsimtemp.xnf"
        cat ${DESIGNNAME}_tim.xnf |sed -e 's/PART,.*/PART, 7/' | sed -e 's/OBUF/BUF/' | sed -e 's/\.\([^0-9]\)/_X\1/g' > xsimtemp.xnf
        echo "xnf2verilog xsimtemp.xnf $DESIGNNAME -arch 7000 -pin $LIB_PATHS/data/xc7000.pin -vlibs $LIB_PATHS/data/verilog7000 -s ${DESIGNNAME}_tim.sdf" | tee -a xepldsim.log
        xnf2verilog xsimtemp.xnf $DESIGNNAME -arch 7000 -pin $LIB_PATHS/data/xc7000.pin -vlibs $LIB_PATHS/data/verilog7000 -s ${DESIGNNAME}_tim.sdf | tee -a xepldsim.log
	echo mv xsimtemp.v ${DESIGNNAME}_tim.v
	mv xsimtemp.v ${DESIGNNAME}_tim.v
	echo mv xsimtemp.stim ${DESIGNNAME}_tim.stim
	mv xsimtemp.stim ${DESIGNNAME}_tim.stim
    breaksw
    case VITAL:
        echo "/bin/cp -p ${DESIGNNAME}_tim.xnf $DESIGNNAME.xnf" | tee -a xepldsim.log
        /bin/cp -p ${DESIGNNAME}_tim.xnf $DESIGNNAME.xnf
        echo "xnf2vss -v ${DESIGNNAME}" | tee -a xepldsim.log
        xnf2vss -v ${DESIGNNAME}
    breaksw
    case VSS:
        echo "/bin/cp -p ${DESIGNNAME}_tim.xnf $DESIGNNAME.xnf" | tee -a xepldsim.log
        /bin/cp -p ${DESIGNNAME}_tim.xnf $DESIGNNAME.xnf
        echo "xnf2vss ${DESIGNNAME}" | tee -a xepldsim.log
        xnf2vss ${DESIGNNAME}
    breaksw
endsw

DONE:
if ( $EXITSTATUS == 0 ) then
    echo "Execution of the xepldsim command completed." | tee -a xepldsim.log
endif
echo "END of xepldsim.log" >> xepldsim.log
exit $EXITSTATUS
#------------------------- end of xepldsim ----------------------------
