gen/fhdl/namer: Improve/Simplify SignalNamespace.get_name method.

This commit is contained in:
Florent Kermarrec 2023-11-06 15:54:19 +01:00
parent 9ce29224a1
commit af508fddc5
1 changed files with 23 additions and 21 deletions

View File

@ -391,18 +391,18 @@ class SignalNamespace:
self.clock_domains = dict() self.clock_domains = dict()
def get_name(self, sig): def get_name(self, sig):
# Get name of a Clock Signal. # Handle Clock and Reset Signals.
# --------------------------- # -------------------------------
if isinstance(sig, ClockSignal): if isinstance(sig, (ClockSignal, ResetSignal)):
sig = self.clock_domains[sig.cd].clk # Retrieve the clock domain from the dictionary.
domain = self.clock_domains.get(sig.cd)
# Get name of a Reset Signal. if domain is None:
# --------------------------- raise ValueError(f"Clock Domain '{sig.cd}' not found.")
if isinstance(sig, ResetSignal): # Assign the appropriate signal from the clock domain.
sig = self.clock_domains[sig.cd].rst sig = domain.clk if isinstance(sig, ClockSignal) else domain.rst
# If the signal is None, the clock domain is missing a clock or reset.
if sig is None: if sig is None:
msg = f"Clock Domain {sig.cd} is reset-less, can't obtain name" raise ValueError(f"Clock Domain '{sig.cd}' is reset-less, can't obtain name.")
raise ValueError(msg)
# Get name of a Regular Signal. # Get name of a Regular Signal.
# ----------------------------- # -----------------------------
@ -411,19 +411,21 @@ class SignalNamespace:
sig_name = sig.name_override sig_name = sig.name_override
# ... else get Name from name_dict. # ... else get Name from name_dict.
else: else:
sig_name = self.name_dict[sig] sig_name = self.name_dict.get(sig)
# If the signal is not in the name_dict, raise an error.
if sig_name is None:
raise ValueError(f"Signal '{sig}' not found in name dictionary.")
# Check/Add numbering suffix when required.
# ----------------------------------------- # Check/Add numbering when required.
try: # ----------------------------------
n = self.sigs[sig] # Retrieve the current count for the signal name, defaulting to 0.
except KeyError: n = self.sigs.get(sig)
try: if n is None:
n = self.counts[sig_name] n = self.counts.get(sig_name, 0)
except KeyError:
n = 0
self.sigs[sig] = n self.sigs[sig] = n
self.counts[sig_name] = n + 1 self.counts[sig_name] = n + 1
# If the count is greater than 0, append it to the signal name.
if n > 0: if n > 0:
sig_name += f"_{n}" sig_name += f"_{n}"