def _concat(self, other): """ Concatenate this with other to one wider value/signal """ w = self._dtype.bit_length() try: other._dtype.bit_length except AttributeError: raise TypeError("Can not concat Bits and", other._dtype) if areValues(self, other): return self._concat__val(other) else: w = self._dtype.bit_length() other_w = other._dtype.bit_length() resWidth = w + other_w Bits = self._dtype.__class__ resT = Bits(resWidth, signed=self._dtype.signed) # is instance of signal if isinstance(other, InterfaceBase): other = other._sig if isinstance(other._dtype, Bits): if other._dtype.signed is not None: other = other._vec() elif other._dtype == BOOL: other = other._auto_cast(BIT) else: raise TypeError(other._dtype) if self._dtype.signed is not None: self = self._vec() return Operator.withRes(AllOps.CONCAT, [self, other], resT)\ ._auto_cast(Bits(resWidth, signed=self._dtype.signed))
def __ne__(self, other): assert self._dtype is other._dtype if areValues(self, other): return self._ne__val(other) else: return Operator.withRes(AllOps.NEQ, [self, other], BOOL)
def __mul__(self, other): Bits = self._dtype.__class__ other = toHVal(other) if not isinstance(other._dtype, Bits): raise TypeError(other) if areValues(self, other): return self._mul__val(other) else: myT = self._dtype if self._dtype.signed is None: self = self._unsigned() if isinstance(other._dtype, Bits): s = other._dtype.signed if s is None: other = other._unsigned() else: raise TypeError("%r %r %r" % (self, AllOps.MUL, other)) if other._dtype == INT: res_w = myT.bit_length() * 2 res_sign = self._dtype.signed else: res_w = myT.bit_length() + other._dtype.bit_length() res_sign = self._dtype.signed or other._dtype.signed subResT = Bits(res_w, signed=res_sign) o = Operator.withRes(AllOps.MUL, [self, other], subResT) resT = Bits(res_w, signed=myT.signed) return o._auto_cast(resT)
def bitsArithOp(self, other, op): other = toHVal(other, self._dtype) assert isinstance(other._dtype, Bits), other._dtype if areValues(self, other): return bitsArithOp__val(self, other, op._evalFn) else: if self._dtype.signed is None: self = self._unsigned() resT = self._dtype if isinstance(other._dtype, Bits): t0 = self._dtype t1 = other._dtype if t0.bit_length() != t1.bit_length(): if not t1.strict_width: # resize to type of this other = other._auto_cast(t1) t1 = other._dtype pass elif not t0.strict_width: # resize self to type of result self = self._auto_cast(t0) t0 = self._dtype pass else: raise TypeError("%r %r %r" % (self, op, other)) if t1.signed != resT.signed: other = other._convSign(t0.signed) else: raise TypeError("%r %r %r" % (self, op, other)) o = Operator.withRes(op, [self, other], self._dtype) return o._auto_cast(resT)
def intOp(self, other, op, resT, evalFn=None): if evalFn is None: evalFn = op._evalFn other = toHVal(other)._auto_cast(INT) if areValues(self, other): return intOp__val(self, other, resT, evalFn) else: return Operator.withRes(op, [self, other], resT)
def boolCmpOp(self, other, op, evalFn=None): other = toHVal(other) if evalFn is None: evalFn = op._evalFn if areValues(self, other): return boolCmpOp__val(self, other, op, evalFn) else: return Operator.withRes(op, [self, other._auto_cast(BOOL)], BOOL)
def __eq__(self, other): if areValues(self, other): if self._dtype == other._dtype: for f in self._dtype.fields: isPadding = f.name is None if not isPadding: sf = getattr(self, f.name) of = getattr(other, f.name) if not (sf == of): return False return True else: return False else: return super(Value, self).__eq__(other)
def bitsArithOp(self, other, op): other = toHVal(other) assert isinstance(other._dtype, (Integer, Bits)) if areValues(self, other): return bitsArithOp__val(self, other, op) else: resT = self._dtype if self._dtype.signed is None: self = self._unsigned() if isinstance(other._dtype, Bits): assert other._dtype.bit_length() == resT.bit_length( ), (op, other._dtype.bit_length(), resT.bit_length()) other = other._convSign(self._dtype.signed) elif isinstance(other._dtype, Integer): pass else: raise TypeError("%r %r %r" % (self, op, other)) o = Operator.withRes(op, [self, other], self._dtype) return o._auto_cast(resT)
def _downto(self, other): other = toHVal(other)._auto_cast(INT) if areValues(self, other): return self._downto__val(other) else: return Operator.withRes(AllOps.DOWNTO, [self, other], SLICE)