Skip to content

Commit d1139ce

Browse files
committed
Merge branch 'feature_stream_split' into develop
2 parents 9f11af9 + b431952 commit d1139ce

File tree

3 files changed

+212
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)