1
+ """
2
+ Configuration and Status Registers
3
+ **********************************
4
+
5
+ The lowest-level description of a register is provided by the ``CSR`` class,
6
+ which maps to the value at a single address on the target bus. Also provided
7
+ are helper classes for dealing with values larger than the CSR buses data
8
+ width.
9
+
10
+ * ``CSRConstant``, for constant values.
11
+ * ``CSRStatus``, for providing information to the CPU.
12
+ * ``CSRStorage``, for allowing control via the CPU.
13
+
14
+ Generating register banks
15
+ =========================
16
+ A module can provide bus-independent CSRs by implementing a ``get_csrs`` method
17
+ that returns a list of instances of the classes described above.
18
+
19
+ Similarly, bus-independent memories can be returned as a list by a
20
+ ``get_memories`` method.
21
+
22
+ To avoid listing those manually, a module can inherit from the ``AutoCSR``
23
+ class, which provides ``get_csrs`` and ``get_memories`` methods that scan for
24
+ CSR and memory attributes and return their list.
25
+ """
26
+
1
27
from migen import *
2
28
from migen .util .misc import xdir
3
29
from migen .fhdl .tracer import get_obj_var_name
@@ -13,6 +39,12 @@ def __init__(self, size, name):
13
39
14
40
15
41
class CSRConstant (DUID ):
42
+ """Register which contains a constant value.
43
+
44
+ Useful for providing information on how a HDL was instantiated to firmware
45
+ running on the device.
46
+ """
47
+
16
48
def __init__ (self , value , bits_sign = None , name = None ):
17
49
DUID .__init__ (self )
18
50
self .value = Constant (value , bits_sign )
@@ -21,20 +53,49 @@ def __init__(self, value, bits_sign=None, name=None):
21
53
raise ValueError ("Cannot extract CSR name from code, need to specify." )
22
54
23
55
def read (self ):
56
+ """Read method for simulation."""
24
57
return self .value .value
25
58
26
59
27
60
class CSR (_CSRBase ):
61
+ """Basic CSR register.
62
+
63
+ Parameters
64
+ ----------
65
+ size : int
66
+ Size of the CSR register in bits.
67
+ Must be less than CSR bus width!
68
+
69
+ name : string
70
+ Provide (or override the name) of the CSR register.
71
+
72
+ Attributes
73
+ ----------
74
+ r : Signal(size), out
75
+ Contains the data written from the bus interface.
76
+ ``r`` is only valid when ``re`` is high.
77
+
78
+ re : Signal(), out
79
+ The strobe signal for ``r``.
80
+ It is active for one cycle, after or during a write from the bus.
81
+
82
+ w : Signal(size), in
83
+ The value to be read from the bus.
84
+ Must be provided at all times.
85
+ """
86
+
28
87
def __init__ (self , size = 1 , name = None ):
29
88
_CSRBase .__init__ (self , size , name )
30
89
self .re = Signal (name = self .name + "_re" )
31
90
self .r = Signal (self .size , name = self .name + "_r" )
32
91
self .w = Signal (self .size , name = self .name + "_w" )
33
92
34
93
def read (self ):
94
+ """Read method for simulation."""
35
95
return (yield self .w )
36
96
37
97
def write (self , value ):
98
+ """Write method for simulation."""
38
99
yield self .r .eq (value )
39
100
yield self .re .eq (1 )
40
101
yield
@@ -56,6 +117,39 @@ def do_finalize(self, busword):
56
117
57
118
58
119
class CSRStatus (_CompoundCSR ):
120
+ """Status Register.
121
+
122
+ The ``CSRStatus`` class is meant to be used as a status register that is
123
+ read-only from the CPU.
124
+
125
+ The user design is expected to drive its ``status`` signal.
126
+
127
+ The advantage of using ``CSRStatus`` instead of using ``CSR`` and driving
128
+ ``w`` is that the width of ``CSRStatus`` can be arbitrary.
129
+
130
+ Status registers larger than the bus word width are automatically broken
131
+ down into several ``CSR`` registers to span several addresses.
132
+
133
+ *Be careful, though:* the atomicity of reads is not guaranteed.
134
+
135
+ Parameters
136
+ ----------
137
+ size : int
138
+ Size of the CSR register in bits.
139
+ Can be bigger than the CSR bus width.
140
+
141
+ reset : string
142
+ Value of the register after reset.
143
+
144
+ name : string
145
+ Provide (or override the name) of the ``CSRStatus`` register.
146
+
147
+ Attributes
148
+ ----------
149
+ status : Signal(size), in
150
+ The value of the CSRStatus register.
151
+ """
152
+
59
153
def __init__ (self , size = 1 , reset = 0 , name = None ):
60
154
_CompoundCSR .__init__ (self , size , name )
61
155
self .status = Signal (self .size , reset = reset )
@@ -69,10 +163,64 @@ def do_finalize(self, busword):
69
163
self .simple_csrs .append (sc )
70
164
71
165
def read (self ):
166
+ """Read method for simulation."""
72
167
return (yield self .status )
73
168
74
169
75
170
class CSRStorage (_CompoundCSR ):
171
+ """Control Register.
172
+
173
+ The ``CSRStorage`` class provides a memory location that can be read and
174
+ written by the CPU, and read and optionally written by the design.
175
+
176
+ It can span several CSR addresses.
177
+
178
+ Parameters
179
+ ----------
180
+ size : int
181
+ Size of the CSR register in bits.
182
+ Can be bigger than the CSR bus width.
183
+
184
+ reset : string
185
+ Value of the register after reset.
186
+
187
+ atomic_write : bool
188
+ Provide an mechanism for atomic CPU writes is provided.
189
+ When enabled, writes to the first CSR addresses go to a back-buffer
190
+ whose contents are atomically copied to the main buffer when the last
191
+ address is written.
192
+
193
+ write_from_dev : bool
194
+ Allow the design to update the CSRStorage value.
195
+ *Warning*: The atomicity of reads by the CPU is not guaranteed.
196
+
197
+ alignment_bits : int
198
+ ???
199
+
200
+ name : string
201
+ Provide (or override the name) of the ``CSRStatus`` register.
202
+
203
+ Attributes
204
+ ----------
205
+ storage_full : Signal(size), out
206
+ ???
207
+
208
+ storage : Signal(size), out
209
+ Signal providing the value of the ``CSRStorage`` object.
210
+
211
+ re : Signal(), in
212
+ The strobe signal indicating a write to the ``CSRStorage`` register.
213
+ It is active for one cycle, after or during a write from the bus.
214
+
215
+ we : Signal(), out
216
+ Only available when ``write_from_dev == True``
217
+ ???
218
+
219
+ dat_w : Signal(), out
220
+ Only available when ``write_from_dev == True``
221
+ ???
222
+ """
223
+
76
224
def __init__ (self , size = 1 , reset = 0 , atomic_write = False , write_from_dev = False , alignment_bits = 0 , name = None ):
77
225
_CompoundCSR .__init__ (self , size , name )
78
226
self .alignment_bits = alignment_bits
@@ -115,9 +263,11 @@ def do_finalize(self, busword):
115
263
self .sync += self .re .eq (sc .re )
116
264
117
265
def read (self ):
266
+ """Read method for simulation."""
118
267
return (yield self .storage ) << self .alignment_bits
119
268
120
269
def write (self , value ):
270
+ """Write method for simulation."""
121
271
yield self .storage .eq (value >> self .alignment_bits )
122
272
yield self .re .eq (1 )
123
273
yield
@@ -162,6 +312,17 @@ def gatherer(self):
162
312
163
313
164
314
class AutoCSR :
315
+ """MixIn to provide bus independent access to CSR registers.
316
+
317
+ A module can inherit from the ``AutoCSR`` class, which provides
318
+ ``get_csrs``, ``get_memories`` and ``get_constants`` methods that scan for
319
+ CSR and memory attributes and return their list.
320
+
321
+ If the module has child objects that implement ``get_csrs``,
322
+ ``get_memories`` or ``get_constants``, they will be called by the
323
+ ``AutoCSR`` methods and their CSR and memories added to the lists returned,
324
+ with the child objects' names as prefixes.
325
+ """
165
326
get_memories = _make_gatherer ("get_memories" , Memory , memprefix )
166
327
get_csrs = _make_gatherer ("get_csrs" , _CSRBase , csrprefix )
167
328
get_constants = _make_gatherer ("get_constants" , CSRConstant , csrprefix )
0 commit comments