def adder_basic(dut, a, b):
    """
    Basic adder co-routine. Simulates an addition of two integer numbers from [0, 2**DATA_WIDTH) and compares the result to
    the software model.
    :param dut: device under test: testbench.sv
    :param a: first summand
    :param b: second summand
    :return: None
    """
    # init clockgen
    cocotb.fork(clock_gen(dut.clk_i, period=clock_period))
    # apply the reset signal
    dut.rst_i <= 1
    yield RisingEdge(dut.clk_i)
    # remove reset signal
    dut.rst_i <= 0
    yield RisingEdge(dut.clk_i)
    # apply the input signal
    dut.a_i <= a
    dut.b_i <= b
    yield RisingEdge(dut.clk_i)
    yield RisingEdge(dut.clk_i)
    # get result
    result = int(dut.sum_o.value)
    # check against software model
    if result != adder_model(a, b):
        raise TestFailure("Mismatch detected: got {}, exp {}!".format(
            result, adder_model(a, b)))
    else:
        dut._log.info("Got value: {}".format(result))
Esempio n. 2
0
def basic_test(dut):
    """
    Randmized test for o_sum = i_a + i_b (signed addition/subtraction)
    """

    # start the clock
    cocotb.fork(Clock(dut.i_clk, 10, units="ns").start())
    clkedge = RisingEdge(dut.i_clk)

    yield clkedge  # synchronize ourselves with the clock

    # start the simulation
    for _ in range(10):

        # randomize the input data
        A = random.randint(-10, 15)
        B = random.randint(-10, 15)
        dut.i_a <= A
        dut.i_b <= B

        # wait for posedge and let the output be resolved
        yield clkedge
        yield ReadWrite(
        )  # there seems some "updating" issue with cocotb here! ... without this statement, this test won't pass

        # format A if it is a negative number
        if A < 0:
            i_A = bin2sign(dut.i_a)
        else:
            i_A = int(dut.i_a)

        # format B if it is a negative number
        if B < 0:
            i_B = bin2sign(dut.i_b)
        else:
            i_B = int(dut.i_b)

        # format the output if it is a negative number
        if adder_model(A, B) < 0:
            o_SUM = bin2sign(dut.o_sum)
        else:
            o_SUM = int(dut.o_sum)

        # compare with the reference model
        if o_SUM != adder_model(A, B):
            raise TestFailure("Randomised test failed with: %s + %s = %s" %
                              (i_A, i_B, o_SUM))
        else:
            dut._log.info("Randomised test passed with: %s + %s = %s" %
                          (i_A, i_B, o_SUM))
Esempio n. 3
0
def adder_basic_test(dut):
    """Test for 5 + 10"""
    #uri = raw_input("What is the Pyro uri of the greeting object? ").strip()

    par_serv = Pyro4.Proxy("PYRONAME:servidor.de.variables"
                           )  # get a Pyro proxy to the greeting object

    if (par_serv.exit_get == True):
        par_serv.exit_set(False)
        time.sleep(1)

    #dut.DATA_WIDTH <= par_serv.depth_get  #  :'(

    yield Timer(2)
    A = 2
    B = 3
    A_old = A
    B_old = B
    #bucle=True
    cocotb.log.info("\n\n - Simulation Loop Started - \n")
    while par_serv.exit_get == False:
        A, B = par_serv.sumandos_get
        if (A != A_old) or (B != B_old):
            A_old = A
            B_old = B
            dut.A = A
            dut.B = B
            yield Timer(20)
            par_serv.X_set(int(dut.X))
            if int(dut.X) != (A + B):
                raise TestFailure("[-] Adder result is incorrect: %s != %s" %
                                  (str(dut.X), adder_model(A, B)))
            else:  # these last two lines are not strictly necessary
                dut._log.info("[+] Ok!, %s + %s = %s !" %
                              (int(dut.A), int(dut.B), int(dut.X)))
        time.sleep(0.1)

        #if par_serv.exit_valor() == True:
        #bucle = False

    if int(dut.X) != adder_model(A, B):
        raise TestFailure("Adder result is incorrect: %s != %s" %
                          (str(dut.X), adder_model(A, B)))
    else:  # these last two lines are not strictly necessary
        dut._log.info("Ok!, %s + %s = %s !" %
                      (int(dut.A), int(dut.B), int(dut.X)))
Esempio n. 4
0
async def adder_basic_test(dut):
    """Test for 5 + 10"""

    A = 5
    B = 10

    dut.A <= A
    dut.B <= B

    await Timer(2, units='ns')

    assert dut.X.value == adder_model(
        A, B), f"Adder result is incorrect: {dut.X.value} != 15"
Esempio n. 5
0
async def adder_randomised_test(dut):
    """Test for adding 2 random numbers multiple times"""
    width = dut.DATA_WIDTH.value
    max_val = 2**width - 1
    for i in range(10):
        A = random.randint(0, max_val)
        B = random.randint(0, max_val)

        dut.A <= A
        dut.B <= B

        await Timer(2, units='ns')
        assert dut.X.value == adder_model(
            A, B
        ), f"Randomised test failed with: {dut.A.value} + {dut.B.value} = {dut.X.value}"
Esempio n. 6
0
def adder_basic_test(dut):
    """Test for 5 + 10"""
    yield Timer(2, units='ns')
    A = 5
    B = 10

    dut.A = A
    dut.B = B

    yield Timer(2, units='ns')

    if int(dut.X) != adder_model(A, B):
        raise TestFailure("Adder result is incorrect: %s != 15" % str(dut.X))
    else:  # these last two lines are not strictly necessary
        dut._log.info("Ok!")
Esempio n. 7
0
def adder_basic_test(dut):
    """Test for 5 + 10"""
    yield Timer(2)
    A = 5
    B = 10

    dut.A = A
    dut.B = B

    yield Timer(2)

    if int(dut.X) != adder_model(A, B):
        raise TestFailure(
            "Adder result is incorrect: %s != 15" % str(dut.X))
    else:  # these last two lines are not strictly necessary
        dut.log.info("Ok!")
Esempio n. 8
0
async def adder_randomised_test(dut):
    """Test for adding 2 random numbers multiple times"""

    for i in range(10):

        A = random.randint(0, 15)
        B = random.randint(0, 15)

        dut.A.value = A
        dut.B.value = B

        await Timer(2, units='ns')

        assert dut.X.value == adder_model(
            A, B), "Randomised test failed with: {A} + {B} = {X}".format(
                A=dut.A.value, B=dut.B.value, X=dut.X.value)
Esempio n. 9
0
def adder_randomised_test(dut):
    """Test for adding 2 random numbers multiple times"""
    yield Timer(2, units='ns')

    for i in range(10):
        A = random.randint(0, 15)
        B = random.randint(0, 15)

        dut.A = A
        dut.B = B

        yield Timer(2, units='ns')

        if int(dut.X) != adder_model(A, B):
            raise TestFailure("Randomised test failed with: %s + %s = %s" %
                              (int(dut.A), int(dut.B), int(dut.X)))
        else:  # these last two lines are not strictly necessary
            dut._log.info("Ok!")
Esempio n. 10
0
def adder_randomised_test(dut):
    """Test for adding 2 random numbers multiple times"""
    yield Timer(2)

    for i in range(10):
        A = random.randint(0, 15)
        B = random.randint(0, 15)

        dut.A = A
        dut.B = B

        yield Timer(2)

        if int(dut.X) != adder_model(A, B):
            raise TestFailure(
                "Randomised test failed with: %s + %s = %s" %
                (int(dut.A), int(dut.B), int(dut.X)))
        else:  # these last two lines are not strictly necessary
            dut.log.info("Ok!")
Esempio n. 11
0
def adder_basic2_test(dut):
    PERIOD = 10
    clk = dut.clk
    cocotb.fork(gen_clk(clk, PERIOD))

    """Test for 5 + 10"""
    yield Timer(20*PERIOD)
    A = 7
    B = 7

    dut.A = A
    dut.B = B

    yield Timer(20*PERIOD)
    # print(dut.test)

    if int(dut.X) != adder_model(A, B):
        raise TestFailure(
            "Adder result is incorrect: %s != 14" % str(dut.X))
    else:  # these last two lines are not strictly necessary
        dut._log.info("Ok!")
Esempio n. 12
0
def adder_random_test(dut):
    PERIOD = 10
    clk = dut.clk
    cocotb.fork(gen_clk(clk, PERIOD))

    """Test for adding 2 random numbers multiple times"""
    yield Timer(20*PERIOD)

    for i in range(10):
        A = random.randint(0, 5)
        B = random.randint(0, 5)
        dut.A = A
        dut.B = B

        yield Timer(20*PERIOD)

        if int(dut.X) != adder_model(A, B):
            raise TestFailure(
                "Randomised test failed with: %s + %s = %s" %
                (int(dut.A), int(dut.B), int(dut.X)))
        else:  # these last two lines are not strictly necessary
            dut._log.info("Ok!")
Esempio n. 13
0
 def adder_modelD(self, transaction):
     result = adder_model(transaction[0], transaction[1])
     self.expected_output.append(
         BinaryValue(value=result,
                     n_bits=int(self.dut.DATA_WIDTH),
                     bigEndian=False))
Esempio n. 14
0
def check(dut, A, B, C):
    if int(dut.X) != adder_model(A, B):
        raise TestFailure("Randomised test failed with: %s + %s = %s" %
                          (int(dut.A), int(dut.B), int(dut.X)))
    else:  # these last two lines are not strictly necessary
        dut._log.info("Ok!")
Esempio n. 15
0
def gr_test(dut):
    """
    Randmized test for o_sum = i_a + i_b (signed addition/subtraction) using GNU Radio
    """

    # start the clock
    cocotb.fork(Clock(dut.i_clk, 10, units="ns").start())
    clkedge = RisingEdge(dut.i_clk)

    yield clkedge  # synchronize ourselves with the clock

    a_lst = []
    b_lst = []
    sum_lst = []
    # start the simulation
    for _ in range(10):

        # randomize the input data
        A = random.randint(-10, 15)
        B = random.randint(-10, 15)
        dut.i_a <= A
        dut.i_b <= B

        # wait for posedge and let the output be resolved
        yield clkedge
        yield ReadWrite(
        )  # there seems some "updating" issue with cocotb here! ... without this statement, this test won't pass

        # format A if it is a negative number
        if A < 0:
            i_A = bin2sign(dut.i_a)
        else:
            i_A = int(dut.i_a)

        # format B if it is a negative number
        if B < 0:
            i_B = bin2sign(dut.i_b)
        else:
            i_B = int(dut.i_b)

        # format the output if it is a negative number
        if adder_model(A, B) < 0:
            o_SUM = bin2sign(dut.o_sum)
        else:
            o_SUM = int(dut.o_sum)

        a_lst.append(i_A)
        b_lst.append(i_B)
        sum_lst.append(o_SUM)

    # pass the data to Gnu Radio and get the return data
    np.array(a_lst, dtype=np.float32).tofile("data_a.bin")
    np.array(b_lst, dtype=np.float32).tofile("data_b.bin")
    main()
    sum_lst_gr = np.fromfile("data_sum.bin", dtype=np.float32)

    # compare the returned data with verilator output
    if np.array_equal(sum_lst_gr, np.array(sum_lst)):
        dut._log.info("gr_test passed")
    else:
        raise TestFailure("gr_test failed")

    # print for convinience
    print(np.fromfile("data_a.bin", dtype=np.float32))
    print(np.fromfile("data_b.bin", dtype=np.float32))
    print(np.fromfile("data_sum.bin", dtype=np.float32))