\usetikzlibrary{chains, positioning, shapes, arrows}\usepackage{delarray}
\tikzset{node distance = 6mm and 8mm,
        box/.style = {shape=rectangle, draw, minimum size=2em, outer sep=0pt, on chain=#1},
        sum/.style = {shape=circle, draw, inner sep=0pt, node contents={$+$}},
%        every path/.append style = {-latex'}
        }


\newcommand{\fibLFSR}[1] % boxes, taps
{     \begin{scope}[start chain=ch1 going left,decoration={
      markings,
      mark=at position 0.5 with {\arrow{>}}}]
    
    \def\inputs{#1}
    % \newcommand\boxes{5}
    \pgfmathtruncatemacro{\boxes}{1}
% this is a real dumb way of getting the first input
    \foreach[count=\j] \t in \inputs{
      \ifnum\j>1
      \else
      \global\let\boxes\t
      \fi
    }
              

    \foreach[count=\j] \i in {\inputs}{
      \ifnum\j<2
      \renewcommand\boxes{5}
      % \draw (0,0) -- (0,10);
      % \pgfmathtruncatemacro{\boxes}{5+0} \fi%
      \fi
    }
    
    \def\taps{#1}
    % \def \boxes {#1} %numboxes
    % \newcommand{\taps}{#2} %tap pos
        \pgfmathtruncatemacro{\boxes}{\boxes - 1}
        \pgfmathtruncatemacro{\boxesMinus}{\boxes - 1}
        \def\scopeyshift{19mm}
          % 1. draw the boxes
          \foreach \i in {0,...,\boxes}{
                \node [box=ch1] (ch1-\i) { }; %{$s_{\i}$};
              }\foreach \i in {0,...,\boxesMinus}{
                
                \pgfmathtruncatemacro{\nextNode}{1 + \i}
                \draw (ch1-\nextNode) -- (ch1-\i); %\pgfmathadd{\i}{1});
              }
              % 2. draw the loop

              \draw[->] (ch1-0) -- ($(ch1-0) + (2,0)$) node (knot){};
            %  \draw 

              \draw[fill=black] ($(knot) - (1,0)$) circle[radius=0.75pt];
              \draw [postaction={decorate}] ($(knot) - (1,0)$) -- ($(knot) - (1,1)$); \draw[->]($(knot) - (1,1)$)--
              ($(ch1-\boxes) - (1,1)$) -- ($(ch1-\boxes) - (1,0)$) --
              ($(ch1-\boxes)- (4mm,0)$); % doesn't hit the edge, don't
              % know why

              % now add taps

              % if node i is a tap, we want a junction between i and
              % i+1, a line down from the junction to the feedback
              % loop, and a (+) at the bottom
              

              \foreach[count=\j] \t in \taps{
                \ifnum\j>1
                \draw[fill=black] ($(ch1-\t) + (7mm,0)$)
                circle[radius=0.75pt];
                \draw[postaction={decorate}] ($(ch1-\t) + (7mm,0)$) -- ($(ch1-\t) +
                (7mm,-1)$) node (jn) [sum] {};\fi
              }
              
              % \draw ($(knot) - (1,0)$) |- (c2);
            
            
            % \node (c2) [sum,below=of ch1-2];
            % \node (c6) [sum,below=of ch1-6];
            
            % \draw (c2) edge (c6);
            
             
            % \draw (c6) -| ([xshift=-5mm] ch1-7.west) -- (ch1-7);
            % \draw (ch1-2) -- (c2);
            % \draw (ch1-6) -- (c6); 
        \end{scope}
      }

\newcommand{\galLFSR}[1] % boxes, taps
{     \begin{scope}[start chain=ch1 going left,decoration={
      markings,
      mark=at position 0.5 with {\arrow{>}}}]
    
    \def\inputs{#1}
    % \newcommand\boxes{5}
    \pgfmathtruncatemacro{\boxes}{1}
% this is a real dumb way of getting the first input
    \foreach[count=\j] \t in \inputs{
      \ifnum\j>1
      \else
      \global\let\boxes\t
      \fi
    }
              

    \foreach[count=\j] \i in {\inputs}{
      \ifnum\j<2
      \renewcommand\boxes{5}
      % \draw (0,0) -- (0,10);
      % \pgfmathtruncatemacro{\boxes}{5+0} \fi%
      \fi
    }
    
    \def\taps{#1}
    % \def \boxes {#1} %numboxes
    % \newcommand{\taps}{#2} %tap pos
        \pgfmathtruncatemacro{\boxes}{\boxes - 1}
        \pgfmathtruncatemacro{\boxesMinus}{\boxes - 1}
        \def\scopeyshift{19mm}
          % 1. draw the boxes
          \foreach \i in {0,...,\boxes}{
                \node [box=ch1] (ch1-\i) { }; %{$s_{\i}$};
              }\foreach \i in {0,...,\boxesMinus}{
                
                \pgfmathtruncatemacro{\nextNode}{1 + \i}
                \draw (ch1-\nextNode) -- (ch1-\i); %\pgfmathadd{\i}{1});
              }
              % 2. draw the loop

              \draw[->] (ch1-0) -- ($(ch1-0) + (2,0)$) node (knot){};
            %  \draw 

              \draw[fill=black] ($(knot) - (1,0)$) circle[radius=0.75pt];
              \draw [postaction={decorate}] ($(knot) - (1,0)$) -- ($(knot) - (1,1)$);
              \draw [->] ($(knot) - (1,1)$) --
              ($(ch1-\boxes) - (1,1)$) -- ($(ch1-\boxes) - (1,0)$) --
              ($(ch1-\boxes)- (4mm,0)$); % doesn't hit the edge, don't
              % know why

              % now add taps

              % if node i is a tap, we want a junction between i and
              % i+1, a line down from the junction to the feedback
              % loop, and a (+) at the TOP
              

              \foreach[count=\j] \tb in \taps{
                \ifnum\j>1
                \pgfmathtruncatemacro{\t}{\boxes-\tb+1}; %galois, reverse
                \draw[fill=black] ($(ch1-\t) + (7mm,-1)$)
                circle[radius=0.75pt];

                \draw[postaction={decorate}] ($(ch1-\t) + (7mm,-1)$) -- ($(ch1-\t) +
                (7mm,0)$) node (jn) [sum] {};\fi
              }
              
              % \draw ($(knot) - (1,0)$) |- (c2);
            
            
            % \node (c2) [sum,below=of ch1-2];
            % \node (c6) [sum,below=of ch1-6];
            
            % \draw (c2) edge (c6);
            
             
            % \draw (c6) -| ([xshift=-5mm] ch1-7.west) -- (ch1-7);
            % \draw (ch1-2) -- (c2);
            % \draw (ch1-6) -- (c6); 
        \end{scope}
      }

\newcommand{\labLFSR}[1] % labels; we assume there are the right
% number
{
  \def\boxes{1}
  \foreach[count=\j] \l in {#1}{
    \global\let\boxes\j
  }
  % reverse order
  \foreach[count=\j] \l in {#1}{
    \pgfmathtruncatemacro{\boxNum}{\boxes-\j}
    \node at (ch1-\boxNum) {\l};
    }
  }

  \newcommand{\fibSetup}[1]
    {
\begin{scope}[start chain=ch1 going left,decoration={
      markings,
      mark=at position 0.5 with {\arrow{>}}}]
    
    \def\inputs{#1}
    % \newcommand\boxes{5}
    \pgfmathtruncatemacro{\boxes}{1}
% this is a real dumb way of getting the first input
    \foreach[count=\j] \t in \inputs{
      \ifnum\j>1
      \else
      \global\let\boxes\t
      \fi
    }
              

    \foreach[count=\j] \i in {\inputs}{
      \ifnum\j<2
      \renewcommand\boxes{5}
      % \draw (0,0) -- (0,10);
      % \pgfmathtruncatemacro{\boxes}{5+0} \fi%
      \fi
    }
    
    \def\taps{#1}
    % \def \boxes {#1} %numboxes
    % \newcommand{\taps}{#2} %tap pos
        \pgfmathtruncatemacro{\boxes}{\boxes - 1}
        \pgfmathtruncatemacro{\boxesMinus}{\boxes - 1}
        \def\scopeyshift{19mm}
          % 1. draw the boxes
          \foreach \i in {0,...,\boxes}{
                \node [box=ch1] (ch1-\i) { }; %{$s_{\i}$};
              }\foreach \i in {0,...,\boxesMinus}{
                
                \pgfmathtruncatemacro{\nextNode}{1 + \i}
                \draw (ch1-\nextNode) -- (ch1-\i); %\pgfmathadd{\i}{1});
              }
              % 2. draw the loop

              \draw[->] (ch1-0) -- ($(ch1-0) + (2,0)$) node (knot){};
            %  \draw 

              \draw[fill=black] ($(knot) - (1,0)$) circle[radius=0.75pt];
              \draw [postaction={decorate}] ($(knot) - (1,0)$) --
              ($(knot) - (1,1)$) node () [label=$\qquad c_\boxes$] {};
              \draw[->]($(knot) - (1,1)$)--
              ($(ch1-\boxes) - (1,1)$) -- ($(ch1-\boxes) - (1,0)$) --
              ($(ch1-\boxes)- (4mm,0)$); % doesn't hit the edge, don't
              % know why

              % now add taps

              % if node i is a tap, we want a junction between i and
              % i+1, a line down from the junction to the feedback
              % loop, and a (+) at the bottom
              

              \foreach[count=\j] \t in {0, ..., \boxes}{
                \ifnum\j>1
                \pgfmathtruncatemacro{\l}{\boxes - \t}
                
                \draw[fill=black] ($(ch1-\t) + (7mm,0)$)
                circle[radius=0.75pt];
                \draw[postaction={decorate}] ($(ch1-\t) + (7mm,0)$) --
                 ($(ch1-\t) + (7mm,-1)$) node (jn) [label=$\qquad c_\l$, sum] {};\fi
              }
              
              % \draw ($(knot) - (1,0)$) |- (c2);
            
            
            % \node (c2) [sum,below=of ch1-2];
            % \node (c6) [sum,below=of ch1-6];
            
            % \draw (c2) edge (c6);
            
             
            % \draw (c6) -| ([xshift=-5mm] ch1-7.west) -- (ch1-7);
            % \draw (ch1-2) -- (c2);
            % \draw (ch1-6) -- (c6); 
        \end{scope}


      }


    