Skip to content

Commit 4a62d41

Browse files
committed
tests for signed operation are added.
1 parent 7e5e18a commit 4a62d41

File tree

9 files changed

+3700
-0
lines changed

9 files changed

+3700
-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: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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.dirname(os.path.abspath(__file__)))))))
8+
9+
from veriloggen import *
10+
import veriloggen.dataflow as dataflow
11+
12+
def mkMain():
13+
# input variiable
14+
x = dataflow.Variable('xdata', valid='xvalid', ready='xready', signed=True)
15+
y = dataflow.Variable('ydata', valid='yvalid', ready='yready', signed=True)
16+
17+
# dataflow definition
18+
z = x + y
19+
20+
# set output attribute
21+
z.output('zdata', valid='zvalid', ready='zready')
22+
23+
df = dataflow.Dataflow(z)
24+
m = df.to_module('main')
25+
26+
return m
27+
28+
def mkTest(numports=8):
29+
m = Module('test')
30+
31+
# target instance
32+
main = mkMain()
33+
34+
params = m.copy_params(main)
35+
ports = m.copy_sim_ports(main)
36+
37+
clk = ports['CLK']
38+
rst = ports['RST']
39+
40+
xdata = ports['xdata']
41+
xvalid = ports['xvalid']
42+
xready = ports['xready']
43+
44+
ydata = ports['ydata']
45+
yvalid = ports['yvalid']
46+
yready = ports['yready']
47+
48+
zdata = ports['zdata']
49+
zvalid = ports['zvalid']
50+
zready = ports['zready']
51+
52+
uut = m.Instance(main, 'uut',
53+
params=m.connect_params(main),
54+
ports=m.connect_ports(main))
55+
56+
reset_done = m.Reg('reset_done', initval=0)
57+
reset_stmt = []
58+
reset_stmt.append( reset_done(0) )
59+
reset_stmt.append( xdata(0) )
60+
reset_stmt.append( xvalid(0) )
61+
reset_stmt.append( ydata(0) )
62+
reset_stmt.append( yvalid(0) )
63+
reset_stmt.append( zready(0) )
64+
65+
simulation.setup_waveform(m, uut)
66+
simulation.setup_clock(m, clk, hperiod=5)
67+
init = simulation.setup_reset(m, rst, reset_stmt, period=100)
68+
69+
nclk = simulation.next_clock
70+
71+
init.add(
72+
Delay(1000),
73+
reset_done(1),
74+
nclk(clk),
75+
Delay(10000),
76+
Systask('finish'),
77+
)
78+
79+
def send(name, data, valid, ready, step=1, waitnum=10):
80+
fsm = FSM(m, name + 'fsm', clk, rst)
81+
count = m.TmpReg(32, initval=0)
82+
83+
fsm.add(valid(0))
84+
fsm.goto_next(cond=reset_done)
85+
for _ in range(waitnum):
86+
fsm.goto_next()
87+
88+
fsm.add(valid(1))
89+
fsm.goto_next()
90+
91+
fsm.add(data(data + step), cond=ready)
92+
fsm.add(count.inc(), cond=ready)
93+
fsm.add(valid(0), cond=AndList(count==5, ready))
94+
fsm.goto_next(cond=AndList(count==5, ready))
95+
96+
for _ in range(waitnum):
97+
fsm.goto_next()
98+
fsm.add(valid(1))
99+
100+
fsm.add(data(data + step), cond=ready)
101+
fsm.add(count.inc(), cond=ready)
102+
fsm.add(valid(0), cond=AndList(count==10, ready))
103+
fsm.goto_next(cond=AndList(count==10, ready))
104+
105+
fsm.make_always()
106+
107+
108+
def receive(name, data, valid, ready, waitnum=10):
109+
fsm = FSM(m, name + 'fsm', clk, rst)
110+
111+
fsm.add(ready(0))
112+
fsm.goto_next(cond=reset_done)
113+
fsm.goto_next()
114+
115+
yinit= fsm.current()
116+
fsm.add(ready(1), cond=valid)
117+
fsm.goto_next(cond=valid)
118+
for i in range(waitnum):
119+
fsm.add(ready(0))
120+
fsm.goto_next()
121+
122+
fsm.goto(yinit)
123+
124+
fsm.make_always()
125+
126+
127+
send('x', xdata, xvalid, xready, step=1, waitnum=10)
128+
send('y', ydata, yvalid, yready, step=-2, waitnum=20)
129+
receive('z', zdata, zvalid, zready, waitnum=5)
130+
131+
132+
m.Always(Posedge(clk))(
133+
If(reset_done)(
134+
If(AndList(xvalid, xready))(
135+
Systask('display', 'xdata=%d', xdata)
136+
),
137+
If(AndList(yvalid, yready))(
138+
Systask('display', 'ydata=%d', ydata)
139+
),
140+
If(AndList(zvalid, zready))(
141+
Systask('display', 'zdata=%d', zdata)
142+
)
143+
)
144+
)
145+
146+
return m
147+
148+
149+
if __name__ == '__main__':
150+
test = mkTest()
151+
verilog = test.to_verilog('tmp.v')
152+
print(verilog)
153+
154+
# run simulator (Icarus Verilog)
155+
sim = simulation.Simulator(test)
156+
rslt = sim.run() # display=False
157+
#rslt = sim.run(display=True)
158+
print(rslt)
159+
160+
# launch waveform viewer (GTKwave)
161+
#sim.view_waveform() # background=False
162+
#sim.view_waveform(background=True)

0 commit comments

Comments
 (0)