def test_inc(self): "Checks that inc(Multi) increases the value by 1" self.assertEquals(str(inc(m_16384)), str(m_16385)) self.assertEquals(str(inc(m_zero)), str(one16)) self.assertEquals(str(inc(m_three)), str(four16)) self.assertEquals(str(inc(neg_one)), str(zero16)) self.assertEquals(str(inc(neg_three)), str(neg_two))
def __call__(self, multi, load, increase, reset, clock): # if load, inc, or reset are set, load should be set for the register reg_load = load | increase | reset # if inc = 1, return (value + 1), else return value if_increase = multimux(self.value, inc(self.value), increase) # if load = 1, return multi, else return if_increase if_out = multimux(if_increase, multi, load) # if reset = 1, return 0, else return if_out if_reset = multimux(if_out, Multi(zero for i in range(16)), reset) self.value = self.reg(if_reset, reg_load, clock) return self.value
def from_num(num): "Helper function to create a 16-bit Multi instance using a number" bnum = bin(num) b = bnum.index('b') + 1 pos = Multi(Bit(digit) for digit in bnum[b:]) pos.insert(0, Bit(0)) pos = pad_to_digits(16, pos)[0] if bnum[0] == '-': neg = inc(~pos) if len(neg) > 16: return neg[1:] return Multi(neg) return Multi(pos)
def test_alu(self): x = zero16 y = neg_one self.assertEquals([str(bit) for bit in alu(x, y, one, zero, one, zero, one, zero)], [str(zero16), str(one), str(zero)]) # 0 self.assertEquals([str(bit) for bit in alu(x, y, one, one, one, one, one, one)], [str(one16), str(zero), str(zero)]) # 1 self.assertEquals([str(bit) for bit in alu(x, y, one, one, one, zero, one, zero)], [str(neg_one), str(zero), str(one)]) # -1 self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, one, one, zero, zero)], [str(zero16), str(one), str(zero)]) # x self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, zero, zero, zero)], [str(neg_one), str(zero), str(one)]) # y self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, one, one, zero, one)], [str(neg_one), str(zero), str(one)]) # ~x self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, zero, zero, one)], [str(zero16), str(one), str(zero)]) # ~y self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, one, one, one, one)], [str(zero16), str(one), str(zero)]) # -x self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, zero, one, one)], [str(one16), str(zero), str(zero)]) # -y self.assertEquals([str(bit) for bit in alu(x, y, zero, one, one, one, one, one)], [str(one16), str(zero), str(zero)]) # x + 1 self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, one, one, one)], [str(zero16), str(one), str(zero)]) # y + 1 self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, one, one, one, zero)], [str(neg_one), str(zero), str(one)]) # x - 1 self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, zero, one, zero)], [str(neg_two), str(zero), str(one)]) # y - 1 self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, zero, zero, one, zero)], [str(neg_one), str(zero), str(one)]) # x + y self.assertEquals([str(bit) for bit in alu(x, y, zero, one, zero, zero, one, one)], [str(one16), str(zero), str(zero)]) # x - y self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, zero, one, one, one)], [str(neg_one), str(zero), str(one)]) # y - x self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, zero, zero, zero, zero)], [str(zero16), str(one), str(zero)]) # x & y self.assertEquals([str(bit) for bit in alu(x, y, zero, one, zero, one, zero, one)], [str(neg_one), str(zero), str(one)]) # x | y x = Multi(from_num(17)) y = three16 self.assertEquals([str(bit) for bit in alu(x, y, one, zero, one, zero, one, zero)], [str(zero16), str(one), str(zero)]) # 0 self.assertEquals([str(bit) for bit in alu(x, y, one, one, one, one, one, one)], [str(one16), str(zero), str(zero)]) # 1 self.assertEquals([str(bit) for bit in alu(x, y, one, one, one, zero, one, zero)], [str(neg_one), str(zero), str(one)]) # -1 self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, one, one, zero, zero)], [str(x), str(zero), str(zero)]) # x self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, zero, zero, zero)], [str(y), str(zero), str(zero)]) # y self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, one, one, zero, one)], [str(~x), str(zero), str(one)]) # ~x self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, zero, zero, one)], [str(~y), str(zero), str(one)]) # ~y self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, one, one, one, one)], [str(from_num(-17)), str(zero), str(one)]) # -x self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, zero, one, one)], [str(neg_three), str(zero), str(one)]) # -y self.assertEquals([str(bit) for bit in alu(x, y, zero, one, one, one, one, one)], [str(inc(x)), str(zero), str(zero)]) # x + 1 self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, one, one, one)], [str(inc(y)), str(zero), str(zero)]) # y + 1 self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, one, one, one, zero)], [str(from_num(16)), str(zero), str(zero)]) # x - 1 self.assertEquals([str(bit) for bit in alu(x, y, one, one, zero, zero, one, zero)], [str(two16), str(zero), str(zero)]) # y - 1 self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, zero, zero, one, zero)], [str(from_num(20)), str(zero), str(zero)]) # x + y self.assertEquals([str(bit) for bit in alu(x, y, zero, one, zero, zero, one, one)], [str(from_num(14)), str(zero), str(zero)]) # x - y self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, zero, one, one, one)], [str(from_num(-14)), str(zero), str(one)]) # y - x self.assertEquals([str(bit) for bit in alu(x, y, zero, zero, zero, zero, zero, zero)], [str(one16), str(zero), str(zero)]) # x & y self.assertEquals([str(bit) for bit in alu(x, y, zero, one, zero, one, zero, one)], [str(from_num(19)), str(zero), str(zero)]) # x | y