Skip to content

Commit fe70e64

Browse files
committed
New test case "parameter"
1 parent 58eb19c commit fe70e64

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

tests/parameter/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
TARGET=*.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

tests/parameter/parameter.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import sys
4+
import os
5+
6+
# the next line can be removed after installation
7+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
8+
9+
from veriloggen import *
10+
11+
def mkLed():
12+
m = Module('blinkled')
13+
inc = m.Localparam('INC', Int(1) + Int(2) * Int(3))
14+
width = m.Parameter('WIDTH', Int(1) * (Int(2) + Int(3)))
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 + Int(1))
28+
)
29+
))
30+
31+
m.Always(Posedge(clk))(
32+
If(rst)(
33+
led(0)
34+
).Else(
35+
If(count == 1024 - 1)(
36+
led(led + inc)
37+
)
38+
))
39+
40+
return m
41+
42+
def mkTest():
43+
m = Module('test')
44+
45+
# target instance
46+
led = mkLed()
47+
48+
# copy paras and ports
49+
params = m.copy_params(led)
50+
ports = m.copy_sim_ports(led)
51+
52+
clk = ports['CLK']
53+
rst = ports['RST']
54+
55+
uut = m.Instance(led, 'uut',
56+
params=m.connect_params(led),
57+
ports=m.connect_ports(led))
58+
59+
lib.simulation.setup_waveform(m, uut, m.get_vars())
60+
lib.simulation.setup_clock(m, clk, hperiod=5)
61+
init = lib.simulation.setup_reset(m, rst, m.make_reset(), period=100)
62+
63+
init.add(
64+
Delay(1000 * 100),
65+
Systask('finish'),
66+
)
67+
68+
return m
69+
70+
if __name__ == '__main__':
71+
test = mkTest()
72+
verilog = test.to_verilog('tmp.v')
73+
print(verilog)

tests/parameter/test_parameter.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import parameter
4+
5+
expected_verilog = """
6+
module test #
7+
(
8+
parameter WIDTH = 1 * (2 + 3)
9+
)
10+
(
11+
);
12+
13+
reg CLK;
14+
reg RST;
15+
wire [(WIDTH - 1):0] LED;
16+
17+
blinkled
18+
#(
19+
.WIDTH(WIDTH)
20+
)
21+
uut
22+
(
23+
.CLK(CLK),
24+
.RST(RST),
25+
.LED(LED)
26+
);
27+
28+
initial begin
29+
$dumpfile("uut.vcd");
30+
$dumpvars(0, uut, CLK, RST, LED);
31+
end
32+
33+
initial begin
34+
CLK = 0;
35+
forever begin
36+
#5 CLK = (!CLK);
37+
end
38+
end
39+
40+
initial begin
41+
RST = 0;
42+
#100;
43+
RST = 1;
44+
#100;
45+
RST = 0;
46+
#100000;
47+
$finish;
48+
end
49+
endmodule
50+
51+
module blinkled #
52+
(
53+
parameter WIDTH = 1 * (2 + 3)
54+
)
55+
(
56+
input CLK,
57+
input RST,
58+
output reg [WIDTH-1:0] LED
59+
);
60+
localparam INC = 1 + 2 * 3;
61+
reg [32-1:0] count;
62+
always @(posedge CLK) begin
63+
if(RST) begin
64+
count <= 0;
65+
end else begin
66+
if(count == 1023) begin
67+
count <= 0;
68+
end else begin
69+
count <= count + 1;
70+
end
71+
end
72+
end
73+
always @(posedge CLK) begin
74+
if(RST) begin
75+
LED <= 0;
76+
end else begin
77+
if(count == 1023) begin
78+
LED <= LED + INC;
79+
end
80+
end
81+
end
82+
endmodule
83+
"""
84+
85+
def test():
86+
test_module = parameter.mkTest()
87+
code = test_module.to_verilog()
88+
89+
from pyverilog.vparser.parser import VerilogParser
90+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
91+
parser = VerilogParser()
92+
expected_ast = parser.parse(expected_verilog)
93+
codegen = ASTCodeGenerator()
94+
expected_code = codegen.visit(expected_ast)
95+
96+
assert(expected_code == code)

0 commit comments

Comments
 (0)