|
| 1 | +""" |
| 2 | +Base types for the generated code are declared here, as well as exceptions and other supporting classes. |
| 3 | +Functionality to read, write and modify registers is also provided here. Several methods are provided, all sharing the same |
| 4 | +interface. |
| 5 | +""" |
| 6 | + |
| 7 | + |
| 8 | +class OutOfRangeReading(Exception): |
| 9 | + """Exception thrown when an out of range value is read from the register.""" |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +class OutOfRangeWriting(Exception): |
| 14 | + """Exception thrown when an out of range value is attempted to be written to the register.""" |
| 15 | + pass |
| 16 | + |
| 17 | + |
| 18 | +class OutOfRangeSpecifying(Exception): |
| 19 | + """Exception thrown when an a boundary is specified that is incoherent (e.g. with the number of bits in a field).""" |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +class FieldNotWritable(Exception): |
| 24 | + """Exception thrown when attempting to write to a read-only field.""" |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +class accesLayer: |
| 29 | + """This interface contains method declarations that should be used by classes that can implement any type of memory |
| 30 | + access the registers (including simulated). """ |
| 31 | + def read_register(self, addr, offset=0, width=32): |
| 32 | + """ |
| 33 | + Reads a register |
| 34 | + :param addr: address |
| 35 | + :param offset: bit offset |
| 36 | + :param width: number of bits after the offset to read |
| 37 | + :return: the requested bits from the register |
| 38 | + """ |
| 39 | + raise NotImplementedError() |
| 40 | + |
| 41 | + def modify_register(self, addr, data, offset=0, width=32): |
| 42 | + """ |
| 43 | + Partially writes a register |
| 44 | + :param addr: address |
| 45 | + :param addr: data to be written |
| 46 | + :param offset: bit offset |
| 47 | + :param width: number of bits after the offset to write |
| 48 | + """ |
| 49 | + raise NotImplementedError() |
| 50 | + |
| 51 | + def write_register(self, addr, data): |
| 52 | + """ |
| 53 | + writes a complete register |
| 54 | + :param addr: address |
| 55 | + :param addr: data to be written |
| 56 | + """ |
| 57 | + raise NotImplementedError() |
| 58 | + |
| 59 | + |
| 60 | +class IP(): |
| 61 | + def __init__(self, parent, base_address, acces_layer=accesLayer): |
| 62 | + self._base_address = base_address |
| 63 | + self._parent = parent |
| 64 | + self._acces_layer = acces_layer |
| 65 | + |
| 66 | + def get_base_address(self): |
| 67 | + return self._base_address |
| 68 | + |
| 69 | + def get_acces_layer(self): |
| 70 | + return self._acces_layer |
| 71 | + |
| 72 | + |
| 73 | +class Register(): |
| 74 | + def __init__(self, parent_ip, address_offset): |
| 75 | + """ |
| 76 | + Constructor for a register of an IP |
| 77 | + :param parent_ip: an IP object to which this register belongs |
| 78 | + :param address_offset: the address offset (from the IP's base address) |
| 79 | + """ |
| 80 | + self._parent_ip = parent_ip |
| 81 | + self._address_offset = address_offset |
| 82 | + self._acces_layer = parent_ip.get_acces_layer() |
| 83 | + |
| 84 | + def get_address_offset(self): |
| 85 | + """ |
| 86 | + Returns the address offset |
| 87 | + :return: the address offset |
| 88 | + """ |
| 89 | + return self._address_offset |
| 90 | + |
| 91 | + def get_parent_ip(self): |
| 92 | + """ |
| 93 | + Returns the parent IP object |
| 94 | + :return: the parent IP object |
| 95 | + """ |
| 96 | + return self._parent_ip |
| 97 | + |
| 98 | + def get_acces_layer(self): |
| 99 | + """ |
| 100 | + Returns the acces layer object currently used. Usually it's the parent IP's access layer |
| 101 | + :return: the acces layer object currently used |
| 102 | + """ |
| 103 | + return self._acces_layer |
| 104 | + |
| 105 | + def get(self): |
| 106 | + """ |
| 107 | + returns the value of this register (as unsigned int) |
| 108 | + :return: the value of this register |
| 109 | + """ |
| 110 | + self.get_acces_layer().read_register(self.get_parent_ip().get_base_address() + |
| 111 | + self.get_address_offset()) |
| 112 | + |
| 113 | + def set(self, value): |
| 114 | + """ |
| 115 | + Sets the value of this register (as unsigned int) |
| 116 | + :value: the value to set this register |
| 117 | + """ |
| 118 | + self.get_acces_layer().write_register(self.get_parent_ip().get_base_address() + |
| 119 | + self.get_address_offset(), value) |
| 120 | + |
| 121 | + |
| 122 | +class Field(): |
| 123 | + """This base class represents a field in the register. It will work like an unsigned integer field and is the prefered |
| 124 | + way of obtaining 'raw values' from the registers but should be derived from and not instanced. """ |
| 125 | + def __init__(self, parent_register, bit_width, bit_offset, access): |
| 126 | + """ |
| 127 | + Constructor for a Field of a register |
| 128 | + :param parent_register: parent register |
| 129 | + :param bit_width: number of bits used by the field |
| 130 | + :param bit_offset: bit offset from the beginning of the register |
| 131 | + :param acces: a string for accessibility ('read-write' / 'read-only') |
| 132 | + """ |
| 133 | + self._parent_register = parent_register |
| 134 | + self._bit_width = bit_width |
| 135 | + self._bit_offset = bit_offset |
| 136 | + self._access = access |
| 137 | + |
| 138 | + def get(self): |
| 139 | + """ |
| 140 | + Returns the value of the field |
| 141 | + :return: value of the field |
| 142 | + """ |
| 143 | + return self._parent_register.get_acces_layer().read_register(self._parent_register.get_parent_ip().get_base_address() + |
| 144 | + self._parent_register.get_address_offset(), |
| 145 | + self._bit_offset, |
| 146 | + self._bit_width) |
| 147 | + |
| 148 | + def set(self, value): |
| 149 | + """ |
| 150 | + Sets the value of the field |
| 151 | + :value: value to be set |
| 152 | + """ |
| 153 | + if self._access == "read-only": |
| 154 | + raise FieldNotWritable() |
| 155 | + |
| 156 | + self._parent_register.get_acces_layer().modify_register(self._parent_register.get_parent_ip().get_base_address() + |
| 157 | + self._parent_register.get_address_offset(), |
| 158 | + value, |
| 159 | + self._bit_offset, |
| 160 | + self._bit_width) |
| 161 | + |
| 162 | + |
| 163 | +class EnumField(Field): |
| 164 | + """Represents an enumerated type.""" |
| 165 | + def __init__(self, parent_register, bit_width, bit_offset, access, enum_type): |
| 166 | + """ |
| 167 | + :param name: name |
| 168 | + :param parent_register: parent register |
| 169 | + :param bit_width: number of bits used by the field |
| 170 | + :param bit_offset: bit offset from the beginning of the register |
| 171 | + :param acces: a string for accessibility ('read-write' / 'read-only') |
| 172 | + :param enum_type: a Python type derived from IntEnum which we want to represent |
| 173 | + """ |
| 174 | + super().__init__(parent_register, bit_width, bit_offset, access) |
| 175 | + self._enum_type = enum_type |
| 176 | + self._int_values = list(map(int, enum_type)) |
| 177 | + |
| 178 | + def get(self): |
| 179 | + """ |
| 180 | + Returns the value of the field |
| 181 | + :return: the current enumeration represented by the field |
| 182 | + """ |
| 183 | + val = super().get() |
| 184 | + if val not in self._int_values: |
| 185 | + return OutOfRangeReading() |
| 186 | + return self._enum_type(val) |
| 187 | + |
| 188 | + def set(self, value): |
| 189 | + """ |
| 190 | + Sets the value of the field |
| 191 | + :value: value to be set. Either an integer or an enumeration of 'our' enumerated type |
| 192 | + """ |
| 193 | + if value in self._int_values: |
| 194 | + super().set(value) |
| 195 | + else: |
| 196 | + raise OutOfRangeWriting() |
| 197 | + |
| 198 | + |
| 199 | +class IntegerField(Field): |
| 200 | + """This class represents an (unsigned) integer. It derives from Field and relies on it for abstracting reads and |
| 201 | + writes. Therefore, this class is basically concerned with boundary checking.""" |
| 202 | + def __init__(self, parent_register, bit_width, bit_offset, access, minimum, maximum): |
| 203 | + """ |
| 204 | + Constructor for a Field of a register |
| 205 | + :param name: name |
| 206 | + :param parent_register: parent register |
| 207 | + :param bit_width: number of bits used by the field |
| 208 | + :param bit_offset: bit offset from the beginning of the register |
| 209 | + :param acces: a string for accessibility ('read-write' / 'read-only') |
| 210 | + :param minimum: minimum value that this field can have (None to assume the minimum representable) |
| 211 | + :param maximum: maximum value that this field can have (None to assume the maximum representable) |
| 212 | + """ |
| 213 | + super().__init__(parent_register, bit_width, bit_offset, access) |
| 214 | + type_minimum = 0 |
| 215 | + type_maximum = (2 ** bit_width) - 1 |
| 216 | + if minimum is not None: |
| 217 | + if minimum < type_minimum: |
| 218 | + raise OutOfRangeSpecifying() |
| 219 | + else: |
| 220 | + self._minimum = minimum |
| 221 | + else: |
| 222 | + self._minimum = type_minimum |
| 223 | + |
| 224 | + if maximum is not None: |
| 225 | + if maximum > type_maximum: |
| 226 | + raise OutOfRangeSpecifying() |
| 227 | + else: |
| 228 | + self._maximum = maximum |
| 229 | + else: |
| 230 | + self._maximum = type_maximum |
| 231 | + |
| 232 | + def get(self): |
| 233 | + """ |
| 234 | + Returns the value of the field |
| 235 | + :return: value of the field |
| 236 | + """ |
| 237 | + val = super().get() |
| 238 | + if val < self._minimum or val > self._maximum: |
| 239 | + raise OutOfRangeReading() |
| 240 | + return val |
| 241 | + |
| 242 | + def set(self, value): |
| 243 | + """ |
| 244 | + Sets the value of the field |
| 245 | + :value: value to be set |
| 246 | + """ |
| 247 | + if value < self._minimum or value > self._maximum: |
| 248 | + raise OutOfRangeWriting() |
| 249 | + else: |
| 250 | + super().set(value) |
| 251 | + |
| 252 | + def get_minimum(self): |
| 253 | + """ |
| 254 | + Gets the minimum value that can be defined for the field |
| 255 | + :return: minimum |
| 256 | + """ |
| 257 | + return self._minimum |
| 258 | + |
| 259 | + def get_maximum(self): |
| 260 | + """ |
| 261 | + Gets the maximum value that can be defined for the field |
| 262 | + :return: maximum |
| 263 | + """ |
| 264 | + return self._maximum |
0 commit comments