Skip to content

Commit 8013c62

Browse files
committed
stream.Sync
1 parent ff77e19 commit 8013c62

File tree

4 files changed

+217
-0
lines changed

4 files changed

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

veriloggen/stream/stypes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,32 @@ def Mux(condition, true_value, false_value):
17571757
return Cond(condition, true_value, false_value)
17581758

17591759

1760+
class _Sync(_SpecialOperator):
1761+
latency = 0
1762+
1763+
def __init__(self, vars, index):
1764+
self.index = index
1765+
_SpecialOperator.__init__(self, *vars)
1766+
self.op = lambda *args: args[index]
1767+
self.graph_label = 'Sync %d' % index
1768+
1769+
def _set_attributes(self):
1770+
var = self.args[self.index]
1771+
self.width = var.width
1772+
self.point = var.point
1773+
self.signed = var.signed
1774+
1775+
1776+
def Sync(*vars):
1777+
""" Synchronize 'start_stage' of multiple variables """
1778+
1779+
ret_vars = []
1780+
for i, var in enumerate(vars):
1781+
ret_vars.append(_Sync(vars, i))
1782+
1783+
return tuple(ret_vars)
1784+
1785+
17601786
class CustomOp(_SpecialOperator):
17611787

17621788
def __init__(self, op, *vars):

0 commit comments

Comments
 (0)