From: Sebastien Bourdeauducq Date: Mon, 29 Sep 2014 11:38:58 +0000 (+0800) Subject: genlib/fsm: make first fsm.act() the reset state, even when using after_*/before_... X-Git-Tag: 24jan2021_ls180~2099^2~287 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=15f67b30d0893062d3a2727c1618354b76d58472;p=litex.git genlib/fsm: make first fsm.act() the reset state, even when using after_*/before_* methods before fsm.act --- diff --git a/migen/genlib/fsm.py b/migen/genlib/fsm.py index f6416504..891edfb8 100644 --- a/migen/genlib/fsm.py +++ b/migen/genlib/fsm.py @@ -43,6 +43,8 @@ class FSM(Module): def act(self, state, *statements): if self.finalized: raise FinalizeError + if self.reset_state is None: + self.reset_state = state if state not in self.actions: self.actions[state] = [] self.actions[state] += statements @@ -95,20 +97,16 @@ class FSM(Module): def do_finalize(self): nstates = len(self.actions) - if self.reset_state is None: - reset_state = next(iter(self.actions.keys())) - else: - reset_state = self.reset_state self.encoding = dict((s, n) for n, s in enumerate(self.actions.keys())) - self.state = Signal(max=nstates, reset=self.encoding[reset_state]) + self.state = Signal(max=nstates, reset=self.encoding[self.reset_state]) self.next_state = Signal(max=nstates) lns = _LowerNextState(self.next_state, self.encoding, self.state_aliases) cases = dict((self.encoding[k], lns.visit(v)) for k, v in self.actions.items() if v) self.comb += [ self.next_state.eq(self.state), - Case(self.state, cases).makedefault(self.encoding[reset_state]) + Case(self.state, cases).makedefault(self.encoding[self.reset_state]) ] self.sync += self.state.eq(self.next_state) @@ -116,8 +114,8 @@ class FSM(Module): for state, signal in self.before_leaving_signals.items(): encoded = self.encoding[state] self.comb += signal.eq((self.state == encoded) & ~(self.next_state == encoded)) - if reset_state in self.after_entering_signals: - self.after_entering_signals[reset_state].reset = 1 + if self.reset_state in self.after_entering_signals: + self.after_entering_signals[self.reset_state].reset = 1 for state, signal in self.before_entering_signals.items(): encoded = self.encoding[state] self.comb += signal.eq(~(self.state == encoded) & (self.next_state == encoded))