Debian Codegraph @

texmf-dist/source/latex/bitpattern/bitpattern.dtx

raw
1 % \iffalse
2 %
3 % bitpattern.dtx
4 %
5 % This file is part of the bitpattern, a LaTeX package to typeset bit
6 % pattern diagrams.
7 % https://bitbucket.org/bourguet/bitpattern
8 %
9 % Copyright 2005--2015 Jean-Marc Bourguet
10 %
11 % This program is provided under the terms of the LaTeX Project Public
12 % License distributed from CTAN archives in directory
13 % macros/latex/base/lppl.txt.
14 %
15 % Run LaTeX on the file 'bitpattern.ins' to get a .sty to place in
16 % your tex hierarchy.
17 %
18 % Run LaTeX on the file 'bitpattern.dtx' to get a manual. If the
19 % |\OnlyDescription| command is commented out, you get commented
20 % sources in the manual, else you get only the user's manual part.
21 %
22 %<*dtx>
23 \ProvidesFile{bitpattern.dtx}
24 %</dtx>
25 % \fi
26 %
27 %% \CheckSum{412}
28 %
29 % \title{Bit pattern diagrams}
30 % \author{Jean-Marc Bourguet\\
31 % \texttt{[email protected]}}
32 %
33 % \maketitle
34 % \begin{abstract}
35 % The |bitpattern| package is designed to typeset bit patterns as
36 % they may appear in description of data format, hardware registers
37 % or transmission protocols. It covers thus more or less the same
38 % application domain as the package |register| and is somewhat
39 % related to |bytefield|.
40 %
41 % Comparared to |register| the formating is more compact, the
42 % syntax less verbose and |bitpattern| allows to choose between big
43 % endian and little endian bit numbering. But |bitpattern| is less
44 % well adapted to the use of long names for the fields and has no
45 % provision for a reset value. |bytefield| is more adequate to
46 % describe multi-word protocols, while |bitpattern| is more adapted
47 % to describe the intra-word structure of a single word.
48 % \end{abstract}
49 %
50 % \iffalse
51 %<*driver>
52 \documentclass{ltxdoc}
53 \RequirePackage[numberFieldsOnce,bigEndian,numberBitsAbove]%
54 {bitpattern}
55 \renewcommand\descriptionlabel[1]{\hspace{\labelsep}\texttt{#1}}
56 %\OnlyDescription
57 \begin{document}
58 \DocInput{bitpattern.dtx}
59 \end{document}
60 %</driver>
61 % \fi
62 % \section{Examples}
63 %
64 % We'll the instruction formats of two processors to describe the
65 % features of |bitpattern|, the DEC PDP-10 and the Intel 8080.
66 %
67 % The PDP-10 was a word-adressable 36-bit computer, something quite
68 % strange to our eyes (it's most familiar feature is probably the
69 % fact that it uses 2's complement) and the reason for which I've
70 % chosen it is that it was the computer on which \TeX{} was first
71 % implemented.
72 %
73 % The PDP-10 has two instruction formats, one used for most of the
74 % instructions:
75 %
76 % \noindent\bitpattern{Op}[9]{AC}[4]IX[4]Y[18]/
77 %
78 % \noindent and the other used for IO instructions:
79 %
80 % \noindent\bitpattern[numberBitsBelow]{111}[3]{Dev}[7]{IOP}[3]IX[4]Y[18]/
81 %
82 % As well at putting the bit numbers above or below the field
83 % description, |bitpattern| allows to number all the bits:
84 %
85 % \noindent\bitpattern[numberAllBits]{Op}[9]{AC}[4]IX[4]Y[18]/
86 %
87 % \noindent or only at the start and end of a field:
88 %
89 % \noindent\bitpattern[numberBitsBelow,numberFieldsTwice]{111}[3]{Dev}[7]{IOP}[3]IX[4]Y[18]/
90 %
91 % Having a long field like \emph{Y} here may take more place than
92 % wanted. |bitpattern| allows to reduce that:
93 %
94 % \noindent\bitpattern{Op}[9]{AC}[4]IX[4]Y[18][9]/
95 %
96 % Other ways to avoid overstepping in the margin include splitting
97 % the pattern in two:
98 %
99 % \noindent\bitpattern[bitWidth=1.5em,numberAllBits]{Op}[9]{AC}[4]IX[4]/ \\
100 % \hspace*{1pt plus 1filll}\bitpattern[numberBitsBelow,startBit=18,bitWidth=1.5em,numberAllBits] Y[18]/
101 %
102 % \noindent and reducing the width of the cells:
103 %
104 % {\centering\bitpattern[bitWidth=0.8em]{Op}[9]{AC}[4]IX[4]Y[18]/ \par}
105 %
106 % The PDP-10 numbered its bits in the big endian way. That's not the
107 % case of the Intel 8080. The 8080 was an 8-bit computer. Trying to
108 % pack instructions in 8-bit forces to use a lot of formats. One of
109 % them was used for the move instruction, and let's take that
110 % opportunity to start and compact the layout of the bit field by
111 % changing the format of the bit numbers and the field descriptions:
112 %
113 % \renewcommand\bpFormatBitNumber[1]{{\tiny\ttfamily\emph{\strut#1}}}
114 % \renewcommand\bpFormatField[1]{{\scriptsize\ttfamily\strut#1}}
115 % {\centering\bitpattern[littleEndian,startBit=7,numberBitsBelow]{01}[2]{D}[3]{S}[3]/\par}
116 %
117 %
118 % \bpSetTickHeight{1pt}
119 % \newlength{\bpDocExampleHeight}
120 % \settoheight{\bpDocExampleHeight}{{\scriptsize\ttfamily 0}}
121 % \newcommand{\bpDocExampleStrut}{\rule{0pt}{\bpDocExampleHeight}}
122 % \renewcommand\bpFormatField[1]{{\scriptsize\ttfamily\bpDocExampleStrut #1}}
123 %
124 % After that it is possible to still be more compact by removing the
125 % numbers, reducing the height of the ticks and avoiding the provide
126 % the space for ascender and descenders in the field name (obviously
127 % that is possible only with convenient field names). When that's
128 % done, you can put a bit pattern in a paragraph without disturbing
129 % it too much, like this
130 % \bitpattern[noBitNumbers,littleEndian,startBit=7]{01}[2]{D}[3]{S}[3]/.
131 % With some text afterwards to see how the next line is handled. The
132 % result seems quite readable, remark for instance that the baseline
133 % of the field description is aligned with the baseline of the line
134 % in which it is included.
135 %
136 % \section{Interface}
137 %
138 % \subsection{\cmd{\bitpattern}}
139 %
140 % \DescribeMacro{\bitpattern} The |\bitpattern| macro is the macro
141 % which formats the patterns.
142 %
143 % \begin{quote}
144 % \cmd{\bitpattern}\oarg{format}\marg{name}\oarg{size}\oarg{width}\dots\verb+/+
145 % \end{quote}
146 %
147 % The first optional argument allows to control how the formatting is
148 % made and is the combination of the following keys:
149 % \begin{description}
150 % \item[littleEndian] indicates that bit numbering is little endian,
151 % that is the leftward bit has the biggest number;
152 % \item[bigEndian] indicates that bit numbering is big endian, that
153 % is the leftward bit has the lowest number;
154 % \item[numberBitsAbove] indicates that the bit numbers should be put
155 % above the fields;
156 % \item[numberBitsBelow] indicates that the bit numbers should be put
157 % below the fields;
158 % \item[noBitNumbers] indicates that there should be no bit numbers;
159 % \item[numberFieldsOnce] indicates that the fields should have
160 % only one bit number;
161 % \item[numberFieldsTwice] indicates that the fields should have
162 % two bit numbers;
163 % \item[numberAllBits] indicates that the fields shouldn't have a bit
164 % number indication;
165 % \item[startBit=X] indicates that the number for the leftmost bit
166 % should be $X$;
167 % \item[bitWidth=dimen] indicates that the size taken by a bit should
168 % be \emph{dimen};
169 % \item[tickHeight=dimen] indicates that the size taken by the small
170 % ticks marking the bits should be \emph{dimen}.
171 % \end{description}
172 %
173 % After the optional argument comes the description of the
174 % fields. Each field is described by its name as a mandatory argument
175 % (which does not have to be included in braces if it takes one
176 % character) followed by two optional arguments giving the size of
177 % the field (1 if omitted) and the width it should take (the same as
178 % its size if it is not specified).
179 %
180 % Field descriptions are ended by a \verb+/+.
181 %
182 % \subsection{Package options}
183 %
184 % The package |bitpattern| accept the following options which set up
185 % the default values for the formatting controls:
186 % \begin{description}
187 % \item[littleEndian] indicates that the default for bit numbering is
188 % little endian, that is the leftward bit has the biggest number;
189 % \item[bigEndian] indicates that the default for bit numbering is
190 % big endian, that is the leftward bit has the lowest number;
191 % \item[numberBitsAbove] indicates that by default the bit numbers
192 % should be put above the fields;
193 % \item[numberBitsBelow] indicates that by default the bit numbers
194 % should be put below the fields;
195 % \item[noBitNumbers] indicates that by default there should be no
196 % bit numbers;
197 % \item[numberFieldsOnce] indicates that by default the fields
198 % should have only one bit number;
199 % \item[numberFieldsTwice] indicates that by default the fields
200 % should have two bit numbers;
201 % \item[numberAllBits] indicates that by default the fields shouldn't
202 % have a bit number indication.
203 % \end{description}
204 %
205 % \subsection{Commands controlling the format}
206 %
207 % \DescribeMacro{\bpLittleEndian} |\bpLittleEndian| changes the
208 % default bit numbering to little endian.
209 %
210 % \DescribeMacro{\bpBigEndian} |\bpBigEndian| changes the default bit
211 % numbering to little endian.
212 %
213 % \DescribeMacro{\bpNumberBitsAbove} |\bpNumberBitsAbove| changes the
214 % default to having the numbering above the fields.
215 %
216 % \DescribeMacro{\bpNumberBitsBelow} |\bpNumberBitsBelow| changes the
217 % default to having the numbering below the fields.
218 %
219 % \DescribeMacro{\bpNoBitNumbers} |\bpNoBitNumbers| changes the
220 % default to having no numbering.
221 %
222 % \DescribeMacro{\bpNumberFieldsOnce} |\bpNumberFieldsOnce| changes the
223 % default to having the numbering done once per field.
224 %
225 % \DescribeMacro{\bpNumberFieldsTwice} |\bpNumberFieldsTwice| changes
226 % the default to having the numbering done twice per field.
227 %
228 % \DescribeMacro{\bpNumberAllBits} |\bpNumberAllBits| changes the
229 % default to having the numbering done for all bits of a field.
230 %
231 % \begin{quote}
232 % \cmd{\bpStartAtBit}\marg{number}
233 % \end{quote}
234 %
235 % \DescribeMacro{\bpStartAtBit} |\bpStartAtBit| gives the default bit
236 % number of the leftmost bit.
237 %
238 % \begin{quote}
239 % \cmd{\bpSetBitWidth}\marg{length}
240 % \end{quote}
241 %
242 % \DescribeMacro{\bpSetBitWidth} |\bpSetBitWidth| gives the default
243 % bit width
244 %
245 % \begin{quote}
246 % \cmd{\bpSetTickHeight}\marg{length}
247 % \end{quote}
248 %
249 % \DescribeMacro{\bpSetTickHeight} |\bpSetTickHeight| gives the
250 % default height for the ticks marking the bits in a multi-bit field.
251 %
252 % \DescribeMacro{\bpFormatField} |\bpFormatField| is a one argument
253 % macro used fo format the field. It can be replaced. Care should
254 % be taken to format all the fields with the same height, so putting
255 % a |\strut| in the replacement is probably in order.
256 %
257 % \DescribeMacro{\bpFormatBitNumber} |\bpFormatBitNumber| is a one
258 % argument macro used fo format the bit numbers. It can be replaced.
259 % Care should be taken to format all the bit numbers with the same
260 % height, so putting a |\strut| in the replacement is perhaps in
261 % order.
262 %
263 % \section{Examples revisited}
264 %
265 % \bpSetTickHeight{2pt}
266 % \renewcommand\bpFormatField[1]{\strut\emph{#1}}
267 % \renewcommand\bpFormatBitNumber[1]{{\tiny\sffamily\strut #1}}
268 % |bitpattern| was loaded by:
269 % \begin{verbatim}
270 % \RequirePackage[numberFieldsOnce,bigEndian,numberBitsAbove]{bitpattern}
271 % \end{verbatim}
272 % \smallskip
273 % \noindent\bitpattern{Op}[9]{AC}[4]IX[4]Y[18]/
274 % \begin{verbatim}
275 % \bitpattern{Op}[9]{AC}[4]IX[4]Y[18]/
276 % \end{verbatim}
277 %
278 % \smallskip
279 % \noindent\bitpattern[numberBitsBelow]{111}[3]{Dev}[7]{IOP}[3]IX[4]Y[18]/
280 % \begin{verbatim}
281 % \bitpattern[numberBitsBelow]{111}[3]{Dev}[7]{IOP}[3]IX[4]Y[18]/
282 % \end{verbatim}
283 %
284 % \smallskip
285 % \noindent\bitpattern[numberAllBits]{Op}[9]{AC}[4]IX[4]Y[18]/
286 % \begin{verbatim}
287 % \bitpattern[numberAllBits]{Op}[9]{AC}[4]IX[4]Y[18]/
288 % \end{verbatim}
289 %
290 % \smallskip
291 % \noindent\bitpattern[numberBitsBelow,numberFieldsTwice]{111}[3]{Dev}[7]{IOP}[3]IX[4]Y[18]/
292 % \begin{verbatim}
293 % \bitpattern[numberBitsBelow,numberFieldsTwice]%
294 % {111}[3]{Dev}[7]{IOP}[3]IX[4]Y[18]/
295 % \end{verbatim}
296 %
297 % \smallskip
298 % \noindent\bitpattern[numberBitsBelow,numberFieldsTwice]111{Dev}[7]{IOP}[3]IX[4]Y[18]/
299 % \begin{verbatim}
300 % \bitpattern[numberBitsBelow,numberFieldsTwice]%
301 % 111{Dev}[7]{IOP}[3]IX[4]Y[18]/
302 % \end{verbatim}
303 %
304 % \smallskip
305 % \noindent\bitpattern{Op}[9]{AC}[4]IX[4]Y[18][9]/
306 % \begin{verbatim}
307 % \bitpattern{Op}[9]{AC}[4]IX[4]Y[18][9]/
308 % \end{verbatim}
309 %
310 % \smallskip
311 % \noindent\bitpattern[bitWidth=1.5em,numberAllBits]{Op}[9]{AC}[4]IX[4]/ \\
312 % \hspace*{1pt plus 1filll}\bitpattern[numberBitsBelow,startBit=18,bitWidth=1.5em,numberAllBits] Y[18]/
313 %
314 % This wasn't composed directly by as two bit patterns, using the
315 % possibility of specifying a starting bit. To put a nicer touch,
316 % the width of the cells has been expanded (the default width had an
317 % overlap so small that going into the margin is a better format),
318 % and the numbering was put above the pattern for the first and below
319 % for the second.
320 % \begin{verbatim}
321 % \noindent\bitpattern[bitWidth=1.5em,numberAllBits]{Op}[9]{AC}[4]IX[4]/ \\
322 % \hspace*{1pt plus 1filll}
323 % \bitpattern[numberBitsBelow,startBit=18,bitWidth=1.5em,numberAllBits] Y[18]/
324 % \end{verbatim}
325 %
326 % \smallskip
327 % {\centering\bitpattern[bitWidth=0.8em]{Op}[9]{AC}[4]IX[4]Y[18]/ \par}
328 % \begin{verbatim}
329 % {\centering\bitpattern[bitWidth=0.8em]{Op}[9]{AC}[4]IX[4]Y[18]/ \par}
330 % \end{verbatim}
331 %
332 % \smallskip
333 % \renewcommand\bpFormatBitNumber[1]{{\tiny\ttfamily\emph{\strut#1}}}
334 % \renewcommand\bpFormatField[1]{{\scriptsize\ttfamily\strut#1}}
335 % {\centering\bitpattern[littleEndian,startBit=7,numberBitsBelow]{01}[2]{D}[3]{S}[3]/\par}
336 % \begin{verbatim}
337 % \renewcommand\bpFormatBitNumber[1]{{\tiny\ttfamily\emph{\strut#1}}}
338 % \renewcommand\bpFormatField[1]{{\scriptsize\ttfamily\strut#1}}
339 % {\centering\bitpattern[littleEndian,startBit=7,numberBitsBelow]{01}[2]{D}[3]{S}[3]/\par}
340 % \end{verbatim}
341 %
342 % \smallskip
343 % \bpSetTickHeight{1pt}
344 % \renewcommand\bpFormatField[1]{{\scriptsize\ttfamily\bpDocExampleStrut #1}}
345 % {\centering\bitpattern[noBitNumbers,littleEndian,startBit=7]{01}[2]{D}[3]{S}[3]/\par}
346 % This example was made to be as compact as possible. Expanding the
347 % strut a little more vertically wouldn't be unwise.
348 % \begin{verbatim}
349 % \bpSetTickHeight{1pt}
350 % \newlength{\bpDocExampleHeight}
351 % \settoheight{\bpDocExampleHeight}{\scriptsize\ttfamily 0}
352 % \newcommand{\bpDocExampleStrut}{\rule{0pt}{\bpDocExampleHeight}}
353 % \renewcommand\bpFormatBitNumber[1]{{\tiny\ttfamily\emph{\strut#1}}}
354 % \renewcommand\bpFormatField[1]{{\scriptsize\ttfamily\bpDocExampleStrut #1}}
355 % {\centering\bitpattern[noBitNumbers,littleEndian,startBit=7]{01}[2]{D}[3]{S}[3]/\par}
356 % \end{verbatim}
357 % Note that if you put a descender, the formatting is then disturbed:
358 % {\centering\bitpattern[noBitNumbers,littleEndian,startBit=7]{01}[2]{D}[3]{p}[3]/\par}
359 %
360 % \smallskip
361 % It is to be noted that usually one does not change the formatting
362 % for every pattern, so setting the optional argument of
363 % \cs{bitpattern} is rarely used.
364 % \StopEventually{}
365 %
366 % \newpage
367 % \section{Implementation}
368 %
369 % First let's have a standard preamble and indicates the package we
370 % requires.
371 %
372 % \begin{macrocode}
373 \NeedsTeXFormat{LaTeX2e}
374 \ProvidesPackage{bitpattern}[2015/12/11 bit pattern diagrams]
375
376 \RequirePackage{keyval}
377 \RequirePackage{calc}
378 \RequirePackage{multido}
379 % \end{macrocode}
380 % Now we'll declare some new |\if| commands which will be set to
381 % describe the options.
382 % \begin{macrocode}
383 \newif\ifbp@NumberBitsAbove
384 \newif\ifbp@NumberBitsBelow
385 \newif\ifbp@HasBitNumbers
386 \newif\ifbp@NumberFieldTwice
387 \newif\ifbp@NumberAllBits
388 \newif\ifbp@NumberBitsBigEndian
389 % \end{macrocode}
390 % We need a counter to store the starting bit number. We are not
391 % using the \LaTeX{} way because we want to use it locally and not
392 % globally.
393 % \begin{macrocode}
394 \newcount\bp@StartBit
395 % \end{macrocode}
396 % \LaTeX{} length behaves are needed, so use them.
397 % \begin{macrocode}
398 \newlength{\bp@BitWidth}
399 \newlength{\bp@TickHeight}
400 % \end{macrocode}
401 %
402 % First the user level macros allowing to change the default. They
403 % are easy, the just set the |\if|.
404 % \begin{macrocode}
405 \newcommand\bpLittleEndian
406 {\bp@NumberBitsBigEndianfalse}
407
408 \newcommand\bpBigEndian
409 {\bp@NumberBitsBigEndiantrue}
410
411 \newcommand\bpNumberBitsAbove
412 {\bp@NumberBitsAbovetrue
413 \bp@NumberBitsBelowfalse}
414
415 \newcommand\bpNumberBitsBelow
416 {\bp@NumberBitsAbovefalse
417 \bp@NumberBitsBelowtrue}
418
419 \newcommand\bpNoBitNumbers
420 {\bp@HasBitNumbersfalse}
421
422 \newcommand\bpNumberFieldsOnce
423 {\bp@HasBitNumberstrue
424 \bp@NumberFieldTwicefalse
425 \bp@NumberAllBitsfalse}
426
427 \newcommand\bpNumberFieldsTwice
428 {\bp@HasBitNumberstrue
429 \bp@NumberFieldTwicetrue
430 \bp@NumberAllBitsfalse}
431
432 \newcommand\bpNumberAllBits
433 {\bp@HasBitNumberstrue
434 \bp@NumberFieldTwicetrue
435 \bp@NumberAllBitstrue}
436 % \end{macrocode}
437 %
438 % About as easy, macros setting the counter and the lengths.
439 % \begin{macrocode}
440 \newcommand\bpStartAtBit[1]{\bp@StartBit=#1}
441 \newcommand\bpSetBitWidth[1]{\setlength{\bp@BitWidth}{#1}}
442 \newcommand\bpSetTickHeight[1]{\setlength{\bp@TickHeight}{#1}}
443 % \end{macrocode}
444 %
445 % Now we set up the keyval handling
446 % \begin{macrocode}
447 \define@key{bitpattern}{littleEndian}[true]{\bpLittleEndian}
448 \define@key{bitpattern}{bigEndian}[true]{\bpBigEndian}
449 \define@key{bitpattern}{numberBitsAbove}[true]{\bpNumberBitsAbove}
450 \define@key{bitpattern}{numberBitsBelow}[true]{\bpNumberBitsBelow}
451 \define@key{bitpattern}{noBitNumbers}[true]{\bpNoBitNumbers}
452 \define@key{bitpattern}{numberFieldsOnce}[true]{\bpNumberFieldsOnce}
453 \define@key{bitpattern}{numberFieldsTwice}[true]{\bpNumberFieldsTwice}
454 \define@key{bitpattern}{numberAllBits}[true]{\bpNumberAllBits}
455
456 \define@key{bitpattern}{startBit}{\bpStartAtBit{#1}}
457 \define@key{bitpattern}{bitWidth}{\bpSetBitWidth{#1}}
458 \define@key{bitpattern}{tickSize}{\bpSetTickHeight{#1}}
459 % \end{macrocode}
460 %
461 % And we can do the same for the package options.
462 % \begin{macrocode}
463 \DeclareOption{littleEndian}{\bpLittleEndian}
464 \DeclareOption{bigEndian}{\bpBigEndian}
465 \DeclareOption{numberBitsAbove}{\bpNumberBitsAbove}
466 \DeclareOption{numberBitsBelow}{\bpNumberBitsBelow}
467 \DeclareOption{noBitNumbers}{\bpNoBitNumbers}
468 \DeclareOption{numberFieldsOnce}{\bpNumberFieldsOnce}
469 \DeclareOption{numberFieldsTwice}{\bpNumberFieldsTwice}
470 \DeclareOption{numberAllBits}{\bpNumberAllBits}
471 % \end{macrocode}
472 %
473 % Set up the default values.
474 % \begin{macrocode}
475 \bpLittleEndian
476 \bpNumberBitsBelow
477 \bpNumberFieldsOnce
478 \AtEndOfPackage{\ifbp@NumberBitsBigEndian
479 \bpStartAtBit{0}\else\bpStartAtBit{7}\fi}
480 \bpSetBitWidth{1em}
481 \bpSetTickHeight{2pt}
482 % \end{macrocode}
483 %
484 % And provide the two formatting commands we are using for the
485 % numbers and the fields.
486 % \begin{macrocode}
487 \providecommand\bpFormatField[1]{\strut\emph{#1}}
488 \providecommand\bpFormatBitNumber[1]{{\tiny\sffamily\strut #1}}
489 % \end{macrocode}
490 %
491 % A few more variables
492 % \begin{macrocode}
493 \newcount\bp@CurBit
494 \newlength{\bp@FieldWidth}
495 \newlength{\bp@RuleWidth}\setlength{\bp@RuleWidth}{0.4pt}
496 \newsavebox{\bp@TickBox}
497 % \end{macrocode}
498 %
499 % Now two wrappers around the public formatting hook which call them
500 % only when the settings are appropriate.
501 % \begin{macrocode}
502 \newcommand\bp@FormatTopBit[1]{\ifbp@HasBitNumbers
503 \ifbp@NumberBitsAbove\bpFormatBitNumber{#1}\fi\fi}
504 \newcommand\bp@FormatBottomBit[1]{\ifbp@HasBitNumbers
505 \ifbp@NumberBitsBelow\bpFormatBitNumber{#1}\fi\fi}
506 % \end{macrocode}
507 %
508 % And an helper to get the rule we want for ticks.
509 % \begin{macrocode}
510 \newcommand\bp@Tick{\rule{\bp@RuleWidth}{\bp@TickHeight}}
511 % \end{macrocode}
512 %
513 % Small utility to get a vbox without additional vertical spacing
514 % \begin{macrocode}
515 \newcommand\bp@vbox[3]
516 {\parbox[#1]{#2}{\lineskip=0pt\lineskiplimit=0pt\baselineskip=0pt\relax#3}}
517 % \end{macrocode}
518 % The function used to put the broken rule when the width is smaller
519 % than the size.
520 % \begin{macrocode}
521 \newcommand\bp@BrokenRuleFill{\dimen0=\bp@RuleWidth\multiply\dimen0 3
522 \leaders \hrule height \bp@RuleWidth\hfill
523 \xleaders\hbox to \dimen0{\rule{\bp@RuleWidth}{\bp@RuleWidth}\hfill}\hfill
524 \leaders \hrule height \bp@RuleWidth\hfill}
525 % \end{macrocode}
526 % The function used to put the ticks when the width is smaller than
527 % the size.
528 % \begin{macrocode}
529 \newcommand\bp@BrokenTicks
530 {\usebox{\bp@TickBox}\usebox{\bp@TickBox}\hfill\usebox{\bp@TickBox}}
531 % \end{macrocode}
532 % The function used to put the ticks in the other cases.
533 % \begin{macrocode}
534 \newcommand\bp@FillWithTicks
535 {\leaders\hbox to \bp@BitWidth{\usebox{\bp@TickBox}}\hfill}
536 % \end{macrocode}
537 %
538 % The functions putting the rule and the ticks, choosing between the
539 % \emph{broken} and non broken version above. They take two
540 % arguments, the size and the width.
541 % \begin{macrocode}
542 \newcommand\bp@RuleFill[2]
543 {\ifnum #1 = #2
544 \leaders \hrule height \bp@RuleWidth\hfill
545 \else
546 \bp@BrokenRuleFill
547 \fi}
548 \newcommand\bp@TicksFill[2]
549 {\ifnum #1 = #2
550 \bp@FillWithTicks
551 \else
552 \bp@BrokenTicks
553 \fi}
554 % \end{macrocode}
555 % Now a macro to put the bit numbers. It is called with two
556 % arguments, the width and the alignment. For broken fiels,
557 % NumberAllBits will be changed to NumberFieldTwice before calling
558 % this.
559 % \begin{macrocode}
560 \newcommand\bp@MakeNumbers[2]{\begingroup\ifbp@NumberAllBits
561 % \end{macrocode}
562 % Numbering all the bits is easy, just pay attention to the
563 % direction.
564 % \begin{macrocode}
565 \mbox{\multido{}{#1}{%
566 \makebox[\bp@BitWidth]{\bpFormatBitNumber{\the\bp@CurBit}}%
567 \advance\bp@CurBit\ifbp@NumberBitsBigEndian 1\else -1\fi}}%
568 \else\ifbp@NumberFieldTwice
569 % \end{macrocode}
570 % Numbering twice per field has to handle the case where the width is
571 % 1.
572 % \begin{macrocode}
573 \mbox{%
574 \makebox[\bp@FieldWidth]%
575 {\makebox[\bp@BitWidth][#2]{\bpFormatBitNumber{\the\bp@CurBit}}%
576 \ifnum #1 > 1
577 \advance\bp@CurBit\ifbp@NumberBitsBigEndian #1\else -#1\fi
578 \advance\bp@CurBit\ifbp@NumberBitsBigEndian -1\else 1\fi
579 \hfill\makebox[\bp@BitWidth][#2]%
580 {\bpFormatBitNumber{\the\bp@CurBit}}\fi}}%
581 \else
582 % \end{macrocode}
583 % When numbering only once, one has to pay attention to the alignment
584 % so that the value is increased beforehand if needed.
585 % \begin{macrocode}
586 \ifx#2r
587 \ifnum #1 > 1
588 \advance\bp@CurBit\ifbp@NumberBitsBigEndian #1\else -#1\fi
589 \advance\bp@CurBit\ifbp@NumberBitsBigEndian -1\else 1\fi
590 \fi\fi
591 \makebox[\bp@FieldWidth][#2]{\bpFormatBitNumber{\the\bp@CurBit}}%
592 \fi\fi\endgroup}
593 % \end{macrocode}
594 % Call |bp@MakeNumbers| in an appropriate way for the top numbering
595 % \begin{macrocode}
596 \newcommand\bp@MakeTopNumbers[1]%
597 {\ifbp@HasBitNumbers
598 \ifbp@NumberBitsAbove
599 \bp@MakeNumbers{#1}{l}
600 \else
601 \makebox[\bp@FieldWidth][l]{\hfill}\fi\fi}
602 % \end{macrocode}
603 % Call |bp@MakeNumbers| in an appropriate way for the bottom
604 % numbering
605 % \begin{macrocode}
606 \newcommand\bp@MakeBottomNumbers[1]%
607 {\ifbp@HasBitNumbers
608 \ifbp@NumberBitsBelow
609 \bp@MakeNumbers{#1}{r}
610 \else
611 \makebox[\bp@FieldWidth][r]{\hfill}\fi\fi}
612 % \end{macrocode}
613 % The \cs{bitpattern} is easy, just parse the optional argument in a
614 % group so that the changes are local and do some more intialization.
615 % The hard work is delegated to \cs{bp@FieldIfPresent}.
616 % \begin{macrocode}
617 \newcommand{\bitpattern}[1][]{%
618 \begingroup
619 \setkeys{bitpattern}{#1}%
620 \savebox{\bp@TickBox}[\bp@BitWidth]{\bp@Tick\hfill}%
621 \bp@CurBit=\bp@StartBit%
622 \bp@FieldIfPresent}
623 % \end{macrocode}
624 % \cs{bp@FieldIfPresent} checks if we are done and format a field if
625 % not.
626 % \begin{macrocode}
627 \newcommand{\bp@FieldIfPresent}{\@ifnextchar/{\bp@Done}{\bp@Field}}
628 % \end{macrocode}
629 % \cs{bp@field} checks if there is an optional argument or call
630 % \cs{bp@DoField}.
631 % \begin{macrocode}
632 \newcommand{\bp@Field}[1]%
633 {\@ifnextchar[{\bp@SizedField[#1]}{\bp@DoField[#1][1][1]}}
634 % \end{macrocode}
635 % \cs{bp@field} checks if there is a second optional argument and
636 % ensure that \cs{bp@DoField} is called with all the arguments set
637 % up.
638 % \begin{macrocode}
639 \def\bp@SizedField[#1][#2]%
640 {\@ifnextchar[{\bp@DoField[#1][#2]}{\bp@DoField[#1][#2][#2]}}
641 % \end{macrocode}
642 % First argument is field name, second is size, third is width. Do
643 % some initialization and then format a compact vbox (two are used so
644 % that the baseline is the one of the field names).
645 % \begin{macrocode}
646 \def\bp@DoField[#1][#2][#3]%
647 {\setlength{\bp@FieldWidth}{\bp@BitWidth * #3}%
648 \ifnum #2 > #3 \ifbp@NumberAllBits\bpNumberFieldsTwice\fi\fi
649 \bp@vbox{t}{\bp@FieldWidth}{%
650 \bp@vbox{b}{\bp@FieldWidth}{%
651 \bp@MakeTopNumbers{#2}
652 \makebox[\bp@FieldWidth][s]{\bp@RuleFill{#2}{#3}}
653 \makebox[\bp@FieldWidth][s]{\bp@TicksFill{#2}{#3}}
654 \makebox[\bp@FieldWidth][s]%
655 {\vrule width \bp@RuleWidth\hfil\bpFormatField{#1}\hfil}}
656 \makebox[\bp@FieldWidth][s]{\bp@TicksFill{#2}{#3}}
657 \makebox[\bp@FieldWidth][s]{\bp@RuleFill{#2}{#3}}
658 \bp@MakeBottomNumbers{#2}}%
659 \advance\bp@CurBit\ifbp@NumberBitsBigEndian #2\else -#2\fi%
660 \bp@FieldIfPresent}
661 % \end{macrocode}
662 % Finalize the pattern by putting a file vertical rule.
663 % \begin{macrocode}
664 \def\bp@Done/{\bp@vbox{t}{\bp@RuleWidth}{%
665 \bp@vbox{b}{\bp@RuleWidth}{%
666 \makebox[\bp@RuleWidth]{\bp@FormatTopBit{}\hfill}
667 \makebox[\bp@RuleWidth]{\bp@RuleFill{0}{0}}
668 \makebox[\bp@RuleWidth]{\bp@Tick\hfill}
669 \makebox[\bp@RuleWidth]%
670 {\vrule width \bp@RuleWidth\hfil\bpFormatField{}\hfil}}
671 \makebox[\bp@RuleWidth]{\bp@Tick\hfill}
672 \makebox[\bp@RuleWidth]{\bp@RuleFill{0}{0}}
673 \makebox[\bp@RuleWidth]{\hfill\bp@FormatBottomBit{}}}\endgroup}
674 % \end{macrocode}
675 % We can now process the option of the package and we are done.
676 % \begin{macrocode}
677 \ProcessOptions
678 % \end{macrocode}
679 %
680 % \Finale
681 %
682 \endinput
683 %%% Local Variables:
684 %%% fill-column: 69
685 %%% End: