#!/bin/bash

# The aim is to provide a methodology for 
# setting the input and output delays such 
# that we can achieve glitch free and consistent 
# RF phasing of the trigger unit output signals

slot=$1
rf=$2

sh wr2rf_init.sh $slot
sh vtu_setup.sh $slot
./wr2rf -s $slot nco-source local

########
# AD9910
########
echo ""; echo "";
echo "We need to determine that the ad9910 ioupdate timing is valid"
echo -n "Press any to build the DDS timing validation array > "
read text

./wr2rf -s $slot dds-sync-cal

echo ""; echo "";
echo -n "Choose a receiver delay (rows) in an open window > "
read dds_rx_sync_delay
echo ""; echo "";
echo -n "Choose a validation delay (cols) in an open window > "
read dds_rx_val_delay

./wr2rf -s $slot dds-sync-set $dds_rx_sync_delay $dds_rx_val_delay

########
# AD9783
########
echo "Now we're going to visually inspect the IQ-DAC timing for the chosen RF channel"
echo -n "Press any key to build and set the DAC timing array > "
read text

./wr2rf -s $slot dac-timing   $rf
./wr2rf -s $slot dac-validate $rf

echo ""; echo "";
echo -n "Was the IQ-DAC timing sample delay correctly chosen and then validated ? 'y' or 'n' > "
read -r input

if [ "$input" = "n" ]; then 
    while [ "input" = "n" ]; do
	echo -n "Chose a new sample delay > "
	read sample_delay
	./wr2rf -s $slot dac-timing-set $rf $sample_delay
	echo -n "Was the IQ-DAC timing sample delay correctly validated ? 'y' or 'n' > "	
	read -r input
    done
fi


#########
# Set the RF frequency
#########
echo ""; echo "";
echo "INJECTION frequencies for protons (approx):"
echo "       For SFTPRO2: Frev=43278.463683 Hz"
echo "       LHCPILOT:    Frev=43347.158955 Hz"
echo "       SFTION1:     Frev=42966.157844 Hz, lead ions"
echo ""; echo "";
echo -n "Enter the Frev frequency that you wish to validate wr2rf timing for > "
read -r freq

freq_wrclk=125000000
prod=`echo "$freq*(2^48)" | bc -q`
ftw_h1=`echo "$prod/($freq_wrclk)" | bc -q`
ftw_h1_hex=`echo "obase=16;ibase=10; $ftw_h1" | bc -q`

echo ""; echo "";
echo "The FTW that matches your chosen frequency has been calculated as:"
echo "0x$ftw_h1_hex"
./wr2rf -s $slot nco-lcfg $rf 0x$ftw_h1_hex

echo ""; echo "";
echo "We are going to determine the delay required"
echo "for nco_reset into the first trigger unit as a sync signal"
echo "It is composed of three elements:"
echo "  - course delay, allows us to delay nco_reset in 16ns steps up to 2us"
echo "  - fine delay, allows us to delay the nco_reset by up to 7 ns"
echo "  - iodelays, allows us to delay the nco_reset in 75ps up to ~2.5ns"

echo ""; echo "";
echo "The course delay for nco_reset is to avoids glitches in the RF signal"
echo "due to restarting the DDS and RFNCO. RFNCO is not stable until 1us after"
echo "an nco_reset. Recommended value is 75 or 1.2 us"
echo -n "Enter the required course delay, in cycles, 0 - 127 > "
read -r cdelay
./wr2rf -s $slot nco-reset-delay $rf $cdelay

echo ""; echo "";
echo "To determine the fine delay value (1 ns interval) we have to monitor some signals:"
echo "  - nco_reset, from the front lemo TMGIO[3], and our trigger signal"
echo "  - Trigger Unit 1 output, from your chosen RF channel = $rf"
echo -n "Press any key, once you are monitoring the skew between these signals > " 

fdelay=0
./wr2rf -s $slot nco-reset-delay $rf $cdelay $fdelay
echo "Note the skew when fdelay=$fdelay, press any key to increase the value of fdelay > "
read text

fdelay=1
./wr2rf -s $slot nco-reset-delay $rf $cdelay $fdelay
echo "Note the skew when fdelay=$fdelay, press any key to increase the value of fdelay > "
read text

fdelay=2
./wr2rf -s $slot nco-reset-delay $rf $cdelay $fdelay
echo "Note the skew when fdelay=$fdelay, press any key to increase the value of fdelay > "
read text

fdelay=3
./wr2rf -s $slot nco-reset-delay $rf $cdelay $fdelay
echo "Note the skew when fdelay=$fdelay, press any key to increase the value of fdelay > "
read text

fdelay=4
./wr2rf -s $slot nco-reset-delay $rf $cdelay $fdelay
echo "Note the skew when fdelay=$fdelay, press any key to increase the value of fdelay > "
read text

fdelay=5
./wr2rf -s $slot nco-reset-delay $rf $cdelay $fdelay
echo -n "Note the skew when fdelay=$fdelay, press any key to increase the value of fdelay > "
read text

fdelay=6
./wr2rf -s $slot nco-reset-delay $rf $cdelay $fdelay
echo -n "Note the skew when fdelay=$fdelay, press any key to increase the value of fdelay > "
read text

fdelay=7
./wr2rf -s $slot nco-reset-delay $rf $cdelay $fdelay
echo -n "Note the skew when fdelay=$fdelay, press any key to increase the value of fdelay > "
read text

echo "At some value of fdelay, it is expected that the "
echo "trigger unit output will have shifted 1 RF clock cycle later."
echo "This value of fdelay is the WORST value we can use: fdelay_worst"
echo -n "Add or subtract the value of 3 from your observed fdelay_worst and enter > "
read -r fdelay_best
./wr2rf -s $slot nco-reset-delay $rf $cdelay $fdelay_best

