#!/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"


#
#  Use archived source files (.rdp, .mem, .rdram) from apps directory
#  This isolates us from rabid changes in the emulator
#
USE_ARCHIVE=""

#
#  Hardcode the random seed used by the simulator so that images will
#  compare from run to run.
#
USE_SEED="100"

#
# 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/e/kishor/simv_ipc_gate"

#
#  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=180000


##################################################
#
#  Usage message
#
##################################################
usage()
{
  echo "Usage: testit [-a] <-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}

  if test "$USE_ARCHIVE" = "TRUE"; then
    RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z.archive
    MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.mem.Z.archive
  else
    RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z
    MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.mem.Z
  fi

  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
  if test -r $MEM_SRC; then
      cat ${MEM_SRC} | uncompress -c > ${MEM_FILE} 
      if test $? -ne 0; then
          echo ".mem file ERROR"
          exit 1
      fi
  else
     echo "Error: $MEM_SRC does not exist or is not readable"
     exit 1
  fi


  if test -r $RDP_SRC; then
      cat ${RDP_SRC} | uncompress -c > ${RDP_FILE}
      if test $? -ne 0; then
          echo ".rdp file ERROR"
          exit 1
      fi
  else
     echo "Error: $RDP_SRC does not exist or is not readable"
     exit 1
  fi

  #  Run simulation
  ${RDP_CV} -e -i ${IN_BASE} -m ${IN_BASE}_cv \
	+tmem=allones.mem \
	-M 500 -S ${USE_SEED} \
	-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}
  if test "$USE_ARCHIVE" = "TRUE"; then
    RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z.archive
    MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.mem.Z.archive
  else
    RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z
    MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.mem.Z
  fi
  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
  if test -r $MEM_SRC; then
      cat ${MEM_SRC} | uncompress -c > ${MEM_FILE} 
      if test $? -ne 0; then
          echo ".mem file ERROR"
          exit 1
      fi
  else
     echo "Error: $MEM_SRC does not exist or is not readable"
     exit 1
  fi


  if test -r $RDP_SRC; then
      cat ${RDP_SRC} | uncompress -c > ${RDP_FILE}
      if test $? -ne 0; then
          echo ".rdp file ERROR"
          exit 1
      fi
  else
     echo "Error: $RDP_SRC does not exist or is not readable"
     exit 1
  fi


  #  Run simulation
  ${RDP_C} -e -i ${IN_BASE} -m ${IN_BASE}_c \
	-M 500 -S ${USE_SEED} \
	-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 or simv2.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}
  if test "$USE_ARCHIVE" = "TRUE"; then
#    RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z.archive
    MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.rdram.Z.archive
  else
#    RDP_SRC=${APPDIR}/test${TNUM}.${NUM}.rdp.Z
    MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.rdram.Z
  fi

#  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
  if test -r $MEM_SRC; then
      cat ${MEM_SRC} | uncompress -c > ${MEM_FILE} 
      if test $? -ne 0; then
          echo ".mem file ERROR"
          exit 1
      fi
  else
     echo "Error: $MEM_SRC does not exist or is not readable"
     exit 1
  fi


#  if test -r $RDP_SRC; then
#      cat ${RDP_SRC} | uncompress -c > ${RDP_FILE}
#      if test $? -ne 0; then
#          echo ".rdp file ERROR"
#          exit 1
#      fi
#  else
#     echo "Error: $RDP_SRC does not exist or is not readable"
#     exit 1
#  fi

  ${RDRAMGCLR} ${INDATA}/${IN_BASE}_io

  #
  # Check to see if the user wants to generate images from 1st tapeout sim.
  #
  if test -z "${USE_HW_ROOT}" ; then
      SIMV_EXE=${SIMVDIR}/simv2.ipc
  else
      SIMV_EXE=${SIMVDIR}/simv.ipc
  fi

  #  Run simulation
  SIMV="${SIMV_EXE} +mmap_rdram=${INDATA}/${IN_BASE}_io	\
		+timelimit=${TIMELIMIT} \
		+gclock_mon=150 +cbus_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 or simv2.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}
  if test "$USE_ARCHIVE" = "TRUE"; then
    MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.rdram.Z.archive
  else
    MEM_SRC=${APPDIR}/test${TNUM}.${NUM}.rdram.Z
  fi
  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
  if test -r $MEM_SRC; then
      cat ${MEM_SRC} | uncompress -c > ${MEM_FILE} 
      if test $? -ne 0; then
          echo ".mem file ERROR"
          exit 1
      fi
  else
     echo "Error: $MEM_SRC does not exist or is not readable"
     exit 1
  fi


  ${RDRAMGCLR} ${INDATA}/${IN_BASE}_gate

  #
  # Check to see if the user wants to generate images from 1st tapeout sim.
  #
  if test -z "${USE_HW_ROOT}"; then
      SIMV_EXE=${SIMVGATEDIR}/simv2.ipc.gates
  else
      SIMV_EXE=${SIMVGATEDIR}/simv.ipc.gates
  fi

  SIMV="${SIMV_EXE} +mmap_rdram=${INDATA}/${IN_BASE}_gate $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
#
##################################################
  if test $# = 0 
  then
    usage
  fi
  
  while getopts "n:t:s:a" a
  do
    case $a in
    	a)  USE_ARCHIVE=TRUE;;
    	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
