Skip to content

Commit cd22eb3

Browse files
committed
lib_simulation_simulator is added: executing a simulation of Veriloggen hardware module on Python with Icarus.
1 parent 580120d commit cd22eb3

File tree

3 files changed

+312
-0
lines changed

3 files changed

+312
-0
lines changed
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
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sys
2+
import os
3+
4+
# the next line can be removed after installation
5+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
6+
7+
from veriloggen import *
8+
9+
def mkLed():
10+
m = Module('blinkled')
11+
width = m.Parameter('WIDTH', 8)
12+
clk = m.Input('CLK')
13+
rst = m.Input('RST')
14+
led = m.OutputReg('LED', width)
15+
count = m.Reg('count', 32)
16+
17+
m.Always(Posedge(clk))(
18+
If(rst)(
19+
count(0)
20+
).Else(
21+
If(count == 16 - 1)(
22+
count(0)
23+
).Else(
24+
count(count + 1)
25+
)
26+
))
27+
28+
m.Always(Posedge(clk))(
29+
If(rst)(
30+
led(0)
31+
).Else(
32+
Systask('display', 'LED:%d count:%d', led, count),
33+
If(count == 16 - 1)(
34+
led(led + 1)
35+
)
36+
))
37+
38+
return m
39+
40+
def mkTest():
41+
m = Module('test')
42+
width = m.Parameter('WIDTH', 8)
43+
clk = m.Reg('CLK')
44+
rst = m.Reg('RST')
45+
led = m.Wire('LED', width)
46+
47+
uut = m.Instance(mkLed(), 'uut',
48+
params=connect_same_name(width),
49+
ports=connect_same_name(clk, rst, led))
50+
51+
lib.simulation.setup_waveform(m, uut)
52+
lib.simulation.setup_clock(m, clk, hperiod=5)
53+
init = lib.simulation.setup_reset(m, rst, period=100)
54+
55+
init.add(
56+
Delay(1000),
57+
Systask('finish'),
58+
)
59+
60+
return m
61+
62+
if __name__ == '__main__':
63+
test = mkTest()
64+
verilog = test.to_verilog()
65+
print(verilog)
66+
sim = lib.simulation.Simulator(test)
67+
rslt = sim.run()
68+
print(rslt)
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
import lib_simulation_simulator
2+
from veriloggen import *
3+
4+
expected_verilog = """
5+
module test #
6+
(
7+
parameter WIDTH = 8
8+
)
9+
(
10+
);
11+
12+
reg CLK;
13+
reg RST;
14+
wire [WIDTH-1:0] LED;
15+
16+
blinkled #
17+
(
18+
.WIDTH(WIDTH)
19+
)
20+
uut
21+
(
22+
.CLK(CLK),
23+
.RST(RST),
24+
.LED(LED)
25+
);
26+
27+
initial begin
28+
$dumpfile("uut.vcd");
29+
$dumpvars(0, uut);
30+
end
31+
32+
initial begin
33+
CLK = 0;
34+
forever begin
35+
#5 CLK = !CLK;
36+
end
37+
end
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+
endmodule
50+
51+
module blinkled #
52+
(
53+
parameter WIDTH = 8
54+
)
55+
(
56+
input CLK,
57+
input RST,
58+
output reg [WIDTH-1:0] LED
59+
);
60+
reg [32-1:0] count;
61+
always @(posedge CLK) begin
62+
if(RST) begin
63+
count <= 0;
64+
end else begin
65+
if(count == 15) begin
66+
count <= 0;
67+
end else begin
68+
count <= count + 1;
69+
end
70+
end
71+
end
72+
always @(posedge CLK) begin
73+
if(RST) begin
74+
LED <= 0;
75+
end else begin
76+
$display("LED:%d count:%d", LED, count);
77+
if(count == 15) begin
78+
LED <= LED + 1;
79+
end
80+
end
81+
end
82+
endmodule
83+
"""
84+
85+
expected_rslt = """\
86+
VCD info: dumpfile uut.vcd opened for output.
87+
LED: x count: x
88+
LED: x count: x
89+
LED: x count: x
90+
LED: x count: x
91+
LED: x count: x
92+
LED: x count: x
93+
LED: x count: x
94+
LED: x count: x
95+
LED: x count: x
96+
LED: x count: x
97+
LED: 0 count: 0
98+
LED: 0 count: 1
99+
LED: 0 count: 2
100+
LED: 0 count: 3
101+
LED: 0 count: 4
102+
LED: 0 count: 5
103+
LED: 0 count: 6
104+
LED: 0 count: 7
105+
LED: 0 count: 8
106+
LED: 0 count: 9
107+
LED: 0 count: 10
108+
LED: 0 count: 11
109+
LED: 0 count: 12
110+
LED: 0 count: 13
111+
LED: 0 count: 14
112+
LED: 0 count: 15
113+
LED: 1 count: 0
114+
LED: 1 count: 1
115+
LED: 1 count: 2
116+
LED: 1 count: 3
117+
LED: 1 count: 4
118+
LED: 1 count: 5
119+
LED: 1 count: 6
120+
LED: 1 count: 7
121+
LED: 1 count: 8
122+
LED: 1 count: 9
123+
LED: 1 count: 10
124+
LED: 1 count: 11
125+
LED: 1 count: 12
126+
LED: 1 count: 13
127+
LED: 1 count: 14
128+
LED: 1 count: 15
129+
LED: 2 count: 0
130+
LED: 2 count: 1
131+
LED: 2 count: 2
132+
LED: 2 count: 3
133+
LED: 2 count: 4
134+
LED: 2 count: 5
135+
LED: 2 count: 6
136+
LED: 2 count: 7
137+
LED: 2 count: 8
138+
LED: 2 count: 9
139+
LED: 2 count: 10
140+
LED: 2 count: 11
141+
LED: 2 count: 12
142+
LED: 2 count: 13
143+
LED: 2 count: 14
144+
LED: 2 count: 15
145+
LED: 3 count: 0
146+
LED: 3 count: 1
147+
LED: 3 count: 2
148+
LED: 3 count: 3
149+
LED: 3 count: 4
150+
LED: 3 count: 5
151+
LED: 3 count: 6
152+
LED: 3 count: 7
153+
LED: 3 count: 8
154+
LED: 3 count: 9
155+
LED: 3 count: 10
156+
LED: 3 count: 11
157+
LED: 3 count: 12
158+
LED: 3 count: 13
159+
LED: 3 count: 14
160+
LED: 3 count: 15
161+
LED: 4 count: 0
162+
LED: 4 count: 1
163+
LED: 4 count: 2
164+
LED: 4 count: 3
165+
LED: 4 count: 4
166+
LED: 4 count: 5
167+
LED: 4 count: 6
168+
LED: 4 count: 7
169+
LED: 4 count: 8
170+
LED: 4 count: 9
171+
LED: 4 count: 10
172+
LED: 4 count: 11
173+
LED: 4 count: 12
174+
LED: 4 count: 13
175+
LED: 4 count: 14
176+
LED: 4 count: 15
177+
LED: 5 count: 0
178+
LED: 5 count: 1
179+
LED: 5 count: 2
180+
LED: 5 count: 3
181+
LED: 5 count: 4
182+
LED: 5 count: 5
183+
LED: 5 count: 6
184+
LED: 5 count: 7
185+
LED: 5 count: 8
186+
LED: 5 count: 9
187+
LED: 5 count: 10
188+
LED: 5 count: 11
189+
LED: 5 count: 12
190+
LED: 5 count: 13
191+
LED: 5 count: 14
192+
LED: 5 count: 15
193+
LED: 6 count: 0
194+
LED: 6 count: 1
195+
LED: 6 count: 2
196+
LED: 6 count: 3
197+
"""
198+
199+
def test():
200+
test_module = lib_simulation_simulator.mkTest()
201+
code = test_module.to_verilog()
202+
203+
from pyverilog.vparser.parser import VerilogParser
204+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
205+
parser = VerilogParser()
206+
expected_ast = parser.parse(expected_verilog)
207+
codegen = ASTCodeGenerator()
208+
expected_code = codegen.visit(expected_ast)
209+
210+
assert(expected_code == code)
211+
212+
sim = lib.simulation.Simulator(test_module)
213+
rslt = sim.run()
214+
215+
assert(expected_rslt == rslt)

0 commit comments

Comments
 (0)