Example: D flip-flops and shift register

Published 2009-08-01 | Author: Martin Scharrer

Example of a custom node shape for drawing D flip-flops. The shape is used to draw a serial shift register.

Download as: [PDF] [TEX]  •  [Open in Overleaf]

D flip-flops and shift register

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% D flip-flops (DFFs) and shift register
% Author: Martin Scharrer



% Data Flip Flip (DFF) shape
  % The 'minimum width' and 'minimum height' keys, not the content, determine
  % the size
  % This is redundant, but makes some things easier:
  % Inherit from rectangle

  % Define same anchor a normal rectangle has
  \anchor{north}{\northeast \pgf@x=0pt}
  \anchor{east}{\northeast \pgf@y=0pt}
  \anchor{south}{\southwest \pgf@x=0pt}
  \anchor{west}{\southwest \pgf@y=0pt}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast \pgf@x=-\pgf@x}
  \anchor{south west}{\southwest}
  \anchor{south east}{\southwest \pgf@x=-\pgf@x}
    \advance\pgf@x by -.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by -.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by +.5\dp\pgfnodeparttextbox%

  % Define anchors for signal ports
  % Draw the rectangle box and the port labels
    % Rectangle box
    % Angle (>) for clock input
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@xc=\pgf@x \pgf@yc=\pgf@y
    \pgfmathsetlength\pgf@x{1.6ex} % size depends on font size
    \advance\pgf@ya by \pgf@x
    \advance\pgf@xb by \pgf@x
    \advance\pgf@yc by -\pgf@x

    % Draw port labels
    \tikzset{flip flop/port labels} % Use font from this style







% Key to add font macros to the current font
\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}} 

% Define default style for this node
\tikzset{flip flop/port labels/.style={font=\sffamily\scriptsize}}
\tikzset{every dff node/.style={draw,minimum width=2cm,minimum 
height=2.828427125cm,very thick,inner sep=1mm,outer sep=0pt,cap=round,add 



\begin{tikzpicture}[font=\sffamily,>=triangle 45]
  \def\N{7}  % Number of Flip-Flops minus one

  % Place FFs
  \foreach \m in {0,...,\N}
    \node [shape=dff] (DFF\m) at ($ 3*(\m,0) $) {Bit \#\m};

  % Connect FFs (Q1 with D1, etc.)
  \def\p{0}  % Used to save the previous number
  \foreach \m in {1,...,\N} { % Note that it starts with 1, not 0
    \draw [->] (DFF\p.Q) -- (DFF\m.D);

  % Connect and label data in- and output port
  \draw [<-] (DFF0.D) -- +(-1,0) node [anchor=east] {input} ;
  \draw [->] (DFF\N.Q) -- +(1,0) node [anchor=west] {output};

  % 'Reset' port label
  \path (DFF0) +(-2cm,+2cm) coordinate (temp)
    node [anchor=east] {reset};
  % Connect resets
  \foreach \m in {0,...,\N}
    \draw [->] (temp) -| (DFF\m.R);

  % 'Set' port label
  \path (DFF0) +(-2cm,-2cm) coordinate (temp)
    node [anchor=east] {set};
  % Connect sets
  \foreach \m in {0,...,\N}
    \draw [->] (temp) -| (DFF\m.S);

  % Clock port label
  \path (DFF0) +(-2cm,-2.5cm) coordinate (temp)
    node [anchor=east] {clock};
  \foreach \m in {0,...,\N}
    \draw [->] (temp) -| ($ (DFF\m.CLK) + (-5mm,0) $) --(DFF\m.CLK);

  % Clock port label
  \path (DFF0) +(-2cm,-3cm) coordinate (temp)
    node [anchor=east] {clock enable};
  \foreach \m in {0,...,\N}
    \draw [->] (temp) -| ($ (DFF\m.CE) + (-7.5mm,0) $) --(DFF\m.CE);



  • #1 Will, June 16, 2010 at 1:01 a.m.

    Thanks for the example!

    I have a question though.

    When computing the location of the 'text' anchor, you use pgfnodeparttextbox. The PGF manual says that this should be avoided:

    "The problem is that the box \pgfnodeparttextbox will most likely not have the correct size when the anchor is computed. After all, the anchor position might be recomputed at a time when several other nodes have been created."

    Taken from page 483 of Manual 2.0

    The code above seems to work fine, however, and I don't know any good alternative.

    Does anyone else know a good alternative which achieves the same functionality but without pgfnodeparttextbox?


  • #2 Sophia, August 26, 2013 at 7:25 a.m.

    This is the same for home contents and general car insurance. Something basic is usually perceived as only what is required by law, meaning basic liability. When looking for your Auto insurance Kettering, ensure that the insurance agency charges you fairly for the service provided.

  • #3 Nicholas, August 28, 2013 at 10:31 a.m.

    Very good website you have here but I was wanting to know if you knew of any community forums that cover the same topics talked about in this article?

    I'd really like to be a part of community where I can get feed-back from other knowledgeable people that share the same interest. If you have any suggestions, please let me know.

    Bless you!

Adding comments is currently not enabled.