Skip to content

Commit 28c9b7b

Browse files
committed
New example of FSM
1 parent 8d7e1a7 commit 28c9b7b

File tree

4 files changed

+407
-0
lines changed

4 files changed

+407
-0
lines changed

sample/fsm/Makefile

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 &

sample/fsm/led.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
valid = m.OutputReg('valid', initval=0)
11+
12+
count = m.Reg('count', width=32, initval=0)
13+
fsm = lib.FSM(m, 'fsm')
14+
15+
for i in range(4):
16+
fsm.goto_next()
17+
18+
# assert valid, then de-assert at the next cycle
19+
fsm.add( valid(1) )
20+
fsm.add( valid(0), delay=1 )
21+
22+
for i in range(4):
23+
fsm.goto_next()
24+
25+
# condition alias
26+
c = (count >= 16)
27+
28+
# assert valid 1 cycle later if the condition is satisfied now
29+
# then de-assert 3 cycles later with same condition
30+
for i in range(4):
31+
fsm.add( valid(1), cond=c, delay=1, keep=2)
32+
fsm.add( valid(0), cond=c, delay=3 )
33+
fsm.goto_next(cond=c)
34+
35+
m.Always(Posedge(clk))(
36+
If(rst)(
37+
m.reset(),
38+
).Else(
39+
count(count + 1),
40+
fsm.to_case()
41+
))
42+
43+
return m
44+
45+
def mkTest():
46+
m = Module('test')
47+
clk = m.Reg('CLK')
48+
rst = m.Reg('RST')
49+
valid = m.Wire('valid')
50+
51+
uut = m.Instance(mkLed(), 'uut',
52+
#ports=(('CLK', clk), ('RST', rst), ('valid', valid)))
53+
ports=connect_same_name(clk, rst, valid))
54+
55+
lib.simulation.setup_waveform(m, uut)
56+
lib.simulation.setup_clock(m, clk, hperiod=5)
57+
init = lib.simulation.setup_reset(m, rst, period=100)
58+
59+
init.add(
60+
Delay(1000),
61+
Systask('finish'),
62+
)
63+
64+
return m
65+
66+
if __name__ == '__main__':
67+
test = mkTest()
68+
verilog = test.to_verilog('tmp.v')
69+
print(verilog)

0 commit comments

Comments
 (0)