#!/bin/bash

slot=$1

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

# 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 16 4620 infinite
#sh vtu_init.sh $slot 1.2 4620    5 highfreq
./wr2rf -s $slot api-vtu-window 1.2 16 16 2 

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.1 diag en 50ms
./wr2rf -s $slot vtu 2.2 diag en 50ms

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

echo "Turn off the trigger unit shaper circuit for bunch clock duty cycle improvement"
./wr2rf -s $slot vtu-out 1.2 shape_n off
./wr2rf -s $slot vtu-out 2.2 shape_n off

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.1 softstart
./wr2rf -s $slot vtu 2.2 softstart


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