Skip to content

Commit d241216

Browse files
committed
Fixed point is supported in veriloggen.thread
1 parent d7b7303 commit d241216

File tree

9 files changed

+600
-31
lines changed

9 files changed

+600
-31
lines changed
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: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
import veriloggen
4+
import thread_fixed_read
5+
6+
expected_verilog = """
7+
module test;
8+
9+
reg CLK;
10+
reg RST;
11+
12+
blinkled
13+
uut
14+
(
15+
.CLK(CLK),
16+
.RST(RST)
17+
);
18+
19+
20+
initial begin
21+
$dumpfile("uut.vcd");
22+
$dumpvars(0, uut);
23+
end
24+
25+
26+
initial begin
27+
CLK = 0;
28+
forever begin
29+
#5 CLK = !CLK;
30+
end
31+
end
32+
33+
34+
initial begin
35+
RST = 0;
36+
#100;
37+
RST = 1;
38+
#100;
39+
RST = 0;
40+
#10000;
41+
$finish;
42+
end
43+
44+
45+
endmodule
46+
47+
48+
49+
module blinkled
50+
(
51+
input CLK,
52+
input RST
53+
);
54+
55+
reg [8-1:0] LED;
56+
reg signed [8-1:0] count;
57+
reg [32-1:0] th_blink;
58+
localparam th_blink_init = 0;
59+
reg signed [32-1:0] _th_blink_times_0;
60+
reg signed [32-1:0] _th_blink_i_1;
61+
62+
always @(posedge CLK) begin
63+
if(RST) begin
64+
count <= 0;
65+
end else begin
66+
count <= count + 'sd8;
67+
end
68+
end
69+
70+
localparam th_blink_1 = 1;
71+
localparam th_blink_2 = 2;
72+
localparam th_blink_3 = 3;
73+
localparam th_blink_4 = 4;
74+
localparam th_blink_5 = 5;
75+
localparam th_blink_6 = 6;
76+
localparam th_blink_7 = 7;
77+
78+
always @(posedge CLK) begin
79+
if(RST) begin
80+
th_blink <= th_blink_init;
81+
_th_blink_times_0 <= 0;
82+
LED <= 0;
83+
_th_blink_i_1 <= 0;
84+
end else begin
85+
case(th_blink)
86+
th_blink_init: begin
87+
_th_blink_times_0 <= 10;
88+
th_blink <= th_blink_1;
89+
end
90+
th_blink_1: begin
91+
LED <= 0;
92+
th_blink <= th_blink_2;
93+
end
94+
th_blink_2: begin
95+
_th_blink_i_1 <= 0;
96+
th_blink <= th_blink_3;
97+
end
98+
th_blink_3: begin
99+
if(_th_blink_i_1 < _th_blink_times_0) begin
100+
th_blink <= th_blink_4;
101+
end else begin
102+
th_blink <= th_blink_7;
103+
end
104+
end
105+
th_blink_4: begin
106+
LED <= count >> 3;
107+
th_blink <= th_blink_5;
108+
end
109+
th_blink_5: begin
110+
$display("led = %d", LED);
111+
th_blink <= th_blink_6;
112+
end
113+
th_blink_6: begin
114+
_th_blink_i_1 <= _th_blink_i_1 + 1;
115+
th_blink <= th_blink_3;
116+
end
117+
endcase
118+
end
119+
end
120+
121+
122+
endmodule
123+
"""
124+
125+
126+
def test():
127+
veriloggen.reset()
128+
test_module = thread_fixed_read.mkTest()
129+
code = test_module.to_verilog()
130+
131+
from pyverilog.vparser.parser import VerilogParser
132+
from pyverilog.ast_code_generator.codegen import ASTCodeGenerator
133+
parser = VerilogParser()
134+
expected_ast = parser.parse(expected_verilog)
135+
codegen = ASTCodeGenerator()
136+
expected_code = codegen.visit(expected_ast)
137+
138+
assert(expected_code == code)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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(
8+
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))))
9+
10+
from veriloggen import *
11+
import veriloggen.thread as vthread
12+
import veriloggen.types.fixed as fx
13+
14+
15+
def mkLed():
16+
m = Module('blinkled')
17+
clk = m.Input('CLK')
18+
rst = m.Input('RST')
19+
led = m.Reg('LED', 8, initval=0)
20+
21+
count = fx.FixedReg(m, 'count', 8, point=3, initval=0)
22+
23+
seq = Seq(m, 'seq', clk, rst)
24+
seq(
25+
count.inc()
26+
)
27+
28+
def blink(times):
29+
led.value = 0
30+
for i in range(times):
31+
led.value = count
32+
print("led = ", led)
33+
34+
th = vthread.Thread(m, 'th_blink', clk, rst, blink)
35+
fsm = th.start(10)
36+
37+
return m
38+
39+
40+
def mkTest():
41+
m = Module('test')
42+
43+
# target instance
44+
led = mkLed()
45+
46+
# copy paras and ports
47+
params = m.copy_params(led)
48+
ports = m.copy_sim_ports(led)
49+
50+
clk = ports['CLK']
51+
rst = ports['RST']
52+
53+
uut = m.Instance(led, 'uut',
54+
params=m.connect_params(led),
55+
ports=m.connect_ports(led))
56+
57+
simulation.setup_waveform(m, uut)
58+
simulation.setup_clock(m, clk, hperiod=5)
59+
init = simulation.setup_reset(m, rst, m.make_reset(), period=100)
60+
61+
init.add(
62+
Delay(10000),
63+
Systask('finish'),
64+
)
65+
66+
return m
67+
68+
if __name__ == '__main__':
69+
test = mkTest()
70+
verilog = test.to_verilog('tmp.v')
71+
print(verilog)
72+
73+
sim = simulation.Simulator(test)
74+
rslt = sim.run()
75+
print(rslt)
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

0 commit comments

Comments
 (0)