def type_check_bool_param(self, name): STR_BOOL = ("TRUE", "FALSE") v = getattr(self, name) if self._store_manager.serializer_cls == VerilogSerializer: # for verilog we need to convert it to "TRUE" or "FALSE" string if not isinstance(v, STR.getValueCls()): if isinstance(v, (bool, BIT.getValueCls())): v = "TRUE" if v else "FALSE" else: assert v in STR_BOOL, (name, v) v = STR.from_py(v) object.__setattr__(self, name, v) else: assert v._dtype == STR, (name, "must be of type ", STR, " or compatible, is:", v) else: if not isinstance(v, BOOL.getValueCls()): if isinstance(v, (str, STR.getValueCls())): v = str(v) if v == "FALSE": v = False elif v == "TRUE": v = True else: raise AssertionError( name, "must be of type ", BOOL, " or string \"TRUE\" or \"FALSE\" or compatible, is:", v) v = BOOL.from_py(v) object.__setattr__(self, name, v) else: assert v._dtype == BOOL, (name, "must be of type ", BOOL, " or compatible, is:", v)
def bitsCmp_detect_useless_cmp(op0, op1, op): v = int(op1) width = op1._dtype.bit_length() if op0._dtype.signed: min_val = -1 if width == 1 else mask(width - 1) - 1 max_val = 0 if width == 1 else mask(width - 1) else: min_val = 0 max_val = mask(width) if v == min_val: # value can not be lower than min_val if op == AllOps.GE: # -> always True return BOOL.from_py(1, 1) elif op == AllOps.LT: # -> always False return BOOL.from_py(0, 1) elif op == AllOps.LE: # convert <= to == to highlight the real function return AllOps.EQ elif v == max_val: # value can not be greater than max_val if op == AllOps.GT: # always False return BOOL.from_py(0, 1) elif op == AllOps.LE: # always True return BOOL.from_py(1, 1) elif op == AllOps.GE: # because value can not be greater than max return AllOps.EQ
def _eq__val(self, other): assert self._dtype.element_t == other._dtype.element_t assert self._dtype.size == other._dtype.size eq = True vld = 1 keysA = set(self.val) keysB = set(other.val) sharedKeys = keysA.union(keysB) lsh = len(sharedKeys) if (lsh == int(self._dtype.size) and len(keysA) == lsh and len(keysB) == lsh): for k in sharedKeys: a = self.val[k] b = other.val[k] eq = eq and bool(a) == bool(b) if not eq: break vld = vld & a.vld_mask & b.vld_mask else: eq = False vld = 0 return BOOL.getValueCls()(BOOL, int(eq), vld)
def _eq__val(self, other): assert self._dtype.elmType == other._dtype.elmType assert self._dtype.size == other._dtype.size eq = True vld = 1 updateTime = -1 keysA = set(self.val) keysB = set(other.val) sharedKeys = keysA.union(keysB) lsh = len(sharedKeys) if (lsh == int(self._dtype.size) and len(keysA) == lsh and len(keysB) == lsh): for k in sharedKeys: a = self.val[k] b = other.val[k] eq = eq and a == b if not eq: break vld = vld & a.vldMask & b.vldMask updateTime = max(updateTime, a.updateTime, b.updateTime) else: eq = False vld = 0 return BOOL.getValueCls()(eq, BOOL, vld, updateTime)
def _eq__val(self, other): assert self._dtype.element_t == other._dtype.element_t eq = True vld = 1 if (len(self.val) == len(other.val)): for a, b in zip(self.val, other.val): eq = eq and bool(a) == bool(b) if not eq: break vld = vld & a.vld_mask & b.vld_mask else: eq = False vld = 0 return BOOL.getValueCls()(BOOL, int(eq), vld)
def hBool(pyVal): """ create hdl bool value (for example bool value in vhdl)""" return BOOL.fromPy(pyVal)
from hwt.hdl.value import Value, areValues from hwt.hdl.types.defs import BOOL from hwt.hdl.operator import Operator from hwt.hdl.operatorDefs import AllOps from hwt.doc_markers import internal BoolVal = BOOL.getValueCls() class HEnumVal(Value): @classmethod def fromPy(cls, val, typeObj, vldMask=None): """ :param val: value of python type bool or None :param typeObj: instance of HEnum :param vldMask: if is None validity is resolved from val if is 0 value is invalidated if is 1 value has to be valid """ if val is None: assert vldMask is None or vldMask == 0 valid = False val = typeObj._allValues[0] else: if vldMask is None or vldMask == 1: assert isinstance(val, str) valid = True else: valid = False val = None
def _eq__val(self, other): eq = self.val == other.val vld = int(self.vldMask and other.vldMask) updateTime = max(self.updateTime, other.updateTime) return BOOL.getValueCls()(eq, BOOL, vld, updateTime)
def _eq__val(self, other): eq = self.val == other.val vld = int(self.vld_mask and other.vld_mask) return BOOL.getValueCls()(BOOL, int(eq), vld)
def hBool(v): return BOOL.from_py(v)