def testMultiInputLogic(): length = 2 print("Input:\t Or: And: Nor: Nand: Xored: Xnored:") for i in range(0, 2**length): state = itot(i, length) ored = logic.Or(state) anded = logic.And(state) nored = logic.Nor(state) nanded = logic.Nand(state) Xored = logic.Xor(state) Xnored = logic.Xnor(state) fmt = (state, ored, anded, nored, nanded, Xored, Xnored) fmtstr = ("%s:\t\t%s\t%s\t%s\t%s\t%s\t%s" % fmt) print(fmtstr) length = 4 print("Input: Or: And: Nor: Nand: Xored: Xnored:") for i in range(0, 2**length): state = itot(i, length) ored = logic.Or(state) anded = logic.And(state) nored = logic.Nor(state) nanded = logic.Nand(state) Xored = logic.Xor(state) Xnored = logic.Xnor(state) fmt = (state, ored, anded, nored, nanded, Xored, Xnored) fmtstr = ("%s:\t%s\t%s\t%s\t%s\t%s\t%s" % fmt) print(fmtstr)
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 testFourBitMagnitudeComparator(): from Circuits import FourBitMagnitudeComparator as Comp comparator = Comp() length = 4 print("Four bit magnitude comparator:") for i in range(0, length * 10): left, right = getRandomInts(4) state1 = itot(left, 4) state2 = itot(right, 4) comparator.setinput(state1, state2, (0, 0, 0)) output = comparator.getoutput() print(left, right, output) print("")
def testPiPoRegister(): from Circuits import PiPoRegister register = PiPoRegister() print("Paralel in paralel out register.") enabled = 0 # once and zero's alternating data = 170 print("data:%s disabled:" % (itot(data, register.length), )) signal = (itot(data, register.length), enabled) register.setinput(signal) print(register.getoutput()) enabled = 1 print("data:%s enabled:" % (itot(data, register.length), )) signal = (itot(data, register.length), enabled) register.setinput(signal) print(register.getoutput()) enabled = 0 data = 0xF0 print("data:%s disabled: " % (itot(data, register.length), )) signal = (itot(data, register.length), enabled) register.setinput(signal) print(register.getoutput()) print("data:%s enabled:" % (itot(data, register.length), )) enabled = 1 signal = (itot(data, register.length), enabled) register.setinput(signal) print(register.getoutput()) print("")
def testHalfAdder(): from Circuits import HalfAdder h1 = HalfAdder() print("Halfadder: ") print(" A |B | Co|S ") for i in range(0, 4): state = itot(i, 2) h1.setinput(state) print("%s-->%s" % (state, h1.getoutput())) 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 testFullAdder(): from Circuits import FullAdder f1 = FullAdder() print("Fulladder: ") print(" A |B |Ci| Co|S ") # create a state and test on it. for i in range(0, 8): # generate four states state = itot(i, 3) f1.setinput(state) print("%s-->%s" % (state, f1.getoutput())) print("")
def testOneBitMagnitudeComparator(): from Circuits import OneBitMagnitudeComparator as comp comparator = comp() length = 2 print("magnitude comparator test:") print(" Ai|Bi Go|Eo|Lo") for i in range(2**length): state = itot(i, length) comparator.setinput(state) output = comparator.getoutput() fmt = (state, output) fmtstr = "%s %s" % fmt print(fmtstr)
def testencoder8to3(): from Circuits import Encoder8to3 encoder = Encoder8to3() inputs = 8 print("Encoder8to3: ") for i in range(0, inputs): inputed = (1 << i) state = itot(inputed, inputs) encoder.setinput(state) output = encoder.getoutput() fmt = (state, output) fmtstr = "%s : %s" % fmt print(fmtstr) 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("")