Skip to content

Commit

Permalink
gen/fhdl/module: Ensure Module/Special/ClockDomains are initialized b…
Browse files Browse the repository at this point in the history
…efore adding them as submodules/specials/clock_domains.
  • Loading branch information
enjoy-digital committed Oct 27, 2023
1 parent fa629b7 commit 856d745
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions litex/gen/fhdl/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class LiteXModule(Module, AutoCSR, AutoDoc):
AutoDoc for CSR and documentation automation, respectively.
"""

@staticmethod
def _is_class_instance(_obj, _cls):
# If obj is cls, it is not an instance and not initialized.
if _obj is _cls:
return False
# Else check if object is an instance of cls.
return isinstance(_obj, _cls)

def __setattr__(m, name, value):
"""
Overrides the default behavior of attribute assignment in Python. This method simplifies the
Expand All @@ -33,13 +41,13 @@ def __setattr__(m, name, value):
# Automatic handling for adding submodules, specials, and clock domains in LiteX.
# - m.module_x = .. equivalent of Migen's m.submodules.module_x = ..
# Note: Do an exception for CSRs that have a specific collection mechanism.
elif (isinstance(value, Module) and ((name, value) not in m._submodules) and (not isinstance(value, _CSRBase))):
elif (m._is_class_instance(value, Module) and ((name, value) not in m._submodules) and (not isinstance(value, _CSRBase))):
setattr(m.submodules, name, value)
# - m.special_x = .. equivalent of Migen's m.specials.special_x = ..
elif isinstance(value, Special) and (value not in m._fragment.specials):
elif m._is_class_instance(value, Special) and (value not in m._fragment.specials):
setattr(m.specials, name, value)
# - m.cd_x = .. equivalent of Migen's m.clock_domains.cd_x = ..
elif isinstance(value, ClockDomain) and (value not in m._fragment.clock_domains):
elif m._is_class_instance(value, ClockDomain) and (value not in m._fragment.clock_domains):
setattr(m.clock_domains, name, value)
# Else use default __setattr__.
else:
Expand Down

0 comments on commit 856d745

Please sign in to comment.