Skip to content

Commit fbdc882

Browse files
committed
lib_pipeline examples are updated.
1 parent b767fbf commit fbdc882

File tree

5 files changed

+498
-2
lines changed

5 files changed

+498
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
TARGET=led.py
2+
TEST=test_led.py
3+
ARGS=
4+
5+
PYTHON=python3
6+
#PYTHON=python
7+
#OPT=-m pdb
8+
#OPT=-m cProfile -s time
9+
#OPT=-m cProfile -o profile.rslt
10+
11+
.PHONY: all
12+
all: test
13+
14+
.PHONY: run
15+
run:
16+
$(PYTHON) $(OPT) $(TARGET) $(ARGS)
17+
18+
.PHONY: test
19+
test:
20+
$(PYTHON) -m pytest -vv $(TEST)
21+
22+
.PHONY: check
23+
check:
24+
$(PYTHON) $(OPT) $(TARGET) $(ARGS) > tmp.v
25+
iverilog -tnull -Wall tmp.v
26+
rm -f tmp.v
27+
28+
.PHONY: clean
29+
clean:
30+
rm -rf *.pyc __pycache__ parsetab.py *.out tmp.v uut.vcd
31+
32+
.PHONY: sim
33+
sim:
34+
iverilog -Wall tmp.v
35+
./a.out
36+
37+
.PHONY: view
38+
view:
39+
gtkwave --giga uut.vcd &
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import sys
2+
import os
3+
4+
from veriloggen import *
5+
6+
def mkLed():
7+
m = Module('blinkled')
8+
clk = m.Input('CLK')
9+
rst = m.Input('RST')
10+
11+
x = m.Input('x', 32)
12+
vx = m.Input('vx')
13+
rx = m.Output('rx')
14+
15+
y = m.Input('y', 32)
16+
vy = m.Input('vy')
17+
ry = m.Output('ry')
18+
19+
z = m.Output('z', 32)
20+
vz = m.Output('vz')
21+
rz = m.Input('rz')
22+
23+
pipe = lib.Pipeline(m, 'pipe')
24+
25+
px = pipe.input(x, valid=vx, ready=rx)
26+
py = pipe.input(y, valid=vy, ready=ry)
27+
pz = pipe(Cond(px > py, px, py))
28+
pz.output(z, valid=vz, ready=rz)
29+
30+
pipe.make_always(clk, rst)
31+
32+
return m
33+
34+
def mkTest(numports=8):
35+
m = Module('test')
36+
37+
# target instance
38+
led = mkLed()
39+
40+
# copy paras and ports
41+
params = m.copy_params(led)
42+
ports = m.copy_sim_ports(led)
43+
44+
clk = ports['CLK']
45+
rst = ports['RST']
46+
47+
x = ports['x']
48+
vx = ports['vx']
49+
rx = ports['rx']
50+
y = ports['y']
51+
vy = ports['vy']
52+
ry = ports['ry']
53+
z = ports['z']
54+
vz = ports['vz']
55+
rz = ports['rz']
56+
57+
uut = m.Instance(led, 'uut',
58+
params=m.connect_params(led),
59+
ports=m.connect_ports(led))
60+
61+
reset_done = m.Reg('reset_done', initval=0)
62+
63+
reset_stmt = []
64+
reset_stmt.append( reset_done(0) )
65+
reset_stmt.append( x(11) )
66+
reset_stmt.append( y(0) )
67+
reset_stmt.append( vx(0) )
68+
reset_stmt.append( vy(0) )
69+
reset_stmt.append( rz(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+
86+
x_count = m.TmpReg(32, initval=0)
87+
y_count = m.TmpReg(32, initval=0)
88+
z_count = m.TmpReg(32, initval=0)
89+
90+
91+
xfsm = lib.FSM(m, 'xfsm')
92+
xfsm.add(vx(0))
93+
xfsm.goto_next(cond=reset_done)
94+
xfsm.add(vx(1))
95+
xfsm.add(x.dec(), cond=rx)
96+
xfsm.add(x_count.inc(), cond=rx)
97+
xfsm.goto_next(cond=AndList(x_count==10, rx))
98+
xfsm.add(vx(0))
99+
xfsm.make_always(clk, rst)
100+
101+
102+
yfsm = lib.FSM(m, 'yfsm')
103+
yfsm.add(vy(0))
104+
yfsm.goto_next(cond=reset_done)
105+
yfsm.add(vy(1))
106+
yfsm.add(y.inc(), cond=ry)
107+
yfsm.add(y_count.inc(), cond=ry)
108+
yfsm.goto_next(cond=AndList(y_count==10, ry))
109+
yfsm.add(vy(0))
110+
yfsm.make_always(clk, rst)
111+
112+
113+
zfsm = lib.FSM(m, 'zfsm')
114+
zfsm.add(rz(0))
115+
zfsm.goto_next(cond=reset_done)
116+
zfsm.goto_next()
117+
zinit= zfsm.current()
118+
zfsm.add(rz(1), cond=vz)
119+
zfsm.goto_next(cond=vz)
120+
for i in range(10):
121+
zfsm.add(rz(0))
122+
zfsm.goto_next()
123+
zfsm.goto(zinit)
124+
zfsm.make_always(clk, rst)
125+
126+
127+
m.Always(Posedge(clk))(
128+
If(reset_done)(
129+
If(AndList(vx, rx))(
130+
Systask('display', 'x=%d', x)
131+
),
132+
If(AndList(vy, ry))(
133+
Systask('display', 'y=%d', y)
134+
),
135+
If(AndList(vz, rz))(
136+
Systask('display', 'z=%d', z)
137+
)
138+
)
139+
)
140+
141+
return m
142+
143+
if __name__ == '__main__':
144+
test = mkTest()
145+
verilog = test.to_verilog('tmp.v')
146+
print(verilog)

0 commit comments

Comments
 (0)