Skip to content

Commit fd8973f

Browse files
committed
csr.reg: migrate to lib.wiring components.
Also, fix FieldPort signature direction.
1 parent 3a562fd commit fd8973f

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

amaranth_soc/csr/reg.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections.abc import Mapping, Sequence
22
from amaranth import *
33
from amaranth.lib import enum, wiring
4-
from amaranth.lib.wiring import In, Out
4+
from amaranth.lib.wiring import In, Out, connect, flipped
55

66
from ..memory import MemoryMap
77
from .bus import Element, Multiplexer
@@ -57,10 +57,10 @@ def __init__(self, shape, access):
5757
self._access = FieldPort.Access(access)
5858

5959
members = {
60-
"r_data": Out(self.shape),
61-
"r_stb": In(1),
62-
"w_data": In(self.shape),
63-
"w_stb": In(1),
60+
"r_data": In(self.shape),
61+
"r_stb": Out(1),
62+
"w_data": Out(self.shape),
63+
"w_stb": Out(1),
6464
}
6565
super().__init__(members)
6666

@@ -154,7 +154,7 @@ def __repr__(self):
154154
return f"csr.FieldPort({self.signature!r})"
155155

156156

157-
class Field(Elaboratable):
157+
class Field(wiring.Component):
158158
_doc_template = """
159159
{description}
160160
@@ -181,15 +181,24 @@ class Field(Elaboratable):
181181
attributes="")
182182

183183
def __init__(self, shape, access):
184-
self.port = FieldPort.Signature(shape, access).create(path=("port",))
184+
FieldPort.Signature.check_parameters(shape, access)
185+
self._shape = Shape.cast(shape)
186+
self._access = FieldPort.Access(access)
187+
super().__init__()
185188

186189
@property
187190
def shape(self):
188-
return self.port.shape
191+
return self._shape
189192

190193
@property
191194
def access(self):
192-
return self.port.access
195+
return self._access
196+
197+
@property
198+
def signature(self):
199+
return wiring.Signature({
200+
"port": Out(FieldPort.Signature(self._shape, self._access)),
201+
})
193202

194203

195204
class FieldMap(Mapping):
@@ -346,7 +355,7 @@ def flatten(self):
346355
assert False # :nocov:
347356

348357

349-
class Register(Elaboratable):
358+
class Register(wiring.Component):
350359
"""CSR register.
351360
352361
Parameters
@@ -407,8 +416,10 @@ def __init__(self, access="rw", fields=None):
407416
raise ValueError(f"Field {'__'.join(field_name)} is writable, but register access "
408417
f"mode is {access!r}")
409418

410-
self.element = Element.Signature(width, access).create(path=("element",))
419+
self._width = width
420+
self._access = access
411421
self._fields = fields
422+
super().__init__()
412423

413424
@property
414425
def fields(self):
@@ -418,6 +429,12 @@ def fields(self):
418429
def f(self):
419430
return self._fields
420431

432+
@property
433+
def signature(self):
434+
return wiring.Signature({
435+
"element": Out(Element.Signature(self._width, self._access)),
436+
})
437+
421438
def __iter__(self):
422439
"""Recursively iterate over the field collection.
423440
@@ -684,7 +701,7 @@ def get_register(self, path):
684701
raise KeyError(path)
685702

686703

687-
class Bridge(Elaboratable):
704+
class Bridge(wiring.Component):
688705
"""CSR bridge.
689706
690707
Parameters
@@ -754,18 +771,20 @@ def get_register_param(path, root, kind):
754771

755772
self._map = register_map
756773
self._mux = Multiplexer(memory_map)
774+
super().__init__()
757775

758776
@property
759777
def register_map(self):
760778
return self._map
761779

762780
@property
763-
def bus(self):
764-
return self._mux.bus
781+
def signature(self):
782+
return self._mux.signature
765783

766784
def elaborate(self, platform):
767785
m = Module()
768786
for register, path in self.register_map.flatten():
769787
m.submodules["__".join(path)] = register
770788
m.submodules.mux = self._mux
789+
connect(m, flipped(self), self._mux)
771790
return m

tests/test_csr_reg.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,43 @@ def test_shape_1_ro(self):
1515
self.assertEqual(sig.shape, unsigned(1))
1616
self.assertEqual(sig.access, FieldPort.Access.R)
1717
self.assertEqual(sig.members, Signature({
18-
"r_data": Out(unsigned(1)),
19-
"r_stb": In(1),
20-
"w_data": In(unsigned(1)),
21-
"w_stb": In(1),
18+
"r_data": In(unsigned(1)),
19+
"r_stb": Out(1),
20+
"w_data": Out(unsigned(1)),
21+
"w_stb": Out(1),
2222
}).members)
2323

2424
def test_shape_8_rw(self):
2525
sig = FieldPort.Signature(8, "rw")
2626
self.assertEqual(sig.shape, unsigned(8))
2727
self.assertEqual(sig.access, FieldPort.Access.RW)
2828
self.assertEqual(sig.members, Signature({
29-
"r_data": Out(unsigned(8)),
30-
"r_stb": In(1),
31-
"w_data": In(unsigned(8)),
32-
"w_stb": In(1),
29+
"r_data": In(unsigned(8)),
30+
"r_stb": Out(1),
31+
"w_data": Out(unsigned(8)),
32+
"w_stb": Out(1),
3333
}).members)
3434

3535
def test_shape_10_wo(self):
3636
sig = FieldPort.Signature(10, "w")
3737
self.assertEqual(sig.shape, unsigned(10))
3838
self.assertEqual(sig.access, FieldPort.Access.W)
3939
self.assertEqual(sig.members, Signature({
40-
"r_data": Out(unsigned(10)),
41-
"r_stb": In(1),
42-
"w_data": In(unsigned(10)),
43-
"w_stb": In(1),
40+
"r_data": In(unsigned(10)),
41+
"r_stb": Out(1),
42+
"w_data": Out(unsigned(10)),
43+
"w_stb": Out(1),
4444
}).members)
4545

4646
def test_shape_0_rw(self):
4747
sig = FieldPort.Signature(0, "w")
4848
self.assertEqual(sig.shape, unsigned(0))
4949
self.assertEqual(sig.access, FieldPort.Access.W)
5050
self.assertEqual(sig.members, Signature({
51-
"r_data": Out(unsigned(0)),
52-
"r_stb": In(1),
53-
"w_data": In(unsigned(0)),
54-
"w_stb": In(1),
51+
"r_data": In(unsigned(0)),
52+
"r_stb": Out(1),
53+
"w_data": Out(unsigned(0)),
54+
"w_stb": Out(1),
5555
}).members)
5656

5757
def test_create(self):

0 commit comments

Comments
 (0)