2012-11-11 17:48:23 -05:00
|
|
|
from operator import itemgetter
|
|
|
|
|
|
|
|
from migen.fhdl.structure import *
|
|
|
|
from migen.fhdl import visit as fhdl
|
|
|
|
|
|
|
|
class AbstractLoad:
|
|
|
|
def __init__(self, target, source):
|
|
|
|
self.target = target
|
|
|
|
self.source = source
|
|
|
|
|
|
|
|
def lower(self):
|
|
|
|
if not self.target.finalized:
|
|
|
|
raise FinalizeError
|
2012-11-28 18:09:35 -05:00
|
|
|
return self.target.sel.eq(self.target.source_encoding[id(self.source)])
|
2012-11-11 17:48:23 -05:00
|
|
|
|
|
|
|
class LowerAbstractLoad(fhdl.NodeTransformer):
|
|
|
|
def visit_unknown(self, node):
|
|
|
|
if isinstance(node, AbstractLoad):
|
|
|
|
return node.lower()
|
|
|
|
else:
|
|
|
|
return node
|
|
|
|
|
|
|
|
class ImplRegister:
|
2012-11-30 11:07:12 -05:00
|
|
|
def __init__(self, name, bits_sign):
|
2012-11-11 17:48:23 -05:00
|
|
|
self.name = name
|
2012-11-30 11:07:12 -05:00
|
|
|
self.storage = Signal(bits_sign, name=self.name)
|
2012-11-11 17:48:23 -05:00
|
|
|
self.source_encoding = {}
|
2012-11-28 18:09:35 -05:00
|
|
|
self.id_to_source = {}
|
2012-11-11 17:48:23 -05:00
|
|
|
self.finalized = False
|
|
|
|
|
|
|
|
def load(self, source):
|
2012-11-28 18:09:35 -05:00
|
|
|
if id(source) not in self.source_encoding:
|
|
|
|
self.source_encoding[id(source)] = len(self.source_encoding) + 1
|
|
|
|
self.id_to_source[id(source)] = source
|
2012-11-11 17:48:23 -05:00
|
|
|
return AbstractLoad(self, source)
|
|
|
|
|
|
|
|
def finalize(self):
|
|
|
|
if self.finalized:
|
|
|
|
raise FinalizeError
|
2012-11-30 11:07:32 -05:00
|
|
|
self.sel = Signal(max=len(self.source_encoding)+1, name="pl_regsel_"+self.name)
|
2012-11-11 17:48:23 -05:00
|
|
|
self.finalized = True
|
|
|
|
|
|
|
|
def get_fragment(self):
|
|
|
|
if not self.finalized:
|
|
|
|
raise FinalizeError
|
|
|
|
# do nothing when sel == 0
|
|
|
|
items = sorted(self.source_encoding.items(), key=itemgetter(1))
|
2012-11-28 19:11:15 -05:00
|
|
|
cases = dict((v, self.storage.eq(self.id_to_source[k])) for k, v in items)
|
|
|
|
sync = [Case(self.sel, cases)]
|
2012-11-11 17:48:23 -05:00
|
|
|
return Fragment(sync=sync)
|