def testXBitPiPoRegister(): from Circuits import XBitPiPoRegister from getch import getch import time import sys bitlength = 4 register = XBitPiPoRegister(length=bitlength) print("\nvariable length parallel in parallel out register:") data = itot(0, bitlength) clock = 0 char = '' while (char != u'q'): if char >= u'0' and char <= u'9': intdata = ttoi(data) shifted = (ord(char) - ord(u'0') - 1) intdata ^= (1 << shifted) data = itot(intdata, bitlength) elif char == u'c': clock = logic.Not(clock) signal = (data, clock) register.setinput(signal) output = register.getoutput() fmt = (clock, data, output, time.time()) fmtstr = "Clock:%s Input:%s Output:%s %s\r" % fmt sys.stdout.write(fmtstr) char = getch()
def testXBitSubtractor(): from Circuits import XBitSubtractor bitlength = 8 print("integer size: %s" % bitlength) subtractor = XBitSubtractor(bitlength) print("XBitSubtractor unsigned: ") for i in range(0, 6): left, right = getRandomInts(bitlength) state1 = itot(left, bitlength) state2 = itot(right, bitlength) subtractor.setinput(state1, state2, 0) answer = ttoi(subtractor.getoutput()[0]) fmt = (ttoi(state1), ttoi(state2), answer) print("%s - %s = %s" % fmt) print("signed: ") for i in range(0, 6): left, right = getRandomInts(bitlength) state1 = itot(left, bitlength) state2 = itot(right, bitlength) subtractor.setinput(state1, state2, 0) output = subtractor.getoutput() if output[1]: answer = -(ttoi(invertTuple(output[0])) + 1) else: answer = ttoi(output[0]) fmt = (ttoi(state1), ttoi(state2), answer) print("%s - %s = %s " % fmt) print("")
def testXBitAdder(): from Circuits import XBitAdder bitlength = 8 print("max integer size: %d" % (bitlength)) adder = XBitAdder(bitlength) print("Xbitadder: ") # run 20 tests for i in range(0, 6): left, right = getRandomInts(bitlength) state1 = itot(left, bitlength) state2 = itot(right, bitlength) adder.setinput(state1, state2, 0) answer = ttoi(adder.getoutput()[0]) fmt = (ttoi(state1), ttoi(state2), answer, (answer == (left + right))) if adder.getoutput()[1]: print("%s + %s = %s :check:%s integer overflow" % fmt) else: print("%s + %s = %s :check:%s" % fmt) print("")
def testFourBitAdder(): from Circuits import FourBitAdder adder = FourBitAdder() bitlength = 4 print("FourBitadder: Addition") for i in range(0, bitlength): left, right = getRandomInts(bitlength) state1 = itot(left, bitlength) state2 = itot(right, bitlength) adder.setinput(state1, state2, 0) output = adder.getoutput() if output[1]: overflow = True else: overflow = False answer = ttoi(output[0]) check = (answer == (left + right)) fmt = (ttoi(state1), ttoi(state2), answer, check) if overflow: fmtstr = "%s + %s = %s :check:%s number overflow" else: fmtstr = "%s + %s = %s :check:%s" print(fmtstr % fmt) print("")
def testSubtractor(): import Circuits as cir subtractor = cir.FourBitSubtractor() bitlength = 4 print("FourBitSubtractor: ") print("printing signed representation:") for i in range(0, 5): left, right = getRandomInts(bitlength) state1 = itot(left, bitlength) state2 = itot(right, bitlength) subtractor.setinput(state1, state2, 0) output = subtractor.getoutput() # check signednes if output[1]: # if signed do this for the right negative number # because if you don't you get to deal with unsigned number. # and thus have overflow, and thus not really good to check for # human readers, unless you like to think about it ofcourse . answer = -(ttoi(invertTuple(output[0])) + 1) else: answer = (ttoi(output[0])) fmt = (left, right, answer) fmtstr = "%s - %s = %s" % fmt print(fmtstr) print("printing unsigned representation: ") for i in range(0, 5): left, right = getRandomInts(bitlength) state1 = itot(left, bitlength) state2 = itot(right, bitlength) subtractor.setinput(state1, state2, 0) output = subtractor.getoutput() answer = ttoi(output[0]) fmt = (left, right, answer) fmtstr = "%s - %s = %s" % fmt print(fmtstr) print("")