litex/migen/pytholite/util.py

34 lines
755 B
Python
Raw Normal View History

import ast
2013-06-25 16:17:39 -04:00
from migen.genlib.fsm import FSM, NextState
2013-06-25 16:17:39 -04:00
def id_next_state(l):
return NextState(id(l))
# entry state is first state returned
class StateAssembler:
def __init__(self):
self.states = []
self.exit_states = []
2014-10-17 05:08:37 -04:00
def assemble(self, n_states, n_exit_states):
self.states += n_states
for exit_state in self.exit_states:
2013-06-25 16:17:39 -04:00
exit_state.insert(0, id_next_state(n_states[0]))
self.exit_states = n_exit_states
2014-10-17 05:08:37 -04:00
def ret(self):
return self.states, self.exit_states
def implement_fsm(states):
2013-06-25 16:17:39 -04:00
fsm = FSM()
for state in states:
fsm.act(id(state), state)
return fsm
def eval_ast(expr, symdict):
if not isinstance(expr, ast.Expression):
expr = ast.Expression(expr)
code = compile(expr, "<ast>", "eval")
return eval(code, symdict)