Skip to content

Commit 89e68b3

Browse files
committed
_list
1 parent c652fba commit 89e68b3

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

tests/core/_list/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

tests/core/_list/_list.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.dirname(os.path.abspath(__file__))))))
8+
9+
from veriloggen import *
10+
11+
def mkLed():
12+
m = Module('blinkled')
13+
width = m.Parameter('WIDTH', 8)
14+
clk = m.Input('CLK')
15+
rst = m.Input('RST')
16+
led = [ m.OutputReg('LED_%d' % i, width) for i in range(8) ]
17+
count = m.Reg('count', 32)
18+
19+
m.Always(Posedge(clk))(
20+
If(rst)(
21+
count(0)
22+
).Else(
23+
If(count == 1023)(
24+
count(0)
25+
).Else(
26+
count(count + 1)
27+
)
28+
))
29+
30+
case_body = [ When(i)( led[i](count[width-1:0], blk=True) ) for i in range(8) ]
31+
32+
m.Always()(
33+
Case(count % 8)(
34+
*case_body
35+
)
36+
)
37+
38+
return m
39+
40+
if __name__ == '__main__':
41+
led = mkLed()
42+
verilog = led.to_verilog()
43+
print(verilog)

tests/core/_list/test__list.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import _list
4+
5+
expected_verilog = """
6+
module blinkled #
7+
(
8+
parameter WIDTH = 8
9+
)
10+
(
11+
input CLK,
12+
input RST,
13+
output reg [WIDTH-1:0] LED_0,
14+
output reg [WIDTH-1:0] LED_1,
15+
output reg [WIDTH-1:0] LED_2,
16+
output reg [WIDTH-1:0] LED_3,
17+
output reg [WIDTH-1:0] LED_4,
18+
output reg [WIDTH-1:0] LED_5,
19+
output reg [WIDTH-1:0] LED_6,
20+
output reg [WIDTH-1:0] LED_7
21+
);
22+
23+
reg [32-1:0] count;
24+
25+
always @(posedge CLK) begin
26+
if(RST) begin
27+
count <= 0;
28+
end else begin
29+
if(count == 1023) begin
30+
count <= 0;
31+
end else begin
32+
count <= count + 1;
33+
end
34+
end
35+
end
36+
37+
38+
always @(*) begin
39+
case(count % 8)
40+
0: begin
41+
LED_0 = count[WIDTH-1:0];
42+
end
43+
1: begin
44+
LED_1 = count[WIDTH-1:0];
45+
end
46+
2: begin
47+
LED_2 = count[WIDTH-1:0];
48+
end
49+
3: begin
50+
LED_3 = count[WIDTH-1:0];
51+
end
52+
4: begin
53+
LED_4 = count[WIDTH-1:0];
54+
end
55+
5: begin
56+
LED_5 = count[WIDTH-1:0];
57+
end
58+
6: begin
59+
LED_6 = count[WIDTH-1:0];
60+
end
61+
7: begin
62+
LED_7 = count[WIDTH-1:0];
63+
end
64+
endcase
65+
end
66+
endmodule
67+
"""
68+
69+
def test():
70+
test_module = _list.mkLed()
71+
code = test_module.to_verilog()
72+
73+
from pyverilog.vparser.parser import VerilogParser
74+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
75+
parser = VerilogParser()
76+
expected_ast = parser.parse(expected_verilog)
77+
codegen = ASTCodeGenerator()
78+
expected_code = codegen.visit(expected_ast)
79+
80+
assert(expected_code == code)

0 commit comments

Comments
 (0)