Skip to content

Commit 27453ad

Browse files
committed
manyled
1 parent 8e8f52d commit 27453ad

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed

sample/manyled/Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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

sample/manyled/led.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
11+
# function to add an LED port
12+
def add_led(m, postfix, limit=1024):
13+
led = m.OutputReg('LED'+postfix, width)
14+
count = m.Reg('count'+postfix, 32)
15+
16+
m.Always(Posedge(clk))(
17+
If(rst)(
18+
count(0)
19+
).Else(
20+
If(count == limit - 1)(
21+
count(0)
22+
).Else(
23+
count(count + 1)
24+
)
25+
))
26+
27+
m.Always(Posedge(clk))(
28+
If(rst)(
29+
led(0)
30+
).Else(
31+
If(count == limit - 1)(
32+
led(led + 1)
33+
)
34+
))
35+
36+
# call 'add_led' to add LED ports
37+
for i in range(4):
38+
add_led(m, '_' + str(i), limit=i*10 + 10)
39+
40+
return m
41+
42+
if __name__ == '__main__':
43+
led = mkLed()
44+
# led.to_verilog(filename='tmp.v')
45+
verilog = led.to_verilog()
46+
print(verilog)

sample/manyled/test_led.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import led
2+
3+
expected_verilog = """
4+
module blinkled #
5+
(
6+
parameter WIDTH = 8
7+
)
8+
(
9+
input CLK,
10+
input RST,
11+
output reg [WIDTH-1:0] LED_0,
12+
output reg [WIDTH-1:0] LED_1,
13+
output reg [WIDTH-1:0] LED_2,
14+
output reg [WIDTH-1:0] LED_3
15+
);
16+
reg [32-1:0] count_0;
17+
reg [32-1:0] count_1;
18+
reg [32-1:0] count_2;
19+
reg [32-1:0] count_3;
20+
always @(posedge CLK) begin
21+
if(RST) begin
22+
count_0 <= 0;
23+
end else begin
24+
if(count_0 == 9) begin
25+
count_0 <= 0;
26+
end else begin
27+
count_0 <= count_0 + 1;
28+
end
29+
end
30+
end
31+
always @(posedge CLK) begin
32+
if(RST) begin
33+
LED_0 <= 0;
34+
end else begin
35+
if(count_0 == 9) begin
36+
LED_0 <= LED_0 + 1;
37+
end
38+
end
39+
end
40+
always @(posedge CLK) begin
41+
if(RST) begin
42+
count_1 <= 0;
43+
end else begin
44+
if(count_1 == 19) begin
45+
count_1 <= 0;
46+
end else begin
47+
count_1 <= count_1 + 1;
48+
end
49+
end
50+
end
51+
always @(posedge CLK) begin
52+
if(RST) begin
53+
LED_1 <= 0;
54+
end else begin
55+
if(count_1 == 19) begin
56+
LED_1 <= LED_1 + 1;
57+
end
58+
end
59+
end
60+
always @(posedge CLK) begin
61+
if(RST) begin
62+
count_2 <= 0;
63+
end else begin
64+
if(count_2 == 29) begin
65+
count_2 <= 0;
66+
end else begin
67+
count_2 <= count_2 + 1;
68+
end
69+
end
70+
end
71+
always @(posedge CLK) begin
72+
if(RST) begin
73+
LED_2 <= 0;
74+
end else begin
75+
if(count_2 == 29) begin
76+
LED_2 <= LED_2 + 1;
77+
end
78+
end
79+
end
80+
always @(posedge CLK) begin
81+
if(RST) begin
82+
count_3 <= 0;
83+
end else begin
84+
if(count_3 == 39) begin
85+
count_3 <= 0;
86+
end else begin
87+
count_3 <= count_3 + 1;
88+
end
89+
end
90+
end
91+
always @(posedge CLK) begin
92+
if(RST) begin
93+
LED_3 <= 0;
94+
end else begin
95+
if(count_3 == 39) begin
96+
LED_3 <= LED_3 + 1;
97+
end
98+
end
99+
end
100+
endmodule
101+
"""
102+
103+
def test_led():
104+
led_module = led.mkLed()
105+
led_code = led_module.to_verilog()
106+
107+
from pyverilog.vparser.parser import VerilogParser
108+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
109+
parser = VerilogParser()
110+
expected_ast = parser.parse(expected_verilog)
111+
codegen = ASTCodeGenerator()
112+
expected_code = codegen.visit(expected_ast)
113+
114+
assert(led_code == expected_code)

sample/manyled/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)