# Example: Timing diagram

Published 2007-10-11 | Authors: Jochem Rutgers, Pascal T. Wolkotte

Demonstration of timing.sty, a convenient set of macros for drawing logic timing diagrams. Written by Pascal Wolkotte and Jochem Rutgers.

The macros are also available as a separate sty file:

Authors: ir. Pascal T. Wolkotte and Jochem Rutgers, University of Twente

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.

% Some macros for logic timing diagrams.
%
% Author: ir. Pascal T. Wolkotte and Jochem Rutgers, University of Twente
\documentclass{article}
% Start of timing.sty

% Some macros for logic timing diagrams.
%
% Author: ir. Pascal T. Wolkotte and Jochem Rutgers, University of Twente
% Version: 0.1
% Date: 2007/10/11

\usepackage{tikz}

\newcounter{wavenum}

\setlength{\unitlength}{1cm}
% advance clock one cycle, not to be called directly
\newcommand*{\clki}{
\draw (t_cur) -- ++(0,.3) -- ++(.5,0) -- ++(0,-.6) -- ++(.5,0) -- ++(0,.3)
node[time] (t_cur) {};
}

\newcommand*{\bitvector}{
\draw[fill=#3] (t_cur) -- ++( .1, .3) -- ++(#2-.2,0) -- ++(.1, -.3)
-- ++(-.1,-.3) -- ++(.2-#2,0) -- cycle;
\path (t_cur) -- node[anchor=mid] {#1} ++(#2,0) node[time] (t_cur) {};
}

% \known{val}{length}
\newcommand*{\known}{
\bitvector{#1}{#2}{white}
}

% \unknown{length}
\newcommand*{\unknown}[XXX]{
\bitvector{#1}{#2}{black!20}
}

% \bit{1 or 0}{length}
\newcommand*{\bit}{
\draw (t_cur) -- ++(0,.6*#1-.3) -- ++(#2,0) -- ++(0,.3-.6*#1)
node[time] (t_cur) {};
}

% \unknownbit{length}
\newcommand*{\unknownbit}{
\draw[ultra thick,black!50] (t_cur) -- ++(#1,0) node[time] (t_cur) {};
}

% \nextwave{name}
\newcommand{\nextwave}{
\path (0,\value{wavenum}) node[left] {#1} node[time] (t_cur) {};
}

% \clk{name}{period}
\newcommand{\clk}{
\nextwave{#1}
\FPeval{\res}{(\wavewidth+1)/#2}
\FPeval{\reshalf}{#2/2}
\foreach \t in {1,2,...,\res}{
\bit{\reshalf}{1}
\bit{\reshalf}{0}
}
}

% \begin{wave}[clkname]{num_waves}{clock_cycles}
\newenvironment{wave}[clk]{
\begin{tikzpicture}[draw=black, yscale=.7,xscale=1]
\tikzstyle{time}=[coordinate]
\setlength{\unitlength}{1cm}
\def\wavewidth{#3}
\setcounter{wavenum}{0}
\nextwave{#1}
\foreach \t in {0,1,...,\wavewidth}{
\draw[dotted] (t_cur) +(0,.5) node[above] {t=\t} -- ++(0,.4-#2);
\clki
}
}{\end{tikzpicture}}

%%% End of timing.sty
\begin{document}
\begin{wave}{13}{5}
\nextwave{req\_addr} \bit{0}{.2} \bit{1}{1} \bit{0}{3} \bit{1}{1} \bit{0}{.8}
\nextwave{simulate} \bit{0}{3.1} \bit{1}{1} \bit{0}{1.9}
\nextwave{output} \unknown{3.3} \known{}{.5} \known{}{.5} \unknown{1.7}
\nextwave{prev\_output} \unknown{3.2} \known{old}{1} \unknown{1.8}
\nextwave{differs} \unknownbit{3.3} \bit{1}{.5} \bit{1}{.4} \unknownbit{1.8}
\nextwave{differs\_r} \unknownbit{4.1} \bit{1}{1} \unknownbit{.9}
\nextwave{req} \unknown{4.3} \known{req}{1} \unknown[X]{.7}
\end{wave}

\end{document}


• #1 Johan, April 27, 2009 at 10:34 a.m.

Thanks for this example, this was a great help!

I added a little command to add a label in between waveforms.

\newcommand{\lbl} {
\node[fill=white,nearly opaque, minimum width=\wavewidth cm] (lbl) at (\wavewidth/2,\value{wavenum}) {};
\node (lbl) at (\wavewidth/2,\value{wavenum}) {#1};
\addtocounter{wavenum}{-1}
}

I've only just started to use TeX though so i'm not sure if this is the best way... :)

Thanks!

• #2 Kjell Magne Fauske, April 27, 2009 at 10:43 a.m.

Thanks for sharing Johan. If you want more options for drawing timing diagrams I recommend taking a look at the new tikz-timing package. I have not tried it yet, but it looks good. 