#!/bin/csh -f

# GRAPE-9 functionality test program (check)

cat << DESC_END
-----------------------------------------------------------------
 GRAPE-9 functionality test program for G6PIPE
   usage: check_g6
-----------------------------------------------------------------
DESC_END

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

if ( $?GDEVICE ) then
    # set the device ID of the 1st card to 'devid'.
    set devid = `echo $GDEVICE | sed 's/\([0-9]\).*$/\1/g'`
    set devs = ($GDEVICE)
else
    set devid = 0
    set ndev = `${rootpath}/hibutil/lsgrape | wc -l`  # num of dev + 1
    @ ndev--
    set devs = ()
    set i = 0
    while (${i} < ${ndev})
        set devs = (${devs} ${i})
        @ i++
    end
endif

set model = `${rootpath}/hibutil/lsgrape -d ${devid} | \
             grep -v devid | sed 's/.*model\([0-9]\+\).*$/\1/g' | \
             sed 's/\s*\([0-9]\+\).*$/\1/' `
echo "model:model${model}"

set rev = `${rootpath}/hibutil/lsgrape -d ${devid} | \
           grep -v devid | sed 's/.*rev\([0-9]\+\).*$/\1/g' | \
           sed 's/\s*\([0-9]\+\).*$/\1/' `
echo "rev:rev${rev}"

set backend = `${rootpath}/hibutil/lsgrape -d ${devid} | \
               grep -v devid | sed 's/.*\(G5.*\|G6.*\)$/\1/g' `
echo "backend:${backend}"

switch (${backend})
  case G6:
    set s8bin = ./s8_g9
    breaksw

  case G6nb:
    set s8bin = ./s8_g9nnb
    set s9bin = ./s9_g96nb
    breaksw

  case *:
    echo "No valid pipeline logic found in device${devid}."
    exit 1
    breaksw
endsw

# now the following variables are set.
#   rev   : The iFPGA revision of the 1st device in use.
#   devs  : A list of device IDs of devices in use.

echo "Start check for ${#devs} device(s) (revision: ${rev}, device IDs: ${devs})..."


#
# tests on each device.
#
set tmpdir = ${rootpath}/tmp
set initdir = ${rootpath}/init
set i = 1
while (${i} <= ${#devs})
    set okfile = ${tmpdir}/device$devs[$i].ok
    set ngfile = ${tmpdir}/device$devs[$i].ng
    (setenv GDEVICE "$devs[$i]" ; \
     setenv GREVISION "${rev}" ; \
     setenv GNDEVICE ${#devs} ; \
     rm ${okfile} ${ngfile} >& /dev/null ; \
     ${rootpath}/g9/test/checksub_g6 ${argv}) &
    sleep 1
    @ i++
end

onintr LABEL_HUP

# wait until all sub programs are done.
set nruns = 1
while (${nruns} > 0)
  set nruns = `ps x | pgrep checksub | wc -l`
  sleep 1
end

set allok = 1
set i = 1
while (${i} <= ${#devs})
    set okfile = ${tmpdir}/device$devs[$i].ok
    set ngfile = ${tmpdir}/device$devs[$i].ng
    if ( -f ${ngfile}) then
        set allok = 0
    else if (! -f ${okfile}) then
        set allok = 0
    endif
    @ i++
end

if (${allok} == 0) then
    echo ""
    echo "Check failed."
    echo ""
    cd ${rootpath}
    exit 1
endif

#
# Tests using all devices.
#
if (${#devs} == 1) then
    # do not perform this test for a single device system.
    cd ${rootpath}
    exit 0
endif

echo ""
echo "Start tests using all devices."
echo ""

setenv GDEVICE "$devs"
setenv GREVISION "${rev}"
set tmpdir = ${rootpath}/tmp

@ ndevMaxN = 2 * ${#devs}
@ eps = ${ndevMaxN} * 1024 / 4
set ndevMaxN = ${ndevMaxN}k

foreach n ( 1k ${ndevMaxN})
  set ntry = 0
  while (${ntry} < 3)
    echo "Start ${s8bin} ${n} try${ntry} devices: ${devs}..."
    set tmpout = ${tmpdir}/tmpouts8.all
    cd ${rootpath}/sample/s8
    switch (${n})
      case "1k":
        echo 256.0 600.0 10.0 1.0 ${initdir}/pl1k 0.01 0.03 > ${tmpdir}/inputpara.all
        breaksw
      case "2k":
        echo 512.0 600.0 10.0 1.0 ${initdir}/pl2k 0.01 0.03 > ${tmpdir}/inputpara.all
        breaksw
      case *:
        echo ${eps} 600.0 2.0 0.2 ${initdir}/pl${n} 0.01 0.03 > ${tmpdir}/inputpara.all
    endsw

    ${s8bin} ${tmpdir}/inputpara.all |& tee ${tmpout}.try${ntry} | grep 'time'
    grep -v -E "cputime|nplus|closed|opened|devid|temp" ${tmpout}.try${ntry} > ${tmpout}.try${ntry}striped
    if (${ntry} != 0) then
      diff -w ${tmpout}.try0striped ${tmpout}.try${ntry}striped
      set s = ${status}
      if (${s} == 0) then
        echo Devices:${devs}    ${s8bin} ${n} try${ntry} OK
      else
        echo Devices:${devs}    ${s8bin} ${n} try${ntry} NG
        echo ""
        echo "Check failed."
        echo ""
        cd ${rootpath}
        exit 1
      endif
    endif

    #
    # test w/s9nb (G6nbPIPE only)
    #
    if (${backend} == G6nb) then
      echo "Start ${s9bin} ${n} try${ntry} devices: ${devs}..."
      set tmpout = ${tmpdir}/tmpouts9.all
      cd ${rootpath}/sample/s9

      switch (${n})
        case "1k":
          echo 128.0 256.0 10.0 1.0 pl1k 2 8.0 7 > ${tmpdir}/inputpara9.all
          breaksw
        case *:
          echo 128.0 256.0 1.0 0.125 pl${n} 2 8.0 7 > ${tmpdir}/inputpara9.all
      endsw

      ${s9bin} ${tmpdir}/inputpara9.all |& tee ${tmpout}.try${ntry} | grep 'time'
      grep -v -E "cputime|nplus|closed|opened|devid|temp|g5" ${tmpout}.try${ntry} > ${tmpout}.try${ntry}striped
      if (${ntry} != 0) then
        diff -w ${tmpout}.try0striped ${tmpout}.try${ntry}striped
        set s = ${status}
        if (${s} == 0) then
          echo Devices:${devs}    ${s9bin} ${n} try${ntry} OK
        else
          echo Devices:${devs}    ${s9bin} ${n} try${ntry} NG
          echo ""
          echo "Check failed."
          echo ""
          cd ${rootpath}
          exit 1
        endif # if error
      endif # if ntry > 0
    endif # s9nb

    @ ntry++

  end # ntry

end # n

echo ""
echo "All devices passed all tests."
echo ""
cd ${rootpath}
exit 0

LABEL_HUP:
killall checksub_g6 s8_g9 s8_g9nnb >& /dev/null
