def setimmediatevalue(self, value): """ Set the value of the underlying simulation object to value. Args: value (ctypes.Structure, cocotb.binary.BinaryValue, int, double) The value to drive onto the simulator object Raises: TypeError This operation will fail unless the handle refers to a modifiable object eg net, signal or variable. We determine the library call to make based on the type of the value Assigning integers less than 32-bits is faster """ if isinstance(value, get_python_integer_types()) and value < 0x7fffffff: simulator.set_signal_val_long(self._handle, value) return if isinstance(value, ctypes.Structure): value = BinaryValue(value=cocotb.utils.pack(value), bits=len(self)) elif isinstance(value, get_python_integer_types()): value = BinaryValue(value=value, bits=len(self), bigEndian=False) elif not isinstance(value, BinaryValue): self._log.critical("Unsupported type for value assignment: %s (%s)" % (type(value), repr(value))) raise TypeError("Unable to set simulator value with type %s" % (type(value))) simulator.set_signal_val_str(self._handle, value.binstr)
def setimmediatevalue(self, value): """ Set the value of the underlying simulation object to value. Args: value (ctypes.Structure, cocotb.binary.BinaryValue, int, double) The value to drive onto the simulator object Raises: TypeError This operation will fail unless the handle refers to a modifiable object eg net, signal or variable. We determine the library call to make based on the type of the value Assigning integers less than 32-bits is faster """ if isinstance(value, get_python_integer_types()) and value < 0x7fffffff: simulator.set_signal_val_long(self._handle, value) return if isinstance(value, ctypes.Structure): value = BinaryValue(value=cocotb.utils.pack(value), bits=len(self)) elif isinstance(value, get_python_integer_types()): value = BinaryValue(value=value, bits=len(self), bigEndian=False) elif isinstance(value, dict): #We're given a dictionary with a list of values and a bit size... num = 0; vallist = list(value["values"]) vallist.reverse() if len(vallist) * value["bits"] != len(self): self._log.critical("Unable to set with array length %d of %d bit entries = %d total, target is only %d bits long" % (len(value["values"]), value["bits"], len(value["values"]) * value["bits"], len(self))); raise TypeError("Unable to set with array length %d of %d bit entries = %d total, target is only %d bits long" % (len(value["values"]), value["bits"], len(value["values"]) * value["bits"], len(self))); for val in vallist: num = (num << value["bits"]) + val; value = BinaryValue(value=num, bits=len(self), bigEndian=False) elif not isinstance(value, BinaryValue): self._log.critical("Unsupported type for value assignment: %s (%s)" % (type(value), repr(value))) raise TypeError("Unable to set simulator value with type %s" % (type(value))) simulator.set_signal_val_str(self._handle, value.binstr)
def setimmediatevalue(self, value): """ Set the value of the underlying simulation object to value. Args: value (ctypes.Structure, cocotb.binary.BinaryValue, int, double) The value to drive onto the simulator object Raises: TypeError This operation will fail unless the handle refers to a modifiable object eg net, signal or variable. We determine the library call to make based on the type of the value Assigning integers less than 32-bits is faster """ if isinstance(value, get_python_integer_types()) and value < 0x7fffffff and len(self) <= 32: simulator.set_signal_val_long(self._handle, value) return if isinstance(value, ctypes.Structure): value = BinaryValue(value=cocotb.utils.pack(value), bits=len(self)) elif isinstance(value, get_python_integer_types()): value = BinaryValue(value=value, bits=len(self), bigEndian=False) elif isinstance(value, dict): #We're given a dictionary with a list of values and a bit size... num = 0; vallist = list(value["values"]) vallist.reverse() if len(vallist) * value["bits"] != len(self): self._log.critical("Unable to set with array length %d of %d bit entries = %d total, target is only %d bits long" % (len(value["values"]), value["bits"], len(value["values"]) * value["bits"], len(self))); raise TypeError("Unable to set with array length %d of %d bit entries = %d total, target is only %d bits long" % (len(value["values"]), value["bits"], len(value["values"]) * value["bits"], len(self))); for val in vallist: num = (num << value["bits"]) + val; value = BinaryValue(value=num, bits=len(self), bigEndian=False) elif not isinstance(value, BinaryValue): self._log.critical("Unsupported type for value assignment: %s (%s)" % (type(value), repr(value))) raise TypeError("Unable to set simulator value with type %s" % (type(value))) simulator.set_signal_val_str(self._handle, value.binstr)
def assign(self, value): """Decides how best to assign the value to the vector We possibly try to be a bit too clever here by first of all trying to assign the raw string as a binstring, however if the string contains any characters that aren't 0, 1, X or Z then we interpret the string as a binary buffer... """ if isinstance(value, get_python_integer_types()): self.value = value elif isinstance(value, str): try: self.binstr = value except ValueError: self.buff = value
def setimmediatevalue(self, value): """Set the value of the underlying simulation object to value. This operation will fail unless the handle refers to a modifiable object, e.g. net, signal or variable. Args: value (int): The value to drive onto the simulator object. Raises: TypeError: If target has an unsupported type for integer value assignment. """ if isinstance(value, BinaryValue): value = int(value) elif not isinstance(value, get_python_integer_types()): self._log.critical("Unsupported type for integer value assignment: %s (%s)" % (type(value), repr(value))) raise TypeError("Unable to set simulator value with type %s" % (type(value))) simulator.set_signal_val_long(self._handle, value)
def __setitem__(self, key, val): ''' BinaryValue uses verilog/vhdl style slices as opposed to python style''' if not isinstance(val, str) and not isinstance(val, get_python_integer_types()): raise TypeError('BinaryValue slices only accept string or integer values') # convert integer to string if isinstance(val, get_python_integer_types()): if isinstance(key, slice): num_slice_bits = abs(key.start - key.stop) + 1 else: num_slice_bits = 1 if val < 0: raise ValueError('Integer must be positive') if val >= 2**num_slice_bits: raise ValueError('Integer is too large for the specified slice ' 'length') val = "{:0{width}b}".format(val, width=num_slice_bits) if isinstance(key, slice): first, second = key.start, key.stop if self.big_endian: if first < 0 or second < 0: raise IndexError('BinaryValue does not support negative ' 'indices') if second > self._n_bits - 1: raise IndexError('High index greater than number of bits.') if first > second: raise IndexError('Big Endian indices must be specified ' 'low to high') if len(val) > (second + 1 - first): raise ValueError('String length must be equal to slice ' 'length') slice_1 = self.binstr[:first] slice_2 = self.binstr[second + 1:] self.binstr = slice_1 + val + slice_2 else: if first < 0 or second < 0: raise IndexError('BinaryValue does not support negative ' 'indices') if first > self._n_bits - 1: raise IndexError('High index greater than number of bits.') if second > first: raise IndexError('Litte Endian indices must be specified ' 'high to low') high = self._n_bits - second low = self._n_bits - 1 - first if len(val) > (high - low): raise ValueError('String length must be equal to slice ' 'length') slice_1 = self.binstr[:low] slice_2 = self.binstr[high:] self.binstr = slice_1 + val + slice_2 else: if len(val) != 1: raise ValueError('String length must be equal to slice ' 'length') index = key if index > self._n_bits - 1: raise IndexError('Index greater than number of bits.') if self.big_endian: self.binstr = self.binstr[:index] + val + self.binstr[index + 1:] else: self.binstr = self.binstr[0:self._n_bits-index-1] + val + self.binstr[self._n_bits-index:self._n_bits]
def __setitem__(self, key, val): """BinaryValue uses Verilog/VHDL style slices as opposed to Python style.""" if not isinstance(val, str) and not isinstance( val, get_python_integer_types()): raise TypeError( 'BinaryValue slices only accept string or integer values') # convert integer to string if isinstance(val, get_python_integer_types()): if isinstance(key, slice): num_slice_bits = abs(key.start - key.stop) + 1 else: num_slice_bits = 1 if val < 0: raise ValueError('Integer must be positive') if val >= 2**num_slice_bits: raise ValueError( 'Integer is too large for the specified slice ' 'length') val = "{:0{width}b}".format(val, width=num_slice_bits) if isinstance(key, slice): first, second = key.start, key.stop if self.big_endian: if first < 0 or second < 0: raise IndexError('BinaryValue does not support negative ' 'indices') if second > self._n_bits - 1: raise IndexError('High index greater than number of bits.') if first > second: raise IndexError('Big Endian indices must be specified ' 'low to high') if len(val) > (second + 1 - first): raise ValueError('String length must be equal to slice ' 'length') slice_1 = self.binstr[:first] slice_2 = self.binstr[second + 1:] self.binstr = slice_1 + val + slice_2 else: if first < 0 or second < 0: raise IndexError('BinaryValue does not support negative ' 'indices') if first > self._n_bits - 1: raise IndexError('High index greater than number of bits.') if second > first: raise IndexError('Litte Endian indices must be specified ' 'high to low') high = self._n_bits - second low = self._n_bits - 1 - first if len(val) > (high - low): raise ValueError('String length must be equal to slice ' 'length') slice_1 = self.binstr[:low] slice_2 = self.binstr[high:] self.binstr = slice_1 + val + slice_2 else: if len(val) != 1: raise ValueError('String length must be equal to slice ' 'length') index = key if index > self._n_bits - 1: raise IndexError('Index greater than number of bits.') if self.big_endian: self.binstr = self.binstr[:index] + val + self.binstr[index + 1:] else: self.binstr = self.binstr[0:self._n_bits - index - 1] + val + self.binstr[self._n_bits - index:self. _n_bits]