Skip to content

Commit 1330dcb

Browse files
committed
goto methods do not return State object
1 parent 5c1f067 commit 1330dcb

File tree

6 files changed

+61
-57
lines changed

6 files changed

+61
-57
lines changed

tests/extension/fsm_/make_case/fsm_make_case.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import os
55

66
# 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__)))))))
7+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(
8+
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))))
89

910
from veriloggen import *
1011

12+
1113
def mkLed():
1214
m = Module('blinkled')
1315
width = m.Parameter('WIDTH', 8)
@@ -20,27 +22,29 @@ def mkLed():
2022
# get the initial index (= 0)
2123
init = fsm.current
2224

23-
fsm( count(count + 1) )
25+
fsm(count(count + 1))
2426
fsm.goto_next()
25-
fsm( count(count + 2) )
27+
fsm(count(count + 2))
2628
fsm.goto_next()
27-
29+
2830
# get the current index (= 2)
2931
here = fsm.current
30-
fsm( count(count + 3) )
32+
fsm(count(count + 3))
3133
fsm.goto_next()
3234

3335
# jump by using label "init"
34-
fsm.goto( dst=init, cond=(count < 1024), else_dst=fsm.next ).inc()
35-
36-
fsm( led(led + 1) )
36+
fsm.goto(dst=init, cond=(count < 1024), else_dst=fsm.next)
37+
fsm.inc()
38+
39+
fsm(led(led + 1))
3740
# jump by using label "here"
3841
fsm.goto(here)
3942

4043
fsm.make_always()
4144

4245
return m
4346

47+
4448
if __name__ == '__main__':
4549
led = mkLed()
4650
verilog = led.to_verilog()

tests/extension/fsm_/make_if/fsm_make_if.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import os
55

66
# 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__)))))))
7+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(
8+
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))))
89

910
from veriloggen import *
1011

12+
1113
def mkLed():
1214
m = Module('blinkled')
1315
width = m.Parameter('WIDTH', 8)
@@ -20,27 +22,29 @@ def mkLed():
2022
# get the initial index (= 0)
2123
init = fsm.current
2224

23-
fsm( count(count + 1) )
25+
fsm(count(count + 1))
2426
fsm.goto_next()
25-
fsm( count(count + 2) )
27+
fsm(count(count + 2))
2628
fsm.goto_next()
27-
29+
2830
# get the current index (= 2)
2931
here = fsm.current
30-
fsm( count(count + 3) )
32+
fsm(count(count + 3))
3133
fsm.goto_next()
3234

3335
# jump by using label "init"
34-
fsm.goto( dst=init, cond=(count < 1024), else_dst=fsm.next ).inc()
35-
36-
fsm( led(led + 1) )
36+
fsm.goto(dst=init, cond=(count < 1024), else_dst=fsm.next)
37+
fsm.inc()
38+
39+
fsm(led(led + 1))
3740
# jump by using label "here"
3841
fsm.goto(here)
39-
42+
4043
fsm.make_always(case=False)
4144

4245
return m
4346

47+
4448
if __name__ == '__main__':
4549
led = mkLed()
4650
verilog = led.to_verilog()

tests/extension/fsm_/state/fsm_state.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,39 @@ def mkLed():
1919

2020
fsm = FSM(m, 'fsm', clk, rst)
2121

22-
s0 = fsm.init # -> State(fsm, 0)
23-
s1 = s0.If(valid).goto_next() # -> State(fsm, 1)
24-
s2 = s1.If(valid).goto_next() # -> State(fsm, 2)
25-
s3 = s2.If(counter[0] == 0).goto(s2.next) # -> State(fsm, 3)
26-
s1_ = s2.Elif(counter[1] == 1).goto(s1) # -> State(fsm, 1)
27-
s2_ = s2.Else.goto(s2) # -> State(fsm, 2)
28-
s0_ = s3.goto(s0) # -> State(fsm, 0)
22+
s0 = fsm.init
23+
s1 = fsm.State(s0).If(valid).goto_next()
24+
s2 = fsm.State(s1).If(valid).goto_next()
25+
s3 = fsm.State(s2).If(counter[0] == 0).goto(fsm.next)
26+
s1_ = fsm.State(s2).Elif(counter[1] == 1).goto(s1)
27+
s2_ = fsm.State(s2).Else.goto(s2)
28+
s0_ = fsm.State(s3).goto(s0)
2929

3030
fsm.Always.If(counter <= 2 ** 8 - 1)(
3131
counter.inc()
3232
).Else(
3333
counter(0)
3434
)
3535

36-
s0.If(counter == 10)(
36+
fsm.State(s0).If(counter == 10)(
3737
valid(0)
3838
).Else(
3939
valid(1)
4040
)
4141

42-
s1.If(counter == 20)(
42+
fsm.State(s1).If(counter == 20)(
4343
valid(0)
4444
).Else(
4545
valid(1)
4646
)
4747

48-
s2.If(counter == 30)(
48+
fsm.State(s2).If(counter == 30)(
4949
valid(0)
5050
).Else(
5151
valid(1)
5252
)
5353

54-
s0_.If(counter == 40)(
54+
fsm.State(s0_).If(counter == 40)(
5555
valid(0)
5656
).Else(
5757
valid(1)

veriloggen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
# Abstract Extension
4545
from .seq.seq import Seq, TmpSeq, make_condition
46-
from .fsm.fsm import FSM, TmpFSM, State
46+
from .fsm.fsm import FSM, TmpFSM
4747
from .pipeline.pipeline import Pipeline
4848

4949
# Extension reset

veriloggen/fsm/fsm.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ def __init__(self, m, name, clk, rst, width=32, initname='init',
8686

8787
# -------------------------------------------------------------------------
8888
def goto(self, dst, cond=None, else_dst=None):
89-
if isinstance(dst, State):
90-
dst = dst.index
91-
9289
if cond is None and 'cond' in self.next_kwargs:
9390
cond = self.next_kwargs['cond']
9491

@@ -108,7 +105,7 @@ def goto(self, dst, cond=None, else_dst=None):
108105
src = index
109106

110107
self._go(src, dst, cond, else_dst)
111-
return State(self, dst)
108+
return dst
112109

113110
def goto_init(self, cond=None):
114111
if cond is None and 'cond' in self.next_kwargs:
@@ -131,7 +128,7 @@ def goto_init(self, cond=None):
131128

132129
dst = 0
133130
self._go(src, dst, cond)
134-
return State(self, dst)
131+
return dst
135132

136133
def goto_next(self, cond=None):
137134
if cond is None and 'cond' in self.next_kwargs:
@@ -155,15 +152,9 @@ def goto_next(self, cond=None):
155152
dst = src + 1
156153
self._go(src, dst, cond=cond)
157154
self.inc()
158-
return State(self, dst)
155+
return dst
159156

160157
def goto_from(self, src, dst, cond=None, else_dst=None):
161-
if isinstance(src, State):
162-
src = src.index
163-
164-
if isinstance(dst, State):
165-
dst = dst.index
166-
167158
if cond is None and 'cond' in self.next_kwargs:
168159
cond = self.next_kwargs['cond']
169160

@@ -173,7 +164,7 @@ def goto_from(self, src, dst, cond=None, else_dst=None):
173164
# self._clear_elif_cond()
174165

175166
self._go(src, dst, cond, else_dst)
176-
return State(self, dst)
167+
return dst
177168

178169
def inc(self):
179170
return self._set_index(None)
@@ -300,6 +291,12 @@ def Elif(self, *cond):
300291

301292
return self
302293

294+
def Then(self):
295+
cond = self._make_cond(self.last_cond)
296+
self._clear_last_cond()
297+
self.If(cond)
298+
return self
299+
303300
def Delay(self, delay):
304301
self.next_kwargs['delay'] = delay
305302
return self
@@ -308,12 +305,6 @@ def Keep(self, keep):
308305
self.next_kwargs['keep'] = keep
309306
return self
310307

311-
def Then(self):
312-
cond = self._make_cond(self.last_cond)
313-
self._clear_last_cond()
314-
self.If(cond)
315-
return self
316-
317308
def LazyCond(self, value=True):
318309
self.next_kwargs['lazy_cond'] = value
319310
return self
@@ -326,6 +317,11 @@ def When(self, index):
326317
self.next_kwargs['index'] = index
327318
return self
328319

320+
def State(self, index=None):
321+
if index is None:
322+
index = fsm.current
323+
return State(self, index)
324+
329325
def Clear(self):
330326
self._clear_next_kwargs()
331327
self._clear_last_if_statement()
@@ -340,15 +336,15 @@ def Always(self):
340336

341337
@property
342338
def init(self):
343-
return State(self, 0)
339+
return 0
344340

345341
@property
346342
def current(self):
347-
return State(self, self.state_count)
343+
return self.state_count
348344

349345
@property
350346
def next(self):
351-
return State(self, self.state_count + 1)
347+
return self.state_count + 1
352348

353349
@property
354350
def current_delay(self):

veriloggen/seq/seq.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ def Elif(self, *cond):
267267

268268
return self
269269

270+
def Then(self):
271+
cond = self._make_cond(self.last_cond)
272+
self._clear_last_cond()
273+
self.If(cond)
274+
return self
275+
270276
def Delay(self, delay):
271277
self.next_kwargs['delay'] = delay
272278
return self
@@ -275,12 +281,6 @@ def Keep(self, keep):
275281
self.next_kwargs['keep'] = keep
276282
return self
277283

278-
def Then(self):
279-
cond = self._make_cond(self.last_cond)
280-
self._clear_last_cond()
281-
self.If(cond)
282-
return self
283-
284284
def LazyCond(self, value=True):
285285
self.next_kwargs['lazy_cond'] = value
286286
return self

0 commit comments

Comments
 (0)