Skip to content

Commit 6297f44

Browse files
committed
Extract invert mask calculation to shared helper function
- Add compute_invert_mask() utility function in platform/utils.py - Replace duplicated inv_mask calculation in silicon.py (2 places) and base.py - Improves maintainability by centralizing the logic - Addresses gatecat review comment on silicon.py:109
1 parent 63fa078 commit 6297f44

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

chipflow_lib/platform/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from amaranth import Module
1111

1212
from .io import IOSignature
13+
from .utils import compute_invert_mask
1314

1415
logger = logging.getLogger(__name__)
1516

@@ -84,7 +85,7 @@ def _wire_up_ports(m: Module, top, platform):
8485
if hasattr(port, 'wire_up'):
8586
port.wire_up(m, wire)
8687
else:
87-
inv_mask = sum(inv << bit for bit, inv in enumerate(port.invert))
88+
inv_mask = compute_invert_mask(port.invert)
8889
if hasattr(wire, 'i'):
8990
m.d.comb += wire.i.eq(port.i ^ inv_mask)
9091
if hasattr(wire, 'o'):

chipflow_lib/platform/silicon.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from ..utils import ChipFlowError
2424
from ..config import Process
2525
from .io import IOModel, IOTripPoint, Sky130DriveMode
26+
from .utils import compute_invert_mask
2627

2728
if TYPE_CHECKING:
2829
from ..config import Config
@@ -105,7 +106,7 @@ def wire_up(self, m, wire):
105106
if self._oe is not None and len(self._oe) == 1 and len(self._oes) > 1:
106107
self._oes.eq(self._oe.replicate(len(self._oes)))
107108

108-
inv_mask = sum(inv << bit for bit, inv in enumerate(self.invert))
109+
inv_mask = compute_invert_mask(self.invert)
109110
if hasattr(wire, 'i') and wire.i is not None:
110111
assert self._i is not None
111112
m.d.comb += wire.i.eq(self._i ^ inv_mask)
@@ -349,7 +350,7 @@ def elaborate(self, platform):
349350
raise TypeError(f"Cannot elaborate SiliconPlatform buffer with port {self.port!r}")
350351

351352
m = Module()
352-
invert = sum(bit << idx for idx, bit in enumerate(self.port.invert))
353+
invert = compute_invert_mask(self.port.invert)
353354
if self.direction is not io.Direction.Input:
354355
if invert != 0:
355356
o_inv = Signal.like(self.o) # type: ignore[reportAttributeAccessIssue]

chipflow_lib/platform/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121
logger = logging.getLogger(__name__)
2222

2323

24+
def compute_invert_mask(invert_list):
25+
"""
26+
Compute a bit mask for signal inversion from a list of boolean invert flags.
27+
28+
Args:
29+
invert_list: List of booleans indicating which bits should be inverted
30+
31+
Returns:
32+
Integer mask where set bits indicate positions to invert
33+
"""
34+
return sum(inv << bit for bit, inv in enumerate(invert_list))
35+
36+
2437
def top_components(config: 'Config') -> Dict[str, wiring.Component]:
2538
"""
2639
Return the top level components for the design, as configured in ``chipflow.toml``

0 commit comments

Comments
 (0)