#!/bin/sh

# Put which branch of Git to use here...
IPBUS_BRANCH="-b v1.3"
TLU_BRANCH="-b 1e000022"

mkdir work
cd work

# IPBB_VERSION="0.3.13"
IPBB_VERSION="0.4.3"
curl -L https://github.com/ipbus/ipbb/archive/v${IPBB_VERSION}.tar.gz | tar xvz
# ( or git clone git@github.com:ipbus/ipbb.git )
source ipbb-${IPBB_VERSION}/env.sh

ipbb init build

cd build
ipbb add git https://github.com/ipbus/ipbus-firmware.git ${IPBUS_BRANCH}
ipbb add git https://ohwr.org/project/fmc-mtlu-gw.git ${TLU_BRANCH}
# For read/write load a valid ssh key and use the repo below ....
# ipbb add git ssh://git@ohwr.org/fmc-projects/fmc-mtlu/fmc-mtlu-gw.git 


# In order to generate the VHDL to decode the addresses follow the instructions at https://ipbus.web.cern.ch/ipbus/doc/user/html/firmware/hwDevInstructions.html
echo "Generating address table VHDL from XML file"
pushd src/fmc-mtlu-gw/AIDA_tlu/projects/TLU_v1e/addr_table
pwd
/opt/cactus/bin/uhal/tools/gen_ipbus_addr_decode -v TLUaddrmap.xml
#copy resulting file ( ipbus_decode_TLUaddrmap.vhd ) to work/build/src/fmc-mtlu-gw/AIDA_tlu/projects/TLU_v1e/firmware/hdl/
mv ipbus_decode_TLUaddrmap.vhd ../firmware/hdl/
popd

ipbb proj create sim TLU_1e_sim fmc-mtlu-gw:AIDA_tlu/projects/sim -t top_sim.dep

source /software/CAD/setup_mentor2019.sh

ipbb sim setup-simlib ipcores fli

# First time or only if machine is rebooted
# Requires sudo rights
## ipbb sim virtualtap --ip 192.168.231.1

# Every time the sources
ipbb sim make-project

# Run the simulation
./vsim -c work.top -do ‘run -all’

# In a different window
## ping 192.168.231.128

deactivate
