Skip to content

Commit a465463

Browse files
committed
Use PinSignature for port wiring
1 parent 089ff07 commit a465463

File tree

5 files changed

+74
-64
lines changed

5 files changed

+74
-64
lines changed

amaranth_orchard/base/gpio.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
from amaranth import *
1+
from amaranth import Module, unsigned
22
from amaranth.lib import wiring
33
from amaranth.lib.wiring import In, Out, flipped, connect
44

55
from amaranth_soc import csr
66

7+
from chipflow_lib.platforms import BidirPinSignature
78

89
__all__ = ["GPIOPins", "GPIOPeripheral"]
910

1011

1112
class GPIOPins(wiring.PureInterface):
1213
class Signature(wiring.Signature):
1314
def __init__(self, width):
15+
if width > 32:
16+
raise ValueError(f"Pin width must be lesser than or equal to 32, not {len(pins.o)}")
1417
self._width = width
15-
super().__init__({
16-
"o": Out(unsigned(width)),
17-
"oe": Out(unsigned(width)),
18-
"i": In(unsigned(width)),
19-
})
18+
super().__init__(
19+
# each pin has seperate output enable
20+
{f"gpio{n}":Out(BidirPinSignature(1)) for n in range(width)}
21+
)
2022

2123
@property
2224
def width(self):
@@ -27,6 +29,10 @@ def create(self, *, path=(), src_loc_at=0):
2729

2830
def __init__(self, width, *, path=(), src_loc_at=0):
2931
super().__init__(self.Signature(width), path=path, src_loc_at=1 + src_loc_at)
32+
33+
@property
34+
def width(self):
35+
return self.signature.width
3036

3137

3238
class GPIOPeripheral(wiring.Component):
@@ -53,7 +59,7 @@ def __init__(self, *, pins):
5359
if len(pins.o) > 32:
5460
raise ValueError(f"Pin width must be lesser than or equal to 32, not {len(pins.o)}")
5561

56-
self.width = len(pins.o)
62+
self.width = pins.width
5763
self.pins = pins
5864

5965
regs = csr.Builder(addr_width=4, data_width=8)
@@ -75,10 +81,8 @@ def elaborate(self, platform):
7581

7682
connect(m, flipped(self.bus), self._bridge.bus)
7783

78-
m.d.comb += [
79-
self.pins.o .eq(self._do.f.pins.data),
80-
self.pins.oe.eq(self._oe.f.pins.data),
81-
]
82-
m.d.sync += self._di.f.pins.r_data.eq(self.pins.i)
84+
m.d.comb += [ getattr(self.pins, f"gpio{n}").o.eq(self._do.f.pins.data[n]) for n in range(self.width)]
85+
m.d.comb += [ getattr(self.pins, f"gpio{n}").oe.eq(self._oe.f.pins.data[n]) for n in range(self.width)]
86+
m.d.comb += [ self._di.f.pins.r_data[n].eq(getattr(self.pins, f"gpio{n}").i) for n in range(self.width)]
8387

8488
return m

amaranth_orchard/io/uart.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
from amaranth import *
1+
from amaranth import Module, Signal, unsigned
22
from amaranth.lib import wiring
33
from amaranth.lib.wiring import In, Out, flipped, connect
44

55
from amaranth_soc import csr
66
from amaranth_stdio.serial import AsyncSerialRX, AsyncSerialTX
77

8+
from chipflow_lib.platforms import OutputPinSignature, InputPinSignature
9+
810

911
__all__ = ["UARTPins", "UARTPeripheral"]
1012

@@ -13,8 +15,8 @@ class UARTPins(wiring.PureInterface):
1315
class Signature(wiring.Signature):
1416
def __init__(self):
1517
super().__init__({
16-
"tx_o": Out(1),
17-
"rx_i": In(1),
18+
"tx": Out(OutputPinSignature(1)),
19+
"rx": In(InputPinSignature(1)),
1820
})
1921

2022
def create(self, *, path=(), src_loc_at=0):
@@ -80,7 +82,7 @@ def elaborate(self, platform):
8082

8183
m.submodules.tx = tx = AsyncSerialTX(divisor=self.init_divisor, divisor_bits=24)
8284
m.d.comb += [
83-
self.pins.tx_o.eq(tx.o),
85+
self.pins.tx.o.eq(tx.o),
8486
tx.data.eq(self._tx_data.f.val.w_data),
8587
tx.ack.eq(self._tx_data.f.val.w_stb),
8688
self._tx_rdy.f.val.r_data.eq(tx.rdy),
@@ -102,7 +104,7 @@ def elaborate(self, platform):
102104
]
103105

104106
m.d.comb += [
105-
rx.i.eq(self.pins.rx_i),
107+
rx.i.eq(self.pins.rx.i),
106108
rx.ack.eq(~rx_avail),
107109
rx.divisor.eq(self._divisor.f.val.data),
108110
self._rx_data.f.val.r_data.eq(rx_buf),

amaranth_orchard/memory/hyperram.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
from amaranth import *
99
from amaranth.lib import wiring
10-
from amaranth.lib.wiring import In, Out, connect, flipped
10+
from amaranth.lib.wiring import Out, connect, flipped
1111
from amaranth.utils import ceil_log2
1212

13-
from amaranth.sim import *
13+
from amaranth.sim import Simulator
1414

1515
from amaranth_soc import csr, wishbone
1616
from amaranth_soc.memory import MemoryMap
1717

18+
from chipflow_lib.platforms import BidirPinSignature, OutputPinSignature
1819

1920
__all__ = ["HyperRAMPins", "HyperRAM"]
2021

@@ -24,15 +25,11 @@ class Signature(wiring.Signature):
2425
def __init__(self, *, cs_count=1):
2526
self.cs_count = cs_count
2627
super().__init__({
27-
"clk_o": Out(1),
28-
"csn_o": Out(cs_count),
29-
"rstn_o": Out(1),
30-
"rwds_o": Out(1),
31-
"rwds_oe": Out(1),
32-
"rwds_i": In(1),
33-
"dq_o": Out(8),
34-
"dq_oe": Out(8),
35-
"dq_i": In(8),
28+
"clk": Out(OutputPinSignature(1)),
29+
"csn": Out(OutputPinSignature(cs_count)),
30+
"rstn": Out(OutputPinSignature(1)),
31+
"rwds": Out(BidirPinSignature(1)),
32+
"dq": Out(BidirPinSignature(1)),
3633
})
3734

3835
def create(self, *, path=(), src_loc_at=0):
@@ -104,7 +101,6 @@ def elaborate(self, platform):
104101

105102
# Data shift register
106103
sr = Signal(48)
107-
sr_shift = Signal()
108104

109105
# Whether or not we need to apply x2 latency
110106
x2_lat = Signal()

amaranth_orchard/memory/spimemio.py

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from amaranth_soc import csr, wishbone
99
from amaranth_soc.memory import MemoryMap
10+
from chipflow_lib.platforms import BidirPinSignature,OutputPinSignature
1011

1112
__all__ = ["QSPIPins", "SPIMemIO"]
1213

@@ -15,12 +16,10 @@ class QSPIPins(wiring.PureInterface):
1516
class Signature(wiring.Signature):
1617
def __init__(self):
1718
super().__init__({
18-
"clk_o": Out(1),
19-
"csn_o": Out(1),
20-
"d_o": Out(4),
21-
"d_oe": Out(4),
22-
"d_i": In(4),
23-
})
19+
"clk": Out(OutputPinSignature(1)),
20+
"csn": Out(OutputPinSignature(1)),
21+
} |
22+
{f"d{n}": Out(BidirPinSignature(1)) for n in range(4)})
2423

2524
def create(self, *, path=(), src_loc_at=0):
2625
return QSPIPins(path=path, src_loc_at=1 + src_loc_at)
@@ -85,32 +84,28 @@ def elaborate(self, platform):
8584
spi_ready = Signal()
8685
# TODO : QSPI
8786

88-
m.submodules.spimemio = Instance(
89-
"spimemio",
90-
i_clk=ClockSignal(),
91-
i_resetn=~ResetSignal(),
92-
i_valid=self.data_bus.cyc & self.data_bus.stb,
93-
o_ready=spi_ready,
94-
i_addr=Cat(Const(0, 2), self.data_bus.adr), # Hack to force a 1MB offset
95-
o_rdata=self.data_bus.dat_r,
96-
o_flash_csb=self.flash.csn_o,
97-
o_flash_clk=self.flash.clk_o,
98-
o_flash_io0_oe=self.flash.d_oe[0],
99-
o_flash_io1_oe=self.flash.d_oe[1],
100-
o_flash_io2_oe=self.flash.d_oe[2],
101-
o_flash_io3_oe=self.flash.d_oe[3],
102-
o_flash_io0_do=self.flash.d_o[0],
103-
o_flash_io1_do=self.flash.d_o[1],
104-
o_flash_io2_do=self.flash.d_o[2],
105-
o_flash_io3_do=self.flash.d_o[3],
106-
i_flash_io0_di=self.flash.d_i[0],
107-
i_flash_io1_di=self.flash.d_i[1],
108-
i_flash_io2_di=self.flash.d_i[2],
109-
i_flash_io3_di=self.flash.d_i[3],
110-
i_cfgreg_we=ctrl_bridge.cfgreg_we,
111-
i_cfgreg_di=ctrl_bridge.cfgreg_di,
112-
o_cfgreg_do=ctrl_bridge.cfgreg_do,
113-
)
87+
verilog_map = {
88+
"i_clk": ClockSignal(),
89+
"i_resetn": ~ResetSignal(),
90+
"i_valid": self.data_bus.cyc & self.data_bus.stb,
91+
"o_ready": spi_ready,
92+
"i_addr": Cat(Const(0, 2), self.data_bus.adr), # Hack to force a 1MB offset
93+
"o_rdata": self.data_bus.dat_r,
94+
"o_flash_csb": self.flash.csn.o,
95+
"o_flash_clk": self.flash.clk.o,
96+
"i_cfgreg_we": ctrl_bridge.cfgreg_we,
97+
"i_cfgreg_di": ctrl_bridge.cfgreg_di,
98+
"o_cfgreg_do": ctrl_bridge.cfgreg_do,
99+
} | {
100+
f"o_flash_io{n}_oe": getattr(self.flash, f"d{n}").oe for n in range(4)
101+
} | {
102+
f"o_flash_io{n}_o": getattr(self.flash, f"d{n}").o for n in range(4)
103+
} | {
104+
f"o_flash_io{n}_i": getattr(self.flash, f"d{n}").i for n in range(4)
105+
}
106+
107+
m.submodules.spimemio = Instance("spimemio", **verilog_map)
108+
114109
# From https://github.com/im-tomu/foboot/blob/master/hw/rtl/picorvspi.py
115110
read_active = Signal()
116111
with m.If(self.data_bus.stb & self.data_bus.cyc & ~read_active):
@@ -123,7 +118,7 @@ def elaborate(self, platform):
123118
m.d.sync += self.data_bus.ack.eq(0)
124119

125120
if platform is not None:
126-
path = Path(__file__).parent / f"verilog/spimemio.v"
121+
path = Path(__file__).parent / "verilog/spimemio.v"
127122
with open(path, 'r') as f:
128123
platform.add_file(path.name, f)
129124

pyproject.toml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ authors = [
1313
readme = {file = "README.md", content-type = "text/markdown"}
1414
license = {file = "LICENSE.txt"}
1515

16-
requires-python = "~=3.8"
16+
requires-python = ">=3.10"
1717
dependencies = [
18-
"amaranth>=0.5,<0.7",
18+
"amaranth>=0.5,<0.6",
19+
"chipflow-lib @ git+https://github.com/ChipFlow/chipflow-lib.git",
20+
"amaranth-soc @ git+https://github.com/amaranth-lang/amaranth-soc@0ad5e245dd130d66a0a16663116ad0c1fe7a8956",
21+
"amaranth-stdio @ git+https://github.com/amaranth-lang/amaranth-stdio",
1922
]
2023

2124
# Build system configuration
@@ -25,3 +28,13 @@ requires = ["pdm-backend"]
2528
build-backend = "pdm.backend"
2629

2730
# Development workflow configuration
31+
32+
[tool.pyright]
33+
diagnosticMode=false
34+
typeCheckingMode = "off"
35+
reportInvalidTypeForm = false
36+
reportMissingImports = false
37+
reportUnboundVariable = false
38+
39+
[tool.ruff.lint]
40+
ignore = ['F403', 'F405']

0 commit comments

Comments
 (0)