blob: b883ec624208d8e0d1b0c492fe9e1d5e11736122 [file] [log] [blame] [view]
Garret Kelly9eebde02019-10-22 15:36:49 -04001---
Timothy Trippel92bbc7b2021-07-12 22:59:01 +00002title: "Getting Started on FPGAs"
Garret Kelly9eebde02019-10-22 15:36:49 -04003---
4
Pirmin Vogel6ae95502021-05-19 19:09:03 +02005Do you want to try out OpenTitan, but don't have a couple thousand or million dollars ready for an ASIC tapeout?
6Running OpenTitan on an FPGA board can be the answer!
lowRISC Contributors802543a2019-08-31 12:12:56 +01007
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -06008<!-- TODO: Switch all calls to fusesoc and the Verilated system to refer to Meson, once it supports fusesoc. -->
9
lowRISC Contributors802543a2019-08-31 12:12:56 +010010## Prerequisites
11
Pirmin Vogel6ae95502021-05-19 19:09:03 +020012To use the OpenTitan on an FPGA you need two things:
lowRISC Contributors802543a2019-08-31 12:12:56 +010013
14* A supported FPGA board
15* A tool from the FPGA vendor
16
17Depending on the design/target combination that you want to synthesize you will need different tools and boards.
18Refer to the design documentation for information what exactly is needed.
19
Garret Kelly9eebde02019-10-22 15:36:49 -040020* [Obtain an FPGA board]({{< relref "fpga_boards.md" >}})
lowRISC Contributors802543a2019-08-31 12:12:56 +010021
Garret Kelly9eebde02019-10-22 15:36:49 -040022Follow the install instructions to [prepare the system]({{< relref "install_instructions#system-preparation" >}}) and to install the [software development tools]({{< relref "install_instructions#software-development" >}}) and [Xilinx Vivado]({{< relref "install_instructions#xilinx-vivado" >}}).
lowRISC Contributors802543a2019-08-31 12:12:56 +010023
24## Create an FPGA bitstream
25
Pirmin Vogel6ae95502021-05-19 19:09:03 +020026Synthesizing a design for an FPGA board is done with the following commands.
lowRISC Contributors802543a2019-08-31 12:12:56 +010027
Timothy Chen25b0c662019-10-17 11:06:24 -070028The FPGA build will pull in a program to act as the boot ROM.
Greg Chadwick567cfd62019-10-31 16:15:33 +000029This must be built before running the FPGA build.
Pirmin Vogel6ae95502021-05-19 19:09:03 +020030This is pulled in from the `sw/device/boot_rom` directory (see the `parameters:` section of the `hw/top_earlgrey/chip_earlgrey_cw310.core` file).
Greg Chadwick567cfd62019-10-31 16:15:33 +000031
32To build it:
33```console
34$ cd $REPO_TOP
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060035$ ./meson_init.sh
Michael Schaffner076450b2021-02-23 18:16:21 -080036$ ninja -C build-out all
Greg Chadwick567cfd62019-10-31 16:15:33 +000037```
38
Pirmin Vogelb41684f2021-07-20 09:49:50 +020039Only the ChipWhisperer CW310 board with the Xilinx Kintex 7 XC7K410T FPGA can fit the whole Earl Grey design.
40When working with the Nexys Video FPGA board, the Earl Grey design has to be modified to reduce its size using a script.
Timothy Chenf835aa32021-01-08 16:27:50 -080041```console
42$ cd $REPO_TOP
Pirmin Vogelde154682021-02-01 11:40:01 +010043$ ./hw/top_earlgrey/util/top_earlgrey_reduce.py --build
Timothy Chenf835aa32021-01-08 16:27:50 -080044```
Pirmin Vogelde154682021-02-01 11:40:01 +010045The `--build` argument is optional and ensures that the boot ROM is rebuilt for the reduced design.
46Alternatively, the boot ROM can be manually regenerated using the previous command.
Timothy Chenf835aa32021-01-08 16:27:50 -080047
Pirmin Vogelb41684f2021-07-20 09:49:50 +020048Next, the actual FPGA implementation can be started.
Pirmin Vogel6ae95502021-05-19 19:09:03 +020049In the following example we synthesize the Earl Grey design for the ChipWhisperer CW310 board using Xilinx Vivado {{< tool_version "vivado" >}}.
50To target the Nexys Video board, replace `cw310` by `nexysvideo` in the instructions below.
lowRISC Contributors802543a2019-08-31 12:12:56 +010051
52```console
Pirmin Vogel37ee04a2021-05-25 13:38:27 +020053$ . /tools/xilinx/Vivado/{{< tool_version "vivado" >}}/settings64.sh
lowRISC Contributors802543a2019-08-31 12:12:56 +010054$ cd $REPO_TOP
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060055$ ./meson_init.sh
Michael Schaffner076450b2021-02-23 18:16:21 -080056$ ninja -C build-out all
Pirmin Vogel6ae95502021-05-19 19:09:03 +020057$ fusesoc --cores-root . run --flag=fileset_top --target=synth lowrisc:systems:chip_earlgrey_cw310
lowRISC Contributors802543a2019-08-31 12:12:56 +010058```
Pirmin Vogelde154682021-02-01 11:40:01 +010059The `fileset_top` flag used above is specific to the OpenTitan project to select the correct fileset.
lowRISC Contributors802543a2019-08-31 12:12:56 +010060
Pirmin Vogel6ae95502021-05-19 19:09:03 +020061The resulting bitstream is located at `build/lowrisc_systems_chip_earlgrey_cw310_0.1/synth-vivado/lowrisc_systems_chip_earlgrey_cw310_0.1.bit`.
Garret Kelly9eebde02019-10-22 15:36:49 -040062See the [reference manual]({{< relref "ref_manual_fpga.md" >}}) for more information.
lowRISC Contributors802543a2019-08-31 12:12:56 +010063
Timothy Chen85d81922021-09-10 11:29:24 -070064### Dealing with FPGA Congestion Issues
65
66The default Vivado tool placement may sometimes result in congested FPGA floorplans.
67When this happens, the implemenation time and results become unpredictable.
68It may become necessary for the user to manually adjust certain placement.
69See [this comment](https://github.com/lowRISC/opentitan/pull/8138#issuecomment-916696830) for a thorough analysis of one such situation and what changes were made to improve congestion.
70
Pirmin Vogel6ae95502021-05-19 19:09:03 +020071## Connecting the ChipWhisperer CW310 board
72
73The ChipWhisperer CW310 board supports different power options.
74It is recommended to power the board via the included DC power adapter.
75To this end:
761. Set the *SW2* switch (to the right of the barrel connector) up to the *5V Regulator* option.
771. Set the switch below the barrel connector to the right towards the *Barrel* option.
781. Set the *Control Power* switch (bottom left corner, *SW7*) to the right.
791. Ensure the *Tgt Power* switch (above the fan, *S1*) is set to the right towards the *Auto* option.
801. Plug the DC power adapter into the barrel jack (*J11*) in the top left corner of the board.
811. Use a USB-C cable to connect your PC with the *USB-C Data* connector (*J8*) in the lower left corner on the board.
82
83You can now use `dmesg` to determine which serial ports have been assigned.
84They should be named `'/dev/ttyACM*'`, e.g. `/dev/ttyACM1`.
85To ensure that you have sufficient access permissions, set up the udev rules as explained in the [Vivado installation instructions]({{< relref "install_instructions#xilinx-vivado" >}}).
86
87## Connecting the Nexys Video board
Tom Roberts0ab9e7e2019-11-04 13:59:22 +000088
89* Use a Micro USB cable to connect the PC with the *PROG*-labeled connector on the board.
90* Use a second Micro USB cable to connect the PC with the *UART*-labled connector on the board.
Pirmin Vogel6ae95502021-05-19 19:09:03 +020091* After connecting the UART, use `dmesg` to determine which serial port was assigned.
92 It should be named `/dev/ttyUSB*`, e.g. `/dev/ttyUSB0`.
93* To ensure that you have sufficient access permissions, set up the udev rules as explained in the [Vivado installation instructions]({{< relref "install_instructions#xilinx-vivado" >}}).
lowRISC Contributors802543a2019-08-31 12:12:56 +010094
95## Flash the bitstream onto the FPGA
96
Pirmin Vogel6ae95502021-05-19 19:09:03 +020097To flash the bitstream onto the FPGA you need to use either the command line or the Vivado GUI (Nexys Video board only).
98Depending on the FPGA device, the flashing itself may take several seconds.
99After completion, a message like this should be visible from the UART:
Satnam Singh145884f2019-12-13 15:17:30 -0800100
101```
102Version: opentitan-snapshot-20191101-1-366-gca61d28
103Build Date: 2019-12-13, 13:15:48
104Bootstrap requested, initialising HW...
105HW initialisation completed, waiting for SPI input...
106```
107
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200108### Using the command line for the ChipWhisperer CW310 board
109
110Use the following command to program the FPGA with the `cw310_loader` tool.
111
112```console
113$ cd $REPO_TOP
114$ ./util/fpga/cw310_loader.py --bitstream build/lowrisc_systems_chip_earlgrey_cw310_0.1/synth-vivado/lowrisc_systems_chip_earlgrey_cw310_0.1.bit
115```
116
117### Using the command line for the Nexys Video board
118
119Use the following command to program the FPGA.
120
121```console
122$ . /tools/xilinx/Vivado/{{< tool_version "vivado" >}}/settings64.sh
123$ cd $REPO_TOP
124$ util/opentitan-pgm-fpga/opentitan-pgm-fpga xc7a200tsbg484-1 build/lowrisc_systems_chip_earlgrey_nexysvideo/synth-vivado/lowrisc_systems_chip_earlgrey_nexysvideo_0.1.bit
125```
126
Greg Chadwick567cfd62019-10-31 16:15:33 +0000127If you have having trouble with programming using the command line, try the GUI.
lowRISC Contributors802543a2019-08-31 12:12:56 +0100128
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200129### Using the Vivado GUI for the Nexys Video board
lowRISC Contributors802543a2019-08-31 12:12:56 +0100130
131```console
Pirmin Vogel37ee04a2021-05-25 13:38:27 +0200132$ . /tools/xilinx/Vivado/{{< tool_version "vivado" >}}/settings64.sh
lowRISC Contributors802543a2019-08-31 12:12:56 +0100133$ cd $REPO_TOP
Michael Schaffner93fe50c2021-03-31 16:25:42 -0700134$ make -C build/lowrisc_systems_chip_earlgrey_nexysvideo_0.1/synth-vivado build-gui
lowRISC Contributors802543a2019-08-31 12:12:56 +0100135```
136
137Now the Vivado GUI opens and loads the project.
138
139* Connect the FPGA board to the PC and turn it on.
140* In the navigation on the left, click on *PROGRAM AND DEBUG* > *Open Hardware Manager* > *Open Target* > *Auto Connect*.
Philipp Wagner1edca412020-12-07 18:36:08 +0000141* Vivado now enumerates all boards and connects to it.
lowRISC Contributors802543a2019-08-31 12:12:56 +0100142* Click on *Program Device* in the menu on the left (or at the top of the screen).
Michael Schaffner93fe50c2021-03-31 16:25:42 -0700143* A dialog titled *Program Device* pops up. Select the file `lowrisc_systems_chip_earlgrey_nexysvideo_0.1.bit` as *Bitstream file*, and leave the *Debug probes file* empty.
lowRISC Contributors802543a2019-08-31 12:12:56 +0100144* Click on *Program* to flash the FPGA with the bitstream.
145* The FPGA is ready as soon as the programming finishes.
146
lowRISC Contributors802543a2019-08-31 12:12:56 +0100147## Testing the demo design
148
Timothy Chend11f4b92019-10-21 12:53:45 -0700149The `hello_world` demo software shows off some capabilities of the design.
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200150Depending on the FPGA board, a slightly different set of tools and commands is used for running applications.
151
152### Running on the ChipWhisperer CW310 board
153
154To load `hello_world` into the FPGA on the ChipWhisperer CW310 board follow the steps shown below.
155
1561. Generate the bitstream and flash it to the FPGA as described above.
1571. Open a serial console (use the device file determined before) and connect.
158 Settings: 115200 baud, 8 bits per byte, no software flow-control for sending and receiving data.
159 ```console
160 $ screen /dev/ttyACM1 115200,cs8,-ixon,-ixoff
161 ```
1621. Run the loading tool.
163 ```console
164 $ cd ${REPO_TOP}
Alexander Williamsad7f46d2021-09-30 09:15:08 -0700165 $ ./util/fpga/cw310_loader.py --firmware build-bin/sw/device/examples/hello_world/hello_world_fpga_cw310.bin --set-pll-defaults
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200166 ```
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200167
168 This should report how the binary is split into frames:
169 ```
170 CW310 Loader: Attemping to find CW310 FPGA Board:
171 No bitstream specified
172 Board found, setting PLL2 to 100 MHz
Pirmin Vogelb41684f2021-07-20 09:49:50 +0200173 INFO: Programming firmware file: build-bin/sw/device/examples/hello_world/hello_world_fpga_cw310.bin
174 Programming OpenTitan with "build-bin/sw/device/examples/hello_world/hello_world_fpga_cw310.bin"...
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200175 Transferring frame 0x00000000 @ 0x00000000.
176 Transferring frame 0x00000001 @ 0x000007D8.
177 Transferring frame 0x00000002 @ 0x00000FB0.
178 Transferring frame 0x00000003 @ 0x00001788.
179 Transferring frame 0x00000004 @ 0x00001F60.
180 Transferring frame 0x00000005 @ 0x00002738.
181 Transferring frame 0x00000006 @ 0x00002F10.
182 Transferring frame 0x80000007 @ 0x000036E8.
183 Loading done.
184 ```
185
186 and then output like this should appear from the UART:
187 ```
188 Version: opentitan-snapshot-20191101-1-4549-g504534121
189 Build Date: 2021-03-02, 18:15:49
190 Bootstrap requested, initialising HW...
191 HW initialisation completed, waiting for SPI input...
192 Processing frame #0, expecting #0
193 Processing frame #1, expecting #1
194 Processing frame #2, expecting #2
195 Processing frame #3, expecting #3
196 Processing frame #4, expecting #4
197 Processing frame #5, expecting #5
198 Processing frame #6, expecting #6
199 Processing frame #7, expecting #7
200 Bootstrap: DONE!
201 Boot ROM initialisation has completed, jump into flash!
202 Hello World!
203 Built at: May 17 2021, 18:44:21
204 Watch the LEDs!
205 Try out the switches on the board
206 or type anything into the console window.
207 The LEDs show the ASCII code of the last character.
208 GPIO switch #0 changed to 1
209 GPIO switch #1 changed to 1
210 GPIO switch #2 changed to 1
211 GPIO switch #3 changed to 1
212 GPIO switch #4 changed to 1
213 GPIO switch #5 changed to 1
214 GPIO switch #6 changed to 1
215 GPIO switch #7 changed to 1
216 FTDI control changed. Enable JTAG.
217 ```
218
2191. Observe the output both on the board and the serial console. Type any text into the console window.
2201. Exit `screen` by pressing CTRL-a k, and confirm with y.
221
Jade Philipoom05d0d102021-11-29 14:58:16 +0000222#### Troubleshooting
223
224If the firmware load fails with a message like `Error transferring frame: 0x00000000`, try pressing the "USB RST" button before loading the bitstream.
225
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200226### Running on the Nexys Video board
227
228In order to load `hello_world` into the FPGA on the Nexys Video board, both the binary and the [loading tool]({{< relref "/sw/host/spiflash/README.md" >}}) must be compiled.
Satnam Singh145884f2019-12-13 15:17:30 -0800229Please follow the steps shown below.
Timothy Chen25b0c662019-10-17 11:06:24 -0700230
lowRISC Contributors802543a2019-08-31 12:12:56 +0100231* Generate the bitstream and flash it to the FPGA as described above.
232* Open a serial console (use the device file determined before) and connect.
Pirmin Vogel85206472020-10-01 13:02:22 +0200233 Settings: 115200 baud, 8N1, no hardware or software flow control.
lowRISC Contributors802543a2019-08-31 12:12:56 +0100234 ```console
Pirmin Vogel85206472020-10-01 13:02:22 +0200235 $ screen /dev/ttyUSB0 115200
lowRISC Contributors802543a2019-08-31 12:12:56 +0100236 ```
Pirmin Vogel85206472020-10-01 13:02:22 +0200237 Note that the Nexsys Video demo program that comes installed on the board runs the UART at 115200 baud as well;
238 expect to see different output if that is running.
Philipp Wagnerd1fec0a2019-10-30 22:57:28 +0000239 This can happen if you connect the serial console before using Vivado to program your new bitstream or you press the *PROG* button that causes the FPGA to reprogram from the code in the on-board SPI flash.
lowRISC Contributors802543a2019-08-31 12:12:56 +0100240* On the Nexys Video board, press the red button labeled *CPU_RESET*.
Tom Roberts0ab9e7e2019-11-04 13:59:22 +0000241* You should see the ROM code report its commit ID and build date.
242* Run the loading tool.
243 ```console
244 $ cd ${REPO_TOP}
Miguel Young de la Sota33d52d22019-12-09 09:15:06 -0600245 $ ./meson_init.sh
Miguel Young de la Sota76526c32020-01-28 10:24:41 -0500246 $ ninja -C build-out sw/device/examples/hello_world/hello_world_export_fpga_nexysvideo
247 $ ninja -C build-out sw/host/spiflash/spiflash_export
Michael Schaffner6c0679a2020-05-06 22:38:53 -0700248 $ build-bin/sw/host/spiflash/spiflash --input build-bin/sw/device/examples/hello_world/hello_world_fpga_nexysvideo.bin
Tom Roberts0ab9e7e2019-11-04 13:59:22 +0000249 ```
Satnam Singh145884f2019-12-13 15:17:30 -0800250
251 which should report how the binary is split into frames:
252
253 ```
254 Running SPI flash update.
255 Image divided into 6 frames.
256 frame: 0x00000000 to offset: 0x00000000
257 frame: 0x00000001 to offset: 0x000003d8
258 frame: 0x00000002 to offset: 0x000007b0
259 frame: 0x00000003 to offset: 0x00000b88
260 frame: 0x00000004 to offset: 0x00000f60
261 frame: 0x80000005 to offset: 0x00001338
262 ```
263
264 and then output like this should appear from the UART:
265 ```
266 Processing frame no: 00000000 exp no: 00000000
267 Processing frame no: 00000001 exp no: 00000001
268 Processing frame no: 00000002 exp no: 00000002
269 Processing frame no: 00000003 exp no: 00000003
270 Processing frame no: 00000004 exp no: 00000004
271 Processing frame no: 80000005 exp no: 00000005
272 bootstrap: DONE!
273 INFO: Boot ROM initialisation has completed, jump into flash!
274 Hello World! Dec 13 2019 15:06:29
275 Watch the LEDs!
276 Try out the switches on the board
277 or type anything into the console window.
278 The LEDs show the ASCII code of the last character.
279 GPIO: Switch 7 changed to 1
280 FTDI control changed. Enable JTAG
281 ```
282
lowRISC Contributors802543a2019-08-31 12:12:56 +0100283* Observe the output both on the board and the serial console. Type any text into the console window.
284* Exit `screen` by pressing CTRL-a k, and confirm with y.
285
286## Develop with the Vivado GUI
287
288Sometimes it is helpful to use the Vivado GUI to debug a design.
289fusesoc makes that easy, with one small caveat: by default fusesoc copies all source files into a staging directory before the synthesis process starts.
290This behavior is helpful to create reproducible builds and avoids Vivado modifying checked-in source files.
291But during debugging this behavior is not helpful.
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200292The `--no-export` option of fusesoc disables copying the source files into the staging area, and `--setup` instructs fusesoc to not start the synthesis process.
lowRISC Contributors802543a2019-08-31 12:12:56 +0100293
294```console
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200295$ # only create Vivado project directory
296$ cd $REPO_TOP
297$ fusesoc --cores-root . run --flag=fileset_top --target=synth --no-export --setup lowrisc:systems:chip_earlgrey_cw310
298```
299
300You can then navigate to the created project directory, and open Vivado
301```console
302$ . /tools/xilinx/Vivado/{{< tool_version "vivado" >}}/settings64.sh
303$ cd $REPO_TOP/build/lowrisc_systems_chip_earlgrey_cw310_0.1/synth-vivado/
304$ vivado
305```
306
307Finally, using the tcl console, you can kick off the project setup with
308```console
309$ source lowrisc_systems_chip_earlgrey_cw310_0.1.tcl
lowRISC Contributors802543a2019-08-31 12:12:56 +0100310```
311
312## Connect with OpenOCD and debug
313
Pirmin Vogel6ae95502021-05-19 19:09:03 +0200314To connect the Nexys Video FPGA board with OpenOCD, run the following command
lowRISC Contributors802543a2019-08-31 12:12:56 +0100315
316```console
317$ cd $REPO_TOP
Philipp Wagner4a7b5a02021-03-17 15:06:56 +0000318$ openocd -s util/openocd -f board/lowrisc-earlgrey-nexysvideo.cfg
lowRISC Contributors802543a2019-08-31 12:12:56 +0100319```
320
Philipp Wagner4a7b5a02021-03-17 15:06:56 +0000321See the [install instructions]({{< relref "install_instructions#openocd" >}}) for guidance on installing OpenOCD.
Alistair Francis3c3ee452019-11-07 13:19:07 -0800322
lowRISC Contributors802543a2019-08-31 12:12:56 +0100323To actually debug through OpenOCD, it must either be connected through telnet or GDB.
324
325### Debug with OpenOCD
326
327The following is an example for using telnet
328
329```console
330$ telnet localhost 4444 // or whatever port that is specificed by the openocd command above
331$ mdw 0x8000 0x10 // read 16 bytes at address 0x8000
332```
333
334### Debug with GDB
335
336An example connection with GDB, which prints the registers after the connection to OpenOCD is established
337
338```console
339$ cd $REPO_TOP
Tom Roberts0ab9e7e2019-11-04 13:59:22 +0000340$ /tools/riscv/bin/riscv32-unknown-elf-gdb -ex "target extended-remote :3333" -ex "info reg" sw/device/boot_rom/rom.elf
lowRISC Contributors802543a2019-08-31 12:12:56 +0100341```
342
343#### Common operations with GDB
344
345Examine 16 memory words in the hex format starting at 0x200005c0
346
347```console
348(gdb) x/16xw 0x200005c0
349```
350
351Press enter again to print the next 16 words.
352Use `help x` to get a description of the command.
353
354If the memory content contains program text it can be disassembled
355
356```console
357(gdb) disassemble 0x200005c0,0x200005c0+16*4
358```
359
360Displaying the memory content can also be delegated to OpenOCD
361
362```console
363(gdb) monitor mdw 0x200005c0 16
364```
365
366Use `monitor help` to get a list of supported commands.
367
Greg Chadwick567cfd62019-10-31 16:15:33 +0000368To single-step use `stepi` or `step`
369
370```console
371(gdb) stepi
372```
373
374`stepi` single-steps an instruction, `step` single-steps a line of source code.
375When testing debugging against the hello_world binary it is likely you will break into a delay loop.
376Here the `step` command will seem to hang as it will attempt to step over the whole delay loop with a sequence of single-step instructions which may take quite some time!
377
lowRISC Contributors802543a2019-08-31 12:12:56 +0100378To change the program which is debugged the `file` command can be used.
379This will update the symbols which are used to get information about the program.
Philipp Wagner544ac9b2019-10-30 22:56:32 +0000380It is especially useful in the context of our `rom.elf`, which resides in the ROM region, which will eventually jump to a different executable as part of the flash region.
lowRISC Contributors802543a2019-08-31 12:12:56 +0100381
382```console
Philipp Wagner544ac9b2019-10-30 22:56:32 +0000383(gdb) file sw/device/examples/hello_world/sw.elf
lowRISC Contributors802543a2019-08-31 12:12:56 +0100384(gdb) disassemble 0x200005c0,0x200005c0+16*4
385```
386
387The output of the disassemble should now contain additional information.