#!/bin/sh -f
#################################################################
#
#  Script to make tabular files.  Make up for lack of prefix
#  rules in smake.
#
#################################################################

#
#  either att, cov, ew, tex, ms
#
TYPE="" 

#
#  Three digit test number
#
TNUM=""


#
#  Simulator, c, cv, io, gate
#
SIM="cv"
#
# The gate level simulation is currently a hand tweaked executable maintained
# by Ken Hayes; coming soon to an account on a server near you...
#
# When we do change the location, we'll reflect that here.  SIMVGATEDIR gets
# used by the gatesim() internal script function, and overwrites SIMVDIR as set
# in the incoming shell environment (which is used by the iosim() internal 
# script function).
#
SIMVGATEDIR="/hosts/baraka/a/hayes.gates"

#
#  Directories
#
PRDEPTH="../../.."
ATTDIR="${PRDEPTH}/apps/rdpatt/InData"
COVDIR="${PRDEPTH}/apps/rdpcov/InData"
EWDIR="${PRDEPTH}/apps/rdpew/InData"
TEXDIR="${PRDEPTH}/apps/rdptex/InData"
MSDIR="${PRDEPTH}/apps/rdpms/InData"

INDATA="InData"
OUTDATA="OutData"


#
#  Tools
#
RDP_CV="rdp_cv $RDP_OPTS"
RDP_C="rdp_c $RDP_OPTS"
RDP_IO="${ROOT}/PR/iosim/src/iosim"
FILTER=${ROOT}/PR/rdpsim/backend/filter
IO_TEST=${ROOT}/PR/iosim/test/rdp_3stall.tst
RDRAM2RDP=rdram2rdp
RDRAMGCLR=rdramgclr
RDRAM2IMG=rdram2image


#  
#  Set environment
#
LD_LIBRARY_PATH="/ecad/vcs/vcs_2.2/sgi/lib:${PRDEPTH}/lib/libimage:${PRDEPTH}/iosim/src/iosim"

export LD_LIBRARY_PATH

TIMELIMIT=21600


##################################################
#
#  Usage message
#
##################################################
usage()
{
  echo "Usage: testit <-n> test_number <-t> type <-s> simulator"
  exit 2
}


##################################################
#
#  Run the C/Verilog simulation (rdp_cv)
#
##################################################
cvsim()
{
  echo CV sim test ${TNUM} start `date` 
  #set -x

  #  Generate input file names
  if [ ${TNUM} -ge 100 ] ; then
      NUM=`expr ${TNUM} - 100`
  else
      NUM=`expr ${TNUM} + 0`
  fi
  IN_BASE=test${TNUM}.${TYPE}
  RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z
  MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.mem.Z
  RDP_FILE=${INDATA}/${IN_BASE}.rdp
  MEM_FILE=${INDATA}/${IN_BASE}_cv.mem

  #  Generate output file names
  OUT_BASE=test${TNUM}_${TYPE}_cv
  OUT_LOG=${OUTDATA}/${OUT_BASE}.out
  COV_FILE=${OUTDATA}/${OUT_BASE}_0.cov
  CVG_FILE=${OUTDATA}/${OUT_BASE}_0.cvg
  RGB_FILE=${OUTDATA}/${OUT_BASE}_0.rgb
  RGBF_FILE=${OUTDATA}/test${TNUM}_${TYPE}_cvf_0.rgb

  #  Copy and uncompress rdp and mem file from apps directory
  zcat ${MEM_SRC} > ${MEM_FILE} 
  if test $? -ne 0; then
      echo ".mem file ERROR"
      exit 1
  fi
  zcat ${RDP_SRC} > ${RDP_FILE}
  if test $? -ne 0; then
      echo ".rdp file ERROR"
      exit 1
  fi

  #  Run simulation
  ${RDP_CV} -e -i ${IN_BASE} -m ${IN_BASE}_cv \
	+tmem=allones.mem \
	-M 500 -S ${NUM} \
	-f ${OUT_BASE} -z | tee ${OUT_LOG}

  if test $? -ne 0 ; then
    echo "RDP_CV FAILED"
    exit 1
  fi

  #  Create output files for comparison
  ${FILTER} -c ${COV_FILE} -i ${CVG_FILE} \
	-n ${RGB_FILE} -o ${RGBF_FILE}

  # cleanup
  rm -f ${MEM_FILE}

  echo CV sim test ${TNUM} finish `date` 
}


##################################################
#
#  Run the C simulation (rdp_c)
#
##################################################
csim()
{
  echo "C sim test ${TNUM} start `date`"
  #set -x

  #  Generate input file names
  if [ ${TNUM} -ge 100 ] ; then
      NUM=`expr ${TNUM} - 100`
  else
      NUM=`expr ${TNUM} + 0`
  fi
  IN_BASE=test${TNUM}.${TYPE}
  RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z
  MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.mem.Z
  RDP_FILE=${INDATA}/${IN_BASE}.rdp
  MEM_FILE=${INDATA}/${IN_BASE}_c.mem

  #  Generate output file names
  OUT_BASE=test${TNUM}_${TYPE}_c
  OUT_LOG=${OUTDATA}/${OUT_BASE}.out
  COV_FILE=${OUTDATA}/${OUT_BASE}_0.cov
  CVG_FILE=${OUTDATA}/${OUT_BASE}_0.cvg
  RGB_FILE=${OUTDATA}/${OUT_BASE}_0.rgb
  RGBF_FILE=${OUTDATA}/test${TNUM}_${TYPE}_cf_0.rgb

  #  Copy and uncompress rdp and mem file from apps directory
  zcat ${MEM_SRC} > ${MEM_FILE} 
  if test $? -ne 0; then
      echo ".mem file ERROR"
      exit 1
  fi
  zcat ${RDP_SRC} > ${RDP_FILE}
  if test $? -ne 0; then
      echo ".rdp file ERROR"
      exit 1
  fi

  #  Run simulation
  ${RDP_C} -e -i ${IN_BASE} -m ${IN_BASE}_c \
	-M 500 -S ${NUM} \
	-f ${OUT_BASE} -z | tee ${OUT_LOG}

  if test $? -ne 0 ; then
    echo "RDP_C FAILED"
    exit 1
  fi

  #  Create output files for comparison
  ${FILTER} -c ${COV_FILE} -i ${CVG_FILE} \
	-n ${RGB_FILE} -o ${RGBF_FILE}

  # cleanup
  rm -f ${MEM_FILE}

  echo "C sim test ${TNUM} finish `date`"
}


##################################################
#
#  Run the io simulation (simv.ipc)
#
##################################################
iosim()
{
  echo "IO sim test ${TNUM} start `date`"
  #set -x

  #  Generate input file names
  if [ ${TNUM} -ge 100 ] ; then
      NUM=`expr ${TNUM} - 100`
  else
      NUM=`expr ${TNUM} + 0`
  fi
  SRC_BASE=test${TNUM}.${TYPE}
  IN_BASE=test${TNUM}_${TYPE}
  RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z
  MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.rdram.Z
  RDP_FILE=${INDATA}/${SRC_BASE}.rdp
  MEM_FILE=${INDATA}/${IN_BASE}_io.rdram

  #  Generate output file names
  OUT_BASE=test${TNUM}_${TYPE}_io
  OUT_LOG=${OUTDATA}/${OUT_BASE}.log
  OUT_LOGF=${OUTDATA}/${OUT_BASE}f.log
  COV_FILE=${OUTDATA}/${OUT_BASE}_0.cov
  CVG_FILE=${OUTDATA}/${OUT_BASE}_0.cvg
  RGB_FILE=${OUTDATA}/${OUT_BASE}_0.rgb
  RGBF_FILE=${OUTDATA}/test${TNUM}_${TYPE}_iof_0.rgb

  #  Copy and uncompress rdp and mem file from apps directory
  zcat ${MEM_SRC} > ${MEM_FILE} 
  if test $? -ne 0; then
      echo ".mem file ERROR"
      exit 1
  fi
  zcat ${RDP_SRC} > ${RDP_FILE}
  if test $? -ne 0; then
      echo ".rdp file ERROR"
      exit 1
  fi
  ${RDRAMGCLR} ${INDATA}/${IN_BASE}_io

  #  Run simulation
  SIMV="${SIMVDIR}/simv.ipc +mmap_rdram=${INDATA}/${IN_BASE}_io \
		+timelimit=${TIMELIMIT} +mspan_mon 		\
		+gclock_mon=150 +mbus_mon $RDP_OPTS"
  export SIMV

  ${RDP_IO} 			\
	-n test${TNUM}_${TYPE} 	\
	-d 0x41 		\
	-f ${IO_TEST} 		\
	-l ${OUT_LOG} 		\
	    >  ${OUT_LOGF}

  if test $? -ne 0 ; then
    echo "IOSIM FAILED"
    exit 1
  fi

  ${RDRAM2IMG} ${INDATA}/${IN_BASE}_io

  #  Create output files for comparison
  ${FILTER} -c ${COV_FILE} -i ${CVG_FILE} \
	-n ${RGB_FILE} -o ${RGBF_FILE}

  # cleanup
  rm -f ${MEM_FILE}

  echo "IO sim test ${TNUM} finish `date`"
}

##################################################
#
#  Run the gate level simulation (simv.ipc.gates)
#
##################################################
gatesim()
{
  echo "GATE sim test ${TNUM} start `date`"
  #set -x

  #  Generate input file names
  if [ ${TNUM} -ge 100 ] ; then
      NUM=`expr ${TNUM} - 100`
  else
      NUM=`expr ${TNUM} + 0`
  fi
  SRC_BASE=test${TNUM}.${TYPE}
  IN_BASE=test${TNUM}_${TYPE}
  RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z
  MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.rdram.Z
  RDP_FILE=${INDATA}/${SRC_BASE}.rdp
  MEM_FILE=${INDATA}/${IN_BASE}_gate.rdram

  #  Generate output file names
  OUT_BASE=test${TNUM}_${TYPE}_gate
  OUT_LOG=${OUTDATA}/${OUT_BASE}.log
  OUT_LOGF=${OUTDATA}/${OUT_BASE}f.log
  COV_FILE=${OUTDATA}/${OUT_BASE}_0.cov
  CVG_FILE=${OUTDATA}/${OUT_BASE}_0.cvg
  RGB_FILE=${OUTDATA}/${OUT_BASE}_0.rgb
  RGBF_FILE=${OUTDATA}/test${TNUM}_${TYPE}_gatef_0.rgb

  #  Copy and uncompress rdp and mem file from apps directory
  zcat ${MEM_SRC} > ${MEM_FILE} 
  if test $? -ne 0; then
      echo ".mem file ERROR"
      exit 1
  fi
  zcat ${RDP_SRC} > ${RDP_FILE}
  if test $? -ne 0; then
      echo ".rdp file ERROR"
      exit 1
  fi
  ${RDRAMGCLR} ${INDATA}/${IN_BASE}_gate

  #
  # Run simulation; can't use mspan, gclock, mbus monitors, because they're not
  # supported at the gate level.  The timelimit is similarly not used, although
  # we probably could use that, with the proviso that the gate level tests take
  # MUCH longer to run to completion than the behavioral (iosim) tests.
  #
  SIMVDIR=${SIMVGATEDIR}
  export SIMVDIR
  SIMV="${SIMVDIR}/simv.ipc.gates +mmap_rdram=${INDATA}/${IN_BASE}_gate \
		+mbus_mon $RDP_OPTS"
  export SIMV

  ${RDP_IO} 			\
	-n test${TNUM}_${TYPE} 	\
	-d 0x41 		\
	-f ${IO_TEST} 		\
	-l ${OUT_LOG} 		\
	    >  ${OUT_LOGF}

  if test $? -ne 0 ; then
    echo "IOSIM FAILED"
    exit 1
  fi

  ${RDRAM2IMG} ${INDATA}/${IN_BASE}_gate

  #  Create output files for comparison
  ${FILTER} -c ${COV_FILE} -i ${CVG_FILE} \
	-n ${RGB_FILE} -o ${RGBF_FILE}

  # cleanup
  rm -f ${MEM_FILE}

  echo "GATE sim test ${TNUM} finish `date`"
}


##################################################
#  Main
#
##################################################
  while getopts "n:t:s:" a
  do
    case $a in
    	n)  TNUM=$OPTARG;;
    	t)  TYPE=$OPTARG;;
    	s)  SIM=$OPTARG;;
    	\?) usage ;;
    esac
  done
  shift `expr $OPTIND - 1`

  #  Determine app directory
  case ${TYPE} in
	"att") APPDIR=$ATTDIR;;
	"tex") APPDIR=$TEXDIR;;
	"cov") APPDIR=$COVDIR;;
	"ew")  APPDIR=$EWDIR;;
	"ms")  APPDIR=$MSDIR;;
	*)     echo "Unrecognized type: ${TYPE}"
	       exit 1;;
  esac
  
  # run simulation
  case $SIM in
  	c)  csim;;
  	cv) cvsim;;
  	io) iosim;;
  	gate) gatesim;;
	*)  echo "Unrecognized simulator: ${SIM}"
  esac

# End testit
