示例#1
0
    def Operator(cls, op, ctx):
        ops = op.operands
        o = op.operator

        op_str = cls._unaryOps.get(o, None)
        if op_str is not None:
            return op_str % (cls._operand(ops[0], o, ctx))

        op_str = cls._binOps.get(o, None)
        if op_str is not None:
            return op_str % (cls._operand(ops[0], o,
                                          ctx), cls._operand(ops[1], o, ctx))

        if o == AllOps.INDEX:
            assert len(ops) == 2
            o0, o1 = ops
            o0_str = cls.asHdl(o0, ctx)
            if ops[1]._dtype == SLICE:
                return "%s.range(%s, %s)" % (o0_str,
                                             cls._operand(o1.val[0], o, ctx),
                                             cls._operand(o1.val[1], o, ctx))
            else:
                return "%s[%s]" % (o0_str, cls._operand(o1, o, ctx))

        elif o == AllOps.TERNARY:
            zero, one = BIT.fromPy(0), BIT.fromPy(1)
            if ops[1] == one and ops[2] == zero:
                # ignore redundant x ? 1 : 0
                return cls.condAsHdl([ops[0]], True, ctx)
            else:
                return "%s ? %s : %s" % (cls.condAsHdl(
                    [ops[0]], True, ctx), cls._operand(
                        ops[1], o, ctx), cls._operand(ops[2], o, ctx))
        elif o == AllOps.RISING_EDGE or o == AllOps.FALLING_EDGE:
            if ctx.isSensitivityList:
                if o == AllOps.RISING_EDGE:
                    _o = ".pos()"
                else:
                    _o = ".neg()"
                return cls._operand(ops[0], o, ctx) + _o
            else:
                raise UnsupportedEventOpErr()
        elif o in [
                AllOps.BitsAsSigned, AllOps.BitsAsUnsigned, AllOps.BitsAsVec,
                AllOps.IntToBits
        ]:
            assert len(ops) == 1
            return "static_cast<%s>(%s)" % (cls.HdlType(
                op.result._dtype, ctx), cls._operand(ops[0], o, ctx))
        elif o == AllOps.POW:
            assert len(ops) == 2
            raise NotImplementedError()
            # return _bin('**')
        elif o == AllOps.CALL:
            return "%s(%s)" % (cls.FunctionContainer(ops[0]), ", ".join(
                map(lambda op: cls._operand(op, o, ctx), ops[1:])))
        else:
            raise NotImplementedError("Do not know how to convert %s to vhdl" %
                                      (o))
示例#2
0
def cast_hbool(self, sigOrVal, toType):
    if toType == BIT:
        if isinstance(sigOrVal, Value):
            v = BIT.getValueCls()(int(sigOrVal.val), BIT, sigOrVal.vldMask,
                                  sigOrVal.updateTime)
            return v
        else:
            return sigOrVal._ternary(BIT.fromPy(1), BIT.fromPy(0))

    return default_auto_cast_fn(self, sigOrVal, toType)
示例#3
0
    def Operator(cls, op, ctx):
        ops = op.operands
        o = op.operator

        op_str = cls._unaryOps.get(o, None)
        if op_str is not None:
            return op_str % (cls._operand(ops[0], o, ctx))

        op_str = cls._binOps.get(o, None)
        if op_str is not None:
            return op_str % (cls._operand(ops[0], o,
                                          ctx), cls._operand(ops[1], o, ctx))

        if o == AllOps.CALL:
            return "%s(%s)" % (cls.FunctionContainer(ops[0]), ", ".join(
                map(lambda op: cls._operand(op, o, ctx), ops[1:])))
        elif o == AllOps.INDEX:
            assert len(ops) == 2
            o1 = ops[0]
            return "%s[%s]" % (cls.asHdl(
                o1, ctx).strip(), cls._operand(ops[1], o, ctx))
        elif o == AllOps.TERNARY:
            zero, one = BIT.fromPy(0), BIT.fromPy(1)
            if ops[1] == one and ops[2] == zero:
                # ignore redundant x ? 1 : 0
                return cls.condAsHdl([ops[0]], True, ctx)
            else:
                return "%s ? %s : %s" % (cls.condAsHdl(
                    [ops[0]], True, ctx), cls._operand(
                        ops[1], o, ctx), cls._operand(ops[2], o, ctx))
        elif o == AllOps.RISING_EDGE or o == AllOps.FALLING_EDGE:
            raise UnsupportedEventOpErr()
        elif o in [AllOps.BitsAsUnsigned, AllOps.BitsAsVec]:
            op, = ops
            op_str = cls._operand(op, o, ctx)
            if bool(op._dtype.signed):
                return "$unsigned(%s)" % op_str
            else:
                return op_str
        else:
            raise NotImplementedError("Do not know how to convert %s to vhdl" %
                                      (o))
示例#4
0
def hBit(pyVal):
    """create hdl bit value (for example STD_LOGIC value in vhdl)"""
    return BIT.fromPy(pyVal)