Skip to content

Commit 43c9900

Browse files
committed
Example code in README is updated.
1 parent d076161 commit 43c9900

File tree

4 files changed

+187
-33
lines changed

4 files changed

+187
-33
lines changed

README.md

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,66 @@ Finally, let's try code generation. Please prepare a Python script as below. The
342342
A Verilog HDL code is represented by using the AST classes defined in 'vparser.ast'.
343343

344344
```python
345+
from __future__ import absolute_import
346+
from __future__ import print_function
347+
import sys
348+
import os
345349
import pyverilog.vparser.ast as vast
346350
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
347351

348-
params = vast.Paramlist(())
349-
clk = vast.Ioport( vast.Input('CLK') )
350-
rst = vast.Ioport( vast.Input('RST') )
351-
width = vast.Width( vast.IntConst('7'), vast.IntConst('0') )
352-
led = vast.Ioport( vast.Output('led', width=width) )
353-
ports = vast.Portlist( (clk, rst, led) )
354-
items = ( vast.Assign( vast.Identifier('led'), vast.IntConst('8') ) ,)
355-
ast = vast.ModuleDef("top", params, ports, items)
356-
357-
codegen = ASTCodeGenerator()
358-
rslt = codegen.visit(ast)
359-
print(rslt)
352+
def main():
353+
datawid = vast.Parameter( 'DATAWID', vast.Rvalue(vast.IntConst('32')) )
354+
params = vast.Paramlist( [datawid] )
355+
clk = vast.Ioport( vast.Input('CLK') )
356+
rst = vast.Ioport( vast.Input('RST') )
357+
width = vast.Width( vast.IntConst('7'), vast.IntConst('0') )
358+
led = vast.Ioport( vast.Output('led', width=width) )
359+
ports = vast.Portlist( [clk, rst, led] )
360+
361+
width = vast.Width( vast.Minus(vast.Identifier('DATAWID'), vast.IntConst('1')), vast.IntConst('0') )
362+
count = vast.Reg('count', width=width)
363+
364+
assign = vast.Assign(
365+
vast.Lvalue(vast.Identifier('led')),
366+
vast.Rvalue(
367+
vast.Partselect(
368+
vast.Identifier('count'), # count
369+
vast.Minus(vast.Identifier('DATAWID'), vast.IntConst('1')), # [DATAWID-1:
370+
vast.Minus(vast.Identifier('DATAWID'), vast.IntConst('8'))))) # :DATAWID-8]
371+
372+
sens = vast.Sens(vast.Identifier('CLK'), type='posedge')
373+
senslist = vast.SensList([ sens ])
374+
375+
assign_count_true = vast.NonblockingSubstitution(
376+
vast.Lvalue(vast.Identifier('count')),
377+
vast.Rvalue(vast.IntConst('0')))
378+
if0_true = vast.Block([ assign_count_true ])
379+
380+
# count + 1
381+
count_plus_1 = vast.Plus(vast.Identifier('count'), vast.IntConst('1'))
382+
assign_count_false = vast.NonblockingSubstitution(
383+
vast.Lvalue(vast.Identifier('count')),
384+
vast.Rvalue(count_plus_1))
385+
if0_false = vast.Block([ assign_count_false ])
386+
387+
if0 = vast.IfStatement(vast.Identifier('RST'), if0_true, if0_false)
388+
statement = vast.Block([ if0 ])
389+
390+
always = vast.Always(senslist, statement)
391+
392+
items = []
393+
items.append(count)
394+
items.append(assign)
395+
items.append(always)
396+
397+
ast = vast.ModuleDef("top", params, ports, items)
398+
399+
codegen = ASTCodeGenerator()
400+
rslt = codegen.visit(ast)
401+
print(rslt)
402+
403+
if __name__ == '__main__':
404+
main()
360405
```
361406

362407
Please type the command as below at the same directory with Pyverilog.
@@ -368,14 +413,27 @@ python test.py
368413
Then Verilog HDL code generated from the AST instances is displayed.
369414

370415
```verilog
371-
module top
416+
module top #
417+
(
418+
parameter DATAWID = 32
419+
)
372420
(
373421
input CLK,
374422
input RST,
375423
output [7:0] led
376424
);
377425
378-
assign led = 8;
426+
reg [DATAWID - 1:0] count;
427+
assign led = count[DATAWID - 1:DATAWID - 8];
428+
429+
always @(posedge CLK) begin
430+
if(RST) begin
431+
count <= 0;
432+
end else begin
433+
count <= count + 1;
434+
end
435+
end
436+
379437
380438
endmodule
381439
```

README.rst

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -377,21 +377,66 @@ using the AST classes defined in 'vparser.ast'.
377377

378378
.. code:: python
379379
380+
from __future__ import absolute_import
381+
from __future__ import print_function
382+
import sys
383+
import os
380384
import pyverilog.vparser.ast as vast
381385
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
382386
383-
params = vast.Paramlist(())
384-
clk = vast.Ioport( vast.Input('CLK') )
385-
rst = vast.Ioport( vast.Input('RST') )
386-
width = vast.Width( vast.IntConst('7'), vast.IntConst('0') )
387-
led = vast.Ioport( vast.Output('led', width=width) )
388-
ports = vast.Portlist( (clk, rst, led) )
389-
items = ( vast.Assign( vast.Identifier('led'), vast.IntConst('8') ) ,)
390-
ast = vast.ModuleDef("top", params, ports, items)
391-
392-
codegen = ASTCodeGenerator()
393-
rslt = codegen.visit(ast)
394-
print(rslt)
387+
def main():
388+
datawid = vast.Parameter( 'DATAWID', vast.Rvalue(vast.IntConst('32')) )
389+
params = vast.Paramlist( [datawid] )
390+
clk = vast.Ioport( vast.Input('CLK') )
391+
rst = vast.Ioport( vast.Input('RST') )
392+
width = vast.Width( vast.IntConst('7'), vast.IntConst('0') )
393+
led = vast.Ioport( vast.Output('led', width=width) )
394+
ports = vast.Portlist( [clk, rst, led] )
395+
396+
width = vast.Width( vast.Minus(vast.Identifier('DATAWID'), vast.IntConst('1')), vast.IntConst('0') )
397+
count = vast.Reg('count', width=width)
398+
399+
assign = vast.Assign(
400+
vast.Lvalue(vast.Identifier('led')),
401+
vast.Rvalue(
402+
vast.Partselect(
403+
vast.Identifier('count'), # count
404+
vast.Minus(vast.Identifier('DATAWID'), vast.IntConst('1')), # [DATAWID-1:
405+
vast.Minus(vast.Identifier('DATAWID'), vast.IntConst('8'))))) # :DATAWID-8]
406+
407+
sens = vast.Sens(vast.Identifier('CLK'), type='posedge')
408+
senslist = vast.SensList([ sens ])
409+
410+
assign_count_true = vast.NonblockingSubstitution(
411+
vast.Lvalue(vast.Identifier('count')),
412+
vast.Rvalue(vast.IntConst('0')))
413+
if0_true = vast.Block([ assign_count_true ])
414+
415+
# count + 1
416+
count_plus_1 = vast.Plus(vast.Identifier('count'), vast.IntConst('1'))
417+
assign_count_false = vast.NonblockingSubstitution(
418+
vast.Lvalue(vast.Identifier('count')),
419+
vast.Rvalue(count_plus_1))
420+
if0_false = vast.Block([ assign_count_false ])
421+
422+
if0 = vast.IfStatement(vast.Identifier('RST'), if0_true, if0_false)
423+
statement = vast.Block([ if0 ])
424+
425+
always = vast.Always(senslist, statement)
426+
427+
items = []
428+
items.append(count)
429+
items.append(assign)
430+
items.append(always)
431+
432+
ast = vast.ModuleDef("top", params, ports, items)
433+
434+
codegen = ASTCodeGenerator()
435+
rslt = codegen.visit(ast)
436+
print(rslt)
437+
438+
if __name__ == '__main__':
439+
main()
395440
396441
Please type the command as below at the same directory with Pyverilog.
397442

@@ -403,14 +448,27 @@ Then Verilog HDL code generated from the AST instances is displayed.
403448

404449
.. code:: verilog
405450
406-
module top
451+
module top #
452+
(
453+
parameter DATAWID = 32
454+
)
407455
(
408456
input CLK,
409457
input RST,
410458
output [7:0] led
411459
);
412460
413-
assign led = 8;
461+
reg [DATAWID - 1:0] count;
462+
assign led = count[DATAWID - 1:DATAWID - 8];
463+
464+
always @(posedge CLK) begin
465+
if(RST) begin
466+
count <= 0;
467+
end else begin
468+
count <= count + 1;
469+
end
470+
end
471+
414472
415473
endmodule
416474

examples/example_ast_code.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,50 @@
1010
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
1111

1212
def main():
13-
params = vast.Paramlist(())
13+
datawid = vast.Parameter( 'DATAWID', vast.Rvalue(vast.IntConst('32')) )
14+
params = vast.Paramlist( [datawid] )
1415
clk = vast.Ioport( vast.Input('CLK') )
1516
rst = vast.Ioport( vast.Input('RST') )
1617
width = vast.Width( vast.IntConst('7'), vast.IntConst('0') )
1718
led = vast.Ioport( vast.Output('led', width=width) )
18-
ports = vast.Portlist( (clk, rst, led) )
19-
items = ( vast.Assign( vast.Identifier('led'), vast.IntConst('8') ) ,)
19+
ports = vast.Portlist( [clk, rst, led] )
20+
21+
width = vast.Width( vast.Minus(vast.Identifier('DATAWID'), vast.IntConst('1')), vast.IntConst('0') )
22+
count = vast.Reg('count', width=width)
23+
24+
assign = vast.Assign(
25+
vast.Lvalue(vast.Identifier('led')),
26+
vast.Rvalue(
27+
vast.Partselect(
28+
vast.Identifier('count'), # count
29+
vast.Minus(vast.Identifier('DATAWID'), vast.IntConst('1')), # [DATAWID-1:
30+
vast.Minus(vast.Identifier('DATAWID'), vast.IntConst('8'))))) # :DATAWID-8]
31+
32+
sens = vast.Sens(vast.Identifier('CLK'), type='posedge')
33+
senslist = vast.SensList([ sens ])
34+
35+
assign_count_true = vast.NonblockingSubstitution(
36+
vast.Lvalue(vast.Identifier('count')),
37+
vast.Rvalue(vast.IntConst('0')))
38+
if0_true = vast.Block([ assign_count_true ])
39+
40+
# count + 1
41+
count_plus_1 = vast.Plus(vast.Identifier('count'), vast.IntConst('1'))
42+
assign_count_false = vast.NonblockingSubstitution(
43+
vast.Lvalue(vast.Identifier('count')),
44+
vast.Rvalue(count_plus_1))
45+
if0_false = vast.Block([ assign_count_false ])
46+
47+
if0 = vast.IfStatement(vast.Identifier('RST'), if0_true, if0_false)
48+
statement = vast.Block([ if0 ])
49+
50+
always = vast.Always(senslist, statement)
51+
52+
items = []
53+
items.append(count)
54+
items.append(assign)
55+
items.append(always)
56+
2057
ast = vast.ModuleDef("top", params, ports, items)
2158

2259
codegen = ASTCodeGenerator()

tests/ast_code_generator_test/test_ast_assign.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
"""
2020

2121
def test():
22-
params = vast.Paramlist(())
22+
params = vast.Paramlist( [] )
2323
clk = vast.Ioport( vast.Input('CLK') )
2424
rst = vast.Ioport( vast.Input('RST') )
2525
width = vast.Width( vast.IntConst('7'), vast.IntConst('0') )
2626
led = vast.Ioport( vast.Output('led', width=width) )
2727
ports = vast.Portlist( (clk, rst, led) )
28-
items = ( vast.Assign( vast.Identifier('led'), vast.IntConst('8') ) ,)
28+
items = [ vast.Assign( vast.Lvalue(vast.Identifier('led')),
29+
vast.Rvalue(vast.IntConst('8'))) ]
2930
ast = vast.ModuleDef("top", params, ports, items)
3031

3132
codegen = ASTCodeGenerator()

0 commit comments

Comments
 (0)