#!/bin/csh -f

#
# GRAPE-9 functionality test program subroutine for G6PIPE
# In order to run this script, environment vars
# GDEVICE and GREVISION must be set.
#
# GDEVICE   : device IDs in use.
# GNDEVICE  : total number of devices under test.
# GREVISION : iFPGA revision.
#

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

setenv HIB_WARNLEVEL "1"

if (! $?GDEVICE) then
    echo "An environment variable GDEVICE must be set."
    exit
endif
set gndevice = $GNDEVICE
set gdevice = $GDEVICE
set tmpdir = ${rootpath}/tmp
set okfile = ${tmpdir}/device${gdevice}.ok
set ngfile = ${tmpdir}/device${gdevice}.ng
set backend = `${rootpath}/hibutil/lsgrape -d ${gdevice} | \
               grep -v devid | sed 's/.*\(G5.*\|G6.*\)$/\1/g' `
echo "backend:${backend}"

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

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

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

if (! $?GREVISION) then
    echo "An environment variable GREVISION must be set."
    exit
endif
set revision = "rev${GREVISION}";

set runtype = $1   # 's' for short run.

echo "----------------------------------------------------------"
echo " GRAPE-9 functionality test program subroutine for G6PIPE"
echo "  usage: checksub_g6"
echo "----------------------------------------------------------"
echo "Start the tests. It may take a few minutes." 
echo

set initdir = ${rootpath}/init

foreach n (1k 2k)
  set tmpout = ${tmpdir}/tmpouts8
  set logdir = ${rootpath}/sample/s8/logg9
  cd ${rootpath}/sample/s8
  if (! (${n} == "64k" && ${runtype} == "s")) then
    echo
    echo "Start ${s8bin} ${n} using device${gdevice}..."
    switch (${n})
      case "1k":
        echo 256.0 600.0 10.0 1.0 ${initdir}/pl1k 0.01 0.03 > ${tmpdir}/inputpara.${gdevice}
        breaksw
      case "2k":
        echo 512.0 600.0 10.0 1.0 ${initdir}/pl2k 0.01 0.03 > ${tmpdir}/inputpara.${gdevice}
        breaksw
    endsw
    if (${gndevice} == 1) then
      # all outputs go to the console.
      ${s8bin} ${tmpdir}/inputpara.${gdevice} |& tee ${tmpout}.${gdevice}
    else
      # some outputs are filtered for readability.
      if (${n} == "64k") then
        ${s8bin} ${tmpdir}/inputpara.${gdevice} |& tee ${tmpout}.${gdevice} | grep -E 't |cputime|DCM'
      else
        ${s8bin} ${tmpdir}/inputpara.${gdevice} |& tee ${tmpout}.${gdevice} > /dev/null
      endif
    endif
#    grep -E 'pot|total|error|nts' ${tmpout}.${gdevice} > ${tmpout}2.${gdevice}
#    grep -E 'pot|total|error|nts' ${logdir}/sample.${n}.${revision} > ${tmpout}3.${gdevice}
    grep -E 'pot|total|error' ${tmpout}.${gdevice} > ${tmpout}2.${gdevice}
    grep -E 'pot|total|error' ${logdir}/sample.${n}.${revision}${logps} > ${tmpout}3.${gdevice}
    diff -w ${tmpout}2.${gdevice} ${tmpout}3.${gdevice} > /dev/null

    set s = ${status}
    if (${s} == 0) then
      echo Device${gdevice} ${s8bin} ${n} OK
    else
      echo Device${gdevice} ${s8bin} ${n} NG
      touch ${ngfile}
      exit 1
    endif
    # end of test w/s8

    #
    # test w/s9nb (G6nbPIPE only)
    #
    if (${backend} == G6nb) then
      set tmpout = ${tmpdir}/tmpouts9
      set logdir = ${rootpath}/sample/s9/logg9
      cd ${rootpath}/sample/s9
      echo
      echo "Start ${s9bin} ${n} using device${gdevice}..."

      switch (${n})
        case "1k":
          echo 128.0 256.0 10.0 1.0 ${initdir}/pl1k 2 8.0 7 > ${tmpdir}/inputpara9.${gdevice}
          breaksw
        case *:
          echo 128.0 256.0 1.0 0.125 ${initdir}/pl${n} 2 8.0 7 > ${tmpdir}/inputpara9.${gdevice}
          breaksw
      endsw
      if (${gndevice} == 1) then
        # all outputs go to the console.
        ${s9bin} ${tmpdir}/inputpara9.${gdevice} |& tee ${tmpout}.${gdevice}
      else
        # some outputs are filtered for readability.
        if (${n} == "64k") then
          ${s9bin} ${tmpdir}/inputpara9.${gdevice} |& tee ${tmpout}.${gdevice} | grep -E 'time | DCM'
        else
          ${s9bin} ${tmpdir}/inputpara9.${gdevice} |& tee ${tmpout}.${gdevice} | tail -5 | grep -E 'time | DCM'
        endif
      endif
      grep -E 'pot|total|DCM|DE' ${tmpout}.${gdevice} > ${tmpout}2.${gdevice}
      grep -E 'pot|total|DCM|DE' ${logdir}/sample.${n}.${revision}${logps} > ${tmpout}3.${gdevice}
      diff -w ${tmpout}2.${gdevice} ${tmpout}3.${gdevice} > /dev/null
      set s = ${status}
      if (${s} == 0) then
        echo Device${gdevice} ${s9bin} ${n} OK
      else
        echo Device${gdevice} ${s9bin} ${n} NG
        touch ${ngfile}
        exit 1
      endif

    endif # s9nb

  endif # !short run

end # foreach n

echo
cd ../..
echo "Device${gdevice} passed all tests." 
echo

touch ${okfile}
exit 0
