Skip to content

Commit 1d69569

Browse files
committed
Remove all passing of pins to constructor
1 parent fec8b4e commit 1d69569

File tree

4 files changed

+45
-125
lines changed

4 files changed

+45
-125
lines changed

amaranth_orchard/base/gpio.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__all__ = ["GPIOPins", "GPIOPeripheral"]
1010

1111

12-
class GPIOPins(wiring.PureInterface):
12+
class GPIOPeripheral(wiring.Component):
1313
class Signature(wiring.Signature):
1414
def __init__(self, width):
1515
if width > 32:
@@ -23,40 +23,30 @@ def __init__(self, width):
2323
def width(self):
2424
return self._width
2525

26-
def create(self, *, path=(), src_loc_at=0):
27-
return GPIOPins(width=self.width, path=path, src_loc_at=1 + src_loc_at)
28-
29-
def __init__(self, width, *, path=(), src_loc_at=0):
30-
super().__init__(self.Signature(width), path=path, src_loc_at=1 + src_loc_at)
31-
32-
@property
33-
def width(self):
34-
return self.signature.width
35-
36-
37-
class GPIOPeripheral(wiring.Component):
3826
class DO(csr.Register, access="rw"):
3927
"""output data (R/W, ignored for pins configured as inputs)"""
28+
4029
def __init__(self, width):
4130
super().__init__({"pins": csr.Field(csr.action.RW, unsigned(width))})
4231

4332
class OE(csr.Register, access="rw"):
4433
"""output enable (R/W) 1=output, 0=input"""
34+
4535
def __init__(self, width):
4636
super().__init__({"pins": csr.Field(csr.action.RW, unsigned(width))})
4737

4838
class DI(csr.Register, access="r"):
4939
"""input data (R)"""
40+
5041
def __init__(self, width):
5142
super().__init__({"pins": csr.Field(csr.action.R, unsigned(width))})
5243

53-
def __init__(self, *, pins: GPIOPins):
44+
def __init__(self, *, width: int):
5445
"""Simple GPIO peripheral.
5546
5647
All pins default to input at power up.
5748
"""
58-
self.width = pins.width
59-
self.pins = pins
49+
self.width = width
6050

6151
regs = csr.Builder(addr_width=4, data_width=8)
6252

@@ -68,6 +58,7 @@ def __init__(self, *, pins: GPIOPins):
6858

6959
super().__init__({
7060
"bus": In(csr.Signature(addr_width=regs.addr_width, data_width=regs.data_width)),
61+
"pins": Out(self.Signature(width))
7162
})
7263
self.bus.memory_map = self._bridge.bus.memory_map
7364

amaranth_orchard/io/uart.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,20 @@
88
from chipflow_lib.platforms import OutputPinSignature, InputPinSignature
99

1010

11-
__all__ = ["UARTPins", "UARTPeripheral"]
11+
__all__ = ["UARTPeripheral"]
1212

1313

14-
class UARTPins(wiring.PureInterface):
14+
class UARTPeripheral(wiring.Component):
1515
class Signature(wiring.Signature):
1616
def __init__(self):
1717
super().__init__({
1818
"tx": Out(OutputPinSignature(1)),
1919
"rx": Out(InputPinSignature(1)),
2020
})
2121

22-
def create(self, *, path=(), src_loc_at=0):
23-
return UARTPins(path=path, src_loc_at=1 + src_loc_at)
24-
2522
def __init__(self, *, path=(), src_loc_at=0):
2623
super().__init__(self.Signature(), path=path, src_loc_at=1 + src_loc_at)
2724

28-
29-
class UARTPeripheral(wiring.Component):
3025
class TxData(csr.Register, access="w"):
3126
"""valid to write to when tx_rdy is 1, will trigger a transmit"""
3227
val: csr.Field(csr.action.W, unsigned(8))
@@ -55,22 +50,22 @@ def __init__(self, init_divisor):
5550
5651
TODO: Interrupts support, perhaps mimic something with upstream Linux kernel support...
5752
"""
58-
def __init__(self, *, init_divisor, pins):
53+
def __init__(self, *, init_divisor):
5954
self.init_divisor = init_divisor
60-
self.pins = pins
6155

6256
regs = csr.Builder(addr_width=5, data_width=8)
6357

64-
self._tx_data = regs.add("tx_data", self.TxData(), offset=0x00)
65-
self._rx_data = regs.add("rx_data", self.RxData(), offset=0x04)
66-
self._tx_rdy = regs.add("tx_rdy", self.TxReady(), offset=0x08)
58+
self._tx_data = regs.add("tx_data", self.TxData(), offset=0x00)
59+
self._rx_data = regs.add("rx_data", self.RxData(), offset=0x04)
60+
self._tx_rdy = regs.add("tx_rdy", self.TxReady(), offset=0x08)
6761
self._rx_avail = regs.add("rx_avail", self.RxAvail(), offset=0x0c)
68-
self._divisor = regs.add("divisor", self.Divisor(init_divisor), offset=0x10)
62+
self._divisor = regs.add("divisor", self.Divisor(init_divisor), offset=0x10)
6963

7064
self._bridge = csr.Bridge(regs.as_memory_map())
7165

7266
super().__init__({
7367
"bus": In(csr.Signature(addr_width=regs.addr_width, data_width=regs.data_width)),
68+
"pins": Out(self.Signature()),
7469
})
7570
self.bus.memory_map = self._bridge.bus.memory_map
7671

amaranth_orchard/memory/hyperram.py

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
# Copyright (c) 2021-2022 gatecat <[email protected]>
66
# SPDX-License-Identifier: BSD-2-Clause
77

8-
from amaranth import *
8+
from amaranth import Module, ClockSignal, ResetSignal, Signal, unsigned, ClockDomain
99
from amaranth.lib import wiring
1010
from amaranth.lib.wiring import In, Out, connect, flipped
1111
from amaranth.utils import ceil_log2
1212

13-
from amaranth.sim import Simulator
14-
1513
from amaranth_soc import csr, wishbone
1614
from amaranth_soc.memory import MemoryMap
1715

1816
from chipflow_lib.platforms import BidirPinSignature, OutputPinSignature
1917

20-
__all__ = ["HyperRAMPins", "HyperRAM"]
18+
__all__ = ["HyperRAM"]
2119

2220

23-
class HyperRAMPins(wiring.PureInterface):
21+
class HyperRAM(wiring.Component):
2422
class Signature(wiring.Signature):
2523
def __init__(self, *, cs_count=1):
2624
super().__init__({
@@ -31,15 +29,6 @@ def __init__(self, *, cs_count=1):
3129
"dq": Out(BidirPinSignature(8)),
3230
})
3331

34-
def create(self, *, path=(), src_loc_at=0):
35-
return HyperRAMPins(cs_count=self.cs_count, src_loc_at=1 + src_loc_at)
36-
37-
def __init__(self, *, cs_count=1, path=(), src_loc_at=0):
38-
super().__init__(self.Signature(cs_count=cs_count), path=path, src_loc_at=1 + src_loc_at)
39-
self.cs_count = cs_count
40-
41-
42-
class HyperRAM(wiring.Component):
4332
class CtrlConfig(csr.Register, access="rw"):
4433
def __init__(self, init_latency):
4534
super().__init__({
@@ -79,6 +68,7 @@ def __init__(self, mem_name=("mem",), *, pins, init_latency=7):
7968
"ctrl_bus": In(csr.Signature(addr_width=regs.addr_width, data_width=regs.data_width)),
8069
"data_bus": In(wishbone.Signature(addr_width=ceil_log2(self.size >> 2), data_width=32,
8170
granularity=8)),
71+
"pins": Out(self.Signature()),
8272
})
8373
self.ctrl_bus.memory_map = ctrl_memory_map
8474
self.data_bus.memory_map = data_memory_map
@@ -142,32 +132,32 @@ def elaborate(self, platform):
142132
self.pins.dq.oe.eq(1),
143133
counter.eq(6),
144134
# Assign CA
145-
sr[47].eq(~self.data_bus.we), # R/W#
146-
sr[46].eq(0), # memory space
147-
sr[45].eq(1), # linear burst
148-
sr[16:45].eq(self.data_bus.adr[2:21]), # upper address
149-
sr[4:16].eq(0), # RFU
150-
sr[1:3].eq(self.data_bus.adr[0:2]), # lower address
151-
sr[0].eq(0), # address LSB (0 for 32-bit xfers)
135+
sr[47].eq(~self.data_bus.we), # R/W#
136+
sr[46].eq(0), # memory space
137+
sr[45].eq(1), # linear burst
138+
sr[16:45].eq(self.data_bus.adr[2:21]), # upper address
139+
sr[4:16].eq(0), # RFU
140+
sr[1:3].eq(self.data_bus.adr[0:2]), # lower address
141+
sr[0].eq(0), # address LSB (0 for 32-bit xfers)
152142
latched_adr.eq(self.data_bus.adr),
153143
latched_we.eq(self.data_bus.we),
154144
is_ctrl_write.eq(0),
155145
]
156146
m.next = "WAIT_CA"
157-
with m.If(self._hram_cfg.f.val.w_stb): # config register write
147+
with m.If(self._hram_cfg.f.val.w_stb): # config register write
158148
m.d.sync += [
159149
csn.eq(~(1 << (self._hram_cfg.f.val.w_data[16:16+ceil_log2(self.cs_count)]))),
160150
self.pins.dq.oe.eq(1),
161151
counter.eq(6),
162152
# Assign CA
163-
sr[47].eq(0), # R/W#
164-
sr[46].eq(1), # memory space
165-
sr[45].eq(1), # linear burst
166-
sr[24:45].eq(1), # upper address
167-
sr[16:24].eq(0), #
168-
sr[4:16].eq(0), # RFU
169-
sr[1:3].eq(0), # lower address
170-
sr[0].eq(0), # address LSB (0 for 32-bit xfers)
153+
sr[47].eq(0), # R/W#
154+
sr[46].eq(1), # memory space
155+
sr[45].eq(1), # linear burst
156+
sr[24:45].eq(1), # upper address
157+
sr[16:24].eq(0), #
158+
sr[4:16].eq(0), # RFU
159+
sr[1:3].eq(0), # lower address
160+
sr[0].eq(0), # address LSB (0 for 32-bit xfers)
171161
latched_cfg.eq(self._hram_cfg.f.val.w_data[0:16]),
172162
is_ctrl_write.eq(1),
173163
]
@@ -260,51 +250,3 @@ def elaborate(self, platform):
260250
]
261251
m.next = "IDLE"
262252
return m
263-
264-
def sim():
265-
pins = HyperRAMPins(cs_count=1)
266-
m = Module()
267-
m.submodules.hram = hram = HyperRAM(pins=pins)
268-
sim = Simulator(m)
269-
sim.add_clock(1e-6)
270-
def process():
271-
yield hram.data_bus.adr.eq(0x5A5A5A)
272-
yield hram.data_bus.dat_w.eq(0xF0F0F0F0)
273-
yield hram.data_bus.sel.eq(1)
274-
yield hram.data_bus.we.eq(1)
275-
yield hram.data_bus.stb.eq(1)
276-
yield hram.data_bus.cyc.eq(1)
277-
for i in range(100):
278-
if (yield hram.data_bus.ack):
279-
yield hram.data_bus.stb.eq(0)
280-
yield hram.data_bus.cyc.eq(0)
281-
yield
282-
yield hram.data_bus.adr.eq(0x5A5A5A)
283-
yield hram.data_bus.sel.eq(1)
284-
yield hram.data_bus.we.eq(0)
285-
yield hram.data_bus.stb.eq(1)
286-
yield hram.data_bus.cyc.eq(1)
287-
yield pins.rwds.i.eq(1)
288-
yield pins.dq.i.eq(0xFF)
289-
for i in range(100):
290-
if (yield hram.data_bus.ack):
291-
yield hram.data_bus.stb.eq(0)
292-
yield hram.data_bus.cyc.eq(0)
293-
yield
294-
yield hram.ctrl_bus.adr.eq(1)
295-
yield hram.ctrl_bus.dat_w.eq(0x55AA)
296-
yield hram.ctrl_bus.sel.eq(0xF)
297-
yield hram.ctrl_bus.we.eq(1)
298-
yield hram.ctrl_bus.stb.eq(1)
299-
yield hram.ctrl_bus.cyc.eq(1)
300-
for i in range(100):
301-
if (yield hram.ctrl_bus.ack):
302-
yield hram.ctrl_bus.stb.eq(0)
303-
yield hram.ctrl_bus.cyc.eq(0)
304-
yield
305-
sim.add_sync_process(process)
306-
with sim.write_vcd("hyperram.vcd", "hyperram.gtkw"):
307-
sim.run()
308-
309-
if __name__ == '__main__':
310-
sim()

amaranth_orchard/memory/spimemio.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from amaranth_soc.memory import MemoryMap
1010
from chipflow_lib.platforms import BidirPinSignature,OutputPinSignature
1111

12-
__all__ = ["QSPIPins", "SPIMemIO"]
12+
__all__ = ["SPIMemIO"]
1313

1414

15-
class QSPIPins(wiring.PureInterface):
15+
class SPIMemIO(wiring.Component):
1616
class Signature(wiring.Signature):
1717
def __init__(self):
1818
super().__init__({
@@ -21,14 +21,6 @@ def __init__(self):
2121
"d": Out(BidirPinSignature(4, all_have_oe=True)),
2222
})
2323

24-
def create(self, *, path=(), src_loc_at=0):
25-
return QSPIPins(path=path, src_loc_at=1 + src_loc_at)
26-
27-
def __init__(self, *, path=(), src_loc_at=0):
28-
super().__init__(self.Signature(), path=path, src_loc_at=1 + src_loc_at)
29-
30-
31-
class SPIMemIO(wiring.Component):
3224
class _ControlBridge(wiring.Component):
3325
bus: In(csr.Signature(addr_width=exact_log2(4), data_width=8))
3426
cfgreg_we: Out(unsigned(4))
@@ -56,12 +48,12 @@ def elaborate(self, platform):
5648
- data_bus is a bus peripheral that directly maps the 16MB of read-only flash memory.
5749
"""
5850

59-
def __init__(self, mem_name=("mem",), cfg_name=("cfg",), *):
51+
def __init__(self, *, mem_name=("mem",), cfg_name=("cfg",)):
6052
self.size = 2**24
6153
size_words = (self.size * 8) // 32
6254

6355
super().__init__({
64-
"qspi": Out(QSPIPins.Signature()),
56+
"pins": Out(QSPIPins.Signature()),
6557
"ctrl_bus": In(csr.Signature(addr_width=exact_log2(4), data_width=8)),
6658
"data_bus": In(wishbone.Signature(addr_width=exact_log2(size_words), data_width=32,
6759
granularity=8)),
@@ -89,19 +81,19 @@ def elaborate(self, platform):
8981
"i_resetn": ~ResetSignal(),
9082
"i_valid": self.data_bus.cyc & self.data_bus.stb,
9183
"o_ready": spi_ready,
92-
"i_addr": Cat(Const(0, 2), self.data_bus.adr), # Hack to force a 1MB offset
84+
"i_addr": Cat(Const(0, 2), self.data_bus.adr), # Hack to force a 1MB offset
9385
"o_rdata": self.data_bus.dat_r,
94-
"o_flash_csb": self.qspi.csn.o,
95-
"o_flash_clk": self.qspi.clk.o,
86+
"o_flash_csb": self.pins.csn.o,
87+
"o_flash_clk": self.pins.clk.o,
9688
"i_cfgreg_we": ctrl_bridge.cfgreg_we,
9789
"i_cfgreg_di": ctrl_bridge.cfgreg_di,
9890
"o_cfgreg_do": ctrl_bridge.cfgreg_do,
9991
} | {
100-
f"o_flash_io{n}_oe": self.qspi.d.oe[n] for n in range(4)
92+
f"o_flash_io{n}_oe": self.pins.d.oe[n] for n in range(4)
10193
} | {
102-
f"o_flash_io{n}_do": self.qspi.d.o[n] for n in range(4)
94+
f"o_flash_io{n}_do": self.pins.d.o[n] for n in range(4)
10395
} | {
104-
f"i_flash_io{n}_di": self.qspi.d.i[n] for n in range(4)
96+
f"i_flash_io{n}_di": self.pins.d.i[n] for n in range(4)
10597
}
10698

10799
m.submodules.spimemio = Instance("spimemio", **verilog_map)

0 commit comments

Comments
 (0)