Skip to content

Commit 0318d5e

Browse files
committed
read_verilog_nested
1 parent de80cb3 commit 0318d5e

File tree

3 files changed

+282
-0
lines changed

3 files changed

+282
-0
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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 [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+
sub_blinkled
31+
#(
32+
.WIDTH(WIDTH)
33+
)
34+
inst_sub_blinkled
35+
(
36+
.CLK(CLK),
37+
.RST(RST),
38+
.LED(LED),
39+
.dummy_out0(dummy_out0),
40+
.dummy_out1(dummy_out1),
41+
.dummy_out2(dummy_out2),
42+
.dummy_in0(dummy_in0),
43+
.dummy_in1(dummy_in1),
44+
.dummy_in2(dummy_in2)
45+
);
46+
47+
endmodule
48+
49+
module sub_blinkled #
50+
(
51+
parameter WIDTH = 8
52+
)
53+
(
54+
input CLK,
55+
input RST,
56+
output reg [WIDTH-1:0] LED,
57+
output [WIDTH-1:0] dummy_out0,
58+
output [WIDTH-1:0] dummy_out1,
59+
output [WIDTH-1:0] dummy_out2,
60+
input [WIDTH-1:0] dummy_in0,
61+
input [WIDTH-1:0] dummy_in1,
62+
input [WIDTH-1:0] dummy_in2
63+
);
64+
65+
reg [32-1:0] count;
66+
67+
always @(posedge CLK) begin
68+
if(RST) begin
69+
count <= 0;
70+
end else begin
71+
if(count == 1023) begin
72+
count <= 0;
73+
end else begin
74+
count <= count + 1;
75+
end
76+
end
77+
end
78+
79+
80+
always @(posedge CLK) begin
81+
if(RST) begin
82+
LED <= 0;
83+
end else begin
84+
if(count == 1023) begin
85+
LED <= LED + 1;
86+
end
87+
end
88+
end
89+
90+
91+
endmodule
92+
"""
93+
94+
95+
def mkTop():
96+
m = Module('top')
97+
width = m.Parameter('WIDTH', 8)
98+
clk = m.Input('CLK')
99+
rst = m.Input('RST')
100+
led = m.Output('LED', width)
101+
102+
sub = Submodule(m, verilog_blinkled, 'inst_blinkled',
103+
arg_params=(('WIDTH', width),),
104+
arg_ports=(('LED', led), ('CLK', clk), ('RST', rst)),
105+
as_io=('dummy_out0', 'dummy_in0'), as_wire=('dummy_out1', 'dummy_in1'),
106+
topmodule='blinkled')
107+
108+
return m
109+
110+
if __name__ == '__main__':
111+
top = mkTop()
112+
verilog = top.to_verilog()
113+
print(verilog)
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import veriloggen
4+
import submodule_read_verilog_nested
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 [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+
sub_blinkled
63+
#(
64+
.WIDTH(WIDTH)
65+
)
66+
inst_sub_blinkled
67+
(
68+
.CLK(CLK),
69+
.RST(RST),
70+
.LED(LED),
71+
.dummy_out0(dummy_out0),
72+
.dummy_out1(dummy_out1),
73+
.dummy_out2(dummy_out2),
74+
.dummy_in0(dummy_in0),
75+
.dummy_in1(dummy_in1),
76+
.dummy_in2(dummy_in2)
77+
);
78+
79+
endmodule
80+
81+
82+
83+
module sub_blinkled #
84+
(
85+
parameter WIDTH = 8
86+
)
87+
(
88+
input CLK,
89+
input RST,
90+
output reg [WIDTH-1:0] LED,
91+
output [WIDTH-1:0] dummy_out0,
92+
output [WIDTH-1:0] dummy_out1,
93+
output [WIDTH-1:0] dummy_out2,
94+
input [WIDTH-1:0] dummy_in0,
95+
input [WIDTH-1:0] dummy_in1,
96+
input [WIDTH-1:0] dummy_in2
97+
);
98+
99+
reg [32-1:0] count;
100+
101+
always @(posedge CLK) begin
102+
if(RST) begin
103+
count <= 0;
104+
end else begin
105+
if(count == 1023) begin
106+
count <= 0;
107+
end else begin
108+
count <= count + 1;
109+
end
110+
end
111+
end
112+
113+
114+
always @(posedge CLK) begin
115+
if(RST) begin
116+
LED <= 0;
117+
end else begin
118+
if(count == 1023) begin
119+
LED <= LED + 1;
120+
end
121+
end
122+
end
123+
124+
125+
endmodule
126+
"""
127+
128+
129+
def test():
130+
test_module = submodule_read_verilog_nested.mkTop()
131+
code = test_module.to_verilog()
132+
133+
from pyverilog.vparser.parser import VerilogParser
134+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
135+
parser = VerilogParser()
136+
expected_ast = parser.parse(expected_verilog)
137+
codegen = ASTCodeGenerator()
138+
expected_code = codegen.visit(expected_ast)
139+
140+
assert(expected_code == code)

0 commit comments

Comments
 (0)