#!/bin/tcsh -f

# Back annotate changes in the backend logic to the interface logic,
# which is originally generated by gwrap_init.
#
# An environment variable GWRAPPATH must
# be set to directory at which this script is installed.

if (${#argv} < 2) then
    cat <<EOF

    This script generates the top-level instance of interface logic
    hierarchy of an given architecture <arch>, so that the backend
    logic <backend> written by the user is wrapped around. Interface
    logic suits other than the top-level instance can be generated by
    'gwrap_init'.

    An environment variable 'GWRAPPATH' must be set to directory at which
    this script itself is installed.

    usage: gwrap_update <arch> <backend>
        arch:
            dr3:     GRAPE-DR TB3 (preliminary)   PCIe x4       125MHz
            g7m1:    KFCR GRAPE-7 model100        PCI-X         133MHz
            g7m8:    KFCR GRAPE-7 model800        PCI-X         133MHz
            g7p1:    KFCR GRAPE-7 model300/600    pFPGA1,4      100MHz
            g7p2:    KFCR GRAPE-7 model300/600    pFPGA2,3,5    100MHz
            g7p6:    KFCR GRAPE-7 model300/600    pFPGA6        100MHz
            dkx4:    PLDA DesignKit               PCIe x4       125MHz
            gx2x4:   PLDA XpressGX2               PCIe x4       125MHz
            gx2x4f:  PLDA XpressGX2               PCIe x4       125MHz, fully licensed core
            gx2x8:   PLDA XpressGX2               PCIe x8       250MHz
            gx2x8f:  PLDA XpressGX2               PCIe x8       250MHz, fully licensed core

        backend: a VHDL file that contains the 'backend' entity.

EOF
    exit
endif

set wdir = $GWRAPPATH
set igen = $wdir/ifpgagen.pl
set udir = $PWD
set arch = $argv[1];
set backend = $argv[2];
echo "arch:${arch}    backend:${backend}"

if (! -e ${backend}) then
    echo "backend file not found. abort."
    exit
endif

switch (${arch})

case g7p[126]:
    cd ${wdir}
    $igen ${arch} ${udir}/${backend} >! ${udir}/pfpga3.vhd
    cd ${udir}
    breaksw

case g7m1:
case g7m8:
case dkx4:
case gx2x4:
case gx2x4f:
case gx2x8:
case gx2x8f:
case dr3:
    cd ${wdir}
    $igen ${arch} ${udir}/${backend} >! ${udir}/ifpga.vhd
    cd ${udir}
    breaksw

default:
    echo invalid arch. abort.
    exit
    breaksw
endsw
