Skip to content

Commit c24948b

Browse files
committed
stream.Reg
1 parent 49b6979 commit c24948b

File tree

4 files changed

+229
-0
lines changed

4 files changed

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

veriloggen/stream/stypes.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,6 +3171,43 @@ def _implement(self, m, seq, svalid=None, senable=None):
31713171
self.sig_data = data
31723172

31733173

3174+
class Reg(_SpecialOperator):
3175+
latency = 1
3176+
3177+
def __init__(self, data, when=None):
3178+
3179+
args = [data]
3180+
if when is not None:
3181+
args.append(when)
3182+
3183+
_SpecialOperator.__init__(self, *args)
3184+
3185+
self.width = data.bit_length()
3186+
self.point = data.get_point()
3187+
self.signed = data.get_signed()
3188+
3189+
self.graph_label = 'Reg'
3190+
self.graph_shape = 'box'
3191+
3192+
def _implement(self, m, seq, svalid=None, senable=None):
3193+
if self.latency != 1:
3194+
raise ValueError("Latency mismatch '%d' != '%s'" %
3195+
(self.latency, 1))
3196+
3197+
width = self.bit_length()
3198+
signed = self.get_signed()
3199+
3200+
arg_data = [arg.sig_data for arg in self.args]
3201+
3202+
data = m.Reg(self.name('data'), width, initval=0, signed=signed)
3203+
self.sig_data = data
3204+
3205+
when_cond = self.args[1].sig_data if len(self.args) == 2 else None
3206+
enable = _and_vars(senable, when_cond)
3207+
3208+
seq(data(arg_data[0]), cond=enable)
3209+
3210+
31743211
class ReadRAM(_SpecialOperator):
31753212
latency = 3
31763213

0 commit comments

Comments
 (0)