\chapter{Trigger inputs}\label{ch:triggerinputs}
The six inputs on the \gls{tlu} can be used to generate a global trigger that is then issued to all the \gls{dut}s.\\
Each input has a programmable voltage discriminator that can be configured in the range [-1.3 : +1.3]~V.\\
All the inputs are protected by clamping diodes that limit the input voltage in the range [-5 : +5]~V.\\
The discriminators are followed by edge-finding and \gls{tdc} logic. Currently only negative edges are registered. A future firmware version will implement user-selectable positive or negative edge detection.\\
The output of the edge finding logic is fed into logic to stretch and delay the pulses by a controllable amount. The stretched and delayed trigger pulses are fed into a look-up table that generates the triggers. Figure~\ref{fig:aida-tlu-trigger-path} illustrates the path of the trigger signals through the \gls{tlu}.

\begin{figure}
	\centering
	\includegraphics[width=\linewidth]{./Images/aida-tlu-trigger-path.pdf}
	\caption{Trigger path in \gls{tlu}}
	\label{fig:aida-tlu-trigger-path}
\end{figure}


\section{Trigger logic}\label{ch:triggerLogic}
The \gls{tlu} has six trigger inputs than can be used to generate a valid trigger event. The number of possible different trigger combinations is $2^6= 64$ so a 64-bit word can be used to decide the valid combinations. In the hardware the 64-bit word is split into two 32-bit words (indicated as \gls{msb} and \gls{lsb} word) and the rules to generate the trigger can be specified by the user by writing in the two 32-bit IPBus registers \verb|TriggerPattern_highW| and \verb|TriggerPattern_lowW|: the first stores the 32 most significative bits of the trigger word, the latter stores the least significative bits.\\
The user can select any combination of the trigger inputs and declare it a valid trigger pattern by setting a 1 in the corresponding trigger configuration word. Multiple 1s indicate that the corresponding patterns are OR-ed.\\
Tables~\ref{tab:trigconfigLow} and ~\ref{tab:trigconfigHigh} show an example of how to determine the trigger configuration words: whenever a valid trigger combination is encountered, the user should put a 1 in the corresponding row under the PATTERN column. The pattern thus obtained is the required word to write in the configuration register.\\
It is important to note that this solution allows the user to set veto pattern as well: for instance if only word 31 from table~\ref{tab:trigconfigLow} were picked, then the \gls{tlu} would only register a trigger when the combination $\overline{I_{5}}$ * $I_{4}$ * $I_{3}$ * $I_{2}$ * $I_{1}$ * $I_{0}$ was presented at its inputs. In other words, in this specific case $I_{5}$ would act as a veto signal and the \gls{tlu} would \textbf{not} produce a global trigger if $I_{5}$=1.

\begin{table}[]
\centering
\begin{tabular}{|l|l|l|l|l|l|l||c||c|l|r|}
\hline
DEC & I5 & I4 & I3 & I2 & I1 & I0 & PATTERN & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}CONFIG. \\ WORD\end{tabular}} &                                  & $2^{n}$ \\ \hline
0  & 0  & 0  & 0  & 0  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                & \parbox[t]{2mm}{\multirow{32}{*}{\rotatebox[origin=c]{90}{LOWEST 32-bits}}} & 1                   \\ \cline{1-8} \cline{11-11}
1  & 0  & 0  & 0  & 0  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 2                   \\ \cline{1-8} \cline{11-11}
2  & 0  & 0  & 0  & 0  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 4                   \\ \cline{1-8} \cline{11-11}
3  & 0  & 0  & 0  & 0  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 8                   \\ \cline{1-9} \cline{11-11}
4  & 0  & 0  & 0  & 1  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                  &                                  & 16                  \\ \cline{1-8} \cline{11-11}
5  & 0  & 0  & 0  & 1  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 32                  \\ \cline{1-8} \cline{11-11}
6  & 0  & 0  & 0  & 1  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 64                  \\ \cline{1-8} \cline{11-11}
7  & 0  & 0  & 0  & 1  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 128                 \\ \cline{1-9} \cline{11-11}
8  & 0  & 0  & 1  & 0  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                  &                                  & 256                 \\ \cline{1-8} \cline{11-11}
9  & 0  & 0  & 1  & 0  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 512                 \\ \cline{1-8} \cline{11-11}
10  & 0  & 0  & 1  & 0  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 1024                \\ \cline{1-8} \cline{11-11}
11  & 0  & 0  & 1  & 0  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 2048                \\ \cline{1-9} \cline{11-11}
12  & 0  & 0  & 1  & 1  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                 &                                  & 4096                \\ \cline{1-8} \cline{11-11}
13  & 0  & 0  & 1  & 1  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 8192                \\ \cline{1-8} \cline{11-11}
14  & 0  & 0  & 1  & 1  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 16384               \\ \cline{1-8} \cline{11-11}
15  & 0  & 0  & 1  & 1  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 32768               \\ \cline{1-9} \cline{11-11}
16  & 0  & 1  & 0  & 0  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                 &                                  & 65536               \\ \cline{1-8} \cline{11-11}
17  & 0  & 1  & 0  & 0  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 131072              \\ \cline{1-8} \cline{11-11}
18  & 0  & 1  & 0  & 0  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 262144              \\ \cline{1-8} \cline{11-11}
19  & 0  & 1  & 0  & 0  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 524288              \\ \cline{1-9} \cline{11-11}
20  & 0  & 1  & 0  & 1  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                 &                                  & 1048576             \\ \cline{1-8} \cline{11-11}
21  & 0  & 1  & 0  & 1  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 2097152             \\ \cline{1-8} \cline{11-11}
22  & 0  & 1  & 0  & 1  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 4194304             \\ \cline{1-8} \cline{11-11}
23  & 0  & 1  & 0  & 1  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 8388608             \\ \cline{1-9} \cline{11-11}
24  & 0  & 1  & 1  & 0  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                 &                                  & 16777216            \\ \cline{1-8} \cline{11-11}
25  & 0  & 1  & 1  & 0  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 33554432            \\ \cline{1-8} \cline{11-11}
26  & 0  & 1  & 1  & 0  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 67108864            \\ \cline{1-8} \cline{11-11}
27  & 0  & 1  & 1  & 0  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 134217728           \\ \cline{1-9} \cline{11-11}
28  & 0  & 1  & 1  & 1  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{8}}}                                                                 &                                  & 268435456           \\ \cline{1-8} \cline{11-11}
29  & 0  & 1  & 1  & 1  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 536870912           \\ \cline{1-8} \cline{11-11}
30  & 0  & 1  & 1  & 1  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 1073741824          \\ \cline{1-8} \cline{11-11}
31  & 0  & 1  & 1  & 1  & 1  & 1  & \rotatebox[origin=c]{90}{1}       &                                                                                    &                                  & 2147483648          \\ \hline
\end{tabular}
\caption{Example of configuration word for the least significative bits of the trigger registers: the only valid configuration is represented by $\overline{I_{5}}$ + $I_{4}$ + $I_{3}$ + $I_{2}$ + $I_{1}$ + $I_{0}$, i.e. a trigger is accepted if all the inputs, except $I_{5}$, present a logic 1 at the same time. The user would then write the resulting word 0x80000000 in the TriggerPattern\_lowW register.}
\label{tab:trigconfigLow}
\end{table}



\begin{table}[]
\centering
\begin{tabular}{|l|l|l|l|l|l|l||c||c|l|r|}
\hline
DEC & I5 & I4 & I3 & I2 & I1 & I0 & PATTERN & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}CONFIG. \\ WORD\end{tabular}} &                                  & $2^{n}$ \\ \hline
32  & 1  & 0  & 0  & 0  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                & \parbox[t]{2mm}{\multirow{32}{*}{\rotatebox[origin=c]{90}{HIGHEST 32-bits}}} & 1                   \\ \cline{1-8} \cline{11-11}
33  & 1  & 0  & 0  & 0  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 2                   \\ \cline{1-8} \cline{11-11}
34  & 1  & 0  & 0  & 0  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 4                   \\ \cline{1-8} \cline{11-11}
35  & 1  & 0  & 0  & 0  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 8                   \\ \cline{1-9} \cline{11-11}
36  & 1  & 0  & 0  & 1  & 0  & 0  & \rotatebox[origin=c]{90}{1}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{F}}}                                                                  &                                  & 16                  \\ \cline{1-8} \cline{11-11}
37  & 1  & 0  & 0  & 1  & 0  & 1  & \rotatebox[origin=c]{90}{1}       &                                                                                    &                                  & 32                  \\ \cline{1-8} \cline{11-11}
38  & 1  & 0  & 0  & 1  & 1  & 0  & \rotatebox[origin=c]{90}{1}       &                                                                                    &                                  & 64                  \\ \cline{1-8} \cline{11-11}
39  & 1  & 0  & 0  & 1  & 1  & 1  & \rotatebox[origin=c]{90}{1}       &                                                                                    &                                  & 128                 \\ \cline{1-9} \cline{11-11}
40  & 1  & 0  & 1  & 0  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{A}}}                                                                  &                                  & 256                 \\ \cline{1-8} \cline{11-11}
41  & 1  & 0  & 1  & 0  & 0  & 1  & \rotatebox[origin=c]{90}{1}       &                                                                                    &                                  & 512                 \\ \cline{1-8} \cline{11-11}
42  & 1  & 0  & 1  & 0  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 1024                \\ \cline{1-8} \cline{11-11}
43  & 1  & 0  & 1  & 0  & 1  & 1  & \rotatebox[origin=c]{90}{1}       &                                                                                    &                                  & 2048                \\ \cline{1-9} \cline{11-11}
44  & 1  & 0  & 1  & 1  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                 &                                  & 4096                \\ \cline{1-8} \cline{11-11}
45  & 1  & 0  & 1  & 1  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 8192                \\ \cline{1-8} \cline{11-11}
46  & 1  & 0  & 1  & 1  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 16384               \\ \cline{1-8} \cline{11-11}
47  & 1  & 0  & 1  & 1  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 32768               \\ \cline{1-9} \cline{11-11}
48  & 1  & 1  & 0  & 0  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                 &                                  & 65536               \\ \cline{1-8} \cline{11-11}
49  & 1  & 1  & 0  & 0  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 131072              \\ \cline{1-8} \cline{11-11}
50  & 1  & 1  & 0  & 0  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 262144              \\ \cline{1-8} \cline{11-11}
51  & 1  & 1  & 0  & 0  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 524288              \\ \cline{1-9} \cline{11-11}
52  & 1  & 1  & 0  & 1  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                 &                                  & 1048576             \\ \cline{1-8} \cline{11-11}
53  & 1  & 1  & 0  & 1  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 2097152             \\ \cline{1-8} \cline{11-11}
54  & 1  & 1  & 0  & 1  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 4194304             \\ \cline{1-8} \cline{11-11}
55  & 1  & 1  & 0  & 1  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 8388608             \\ \cline{1-9} \cline{11-11}
56  & 1  & 1  & 1  & 0  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{0}}}                                                                 &                                  & 16777216            \\ \cline{1-8} \cline{11-11}
57  & 1  & 1  & 1  & 0  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 33554432            \\ \cline{1-8} \cline{11-11}
58  & 1  & 1  & 1  & 0  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 67108864            \\ \cline{1-8} \cline{11-11}
59  & 1  & 1  & 1  & 0  & 1  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 134217728           \\ \cline{1-9} \cline{11-11}
60  & 1  & 1  & 1  & 1  & 0  & 0  & \rotatebox[origin=c]{90}{0}       & \parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{8}}}                                                                 &                                  & 268435456           \\ \cline{1-8} \cline{11-11}
61  & 1  & 1  & 1  & 1  & 0  & 1  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 536870912           \\ \cline{1-8} \cline{11-11}
62  & 1  & 1  & 1  & 1  & 1  & 0  & \rotatebox[origin=c]{90}{0}       &                                                                                    &                                  & 1073741824          \\ \cline{1-8} \cline{11-11}
63  & 1  & 1  & 1  & 1  & 1  & 1  & \rotatebox[origin=c]{90}{1}       &                                                                                    &                                  & 2147483648          \\ \hline
\end{tabular}
\caption{Example of the most significative word of the register: a valid trigger is obtained when the inputs show the same configuration as row DEC 36, 37, 38, 39, 41, 43 and 63. These configuration are in logic OR with that presented in table~\ref{tab:trigconfigLow}. The resulting configuration word is 0x80000AF0.}
\label{tab:trigconfigHigh}
\end{table}
\noindent The default configuration in the firmware is Hi=~0xFFFFFFFF, Low=~0xFFFEFFFE, which means that as long as any trigger input fires, a trigger will be generated. These words are loaded in the \gls{fpga} every time a new image is programmed.\\

\begin{alertinfo}{Trigger logic definition}
    The user should pay attention to what trigger logic they want to define in order to avoid confusion in the data.\\
    A ``1'' in the logic table means that the corresponding input must be active to produce a valid trigger. Similarly, a ``0'' indicates that the corresponding input must be inactive (i.e. is a veto, not an ignore). Any change in input configuration will cause the logic to re-assess the trigger status.\\ Section~\ref{{ch:trigexamples}} provides a few examples.\\
\end{alertinfo}

\begin{alertinfo}{Bit 0 meaning}
    A 1 in the lowest bit of the \gls{lsb} word indicates that $\overline{I_{5}}$ * $\overline{I_{4}}$ * $\overline{I_{3}}$ * $\overline{I_{2}}$ * $\overline{I_{1}}$ * $\overline{I_{0}}$ is a valid trigger combination, so the \gls{tlu} will produce a trigger when all the inputs are inactive (i.e. even if all the inputs are unplugged).\\
    Except for a few very specific cases, this is generally not a desirable behaviour.
\end{alertinfo}

\subsubsection{Trigger configuration helper script}\label{ch:trighelpscript}
A lightweight script is available to help configuring the \gls{tlu} trigger.\\
The file can be found in the:\\
\verb|./Documentation/Misc|\\
folder of the \gls{ohwr} documentation repository (\href{https://ohwr.org/project/fmc-mtlu}{AIDA-2020 TLU}).\\
The script is written in Python 3.x and it should be possible to run it in stand-alone mode on any machine by simply using the command\\
\verb|Python trigger_configuration_helper|\\
This will create an interactive shell; when started, the shell will ask a series of questions to the user and then generates the two 32-bit words that need to be written in the trigger registers.\\
The script is only meant to be a quick and simple way to generate the configuration words without having to dwell too much in the details provided in section~\ref{ch:triggerLogic}. This means that it can only offer a limited set of configurations that should, nonetheless, be enough for most of the user cases.\\
To take advantage of the full flexibility of the \gls{tlu} trigger and generate  more advanced configurations (such as mutually excluding trigger inputs) the user should refer to section~\ref{ch:triggerLogic}.\\
To use the script:
\begin{itemize}
  \item In the script folder, type \verb|Python trigger_configuration_helper|. This will open an interactive shell. The shell supports tab-autocompletion.
  \item In the shell, type \verb|start|
  \item For each of the LEMO trigger inputs, specify if the signal is to be considered ACTIVE or VETO. Active signals must be asserted  to create a valid trigger. VETO signals must be de-asserted to create a valid signal. There is also the option to set an input as DO-NOT-CARE (DNC): these inputs are not taken into account for the purpose of trigger generation but they are still time-stamped if asserted.\\
      The DNC option can be useful if the user want to connect signals that are not part of the trigger generation but for which it is required to register the status and the time-stamp whenever a valid trigger is produced.\\
      The DNC option can be used for any unconnected input.\\
      The VETO option should be used, for instance, for any inputs that is used as a shutter signal (see section~\ref{ch:shutter}).
  \item If more than one active input is present, it is possible to configure the \gls{tlu} to generate a trigger if at least $N$ are asserted (as opposed to all of them). The shell will ask for the minimum number of ACTIVE signals that should be asserted, at the same time, to generate a valid trigger.
  \item The shell will produce a list of all the configurations that will produce a valid trigger, followed by the two values to be written in the registers.
  \item If the user is happy with those, they can just copy those values and use them to configure the \gls{tlu} for instance by including them in the EUDAQ configuration file. It is always possible to restart the procedure by typing \verb|start| again.
  \item Please note that a VETO signal is always prioritized: if a VETO signal is asserted, the \gls{tlu} will not produce any trigger.
  \item Also note that a DNC signal, even if asserted, does not contribute to the count of the minimum number of active inputs $N$.
\end{itemize}

\begin{alertinfo}{Warning}
    The script only tells the user what values to write in the trigger configuration registers, but does not write them itself.\\
    It is up to the user to program the \gls{tlu} with those values, in whichever method is preferred (EUDAQ, Python scripts, etc).
\end{alertinfo}

\subsubsection{Example of trigger configurations}\label{ch:trigexamples}
In this example we have connected a pulse generator to two inputs of the \gls{tlu}, namely \verb|IN_1| and \verb|IN_5|. The inputs fire with a small, random delay with respect to each other.\\
In order to ensure that the signals overlap adequately, we use the \emph{stretch} register (see chapter~\ref{ch:triggerLogic}) to increase the length of the pulses: we extend \emph{in0} to 10 clock cycles and \emph{in4} to 8 clock cycles, where the clock has a frequency of 160~MHz. The resulting signals are shown in figure~\ref{Fig:exampleExtendedTriggers}.
\begin{figure}[h]
  \centering
  \includegraphics[width=.99\textwidth]{./Images/Initial.png}
  \caption{Input pulses (yellow) and corresponding stretched signals (red). Input 0 is stretched by 10 cycles, input 4 by 8, hence the difference in pulse widths.}
  \label{Fig:exampleExtendedTriggers}
\end{figure}\\
We can now define the trigger logic to be used to assert a valid trigger: we only consider the lower 32-bits of the trigger word and see how different values can produce very different results.
\begin{itemize}
    \item Trigger \gls{lsb} word= 0x00020000. This indicates that the only valid trigger combination occurs when both \verb|IN_1| and \verb|IN_5| are high. The valid trigger goes high 1 clock cycle after this condition is met and remains high up to 1 clock cycle after the condition is no longer valid. This is illustrated in figure~\ref{Fig:exampleTrig00020000}.
        \begin{figure}[h]
            \centering
            \includegraphics[width=.99\textwidth]{./Images/Trigger0x00020000.png}
            \caption{Trigger configuration 0x00020000. The valid trigger (blue) is asserted only when both signals are high. This condition occurs at frame 39. The trigger is asserted on the following frame.}
            \label{Fig:exampleTrig00020000}
        \end{figure}\\
    \item Trigger \gls{lsb} word= 0x00020002. This indicates that a valid trigger is achieved in two separated configurations (in logic OR): when both inputs are high at the same time (as in the previous case) or if \verb|IN_1| is active on its own. This is illustrated in figure~\ref{Fig:exampleTrig00020002}. It can be seen that the valid trigger is asserted immediately one clock cycle after \verb|IN_1| is high and remains high as long as this condition is met. One might assume that specifying the combination with \verb|IN_5| is redundant, but the following example should show that this is not the case.
        \begin{figure}[h]
            \centering
            \includegraphics[width=.99\textwidth]{./Images/Trigger0x00020002.png}
            \caption{Trigger configuration 0x00020002. The valid trigger (blue) is asserted if \texttt{IN\_1} is high OR when \texttt{IN\_1} and \texttt{IN\_5} are both high at the same time.}
            \label{Fig:exampleTrig00020002}
        \end{figure}\\
    \item Trigger \gls{lsb} word= 0x00000002. This indicates that the only valid configuration is the one where only \verb|IN_1| is high. It is important to understand that in this configuration all other inputs act as veto. This might produce unexpected results if the user is not careful\footnote{Specifically, pulse stretch, pulse delay and trigger logic must be configured correctly to avoid unwanted results.}.\\
        In figure~\ref{Fig:exampleTrig00000002} it is possible to see that the logic produces two separated trigger valid pulses, both shorter than the ones in previous examples: the first one is due to \verb|IN_1| going high while \verb|IN_5| is low. As soon as \verb|IN_5| goes high, the trigger condition is no longer met. When \verb|IN_5| returns low, a trigger condition is met again because \verb|IN_1| is still high. In this specific case, the double pulse is caused by the different width of the pulses.
        \begin{figure}[h]
            \centering
            \includegraphics[width=.99\textwidth]{./Images/Trigger0x00000002.png}
            \caption{Trigger configuration 0x00000002. The valid trigger (blue) is asserted only when \texttt{IN\_1} is active on its own. As such, two separated trigger pulses are produced because \texttt{IN\_5} goes high and returns low before \texttt{IN\_1}.}
            \label{Fig:exampleTrig00000002}
        \end{figure}\\
\end{itemize}

\section{Stretch and delay}
The trigger logic is designed to detect edge transitions\footnote{By default transitions from low voltage to high voltage are detected (e.g. the leading edge of TTL pulses). The bottom 6 bits of \gls{ipbus} register InvertEdgeW can be used to select triggering on high voltage to low voltage transitions (e.g. NIM pulses, PMT pulses)}  at the trigger inputs and produce a pulse for each transition detected.\\
The pulse has an initial duration of one clock cycle (f= 160~MHz, one cycle 6.25~ns) and occurs on the next rising edge of the 160~MHz internal clock.\\
Each pulse can be stretched and delayed in integer numbers of clock cycles (25~ns by default) to compensate for differences in cable length. Two separate 5-bit registers are used for the task: the value written in the registers will stretch/delay the pulse by a corresponding number of clock cycles.\\
Diagram~\ref{Fig:trigger_stretchdelay} shows the effect of the delay and stretch words on the trigger logic.
\begin{figure}[h]
  \centering
  \includegraphics[width=.95\textwidth]{./Images/tlu_trigger_logic.pdf}
  %\includesvg[width=.90\textwidth]{./Images/tlu_trigger_logic.svg}
  \caption{Effect of the stretch and delay values. In1 is delayed by 2 clock cycles (t1= 12.5~ns) and stretched by 5 clock cycles (t2= 31.25~ns) to create a coincidence window with in5 and produce the resulting trigger signal.}
  \label{Fig:trigger_stretchdelay}
\end{figure}\\
 Further details on how to configure the stretch and delay values are provided in section~\ref{ch:EUDAQPar}. 