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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [docs/] [userguide/] [content.adoc] - Blame information for rev 64

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

Line No. Rev Author Line
1 60 zero_gravi
Let's Get It Started!
2
 
3 63 zero_gravi
This user guide uses the NEORV32 project _as is_ from the official `neorv32` repository.
4
To make your first NEORV32 project run, follow the guides from the upcoming sections. It is recommended to
5
follow these guides step by step and eventually in the presented order.
6 60 zero_gravi
 
7 63 zero_gravi
[TIP]
8
This guide uses the minimalistic and platform/toolchain agnostic SoC test setups from
9
`rtl/test_setups` for illustration. You can use one of the provided test setups for
10
your first FPGA tests. Alternatively, have a look at the `setups` folder,
11
which provides more sophisticated example setups for various FPGAs/FPGA boards and toolchains.
12
 
13
 
14 60 zero_gravi
:sectnums:
15 61 zero_gravi
== Software Toolchain Setup
16 60 zero_gravi
 
17 61 zero_gravi
To compile (and debug) executables for the NEORV32 a RISC-V toolchain is required.
18
There are two possibilities to get this:
19 60 zero_gravi
 
20 63 zero_gravi
1. Download and _build_ the official RISC-V GNU toolchain yourself.
21 61 zero_gravi
2. Download and install a prebuilt version of the toolchain; this might also done via the package manager / app store of your OS
22 60 zero_gravi
 
23 63 zero_gravi
[NOTE]
24
The default toolchain prefix (`RISCV_PREFIX` variable) for this project is **`riscv32-unknown-elf-`**. Of course you can use any other RISC-V
25
toolchain (like `riscv64-unknown-elf-`) that is capable to emit code for a `rv32` architecture. Just change `RISCV_PREFIX`
26
according to your needs.
27 60 zero_gravi
 
28
 
29
:sectnums:
30
=== Building the Toolchain from Scratch
31
 
32 61 zero_gravi
To build the toolchain by yourself you can follow the guide from the official https://github.com/riscv/riscv-gnu-toolchain GitHub page.
33
You need to make sure the generated toolchain fits the architecture of the NEORV32 core. To get a toolchain that even supports minimal
34
ISA extension configurations, it is recommend to compile for `rv32i` only. Please note that this minimal ISA also provides further ISA
35
extensions like `m` or `c`. Of course you can use a `multilib` approach to generate
36
toolchains for several target ISAs.
37 60 zero_gravi
 
38 61 zero_gravi
.Configuring GCC build for `rv32i` (minimal ISA)
39 60 zero_gravi
[source,bash]
40
----
41
riscv-gnu-toolchain$ ./configure --prefix=/opt/riscv --with-arch=rv32i –-with-abi=ilp32
42
riscv-gnu-toolchain$ make
43
----
44
 
45 63 zero_gravi
[IMPORTANT]
46
Keep in mind that – for instance – a toolchain build with `--with-arch=rv32imc` only provides library code compiled with
47
compressed (`C`) and `mul`/`div` instructions (`M`)! Hence, this code cannot be executed (without
48
emulation) on an architecture without these extensions!
49 60 zero_gravi
 
50 63 zero_gravi
 
51 60 zero_gravi
:sectnums:
52
=== Downloading and Installing a Prebuilt Toolchain
53
 
54
Alternatively, you can download a prebuilt toolchain.
55
 
56 61 zero_gravi
:sectnums:
57
==== Use The Toolchain I have Build
58 60 zero_gravi
 
59 61 zero_gravi
I have compiled a GCC toolchain on a 64-bit x86 Ubuntu (Ubuntu on Windows, actually) and uploaded it to
60 60 zero_gravi
GitHub. You can directly download the according toolchain archive as single _zip-file_ within a packed
61 61 zero_gravi
release from https://github.com/stnolting/riscv-gcc-prebuilt.
62 60 zero_gravi
 
63
Unpack the downloaded toolchain archive and copy the content to a location in your file system (e.g.
64
`/opt/riscv`). More information about downloading and installing my prebuilt toolchains can be found in
65
the repository's README.
66
 
67
 
68 61 zero_gravi
:sectnums:
69
==== Use a Third Party Toolchain
70
 
71 60 zero_gravi
Of course you can also use any other prebuilt version of the toolchain. There are a lot  RISC-V GCC packages out there -
72 61 zero_gravi
even for Windows. On Linux system you might even be able to fetch a toolchain via your distribution's package manager.
73 60 zero_gravi
 
74
[IMPORTANT]
75
Make sure the toolchain can (also) emit code for a `rv32i` architecture, uses the `ilp32` or `ilp32e` ABI and **was not build** using
76
CPU extensions that are not supported by the NEORV32 (like `D`).
77
 
78
 
79
:sectnums:
80
=== Installation
81
 
82 61 zero_gravi
Now you have the toolchain binaries. The last step is to add them to your `PATH` environment variable (if you have not
83
already done so): make sure to add the _binaries_ folder (`bin`) of your toolchain.
84 60 zero_gravi
 
85
[source,bash]
86
----
87
$ export PATH:$PATH:/opt/riscv/bin
88
----
89
 
90
You should add this command to your `.bashrc` (if you are using bash) to automatically add the RISC-V
91
toolchain at every console start.
92
 
93
:sectnums:
94
=== Testing the Installation
95
 
96
To make sure everything works fine, navigate to an example project in the NEORV32 example folder and
97
execute the following command:
98
 
99
[source,bash]
100
----
101
neorv32/sw/example/blink_led$ make check
102
----
103
 
104 61 zero_gravi
This will test all the tools required for the generating NEORV32 executables.
105
Everything is working fine if `Toolchain check OK` appears at the end.
106 60 zero_gravi
 
107
 
108
 
109
<<<
110
// ####################################################################################################################
111
:sectnums:
112
== General Hardware Setup
113
 
114 63 zero_gravi
This guide shows the basics of setting up a NEORV32 project for FPGA implementation (or simulation only)
115
_from scratch_. It uses a _simplified_ test "SoC" setup of the processor to keeps things simple at the beginning.
116
This simple setup is intended for evaluation or as "hello world" project to check out the NEORV32
117
on _your_ FPGA board.
118 60 zero_gravi
 
119
[TIP]
120 63 zero_gravi
If you want to use a more sophisticated pre-defined setup to start with, check out the
121
`setups` folder, which provides example setups for various FPGA, boards and toolchains.
122 60 zero_gravi
 
123 63 zero_gravi
The NEORV32 project features two minimalistic pre-configured test setups in
124
https://github.com/stnolting/neorv32/blob/master/rtl/test_setups[`rtl/test_setups`].
125
Both test setups only implement very basic processor and CPU features.
126
The main difference between the two setups is the processor boot concept - so how to get a software executable
127
_into_ the processor:
128 60 zero_gravi
 
129 63 zero_gravi
* **`rtl/test_setups/neorv32_testsetup_approm.vhd`**: this setup does not require a connection via UART. The
130
software executable is "installed" into the bitstream to initialize a read-only memory. Use this setup
131
if your FPGA board does _not_ provide a UART interface.
132
* **`rtl/test_setups/neorv32_testsetup_bootloader.vhd`**: this setups uses the UART and the default NEORV32
133
bootloader to upload new software executables. Use this setup if your board _does_ provide a UART interface.
134
 
135
.NEORV32 "hello world" test setup (`rtl/test_setups/neorv32_testsetup_bootloader.vhd`)
136
image::neorv32_test_setup.png[align=center]
137
 
138
.External Clock Source
139
[NOTE]
140
These test setups are intended to be directly used as **design top entity**. Of course you can also instantiate them
141
into another design unit. If your FPGA board only provides _very fast_ external clock sources (like on the FOMU board)
142
you might need to add clock management components (PLLs, DCMs, MMCMs, ...) to the test setup or to the according top entity
143
if you instantiate one of the test setups.
144
 
145 61 zero_gravi
[start=1]
146 60 zero_gravi
. Create a new project with your FPGA EDA tool of choice.
147 63 zero_gravi
. Add all VHDL files from the project's `rtl/core` folder to your project.
148 64 zero_gravi
 
149
.Internal Memories
150
[IMPORTANT]
151
For a _general_ first setup (technology-independent) use the _default_ memory architectures for the internal memories
152
(IMEM and DMEM). These are located in `rtl/core/mem`, so **make sure to add the files from `rtl/core/mem` to your project, too**. +
153
 +
154
If synthesis cannot efficiently map those default memory descriptions to the available memory resources, you can later replace the
155
default memory architectures by optimized platform-specific memory architectures. **Example:** The `setups/radiant/UPduino_v3`
156
example setup uses optimized memory primitives. Hence, it does not include the default memory architectures from
157
`rtl/core/mem` as these are replaced by device-specific implementations. However, it still has to include the entity
158
definitions from `rtl/core`.
159
 
160
[start=3]
161 61 zero_gravi
. Make sure to add all the rtl files to a new library called `neorv32`. If your FPGA tools does not
162
provide a field to enter the library name, check out the "properties" menu of the added rtl files.
163 64 zero_gravi
 
164
.Compile order
165
[NOTE]
166
Some tools (like Lattice Radiant) might require a _manual compile order_ of the VHDL source files to identify the dependencies.
167
The package file `neorv32_package.vhd` should be analyzed first followed by the memory image files (`neorv32_application_imagevhd`
168
and `neorv32_bootloader_image.vhd`) and the entity-only files (`neorv32_*mem.entity.vhd`).
169
 
170
[start=4]
171 63 zero_gravi
. The `rtl/core/neorv32_top.vhd` VHDL file is the top entity of the NEORV32 processor, which can be
172
instantiated into the "real" project. However, in this tutorial we will use one of the pre-defined
173
test setups from `rtl/test_setups` (see above).
174 61 zero_gravi
 
175
[IMPORTANT]
176
Make sure to include the `neorv32` package into your design when instantiating the processor: add
177
`library neorv32;` and `use neorv32.neorv32_package.all;` to your design unit.
178
 
179
[start=5]
180 63 zero_gravi
. Add the pre-defined test setup of choice to the project, too, and select it as _top entity_.
181
. The entity of both test setups
182
provide a minimal set of configuration generics, that might have to be adapted to match your FPGA and board:
183 60 zero_gravi
 
184 63 zero_gravi
.Test setup entity - configuration generics
185 60 zero_gravi
[source,vhdl]
186
----
187 63 zero_gravi
  generic (
188
    -- adapt these for your setup --
189
    CLOCK_FREQUENCY   : natural := 100000000; <1>
190
    MEM_INT_IMEM_SIZE : natural := 16*1024;   <2>
191
    MEM_INT_DMEM_SIZE : natural := 8*1024     <3>
192
  );
193 60 zero_gravi
----
194 61 zero_gravi
<1> Clock frequency of `clk_i` signal in Hertz
195
<2> Default size of internal instruction memory: 16kB
196
<3> Default size of internal data memory: 8kB
197 60 zero_gravi
 
198 63 zero_gravi
[start=7]
199
. If you feel like it – or if your FPGA does not provide sufficient resources – you can modify the
200
_memory sizes_ (`MEM_INT_IMEM_SIZE` and `MEM_INT_DMEM_SIZE` – marked with notes "2" and "3"). But as mentioned
201
above, let's keep things simple at first and use the standard configuration for now.
202
. There is one generic that _has to be set according to your FPGA board_ setup: the actual clock frequency
203
of the top's clock input signal (`clk_i`). Use the `CLOCK_FREQUENCY` generic to specify your clock source's
204
frequency in Hertz (Hz).
205 60 zero_gravi
 
206
[NOTE]
207 63 zero_gravi
If you have changed the default memory configuration (`MEM_INT_IMEM_SIZE` and `MEM_INT_DMEM_SIZE` generics)
208 61 zero_gravi
keep those new sizes in mind – these values are required for setting
209 60 zero_gravi
up the software framework in the next section <<_general_software_framework_setup>>.
210
 
211 63 zero_gravi
[start=9]
212 60 zero_gravi
. Depending on your FPGA tool of choice, it is time to assign the signals of the test setup top entity to
213 63 zero_gravi
the according pins of your FPGA board. All the signals can be found in the entity declaration of the
214
corresponding test setup:
215 60 zero_gravi
 
216 63 zero_gravi
.Entity signals of `neorv32_testsetup_approm.vhd`
217 60 zero_gravi
[source,vhdl]
218
----
219
  port (
220
    -- Global control --
221 63 zero_gravi
    clk_i       : in  std_ulogic; -- global clock, rising edge
222
    rstn_i      : in  std_ulogic; -- global reset, low-active, async
223 60 zero_gravi
    -- GPIO --
224 63 zero_gravi
    gpio_o      : out std_ulogic_vector(7 downto 0) -- parallel output
225
  );
226
----
227
 
228
.Entity signals of `neorv32_testsetup_bootloader.vhd`
229
[source,vhdl]
230
----
231
  port (
232
    -- Global control --
233
    clk_i       : in  std_ulogic; -- global clock, rising edge
234
    rstn_i      : in  std_ulogic; -- global reset, low-active, async
235
    -- GPIO --
236 60 zero_gravi
    gpio_o      : out std_ulogic_vector(7 downto 0); -- parallel output
237
    -- UART0 --
238
    uart0_txd_o : out std_ulogic; -- UART0 send data
239 63 zero_gravi
    uart0_rxd_i : in  std_ulogic  -- UART0 receive data
240
  );
241 60 zero_gravi
----
242
 
243 63 zero_gravi
.Signal Polarity
244
[NOTE]
245
If your FPGA board has inverse polarity for certain input/output you can add `not` gates. Example: The reset signal
246
`rstn_i` is low-active by default; the LEDs connected to `gpio_o` high-active by default.
247
You can do this in your board top if you instantiate the test setup,
248
or _inside_ the test setup if this is your top entity (low-active LEDs example: `gpio_o <= NOT con_gpio_o(7 downto 0);`).
249
 
250
[start=10]
251 60 zero_gravi
. Attach the clock input `clk_i` to your clock source and connect the reset line `rstn_i` to a button of
252
your FPGA board. Check whether it is low-active or high-active – the reset signal of the processor is
253
**low-active**, so maybe you need to invert the input signal.
254 63 zero_gravi
. If possible, connected _at least_ bit `0` of the GPIO output port `gpio_o` to a LED (see "Signal Polarity" note above).
255
. Finally, if your are using the UART-based test setup (`neorv32_testsetup_bootloader.vhd`)
256
connect the UART communication signals `uart0_txd_o` and `uart0_rxd_i` to the host interface (e.g. USB-UART converter).
257 60 zero_gravi
. Perform the project HDL compilation (synthesis, mapping, bitstream generation).
258 61 zero_gravi
. Program the generated bitstream into your FPGA and press the button connected to the reset signal.
259 63 zero_gravi
. Done! The LED at `gpio_o(0)` should be flashing now.
260 60 zero_gravi
 
261 63 zero_gravi
[TIP]
262
After the GCC toolchain for compiling RISC-V source code is ready (chapter <<_general_software_framework_setup>>),
263
you can advance to one of these chapters to learn how to get a software executable into your processor setup:
264
* If you are using the `neorv32_testsetup_approm.vhd` setup: See section <<_installing_an_executable_directly_into_memory>>.
265
* If you are using the `neorv32_testsetup_bootloader.vhd` setup: See section <<_uploading_and_starting_of_a_binary_executable_image_via_uart>>.
266 60 zero_gravi
 
267
 
268 63 zero_gravi
 
269 60 zero_gravi
<<<
270
// ####################################################################################################################
271
:sectnums:
272
== General Software Framework Setup
273
 
274 61 zero_gravi
To allow executables to be _actually executed_ on the NEORV32 Processor the configuration of the software framework
275
has to be aware to the hardware configuration. This guide focuses on the memory configuration. To enabled
276
certain CPU ISA festures refer to the <<_enabling_risc_v_cpu_extensions>> section.
277 60 zero_gravi
 
278 61 zero_gravi
[TIP]
279
If you have **not** changed the _default_ memory configuration in section <<_general_hardware_setup>>
280
you are already done and you can skip the rest of this guide.
281
 
282 60 zero_gravi
[start=1]
283
. Open the NEORV32 linker script `sw/common/neorv32.ld` with a text editor. Right at the
284 61 zero_gravi
beginning of this script you will find the `MEMORY` configuration listing the different memory section:
285 60 zero_gravi
 
286 61 zero_gravi
.Cut-out of the linker script `neorv32.ld`: `ram` memory section configuration
287 60 zero_gravi
[source,c]
288
----
289
MEMORY
290
{
291 61 zero_gravi
  ram  (rwx) : ORIGIN = 0x80000000, LENGTH = DEFINED(make_bootloader) ? 512 : 8*1024 # <1>
292
...
293 60 zero_gravi
----
294 61 zero_gravi
<1> Size of the data memory address space (right-most value) (internal/external DMEM); here 8kB
295 60 zero_gravi
 
296 61 zero_gravi
[start=2]
297
. We only need to change the `ram` section, which presents the available data address space.
298
If you have changed the DMEM (_MEM_INT_DMEM_SIZE_ generic) size adapt the `LENGTH` parameter of the `ram`
299
section (here: `8*1024`) so it is equal to your DMEM hardware configuration.
300 60 zero_gravi
 
301 61 zero_gravi
[IMPORTANT]
302
Make sure you only modify the _right-most_ value (here: 8*1024)! +
303
The "`512`" are not relevant for the application.
304
 
305 60 zero_gravi
[start=3]
306 61 zero_gravi
. Done! Save your changes and close the linker script.
307 60 zero_gravi
 
308 61 zero_gravi
.Advanced: Section base address and size
309 60 zero_gravi
[IMPORTANT]
310 61 zero_gravi
More information can be found in the datasheet section https://stnolting.github.io/neorv32/#_address_space[Address Space].
311 60 zero_gravi
 
312
 
313
 
314
<<<
315
// ####################################################################################################################
316
:sectnums:
317
== Application Program Compilation
318
 
319 62 zero_gravi
This guide shows how to compile an example C-code application into a NEORV32 executable that
320 61 zero_gravi
can be uploaded via the bootloader or the on-chip debugger.
321
 
322
[IMPORTANT]
323
If your FPGA board does not provide such an interface - don't worry!
324
Section <<_installing_an_executable_directly_into_memory>> shows how to
325
run custom programs on your FPGA setup without having a UART.
326
 
327 60 zero_gravi
[start=1]
328 61 zero_gravi
. Open a terminal console and navigate to one of the project's example programs. For instance, navigate to the
329
simple `sw/example_blink_led` example program. This program uses the NEORV32 GPIO module to display
330 60 zero_gravi
an 8-bit counter on the lowest eight bit of the `gpio_o` output port.
331
. To compile the project and generate an executable simply execute:
332
 
333
[source,bash]
334
----
335 61 zero_gravi
neorv32/sw/example/blink_led$ make clean_all exe
336 60 zero_gravi
----
337
 
338
[start=3]
339 61 zero_gravi
. We are using the `clean_all` taret to make sure everything is re-build.
340 60 zero_gravi
. This will compile and link the application sources together with all the included libraries. At the end,
341 61 zero_gravi
your application is transformed into an ELF file (`main.elf`). The _NEORV32 image generator_ (in `sw/image_gen`)
342
takes this file and creates a final executable. The makefile will show the resulting memory utilization and
343
the executable size:
344 60 zero_gravi
 
345
[source,bash]
346
----
347 61 zero_gravi
neorv32/sw/example/blink_led$ make clean_all exe
348 60 zero_gravi
Memory utilization:
349 61 zero_gravi
   text    data     bss     dec     hex filename
350
   3176       0     120    3296     ce0 main.elf
351
Compiling ../../../sw/image_gen/image_gen
352 60 zero_gravi
Executable (neorv32_exe.bin) size in bytes:
353 62 zero_gravi
3188
354 60 zero_gravi
----
355
 
356 61 zero_gravi
[start=5]
357
. That's it. The `exe` target has created the actual executable `neorv32_exe.bin` in the current folder
358
that is ready to be uploaded to the processor.
359 60 zero_gravi
 
360
[TIP]
361 61 zero_gravi
The compilation process will also create a `main.asm` assembly listing file in the current folder, which
362
shows the actual assembly code of the application.
363 60 zero_gravi
 
364
 
365
 
366
<<<
367
// ####################################################################################################################
368
:sectnums:
369
== Uploading and Starting of a Binary Executable Image via UART
370
 
371 61 zero_gravi
Follow this guide to use the bootloader to upload an executable via UART.
372 60 zero_gravi
 
373 61 zero_gravi
[NOTE]
374
This concept uses the default "Indirect Boot" scenario that uses the bootloader to upload new executables.
375
See datasheet section https://stnolting.github.io/neorv32/#_indirect_boot[Indirect Boot] for more information.
376 60 zero_gravi
 
377 61 zero_gravi
[IMPORTANT]
378
If your FPGA board does not provide such an interface - don't worry!
379
Section <<_installing_an_executable_directly_into_memory>> shows how to
380
run custom programs on your FPGA setup without having a UART.
381 60 zero_gravi
 
382
[start=1]
383 61 zero_gravi
. Connect the primary UART (UART0) interface of your FPGA board to a serial port of your host computer.
384
. Start a terminal program. In this tutorial, I am using TeraTerm for Windows. You can download it fore free
385
from https://ttssh2.osdn.jp/index.html.en
386 60 zero_gravi
 
387 61 zero_gravi
[NOTE]
388
_Any_ terminal program that can connect to a serial port should work. However, make sure the program
389
can transfer data in _raw_ byte mode without any protocol overhead around it.
390 60 zero_gravi
 
391
[start=3]
392 61 zero_gravi
. Open a connection to the the serial port your UART is connected to. Configure the terminal setting according to the
393 60 zero_gravi
following parameters:
394
 
395
* 19200 Baud
396
* 8 data bits
397
* 1 stop bit
398
* no parity bits
399 61 zero_gravi
* _no_ transmission/flow control protocol
400
* receiver (host computer) newline on `\r\n` (carriage return & newline)
401 60 zero_gravi
 
402
[start=4]
403 61 zero_gravi
. Also make sure that single chars are send from your computer _without_ any consecutive "new line" or "carriage
404 60 zero_gravi
return" commands (this is highly dependent on your terminal application of choice, TeraTerm only
405
sends the raw chars by default).
406
. Press the NEORV32 reset button to restart the bootloader. The status LED starts blinking and the
407
bootloader intro screen appears in your console. Hurry up and press any key (hit space!) to abort the
408
automatic boot sequence and to start the actual bootloader user interface console.
409
 
410
.Bootloader console; aborted auto-boot sequence
411
[source,bash]
412
----
413
<< NEORV32 Bootloader >>
414
 
415
BLDV: Mar 23 2021
416
HWV:  0x01050208
417
CLK:  0x05F5E100
418
MISA: 0x40901105
419
ZEXT: 0x00000023
420
PROC: 0x0EFF0037
421
IMEM: 0x00004000 bytes @ 0x00000000
422
DMEM: 0x00002000 bytes @ 0x80000000
423
 
424
Autoboot in 8s. Press key to abort.
425
Aborted.
426
 
427
Available commands:
428
h: Help
429
r: Restart
430
u: Upload
431
s: Store to flash
432
l: Load from flash
433
e: Execute
434
CMD:>
435
----
436
 
437
[start=6]
438 61 zero_gravi
. Execute the "Upload" command by typing `u`. Now the bootloader is waiting for a binary executable to be send.
439 60 zero_gravi
 
440
[source,bash]
441
----
442
CMD:> u
443
Awaiting neorv32_exe.bin...
444
----
445
 
446
[start=7]
447 61 zero_gravi
. Use the "send file" option of your terminal program to send a NEORV32 executable (`neorv32_exe.bin`).
448
. Again, make sure to transmit the executable in raw binary mode (no transfer protocol).
449
When using TeraTerm, select the "binary" option in the send file dialog.
450 60 zero_gravi
. If everything went fine, OK will appear in your terminal:
451
 
452
[source,bash]
453
----
454
CMD:> u
455
Awaiting neorv32_exe.bin... OK
456
----
457
 
458
[start=10]
459 61 zero_gravi
. The executable is now in the instruction memory of the processor. To execute the program right
460 60 zero_gravi
now run the "Execute" command by typing `e`:
461
 
462
[source,bash]
463
----
464
CMD:> u
465
Awaiting neorv32_exe.bin... OK
466
CMD:> e
467
Booting...
468
Blinking LED demo program
469
----
470
 
471
[start=11]
472 61 zero_gravi
. If everything went fine, you should see the LEDs blinking.
473 60 zero_gravi
 
474 61 zero_gravi
[NOTE]
475
The bootloader will print error codes if something went wrong.
476
See section https://stnolting.github.io/neorv32/#_bootloader[Bootloader] of the NEORV32 datasheet for more information.
477 60 zero_gravi
 
478 61 zero_gravi
[TIP]
479
See section <<_programming_an_external_spi_flash_via_the_bootloader>> to learn how to use an external SPI
480
flash for nonvolatile program storage.
481 60 zero_gravi
 
482 61 zero_gravi
[TIP]
483
Executables can also be uploaded via the **on-chip debugger**.
484
See section <<_debugging_with_gdb>> for more information.
485
 
486
 
487
 
488 60 zero_gravi
<<<
489
// ####################################################################################################################
490
:sectnums:
491 61 zero_gravi
== Installing an Executable Directly Into Memory
492 60 zero_gravi
 
493 61 zero_gravi
If you do not want to use the bootloader (or the on-chip debugger) for executable upload or if your setup does not provide
494
a serial interface for that, you can also directly install an application into embedded memory.
495 60 zero_gravi
 
496 61 zero_gravi
This concept uses the "Direct Boot" scenario that implements the processor-internal IMEM as ROM, which is
497
pre-initialized with the application's executable during synthesis. Hence, it provides _non-volatile_ storage of the
498
executable inside the processor. This storage cannot be altered during runtime and any source code modification of
499
the application requires to re-program the FPGA via the bitstream.
500
 
501
[TIP]
502
See datasheet section https://stnolting.github.io/neorv32/#_direct_boot[Direct Boot] for more information.
503
 
504
 
505
 
506
Using the IMEM as ROM:
507
 
508
* for this boot concept the bootloader is no longer required
509
* this concept only works for the internal IMEM (but can be extended to work with external memories coupled via the processor's bus interface)
510 62 zero_gravi
* make sure that the memory components (like block RAM) the IMEM is mapped to support an initialization via the bitstream
511 61 zero_gravi
 
512 60 zero_gravi
[start=1]
513 61 zero_gravi
. At first, make sure your processor setup actually implements the internal IMEM: the `MEM_INT_IMEM_EN` generics has to be set to `true`:
514
 
515
.Processor top entity configuration - enable internal IMEM
516
[source,vhdl]
517
----
518
  -- Internal Instruction memory --
519
  MEM_INT_IMEM_EN => true, -- implement processor-internal instruction memory
520
----
521
 
522
[start=2]
523
. For this setup we do not want the bootloader to be implemented at all. Disable implementation of the bootloader by setting the
524 62 zero_gravi
`INT_BOOTLOADER_EN` generic to `false`. This will also modify the processor-internal IMEM so it is initialized with the executable during synthesis.
525 61 zero_gravi
 
526
.Processor top entity configuration - disable internal bootloader
527
[source,vhdl]
528
----
529
  -- General --
530
  INT_BOOTLOADER_EN => false, -- boot configuration: false = boot from int/ext (I)MEM
531
----
532
 
533
[start=3]
534
. To generate an "initialization image" for the IMEM that contains the actual application, run the `install` target when compiling your application:
535
 
536
[source,bash]
537
----
538
neorv32/sw/example/blink_led$ make clean_all install
539
Memory utilization:
540
   text    data     bss     dec     hex filename
541
   3176       0     120    3296     ce0 main.elf
542
Compiling ../../../sw/image_gen/image_gen
543
Installing application image to ../../../rtl/core/neorv32_application_image.vhd
544
----
545
 
546
[start=4]
547
. The `install` target has compiled all the application sources but instead of creating an executable (`neorv32_exe.bit`) that can be uploaded via the
548
bootloader, it has created a VHDL memory initialization image `core/neorv32_application_image.vhd`.
549
. This VHDL file is automatically copied to the core's rtl folder (`rtl/core`) so it will be included for the next synthesis.
550
. Perform a new synthesis. The IMEM will be build as pre-initialized ROM (inferring embedded memories if possible).
551
. Upload your bitstream. Your application code now resides unchangeable in the processor's IMEM and is directly executed after reset.
552
 
553
 
554
The synthesis tool / simulator will print asserts to inform about the (IMEM) memory / boot configuration:
555
 
556
[source]
557
----
558
NEORV32 PROCESSOR CONFIG NOTE: Boot configuration: Direct boot from memory (processor-internal IMEM).
559
NEORV32 PROCESSOR CONFIG NOTE: Implementing processor-internal IMEM as ROM (3176 bytes), pre-initialized with application.
560
----
561
 
562
 
563
 
564
<<<
565
// ####################################################################################################################
566
:sectnums:
567
== Setup of a New Application Program Project
568
 
569
[start=1]
570
. The easiest way of creating a _new_ software application project is to copy an _existing_ one. This will keep all
571
file dependencies. For example you can copy `sw/example/blink_led` to `sw/example/flux_capacitor`.
572
. If you want to place you application somewhere outside `sw/example` you need to adapt the application's makefile.
573
In the makefile you will find a variable that keeps the relative or absolute path to the NEORV32 repo home
574 60 zero_gravi
folder. Just modify this variable according to your new project's home location:
575
 
576
[source,makefile]
577
----
578
# Relative or absolute path to the NEORV32 home folder (use default if not set by user)
579
NEORV32_HOME ?= ../../..
580
----
581
 
582
[start=3]
583 61 zero_gravi
. If your project contains additional source files outside of the project folder, you can add them to
584
the `APP_SRC` variable:
585 60 zero_gravi
 
586
[source,makefile]
587
----
588
# User's application sources (add additional files here)
589
APP_SRC = $(wildcard *.c) ../somewhere/some_file.c
590
----
591
 
592
[start=4]
593 61 zero_gravi
. You also can add a folder containing your application's include files to the
594
`APP_INC` variable (do not forget the `-I` prefix):
595 60 zero_gravi
 
596
[source,makefile]
597
----
598
# User's application include folders (don't forget the '-I' before each entry)
599
APP_INC = -I . -I ../somewhere/include_stuff_folder
600
----
601
 
602
 
603
 
604
<<<
605
// ####################################################################################################################
606
:sectnums:
607
== Enabling RISC-V CPU Extensions
608
 
609 61 zero_gravi
Whenever you enable/disable a RISC-V CPU extensions via the according `CPU_EXTENSION_RISCV_x` generic, you need to
610 60 zero_gravi
adapt the toolchain configuration so the compiler can actually generate according code for it.
611
 
612
To do so, open the makefile of your project (for example `sw/example/blink_led/makefile`) and scroll to the
613 61 zero_gravi
"USER CONFIGURATION" section right at the beginning of the file. You need to modify the `MARCH` variable and eventually
614
the `MABI` variable according to your CPU hardware configuration.
615 60 zero_gravi
 
616
[source,makefile]
617
----
618
# CPU architecture and ABI
619
MARCH = -march=rv32i # <1>
620
MABI = -mabi=ilp32 # <2>
621
----
622
<1> MARCH = Machine architecture ("ISA string")
623
<2> MABI = Machine binary interface
624
 
625 61 zero_gravi
For example, if you enable the RISC-V `C` extension (16-bit compressed instructions) via the `CPU_EXTENSION_RISCV_C`
626 62 zero_gravi
generic (set `true`) you need to add the `c` extension also to the `MARCH` ISA string in order to make the compiler
627 61 zero_gravi
emit compressed instructions.
628 60 zero_gravi
 
629 62 zero_gravi
.Privileged Architecture Extensions
630
[IMPORTANT]
631
Privileged architecture extensions like `Zicsr` or `Zifencei` are "used" _implicitly_ by the compiler. Hence, according
632
instruction will only be generated when "encoded" via inline assembly or when linking according libraries. In this case,
633
these instruction will _always_ be emitted (even if the according extension is not specified in `MARCH`). +
634
**I recommend to _not_ specify any privileged architecture extensions in `MARCH`.**
635
 
636 61 zero_gravi
[WARNING]
637
ISA extension enabled in hardware can be a superset of the extensions enabled in software, but not the other way
638
around. For example generating compressed instructions for a CPU configuration that has the `c` extension disabled
639
will cause _illegal instruction exceptions_ at runtime.
640 60 zero_gravi
 
641 61 zero_gravi
You can also override the default `MARCH` and `MABI` configurations from the makefile when invoking the makefile:
642
 
643 60 zero_gravi
[source,bash]
644
----
645
$ make MARCH=-march=rv32ic clean_all all
646
----
647
 
648
[NOTE]
649 62 zero_gravi
The RISC-V ISA string for `MARCH` follows a certain _canonical_ structure:
650
`rev32[i/e][m][a][f][d][g][q][c][b][v][n]...` For example `rv32imac` is valid while `rv32icma` is not.
651 60 zero_gravi
 
652
 
653
 
654
<<<
655
// ####################################################################################################################
656
:sectnums:
657 63 zero_gravi
== Application-Specific Processor Configuration
658
 
659
Due to the processor's configuration options, which are mainly defined via the top entity VHDL generics, the SoC
660
can be tailored to the application-specific requirements. Note that this chapter does not focus on optional
661
_SoC features_ like IO/peripheral modules. It rather gives ideas on how to optimize for _overall goals_
662
like performance and area.
663
 
664
[NOTE]
665
Please keep in mind that optimizing the design in one direction (like performance) will also effect other potential
666
optimization goals (like area and energy).
667
 
668
=== Optimize for Performance
669
 
670
The following points show some concepts to optimize the processor for performance regardless of the costs
671
(i.e. increasing area and energy requirements):
672
 
673
* Enable all performance-related RISC-V CPU extensions that implement dedicated hardware accelerators instead
674
of emulating operations entirely in software:  `M`, `C`, `Zfinx`
675
* Enable mapping of compleX CPU operations to dedicated hardware: `FAST_MUL_EN => true` to use DSP slices for
676
multiplications, `FAST_SHIFT_EN => true` use a fast barrel shifter for shift operations.
677
* Implement the instruction cache: `ICACHE_EN => true`
678
* Use as many _internal_ memory as possible to reduce memory access latency: `MEM_INT_IMEM_EN => true` and
679
`MEM_INT_DMEM_EN => true`, maximize `MEM_INT_IMEM_SIZE` and `MEM_INT_DMEM_SIZE`
680
* Increase the CPU's instruction prefetch buffer size: `CPU_IPB_ENTRIES`
681
* _To be continued..._
682
 
683
 
684
=== Optimize for Size
685
 
686
The NEORV32 is a size-optimized processor system that is intended to fit into tiny niches within large SoC
687
designs or to be used a customized microcontroller in really tiny / low-power FPGAs (like Lattice iCE40).
688
Here are some ideas how to make the processor even smaller while maintaining it's _general purpose system_
689
concept and maximum RISC-V compatibility.
690
 
691
**SoC**
692
 
693
* This is obvious, but exclude all unused optional IO/peripheral modules from synthesis via the processor
694
configuration generics.
695
* If an IO module provides an option to configure the number of "channels", constrain this number to the
696
actually required value (e.g. the PWM module `IO_PWM_NUM_CH` or the external interrupt controller `XIRQ_NUM_CH`).
697
* Reduce the FIFO sizes of implemented modules (e.g. `SLINK_TX_FIFO`).
698
* Disable the instruction cache (`ICACHE_EN => false`) if the design only uses processor-internal IMEM
699
and DMEM memories.
700
* _To be continued..._
701
 
702
**CPU**
703
 
704
* Use the _embedded_ RISC-V CPU architecture extension (`CPU_EXTENSION_RISCV_E`) to reduce block RAM utilization.
705
* The compressed instructions extension (`CPU_EXTENSION_RISCV_C`) requires additional logic for the decoder but
706
also reduces program code size by approximately 30%.
707
* If not explicitly used/required, constrain the CPU's counter sizes: `CPU_CNT_WIDTH` for `[m]instret[h]`
708
(number of instruction) and `[m]cycle[h]` (number of cycles) counters. You can even remove these counters
709
by setting `CPU_CNT_WIDTH => 0` if they are not used at all (note, this is not RISC-V compliant).
710
* Reduce the CPU's prefetch buffer size (`CPU_IPB_ENTRIES`).
711
* Map CPU shift operations to a small and iterative shifter unit (`FAST_SHIFT_EN => false`).
712
* If you have unused DSP block available, you can map multiplication operations to those slices instead of
713
using LUTs to implement the multiplier (`FAST_MUL_EN => true`).
714
* If there is no need to execute division in hardware, use the `Zmmul` extension instead of the full-scale
715
`M` extension.
716
* Disable CPU extension that are not explicitly used (`A`, `U`, `Zfinx`).
717
* _To be continued..._
718
 
719
=== Optimize for Clock Speed
720
 
721
The NEORV32 Processor and CPU are designed to provide minimal logic between register stages to keep the
722
critical path as short as possible. When enabling additional extension or modules the impact on the existing
723
logic is also kept at a minimum to prevent timing degrading. If there is a major impact on existing
724
logic (example: many physical memory protection address configuration registers) the VHDL code automatically
725
adds additional register stages to maintain critical path length. Obviously, this increases operation latency.
726
 
727
In order to optimize for a minimal critical path (= maximum clock speed) the following points should be considered:
728
 
729
* Complex CPU extensions (in terms of hardware requirements) should be avoided (examples: floating-point unit, physical memory protection).
730
* Large carry chains (>32-bit) should be avoided (constrain CPU counter sizes: e.g. `CPU_CNT_WIDTH => 32` and `HPM_NUM_CNTS => 32`).
731
* If the target FPGA provides sufficient DSP resources, CPU multiplication operations can be mapped to DSP slices (`FAST_MUL_EN => true`)
732
reducing LUT usage and critical path impact while also increasing overall performance.
733
* Use the synchronous (registered) RX path configuration of the external memory interface (`MEM_EXT_ASYNC_RX => false`).
734
* _To be continued..._
735
 
736
[NOTE]
737
The short and fixed-length critical path allows to integrate the core into existing clock domains.
738
So no clock domain-crossing and no sub-clock generation is required. However, for very high clock
739
frequencies (this is technology / platform dependent) clock domain crossing becomes crucial for chip-internal
740
connections.
741
 
742
 
743
=== Optimize for Energy
744
 
745
There are no _dedicated_ configuration options to optimize the processor for energy (minimal consumption;
746
energy/instruction ratio) yet. However, a reduced processor area (<<_optimize_for_size>>) will also reduce
747
static energy consumption.
748
 
749
To optimize your setup for low-power applications, you can make use of the CPU sleep mode (`wfi` instruction).
750
Put the CPU to sleep mode whenever possible. Disable all processor modules that are not actually used (exclude them
751
from synthesis if the will be _never_ used; disable the module via it's control register if the module is not
752
_currently_ used). When is sleep mode, you can keep a timer module running (MTIME or the watch dog) to wake up
753
the CPU again. Since the wake up is triggered by _any_ interrupt, the external interrupt controller can also
754
be used to wake up the CPU again. By this, all timers (and all other modules) can be deactivated as well.
755
 
756
.Processor-internal clock generator shutdown
757
[TIP]
758
If _no_ IO/peripheral module is currently enabled, the processor's internal clock generator circuit will be
759
shut down reducing switching activity and thus, dynamic energy consumption.
760
 
761
 
762
 
763
<<<
764
// ####################################################################################################################
765
:sectnums:
766 64 zero_gravi
== Adding Custom Hardware Modules
767
 
768
In resemblance to the RISC-V ISA, the NEORV32 processor was designed to ease customization and _extensibility_.
769
The processor provides several predefined options to add application-specific custom hardware modules and accelerators.
770
 
771
 
772
=== Standard (_External_) Interfaces
773
 
774
The processor already provides a set of standard interfaces that are intended to connect _chip-external_ devices.
775
However, these interfaces can also be used chip-internally. The most suitable interfaces are
776
https://stnolting.github.io/neorv32/#_general_purpose_input_and_output_port_gpio[GPIO],
777
https://stnolting.github.io/neorv32/#_primary_universal_asynchronous_receiver_and_transmitter_uart0[UART],
778
https://stnolting.github.io/neorv32/#_serial_peripheral_interface_controller_spi[SPI] and
779
https://stnolting.github.io/neorv32/#_two_wire_serial_interface_controller_twi[TWI].
780
 
781
The SPI and (especially) the GPIO interfaces might be the most straightforward approaches since they
782
have a minimal  protocol overhead. Device-specific interrupt capabilities can be added using the
783
https://stnolting.github.io/neorv32/#_external_interrupt_controller_xirq[External Interrupt Controller (XIRQ)].
784
Beyond simplicity, these interface only provide a very limited bandwidth and require more sophisticated
785
software handling ("bit-banging" for the GPIO).
786
 
787
 
788
=== External Bus Interface
789
 
790
The https://stnolting.github.io/neorv32/#_processor_external_memory_interface_wishbone_axi4_lite[External Bus Interface]
791
provides the classic approach to connect to custom IP. By default, the bus interface implements the widely adopted
792
Wishbone interface standard. However, this project also includes wrappers to bridge to other protocol standards like ARM's
793
AXI4-Lite or Intel's Avalon. By using a full-featured bus protocol, complex SoC structures can be implemented (including
794
several modules and even multi-core architectures). Many FPGA EDA tools provide graphical editors to build and customize
795
whole SoC architectures and even include pre-defined IP libraries.
796
 
797
.Example AXI SoC using Xilinx Vivado
798
image::neorv32_axi_soc.png[]
799
 
800
The bus interface uses a memory-mapped approach. All data transfers are handled by simple load/store operations since the
801
external bus interface is mapped into the processor's https://stnolting.github.io/neorv32/#_address_space[address space].
802
This allows a very simple still high-bandwidth communications.
803
 
804
 
805
=== Stream Link Interface
806
 
807
The NEORV32 https://stnolting.github.io/neorv32/#_stream_link_interface_slink[Stream Link Interface] provides
808
point-to-point, unidirectional and parallel data channels that can be used to transfer streaming data. In
809
contrast to the external bus interface, the streaming data does not provide any kind of "direction" control,
810
so it can be seen as "constant address bursts". The stream link interface provides less protocol overhead
811
and less latency than the bus interface. Furthermore, FIFOs can be be configured to each direction (RX/TX) to
812
allow more CPU-independent operation.
813
 
814
 
815
=== Custom Functions Subsystem
816
 
817
The https://stnolting.github.io/neorv32/#_custom_functions_subsystem_cfs[NEORV32 Custom Functions Subsystem]
818
is as "empty" template for a processor-internal module. It provides 32 32-bit memory-mapped interface
819
registers that can be used to communicate with any arbitrary custom design logic. The intentions of this
820
subsystem is to provide a simple base, where the user can concentrate on implementing the actual design logic
821
rather than taking care of the communication between the CPU/software and the design logic. The interface
822
registers are already allocated within the processor's address space and are supported by the software framework
823
via low-level hardware access mechanisms. Additionally, the CFS provides a direct pre-defined interrupt channel to
824
the CPU, which is also supported by the _NEORV32 runtime environment_.
825
 
826
 
827
 
828
<<<
829
// ####################################################################################################################
830
:sectnums:
831 61 zero_gravi
== Customizing the Internal Bootloader
832 60 zero_gravi
 
833 61 zero_gravi
The NEORV32 bootloader provides several options to configure and customize it for a certain application setup.
834
This configuration is done by passing _defines_ when compiling the bootloader. Of course you can also
835
modify to bootloader source code to provide a setup that perfectly fits your needs.
836 60 zero_gravi
 
837 61 zero_gravi
[IMPORTANT]
838
Each time the bootloader sources are modified, the bootloader has to be re-compiled (and re-installed to the
839
bootloader ROM) and the processor has to be re-synthesized.
840 60 zero_gravi
 
841 61 zero_gravi
[NOTE]
842
Keep in mind that the maximum size for the bootloader is limited to 32kB and should be compiled using the
843
base ISA `rv32i` only to ensure it can work independently of the actual CPU configuration.
844 60 zero_gravi
 
845 61 zero_gravi
.Bootloader configuration parameters
846
[cols="<2,^1,^2,<6"]
847
[options="header", grid="rows"]
848
|=======================
849
| Parameter | Default | Legal values | Description
850
4+^| Serial console interface
851
| `UART_EN`   | `1` | `0`, `1` | Set to `0` to disable UART0 (no serial console at all)
852
| `UART_BAUD` | `19200` | _any_ | Baud rate of UART0
853
4+^| Status LED
854
| `STATUS_LED_EN`  | `1` | `0`, `1` | Enable bootloader status led ("heart beat") at `GPIO` output port pin #`STATUS_LED_PIN` when `1`
855
| `STATUS_LED_PIN` | `0` | `0` ... `31` | `GPIO` output pin used for the high-active status LED
856
4+^| Boot configuration
857
| `AUTO_BOOT_SPI_EN`  | `0` | `0`, `1` | Set `1` to enable immediate boot from external SPI flash
858
| `AUTO_BOOT_OCD_EN`  | `0` | `0`, `1` | Set `1` to enable boot via on-chip debugger (OCD)
859
| `AUTO_BOOT_TIMEOUT` | `8` | _any_ | Time in seconds after the auto-boot sequence starts (if there is no UART input by user); set to 0 to disabled auto-boot sequence
860
4+^| SPI configuration
861 63 zero_gravi
| `SPI_EN`                | `1` | `0`, `1` | Set `1` to enable the usage of the SPI module (including load/store executables from/to SPI flash options)
862 61 zero_gravi
| `SPI_FLASH_CS`          | `0` | `0` ... `7` | SPI chip select output (`spi_csn_o`) for selecting flash
863
| `SPI_FLASH_SECTOR_SIZE` | `65536` | _any_ | SPI flash sector size in bytes
864
| `SPI_FLASH_CLK_PRSC`    | `CLK_PRSC_8`  | `CLK_PRSC_2` `CLK_PRSC_4` `CLK_PRSC_8` `CLK_PRSC_64` `CLK_PRSC_128` `CLK_PRSC_1024` `CLK_PRSC_2024` `CLK_PRSC_4096` | SPI clock pre-scaler (dividing main processor clock)
865
| `SPI_BOOT_BASE_ADDR`    | `0x08000000` | _any_ 32-bit value | Defines the _base_ address of the executable in external flash
866
|=======================
867 60 zero_gravi
 
868 61 zero_gravi
Each configuration parameter is implemented as C-language `define` that can be manually overridden (_redefined_) when
869
invoking the bootloader's makefile. The according parameter and its new value has to be _appended_
870 64 zero_gravi
(using `+=`) to the makefile `USER_FLAGS` variable. Make sure to use the `-D` prefix here.
871 60 zero_gravi
 
872 61 zero_gravi
For example, to configure a UART Baud rate of 57600 and redirecting the status LED to output pin 20
873
use the following command (_in_ the bootloader's source folder `sw/bootloader`):
874 60 zero_gravi
 
875 61 zero_gravi
.Example: customizing, re-compiling and re-installing the bootloader
876
[source,console]
877 60 zero_gravi
----
878 61 zero_gravi
$ make USER_FLAGS+=-DUART_BAUD=57600 USER_FLAGS+=-DSTATUS_LED_PIN=20 clean_all bootloader
879 60 zero_gravi
----
880
 
881 61 zero_gravi
[NOTE]
882
The `clean_all` target ensure that all libraries are re-compiled. The `bootloader` target will automatically
883
compile and install the bootloader to the HDL boot ROM (updating `rtl/core/neorv32_bootloader_image.vhd`).
884 60 zero_gravi
 
885 61 zero_gravi
:sectnums:
886
=== Bootloader Boot Configuration
887 60 zero_gravi
 
888 61 zero_gravi
The bootloader provides several _boot configurations_ that define where the actual application's executable
889
shall be fetched from. Note that the non-default boot configurations provide a smaller memory footprint
890
reducing boot ROM implementation costs.
891 60 zero_gravi
 
892 61 zero_gravi
:sectnums!:
893
==== Default Boot Configuration
894 60 zero_gravi
 
895 61 zero_gravi
The _default_ bootloader configuration provides a UART-based user interface that allows to upload new executables
896
at any time. Optionally, the executable can also be programmed to an external SPI flash by the bootloader (see
897
section <<_programming_an_external_spi_flash_via_the_bootloader>>).
898 60 zero_gravi
 
899 61 zero_gravi
This configuration also provides an _automatic boot sequence_ (auto-boot) which will start fetching an executable
900
from external SPI flash using the default SPI configuration. By this, the default bootloader configuration
901
provides a "non volatile program storage" mechanism that automatically boot from external SPI flash
902
(after `AUTO_BOOT_TIMEOUT`) while still providing the option to re-program SPI flash at any time
903
via the UART interface.
904 60 zero_gravi
 
905 61 zero_gravi
:sectnums!:
906
==== `AUTO_BOOT_SPI_EN`
907 60 zero_gravi
 
908 61 zero_gravi
The automatic boot from SPI flash (enabled when `AUTO_BOOT_SPI_EN` is `1`) will fetch an executable from an external
909
SPI flash (using the according _SPI configuration_) right after reset. The bootloader will start fetching
910
the image at SPI flash base address `SPI_BOOT_BASE_ADDR`.
911 60 zero_gravi
 
912 61 zero_gravi
Note that there is _no_ UART console to interact with the bootloader. However, this boot configuration will
913
output minimal status messages via UART (if `UART_EN` is `1`).
914 60 zero_gravi
 
915 61 zero_gravi
:sectnums!:
916
==== `AUTO_BOOT_OCD_EN`
917 60 zero_gravi
 
918 61 zero_gravi
If `AUTO_BOOT_OCD_EN` is `1` the bootloader is implemented as minimal "halt loop" to be used with the on-chip debugger.
919
After initializing the hardware, the CPU waits in this endless loop until the on-chip debugger takes control over
920
the core (to upload and run the actual executable). See section <<_debugging_using_the_on_chip_debugger>>
921
for more information on how to use the on-chip debugger to upload and run executables.
922 60 zero_gravi
 
923 61 zero_gravi
[NOTE]
924
All bootloader boot configuration support uploading new executables via the on-chip debugger.
925 60 zero_gravi
 
926 61 zero_gravi
[WARNING]
927
Note that this boot configuration does not load any executable at all! Hence,
928 62 zero_gravi
this boot configuration is intended to be used with the on-chip debugger only.
929 60 zero_gravi
 
930
 
931
 
932 61 zero_gravi
<<<
933
// ####################################################################################################################
934
:sectnums:
935
== Programming an External SPI Flash via the Bootloader
936 60 zero_gravi
 
937 61 zero_gravi
The default processor-internal NEORV32 bootloader supports automatic booting from an external SPI flash.
938
This guide shows how to write an executable to the SPI flash via the bootloader so it can be automatically
939
fetched and executed after processor reset. For example, you can use a section of the FPGA bitstream configuration
940
memory to store an application executable.
941 60 zero_gravi
 
942 61 zero_gravi
[NOTE]
943
This section assumes the _default_ configuration of the NEORV32 bootloader.
944
See section <<_customizing_the_internal_bootloader>> on how to customize the bootloader and its setting
945
(for example the SPI chip-select port, the SPI clock speed or the flash base address for storing the executable).
946 60 zero_gravi
 
947
 
948 61 zero_gravi
:sectnums:
949
=== SPI Flash
950 60 zero_gravi
 
951 61 zero_gravi
The bootloader can access an SPI compatible flash via the processor top entity's SPI port. By default, the flash
952
chip-select line is to `spi_csn_o(0)` and uses 1/8 of the processor's main clock as clock frequency.
953
The SPI flash has to support single-byte read and write, 24-bit addresses and at least the following standard commands:
954 60 zero_gravi
 
955 61 zero_gravi
* READ `0x03`
956
* READ STATUS `0x05`
957
* WRITE ENABLE `0x06`
958
* PAGE PROGRAM `0x02`
959
* SECTOR ERASE `0xD8`
960
* READ ID `0x9E`
961 60 zero_gravi
 
962 61 zero_gravi
Compatible (FGPA configuration) SPI flash memories are for example the "Winbond W25Q64FV2 or the "Micron N25Q032A".
963 60 zero_gravi
 
964
 
965
:sectnums:
966 61 zero_gravi
=== Programming an Executable
967 60 zero_gravi
 
968
[start=1]
969
. At first, reset the NEORV32 processor and wait until the bootloader start screen appears in your terminal program.
970
. Abort the auto boot sequence and start the user console by pressing any key.
971 61 zero_gravi
. Press u to upload the executable that you want to store to the external flash:
972 60 zero_gravi
 
973
[source]
974
----
975
CMD:> u
976
Awaiting neorv32_exe.bin...
977
----
978
 
979
[start=4]
980 61 zero_gravi
. Send the binary in raw binary via your terminal program. When the upload is completed and "OK"
981 60 zero_gravi
appears, press `p` to trigger the programming of the flash (do not execute the image via the `e`
982
command as this might corrupt the image):
983
 
984
[source]
985
----
986
CMD:> u
987
Awaiting neorv32_exe.bin... OK
988
CMD:> p
989
Write 0x000013FC bytes to SPI flash @ 0x00800000? (y/n)
990
----
991
 
992
[start=5]
993
. The bootloader shows the size of the executable and the base address inside the SPI flash where the
994
executable is going to be stored. A prompt appears: Type `y` to start the programming or type `n` to
995 61 zero_gravi
abort.
996 60 zero_gravi
 
997 61 zero_gravi
[TIP]
998
Section <<_customizing_the_internal_bootloader>> show the according C-language `define` that can be modified
999
to specify the base address of the executable inside the SPI flash.
1000
 
1001 60 zero_gravi
[source]
1002
----
1003
CMD:> u
1004
Awaiting neorv32_exe.bin... OK
1005
CMD:> p
1006 61 zero_gravi
Write 0x000013FC bytes to SPI flash @ 0x08000000? (y/n) y
1007 60 zero_gravi
Flashing... OK
1008
CMD:>
1009
----
1010
 
1011
[start=6]
1012
. If "OK" appears in the terminal line, the programming process was successful. Now you can use the
1013
auto boot sequence to automatically boot your application from the flash at system start-up without
1014
any user interaction.
1015
 
1016
 
1017
 
1018
<<<
1019
// ####################################################################################################################
1020
:sectnums:
1021 61 zero_gravi
== Packaging the Processor as IP block for Xilinx Vivado Block Designer
1022
 
1023 62 zero_gravi
[start=1]
1024 64 zero_gravi
. Import all the core files from `rtl/core` (including default internal memory architectures from `rtl/core/mem`)
1025
and assign them to a _new_ design library `neorv32`.
1026 62 zero_gravi
. Instantiate the `rtl/wrappers/neorv32_top_axi4lite.vhd` module.
1027
. Then either directly use that module in a new block-design ("Create Block Design", right-click -> "Add Module",
1028
thats easier for a first try) or package it ("Tools", "Create and Package new IP") for the use in other projects.
1029
. Connect your AXI-peripheral directly to the core's AXI4-Interface if you only have one, or to an AXI-Interconnect
1030
(from the IP-catalog) if you have multiple peripherals.
1031
. Connect ALL the `ACLK` and `ARESETN` pins of all peripherals and interconnects to the processor's clock and reset
1032
signals to have a _unified_ clock and reset domain (easier for a first setup).
1033
. Open the "Address Editor" tab and let Vivado assign the base-addresses for the AXI-peripherals (you can modify them
1034
according to your needs).
1035
. For all FPGA-external signals (like UART signals) make all the connections you need "external"
1036
(right-click on the signal/pin -> "Make External").
1037
. Save everything, let VIVADO create a HDL-Wrapper for the block-design and choose this as your _Top Level Design_.
1038
. Define your constraints and generate your bitstream.
1039 61 zero_gravi
 
1040 62 zero_gravi
[NOTE]
1041
Guide provided by GitHub user https://github.com/AWenzel83[`AWenzel83`] from
1042
https://github.com/stnolting/neorv32/discussions/52#discussioncomment-819013
1043 61 zero_gravi
 
1044
 
1045 62 zero_gravi
 
1046 61 zero_gravi
<<<
1047
// ####################################################################################################################
1048
:sectnums:
1049 60 zero_gravi
== Simulating the Processor
1050
 
1051 64 zero_gravi
The NEORV32 project includes a core CPU, built-in peripherals in the Processor Subsystem, and additional peripherals in
1052
the templates and examples.
1053
Therefore, there is a wide range of possible testing and verification strategies.
1054
 
1055
On the one hand, a simple smoke testbench allows ensuring that functionality is correct from a software point of view.
1056
That is used for running the RISC-V architecture tests, in order to guarantee compliance with the ISA specification(s).
1057
 
1058
On the other hand, http://vunit.github.io/[VUnit] and http://vunit.github.io/verification_components/user_guide.html[Verification Components] are used for verifying the functionality of the various peripherals from a hardware point of view.
1059
 
1060 61 zero_gravi
:sectnums:
1061
=== Testbench
1062
 
1063 64 zero_gravi
A plain-VHDL (no third-party libraries) testbench (`sim/simple/neorv32_tb.simple.vhd`) can be used for simulating and
1064
testing the processor.
1065
This testbench features a 100MHz clock and enables all optional peripheral and CPU extensions except for the `E`
1066
extension and the TRNG IO module (that CANNOT be simulated due to its combinatorial (looped) architecture).
1067 60 zero_gravi
 
1068
The simulation setup is configured via the "User Configuration" section located right at the beginning of
1069
the testbench's architecture. Each configuration constant provides comments to explain the functionality.
1070
 
1071
Besides the actual NEORV32 Processor, the testbench also simulates "external" components that are connected
1072
to the processor's external bus/memory interface. These components are:
1073
 
1074
* an external instruction memory (that also allows booting from it)
1075
* an external data memory
1076
* an external memory to simulate "external IO devices"
1077
* a memory-mapped registers to trigger the processor's interrupt signals
1078
 
1079
The following table shows the base addresses of these four components and their default configuration and
1080 64 zero_gravi
properties:
1081 60 zero_gravi
 
1082 64 zero_gravi
[NOTE]
1083
====
1084
Attributes:
1085
 
1086
* `r` = read
1087
* `w` = write
1088
* `e` = execute
1089
* `a` = atomic accesses possible
1090
* `8` = byte-accessible
1091
* `16` = half-word-accessible
1092
* `32` = word-accessible
1093
====
1094
 
1095 60 zero_gravi
.Testbench: processor-external memories
1096
[cols="^4,>3,^5,<11"]
1097
[options="header",grid="rows"]
1098
|=======================
1099
| Base address | Size          | Attributes           | Description
1100
| `0x00000000` | `imem_size_c` | `r/w/e,  a, 8/16/32` | external IMEM (initialized with application image)
1101
| `0x80000000` | `dmem_size_c` | `r/w/e,  a, 8/16/32` | external DMEM
1102
| `0xf0000000` |      64 bytes | `r/w/e, !a, 8/16/32` | external "IO" memory, atomic accesses will fail
1103
| `0xff000000` |       4 bytes | `-/w/-,  a,  -/-/32` | memory-mapped register to trigger "machine external", "machine software" and "SoC Fast Interrupt" interrupts
1104
|=======================
1105
 
1106 64 zero_gravi
[IMPORTANT]
1107 63 zero_gravi
The simulated NEORV32 does not use the bootloader and _directly boots_ the current application image (from
1108
the `rtl/core/neorv32_application_image.vhd` image file).
1109 60 zero_gravi
 
1110 63 zero_gravi
.UART output during simulation
1111 64 zero_gravi
[IMPORTANT]
1112 60 zero_gravi
Data written to the NEORV32 UART0 / UART1 transmitter is send to a virtual UART receiver implemented
1113
as part of the testbench. Received chars are send to the simulator console and are also stored to a log file
1114 63 zero_gravi
(`neorv32.testbench_uart0.out` for UART0, `neorv32.testbench_uart1.out` for UART1) inside the simulation's home folder.
1115
**Please note that printing via the native UART receiver takes a lot of time.** For faster simulation console output
1116
see section <<_faster_simulation_console_output>>.
1117 60 zero_gravi
 
1118
 
1119 61 zero_gravi
:sectnums:
1120
=== Faster Simulation Console Output
1121
 
1122 60 zero_gravi
When printing data via the UART the communication speed will always be based on the configured BAUD
1123
rate. For a simulation this might take some time. To have faster output you can enable the **simulation mode**
1124 64 zero_gravi
for UART0/UART1 (see section https://stnolting.github.io/neorv32/#_primary_universal_asynchronous_receiver_and_transmitter_uart0[Documentation: Primary Universal Asynchronous Receiver and Transmitter (UART0)]).
1125 60 zero_gravi
 
1126 64 zero_gravi
ASCII data sent to UART0|UART1 will be immediately printed to the simulator console and logged to files in the simulator
1127
execution directory:
1128 60 zero_gravi
 
1129 64 zero_gravi
* `neorv32.uart?.sim_mode.text.out`: ASCII data.
1130
* `neorv32.uart?.sim_mode.data.out`: all written 32-bit dumped as 8-char hexadecimal values.
1131 60 zero_gravi
 
1132 64 zero_gravi
You can "automatically" enable the simulation mode of UART0/UART1 when compiling an application.
1133
In this case, the "real" UART0/UART1 transmitter unit is permanently disabled.
1134
To enable the simulation mode just compile and install your application and add _UART?_SIM_MODE_ to the compiler's
1135
_USER_FLAGS_ variable (do not forget the `-D` suffix flag):
1136 60 zero_gravi
 
1137
[source, bash]
1138
----
1139
sw/example/blink_led$ make USER_FLAGS+=-DUART0_SIM_MODE clean_all all
1140
----
1141
 
1142 63 zero_gravi
The provided define will change the default UART0/UART1 setup function in order to set the simulation
1143
mode flag in the according UART's control register.
1144 60 zero_gravi
 
1145
[NOTE]
1146
The UART simulation output (to file and to screen) outputs "complete lines" at once. A line is
1147
completed with a line feed (newline, ASCII `\n` = 10).
1148
 
1149
 
1150 61 zero_gravi
:sectnums:
1151 64 zero_gravi
=== Simulation using a shell script (with GHDL)
1152 60 zero_gravi
 
1153 64 zero_gravi
To simulate the processor using _GHDL_ navigate to the `sim/simple/` folder and run the provided shell script.
1154 61 zero_gravi
Any arguments that are provided while executing this script are passed to GHDL.
1155
For example the simulation time can be set to 20ms using `--stop-time=20ms` as argument.
1156 60 zero_gravi
 
1157
[source, bash]
1158
----
1159 64 zero_gravi
neorv32/sim/simple$ sh ghdl_sim.sh --stop-time=20ms
1160 60 zero_gravi
----
1161
 
1162
 
1163 63 zero_gravi
:sectnums:
1164 64 zero_gravi
=== Simulation using Application Makefiles (In-Console with GHDL)
1165 60 zero_gravi
 
1166 63 zero_gravi
To directly compile and run a program in the console (using the default testbench and GHDL
1167
as simulator) you can use the `sim` makefile target. Make sure to use the UART simulation mode
1168
(`USER_FLAGS+=-DUART0_SIM_MODE` and/or `USER_FLAGS+=-DUART1_SIM_MODE`) to get
1169
faster / direct-to-console UART output.
1170
 
1171
[source, bash]
1172
----
1173
sw/example/blink_led$ make USER_FLAGS+=-DUART0_SIM_MODE clean_all sim
1174
[...]
1175
Blinking LED demo program
1176
----
1177
 
1178
 
1179
:sectnums:
1180 64 zero_gravi
==== Hello World!
1181 63 zero_gravi
 
1182 64 zero_gravi
To do a quick test of the NEORV32 make sure to have https://github.com/ghdl/ghdl[GHDL] and a
1183
[RISC-V gcc toolchain](https://github.com/stnolting/riscv-gcc-prebuilt) installed.
1184
Navigate to the project's `sw/example/hello_world` folder and run `make USER_FLAGS+=-DUART0_SIM_MODE MARCH=-march=rv32imac clean_all sim`:
1185 63 zero_gravi
 
1186
[TIP]
1187
The simulator will output some _sanity check_ notes (and warnings or even errors if something is ill-configured)
1188
right at the beginning of the simulation to give a brief overview of the actual NEORV32 SoC and CPU configurations.
1189
 
1190
[source, bash]
1191
----
1192
stnolting@Einstein:/mnt/n/Projects/neorv32/sw/example/hello_world$ make USER_FLAGS+=-DUART0_SIM_MODE MARCH=-march=rv32imac clean_all sim
1193
../../../sw/lib/source/neorv32_uart.c: In function 'neorv32_uart0_setup':
1194
../../../sw/lib/source/neorv32_uart.c:301:4: warning: #warning UART0_SIM_MODE (primary UART) enabled! Sending all UART0.TX data to text.io simulation output instead of real UART0 transmitter. Use this for simulations only! [-Wcpp]
1195 64 zero_gravi
  301 |   #warning UART0_SIM_MODE (primary UART) enabled! Sending all UART0.TX data to text.io simulation output instead of real UART0 transmitter. Use this for simulations only! <1>
1196 63 zero_gravi
      |    ^~~~~~~
1197
Memory utilization:
1198
   text    data     bss     dec     hex filename
1199 64 zero_gravi
   4612       0     120    4732    127c main.elf <2>
1200 63 zero_gravi
Compiling ../../../sw/image_gen/image_gen
1201 64 zero_gravi
Installing application image to ../../../rtl/core/neorv32_application_image.vhd <3>
1202 63 zero_gravi
Simulating neorv32_application_image.vhd...
1203 64 zero_gravi
Tip: Compile application with USER_FLAGS+=-DUART[0/1]_SIM_MODE to auto-enable UART[0/1]'s simulation mode (redirect UART output to simulator console). <4>
1204
Using simulation runtime args: --stop-time=10ms <5>
1205
../rtl/core/neorv32_top.vhd:347:3:@0ms:(assertion note): NEORV32 PROCESSOR IO Configuration: GPIO MTIME UART0 UART1 SPI TWI PWM WDT CFS SLINK NEOLED XIRQ <6>
1206 63 zero_gravi
../rtl/core/neorv32_top.vhd:370:3:@0ms:(assertion note): NEORV32 PROCESSOR CONFIG NOTE: Boot configuration: Direct boot from memory (processor-internal IMEM).
1207
../rtl/core/neorv32_top.vhd:394:3:@0ms:(assertion note): NEORV32 PROCESSOR CONFIG NOTE: Implementing on-chip debugger (OCD).
1208
../rtl/core/neorv32_cpu.vhd:169:3:@0ms:(assertion note): NEORV32 CPU ISA Configuration (MARCH): RV32IMACU_Zbb_Zicsr_Zifencei_Zfinx_Debug
1209
../rtl/core/neorv32_cpu.vhd:189:3:@0ms:(assertion note): NEORV32 CPU CONFIG NOTE: Implementing NO dedicated hardware reset for uncritical registers (default, might reduce area). Set package constant  = TRUE to configure a DEFINED reset value for all CPU registers.
1210
../rtl/core/neorv32_imem.vhd:107:3:@0ms:(assertion note): NEORV32 PROCESSOR CONFIG NOTE: Implementing processor-internal IMEM as ROM (16384 bytes), pre-initialized with application (4612 bytes).
1211
../rtl/core/neorv32_dmem.vhd:89:3:@0ms:(assertion note): NEORV32 PROCESSOR CONFIG NOTE: Implementing processor-internal DMEM (RAM, 8192 bytes).
1212
../rtl/core/neorv32_wishbone.vhd:136:3:@0ms:(assertion note): NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing STANDARD Wishbone protocol.
1213
../rtl/core/neorv32_wishbone.vhd:140:3:@0ms:(assertion note): NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing auto-timeout (255 cycles).
1214
../rtl/core/neorv32_wishbone.vhd:144:3:@0ms:(assertion note): NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing LITTLE-endian byte order.
1215
../rtl/core/neorv32_wishbone.vhd:148:3:@0ms:(assertion note): NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing registered RX path.
1216
../rtl/core/neorv32_slink.vhd:161:3:@0ms:(assertion note): NEORV32 PROCESSOR CONFIG NOTE: Implementing 8 RX and 8 TX stream links.
1217 64 zero_gravi
<7>
1218 63 zero_gravi
                                                                                       ##
1219
                                                                                       ##         ##   ##   ##
1220
 ##     ##   #########   ########    ########   ##      ##   ########    ########      ##       ################
1221
####    ##  ##          ##      ##  ##      ##  ##      ##  ##      ##  ##      ##     ##     ####            ####
1222
## ##   ##  ##          ##      ##  ##      ##  ##      ##          ##         ##      ##       ##   ######   ##
1223
##  ##  ##  #########   ##      ##  #########   ##      ##      #####        ##        ##     ####   ######   ####
1224
##   ## ##  ##          ##      ##  ##    ##     ##    ##           ##     ##          ##       ##   ######   ##
1225
##    ####  ##          ##      ##  ##     ##     ##  ##    ##      ##   ##            ##     ####            ####
1226
##     ##    #########   ########   ##      ##      ##       ########   ##########     ##       ################
1227
                                                                                       ##         ##   ##   ##
1228
                                                                                       ##
1229
Hello world! :)
1230
----
1231 64 zero_gravi
<1> Notifier that "simulation mode" of UART0 is enabled (by the `USER_FLAGS+=-DUART0_SIM_MODE` makefile flag). All UART0 output is send to the simulator console.
1232
<2> Final executable size (`text`) and _static_ data memory requirements (`data`, `bss`).
1233
<3> The application code is _installed_ as pre-initialized IMEM. This is the default approach for simulation.
1234
<4> A note regarding UART "simulation mode", but we have already enabled that.
1235
<5> List of (default) arguments that were send to the simulator. Here: maximum simulation time (10ms).
1236
<6> "Sanity checks" from the core's VHDL files. These reports give some brief information about the SoC/CPU configuration (-> generics). If there are problems with the current configuration, an ERROR will appear.
1237
<7> Execution of the actual program starts.
1238 63 zero_gravi
 
1239
 
1240
:sectnums:
1241 64 zero_gravi
=== Advanced Simulation using VUnit
1242 63 zero_gravi
 
1243 64 zero_gravi
https://vunit.github.io/[VUnit] is an open source unit testing framework for VHDL/SystemVerilog.
1244
It allows continuous and automated testing of HDL code by complementing traditional testing methodologies.
1245
The motto of VUnit is _"testing early and often"_ through automation.
1246 63 zero_gravi
 
1247 64 zero_gravi
VUnit is composed by a http://vunit.github.io/py/ui.html[Python interface] and multiple optional
1248
http://vunit.github.io/vhdl_libraries.html[VHDL libraries].
1249
The Python interface allows declaring sources and simulation options, and it handles the compilation, execution and
1250
gathering of the results regardless of the simulator used.
1251
That allows having a single `run.py` script to be used with GHDL, ModelSim/QuestaSim, Riviera PRO, etc.
1252
On the other hand, the VUnit's VHDL libraries provide utilities for assertions, logging, having virtual queues, handling CSV files, etc.
1253
The http://vunit.github.io/verification_components/user_guide.html[Verification Component Library] uses those features
1254
for abstracting away bit-toggling when verifying standard interfaces such as Wishbone, AXI, Avalon, UARTs, etc.
1255 63 zero_gravi
 
1256 64 zero_gravi
Testbench sources in `sim` (such as `sim/neorv32_tb.vhd` and `sim/uart_rx*.vhd`) use VUnit's VHDL libraries for testing
1257
NEORV32 and peripherals.
1258
The entrypoint for executing the tests is `sim/run.py`.
1259 63 zero_gravi
 
1260 64 zero_gravi
[source, bash]
1261
----
1262
# ./sim/run.py -l
1263
neorv32.neorv32_tb.all
1264
Listed 1 tests
1265 63 zero_gravi
 
1266 64 zero_gravi
# ./sim/run.py -v
1267
Compiling into neorv32:   rtl/core/neorv32_uart.vhd                                                                                            passed
1268
Compiling into neorv32:   rtl/core/neorv32_twi.vhd                                                                                             passed
1269
Compiling into neorv32:   rtl/core/neorv32_trng.vhd                                                                                            passed
1270
...
1271
----
1272 63 zero_gravi
 
1273 64 zero_gravi
See http://vunit.github.io/user_guide.html[VUnit: User Guide] and http://vunit.github.io/cli.html[VUnit: Command Line Interface] for further info about VUnit's features.
1274
 
1275
 
1276 60 zero_gravi
<<<
1277
// ####################################################################################################################
1278
:sectnums:
1279
== Building the Documentation
1280
 
1281 61 zero_gravi
The documentation (datasheet + user guide) is written using `asciidoc`. The according source files
1282
can be found in `docs/...`. The documentation of the software framework is written _in-code_ using `doxygen`.
1283 60 zero_gravi
 
1284 62 zero_gravi
A makefiles in the project's `docs` directory is provided to build all of the documentation as HTML pages
1285 60 zero_gravi
or as PDF documents.
1286
 
1287
[TIP]
1288 61 zero_gravi
Pre-rendered PDFs are available online as _nightly pre-releases_: https://github.com/stnolting/neorv32/releases.
1289 60 zero_gravi
The HTML-based documentation is also available online at the project's https://stnolting.github.io/neorv32/[GitHub Pages].
1290
 
1291
The makefile provides a help target to show all available build options and their according outputs.
1292
 
1293
[source,bash]
1294
----
1295 62 zero_gravi
neorv32/docs$ make help
1296 60 zero_gravi
----
1297
 
1298
.Example: Generate HTML documentation (data sheet) using `asciidoctor`
1299
[source,bash]
1300
----
1301 62 zero_gravi
neorv32/docs$ make html
1302 60 zero_gravi
----
1303
 
1304
[TIP]
1305
If you don't have `asciidoctor` / `asciidoctor-pdf` installed, you can still generate all the documentation using
1306
a _docker container_ via `make container`.
1307
 
1308
 
1309
 
1310
<<<
1311
// ####################################################################################################################
1312
:sectnums:
1313
== FreeRTOS Support
1314
 
1315
A NEORV32-specific port and a simple demo for FreeRTOS (https://github.com/FreeRTOS/FreeRTOS) are
1316 61 zero_gravi
available in the `sw/example/demo_freeRTOS` folder. See the according documentation (`sw/example/demo_freeRTOS/README.md`)
1317
for more information.
1318 60 zero_gravi
 
1319
 
1320
 
1321
// ####################################################################################################################
1322
:sectnums:
1323
== RISC-V Architecture Test Framework
1324
 
1325
The NEORV32 Processor passes the according tests provided by the official RISC-V Architecture Test Suite
1326
(V2.0+), which is available online at GitHub: https://github.com/riscv/riscv-arch-test
1327
 
1328
All files required for executing the test framework on a simulated instance of the processor (including port
1329 62 zero_gravi
files) are located in the `sw/isa-test` folder of the NEORV32 repository. The test framework is executed via the
1330
`sim/run_riscv_arch_test.sh` script. Take a look at the provided `sim/README.md`
1331
(https://github.com/stnolting/neorv32/tree/master/sim[online at GitHub])
1332 60 zero_gravi
file for more information on how to run the tests and how testing is conducted in detail.
1333
 
1334
 
1335
 
1336
<<<
1337
// ####################################################################################################################
1338
:sectnums:
1339
== Debugging using the On-Chip Debugger
1340
 
1341 61 zero_gravi
The NEORV32 on-chip debugger allows _online_ in-system debugging via an external JTAG access port from a
1342 60 zero_gravi
host machine. The general flow is independent of the host machine's operating system. However, this tutorial uses
1343
Windows and Linux (Ubuntu on Windows) in parallel.
1344
 
1345 61 zero_gravi
[TIP]
1346
See datasheet section https://stnolting.github.io/neorv32/#_on_chip_debugger_ocd[On Chip Debugger (OCD)]
1347
for more information.
1348
 
1349 60 zero_gravi
[NOTE]
1350
This tutorial uses `gdb` to **directly upload an executable** to the processor. If you are using the default
1351
processor setup _with_ internal instruction memory (IMEM) make sure it is implemented as RAM
1352 61 zero_gravi
(_INT_BOOTLOADER_EN_ generic = true).
1353 60 zero_gravi
 
1354 64 zero_gravi
[IMPORTANT]
1355
The on-chip debugger is only implemented if the _ON_CHIP_DEBUGGER_EN_ generic is set _true_. Furthermore, it requires
1356
the `Zicsr` and `Zifencei` CPU extension to be implemented (top generics _CPU_EXTENSION_RISCV_Zicsr_
1357
and _CPU_EXTENSION_RISCV_Zifencei_ = true).
1358 60 zero_gravi
 
1359 64 zero_gravi
 
1360 60 zero_gravi
:sectnums:
1361
=== Hardware Requirements
1362
 
1363
Make sure the on-chip debugger of your NEORV32 setups is implemented (_ON_CHIP_DEBUGGER_EN_ generic = true).
1364
Connect a JTAG adapter to the NEORV32 `jtag_*` interface signals. If you do not have a full-scale JTAG adapter, you can
1365
also use a FTDI-based adapter like the "FT2232H-56Q Mini Module", which is a simple and inexpensive FTDI breakout board.
1366
 
1367
.JTAG pin mapping
1368
[cols="^3,^2,^2"]
1369
[options="header",grid="rows"]
1370
|=======================
1371
| NEORV32 top signal | JTAG signal | FTDI port
1372
| `jtag_tck_i`       | TCK         | D0
1373
| `jtag_tdi_i`       | TDI         | D1
1374
| `jtag_tdo_o`       | TDO         | D2
1375
| `jtag_tms_i`       | TMS         | D3
1376
| `jtag_trst_i`      | TRST        | D4
1377
|=======================
1378
 
1379
[TIP]
1380
The low-active JTAG _test reset_ (TRST) signals is _optional_ as a reset can also be triggered via the TAP controller.
1381
If TRST is not used make sure to pull the signal _high_.
1382
 
1383
 
1384
:sectnums:
1385
=== OpenOCD
1386
 
1387
The NEORV32 on-chip debugger can be accessed using the https://github.com/riscv/riscv-openocd[RISC-V port of OpenOCD].
1388
Prebuilt binaries can be obtained - for example - from https://www.sifive.com/software[SiFive]. A pre-configured
1389
OpenOCD configuration file (`sw/openocd/openocd_neorv32.cfg`) is available that allows easy access to the NEORV32 CPU.
1390
 
1391
[NOTE]
1392
You might need to adapt `ftdi_vid_pid`, `ftdi_channel` and `ftdi_layout_init` in `sw/openocd/openocd_neorv32.cfg`
1393
according to your interface chip and your operating system.
1394
 
1395
[TIP]
1396
If you want to modify the JTAG clock speed (via `adapter speed` in `sw/openocd/openocd_neorv32.cfg`) make sure to meet
1397
the clock requirements noted in https://stnolting.github.io/neorv32/#_debug_module_dm[Documentation: Debug Transport Module (DTM)].
1398
 
1399
To access the processor using OpenOCD, open a terminal and start OpenOCD with the pre-configured configuration file.
1400
 
1401
.Connecting via OpenOCD (on Windows)
1402
[source, bash]
1403
--------------------------
1404
N:\Projects\neorv32\sw\openocd>openocd -f openocd_neorv32.cfg
1405
Open On-Chip Debugger 0.11.0-rc1+dev (SiFive OpenOCD 0.10.0-2020.12.1)
1406
Licensed under GNU GPL v2
1407
For bug reports:
1408
        https://github.com/sifive/freedom-tools/issues
1409
1
1410
Info : Listening on port 6666 for tcl connections
1411
Info : Listening on port 4444 for telnet connections
1412
Info : clock speed 1000 kHz
1413
Info : JTAG tap: neorv32.cpu tap/device found: 0x0cafe001 (mfg: 0x000 (), part: 0xcafe, ver: 0x0)
1414
Info : datacount=1 progbufsize=2
1415
Info : Disabling abstract command reads from CSRs.
1416
Info : Examined RISC-V core; found 1 harts
1417
Info :  hart 0: XLEN=32, misa=0x40801105
1418
Info : starting gdb server for neorv32.cpu.0 on 3333
1419
Info : Listening on port 3333 for gdb connections
1420
--------------------------
1421
 
1422
OpenOCD has successfully connected to the NEORV32 on-chip debugger and has examined the CPU (showing the content of
1423
the `misa` CSRs). Now you can use `gdb` to connect via port 3333.
1424
 
1425
 
1426
:sectnums:
1427
=== Debugging with GDB
1428
 
1429
This guide uses the simple "blink example" from `sw/example/blink_led` as simplified test application to
1430
show the basics of in-system debugging.
1431
 
1432
At first, the application needs to be compiled. We will use the minimal machine architecture configuration
1433
(`rv32i`) here to be independent of the actual processor/CPU configuration.
1434
Navigate to `sw/example/blink_led` and compile the application:
1435
 
1436
.Compile the test application
1437
[source, bash]
1438
--------------------------
1439 64 zero_gravi
.../neorv32/sw/example/blink_led$ make MARCH=-march=rv32i USER_FLAGS+=-g clean_all all
1440 60 zero_gravi
--------------------------
1441
 
1442 64 zero_gravi
.Adding debug symbols to the executable
1443
[NOTE]
1444
`USER_FLAGS+=-g` passes the `-g` flag to the compiler so it adds debug information/symbols
1445
to the generated ELF file. This is optional but will provide more sophisticated information for debugging
1446
(like source file line numbers).
1447
 
1448 60 zero_gravi
This will generate an ELF file `main.elf` that contains all the symbols required for debugging.
1449
Furthermore, an assembly listing file `main.asm` is generated that we will use to define breakpoints.
1450
 
1451
Open another terminal in `sw/example/blink_led` and start `gdb`.
1452 61 zero_gravi
The GNU debugger is part of the toolchain (see <<_software_toolchain_setup>>).
1453 60 zero_gravi
 
1454
.Starting GDB (on Linux (Ubuntu on Windows))
1455
[source, bash]
1456
--------------------------
1457
.../neorv32/sw/example/blink_led$ riscv32-unknown-elf-gdb
1458
GNU gdb (GDB) 10.1
1459
Copyright (C) 2020 Free Software Foundation, Inc.
1460
License GPLv3+: GNU GPL version 3 or later 
1461
This is free software: you are free to change and redistribute it.
1462
There is NO WARRANTY, to the extent permitted by law.
1463
Type "show copying" and "show warranty" for details.
1464
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=riscv32-unknown-elf".
1465
Type "show configuration" for configuration details.
1466
For bug reporting instructions, please see:
1467
.
1468
Find the GDB manual and other documentation resources online at:
1469
    .
1470
 
1471
For help, type "help".
1472
Type "apropos word" to search for commands related to "word".
1473
(gdb)
1474
--------------------------
1475
 
1476 64 zero_gravi
Now connect to OpenOCD using the default port 3333 on your machine.
1477
We will use the previously generated ELF file `main.elf` from the `blink_led` example.
1478
Finally, upload the program to the processor and start debugging.
1479 60 zero_gravi
 
1480
[NOTE]
1481
The executable that is uploaded to the processor is **not** the default NEORV32 executable (`neorv32_exe.bin`) that
1482
is used for uploading via the bootloader. Instead, all the required sections (like `.text`) are extracted from `mail.elf`
1483
by GDB and uploaded via the debugger's indirect memory access.
1484
 
1485
.Running GDB
1486
[source, bash]
1487
--------------------------
1488 64 zero_gravi
(gdb) target extended-remote localhost:3333 <1>
1489 60 zero_gravi
Remote debugging using localhost:3333
1490
warning: No executable has been specified and target does not support
1491
determining executable automatically.  Try using the "file" command.
1492
0xffff0c94 in ?? () <2>
1493
(gdb) file main.elf <3>
1494
A program is being debugged already.
1495
Are you sure you want to change the file? (y or n) y
1496
Reading symbols from main.elf...
1497
(gdb) load <4>
1498
Loading section .text, size 0xd0c lma 0x0
1499
Loading section .rodata, size 0x39c lma 0xd0c
1500
Start address 0x00000000, load size 4264
1501
Transfer rate: 43 KB/sec, 2132 bytes/write.
1502
(gdb)
1503
--------------------------
1504
<1> Connect to OpenOCD
1505
<2> The CPU was still executing code from the bootloader ROM - but that does not matter here
1506
<3> Select `mail.elf` from the `blink_led` example
1507
<4> Upload the executable
1508
 
1509
After the upload, GDB will make the processor jump to the beginning of the uploaded executable
1510
(by default, this is the beginning of the instruction memory at `0x00000000`) skipping the bootloader
1511
and halting the CPU right before executing the `blink_led` application.
1512
 
1513
 
1514
:sectnums:
1515
==== Breakpoint Example
1516
 
1517
The following steps are just a small showcase that illustrate a simple debugging scheme.
1518
 
1519
While compiling `blink_led`, an assembly listing file `main.asm` was generated.
1520
Open this file with a text editor to check out what the CPU is going to do when resumed.
1521
 
1522
The `blink_led` example implements a simple counter on the 8 lowest GPIO output ports. The program uses
1523
"busy wait" to have a visible delay between increments. This waiting is done by calling the `neorv32_cpu_delay_ms`
1524
function. We will add a _breakpoint_ right at the end of this wait function so we can step through the iterations
1525
of the counter.
1526
 
1527
.Cut-out from `main.asm` generated from the `blink_led` example
1528
[source, assembly]
1529
--------------------------
1530
00000688 <__neorv32_cpu_delay_ms_end>:
1531
 688:   01c12083                lw      ra,28(sp)
1532
 68c:   02010113                addi    sp,sp,32
1533
 690:   00008067                ret
1534
--------------------------
1535
 
1536
The very last instruction of the `neorv32_cpu_delay_ms` function is `ret` (= return)
1537
at hexadecimal `690` in this example. Add this address as _breakpoint_ to GDB.
1538
 
1539
[NOTE]
1540
The address might be different if you use a different version of the software framework or
1541
if different ISA options are configured.
1542
 
1543
.Adding a GDB breakpoint
1544
[source, bash]
1545
--------------------------
1546
(gdb) b * 0x690
1547
Breakpoint 1 at 0x690
1548
--------------------------
1549
 
1550 64 zero_gravi
.How do breakpoints work?
1551
[TIP]
1552
The NEORV32 on-chip debugger does not provide any hardware breakpoints (RISC-V "trigger modules") that compare an address like the PC
1553
with a predefined value. Instead, gdb will modify the actual executable in IMEM: the actual instruction at the address
1554
of the specified breakpoint is replaced by a `break` / `c.break` instruction. Whenever execution reaches this instruction, debug mode is
1555
re-entered and the debugger restores the original instruction at this address to maintain original program behavior.
1556
 
1557 60 zero_gravi
Now execute `c` (= continue). The CPU will resume operation until it hits the break-point.
1558
By this we can "step" from increment to increment.
1559
 
1560
.Iterating from breakpoint to breakpoint
1561
[source, bash]
1562
--------------------------
1563
Breakpoint 1 at 0x690
1564
(gdb) c
1565
Continuing.
1566
 
1567
Breakpoint 1, 0x00000690 in neorv32_cpu_delay_ms ()
1568
(gdb) c
1569
Continuing.
1570
 
1571
Breakpoint 1, 0x00000690 in neorv32_cpu_delay_ms ()
1572
(gdb) c
1573
Continuing.
1574
--------------------------
1575
 
1576
include::../legal.adoc[]

powered by: WebSVN 2.1.0

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