OpenCores
URL https://opencores.org/ocsvn/lxp32/lxp32/trunk

Subversion Repositories lxp32

[/] [lxp32/] [trunk/] [doc/] [src/] [trm/] [lxp32-trm.tex] - Blame information for rev 9

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 ring0_mipt
% !TEX TS-program = lualatex
2
\documentclass[a4paper,12pt,twoside,extrafontsizes]{memoir}
3
 
4
\input{preamble.tex}
5
 
6
\begin{document}
7
 
8
\input{frontmatter.tex}
9
 
10
\mainmatter
11
 
12
\chapter{Introduction}
13
 
14
\section{Main features}
15
 
16
\lxp{} (\emph{Lightweight eXecution Pipeline}) is a small 32-bit CPU IP core optimized for FPGA implementation. Its key features include:
17
 
18
\begin{itemize}
19
        \item portability (described in behavioral VHDL-93, not tied to any particular vendor);
20
        \item 3-stage hazard-free pipeline;
21
        \item 256 registers implemented as a RAM block;
22
        \item a simple instruction set with only 30 distinct opcodes;
23
        \item separate instruction and data buses, optional instruction cache;
24
        \item WISHBONE compatibility;
25
        \item 8 interrupts with hardwired priorities;
26
        \item optional divider.
27
\end{itemize}
28
 
29
As a lightweight CPU core, \lxp{} lacks some features of more advanced processors, such as nested interrupt handling, debugging support, floating-point and memory management units. \lxp{} is based on an original ISA (Instruction Set Architecture) which does not currently have a C compiler. It can be programmed in the assembly language covered by Appendix \ref{app:assemblylanguage}.
30
 
31
Two major hardware versions of the CPU are provided: \lxp{}U which does not include an instruction cache and uses the Low Latency Interface (Section \ref{sec:lli}) to fetch instructions, and \lxp{}C which fetches instructions over a cached WISHBONE bus protocol. These versions are otherwise identical and have the same instruction set architecture.
32
 
33
\section{Implementation estimates}
34
 
35
Typical results of \lxp{} core FPGA implementation are presented in Table \ref{tab:implementation}. Note that these data are only useful as rough estimates, since actual results depend greatly on tool versions and configuration, design constraints, device utilization ratio and other factors.
36
 
37
Data on two configurations are provided:
38
 
39
\begin{itemize}
40
        \item \emph{Compact}: \lxp{}U (without instruction cache), no divider, 2-cycle multiplier.
41
        \item \emph{Full}: \lxp{}C (with instruction cache), divider, 2-cycle multiplier.
42
\end{itemize}
43
 
44
The slowest speed grade was used for clock frequency estimation.
45
 
46
\begin{table}[htbp]
47
        \caption{Typical results of \lxp{} core FPGA implementation}
48
        \label{tab:implementation}
49
        \begin{tabularx}{\textwidth}{Q{0.5\textwidth}LL}
50
                \toprule
51
                Resource & Compact & Full \\
52
                \midrule
53
                \multicolumn{3}{c}{Microsemi\textregistered{} IGLOO\textregistered{}2 M2GL005-FG484} \\
54
                \midrule
55
                Logic elements (LUT+DFF) & 1457 & 2086 \\
56
                \hspace*{1em}LUTs & 1421 & 1999 \\
57
                \hspace*{1em}Flip-flops & 706 & 1110 \\
58
                Mathblocks (MACC) & 3 & 3 \\
59
                RAM blocks (RAM1K18) & 2 & 3 \\
60
                Clock frequency & 107.7 MHz & 109.2 MHz \\
61
                \midrule
62
                \multicolumn{3}{c}{Xilinx\textregistered{} Artix\textregistered{}-7 xc7a15tfgg484-1} \\
63
                \midrule
64
                Slices & 235 & 365 \\
65
                \hspace*{1em}LUTs & 666 & 1011 \\
66
                \hspace*{1em}Flip-flops & 528 & 883 \\
67
                DSP blocks (DSP48E1) & 4 & 4 \\
68
                RAM blocks (RAMB18E1) & 2 & 3 \\
69
                Clock frequency & 111.9 MHz & 120.2 MHz \\
70
                \bottomrule
71
        \end{tabularx}
72
\end{table}
73
 
74
\section{Structure of this manual}
75
 
76
General description of the \lxp{} operation from a software developer's point of view can be found in Chapter \ref{ch:isa}, \styledtitleref{ch:isa}. Future versions of the \lxp{} CPU are intended to be at least backwards compatible with this architecture.
77
 
78
Topics related to hardware, such as synthesis, implementation and interfacing other IP cores, are covered in Chapter \ref{ch:integration}, \styledtitleref{ch:integration}. A brief description of the \lxp{} pipelined architecture is provided in Chapter \ref{ch:pipeline}, \styledtitleref{ch:pipeline}. The \lxp{} IP core package includes a verification environment (self-checking testbench) which can be used to simulate the design as described in Chapter \ref{ch:simulation}, \styledtitleref{ch:simulation}.
79
 
80
Documentation for tools shipped with the \lxp{} IP core package (assembler/linker, disassembler and interconnect generator) is provided in Chapter \ref{ch:developmenttools}, \styledtitleref{ch:developmenttools}.
81
 
82
Appendices include a detailed description of the \lxp{} instruction set, instruction cycle counts and \lxp{} assembly language definition. WISHBONE datasheet required by the WISHBONE specification is also provided.
83
 
84
\chapter{Instruction set architecture}
85
\label{ch:isa}
86
 
87
\section{Data format}
88
 
89
Most \lxp{} instructions work with 32-bit data words. A few instructions that address individual bytes use little-endian order, that is, the least significant byte is stored at the lowest address. Signed values are encoded in a 2's complement format.
90
 
91
\section{Instruction format}
92
\label{sec:instructionformat}
93
 
94
All \lxp{} instructions are encoded as 32-bit words, with the exception of \instr{lc} (\instrname{Load Constant}), which occupies two adjacent 32-bit words. Instructions in memory must be aligned to word boundaries.
95
 
96
Most arithmetic and logical instructions take two source operands and write the result to an independent destination register. General instruction format is presented on Figure \ref{fig:instructionformat}.
97
 
98
\begin{figure}[htbp]
99
        \centering
100
        \includegraphics[scale=1.2]{images/instructionformat.pdf}
101
        \caption{\lxp{} instruction format}
102
        \label{fig:instructionformat}
103
\end{figure}
104
 
105
This format includes the following fields:
106
 
107
\begin{enumerate}
108
        \item OPCODE -- a 6-bit instruction code (see Appendix \ref{app:instructionset}).
109
        \item T1 -- type of the RD1 field.
110
        \item T2 -- type of the RD2 field.
111
        \item DST -- register number (usually the destination register).
112
        \item RD1 -- register/direct operand 1.
113
        \item RD2 -- register/direct operand 2.
114
\end{enumerate}
115
 
116
Some of these fields may not have meaning for a particular instruction; such unused fields are replaced with zeros.
117
 
118
DST field specifies one of the 256 \lxp{} registers. RD1 and RD2 fields can denote either source register operands or direct (immediate) operands: if the corresponding T field is 1, RD value is a register number, otherwise it is interpreted as a direct signed byte in a 2's complement format (valid values range from -128 to 127).
119
 
120
For example, consider the following instruction that adds \code{10} to \code{r0} and writes the result to \code{r1}:
121
 
122
\begin{codepar}
123
    \instr{add} r1, r0, 10
124
\end{codepar}
125
 
126
In this example, OPCODE is \code{010000}, T1 is \code{1}, T2 is \code{0}, DST is \code{00000001}, RD1 is \code{00000000} and RD2 is \code{00001010}. Hence, the instruction is encoded as \code{0x4201000A}.
127
 
128
For convenience, some instructions have alias mnemonics. For example, \lxp{} does not have a distinct \instr{mov} opcode: instead, \code{\instr{mov} dst, src} is an alias for \code{\instr{add} dst, src, 0}.
129
 
130
A complete list of \lxp{} instructions is provided in Appendix \ref{app:instructionset}.
131
 
132
\section{Registers}
133
 
134
\lxp{} has 256 registers denoted as \code{r0} -- \code{r255}. The first 240 of them (from \code{r0} to \code{r239}) are general-purpose registers (GPR), the last 16 (from \code{r240} to \code{r255}) are special-purpose registers (SPR). For convenience, some special-purpose registers have alias names: for example, \code{r255} can be also referred to as \code{sp} (stack pointer). Special purpose registers are listed in Table \ref{tab:spr}. Some of these registers are reserved: the software should not access them.
135
 
136
\begin{table}[htbp]
137
        \caption{\lxp{} special-purpose registers}
138
        \label{tab:spr}
139
        \begin{tabularx}{\textwidth}{llL}
140
                \toprule
141
                Alias name & Generic name & Description \\
142
                \midrule
143
                \code{iv0} & \code{r240} & Interrupt vector 0 (Section \ref{sec:interrupthandling}) \\
144
                \code{iv1} & \code{r241} & Interrupt vector 1 (Section \ref{sec:interrupthandling}) \\
145
                \code{iv2} & \code{r242} & Interrupt vector 2 (Section \ref{sec:interrupthandling}) \\
146
                \code{iv3} & \code{r243} & Interrupt vector 3 (Section \ref{sec:interrupthandling}) \\
147
                \code{iv4} & \code{r244} & Interrupt vector 4 (Section \ref{sec:interrupthandling}) \\
148
                \code{iv5} & \code{r245} & Interrupt vector 5 (Section \ref{sec:interrupthandling}) \\
149
                \code{iv6} & \code{r246} & Interrupt vector 6 (Section \ref{sec:interrupthandling}) \\
150
                \code{iv7} & \code{r247} & Interrupt vector 7 (Section \ref{sec:interrupthandling}) \\
151
                \multicolumn{1}{l}{---} & \code{r248}\,--\,\code{r251} & \emph{Reserved} \\
152
                \code{cr}  & \code{r252} & Control register (Section \ref{sec:interrupthandling}) \\
153
                \code{irp} & \code{r253} & Interrupt return pointer (Section \ref{sec:interrupthandling}) \\
154
                \code{rp}  & \code{r254} & Return pointer (Section \ref{sec:callingprocedures})\\
155
                \code{sp}  & \code{r255} & Stack pointer (Section \ref{sec:stack}) \\
156
                \bottomrule
157
        \end{tabularx}
158
\end{table}
159
 
160
All registers are zero-initialized during the CPU reset.
161
 
162
\section{Addressing}
163
\label{sec:addressing}
164
 
165
All addressing in \lxp{} is indirect. In order to access a memory location, its address must be stored in a register; any available register can be used for this purpose.
166
 
167
\lxp{} uses a 32-bit address space. Each address refers to an individual byte. Some instructions, namely \instr{lsb} (\instrname{Load Signed Byte}), \instr{lub} (\instrname{Load Unsigned Byte}) and \instr{sb} (\instrname{Store Byte}) provide  byte-granular access, in which case all 32 bits in the address are significant. Otherwise the least two address bits are ignored as \lxp{} doesn't support unaligned access to 32-bit data words (during simulation, a warning is emitted if such a transaction is attempted).
168
 
169
A special rule applies to pointers that refer to instructions: since instructions are always word-aligned, the least significant bit is interpreted as the \code{IRF} (\emph{Interrupt Return Flag}). See Section \ref{sec:interrupthandling} for details.
170
 
171
\section{Stack}
172
\label{sec:stack}
173
 
174
The current pointer to the top of the stack is stored in the \code{sp} register. To the hardware this register is not different from general purpose registers, that is, in no situation does the CPU access the stack implicitly (procedure calls and interrupts use register-based conventions).
175
 
176
Software can access the stack as follows:
177
 
178
\begin{codepar}
179
    \emph{// push r0 on the stack}
180
    \instr{sub} sp, sp, 4
181
    \instr{sw} sp, r0
182
    \emph{// pop r0 from the stack}
183
    \instr{lw} r0, sp
184
    \instr{add} sp, sp, 4
185
\end{codepar}
186
 
187
Before using the stack, the \code{sp} register must be set up to point to a valid memory location. The simplest software can operate stackless, or even without data memory altogether if registers are enough to store the program state.
188
 
189
\section{Calling procedures}
190
\label{sec:callingprocedures}
191
 
192
\lxp{} provides a \instr{call} instruction which saves the address of the next instruction in the \code{rp} register and transfers execution to the address stored in the register operand. Return from a procedure is performed by the \code{\instr{jmp} rp} instruction which also has a \instr{ret} alias.
193
 
194
If a procedure must in turn call a nested procedure itself, the return address in the \code{rp} register will be overwritten by the \instr{call} instruction. Hence, unless it is a tail call (see below), the procedure must save the \code{rp} value somewhere; the most general solution is to use the stack:
195
 
196
\begin{codepar}
197
    \instr{sub} sp, sp, 4
198
    \instr{sw} sp, rp
199
    ...
200
    \instr{lc} r0, Nested_proc
201
    \instr{call} r0
202
    ...
203
    \instr{lw} rp, sp
204
    \instr{add} sp, sp, 4
205
    \instr{ret}
206
\end{codepar}
207
 
208
Procedures that don't use the \instr{call} instruction (sometimes called \emph{leaf procedures}) don't need to save the \code{rp} value.
209
 
210
Since \instr{ret} is just an alias for \code{\instr{jmp} rp}, one can also use \instrname{Compare and Jump} instructions (\instr{cjmp\emph{xxx}}) to perform a conditional procedure return. For example, consider the following procedure which calculates the absolute value of \code{r1}:
211
 
212
\begin{codepar}
213
Abs_proc:
214
    \instr{cjmpsge} rp, r1, 0 \emph{// return immediately if r1>=0}
215
    \instr{neg} r1, r1 \emph{// otherwise, negate r1}
216
    \instr{ret} \emph{// jmp rp}
217
\end{codepar}
218
 
219
A \emph{tail call} is a special type of procedure call where the calling procedure calls a nested procedure as the last action before return. In such cases the \instr{call} instruction can be replaced with \instr{jmp}, so that when the nested procedure executes \instr{ret}, it returns directly to the caller's parent procedure.
220
 
221
Although the \lxp{} architecture doesn't mandate any particular calling convention, some general recommendations are presented below:
222
 
223
\begin{enumerate}
224
        \item Pass arguments and return values through the \code{r1}--\code{r31} registers (a procedure can have multiple return values).
225
        \item If necessary, the \code{r0} register can be used to load the procedure address.
226
        \item Designate \code{r0}--\code{r31} registers as \emph{caller-saved}, that is, they are not guaranteed to be preserved during procedure calls and must be saved by the caller if needed. The procedure can use them for any purpose, regardless of whether they are used to pass arguments and/or return values.
227
\end{enumerate}
228
 
229
\section{Interrupt handling}
230
\label{sec:interrupthandling}
231
 
232
\subsection{Control register}
233
 
234
\lxp{} supports 8 interrupts with hardwired priority levels (interrupts with lower vector numbers have higher priority). Interrupts vectors (pointers to interrupt handlers) are stored in the \code{iv0}--\code{iv7} registers. Interrupt handling is controlled by the \code{cr} register (Table \ref{tab:cr}).
235
 
236
\begin{table}[htbp]
237
        \caption{Control register}
238
        \label{tab:cr}
239
        \begin{tabularx}{\textwidth}{lL}
240
                \toprule
241
                Bit & Description \\
242
                \midrule
243
 
244
                1      & Enable interrupt 1 \\
245
                & \ldots \\
246
                7      & Enable interrupt 7 \\
247
                8      & Temporarily block interrupt 0 \\
248
                9      & Temporarily block interrupt 1 \\
249
                & \ldots \\
250
                15     & Temporarily block interrupt 7 \\
251
                31--16 & \emph{Reserved} \\
252
                \bottomrule
253
        \end{tabularx}
254
\end{table}
255
 
256
Disabled interrupts are ignored altogether: if the CPU receives an interrupt request signal while the corresponding interrupt is disabled, the interrupt handler will not be called even if the interrupt is enabled later. Conversely, temporarily blocked interrupts are still registered, but their handlers are not called until they are unblocked.
257
 
258
Like other registers, \code{cr} is zero-initialized during the CPU reset, meaning that no interrupts are initially enabled.
259
 
260
\subsection{Invoking interrupt handlers}
261
 
262
Interrupt handlers are invoked by the CPU similarly to procedures (Section \ref{sec:callingprocedures}), the difference being that in this case return address is stored in the \code{irp} register (as opposed to \code{rp}), and the least significant bit of the register (\code{IRF} -- \emph{Interrupt Return Flag}) is set.
263
 
264
An interrupt handler returns using the \code{\instr{jmp} irp} instruction which also has an \instr{iret} alias. Until the interrupt handler returns, the CPU will defer further interrupt processing (although incoming interrupt requests will still be registered). This also means that the \code{irp} register value will not be unexpectedly overwritten. When executing the \code{\instr{jmp} irp} instruction, the CPU will recognize the \code{IRF} flag and resume interrupt processing as usual. It is also possible to perform a conditional return from the interrupt handler, similarly to the technique described in Section \ref{sec:callingprocedures} for conditional procedure returns.
265
 
266
\subsection{Non-returnable interrupts}
267
 
268
If an interrupt vector has the least significant bit (\code{IRF}) set, the CPU will resume interrupt processing immediately. One should not try to invoke \instr{iret} from such a handler since the \code{irp} register could have been overwritten by another interrupt. This technique can be useful when the CPU's only task is to process external events:
269
 
270
\begin{codeparbreakable}
271
\emph{// Set the IRF to mark the interrupt as non-returnable}
272
    \instr{lc} iv0, main\_loop@1
273
    \instr{mov} cr, 1 \emph{// enable the interrupt}
274
    \instr{hlt} \emph{// wait for an interrupt request}
275
main\_loop:
276
\emph{// Process the event...}
277
    \instr{hlt} \emph{// wait for the next interrupt request}
278
\end{codeparbreakable}
279
 
280
Note that \instr{iret} is never called in this example.
281
 
282
\chapter{Integration}
283
\label{ch:integration}
284
 
285
\section{Overview}
286
 
287
The \lxp{} IP core is delivered in a form of a synthesizable RTL description expressed in \mbox{VHDL-93}. It does not use any technology specific primitives and should work out of the box with major FPGA synthesis software. \lxp{} can be integrated in both VHDL and Verilog\textregistered{} based SoC designs.
288
 
289
Major \lxp{} hardware versions have separate top-level design units:
290
 
291
\begin{itemize}
292
        \item \shellcmd{lxp32u\_top} -- \lxp{}U (without instruction cache),
293
        \item \shellcmd{lxp32c\_top} -- \lxp{}C (with instruction cache).
294
\end{itemize}
295
 
296
A high level block diagram of the CPU is presented on Figure \ref{fig:blockdiagram}. Schematic symbols for \lxp{}U and \lxp{}C are shown on Figure \ref{fig:symbols}.
297
 
298
\begin{figure}[htbp]
299
        \centering
300
        \includegraphics[scale=0.85]{images/blockdiagram.pdf}
301
        \caption{\lxp{} CPU block diagram}
302
        \label{fig:blockdiagram}
303
\end{figure}
304
 
305
\begin{figure}[htbp]
306
        \centering
307
        \includegraphics[scale=0.85]{images/symbols.pdf}
308
        \caption{Schematic symbols for \lxp{}U and \lxp{}C}
309
        \label{fig:symbols}
310
\end{figure}
311
 
312
\lxp{}U uses the Low Latency Interface (LLI) described in Section \ref{sec:lli} to fetch instructions. This interface is designed to interact with low latency on-chip peripherals such as RAM blocks. It works best with slaves that can return the instruction on the next cycle after its address has been set, although the slave can still introduce wait states if needed. Low Latency Interface can be also connected to a custom (external) instruction cache.
313
 
314
To achieve the least possible latency, some LLI outputs are not registered. For this reason the LLI is not suitable for interaction with off-chip peripherals.
315
 
316
\lxp{}C is designed to work with high latency memory controllers and uses a simple instruction cache based on a ring buffer. The instructions are fetched over the WISHBONE instruction bus. To maximize throughput, the CPU makes use of the WISHBONE registered feedback signals [CTI\_O()] and [BTE\_O()]. All outputs on this bus are registered. This version is also recommended for use in situations where LLI combinatorial delays are unacceptable.
317
 
318
Both \lxp{}U and \lxp{}C use the WISHBONE protocol for the data bus.
319
 
320
\section{Ports}
321
 
322
\begin{ctabular}{lccl}
323
        \toprule
324
        Port & Direction & Bus width & Description \\
325
        \midrule
326
        \tabcutin{4}{Global signals} \\
327
        \midrule
328
        \signal{clk\_i} & in & 1 & System clock \\
329
        \signal{rst\_i} & in & 1 & Synchronous reset, active high \\
330
        \midrule
331
        \tabcutin{4}{Instruction bus -- Low Latency Interface (\lxp{}U only)} \\
332
        \midrule
333
        \signal{lli\_re\_o} & out & 1 & Read enable output, active high \\
334
        \signal{lli\_adr\_o} & out & 30 & Address output \\
335
        \signal{lli\_dat\_i} & in & 32 & Data input \\
336
        \signal{lli\_busy\_i} & in & 1 & Busy flag input, active high \\
337
        \midrule
338
        \tabcutin{4}{Instruction bus -- WISHBONE (\lxp{}C only)} \\
339
        \midrule
340
        \signal{ibus\_cyc\_o} & out & 1 & Cycle output \\
341
        \signal{ibus\_stb\_o} & out & 1 & Strobe output \\
342
        \signal{ibus\_cti\_o} & out & 3 & Cycle type identifier \\
343
        \signal{ibus\_bte\_o} & out & 2 & Burst type extension \\
344
        \signal{ibus\_ack\_i} & in & 1 & Acknowledge input \\
345
        \signal{ibus\_adr\_o} & out & 30 & Address output \\
346
        \signal{ibus\_dat\_i} & in & 32 & Data input \\
347
        \midrule
348
        \tabcutin{4}{Data bus} \\
349
        \midrule
350
        \signal{dbus\_cyc\_o} & out & 1 & Cycle output \\
351
        \signal{dbus\_stb\_o} & out & 1 & Strobe output \\
352
        \signal{dbus\_we\_o} & out & 1 & Write enable output \\
353
        \signal{dbus\_sel\_o} & out & 4 & Select output \\
354
        \signal{dbus\_ack\_i} & in & 1 & Acknowledge input \\
355
        \signal{dbus\_adr\_o} & out & 30 & Address output \\
356
        \signal{dbus\_dat\_o} & out & 32 & Data output \\
357
        \signal{dbus\_dat\_i} & in & 32 & Data input \\
358
        \midrule
359
        \tabcutin{4}{Other ports} \\
360
        \midrule
361
        \signal{irq\_i} & in & 8 & Interrupt requests \\
362
        \bottomrule
363
\end{ctabular}
364
 
365
\section{Generics}
366
\label{sec:generics}
367
 
368
The following generics can be used to configure the \lxp{} IP core parameters.
369
 
370
\subsection{DBUS\_RMW}
371
 
372
By default, \lxp{} uses the \signal{dbus\_sel\_o} (byte enable) port to perform byte-granular write transactions initiated by the \instr{sb} (\instrname{Store Byte}) instruction. If this option is set to \code{true}, \signal{dbus\_sel\_o} is always tied to \code{"1111"}, and byte-granular write access is performed using the RMW (read-modify-write) cycle. The latter method is slower, but can work with slaves that do not have the [SEL\_I()] port.
373
 
374
This feature is designed with the assumption that read and write transactions do not cause side effects, thus it can be unsuitable for some slaves.
375
 
376
\subsection{DIVIDER\_EN}
377
 
378
\lxp{} includes a divider unit which has quite a low performance but occupies a considerable amount of resources. It can be disabled by setting this option to \code{false}.
379
 
380
\subsection{IBUS\_BURST\_SIZE}
381
 
382
Instruction bus burst size. Default value is 16. Only for \lxp{}C.
383
 
384
\subsection{IBUS\_PREFETCH\_SIZE}
385
 
386
Number of words that the instruction cache will read ahead from the current instruction pointer. Default value is 32. Only for \lxp{}C.
387
 
388
\subsection{MUL\_ARCH}
389
 
390
\lxp{} provides three multiplier options:
391
 
392
\begin{itemize}
393
        \item \code{"dsp"} is the fastest architecture designed for technologies that provide fast parallel $16 \times 16$ multipliers, which includes most modern FPGA families. One multiplication takes 2 clock cycles.
394
        \item \code{"opt"} architecture uses a semi-parallel multiplication algorithm based on carry-save accumulation of partial products. It is designed for technologies that do not provide fast $16 \times 16$ multipliers. One multiplication takes 6 clock cycles.
395
        \item \code{"seq"} is a fully sequential design. One multiplication takes 34 clock cycles.
396
\end{itemize}
397
 
398
The default multiplier architecture is \code{"dsp"}. This option is recommended for most modern FPGA devices regardless of optimization goal since it is not only the fastest, but also occupies the least amount of general-purpose logic resources. However, it will create a timing bottleneck on technologies that lack fast multipliers.
399
 
400
For older FPGA families that don't provide dedicated multipliers the \code{"opt"} architecture can be used if decent throughput is still needed. It is designed to avoid creating a timing bottleneck on such technologies. Alternatively, \code{"seq"} architecture can be used when throughput is not a concern.
401
 
402
\subsection{START\_ADDR}
403
 
404
Address of the first instruction to be executed after CPU reset. Default value is \code{0}. The two least significant bits are ignored as instructions are always word-aligned.
405
 
406
\section{Clock and reset}
407
\label{sec:clockreset}
408
 
409
All flip-flops in the CPU are triggered by a rising edge of the \signal{clk\_i} signal. No specific requirements are imposed on the \signal{clk\_i} signal apart from usual constraints on setup and hold times.
410
 
411
\lxp{} is reset synchronously when the \signal{rst\_i} signal is asserted. If the system reset signal comes from an asynchronous source, a synchronization circuit must be used; an example of such a circuit is shown on Figure \ref{fig:resetsync}.
412
 
413
\begin{figure}[htbp]
414
        \centering
415
        \includegraphics[scale=1]{images/resetsync.pdf}
416
        \caption{Reset synchronization circuit}
417
        \label{fig:resetsync}
418
\end{figure}
419
 
420
In SRAM-based FPGAs flip-flops and RAM blocks have deterministic state after a bitstream is loaded. On such technologies \lxp{} can operate without reset. In this case the \signal{rst\_i} port can be tied to a logical \code{0} in the RTL design to allow the synthesizer to remove redundant logic.
421
 
422
\signal{clk\_i} and \signal{rst\_i} signals also serve the role of [CLK\_I] and [RST\_I] WISHBONE signals, respectively, for both instruction and data buses.
423
 
424
\section{Low Latency Interface}
425
\label{sec:lli}
426
 
427
Low Latency Interface (LLI) is a simple pipelined synchronous protocol with a typical latency of 1 cycle used by \lxp{}U to fetch instructions. It was designed to allow simple connection of the CPU to on-chip program RAM or cache. The timing diagram of the LLI is shown on Figure \ref{fig:llitiming}.
428
 
429
\begin{figure}[htbp]
430
        \centering
431
        \includegraphics[scale=1]{images/llitiming.pdf}
432
        \caption{Low Latency Interface timing diagram (\lxp{}U)}
433
        \label{fig:llitiming}
434
\end{figure}
435
 
436
To request a word, the master produces its address on \signal{lli\_adr\_o} and asserts \signal{lli\_re\_o}. The request is considered valid when \signal{lli\_re\_o} is high and \signal{lli\_busy\_i} is low on the same clock cycle. On the next cycle after a valid request, the slave must either produce data on \signal{lli\_dat\_i} or assert \signal{lli\_busy\_i} to indicate that data are not ready. \signal{lli\_busy\_i} must be held high until the valid data are present on the \signal{lli\_dat\_i} port.
437
 
438
The data provided by the slave are only required to be valid on the next cycle after a valid request (if \signal{lli\_busy\_i} is not asserted) or on the cycle when \signal{lli\_busy\_i} is deasserted after being held high. Otherwise \signal{lli\_dat\_i} is undefined.
439
 
440
The values of \signal{lli\_re\_o} and \signal{lli\_adr\_o} are not guaranteed to be preserved by the master while the slave is busy.
441
 
442
The simplest slaves such as on-chip RAM blocks which are never busy can be trivially connected to the LLI by connecting address, data and read enable ports and tying the \signal{lli\_busy\_i} signal to a logical \code{0} (you can even ignore \signal{lli\_re\_o} in this case, although doing so can theoretically increase power consumption).
443
 
444
Since the \signal{lli\_re\_o} output signal is not registered, this interface is not suitable for interaction with off-chip peripherals. Also, care should be taken to avoid introducing too much additional combinatorial delay on its outputs.
445
 
446
The instruction bus, whether LLI or WISHBONE, doesn't support access to individual bytes and uses a 30-bit address port to address 32-bit words (instructions are always word-aligned). The lower two bits of the 32-bit address are ignored for the purpose of addressing. Consider the following example:
447
 
448
\begin{codeparbreakable}
449
    \instr{lc} r0, 0x10000000
450
    \instr{jmp} r0
451
\emph{// 0x04000000 will appear on lli_adr_o or ibus_adr_o}
452
\end{codeparbreakable}
453
 
454
\section{WISHBONE instruction bus}
455
 
456
The \lxp{}C CPU fetches instructions over the WISHBONE bus. Its parameters are defined in the WISHBONE datasheet (Appendix \ref{app:wishbonedatasheet}). For a detailed description of the bus protocol refer to the WISHBONE specification, revision B3.
457
 
458
With classic WISHBONE handshake decent throughput can be only achieved when the slave is able to terminate cycles asynchronously. It is usually possible only for the simplest slaves which should probably be using the Low Latency Interface instead. To maximize throughput for complex, high latency slaves, \lxp{}C instruction bus uses optional WISHBONE address tags [CTI\_O()] (Cycle Type Identifier) and [BTE\_O()] (Burst Type Extension). These signals are hints allowing the slave to predict the address that will be set by the master in the next cycle and prepare data in advance. The slave can ignore these hints, processing requests as classic WISHBONE cycles, although performance would almost certainly suffer in this case.
459
 
460
A typical \lxp{}C instruction bus burst timing diagram is shown on Figure \ref{fig:ibustiming}.
461
 
462
\begin{figure}[htbp]
463
        \centering
464
        \includegraphics[scale=0.786]{images/ibustiming.pdf}
465
        \caption{Typical WISHBONE instruction bus burst (\lxp{}C)}
466
        \label{fig:ibustiming}
467
\end{figure}
468
 
469
\section{WISHBONE data bus}
470
 
471
\lxp{} uses the WISHBONE bus to interact with data memory and other peripherals. This bus is distinct from the instruction bus; its parameters are defined in the WISHBONE datasheet (Appendix \ref{app:wishbonedatasheet}).
472
 
473
The data bus uses a 30-bit \signal{dbus\_adr\_o} port to address 32-bit words; the \signal{dbus\_sel\_o} port is used to select individual bytes to be written or read. The upper 30 bits of the address appear on the \signal{dbus\_adr\_o} port, while the lower two bits are decoded to create a 4-bit \signal{dbus\_sel\_o} signal. Consider:
474
 
475
\begin{codeparbreakable}
476
    \instr{lc} r0, 0x20000002
477
    \instr{sb} r0, 0x55
478
\emph{// write 0x55 to the address in r0}
479
\emph{// 0x08000000 will appear on dbus_adr_o}
480
\emph{// 0x4 will appear on dbus_sel_o}
481
\end{codeparbreakable}
482
 
483
The byte-granular access feature is optional. If it is not needed, the \signal{dbus\_sel\_o} port can be left unconnected. It is also possible to set the \code{DBUS\_RMW} generic to \code{true} to enable byte-granular access emulation using the read-modify-write (RMW) cycle, which works even if the interconnect or slave doesn't provide the [SEL\_I()] port (Section \ref{sec:generics}).
484
 
485
For a detailed description of the bus protocol refer to the WISHBONE specification, revision B3.
486
 
487
Typical timing diagrams for write and read cycles are shown on Figure \ref{fig:dbustiming}. In these examples the peripheral terminates the cycle asynchronously; however, it can also introduce wait states by delaying the \signal{dbus\_ack\_i} signal.
488
 
489
\begin{figure}[htbp]
490
        \centering
491
        \includegraphics[scale=0.928]{images/dbustiming.pdf}
492
        \caption{Typical WISHBONE data bus WRITE and READ cycles}
493
        \label{fig:dbustiming}
494
\end{figure}
495
 
496
\section{Interrupts}
497
 
498
\lxp{} registers an interrupt condition when the corresponding request signal goes from \code{0} to \code{1}. Transitions from \code{1} to \code{0} are ignored. All interrupt request signals must be synchronous with the system clock (\signal{clk\_i}); if coming from an asynchronous source, they must be synchronized using a sequence of at least two flip-flops clocked by \signal{clk\_i}. These flip-flops are not included in the \lxp{} core in order not to increase interrupt processing delay for interrupt sources that are inherently synchronous. Failure to properly synchronize interrupt request signals will cause timing violations that will manifest itself as intermittent, hard to debug faults.
499
 
500
\section{Synthesis and optimization}
501
\label{sec:synthesis}
502
 
503
\subsection{Technology specific primitives}
504
 
505
\lxp{} RTL design is described in behavioral VHDL. However, it can also benefit from certain special resources provided by most FPGA devices, namely, RAM blocks and dedicated multipliers. For improved portability, hardware description that can potentially be mapped to such resources is localized in separate design units:
506
 
507
\begin{itemize}
508
        \item \shellcmd{lxp32\_ram256x32} -- a dual-port synchronous $256 \times 32$ bit RAM with one write port and one read port;
509
        \item \shellcmd{lxp32\_mul16x16} -- an unsigned $16 \times 16$ multiplier with an output register.
510
\end{itemize}
511
 
512
These design units contain behavioral description of respective hardware that is recognizable by FPGA synthesis tools. Usually no adjustments are needed as the synthesizer will automatically infer an appropriate primitive from its behavioral description. If automatic inference produces unsatisfactory results, these design units can be replaced with library element wrappers. The same is true for ASIC logic synthesis software which is unlikely to infer complex primitives.
513
 
514
\lxp{} implements its own bypass logic dealing with situations when RAM read and write addresses collide. It does not depend on the read/write conflict resolution behavior of the underlying primitive.
515
 
516
\subsection{General optimization guidelines}
517
 
518
This subsection contains general advice on achieving satisfactory synthesis results regardless of the optimization goal. Some of these suggestions are also mentioned in other parts of this manual.
519
 
520
\begin{enumerate}
521
        \item If the technology doesn't provide dedicated multiplier resources, consider using \code{"opt"} or \code{"seq"} multiplier architecture (Section \ref{sec:generics}).
522
 
523
        \item Ensure that the instruction bus has adequate throughput. For \lxp{}C, check that the slave supports the WISHBONE registered feedback signals [CTI\_I()] and [BTE\_I()].
524
 
525
        \item Multiplexing instruction and data buses, or connecting them to the same interconnect that allows only one master at a time to be active (i.e. \emph{shared bus} interconnect topology) is not recommended. If you absolutely must do so, assign a higher priority level to the data bus, otherwise instruction prefetches will massively slow down data transactions.
526
 
527
        \item For small programs, consider mapping code and data memory to the beginning or end of the address space (i.e. \code{0x00000000}--\code{0x000FFFFF} or \code{0xFFF00000}--\code{0xFFFFFFFF}) to be able to load pointers with the \instr{lcs} instruction which saves both memory and CPU cycles as compared to \instr{lc}.
528
\end{enumerate}
529
 
530
\subsection{Optimizing for timing}
531
 
532
\begin{enumerate}
533
        \item Set up reasonable timing constraints. Do not overconstrain the design by more than 10--15~\%.
534
 
535
        \item Analyze the worst path. The natural \lxp{} timing bottleneck usually goes from the scratchpad (register file) output through the ALU (in the Execute stage) to the scratchpad input. If timing analysis lists other critical paths, the problem can lie elsewhere. If the \signal{rst\_i} signal becomes a bottleneck, promote it to a global network or, with SRAM-based FPGAs, consider operating without reset (see Section \ref{sec:clockreset}). Critical paths affecting the WISHBONE state machines could indicate problems with interconnect performance.
536
 
537
        \item Configure the synthesis tool to reduce the fanout limit. Note that setting this limit to a too small value can lead to an opposite effect.
538
 
539
        \item Synthesis tools can support additional options to improve timing, such as the \emph{Retiming} algorithm which rearranges registers and combinatorial logic across the pipeline in attempt to balance delays. The efficiency of such algorithms is not very predictable. In general, sloppy designs are the most likely to benefit from it, while for a carefully designed circuit timing can sometimes get worse.
540
\end{enumerate}
541
 
542
\subsection{Optimizing for area}
543
 
544
\begin{enumerate}
545
        \item Consider disabling the divider if not using it (see Section \ref{sec:generics}).
546
 
547
        \item Relaxing timing constraints can sometimes allow the synthesizer to produce a more area-efficient circuit.
548
 
549
        \item Increase the fanout limit in the synthesizer settings to reduce buffer replication.
550
\end{enumerate}
551
 
552
\chapter{Hardware architecture}
553
\label{ch:pipeline}
554
 
555
The \lxp{} CPU is based on a 3-stage hazard-free pipelined architecture and uses a large RAM-based register file (scratchpad) with two read ports and one write port. The pipeline includes the following stages:
556
 
557
\begin{itemize}
558
        \item\emph{Fetch} -- fetches instructions from the program memory.
559
        \item\emph{Decode} -- decodes instructions and reads register operand values from the scratchpad.
560
        \item\emph{Execute} -- executes instructions and writes the results (if any) to the scratchpad.
561
\end{itemize}
562
 
563
\lxp{} instructions are encoded in such a way that operand register numbers can be known without decoding the instruction (Section \ref{sec:instructionformat}). When the \emph{Fetch} stage produces an instruction, scratchpad input addresses are set immediately, before the instruction itself is decoded. If the instruction does not use one or both of the register operands, the corresponding data read from the scratchpad are discarded. Collision bypass logic in the scratchpad detects situations where the \emph{Decode} stage tries to read a register which is currently being written by the \emph{Execute} stage and forwards its value, bypassing the RAM block and avoiding Read After Write (RAW) pipeline hazards. Other types of data hazards are also impossible with this architecture.
564
 
565
As an example, consider the following simple code chunk:
566
 
567
\begin{codepar}
568
    \instr{mov} r0, 10 \emph{// alias for add r0, 10, 0}
569
    \instr{mov} r1, 20 \emph{// alias for add r1, 20, 0}
570
    \instr{add} r2, r0, r1
571
\end{codepar}
572
 
573
Table \ref{tab:examplepipeline} illustrates how this chunk is processed by the \lxp{} pipeline. Note that on the fourth cycle the \emph{Decode} stage requests the \code{r1} register value while the \emph{Execute} stage writes to the same register. Collision bypass logic in the scratchpad ensures that the \emph{Decode} stage reads the correct (new) value of \code{r1} without stalling the pipeline.
574
 
575
\begin{table}[htbp]
576
        \caption{Example of the \lxp{} pipeline operation}
577
        \small
578
        \label{tab:examplepipeline}
579
        \begin{tabularx}{\textwidth}{lllL}
580
                \toprule
581
                Cycle & Fetch & Decode & Execute \\
582
                \midrule
583
                1 & \code{\instr{add} r0, 10, 0} & & \\
584
                \midrule
585
                2 & \code{\instr{add} r1, 20, 0} & \code{\instr{add} r0, 10, 0} & \\
586
                  & & Request \code{r10} (discarded) & \\
587
                  & & Request \code{r0} (discarded) & \\
588
                  & & Pass 10 and 0 as operands & \\
589
                \midrule
590
                3 & \code{\instr{add} r2, r0, r1} & \code{\instr{add} r1, 20, 0} & Perform the addition \\
591
                  & & Request \code{r20} (discarded) & Write 10 to \code{r0} \\
592
                  & & Request \code{r0} (discarded) & \\
593
                  & & Pass 20 and 0 as operands & \\
594
                \midrule
595
                4 & & \code{\instr{add} r2, r0, r1} & Perform the addition \\
596
                  & & Request \code{r0} & Write 20 to \code{r1} \\
597
                  & & Request \code{r1} (bypass) & \\
598
                  & & Pass 10 and 20 as operands & \\
599
                \midrule
600
                5 & & & Perform the addition \\
601
                  & & & Write 30 to \code{r2} \\
602
                \bottomrule
603
        \end{tabularx}
604
\end{table}
605
 
606
When an instruction takes more than one cycle to execute, the \emph{Execute} stage simply stalls the pipeline.
607
 
608
Branch hazards are impossible in \lxp{} as well since the pipeline is flushed whenever an execution transfer occurs.
609
 
610
\chapter{Simulation}
611
\label{ch:simulation}
612
 
613
\lxp{} package includes an automated verification environment (self-checking testbench) which verifies the \lxp{} CPU functional correctness. The environment consists of two major parts: a test platform which is a SoC-like design providing peripherals for the CPU to interact with, and the testbench itself which loads test firmware and monitors the platform's output signals. Like the CPU itself, the test environment is written in VHDL-93.
614
 
615
A separate testbench for the instruction cache (\shellcmd{lxp32\_icache}) is also provided. It can be invoked similarly to the main CPU testbench.
616
 
617
\section{Requirements}
618
 
619
The following software is required to simulate the \lxp{} design:
620
 
621
\begin{itemize}
622
        \item An HDL simulator supporting VHDL-93. \lxp{} package includes scripts (makefiles) for the following simulators:
623
 
624
        \begin{itemize}
625
                \item GHDL -- a free and open-source VHDL simulator which supports multiple operating systems\footnote{\url{http://ghdl.free.fr/}};
626
                \item Mentor Graphics\textregistered{} ModelSim\textregistered{} simulator (\shellcmd{vsim});
627
                \item Xilinx\textregistered{} Vivado\textregistered{} Simulator (\shellcmd{xsim}).
628
        \end{itemize}
629
 
630
        With GHDL, a waveform viewer such as GTKWave is also recommended (Figure \ref{fig:gtkwave})\footnote{\url{http://gtkwave.sourceforge.net/}}.
631
 
632
        Some FPGA vendors provide limited versions of the ModelSim\textregistered{} simulator for free as parts of their design suites. These versions should suffice for \lxp{} simulation.
633
 
634
        Other simulators can be used with some preparations (Section \ref{sec:simmanual}).
635
 
636
        \item GNU \shellcmd{make} and \shellcmd{coreutils} are needed to simulate the design using the provided makefiles. Under Microsoft\textregistered{} Windows\textregistered{}, MSYS or Cygwin can be used.
637
        \item \lxp{} assembler/linker program (\shellcmd{lxp32asm}) must be present (Section \ref{sec:lxp32asm}). A prebuilt executable for Microsoft\textregistered{} Windows\textregistered{} is already included in the \lxp{} package, for other operating systems \shellcmd{lxp32asm} must be built from source (Section \ref{sec:buildfromsource}).
638
\end{itemize}
639
 
640
\begin{figure}[htbp]
641
        \centering
642
        \includegraphics[scale=0.65]{images/gtkwave.png}
643
        \caption{GTKWave displaying the \lxp{} waveform dump produced by GHDL}
644
        \label{fig:gtkwave}
645
\end{figure}
646
 
647
\section{Running simulation using makefiles}
648
 
649
To simulate the design, go to the \shellcmd{verify/lxp32/run/<\emph{simulator}>} directory and run \shellcmd{make}. The following make targets are supported:
650
 
651
\begin{itemize}
652
        \item \shellcmd{batch} -- simulate the design in batch mode. Results will be written to the standard output. This is the default target.
653
        \item \shellcmd{gui} -- simulate the design in GUI mode. Note: since GHDL doesn't have a GUI, the simulation itself will be run in batch mode; upon a completion, GTKWave will be run automatically to display the dumped waveforms.
654
        \item \shellcmd{compile} -- compile only, don't run simulation.
655
        \item \shellcmd{clean} -- delete all the produced artifacts.
656
\end{itemize}
657
 
658
\section{Running simulation manually}
659
\label{sec:simmanual}
660
 
661
\lxp{} testbench can be also run manually. The following steps must be performed:
662
 
663
\begin{enumerate}
664
        \item Compile the test firmware in the \shellcmd{verify/lxp32/src/firmware} directory:
665
 
666
        \begin{codepar}
667
    lxp32asm -f textio \emph{filename}.asm -o \emph{filename}.ram
668
        \end{codepar}
669
 
670
        Produced \shellcmd{*.ram} files must be placed to the simulator's working directory.
671
        \item Compile the \lxp{} RTL description (\shellcmd{rtl} directory).
672
        \item Compile the common package (\shellcmd{verify/common\_pkg}).
673
        \item Compile the test platform (\shellcmd{verify/lxp32/src/platform} directory).
674
        \item Compile the testbench itself (\shellcmd{verify/lxp32/src/tb} directory).
675
        \item Simulate the \shellcmd{tb} design unit defined in the \shellcmd{tb.vhd} file.
676
\end{enumerate}
677
 
678
\section{Testbench parameters}
679
 
680
Simulation parameters can be configured by overriding generics defined by the \shellcmd{tb} design unit:
681
 
682
\begin{itemize}
683
        \item \code{CPU\_DBUS\_RMW} -- \code{DBUS\_RMW} CPU generic value (see Section \ref{sec:generics}).
684
        \item \code{CPU\_MUL\_ARCH} -- \code{MUL\_ARCH} CPU generic value (see Section \ref{sec:generics}).
685
        \item \code{MODEL\_LXP32C} -- simulate the \lxp{}C version. By default, this option is set to \code{true}. If set to \code{false}, \lxp{}U is simulated instead.
686
        \item \code{TEST\_CASE} -- if set to a non-empty string, specifies the file name of a test case to run. If set to an empty string (default), all tests are executed.
687
        \item \code{THROTTLE\_DBUS} -- perform pseudo-random data bus throttling. By default, this option is set to \code{true}.
688
        \item \code{THROTTLE\_IBUS} -- perform pseudo-random instruction bus throttling. By default, this option is set to \code{true}.
689
        \item \code{VERBOSE} -- print more messages.
690
\end{itemize}
691
 
692
\chapter{Development tools}
693
\label{ch:developmenttools}
694
 
695
\section{\shellcmd{lxp32asm} -- Assembler and linker}
696
\label{sec:lxp32asm}
697
 
698
\shellcmd{lxp32asm} is a combined assembler and linker for the \lxp{} platform. It takes one or more input files and produces executable code for the CPU. Input files can be either source files in the \lxp{} assembly language (Appendix \ref{app:assemblylanguage}) or \emph{linkable objects}. Linkable object is a relocatable format for storing compiled \lxp{} code together with symbol information.
699
 
700
\shellcmd{lxp32asm} operates in two stages:
701
 
702
\begin{enumerate}
703
        \item Compile.
704
 
705
        Source files are compiled to linkable objects.
706
 
707
        \item Link.
708
 
709
        Linkable objects are combined into a single executable module. References to symbols defined in external modules are resolved at this stage.
710
\end{enumerate}
711
 
712
In the simplest case there is only one input source file which doesn't contain external symbol references. If there are multiple input files, one of them must define the \code{entry} (or \code{Entry}) symbol at the beginning of the code.
713
 
714
\subsection{Command line syntax}
715
\label{subsec:assemblercmdline}
716
 
717
\begin{codepar}
718
    lxp32asm [ \emph{options} | \emph{input files} ]
719
\end{codepar}
720
 
721
\subsubsection{General options}
722
 
723
\begin{itemize}
724
        \item \shellcmd{-c} -- compile only (skip the Link stage).
725
 
726
        \item \shellcmd{-h}, \shellcmd{--help} -- display a short help message and exit.
727
 
728
        \item \shellcmd{-o \emph{file}} -- output file name.
729
 
730
        \item \shellcmd{--} -- do not interpret the subsequent command line arguments as options. Can be used if there are input file names starting with a dash.
731
\end{itemize}
732
 
733
\subsubsection{Compiler options}
734
 
735
\begin{itemize}
736
        \item \shellcmd{-i \emph{dir}} -- add \emph{dir} to the list of directories used to search for included files. Multiple directories can be specified with multiple \shellcmd{-i} arguments.
737
\end{itemize}
738
 
739
\subsubsection{Linker options (ignored in compile-only mode)}
740
 
741
\begin{itemize}
742
        \item \shellcmd{-a \emph{align}} -- object alignment. Must be a power of 2 and can't be less than 4. Default value is 4.
743
 
744
        \item \shellcmd{-b \emph{addr}} -- base address, that is, the address in memory where the executable image will be located. Must be a multiple of object alignment. Default value is 0.
745
 
746
        \item \shellcmd{-f \emph{fmt}} -- executable image format. See below for the list of supported formats.
747
 
748
        \item \shellcmd{-m \emph{file}} -- generate a map file. A map file is a human-readable list of all object and symbol addresses in the executable image.
749
 
750
        \item \shellcmd{-s \emph{size}} -- size of the executable image. Must be a multiple of 4. If total code size is less than the specified value, the executable image is padded with zeros. By default, the image is not padded.
751
\end{itemize}
752
 
753
\subsection{Output formats}
754
 
755
Output formats that can be specified with the \shellcmd{-f} command line option are listed below.
756
 
757
\begin{itemize}
758
        \item \shellcmd{bin} -- raw binary image (little-endian). This is the default format.
759
        \item \shellcmd{textio} -- text format representing binary data as a sequence of zeros and ones. This format can be directly read from VHDL (using the \code{std.textio} package) or Verilog\textregistered{} (using the \code{\$readmemb} function).
760
        \item \shellcmd{dec} -- text format representing each word as a decimal number.
761
        \item \shellcmd{hex} -- text format representing each word as a hexadecimal number.
762
\end{itemize}
763
 
764
\section{\shellcmd{lxp32dump} -- Disassembler}
765
 
766
\shellcmd{lxp32dump} takes an executable image and produces a source file in \lxp{} assembly language. The produced file is a valid program that can be compiled by \shellcmd{lxp32asm}.
767
 
768
\subsection{Command line syntax}
769
 
770
\begin{codepar}
771
    lxp32dump [ \emph{options} | \emph{input file} ]
772
\end{codepar}
773
 
774
Supported options are:
775
 
776
\begin{itemize}
777
        \item \shellcmd{-b \emph{addr}} -- executable image base address, only used for comments.
778
 
779
        \item \shellcmd{-f \emph{fmt}} -- input file format. All \shellcmd{lxp32asm} output formats are supported. If this option is not supplied, autodetection is performed.
780
 
781
        \item \shellcmd{-h}, \shellcmd{--help} -- display a short help message and exit.
782
 
783
        \item \shellcmd{-na} -- do not use instruction aliases (such as \instr{mov}, \instr{ret}, \instr{not}) and register aliases (such as \code{sp}, \code{rp}).
784
 
785
        \item \shellcmd{-o \emph{file}} -- output file name. By default, the standard output stream is used.
786
 
787
        \item \shellcmd{--} -- do not interpret subsequent command line arguments as options.
788
\end{itemize}
789
 
790
\section{\shellcmd{wigen} -- Interconnect generator}
791
 
792
\shellcmd{wigen} is a small tool that generates VHDL description of a simple WISHBONE interconnect based on shared bus topology. It supports any number of masters and slaves. The interconnect can then be used to create a SoC based on \lxp{}.
793
 
794
For interconnects with multiple masters a priority-based arbitration circuit is inserted with lower-numbered masters taking precedence. However, when a bus cycle is in progress ([CYC\_O] is asserted by the active master), the arbiter will not interrupt it even if a master with a higher priority level requests bus ownership.
795
 
796
\subsection{Command line syntax}
797
 
798
\begin{codepar}
799
        wigen [ \emph{option(s)} ] \emph{nm} \emph{ns} \emph{ma} \emph{sa} \emph{ps} [ \emph{pg} ]
800
\end{codepar}
801
 
802
\begin{itemize}
803
        \item\shellcmd{\emph{nm}} -- number of masters,
804
        \item\shellcmd{\emph{ns}} -- number of slaves,
805
        \item\shellcmd{\emph{ma}} -- master address width,
806
        \item\shellcmd{\emph{sa}} -- slave address width,
807
        \item\shellcmd{\emph{ps}} -- port size (8, 16, 32 or 64),
808
        \item\shellcmd{\emph{pg}} -- port granularity (8, 16, 32 or 64, default: the same as port size).
809
\end{itemize}
810
 
811
Supported options are:
812
 
813
\begin{itemize}
814
        \item \shellcmd{-e \emph{entity}} -- name of the design entity (default is \code{"intercon"}).
815
 
816
        \item \shellcmd{-h}, \shellcmd{--help} -- display a short help message and exit.
817
 
818
        \item \shellcmd{-o \emph{file}} -- output file name (default is \shellcmd{\emph{entity}.vhd}).
819
 
820
        \item \shellcmd{-p} -- generate pipelined arbiter (reduced combinatorial delays, increased latency).
821
 
822
        \item \shellcmd{-r} -- generate WISHBONE registered feedback signals ([CTI\_IO()] and [BTE\_IO()]).
823
 
824
        \item \shellcmd{-u} -- generate unsafe slave decoder (reduced combinatorial delays and resource usage, may not work properly if the address is invalid).
825
\end{itemize}
826
 
827
\section{Building from source}
828
\label{sec:buildfromsource}
829
 
830
Prebuilt tool executables for 32-bit Microsoft\textregistered{} Windows\textregistered{} are included in the \lxp{} IP core package. For other platforms the tools must be built from source. Since they are developed in \cplusplus{} using only the standard library, it should be possible to build them for any platform that provides a modern \cplusplus{} compiler.
831
 
832
\subsection{Requirements}
833
 
834
The following software is required to build \lxp{} tools from source:
835
 
836
\begin{enumerate}
837
        \item A modern \cplusplus{} compiler, such as Microsoft\textregistered{} Visual Studio\textregistered{} 2013 or newer, GCC 4.8 or newer, Clang 3.4 or newer.
838
        \item CMake 3.3 or newer.
839
\end{enumerate}
840
 
841
\subsection{Build procedure}
842
 
843
This software uses CMake as a build system generator. Building it involves two steps: first, the \shellcmd{cmake} program is invoked to generate a native build environment (a set of Makefiles or an IDE project); second, the generated environment is used to build the software. More details can be found in the CMake documentation.
844
 
845
\subsubsection{Examples}
846
 
847
In the following examples, it is assumed that the commands are run from the \shellcmd{tools} subdirectory of the \lxp{} IP core package tree.
848
 
849
For Microsoft\textregistered{} Visual Studio\textregistered{}:
850
 
851
\begin{codepar}
852
    mkdir build
853
    cd build
854
    cmake -G "NMake Makefiles" ../src
855
    nmake
856
    nmake install
857
\end{codepar}
858
 
859
For MSYS:
860
 
861
\begin{codepar}
862
    mkdir build
863
    cd build
864
    cmake -G "MSYS Makefiles" ../src
865
    make
866
    make install
867
\end{codepar}
868
 
869
For MinGW without MSYS:
870
 
871
\begin{codepar}
872
    mkdir build
873
    cd build
874
    cmake -G "MinGW Makefiles" ../src
875
    mingw32-make
876
    mingw32-make install
877
\end{codepar}
878
 
879
For other platforms:
880
 
881
\begin{codepar}
882
    mkdir build
883
    cd build
884
    cmake ../src
885
    make
886
    make install
887
\end{codepar}
888
 
889
\appendix
890
 
891
\chapter{Instruction set reference}
892
\label{app:instructionset}
893
 
894
See Section \ref{sec:instructionformat} for a general description of \lxp{} instruction encoding.
895
 
896
\section{List of instructions by group}
897
 
898
\begin{ctabular}{lll}
899
        \toprule
900
        Instruction & Description & Opcode \\
901
        \midrule
902
        \tabcutin{3}{Data transfer} \\
903
        \midrule
904
        \hyperref[subsec:instr:mov]{\instr{mov}} & Move & alias for \code{\instr{add} dst, src, 0} \\
905
        \hyperref[subsec:instr:lc]{\instr{lc}} & Load Constant & \code{000001} \\
906
        \hyperref[subsec:instr:lcs]{\instr{lcs}} & Load Constant Short & \code{101xxx} \\
907
        \hyperref[subsec:instr:lw]{\instr{lw}} & Load Word & \code{001000} \\
908
        \hyperref[subsec:instr:lub]{\instr{lub}} & Load Unsigned Byte & \code{001010} \\
909
        \hyperref[subsec:instr:lsb]{\instr{lsb}} & Load Signed Byte & \code{001011} \\
910
        \hyperref[subsec:instr:sw]{\instr{sw}} & Store Word & \code{001100} \\
911
        \hyperref[subsec:instr:sb]{\instr{sb}} & Store Byte & \code{001110} \\
912
        \midrule
913
        \tabcutin{3}{Arithmetic operations} \\
914
        \midrule
915
        \hyperref[subsec:instr:add]{\instr{add}} & Add & \code{010000} \\
916
        \hyperref[subsec:instr:sub]{\instr{sub}} & Subtract & \code{010001} \\
917
        \hyperref[subsec:instr:neg]{\instr{neg}} & Negate & alias for \code{\instr{sub} dst, 0, src} \\
918
        \hyperref[subsec:instr:mul]{\instr{mul}} & Multiply & \code{010010} \\
919
        \hyperref[subsec:instr:divu]{\instr{divu}} & Divide Unsigned & \code{010100} \\
920
        \hyperref[subsec:instr:divs]{\instr{divs}} & Divide Signed & \code{010101} \\
921
        \hyperref[subsec:instr:modu]{\instr{modu}} & Modulo Unsigned & \code{010110} \\
922
        \hyperref[subsec:instr:mods]{\instr{mods}} & Modulo Signed & \code{010111} \\
923
        \midrule
924
        \tabcutin{3}{Bitwise operations} \\
925
        \midrule
926
        \hyperref[subsec:instr:not]{\instr{not}} & Bitwise Not & alias for \code{\instr{xor} dst, src, -1} \\
927
        \hyperref[subsec:instr:and]{\instr{and}} & Bitwise And & \code{011000} \\
928
        \hyperref[subsec:instr:or]{\instr{or}} & Bitwise Or & \code{011001} \\
929
        \hyperref[subsec:instr:xor]{\instr{xor}} & Bitwise Exclusive Or & \code{011010}\\
930
        \hyperref[subsec:instr:sl]{\instr{sl}} & Shift Left & \code{011100} \\
931
        \hyperref[subsec:instr:sru]{\instr{sru}} & Shift Right Unsigned & \code{011110} \\
932
        \hyperref[subsec:instr:srs]{\instr{srs}} & Shift Right Signed & \code{011111} \\
933
        \midrule
934
        \tabcutin{3}{Execution transfer} \\
935
        \midrule
936
        \hyperref[subsec:instr:jmp]{\instr{jmp}} & Jump & \code{100000} \\
937
        \hyperref[subsec:instr:cjmpxxx]{\instr{cjmp\emph{xxx}}} & Compare and Jump & \code{11\emph{xxxx}} (\code{\emph{xxxx}} = condition) \\
938
        \hyperref[subsec:instr:call]{\instr{call}} & Call Procedure & \code{100001} \\
939
        \hyperref[subsec:instr:ret]{\instr{ret}} & Return from Procedure & alias for \code{\instr{jmp} rp} \\
940
        \hyperref[subsec:instr:iret]{\instr{iret}} & Interrupt Return & alias for \code{\instr{jmp} irp}\\
941
        \midrule
942
        \tabcutin{3}{Miscellaneous instructions} \\
943
        \midrule
944
        \hyperref[subsec:instr:nop]{\instr{nop}} & No Operation & \code{000000} \\
945
        \hyperref[subsec:instr:hlt]{\instr{hlt}} & Halt & \code{000010} \\
946
\end{ctabular}
947
 
948
\section{Alphabetical list of instructions}
949
 
950
\settocdepth{subsection}
951
 
952
{
953
\setlength{\parindent}{0pt}
954
\nonzeroparskip
955
 
956
\subsection{\instr{add} -- Add}
957
\label{subsec:instr:add}
958
 
959
\subsubsection{Syntax}
960
 
961
\code{\instr{add} DST, RD1, RD2}
962
 
963
\subsubsection{Encoding}
964
 
965
\code{010000 T1 T2 DST RD1 RD2}
966
 
967
Example: \code{\instr{add} r2, r1, 10} $\rightarrow$ \code{0x4202010A}
968
 
969
\subsubsection{Operation}
970
 
971
\code{DST := RD1 + RD2}
972
 
973
\subsection{\instr{and} -- Bitwise And}
974
\label{subsec:instr:and}
975
 
976
\subsubsection{Syntax}
977
 
978
\code{\instr{and} DST, RD1, RD2}
979
 
980
\subsubsection{Encoding}
981
 
982
\code{011000 T1 T2 DST RD1 RD2}
983
 
984
Example: \code{\instr{and} r2, r1, 0x3F} $\rightarrow$ \code{0x6202013F}
985
 
986
\subsubsection{Operation}
987
 
988
\code{DST := RD1 $\land$ RD2}
989
 
990
\subsection{\instr{call} -- Call Procedure}
991
\label{subsec:instr:call}
992
 
993
Save a pointer to the next instruction in the \code{rp} register and transfer execution to the address pointed by the operand.
994
 
995
\subsubsection{Syntax}
996
 
997
\code{\instr{call} RD1}
998
 
999
\subsubsection{Encoding}
1000
 
1001
\code{100001 1 0 11111110 RD1 00000000}
1002
 
1003
RD1 must be a register.
1004
 
1005
Example: \code{\instr{call} r1} $\rightarrow$ \code{0x86FE0100}
1006
 
1007
\subsubsection{Operation}
1008
 
1009
\code{rp := \emph{return\_address}}
1010
 
1011
\code{goto RD1}
1012
 
1013
Pointer in RD1 is interpreted as described in Section \ref{sec:addressing}.
1014
 
1015
\subsection{\instr{cjmp\emph{xxx}} -- Compare and Jump}
1016
\label{subsec:instr:cjmpxxx}
1017
 
1018
Compare two operands and transfer execution to the specified address if a condition is satisfied.
1019
 
1020
\subsubsection{Syntax}
1021
 
1022
\code{\instr{cjmpe} DST, RD1, RD2} (Equal)
1023
 
1024
\code{\instr{cjmpne} DST, RD1, RD2} (Not Equal)
1025
 
1026
\code{\instr{cjmpsg} DST, RD1, RD2} (Signed Greater)
1027
 
1028
\code{\instr{cjmpsge} DST, RD1, RD2} (Signed Greater or Equal)
1029
 
1030
\code{\instr{cjmpsl} DST, RD1, RD2} (Signed Less)
1031
 
1032
\code{\instr{cjmpsle} DST, RD1, RD2} (Signed Less or Equal)
1033
 
1034
\code{\instr{cjmpug} DST, RD1, RD2} (Unsigned Greater)
1035
 
1036
\code{\instr{cjmpuge} DST, RD1, RD2} (Unsigned Greater or Equal)
1037
 
1038
\code{\instr{cjmpul} DST, RD1, RD2} (Unsigned Less)
1039
 
1040
\code{\instr{cjmpule} DST, RD1, RD2} (Unsigned Less or Equal)
1041
 
1042
\subsubsection{Encoding}
1043
 
1044
\code{OPCODE T1 T2 DST RD1 RD2}
1045
 
1046
Opcodes:
1047
 
1048
\begin{tabularx}{\textwidth}{lL}
1049
\instr{cjmpe}   & \code{111000} \\
1050
\instr{cjmpne}  & \code{110100} \\
1051
\instr{cjmpsg}  & \code{110001} \\
1052
\instr{cjmpsge} & \code{111001} \\
1053
\instr{cjmpug}  & \code{110010} \\
1054
\instr{cjmpuge} & \code{111010} \\
1055
\end{tabularx}
1056
 
1057
\instr{cjmpsl}, \instr{cjmpsle}, \instr{cjmpul}, \instr{cjmpule} instructions are aliases for \instr{cjmpsg}, \instr{cjmpsge}, \instr{cjmpug}, \instr{cjmpuge}, respectively, with RD1 and RD2 operands swapped.
1058
 
1059
Example: \code{\instr{cjmpuge} r2, r1, 5} $\rightarrow$ \code{0xEA020105}
1060
 
1061
\subsubsection{Operation}
1062
 
1063
\code{if \emph{condition} then goto DST}
1064
 
1065
Pointer in DST is interpreted as described in Section \ref{sec:addressing}. Unlike most instructions, \instr{cjmp\emph{xxx}} does not write to DST.
1066
 
1067
\subsection{\instr{divs} -- Divide Signed}
1068
\label{subsec:instr:divs}
1069
 
1070
\subsubsection{Syntax}
1071
 
1072
\code{\instr{divs} DST, RD1, RD2}
1073
 
1074
\subsubsection{Encoding}
1075
 
1076
\code{010101 T1 T2 DST RD1 RD2}
1077
 
1078
Example: \code{\instr{divs} r2, r1, -3} $\rightarrow$ \code{0x560201FD}
1079
 
1080
\subsubsection{Operation}
1081
 
1082
\code{DST := (\emph{signed}) RD1 / (\emph{signed}) RD2}
1083
 
1084
The result is rounded towards zero and is undefined if RD2 is zero. If the CPU was configured without a divider, this instruction returns \code{0}.
1085
 
1086
\subsection{\instr{divu} -- Divide Unsigned}
1087
\label{subsec:instr:divu}
1088
 
1089
\subsubsection{Syntax}
1090
 
1091
\code{\instr{divu} DST, RD1, RD2}
1092
 
1093
\subsubsection{Encoding}
1094
 
1095
\code{010100 T1 T2 DST RD1 RD2}
1096
 
1097
Example: \code{\instr{divu} r2, r1, 73} $\rightarrow$ \code{0x52020107}
1098
 
1099
\subsubsection{Operation}
1100
 
1101
\code{DST := RD1 / RD2}
1102
 
1103
The result is rounded towards zero and is undefined if RD2 is zero. If the CPU was configured without a divider, this instruction returns \code{0}.
1104
 
1105
\subsection{\instr{hlt} -- Halt}
1106
\label{subsec:instr:hlt}
1107
 
1108
Wait for an interrupt.
1109
 
1110
\subsubsection{Syntax}
1111
 
1112
\code{\instr{hlt}}
1113
 
1114
\subsubsection{Encoding}
1115
 
1116
\code{000010 0 0 00000000 00000000 00000000}
1117
 
1118
\subsubsection{Operation}
1119
 
1120
Pause execution until an interrupt is received.
1121
 
1122
\subsection{\instr{jmp} -- Jump}
1123
\label{subsec:instr:jmp}
1124
 
1125
Transfer execution to the address pointed by the operand.
1126
 
1127
\subsubsection{Syntax}
1128
 
1129
\code{\instr{jmp} RD1}
1130
 
1131
\subsubsection{Encoding}
1132
 
1133
\code{100000 1 0 00000000 RD1 00000000}
1134
 
1135
RD1 must be a register.
1136
 
1137
Example: \code{\instr{jmp} r1} $\rightarrow$ \code{0x82000100}
1138
 
1139
\subsubsection{Operation}
1140
 
1141
\code{goto RD1}
1142
 
1143
Pointer in RD1 is interpreted as described in Section \ref{sec:addressing}.
1144
 
1145
\subsection{\instr{iret} -- Interrupt Return}
1146
\label{subsec:instr:iret}
1147
 
1148
Return from an interrupt handler.
1149
 
1150
\subsubsection{Syntax}
1151
 
1152
\instr{iret}
1153
 
1154
Alias for \code{\instr{jmp} irp}.
1155
 
1156
\subsection{\instr{lc} -- Load Constant}
1157
\label{subsec:instr:lc}
1158
 
1159
Load a 32-bit word to the specified register. Note that values from the [-1048576; 1048575] range can be loaded more efficiently using the \instr{lcs} instruction.
1160
 
1161
\subsubsection{Syntax}
1162
 
1163
\code{\instr{lc} DST, WORD32}
1164
 
1165
\subsubsection{Encoding}
1166
 
1167
\code{000001 0 0 DST 00000000 00000000 WORD32}
1168
 
1169
Unlike other instructions, \instr{lc} occupies two 32-bit words.
1170
 
1171
Example: \code{\instr{lc} r1, 0x12345678} $\rightarrow$ \code{0x04010000 0x12345678}
1172
 
1173
\subsubsection{Operation}
1174
 
1175
\code{DST := WORD32}
1176
 
1177
\subsection{\instr{lcs} -- Load Constant Short}
1178
\label{subsec:instr:lcs}
1179
 
1180
Load a signed value from the [-1048576; 1048575] range (a sign extended 21-bit value) to the specified register. Unlike the \instr{lc} instruction, this instruction is encoded as a single word.
1181
 
1182
\subsubsection{Syntax}
1183
 
1184
\code{\instr{lcs} DST, VAL}
1185
 
1186
\subsubsection{Encoding}
1187
 
1188
\code{101 VAL[20:16] DST VAL[15:0]}
1189
 
1190
Example: \code{\instr{lcs} r1, -1000000} $\rightarrow$ \code{0xB001BDC0}
1191
 
1192
\subsubsection{Operation}
1193
 
1194
\code{DST := (\emph{signed}) VAL}
1195
 
1196
\subsection{\instr{lsb} -- Load Signed Byte}
1197
\label{subsec:instr:lsb}
1198
 
1199
Load a byte from the specified address to the register, performing sign extension.
1200
 
1201
\subsubsection{Syntax}
1202
 
1203
\code{\instr{lsb} DST, RD1}
1204
 
1205
\subsubsection{Encoding}
1206
 
1207
\code{001011 1 0 DST RD1 00000000}
1208
 
1209
RD1 must be a register.
1210
 
1211
Example: \code{\instr{lsb} r2, r1} $\rightarrow$ \code{0x2E020100}
1212
 
1213
\subsubsection{Operation}
1214
 
1215
\code{DST := (\emph{signed}) (*(BYTE*)RD1)}
1216
 
1217
Pointer in RD1 is interpreted as described in Section \ref{sec:addressing}.
1218
 
1219
\subsection{\instr{lub} -- Load Unsigned Byte}
1220
\label{subsec:instr:lub}
1221
 
1222
Load a byte from the specified address to the register. Higher 24 bits are zeroed.
1223
 
1224
\subsubsection{Syntax}
1225
 
1226
\code{\instr{lub} DST, RD1}
1227
 
1228
\subsubsection{Encoding}
1229
 
1230
\code{001010 1 0 DST RD1 00000000}
1231
 
1232
RD1 must be a register.
1233
 
1234
Example: \code{\instr{lub} r2, r1} $\rightarrow$ \code{0x2A020100}
1235
 
1236
\subsubsection{Operation}
1237
 
1238
\code{DST := *(BYTE*)RD1}
1239
 
1240
Pointer in RD1 is interpreted as described in Section \ref{sec:addressing}.
1241
 
1242
\subsection{\instr{lw} -- Load Word}
1243
\label{subsec:instr:lw}
1244
 
1245
Load a word from the specified address to the register.
1246
 
1247
\subsubsection{Syntax}
1248
 
1249
\code{\instr{lw} DST, RD1}
1250
 
1251
\subsubsection{Encoding}
1252
 
1253
\code{001000 1 0 DST RD1 00000000}
1254
 
1255
RD1 must be a register.
1256
 
1257
Example: \code{\instr{lw} r2, r1} $\rightarrow$ \code{0x22020100}
1258
 
1259
\subsubsection{Operation}
1260
 
1261
\code{DST := *RD1}
1262
 
1263
Pointer in RD1 is interpreted as described in Section \ref{sec:addressing}.
1264
 
1265
\subsection{\instr{mods} -- Modulo Signed}
1266
\label{subsec:instr:mods}
1267
 
1268
\subsubsection{Syntax}
1269
 
1270
\code{\instr{mods} DST, RD1, RD2}
1271
 
1272
\subsubsection{Encoding}
1273
 
1274
\code{010111 T1 T2 DST RD1 RD2}
1275
 
1276
Example: \code{\instr{mods} r2, r1, 10} $\rightarrow$ \code{0x5E02010A}
1277
 
1278
\subsubsection{Operation}
1279
 
1280
\code{DST := (\emph{signed}) RD1 mod (\emph{signed}) RD2}
1281
 
1282
Modulo operation satisfies the following condition: if $Q=A/B$ and $R=A \mod B$, then $A=B \cdot Q+R$.
1283
 
1284
The result is undefined if RD2 is zero. If the CPU was configured without a divider, this instruction returns \code{0}.
1285
 
1286
\subsection{\instr{modu} -- Modulo Unsigned}
1287
\label{subsec:instr:modu}
1288
 
1289
\subsubsection{Syntax}
1290
 
1291
\code{\instr{modu} DST, RD1, RD2}
1292
 
1293
\subsubsection{Encoding}
1294
 
1295
\code{010110 T1 T2 DST RD1 RD2}
1296
 
1297
Example: \code{\instr{modu} r2, r1, 10} $\rightarrow$ \code{0x5A02010A}
1298
 
1299
\subsubsection{Operation}
1300
 
1301
\code{DST := RD1 mod RD2}
1302
 
1303
Modulo operation satisfies the following condition: if $Q=A/B$ and $R=A \mod B$, then $A=B \cdot Q+R$.
1304
 
1305
The result is undefined if RD2 is zero. If the CPU was configured without a divider, this instruction returns \code{0}.
1306
 
1307
\subsection{\instr{mov} -- Move}
1308
\label{subsec:instr:mov}
1309
 
1310
\subsubsection{Syntax}
1311
 
1312
\code{\instr{mov} DST, RD1}
1313
 
1314
Alias for \code{\instr{add} DST, RD1, 0}
1315
 
1316
\subsection{\instr{mul} -- Multiply}
1317
\label{subsec:instr:mul}
1318
 
1319
Multiply two 32-bit values. The result is also 32-bit.
1320
 
1321
\subsubsection{Syntax}
1322
 
1323
\code{\instr{mul} DST, RD1, RD2}
1324
 
1325
\subsubsection{Encoding}
1326
 
1327
\code{010010 T1 T2 DST RD1 RD2}
1328
 
1329
Example: \code{\instr{mul} r2, r1, 3} $\rightarrow$ \code{0x4A020103}
1330
 
1331
\subsubsection{Operation}
1332
 
1333
\code{DST := RD1 * RD2}
1334
 
1335
Since the product width is the same as the operand width, the result of a multiplication does not depend on operand signedness.
1336
 
1337
\subsection{\instr{neg} -- Negate}
1338
\label{subsec:instr:neg}
1339
 
1340
\subsubsection{Syntax}
1341
 
1342
\code{\instr{neg} DST, RD2}
1343
 
1344
Alias for \code{\instr{sub} DST, 0, RD2}
1345
 
1346
\subsection{\instr{nop} -- No Operation}
1347
\label{subsec:instr:nop}
1348
 
1349
\subsubsection{Syntax}
1350
 
1351
\instr{nop}
1352
 
1353
\subsubsection{Encoding}
1354
 
1355
\code{000000 0 0 00000000 00000000 00000000}
1356
 
1357
\subsubsection{Operation}
1358
 
1359
This instruction does not alter the machine state.
1360
 
1361
\subsection{\instr{not} -- Bitwise Not}
1362
\label{subsec:instr:not}
1363
 
1364
\subsubsection{Syntax}
1365
 
1366
\code{\instr{not} DST, RD1}
1367
 
1368
Alias for \code{\instr{xor} DST, RD1, -1}.
1369
 
1370
\subsection{\instr{or} -- Bitwise Or}
1371
\label{subsec:instr:or}
1372
 
1373
\subsubsection{Syntax}
1374
 
1375
\code{\instr{or} DST, RD1, RD2}
1376
 
1377
\subsubsection{Encoding}
1378
 
1379
\code{011001 T1 T2 DST RD1 RD2}
1380
 
1381
Example: \code{\instr{or} r2, r1, 0x3F} $\rightarrow$ \code{0x6602013F}
1382
 
1383
\subsubsection{Operation}
1384
 
1385
\code{DST := RD1 $\lor$ RD2}
1386
 
1387
\subsection{\instr{ret} -- Return from Procedure}
1388
\label{subsec:instr:ret}
1389
 
1390
Return from a procedure.
1391
 
1392
\subsubsection{Syntax}
1393
 
1394
\instr{ret}
1395
 
1396
Alias for \code{\instr{jmp} rp}.
1397
 
1398
\subsection{\instr{sb} -- Store Byte}
1399
\label{subsec:instr:sb}
1400
 
1401
Store the lowest byte from the register to the specified address.
1402
 
1403
\subsubsection{Syntax}
1404
 
1405
\code{\instr{sb} RD1, RD2}
1406
 
1407
\subsubsection{Encoding}
1408
 
1409
\code{001110 1 T2 00000000 RD1 RD2}
1410
 
1411
RD1 must be a register.
1412
 
1413
Example: \code{\instr{sb} r2, r1} $\rightarrow$ \code{0x3B000201}
1414
 
1415
\subsubsection{Operation}
1416
 
1417
\code{*(BYTE*)RD1 := RD2 $\land$ 0x000000FF}
1418
 
1419
Pointer in RD1 is interpreted as described in Section \ref{sec:addressing}.
1420
 
1421
\subsection{\instr{sl} -- Shift Left}
1422
\label{subsec:instr:sl}
1423
 
1424
\subsubsection{Syntax}
1425
 
1426
\code{\instr{sl} DST, RD1, RD2}
1427
 
1428
\subsubsection{Encoding}
1429
 
1430
\code{011100 T1 T2 DST RD1 RD2}
1431
 
1432
Example: \code{\instr{sl} r2, r1, 5} $\rightarrow$ \code{0x72020105}
1433
 
1434
\subsubsection{Operation}
1435
 
1436
\code{DST := RD1 << RD2}
1437
 
1438
The result is undefined if RD2 is outside the [0; 31] range.
1439
 
1440
\subsection{\instr{srs} -- Shift Right Signed}
1441
\label{subsec:instr:srs}
1442
 
1443
\subsubsection{Syntax}
1444
 
1445
\code{\instr{srs} DST, RD1, RD2}
1446
 
1447
\subsubsection{Encoding}
1448
 
1449
\code{011111 T1 T2 DST RD1 RD2}
1450
 
1451
Example: \code{\instr{srs} r2, r1, 5} $\rightarrow$ \code{0x7E020105}
1452
 
1453
\subsubsection{Operation}
1454
 
1455
\code{DST := ((\emph{signed}) RD1) >> RD2}
1456
 
1457
The result is undefined if RD2 is outside the [0; 31] range.
1458
 
1459
\subsection{\instr{sru} -- Shift Right Unsigned}
1460
\label{subsec:instr:sru}
1461
 
1462
\subsubsection{Syntax}
1463
 
1464
\code{\instr{sru} DST, RD1, RD2}
1465
 
1466
\subsubsection{Encoding}
1467
 
1468
\code{011110 T1 T2 DST RD1 RD2}
1469
 
1470
Example: \code{\instr{sru} r2, r1, 5} $\rightarrow$ \code{0x7A020105}
1471
 
1472
\subsubsection{Operation}
1473
 
1474
\code{DST := RD1 >> RD2}
1475
 
1476
The result is undefined if RD2 is outside the [0; 31] range.
1477
 
1478
\subsection{\instr{sub} -- Subtract}
1479
\label{subsec:instr:sub}
1480
 
1481
\subsubsection{Syntax}
1482
 
1483
\code{\instr{sub} DST, RD1, RD2}
1484
 
1485
\subsubsection{Encoding}
1486
 
1487
\code{010001 T1 T2 DST RD1 RD2}
1488
 
1489
Example: \code{\instr{sub} r2, r1, 5} $\rightarrow$ \code{0x46020105}
1490
 
1491
\subsubsection{Operation}
1492
 
1493
\code{DST := RD1 - RD2}
1494
 
1495
\subsection{\instr{sw} -- Store Word}
1496
\label{subsec:instr:sw}
1497
 
1498
Store the value of the register to the specified address.
1499
 
1500
\subsubsection{Syntax}
1501
 
1502
\code{\instr{sw} RD1, RD2}
1503
 
1504
\subsubsection{Encoding}
1505
 
1506
\code{001100 1 T2 00000000 RD1 RD2}
1507
 
1508
RD1 must be a register.
1509
 
1510
Example: \code{\instr{sw} r2, r1} $\rightarrow$ \code{0x33000201}
1511
 
1512
\subsubsection{Operation}
1513
 
1514
\code{*RD1 := RD2}
1515
 
1516
Pointer in RD1 is interpreted as described in Section \ref{sec:addressing}.
1517
 
1518
\subsection{\instr{xor} -- Bitwise Exclusive Or}
1519
\label{subsec:instr:xor}
1520
 
1521
\subsubsection{Syntax}
1522
 
1523
\code{\instr{xor} DST, RD1, RD2}
1524
 
1525
\subsubsection{Encoding}
1526
 
1527
\code{011010 T1 T2 DST RD1 RD2}
1528
 
1529
Example: \code{\instr{xor} r2, r1, 0x3F} $\rightarrow$ \code{0x6A02013F}
1530
 
1531
\subsubsection{Operation}
1532
 
1533
\code{DST := RD1 $\oplus$ RD2}
1534
 
1535
}
1536
 
1537
\settocdepth{section}
1538
 
1539
\chapter{Instruction cycle counts}
1540
 
1541
Cycle counts for \lxp{} instructions are listed in Table \ref{tab:cycles}, based on an assumption that no pipeline stalls are caused by the instruction bus latency or cache misses. These data are provided for reference purposes; the software should not depend on them as they can change in future hardware revisions.
1542
 
1543
\begin{table}[htbp]
1544
        \centering
1545
        \caption{Instruction cycle counts}
1546
        \label{tab:cycles}
1547
        \begin{tabularx}{0.8\textwidth}{LLLL}
1548
                \toprule
1549
                Instruction & Cycles & Instruction & Cycles \\
1550
                \midrule
1551
                \instr{add} & 1 & \instr{modu} & 37 \\
1552
                \instr{and} & 1 & \instr{mov} & 1 \\
1553
                \instr{call} & 4 & \instr{mul} & 2, 6 or 34\footnotemark[3] \\
1554
                \instr{cjmp\emph{xxx}} & 5 or 2\footnotemark[1] & \instr{neg} & 1 \\
1555
                \instr{divs} & 36 & \instr{nop} & 1 \\
1556
                \instr{divu} & 36 & \instr{not} & 1 \\
1557
                \instr{hlt} & N/A & \instr{or} & 1 \\
1558
                \instr{jmp} & 4 & \instr{ret} & 4 \\
1559
                \instr{iret} & 4 & \instr{sb} & $\ge$ 2\footnotemark[2] \\
1560
                \instr{lc} & 2 & \instr{sl} & 2 \\
1561
                \instr{lcs} & 1 & \instr{srs} & 2 \\
1562
                \instr{lsb} & $\ge$ 3\footnotemark[2] & \instr{sru} & 2 \\
1563
                \instr{lub} & $\ge$ 3\footnotemark[2] & \instr{sub} & 1 \\
1564
                \instr{lw} & $\ge$ 3\footnotemark[2] & \instr{sw} & $\ge$ 2\footnotemark[2] \\
1565
                \instr{mods} & 37 & \instr{xor} & 1 \\
1566
                \bottomrule
1567
        \end{tabularx}
1568
\end{table}
1569
 
1570
\footnotetext[1]{Depends on whether the jump is taken or not.}
1571
\footnotetext[2]{Depends on the data bus latency.}
1572
\footnotetext[3]{Depends on the multiplier architecture. See Section \ref{sec:generics}.}
1573
 
1574
\chapter{LXP32 assembly language}
1575
\label{app:assemblylanguage}
1576
 
1577
This appendix defines the assembly language used by \lxp{} development tools.
1578
 
1579
\section{Comments}
1580
 
1581
\lxp{} assembly language supports C style comments that can span across multiple lines and single-line \cplusplus{} style comments:
1582
 
1583
\begin{codepar}\itshape
1584
    /*
1585
     * This is a comment.
1586
     */
1587
 
1588
    // This is also a comment
1589
\end{codepar}
1590
 
1591
From a parser's point of view comments are equivalent to whitespace.
1592
 
1593
\section{Literals}
1594
 
1595
\lxp{} assembly language uses numeric and string literals similar to those provided by the C programming language.
1596
 
1597
Numeric literals can take form of decimal, hexadecimal or octal numbers. Literals prefixed with \code{0x} are interpreted as hexadecimal, literals prefixed with \code{0} are interpreted as octal, other literals are interpreted as decimal. A numeric literal can also start with an unary plus or minus sign which is also considered a part of the literal.
1598
 
1599
String literals must be enclosed in double quotes. The most common escape sequences used in C are supported (Table \ref{tab:stringescape}). Note that strings are not null-terminated in the LXP32 assembly language; when required, terminating null character must be inserted explicitly.
1600
 
1601
\begin{table}[htbp]
1602
        \caption{Escape sequences used in string literals}
1603
        \label{tab:stringescape}
1604
        \begin{tabularx}{\textwidth}{lL}
1605
                \toprule
1606
                Sequence & Interpretation \\
1607
                \midrule
1608
                \code{\textbackslash\textbackslash} & Backslash character \\
1609
                \code{\textbackslash "} & Double quotation mark \\
1610
                \code{\textbackslash '} & Single quotation mark (can be also used directly) \\
1611
                \code{\textbackslash t} & Tabulation character \\
1612
                \code{\textbackslash n} & Line feed \\
1613
                \code{\textbackslash r} & Carriage return \\
1614
                \code{\textbackslash x\emph{XX}} & Character with a hexadecimal code of \emph{XX} (1--2 digits) \\
1615
                \code{\textbackslash \emph{XXX}} & Character with an octal code of \emph{XXX} (1--3 digits) \\
1616
                \bottomrule
1617
        \end{tabularx}
1618
\end{table}
1619
 
1620
\section{Symbols}
1621
\label{sec:symbols}
1622
 
1623
Symbols (labels) are used to refer to data or code locations. \lxp{} assembly language does not have distinct code and data labels: symbols are used in both these contexts.
1624
 
1625
Symbol names must be valid identifiers. A valid identifier must start with an alphabetic character or an underscore, and may contain alphanumeric characters and underscores.
1626
 
1627
A symbol definition must be the first token in a source code line followed by a colon. A symbol definition can occupy a separate line (in which case it refers to the following statement). Alternatively, a statement can follow the symbol definition on the same line.
1628
 
1629
Symbols can be used as operands to the \instr{lc} and \instr{lcs} instruction statements. A symbol reference can end with a \code{@\emph{n}} sequence, where \code{\emph{n}} is a numeric literal; in this case it is interpreted as an offset (in bytes) relative to the symbol definition. For the \instr{lcs} instruction, the resulting address must still fit into the sign extended 21-bit value range (\code{0x00000000}--\code{0x000FFFFF} or \code{0xFFF00000}--\code{0xFFFFFFFF}), otherwise the linker will report an error.
1630
 
1631
By default all symbols are local, that is, they can be only referenced from the module where they were defined. To make a symbol accessible from other modules, use the \instr{\#export} directive. To reference a symbol defined in another module use the \instr{\#import} directive.
1632
 
1633
A symbol named \code{entry} or \code{Entry} has a special meaning: it is used to inform the linker about the program entry point if there are multiple input files. It does not have to be exported. If defined, this symbol must precede the first instruction or data definition statement in the module. Only one module in the program can define the entry symbol.
1634
 
1635
\begin{codeparbreakable}
1636
    \instr{lc} r10, jump\_label
1637
    \instr{lc} r11, data\_word
1638
\emph{// ...}
1639
    \instr{sw} r11, r0 \emph{// store the value of r0 to the}
1640
               \emph{// location pointed by data\_word}
1641
    \instr{jmp} r10    \emph{// transfer execution to jump\_label}
1642
\emph{// ...}
1643
jump\_label:
1644
    \instr{mov} r1, r0
1645
\emph{// ...}
1646
data\_word:
1647
    \instr{.word} 0x12345678
1648
\end{codeparbreakable}
1649
 
1650
\section{Statements}
1651
 
1652
Each statement occupies a single source code line. There are three kinds of statements:
1653
 
1654
\begin{itemize}
1655
        \item \emph{Directives} provide directions for the assembler that do not directly cause code generation.
1656
        \item \emph{Data definition statements} insert arbitrary data to the generated code.
1657
        \item \emph{Instruction statements} insert \lxp{} CPU instructions to the generated code.
1658
\end{itemize}
1659
 
1660
\subsection{Directives}
1661
 
1662
The first token of a directive statement always starts with the \code{\#} character.
1663
 
1664
\begin{codepar}
1665
\instr{\#define} \emph{identifier} [ \emph{token} ... ]
1666
\end{codepar}
1667
 
1668
Defines a macro that will be substituted with zero or more tokens. The \code{\emph{identifier}} must satisfy the requirements listed in Section \ref{sec:symbols}. Tokens can be anything, including keywords, identifiers, literals and separators (i.e. comma and colon characters).
1669
 
1670
\begin{codepar}
1671
\instr{\#error} [ \emph{msg} ]
1672
\end{codepar}
1673
 
1674
Raises a compiler error. If \emph{msg} is supplied, uses it as an error message.
1675
 
1676
\begin{codepar}
1677
\instr{\#export} \emph{identifier}
1678
\end{codepar}
1679
 
1680
Declares \code{\emph{identifier}} as an exported symbol. Exported symbols can be referenced by other modules.
1681
 
1682
\begin{codepar}
1683
\instr{\#ifdef} | \instr{\#ifndef} \emph{identifier}
1684
\code{...}
1685
\instr{\#else}
1686
\code{...}
1687
\instr{\#endif}
1688
\end{codepar}
1689
 
1690
Define C preprocessor-style conditional sections which are processed or not based on whether a certain macro has been defined. \instr{\#else} is optional. Can be nested.
1691
 
1692
\begin{codepar}
1693
\instr{\#import} \emph{identifier}
1694
\end{codepar}
1695
 
1696
Declares \code{\emph{identifier}} as an imported symbol. Used to refer to symbols exported by other modules.
1697
 
1698
\begin{codepar}
1699
\instr{\#include} \emph{filename}
1700
\end{codepar}
1701
 
1702
Processes \code{\emph{filename}} contents as it were literally inserted at the point of the \instr{\#include} directive. \code{\emph{filename}} must be a string literal.
1703
 
1704
\begin{codepar}
1705
\instr{\#message} \emph{msg}
1706
\end{codepar}
1707
 
1708
Prints \code{\emph{msg}} to the standard output stream. \code{\emph{msg}} must be a string literal.
1709
 
1710
\subsection{Data definition statements}
1711
 
1712
The first token of a data definition statement always starts with the \code{.} (period) character.
1713
 
1714
\begin{codepar}
1715
\instr{.align} [ \emph{alignment} ]
1716
\end{codepar}
1717
 
1718
Ensures that code generated by the next data definition or instruction statement is aligned to a multiple of \code{\emph{alignment}} bytes, inserting padding zeros if needed. \code{\emph{alignment}} must be a power of 2 and can't be less than 4. Default \code{\emph{alignment}} is 4. Instructions and words are always at least word-aligned; the \instr{.align} statement can be used to align them to a larger boundary, or to align byte data (see below).
1719
 
1720
The \instr{.align} statement is not guaranteed to work if the requested alignment is greater than the section alignment specified for the linker (see Subsection \ref{subsec:assemblercmdline}).
1721
 
1722
\begin{codepar}
1723
\instr{.byte} \emph{token} [, \emph{token} ... ]
1724
\end{codepar}
1725
 
1726
Inserts one or more bytes to the output code. Each \code{\emph{token}} can be either a numeric literal with a valid range of [-128; 255] or a string literal. By default, bytes are not aligned.
1727
 
1728
To define a null-terminated string, the terminating null character must be inserted explicitly.
1729
 
1730
\begin{codepar}
1731
\instr{.reserve} \emph{n}
1732
\end{codepar}
1733
 
1734
Inserts \code{\emph{n}} zero bytes to the output code.
1735
 
1736
\begin{codepar}
1737
\instr{.word} \emph{token} [, \emph{token} ... ]
1738
\end{codepar}
1739
 
1740
Inserts one or more 32-bit words to the output code. Tokens must be numeric literals.
1741
 
1742
\subsection{Instruction statements}
1743
 
1744
Instruction statements have the following general syntax:
1745
 
1746
\begin{codepar}
1747
    \instr{\emph{instruction}} [ \emph{operand} [, \emph{operand} ... ] ]
1748
\end{codepar}
1749
 
1750
Depending on the instruction, operands can be registers, numeric literals or symbols. Supported instructions are listed in Appendix \ref{app:instructionset}.
1751
 
1752
\chapter{WISHBONE datasheet}
1753
\label{app:wishbonedatasheet}
1754
 
1755
\section[Instruction bus (LXP32C only)]{Instruction bus (\lxp{}C only)}
1756
 
1757
\begin{ctabular}{ll}
1758
        \toprule
1759
        \tabcutin{2}{\makebox[0.9\textwidth][c]{General information}} \\
1760
        \midrule
1761
        WISHBONE revision & B3 \\
1762
        Type of interface & MASTER \\
1763
        Supported cycles  & BLOCK READ \\
1764
        \midrule
1765
        \tabcutin{2}{Signal names} \\
1766
        \midrule
1767
        \signal{clk\_i}       & CLK\_I \\
1768
        \signal{rst\_i}       & RST\_I \\
1769
        \signal{ibus\_cyc\_o} & CYC\_O \\
1770
        \signal{ibus\_stb\_o} & STB\_O \\
1771
        \signal{ibus\_cti\_o} & CTI\_O() \\
1772
        \signal{ibus\_bte\_o} & BTE\_O() \\
1773
        \signal{ibus\_ack\_i} & ACK\_I \\
1774
        \signal{ibus\_adr\_o} & ADR\_O() \\
1775
        \signal{ibus\_dat\_i} & DAT\_I() \\
1776
        \midrule
1777
        \tabcutin{2}{Supported tag signals} \\
1778
        \midrule
1779
        \signal{ibus\_cti\_o} & Cycle Type Identifier (address tag) \\
1780
        & \hspace{\parindent} ``010'' (Incrementing burst cycle) \\
1781
        & \hspace{\parindent} ``111'' (End-of-Burst) \\
1782
        \signal{ibus\_bte\_o} & Burst Type Extension (address tag) \\
1783
        & \hspace{\parindent} ``00'' (Linear burst) \\
1784
        \midrule
1785
        \tabcutin{2}{Dimensions} \\
1786
        \midrule
1787
        Port size & 32 \\
1788
        Port granularity & 32 \\
1789
        Maximum operand size & 32 \\
1790
        Data transfer ordering & BIG/LITTLE ENDIAN \\
1791
        Data transfer sequence & UNDEFINED \\
1792
        \bottomrule
1793
\end{ctabular}
1794
 
1795
\section{Data bus}
1796
 
1797
\begin{ctabular}{ll}
1798
        \toprule
1799
        \tabcutin{2}{\makebox[0.9\textwidth][c]{General information}} \\
1800
        \midrule
1801
        WISHBONE revision & B3 \\
1802
        Type of interface & MASTER \\
1803
        Supported cycles  & SINGLE READ/WRITE \\
1804
                          & RMW \\
1805
        \midrule
1806
        \tabcutin{2}{Signal names} \\
1807
        \midrule
1808
        \signal{clk\_i}       & CLK\_I \\
1809
        \signal{rst\_i}       & RST\_I \\
1810
        \signal{dbus\_cyc\_o} & CYC\_O \\
1811
        \signal{dbus\_stb\_o} & STB\_O \\
1812
        \signal{dbus\_we\_o}  & WE\_O \\
1813
        \signal{dbus\_sel\_o} & SEL\_O() \\
1814
        \signal{dbus\_ack\_i} & ACK\_I \\
1815
        \signal{dbus\_adr\_o} & ADR\_O() \\
1816
        \signal{dbus\_dat\_o} & DAT\_O() \\
1817
        \signal{dbus\_dat\_i} & DAT\_I() \\
1818
        \midrule
1819
        \tabcutin{2}{Dimensions} \\
1820
        \midrule
1821
        Port size & 32 \\
1822
        Port granularity & 8 \\
1823
        Maximum operand size & 32 \\
1824
        Data transfer ordering & LITTLE ENDIAN \\
1825
        Data transfer sequence & UNDEFINED \\
1826
        \bottomrule
1827
\end{ctabular}
1828
 
1829
\chapter{List of changes}
1830
 
1831
\section*{Version 1.2 (2021-10-21)}
1832
 
1833
This release introduces a few non-breaking changes to the software and testbench. The CPU RTL description hasn't been changed from the previous release.
1834
 
1835
\begin{itemize}
1836
        \item \shellcmd{lxp32asm} now supports C-style conditional processing directives: \instr{\#ifdef}, \instr{\#ifndef}, \instr{\#else} and \instr{\#endif}.
1837
        \item \instr{\#define} directive can now declare a macro with zero subsitute tokens.
1838
        \item A new \instr{\#error} directive.
1839
        \item Minor changes to the testbench.
1840
\end{itemize}
1841
 
1842
\section*{Version 1.1 (2019-01-11)}
1843
 
1844
This release introduces a minor but technically breaking hardware change: the START\_ADDR generic, which used to be 30-bit, has been for convenience extended to a full 32-bit word; the two least significant bits are ignored.
1845
 
1846
The other breaking change affects the assembly language syntax. Previously all symbols used to be public, and multiple modules could not define symbols with the same name. As of now only symbols explicitly exported using the \instr{\#export} directive are public. \instr{\#extern} directive has been replaced by \instr{\#import}.
1847
 
1848
Other notable changes include:
1849
 
1850
\begin{itemize}
1851
        \item A new instruction, \instr{lcs} (\instrname{Load Constant Short}), has been added, which loads a 21-bit sign extended constant to a register. Unlike \instr{lc}, it is encoded as a single word and takes one cycle to execute.
1852
        \item Optimizations in the divider unit. Division instructions (\instr{divs} and \instr{divu}) now take one fewer cycle to execute (modulo instructions are unaffected).
1853
        \item LXP32 assembly language now supports a new instruction alias, \instr{neg} (\instrname{Negate}), which is equivalent to \code{\instr{sub} dst, 0, src}.
1854
\end{itemize}
1855
 
1856
\section*{Version 1.0 (2016-02-20)}
1857
 
1858
Initial public release.
1859
 
1860
\end{document}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.