1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Copyright (c) 2011-2013, ETH Zurich.
3% All rights reserved.
4%
5% This file is distributed under the terms in the attached LICENSE file.
6% If you do not find this file, copies can be found by writing to:
7% ETH Zurich D-INFK, Universitaetstr 6, CH-8092 Zurich. Attn: Systems Group.
8%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9
10\documentclass[a4paper,11pt,twoside]{report}
11\usepackage{bftn}
12\usepackage{calc}
13\usepackage{verbatim}
14\usepackage{xspace}
15\usepackage{pifont}
16\usepackage{textcomp}
17\usepackage{amsmath}
18\usepackage{multirow}
19
20\title{Mackerel User Guide}
21\author{Barrelfish project}
22% \date{\today}		% Uncomment (if needed) - date is automatic
23\tnnumber{2}
24\tnkey{Mackerel}
25
26\begin{document}
27\maketitle			% Uncomment for final draft
28
29\begin{versionhistory}
30\vhEntry{1.2}{31.05.2010}{TR}{Initial version under new formatting}
31\vhEntry{1.3}{15.08.2011}{TR}{Added support for imports and new
32  command-line options}
33\vhEntry{1.4}{09.12.2011}{TR}{New mapping for constants}
34\vhEntry{1.5}{11.03.2013}{TR}{Added quick reference, corrected errors,
35  and clarified field insertions}
36\end{versionhistory}
37
38% \intro{Abstract}		% Insert abstract here
39% \intro{Acknowledgements}	% Uncomment (if needed) for acknowledgements
40\tableofcontents		% Uncomment (if needed) for final draft
41% \listoffigures		% Uncomment (if needed) for final draft
42% \listoftables			% Uncomment (if needed) for final draft
43\cleardoublepage
44\setcounter{secnumdepth}{2}
45
46\newcommand{\fnname}[1]{\textit{\texttt{#1}}}%
47\newcommand{\datatype}[1]{\textit{\texttt{#1}}}%
48\newcommand{\varname}[1]{\texttt{#1}}%
49\newcommand{\keywname}[1]{\textbf{\texttt{#1}}}%
50\newcommand{\tabindent}{\hspace*{3ex}}%
51\newcommand{\Mac}{Mackerel\xspace}
52
53
54%-------INTRODUCTION 
55
56
57\chapter{Introduction and Usage} 
58
59\Mac is a domain-specific language used to describe hardware devices
60and the in-memory formats of registers and data structures like
61descriptor lists, page-table entries, etc.   
62
63The \Mac language itself is designed to be easy to transcribe into
64from a hardware data book or similar description - most of the
65constructs correspond to the kinds of concepts one finds in hardware
66descriptions. 
67
68A feature of the \Mac language is that, while traditional comments in
69\Mac specifications are allowed, explanatory text in English is
70included as part of a \Mac specification itself, and used to generate
71debugging code which explains the meaning of register values. 
72
73 Typically, it takes as input a specification written in the
74Mackerel language and outputs a C header file consisting of a large
75number of C inline function definitions to access and manipulate
76registers and in-memory data structures, format them for printing in
77human-readable form, etc.  
78
79The \Mac compiler is written in Haskell using the Parsec parsing
80library.  A quick roadmap of the source code files is provided in
81Chapter \ref{chap:sourcetree}. 
82
83To manually invoke \Mac, run:
84
85\verb~> mackerel --shift-driver -c devFile.dev -o devFile.h~
86
87This will translate a device file into a single C header file containing all
88relevant functions for accessing the device, as described in
89Chapter~\ref{chap:shiftdriver}. 
90
91If you wish to use the (deprecated) bitfield-based back-end instead,
92you can use:
93
94\verb~> mackerel --bitfield-driver -c devFile.dev -o devFile.h~
95
96This backend should not be used for new code, as it is not portable
97across compilers.  It is described in
98Chapter~\ref{chap:bitfielddriver}. 
99
100\section{Command-line options}
101
102The complete list of comand-line options supported by the current
103Mackerel compiler is as follows:
104\begin{itemize}
105\item \texttt{-c} \textit{filename} or
106  \texttt{--input-file=}\textit{filename}: This option is mandatory
107  and specifies the pathname to be read.  This pathname must specify
108  the file: the main input file will not be searched for along the
109  search path. 
110
111\item \texttt{-I} \textit{dir} or \texttt{--include-dir}
112  \textit{dir}: this option can be supplied multiple times and
113  specifies the search path for imported files. 
114
115\item \texttt{-v} or \texttt{--verbose}: Increases the verbosity level
116  of the compiler.
117
118\item \texttt{-o} \textit{filename} or \texttt{--output-file}
119  \textit{filename}: this option, if supplied, specifies the name of
120  the C header file which will be written by the compiler.  If not
121  given, the filename will be derived from the input filename by
122  removing all the directory components (i.e. it will reside in the
123  current working directory) and appending \texttt{.h}.
124
125\item \texttt{-S} or \texttt{shift-driver}: Specifies the shift driver
126  backend; this is the default. 
127
128\item \texttt{-B} or \texttt{bitfield-driver}: Specifies the bitfield
129  driver backend; this is deprecated.  Furthermore, imports are
130  (deliberately) not supported by the bitfield driver.  If multiple
131  \texttt{-S} and \texttt{-B} options are specified, the last one
132  determines the compiler output. 
133
134\end{itemize}
135
136
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138
139
140\chapter{Lexical Conventions}\label{chap:lexical}
141The following convention has been adopted for Mackerel. It is similar to
142the convention opted by modern day programming languages like C and Java.\\
143
144\begin{description}
145\item[Whitespace:]  As in C and Java, \Mac considers sequences of
146  space, newline, tab, and carriage return characters to be
147  whitespace.  Whitespace is generally not significant. 
148
149\item[Comments:] \Mac supports C-style comments.  Single line comments
150  start with \texttt{//} and continue until the end of the line.
151  Multiline comments are enclosed between \texttt{/*} and \texttt{*/};
152  anything inbetween is ignored and treated as white space.
153
154\item[Identifiers:] Valid \Mac identifiers are sequences of numbers
155  (0-9), letters (a-z, A-Z) and the underscore character ``\texttt{\_}''.  They
156  must start with a letter or ``\texttt{\_}''.  
157  \begin{align*}
158    identifier & \rightarrow ( letter \mid \_ ) (letter \mid digit \mid \_)^{\textrm{*}} \\
159    letter & \rightarrow (\textsf{A \ldots Z} \mid  \textsf{a \ldots z})\\
160    digit & \rightarrow (\textsf{0 \ldots 9})
161\end{align*}
162
163  Note that a single underscore ``\texttt{\_}'' by itself is a special,
164  ``don't care'' or anonymous identifier which is treated differently
165  inside the language. 
166  
167\item[Integer Literals:] A \Mac integer literal is a sequence of
168  digits, optionally preceded by a radix specifier.  As in C, decimal (base 10)
169  literals have no specifier and hexadecimal literals start with
170  \texttt{0x}.  Binary literals start with \texttt{0b}. 
171
172  In addition, as a special case the string \texttt{1s} can be used to
173  indicate an integer which is composed entirely of binary 1's. 
174
175\begin{align*}
176digit & \rightarrow (\textsf{0 \ldots 9})^{\textrm{1}}\\
177hexadecimal & \rightarrow (\textsf{0x})(\textsf{0 \ldots 9} \mid \textsf{A \ldots F} \mid \textsf{a \ldots f})^{\textrm{1}}\\
178binary & \rightarrow (\textsf{0b})(\textsf{0, 1})^{\textrm{1}}\\
179allones & \rightarrow \textsf{1s}\\
180\end{align*}
181
182
183\item[Reserved words:] The following are reserved words in \Mac:
184\begin{alltt}
185  \begin{tabbing}
186xxxxxxxxx \= xxxxxxxxx \= xxxxxxxxx \= xxxxxxxxx \= xxxxxxxxx \= xxxxxxxxx \kill
187addr \> also \> bytewise \> constants \> datatype \> device \\
188io \> lsbfirst \> many \> msbfirst \> pci \> regarray  \\
189register \> regtype \> space \> stepwise \> type \> valuewise \\
190import 
191  \end{tabbing}
192\end{alltt}
193
194\item[Special characters:] The following characters are used as operators,
195  separators, terminators or other special purposes in \Mac:
196\begin{alltt}
197
198  \{ \} [ ] ( ) + - * / ; , . _ = 
199
200\end{alltt}
201
202\end{description}
203
204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205\chapter{Declarations}\label{chap:declarations}
206
207A \Mac source file must consist of zero or more \textit{import}
208declarations, followed by a single \textit{device
209  specification}, which itself consists of further specifications of
210registers, register types, etc. 
211
212\section{Imports}\label{sec:imports}
213
214An import declaration makes the definitions in a different device file
215available in the current device definition, as described below.  The
216syntax of an import declaration is as follows:
217
218\begin{syntax}
219import \synit{device};
220\end{syntax}
221
222\begin{description}
223\item[device] is the name of a device from which to import
224  definitions.  
225\end{description}
226
227The \Mac compiler will search for a file with the appropriate name and
228parse this at the same time as the main file, along with this file's
229imports, and so on.  Cyclic dependencies between device files will not
230cause errors, but at present are unlikely to result in C header files
231which will successfully compile. 
232
233\section{Devices}\label{sec:devices}
234
235A device declaration in \Mac specifies a particular type of hardware
236device (such as an ioAPIC or a particular Ethernet controller).   The
237syntax is as follows:
238
239\begin{syntax}
240device \synit{name} [lsbfirst|msbfirst] ( \synit{args} ) "\synit{description}"
241\verb+{+
242  \synit{declaration};
243  \ldots
244\verb+}+;
245\end{syntax}
246
247\begin{description}
248\item[name] is an identifier for the device type, and will be used to
249  generate identifiers in the target language (typically C).  
250  The name of the name of the device \emph{must} correspond to the
251  filename of the file, including case sensitivity: for example, 
252  the file \texttt{xapic.dev} will define a device type
253  of name \texttt{xapic}. 
254
255\item[lsbfirst or msbfirst] optionally specifies the \emph{bit order}
256  of the declarations 
257  inside the device file; if specified, it must be \texttt{lsbfirst}
258  (the default) or \texttt{msbfirst}.  
259
260  Note that this does not say \emph{anything} about the endianness of
261  the hardware device, or the host platform, or the execution
262  environment.  It simply says which order the bitfields in particular
263  registers are listed in the declarations which follow --
264  least-significant bits first or most-sigificant bits first. 
265
266  The reason for this feature is that \Mac specifications are
267  typically transcribed from chip datasheets, and while these are
268  generally unambiguous there is no clear convention on which order to
269  list a hardware register bitfields in a datasheet.   This feature 
270  means that the \Mac specification can more closely resemble to text
271  in the datasheet.  
272
273  When transcribing a datasheet into a \Mac specification, if the
274  fields in a register or register type are listed starting with the
275  one which includes bit 0, specify \texttt{lsbfirst}.   If the fields
276  are listed starting with the one which includes the high bit in the
277  register, specify \texttt{msbfirst}.  In both cases type the fields
278  in the order they are printed in the datasheet. 
279
280\item[args] are the arguments that specify how to access the device.
281  They are treated as base addresses in one of several \emph{address
282    spaces} supported by \Mac (see Section~\ref{sec:spaces}
283  below).  Typical devices have a single base address in either memory
284  or I/O space, though some device types (of which only one instance
285  ever exists, and at a fixed address) have no arguments, and others
286  may have more if they have multiple register sets which can appear
287  at different addresses.   
288
289  The arguments will become arguments to the function that intializes
290  the struct used to represent the device in \Mac-generated C.
291  They are also used within the device declaration to specify the
292  locations of individual registers or groups of registers. 
293
294  Arguments are declared in C-style type notation as either of type
295  \texttt{address} (in memory space) or \texttt{io} (in I/O
296  space).  By convention a single device argument is named
297  \texttt{base}. 
298
299\item [description] is a string literal in double quotes, which
300  describes the device type being specified, for example \texttt{"AC97
301  Baseline Audio"}. 
302
303\end{description}
304
305After the description string follows in curly brackets a series of
306declarations which specify details of the device hardware.   Each
307declaration must be one of the following:
308
309\begin{itemize}
310
311  \item A \texttt{space} declaration, describing a device-specific
312    address space for registers in addition to the built-in address
313    spaces. 
314  
315  \item A \texttt{constants} list, analogous to a C enumeration type
316
317  \item A \texttt{register} declaration, describing a particular
318    hardware register or group of registers
319    
320  \item A \texttt{regtype} definition, giving the type and format of
321    other hardware register values. 
322
323\end{itemize}
324
325\section{Address spaces}\label{sec:spaces}
326
327Registers defined in a \Mac file
328reside at specified addresses in an \emph{address space} (unless they
329are specified as \texttt{noaddr}, see Section~\ref{sec:registers}). 
330
331There are currently 3 built-in address spaces that \Mac understands,
332and addresses in these spaces require both a base and an offset.  Base 
333values are given as an arguments to the device specification, and so
334each device argument must be declared as being of one of the following
335types: 
336
337\begin{description}
338
339\item[addr]: Physical memory, for memory-mapped I/O.  Register
340  locations in physical address space are given as offsets relative to
341  a base address which is an argument to the device definition. 
342
343\item[io]: I/O port space, for \texttt{ia32} machines.  An I/O address
344  is a 16-bit port number, and register locations in I/O port space
345  are given as offsets relative to a base address which is an argument
346  to the device definition.   
347
348\item[pci]: PCI Configuration space.  A PCI configuration space
349  address is a 4-tuple of \textit{(bus, device, function, offset)}, however,
350  \Mac only deals in integer offsets from an opaque ``base'' address,
351  allowing for different addressing schemes depending on hardware.  
352
353\end{description}
354
355In addition, \Mac allows the specification of per-device address
356spaces.  This feature can express a variety of hardware features, for
357example processor model-specific registers, co-processor registers, 
358device registers that must be accessed indirectly through another
359index registers, etc.  
360
361The syntax for defining a new address space is:
362
363\begin{syntax}
364space \synit{name}(\synit{index}) bytewise|valuewise|stepwise(\textit{step})|registerwise
365          "\synit{description}"; 
366\end{syntax}
367
368\begin{description}
369\item[name] is an identifier for the address space, and is used in
370  register declarations instead of a builtin space such as
371  \texttt{addr} or \texttt{io}. 
372
373\item [index] is an identifier for the argument giving the address for
374  the registers
375
376\item [bytewise, valuewise or registerwise] defines how registers in the new
377  space are addressed.  Bytewise addresses are like conventional
378  memory addresses, for example two adjacent 32-bit registers in a
379  bytewise-addressed space will have offsets that differ by 4.  All
380  the built-in address spaces (\texttt{addr}, \texttt{pci}, and
381  \texttt{io}) are bytewise-addressed. 
382
383  In contrast, registers in a valuewise-addressed space are indexed by
384  a simple integer regardless of size, meaning that two adjacent
385  32-bit registers will have addresses that differ only by 1.
386  A good example of a valuewise address space is the set of ia32
387  model-specific registers (MSRs). 
388
389  If neither of these is sufficient, \texttt{stepwise} allows you to
390  explicitly specify how many bytes are occupied by each address in
391  the address space.  \texttt{bytewise} is therefore a synonym for
392  \texttt{stepwise(1)}, but \texttt{stepwise(4)} might be useful for
393  devices (such as some IOAPICs) where registers are indexed by 32-bit
394  values but can be accessed as 64-bit quantities. 
395
396  Alternatively, \texttt{registerwise} gives more freedom by calling a
397  custom function to access each register. For a 32-bit register \texttt{R} in the
398  registerwise address space \texttt{S}, the read and write functions are
399  declared as follows:
400%
401  \begin{syntax}
402uint32_t __DN(S_read_32_R)(void);
403void __DN(S_write_32_R)(uint32_t _regval);
404  \end{syntax}
405
406
407\item [``description''] is a string literal in double quotes, which
408  describes the constant type being specified, for example
409  \texttt{"PHY control registers"}. 
410
411\end{description}
412
413Register addresses in user-defined address spaces are always given
414simply as offsets, since the base is implicit in the device
415definition.  
416
417\Mac files cannot currently say how registers in a particular space
418are accessed; this functionality must be provided externally by the
419programmer (typically by short inline functions).  
420
421Here is how ia32 MSRs are defined: 
422
423\begin{example}
424space msr valuewise "Model-specific Registers";
425register platform\_id ro msr(0x17) "Platform ID" \verb+{+
426    \_	50;
427    id	3 "platform id";
428    \_	11;
429\verb+}+;
430\ldots
431\end{example}
432
433As a second example, here is a set of space declarations (and
434associated index and data registers) from the legacy PC real-time
435clock (RTC) definition: 
436
437\begin{example}
438space std(idx) valuewise "Standard register space";
439register ndx rw io(base, 0x70) "Standard index" 
440             type(uint8);
441register target rw io(base, 0x71) "Standard target" 
442             type(uint8);
443regarray standard rw std(0x00)[256] type(uint8);
444
445space ext(idx) valuewise "Extended register space"; 
446register endx rw io(base, 0x72) "Extended index" 
447             type(uint8);
448register etarget rw io(base, 0x73) "Extended target''
449             type(uint8);
450regarray extended rw std(0x00)[256] type(uint8);
451\end{example}
452
453\section{Constants}\label{sec:constants}
454
455A \texttt{constant} declaration defines a series of related values,
456for example, the possible values of a given register field.  Each
457constant type consists of an identifier for the type, a textual
458description, and a series of possible values.   Each value in turn
459consists of an identifier for the value, the numerical value itself,
460and a textual description of its meaning. 
461
462The syntax is:
463
464\begin{syntax}
465constants \synit{name} [width( \synit{width} )] "\synit{description}" \verb+{+ 
466   \synit{name\(\sb{1}\)} = \synit{value\(\sb{1}\)} ["\synit{description\(\sb{1}\)}"] ;
467   \ldots
468\verb+}+; 
469\end{syntax}
470
471
472\begin{description}
473\item[name] is an identifier for the constant type, and will be used to
474  generate identifiers in the target language (typically C).  The
475  scope of this identifier is the enclosing device specification.  
476
477\item[width] is optional, and specifies the width of the constant
478  value in bits.  This is useful, for example, when the type is used
479  as the type of a register. 
480
481\item [``description''] is a string literal in double quotes, which
482  describes the constant type being specified, for example
483  \texttt{"Vector delivery mode"}. 
484\end{description}
485
486Each constant value is given as follows:
487
488\begin{description}
489
490\item[name$_i$] is an identifier for the particular value of the
491  constant type.  The scope of this identifier is the enclosing
492  constant declaration, not the device declaration as a whole.
493
494\item[value$_i$] is an expression giving the corresponding numerical
495  value.  This will usually be a literal like \texttt{0b1101} or
496  \texttt{0xf}, though it can potentially be any arithmetic expression
497  supported by \Mac. 
498
499\item[``description$_i$''] is an optional string literal in double
500  quotes, which describes the meaning of the value being specified,
501  for example \texttt{"Lowest priority"}.  If not specified, it
502  defaults to the identifier name$_i$. 
503
504\end{description}
505
506Here is a complete example of a constants declaration, taken from the
507xAPIC definition: 
508
509\begin{example}
510constants vdm "Vector delivery mode" \verb+{+
511       fixed   = 0b000 "Fixed";
512       lowest  = 0b001 "Lowest priority";
513       smi     = 0b010 "SMI";
514       nmi     = 0b100 "NMI";
515       init    = 0b101 "INIT";
516       startup = 0b110 "Start Up";
517       extint  = 0b111 "ExtINT";
518\verb+}+;
519\end{example}
520
521Note that the collection of constant values does not have to be
522complete or contiguous. 
523
524\section{Register types}\label{sec:regtypes}
525
526A \texttt{regtype} declaration defines a data type corresponding to the
527format of one or more registers; it can be thought of as a translation
528of the bitfield descriptions typically found in data sheets.   A
529\texttt{regtype} doesn't define any particular hardware registers
530(see \texttt{register} declarations below), merely a data type for
531representing their contents. 
532The syntax is as follows:
533
534\begin{syntax}
535regtype \synit{name} ["\synit{description}"] \verb+{+
536   \synit{fieldname}\(\sb{1}\) \synit{width} [type(\synit{fieldtype})] [\synit{attr}] ["\synit{description}"];
537   \ldots
538\verb+}+;
539\end{syntax}
540
541\begin{description}
542
543\item[name] is an identifier for the register type.  Its scope is the
544  enclosing device declaration. 
545
546\item[description] is an optional string in double quotes, giving a
547  description of the register type, for example \texttt{"LVT
548    monitor"}.   If not present, it defaults to the identifier.  
549
550\end{description}
551
552The field descriptions declare the individual bitfields which make up
553the register type.  The order in which these are given is determined
554by the \texttt{lsbfirst} or \texttt{msbfirst} declaration in the
555\texttt{device} specification: if \texttt{lsbfirst}, the first field
556given starts at bit 0, etc.
557
558\begin{description}
559
560\item[fieldname] is an identifier for the bit field.  It can be
561  given as an underscore (\texttt{\_}) to indicate ``don't care'' or
562  ``reserved'' fields, in which case the field is
563  assumed to have an attributed of \texttt{rsvd} (see below), and no
564  description is needed. 
565
566\item[width] is an integer giving the width of the field in bits. 
567
568\item[fieldtype] is an optional identifier giving the type of this field.
569  This must be either the identifier of a \texttt{constants} declaration
570  somewhere in the enclosing \texttt{device} declaration, for example
571  \texttt{type(vdm)}, or a qualified name of the form
572  \textit{device}\texttt{.}\textit{constdef}, where \textit{device} is
573  the name of a device definition previously imported. 
574
575\item[attr] is an optional field
576  attribute.  See section~\ref{sec:fieldattrs} for more information. 
577
578\item[description] is an optional string in double quotes, giving a
579  description of the field, for example \texttt{"Logical APIC ID"}. 
580  If not present, it defaults to the identifier.  
581
582\end{description}
583
584% feature from old version:
585% There is another variation for defining the bit-width for register layout in
586% bit-fields, called {\it location based width}, besides {\it normal mode}. It
587% may be more error prone and tiresome process for the user, to manually
588% calculate the bit width. Mackerel can do this for users. To automate the
589% process of calculating bit-width, user can specify exact register layout and
590% Mackerel will put appropriate bit-width. User just has to put bit location
591% it into (), to distuinguish it from normal mode.
592% 
593% \verb+(startbit - endbit)+
594% 
595% A number 8 in field declaration, in normal mode, represents 8 bits but (8)
596% represents single bit. Like wise, (9-11) (which are starting and ending
597% location of that particular field) represents 3 bits in register layout. By
598% using this mode of specifying the register layout, user can copy bit location
599% as it is from the hardware specificaion to dev file avoiding any confusions.
600
601
602Here is a complete example of a \texttt{regtype} declaration, taken from the
603xAPIC definition: 
604\begin{example}
605  regtype lvt_lint "LVT Int" \verb+{+
606    vector\verb+    +8 "Vector";
607    dlv_mode\verb+  +4 type(vdm) "Delivery mode";
608    _\verb+         +1;
609    status\verb+    +1 "Delivery status";
610    pinpol\verb+    +1 "Pin polarity";
611    rirr\verb+      +1 "Remote IRR";
612    trig_mode\verb+ +1 "Trigger mode";
613    mask\verb+      +1 "Mask";
614    _\verb+         +15;
615  \verb+}+;
616\end{example}
617
618\section{Registers}\label{sec:registers}
619
620A \texttt{register} declaration defines a particular hardware register
621on the device.  
622
623\begin{syntax}
624register \synit{name} [\synit{attr}] [also] 
625             \{ noaddr | \synit{space}( \synit{address} ) \} ["\synit{description}"] \synit{type} ;
626\end{syntax}
627
628\begin{description}
629
630\item[name] is an identifier for the register.  Its scope is the
631  enclosing \texttt{device} declaration. 
632
633\item[attr] is an (optional) attribute.  See
634  Section~\ref{sec:fieldattrs} for more information.
635
636\item[space] gives the address space of this register
637  (e.g. \texttt{addr}, \texttt{io}, \texttt{pci}, or a per-device
638  user-defined address space).
639
640\item[address] gives the address of the register in the address
641  space.  For builtin address spaces this is given as a
642  comma-separated pair of \textit{(base, offset)} where \textit{base}
643  is a device argument of the correct type and \textit{offset} is an
644  integer-valued \Mac expression.   For user-defined address spaces,
645  the address is a single \Mac integer expression giving the register
646  index.  
647
648  By default, it is an error to specify more than one
649  register at the same location, or at locations which overlap.  By
650  preceding the address specifier with the \texttt{also} keyword, you
651  can allow registers to coexist at the same location. 
652
653\item[noaddr] is an alternative to the address space definition, and
654  is used for registers which have no address (or, alternatively, an
655  implicit address).  A good example of this kind of register is a
656  coprocessor register which requires custom assembler instructions to
657  read and write.  Mackerel generates slightly different access code
658  for this type of register, specified later in this document, and
659  requires the developer to supply the raw register access functions. 
660
661\item[description] is an optional string in double quotes, giving a
662  description of the register, for example \texttt{"Error status"}. 
663  If not present, it defaults to the name identifier.  
664
665\item[type] gives the format of the register.  It can consist of
666  \texttt{type(}\textit{name}\texttt{)}, where \textit{name} is
667  either:
668  \begin{itemize}
669  \item the identifier of a register type previously declared with a
670    \texttt{regtype} declaration;
671  \item the identifier of a constants type previously declared with a
672    \texttt{constants} declaration which included an explicit
673    \texttt{width()} specifier;
674  \item one of the built in register types \texttt{uint8},
675    \texttt{uint16}, \texttt{uint32}, or \texttt{uint64}; or
676  \item a qualified name of the form
677    \textit{device}\texttt{.}\textit{type}, where \textit{device}
678    is the name of a device definition previously imported.  
679  \end{itemize}
680
681  Alternatively, the type can be given as a sequence of fields in
682  braces, exactly as in a \texttt{regtype} declaration.  In effect,
683  this latter form defines both a register type and a register at the
684  same time, with the same name. 
685
686\end{description}
687
688Here are some examples of \texttt{register} definitions:
689
690\begin{example}
691  register dfr rw at (base, 0x00e0) "Destination Format" \{
692    _           28 mb1;
693    model       4 type(model_type) "Model";
694  \};
695\end{example}
696\begin{example}
697  register apr ro at (base, 0x0090) "Arbitration priority" type(priority);
698\end{example}
699\begin{example}
700  register isr0 ro at (base, 0x0100) "ISR bits 0-31" type(uint32);
701\end{example}
702\begin{example}
703  register cr4 noaddr "Control register 4" \{
704    _           49 mbz;
705    smxe        1 "SMX enable";
706    vmxe        1 "VMX enable";
707    _           2 mbz;
708    osxmmexcpt  1 "OS support for unmasked SIMD FP exceptions";
709    osfxsr      1 "OS support for FXSAVE and FXRSTOR instructions";
710    pce         1 "Performance-monitoring counter enable";
711    pge         1 "Page global enable";
712    mce         1 "Machine-check enable";
713    pae         1 "Physical address extensions";
714    pse         1 "Page size extensions";
715    de          1 "Debugging extensions";
716    tsd         1 "Time stamp disable";
717    pvi         1 "Protected-mode virtual interrupts";
718    vme         1 "Virtual 8086 mode extensions";
719  \};
720\end{example}
721
722
723
724\section{Register Arrays}\label{sec:regarrays}
725
726A \texttt{regarray} declaration defines an array of hardware registers
727on the device, all of which have the same type and attributes. 
728
729\begin{syntax}
730regarray \synit{name} [\synit{attr}] [also] 
731             \synit{space}( \synit{address} ) ["\synit{description}"] \synit{type} ;
732\end{syntax}
733
734All fields are as for register declarations, except: 
735
736\begin{description}
737
738\item[address] gives the address of the registers which form the array
739  in the address space.  This is given as for a single
740  \texttt{register} declaration, followed by an array specifier in
741  square brackets.  
742
743  There are three forms of array specifier. The simplest looks like
744  this:
745  \begin{example}
746    regarray ier ro addr(base, 0x0480) [ 8 ] 
747                 "IER" type(uint32);
748  \end{example}
749  This specifies a contiguous array of 8 32-bit registers starting at
750  \texttt{base + 0x0480}.  This example therefore occupies 32 bytes of
751  IO space.  Alternatively: 
752  \begin{example}
753    regarray ier ro addr(base, 0x0480) [ 8; 0x10 ] 
754                 "IER" type(uint32);
755  \end{example}
756  This second example specifies 8 32-bit registers, which occur every 16 bytes,
757  starting at \texttt{base + 0x0480}.  Finally, one can explicitly
758  list the individual locations for the array (note: the back-end does
759  not yet generate correct code for this case):
760  \begin{example}
761    regarray ier ro addr(base, 0x0480) [ 0, 0x10, 0x18 ] 
762                 "IER" type(uint32);
763  \end{example}
764  This third example specifies 3 32-bit registers, at offsets of
765  \texttt{0x480}, \texttt{0x490}, and \texttt{0x498} from
766  \texttt{base}. 
767
768\end{description}
769
770\section{Data types}\label{sec:datatypes}
771
772A \texttt{datatype} declaration defines a data type corresponding to the
773format of a structure in memory; it can be used to represent in-memory
774hardware-defined datastructures like page table entries or DMA
775descriptors. 
776
777The syntax is as follows:
778
779\begin{syntax}
780datatype \synit{name} [lsbfirst|msbfirst (\synit{wordsize})] ["\synit{descr}"] \verb+{+
781   \synit{fieldname}\(\sb{1}\) \synit{width} [type(\synit{fieldtype})] [\synit{attr}] ["\synit{description}"];
782   \ldots
783\verb+}+;
784\end{syntax}
785
786\begin{description}
787
788\item[name] is an identifier for the data type.  Its scope is the
789  enclosing device declaration. 
790
791\item[lsbfirst or msbfirst] together with a (required) word size
792  specify how the fields in the specification are ordered.  This is
793  described in more detail below. 
794
795\item[description] is an optional string in double quotes, giving a
796  description of the register type, for example \texttt{"LVT
797    monitor"}.   If not present, it defaults to the identifier.  
798
799\end{description}
800
801The field descriptions declare the individual bitfields which make up
802the data type.  
803
804The order in which these are given is determined
805by the \texttt{lsbfirst} or \texttt{msbfirst} declaration.  Data
806structures in memory are sometimes listed in data books as a sequence
807of words, each of which is formatted into bit fields.  In order for
808authors of device specifications to easily translate these
809descriptions into Mackerel, one can specify the ``word size'' used in
810the description along with the order in which bit fields within a
811word are listed.  For example, ``\texttt{msbfirst(32)}'' means that
812the bit fields are to be grouped into words of 32 bits each, and
813within each word the fields are listed most-significant-bit (i.e. the
814field containing bit 31) first.  On an Intel architecture machine,
815this will cause the resulting C declaration to be derived by taking
816the first set of fields whose width adds up to 32, reversing their
817order in the struct declaration, then the next group, etc. 
818
819Specifying a word size of 0 will cause the entire set of fields to be
820reordered (if necessary) in one go.  If no bit order and word size are
821specified, the default is that of the device itself, with a word size
822of 0. 
823
824\begin{description}
825
826\item[fieldname] is an identifier for the bit field.  It can be
827  given as an underscore (\texttt{\_}) to indicate ``don't care'' or
828  ``reserved'' fields, in which case the field is
829  assumed to have an attributed of \texttt{rsvd} (see below), and no
830  description is needed. 
831
832\item[width] is an integer giving the width of the field in bits. 
833
834\item[fieldtype] is an optional identifier giving the type of this field.
835  This must be the identifier of a \texttt{constants} declaration
836  somewhere in the enclosing \texttt{device} declaration, for example
837  \texttt{type(vdm)}. 
838
839\item[attr] is an optional field
840  attribute.  See section~\ref{sec:fieldattrs} for more information.
841  The only field attributes allowed for data types are \texttt{rsvd},
842  \texttt{mbz}, \texttt{mb1}, or \texttt{rw}.  ``Don't care'' fields
843  default to \texttt{rsvd}; others default to \texttt{rw}. 
844
845\item[description] is an optional string in double quotes, giving a
846  description of the field, for example \texttt{"Command header"}. 
847  If not present, it defaults to the identifier.  
848
849\end{description}
850
851Here is a complete example of a \texttt{regtype} declaration, taken from the
852a SATA AHCI port definition: 
853\begin{example}
854  datatype cls msbfirst(32) "Command list structure" \verb+{+
855      prdtl\verb+ +16 "Physical region descriptor table length";
856      pmp\verb+   +4  "Port multiplier port";
857      \_\verb+     +1;
858      c\verb+     +1 "Clear busy upon R\_OK";
859      b\verb+     +1 "BIST";
860      r\verb+     +1 "Reset";
861      p\verb+     +1 "Prefetchable";
862      w\verb+     +1 "Write";
863      a\verb+     +1 "ATAPI";
864      cfl\verb+   +5 "Command FIS length";
865      prdbc\verb+ +32 "Physical region descriptor byte count";
866      ctba\verb+  +32 "Command table descriptor base addr";
867      ctbau\verb+ +32 "Command table descriptor base addr upper";
868      \_\verb+     +32;
869      \_\verb+     +32;
870      \_\verb+     +32;
871      \_\verb+     +32;
872  \verb+}+;
873\end{example}
874
875\section{Field and register attributes}\label{sec:fieldattrs}
876
877This section lists the various attributes that can be applied to
878register fields or entire registers.  Some of these values can only be
879applied to register fields, and not to entire registers.   Some of
880these attributes are functionally equivalent from the point of view of
881client code generated by \Mac, but the aim is to facilitate typing in
882information from data sheets. 
883
884\begin{description}
885
886\item[rw] Read/write.  The value is readable and writable.  This is
887  the default. 
888
889\item[ro] Read-only.  The value is readable, but not writeable.  No
890  code will be generated to handle writes to this value. 
891
892\item[wo] Write-only.  The value is writable, but not readable.  No
893  code will be generated to handle reads from this value, but
894  generated code may keep a ``shadow'' copy of the last value written
895  to aid in debugging. 
896
897\item[rc] Read to clear.  The value is readable, but not writeable,
898  and a read operation will clear the value (this is sometimes seen
899  with statistical count registers, for example).  Driver code
900  generated for registers will behave as if the register was
901  \textbf{ro}, so care must be taken if extra reads are inserted.  In
902  particular, remember that printing the value of the register in
903  debugging code will clear it. 
904
905\item[rwzc] Read/write zero to clear. The value is readable. A write of 0
906  clears (sets to 0) the corresponding bit, and a write of 1 has no effect.
907
908\item[rw1c] Read/write one to clear.  The value is readable. A write of 1
909  clears (sets to 0) the corresponding bit, and a write of 0 has no
910  effect. 
911
912\item[rwc] Read/write clear.  A synonmym for \textbf{rw1c}.
913
914\item[ros] Read-only sticky.  Value is readable and not writeable.  In
915  Intel nomenclature, the bits can only be reset with a power cycle;
916  in \Mac this attribute is simply a synonym for \texttt{ro}. 
917
918\item[rw1cs] Read and write one to clear and sticky.  In Intel
919  nomenclature, this is equivalent to \texttt{rwc} but bits can only
920  be reset on a power cycle.  
921
922\item[rwcs] Read, write to clear and sticky.  A synonym for \textbf{rw1cs}.
923
924\item[rwo] Read/write once. Can be written to precisely once after
925  reset, and thereafter is read-only. 
926
927\item[rws] Read/write and sticky.  Software can read and write to this
928  bit, but the bit can only be reset after a power cycle. 
929
930\item[rsvd] Reserved.  \Mac generates code to allow clients to read
931  reserved bits, but when writing a register value, code generated by \Mac
932  will ensure the bit values are preserved, if necessary by performing
933  a read before the write. 
934
935\item[mbz] Must be zero.  \Mac will generate no code to read the
936  value, but will ensure that it is always written as 0. 
937
938\item[mb1] Must be one.  \Mac will  generate no code to read the
939  value, but will ensure that it is always written as 1. 
940
941\end{description}
942
943Register attributes can be \texttt{rw}, \texttt{ro}, or \texttt{wo},
944and provide a default attribute for each field of the register.  If
945no attributes at all are specified, \texttt{rw} is assumed, except in
946the case of an anonymous register field (denoted by an underscore
947``\texttt{\_}''), which defaults to 
948\texttt{rsvd} regardless of the register attribute.  The motivation for
949this design choice is that unused fields of registers are frequently
950``reserved'' rather than ``don't care''.  The default behaviour will
951therefore result in potential inefficiency (due to reading the value
952before writing it), but never incorrect behaviour (due to writing the
953wrong values to a reserved field).
954 
955%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956\chapter{C mapping for device drivers}\label{chap:shiftdriver}
957
958For each device specification, \Mac generates a single C header file
959giving all the necessary definitions and declarations.   While
960developers are not expected to look at this file in normal use, it is
961designed to be fairly readable to curious humans, and is worth
962examining to understand what \Mac is generating (and to find bugs in
963\Mac). 
964
965The file for a device called, say, \texttt{apic} is conventionally named
966\texttt{apic\_dev.h}, and in Barrelfish is usually found in
967the \texttt{dev} subdirectory of the main include directory; thus a C
968file would include it with:
969
970\begin{alltt}
971#include <dev/apic\_dev.h>
972\end{alltt}
973
974This chapter will describe the contents of this header file. 
975
976\section{Preamble}
977
978The header file is protected by the customary macro to ensure that it
979is only included once - for example, for a device called \texttt{DEV} this is
980``\texttt{\_\_DEV\_H}''. 
981
982The file includes only one other header file: \texttt{mackerel.h}.
983This latter header file includes declarations for the low-level read
984and write functions that \Mac code requires, and some other
985preprocessor macros to ensure correct C code generation.   
986
987
988%% \Mac prefixes all declarations in the file with the
989%% name of the file plus an underscore (``\texttt{DEV\_}'').  However, for
990%% most (though not all) declarations this prefix can be overridden by
991%% defining the macro ``\texttt{DEV\_PREFIX}'' before including the file. 
992
993%% This is at first sight a little confusing.  Suppose we have a device
994%% called \texttt{pc16550d\_uart}.   By default, any declaration
995%% \texttt{foo} in \Mac file will correspond to
996%% \texttt{pc16550d\_uart\_foo} in the header file. 
997
998%% If, however, we include a line:
999
1000%% \begin{quote}
1001%%  \texttt{\#define pc16550d\_uart\_PREFIX uart}
1002%% \end{quote}
1003
1004%% -- before including the header file, that declaration is likely to now
1005%% be \texttt{uart\_foo}. 
1006
1007In the rest of this chapter, we'll use ``\texttt{DN\_}'' as the
1008device prefix that cannot be redefined in this manner, and ``\texttt{DP\_}''
1009as device prefix that can. 
1010
1011\section{Device-level declarations}
1012
1013For the device, \Mac generates: 
1014
1015\begin{enumerate}
1016
1017\item a C structure type to represent what it knows about the
1018  device, called \texttt{DN\_t}.  To access a device from C via \Mac,
1019  you first need to declare one of these, and then call a function
1020  (see below) to initialize it. 
1021  
1022  The struct contains fields which correspond to each of the arguments
1023  which define the addresses of the device, plus a ``shadow'' copy of
1024  every register on the device which has at least one write-only field -
1025  this is used to keep track of the last value written to the register. 
1026
1027\item The initialization function \texttt{DN\_initialize}.  For a
1028  device type with an argment of ``\texttt{io base}'', the
1029  initialization function looks like:
1030
1031  \begin{quote}
1032    \texttt{void DN\_initialize( DN\_t *\_dev, mackerel\_io\_t base );}
1033  \end{quote}
1034  
1035  The idiom for accessing a device at IO address \texttt{ia} would be:
1036  
1037  \begin{quote}
1038    \texttt{DN\_t dev;}\\
1039    \texttt{DN\_initialize( \&dev, ia );}\\
1040    \ldots\\
1041    \textit{code to access registers via \texttt{dev}}
1042  \end{quote}
1043
1044\item An enumeration declaration \texttt{enum
1045  DN\_initials}, with a field \texttt{DN\_REG\_initial} for each
1046  register \texttt{REG} defined in the device.  Currently, all these
1047  enumeration fields are set to \texttt{0x00}, and are used inside the
1048  generated initialization function to initialize any shadow copies of
1049  registers in the device struct.  
1050  
1051\item An \texttt{snprintf}-like function to
1052  pretty-print \emph{all} the register contents of the device, with prototype:
1053  \begin{quote}
1054    \texttt{int DN\_pr(char *s, size\_t sz, DN\_t *v);}
1055  \end{quote}
1056  For a complex device, this is a lot of information, and may require a
1057  large buffer to fully capture.  This is also potentially a large
1058  function to inline.  It will result in reads from all readable device
1059  registers, including ``read to clear'' (\textbf{rc}) registers and
1060  fields.  For write-only registers, this function will print the shadow
1061  copy of the register maintained in the device structure, which holds
1062  the last value written to the register (or 0 if the register has not
1063  been written since the initialization function was called).  The
1064  output identifies which values are ``real'' and which are shadow
1065  copies, includes descriptions of all register fields, and translates
1066  symbolic values into their names and descriptions. 
1067\end{enumerate}
1068
1069%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1070\section{Constants}
1071
1072For a constants declaration of type \texttt{CNSTS}, having fields
1073\texttt{FIELD1} and \texttt{FIELD2}, \Mac will generate the following:
1074
1075\begin{enumerate}
1076
1077\item A type definition which defines type \texttt{DP\_CNSTS\_t} to be
1078  an unsigned integer type (e.g.\ \texttt{uint8\_t} or
1079  \texttt{uint64\_t}).  The type is the smallest such type large
1080  enough to hold the largest value in the constants declaration, or
1081  (if the width is explicitly given) the smallest such type wider than
1082  the given width. 
1083
1084\item A set of CPP macro definitions \texttt{DP\_FIELD1} and
1085  \texttt{DP\_FIELD2}, each of which expands to a C expression
1086  consisting of the field value cast to type \texttt{DP\_CNSTS\_t}.  
1087
1088  If the field value is specified as \texttt{1s}, the macro will
1089  expand to the C value \texttt{-1LL} cast to type
1090  \texttt{DP\_CNSTS\_t}.  
1091
1092  Note that the field names are not prefixed by the constants name,
1093  only the device prefix. 
1094
1095  The motivation for using CPP macros (rather than C enumerations, as
1096  in an earlier version of Mackerel) is that \texttt{enum} types are
1097  only the size of C \texttt{int}s, whereas Mackerel can specify
1098  values in \texttt{constants} declarations which are larger than
1099  this (such as 64-bit values). 
1100
1101  The motivation for not using constant unsigned integer declarations
1102  is that macros allow us to only generate a header file, without
1103  cluttering up the data segment with multiple copies of constants if
1104  the generated header file is included more than once. 
1105      
1106\item A function which takes an argument of type \texttt{DP\_CNSTS\_t}
1107  and returns a pointer to a string containing the description of the
1108  field value, or NULL if the argument does not correspond to a field
1109  value:
1110  \begin{quote}
1111    \texttt{char *DP\_CNSTS\_describe(DP\_CNSTS\_t e);}
1112  \end{quote}
1113  Note that this function can also simply be used as a test of whether
1114  the value is valid. 
1115
1116\item An \texttt{snprintf}-like function to pretty-print values of
1117  type \texttt{DP\_CNSTS\_t}, with prototype:
1118  \begin{quote}
1119    \texttt{int DP\_CNSTS\_prtval(char *s, size\_t sz, DP\_CNSTS\_t e);}
1120  \end{quote}
1121
1122\end{enumerate}
1123
1124\section{Register types}\label{sec:c-regtype}. 
1125
1126For a \texttt{regtype} declaration \texttt{REGTYPE}, \Mac will
1127generate the following: 
1128
1129\begin{enumerate}
1130
1131\item A type definition \texttt{DP\_REGTYPE\_t}, which is an unsigned
1132  integer of the same size as the register. 
1133
1134\item A CPP macro \texttt{DP\_REGTYPE\_default}, which is an integer
1135  literal giving the ``default'' value for the register type.  Note
1136  that since this is a macro, the prefix of the name cannot be
1137  overridden using the trick mentioned above. 
1138
1139\item An \texttt{snprintf}-like function to pretty-print values of the
1140  register type, with prototype:
1141  \begin{quote}
1142    \texttt{int DP\_REGTYPE\_prtval(char *s, size\_t sz, DP\_REGTYPE\_t v);}
1143  \end{quote}
1144
1145\item For every field \texttt{FLD} (of type \texttt{FLDTYPE\_t}) of the
1146  register type, a function to insert a value into that field of a value
1147  of type \texttt{DP\_REGTYPE\_t}: 
1148  \begin{quote}
1149    \texttt{DP\_REGTYPE\_t DP\_REGTYPE\_FLD\_insert(DP\_REGTYPE\_t r, FLDTYPE\_t val );}
1150  \end{quote}
1151  Note that this insertion is \textit{not} in place, in contrast to
1152  the function generated for \texttt{datatype} declarations (see
1153  below).  Instead, this it returns a new value of the register type
1154  with the specified field modified, and so is intended to be used in
1155  a ``functional'' style. 
1156
1157\item For every field \texttt{FLD} (of type \texttt{FLDTYPE\_t}) of the
1158  register type, a function to extract a single field from a value
1159  of type \texttt{DP\_REGTYPE\_t}: 
1160  \begin{quote}
1161    \texttt{FLDTYPE\_t DP\_REGTYPE\_FLD\_extract(DP\_REGTYPE\_t r);}
1162  \end{quote}
1163
1164\end{enumerate}
1165
1166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1167\section{Data types}\label{sec:c-datatype}. 
1168
1169For a \texttt{datatype} declaration
1170\texttt{DATATYPE}, \Mac will generate the following:
1171
1172\begin{enumerate}
1173
1174\item A type definition \texttt{DP\_DATATYPE\_t}, which is of type
1175  \texttt{uint8\_t *}.  This is the canonical way to refer to values
1176  of the datatype in memory. 
1177
1178\item A \texttt{static const size\_t DP\_DATATYPE\_size}, which is the
1179  size in bytes of the data type.  This should be used in place of
1180  \texttt{sizeof(DP\_DATATYPE\_t)}, which will the wrong result in
1181  general. 
1182
1183\item A type definition \texttt{DP\_DATATYPE\_array\_t}, which is of type
1184  \texttt{uint8\_t[sz]}, where \texttt{sz} is the size in bytes of the
1185  datatype.  This should be used sparingly, for example to allocate a
1186  value of the datatype on the stack. 
1187
1188\item An \texttt{snprintf}-like function to pretty-print values of the
1189  datatype, with prototype:
1190  \begin{quote}
1191    \texttt{int DP\_DATATYPE\_prtval(char *s, size\_t sz, DP\_DATATYPE\_t v);}
1192  \end{quote}
1193
1194\item For every field \texttt{FLD} (of type \texttt{FLDTYPE\_t}) of the
1195  datatype, a function to insert a value into that field of a value
1196  of type \texttt{DP\_DATATYPE\_t}: 
1197  \begin{quote}
1198    \texttt{void DP\_DATATYPE\_FLD\_insert(DP\_DATATYPE\_t r, FLDTYPE\_t val );}
1199  \end{quote}
1200  Note that this insertion is \textit{in place}, in contrast to the
1201  ``functional'' style used for register types. 
1202
1203\item For every field \texttt{FLD} (of type \texttt{FLDTYPE\_t}) of the
1204  datatype, a function to extract a single field from a value
1205  of type \texttt{DP\_DATATYPE\_t}: 
1206  \begin{quote}
1207    \texttt{FLDTYPE\_t DP\_DATATYPE\_FLD\_extract(DP\_DATATYPE\_t r);}
1208  \end{quote}
1209
1210\end{enumerate}
1211
1212
1213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1214\section{Registers}
1215
1216For a register \texttt{REG} that defines its own type, \Mac defines
1217a register type with the same name and generates the declarations
1218defined in section~\ref{sec:c-regtype}. 
1219
1220For each register \texttt{REG} with type \texttt{REGTYPE}, \Mac will
1221generate some of the following:
1222
1223\begin{enumerate}
1224
1225\item If the register has fields which are readable, a function to
1226  read the contents of the register:
1227  \begin{quote}
1228    \texttt{DP\_REGTYPE\_t DP\_REG\_rd( DN\_t *dev );}
1229  \end{quote}
1230
1231\item If the register address is not given as \texttt{noaddr}, a function to
1232  read the raw contents of the register:
1233  \begin{quote}
1234    \texttt{DP\_REGTYPE\_t DP\_REG\_rawrd( DN\_t *dev );}
1235  \end{quote}
1236  On the other hand, if the register address is given as
1237  \texttt{noaddr}, the developer is required to supply this function
1238  herself -- Mackerel-generated code will use it to access the
1239  register. 
1240
1241\item If the register has fields which are writeable, a function to
1242  write the contents of the register:
1243  \begin{quote}
1244    \texttt{void DP\_REG\_wr( DN\_t *dev, DP\_REGTYPE\_t val );}
1245  \end{quote}
1246  Note that this is \emph{not} a simple write of the value: any fields
1247  which must be 1 or 0 are set accordingly, and reserved fields are
1248  read from the register before writing back the value.  Therefore,
1249  this function will only write the bit fields of \texttt{val} which
1250  are meaningful to write according to the register specification.
1251  However, this function will not perform a read from the register
1252  unless the register contains reserved fields. 
1253
1254\item If the register address is not given as \texttt{noaddr}, a function to
1255  write raw values to the register:
1256  \begin{quote}
1257    \texttt{DP\_REGTYPE\_t DP\_REG\_rawwr( DN\_t *dev, DP\_REGTYPE\_t val );}
1258  \end{quote}
1259  This function is potentially dangerous, since it makes no guarantees
1260  that certain fields of the register are set to zero or one, or
1261  preserves the contents of reserved fields.  Use with caution. 
1262
1263  On the other hand, if the register address is given as
1264  \texttt{noaddr}, the developer is required to supply this function
1265  herself -- Mackerel-generated code will use it to access the
1266  register.  Again, in this case, the function should not try to
1267  preserve fields, since Mackerel will make sure that the
1268  ``sanitized'' register and field writing functions will do this
1269  correctly. 
1270
1271\item For every readable field \texttt{FLD} (of type \texttt{FLDTYPE})
1272  of the register, a function to read just that single field from the
1273  register: 
1274  \begin{quote}
1275    \texttt{FLDTYPE DP\_REG\_FLD\_rdf( DN\_t *dev );}
1276  \end{quote}
1277
1278\item For every writeable field \texttt{FLD} of the register, a function to
1279  write just that single field to the register:
1280  \begin{quote}
1281    \texttt{void DP\_REG\_FLD\_wrf( DN\_t *dev, \texttt{FLDTYPE} val );}
1282  \end{quote}
1283  
1284  These functions should be used with care.  It is of
1285  course impossible to write only one field, hence a complete write to
1286  the register will occur.  Furthermore, the values of the other
1287  read/writeable or reserved register fields will be determined first
1288  by doing a hidden read from the register.   Any write-only 
1289  fields will then be initialized from the shadow copy.  Any ``must be
1290  zero'' or ``must be 1'' fields will be set to their respective values.  
1291
1292\item For every write-only field \texttt{FLD} (of type \texttt{FLDTYPE})
1293  of the register, a function to read just that single field from the
1294  shadow copy of the register contents: 
1295  \begin{quote}
1296    \texttt{FLDTYPE DP\_REG\_FLD\_rd\_shadow( DN\_t *dev );}
1297  \end{quote}
1298  
1299\item An \texttt{snprintf}-like function to pretty-print the
1300  current value of the register, or its shadow copy if the register is
1301  not readable, with prototype:
1302  \begin{quote}
1303    \texttt{int DP\_REG\_pr( char *s, size\_t sz, DN\_t *dev );}
1304  \end{quote}
1305
1306\end{enumerate}
1307
1308\section{Register Arrays}\label{sec:c-regarray}
1309
1310For a register array \texttt{RARR} that defines its own type, \Mac defines
1311a register type with the same name and generates the declarations
1312defined in section~\ref{sec:c-regtype}. 
1313
1314For each register array \texttt{RARR} with type \texttt{REGTYPE}, \Mac will
1315generate some of the following:
1316
1317\begin{enumerate}
1318
1319\item A CPP macro \texttt{DP\_DATATYPE\_size}, which is an integer
1320  literal cast to a \texttt{size\_t} giving the length in bytes of the
1321  data type. 
1322
1323\item If the register array has fields which are readable, a function to
1324  read the contents of a register:
1325  \begin{quote}
1326    \texttt{DP\_REGTYPE\_t DP\_REG\_rd( DN\_t *dev, int i );}
1327  \end{quote}
1328
1329\item If the register array has fields which are writeable, a function to
1330  write the contents of a register:
1331  \begin{quote}
1332    \texttt{void DP\_REG\_wr( DN\_t *dev, int i, DP\_REGTYPE\_t val );}
1333  \end{quote}
1334  Note that this is \emph{not} a simple write of the value: any fields
1335  which must be 1 or 0 are set accordingly, and reserved fields are
1336  read from the register before writing back the value.  Therefore,
1337  this function will only write the bit fields of \texttt{val} which
1338  are meaningful to write according to the register specification.
1339  However, this function will not perform a read from the register
1340  unless the register contains reserved fields. 
1341
1342\item For every readable field \texttt{FLD} (of type \texttt{FLDTYPE})
1343  of the register array, a function to read just that single field
1344  from a register: 
1345  \begin{quote}
1346    \texttt{FLDTYPE DP\_REG\_FLD\_rdf( DN\_t *dev, int i );}
1347  \end{quote}
1348
1349\item For every writeable field \texttt{FLD} of the register array, a
1350  function to write just that single field to a register:
1351  \begin{quote}
1352    \texttt{void DP\_REG\_FLD\_wrf( DN\_t *dev, int i, \texttt{FLDTYPE} val );}
1353  \end{quote}
1354  
1355  These functions should be used with care.  It is of
1356  course impossible to write only one field, hence a complete write to
1357  the register will occur.  Furthermore, the values of the other
1358  read/writeable or reserved register fields will be determined first
1359  by doing a hidden read from the register.   Any write-only 
1360  fields will then be initialized from the shadow copy.  Any ``must be
1361  zero'' or ``must be 1'' fields will be set to their respective values.  
1362
1363\item For every write-only field \texttt{FLD} (of type \texttt{FLDTYPE})
1364  of the register array, a function to read just that single field from the
1365  shadow copy of a register's contents: 
1366  \begin{quote}
1367    \texttt{FLDTYPE DP\_REG\_FLD\_rd\_shadow( DN\_t *dev );}
1368  \end{quote}
1369  
1370\item An \texttt{snprintf}-like function to pretty-print the
1371  current value of one register in the array, or its shadow copy
1372  if the field is write-only, with prototype:
1373  \begin{quote}
1374    \texttt{int DP\_REG\_pri( char *s, size\_t sz, DN\_t *dev, int i );}
1375  \end{quote}
1376  
1377\item An \texttt{snprintf}-like function to pretty-print the
1378  current value of all registers in the array, or their shadow copies
1379  if the field is write-only, with prototype:
1380  \begin{quote}
1381    \texttt{int DP\_REG\_pr( char *s, size\_t sz, DN\_t *dev );}
1382  \end{quote}
1383
1384\end{enumerate}
1385
1386\section{Address spaces}\label{sec:c-spaces}
1387
1388It is the responsibility of the programmer to supply functions to
1389access registers in user-defined address spaces (though it's common to
1390call \Mac{}-generated code in turn to do this).   The register access
1391code generated by \Mac for a 32-bit wide register declared in an
1392address space \texttt{SPACE} will call the following function for a
1393read:
1394  \begin{quote}
1395    \texttt{uint32\_t DP\_SPACE\_read\_32( DN\_t *dev, size\_t offset );}
1396  \end{quote}
1397-- and the following function for a write:
1398  \begin{quote}
1399    \texttt{void DP\_SPACE\_write\_32( DN\_t *dev, size\_t offset,
1400      uint32\_t value );}
1401  \end{quote}
1402
1403These functions must be supplied by the programmer in a file named
1404\texttt{DP\_spaces.h}, which is included by the generated header file
1405\emph{if and only if} the device specification defines new address
1406spaces.  Note that only one header file is required, rather than one
1407for each user-defined address space.
1408
1409Since the device is passed as an argument, the functions can access
1410any device fields (including device arguments and shadow copies of
1411write-only registers) 
1412
1413They are not
1414declared anywhere in the generated header file, which means that not
1415all the read/write functions for an 
1416address space need to be supplied, only the ones actually used.
1417Furthermore, these can be declared to be \texttt{static inline}, or
1418\texttt{extern}, etc.\ according to taste.
1419
1420\section{Overall header file structure}
1421
1422The generated header file is not too difficult to read, and doing so
1423can be useful for debugging purposes.  The declarations emitted by
1424\Mac occur in the file in the following order (to minimise the need
1425for forward declarations):
1426
1427\begin{enumerate}
1428\item Preamble, naturally.
1429\item Type, print, and check declarations for each ``constants''
1430  clause.
1431\item Type, union, and print declarations for each register type
1432  (including implicit types). 
1433\item The device structure, and its initialization function.
1434\item Read, write, and print functions for each register
1435\item The device print function. 
1436\end{enumerate}
1437
1438In addition, comments in the generated file are intended to help you
1439find the relevant declarations quickly if you need to. 
1440 
1441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442\chapter{Bitfield C mapping for device drivers}\label{chap:bitfielddriver} 
1443
1444\section*{Important}
1445This appendix contains the deprecated C mapping which uses bitfield
1446structures to represent registers.  This back end should not be used,
1447since it is not portable across architectures and does not guarantee
1448that register accesses will be of the same size as the register, which
1449breaks some architectures (such as Rock Creek).   It is included here
1450to help understand legacy code.  Such code should be converted as soon
1451as possible. 
1452
1453For each device specification, \Mac generates a single C header file
1454giving all the necessary definitions and declarations.   While
1455developers are not expected to look at this file in normal use, it is
1456designed to be fairly readable to curious humans, and is worth
1457examining to understand what \Mac is generating (and to find bugs in
1458\Mac). 
1459
1460This chapter will describe the contents of this header file. 
1461
1462\section{Preamble}
1463
1464The header file is protected by the customary macro to ensure that it
1465is only included once - for example, for a device called \texttt{DEV} this is
1466``\texttt{\_\_DEV\_H}''. 
1467
1468The file includes only one other header file: \texttt{mackerel.h}.
1469This latter header file includes declarations for the low-level read
1470and write functions that \Mac code requires, and some other
1471preprocessor macros to ensure correct C code generation.   
1472
1473\Mac prefixes all declarations in the file with the
1474name of the file plus an underscore (``\texttt{DEV\_}''). 
1475
1476
1477%%  However, for
1478%% most (though not all) declarations this prefix can be overridden by
1479%% defining the macro ``\texttt{DEV\_PREFIX}'' before including the file. 
1480
1481%% This is at first sight a little confusing.  Suppose we have a device
1482%% called \texttt{pc16550d\_uart}.   By default, any declaration
1483%% \texttt{foo} in \Mac file will correspond to
1484%% \texttt{pc16550d\_uart\_foo} in the header file. 
1485
1486%% If, however, we include a line:
1487
1488%% \begin{quote}
1489%%  \texttt{\#define pc16550d\_uart\_PREFIX uart}
1490%% \end{quote}
1491
1492%% -- before including the header file, that declaration is likely to now
1493%% be \texttt{uart\_foo}. 
1494
1495In the rest of this chapter, we'll use ``\texttt{DN\_}'' as the
1496device prefix that cannot be redefined in this manner, and ``\texttt{DP\_}''
1497as device prefix that can. 
1498
1499\section{Device-level declarations}
1500
1501\Mac provides a C structure type to represent what it knows about the
1502device, called \texttt{DN\_t}.  To access a device from C via \Mac,
1503you first need to declare one of these, and the call a function to
1504initialize it. 
1505
1506The initialize function is called \texttt{DN\_initialize}.  For a
1507device type with an argment of ``\texttt{io base}'', the
1508initialization function looks like:
1509
1510\begin{quote}
1511\texttt{void DN\_initialize( DN\_t *\_dev, mackerel\_io\_t base );}
1512\end{quote}
1513
1514The idiom for accessing a device at IO address \texttt{ia} would be:
1515
1516\begin{quote}
1517  \texttt{DN\_t dev;}\\
1518  \texttt{DN\_initialize( \&dev, ia );}\\
1519  \ldots\\
1520  \textit{code to access registers via \texttt{dev}}
1521\end{quote}
1522
1523In addition, \Mac generates an \texttt{snprintf}-like function to
1524pretty-print \emph{all} the register contents of the device, with prototype:
1525\begin{quote}
1526  \texttt{int DN\_pr(char *s, size\_t sz, DP\_CNSTS\_t v);}
1527\end{quote}
1528For a complex device, this is a lot of information, and may require a
1529large buffer to fully capture.  This is also potentially a large
1530function to inline.  It will result in reads from all readable device
1531registers, including ``read to clear'' (\textbf{rc}) registers and
1532fields.  For write-only registers, this function will print the shadow
1533copy of the register maintained in the device structure, which holds
1534the last value written to the register (or 0 if the register has not
1535been written since the initialization function was called).  The
1536output identifies which values are ``real'' and which are shadow
1537copies, includes descriptions of all register fields, and translates
1538symbolic values into their names and descriptions. 
1539
1540\section{Constants}
1541
1542For a constants declaration of type \texttt{CNSTS}, having fields
1543\texttt{FIELD1} and \texttt{FIELD2}, \Mac will generate the following:
1544
1545\begin{enumerate}
1546
1547\item An enumeration type \texttt{DP\_CNSTS\_t} with fields
1548  \texttt{DP\_FIELD1} and \texttt{DP\_FIELD2}.  Note that the field
1549  names are not prefixed by the enumeration name, only the device
1550  prefix. 
1551
1552\item An \texttt{snprintf}-like function to pretty-print values of the
1553  enumeration, with prototype:
1554  \begin{quote}
1555    \texttt{int DP\_CNSTS\_prt(char *s, size\_t sz, DP\_CNSTS\_t e);}
1556  \end{quote}
1557      
1558\item A function which returns 1 if an enumeration value is valid, 0
1559  if otherwise:
1560  \begin{quote}
1561    \texttt{int DP\_CNSTS\_chk(DP\_CNSTS\_t e);}
1562  \end{quote}
1563
1564\end{enumerate}
1565
1566\section{Register types}\label{sec:old:c-regtype}. 
1567
1568For a \texttt{regtype} declaration \texttt{REGTYPE}, \Mac will generate the following:
1569
1570\begin{enumerate}
1571
1572\item A bitfield structure type \texttt{DP\_REGTYPE\_t}, whose field
1573  names are those of the fields specified in the \Mac file, with no
1574  prefix, and whose physical layout corresponds to the hardware
1575  register type.
1576
1577\item A union type \texttt{DP\_REGTYPE\_un}, whose members are the
1578  bitfield struct above (as ``\texttt{val}'') and the smallest
1579  enclosing unsigned integer type (as ``\texttt{raw}''),. 
1580
1581\item An \texttt{snprintf}-like function to pretty-print values of the
1582  register type, with prototype:
1583  \begin{quote}
1584    \texttt{int DP\_REGTYPE\_prtval(char *s, size\_t sz, DP\_CNSTS\_t v);}
1585  \end{quote}
1586
1587\end{enumerate}
1588
1589This functionality is being replaced by one based on shifts and masks,
1590rather than using bitfields, which have portability issues. 
1591For a \texttt{regtype} declaration \texttt{REGTYPE}, \Mac will soon
1592generate the following: 
1593
1594\begin{enumerate}
1595
1596\item A type definition \texttt{DP\_REGTYPE\_t} which is an unsigned
1597  integer of the same size as the register. 
1598
1599\item For every field \texttt{FLD} (of type \texttt{FLDTYPE}) of the
1600  register type, a function to insert a value into that field of a value
1601  of type \texttt{DP\_REGTYPE\_t}: 
1602  \begin{quote}
1603    \texttt{DP\_REGTYPE\_t DP\_REGTYPE\_FLD\_ins(DP\_REGTYPE\_t r, FLD\_TYPE val );}
1604  \end{quote}
1605
1606\item For every field \texttt{FLD} (of type \texttt{FLDTYPE}) of the
1607  register type, a function to extract a single field from a value
1608  of type \texttt{DP\_REGTYPE\_t}: 
1609  \begin{quote}
1610    \texttt{FLDTYPE DP\_REGTYPE\_FLD\_ext(DP\_REGTYPE\_t r);}
1611  \end{quote}
1612
1613\end{enumerate}
1614
1615\section{Data types}\label{sec:old:c-datatype}. 
1616
1617For a \texttt{datatype} declaration
1618\texttt{DATATYPE}, \Mac will generate the following:
1619
1620\begin{enumerate}
1621
1622\item A bitfield structure type \texttt{DP\_DATATYPE\_t}, whose field
1623  names are those of the fields specified in the \Mac file, with no
1624  prefix, and whose physical layout corresponds to the hardware data type.
1625
1626\item An \texttt{snprintf}-like function to pretty-print values of the
1627  data type, with prototype:
1628  \begin{quote}
1629    \texttt{int DP\_DATATYPE\_prtval(char *s, size\_t sz, DP\_CNSTS\_t v);}
1630  \end{quote}
1631
1632\end{enumerate}
1633
1634\section{Registers}
1635
1636For a register \texttt{REG} that defines its own type, \Mac defines
1637a register type with the same name and generates the declarations
1638defined in section~\ref{sec:old:c-regtype}. 
1639
1640For each register \texttt{REG} with type \texttt{REGTYPE}, \Mac will
1641generate some of the following:
1642
1643\begin{enumerate}
1644
1645\item If the register has fields which are readable, a function to
1646  read a ``raw'' (integer) value from the register:
1647  \begin{quote}
1648    \texttt{uint$x$\_t DP\_REG\_rd\_raw( DN\_t *dev );}
1649  \end{quote}
1650  -- where \texttt{uint$x$\_t} is a standard C unsigned integer type
1651  with a fixed size, such as \texttt{uint32\_t}., 
1652
1653\item If the register has fields which are readable, a function to
1654  read a bitfield value from the register:
1655  \begin{quote}
1656    \texttt{DP\_REGTYPE\_t  DP\_REG\_rd( DN\_t *dev );}
1657  \end{quote}
1658
1659\item If the register has fields which are not readable, a function to
1660  read a bitfield value from a saved copy of the last value written to
1661  the register using this device struct:
1662  \begin{quote}
1663    \texttt{DP\_REGTYPE\_t  DP\_REG\_rd\_shadow( DN\_t *dev );}
1664  \end{quote}
1665
1666\item If the register has fields which are writeable, a function to
1667  write a ``raw'' (integer) value to the register:
1668  \begin{quote}
1669    \texttt{void DP\_REG\_wr\_raw( DN\_t *dev, uint$x$\_t val );}
1670  \end{quote}
1671  -- where \texttt{uint$x$\_t} is smallest suitable standard C
1672  unsigned integer type with a fixed size, such as
1673  \texttt{uint32\_t}.
1674
1675\item If the register has fields which are writeable, a function to
1676  write a bitfield value to the register:
1677  \begin{quote}
1678    \texttt{void DP\_REG\_wr( DN\_t *dev, DP\_REGTYPE val );}
1679  \end{quote}
1680  
1681  Unlike the raw write function, this will (only if necessary) perform
1682  a read first from the register to ensure that ``reserved'' fields
1683  will be written back with their correct values.  It will also force
1684  any ``must be zero'' or ``must be 1'' fields to be their respective 
1685  values.  
1686
1687\item For every writeable field \texttt{FLD} of the register, a function to
1688  write just that single field to the register:
1689  \begin{quote}
1690    \texttt{void DP\_REG\_FLD\_wrf( DN\_t *dev, \texttt{uint$x$\_t} val );}
1691  \end{quote}
1692  -- where \texttt{uint$x$\_t} is smallest suitable standard C
1693  unsigned integer type with a fixed size, such as
1694  \texttt{uint8\_t}. 
1695  
1696  These functions should be used with care.  It is of
1697  course impossible to write only one field, hence a complete write to
1698  the register will occur.  Furthermore, the values of the other
1699  read/writeable or reserved register fields will be determined first
1700  by doing a hidden read from the register.   Any write-only 
1701  fields will then be initialized from the shadow copy.  Any ``must be
1702  zero'' or ``must be 1'' fields will be set to their respective values.  
1703  
1704\item An \texttt{snprintf}-like function to pretty-print the
1705  current value of the register, or its shadow copy if the register is
1706  not readable, with prototype:
1707  \begin{quote}
1708    \texttt{int DP\_REG\_pr( char *s, size\_t sz, DN\_t *dev );}
1709  \end{quote}
1710
1711\end{enumerate}
1712
1713\section{Register Arrays}\label{sec:old:c-regarray}
1714
1715For a register array \texttt{RARR} that defines its own type, \Mac defines
1716a register type with the same name and generates the declarations
1717defined in section~\ref{sec:old:c-regtype}. 
1718
1719For each register array \texttt{RARR} with type \texttt{REGTYPE}, \Mac will
1720generate some of the following:
1721
1722\begin{enumerate}
1723
1724\item A static constant giving the length of the array (42 in this example):
1725  \begin{quote}
1726    \texttt{static const int DP\_RARR\_length = 0x26;}
1727  \end{quote}
1728
1729\item If the registers have fields which are readable, a function to
1730  read a ``raw'' (integer) value from a register:
1731  \begin{quote}
1732    \texttt{uint$x$\_t DP\_RARR\_rd\_raw( DN\_t *dev, int i );}
1733  \end{quote}
1734  -- where \texttt{uint$x$\_t} is a standard C unsigned integer type
1735  with a fixed size, such as \texttt{uint32\_t}, and \texttt{i} is an
1736  index into the array.
1737
1738\item If the registers have fields which are readable, a function to
1739  read a bitfield value from a register:
1740  \begin{quote}
1741    \texttt{DP\_REGTYPE\_t  DP\_RARR\_rd( DN\_t *dev, int i );}
1742  \end{quote}
1743
1744\item If the registers have fields which are not readable, a function to
1745  read a bitfield value from a saved copy of the last value written to
1746  each register using this device struct:
1747  \begin{quote}
1748    \texttt{DP\_REGTYPE\_t  DP\_RARR\_rd\_shadow( DN\_t *dev, int i );}
1749  \end{quote}
1750
1751\item If the registers have fields which are writeable, a function to
1752  write a ``raw'' (integer) value to a register:
1753  \begin{quote}
1754    \texttt{void DP\_RARRG\_wr\_raw( DN\_t *dev, int i,  uint$x$\_t val );}
1755  \end{quote}
1756  -- where \texttt{uint$x$\_t} is smallest suitable standard C
1757  unsigned integer type with a fixed size, such as
1758  \texttt{uint32\_t}.
1759
1760\item If the registers have fields which are writeable, a function to
1761  write a bitfield value to a register:
1762  \begin{quote}
1763    \texttt{void DP\_RARR\_wr( DN\_t *dev, int i,  DP\_REGTYPE val );}
1764  \end{quote}
1765  -- where \texttt{uint$x$\_t} is smallest suitable standard C
1766  unsigned integer type with a fixed size, such as
1767  \texttt{uint32\_t}. 
1768  
1769  Unlike the raw write function, this will (only if necessary) perform
1770  a read first from the register to ensure that ``reserved'' fields
1771  will be written back with their correct values.  It will also force
1772  any ``must be zero'' or ``must be 1'' fields to be their respective 
1773  values.  
1774  
1775\item An \texttt{snprintf}-like function to pretty-print the
1776  current value of one element of the array, or its shadow copy if the
1777  register is not readable, with prototype:
1778  \begin{quote}
1779    \texttt{int DP\_RARR\_pri( char *s, size\_t sz, DN\_t *dev, int i );}
1780  \end{quote}
1781  
1782\item An \texttt{snprintf}-like function to pretty-print the
1783  entire register array, or its shadow copies if the registers in the
1784  array are not readable, with prototype:
1785  \begin{quote}
1786    \texttt{int DP\_RARR\_pr( char *s, size\_t sz, DN\_t *dev );}
1787  \end{quote}
1788
1789\end{enumerate}
1790
1791\section{Address spaces}\label{sec:old:c-spaces}
1792
1793It is the responsibility of the programmer to supply functions to
1794access registers in user-defined address spaces (though it's common to
1795call \Mac{}-generated code in turn to do this).   The register access
1796code generated by \Mac for a 32-bit wide register declared in an
1797address space \texttt{SPACE} will call the following function for a
1798read:
1799  \begin{quote}
1800    \texttt{uint32\_t DP\_SPACE\_read\_32( DN\_t *dev, size\_t offset );}
1801  \end{quote}
1802-- and the following function for a write:
1803  \begin{quote}
1804    \texttt{void DP\_SPACE\_write\_32( DN\_t *dev, size\_t offset,
1805      uint32\_t value );}
1806  \end{quote}
1807
1808These functions must be supplied by the programmer in a file named
1809\texttt{DP\_spaces.h}, which is included by the generated header file
1810\emph{if and only if} the device specification defines new address
1811spaces.  Note that only one header file is required, rather than one
1812for each user-defined address space.
1813
1814Since the device is passed as an argument, the functions can access
1815any device fields (including device arguments and shadow copies of
1816write-only registers) 
1817
1818They are not
1819declared anywhere in the generated header file, which means that not
1820all the read/write functions for an 
1821address space need to be supplied, only the ones actually used.
1822Furthermore, these can be declared to be \texttt{static inline}, or
1823\texttt{extern}, etc.\ according to taste.
1824
1825\section{Overall header file structure}
1826
1827The generated header file is not too difficult to read, and doing so
1828can be useful for debugging purposes.  The declarations emitted by
1829\Mac occur in the file in the following order (to minimise the need
1830for forward declarations):
1831
1832\begin{enumerate}
1833\item Preamble, naturally.
1834\item Type, print, and check declarations for each ``constants''
1835  clause.
1836\item Type, union, and print declarations for each register type
1837  (including implicit types). 
1838\item The device structure, and its initialization function.
1839\item Read, write, and print functions for each register
1840\item The device print function. 
1841\end{enumerate}
1842
1843In addition, comments in the generated file are intended to help you
1844find the relevant declarations quickly if you need to. 
1845
1846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1847\chapter{Migrating from the bit-field based version of Mackerel}
1848\label{chap:migrating}
1849
1850Previous versions of Mackerel generated a C API by defining packed C
1851bitfield structs for register types.  This version of Mackerel avoids
1852this in favor of masks and shifts, hidden behind inline functions.
1853This chapter gives some hints for migrating code written using the old
1854Mackerel to the new version. 
1855
1856\begin{itemize}
1857
1858\item The initialize function for a device structure remains the same. 
1859
1860\item \texttt{DEV\_REG\_wr\_raw()} and \texttt{DEV\_REG\_rd\_raw()}
1861  functions should be replaced with \texttt{DEV\_REG\_wr()} and
1862  \texttt{DEV\_REG\_rd()} with the same arguments.  
1863
1864\item The following code to set individual register fields:
1865
1866\begin{verbatim}
1867    //deassert PHY_RESET
1868    {
1869        e1000_ctrl_t c = e1000_ctrl_rd(d);
1870        c.phy_rst = 0;
1871        e1000_ctrl_wr(d, c);
1872        e1000_status_t s = e1000_status_rd(d);
1873        s.phyra = 0;
1874        e1000_status_wr(d, s);
1875    }
1876\end{verbatim}
1877
1878 -- must be replaced with:
1879
1880\begin{verbatim}
1881    //deassert PHY_RESET
1882    e1000_ctrl_phy_rst_wrf(d, 0);
1883    e1000_status_phyra_wrf(d, 0);
1884\end{verbatim}
1885
1886\item The following code to read individual register fields:
1887
1888\begin{verbatim}
1889    speed = e1000_status_rd(d).speed;
1890\end{verbatim}
1891
1892 -- must be replaced by: 
1893
1894\begin{verbatim}
1895    speed = e1000_status_speed_rdf(d);
1896\end{verbatim}
1897
1898\item The following code to access a field from a register:
1899
1900\begin{verbatim}
1901    // test LAN ID to see if we need to modify the MAC from EEPROM
1902    {
1903        e1000_status_t s = e1000_status_rd(d);
1904        if (s.lan_id == e1000_lan_b) {
1905            mac_word2 ^= e1000_lan_b_mask;
1906        }
1907    }
1908\end{verbatim}
1909
1910  -- must be replaced by:
1911
1912\begin{verbatim}
1913    // test LAN ID to see if we need to modify the MAC from EEPROM
1914    {
1915        e1000_status_t s = e1000_status_rd(d);
1916        if (e1000_status_lan_id_extract(s) == e1000_lan_b) {
1917            mac_word2 ^= e1000_lan_b_mask;
1918        }
1919    }
1920\end{verbatim}
1921
1922\item The following idiom for setting fields using struct literals:
1923
1924\begin{verbatim}
1925    e1000_rxdctl_wr(d, 0, (e1000_rxdctl_t){ .gran=1, .wthresh=1 } );
1926\end{verbatim}
1927
1928 -- should be replaced by:
1929
1930\begin{verbatim}
1931    e1000_rxdctl_t tmp = e1000_rxdctl_default;
1932    tmp = e1000_rxdctl_gran_insert(tmp,1);
1933    tmp = e1000_rxdctl_wthresh_insert(tmp,1);
1934    e1000_rxdctl_wr(d, 0, tmp);
1935\end{verbatim}
1936\end{itemize}
1937
1938%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1939\chapter{Source code structure}\label{chap:sourcetree}
1940
1941The \Mac source directory contains the files listed below.
1942
1943\begin{description}
1944
1945\item[Attr.hs] Dealing with register field attributes.
1946\item[BitFieldDriver.hs] Old code generator for device drivers using
1947  bitfields, deprecated.
1948\item[CAbsSyntax.hs] Contains combinators for rendering C code.
1949\item[CSyntax.hs] Old-style C combinators, deprecated.
1950\item[Checks.hs] Compile-time checks.
1951\item[Dev.hs] Overall device representation.
1952\item[Fields.hs] Representing register fields.
1953\item[MackerelParser.hs] ParSec parser for language.
1954\item[Main.hs] Top level file, which is used to run \Mac.
1955\item[Poly.hs] Polynomial arithmetic for compile-time expression
1956  reduction (currently unused)
1957\item[RegisterTable.hs] Representing registers and register arrays.
1958\item[ShiftDriver.hs] Code generator for device drivers using shifts and masks.
1959\item[Space.hs] Representing address spaces.
1960\item[TypeName.hs] Data type for scoping type names when importing.
1961\item[TypeTable.hs] Representing register, data, and constants types.
1962\end{description}
1963
1964%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1965\chapter{Quick reference}\label{chap:quickref}
1966
1967In the following quick reference, the following identifiers are used
1968as examples, assuming a Mackerel file named \texttt{dev1.dev}: 
1969
1970\begin{description}
1971\item[dev1] A device definition.\\
1972\item[const1] A constants definition.
1973\item[cval1] One value of the constants definition.\\
1974\item[rtype2] A register type definition.
1975\item[field2] A field in the above register type.
1976\item[ftype2] The type of this field.\\
1977\item[dtype3] A data type definition.
1978\item[field3] A field in the above data type.
1979\item[ftype3] The type of this field.\\
1980\item[reg4] A register definition with its own type.
1981\item[rtype4] Type of the above register.
1982\item[field4] A field in the above register.
1983\item[ftype4] The type of this field.\\
1984\item[array5] A register array.
1985\item[field5] A field in the elements of the above array.
1986\item[ftype5] The type of this field.
1987\end{description}
1988
1989\clearpage
1990\section*{Quick list of Mackerel C declarations:}
1991{\footnotesize
1992\begin{tabular}{|l|l|}
1993\hline 
1994\textbf{C declaration} & \textbf{Description} \\ \hline 
1995\multicolumn{2}{|l|}{Device declarations} \\ \hline
1996\texttt{struct dev1\_t} & Device structure \\ 
1997\texttt{typedef dev1\_t} & Device type defintion (as above) \\ 
1998\texttt{enum dev1\_initials} & Initial register values \\
1999\texttt{void dev1\_initialize( dev1\_t *dev, ... )} & Initialization \\
2000\texttt{int dev1\_pr( char *s, size\_t sz, device\_t *v )} & Print \\
2001\hline
2002
2003\multicolumn{2}{|l|}{Constants declarations} \\ \hline
2004\texttt{typedef dev1\_const1\_t} & Integer type \\
2005\texttt{\#define dev1\_cval1} & Macro giving integer value \\
2006\texttt{char *dev1\_const1\_describe( dev1\_const1\_t e )} & Description \\
2007\texttt{int dev1\_const1\_prtval( char *s, size\_t sz,
2008  dev1\_const1\_t e )} & Print \\
2009\texttt{int dev1\_const1\_chk( dev1\_const1\_t e)} & Check value \\
2010\hline
2011
2012\multicolumn{2}{|l|}{Register type declarations} \\ \hline
2013\texttt{typedef dev1\_rtype2\_t} & Type defintion \\
2014\texttt{\#define dev1\_rtype2\_default} & Default value \\
2015\texttt{int dev1\_rtype2\_prtval( char *s, size\_t sz,
2016  dev1\_rtype2\_t v )} & Print \\
2017\texttt{dev1\_rtype2\_t dev1\_rtype2\_field2\_insert( dev1\_rtype2\_t r, ftype2\_t val )} & Insert a field value \\
2018\texttt{ftype2\_t dev1\_rtype2\_field2\_extract( dev1\_rtype2\_t r )} & Extract field value\\
2019\hline
2020
2021\multicolumn{2}{|l|}{Datatype declarations} \\ \hline
2022\texttt{typedef dev1\_dtype3\_t} & Pointer to datatype \\
2023\texttt{const size\_t dev1\_dtype3\_size} & Size \\
2024\texttt{typedef dev1\_dtype3\_array\_t} & Byte array of the same size \\
2025\texttt{int dev1\_dtype3\_prtval( char *s, size t sz, dev1\_dtype3\_t
2026  v )} & Print \\
2027\texttt{void dev1\_dtype3\_field3\_insert( dev1\_dtype3\_t r, ftype3\_t val )} & Insert field \\
2028\texttt{ftype3\_t dev1\_dtype3\_field3\_extract( dev1\_dtype3\_t r )} & Extract field \\
2029\hline
2030
2031\multicolumn{2}{|l|}{Register declarations} \\ \hline
2032\texttt{dev1\_rtype4\_t dev1\_reg4\_rd( dev1\_t *dev )} & Read register \\
2033\texttt{dev1\_rtype4\_t dev1\_reg4\_rawrd( dev1\_t *dev )} & Raw read \\
2034\texttt{void dev1\_reg4\_wr( dev1\_t *dev, dev1\_rtype4\_t val )} & Write register \\
2035\texttt{void dev1\_reg4\_rawwr( dev1\_t *dev, dev1\_rtype4\_t val )} & Raw write \\
2036\texttt{ftype4\_t dev1\_reg4\_field4\_rdf( dev1\_t *dev )} & Read field \\
2037\texttt{void dev1\_reg4\_field4\_wrf( dev1\_t *dev, ftype4 val )} & Write field \\
2038\texttt{ftype4\_t dev1\_reg4\_field4\_rd\_shaddow( dev1\_t *dev )} & Read shadow \\
2039\texttt{int dev1\_reg4\_pr( char *s, size\_t sz, dev1\_t *dev )} & Print \\
2040\hline
2041
2042\multicolumn{2}{|l|}{Register array declarations} \\ \hline
2043\texttt{const int dev1\_array5\_length} & Num. registers \\
2044\texttt{dev1\_rtype5\_t dev1\_reg5\_rd( dev1\_t *dev, int i )} & Read register \\
2045\texttt{dev1\_rtype5\_t dev1\_reg5\_rawrd( dev1\_t *dev, int i )} & Raw read \\
2046\texttt{void dev1\_reg5\_wr( dev1\_t *dev, int i, dev1\_rtype5\_t val )} & Write register \\
2047\texttt{void dev1\_reg5\_rawwr( dev1\_t *dev, int i, dev1\_rtype5\_t val )} & Raw write \\
2048
2049\texttt{ftype5\_t dev1\_reg5\_field5\_rdf( dev1\_t *dev, int i )} & Read field \\
2050\texttt{void dev1\_reg5\_field5\_wrf( dev1\_t *dev, int i, ftype5 val )} & Write field \\
2051\texttt{ftype5\_t dev1\_reg5\_field5\_rd\_shaddow( dev1\_t *dev, int i
2052  )} & Read shadow \\
2053\texttt{int dev1\_reg5\_pri( char *s, size\_t sz, dev1\_t *dev, int i )} & Print \\
2054\texttt{int dev1\_reg5\_pr( char *s, size\_t sz, dev1\_t *dev )} &
2055Print array \\
2056\hline
2057
2058\multicolumn{2}{|l|}{Space access functions} \\ \hline
2059\texttt{uint\textit{x}\_t dev1\_space6\_read\_\textit{x}(
2060    dev1\_t *dev, size\_t offset )} & Read a value \\
2061\texttt{void dev1\_space6\_write\textit{x}(
2062    dev1\_t *dev, size\_t offset, uint\textit{x}\_t value )} & Write a
2063    value \\
2064\hline
2065
2066\end{tabular}}
2067
2068\end{document}
2069