Skip to content

Commit 8822904

Browse files
committed
Merge branch 'fix_reset_visitor'
2 parents 5a71c9c + 5f93669 commit 8822904

File tree

4 files changed

+223
-2
lines changed

4 files changed

+223
-2
lines changed
+29
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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(
8+
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))))
9+
10+
from veriloggen import *
11+
12+
13+
def mkLed():
14+
m = Module('blinkled')
15+
interval = m.Parameter('INTERVAL', 16)
16+
clk = m.Input('CLK')
17+
rst = m.Input('RST')
18+
led = m.OutputReg('LED', 8, initval=0)
19+
20+
count0 = m.Reg('count0', 4, initval=0)
21+
count1 = m.Reg('count1', 4, initval=0)
22+
23+
seq = Seq(m, 'seq', clk, rst)
24+
25+
seq(
26+
Systask('display', 'LED:%d count0:%d count1:%d', led, count0, count1)
27+
)
28+
29+
seq.If(Cat(count0, count1) < interval-1)(
30+
Cat(count0, count1[0:4])(Cat(count0, count1) + 1)
31+
)
32+
seq.If(Cat(count0, count1) == interval-1)(
33+
Cat(count0, count1[0:4])(0)
34+
)
35+
seq.If(Cat(count0, count1) == interval-1)(
36+
led(led + 1)
37+
)
38+
39+
seq.make_always()
40+
41+
return m
42+
43+
44+
def mkTest():
45+
m = Module('test')
46+
47+
# target instance
48+
led = mkLed()
49+
50+
# copy paras and ports
51+
params = m.copy_params(led)
52+
ports = m.copy_sim_ports(led)
53+
54+
clk = ports['CLK']
55+
rst = ports['RST']
56+
57+
uut = m.Instance(led, 'uut',
58+
params=m.connect_params(led),
59+
ports=m.connect_ports(led))
60+
61+
#simulation.setup_waveform(m, uut)
62+
simulation.setup_clock(m, clk, hperiod=5)
63+
init = simulation.setup_reset(m, rst, m.make_reset(), period=100)
64+
65+
init.add(
66+
Delay(1000),
67+
Systask('finish'),
68+
)
69+
70+
return m
71+
72+
73+
if __name__ == '__main__':
74+
test = mkTest()
75+
verilog = test.to_verilog()
76+
print(verilog)
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 seq_countup_cat
5+
6+
expected_verilog = """
7+
module test #
8+
(
9+
parameter INTERVAL = 16
10+
)
11+
(
12+
13+
);
14+
15+
reg CLK;
16+
reg RST;
17+
wire [8-1:0] LED;
18+
19+
blinkled
20+
#(
21+
.INTERVAL(INTERVAL)
22+
)
23+
uut
24+
(
25+
.CLK(CLK),
26+
.RST(RST),
27+
.LED(LED)
28+
);
29+
30+
31+
initial begin
32+
CLK = 0;
33+
forever begin
34+
#5 CLK = !CLK;
35+
end
36+
end
37+
38+
39+
initial begin
40+
RST = 0;
41+
#100;
42+
RST = 1;
43+
#100;
44+
RST = 0;
45+
#1000;
46+
$finish;
47+
end
48+
49+
50+
endmodule
51+
52+
53+
54+
module blinkled #
55+
(
56+
parameter INTERVAL = 16
57+
)
58+
(
59+
input CLK,
60+
input RST,
61+
output reg [8-1:0] LED
62+
);
63+
64+
reg [4-1:0] count0;
65+
reg [4-1:0] count1;
66+
localparam [4-1:0] _tmp_0 = (0 >> 0) & { 4{ 1'd1 } };
67+
68+
always @(posedge CLK) begin
69+
if(RST) begin
70+
{ count0, count1[3:0] } <= { 4'd0, _tmp_0 };
71+
LED <= 0;
72+
end else begin
73+
$display("LED:%d count0:%d count1:%d", LED, count0, count1);
74+
if({ count0, count1 } < INTERVAL - 1) begin
75+
{ count0, count1[3:0] } <= { count0, count1 } + 1;
76+
end
77+
if({ count0, count1 } == INTERVAL - 1) begin
78+
{ count0, count1[3:0] } <= 0;
79+
end
80+
if({ count0, count1 } == INTERVAL - 1) begin
81+
LED <= LED + 1;
82+
end
83+
end
84+
end
85+
86+
87+
endmodule
88+
"""
89+
90+
91+
def test():
92+
veriloggen.reset()
93+
test_module = seq_countup_cat.mkTest()
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/seq/reset_visitor.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import sys
6+
import copy
67

78
import veriloggen.core.vtypes as vtypes
89

@@ -57,9 +58,21 @@ def visit_Slice(self, node):
5758
def visit_Cat(self, node):
5859
left_values = []
5960
right_values = []
61+
6062
for v in node.vars:
6163
val = self.visit(v)
62-
right = vtypes.IntX() if val is None else val.right
64+
width = v.bit_length()
65+
if val is None:
66+
right = vtypes.IntX(width)
67+
elif isinstance(val.right, int):
68+
right = vtypes.Int(val.right, width)
69+
elif isinstance(val.right, vtypes._Constant):
70+
right = copy.deepcopy(val.right)
71+
right.width = width
72+
else:
73+
right = v._get_module().TmpLocalparam(val.right, width)
74+
6375
left_values.append(v)
6476
right_values.append(right)
65-
return vtypes.Subst(vtypes.Cat(tuple(left_values)), vtypes.Cat(tuple(right_values)))
77+
78+
return vtypes.Subst(vtypes.Cat(*left_values), vtypes.Cat(*right_values))

0 commit comments

Comments
 (0)