sync_up: Discussion, add rest of points from thread
[libreriscv.git] / HDL_workflow / microwatt.mdwn
1 # Tutorial for setting up Microwatt chroot and running simulations
2
3 Useful Links (External):
4
5 * <https://codeconstruct.com.au/docs/microwatt-orangecrab/>
6 * <https://shenki.github.io/boot-linux-on-microwatt/>
7 * <https://github.com/gregdavill/OrangeCrab-test-sw>
8 * [Verilator docs, commands](https://verilator.org/guide/latest/exe_verilator.html)
9 * [Verilator runtime command documentation](https://verilator.org/guide/latest/exe_sim.html)
10 * Tutorials for how to work with verilator:
11 [part1](https://www.itsembedded.com/dhd/verilator_1/),
12 [part2](https://www.itsembedded.com/dhd/verilator_2/)
13
14 Useful links (Libre-SOC):
15
16 * Libre-SOC page covering our workflow: [[HDL_workflow]]
17 * Devscripts Libre-SOC page: [[devscripts]]
18 * Original Microwatt Libre-SOC page: [[microwatt]]
19 * [Libre-SOC Microwatt repo branch](https://git.libre-soc.org/?p=microwatt.git;a=tree;hb=refs/heads/verilator_trace)
20 * [Libre-SOC devscripts repo](https://git.libre-soc.org/?p=dev-env-setup.git;a=tree)
21
22 Other Tutorials (Libre-SOC):
23
24 * First steps for working with PowerISA instructions Libre-SOC page:
25 [[/docs/firststeps]]
26
27 ## Video Tutorial
28
29 [43min tutorial](https://youtu.be/02LCl3ang8g) was made and uploaded to
30 Youtube, covering some of the material you'll find on this page.
31
32 ## Development environment scripts
33
34 If you haven't already, clone Libre-SOC's development environment setup scripts.
35 These are bash scripts, and greatly simplify the time it takes to create a:
36
37 - Stable environment
38 - With all software and libraries at specific versions
39 (which are known to work).
40
41 These attributes are absolutely critical, and no support will be
42 provided, unless you use these scripts to setup a development environment. This
43 helps us fix any bugs in the scripts, and make sure everyone runs on the same
44 page.
45
46 $ git clone https://git.libre-soc.org/git/dev-env-setup.git
47
48
49 Do *look through* the
50 [code](https://git.libre-soc.org/?p=dev-env-setup.git;a=tree) before running
51 any of those scripts.
52 This is your own legal responsibility (to not run
53 arbitrary code off of the internet) and we take no responsibility or accept
54 any liability whatsoever for your use or misuse of them.
55
56 It is expected for you to use Debian for the host OS (anything else
57 is unsupported: many contributors have repeatedly gotten into trouble by not
58 following this advice), while all the chroots - which are developed
59 very specifically for "reproducible builds" - run Debian 10 (Buster).
60
61 ## Setting up chroot
62
63 Scripts we will be using for the setup are:
64
65 * `mk-deb-chroot`, `cp-scripts-to-chroot` for chroot setup
66 * `install-hdl-apt-reqs`, `verilator-install`, `hdl-tools-yosys` for working
67 with Microwatt
68
69 (*Current limitation for `mk-deb-chroot`, is that you must be the first user on
70 the host machine, having user ID 1000.*)
71
72 Commands to run in terminal to setup a new chroot environment for microwatt
73 simulations.
74
75 $ cd dev-env-setup
76 $ sudo bash
77 # ./mk-deb-chroot microwatt
78 # ./cp-scripts-to-chroot microwatt
79 # exit
80 $ schroot -c microwatt
81 (microwatt):$ cd dev-env-setup
82 (microwatt):$ sudo bash
83 (microwatt):# ./install-hdl-apt-reqs
84 (microwatt):# ./verilator-install
85 (microwatt):# ./hdl-tools-yosys
86 (microwatt):# exit
87 (microwatt):$ cd ~/src/
88 (microwatt):$ git clone https://git.libre-soc.org/git/microwatt.git
89 (microwatt):$ cd microwatt
90 (microwatt):$ git checkout verilator_trace
91
92 Make sure verilator binaries in $PATH:
93
94 (microwatt):$ export PATH=/usr/local/verilator/bin:$PATH
95 (microwatt):$ export GHDLSYNTH=ghdl
96
97 (GHDLSYNTH needs to be redefined because the Makefile has default `ghdl.so`,
98 but somewhere else '.so' gets appended. You may see the following error if you
99 don't redefine:
100 `ERROR: Can't load module
101 ./ghdl.so':/usr/local/bin/../share/yosys/plugins/**ghdl.so.so**`)
102 [IRC](https://libre-soc.org/irclog/%23libre-soc.2023-01-25.log.html#t2023-01-25T11:10:47)
103
104 ## Compiling the verilator sim for Microwatt
105
106 * [Libre-SOC Microwatt repo branch, Makefile](https://git.libre-soc.org/?p=microwatt.git;a=blob;f=Makefile;hb=refs/heads/verilator_trace)
107
108 Verilator creates a fairly fast simulation by converting the HDL design to C++,
109 and then compiling a binary which the user runs.
110
111 To compile the verilator simulation, first set verilator as the target for the
112 makefile:
113
114 (microwatt):$ export FPGA_TARGET=verilator
115
116 Before compiling, you can change the `THREADS` variable in the makefile, which
117 will allow the compiled verilator simulation binary to use more than 1 thread
118 (*make sure to check how many CPU threads you have before changing this!*)
119
120 To compile the verilator simulation binary, call make with the
121 `microwatt-verilator` rule.
122
123 (microwatt):$ make microwatt-verilator
124
125 ## Compiling hello world code
126
127 We need some code to actually run on the core, so start with the 'hello world'.
128 Instructions assume you're still in the microwatt directory.
129
130 (microwatt):$ cd hello_world
131 (microwatt):$ make
132
133 A `hello_world.bin` should be generated (the final binary to be loaded), as
134 well as an
135 [.elf file](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format), and
136 .hex (representing the binary data as hex text strings).
137
138 To view the symbol table (useful to see where various sections of the binary
139 begin):
140
141 (microwatt):$ powerpc64le-linux-gnu-objdump -h hello_world.elf
142 (microwatt):$ powerpc64le-linux-gnu-objdump -x hello_world.elf
143
144 `-h` shows just the section headers, `-x` shows all headers.
145
146 And to view the disassembly (great for learning about the PowerISA instructions,
147 and for associating the binary hex with actual instructions), you can view the
148 automatically generated `hello_world.as` file.
149
150 Command to generate the disassembly:
151
152 (microwatt):$ powerpc64le-linux-gnu-objdump -D hello_world.elf
153
154 For more information about `objdump` (common utility, not just for PowerISA),
155 see the manual pages.
156
157 (microwatt):$ man powerpc64le-linux-gnu-objdump
158
159 The binary is ready to go, now it can be loaded into the simulation.
160
161 ## Simulation
162
163 ### Command line args
164
165 To find out the `microwatt-verilator` arguments, you can check with `-h` arg:
166
167 (microwatt):$ ./microwatt-verilator -h
168
169 Some of the arguments are explained in further sections.
170
171 ### Running
172
173 Run the `microwatt-verilator` binary, with `hello_world/hello_world.bin` as an
174 argument:
175
176 (microwatt):$ time ./microwatt-verilator hello_world/hello_world.bin
177
178 `time` is a utility you can use to measure how long it takes to run the sim.
179
180 A pretty ASCII art of a lightbulb should be printed, and then the user can type
181 any characters, which will be echoed back. To end the simulation press Ctrl+C.
182
183 If no characters are appearing after about 20 seconds, stop the simulation,
184 as there might be other issues.
185
186 Single-threaded verilator sim binary, on a 2nd gen intel i5 (sandybridge)
187 takes 53 seconds to print the ASCII lightbulb.
188
189 On another dev's machine, ASUS KGPE D16, this takes just over a minute.
190
191 (*You'll find that uart printout is one of the longer parts of the simulation
192 in general.*)
193
194 ## Analysing results after simulation
195
196 The following files will be generated during the sim:
197
198 - `bram.dump` - Shows the PC address and instruction being executed. If the sim
199 hangs without any printing, view this file, as the processor may have hit an
200 exception etc. Grows in size as the sim runs.
201
202 - `bram.snapshot.[NUMBER]`, `verilator.save.[NUMBER]` - Snapshot files of the
203 contents of bram and verilator model respectively. Can be used to resume the
204 simulation. The number on the end corresponds to the tick time (i.e.
205 `bram.snapshot.1999990`/`verilator.save.1999990`). First the verilator model is
206 loaded, and then the bram contents are loaded. See lines `#65-108` and
207 `#189-195` of the
208 [microwatt-verilator.cpp file](https://git.libre-soc.org/?p=microwatt.git;a=blob;f=verilator/microwatt-verilator.cpp;h=a226393f6ba74d5e3e1ffdb729d731d2311d53ad;hb=refs/heads/verilator_trace).
209 Pass the tick number on the end of the filename with the '-s' flag:
210
211 (microwatt):$ ./microwatt-verilator hello_world/hello_world.bin -s 1999990
212
213 You'll get a message like this:
214
215 loading hello_world/hello_world.bin at 0x0 size 0x1888
216 loading bram.snapshot.1999990 at 0x0 size 0x10000000
217 restored at 1999990
218
219 These snapshots are generated at intervals of every 2,000,000 ticks.
220
221 - `microwatt-verilator.vcd` - GTKWave waveform file, allowing you to look at
222 processor signals and transitions during simulation.
223 Pass `-d` flag to `microwatt-verilator` binary:
224
225 (microwatt):$ ./microwatt-verilator hello_world/hello_world.bin -d
226
227 **NOTE**: Trace dumping will generate a large VCD file (about 6GB for the hello
228 world example)!
229
230 If you want GTKWave to load it faster, convert to fst first:
231
232 (microwatt):$ vcd2fst --vcdname=microwatt-verilator.vcd --fstname=microwatt-verilator.fst
233 (microwatt):$ gtkwave microwatt-verilator.fst
234
235 Fst files are orders-of-magnitude smaller (about 20MB vs 6GB), but are specific
236 to the GTKWave tool.
237
238 ## Micropython
239
240 The Microwatt repo comes with a pre-compiled
241 [micropython binary](https://git.libre-soc.org/?p=microwatt.git;a=tree;f=micropython;h=18fa078c8145bdaa75667a0ab04eb0b261245665;hb=refs/heads/verilator_trace)
242 (version 1.12), which you can try out after confirming 'hello world' works.
243 Bear in mind, not all features of python will be available. Such as
244 floating-point numbers.
245
246 For micropython to work, you'll need to increase the RAM size in the makefile.
247 Go to the microwatt-verilator makefile, and comment out the following lines:
248
249 MEMORY_SIZE=8192
250 RAM_INIT_FILE=hello_world/hello_world.hex
251
252 And uncomment the following:
253
254 MEMORY_SIZE=393216
255 RAM_INIT_FILE=micropython/firmware.hex
256
257 This will increase the RAM size from 8KiB to 384KiB. The `RAM_INIT_FILE` in
258 these examples isn't doing anything, however good practice to follow.
259
260 Clean up generated files, and recompile:
261
262 (microwatt):$ make clean
263 (microwatt):$ make microwatt-verilator
264
265 Once the binary has been built, run the same way as before, but point to the
266 micropython firmware binary:
267
268 (microwatt):$ microwatt-verilator micropython/firmware.bin
269
270 On the same system as above, with 1 thread, it took 49 seconds to get to the
271 micropython shell.
272
273 ## Verilator runtime commands
274 A few examples:
275
276 # Show the version of verilator being used
277 (microwatt):$ ./microwatt-verilator +verilator+version
278
279 ## Building `microwatt-verilator` using the Libre-SOC core
280
281 In the Makefile, you need to set `EXTERNAL_CORE` to true, and copy the
282 generated core from soc repo to microwatt. *(If you use a separate chroot to
283 generate Libre-SOC cores, then you'll need to copy from that chroot to
284 microwatt chroot from host.*
285
286 cd /path/to/soc
287 make microwatt_external_core
288 cp external_core_top.v /path/to/microwatt
289
290 Then compile verilator sim binary as before:
291
292 cd ~/src/microwatt/
293 export FPGA_TARGET=verilator
294 export GHDLSYNTH=ghdl
295 make microwatt-verilator
296
297 ## Running Linux kernel
298
299 To run Linux on Microwatt, you'll need two binaries:
300
301 - The `sdram_init.bin`, which is easy to compile (no additional software
302 required).
303
304 - The `dtbImage.microwatt` device tree Linux kernel. This can be compiled (see
305 below), or a copy can be downloaded from: <https://ftp.libre-soc.org/dtbImage.microwatt>.
306
307 Like with the micropython example, you'll need to increase the RAM size in the
308 makefile, and recompile the microwatt-verilator binary.
309
310 Uncomment the following:
311
312 MEMORY_SIZE=536870912
313
314 Which will change the RAM size to 512KiB.
315
316 As there is no `dtbImage.microwatt.hex`, you can leave `RAM_INIT_FILE` unchanged.
317
318 ### Building the kernel - TODO:
319
320 *(Please don't build the kernel yourself, until you've tested with the existing kernel linked
321 above!)*
322
323 On a POWER9 there is no need to install gcc-powerpc64le-linux-gnu,
324 you can omit CROSS_COMPILE and ARCH in this case
325
326 apt install gcc-powerpc64le-linux-gnu
327 apt install flex bison lz4
328 git clone -b microwatt-5.7 https://git.kernel.org/pub/scm/linux/kernel/git/joel/microwatt.git
329 cd microwatt
330 wget https://ftp.libre-soc.org/microwatt-linux-5.7.patch
331 patch -p1 < microwatt-linux-5.7.patch
332 wget https://ftp.libre-soc.org/rootfs.cpio
333 CROSS_COMPILE="ccache powerpc64le-linux-gnu-" ARCH=powerpc make -j8 O=microwatt microwatt_defconfig
334 CROSS_COMPILE="ccache powerpc64le-linux-gnu-" ARCH=powerpc make -j8 O=microwatt
335
336 This will produce a file
337 microwatt/arch/powerpc/boot/dtbImage.microwatt
338
339 ### Building `sdram_init.bin`
340 This needs gcc-powerpc64le-linux-gnu (already included in the setup step) if
341 cross compilation is used. It is assumed you're already in `~/src/microwatt/`
342 directory.
343
344 (microwatt):$ cd litedram/gen-src/sdram_init/
345 (microwatt):$ make
346
347 The resulting binary will be in the `obj/` directory.
348
349 ### Running the simulation
350
351 Make sure to return back to `src/microwatt/`.
352
353 (microwatt):$ cd ~/src/microwatt/
354 (microwatt):$ cp microwatt/arch/powerpc/boot/dtbImage.microwatt
355 (microwatt):$ ./microwatt-verilator sdram_init.bin dtbImage.microwatt
356
357 This will take some time...
358
359 ### Sysconn information
360
361 TODO WIP integrate from <https://libre-soc.org/irclog/%23libre-soc.2022-01-26.log.html>
362
363 Sysconn is a module which includes information about the SoC, and the info is
364 printed at the start of the simulation.
365
366 ### Time benchmarks
367
368 `microwatt-verilator` was compiled with 3 threads for faster simulation.
369
370 - Time to finish printing Sysconn info: about 1min
371 - Time to allocate bytes to kernel: ?
372 - Time to login prompt: about 1 hour
373 - Time to user shell: ?
374
375 ### TODO: buildroot
376
377 * https://github.com/shenki/buildroot/commits/microwatt
378 * https://codeconstruct.com.au/docs/microwatt-orangecrab/
379
380 ## FPGA Development - TODO: Need checking
381 ### Building the bitstring for OrangeCrab
382
383 cd microwatt
384 export FPGA_TARGET=ORANGE-CRAB
385 export GHDLSYNTH=ghdl
386 make microwatt.bit
387
388 ### flashing the bitstring to the OrangeCrab
389
390 make prog # this will run OpenOCD
391
392 ### Notes for ulx3s
393
394 notes for how to compile for ulx3s
395
396 git clone https://github.com/kost/fujprog
397 (follow build procedure shown in fujprog README)
398 git clone https://git.libre-soc.org/git/microwatt.git
399 git checkout -b verilator_trace
400 export FPGA_TARGET=ulx3s
401 make microwatt.svf
402 fujprog microwatt.svf
403
404
405 ### Notes for nextpnr-xilinx
406
407 superceded: see page [[nextpnr-xilinx]] and devscript
408 <https://git.libre-soc.org/?p=dev-env-setup.git;a=blob;f=nextpnr-xilinx-install;hb=HEAD>
409
410 for compiling nextpnr-xilinx and making it useable for nmigen
411 to compile for the digilent arty-a7-100t, requires a little
412 futzing around, using the symbiflow version of prjxray-db
413 instead of the one recommended as a submodule
414
415 git clone https://github.com/gatecat/nextpnr-xilinx
416 cd nextpnr-xilinx
417 git checkout cd8b15db6ff5c1a7f10a9e
418 git submodule init
419 git submodule update
420 cd xilinx/external
421 mv prjxray-db prjxray-db-no
422 git clone https://github.com/SymbiFlow/prjxray-db
423 cd prjxray-db
424 git checkout 0a0addedd73e7
425 cp ./artix7/xc7a100t/*.json \
426 ./artix7/xc7a100tcsg324-1
427 cd ../../..
428 cmake -DARCH=xilinx .
429 make
430 make install
431 python3 xilinx/python/bbaexport.py --device xc7a100tcsg324-1 --bba xilinx/xc7a100t.bba
432 ./bbasm --l xilinx/xc7a100t.bba xilinx/xc7a100t.bin
433 mkdir -p /usr/share/nextpnr/xilinx-chipdb
434 cp xilinx/*.bin /usr/share/nextpnr/xilinx-chipdb
435 cp -aux xilinx/external/prjxray-db /usr/share/nextpnr
436
437 # Additional Useful Info for UART <-> USB Serial Interface Through OrangeCrab's Built-in USB Interface
438
439 This uses OrangeCrab's built-in USB interface, rather than needing a
440 separate USB-serial adapter. see the following for further details:
441
442 * <https://github.com/antonblanchard/microwatt/pull/347#issuecomment-1058800570>
443 * <https://github.com/antonblanchard/microwatt/pull/347#issuecomment-1058834790>
444
445 # running orangecrab-examples before flashing microwatt
446
447 See <https://github.com/orangecrab-fpga/orangecrab-hardware/blob/main/contrib/10-orangecrab.rules>
448
449 If the OrangeCrab is running in DFU mode, lsusb will show:
450
451 1209:5af0 Generic OrangeCrab r0.2 DFU Bootloader v3.1-6-g62e92e2
452
453 OrangeCrab has two DFU devices:
454
455 Found DFU: [1209:5af0] ver=0101, devnum=22, cfg=1, intf=0, path="1-4.2", alt=1, name="0x00100000 RISC-V Firmware", serial="UNKNOWN"
456 Found DFU: [1209:5af0] ver=0101, devnum=22, cfg=1, intf=0, path="1-4.2", alt=0, name="0x00080000 Bitstream", serial="UNKNOWN"
457
458 Then clone and patch orangecrab-examples:
459
460 git clone https://github.com/orangecrab-fpga/orangecrab-examples
461 patch -p1 < orangecrab-examples.diff
462
463 To build and flash the example:
464
465 pushd orangecrab-examples/nmigen
466 python3 blink.py
467 popd
468 sudo dfu-util -D orangecrab-examples/nmigen/build/top.bit -a 0