Skip to content

Commit 687efb9

Browse files
committed
Submodule for simulation
1 parent 01588bd commit 687efb9

File tree

4 files changed

+305
-0
lines changed

4 files changed

+305
-0
lines changed

tests/core/submodule_/sim/Makefile

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: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
m.Always(Posedge(clk))(
51+
If(rst)(
52+
).Else(
53+
Systask('display', "LED:%d count:%d", led, count)
54+
))
55+
56+
return m
57+
58+
59+
def mkTop():
60+
m = Module('top')
61+
width = m.Parameter('WIDTH', 8)
62+
clk = m.Input('CLK')
63+
rst = m.Input('RST')
64+
led = m.Output('LED', width)
65+
66+
sub = Submodule(m, mkLed(), 'inst_blinkled',
67+
arg_params=(('WIDTH', width),),
68+
arg_ports=(('LED', led), ('CLK', clk), ('RST', rst)),
69+
as_io=('dummy_out0', 'dummy_in0'), as_wire=('dummy_out1', 'dummy_in1'))
70+
71+
return m
72+
73+
74+
def mkTest():
75+
m = Module('test')
76+
77+
# target instance
78+
led = mkTop()
79+
uut = Submodule(m, led)
80+
81+
clk = uut['CLK']
82+
rst = uut['RST']
83+
84+
simulation.setup_waveform(m, uut, m.get_vars())
85+
simulation.setup_clock(m, clk, hperiod=5)
86+
init = simulation.setup_reset(m, rst, m.make_reset(), period=100)
87+
88+
init.add(
89+
Delay(1000 * 100),
90+
Systask('finish'),
91+
)
92+
93+
return m
94+
95+
if __name__ == '__main__':
96+
test = mkTest()
97+
verilog = test.to_verilog(filename='tmp.v')
98+
#verilog = test.to_verilog()
99+
print(verilog)
100+
101+
sim = simulation.Simulator(test)
102+
rslt = sim.run()
103+
print(rslt)
104+
105+
# sim.view_waveform()
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import veriloggen
4+
import submodule_sim
5+
6+
expected_verilog = """
7+
module test #
8+
(
9+
parameter inst_top_WIDTH = 8
10+
)
11+
(
12+
13+
);
14+
15+
reg inst_top_CLK;
16+
reg inst_top_RST;
17+
wire [inst_top_WIDTH-1:0] inst_top_LED;
18+
wire [inst_top_WIDTH-1:0] inst_top_inst_blinkled_dummy_out0;
19+
reg [inst_top_WIDTH-1:0] inst_top_inst_blinkled_dummy_in0;
20+
21+
top
22+
#(
23+
.WIDTH(inst_top_WIDTH)
24+
)
25+
inst_top
26+
(
27+
.CLK(inst_top_CLK),
28+
.RST(inst_top_RST),
29+
.LED(inst_top_LED),
30+
.inst_blinkled_dummy_out0(inst_top_inst_blinkled_dummy_out0),
31+
.inst_blinkled_dummy_in0(inst_top_inst_blinkled_dummy_in0)
32+
);
33+
34+
35+
initial begin
36+
$dumpfile("uut.vcd");
37+
$dumpvars(0, inst_top, inst_top_CLK, inst_top_RST, inst_top_LED, inst_top_inst_blinkled_dummy_out0, inst_top_inst_blinkled_dummy_in0);
38+
end
39+
40+
41+
initial begin
42+
inst_top_CLK = 0;
43+
forever begin
44+
#5 inst_top_CLK = !inst_top_CLK;
45+
end
46+
end
47+
48+
49+
initial begin
50+
inst_top_RST = 0;
51+
#100;
52+
inst_top_RST = 1;
53+
#100;
54+
inst_top_RST = 0;
55+
#100000;
56+
$finish;
57+
end
58+
59+
60+
endmodule
61+
62+
63+
64+
module top #
65+
(
66+
parameter WIDTH = 8
67+
)
68+
(
69+
input CLK,
70+
input RST,
71+
output [WIDTH-1:0] LED,
72+
output [WIDTH-1:0] inst_blinkled_dummy_out0,
73+
input [WIDTH-1:0] inst_blinkled_dummy_in0
74+
);
75+
76+
wire [WIDTH-1:0] inst_blinkled_dummy_out2;
77+
reg [WIDTH-1:0] inst_blinkled_dummy_in2;
78+
wire [WIDTH-1:0] inst_blinkled_dummy_out1;
79+
wire [WIDTH-1:0] inst_blinkled_dummy_in1;
80+
81+
blinkled
82+
#(
83+
.WIDTH(WIDTH)
84+
)
85+
inst_blinkled
86+
(
87+
.CLK(CLK),
88+
.RST(RST),
89+
.LED(LED),
90+
.dummy_out0(inst_blinkled_dummy_out0),
91+
.dummy_out1(inst_blinkled_dummy_out1),
92+
.dummy_out2(inst_blinkled_dummy_out2),
93+
.dummy_in0(inst_blinkled_dummy_in0),
94+
.dummy_in1(inst_blinkled_dummy_in1),
95+
.dummy_in2(inst_blinkled_dummy_in2)
96+
);
97+
98+
99+
endmodule
100+
101+
102+
103+
module blinkled #
104+
(
105+
parameter WIDTH = 8
106+
)
107+
(
108+
input CLK,
109+
input RST,
110+
output reg [WIDTH-1:0] LED,
111+
output [WIDTH-1:0] dummy_out0,
112+
output [WIDTH-1:0] dummy_out1,
113+
output [WIDTH-1:0] dummy_out2,
114+
input [WIDTH-1:0] dummy_in0,
115+
input [WIDTH-1:0] dummy_in1,
116+
input [WIDTH-1:0] dummy_in2
117+
);
118+
119+
reg [32-1:0] count;
120+
121+
always @(posedge CLK) begin
122+
if(RST) begin
123+
count <= 0;
124+
end else begin
125+
if(count == 1023) begin
126+
count <= 0;
127+
end else begin
128+
count <= count + 1;
129+
end
130+
end
131+
end
132+
133+
134+
always @(posedge CLK) begin
135+
if(RST) begin
136+
LED <= 0;
137+
end else begin
138+
if(count == 1023) begin
139+
LED <= LED + 1;
140+
end
141+
end
142+
end
143+
144+
145+
always @(posedge CLK) begin
146+
if(RST) begin
147+
end else begin
148+
$display("LED:%d count:%d", LED, count);
149+
end
150+
end
151+
152+
153+
endmodule
154+
"""
155+
156+
157+
def test():
158+
test_module = submodule_sim.mkTest()
159+
code = test_module.to_verilog()
160+
161+
from pyverilog.vparser.parser import VerilogParser
162+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
163+
parser = VerilogParser()
164+
expected_ast = parser.parse(expected_verilog)
165+
codegen = ASTCodeGenerator()
166+
expected_code = codegen.visit(expected_ast)
167+
168+
assert(expected_code == code)

veriloggen/simulation/simulation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import veriloggen.core.vtypes as vtypes
99
import veriloggen.core.module as module
10+
import veriloggen.core.submodule as submodule
1011

1112

1213
def setup_waveform(m, *uuts):
@@ -23,6 +24,8 @@ def setup_waveform(m, *uuts):
2324
if isinstance(u, vtypes._Variable) and u.length is not None:
2425
continue
2526
new_uuts.append(u)
27+
elif isinstance(uut, submodule.Submodule):
28+
new_uuts.append(uut.inst)
2629
else:
2730
if isinstance(uut, vtypes._Variable) and uut.length is not None:
2831
continue

0 commit comments

Comments
 (0)