Skip to content

Commit 01717d6

Browse files
committed
submodule support child definitions in text format
1 parent 2a64541 commit 01717d6

File tree

4 files changed

+220
-1
lines changed

4 files changed

+220
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
TARGET=$(shell ls *.py | grep -v test | grep -v parsetab.py)
2+
ARGS=
3+
4+
PYTHON=python3
5+
#PYTHON=python
6+
#OPT=-m pdb
7+
#OPT=-m cProfile -s time
8+
#OPT=-m cProfile -o profile.rslt
9+
10+
.PHONY: all
11+
all: test
12+
13+
.PHONY: run
14+
run:
15+
$(PYTHON) $(OPT) $(TARGET) $(ARGS)
16+
17+
.PHONY: test
18+
test:
19+
$(PYTHON) -m pytest -vv
20+
21+
.PHONY: check
22+
check:
23+
$(PYTHON) $(OPT) $(TARGET) $(ARGS) > tmp.v
24+
iverilog -tnull -Wall tmp.v
25+
rm -f tmp.v
26+
27+
.PHONY: clean
28+
clean:
29+
rm -rf *.pyc __pycache__ parsetab.py .cache *.out *.png *.dot tmp.v uut.vcd
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import sys
4+
import os
5+
import collections
6+
7+
# the next line can be removed after installation
8+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(
9+
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))))
10+
11+
from veriloggen import *
12+
13+
verilog_blinkled = """
14+
module blinkled #
15+
(
16+
parameter WIDTH = 8
17+
)
18+
(
19+
input CLK,
20+
input RST,
21+
output reg [WIDTH-1:0] LED,
22+
output [WIDTH-1:0] dummy_out0,
23+
output [WIDTH-1:0] dummy_out1,
24+
output [WIDTH-1:0] dummy_out2,
25+
input [WIDTH-1:0] dummy_in0,
26+
input [WIDTH-1:0] dummy_in1,
27+
input [WIDTH-1:0] dummy_in2
28+
);
29+
30+
reg [32-1:0] count;
31+
32+
always @(posedge CLK) begin
33+
if(RST) begin
34+
count <= 0;
35+
end else begin
36+
if(count == 1023) begin
37+
count <= 0;
38+
end else begin
39+
count <= count + 1;
40+
end
41+
end
42+
end
43+
44+
45+
always @(posedge CLK) begin
46+
if(RST) begin
47+
LED <= 0;
48+
end else begin
49+
if(count == 1023) begin
50+
LED <= LED + 1;
51+
end
52+
end
53+
end
54+
55+
56+
endmodule
57+
"""
58+
59+
60+
def mkTop():
61+
m = Module('top')
62+
width = m.Parameter('WIDTH', 8)
63+
clk = m.Input('CLK')
64+
rst = m.Input('RST')
65+
led = m.Output('LED', width)
66+
67+
sub = Submodule(m, verilog_blinkled, 'inst_blinkled',
68+
arg_params=(('WIDTH', width),),
69+
arg_ports=(('LED', led), ('CLK', clk), ('RST', rst)),
70+
as_io=('dummy_out0', 'dummy_in0'), as_wire=('dummy_out1', 'dummy_in1'))
71+
72+
return m
73+
74+
if __name__ == '__main__':
75+
top = mkTop()
76+
verilog = top.to_verilog()
77+
print(verilog)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import veriloggen
4+
import submodule_read_verilog
5+
6+
expected_verilog = """
7+
module top #
8+
(
9+
parameter WIDTH = 8
10+
)
11+
(
12+
input CLK,
13+
input RST,
14+
output [WIDTH-1:0] LED,
15+
output [WIDTH-1+1-1:0] dummy_out0,
16+
input [WIDTH-1+1-1:0] dummy_in0
17+
);
18+
19+
wire [WIDTH-1+1-1:0] dummy_out2;
20+
reg [WIDTH-1+1-1:0] dummy_in2;
21+
wire [WIDTH-1+1-1:0] dummy_out1;
22+
wire [WIDTH-1+1-1:0] dummy_in1;
23+
24+
blinkled
25+
#(
26+
.WIDTH(WIDTH)
27+
)
28+
inst_blinkled
29+
(
30+
.CLK(CLK),
31+
.RST(RST),
32+
.LED(LED),
33+
.dummy_out0(dummy_out0),
34+
.dummy_out1(dummy_out1),
35+
.dummy_out2(dummy_out2),
36+
.dummy_in0(dummy_in0),
37+
.dummy_in1(dummy_in1),
38+
.dummy_in2(dummy_in2)
39+
);
40+
41+
42+
endmodule
43+
44+
45+
46+
module blinkled #
47+
(
48+
parameter WIDTH = 8
49+
)
50+
(
51+
input CLK,
52+
input RST,
53+
output reg [WIDTH-1:0] LED,
54+
output [WIDTH-1:0] dummy_out0,
55+
output [WIDTH-1:0] dummy_out1,
56+
output [WIDTH-1:0] dummy_out2,
57+
input [WIDTH-1:0] dummy_in0,
58+
input [WIDTH-1:0] dummy_in1,
59+
input [WIDTH-1:0] dummy_in2
60+
);
61+
62+
reg [32-1:0] count;
63+
64+
always @(posedge CLK) begin
65+
if(RST) begin
66+
count <= 0;
67+
end else begin
68+
if(count == 1023) begin
69+
count <= 0;
70+
end else begin
71+
count <= count + 1;
72+
end
73+
end
74+
end
75+
76+
77+
always @(posedge CLK) begin
78+
if(RST) begin
79+
LED <= 0;
80+
end else begin
81+
if(count == 1023) begin
82+
LED <= LED + 1;
83+
end
84+
end
85+
end
86+
87+
88+
endmodule
89+
"""
90+
91+
92+
def test():
93+
test_module = submodule_read_verilog.mkTop()
94+
code = test_module.to_verilog()
95+
96+
from pyverilog.vparser.parser import VerilogParser
97+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
98+
parser = VerilogParser()
99+
expected_ast = parser.parse(expected_verilog)
100+
codegen = ASTCodeGenerator()
101+
expected_code = codegen.visit(expected_ast)
102+
103+
assert(expected_code == code)

veriloggen/core/submodule.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import collections
44
import veriloggen.core.module as module
55
import veriloggen.core.vtypes as vtypes
6+
import veriloggen.verilog.from_verilog as from_verilog
67

78

89
class Submodule(vtypes.VeriloggenNode):
@@ -11,9 +12,18 @@ class Submodule(vtypes.VeriloggenNode):
1112
def __init__(self, parent, child,
1213
name=None, prefix=None,
1314
arg_params=None, arg_ports=None,
14-
as_io=None, as_wire=None):
15+
as_io=None, as_wire=None, topmodule=None):
1516

1617
self.parent = parent
18+
19+
# child module definition as Verilog HDL source code text
20+
if isinstance(child, str):
21+
modules = from_verilog.read_verilog_module_str(child)
22+
if topmodule is None:
23+
child = list(modules.values())[0]
24+
else:
25+
child = modules[topmodule]
26+
1727
self.child = child
1828

1929
if name is None:

0 commit comments

Comments
 (0)