pipeline


\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{longtable}

\iffalse
The following table illustrates the state of a 4-stage processor pipeline with data forwarding paths during the execution of the following program:

LOAD R1, R8, numX   % Carreguem x a R1
LOAD R2, R8, num1   % Carreguem 2 a R2
LOAD R3, R8, num2   % Carreguem 3 a R3
BLE R1, R7, final   % Comprovem que x > -1
SQR R4, R1          % Calculem x^2
DIV R5, R1, R3      % Calculem x/3
DIV R3, R4, R2      % Calculem x^2/2
MUL R5, R4, R5      % x/3 * x^2 = x^3/3
SQR R6, R3          % (x^2/2)^2 = x^4/4
SUB R1, R1, R3      % Calculem x - x^2/2
SUB R5, R5, R6      % Calculem x^3/3 - x^4/4
ADD R1, R1, R5      % Calculem x- x^2/2 + x^3/3 - x^4/4
STORE R1, R8, numY  % Guardem el resultat a memoria
final:

\fi



\begin{document}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|l|} 
\hline
 \textbf{Cycle}      & \textbf{0}             & \textbf{1}             & \textbf{2}           & \textbf{3}              & \textbf{4}               & \textbf{5}                           & \textbf{6}                                           & \textbf{7}           & \textbf{8}           & \textbf{9}            \\ 
\hline
\textbf{Read PC}     & 100                    & 101                    & 102                  & 103                     & 104                      & 105                                  & 106                                                  & 107                  & 108                  & 109                   \\
\textbf{Write IR}    & LOAD                   & LOAD                   & LOAD                 & BLE                     & SQR                      & DIV                                  & NO-OP                                                & DIV                  & MUL                  & NO-OP                 \\ 
\hline
\textbf{Write RA}    &                        &                        &                      &                         & R1                       & R1                                   & R1                                                   &                      & R4                   & R4                    \\
\textbf{Write RB}    &                        & R8                     & R8                   & R8                      & R7                       &                                      & R3                                                   &                      & R2                   & R5                    \\ 
\hline
\textbf{Write AR}    &                        &                        & {[}R8]+numX          & {[}R8]+num1             & {[}R8]+num2              &                                      &                                                      &                      &                      &                       \\
\textbf{Write DR}    &                        &                        &                      &                         &                          &                                      & R1*R1                                                & R1/R3                &                      & R4/R2                 \\ 
\hline
\textbf{Write CR}    &                        &                        &                      & {[}R1]=x                & {[}R2]=2                 & {[}R3]=3                             &                                                      & {[}R4]=$x^2$         & {[}R5]=$\frac{x}{3}$ &                       \\
\textbf{Write MEM}   &                        &                        &                      &                         &                          &                                      &                                                      &                      &                      &                       \\
\textbf{Write PC}    & 101                    & 102                    & 103                  & 104                     & 105                      & 106                                  & 107                                                  & 108                  & 109                  & 110                   \\ 
\hline
\multicolumn{1}{l}{} & \multicolumn{1}{l}{}   & \multicolumn{1}{l}{}   & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}    & \multicolumn{1}{l}{}     & \multicolumn{1}{l}{}                 & \multicolumn{1}{l}{}                                 & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}  \\ 
\hline
\textbf{Cycle}       & \textbf{10}            & \textbf{11}            & \textbf{12}          & \textbf{13}             & \textbf{14}              & \textbf{15}                          & \textbf{16}                                          & \textbf{17}          & \textbf{18}          & \textbf{19}           \\ 
\hline
\textbf{Read PC}     & 110                    & 111                    & 112                  & 113                     & 114                      & 115                                  & 116                                                  & 117                  & 118                  & 119                   \\
\textbf{Write IR}    & SQR                    & SUB                    & SUB                  & ADD                     & STORE                    &                                      &                                                      &                      &                      &                       \\ 
\hline
\textbf{Write RA}    &                        & R3                     & R1                   & R5                      & R5-R6   \tikzmark{b4}                    & \tikzmark{c2}R1+R5                                  &                                                      &                      &                      &                       \\
\textbf{Write RB}    &                        &                        & R3                   & \tikzmark{a2}(R6)                      & \tikzmark{b2}(R1)                       &                                      &                                                      &                      &                      &                       \\ 
\hline
\textbf{Write AR}    &                        &                        &                      &                         &                          &                                      & {[}R8]+numY                                          &                      &                      &                       \\
\textbf{Write DR}    & R4*R5                  &                        & R3*R3                & R1-R3                   & R5-R6 \tikzmark{b3}                   & \tikzmark{c1} R1+R5                                &                                                      &                      &                      &                       \\ 
\hline
\textbf{Write CR}    & {[}R3]=$\frac{x^2}{2}$ & {[}R5]=$\frac{x^3}{3}$ &                      & \tikzmark{a1}{[}R6]=$\frac{x^4}{4}$ & \tikzmark{b1}{[}R1]=$x-\frac{x^2}{2}$ & {[}R5]=$\frac{x^3}{3}-\frac{x^4}{4}$ & {[}R1]=$x-\frac{x^2}{2}+\frac{x^3}{3}-\frac{x^4}{4}$ &                      &                      &                       \\
\textbf{Write MEM}   &                        &                        &                      &                         &                          &                                      &                                                      & {[}R8+numY]=[R1]     &                      &                       \\
\textbf{Write PC}    & 111                    & 112                    & 113                  & 114                     & 115                      &                                      &                                                      &                      &                      &                       \\
\hline
\end{tabular}
 \begin{tikzpicture}[overlay, remember picture, yshift=.25\baselineskip, shorten >=.5pt, shorten <=.5pt]
    \draw [->] ({pic cs:a1}) [bend left] to ({pic cs:a2});
    \draw [->] ({pic cs:b1}) [bend left] to ({pic cs:b2});
    \draw [->] ({pic cs:b3}) [bend right] to ({pic cs:b4});
    \draw [->] ({pic cs:c1}) [bend left] to ({pic cs:c2});
    
\end{tikzpicture}
\end{document}