Skip to content

Commit 7e1b35e

Browse files
committed
A new test case is added: ternary operator
1 parent a20268b commit 7e1b35e

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

sample/tests/cond/Makefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
TARGET=led.py
2+
TEST=test_led.py
3+
ARGS=
4+
5+
PYTHON=python3
6+
#PYTHON=python
7+
#OPT=-m pdb
8+
#OPT=-m cProfile -s time
9+
#OPT=-m cProfile -o profile.rslt
10+
11+
.PHONY: all
12+
all: test
13+
14+
.PHONY: run
15+
run:
16+
$(PYTHON) $(OPT) $(TARGET) $(ARGS)
17+
18+
.PHONY: test
19+
test:
20+
$(PYTHON) -m pytest -vv $(TEST)
21+
22+
.PHONY: check
23+
check:
24+
$(PYTHON) $(OPT) $(TARGET) $(ARGS) > tmp.v
25+
iverilog -tnull -Wall tmp.v
26+
rm -f tmp.v
27+
28+
.PHONY: clean
29+
clean:
30+
rm -rf *.pyc __pycache__ parsetab.py *.out tmp.v uut.vcd
31+
32+
.PHONY: sim
33+
sim:
34+
iverilog -Wall tmp.v
35+
./a.out
36+
37+
.PHONY: view
38+
view:
39+
gtkwave --giga uut.vcd &

sample/tests/cond/led.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import sys
2+
import os
3+
from veriloggen import *
4+
5+
def mkLed():
6+
m = Module('blinkled')
7+
width = m.Parameter('WIDTH', 8)
8+
clk = m.Input('CLK')
9+
rst = m.Input('RST')
10+
led = m.OutputReg('LED', width)
11+
count = m.Reg('count', 32)
12+
13+
m.Always(Posedge(clk))(
14+
If(rst)(
15+
count(0)
16+
).Else(
17+
count( Cond(count==1023, 0, count + 1) )
18+
))
19+
20+
m.Always(Posedge(clk))(
21+
If(rst)(
22+
led(0)
23+
).Else(
24+
led( Cond(count==1024-1, led+1, led) )
25+
))
26+
27+
return m
28+
29+
def mkTest():
30+
m = Module('test')
31+
32+
# target instance
33+
led = mkLed()
34+
35+
# copy paras and ports
36+
params = m.copy_params(led)
37+
ports = m.copy_sim_ports(led)
38+
39+
clk = ports['CLK']
40+
rst = ports['RST']
41+
42+
uut = m.Instance(led, 'uut',
43+
params=m.connect_params(led),
44+
ports=m.connect_ports(led))
45+
46+
lib.simulation.setup_waveform(m, uut, m.get_vars())
47+
lib.simulation.setup_clock(m, clk, hperiod=5)
48+
init = lib.simulation.setup_reset(m, rst, m.make_reset(), period=100)
49+
50+
init.add(
51+
Delay(1000 * 100),
52+
Systask('finish'),
53+
)
54+
55+
return m
56+
57+
if __name__ == '__main__':
58+
test = mkTest()
59+
verilog = test.to_verilog('tmp.v')
60+
print(verilog)

sample/tests/cond/test_led.py

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

sample/tests/cond/veriloggen

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../veriloggen

0 commit comments

Comments
 (0)