#!/usr/bin/env bash


source config.sh

pushd .

cd $PROJ

mkdir -p $SCRATCH
cd $SCRATCH

#Prepare code library
$VLIB $TOP

check $? "Crate Modelsim library"

$VMAP work $SCRATCH/$TOP

check $? "Map Modelsim library"


#Compile according to FOF files
function _VHD_COM {
  $VCOM -2008 $1

  check $? "VHDL compile $1"
}

function IP_COMP {
  $VCOM ${1%.*}.vhd

  check $? "VHDL compile $1"
}

function _V_COM {
  $VLOG $1
  check $? "Verilog compile"
}

function _SV_COM {
  $VLOG -sv +incdir+$INCLUDE $1
  check $? "SystemVerilog compile"
}

source $CONFIG/tb.fof

_SV_COM $TC/tc_$2.sv
_SV_COM "$TB/tb.sv +define+TC_NAME=tc_$2"

#Make code
$VMAKE work > Makefile
check $? "Generate makefile"

make
check $? "make"


#$VSIM -c work.tb -do "run -all"
if [ $1 == "gui" ]; then
  $VSIM work.tb +nowarnTSCALE -default_radix 16
else
 $VSIM -c work.tb  +nowarnTSCALE -do "run -all"
fi;

check $? "Simulation"



popd
