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