def test_pc(self): pc = pc_factory() for num in [-32768, -1, 0, 1, 32763, -21845, 21845, 10922, -10922]: num_bool16 = int_to_bool16(num) num_plus_1 = int_to_bool16(num + 1) num_plus_2 = int_to_bool16(num + 2) num_plus_3 = int_to_bool16(num + 3) num_plus_4 = int_to_bool16(num + 4) # Reset to 0 self.assertEqual(pc(reset=True), (False, ) * 16) # Input to num but don't load self.assertEqual(pc(in_=num_bool16), (False, ) * 16) # Load num self.assertEqual(pc(in_=num_bool16, load=True), num_bool16) # Don't do anything to see if output remains the same self.assertEqual(pc(), num_bool16) # Increase counter by 1 self.assertEqual(pc(inc=True), num_plus_1) # Increase counter by 1 and input unused number self.assertEqual(pc(in_=num_bool16, inc=True), num_plus_2) # Increase counter by 1 self.assertEqual(pc(inc=True), num_plus_3) # Increase counter by 1 and input unused number self.assertEqual(pc(in_=num_bool16, inc=True), num_plus_4) # Load num, increase counter, and reset self.assertEqual( pc(in_=num_bool16, load=True, inc=True, reset=True), (False, ) * 16) # Load num and increase counter self.assertEqual(pc(in_=num_bool16, load=True, inc=True), num_bool16) # Increase num self.assertEqual(pc(inc=True), num_plus_1) # Do nothing self.assertEqual(pc(), num_plus_1)
def test_adder_16(self): # for a, b in self.values: for a, b in ((5, 25), ): a_bools = int_to_bool16(a) b_bools = int_to_bool16(b) c_bools = adder_16(a_bools, b_bools) c = bool16_to_int(c_bools) self.assertEqual(a + b, c)
def test_alu(self): """2,880 combinations will be tested here. (9+8+7+6+5+4+3+2+1 * 2^6 = 2,880)""" for x, y in self.test_pairs: x = int_to_bool16(x) y = int_to_bool16(y) for params in self.possible_params: zx, nx, zy, ny, f, no = params expected = self.py_alu(zx, nx, zy, ny, f, no, x, y) alu_out = alu(zx, nx, zy, ny, f, no, x, y) self.assertEqual(alu_out, expected)
def test_register(self): register = register_16_bit_factory() for num in self.test_numbers: num_bool16 = int_to_bool16(num) self.assertEqual(register((False, ) * 16, True), (False, ) * 16) self.assertEqual(register(num_bool16, False), (False, ) * 16) self.assertEqual(register(num_bool16, True), num_bool16) self.assertEqual(register((False, ) * 16, False), num_bool16)
def test_ram_512(self): nums = [int_to_bool16(randint(-32000, 32000)) for _ in range(512)] addresses = list(product([True, False], repeat=9)) ram_512 = ram_512_factory() for num, address in zip(nums, addresses): ram_512(in_=num, load=True, address=address) for num, address in zip(nums, addresses): out = ram_512(in_=(False, ) * 16, load=False, address=address) self.assertEqual(num, out)
def test_fake_ram_16k(self): # Limit test to 50 random integers stored in 50 random addresses nums = [int_to_bool16(randint(-32000, 32000)) for _ in range(50)] addresses = sample(list(product([True, False], repeat=14)), 50) fake_ram_16k = FakeRAM() for num, address in zip(nums, addresses): fake_ram_16k(in_=num, load=True, address=address) for num, address in zip(nums, addresses): out = fake_ram_16k(in_=(False, ) * 16, load=False, address=address) self.assertEqual(num, out)
def test_unary_alu(self): for num in self.test_numbers: num_bool16 = int_to_bool16(num) num_z = bool16_to_int( unary_alu(zero=True, negation=False, in_=num_bool16)) num_n = bool16_to_int( unary_alu(zero=False, negation=True, in_=num_bool16)) num_zn = bool16_to_int( unary_alu(zero=True, negation=True, in_=num_bool16)) num_plain = bool16_to_int( unary_alu(zero=False, negation=False, in_=num_bool16)) self.assertEqual(0, num_z) self.assertEqual(~num, num_n) self.assertEqual(~0, num_zn) self.assertEqual(num, num_plain)
def test_mux_4way(self): a, b, c, d = [ int_to_bool16(num) for num in [-21845, 21845, 10922, -10922] ] for selectors in list(product([True, False], repeat=2)): out = mux_16_4way(selectors, a, b, c, d) if selectors[1]: if selectors[0]: expected = a else: expected = b else: if selectors[0]: expected = c else: expected = d self.assertEqual(expected, out)
def test_mux_8way(self): a, b, c, d, e, f, g, h = [ int_to_bool16(num) for num in [-32768, -1, 0, 1, 32763, -21845, 21845, 10922] ] for selectors in list(product([True, False], repeat=3)): out = mux_16_8way(selectors, a, b, c, d, e, f, g, h) """ returns a if selectors = True, True, True returns b if selectors = True, True, False returns c if selectors = True, False, True returns d if selectors = True, False, False returns e if selectors = False, True, True returns f if selectors = False, True, False returns g if selectors = False, False, True returns h if selectors = False, False, False """ if selectors[2]: if selectors[1]: if selectors[0]: expected = a else: expected = b else: if selectors[0]: expected = c else: expected = d else: if selectors[1]: if selectors[0]: expected = e else: expected = f else: if selectors[0]: expected = g else: expected = h self.assertEqual(expected, out)
def test_ram_8(self): ram_8 = ram_8_factory() nums = [-32768, -1, 0, 1, 32763, -21845, 21845, 10922] a, b, c, d, e, f, g, h = [int_to_bool16(num) for num in nums] a_address = True, True, True b_address = True, True, False c_address = True, False, True d_address = True, False, False e_address = False, True, True f_address = False, True, False g_address = False, False, True h_address = False, False, False ram_8(in_=a, load=True, address=a_address) ram_8(in_=b, load=True, address=b_address) ram_8(in_=c, load=True, address=c_address) ram_8(in_=d, load=True, address=d_address) ram_8(in_=e, load=True, address=e_address) ram_8(in_=f, load=True, address=f_address) ram_8(in_=g, load=True, address=g_address) ram_8(in_=h, load=True, address=h_address) self.assertEqual( a, ram_8(in_=(False, ) * 16, load=False, address=a_address)) self.assertEqual( b, ram_8(in_=(False, ) * 16, load=False, address=b_address)) self.assertEqual( c, ram_8(in_=(False, ) * 16, load=False, address=c_address)) self.assertEqual( d, ram_8(in_=(False, ) * 16, load=False, address=d_address)) self.assertEqual( e, ram_8(in_=(False, ) * 16, load=False, address=e_address)) self.assertEqual( f, ram_8(in_=(False, ) * 16, load=False, address=f_address)) self.assertEqual( g, ram_8(in_=(False, ) * 16, load=False, address=g_address)) self.assertEqual( h, ram_8(in_=(False, ) * 16, load=False, address=h_address))
def _load_memory(self, register: Callable, num: int): num_bool16 = int_to_bool16(num) register(in_=num_bool16, load=True, clock=False) register(clock=True) register(clock=False)
def test_inc_16(self): for i in (0, 1, -1, -32767, 32765, -1632, 1632): i_plus_1 = int_to_bool16(i + 1) i = int_to_bool16(i) self.assertEqual(i_plus_1, inc_16(i))
def _set_register_to_int(self, register, num: int): num_bool16 = int_to_bool16(num) register(num_bool16, True, False) register(clock=True) register(clock=False)
def test_zero(self): for num in self.test_numbers: num_bool16 = int_to_bool16(num) self.assertEqual(num == 0, equal_to_zero(num_bool16))
def test_negative(self): for num in self.test_numbers: num_bool16 = int_to_bool16(num) self.assertEqual(num < 0, less_than_zero(num_bool16))