1 |
210 |
ja_rd |
\chapter{Code samples}
|
2 |
|
|
\label{samples}
|
3 |
|
|
|
4 |
|
|
Directory /src directory contains a few test applications that can be
|
5 |
|
|
simulated and run on real hardware, except for the opcode test which can
|
6 |
|
|
only be simulated. See the readme file and the makefile for each program.\\
|
7 |
|
|
|
8 |
|
|
Please read the /src/reame.txt file for information that will probably be
|
9 |
|
|
more up-to-date than this doc.\\
|
10 |
|
|
|
11 |
|
|
The makefiles have been tested with the CodeSourcery toolchain for windows
|
12 |
|
|
(that can be downloaded from www.codesourcery.com) and with the Buildroot
|
13 |
|
|
toolchain for GNU/Linux.\\
|
14 |
|
|
|
15 |
|
|
Most makefiles have two targets, to create a simulation test bench and a
|
16 |
|
|
synthesizable demo.\\
|
17 |
|
|
|
18 |
|
|
Target 'sim' will build the simulation test bench package files as described
|
19 |
|
|
in section~\ref{logic_simulation}.
|
20 |
|
|
|
21 |
|
|
Target 'demo' will build a synthesizable demo; it will compile the sample
|
22 |
|
|
sources and place the resulting object code in file
|
23 |
221 |
ja_rd |
'/vhdl/SoC/bootstrap\_code\_pkg.vhdl'.\\
|
24 |
210 |
ja_rd |
|
25 |
|
|
The build process will produce two or more binary files ('*.code' and
|
26 |
|
|
'*.data', or '*.bin') that can be run on the software simulator, plus a
|
27 |
|
|
listing file (*.lst) handy for debugging.\\
|
28 |
|
|
|
29 |
|
|
All projects include a DOS batch file 'swsim.bat' that invokes the
|
30 |
|
|
software simulator with the proper parameters. As an example, these are the
|
31 |
|
|
contents of the 'swsim.bat' file for the 'hello' demo:
|
32 |
|
|
|
33 |
221 |
ja_rd |
\needspace{8\baselineskip}
|
34 |
210 |
ja_rd |
\begin{verbatim}
|
35 |
|
|
@rem Run software simulator in hands-off mode
|
36 |
|
|
..\..\tools\slite\slite\bin\Debug\slite.exe ^
|
37 |
|
|
--bram=hello.code ^
|
38 |
|
|
--trigger=bfc00000 ^
|
39 |
|
|
--noprompt ^
|
40 |
|
|
--nomips32 ^
|
41 |
|
|
--map=hello.map ^
|
42 |
|
|
--trace_log=trace_log.txt
|
43 |
|
|
\end{verbatim}\\
|
44 |
|
|
|
45 |
|
|
As you can see, the simulator is invoked in 'batch' or 'hands-off' mode, so
|
46 |
|
|
the simulated program will be run to completion, generating a simulation
|
47 |
|
|
log. The point of this is comparing that log to the log generated by the
|
48 |
|
|
Modelsim simulation of the same program, as has already been explained.
|
49 |
|
|
|
50 |
|
|
The python script 'bin2hdl.py' is used to insert binary data on vhdl
|
51 |
|
|
templates.
|
52 |
|
|
Assuming you have Python 2.5 or later in your machine, call the script with:
|
53 |
|
|
|
54 |
221 |
ja_rd |
\needspace{1\baselineskip}
|
55 |
210 |
ja_rd |
\begin{verbatim}
|
56 |
|
|
python bin2hdl.py --help
|
57 |
|
|
\end{verbatim}\\
|
58 |
|
|
|
59 |
|
|
to get a short description (see section~\ref{python_script}).
|
60 |
|
|
|
61 |
221 |
ja_rd |
\section{Support Code}
|
62 |
|
|
\label{support_code}
|
63 |
|
|
|
64 |
|
|
\subsection{Bootstrap Code}
|
65 |
|
|
\label{asm_bootstrap_code}
|
66 |
|
|
|
67 |
|
|
File \emph{'/src/common/bootstrap.s'} contains the reset code and the stub
|
68 |
|
|
interrupt handler.
|
69 |
|
|
|
70 |
|
|
This code is meant to be placed at the reset vector address; in the present
|
71 |
|
|
revision of the project, the bootstrap code would be placed in the
|
72 |
|
|
bootstrap BRAM of the SoC module (see section ~\ref{bootstrap_code}). It
|
73 |
|
|
can be placed in external memory if the SoC memory map and the makefiles are
|
74 |
|
|
altered accordingly.
|
75 |
|
|
|
76 |
|
|
The reset code initializes both I-Cache and D-Cache and jumps to symbol
|
77 |
|
|
\texttt{'entry'} with the CPU in kernel mode and interrupts disabled.
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
The interrupt code is somewhat more elaborate but nevertheless is still
|
81 |
|
|
a stub. The interrupt code will find out the cause of the interrupt and will
|
82 |
|
|
proceed.
|
83 |
|
|
|
84 |
|
|
Table ~\ref{tab_irq_handling} shows interrupt sources recognized in the
|
85 |
|
|
current version, and how the interrupt code deals with them.
|
86 |
|
|
|
87 |
|
|
\begin{table}[h]
|
88 |
|
|
\caption{Interrupt handling\label{tab_irq_handling}}
|
89 |
|
|
\begin{tabularx}{\textwidth}{ l|X }
|
90 |
|
|
\toprule
|
91 |
|
|
Cause & Processing \\
|
92 |
|
|
\midrule
|
93 |
|
|
SYSCALL instruction & Return immediately (STUB). \\
|
94 |
|
|
BREAK instruction & Return immediately (STUB). \\
|
95 |
|
|
Invalid opcode & Return immediately (STUB). \\
|
96 |
|
|
Unimplemented MIPS-32 opcode & Emulate opcode. \\
|
97 |
|
|
\bottomrule
|
98 |
|
|
\end{tabularx}
|
99 |
|
|
\end{table}
|
100 |
|
|
|
101 |
|
|
Please note that the interrupt code does not even \emph{know} there is
|
102 |
|
|
such a thing as a hardware interrupt -- hardware interrupts are not
|
103 |
|
|
implemented yet in the CPU.
|
104 |
|
|
|
105 |
|
|
As can be seen, most of the interrupt processing is missing, replaced by
|
106 |
|
|
stubs. Eventually, those stubs will be replaced with calls to C functions
|
107 |
|
|
that will be defined in the supporting libraries. I have yet to work out
|
108 |
|
|
exactly how to do it without reinventing the wheel.
|
109 |
|
|
|
110 |
|
|
The only interrupt processing that is performed (even if only partially) is
|
111 |
|
|
the emulation of \emph{some} MIPS-32 opcodes.
|
112 |
|
|
|
113 |
|
|
\subsection{MIPS-32 Opcode Emulation}
|
114 |
|
|
\label{mips32_opcode_emulation}
|
115 |
|
|
|
116 |
|
|
I have found out that most MIPS toolchains target the MIPS-32 architecture
|
117 |
|
|
and can only be made to work with the MIPS-I architecture with some
|
118 |
|
|
difficulty. This applies specially to the C support libraries, where
|
119 |
|
|
MIPS-32 opcodes are used occasionally.
|
120 |
|
|
Other reasons, such as the availability of MIPS-32 ports of
|
121 |
|
|
uClinux, make at least partial compatibility to MIPS-32 very desirable.
|
122 |
|
|
|
123 |
|
|
On the other hand, extending the core to implement the full MIPS-32 specs (as
|
124 |
|
|
opposed to the far simpler MIPS-I) might not be possible without running
|
125 |
|
|
into a patent minefield -- I may be wrong in this.
|
126 |
|
|
|
127 |
|
|
Therefore, I have chosen to just emulate whatever subset of the
|
128 |
|
|
MIPS-32 ISA is enough to overcome the above obstacles. I have run some
|
129 |
|
|
experiments with uClinux and have found out that only a few MIPS-32 opcodes
|
130 |
|
|
need to be emulated -- see table ~\ref{tab_emulated_opcodes}.
|
131 |
|
|
|
132 |
|
|
\begin{table}[h]
|
133 |
|
|
\caption{Emulated MIPS-32 opcodes\label{tab_emulated_opcodes}}
|
134 |
|
|
\begin{tabularx}{\textwidth}{ l|X }
|
135 |
|
|
\toprule
|
136 |
|
|
Opcode & Emulation \\
|
137 |
|
|
\midrule
|
138 |
|
|
EXT & Fully emulated. \\
|
139 |
|
|
INS & Fully emulated. \\
|
140 |
|
|
CLO & Fully emulated. \\
|
141 |
|
|
CLZ & Fully emulated. \\
|
142 |
|
|
\midrule
|
143 |
|
|
MUL (3-reg version) & To Be Done. \\
|
144 |
|
|
LWL & To Be Done. \\
|
145 |
|
|
LWR & To Be Done. \\
|
146 |
|
|
SWL & To Be Done. \\
|
147 |
|
|
SWR & To Be Done. \\
|
148 |
|
|
\bottomrule
|
149 |
|
|
\end{tabularx}
|
150 |
|
|
\end{table}
|
151 |
|
|
|
152 |
|
|
Opcode emulation is implemented in file \emph{/src/common/opcode\_emu.s}.
|
153 |
|
|
|
154 |
|
|
The emulation code has been tested (code sample \emph{'opcodes'}) but is
|
155 |
|
|
still unfinished: not only are there opcodes to be emulated, as can
|
156 |
|
|
be seen in table ~\ref{tab_emulated_opcodes}, but the emulation does NOT
|
157 |
|
|
work when the MIPS-32 opcode is in a delay slot; or more precisely, the
|
158 |
|
|
jump instruction of the delay slot is not emulated as it should -- this is
|
159 |
|
|
just work to be done, nothing specially difficult.
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
If the trapped MIPS-32 opcode is not one of the emulated opcodes, it is
|
163 |
|
|
ignored exactly as if it was a NOP. A flag is raised in a special, reserved
|
164 |
|
|
area of the BSS segment -- see the source for details. Eventually the trap
|
165 |
|
|
handling will be complete and unimplemented MIPS-32 opcodes will be dealt
|
166 |
|
|
with as undefined opcodes.
|
167 |
|
|
|
168 |
|
|
Note that opcode emulation can be disabled in the makefiles, by defining
|
169 |
|
|
symbol \texttt{NO\_EMU\_MIPS32} when assembling \emph{bootstrap.s}.
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
\subsection{C Startup Code}
|
173 |
|
|
\label{c_startup_code}
|
174 |
|
|
|
175 |
|
|
File \emph{'/src/common/c\_startup.s'} contains the C startup code used
|
176 |
|
|
in all C code samples.
|
177 |
|
|
|
178 |
|
|
This startup code does what's usual in these cases, namely:
|
179 |
|
|
|
180 |
|
|
\begin{enumerate}
|
181 |
|
|
\item Initialize the stack at the end of the bss area.
|
182 |
|
|
\item Clear the bss area.
|
183 |
|
|
\item Move the data section from FLASH to RAM (if applicable).
|
184 |
|
|
\item Call main().
|
185 |
|
|
\item Freeze in endless loop after main() returns, if it does.
|
186 |
|
|
\end{enumerate}
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
See the makefile of any of the C code samples for examples on how to
|
190 |
|
|
assemble, configure and link the startup code.
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
\subsection{C Support Library Replacement}
|
194 |
|
|
\label{libsoc}
|
195 |
|
|
|
196 |
|
|
The core will eventually be tested with one of the regular, free libc
|
197 |
|
|
replacement libraries available. It isn't yet because building those
|
198 |
|
|
libraries for a MIPS-I target (i.e. with no MIPS-32 stuff mixed in) has
|
199 |
|
|
turned out to be much more difficult than I had anticipated.
|
200 |
|
|
|
201 |
|
|
In order to be able to use the C toolchain and provide some code samples in
|
202 |
|
|
C, I have built a minimalistic libc replacement called 'libsoc'. The
|
203 |
|
|
source code can be found in \emph{/src/common/libsoc/src}.
|
204 |
|
|
|
205 |
|
|
This mini-libc provides only those C support functions I have found
|
206 |
|
|
necessary so far; it will be extended with new functionality as I add more
|
207 |
|
|
code samples, until I can finally use some real C library.
|
208 |
|
|
|
209 |
|
|
Apart from a number of utility functions that the C runtime needs, libsoc
|
210 |
|
|
provides the following:
|
211 |
|
|
|
212 |
|
|
\begin{itemize}
|
213 |
|
|
\item Floating point support (SW, float and double).
|
214 |
|
|
\item \texttt{putchar} and \texttt{getchar} using the SoC UART.
|
215 |
|
|
\item Replacement for the built-in \texttt{printf} provided by gcc.
|
216 |
|
|
\end{itemize}
|
217 |
|
|
|
218 |
|
|
That's it. Yet, even this little is enough to run the Adventure demo...
|
219 |
|
|
|
220 |
|
|
All the source code has been lifted from the original Plasma supporting code
|
221 |
|
|
or has been scavenged from the net -- see credits in the file headers.
|
222 |
|
|
|