11from collections .abc import Mapping , Sequence
22from amaranth import *
33from amaranth .lib import enum , wiring
4- from amaranth .lib .wiring import In , Out
4+ from amaranth .lib .wiring import In , Out , connect , flipped
55
66from ..memory import MemoryMap
77from .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
195204class 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
0 commit comments