Skip to content

Commit 045fe84

Browse files
committed
The functions of copy_ports and connect_ports are improved.
1 parent f488d29 commit 045fe84

File tree

4 files changed

+297
-29
lines changed

4 files changed

+297
-29
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 *.out tmp.v uut.vcd
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))))
9+
10+
from veriloggen import *
11+
12+
def mkLed():
13+
m = Module('blinkled')
14+
width = m.Parameter('WIDTH', 8)
15+
clk = m.Input('CLK')
16+
rst = m.Input('RST')
17+
led = m.OutputReg('LED', width)
18+
count = m.Reg('count', 32)
19+
20+
m.Always(Posedge(clk))(
21+
If(rst)(
22+
count(0)
23+
).Else(
24+
If(count == 1023)(
25+
count(0)
26+
).Else(
27+
count(count + 1)
28+
)
29+
))
30+
31+
m.Always(Posedge(clk))(
32+
If(rst)(
33+
led( 0 )
34+
).Else(
35+
If(count == 1023)(
36+
led( led + 1 )
37+
)
38+
))
39+
40+
return m
41+
42+
def mkTop():
43+
m = Module('top')
44+
led = mkLed()
45+
46+
clk = m.Input('CLK')
47+
rst = m.Input('RST')
48+
49+
params = m.copy_params(led, prefix='A_')
50+
ports = m.copy_ports(led, prefix='A_', exclude=('CLK', 'RST'))
51+
52+
params = m.copy_params(led, prefix='B_')
53+
ports = m.copy_ports(led, prefix='B_', exclude=('CLK', 'RST'))
54+
55+
m.Instance(led, 'inst_blinkled_a',
56+
m.connect_params(led, prefix='A_'),
57+
m.connect_ports(led, prefix='') + m.connect_ports(led, prefix='A_'))
58+
59+
60+
m.Instance(led, 'inst_blinkled_b',
61+
m.connect_params(led, prefix='B_'),
62+
m.connect_ports(led, prefix='') + m.connect_ports(led, prefix='B_'))
63+
64+
return m
65+
66+
if __name__ == '__main__':
67+
top = mkTop()
68+
verilog = top.to_verilog()
69+
print(verilog)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import multiple_instances
4+
5+
expected_verilog = """
6+
module top #
7+
(
8+
parameter A_WIDTH = 8,
9+
parameter B_WIDTH = 8
10+
)
11+
(
12+
input CLK,
13+
input RST,
14+
output [A_WIDTH-1:0] A_LED,
15+
output [B_WIDTH-1:0] B_LED
16+
);
17+
18+
blinkled
19+
#(
20+
.WIDTH(A_WIDTH)
21+
)
22+
inst_blinkled_a
23+
(
24+
.CLK(CLK),
25+
.RST(RST),
26+
.LED(A_LED)
27+
);
28+
29+
blinkled
30+
#(
31+
.WIDTH(B_WIDTH)
32+
)
33+
inst_blinkled_b
34+
(
35+
.CLK(CLK),
36+
.RST(RST),
37+
.LED(B_LED)
38+
);
39+
40+
endmodule
41+
42+
module blinkled #
43+
(
44+
parameter WIDTH = 8
45+
)
46+
(
47+
input CLK,
48+
input RST,
49+
output reg [WIDTH-1:0] LED
50+
);
51+
52+
reg [32-1:0] count;
53+
54+
always @(posedge CLK) begin
55+
if(RST) begin
56+
count <= 0;
57+
end else begin
58+
if(count == 1023) begin
59+
count <= 0;
60+
end else begin
61+
count <= count + 1;
62+
end
63+
end
64+
end
65+
66+
always @(posedge CLK) begin
67+
if(RST) begin
68+
LED <= 0;
69+
end else begin
70+
if(count == 1023) begin
71+
LED <= LED + 1;
72+
end
73+
end
74+
end
75+
76+
endmodule
77+
"""
78+
79+
def test_led():
80+
test_module = multiple_instances.mkTop()
81+
code = test_module.to_verilog()
82+
83+
from pyverilog.vparser.parser import VerilogParser
84+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
85+
parser = VerilogParser()
86+
expected_ast = parser.parse(expected_verilog)
87+
codegen = ASTCodeGenerator()
88+
expected_code = codegen.visit(expected_ast)
89+
90+
assert(expected_code == code)

0 commit comments

Comments
 (0)