#!/bin/csh -f

#
# reconfigure the configuration ROMs.
#
# erase and then write a .rpd file for each iFPGA's ROM.
#

set cmdname  = $0:t;
set rootpath = $0:h/../..
pushd $rootpath > /dev/null
set rootpath = `pwd`
popd > /dev/null

if ( $#argv < 1 ) then
    echo "reconfigure the configuration ROM(s) of iFPGA(s).";
    echo "  usage: $cmdname <.rpd file> [iFPGA IDs]";
    echo "     ex) $cmdname g5pipe.rpd 0 1 2 3";
    exit;
endif

set rpdfile = $argv[1];

if ( $#argv < 2 ) then
    set ids = (dummy 0 1 2 3)
else
    set ids = ($argv)
endif

echo ids:$ids

while ($#ids > 1)
    shift ids
    set devid = $ids[1];
    if ($devid < 0) then
        echo "Too small iFPGA ID:$devid. Abort."
        exit 1
    endif
    if ($devid > 3) then
        echo "Too large iFPGA ID:$devid. Abort."
        exit 1
    endif

#    echo rpdfile:${rpdfile}    devid:${devid}
    ${rootpath}/hibutil/hibtest 19 ${devid};
    ${rootpath}/hibutil/hibtest 20 ${rpdfile} ${devid};
end
