#!/bin/bash

slot=$1

# Check we have basic access to the register space over VME
./wr2rf -s $slot hwver

# Enable RFNCO to receive FTWs via WR
./wr2rf -s $slot nco-wrcfg 1
./wr2rf -s $slot nco-wrcfg 2

# Stop the vtus if they are currently running
./wr2rf -s $slot vtu 1.1 softstop
./wr2rf -s $slot vtu 1.2 softstop
./wr2rf -s $slot vtu 2.2 softstop
./wr2rf -s $slot vtu 2.1 softstop

# Basic programming of each VTU
# TU 1 programmed to generate frev, 
# TU 2 programmed to generate bunch clock
# With this setup, the first frev occurs at the start of the second turn
# and the first bunch clock occurs at the start of the third turn
sh vtu_init.sh $slot 1.1 4620 4620 infinite
sh vtu_init.sh $slot 1.2 4620    5 highfreq

sh vtu_init.sh $slot 2.1 4620 4620 infinite
sh vtu_init.sh $slot 2.2 4620    5 highfreq

# Enable diag units and counters
./wr2rf -s $slot vtu 1.1 diag en 50ms
./wr2rf -s $slot vtu 1.2 diag en 50ms
./wr2rf -s $slot vtu 2.2 diag en 50ms
./wr2rf -s $slot vtu 2.1 diag en 50ms

echo "Bring trigger units out of reset" 
./wr2rf -s $slot vtu-out 1.1 reset 0
./wr2rf -s $slot vtu-out 1.2 reset 0
./wr2rf -s $slot vtu-out 2.1 reset 0
./wr2rf -s $slot vtu-out 2.2 reset 0

echo "Dump trigger units output status " 
./wr2rf -s $slot vtu-out 1.1
./wr2rf -s $slot vtu-out 1.2
./wr2rf -s $slot vtu-out 2.1
./wr2rf -s $slot vtu-out 2.2

# Use a software start signal
./wr2rf -s $slot vtu 1.1 softstart
./wr2rf -s $slot vtu 1.2 softstart
./wr2rf -s $slot vtu 2.2 softstart
./wr2rf -s $slot vtu 2.1 softstart

echo "Trigger Units waiting for a sync pulse - via RFframe FTW ..."
