def byte_and(): a = Bit() b = Bit() c = Bit() d = Bit() m1 = Byte([a, b, a, b]) m2 = Byte([d, b, a, c]) test_values = """\ 0000 0000 0001 0000 0010 0000 0011 0000 0100 0100 0101 0100 0110 0101 0111 0101 1000 0010 1001 1010 1010 0010 1011 1010 1100 0110 1101 1110 1110 0111 1111 1111""" inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines()) outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines())) write_test("byte_and", inputs, outputs) compile_program(m1 & m2)
def ex2(): a = Bit() b = Bit() test_values = """\ 00 0 01 0 10 0 11 1""" inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines()) outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines())) write_test('ex2', inputs, outputs) compile_program([a & b])
def ex4(): a = Bit() b = Bit() x = (a & b) ^ (~a) y = a ^ x test_values = """\ 00 11 01 11 10 01 11 10""" inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines()) outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines())) write_test('ex4', inputs, outputs) compile_program([x, y])
def ex3(): a = Bit() b = Bit() c = Bit() x = (a & b) | (~c & nand(b, a)) y = ~x test_values = """\ 111 10 110 10 101 01 100 10 011 01 010 10 001 01 000 10""" inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines()) outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines())) write_test('ex3', inputs, outputs) compile_program([x, y])
def ex1(): a = Bit() b = Bit() c = Bit() x = ~a & (b | c) y = nand(b, x) test_values = """\ 000 01 001 11 010 10 011 10 100 01 101 01 110 01 111 01""" inputs = '\n'.join(' '.join(line.split(' ')[0]) for line in test_values.splitlines()) outputs = ''.join(' ' + out + '\n' for out in (' '.join(line.split(' ')[1]) for line in test_values.splitlines())) write_test('ex1', inputs, outputs) compile_program([x, y])
def mux(bit1, bit2, sel): bit = (bit1 & ~sel) | (bit2 & sel) compile_program([bit])
def dmux(bit, sel): dmux_a, dmux_b = (bit & ~sel), (bit & sel) compile_program([dmux_a, dmux_b])