#!/bin/sh

SIMNAME=systemsim
export SIMNAME

EXEC_DIR=${SYSTEMSIM_TOP}/bin
export EXEC_DIR

LIB_DIR=${EXEC_DIR}/lib
export LIB_DIR

TCL_LIBRARY=${LIB_DIR}/tcl8.3
export TCL_LIBRARY

TK_LIBRARY=${LIB_DIR}/tk8.3
export TK_LIBRARY

# This is used by the emitter profiler tools
PROFILER_DIR=${LIB_DIR}/ppc/profiler
export PROFILER_DIR

#
# check if we want -cyclesim or -emit
#
DEFAULT=-apache
BASE=
CYCLESIM=
EMIT=
DEBUGGER=
DISCLAIM=

# we look for arguments that define the type of
# simulator that we want to run.  This comes in three
# flavors:

#      The base simulator -- apache, spruce, walnut, ebony, ...
#      with or without cyclesim
#      with or without emit

# We process arguments until we get to one that we
# don't understand, and stop there.  This means that
# we must first specify the "systemsim" script arguments,
# and then the simulator program arguments (such as -q -f, ...)
# We can say "systemsim -cyclesim -apache -q -f foo.tcl",
# but we can't say "systemsim -q -cyclesim -apache".
# Must have "systemsim [script-arguments] [program-arguments]"

# if there are no systemsim script-arguments, we use the
# default systemsim executable, as defined above by DEFAULT.
# if there are systemsim script arguments, we reset DEFAULT
# to empty, and then define it according to the arguments.

CHECKARGS=0
if [ ! -z $1 ]
then
     CHECKARGS=1
fi

# get rid of gdbcmds.tmp if it exists
/bin/rm -f gdbcmds.tmp

while [ $CHECKARGS = 1 ]
do
    case $1 in

    -gdb*)    DEBUGGER=gdb
              shift
              echo set args $*  > gdbcmds.tmp
              DEFAULT=
              ;;

    -ddd*)    DEBUGGER=ddd
              DEFAULT=
              shift ;;

    -valgrind*) DEBUGGER="valgrind -q --error-limit=no"
              DEFAULT=
              shift ;;

    -insight*) DEBUGGER="TCL_LIBRARY= TK_LIBRARY= insight"
              # Insight needs a little extra help due to tcl conflicts.
              echo set environment TCL_LIBRARY=${EXEC_DIR}/lib/tcl8.3 > gdbcmds.tmp
              echo set environment  TK_LIBRARY=${EXEC_DIR}/lib/tk8.3 >> gdbcmds.tmp
              echo set radix 16                                      >> gdbcmds.tmp
              shift
              echo set args $*                                       >> gdbcmds.tmp
              DEFAULT=
              ;;

    -cycle*)  CYCLESIM=-cyclesim
              DEFAULT=
              shift ;;

    -simple*) CYCLESIM=
              DEFAULT=
              shift ;;

    -emit*)   EMIT=-emit
              DEFAULT=
              shift ;;

    -noemit*) EMIT=
              DEFAULT=
              shift ;;

    -walnut*) BASE=-walnut
              DEFAULT=
              shift ;;

    -fsp0*)   BASE=-fsp0
              DEFAULT=
              shift ;;

    -fsp1*)   BASE=-fsp1
              DEFAULT=
              shift ;;

    -ebony*)  BASE=-ebony
              DEFAULT=
              shift ;;

    -apache*) BASE=-apache
              DEFAULT=
              shift ;;

    -gr*)     BASE=-gr
              DEFAULT=
              shift ;;

    -percs)   BASE=-percs
              DEFAULT=
              shift ;;

    -percs-d*)  BASE=-percs-d
              DEFAULT=
              shift ;;

    -percs-extend)   BASE=-percs-extend
              DEFAULT=
              shift ;;

    -percs-extend-d*)  BASE=-percs-extend-d
              DEFAULT=
              shift ;;

    -gpul*)   BASE=-gpul
              DEFAULT=
              shift ;;

    -eclipz*) BASE=-eclipz
              DEFAULT=
              shift ;;

    -agua*)   BASE=-agua
              DEFAULT=
              shift ;;

    -tca*)    BASE=-tca
              DEFAULT=
              shift ;;

    -bway*)   BASE=-bway
              DEFAULT=
              shift ;;

    -spruce*) BASE=-spruce
              DEFAULT=
              shift ;;

    -sti*)    BASE=-sti
              DEFAULT=
              shift ;;

    -cell*)   BASE=-cell
              DEFAULT=
              DISCLAIM="${SYSTEMSIM_TOP}/bin/lib/cell/disclaim.txt"
              shift ;;

    -zebra*)  BASE=-zebra
              DEFAULT=
              shift ;;

    *)        CHECKARGS=0
              ;;
    esac

done


# if there are simulator script arguments, we reset DEFAULT
# to empty, and then define it according to the argmuments.

if [ "${DEFAULT}" = "" ]
then
    DEFAULT=${BASE}${CYCLESIM}${EMIT}
fi

#
# Display disclaimer text, if applicable
#
if [ "${DISCLAIM}" != "" ]
then
    if [ -r ${DISCLAIM} ] 
    then 
        cat $DISCLAIM
    fi
fi

if [ "${DEBUGGER}" = "" ]
then
    # run simulator executable
    exec ${EXEC_DIR}/${SIMNAME}$DEFAULT $*
else
    # debug simulator executable. 
    if [ -e gdbcmds.tmp ]; then
        eval ${DEBUGGER} ${EXEC_DIR}/${SIMNAME}$DEFAULT -x gdbcmds.tmp
    else
	if  [ "${DEBUGGER}" = "ddd" ]; then
	    ${DEBUGGER} --args ${EXEC_DIR}/${SIMNAME}$DEFAULT $*
	else
	    ${DEBUGGER} ${EXEC_DIR}/${SIMNAME}$DEFAULT $*
	fi
    fi
fi

