Пример #1
0
def test_0_nor():
    network = core.Network()
    idx = network.add_gate(core.NOR)

    assert network.read(idx) is True
    network.step()
    assert network.read(idx) is True
Пример #2
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()
Пример #3
0
    def __init__(self):

        # A Batch is a collection of vertex lists for batched rendering.
        self.batch = pyglet.graphics.Batch()

        # A TextureGroup manages an OpenGL texture.
        group = MultiTextureGroup(gl.TEXTURE1,
                                  image.load(TEXTURE_PATH).get_texture())
        data = image.ImageData(1, 1, 'RGB', bytes([1, 2, 3]))
        self.group = MultiTextureGroup(gl.TEXTURE0, data.get_texture(), group)

        # A mapping from position to the texture of the block at that position.
        # This defines all the blocks that are currently in the world.
        self.world = {}

        # Same mapping as `world` but contains block orientations
        self.orientation = {}

        # Same mapping as `world` but contains core network ids
        self.line = {}

        # Mapping from position to a pyglet `VertextList` for all shown blocks.
        self._shown = {}

        self.network = core.Network()

        self._initialize()
Пример #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()
Пример #5
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
Пример #6
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
Пример #7
0
def test_step():
    network = core.Network()
    idx_0 = network.add_gate(core.SWITCH)
    idx_1 = network.add_gate(core.NOR)
    idx_2 = network.add_gate(core.NOR)
    network.add_link(idx_0, idx_1)
    network.add_link(idx_1, idx_2)

    network.drain()
    assert network.read(idx_0) is False
    assert network.read(idx_1) is True
    assert network.read(idx_2) is False

    network.write(idx_0, True)
    assert network.read(idx_0) is True
    assert network.read(idx_1) is True
    assert network.read(idx_2) is False

    assert network.step() is True
    assert network.read(idx_0) is True
    assert network.read(idx_1) is False
    assert network.read(idx_2) is False

    assert network.step() is False
    assert network.read(idx_0) is True
    assert network.read(idx_1) is False
    assert network.read(idx_2) is True

    assert network.step() is False
    assert network.read(idx_0) is True
    assert network.read(idx_1) is False
    assert network.read(idx_2) is True
Пример #8
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]
Пример #9
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()
Пример #10
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
Пример #11
0
def test_tie():
    network = core.Network()
    idx = network.add_gate(core.TIE)
    assert network.read(idx) is False
    network.write(idx, True)
    assert network.read(idx) is True
    network.write(idx, False)
    assert network.read(idx) is False
Пример #12
0
def test_switch():
    network = core.Network()
    idx = network.add_gate(core.SWITCH)
    assert network.read(idx) is False
    network.write(idx, True)
    assert network.read(idx) is True
    network.write(idx, False)
    assert network.read(idx) is False
Пример #13
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()
Пример #14
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
Пример #15
0
def test_address_decode(addr, a, b, c, d):
    network = core.Network()
    address = test_utils.BinaryIn(network, 2)
    control_lines = mux.address_decode(address)

    address.write(addr)
    network.drain()
    assert control_lines[0].read() == a
    assert control_lines[1].read() == b
    assert control_lines[2].read() == c
    assert control_lines[3].read() == d
Пример #16
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
Пример #17
0
def test_bit_mux():
    network = core.Network()
    lines = test_utils.BinaryIn(network, 3)
    data_lines = [lines[0], lines[1]]
    address = [lines[2]]
    res = mux.bit_mux(address, *data_lines)

    for i in range(8):
        lines.write(i)
        network.drain()
        if i in {1, 3, 6, 7}:
            assert res.read()
        else:
            assert not res.read()
Пример #18
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)') == []
Пример #19
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
Пример #20
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
Пример #21
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)
Пример #22
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
Пример #23
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
Пример #24
0
def main():
    network = core.Network()
    clock = Switch(network)
    write, res = computer(clock, primes())
    print()

    res = BinaryOut(res)
    network.drain()

    last = 0
    for i in range(5000):
        clock.write(True)
        network.drain()
        output = write.read()
        clock.write(False)
        network.drain()
        if output:
            print(i - last, res.read())
            last = i
Пример #25
0
def test_1_nor(input_type):
    network = core.Network()
    a_idx = network.add_gate(input_type)
    idx = network.add_gate(core.NOR)
    network.add_link(a_idx, idx)

    network.write(a_idx, False)
    assert network.read(idx) is True
    network.step()
    assert network.read(idx) is True

    network.write(a_idx, True)
    assert network.read(idx) is True
    network.step()
    assert network.read(idx) is False

    network.write(a_idx, False)
    assert network.read(idx) is False
    network.step()
    assert network.read(idx) is True
Пример #26
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]
Пример #27
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()
Пример #28
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()
Пример #29
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()
Пример #30
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()