How to use the cocotb.binary.BinaryValue function in cocotb

To help you get started, we’ve selected a few cocotb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cocotb / cocotb / tests / pytest / test_binary_value.py View on Github external
def test_backwards_compatibility():
    """
    Test backwards-compatibility wrappers for BinaryValue
    """

    # bits is deprecated in favor of n_bits
    with pytest.deprecated_call():
        vec = BinaryValue(value=0, bits=16)
    assert vec.n_bits == 16

    vec = BinaryValue(0, 16)
    assert vec.n_bits == 16

    with pytest.raises(TypeError):
        BinaryValue(value=0, bits=16, n_bits=17)
github cocotb / cocotb / tests / pytest / test_binary_value.py View on Github external
def test_init_unsigned_negative_value():
    with pytest.raises(ValueError):
        BinaryValue(value=-8, n_bits=5, bigEndian=True, binaryRepresentation=BinaryRepresentation.UNSIGNED)
        pytest.fail("Expected ValueError when assigning negative number to unsigned BinaryValue")
github cocotb / cocotb / tests / pytest / test_binary_value.py View on Github external
def test_init_little_endian_signed():
    bin1 = BinaryValue(value=3, n_bits=3, bigEndian=False, binaryRepresentation=BinaryRepresentation.SIGNED_MAGNITUDE)
    assert bin1._str == "011"
    assert bin1.binstr == "011"
    assert bin1.integer == 3

    bin2 = BinaryValue(value=-1, n_bits=2, bigEndian=False, binaryRepresentation=BinaryRepresentation.SIGNED_MAGNITUDE)
    assert bin2._str == "11"
    assert bin2.binstr == "11"
    assert bin2.integer == -1

    bin3 = BinaryValue(value="1001011", bigEndian=False, binaryRepresentation=BinaryRepresentation.SIGNED_MAGNITUDE)
    assert bin3._str == "1001011"
    assert bin3.binstr == "1001011"
    assert bin3.integer == -11
github cocotb / cocotb / tests / pytest / test_binary_value.py View on Github external
def test_init_little_endian_unsigned():
    bin1 = BinaryValue(value=3, n_bits=3, bigEndian=False, binaryRepresentation=BinaryRepresentation.UNSIGNED)
    assert bin1._str == "011"
    assert bin1.binstr == "011"
    assert bin1.integer == 3

    bin2 = BinaryValue(value=5, n_bits=5, bigEndian=False, binaryRepresentation=BinaryRepresentation.UNSIGNED)
    assert bin2._str == "00101"
    assert bin2.binstr == "00101"
    assert bin2.integer == 5

    bin3 = BinaryValue(value=12, n_bits=8, bigEndian=False, binaryRepresentation=BinaryRepresentation.UNSIGNED)
    assert bin3._str == "00001100"
    assert bin3.binstr == "00001100"
    assert bin3.integer == 12

    bin4 = BinaryValue(value="010110", bigEndian=False, binaryRepresentation=BinaryRepresentation.UNSIGNED)
    assert bin4._str == "010110"
    assert bin4.binstr == "010110"
    assert bin4.integer == 22

    bin5 = BinaryValue(value="1001011", bigEndian=False, binaryRepresentation=BinaryRepresentation.UNSIGNED)
    assert bin5._str == "1001011"
github cocotb / cocotb / tests / pytest / test_binary_value.py View on Github external
def test_init_little_endian_signed():
    bin1 = BinaryValue(value=3, n_bits=3, bigEndian=False, binaryRepresentation=BinaryRepresentation.SIGNED_MAGNITUDE)
    assert bin1._str == "011"
    assert bin1.binstr == "011"
    assert bin1.integer == 3

    bin2 = BinaryValue(value=-1, n_bits=2, bigEndian=False, binaryRepresentation=BinaryRepresentation.SIGNED_MAGNITUDE)
    assert bin2._str == "11"
    assert bin2.binstr == "11"
    assert bin2.integer == -1

    bin3 = BinaryValue(value="1001011", bigEndian=False, binaryRepresentation=BinaryRepresentation.SIGNED_MAGNITUDE)
    assert bin3._str == "1001011"
    assert bin3.binstr == "1001011"
    assert bin3.integer == -11
github im-tomu / valentyusb / sim / test-eptri.py View on Github external
def __init__(self, dut):
        self.dut = dut
        self.csrs = dict()
        with open("csr.csv", newline='') as csr_csv_file:
            csr_csv = csv.reader(csr_csv_file)
            # csr_register format: csr_register, name, address, size, rw/ro
            for row in csr_csv:
                if row[0] == 'csr_register':
                    self.csrs[row[1]] = int(row[2], base=0)
        cocotb.fork(Clock(dut.clk48, 20800, 'ps').start())
        self.wb = WishboneMaster(dut, "wishbone", dut.clk12, timeout=20)

        # Set the signal "test_name" to match this test
        import inspect
        tn = cocotb.binary.BinaryValue(value=None, n_bits=4096)
        tn.buff = inspect.stack()[1][3]
        self.dut.test_name = tn
github cocotb / cocotb / tests / pytest / test_binary_value.py View on Github external
def test_init_not_enough_bits():
    with pytest.warns(RuntimeWarning, match=TRUNCATION_MATCH):
        bin1_unsigned = BinaryValue(value=128, n_bits=7, bigEndian=True,
                                    binaryRepresentation=BinaryRepresentation.UNSIGNED)
    assert bin1_unsigned._str == "0000000"
    assert bin1_unsigned.binstr == "0000000"
    assert bin1_unsigned.integer == 0

    with pytest.warns(RuntimeWarning, match=TRUNCATION_MATCH):
        bin1_sigmag = BinaryValue(value=128, n_bits=7, bigEndian=True,
                                  binaryRepresentation=BinaryRepresentation.SIGNED_MAGNITUDE)
    assert bin1_sigmag._str == "0000000"
    assert bin1_sigmag.binstr == "0000000"
    assert bin1_sigmag.integer == 0

    with pytest.warns(RuntimeWarning, match=TRUNCATION_MATCH):
        bin1_twoscomp = BinaryValue(value=128, n_bits=7, bigEndian=True,
                                    binaryRepresentation=BinaryRepresentation.TWOS_COMPLEMENT)
    assert bin1_twoscomp._str == "0000000"
github im-tomu / valentyusb / sim / test-dummyusb.py View on Github external
def __init__(self, dut):
        self.dut = dut
        self.csrs = dict()
        with open("csr.csv", newline='') as csr_csv_file:
            csr_csv = csv.reader(csr_csv_file)
            # csr_register format: csr_register, name, address, size, rw/ro
            for row in csr_csv:
                if row[0] == 'csr_register':
                    self.csrs[row[1]] = int(row[2], base=0)
        cocotb.fork(Clock(dut.clk48, 20800, 'ps').start())
        self.wb = WishboneMaster(dut, "wishbone", dut.clk12, timeout=20)

        # Set the signal "test_name" to match this test
        import inspect
        tn = cocotb.binary.BinaryValue(value=None, n_bits=4096)
        tn.buff = inspect.stack()[1][3]
        self.dut.test_name = tn
github cocotb / cocotb / cocotb / handle.py View on Github external
handle (int): The GPI handle to the simulator object.
            path (str): Path to this handle, ``None`` if root.
            handle_type: The type of the handle
                (``simulator.INTEGER``, ``simulator.ENUM``,
                ``simulator.REAL``, ``simulator.STRING``).
        """
        NonHierarchyObject.__init__(self, handle, path)
        if handle_type in [simulator.INTEGER, simulator.ENUM]:
            self._value = self._handle.get_signal_val_long()
        elif handle_type == simulator.REAL:
            self._value = self._handle.get_signal_val_real()
        elif handle_type == simulator.STRING:
            self._value = self._handle.get_signal_val_str()
        else:
            val = self._handle.get_signal_val_binstr()
            self._value = BinaryValue(n_bits=len(val))
            try:
                self._value.binstr = val
            except Exception:
                self._value = val
github cocotb / cocotb / cocotb / handle.py View on Github external
if isinstance(value, ctypes.Structure):
            value = BinaryValue(value=cocotb.utils.pack(value), n_bits=len(self))
        elif isinstance(value, int):
            value = BinaryValue(value=value, n_bits=len(self), bigEndian=False)
        elif isinstance(value, dict):
            # We're given a dictionary with a list of values and a bit size...
            num = 0
            vallist = list(value["values"])
            vallist.reverse()
            if len(vallist) * value["bits"] != len(self):
                raise TypeError("Unable to set with array length %d of %d bit entries = %d total, target is only %d bits long" %
                                (len(value["values"]), value["bits"], len(value["values"]) * value["bits"], len(self)))

            for val in vallist:
                num = (num << value["bits"]) + val
            value = BinaryValue(value=num, n_bits=len(self), bigEndian=False)

        elif not isinstance(value, BinaryValue):
            raise TypeError(
                "Unsupported type for value assignment: {} ({!r})"
                .format(type(value), value))

        call_sim(self, self._handle.set_signal_val_binstr, set_action, value.binstr)