Beispiel #1
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
Beispiel #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]
Beispiel #3
0
def test_half_adder():
    network = core.Network()
    a = gates.Switch(network)
    b = gates.Switch(network)
    r, c = adders.half_adder(a, b)
    network.drain()

    a.write(False)
    b.write(False)
    network.drain()
    assert not r.read()
    assert not c.read()

    a.write(True)
    b.write(False)
    network.drain()
    assert r.read()
    assert not c.read()

    a.write(False)
    b.write(True)
    network.drain()
    assert r.read()
    assert not c.read()

    a.write(True)
    b.write(True)
    network.drain()
    assert not r.read()
    assert c.read()
Beispiel #4
0
def test_ms_d_flop_basic():
    network = core.Network()
    clock = gates.Switch(network)
    data = gates.Switch(network)
    flop, flop_ = latches.ms_d_flop(gates.Not(data), clock, gates.Not(clock))
    network.drain()
    assert not flop.read()

    # clock a 1 through
    data.write(True)
    network.drain()
    assert not flop.read()
    assert flop_.read()
    clock.write(True)
    network.drain()
    assert not flop.read()
    assert flop_.read()
    clock.write(False)
    network.drain()
    assert flop.read()
    assert not flop_.read()

    # and back to 0
    data.write(False)
    network.drain()
    assert flop.read()
    assert not flop_.read()
    clock.write(True)
    network.drain()
    assert flop.read()
    assert not flop_.read()
    clock.write(False)
    network.drain()
    assert not flop.read()
    assert flop_.read()
Beispiel #5
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()
Beispiel #6
0
def test_short_cut_find():
    n = core.Network()
    a = gates.Switch(n)
    b = gates.Switch(n)
    c = gates.Switch(n)
    r, co = adders.full_adder(a, b, c)
    assert a.find('full_adder(0.1)') is co
    assert a.find('full_adder(0.0)') is r
Beispiel #7
0
def test_find():
    n = core.Network()
    a = gates.Switch(n)
    b = gates.Switch(n)
    c = gates.Switch(n)
    r, co = adders.full_adder(a, b, c)
    assert a.find('full_adder(0.half_adder(0.nor.nor.1).nor.nor.1)') is co
    assert a.find(
        'full_adder(0.half_adder(0.nor.nor.nor.0).half_adder(0.nor.nor.nor.0).0)'
    ) is r
Beispiel #8
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()
Beispiel #9
0
def test_list():
    n = core.Network()
    a = gates.Switch(n)
    b = gates.Switch(n)
    c = gates.Switch(n)
    r, co = adders.full_adder(a, b, c)
    assert a.list('') == ['full_adder(0']
    assert a.list('full_adder(0') == ['0)', '1)', 'half_adder(0']
    assert a.list('full_adder(0.half_adder(0') == ['0)', '1)', 'nor', 'nor']
    assert a.list('full_adder(0.half_adder(0.nor') == ['nor']
    assert a.list('full_adder(0.half_adder(0.nor.nor') == ['nor', '1)']
    assert a.list('full_adder(0.half_adder(0.nor.nor.1)') == ['nor']
    assert a.list('full_adder(0.half_adder(0.nor.nor.1).nor') == ['nor']
    assert a.list('full_adder(0.half_adder(0.nor.nor.1).nor.nor') == ['1)']
    assert a.list('full_adder(0.half_adder(0.nor.nor.1).nor.nor.1)') == []
Beispiel #10
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
Beispiel #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
Beispiel #12
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]
Beispiel #13
0
def test_gated_d_latch():
    network = core.Network()
    clock = gates.Switch(network)
    data = gates.Switch(network)
    latch, latch_ = latches.gated_d_latch(gates.Not(data), gates.Not(clock))
    network.drain()
    assert not latch.read()
    assert latch_.read()

    data.write(True)
    network.drain()
    assert not latch.read()
    assert latch_.read()

    clock.write(True)
    network.drain()
    assert latch.read()
    assert not latch_.read()

    data.write(False)
    network.drain()
    assert not latch.read()
    assert latch_.read()
Beispiel #14
0
def test_ms_d_flop_timing():
    network = core.Network()
    clock = gates.Switch(network)
    data = gates.Switch(network)
    flop, flop_ = latches.ms_d_flop(gates.Not(data), clock, gates.Not(clock))
    network.drain()
    assert not flop.read()

    # clock a 1 through
    data.write(True)
    network.drain()
    assert not flop.read()  # data has no impact
    assert flop_.read()
    clock.write(True)
    network.drain()
    assert not flop.read()  # clock high data in
    assert flop_.read()
    clock.write(False)
    data.write(False)
    network.drain()
    assert flop.read()  # clock low stored data out
    assert not flop_.read()

    # and back to 0
    data.write(False)
    network.drain()
    assert flop.read()  # data has no impact
    assert not flop_.read()
    clock.write(True)
    network.drain()
    assert flop.read()  # clock high data in
    assert not flop_.read()
    clock.write(False)
    data.write(True)
    network.drain()
    assert not flop.read()  # clock low stored data out
    assert flop_.read()
Beispiel #15
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()
Beispiel #16
0
def test_full_adder():
    network = core.Network()
    a = gates.Switch(network)
    b = gates.Switch(network)
    c = gates.Switch(network)
    r, co = adders.full_adder(a, b, c)
    network.drain()

    a.write(False)
    b.write(False)
    c.write(False)
    network.drain()
    assert not r.read()
    assert not co.read()

    a.write(True)
    b.write(False)
    c.write(False)
    network.drain()
    assert r.read()
    assert not co.read()

    a.write(False)
    b.write(True)
    c.write(False)
    network.drain()
    assert r.read()
    assert not co.read()

    a.write(True)
    b.write(True)
    c.write(False)
    network.drain()
    assert not r.read()
    assert co.read()

    a.write(False)
    b.write(False)
    c.write(True)
    network.drain()
    assert r.read()
    assert not co.read()

    a.write(True)
    b.write(False)
    c.write(True)
    network.drain()
    assert not r.read()
    assert co.read()

    a.write(False)
    b.write(True)
    c.write(True)
    network.drain()
    assert not r.read()
    assert co.read()

    a.write(True)
    b.write(True)
    c.write(True)
    network.drain()
    assert r.read()
    assert co.read()