fixed image size
[libreriscv.git] / docs / learning_nmigen.mdwn
1 # Learning nmigen
2
3 * links to community <https://nmigen.info/nmigen/latest/tutorial.html>
4 * Useful counter tutorial (gtkwave and verilog) for latest nmigen <https://nmigen.info/nmigen/latest/start.html>
5 * Robert Baruch's nmigen tutorials are really good:
6 <https://github.com/RobertBaruch/nmigen-tutorial>
7 * <https://github.com/GuzTech/ulx3s-nmigen-examples>
8 * <https://github.com/icebreaker-fpga/icebreaker-nmigen-examples>
9 * <https://github.com/kbob/nmigen-examples>
10 * <https://vivonomicon.com/2020/04/14/learning-fpga-design-with-nmigen/54>
11 * <https://www.youtube.com/watch?v=yDJNwxY05-s>
12
13 # Sanity Check (*You'll need it*) Tutorial - Simulation Waveforms, Verilog, Block Diagram
14
15 ## Testbench, GTKWave, Verilog Output
16
17 * nMigen code for counter and testbench here: <https://nmigen.info/nmigen/latest/start.html>
18
19 1. Create a file called "up_counter.py" containing the 16-bit up counter code from "Implementing a counter" section.
20
21 1. Create a file called "tb_up_counter.py" containing the testbench from "Testing a counter".
22
23 1. To the testbench file, add the import statement for the counter module (better get used to separating your sim/stimulus and module classes from the beginning):
24
25 from up_counter import UpCounter
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:
30
31 $python3 tb_up_counter.py
32
33 1. Launch GTKWave by calling:
34
35 $gtkwave up_counter.vcd
36
37 1. Now you should be able to inspect the signals and check counter behaviour (although the test bench also does this).
38
39 1. To generate the verilog equivalent, call the file we created earlier. The script will create a up_counter.v verilog file.
40
41 $python3 conv_to_verilog.py
42
43 ## Block Digram with Yosys
44
45 1. 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. *You may need to install xdot separately with apt*. Xdot is **interactive** (you can click on blocks and nodes!).
46
47 $yosys
48 yosys> read_verilog up_counter.v
49 yosys> show
50
51 1. Outside of Yosys, you can run xdot directly:
52
53 $xdot ~/.yosys_show.dot
54
55 1. If you want to generate a static image, you can use graphviz's "dot" command to generate an image (for example PNG).
56
57 $dot ~/.yosys_show.dot -Tpng -o up_counter.png
58
59 1. Now you can improve your understanding with the nMigen, verilog, and block diagram views side-by-side!
60
61 [[!img nmigen_verilog_tb.png size="600x"]]