hwt/hdl/types/bitsVal.py
def fromPy(cls, val, typeObj, vldMask=None):
        """
        Construct value from pythonic value (int, bytes, enum.Enum member)
        """
        assert not isinstance(val, Value)
        if val is None:
            vld = 0
            val = 0
            assert vldMask is None or vldMask == 0
        else:
            allMask = typeObj.all_mask()
            w = typeObj.bit_length()
            if isinstance(val, bytes):
                val = int.from_bytes(
                    val, byteorder="little", signed=bool(typeObj.signed))
            else:
                try:
                    val = int(val)
                except TypeError as e:
                    if isinstance(val, enum.Enum):
                        val = int(val.value)
                    else:
                        raise e

            if vldMask is None:
                vld = allMask
            else:
                assert vldMask <= allMask and vldMask >= 0
                vld = vldMask

            if val < 0:
                assert typeObj.signed
                assert signFix(val & allMask, w) == val, (
                    val, signFix(val & allMask, w))
                val = signFix(val & vld, w)
            else:
                if typeObj.signed:
                    msb = 1 << (w - 1)
                    if msb & val:
                        assert val < 0, val

                if val & allMask != val:
                    raise ValueError(
                        "Not enought bits to represent value",
                        val, val & allMask)
                val = val & vld

        return cls(val, typeObj, vld)
def fromPy(cls, val, typeObj, vldMask=None):
        """
        Construct value from pythonic value (int, bytes, enum.Enum member)
        """
        assert not isinstance(val, Value)
        if val is None:
            vld = 0
            val = 0
            assert vldMask is None or vldMask == 0
        else:
            allMask = typeObj.all_mask()
            w = typeObj.bit_length()
            if isinstance(val, bytes):
                val = int.from_bytes(
                    val, byteorder="little", signed=bool(typeObj.signed))
            else:
                try:
                    val = int(val)
                except TypeError as e:
                    if isinstance(val, enum.Enum):
                        val = int(val.value)
                    else:
                        raise e

            if vldMask is None:
                vld = allMask
            else:
                assert vldMask <= allMask and vldMask >= 0
                vld = vldMask

            if val < 0:
                assert typeObj.signed
                assert signFix(val & allMask, w) == val, (
                    val, signFix(val & allMask, w))
                val = signFix(val & vld, w)
            else:
                if typeObj.signed:
                    msb = 1 << (w - 1)
                    if msb & val:
                        assert val < 0, val

                if val & allMask != val:
                    raise ValueError(
                        "Not enought bits to represent value",
                        val, val & allMask)
                val = val & vld

        return cls(val, typeObj, vld)
Construct value from pythonic value (int, bytes, enum.Enum member)