1- import warnings
2-
31from .. import *
4- with warnings .catch_warnings ():
5- warnings .filterwarnings (action = "ignore" , category = DeprecationWarning )
6- from ..hdl .rec import *
7- from ..lib .wiring import In , Out , Signature , flipped , FlippedInterface
8-
9-
10- __all__ = ["pin_layout" , "Pin" ]
11-
12-
13- def _pin_signature (width , dir , xdr = 0 ):
14- if not isinstance (width , int ) or width < 0 :
15- raise TypeError ("Width must be a non-negative integer, not {!r}"
16- .format (width ))
17- if dir not in ("i" , "o" , "oe" , "io" ):
18- raise TypeError ("Direction must be one of \" i\" , \" o\" , \" io\" , or \" oe\" , not {!r}" ""
19- .format (dir ))
20- if not isinstance (xdr , int ) or xdr < 0 :
21- raise TypeError ("Gearing ratio must be a non-negative integer, not {!r}"
22- .format (xdr ))
23-
24- members = {}
25- if dir in ("i" , "io" ):
26- if xdr > 0 :
27- members ["i_clk" ] = In (1 )
28- if xdr > 2 :
29- members ["i_fclk" ] = In (1 )
30- if xdr in (0 , 1 ):
31- members ["i" ] = In (width )
32- else :
33- for n in range (xdr ):
34- members [f"i{ n } " ] = In (width )
35- if dir in ("o" , "oe" , "io" ):
36- if xdr > 0 :
37- members ["o_clk" ] = Out (1 )
38- if xdr > 2 :
39- members ["o_fclk" ] = Out (1 )
40- if xdr in (0 , 1 ):
41- members ["o" ] = Out (width )
42- else :
43- for n in range (xdr ):
44- members [f"o{ n } " ] = Out (width )
45- if dir in ("oe" , "io" ):
46- members ["oe" ] = Out (1 )
47- return Signature (members )
2+ from ..lib .wiring import In , Out , Signature , PureInterface
483
494
50- def pin_layout (width , dir , xdr = 0 ):
51- """
52- Layout of the platform interface of a pin or several pins, which may be used inside
53- user-defined records.
54-
55- See :class:`Pin` for details.
56- """
57- fields = []
58- for name , member in _pin_signature (width , dir , xdr ).members .items ():
59- fields .append ((name , member .shape ))
60- return Layout (fields )
5+ __all__ = ["Pin" ]
616
627
63- class Pin (Record ):
8+ class Pin (PureInterface ):
649 """
6510 An interface to an I/O buffer or a group of them that provides uniform access to input, output,
6611 or tristate buffers that may include a 1:n gearbox. (A 1:2 gearbox is typically called "DDR".)
6712
68- A :class:`Pin` is identical to a :class:`Record` that uses the corresponding :meth:`pin_layout`
69- except that it allows accessing the parameters like ``width`` as attributes. It is legal to use
70- a plain :class:`Record` anywhere a :class:`Pin` is used, provided that these attributes are
71- not necessary.
13+ This is an interface object using :class:`Pin.Signature` as its signature. The signature flows
14+ are defined from the point of view of a component that drives the I/O buffer.
7215
7316 Parameters
7417 ----------
@@ -87,8 +30,8 @@ class Pin(Record):
8730 are present instead, where ``N in range(0, N)``. For example, if ``xdr=2``, the I/O buffer
8831 is DDR; the signal ``i0`` reflects the value at the rising edge, and the signal ``i1``
8932 reflects the value at the falling edge.
90- name : str
91- Name of the underlying record .
33+ path : tuple of str
34+ As in :class:`PureInterface`, used to name the created signals .
9235
9336 Attributes
9437 ----------
@@ -119,23 +62,76 @@ class Pin(Record):
11962 cannot change direction more than once per cycle, so at most one output enable signal
12063 is present.
12164 """
122- def __init__ (self , width , dir , * , xdr = 0 , name = None , src_loc_at = 0 ):
123- self .width = width
124- self .dir = dir
125- self .xdr = xdr
12665
127- super ().__init__ (pin_layout (self .width , self .dir , self .xdr ),
128- name = name , src_loc_at = src_loc_at + 1 )
66+ class Signature (Signature ):
67+ """A signature for :class:`Pin`. The parameters are as defined on the ``Pin`` class,
68+ and are accessible as attributes.
69+ """
70+ def __init__ (self , width , dir , * , xdr = 0 ):
71+ if not isinstance (width , int ) or width < 0 :
72+ raise TypeError ("Width must be a non-negative integer, not {!r}"
73+ .format (width ))
74+ if dir not in ("i" , "o" , "oe" , "io" ):
75+ raise TypeError ("Direction must be one of \" i\" , \" o\" , \" io\" , or \" oe\" , not {!r}" ""
76+ .format (dir ))
77+ if not isinstance (xdr , int ) or xdr < 0 :
78+ raise TypeError ("Gearing ratio must be a non-negative integer, not {!r}"
79+ .format (xdr ))
80+
81+ self .width = width
82+ self .dir = dir
83+ self .xdr = xdr
84+
85+ members = {}
86+ if dir in ("i" , "io" ):
87+ if xdr > 0 :
88+ members ["i_clk" ] = Out (1 )
89+ if xdr > 2 :
90+ members ["i_fclk" ] = Out (1 )
91+ if xdr in (0 , 1 ):
92+ members ["i" ] = In (width )
93+ else :
94+ for n in range (xdr ):
95+ members [f"i{ n } " ] = In (width )
96+ if dir in ("o" , "oe" , "io" ):
97+ if xdr > 0 :
98+ members ["o_clk" ] = Out (1 )
99+ if xdr > 2 :
100+ members ["o_fclk" ] = Out (1 )
101+ if xdr in (0 , 1 ):
102+ members ["o" ] = Out (width )
103+ else :
104+ for n in range (xdr ):
105+ members [f"o{ n } " ] = Out (width )
106+ if dir in ("oe" , "io" ):
107+ members ["oe" ] = Out (1 )
108+ super ().__init__ (members )
109+
110+ def __eq__ (self , other ):
111+ return (type (self ) is type (other ) and
112+ self .width == other .width and
113+ self .dir == other .dir and
114+ self .xdr == other .xdr )
115+
116+ def create (self , * , path = None , src_loc_at = 0 ):
117+ return Pin (self .width , self .dir , xdr = self .xdr , path = path , src_loc_at = 1 + src_loc_at )
118+
119+ def __init__ (self , width , dir , * , xdr = 0 , name = None , path = None , src_loc_at = 0 ):
120+ if name is not None :
121+ if path is None :
122+ raise ValueError ("Cannot pass both name and path" )
123+ path = (name ,)
124+ signature = Pin .Signature (width , dir , xdr = xdr )
125+ super ().__init__ (signature , path = path , src_loc_at = src_loc_at + 1 )
129126
130127 @property
131- def signature (self ):
132- return _pin_signature (self .width , self .dir , self .xdr )
133-
134- def eq (self , other ):
135- first_field , _ , _ = next (iter (Pin (1 , dir = "o" ).layout ))
136- warnings .warn (f"`pin.eq(...)` is deprecated; use `pin.{ first_field } .eq(...)` here" ,
137- DeprecationWarning , stacklevel = 2 )
138- if isinstance (self , FlippedInterface ):
139- return Record .eq (flipped (self ), other )
140- else :
141- return Record .eq (self , other )
128+ def width (self ):
129+ return self .signature .width
130+
131+ @property
132+ def dir (self ):
133+ return self .signature .dir
134+
135+ @property
136+ def xdr (self ):
137+ return self .signature .xdr
0 commit comments