Skip to content

Commit 01588bd

Browse files
committed
Submodule
1 parent 990e2dc commit 01588bd

File tree

11 files changed

+600
-33
lines changed

11 files changed

+600
-33
lines changed

tests/core/submodule_/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
PYTHON=python3
2+
#PYTHON=python
3+
4+
.PHONY: all
5+
all: clean
6+
7+
.PHONY: test
8+
test:
9+
$(PYTHON) -m pytest -vv
10+
11+
.PHONY: run
12+
run:
13+
find . -maxdepth 1 -type d | grep "./" | xargs -I {} make run -C {}
14+
15+
.PHONY: clean
16+
clean:
17+
rm -rf *.pyc __pycache__ parsetab.py .cache *.out *.png *.dot tmp.v uut.vcd
18+
find . -maxdepth 1 -type d | grep "./" | xargs -I {} make clean -C {}
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
14+
def mkLed():
15+
m = Module('blinkled')
16+
width = m.Parameter('WIDTH', 8)
17+
clk = m.Input('CLK')
18+
rst = m.Input('RST')
19+
led = m.OutputReg('LED', width)
20+
21+
dummy_out0 = m.Output('dummy_out0', width)
22+
dummy_out1 = m.Output('dummy_out1', width)
23+
dummy_out2 = m.Output('dummy_out2', width)
24+
dummy_in0 = m.Input('dummy_in0', width)
25+
dummy_in1 = m.Input('dummy_in1', width)
26+
dummy_in2 = m.Input('dummy_in2', width)
27+
28+
count = m.Reg('count', 32)
29+
30+
m.Always(Posedge(clk))(
31+
If(rst)(
32+
count(0)
33+
).Else(
34+
If(count == 1023)(
35+
count(0)
36+
).Else(
37+
count(count + 1)
38+
)
39+
))
40+
41+
m.Always(Posedge(clk))(
42+
If(rst)(
43+
led(0)
44+
).Else(
45+
If(count == 1023)(
46+
led(led + 1)
47+
)
48+
))
49+
50+
return m
51+
52+
53+
def mkTop():
54+
m = Module('top')
55+
width = m.Parameter('WIDTH', 8)
56+
clk = m.Input('CLK')
57+
rst = m.Input('RST')
58+
led = m.Output('LED', width)
59+
60+
sub = Submodule(m, mkLed(), 'inst_blinkled',
61+
arg_params=(('WIDTH', width),),
62+
arg_ports=(('LED', led), ('CLK', clk), ('RST', rst)),
63+
as_io=('dummy_out0', 'dummy_in0'), as_wire=('dummy_out1', 'dummy_in1'))
64+
65+
return m
66+
67+
if __name__ == '__main__':
68+
top = mkTop()
69+
verilog = top.to_verilog()
70+
print(verilog)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import veriloggen
4+
import submodule_named_args
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:0] inst_blinkled_dummy_out0,
16+
input [WIDTH-1:0] inst_blinkled_dummy_in0
17+
);
18+
19+
wire [WIDTH-1:0] inst_blinkled_dummy_out2;
20+
reg [WIDTH-1:0] inst_blinkled_dummy_in2;
21+
wire [WIDTH-1:0] inst_blinkled_dummy_out1;
22+
wire [WIDTH-1:0] inst_blinkled_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(inst_blinkled_dummy_out0),
34+
.dummy_out1(inst_blinkled_dummy_out1),
35+
.dummy_out2(inst_blinkled_dummy_out2),
36+
.dummy_in0(inst_blinkled_dummy_in0),
37+
.dummy_in1(inst_blinkled_dummy_in1),
38+
.dummy_in2(inst_blinkled_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+
def test():
92+
test_module = submodule_named_args.mkTop()
93+
code = test_module.to_verilog()
94+
95+
from pyverilog.vparser.parser import VerilogParser
96+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
97+
parser = VerilogParser()
98+
expected_ast = parser.parse(expected_verilog)
99+
codegen = ASTCodeGenerator()
100+
expected_code = codegen.visit(expected_ast)
101+
102+
assert(expected_code == code)
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
14+
def mkLed():
15+
m = Module('blinkled')
16+
width = m.Parameter('WIDTH', 8)
17+
clk = m.Input('CLK')
18+
rst = m.Input('RST')
19+
led = m.OutputReg('LED', width)
20+
21+
dummy_out0 = m.Output('dummy_out0', width)
22+
dummy_out1 = m.Output('dummy_out1', width)
23+
dummy_out2 = m.Output('dummy_out2', width)
24+
dummy_in0 = m.Input('dummy_in0', width)
25+
dummy_in1 = m.Input('dummy_in1', width)
26+
dummy_in2 = m.Input('dummy_in2', width)
27+
28+
count = m.Reg('count', 32)
29+
30+
m.Always(Posedge(clk))(
31+
If(rst)(
32+
count(0)
33+
).Else(
34+
If(count == 1023)(
35+
count(0)
36+
).Else(
37+
count(count + 1)
38+
)
39+
))
40+
41+
m.Always(Posedge(clk))(
42+
If(rst)(
43+
led(0)
44+
).Else(
45+
If(count == 1023)(
46+
led(led + 1)
47+
)
48+
))
49+
50+
return m
51+
52+
53+
def mkTop():
54+
m = Module('top')
55+
width = m.Parameter('WIDTH', 8)
56+
clk = m.Input('CLK')
57+
rst = m.Input('RST')
58+
led = m.Output('LED', width)
59+
60+
sub = Submodule(m, mkLed(), 'inst_blinkled',
61+
arg_params=(width,),
62+
arg_ports=(clk, rst, led),
63+
as_io=('dummy_out0', 'dummy_in0'), as_wire=('dummy_out1', 'dummy_in1'))
64+
65+
return m
66+
67+
if __name__ == '__main__':
68+
top = mkTop()
69+
verilog = top.to_verilog()
70+
print(verilog)

0 commit comments

Comments
 (0)