bug 1048, ls011: Add Fixed Load Shifted Post-Update section
[libreriscv.git] / docs / learning_nmigen.mdwn
1 # Learning nmigen
2
3 * Link to the mail thread: <http://lists.libre-soc.org/pipermail/libre-soc-dev/2021-October/003858.html>
4 * Links to community <https://gitlab.com/nmigen/nmigen/blob/master/docs/tutorial.rst>
5 * Useful counter tutorial (gtkwave and verilog) for latest nmigen
6 <https://gitlab.com/nmigen/nmigen/blob/master/docs/start.rst>
7 * Robert Baruch's nmigen tutorials are really good:
8 <https://github.com/RobertBaruch/nmigen-tutorial>
9 * <https://github.com/GuzTech/ulx3s-nmigen-examples>
10 * <https://github.com/kbob/nmigen-examples>
11 * <https://vivonomicon.com/2020/04/14/learning-fpga-design-with-nmigen/54>
12 * <https://www.youtube.com/watch?v=yDJNwxY05-s>
13
14 # Sanity Check (*You'll need it*) Tutorial - Simulation Waveforms, Verilog, Block Diagram
15
16 ## Testbench, GTKWave, Verilog Output
17
18 nMigen code for counter and testbench here:
19 <https://gitlab.com/nmigen/nmigen/blob/master/docs/start.rst>
20
21 1. Create a file called "up_counter.py" containing the 16-bit up counter code from "Implementing a counter" section.
22
23 1. Create a file called "tb_up_counter.py" containing the testbench from "Testing a counter".
24
25 1. To the testbench file, add the import statement "from up_counter import UpCounter" for the counter module (better get used to separating your sim/stimulus and module classes from the beginning):
26
27 1. Create a file called "conv_to_verilog.py" and copy the code from "Converting a counter" section. Also add the import statement as with the testbench.
28
29 1. Generate GTKWave .vcd file by running the testbench script.
30
31 1. Launch GTKWave. Now you should be able to inspect the signals and check counter behaviour (although the test bench also does this).
32
33 1. To generate the verilog equivalent, call the file we created earlier. The script will create a up_counter.v verilog file.
34
35 Commands:
36
37 $ python3 tb_up_counter.py
38 $ gtkwave up_counter.vcd &
39 $ python3 conv_to_verilog.py
40
41 ## Block Digram with Yosys
42
43 Open yosys in interactive mode and load the generated verilog file. Calling "show" should generate the diagram .dot file (as a temp file "~/.yosys_show.dot") and open it using xdot. For multi-level modules, you can specify the level of hierarchy by specifying the name of the module. For example "show top" will display the diagram of the top-level (without the underlying the details of the sub-modules).
44
45 *You may need to install xdot separately with apt*. Xdot is **interactive** (you can click on blocks and nodes!).
46
47 Yosys commands:
48
49 $ yosys
50 yosys> read_verilog up_counter.v
51 yosys> show
52
53 Outside of Yosys, commands for diagram (SVG format for static images also supported):
54
55 $ xdot ~/.yosys_show.dot
56 $ dot ~/.yosys_show.dot -Tpng -o up_counter.png
57
58 Here's a sight to behold:
59
60 [[!img nmigen_verilog_tb.png size="600x"]]
61
62 Now you can improve your understanding with the nMigen, verilog, and block diagram views side-by-side!