Exemple #1
0
def test_mux4way16bit():
    for i in range(n_iterations):
        sel = [randrange(2), randrange(2)]

        a_dec = randrange(random_ceil)
        a_bin = conversions.dec_to_bin_arr(n_bit, a_dec)

        b_dec = randrange(random_ceil)
        b_bin = conversions.dec_to_bin_arr(n_bit, b_dec)

        c_dec = randrange(random_ceil)
        c_bin = conversions.dec_to_bin_arr(n_bit, c_dec)

        d_dec = randrange(random_ceil)
        d_bin = conversions.dec_to_bin_arr(n_bit, d_dec)

        result = mux4Way16Bit.compute(sel, a_bin, b_bin, c_bin, d_bin)

        if sel == [0, 0]:
            expected_out = a_bin
            assert expected_out == result, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {} should output: {} but instead got: {}'.format(sel,
                                                                                                                                   a_dec,
                                                                                                                                   b_dec,
                                                                                                                                   c_dec,
                                                                                                                                   d_dec,
                                                                                                                                   expected_out,
                                                                                                                                   result)
        elif sel == [0, 1]:
            expected_out = b_bin
            assert expected_out == result, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {} should output: {} but instead got: {}'.format(
                sel,
                a_dec,
                b_dec,
                c_dec,
                d_dec,
                expected_out,
                result)

        elif sel == [1, 0]:
            expected_out = c_bin
            assert expected_out == result, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {} should output: {} but instead got: {}'.format(
                sel,
                a_dec,
                b_dec,
                c_dec,
                d_dec,
                expected_out,
                result)

        elif sel == [1, 1]:
            expected_out = d_bin
            assert expected_out == result, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {} should output: {} but instead got: {}'.format(
                sel,
                a_dec,
                b_dec,
                c_dec,
                d_dec,
                expected_out,
                result)
def test_and2way16bit():
    for i in range(n_iterations):
        a_dec = randrange(random_ceil)
        b_dec = randrange(random_ceil)

        a_bin = conversions.dec_to_bin_arr(n_bit, a_dec)
        b_bin = conversions.dec_to_bin_arr(n_bit, b_dec)

        expected_result = conversions.dec_to_bin_arr(n_bit, a_dec & b_dec)
        actual_result = and2Way16Bit.compute(a_bin, b_bin)
        assert actual_result == expected_result, 'Error, a[{}], b[{}] should output: [{}] but got: [{}]'.format(
            a_bin, b_bin, expected_result, actual_result)
Exemple #3
0
def test_pc_2():

    _in = zero
    result = pc.compute(_in, inc=0, load=0, reset=1)

    _in = conversions.dec_to_bin_arr(16, 6)
    result = pc.compute(_in, inc=0, load=1, reset=0)
    assert result == zero

    result = pc.compute(_in, inc=1, load=0, reset=0)
    assert result == _in

    result = pc.compute(_in, inc=1, load=0, reset=0)
    assert result == conversions.dec_to_bin_arr(16, 7)
Exemple #4
0
def test_dmux2way16bit():
    for i in range(n_iterations):
        selector_input = i % 2

        input_dec = randrange(random_ceil)
        input_bin = conversions.dec_to_bin_arr(n_bit, input_dec)

        result = dMux2Way16Bit.compute(selector_input, input_bin)

        if selector_input:
            assert result == [
                zero, input_bin
            ], 'Error, sel[{}], in{} should output: {} but got: {}'.format(
                selector_input, input_dec, [0, input_dec], [
                    conversions.bin_arr_to_dec(result[0]),
                    conversions.bin_arr_to_dec(result[1])
                ])
        else:
            assert result == [
                input_bin, zero
            ], 'Error, sel[{}], in{} should output: {} but got: {}'.format(
                selector_input, input_bin, [zero, input_bin], [
                    conversions.bin_arr_to_dec(result[0]),
                    conversions.bin_arr_to_dec(result[1])
                ])
def test_demux4way16bit():
    for i in range(n_iterations):
        selector_input = [randrange(2), randrange(2)]

        input_dec = randrange(random_ceil)
        input_bin = conversions.dec_to_bin_arr(n_bit, input_dec)

        result = dMux4Way16Bit.compute(selector_input, input_bin)
        result_dec = [
            conversions.bin_arr_to_dec(result[0]),
            conversions.bin_arr_to_dec(result[1]),
            conversions.bin_arr_to_dec(result[2]),
            conversions.bin_arr_to_dec(result[3])
        ]

        if selector_input == [0, 0]:
            expected_result = [input_bin, zero, zero, zero]
            expected_result_dec = expected_result_to_dec(expected_result)
            assert result == expected_result, 'Error, sel{}, in[{}] should output: {} but got: {}'.format(
                selector_input, input_dec, expected_result_dec, result_dec)
        elif selector_input == [0, 1]:
            expected_result = [zero, input_bin, zero, zero]
            expected_result_dec = expected_result_to_dec(expected_result)
            assert result == expected_result, 'Error, sel{}, in[{}] should output: {} but got: {}'.format(
                selector_input, input_dec, expected_result_dec, result_dec)
        elif selector_input == [1, 0]:
            expected_result = [zero, zero, input_bin, zero]
            expected_result_dec = expected_result_to_dec(expected_result)
            assert result == expected_result, 'Error, sel{}, in[{}] should output: {} but got: {}'.format(
                selector_input, input_dec, expected_result_dec, result_dec)
        elif selector_input == [1, 1]:
            expected_result = [zero, zero, zero, input_bin]
            expected_result_dec = expected_result_to_dec(expected_result)
            assert result == expected_result, 'Error, sel{}, in[{}] should output: {} but got: {}'.format(
                selector_input, input_dec, expected_result_dec, result_dec)
Exemple #6
0
def test_pc():
    _in = conversions.dec_to_bin_arr(16, 25)
    result = pc.compute(_in, inc=0, load=1, reset=0)
    assert result == zero, 'Error, expected {} but got {}'.format(zero, result)

    result = pc.compute(_in, inc=0, load=0, reset=0)
    assert result == _in, 'Error, expected {} but got {}'.format(_in, result)

    result = pc.compute(_in, inc=1, load=0, reset=0)
    assert result == _in, 'Error, expected {} but got {}'.format(_in, result)

    result = pc.compute(_in, inc=1, load=0, reset=1)
    assert result == conversions.dec_to_bin_arr(
        16, 26), 'Error, expected {} but got {}'.format(
            conversions.dec_to_bin_arr(16, 26), result)

    result = pc.compute(_in, inc=1, load=0, reset=0)
    assert result == zero, 'Error, expected {} but got {}'.format(zero, result)

    result = pc.compute(_in, inc=0, load=0, reset=0)
    assert result == zero[:15] + [1], 'Error, expected {} but got {}'.format(
        (zero[:15] + 1), result)
Exemple #7
0
def test_mux2way16bit():
    for i in range(400):
        sel = i % 2

        a_dec = randrange(random_ceil)
        a_bin = conversions.dec_to_bin_arr(n_bit, a_dec)

        b_dec = randrange(random_ceil)
        b_bin = conversions.dec_to_bin_arr(n_bit, b_dec)

        result = mux2Way16Bit.compute(sel, a_bin, b_bin)
        result_dec = conversions.bin_arr_to_dec(result)

        if sel == 0:
            expected_out = a_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {} should output: {} but instead got: {}'.format(
                sel, a_dec, b_dec, expected_out_dec, result_dec)
        else:
            expected_out = b_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {} should output: {} but instead got: {}'.format(
                sel, a_dec, b_dec, expected_out_dec, result_dec)
Exemple #8
0
def test_cpu():

    instr = instructions[0]  # @i
    mem_in = zero

    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    # assert cpu.load_pc == 0
    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
    assert cpu.d_register.value() == zero
    assert alu_out == zero
    assert load_pc == 0
    assert mem_dir == zero

    instr = instructions[1]  # M=1
    mem_in = zero

    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.jump_decoder_out == 0
    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
    assert cpu.d_register.value() == zero
    assert alu_out == conversions.dec_to_bin_arr(n_bit, 1)
    assert load_pc == 0
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

    instr = instructions[2]  # @sum
    mem_in = conversions.dec_to_bin_arr(n_bit, 1)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 17)
    assert cpu.d_register.value() == zero
    assert load_pc == 0
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

    instr = instructions[3]  # M=0
    mem_in = conversions.dec_to_bin_arr(n_bit, 0)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.jump_decoder_out == 0
    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 17)
    assert cpu.d_register.value() == zero
    assert load_pc == 0
    assert alu_out == zero
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 17)

    sum_ = 0

    # (LOOP)

    for i in range(100):

        instr = instructions[4]  # @i
        mem_in = conversions.dec_to_bin_arr(n_bit, 0)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(n_bit, i)
        assert load_pc == 0
        if i == 0:
            assert mem_dir == conversions.dec_to_bin_arr(n_bit, 17)
        else:
            assert mem_dir == conversions.dec_to_bin_arr(n_bit, 4)

        instr = instructions[5]  # D = M
        mem_in = conversions.dec_to_bin_arr(n_bit, i + 1)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.jump_decoder_out == 0
        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert load_pc == 0
        assert alu_out == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

        instr = instructions[6]  # @100
        mem_in = conversions.dec_to_bin_arr(n_bit, i + 1)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 100)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert load_pc == 0
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

        instr = instructions[7]  # D = D-A
        mem_in = conversions.dec_to_bin_arr(n_bit, 0)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 100)
        assert cpu.d_register.value() == cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert load_pc == 0
        assert alu_out == cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 100)

        instr = instructions[8]  # @END
        mem_in = conversions.dec_to_bin_arr(n_bit, 0)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 18)
        assert cpu.d_register.value() == cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert load_pc == 0
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 100)

        instr = instructions[9]  # D;JGT
        mem_in = conversions.dec_to_bin_arr(n_bit, 0)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 18)
        assert cpu.d_register.value() == cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert load_pc == 0
        assert cpu.alu_out == cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 18)

        instr = instructions[10]  # @i
        mem_in = conversions.dec_to_bin_arr(n_bit, 0)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
        assert cpu.d_register.value() == cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert load_pc == 0
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 18)

        instr = instructions[11]  # D=M
        mem_in = conversions.dec_to_bin_arr(n_bit, i + 1)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert load_pc == 0
        assert cpu.alu_out == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

        instr = instructions[12]  # @sum
        mem_in = conversions.dec_to_bin_arr(n_bit, i + 1)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 17)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert load_pc == 0
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

        instr = instructions[13]  # M=D+M
        mem_in = conversions.dec_to_bin_arr(n_bit, 0)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 17)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert load_pc == 0
        assert cpu.alu_out == conversions.dec_to_bin_arr(n_bit, (sum_ + i + 1))
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 17)

        instr = instructions[14]  # @i
        mem_in = conversions.dec_to_bin_arr(n_bit, (sum_ + i + 1))
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert load_pc == 0
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 17)

        instr = instructions[15]  # M=M+1
        mem_in = conversions.dec_to_bin_arr(n_bit, sum_ + i + 1)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert load_pc == 0
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

        instr = instructions[16]  # @LOOP
        mem_in = conversions.dec_to_bin_arr(n_bit, i + 2)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 4)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert load_pc == 0
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

        instr = instructions[17]  # 0;JMP
        mem_in = conversions.dec_to_bin_arr(n_bit, 0)
        alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

        assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 4)
        assert cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert load_pc == 1
        assert cpu.alu_out == conversions.dec_to_bin_arr(n_bit, 0)
        assert mem_dir == conversions.dec_to_bin_arr(n_bit, 4)

    instr = instructions[4]  # @i
    mem_in = conversions.dec_to_bin_arr(n_bit, 0)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
    assert cpu.d_register.value() == conversions.dec_to_bin_arr(n_bit, 100)
    assert load_pc == 0
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 4)

    instr = instructions[5]  # D = M
    mem_in = conversions.dec_to_bin_arr(n_bit, 101)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.jump_decoder_out == 0
    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 16)
    assert cpu.d_register.value() == conversions.dec_to_bin_arr(n_bit, 101)
    assert load_pc == 0
    assert alu_out == conversions.dec_to_bin_arr(n_bit, 101)
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

    instr = instructions[6]  # @100
    mem_in = conversions.dec_to_bin_arr(n_bit, 101)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 100)
    assert cpu.d_register.value() == conversions.dec_to_bin_arr(n_bit, 101)
    assert load_pc == 0
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 16)

    instr = instructions[7]  # D = D-A
    mem_in = conversions.dec_to_bin_arr(n_bit, 0)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 100)
    assert cpu.d_register.value() == conversions.dec_to_bin_arr(n_bit, 1)
    assert load_pc == 0
    assert alu_out == conversions.dec_to_bin_arr(n_bit, 1)
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 100)

    instr = instructions[8]  # @END
    mem_in = conversions.dec_to_bin_arr(n_bit, 0)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 18)
    assert cpu.d_register.value() == conversions.dec_to_bin_arr(n_bit, 1)
    assert load_pc == 0
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 100)

    instr = instructions[9]  # D;JGT
    mem_in = conversions.dec_to_bin_arr(n_bit, 0)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 18)
    assert cpu.d_register.value() == conversions.dec_to_bin_arr(n_bit, 1)
    assert load_pc == 1
    assert cpu.alu_out == conversions.dec_to_bin_arr(n_bit, 1)
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 18)

    instr = instructions[18]  # @END
    mem_in = conversions.dec_to_bin_arr(n_bit, 0)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 18)
    assert cpu.d_register.value() == conversions.dec_to_bin_arr(n_bit, 1)
    assert load_pc == 0
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 18)

    instr = instructions[19]  # 0; JMP
    mem_in = conversions.dec_to_bin_arr(n_bit, 0)
    alu_out, load_pc, mem_dir = cpu.compute(instr, mem_in)

    assert cpu.a_register.value() == conversions.dec_to_bin_arr(n_bit, 18)
    assert cpu.d_register.value() == conversions.dec_to_bin_arr(n_bit, 1)
    assert load_pc == 1
    assert cpu.alu_out == conversions.dec_to_bin_arr(n_bit, 0)
    assert mem_dir == conversions.dec_to_bin_arr(n_bit, 18)
Exemple #9
0
def test_emulator():

    n_iterations = 3

    emulator.load_instructions_mock(instructions)

    for i, instr in enumerate(instructions):
        assert emulator.instruction_memory.mem[i] == instr

    # @i
    emulator.run(1)

    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 16)
    assert emulator.cpu.d_register.value() == zero
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == zero

    # M=1
    emulator.run(1)

    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 16)
    assert emulator.cpu.d_register.value() == zero
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == zero

    # @sum
    emulator.run(1)

    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 17)
    assert emulator.cpu.d_register.value() == zero
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == one
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == zero

    # M=0
    emulator.run(1)

    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 17)
    assert emulator.cpu.d_register.value() == zero
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == one
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == zero

    sum_value = conversions.dec_to_bin_arr(n_bit, 0)

    for i in range(100):

        # @i
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 16)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # D=M
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 16)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # @100
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 100)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # D = D-A
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 100)
        assert emulator.cpu.d_register.value() == emulator.cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # @END
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 18)
        assert emulator.cpu.d_register.value() == emulator.cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # D;JGT
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 18)
        assert emulator.cpu.d_register.value() == emulator.cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # @i
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 16)
        assert emulator.cpu.d_register.value() == emulator.cpu.alu.minus_x(
            conversions.dec_to_bin_arr(n_bit, (100 - (i + 1))), zero)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # D=M
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 16)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # @sum
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 17)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # M=D+M
        sum_value_temp = sum_value
        sum_value = emulator.cpu.alu.x_plus_y(
            emulator.cpu.d_register.value(),
            emulator.data_memory.mem[conversions.bin_arr_to_dec(
                instructions[2])])

        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 17)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value_temp

        # @i
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 16)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # M=M+1
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 16)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # @LOOP
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 4)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 2)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

        # 0;JMP
        emulator.run(1)
        assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
            n_bit, 4)
        assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
            n_bit, i + 1)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[0])] == conversions.dec_to_bin_arr(n_bit, i + 2)
        assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
            instructions[2])] == sum_value

    # @i
    emulator.run(1)
    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 16)
    assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
        n_bit, 100)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == conversions.dec_to_bin_arr(n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == sum_value

    # D=M
    emulator.run(1)
    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 16)
    assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
        n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == conversions.dec_to_bin_arr(n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == sum_value

    # @100
    emulator.run(1)
    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 100)
    assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
        n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == conversions.dec_to_bin_arr(n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == sum_value

    # D=D-A
    emulator.run(1)
    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 100)
    assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
        n_bit, 1)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == conversions.dec_to_bin_arr(n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == sum_value

    # @END
    emulator.run(1)
    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 18)
    assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
        n_bit, 1)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == conversions.dec_to_bin_arr(n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == sum_value

    # D;JGT
    emulator.run(1)
    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 18)
    assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
        n_bit, 1)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == conversions.dec_to_bin_arr(n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == sum_value

    # @END
    emulator.run(1)
    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 18)
    assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
        n_bit, 1)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == conversions.dec_to_bin_arr(n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == sum_value

    # 0; JMP
    emulator.run(1)
    assert emulator.cpu.a_register.value() == conversions.dec_to_bin_arr(
        n_bit, 18)
    assert emulator.cpu.d_register.value() == conversions.dec_to_bin_arr(
        n_bit, 1)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[0])] == conversions.dec_to_bin_arr(n_bit, 101)
    assert emulator.data_memory.mem[conversions.bin_arr_to_dec(
        instructions[2])] == sum_value
Exemple #10
0
import os

sys.path.append(os.environ['ROOT_FOLDER'] + '/logic_gates')
from dmux2way16bit import DMux2Way16Bit

sys.path.append(os.environ['ROOT_FOLDER'] + '/utils')
import conversions

from random import randrange

n_iterations = 100
n_bit = 16
random_ceil = conversions.bin_to_dec('1' * 16)

dMux2Way16Bit = DMux2Way16Bit()
zero = conversions.dec_to_bin_arr(n_bit, 0)


def test_dmux2way16bit():
    for i in range(n_iterations):
        selector_input = i % 2

        input_dec = randrange(random_ceil)
        input_bin = conversions.dec_to_bin_arr(n_bit, input_dec)

        result = dMux2Way16Bit.compute(selector_input, input_bin)

        if selector_input:
            assert result == [
                zero, input_bin
            ], 'Error, sel[{}], in{} should output: {} but got: {}'.format(
Exemple #11
0
def test_mux8way1bit():
    for i in range(n_iterations):

        sel = [randrange(2), randrange(2), randrange(2)]
        sel_dec = conversions.bin_arr_to_dec(sel)

        a_dec = randrange(random_ceil)
        a_bin = conversions.dec_to_bin_arr(n_bit, a_dec)

        b_dec = randrange(random_ceil)
        b_bin = conversions.dec_to_bin_arr(n_bit, b_dec)

        c_dec = randrange(random_ceil)
        c_bin = conversions.dec_to_bin_arr(n_bit, c_dec)

        d_dec = randrange(random_ceil)
        d_bin = conversions.dec_to_bin_arr(n_bit, d_dec)

        e_dec = randrange(random_ceil)
        e_bin = conversions.dec_to_bin_arr(n_bit, e_dec)

        f_dec = randrange(random_ceil)
        f_bin = conversions.dec_to_bin_arr(n_bit, f_dec)

        g_dec = randrange(random_ceil)
        g_bin = conversions.dec_to_bin_arr(n_bit, g_dec)

        h_dec = randrange(random_ceil)
        h_bin = conversions.dec_to_bin_arr(n_bit, h_dec)

        result = mux8Way1Bit.compute(sel, a_bin, b_bin, c_bin, d_bin, e_bin,
                                     f_bin, g_bin, h_bin)
        result_dec = conversions.bin_arr_to_dec(result)

        if sel == [0, 0, 0]:
            expected_out = a_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {}, e: {}, f: {}, '\
                                           'g: {}, h: {} should output: {} but instead got: {}'.format(sel_dec,
                                                                                                       a_dec,
                                                                                                       b_dec,
                                                                                                       c_dec,
                                                                                                       d_dec,
                                                                                                       e_dec,
                                                                                                       f_dec,
                                                                                                       g_dec,
                                                                                                       h_dec,
                                                                                                       expected_out_dec,
                                                                                                       result_dec)
        if sel == [0, 0, 1]:
            expected_out = b_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {}, e: {}, f: {}, ' \
                                           'g: {}, h: {} should output: {} but instead got: {}'.format(sel_dec,
                                                                                                       a_dec,
                                                                                                       b_dec,
                                                                                                       c_dec,
                                                                                                       d_dec,
                                                                                                       e_dec,
                                                                                                       f_dec,
                                                                                                       g_dec,
                                                                                                       h_dec,
                                                                                                       expected_out_dec,
                                                                                                       result_dec)
        if sel == [0, 1, 0]:
            expected_out = c_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {}, e: {}, f: {}, ' \
                                           'g: {}, h: {} should output: {} but instead got: {}'.format(sel_dec,
                                                                                                       a_dec,
                                                                                                       b_dec,
                                                                                                       c_dec,
                                                                                                       d_dec,
                                                                                                       e_dec,
                                                                                                       f_dec,
                                                                                                       g_dec,
                                                                                                       h_dec,
                                                                                                       expected_out_dec,
                                                                                                       result_dec)
        if sel == [0, 1, 1]:
            expected_out = d_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {}, e: {}, f: {}, ' \
                                           'g: {}, h: {} should output: {} but instead got: {}'.format(sel_dec,
                                                                                                       a_dec,
                                                                                                       b_dec,
                                                                                                       c_dec,
                                                                                                       d_dec,
                                                                                                       e_dec,
                                                                                                       f_dec,
                                                                                                       g_dec,
                                                                                                       h_dec,
                                                                                                       expected_out_dec,
                                                                                                       result_dec)
        if sel == [1, 0, 0]:
            expected_out = e_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {}, e: {}, f: {}, ' \
                                           'g: {}, h: {} should output: {} but instead got: {}'.format(sel_dec,
                                                                                                       a_dec,
                                                                                                       b_dec,
                                                                                                       c_dec,
                                                                                                       d_dec,
                                                                                                       e_dec,
                                                                                                       f_dec,
                                                                                                       g_dec,
                                                                                                       h_dec,
                                                                                                       expected_out_dec,
                                                                                                       result_dec)
        if sel == [1, 0, 1]:
            expected_out = f_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {}, e: {}, f: {}, ' \
                                           'g: {}, h: {} should output: {} but instead got: {}'.format(sel_dec,
                                                                                                       a_dec,
                                                                                                       b_dec,
                                                                                                       c_dec,
                                                                                                       d_dec,
                                                                                                       e_dec,
                                                                                                       f_dec,
                                                                                                       g_dec,
                                                                                                       h_dec,
                                                                                                       expected_out_dec,
                                                                                                       result_dec)
        if sel == [1, 1, 0]:
            expected_out = g_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {}, e: {}, f: {}, ' \
                                           'g: {}, h: {} should output: {} but instead got: {}'.format(sel_dec,
                                                                                                       a_dec,
                                                                                                       b_dec,
                                                                                                       c_dec,
                                                                                                       d_dec,
                                                                                                       e_dec,
                                                                                                       f_dec,
                                                                                                       g_dec,
                                                                                                       h_dec,
                                                                                                       expected_out_dec,
                                                                                                       result_dec)
        if sel == [1, 1, 1]:
            expected_out = h_bin
            expected_out_dec = conversions.bin_arr_to_dec(expected_out)
            assert result == expected_out, 'Error, the input sel: {}, a: {}, b: {}, c: {}, d: {}, e: {}, f: {}, ' \
                                           'g: {}, h: {} should output: {} but instead got: {}'.format(sel_dec,
                                                                                                       a_dec,
                                                                                                       b_dec,
                                                                                                       c_dec,
                                                                                                       d_dec,
                                                                                                       e_dec,
                                                                                                       f_dec,
                                                                                                       g_dec,
                                                                                                       h_dec,
                                                                                                       expected_out_dec,
                                                                                                       result_dec)