Skip to content

Commit

Permalink
Enable mixed capitalization
Browse files Browse the repository at this point in the history
  • Loading branch information
c-93 authored and imphil committed Oct 20, 2021
1 parent 97062b4 commit b4196ba
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions src/cocotb_bus/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""
from cocotb.handle import _AssignmentResult


def _build_sig_attr_dict(signals):
if isinstance(signals, dict):
return signals
Expand Down Expand Up @@ -54,24 +53,13 @@ def __init__(self, entity, name, signals, optional_signals=[], bus_separator="_"
self._entity = entity
self._name = name
self._signals = {}

if case_insensitive:
signal_names = dir(self._entity)
signal_name_mapping = dict((n.casefold(), n) for n in signal_names)

for attr_name, sig_name in _build_sig_attr_dict(signals).items():
if name:
signame = name + bus_separator + sig_name
else:
signame = sig_name

# case insensitive remap
if case_insensitive:
signame_folded = signame.casefold()
if signame not in signal_names and signame_folded in signal_name_mapping:
signame = signal_name_mapping[signame_folded]

self._add_signal(attr_name, signame, array_idx)
self._add_signal(attr_name, signame, array_idx, case_insensitive)

# Also support a set of optional signals that don't have to be present
for attr_name, sig_name in _build_sig_attr_dict(optional_signals).items():
Expand All @@ -80,22 +68,24 @@ def __init__(self, entity, name, signals, optional_signals=[], bus_separator="_"
else:
signame = sig_name

# case insensitive remap
if case_insensitive:
signame_folded = signame.casefold()
if signame not in signal_names and signame_folded in signal_name_mapping:
signame = signal_name_mapping[signame_folded]

self._entity._log.debug("Signal name {}".format(signame))
if hasattr(entity, signame):
self._add_signal(attr_name, signame, array_idx)
self._add_signal(attr_name, signame, array_idx, case_insensitive)
else:
self._entity._log.debug("Ignoring optional missing signal "
"%s on bus %s" % (sig_name, name))

def _add_signal(self, attr_name, signame, array_idx=None):
def _caseInsensGetattr(self, obj, attr):
for a in dir(obj):
if a.casefold() == attr.casefold():
return getattr(obj, a)

def _add_signal(self, attr_name, signame, array_idx=None, case_insensitive=True):
self._entity._log.debug("Signal name {}, idx {}".format(signame, array_idx))
handle = getattr(self._entity, signame)
if case_insensitive:
handle = self._caseInsensGetattr(self._entity, signame)
else:
handle = getattr(self._entity, signame)
if array_idx is not None:
handle = handle[array_idx]
setattr(self, attr_name, handle)
Expand Down

1 comment on commit b4196ba

@shareefj
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imphil This commit seems to have broken an old testbench of mine where I'm using packed structs for signals. Unless I specifically define case_insensitive=False when instantiating my BusDriver, the handle returned by self._caseInsenseGetattr() is None.

I'm passing in a dict of signals such as:

self._signals = {
  'HTRANS': 's_ahb_h2d.htrans'
}

Please sign in to comment.