17
17
"""
18
18
19
19
import random
20
- from dataclasses import asdict
21
- from dataclasses import astuple
22
- from dataclasses import dataclass
23
- from dataclasses import fields
20
+ import dataclasses
24
21
25
22
from cocotb .binary import BinaryValue
26
23
@@ -42,21 +39,23 @@ class MyStruct(XLSStruct):
42
39
Returns:
43
40
type: The dataclass-decorated class with repr disabled.
44
41
"""
45
- return dataclass (cls , repr = False )
42
+ return dataclasses . dataclass (cls , repr = False )
46
43
47
- @dataclass
44
+ @dataclasses . dataclass
48
45
class XLSStruct :
49
- """
50
- Represents XLS struct on the Python side, allowing serialization/deserialization
51
- to/from common formats and usage with XLS{Driver, Monitor}.
46
+ """Represents XLS struct on the Python side.
47
+
48
+ Allows serialization/deserialization to/from common formats and usage with
49
+ XLS{Driver, Monitor}.
52
50
53
- The intended way to use this class is to inherit from it, specify the fields with
54
- <field>: <width> [= <default value>] syntax and decorate the inheriting class with
55
- @XLSDataclass. Objects of this class can be instantiated and used like usual
56
- dataclass objects, with a few extra methods and properties available. They can also
57
- be passed as arguments to XLSChannelDriver.send and will be serialized to expected
58
- bit vector. Class can be passed to XLSChannelMonitor ``struct`` constructor argument
59
- to automatically deserialize all transfers to the provided struct.
51
+ The intended way to use this class is to inherit from it, specify the fields
52
+ with <field>: <width> [= <default value>] syntax and decorate the inheriting
53
+ class with @XLSDataclass. Objects of this class can be instantiated and used
54
+ like usual dataclass objects, with a few extra methods and properties
55
+ available. They can also be passed as arguments to XLSChannelDriver.send and
56
+ will be serialized to expected bit vector. Class can be passed to
57
+ XLSChannelMonitor ``struct`` constructor argument to automatically
58
+ deserialize all transfers to the provided struct.
60
59
61
60
Example:
62
61
@@ -88,7 +87,7 @@ def _masks(cls):
88
87
returns [2'b11, 3'b111, 4'b1111].
89
88
"""
90
89
masks = []
91
- for field in fields (cls ):
90
+ for field in dataclasses . fields (cls ):
92
91
width = field .type
93
92
masks += [(1 << width ) - 1 ]
94
93
return masks
@@ -101,7 +100,7 @@ def _positions(cls):
101
100
returns [20, 18, 15, 11, 6, 0]
102
101
"""
103
102
positions = []
104
- for i , field in enumerate (fields (cls )):
103
+ for i , field in enumerate (dataclasses . fields (cls )):
105
104
width = field .type
106
105
if i == 0 :
107
106
positions += [cls .total_width - width ]
@@ -113,17 +112,18 @@ def _positions(cls):
113
112
@property
114
113
def total_width (cls ):
115
114
"""Returns total bit width of the struct."""
116
- return sum (field .type for field in fields (cls ))
115
+ return sum (field .type for field in dataclasses . fields (cls ))
117
116
118
117
@property
119
118
def value (self ):
120
119
"""Returns struct's value as a Python integer."""
121
120
value = 0
122
121
masks = self ._masks ()
123
122
positions = self ._positions ()
124
- for field_val , mask , pos in zip (astuple (self ), masks , positions ):
123
+ for field_val , mask , pos in zip (
124
+ dataclasses .astuple (self ), masks , positions ):
125
125
if field_val > mask :
126
- raise TruncationError (f "Signal value is wider than its bit width" )
126
+ raise TruncationError ("Signal value is wider than its bit width" )
127
127
value |= (field_val & mask ) << pos
128
128
return value
129
129
@@ -151,15 +151,15 @@ def from_int(cls, value):
151
151
instance = {}
152
152
masks = cls ._masks ()
153
153
positions = cls ._positions ()
154
- for field , mask , pos in zip (fields (cls ), masks , positions ):
154
+ for field , mask , pos in zip (dataclasses . fields (cls ), masks , positions ):
155
155
instance [field .name ] = (value >> pos ) & mask
156
156
return cls (** instance )
157
157
158
158
@classmethod
159
159
def randomize (cls ):
160
160
"""Returns an instance of the struct with all fields' values randomized."""
161
161
instance = {}
162
- for field in fields (cls ):
162
+ for field in dataclasses . fields (cls ):
163
163
instance [field .name ] = random .randrange (0 , 2 ** field .type )
164
164
return cls (** instance )
165
165
@@ -168,5 +168,7 @@ def __str__(self):
168
168
169
169
def __repr__ (self ):
170
170
classname = self .__class__ .__name__
171
- class_fields = [f"{ name } ={ hex (value )} " for name , value in asdict (self ).items ()]
171
+ class_fields = [
172
+ f"{ name } ={ hex (value )} " for name , value in dataclasses .asdict (self ).items ()
173
+ ]
172
174
return f"{ classname } ({ ', ' .join (class_fields )} )"
0 commit comments