def convertBits__val(self: Bits, val: "BitVal", toType: HdlType): if toType == BOOL: return val != self.getValueCls().from_py(self, 0) elif isinstance(toType, Bits): if self.signed != toType.signed: if self.strict_sign and bool(self.signed) != bool(toType.signed): raise TypeConversionErr(self, toType) val = val._convSign__val(toType.signed) w_from, w_to = self.bit_length(), toType.bit_length() if w_from != w_to: if self.strict_width: raise TypeConversionErr(self, toType) if w_from > w_to: # cut off some bits from value new_m = val.vld_mask & toType.all_mask() else: # w_from < w_to, extend the value to some bit length extra_mask_bits = mask(w_to - w_from) new_m = set_bit_range(val.vld_mask, w_from, w_to - w_from, extra_mask_bits) val = toType.from_py(val.val, new_m) if val._dtype != toType: # sign and width checked, only name, strict_* flags can be different val = toType.from_py(val.val, val.vld_mask) return val elif toType == INT: return INT.getValueCls()(INT, val.val, int(val._is_full_valid())) return default_auto_cast_fn(self, val, toType)
def __call__(self, source) -> Assignment: """ Create assignment to this signal :attention: it is not call of function it is operator of assignment :return: list of assignments """ if isinstance(source, InterfaceBase): assert source._isAccessible source = source._sig if source is None: source = self._dtype.from_py(None) else: source = toHVal(source, suggestedType=self._dtype) err = False try: source = source._auto_cast(self._dtype) except TypeConversionErr: err = True if err: raise TypeConversionErr( ("Can not connect %r (of type %r) to %r " "(of type %r) due type incompatibility") % (source, source._dtype, self, self._dtype)) tmp = self._getIndexCascade() if tmp: mainSig, indexCascade = tmp self = mainSig else: indexCascade = None # self = self._tryMyIndexToEndpoint() return Assignment(source, self, indexCascade)
def convertBits__val(self: Bits, val: "BitVal", toType: HdlType): if toType == BOOL: return val != self.getValueCls().from_py(self, 0) elif isinstance(toType, Bits): if self.signed != toType.signed: if self.strict_sign and bool(self.signed) != bool(toType.signed): raise TypeConversionErr(self, toType) val = val._convSign__val(toType.signed) if self.bit_length() != toType.bit_length(): if self.strict_width: raise TypeConversionErr(self, toType) val = toType.from_py(val.val, val.vld_mask & toType.all_mask()) if val._dtype != toType: # sign and width checked, only name, strict_* flags can be different val = toType.from_py(val.val, val.vld_mask) return val elif toType == INT: return INT.getValueCls()(INT, val.val, int(val._is_full_valid())) return default_auto_cast_fn(self, val, toType)
def __call__( self, source, dst_resolve_fn=lambda x: x._getDestinationSignalForAssignmentToThis() ) -> Assignment: """ Create assignment to this signal :attention: it is not call of function it is operator of assignment :return: list of assignments """ assert not self._const, self if isinstance(source, InterfaceBase): assert source._isAccessible, ( source, "must be a Signal Interface which is accessible in current scope" ) source = source._sig try: requires_type_check = False if source is None: source = self._dtype.from_py(None) else: requires_type_check = True source = toHVal(source, suggestedType=self._dtype) except Exception as e: # simplification of previous exception traceback e_simplified = copy(e) raise e_simplified if requires_type_check: err = False try: source = source._auto_cast(self._dtype) except TypeConversionErr: err = True if err: raise TypeConversionErr( ("Can not connect %r (of type %r) to %r " "(of type %r) due type incompatibility") % (source, source._dtype, self, self._dtype)) try: mainSig, indexCascade = self._getIndexCascade() mainSig = dst_resolve_fn(mainSig) return Assignment(source, mainSig, indexCascade) except Exception as e: # simplification of previous exception traceback e_simplified = copy(e) raise e_simplified
def reinterptet_harray_to_bits(typeFrom, sigOrVal, bitsT): """ Cast HArray signal or value to signal or value of type Bits """ size = int(typeFrom.size) widthOfElm = typeFrom.element_t.bit_length() w = bitsT.bit_length() if size * widthOfElm != w: raise TypeConversionErr("Size of types is different", size * widthOfElm, w) partT = Bits(widthOfElm) parts = [p._reinterpret_cast(partT) for p in sigOrVal] return Concat(*reversed(parts))._reinterpret_cast(bitsT)
def __pow__(self, source): """ Create assignment to this signal :attention: it is not power operator it is assignment :return: list of assignments """ if isinstance(source, InterfaceBase): assert source._isAccessible source = source._sig if source is None: source = self._dtype.fromPy(None) else: source = toHVal(source) err = False try: source = source._dtype.convert(source, self._dtype) except TypeConversionErr: err = True if err: raise TypeConversionErr( "Can not connect %r (of type %r) to %r (of type %r) due type incompatibility" % (source, source._dtype, self, self._dtype)) tmp = self._getIndexCascade() if tmp: mainSig, indexCascade = tmp self = mainSig else: indexCascade = None # self = self._tryMyIndexToEndpoint() a = Assignment(source, self, indexCascade) self.drivers.append(a) if not isinstance(source, Value): source.endpoints.append(a) return [a]
def reinterpret_harray_to_harray(typeFrom, sigOrVal, arrayT): mySize = int(typeFrom.size) myWidthOfElm = typeFrom.element_t.bit_length() size = int(arrayT.size) widthOfElm = arrayT.element_t.bit_length() if size * widthOfElm != mySize * myWidthOfElm: raise TypeConversionErr("Size of types is different", size * widthOfElm, mySize * myWidthOfElm) if isinstance(typeFrom.element_t, Bits): reinterpretElmToType = None else: reinterpretElmToType = Bits(myWidthOfElm) res = arrayT.from_py(None) for i in range(size): start = i * widthOfElm end = (i + 1) * widthOfElm item = getBits_from_array(sigOrVal, myWidthOfElm, start, end, reinterpretElmToType) res[i] = item._reinterpret_cast(arrayT.element_t) return res
def default_auto_cast_fn(typeFrom, sigOrVal, toType): raise TypeConversionErr( "Conversion of %r of type \n%r to type %r is not implemented", (sigOrVal, typeFrom, toType))
def default_reinterpret_cast_fn(typeFrom, sigOrVal, toType): raise TypeConversionErr( "Reinterpretation of %r of type \n%r to type %r is not implemented", (sigOrVal, typeFrom, toType))
def __call__(self, source, dst_resolve_fn=lambda x: x. _getDestinationSignalForAssignmentToThis(), exclude=None, fit=False) -> Assignment: """ Create assignment to this signal :attention: it is not call of function it is operator of assignment :return: list of assignments """ assert not self._const, self if exclude is not None and (self in exclude or source in exclude): return [] if self.hidden: try: d = self.singleDriver() except: d = None operator = getattr(d, "operator", None) if operator is not None: assert operator.allowsAssignTo, ( "Assignment to", self, "is not allowed by operator definition") if isinstance(source, InterfaceBase): assert source._isAccessible, ( source, "must be a Signal Interface which is accessible in current scope" ) source = source._sig try: if source is None: requires_type_check = False source = self._dtype.from_py(None) else: requires_type_check = True source = toHVal(source, suggestedType=self._dtype) except Exception as e: # simplification of previous exception traceback e_simplified = copy(e) raise e_simplified if requires_type_check: err = False try: if fit: source = fitTo_t(source, self._dtype) source = source._auto_cast(self._dtype) except TypeConversionErr: err = True if err: raise TypeConversionErr( ("Can not connect %r (of type %r) to %r " "(of type %r) due type incompatibility") % (source, source._dtype, self, self._dtype)) try: mainSig, indexCascade = self._getIndexCascade() mainSig = dst_resolve_fn(mainSig) return Assignment(source, mainSig, indexCascade) except Exception as e: # simplification of previous exception traceback e_simplified = copy(e) raise e_simplified
def defaultConvert(self, sigOrVal, toType): raise TypeConversionErr("Conversion of %r of type \n%r to type %r is not implemented" % (sigOrVal, self, toType))