def test_mux8(self): a = Bus(8) b = Bus(8) select = Wire() out = Bus(8) inputs = Bus([a, b, select]) Mux8(a=a, b=b, select=select, out=out) for x in [0, 1, 2, 10, 42, 64, 77, 99, 100, 127, 128, 196, 254, 255]: for y in [ 0, 1, 2, 3, 5, 20, 25, 33, 50, 101, 127, 128, 250, 254, 255 ]: inputs.reset() a.value = x b.value = y select.value = False # print("test_mux8 SELECT = 0 A =", a.value, "B =", b.value, "OUT =", out.value) self.assertEqual(a.value, out.value, "OUT == A") inputs.reset() a.value = x b.value = y select.value = True # print("test_mux8 SELECT = 1 A =", a.value, "B =", b.value, "OUT =", out.value) self.assertEqual(b.value, out.value, "OUT == B")
def test_add8(self): a = Bus(8) b = Bus(8) out = Bus(8) cin = Wire() cout = Wire() inputs = Bus([a, b, cin]) Add8(a=a, b=b, cin=cin, out=out, cout=cout) a.value = 2 b.value = 3 cin.value = False self.assertEqual(out.value, 5) for x in [0, 1, 2, 10, 42, 64, 77, 99, 100, 127, 128, 196, 254, 255]: for y in [ 0, 1, 2, 3, 5, 20, 25, 33, 50, 101, 127, 128, 250, 254, 255 ]: for z in [0, 1]: inputs.reset() a.value = x b.value = y cin.value = bool(z) # print("test_add8", x, "+", y, "+", z, "=", out.value) self.assertEqual(out.value, (x + y + z) % 256) self.assertIs(cout.value, x + y + z >= 256)
def test_int(self): d = { 'a': Wire('a', input1=1), 'b': Wire('b', input1=2), 'c': Wire('c', 'OR', 'a', 'b') } self.assertEqual(evaluate(d, d['a']), 1) self.assertEqual(evaluate(d, d['b']), 2) self.assertEqual(evaluate(d, d['c']), 3)
def setUp(self): reset_globals() self.inputs = Bus([Bus(8), Bus(8), Bus(8), Wire()]) self.a, self.b, self.op, self.cin = self.inputs self.out = Bus(8) self.cout = Wire() self.alu = ALU( a=self.a, b=self.b, op=self.op, cin=self.cin, out=self.out, cout=self.cout, )
def print_binary_gates(self): """ Print truth tables for each gate without assertions. """ for GateClass in [NAND, AND, OR, NOR, XOR, XNOR]: x = Wire() y = Wire() gate = GateClass(a=x, b=y) z = gate.out for x_value in [True, False]: for y_value in [True, False]: x.reset() y.reset() x.value = x_value y.value = y_value print(type(gate).__name__, x.value, y.value, " =>", z.value, sep="\t") print()
def test_not_gate(self): inp = Wire() out = Wire() NOT(inp, out) inp.value = True self.assertIs(out.value, False) inp.reset() inp.value = False self.assertIs(out.value, True)
def test_mux(self): a, b, sel = inputs = Bus(3) out = Wire() mux = Mux(a=a, b=b, select=sel, out=out) for x in [0, 1]: for y in [0, 1]: inputs.reset() a.value = bool(x) b.value = bool(y) sel.value = False self.assertIs(out.value, a.value, "OUT == A") inputs.reset() a.value = bool(x) b.value = bool(y) sel.value = True self.assertIs(out.value, b.value, "OUT == B")
def test_int(self): self.assertEqual(evaluate(value=Wire(input1=1)), 1) self.assertEqual(evaluate(value=Wire(cmd='NOT', input1=1)), 65534) self.assertEqual(evaluate(value=Wire(cmd='OR', input1=1, input2=2)), 3) self.assertEqual(evaluate(value=Wire(cmd='OR', input1=1, input2=3)), 3) self.assertEqual(evaluate(value=Wire(cmd='OR', input1=3, input2=12)), 15) self.assertEqual(evaluate(value=Wire(cmd='AND', input1=1, input2=2)), 0) self.assertEqual(evaluate(value=Wire(cmd='AND', input1=3, input2=6)), 2) self.assertEqual(evaluate(value=Wire(cmd='AND', input1=15, input2=6)), 6) self.assertEqual( evaluate(value=Wire(cmd='LSHIFT', input1=1, input2=3)), 8) self.assertEqual( evaluate(value=Wire(cmd='LSHIFT', input1=2, input2=4)), 32) self.assertEqual( evaluate(value=Wire(cmd='LSHIFT', input1=512, input2=8)), 0) self.assertEqual( evaluate(value=Wire(cmd='RSHIFT', input1=256, input2=4)), 16) self.assertEqual( evaluate(value=Wire(cmd='RSHIFT', input1=63, input2=4)), 3) self.assertEqual( evaluate(value=Wire(cmd='RSHIFT', input1=31, input2=5)), 0)