Skip to content

Commit bfd7fb2

Browse files
committed
Added a new example using Scratchpad.
1 parent 3df976b commit bfd7fb2

File tree

6 files changed

+216
-2
lines changed

6 files changed

+216
-2
lines changed

tests/extension/thread_/stream_scratchpad_multi/test_thread_stream_scratchpad_multi.py renamed to tests/extension/thread_/stream_scratchpad_chain/test_thread_stream_scratchpad_chain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
import os
55
import veriloggen
6-
import thread_stream_scratchpad_multi
6+
import thread_stream_scratchpad_chain
77

88

99
def test(request):
1010
veriloggen.reset()
1111

1212
simtype = request.config.getoption('--sim')
1313

14-
rslt = thread_stream_scratchpad_multi.run(filename=None, simtype=simtype,
14+
rslt = thread_stream_scratchpad_chain.run(filename=None, simtype=simtype,
1515
outputfile=os.path.splitext(os.path.basename(__file__))[0] + '.out')
1616

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

0 commit comments

Comments
 (0)