Exemplo n.º 1
0
def test_no_jump_zero_with_non_zero():
    network = core.Network()
    clock = gates.Switch(network)
    write_flag = gates.Switch(network)
    address = test_utils.BinaryIn(network, 2)
    data_in = test_utils.BinaryIn(network, 8)
    jump_out, pc_in, pc_write = jump.jump(clock, write_flag, address, data_in)
    jump_out = test_utils.BinaryOut(jump_out)
    pc_in = test_utils.BinaryOut(pc_in)
    network.drain()

    data_in.write(212)
    address.write(1)
    write_flag.write(1)
    network.drain()
    assert not pc_write.read()

    clock.write(1)
    network.drain()
    assert not pc_write.read()

    clock.write(0)
    network.drain()
    assert not pc_write.read()
    write_flag.write(0)

    data_in.write(1)
    address.write(2)
    network.drain()
    assert not pc_write.read()

    write_flag.write(1)
    network.drain()
    assert not pc_write.read()
Exemplo n.º 2
0
def test_memory():
    network = core.Network()
    clock = gates.Switch(network)
    write_flag = gates.Switch(network)
    address = test_utils.BinaryIn(network, 8)
    data_in = test_utils.BinaryIn(network, 8)
    mem = memory.memory(clock, write_flag, address, data_in, 4)
    data_out = test_utils.BinaryOut(mem)
    network.drain()

    def write(value, addr):
        data_in.write(value)
        address.write(addr)
        write_flag.write(1)
        network.drain()
        clock.write(1)
        network.drain()
        write_flag.write(0)
        clock.write(0)
        network.drain()

    def read(addr):
        address.write(addr)
        network.drain()
        return data_out.read()

    data = [0] * 8
    for i in range(16):
        v = random.randrange(256)
        a = random.randrange(8)
        write(v, a)
        data[a] = v

        a = random.randrange(8)
        assert read(a) == data[a]
Exemplo n.º 3
0
def test_1byte_memory():
    network = core.Network()
    clock = gates.Switch(network)
    write_flag = gates.Switch(network)
    data_in = test_utils.BinaryIn(network, 8)
    mem = memory.memory(clock, write_flag, [], data_in, 0)
    data_out = test_utils.BinaryOut(mem)
    network.drain()

    def write(value):
        data_in.write(value)
        write_flag.write(1)
        network.drain()
        clock.write(1)
        network.drain()
        write_flag.write(0)
        clock.write(0)
        network.drain()

    def read():
        network.drain()
        return data_out.read()

    for i in range(16):
        v = random.randrange(256)
        write(v)
        assert read() == v
Exemplo n.º 4
0
def test_register():
    network = core.Network()
    clock = gates.Switch(network)
    data = test_utils.BinaryIn(network, 8)
    register = latches.register(data, clock)
    res = test_utils.BinaryOut(register)
    network.drain()
    assert res.read() == 0

    # clock a value through
    v1 = random.randrange(256)
    data.write(v1)
    network.drain()
    assert res.read() == 0
    clock.write(True)
    network.drain()
    assert res.read() == 0
    clock.write(False)
    network.drain()
    assert res.read() == v1

    # and a different value
    v2 = random.randrange(256)
    data.write(v2)
    network.drain()
    assert res.read() == v1
    clock.write(True)
    network.drain()
    assert res.read() == v1
    clock.write(False)
    network.drain()
    assert res.read() == v2
Exemplo n.º 5
0
 def __init__(self, module_func):
     self.network = core.Network()
     self.clock = gates.Switch(self.network)
     self.write_flag = gates.Switch(self.network)
     self.address = test_utils.BinaryIn(self.network, 3)
     self.data_in = test_utils.BinaryIn(self.network, 8)
     module = module_func(self.clock, self.write_flag, self.address,
                          self.data_in)
     self.data_out = test_utils.BinaryOut(module)
     self.network.drain()
Exemplo n.º 6
0
def test_word_mux():
    network = core.Network()
    inputs = [test_utils.BinaryIn(network, 2, value=3 - i) for i in range(4)]
    address = test_utils.BinaryIn(network, 2)
    m = mux.word_mux(address, *inputs)
    res = test_utils.BinaryOut(m)

    for i in range(4):
        address.write(i)
        network.drain()
        assert res.read() == 3 - i
Exemplo n.º 7
0
def test_jump():
    network = core.Network()
    clock = gates.Switch(network)
    write_flag = gates.Switch(network)
    address = test_utils.BinaryIn(network, 2)
    data_in = test_utils.BinaryIn(network, 8)
    jump_out, pc_in, pc_write = jump.jump(clock, write_flag, address, data_in)
    jump_out = test_utils.BinaryOut(jump_out)
    pc_in = test_utils.BinaryOut(pc_in)
    network.drain()

    data_in.write(123)
    address.write(0)
    network.drain()
    assert not pc_write.read()

    write_flag.write(True)
    network.drain()
    assert pc_in.read() == 123
    assert pc_write.read()

    write_flag.write(False)
    network.drain()
    assert not pc_write.read()
Exemplo n.º 8
0
def test_ripple_multiplier():
    network = core.Network()
    a = test_utils.BinaryIn(network, 8)
    b = test_utils.BinaryIn(network, 8)
    r, c = multipliers.ripple_multiplier(a, b)
    r = test_utils.BinaryOut(r)

    for i in range(10):
        v1 = random.randrange(32)
        v2 = random.randrange(32)
        print(v1, v2)
        a.write(v1)
        b.write(v2)
        network.drain()
        assert c.read() == (v1 * v2 >= 256)
        assert r.read() == (v1 * v2) % 256
Exemplo n.º 9
0
def test_ripple_subtractor():
    network = core.Network()
    a = test_utils.BinaryIn(network, 8)
    b = test_utils.BinaryIn(network, 8)
    r, c = adders.ripple_subtractor(a, b)
    r = test_utils.BinaryOut(r)

    for i in range(10):
        v1 = random.randrange(256)
        v2 = random.randrange(256)
        print(v1, v2)
        a.write(v1)
        b.write(v2)
        network.drain()
        assert c.read() == (v1 < v2)
        assert r.read() == (v1 - v2) % 256
Exemplo n.º 10
0
def test_ripple_sum():
    count = random.randrange(2, 10)

    network = core.Network()
    inputs = [test_utils.BinaryIn(network, 8) for i in range(count)]
    res, carry = adders.ripple_sum(*inputs)
    res = test_utils.BinaryOut(res)

    for i in range(10):
        values = [random.randrange(512 // count) for i in range(count)]
        print(values)
        for i, v in zip(inputs, values):
            i.write(v)
        network.drain()
        assert carry.read() == (sum(values) >= 256)
        assert res.read() == (sum(values) % 256)
Exemplo n.º 11
0
def test_low_literal():
    network = core.Network()
    clock = gates.Switch(network)
    write = gates.Switch(network)
    data_in = test_utils.BinaryIn(network, 8)
    address = test_utils.BinaryIn(network, 8)
    low_literal = literals.low_literal(clock, write, address, data_in, 4)
    data_out = test_utils.BinaryOut(low_literal)

    # read a value
    v = random.randrange(16)
    address.write(v)
    assert data_out.read() == v

    # and another
    v = random.randrange(16)
    address.write(v)
    assert data_out.read() == v
Exemplo n.º 12
0
def test_ripple_incr():
    network = core.Network()
    a = test_utils.BinaryIn(network, 8)
    r, c = adders.ripple_incr(a)
    r = test_utils.BinaryOut(r)

    for i in range(10):
        v = random.randrange(255)
        print(v)
        a.write(v)
        network.drain()
        assert c.read() == 0
        assert r.read() == v + 1

    a.write(255)
    network.drain()
    assert c.read() == 1
    assert r.read() == 0
Exemplo n.º 13
0
def test_rom():
    network = core.Network()
    clock = gates.Switch(network)
    write_flag = gates.Switch(network)
    address = test_utils.BinaryIn(network, 8)
    data_in = test_utils.BinaryIn(network, 8)
    data = [random.randrange(256) for i in range(16)]
    rom = memory.rom(clock, write_flag, address, data_in, 4, data)
    data_out = test_utils.BinaryOut(rom)
    network.drain()

    def read(addr):
        address.write(addr)
        write_flag.write(0)
        network.drain()
        return data_out.read()

    for i in range(100):
        a = random.randrange(16)
        assert read(a) == data[a]