#!/bin/bash -x
#
# set tabsbtop=4

LOG=$1
export ARM7TDMI_CYCLE_ACCURATE_MODEL=1

start_sim() {
    ###### START simulator, only if another one is not already running ########
    #sleep 10
    #PS_NCSIM=`ps ax | grep ncver`
    #if [ ${#PS_NCSIM} -lt 200 ]; then
        cd ${SC_PATH}/hw/chip/sim
        export SIMDEFS="+define+SAk9f1g08 +define+FASTFLASH"
        make cov.cpu >> ${LOG_PATH}/arm_sim.log 2>&1 &
        sleep 20
    #else
    #    echo ncverilog is already running, so exit
    #    exit 1
    #fi
}

#
# Run one test
#
# Arguments:  $1 is the category of test (subdirectory under sc/tests)
#             $2 is base name of test ("foo" if "foo.bin")
#             $3 is the cycle limit for the test (-a"-l$2")
#             $4 Test name
#
run_test() {
    echo run_test dir = $1, name = $2, cycles = $3
    start_sim
    cd ${SC_PATH}/sw/sc/tests/$1
    ${ROOT}/usr/bin/x86/cpu -L $LOG -ccpu0 -En -a"-l$3" -q -r${ROOT}/usr/bin/sc/flip.bin -D$2.bin -n"$4" $5 > ${LOG_PATH}/$2.log 2>&1
    sleep 300    
}

rm -f ${LOG_PATH}/arm_sim.log

##### Run Tests on the real CPU  ######

run_test cpu memory    90000  "CPU memory Test"
run_test cpu cache     35000  "CPU cache Test"
run_test cpu cache_rand 900000  "CPU random cache Test" "-Ssdram"
run_test cpu dcache1   20000  "CPU DCACHE Test"
run_test cpu fiq       20000  "CPU FIQ Test"
run_test cpu interrupt 32000  "CPU INTERRUPT Test"
run_test cpu usermode  20000  "CPU User mode Test"
run_test cpu mixfiq    82000  "CPU Mix FIQ Test"
run_test cpu mixreg    60000  "CPU MIX REG Test"
run_test cpu mixuser   40000  "CPU MIX User Test"
run_test cpu mixabort  30000  "CPU Mix Abort Test"
run_test cpu recursive 15000  "CPU Recursive Test"
run_test cpu dcache1   125000 "CPU Shared Cacheline Test"
run_test cpu coproc    120000 "CPU Co-Process Test (word locks)"
run_test cpu coprocb   55000  "CPU Co-Process Test (byte locks)"
run_test dft kilo 180000 "CPU DFT kilopass test"

###### Run USB test ######
start_sim
cd ${SC_PATH}/sw/sc/tests/usb
${SC_PATH}/sw/dv/iosim/tests/usb/cpu -L $LOG -ccpu0 -En -q -n "CPU USB test" -r${ROOT}/usr/bin/sc/flip.bin -Dusbtest.bin > ${LOG_PATH}/cpu_usb.log 2>&1
sleep 300    

##### Run DFT Jtag test
start_sim
cd ${SC_PATH}/sw/dv/iosim/tests/dft/cpu
./jtagtest -L $LOG -ccpu0 -En -q -n "CPU DFT Jtag test" > ${LOG_PATH}/dftjtag.log 2>&1
sleep 300    
