Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 24th, 2013  |  syntax: Python  |  size: 1.72 KB  |  views: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from collections import namedtuple, OrderedDict
  2.  
  3. from migen.fhdl.std import *
  4. from migen.fhdl.visit import NodeTransformer
  5.  
  6. class AnonymousState:
  7.         pass
  8.  
  9. NextState = namedtuple("NextState", "state")
  10.  
  11. class _LowerNextState(NodeTransformer):
  12.         def __init__(self, state_signal, next_state_signal, encoding, aliases):
  13.                 self.state_signal = state_signal
  14.                 self.next_state_signal = next_state_signal
  15.                 self.encoding = encoding
  16.                 self.aliases = aliases
  17.                
  18.         def visit_unknown(self, node):
  19.                 if isinstance(node, NextState):
  20.                         index = _index_is(self.states, node.target_state)
  21.                         estate = getattr(self.fsm, self.stnames[index])
  22.                         return self.fsm.next_state(estate)
  23.                 else:
  24.                         return node
  25.  
  26. class FSM(Module):
  27.         def __init__(self):
  28.                 self._actions = OrderedDict()
  29.                 self._state_aliases = dict()
  30.                 self.reset_state = None
  31.  
  32.         def act(self, state, *statements):
  33.                 if state not in self._actions:
  34.                         self._actions[state] = []
  35.                 self._actions[state] += statements
  36.  
  37.         def delayed_enter(self, name, target, delay):
  38.                 if delay:
  39.                         state = name
  40.                         for i in range(delay):
  41.                                 if i == delay - 1:
  42.                                         next_state = target
  43.                                 else:
  44.                                         next_state = AnonymousState()
  45.                                 self.act(state, NextState(next_state))
  46.                                 state = next_state
  47.                 else:
  48.                         self._state_aliases[name] = target
  49.        
  50.         def finalize(self):
  51.                 nstates = len(self._actions)
  52.                 encoding = dict((s, n) for n, s in enumerate(self._actions.keys()))
  53.                 state = Signal(max=nstates)
  54.                 next_state = Signal(max=nstates)
  55.                 lns = _LowerNextState(state, next_state, encoding, self._state_aliases)
  56.                 cases = dict((encoding[k], lns.visit(v)) for k, v in self._actions.items() if v)
  57.                 self.comb += [
  58.                         next_state.eq(state),
  59.                         Case(state, cases)
  60.                 ]
  61.                 self.sync += state.eq(next_state)
clone this paste RAW Paste Data