Skip to content

Commit a71fb80

Browse files
committed
source_join_and_run is implemented.
1 parent 16752bb commit a71fb80

File tree

5 files changed

+239
-19
lines changed

5 files changed

+239
-19
lines changed

tests/extension/thread_/stream_reduce_source_join/thread_stream_reduce_source_join.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def mkTest(memimg_name=None):
122122
params=m.connect_params(led),
123123
ports=m.connect_ports(led))
124124

125-
simulation.setup_waveform(m, uut)
125+
# simulation.setup_waveform(m, uut)
126126
simulation.setup_clock(m, clk, hperiod=5)
127127
init = simulation.setup_reset(m, rst, m.make_reset(), period=100)
128128

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
4+
import os
5+
import veriloggen
6+
import thread_stream_reduce_source_join_and_run
7+
8+
9+
def test(request):
10+
veriloggen.reset()
11+
12+
simtype = request.config.getoption('--sim')
13+
14+
rslt = thread_stream_reduce_source_join_and_run.run(filename=None, simtype=simtype,
15+
outputfile=os.path.splitext(os.path.basename(__file__))[0] + '.out')
16+
17+
verify_rslt = rslt.splitlines()[-1]
18+
assert(verify_rslt == '# verify: PASSED')
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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.axi as axi
13+
14+
15+
def mkLed():
16+
m = Module('blinkled')
17+
clk = m.Input('CLK')
18+
rst = m.Input('RST')
19+
20+
datawidth = 32
21+
addrwidth = 10
22+
myaxi = vthread.AXIM(m, 'myaxi', clk, rst, datawidth)
23+
ram_a = vthread.RAM(m, 'ram_a', clk, rst, datawidth, addrwidth)
24+
ram_b = vthread.RAM(m, 'ram_b', clk, rst, datawidth, addrwidth)
25+
26+
strm = vthread.Stream(m, 'mystream', clk, rst)
27+
a = strm.source('a')
28+
size = strm.constant('size')
29+
sum, sum_valid = strm.ReduceAddValid(a, size)
30+
strm.sink(sum, 'sum', when=sum_valid, when_name='sum_valid')
31+
32+
def comp_stream(size, offset):
33+
strm.set_source('a', ram_a, offset, size)
34+
strm.set_constant('size', size)
35+
strm.set_sink('sum', ram_b, offset, 1)
36+
strm.run()
37+
38+
strm.set_source('a', ram_a, offset + size, size + size)
39+
strm.set_constant('size', size + size)
40+
strm.set_sink('sum', ram_b, offset + 1, 1)
41+
strm.source_join_and_run()
42+
43+
strm.set_source('a', ram_a, offset + size + size + size, size + size + size)
44+
strm.set_constant('size', size + size + size)
45+
strm.set_sink('sum', ram_b, offset + 2, 1)
46+
strm.source_join_and_run()
47+
48+
strm.source_join()
49+
strm.sink_join()
50+
51+
def comp_sequential(size, offset):
52+
sum = 0
53+
for i in range(size):
54+
a = ram_a.read(i + offset)
55+
sum += a
56+
ram_b.write(offset, sum)
57+
58+
sum = 0
59+
for i in range(size + size):
60+
a = ram_a.read(i + offset + size)
61+
sum += a
62+
ram_b.write(offset + 1, sum)
63+
64+
sum = 0
65+
for i in range(size + size + size):
66+
a = ram_a.read(i + offset + size + size + size)
67+
sum += a
68+
ram_b.write(offset + 2, sum)
69+
70+
def check(size, offset_stream, offset_seq):
71+
all_ok = True
72+
for i in range(size):
73+
st = ram_b.read(i + offset_stream)
74+
sq = ram_b.read(i + offset_seq)
75+
if vthread.verilog.NotEql(st, sq):
76+
all_ok = False
77+
if all_ok:
78+
print('# verify: PASSED')
79+
else:
80+
print('# verify: FAILED')
81+
82+
def comp(size):
83+
offset = 0
84+
myaxi.dma_read(ram_a, offset, 0, size * 6)
85+
comp_stream(size, offset)
86+
myaxi.dma_write(ram_b, offset, 1024, 1)
87+
88+
offset = size
89+
myaxi.dma_read(ram_a, offset, 0, size * 6)
90+
comp_sequential(size, offset)
91+
myaxi.dma_write(ram_b, offset, 1024 * 2, 1)
92+
93+
check(3, 0, offset)
94+
95+
vthread.finish()
96+
97+
th = vthread.Thread(m, 'th_comp', clk, rst, comp)
98+
fsm = th.start(32)
99+
100+
return m
101+
102+
103+
def mkTest(memimg_name=None):
104+
m = Module('test')
105+
106+
# target instance
107+
led = mkLed()
108+
109+
# copy paras and ports
110+
params = m.copy_params(led)
111+
ports = m.copy_sim_ports(led)
112+
113+
clk = ports['CLK']
114+
rst = ports['RST']
115+
116+
memory = axi.AxiMemoryModel(m, 'memory', clk, rst, memimg_name=memimg_name)
117+
memory.connect(ports, 'myaxi')
118+
119+
uut = m.Instance(led, 'uut',
120+
params=m.connect_params(led),
121+
ports=m.connect_ports(led))
122+
123+
# simulation.setup_waveform(m, uut)
124+
simulation.setup_clock(m, clk, hperiod=5)
125+
init = simulation.setup_reset(m, rst, m.make_reset(), period=100)
126+
127+
init.add(
128+
Delay(1000000),
129+
Systask('finish'),
130+
)
131+
132+
return m
133+
134+
135+
def run(filename='tmp.v', simtype='iverilog', outputfile=None):
136+
137+
if outputfile is None:
138+
outputfile = os.path.splitext(os.path.basename(__file__))[0] + '.out'
139+
140+
memimg_name = 'memimg_' + outputfile
141+
142+
test = mkTest(memimg_name=memimg_name)
143+
144+
if filename is not None:
145+
test.to_verilog(filename)
146+
147+
sim = simulation.Simulator(test, sim=simtype)
148+
rslt = sim.run(outputfile=outputfile)
149+
lines = rslt.splitlines()
150+
if simtype == 'verilator' and lines[-1].startswith('-'):
151+
rslt = '\n'.join(lines[:-1])
152+
return rslt
153+
154+
155+
if __name__ == '__main__':
156+
rslt = run(filename='tmp.v')
157+
print(rslt)

veriloggen/thread/stream.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Stream(BaseStream):
4747
'run', 'join', 'done',
4848
'source_join', 'source_done',
4949
'sink_join', 'sink_done',
50+
'source_join_and_run',
5051
'enable_dump', 'disable_dump')
5152
ram_delay = 4
5253

@@ -932,19 +933,27 @@ def read_sink(self, fsm, name):
932933
return var.sink_ram_wdata
933934

934935
def run(self, fsm):
935-
# entry point
936-
self.fsm._set_index(0)
937-
938936
cond = self._set_flag(fsm)
937+
938+
self._run(cond)
939+
940+
fsm.goto_next()
941+
fsm.goto_next()
942+
943+
return 0
944+
945+
def _run(self, cond):
939946
add_mux(self.start_flag, cond, 1)
940947

941-
# after started
942-
if self.fsm_synthesized:
943-
fsm.goto_next()
944-
fsm.goto_next()
945-
return
948+
if not self.fsm_synthesized:
949+
self._synthesize_run()
950+
self.fsm_synthesized = True
946951

947-
self.fsm_synthesized = True
952+
return 0
953+
954+
def _synthesize_run(self):
955+
# entry point
956+
self.fsm._set_index(0)
948957

949958
start_cond = vtypes.Ands(self.fsm.here, self.start_flag)
950959

@@ -1122,9 +1131,6 @@ def run(self, fsm):
11221131

11231132
self.fsm.goto_init()
11241133

1125-
fsm.goto_next()
1126-
fsm.goto_next()
1127-
11281134
return 0
11291135

11301136
def join(self, fsm):
@@ -1150,6 +1156,16 @@ def sink_join(self, fsm):
11501156
def sink_done(self, fsm):
11511157
return vtypes.Not(self.sink_busy)
11521158

1159+
def source_join_and_run(self, fsm):
1160+
cond = vtypes.Ands(fsm.here, vtypes.Not(self.source_busy))
1161+
1162+
self._run(cond)
1163+
1164+
fsm.If(vtypes.Not(self.source_busy)).goto_next()
1165+
fsm.goto_next()
1166+
1167+
return 0
1168+
11531169
def enable_dump(self, fsm):
11541170
if not self.dump:
11551171
raise TypeError('dump mode is disabled.')
@@ -1233,7 +1249,7 @@ def _setup_source_ram_dump(self, ram, var, read_enable, read_data):
12331249
'o' if data_base == 8 else
12341250
'd' if (data_base == 10 and
12351251
(not hasattr(ram, 'point') or ram.point <= 0)) else
1236-
#'f' if (data_base == 10 and
1252+
# 'f' if (data_base == 10 and
12371253
# hasattr(ram, 'point') and ram.point > 0) else
12381254
'g' if (data_base == 10 and
12391255
hasattr(ram, 'point') and ram.point > 0) else
@@ -1242,15 +1258,15 @@ def _setup_source_ram_dump(self, ram, var, read_enable, read_data):
12421258
'0o' if data_base == 8 else
12431259
' ' if data_base == 10 else
12441260
'0x')
1245-
#if data_base_char == 'f':
1261+
# if data_base_char == 'f':
12461262
# point_len = int(math.ceil(ram.point / math.log(10, 2)))
12471263
# point_len = max(point_len, 8)
12481264
# total_len = int(math.ceil(ram.datawidth / math.log(10, 2)))
12491265
# total_len = max(total_len, point_len)
12501266
# data_vfmt = ''.join([data_prefix, '%',
12511267
# '%d.%d' % (total_len + 1, point_len),
12521268
# data_base_char])
1253-
#else:
1269+
# else:
12541270
# data_vfmt = ''.join([data_prefix, '%', data_base_char])
12551271
data_vfmt = ''.join([data_prefix, '%', data_base_char])
12561272

@@ -1695,7 +1711,7 @@ def _setup_sink_ram_dump(self, ram, var, write_enable):
16951711
'o' if data_base == 8 else
16961712
'd' if (data_base == 10 and
16971713
(not hasattr(ram, 'point') or ram.point <= 0)) else
1698-
#'f' if (data_base == 10 and
1714+
# 'f' if (data_base == 10 and
16991715
# hasattr(ram, 'point') and ram.point > 0) else
17001716
'g' if (data_base == 10 and
17011717
hasattr(ram, 'point') and ram.point > 0) else
@@ -1704,15 +1720,15 @@ def _setup_sink_ram_dump(self, ram, var, write_enable):
17041720
'0o' if data_base == 8 else
17051721
' ' if data_base == 10 else
17061722
'0x')
1707-
#if data_base_char == 'f':
1723+
# if data_base_char == 'f':
17081724
# point_len = int(math.ceil(ram.point / math.log(10, 2)))
17091725
# point_len = max(point_len, 8)
17101726
# total_len = int(math.ceil(ram.datawidth / math.log(10, 2)))
17111727
# total_len = max(total_len, point_len)
17121728
# data_vfmt = ''.join([data_prefix, '%',
17131729
# '%d.%d' % (total_len + 1, point_len),
17141730
# data_base_char])
1715-
#else:
1731+
# else:
17161732
# data_vfmt = ''.join([data_prefix, '%', data_base_char])
17171733
data_vfmt = ''.join([data_prefix, '%', data_base_char])
17181734

0 commit comments

Comments
 (0)