Skip to content

Commit b075fd7

Browse files
committed
lib.dataflow supports custom operator for accumulator (acc_custom).
1 parent 216a432 commit b075fd7

File tree

4 files changed

+452
-1
lines changed

4 files changed

+452
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
TARGET=*.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 *.out tmp.v uut.vcd
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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(os.path.dirname(os.path.abspath(__file__))))))
8+
9+
from veriloggen import *
10+
11+
def mkLed():
12+
m = Module('blinkled')
13+
clk = m.Input('CLK')
14+
rst = m.Input('RST')
15+
x = m.Input('x', 32)
16+
vx = m.Input('vx')
17+
rx = m.Output('rx')
18+
y = m.Output('y', 32)
19+
vy = m.Output('vy')
20+
ry = m.Input('ry')
21+
prst = m.Input('prst')
22+
23+
df = lib.Dataflow(m, 'df', clk, rst)
24+
25+
px = df.input(x, valid=vx, ready=rx)
26+
27+
# custom operator: should return vtypes._Numeric object
28+
def op_max(left, right):
29+
return Cond(left > right, left, right)
30+
31+
psum = df.acc_custom(px, op_max, initval=0, resetcond=prst)
32+
psum.output(y, valid=vy, ready=ry)
33+
34+
df.make_always()
35+
36+
return m
37+
38+
def mkTest(numports=8):
39+
m = Module('test')
40+
41+
# target instance
42+
led = mkLed()
43+
44+
# copy paras and ports
45+
params = m.copy_params(led)
46+
ports = m.copy_sim_ports(led)
47+
48+
clk = ports['CLK']
49+
rst = ports['RST']
50+
x = ports['x']
51+
vx = ports['vx']
52+
rx = ports['rx']
53+
y = ports['y']
54+
vy = ports['vy']
55+
ry = ports['ry']
56+
prst = ports['prst']
57+
58+
uut = m.Instance(led, 'uut',
59+
params=m.connect_params(led),
60+
ports=m.connect_ports(led))
61+
62+
reset_done = m.Reg('reset_done', initval=0)
63+
64+
reset_stmt = []
65+
reset_stmt.append( reset_done(0) )
66+
reset_stmt.append( prst(0) )
67+
reset_stmt.append( x(0) )
68+
reset_stmt.append( vx(0) )
69+
reset_stmt.append( ry(0) )
70+
71+
lib.simulation.setup_waveform(m, uut)
72+
lib.simulation.setup_clock(m, clk, hperiod=5)
73+
init = lib.simulation.setup_reset(m, rst, reset_stmt, period=100)
74+
75+
nclk = lib.simulation.next_clock
76+
77+
init.add(
78+
Delay(1000),
79+
reset_done(1),
80+
nclk(clk),
81+
Delay(10000),
82+
Systask('finish'),
83+
)
84+
85+
x_count = m.TmpReg(32, initval=0)
86+
y_count = m.TmpReg(32, initval=0)
87+
88+
89+
xfsm = lib.FSM(m, 'xfsm', clk, rst)
90+
xfsm.add(vx(0))
91+
xfsm.goto_next(cond=reset_done)
92+
xfsm.add(vx(1))
93+
xfsm.add(x.inc(), cond=rx)
94+
xfsm.add(x_count.inc(), cond=rx)
95+
xfsm.goto_next(cond=AndList(x_count==10, rx))
96+
xfsm.add(vx(0))
97+
xfsm.make_always()
98+
99+
100+
yfsm = lib.FSM(m, 'yfsm', clk, rst)
101+
yfsm.add(ry(0))
102+
yfsm.goto_next(cond=reset_done)
103+
yfsm.goto_next()
104+
yinit= yfsm.current()
105+
yfsm.add(ry(1), cond=vy)
106+
yfsm.goto_next(cond=vy)
107+
for i in range(10):
108+
yfsm.add(ry(0))
109+
yfsm.goto_next()
110+
yfsm.goto(yinit)
111+
yfsm.make_always()
112+
113+
114+
m.Always(Posedge(clk))(
115+
If(reset_done)(
116+
If(AndList(vx, rx))(
117+
Systask('display', 'x=%d', x)
118+
),
119+
If(AndList(vy, ry))(
120+
Systask('display', 'y=%d', y)
121+
)
122+
)
123+
)
124+
125+
return m
126+
127+
if __name__ == '__main__':
128+
test = mkTest()
129+
verilog = test.to_verilog('tmp.v')
130+
print(verilog)

0 commit comments

Comments
 (0)