#!/bin/csh -f

#
# GRAPE-DR functionality test program (check)
# 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.  0 for model450/1800, 1 for model460/2000
#

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

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-DR functionality test program (check)"
echo ""
echo "  usage: check"
echo "-------------------------------------------------"
echo "Start the tests. It may take a few minutes." 
echo

set tmpout = ${tmpdir}/tmpouts9
set logdir = ${rootpath}/sample/s9/loggdr
cd ${rootpath}/sample/s9

foreach n (1k 8k 64k)
  if (! (${n} == "64k" && ${runtype} == "s")) then
    echo
    echo "Start s9 ${n} using device${gdevice}..."
    switch (${n})
      case "1k"
        echo 128.0 256.0 10.0 1.0 pl1k 2 8.0 7 > ${tmpdir}/inputpara9.${gdevice}
        breaksw
      case "8k"
        echo 128.0 256.0 1.0 0.125 pl8k 2 8.0 7 > ${tmpdir}/inputpara9.${gdevice}
        breaksw
      case "64k"
        if (${gndevice} == 1) then
          grep DCM ${logdir}/sample.${n}.${revision}
        endif
        echo 128.0 256.0 0.5 0.0625 pl64k 2 8.0 7 > ${tmpdir}/inputpara9.${gdevice}
        breaksw
    endsw
    if (${gndevice} == 1) then
      # all outputs go to the console.
      ./s9_gdr ${tmpdir}/inputpara9.${gdevice} |& tee ${tmpout}.${gdevice}
    else
      # some outputs are filtered for readability.
      if (${n} == "64k") then
        ./s9_gdr ${tmpdir}/inputpara9.${gdevice} |& tee ${tmpout}.${gdevice} | grep -E 'time | DCM'
      else
        ./s9_gdr ${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} > ${tmpout}3.${gdevice}
    diff -w ${tmpout}2.${gdevice} ${tmpout}3.${gdevice} > /dev/null
    set s = ${status}
    if (${s} == 0) then
      echo Device${gdevice} s9 ${n} OK
    else
      echo Device${gdevice} s9 ${n} NG
      touch ${ngfile}
      exit 1
    endif
  endif
end

set tmpout = ${tmpdir}/tmpouts8
set logdir = ${rootpath}/sample/s8/loggdr
cd ../s8

foreach n (1k 8k 64k)
  if (! (${n} == "64k" && ${runtype} == "s")) then
    echo
    echo "Start s8 ${n} using device${gdevice}..."
    switch (${n})
      case "1k"
        echo 256.0 600.0 10.0 1.0 ../s9/pl1k 0.01 0.03 > ${tmpdir}/inputpara.${gdevice}
        breaksw
      case "8k"
        echo 2048.0 600.0 1.0 0.125 ../s9/pl8k 0.01 0.03 > ${tmpdir}/inputpara.${gdevice}
        breaksw
      case "64k"
        if (${gndevice} == 1) then
          grep pot ${logdir}/sample.${n}.${revision}
        endif
        echo 16384.0 600.0 0.5 0.0625 ../s9/pl64k 0.01 0.03 > ${tmpdir}/inputpara.${gdevice}
        breaksw
    endsw
    if (${gndevice} == 1) then
      # all outputs go to the console.
      ./s8_gdr ${tmpdir}/inputpara.${gdevice} |& tee ${tmpout}.${gdevice}
    else
      # some outputs are filtered for readability.
      if (${n} == "64k") then
        ./s8_gdr ${tmpdir}/inputpara.${gdevice} |& tee ${tmpout}.${gdevice} | grep -E 't |cputime|DCM'
      else
        ./s8_gdr ${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} > ${tmpout}3.${gdevice}
    diff -w ${tmpout}2.${gdevice} ${tmpout}3.${gdevice} > /dev/null
    set s = ${status}
    if (${s} == 0) then
      echo Device${gdevice} s8 ${n} OK
    else
      echo Device${gdevice} s8 ${n} NG
      touch ${ngfile}
      exit 1
    endif
  endif
end

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

touch ${okfile}
exit 0
