source: Deliverables/D4.1/ITP-Paper/itp-2011.tex @ 571

Last change on this file since 571 was 571, checked in by mulligan, 9 years ago

Finished tightening everything up. Just under 16 pages. Cannot add another sentence without it going over!

File size: 56.6 KB
Line 
1\documentclass{llncs}
2
3\usepackage{amsfonts}
4\usepackage{amsmath}
5\usepackage{amssymb} 
6\usepackage[english]{babel}
7\usepackage{color}
8\usepackage{fancybox}
9\usepackage{graphicx}
10\usepackage[colorlinks]{hyperref}
11\usepackage[utf8x]{inputenc}
12\usepackage{listings}
13\usepackage{mdwlist}
14\usepackage{microtype}
15\usepackage{stmaryrd}
16\usepackage{url}
17
18\newlength{\mylength} 
19\newenvironment{frametxt}%
20        {\setlength{\fboxsep}{5pt}
21                \setlength{\mylength}{\linewidth}%
22                \addtolength{\mylength}{-2\fboxsep}%
23                \addtolength{\mylength}{-2\fboxrule}%
24                \Sbox
25                \minipage{\mylength}%
26                        \setlength{\abovedisplayskip}{0pt}%
27                        \setlength{\belowdisplayskip}{0pt}%
28                }%
29                {\endminipage\endSbox
30                        \[\fbox{\TheSbox}\]}
31
32\lstdefinelanguage{matita-ocaml}
33  {keywords={definition,coercion,lemma,theorem,remark,inductive,record,qed,let,in,rec,match,return,with,Type,try,on,to},
34   morekeywords={[2]whd,normalize,elim,cases,destruct},
35   morekeywords={[3]type,of,val,assert,let,function},
36   mathescape=true,
37  }
38\lstset{language=matita-ocaml,basicstyle=\small\tt,columns=flexible,breaklines=false,
39        keywordstyle=\color{red}\bfseries,
40        keywordstyle=[2]\color{blue},
41        keywordstyle=[3]\color{blue}\bfseries,
42        commentstyle=\color{green},
43        stringstyle=\color{blue},
44        showspaces=false,showstringspaces=false}
45\lstset{extendedchars=false}
46\lstset{inputencoding=utf8x}
47\DeclareUnicodeCharacter{8797}{:=}
48\DeclareUnicodeCharacter{10746}{++}
49\DeclareUnicodeCharacter{9001}{\ensuremath{\langle}}
50\DeclareUnicodeCharacter{9002}{\ensuremath{\rangle}}
51
52\author{Dominic P. Mulligan\thanks{The project CerCo acknowledges the financial support of the Future and
53Emerging Technologies (FET) programme within the Seventh Framework
54Programme for Research of the European Commission, under FET-Open grant
55number: 243881} \and Claudio Sacerdoti Coen$^\star$}
56\authorrunning{D. P. Mulligan and C. Sacerdoti Coen}
57\title{An executable formalisation of the MCS-51 microprocessor in Matita}
58\titlerunning{An executable formalisation of the MCS-51}
59\institute{Dipartimento di Scienze dell'Informazione, Universit\`a di Bologna}
60
61\bibliographystyle{plain}
62
63\begin{document}
64
65\maketitle
66
67\begin{abstract}
68We summarise our formalisation of an emulator for the MCS-51 microprocessor in the Matita proof assistant.
69The MCS-51 is a widely used 8-bit microprocessor, especially popular in embedded devices.
70
71The formalisation proceeded in two stages, first implementing an O'Caml prototype for quickly `ironing out' bugs.
72We then ported the O'Caml emulator to Matita.
73Though mostly straight-forward, this porting presented multiple problems.
74Of particular interest is how we handled the non-orthoganality of the MSC-51's instruction set.
75In O'Caml, this was handled with polymorphic variants.
76In Matita, we achieved the same effect with a non-standard use of dependent types.
77
78Both the O'Caml and Matita emulators are `executable'.
79Assembly programs may be animated within Matita, producing a trace of instructions executed.
80The formalisation is a major component of the ongoing EU-funded CerCo project.
81\end{abstract}
82
83%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84% SECTION                                                                      %
85%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86\section{Introduction}
87\label{sect.introduction}
88
89Formal methods aim to increase our confidence in the design and implementation of software.
90Ideally, all software should come equipped with a formal specification and a proof of correctness for the corresponding implementation.
91The majority of programs are written in high level languages and then compiled into low level ones.
92Specifications are therefore also given at a high level and correctness can be proved by reasoning on the program's source code.
93The code that is actually run, however, is not the high level source code that we reason on, but low level code generated by the compiler.
94A few questions now arise:
95\begin{itemize*}
96\item
97What properties are preserved during compilation?
98\item
99What properties are affected by the compilation strategy?
100\item
101To what extent can you trust your compiler in preserving those properties?
102\end{itemize*}
103These questions, and others like them, motivate a current `hot topic' in computer science research: \emph{compiler verification} (for instance~\cite{leroy:formal:2009,chlipala:verified:2010}, and many others).
104So far, the field has only been focused on the first and last questions.
105Much attention has been placed on verifying compiler correctness with respect to extensional properties of programs.
106These are `easily' preserved during compilation.
107
108If we consider intensional properties of programs---space, time, and so forth---the situation is more complex.
109To express these properties, and reason about them, we must adopt a cost model that assigns a cost to single, or blocks, of instructions.
110A compositional cost model, assigning the same cost to all occurrences of one instruction, would be ideal.
111However, compiler optimisations are inherently non-compositional: each occurrence of a high level instruction may be compiled in a different way depending on its context.
112Therefore both the cost model and intensional specifications are affected by the compilation process.
113
114In the CerCo project (`Certified Complexity')~\cite{cerco:2011} we approach the problem of reasoning about intensional properties of programs as follows.
115We are currently developing a compiler that induces a cost model on high level source code.
116Costs are assigned to each block of high level instructions by considering the costs of the corresponding blocks of compiled code.
117The cost model is therefore inherently non-compositional, but has the potential to be extremely \emph{precise}, capturing a program's \emph{realistic} cost.
118That is, the compilation process is taken into account, not ignored.
119A prototype compiler, where no approximation of the cost is provided, has been developed.
120(The technical details of the cost model is explained in~\cite{amadio:certifying:2010}.)
121
122We believe that our approach is applicable to certifying real time programs.
123A user can certify that `deadlines' are met whilst wringing as many clock cycles from the processor---using a cost model that does not over-estimate---as possible.
124
125We also see our approach as being relevant to compiler verification (and construction) itself.
126\emph{An optimisation specified only extensionally is only half specified}.
127Though the optimisation may preserve the denotational semantics of a program, there is no guarantee that any intensional properties of the program will be improved.
128
129Another potential application is toward completeness and correctness of the compilation process in the presence of space constraints.
130A compiler could reject a source program targetting an embedded system when the size of the compiled code exceeds the available ROM size.
131Preservation of a program's semantics may only be required for those programs that do not exhaust the stack or heap.
132The statement of completeness of the compiler must therefore take in to account a realistic cost model.
133
134With the CerCo methodology, we assume we can assign to object code exact and realistic costs for sequential blocks of instructions.
135This is possible with modern processors (see~\cite{bate:wcet:2011,yan:wcet:2008} for instance) but difficult, as the structure and execution of a program itself has an influence on the speed of processing.
136Caching, memory effects, and advanced features such as branch prediction all have an effect on execution speed.
137For this reason CerCo decided to focus on 8-bit microprocessors.
138These are still used in embedded systems, with the advantage of a predictable cost model due to their relative paucity of features.
139
140We have fully formalised an executable formal semantics of a family of 8-bit Freescale microprocessors~\cite{oliboni:matita:2008}, and provided a similar executable formal semantics for the MCS-51 microprocessor.
141The latter is what we describe in this paper.
142The focus of the formalisation has been on capturing the intensional behaviour of the processor.
143However, the design of the MCS-51 itself has caused problems in our formalisation.
144For example, the MCS-51 has a highly unorthogonal instruction set.
145To cope with this unorthogonality, and to produce an executable specification, we rely on Matita's dependent types.
146
147\paragraph{The MCS-51}\quad
148The MCS-51 is an 8-bit microprocessor introduced by Intel in the late 1970s.
149Commonly called the 8051, in the decades since its introduction the processor has become a popular component of embedded systems.
150The processor, its successor the 8052, and derivatives are still manufactured \emph{en masse} by a host of vendors.
151
152The 8051 is a well documented processor, and has the support of numerous open source and commercial tools, such as compilers and emulators.
153For instance, the open source Small Device C Compiler (SDCC) recognises a dialect of C~\cite{sdcc:2010}, and other compilers for BASIC, Forth and Modula-2 are also extant.
154An open source emulator for the processor, MCU 8051 IDE, is also available~\cite{mcu8051ide:2010}.
155Both MCU 8051 IDE and SDCC were used in for validating our formalisation.
156
157\begin{figure}[t]
158\setlength{\unitlength}{0.87pt}
159\begin{picture}(410,250)(-50,200)
160%\put(-50,200){\framebox(410,250){}}
161\put(12,410){\makebox(80,0)[b]{Internal (256B)}}
162\put(13,242){\line(0,1){165}}
163\put(93,242){\line(0,1){165}}
164\put(13,407){\line(1,0){80}}
165\put(12,400){\makebox(0,0)[r]{0h}}  \put(14,400){\makebox(0,0)[l]{Register bank 0}}
166\put(13,393){\line(1,0){80}}
167\put(12,386){\makebox(0,0)[r]{8h}}  \put(14,386){\makebox(0,0)[l]{Register bank 1}}
168\put(13,379){\line(1,0){80}}
169\put(12,372){\makebox(0,0)[r]{10h}}  \put(14,372){\makebox(0,0)[l]{Register bank 2}}
170\put(13,365){\line(1,0){80}}
171\put(12,358){\makebox(0,0)[r]{18h}} \put(14,358){\makebox(0,0)[l]{Register bank 3}}
172\put(13,351){\line(1,0){80}}
173\put(12,344){\makebox(0,0)[r]{20h}} \put(14,344){\makebox(0,0)[l]{Bit addressable}}
174\put(13,323){\line(1,0){80}}
175\put(12,316){\makebox(0,0)[r]{30h}}
176  \put(14,309){\makebox(0,0)[l]{\quad \vdots}}
177\put(13,291){\line(1,0){80}}
178\put(12,284){\makebox(0,0)[r]{80h}}
179  \put(14,263){\makebox(0,0)[l]{\quad \vdots}}
180\put(12,249){\makebox(0,0)[r]{ffh}}
181\put(13,242){\line(1,0){80}}
182
183\qbezier(-2,407)(-6,407)(-6,393)
184\qbezier(-6,393)(-6,324)(-10,324)
185\put(-12,324){\makebox(0,0)[r]{Indirect/stack}}
186\qbezier(-6,256)(-6,324)(-10,324)
187\qbezier(-2,242)(-6,242)(-6,256)
188
189\qbezier(94,407)(98,407)(98,393)
190\qbezier(98,393)(98,349)(102,349)
191\put(104,349){\makebox(0,0)[l]{Direct}}
192\qbezier(98,305)(98,349)(102,349)
193\qbezier(94,291)(98,291)(98,305)
194
195\put(102,242){\framebox(20,49){SFR}}
196% bit access to sfrs?
197
198\qbezier(124,291)(128,291)(128,277)
199\qbezier(128,277)(128,266)(132,266)
200\put(134,266){\makebox(0,0)[l]{Direct}}
201\qbezier(128,257)(128,266)(132,266)
202\qbezier(124,242)(128,242)(128,256)
203
204\put(164,410){\makebox(80,0)[b]{External (64kB)}}
205\put(164,220){\line(0,1){187}}
206\put(164,407){\line(1,0){80}}
207\put(244,220){\line(0,1){187}}
208\put(164,242){\line(1,0){80}}
209\put(163,400){\makebox(0,0)[r]{0h}}
210\put(164,324){\makebox(80,0){Paged access}}
211  \put(164,310){\makebox(80,0){Direct/indirect}}
212\put(163,235){\makebox(0,0)[r]{80h}}
213  \put(164,228){\makebox(80,0){\vdots}}
214  \put(164,210){\makebox(80,0){Direct/indirect}}
215
216\put(264,410){\makebox(80,0)[b]{Code (64kB)}}
217\put(264,220){\line(0,1){187}}
218\put(264,407){\line(1,0){80}}
219\put(344,220){\line(0,1){187}}
220\put(263,400){\makebox(0,0)[r]{0h}}
221  \put(264,228){\makebox(80,0){\vdots}}
222  \put(264,324){\makebox(80,0){Direct}}
223  \put(264,310){\makebox(80,0){PC relative}}
224\end{picture}
225\caption{The 8051 memory model}
226\label{fig.memory.layout}
227\end{figure}
228
229The 8051 has a relatively straightforward architecture, unencumbered by advanced features of modern processors, making it an ideal target for formalisation.
230A high-level overview of the processor's memory layout, along with the ways in which different memory spaces may be addressed, is provided in Figure~\ref{fig.memory.layout}.
231
232Processor RAM is divided into numerous segments, with the most prominent division being between internal and (optional) external memory.
233Internal memory, commonly provided on the die itself with fast access, is composed of 256 bytes, but, in direct addressing mode, half of them are overloaded with 128 bytes of memory-mapped Special Function Registers (SFRs).
234SFRs control the operation of the processor.
235Internal RAM (IRAM) is divided again into 8 general purpose bit-addressable registers (R0--R7).
236These sit in the first 8 bytes of IRAM, though can be programmatically `shifted up' as needed.
237Bit memory, followed by a small amount of stack space, resides in the memory space immediately following the register banks.
238What remains of IRAM may be treated as general purpose memory.
239A schematic view of IRAM layout is also provided in Figure~\ref{fig.memory.layout}.
240
241External RAM (XRAM), limited to a maximum size of 64 kilobytes, is optional, and may be provided on or off chip, depending on the vendor.
242XRAM is accessed using a dedicated instruction, and requires 16 bits to address fully.
243External code memory (XCODE) is often stored as an EPROM, and limited to 64 kilobytes in size.
244However, depending on the particular processor model, a dedicated on-die read-only memory area for program code (ICODE) may be supplied.
245
246Memory may be addressed in numerous ways: immediate, direct, indirect, external direct and code indirect.
247As the latter two addressing modes hint, there are some restrictions enforced by the 8051, and its derivatives, on which addressing modes may be used with specific types of memory.
248For instance, the extra 128 bytes of IRAM of the 8052 cannot be addressed using indirect addressing; rather, external (in)direct addressing must be used. Moreover, some memory segments are addressed using 8-bit pointers while others require 16-bits.
249
250The 8051 possesses an 8-bit Arithmetic and Logic Unit (ALU), with a variety of instructions for performing arithmetic and logical operations on bits and integers.
251Two 8-bit general purpose accumulators, A and B, are provided.
252
253Communication with the device is handled by an inbuilt UART serial port and controller.
254This can operate in numerous modes.
255Serial baud rate is determined by one of two 16-bit timers included with the 8051, which can be set to multiple modes of operation.
256(The 8052 provides an additional 16-bit timer.)
257The 8051 also provides a 4 byte bit-addressable I/O port.
258
259The programmer may take advantage of an interrupt mechanism.
260This is especially useful when dealing with I/O involving the serial device, as an interrupt can be set when a whole character is sent or received via the UART.
261
262Interrupts immediately halt the flow of execution of the processor, and cause the program counter to jump to a fixed address, where the requisite interrupt handler is stored.
263However, interrupts may be set to one of two priorities: low and high.
264The interrupt handler of an interrupt with high priority is executed ahead of the interrupt handler of an interrupt of lower priority, interrupting a currently executing handler of lower priority, if necessary.
265
266The 8051 has interrupts disabled by default.
267The programmer is free to handle serial input and output manually, by poking serial flags in the SFRs.
268`Exceptional circumstances' that would otherwise trigger an interrupt on more modern processors, (e.g. division by zero) are also signalled by setting flags.
269
270%\begin{figure}[t]
271%\begin{center}
272%\includegraphics[scale=0.5]{iramlayout.png}
273%\end{center}
274%\caption{Schematic view of 8051 IRAM layout}
275%\label{fig.iram.layout}
276%\end{figure}
277
278\paragraph{Overview of paper}\quad
279In Section~\ref{sect.design.issues.formalisation} we discuss design issues in the development of the formalisation.
280In Section~\ref{sect.validation} we discuss how we validated the design and implementation of our emulator to ensure that what we formalised was an accurate model of an MCS-51 series microprocessor.
281In Section~\ref{sect.related.work} we describe previous work, with an eye toward describing its relation with the work described herein.
282In Section~\ref{sect.conclusions} we conclude.
283
284%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285% SECTION                                                                      %
286%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287\section{Design issues in the formalisation}
288\label{sect.design.issues.formalisation}
289
290From hereonin, we typeset O'Caml source with \texttt{\color{blue}{blue}} and Matita source with \texttt{\color{red}{red}}.
291Matita's syntax is straightforward if familiar with Coq or O'Caml.
292One subtlety is the use of `\texttt{?}' in an argument position denoting an argument to be inferred.
293
294A full account of the formalisation can be found in~\cite{cerco-report:2011}.
295
296\subsection{Development strategy}
297\label{subsect.development.strategy}
298
299Our implementation progressed in two stages.
300We began with an emulator written in O'Caml to `iron out' any bugs in the design and implementation.
301O'Caml's ability to perform file I/O also eased debugging and validation.
302Once we were happy with the design of the O'Caml emulator, we moved to Matita.
303
304Matita's syntax is lexically similar to O'Caml's.
305This eased the translation, as swathes of code were copied with minor modifications.
306However, several major issues had to be addresses when moving from to Matita.
307These are now discussed.
308
309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310% SECTION                                                                      %
311%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312\subsection{Representation of bytes, words, etc.}
313\label{subsect.representation.integers}
314
315\begin{figure}[t]
316\begin{minipage}[t]{0.45\textwidth}
317\vspace{0pt}
318\begin{lstlisting}
319type 'a vect = bit list
320type nibble = [`Sixteen] vect
321type byte = [`Eight] vect
322$\color{blue}{\mathtt{let}}$ split_word w = split_nth 4 w
323$\color{blue}{\mathtt{let}}$ split_byte b = split_nth 2 b
324\end{lstlisting}
325\end{minipage}
326%
327\begin{minipage}[t]{0.55\textwidth}
328\vspace{0pt}
329\begin{lstlisting}
330type 'a vect
331type word = [`Sixteen] vect
332type byte = [`Eight] vect
333val split_word: word -> byte * word
334val split_byte: byte -> nibble * nibble
335\end{lstlisting}
336\end{minipage}
337\caption{Sample of O'Caml implementation and interface for bitvectors module}
338\label{fig.ocaml.implementation.bitvectors}
339\end{figure}
340
341The formalization of MCS-51 must deal with bytes (8-bits), words (16-bits), and also more exoteric quantities (7, 3 and 9-bits).
342To avoid difficult-to-trace size mismatch bugs, we represented all quantities using bitvectors, i.e. fixed length vectors of booleans.
343In our O'Caml emulator, we `faked' bitvectors using phantom types~\cite{leijen:domain:1999} implemented with polymorphic variants~\cite{garrigue:programming:1998}, as in Figure~\ref{fig.ocaml.implementation.bitvectors}.
344From within the bitvector module (left column) bitvectors are just lists of bits and no guarantee is provided on sizes.
345However, the module's interface (right column) enforces size invariants in the rest of the code.
346
347In Matita, we are able to use the full power of dependent types to always work with vectors of a known size:
348\begin{lstlisting}
349inductive Vector (A: Type[0]): nat $\rightarrow$ Type[0] ≝
350  VEmpty: Vector A O
351| VCons: $\forall$n: nat. A $\rightarrow$ Vector A n $\rightarrow$ Vector A (S n).
352\end{lstlisting}
353We define \texttt{BitVector} as a specialization of \texttt{Vector} to \texttt{bool}.
354We may use Matita's type system to provide precise typings for functions that are polymorphic in the size without code duplication:
355\begin{lstlisting}
356let rec split (A: Type[0]) (m,n: nat) on m:
357   Vector A (plus m n) $\rightarrow$ (Vector A m) $\times$ (Vector A n) := ...
358\end{lstlisting}
359
360%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361% SECTION                                                                      %
362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363\subsection{Representing memory}
364\label{subsect.representing.memory}
365
366The MCS-51 has numerous disjoint memory spaces addressed by differently sized pointers.
367In our O'Caml implementation, we use a map data structure (from the standard library) for each space.
368Matita's standard library is small, and does not contain a generic map data structure.
369We had the opportunity of crafting a dependently typed special-purpose data structure for the job to enforce the correspondence between the size of pointer and the size of the memory space.
370Further, we assumed that large swathes of memory would often be uninitialized.
371
372We picked a modified form of trie of fixed height $h$.
373Paths are represented by bitvectors (already used in our implementation for addresses and registers) of length $h$:
374\begin{lstlisting}
375inductive BitVectorTrie (A: Type[0]): nat $\rightarrow$ Type[0] ≝
376  Leaf: A $\rightarrow$ BitVectorTrie A 0
377| Node: ∀n. BitVectorTrie A n $\rightarrow$ BitVectorTrie A n $\rightarrow$ BitVectorTrie A (S n)
378| Stub: ∀n. BitVectorTrie A n.
379\end{lstlisting}
380\texttt{Stub} is a constructor that can appear at any point in a trie.
381It represents `uninitialized data'.
382Performing a lookup in memory is now straight-forward.
383The only subtlety over normal trie lookup is how we handle \texttt{Stub}.
384We traverse a path, and upon encountering \texttt{Stub}, we return a default value\footnote{All manufacturer data sheets that we consulted were silent on the subject of what should be returned if we attempt to access uninitialized memory.  We defaulted to simply returning zero, though our \texttt{lookup} function is parametric in this choice.  We do not believe that this is an outrageous decision, as SDCC for instance generates code which first `zeroes out' all memory in a preamble before executing the program proper.  This is in line with the C standard, which guarantees that all global variables will be zero initialized piecewise.}.
385
386%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387% SECTION                                                                      %
388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389\subsection{Labels and pseudoinstructions}
390\label{subsect.labels.pseudoinstructions}
391
392Aside from implementing the core MCS-51 instruction set, we also provided \emph{pseudoinstructions}, \emph{labels} and \emph{cost labels}.
393The purpose of \emph{cost labels} will be explained in Subsection~\ref{subsect.computation.cost.traces}.
394
395Introducing pseudoinstructions had the effect of simplifying a C compiler---another component of the CerCo project---that was being implemented in parallel with our implementation.
396To see why, consider the fact that the MCS-51's instruction set has numerous instructions for unconditional and conditional jumps to memory locations.
397For instance, the instructions \texttt{AJMP}, \texttt{JMP} and \texttt{LJMP} all perform unconditional jumps.
398However, these instructions differ in how large the maximum size of the offset of the jump to be performed can be.
399Further, all jump instructions require a concrete memory address---to jump to---to be specified.
400Compilers that support separate compilation cannot directly compute these offsets and select the appropriate jump instructions.
401These operations are also burdensome for compilers that do not do separate compilation and are handled by assemblers.
402We followed suit.
403
404While introducing pseudoinstructions, we also introduced labels for locations to jump to, and for global data.
405To specify global data via labels, we introduced a preamble before the program where labels and the size of reserved space for data is stored.
406A pseudoinstruction \texttt{Mov} moves (16-bit) data stored at a label into the MCS-51's one 16-bit register, \texttt{DPTR}.
407
408Our pseudoinstructions and labels induce an assembly language similar to that of SDCC's.
409All pseudoinstructions and labels are `assembled away' prior to program execution.
410Jumps are computed in two stages.
411A map associating memory addresses to labels is built, before replacing pseudojumps with concrete jumps to the correct address.
412The algorithm currently implemented does not try to minimize object code size by picking the shortest possible jump instruction.
413A better algorithm is left for future work.
414
415%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416% SECTION                                                                      %
417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418\subsection{Anatomy of the (Matita) emulator}
419\label{subsect.anatomy.matita.emulator}
420
421The internal state of the Matita emulator is represented as a record:
422\begin{lstlisting}
423record Status: Type[0] ≝ {
424  code_memory: BitVectorTrie Byte 16;
425  low_internal_ram: BitVectorTrie Byte 7;
426  high_internal_ram: BitVectorTrie Byte 7;
427  external_ram: BitVectorTrie Byte 16;
428  program_counter: Word;
429  special_function_registers_8051: Vector Byte 19;
430  special_function_registers_8052: Vector Byte 5;
431  ...  }.
432\end{lstlisting}
433This record encapsulates the current memory contents, the program counter, the state of the current SFRs, and so on.
434
435Here the MCS-51's memory model is implemented using four disjoint memory spaces, plus SFRs.
436From the programmer's point of view, what \emph{really} matters are the addressing modes that are in a many-to-many relationship with the spaces.
437\texttt{DIRECT} addressing can be used to address either lower IRAM (if the first bit is 0) or the SFRs (if the first bit is 1), for instance.
438That's why DIRECT uses 8-bit addresses but pointers to lower IRAM only use 7 bits.
439The complexity of the memory model is captured in a pair of functions, \texttt{get\_arg\_XX} and \texttt{set\_arg\_XX}, that `get' and `set' data of size \texttt{XX} from memory.
440
441%Overlapping, and checking which addressing modes can be used to address particular memory spaces, is handled through numerous \texttt{get\_arg\_XX} and \texttt{set\_arg\_XX} (for 1, 8 and 16 bits) functions.
442
443Both the Matita and O'Caml emulators follow the classic `fetch-decode-execute' model of processor operation.
444The next instruction to be processed, indexed by the program counter, is fetched from code memory with \texttt{fetch}.
445An updated program counter, along with its concrete cost in processor cycles, is also returned.
446These costs are taken from a Siemens Semiconductor Group data sheet for the MCS-51~\cite{siemens:2011}, and will likely vary across manufacturers and derivatives of the processor.
447\begin{lstlisting}
448definition fetch: BitVectorTrie Byte 16 $\rightarrow$ Word $\rightarrow$ instruction $\times$ Word $\times$ nat
449\end{lstlisting}
450Instruction are assembled to bit encodings by \texttt{assembly1}:
451\begin{lstlisting}
452definition assembly1: instruction $\rightarrow$ list Byte
453\end{lstlisting}
454An assembly program---comprising a preamble containing global data and a list of pseudoinstructions---is assembled using \texttt{assembly}.
455Pseudoinstructions and labels are eliminated in favour of instructions from the MCS-51 instruction set.
456A map associating memory locations and cost labels (see Subsection~\ref{subsect.computation.cost.traces}) is produced.
457\begin{lstlisting}
458definition assembly:
459  assembly_program $\rightarrow$ option (list Byte $\times$ (BitVectorTrie String 16))
460\end{lstlisting}
461A single fetch-decode-execute cycle is performed by \texttt{execute\_1}:
462\begin{lstlisting}
463definition execute_1: Status $\rightarrow$ Status
464\end{lstlisting}
465The \texttt{execute} functions performs a fixed number of cycles by iterating
466\texttt{execute\_1}:
467\begin{lstlisting}
468let rec execute (n: nat) (s: Status) on n: Status := ...
469\end{lstlisting}
470This differs from the O'Caml emulator, which executed a program indefinitely.
471A callback function was also accepted as an argument, which could `witness' the execution as it happened, providing a print-out of the processor state.
472Due to Matita's termination requirement, \texttt{execute} cannot execute a program indefinitely.
473An alternative approach would be to produce an infinite stream of statuses representing an execution trace.
474Matita supports infinite streams through co-inductive types.
475
476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477% SECTION                                                                      %
478%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479\subsection{Instruction set unorthogonality}
480\label{subsect.instruction.set.unorthogonality}
481
482A peculiarity of the MCS-51 is its unorthogonal instruction set.
483For instance, the \texttt{MOV} instruction can be invoked using one of 16 combinations of addressing modes out of a possible 361.
484
485% Show example of pattern matching with polymorphic variants
486
487Such unorthogonality in the instruction set was handled with the use of polymorphic variants in O'Caml.
488For instance, we introduced types corresponding to each addressing mode:
489\begin{lstlisting}
490type direct = [ `DIRECT of byte ]
491type indirect = [ `INDIRECT of bit ]
492...
493\end{lstlisting}
494Which were then combined in our inductive datatype for assembly instructions using the union operator `$|$':
495\begin{lstlisting}
496type 'addr preinstruction =
497 [ `ADD of acc * [ reg | direct | indirect | data ]
498...
499 | `MOV of
500    (acc * [ reg | direct | indirect | data ],
501     [ reg | indirect ] * [ acc | direct | data ],
502     direct * [ acc | reg | direct | indirect | data ],
503     dptr * data16,
504     carry * bit,
505     bit * carry
506     ) union6
507...
508\end{lstlisting}
509Here, \texttt{union6} is a disjoint union type, defined as follows:
510\begin{lstlisting}
511type ('a,'b,'c,'d,'e,'f) union6 = [ `U1 of 'a | ... | `U6 of 'f ]
512\end{lstlisting}
513For our purposes, the types \texttt{union2}, \texttt{union3} and \texttt{union6} sufficed.
514
515This polymorphic variant machinery worked well: it introduced a certain level of type safety (for instance, the type of \texttt{MOV} above guarantees it cannot be invoked with arguments in the \texttt{carry} and \texttt{data16} addressing modes, respectively), and also allowed us to pattern match against instructions, when necessary.
516However, this polymorphic variant machinery is \emph{not} present in Matita.
517We needed some way to produce the same effect, which Matita supported.
518For this task, we used dependent types.
519
520We first provided an inductive data type representing all possible addressing modes, a type that functions will pattern match against:
521\begin{lstlisting}
522inductive addressing_mode: Type[0] ≝
523  DIRECT: Byte $\rightarrow$ addressing_mode
524| INDIRECT: Bit $\rightarrow$ addressing_mode
525...
526\end{lstlisting}
527We also wished to express in the type of functions the \emph{impossibility} of pattern matching against certain constructors.
528In order to do this, we introduced an inductive type of addressing mode `tags'.
529The constructors of \texttt{addressing\_mode\_tag} are in one-to-one correspondence with the constructors of \texttt{addressing\_mode}:
530\begin{lstlisting}
531inductive addressing_mode_tag : Type[0] ≝
532  direct: addressing_mode_tag
533| indirect: addressing_mode_tag
534...
535\end{lstlisting}
536The \texttt{is\_a} function checks if an \texttt{addressing\_mode} matches an \texttt{addressing\_mode\_tag}:
537\begin{lstlisting}
538let rec is_a (d: addressing_mode_tag) (A: addressing_mode) on d :=
539  match d with
540   [ direct $\Rightarrow$ match A with [ DIRECT _ $\Rightarrow$ true | _ $\Rightarrow$ false ]
541   | indirect $\Rightarrow$ match A with [ INDIRECT _ $\Rightarrow$ true | _ $\Rightarrow$ false ]
542...
543\end{lstlisting}
544The \texttt{is\_in} function checks if an \texttt{addressing\_mode} matches a set of tags represented as a vector. It simply extends the \texttt{is\_a} function in the obvious manner.
545
546A \texttt{subaddressing\_mode} is an \emph{ad hoc} non-empty $\Sigma$-type of \texttt{addressing\_mode}s constrained to be in a set of tags:
547\begin{lstlisting}
548record subaddressing_mode n (l: Vector addressing_mode_tag (S n)): Type[0] :=
549 { subaddressing_modeel :> addressing_mode;
550   subaddressing_modein: bool_to_Prop (is_in ? l subaddressing_modeel) }.
551\end{lstlisting}
552An implicit coercion is provided to promote vectors of tags (denoted with $\llbracket - \rrbracket$) to the corresponding \texttt{subaddressing\_mode} so that we can use a syntax close to that of O'Caml to specify \texttt{preinstruction}s:
553\begin{lstlisting}
554inductive preinstruction (A: Type[0]): Type[0] ≝
555   ADD: $\llbracket$ acc_a $\rrbracket$ $\rightarrow$ $\llbracket$ register; direct; indirect; data $\rrbracket$ $\rightarrow$ preinstruction A
556 | ADDC: $\llbracket$ acc_a $\rrbracket$ $\rightarrow$ $\llbracket$ register; direct; indirect; data $\rrbracket$ $\rightarrow$ preinstruction A
557...
558\end{lstlisting}
559The constructor \texttt{ADD} expects two parameters, the first being the accumulator A (\texttt{acc\_a}), the second being a register, direct, indirect or data addressing mode.
560
561% One of these coercions opens up a proof obligation which needs discussing
562% Have lemmas proving that if an element is a member of a sub, then it is a member of a superlist, and so on
563The final component is a pair of type coercions from \texttt{addressing\_mode} to \texttt{subaddressing\_mode} and from \texttt{subaddressing\_mode} to \texttt{Type$\lbrack0\rbrack$}, respectively.
564The first is a forgetful coercion, while the second opens a proof obligation wherein we must prove that the provided value is in the admissible set.
565These coercions were first introduced by PVS to implement subset types~\cite{shankar:principles:1999}, and later in Coq as part of Russell~\cite{sozeau:subset:2006}.
566In Matita all coercions can open proof obligations.
567
568Proof obligations require us to state and prove a few auxilliary lemmas related to the transitivity of subtyping.
569For instance, an \texttt{addressing\_mode} that belongs to an allowed set also belongs to any one of its supersets.
570At the moment, Matita's automation exploits these lemmas to completely solve all the proof obligations opened in our formalisation.
571The \texttt{execute\_1} function, for instance, opens over 200 proof obligations during type checking.
572
573The machinery just described allows us to restrict the set of \texttt{addressing\_mode}s expected by a function and use this information during pattern matching.
574This allows us to skip impossible cases.
575For instance, consider \texttt{set\_arg\_16}, which expects only a \texttt{DPTR}:
576\begin{lstlisting}
577definition set_arg_16: Status $\rightarrow$ Word $\rightarrow$ $\llbracket$ dptr $\rrbracket$ $\rightarrow$ Status ≝ $~\lambda$s, v, a.
578   match a return $\lambda$x. bool_to_Prop (is_in ? $\llbracket$ dptr $\rrbracket$ x) $\rightarrow$ ? with
579     [ DPTR $\Rightarrow$ $\lambda$_: True.
580       let 〈 bu, bl 〉 := split $\ldots$ eight eight v in
581       let status := set_8051_sfr s SFR_DPH bu in
582       let status := set_8051_sfr status SFR_DPL bl in
583         status
584     | _ $\Rightarrow$ $\lambda$_: False. $\bot$ ] $~$(subaddressing_modein $\ldots$ a).
585\end{lstlisting}
586We give a proof (the expression \texttt{(subaddressing\_modein} $\ldots$ \texttt{a)}) that the argument $a$ is in the set $\llbracket$ \texttt{dptr} $\rrbracket$ to the \texttt{match} expression.
587In every case but \texttt{DPTR}, the proof is a proof of \texttt{False}, and the system opens a proof obligation $\bot$ that can be discarded using \emph{ex falso}.
588Attempting to match against a disallowed addressing mode (replacing \texttt{False} with \texttt{True} in the branch) produces a type error.
589
590We tried other dependently and non-dependently typed solutions before settling on this approach.
591As we need a large number of different combinations of addressing modes to describe the whole instruction set, it is infeasible to declare a datatype for each one of these combinations.
592The current solution is closest to the corresponding O'Caml code, to the point that the translation from O'Caml to Matita is almost syntactical.
593We would like to investigate the possibility of changing the code extraction procedure of Matita so that it recognises this programming pattern and outputs O'Caml code using polymorphic variants.
594
595% Talk about extraction to O'Caml code, which hopefully will allow us to extract back to using polymorphic variants, or when extracting vectors we could extract using phantom types
596% Discuss alternative approaches, i.e. Sigma types to piece together smaller types into larger ones, as opposed to using a predicate to `cut out' pieces of a larger type, which is what we did
597
598%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599% SECTION                                                                      %
600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601\subsection{I/O and timers}
602\label{subsect.i/o.timers}
603
604% `Real clock' for I/O and timers
605The O'Caml emulator has code for handling timers, asynchronous I/O and interrupts (these are not yet ported to the Matita emulator).
606All three of these features interact with each other in subtle ways.
607Interrupts can `fire' when an input is detected on the processor's UART port, and, in certain modes, timers reset when a high signal is detected on one of the MCS-51's communication pins.
608
609To accurately model timers and I/O, we add an unbounded integral field \texttt{clock} to the central \texttt{status} record.
610This field is only logical, since it does not represent any quantity stored in the physical processor, and is used to keep track of the current `processor time'.
611Before every execution step, \texttt{clock} is incremented by the number of processor cycles that the instruction just fetched will take to execute.
612The emulator then executes the instruction, followed by the code implementing the timers and I/O\footnote{Though it isn't fully specified by the manufacturer's data sheets if I/O is handled at the beginning or the end of each cycle.}.
613In order to model I/O, we also store in \texttt{status} a \emph{continuation} which is a description of the behaviour of the environment:
614\begin{lstlisting}
615type line =
616  [ `P1 of byte | `P3 of byte
617  | `SerialBuff of [ `Eight of byte | `Nine of BitVectors.bit * byte ]]
618type continuation =
619  [`In of time * line * epsilon * continuation] option *
620  [`Out of (time -> line -> time * continuation)]
621\end{lstlisting}
622At each moment, the second projection of the continuation $k$ describes how the environment will react to an output event performed in the future by the processor.
623Suppose $\pi_2(k)(\tau,o) = \langle \tau',k' \rangle$.
624If the emulator at time $\tau$ starts an asynchronous output $o$ either on the P1 or P3 output lines, or on the UART, then the environment will receive the output at time $\tau'$.
625Moreover \texttt{status} is immediately updated with the continuation $k'$.
626
627Further, if $\pi_1(k) = \mathtt{Some}~\langle \tau',i,\epsilon,k'\rangle$, then at time $\tau'$ the environment will send the asynchronous input $i$ to the emulator and \texttt{status} is updated with the continuation $k'$.
628This input is visible to the emulator only at time $\tau' + \epsilon$.
629
630The time required to perform an I/O operation is partially specified in the data sheets of the UART module.
631This computation is complex so we prefer to abstract over it.
632We leave the computation of the delay time to the environment.
633
634We use only the P1 and P3 lines despite the MCS-51 having four output lines, P0--P3.
635This is because P0 and P2 become inoperable if the processor is equipped with XRAM (we assume it is).
636
637The UART port can work in several modes, depending on the how the SFRs are set.
638In an asyncrhonous mode, the UART transmits 8 bits at a time, using a ninth line for synchronisation.
639In a synchronous mode the ninth line is used to transmit an additional bit.
640
641%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
642% SECTION                                                                      %
643%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644\subsection{Computation of cost traces}
645\label{subsect.computation.cost.traces}
646
647As mentioned in Subsection~\ref{subsect.labels.pseudoinstructions} we introduced a notion of \emph{cost label}.
648Cost labels are inserted by the prototype C compiler at specific locations in the object code.
649Roughly, for those familiar with control flow graphs, they are inserted at the start of every basic block.
650
651Cost labels are used to calculate a precise costing for a program by marking the location of basic blocks.
652During the assembly phase, where labels and pseudoinstructions are eliminated, a map is generated associating cost labels with memory locations.
653This map is later used in a separate analysis which computes the cost of a program by traversing through a program, fetching one instruction at a time, and computing the cost of blocks.
654These block costings are stored in another map, and will later be passed back to the prototype compiler.
655
656%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657% SECTION                                                                      %
658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659\section{Validation}
660\label{sect.validation}
661
662We spent considerable effort attempting to ensure that what we have formalised is an accurate model of the MCS-51 microprocessor.
663
664We made use of multiple data sheets, each from a different semiconductor manufacturer.
665This helped us triangulate errors in the specification of the processor's instruction set, and its behaviour, for instance, in a data sheet from Philips Semiconductor.
666
667The O'Caml prototype was especially useful for validation purposes.
668We wrote a module for parsing and loading Intel HEX format files.
669Intel HEX is a standard format that all compilers targetting the MCS-51, and similar processors, produce.
670It is essentially a snapshot of the processor's code memory in compressed form.
671Using this we were able to compile C programs with SDCC and load the resulting program directly into our emulator's code memory, ready for execution.
672Further, we can produce a HEX file from our emulator's code memory for loading into third party tools.
673After each step of execution, we can print out both the instruction that had been executed and a snapshot of the processor's state, including all flags and register contents.
674For example:
675\begin{frametxt}
676\begin{small}
677\begin{verbatim}
678...
67908: mov 81 #07
680
681 Processor status:                               
682
683   ACC: 0   B: 0   PSW: 0
684    with flags set as:
685     CY: false  AC: false  FO: false  RS1: false
686     RS0: false  OV: false UD: false  P: false
687   SP: 7  IP: 0  PC: 8  DPL: 0  DPH: 0  SCON: 0
688   SBUF: 0  TMOD: 0  TCON: 0
689   Registers:                                   
690    R0: 0  R1: 0  R2: 0  R3: 0  R4: 0  R5: 0  R6: 0  R7: 0
691...
692\end{verbatim}
693\end{small}
694\end{frametxt}
695Here, the trace indicates that the instruction \texttt{mov 81 \#07} has just been executed by the processor, which is now in the state indicated.
696These traces were useful in spotting anything that was `obviously' wrong with the execution of the program.
697
698We further used MCU 8051 IDE as a reference, which allows a user to step through an assembly program one instruction at a time.
699Using our execution traces, we were able to step through a compiled program in MCU 8051 IDE and compare the resulting execution trace with the trace produced by our emulator.
700
701Our Matita formalisation was largely copied from the O'Caml source code, apart from the changes already mentioned.
702However, as the Matita emulator is executable, we could perform further validation by comparing the trace of a program's execution in the Matita emulator with the trace of the same program in the O'Caml emulator.
703
704%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
705% SECTION                                                                      %
706%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707\section{Related work}
708\label{sect.related.work}
709A large body of literature on the formalisation of microprocessors exists.
710The majority of it deals with proving correctness of implementations of microprocessors at the microcode or gate level.
711We are interested in providing a precise specification of the behaviour of the microprocessor in order to prove the correctness of a compiler which will target the processor.
712In particular, we are interested in intensional properties of the processor; precise timings of instruction execution in clock cycles.
713Moreover, in addition to formalising the interface of an MCS-51 processor, we have also built a complete MCS-51 ecosystem: UART, I/O lines, and hardware timers, complete with an assembler.
714
715Work closely related to our own can be found in~\cite{fox:trustworthy:2010}.
716Here, the authors describe the formalisation, in HOL4, of the ARMv7 instruction set architecture.
717They further point to an excellent list of references to related work in the literature for the interested reader.
718This formalisation also considers the machine code level, opposed to their formalisation, which only considering an abstract assembly language.
719In particular, instruction decoding is explicitly modeled inside HOL4's logic.
720We go further in also providing an assembly language, complete with assembler, to translate instructions and pseudoinstruction into machine code.
721
722Further, in~\cite{fox:trustworthy:2010} the authors validated their formalisation by using development boards and random testing.
723We currently rely on non-exhaustive testing against a third party emulator.
724We recognise the importance of this exhaustive testing, but currently leave it for future work.
725
726Executability is another key difference between our work and that of~\cite{fox:trustworthy:2010}.
727Our formalisation is executable: applying the emulation function to an input state eventually reduces to an output state.
728This is because Matita is based on a logic, CIC, which internalizes conversion.
729In~\cite{fox:trustworthy:2010} the authors provide an automation layer to derive single step theorems: if the processor is in a particular state that satisfies some preconditions, then after execution of an instruction it will reside in another state satisfying some postconditions.
730We do not need single step theorems of this form.
731
732Our main difficulties resided in the non-uniformity of an old 8-bit architecture, in terms of the instruction set, addressing modes and memory models.
733In contrast, the ARM instruction set and memory model is relatively uniform, simplifying any formalisation considerably.
734
735Perhaps the closest project to CerCo is CompCert~\cite{leroy:formally:2009}.
736CompCert concerns the certification of a C compiler and includes a formalisation in Coq of a subset of PowerPC.
737(Coq and Matita essentially share the same logic.)
738
739Despite this similarity, the two formalisations do not have much in common.
740First, CompCert provides a formalisation at the assembly level (no instruction decoding).
741This impels them to trust an unformalised assembler and linker, whereas we provide our own.
742Our formalisation is \emph{directly} executable, while the one in CompCert only provides a relation that describes execution.
743I/O is also not considered at all in CompCert.
744Moreover an idealized abstract and uniform memory model is assumed, while we take into account the complicated overlapping memory model of the MCS-51 architecture.
745Finally, 82 instructions of the more than 200 offered by the processor are formalised in CompCert, and the assembly language is augmented with macro instructions that are turned into `real' instructions only during communication with the external assembler.
746Even from a technical level the two formalisations differ: we tried to exploit dependent types whilst CompCert largely sticks to a non-dependent fragment of Coq.
747
748In~\cite{atkey:coqjvm:2007} an executable specification of the Java Virtual Machine, using dependent types, is presented.
749As we do, dependent types there are used to remove spurious partiality from the model.
750They also lower the need for over-specifying the behaviour of the processor in impossible cases.
751Our use of dependent types will also help to maintain invariants when we prove the correctness of the CerCo prototype C compiler.
752
753Finally~\cite{sarkar:semantics:2009} provides an executable semantics for x86-CC multiprocessor machine code.
754This machine code exhibits a high degree of non-uniformity similar to the MCS-51.
755However, only a small subset of the instruction set is considered, and they over-approximate the possibilities of unorthogonality of the instruction set, largely dodging the problems we had to face.
756
757Further, it seems that the definition of the decode function is potentially error prone.
758A small domain specific language of patterns is formalised in HOL4.
759This is similar to the specification language of the x86 instruction set found in manufacturer's data sheets.
760A decode function is implemented by copying lines from data sheets into the proof script, which are then interpreted.
761
762We are currently considering implementing a similar domain specific language in Matita.
763However, we would prefer to certify in Matita the compiler for this language.
764Data sheets could then be compiled down to the efficient code that we currently provide, instead of inefficiently interpreting the data sheets every time an instruction is executed.
765
766%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767% SECTION                                                                      %
768%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769\section{Conclusions}
770\label{sect.conclusions}
771
772In CerCo, we are interested in the certification of a compiler for C that induces a precise cost model on the source code.
773Our cost model assigns costs to blocks of instructions by tracing the way that blocks are compiled, and by computing exact costs on generated machine language.
774To perform this accurately, we have provided an executable semantics for the MCS-51 family of processors.
775The formalisation was done twice, first in O'Caml and then in Matita, and captures the exact timings of the processor (according to a Siemen's data sheet).
776Moreover, the O'Caml formalisation also considers timers and I/O.
777Adding support for I/O and timers in Matita is on-going work that will not present any major problem, and was delayed only because the addition is not immediately useful for the formalisation of the CerCo compiler.
778
779The formalisation is done at machine level and not at assembly level; we also formalise fetching and decoding.
780We separately provide an assembly language, enhanched with labels and pseudoinstructions, and an assembler from this language to machine code.
781This assembly language is similar to those found in `industrial strength' compilers, such as SDCC.
782We introduce cost labels in the machine language to relate the data flow of the assembly program to that of the C source language, in order to associate costs to the C program.
783For the O'Caml version, we provide a parser and pretty printer from code memory to Intel HEX format.
784Hence we can perform testing on programs compiled using any free or commercial compiler.
785
786Our main difficulty in formalising the MCS-51 was the unorthogonality of its memory model and instruction set.
787These problems are easily handled in O'Caml by using advanced language features like polymorphic variants and phantom types, simulating Generalized Abstract Data Types.
788In Matita, we use dependent types to recover the same flexibility, to reduce spurious partiality, and to grant invariants that will be later useful in other formalisations in the CerCo project.
789
790The formalisation has been partially verified by computing execution traces on selected programs and comparing them with an existing emulator.
791All instructions have been tested at least once, but we have not yet pushed testing further, for example with random testing or by using development boards.
792I/O in particular has not been tested yet, and it is currently unclear how to provide exhaustive testing in the presence of I/O.
793Finally, we are aware of having over-specified the processor in several places, by fixing a behaviour hopefully consistent with the real machine, where manufacturer data sheets are ambiguous or under-specified.
794
795\bibliography{itp-2011.bib}
796
797\end{document}
798
799\newpage
800
801\appendix
802
803\section{Listing of main O'Caml functions}
804\label{sect.listing.main.ocaml.functions}
805
806\subsubsection{From \texttt{ASMInterpret.ml(i)}}
807
808\begin{center}
809\begin{tabular*}{\textwidth}{p{3cm}@{\quad}p{9cm}}
810Name & Description \\
811\hline
812\texttt{assembly} & Assembles an abstract syntax tree representing an 8051 assembly program into a list of bytes, its compiled form. \\
813\texttt{initialize} & Initializes the emulator status. \\
814\texttt{load} & Loads an assembled program into the emulator's code memory. \\
815\texttt{fetch} & Fetches the next instruction, and automatically increments the program counter. \\
816\texttt{execute} & Emulates the processor.  Accepts as input a function that pretty prints the emulator status after every emulation loop. \\
817\end{tabular*}
818\end{center}
819
820\subsubsection{From \texttt{ASMCosts.ml(i)}}
821
822\begin{center}
823\begin{tabular*}{\textwidth}{p{3cm}@{\quad}p{9cm}}
824Name & Description \\
825\hline
826\texttt{compute} & Computes a map associating costings to basic blocks in the program.
827\end{tabular*}
828\end{center}
829
830\subsubsection{From \texttt{IntelHex.ml(i)}}
831
832\begin{center}
833\begin{tabular*}{\textwidth}{p{3cm}@{\quad}p{9cm}}
834Name & Description \\
835\hline
836\texttt{intel\_hex\_of\_file} & Reads in a file and parses it if in Intel IHX format, otherwise raises an exception. \\
837\texttt{process\_intel\_hex} & Accepts a parsed Intel IHX file and populates a hashmap (of the same type as code memory) with the contents.
838\end{tabular*}
839\end{center}
840
841\subsubsection{From \texttt{Physical.ml(i)}}
842
843\begin{center}
844\begin{tabular*}{\textwidth}{p{3cm}@{\quad}p{9cm}}
845Name & Description \\
846\hline
847\texttt{subb8\_with\_c} & Performs an eight bit subtraction on bitvectors.  The function also returns the most important PSW flags for the 8051: carry, auxiliary carry and overflow. \\
848\texttt{add8\_with\_c} & Performs an eight bit addition on bitvectors.  The function also returns the most important PSW flags for the 8051: carry, auxiliary carry and overflow. \\
849\texttt{dec} & Decrements an eight bit bitvector with underflow, if necessary. \\
850\texttt{inc} & Increments an eight bit bitvector with overflow, if necessary.
851\end{tabular*}
852\end{center}
853
854\newpage
855
856\section{Listing of main Matita functions}
857\label{sect.listing.main.matita.functions}
858
859\subsubsection{From \texttt{Arithmetic.ma}}
860
861\begin{center}
862\begin{tabular*}{\textwidth}{p{3cm}p{9cm}}
863Title & Description \\
864\hline
865\texttt{add\_n\_with\_carry} & Performs an $n$ bit addition on bitvectors.  The function also returns the most important PSW flags for the 8051: carry, auxiliary carry and overflow. \\
866\texttt{sub\_8\_with\_carry} & Performs an eight bit subtraction on bitvectors. The function also returns the most important PSW flags for the 8051: carry, auxiliary carry and overflow. \\
867\texttt{half\_add} & Performs a standard half addition on bitvectors, returning the result and carry bit. \\
868\texttt{full\_add} & Performs a standard full addition on bitvectors and a carry bit, returning the result and a carry bit.
869\end{tabular*}
870\end{center}
871
872\subsubsection{From \texttt{Assembly.ma}}
873
874\begin{center}
875\begin{tabular*}{\textwidth}{p{3cm}p{9cm}}
876Title & Description \\
877\hline
878\texttt{assemble1} & Assembles a single 8051 assembly instruction into its memory representation. \\
879\texttt{assemble} & Assembles an 8051 assembly program into its memory representation.\\
880\texttt{assemble\_unlabelled\_program} &\\& Assembles a list of (unlabelled) 8051 assembly instructions into its memory representation.
881\end{tabular*}
882\end{center}
883
884\subsubsection{From \texttt{BitVectorTrie.ma}}
885
886\begin{center}
887\begin{tabular*}{\textwidth}{p{3cm}p{9cm}}
888Title & Description \\
889\hline
890\texttt{lookup} & Returns the data stored at the end of a particular path (a bitvector) from the trie.  If no data exists, returns a default value. \\
891\texttt{insert} & Inserts data into a tree at the end of the path (a bitvector) indicated.  Automatically expands the tree (by filling in stubs) if necessary.
892\end{tabular*}
893\end{center}
894
895\subsubsection{From \texttt{DoTest.ma}}
896
897\begin{center}
898\begin{tabular*}{\textwidth}{p{3cm}p{9cm}}
899Title & Description \\
900\hline
901\texttt{execute\_trace} & Executes an assembly program for a fixed number of steps, recording in a trace which instructions were executed.
902\end{tabular*}
903\end{center}
904
905\subsubsection{From \texttt{Fetch.ma}}
906
907\begin{center}
908\begin{tabular*}{\textwidth}{p{3cm}p{9cm}}
909Title & Description \\
910\hline
911\texttt{fetch} & Decodes and returns the instruction currently pointed to by the program counter and automatically increments the program counter the required amount to point to the next instruction. \\
912\end{tabular*}
913\end{center}
914
915\subsubsection{From \texttt{Interpret.ma}}
916
917\begin{center}
918\begin{tabular*}{\textwidth}{p{3cm}p{9cm}}
919Title & Description \\
920\hline
921\texttt{execute\_1} & Executes a single step of an 8051 assembly program. \\
922\texttt{execute} & Executes a fixed number of steps of an 8051 assembly program.
923\end{tabular*}
924\end{center}
925
926\subsubsection{From \texttt{Status.ma}}
927
928\begin{center}
929\begin{tabular*}{\textwidth}{p{3cm}p{9cm}}
930Title & Description \\
931\hline
932\texttt{load} & Loads an assembled 8051 assembly program into code memory.
933\end{tabular*}
934\end{center}
935\end{document}
Note: See TracBrowser for help on using the repository browser.