Skip to content

Commit 9afad93

Browse files
committed
fsm example is updated.
1 parent 28c9b7b commit 9afad93

File tree

3 files changed

+163
-139
lines changed

3 files changed

+163
-139
lines changed

sample/fsm/led.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,42 @@ def mkLed():
77
m = Module('blinkled')
88
clk = m.Input('CLK')
99
rst = m.Input('RST')
10+
ready = m.Input('ready')
1011
valid = m.OutputReg('valid', initval=0)
11-
1212
count = m.Reg('count', width=32, initval=0)
13-
fsm = lib.FSM(m, 'fsm')
1413

15-
for i in range(4):
14+
fsm = lib.FSM(m, 'fsm')
15+
16+
for i in range(2):
1617
fsm.goto_next()
1718

1819
# assert valid, then de-assert at the next cycle
1920
fsm.add( valid(1) )
2021
fsm.add( valid(0), delay=1 )
2122

22-
for i in range(4):
23+
for i in range(2):
24+
fsm.goto_next()
25+
26+
# assert valid and go to the next state if a condition is satisfied now
27+
# then de-assert at the next cycle with the same condition
28+
fsm.add( valid(1), cond=(ready==1) )
29+
fsm.add( valid(0), cond=(ready==1), delay=1 )
30+
fsm.goto_next(cond=(ready==1))
31+
32+
for i in range(2):
2333
fsm.goto_next()
2434

2535
# condition alias
26-
c = (count >= 16)
36+
c = AndList((count >= 16), (ready==1))
2737

28-
# assert valid 1 cycle later if the condition is satisfied now
29-
# then de-assert 3 cycles later with same condition
38+
# assert valid 1 cycle later if a condition is satisfied now
39+
# then de-assert 3 cycles later with the same condition
3040
for i in range(4):
3141
fsm.add( valid(1), cond=c, delay=1, keep=2)
3242
fsm.add( valid(0), cond=c, delay=3 )
3343
fsm.goto_next(cond=c)
34-
44+
45+
# build always statement
3546
m.Always(Posedge(clk))(
3647
If(rst)(
3748
m.reset(),
@@ -46,17 +57,20 @@ def mkTest():
4657
m = Module('test')
4758
clk = m.Reg('CLK')
4859
rst = m.Reg('RST')
49-
valid = m.Wire('valid')
60+
ready = m.Reg('ready', initval=0)
61+
valid = m.Wire('valid')
5062

5163
uut = m.Instance(mkLed(), 'uut',
52-
#ports=(('CLK', clk), ('RST', rst), ('valid', valid)))
53-
ports=connect_same_name(clk, rst, valid))
64+
#ports=(('CLK', clk), ('RST', rst), ('ready', ready), ('valid', valid)))
65+
ports=connect_same_name(clk, rst, ready, valid))
5466

5567
lib.simulation.setup_waveform(m, uut)
5668
lib.simulation.setup_clock(m, clk, hperiod=5)
57-
init = lib.simulation.setup_reset(m, rst, period=100)
69+
init = lib.simulation.setup_reset(m, rst, m.reset(), period=100)
5870

5971
init.add(
72+
[ lib.simulation.next_clock(clk) for i in range(8) ],
73+
ready(1),
6074
Delay(1000),
6175
Systask('finish'),
6276
)

0 commit comments

Comments
 (0)