@@ -22,7 +22,7 @@ What's Pyverilog?
2222Pyverilog is open-source hardware design processing toolkit for Verilog HDL.
2323All source codes are written in Python.
2424
25- Pyverilog includes (1) code parser, (2) dataflow analyzer, (3) control-flow analyzer and (4) code generator.
25+ Pyverilog includes ** (1) code parser, (2) dataflow analyzer, (3) control-flow analyzer and (4) code generator** .
2626You can create your own design analyzer, code translator and code generator of Verilog HDL based on this toolkit.
2727
2828
@@ -39,7 +39,7 @@ Software Requirements
3939 - apt-get install iverilog
4040
4141
42- Getting Started
42+ Functions
4343------------------------------
4444
4545This software includes various tools for Verilog HDL design.
@@ -49,5 +49,282 @@ This software includes various tools for Verilog HDL design.
4949* controlflow: Control-flow analyzer with condition analyzer that identify when a signal is activated.
5050* ast\_ code\_ generator: Verilog HDL code generator from AST.
5151
52- To use them, please type 'make' in each sub directory.
52+
53+ Getting Started
54+ ------------------------------
55+
56+ First, please prepare a Verilog HDL source file as below. The file name is 'test.v'.
57+ This sample design adds the input value internally whtn the enable signal is asserted. Then is outputs its partial value to the LED.
58+
59+ ``` verilog
60+ module top
61+ (
62+ input CLK,
63+ input RST,
64+ input enable,
65+ input [31:0] value,
66+ output [7:0] led
67+ );
68+ reg [31:0] count;
69+ reg [7:0] state;
70+ assign led = count[23:16];
71+ always @(posedge CLK) begin
72+ if(RST) begin
73+ count <= 0;
74+ state <= 0;
75+ end else begin
76+ if(state == 0) begin
77+ if(enable) state <= 1;
78+ end else if(state == 1) begin
79+ state <= 2;
80+ end else if(state == 2) begin
81+ count <= count + value;
82+ state <= 0;
83+ end
84+ end
85+ end
86+ endmodule
87+ ```
88+
89+ ** Code parser**
90+
91+ Let's try syntax analysis. Please type the command as below.
92+
93+ ```
94+ python3.3 pyverilog/vparser/parser.py test.v
95+ ```
96+
97+ Then you got the result as below. The result of syntax analysis is displayed.
98+
99+ ```
100+ Source:
101+ Description:
102+ ModuleDef: top
103+ Paramlist:
104+ Portlist:
105+ Ioport:
106+ Input: CLK, False
107+ Width:
108+ IntConst: 0
109+ IntConst: 0
110+ Ioport:
111+ Input: RST, False
112+ Width:
113+ IntConst: 0
114+ IntConst: 0
115+ Ioport:
116+ Input: enable, False
117+ Width:
118+ IntConst: 0
119+ IntConst: 0
120+ Ioport:
121+ Input: value, False
122+ Width:
123+ IntConst: 31
124+ IntConst: 0
125+ Ioport:
126+ Output: led, False
127+ Width:
128+ IntConst: 7
129+ IntConst: 0
130+ Decl:
131+ Reg: count, False
132+ Width:
133+ IntConst: 31
134+ IntConst: 0
135+ Decl:
136+ Reg: state, False
137+ Width:
138+ IntConst: 7
139+ IntConst: 0
140+ Assign:
141+ Lvalue:
142+ Identifier: led
143+ Rvalue:
144+ Partselect:
145+ Identifier: count
146+ IntConst: 23
147+ IntConst: 16
148+ Always:
149+ SensList:
150+ Sens: posedge
151+ Identifier: CLK
152+ Block: None
153+ IfStatement:
154+ Identifier: RST
155+ Block: None
156+ NonblockingSubstitution:
157+ Lvalue:
158+ Identifier: count
159+ Rvalue:
160+ IntConst: 0
161+ NonblockingSubstitution:
162+ Lvalue:
163+ Identifier: state
164+ Rvalue:
165+ IntConst: 0
166+ Block: None
167+ IfStatement:
168+ Eq:
169+ Identifier: state
170+ IntConst: 0
171+ Block: None
172+ IfStatement:
173+ Identifier: enable
174+ NonblockingSubstitution:
175+ Lvalue:
176+ Identifier: state
177+ Rvalue:
178+ IntConst: 1
179+ IfStatement:
180+ Eq:
181+ Identifier: state
182+ IntConst: 1
183+ Block: None
184+ NonblockingSubstitution:
185+ Lvalue:
186+ Identifier: state
187+ Rvalue:
188+ IntConst: 2
189+ IfStatement:
190+ Eq:
191+ Identifier: state
192+ IntConst: 2
193+ Block: None
194+ NonblockingSubstitution:
195+ Lvalue:
196+ Identifier: count
197+ Rvalue:
198+ Plus:
199+ Identifier: count
200+ Identifier: value
201+ NonblockingSubstitution:
202+ Lvalue:
203+ Identifier: state
204+ Rvalue:
205+ IntConst: 0
206+ ```
207+
208+ ** Dataflow analyzer**
209+
210+ Let's try dataflow analysis. Please type the command as below.
211+
212+ ```
213+ python3.3 pyverilog/dataflow/dataflow_analyzer.py -t top test.v
214+ ```
215+
216+ Then you got the result as below. The result of each signal definition and each signal assignment are displayed.
217+
218+ ```
219+ Directive:
220+ Instance:
221+ (top, 'top')
222+ Term:
223+ (Term name:top.led type:{'Output'} msb:(IntConst 7) lsb:(IntConst 0))
224+ (Term name:top.enable type:{'Input'} msb:(IntConst 0) lsb:(IntConst 0))
225+ (Term name:top.CLK type:{'Input'} msb:(IntConst 0) lsb:(IntConst 0))
226+ (Term name:top.count type:{'Reg'} msb:(IntConst 31) lsb:(IntConst 0))
227+ (Term name:top.state type:{'Reg'} msb:(IntConst 7) lsb:(IntConst 0))
228+ (Term name:top.RST type:{'Input'} msb:(IntConst 0) lsb:(IntConst 0))
229+ (Term name:top.value type:{'Input'} msb:(IntConst 31) lsb:(IntConst 0))
230+ Bind:
231+ (Bind dest:top.count tree:(Branch Cond:(Terminal top.RST) True:(IntConst 0) False:(Branch Cond:(Operator Eq Next:(Terminal top.state),(IntConst 0)) False:(Branch Cond:(Operator Eq Next:(Terminal top.state),(IntConst 1)) False:(Branch Cond:(Operator Eq Next:(Terminal top.state),(IntConst 2)) True:(Operator Plus Next:(Terminal top.count),(Terminal top.value)))))))
232+ (Bind dest:top.state tree:(Branch Cond:(Terminal top.RST) True:(IntConst 0) False:(Branch Cond:(Operator Eq Next:(Terminal top.state),(IntConst 0)) True:(Branch Cond:(Terminal top.enable) True:(IntConst 1)) False:(Branch Cond:(Operator Eq Next:(Terminal top.state),(IntConst 1)) True:(IntConst 2) False:(Branch Cond:(Operator Eq Next:(Terminal top.state),(IntConst 2)) True:(IntConst 0))))))
233+ (Bind dest:top.led tree:(Partselect Var:(Terminal top.count) MSB:(IntConst 23) LSB:(IntConst 16)))
234+ ```
235+
236+ Let's view the result of dataflow analysis as a picture file. Now we select 'led' as the target. Please type the command as below.
237+
238+ ```
239+ python3.3 pyverilog/dataflow/graphgen.py -t top -s top.led test.v
240+ ```
241+
242+ Then you got a png file (out.png). The picture shows that the definition of 'led' is a part-selection of 'count' from 23-bit to 16-bit.
243+
244+ ![ out.png] ( http://cdn-ak.f.st-hatena.com/images/fotolife/s/sxhxtxa/20140101/20140101045641.png )
245+
246+ ** Control-flow analyzer**
247+
248+ Let's try control-flow analysis. Please type the command as below.
249+
250+ ```
251+ python2.7 pyverilog/controlflow/controlflow_analyzer.py -t top test.v
252+ ```
253+
254+ Then you got the result as below. The result shows that the state machine structure and transition conditions to the next state in the state machine.
255+
256+ ```
257+ FSM signal: top.count, Condition list length: 4
258+ FSM signal: top.state, Condition list length: 5
259+ Condition: (Ulnot, Eq), Inferring transition condition
260+ Condition: (Eq, top.enable), Inferring transition condition
261+ Condition: (Ulnot, Ulnot, Eq), Inferring transition condition
262+ # SIGNAL NAME: top.state
263+ # DELAY CNT: 0
264+ 0 --(top_enable>'d0)--> 1
265+ 1 --None--> 2
266+ 2 --None--> 0
267+ Loop
268+ (0, 1, 2)
269+ ```
270+
271+ You got also a png file (top_state.png). The picture shows that the graphical structure of the state machine.
272+
273+ ![ top_state.png] ( http://cdn-ak.f.st-hatena.com/images/fotolife/s/sxhxtxa/20140101/20140101045835.png )
274+
275+ ** Code generator**
276+
277+ Finally, let's try code generation. Please prepare a Python script as below. The file name is 'test.py'.
278+ A Verilog HDL code is represented by using the AST classes defined in 'vparser.ast'.
279+
280+ ``` python
281+ import pyverilog.vparser.ast as vast
282+ from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
283+
284+ params = vast.Paramlist(())
285+ clk = vast.Ioport( vast.Input(' CLK' ) )
286+ rst = vast.Ioport( vast.Input(' RST' ) )
287+ width = vast.Width( vast.IntConst(' 7' ), vast.IntConst(' 0' ) )
288+ led = vast.Ioport( vast.Output(' led' , width = width) )
289+ ports = vast.Portlist( (clk, rst, led) )
290+ items = ( vast.Assign( vast.Identifier(' led' ), vast.IntConst(' 8' ) ) ,)
291+ ast = vast.ModuleDef(" top" , params, ports, items)
292+
293+ codegen = ASTCodeGenerator()
294+ rslt = codegen.visit(ast)
295+ print (rslt)
296+ ```
297+
298+ Please type the command as below at the same directory with Pyverilog.
299+
300+ ```
301+ python3.3 test.py
302+ ```
303+
304+ Then Verilog HDL code generated from the AST instances is displayed.
305+
306+ ``` verilog
307+
308+ module top
309+ (
310+ input [0:0] CLK,
311+ input [0:0] RST,
312+ output [7:0] led
313+
314+ );
315+ assign led = 8;
316+ endmodule
317+
318+ ```
319+
320+
321+ Related Project and Site
322+ ------------------------------
323+
324+ [ PyCoRAM] ( http://shtaxxx.github.io/PyCoRAM/ )
325+ - Python-based Implementation of CoRAM Memory Architecture for AXI4 Interconnection on FPGAs
326+
327+ [ shtaxxx.hatenablog.com] ( http://shtaxxx.hatenablog.com/entry/2014/01/01/045856 )
328+ - Blog entry for introduction and examples of Pyverilog (in Japansese)
329+
53330
0 commit comments