Skip to content

Commit 3b407c6

Browse files
committed
Make wiring up ports common code and fix invert case
1 parent 8084ce6 commit 3b407c6

File tree

7 files changed

+39
-33
lines changed

7 files changed

+39
-33
lines changed

chipflow_lib/pin_lock.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def allocate_pins(name: str, member: Dict[str, Any], pins: List[str], port_name:
6262
'type': 'io',
6363
'port_name': port_name,
6464
'options': options}
65+
if 'invert' in sig and sig['invert']:
66+
pin_map[name]['invert'] = sig['invert']
67+
6568
logger.debug(f"added '{name}':{pin_map[name]} to pin_map")
6669
return pin_map, pins[width:]
6770
elif member['type'] == 'interface':

chipflow_lib/platforms/silicon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def instantiate_ports(self, m: Module):
292292
setattr(m.submodules, reset, rst_buffer)
293293
setattr(m.submodules, reset + "_sync", FFSynchronizer(rst_buffer.i, ResetSignal()))
294294

295-
self.pinlock = pinlock
295+
self._pinlock = pinlock
296296

297297
def request(self, name=None, **kwargs):
298298
if "$" in name:

chipflow_lib/platforms/sim.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def instantiate_ports(self, m: Module):
7373
for component, iface in pinlock.port_map.items():
7474
for k, v in iface.items():
7575
for name, port in v.items():
76-
self._ports[port.port_name] = io.SimulationPort(port.direction, port.width, invert=port.invert, name=f"{component}-{name}")
76+
invert = port.invert if port.invert else False
77+
self._ports[port.port_name] = io.SimulationPort(port.direction, port.width, invert=invert, name=f"{component}-{name}")
7778

7879
for clock, name in self._config["chipflow"]["clocks"].items():
7980
if name not in pinlock.package.clocks:

chipflow_lib/platforms/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ class Port(pydantic.BaseModel):
376376
pins: List[str]
377377
port_name: str
378378
direction: Optional[str] = None
379-
invert: Union[bool, Iterable[bool]] = False
379+
invert: Optional[Iterable[bool]] = None
380380
options: Optional[dict] = None
381381

382382
@property

chipflow_lib/steps/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import os
66
from abc import ABC
77

8+
from amaranth import Module
9+
10+
from ..platforms.utils import IOSignature
11+
812
def setup_amaranth_tools():
913
_amaranth_settings = {
1014
"AMARANTH_USE_YOSYS": "system",
@@ -36,3 +40,26 @@ def build_cli_parser(self, parser):
3640
def run_cli(self, args):
3741
"Called when this step's is used from `chipflow` command"
3842
self.build()
43+
44+
45+
def _wire_up_ports(m: Module, top, platform):
46+
for n, t in top.items():
47+
setattr(m.submodules, n, t)
48+
49+
for component, iface in platform._pinlock.port_map.items():
50+
for iface_name, member, in iface.items():
51+
for name, port in member.items():
52+
iface = getattr(top[component], iface_name)
53+
wire = (iface if isinstance(iface.signature, IOSignature)
54+
else getattr(iface, name))
55+
if port.invert:
56+
inv_mask = sum(inv << bit for bit, inv in enumerate(port.invert))
57+
else:
58+
inv_mask = 0
59+
port = platform._ports[port.port_name]
60+
if hasattr(wire, 'i'):
61+
m.d.comb += wire.i.eq(port.i ^ inv_mask)
62+
for d in ['o', 'oe']:
63+
if hasattr(wire, d):
64+
m.d.comb += getattr(port, d).eq(getattr(wire, d) ^ inv_mask)
65+

chipflow_lib/steps/silicon.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
from amaranth import *
1717
from halo import Halo
1818

19-
from . import StepBase
19+
from . import StepBase, _wire_up_ports
2020
from .. import ChipFlowError
2121
from ..cli import log_level
22-
from ..platforms import SiliconPlatform, top_interfaces, load_pinlock
23-
from ..platforms.utils import IOSignature
22+
from ..platforms import SiliconPlatform, top_interfaces, load_pinlock, IOSignature
2423

2524

2625
logger = logging.getLogger(__name__)
@@ -45,16 +44,7 @@ def elaborate(self, platform: SiliconPlatform):
4544
top, interfaces = top_interfaces(self._config)
4645
logger.debug(f"SiliconTop top = {top}, interfaces={interfaces}")
4746

48-
for n, t in top.items():
49-
setattr(m.submodules, n, t)
50-
51-
for component, iface in platform.pinlock.port_map.items():
52-
for iface_name, member, in iface.items():
53-
for name, port in member.items():
54-
iface = getattr(top[component], iface_name)
55-
wire = (iface if isinstance(iface.signature, IOSignature)
56-
else getattr(iface, name))
57-
platform.ports[port.port_name].wire(m, wire)
47+
_wire_up_ports(m, top, platform)
5848
return m
5949

6050

chipflow_lib/steps/sim.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
from amaranth import *
1414

15+
from . import StepBase, _wire_up_ports
1516
from .. import ChipFlowError, _ensure_chipflow_root
16-
from . import StepBase
1717
from ..platforms import SimPlatform, top_interfaces
1818
from ..platforms.utils import IOSignature
1919
from ..platforms.sim import VARIABLES, TASKS, DOIT_CONFIG
@@ -93,22 +93,7 @@ def build(self):
9393
top, interfaces = top_interfaces(self._config)
9494
logger.debug(f"SiliconTop top = {top}, interfaces={interfaces}")
9595

96-
for n, t in top.items():
97-
setattr(m.submodules, n, t)
98-
99-
for component, iface in self._platform._pinlock.port_map.items():
100-
for iface_name, member, in iface.items():
101-
for name, port in member.items():
102-
iface = getattr(top[component], iface_name)
103-
wire = (iface if isinstance(iface.signature, IOSignature)
104-
else getattr(iface, name))
105-
port = self._platform._ports[port.port_name]
106-
if hasattr(wire, 'i'):
107-
m.d.comb += wire.i.eq(port.i)
108-
for d in ['o', 'oe']:
109-
if hasattr(wire, d):
110-
m.d.comb += getattr(port, d).eq(getattr(wire, d))
111-
96+
_wire_up_ports(m, top, self._platform)
11297

11398
#FIXME: common source for build dir
11499
self._platform.build(m)

0 commit comments

Comments
 (0)