litex/migen/fhdl/decorators.py

116 lines
3.2 KiB
Python
Raw Normal View History

import warnings
2013-07-25 11:56:31 -04:00
from migen.fhdl.structure import *
2015-04-03 16:55:20 -04:00
from migen.fhdl.module import Module
2013-07-26 09:42:14 -04:00
from migen.fhdl.tools import insert_reset, rename_clock_domain
2013-07-25 11:56:31 -04:00
class ModuleTransformer:
# overload this in derived classes
def transform_instance(self, i):
pass
2013-07-25 11:56:31 -04:00
# overload this in derived classes
def transform_fragment(self, i, f):
2013-07-25 11:56:31 -04:00
pass
def wrap_class(self, victim):
class Wrapped(victim):
def __init__(i, *args, **kwargs):
victim.__init__(i, *args, **kwargs)
self.transform_instance(i)
def get_fragment(i):
f = victim.get_fragment(i)
self.transform_fragment(i, f)
return f
Wrapped.__name__ = victim.__name__
# "{}_{}".format(self.__class__.__name__, victim.__name__)
return Wrapped
def wrap_instance(self, victim):
self.transform_instance(victim)
orig_get_fragment = victim.get_fragment
def get_fragment():
f = orig_get_fragment()
self.transform_fragment(victim, f)
return f
victim.get_fragment = get_fragment
return victim
def __call__(self, victim):
2015-04-03 16:55:20 -04:00
if isinstance(victim, Module):
return self.wrap_instance(victim)
2015-04-03 16:55:20 -04:00
else:
return self.wrap_class(victim)
@classmethod
def adhoc(cls, i, *args, **kwargs):
warnings.warn("deprecated, use the plain transformer", DeprecationWarning, 2)
return cls(*args, **kwargs)(i)
def DecorateModule(transformer, *args, **kwargs):
warnings.warn("deprecated, use the plain transformer", DeprecationWarning, 2)
return transformer.__self__(*args, **kwargs)
class ControlInserter(ModuleTransformer):
control_name = None # override this
def __init__(self, clock_domains=None):
self.clock_domains = clock_domains
def transform_instance(self, i):
if self.clock_domains is None:
ctl = Signal(name=self.control_name)
assert not hasattr(i, self.control_name)
setattr(i, self.control_name, ctl)
2013-07-25 11:56:31 -04:00
else:
for cd in self.clock_domains:
name = self.control_name + "_" + cd
2013-07-25 11:56:31 -04:00
ctl = Signal(name=name)
assert not hasattr(i, name)
setattr(i, name, ctl)
2013-07-25 11:56:31 -04:00
def transform_fragment(self, i, f):
if self.clock_domains is None:
2013-07-25 11:56:31 -04:00
if len(f.sync) != 1:
raise ValueError("Control signal clock domains must be specified when module has more than one domain")
cdn = list(f.sync.keys())[0]
to_insert = [(getattr(i, self.control_name), cdn)]
2013-07-25 11:56:31 -04:00
else:
to_insert = [(getattr(i, self.control_name + "_" + cdn), cdn)
2015-04-05 02:20:23 -04:00
for cdn in self.clock_domains]
self.transform_fragment_insert(i, f, to_insert)
2013-07-25 11:56:31 -04:00
class CEInserter(ControlInserter):
control_name = "ce"
2014-10-17 05:08:37 -04:00
def transform_fragment_insert(self, i, f, to_insert):
2013-07-25 11:56:31 -04:00
for ce, cdn in to_insert:
f.sync[cdn] = [If(ce, *f.sync[cdn])]
InsertCE = CEInserter.adhoc
class ResetInserter(ControlInserter):
control_name = "reset"
2013-07-25 11:56:31 -04:00
def transform_fragment_insert(self, i, f, to_insert):
2013-07-25 11:56:31 -04:00
for reset, cdn in to_insert:
f.sync[cdn] = insert_reset(reset, f.sync[cdn])
2013-07-26 09:42:14 -04:00
InsertReset = ResetInserter.adhoc
class ClockDomainsRenamer(ModuleTransformer):
def __init__(self, cd_remapping):
2013-07-26 09:42:14 -04:00
if isinstance(cd_remapping, str):
cd_remapping = {"sys": cd_remapping}
self.cd_remapping = cd_remapping
2013-07-26 09:42:14 -04:00
def transform_fragment(self, i, f):
for old, new in self.cd_remapping.items():
2013-07-26 09:42:14 -04:00
rename_clock_domain(f, old, new)
RenameClockDomains = ClockDomainsRenamer.adhoc