#!/bin/csh -f

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

set BASENAME=""
set CSTFILE=""
set DESIGNNAME=""
set DETAIL=""
set EXTENSION=""
set MULTI="AUTO"
set EXTENSION=""
set EXITSTATUS=0
set FPTERMS=""
set IGNORELOC="OFF"
set IGNORETS="OFF"
set INPUTS=""
set LOWPWR="INDESIGN"
set MRINPUT="INDESIGN"
set NOGCK="INDESIGN"
set NOGSR="INDESIGN"
set NOGTS="INDESIGN"
set NOIFD="INDESIGN"
set NOTA=""
set NOTSPREP=""
set PART="9"
set PINFREEZE="OFF"
set PRELOAD="INDESIGN"
set PTERMS=""
set SIGNATURE=""
set TIMING="ON"
set XNF=""

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

The xepld command reads schematics, netlists or equation based designs 
and fits to a Xilinx EPLD.

Usage:  xepld [options] <designname>

Options:
   -cst	<filename>	Use timing constraints from <filename>.cst
   -detail		Detailed timing report (default is summary report)
   -ignoreloc		Ignore all pin assignments in design
   -ignorets		Ignore all timing specifications in design
   -lowpwr		Low power mode for all logic (default is high-speed)
   -nogck		No Global Clock optimization
   -nogts		No Global 3-State optimization
   -nota		No Timing Analysis Report
   -notiming		Disable general timing optimization
   -p <parttype...>	Target part type; may be one of:
			   9 = any 9500 family device (automatic selection)
			   "dddd[-ss][pppp]" = specific device (dddd),
					       speed (-ss), package (pppp)
                                               use "*" as wildcard
                           "dddd-sspppp,dddd-sspppp,..." = list of preferences
			   INDESIGN = specified in design
			   (default is automatic selection from 9500 family)
   -pinfreeze		Pin-freeze using previous <designname>.gyd
   -pterms <n>		Maximum number of pterms per equation
   -s <signature>	User signature string (default is <designname>)

<designname> can be one of the following formats (in order of precedence):
   Viewlogic schematic (sch/<designname>.1)
   Viewlogic synthesis wirelist (wir/<designname>.1)
   Mentor schematic (<designname>)
   Synopsys FPGA Compiler netlist (<designname>.sxnf)
   Synopsys Design Compiler EDIF netlist (<designname>.sedif)
   XNF netlist (<designname>.xnf)
   EDIF netlist (<designname>.edif)
   Xilinx PLUSASM equation file (<designname>.pld)

EOF
    goto DONE
endif

while ( $#argv )
    switch ( "$argv[1]" )
 
        case -cst:
            shift
            set CSTFILE=$argv[1]
        breaksw

        case -notiming:
            set TIMING="OFF"
        breaksw

        case -detail:
            set DETAIL="ON"
        breaksw

        case -fpterms:
            shift
            set FPTERMS=$argv[1]
        breaksw

        case -ignoreloc:
            set IGNORELOC="ON"
        breaksw

        case -ignorets:
            set IGNORETS="ON"
        breaksw

        case -inputs:
            shift
            set INPUTS=$argv[1]
        breaksw

        case -lowpwr:
            set LOWPWR="ON"
        breaksw

        case -mrinput:
            set MRINPUT="ON"
        breaksw

        case -multi:
            set MULTI="MULTIPLE"
        breaksw

        case -nogck:
            set NOGCK="OFF"
        breaksw

        case -nogsr:
            set NOGSR="OFF"
        breaksw

        case -nogts:
            set NOGTS="OFF"
        breaksw

        case -noifd:
            set NOIFD="OFF"
        breaksw

        case -nota:
            set NOTA=" -nota"
        breaksw

        case -p:
        case -part:
        case -parttype:
            shift
            set PART=$argv[1]:q
        breaksw

        case -pinfreeze:
            set PINFREEZE="ON"
        breaksw

        case -preload:
            set PRELOAD="ON"
        breaksw

        case -pterms:
            shift
            set PTERMS=$argv[1]
        breaksw

        case -s:
            shift
            set SIGNATURE=$argv[1]
        breaksw

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

CHECK_DESIGNNAME:
if ( $DESIGNNAME == "" ) then
    echo "Error: Missing Design Name." | tee -a xepld.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 xepld.log
    echo "      The xepld command can only run from the current directory." | tee -a xepld.log
    set EXITSTATUS=2
    goto DONE
endif

CTL_OPTIONS:
echo > $BASENAME.ctl
if ( $PART:q == "7" ) then
    set PART=${PART}"3*"
else if ( $PART:q == "9" ) then
    set PART=${PART}"5*"
endif
if ( $PART:q == "INDESIGN" ) then
    echo "DEVICE:INDESIGN" >> $BASENAME.ctl
else if ( $PART:q == "" ) then
    echo "DEVICE:"$MULTI >> $BASENAME.ctl
else
    echo "DEVICE:"$MULTI >> $BASENAME.ctl
    echo "DEVICE_OPTIONS:"$PART:q >> $BASENAME.ctl
endif

echo "TIMING_OPT:"$TIMING >> $BASENAME.ctl
echo "IGNORE_PIN_ASSIGNMENT:"$IGNORELOC >> $BASENAME.ctl
echo "IGNORE_TSPEC:"$IGNORETS >> $BASENAME.ctl
if ( $INPUTS != "" ) then
    echo "MC9500_INPUT_LIMIT:"$INPUTS >> $BASENAME.ctl
endif
echo "LOWPWR:"$LOWPWR >> $BASENAME.ctl
echo "FASTCLOCK_OPT:"$NOGCK >> $BASENAME.ctl
echo "FOE_OPT:"$NOGTS >> $BASENAME.ctl
if ( `echo $PART:q | cut -c-1` == "7") then
    echo "MRINPUT:"$MRINPUT >> $BASENAME.ctl
    echo "REGISTER_OPT:"$NOIFD >> $BASENAME.ctl
    echo "PRELOAD_OPT:"$PRELOAD >> $BASENAME.ctl
endif
if ( $PINFREEZE == "ON" ) then
    echo "PIN_FREEZE:"$BASENAME.gyd >> $BASENAME.ctl
endif
if ( $FPTERMS != "" ) then
    echo "MC7000_FPTERMS:"$FPTERMS >> $BASENAME.ctl
endif
if ( $PTERMS != "" ) then
    echo "MC9500_PTERM_LIMIT:"$PTERMS >> $BASENAME.ctl
endif

XEMAKE_OPTIONS:
if ( $CSTFILE != "" ) then
    set CSTFILE=" -cst "$CSTFILE
endif
if ( $PART:q == "INDESIGN" ) then
    set FAMILY=""
else if ( $PART:q == "" ) then
    set FAMILY="9"
else
    set FAMILY=""`echo $PART:q | cut -c-1`
endif
if ( $FAMILY != "9" ) then
    echo "Error: Illegal part type "$PART:q"." | tee -a xepld.log
    set EXITSTATUS=2
    goto HELP
else
    set FAMILY=" -p "$FAMILY
endif
if ( $SIGNATURE != "" ) then
    set SIGNATURE=" -s "$SIGNATURE
endif
if ( $IGNORETS == "ON" ) then
    set NOTSPREP=" -notsprep"
endif
if ( -e $BASENAME.pld && ! (-e $BASENAME.cst) ) then
    set NOTSPREP=" -notsprep"
endif

BACKUP:
if ( -f backup ) then
    echo "Moving file backup to backup.bak" | tee -a xepld.log
    /bin/mv -f backup backup.bak
else if ( -d backup ) then
else
    /bin/mkdir -p backup
endif

if ( -e $BASENAME.eqn ) then
    echo "Saving file $BASENAME.eqn to backup directory" | tee -a xepld.log
    /bin/cp -p $BASENAME.eqn backup
endif
if ( -e $BASENAME.gyd ) then
    echo "Saving file $BASENAME.gyd to backup directory" | tee -a xepld.log
    /bin/cp -p $BASENAME.gyd backup
endif
if ( -e $BASENAME.jed ) then
    echo "Saving file $BASENAME.jed to backup directory" | tee -a xepld.log
    /bin/cp -p $BASENAME.jed backup
endif
if ( -e $BASENAME.pin ) then
    echo "Saving file $BASENAME.pin to backup directory" | tee -a xepld.log
    /bin/cp -p $BASENAME.pin backup
endif
if ( -e $BASENAME.prg ) then
    echo "Saving file $BASENAME.prg to backup directory" | tee -a xepld.log
    /bin/cp -p $BASENAME.prg backup
endif
if ( -e $BASENAME.res ) then
    echo "Saving file $BASENAME.res to backup directory" | tee -a xepld.log
    /bin/cp -p $BASENAME.res backup
endif
if ( -e $BASENAME.rpt ) then
    echo "Saving file $BASENAME.rpt to backup directory" | tee -a xepld.log
    /bin/cp -p $BASENAME.rpt backup
endif
if ( -e $BASENAME.tim ) then
    echo "Saving file $BASENAME.tim to backup directory" | tee -a xepld.log
    /bin/cp -p $BASENAME.tim backup
endif

SEARCH_ORDER:
if ( $EXTENSION == "" ) then
    if ( -e sch/$BASENAME.1 || -e wir/$BASENAME.1 ) then 
        set EXTENSION="1"
    else if ( -e $BASENAME.mgc_component.attr ) then
        set EXTENSION="mentor"
    else if ( -e $BASENAME.sedif ) then
        set EXTENSION="sedif"
    else if ( -e $BASENAME.edif ) then
        set EXTENSION="edif"
    else if ( -e $BASENAME.sxnf ) then
        set EXTENSION="sxnf"
    else if ( -e $BASENAME.pld ) then
        set EXTENSION="pld"
    else if ( -e $BASENAME.xnf ) then
        set EXTENSION="xnf"
    else
        echo "Error: No design file found for $BASENAME with any recognized extension." | tee -a xepld.log
    set EXITSTATUS=2
    goto HELP
    endif
else if ( $EXTENSION != "1" && $EXTENSION != "sedif" && $EXTENSION != "edif" && $EXTENSION != "sxnf" && $EXTENSION != "pld" && $EXTENSION != "xnf" ) then
    echo "Error: Illegal design file extension "$EXTENSION"."
    set EXITSTATUS=2
    goto HELP
endif

switch ( "$EXTENSION" )
    case mentor:
        echo "men2xnf8 $BASENAME -p 7" | tee -a xepld.log
        men2xnf8 $BASENAME -p 7
        set XNF=" -x "
        set EXTENSION="xnf"
    breaksw
    case sedif:
        echo "edif2xnf -f -lcanet 5 -p 7 -s -u $BASENAME.sedif" | tee -a xepld.log
        edif2xnf -f -lcanet 5 -p 7 -s -u $BASENAME.sedif
        set XNF=" -x "
        set EXTENSION="xnf"
    breaksw
    case edif:
        echo "edif2xnf -f -lcanet 5 -p 7 -s -u $BASENAME.edif" | tee -a xepld.log
        edif2xnf -f -lcanet 5 -p 7 -s -u $BASENAME.edif
        set XNF=" -x "
        set EXTENSION="xnf"
    breaksw
    case sxnf:
        echo "sts $BASENAME.sxnf $BASENAME.xnf" | tee -a xepld.log
        sts $BASENAME.sxnf $BASENAME.xnf
        set XNF=" -x "
        set EXTENSION="xnf"
    breaksw
    case xnf:
        set XNF=" -x "
        set EXTENSION="xnf"
    breaksw
endsw

rm -f $BASENAME.vm6
echo "xemake6 $CSTFILE $FAMILY $SIGNATURE -nota $NOTSPREP $XNF $BASENAME.$EXTENSION" | tee -a xepld.log
xemake6 $CSTFILE $FAMILY $SIGNATURE -nota $NOTSPREP $XNF $BASENAME.$EXTENSION

rm -f ${BASENAME}_tim.xnf
if ( -e $BASENAME.vm6 ) then
    echo "tsim $BASENAME ${BASENAME}_tim.xnf" | tee -a xepld.log
    tsim $BASENAME ${BASENAME}_tim.xnf
endif

if ( $NOTA != "" ) then
else if ( $DETAIL != "" ) then
    echo "taengine -detail -f $BASENAME -l $BASENAME.tim" | tee -a xepld.log
    taengine -detail -f $BASENAME -l $BASENAME.tim
else
    echo "taengine -f $BASENAME -l $BASENAME.tim" | tee -a xepld.log
    taengine -f $BASENAME -l $BASENAME.tim
endif

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