Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LogicGate:
- def __init__(self, n):
- self.label = n
- self.output = None
- self.visited = False
- def getLabel(self):
- return self.label
- def getOutput(self):
- print("Visiting:",self.label)
- self.output = self.performGateLogic()
- print(self.label, "Output:", self.output)
- return self.output
- class BinaryGate(LogicGate):
- def __init__(self, n):
- LogicGate.__init__(self, n)
- self.pinA = None
- self.pinB = None
- def getPinA(self):
- if self.pinA == None:
- return int(input("Enter Pin A input for gate " + self.getLabel() + "-->"))
- else:
- a_output = self.pinA.getFrom().getOutput()
- print(self.label, "Pin A:", a_output)
- return a_output
- def getPinB(self):
- if self.pinB == None:
- return int(input("Enter Pin B input for gate " + self.getLabel() + "-->"))
- else:
- b_output = self.pinB.getFrom().getOutput()
- print(self.label, "Pin B:", b_output)
- return b_output
- def setNextPin(self,source):
- if self.pinA == None:
- self.pinA = source
- else:
- if self.pinB == None:
- self.pinB = source
- else:
- raise RuntimeError("Error: NO EMPTY PINS")
- class UnaryGate(LogicGate):
- def __init__(self, n):
- LogicGate.__init__(self, n)
- self.pin = None
- def getPin(self):
- if self.pin == None:
- return int(input("Enter Pin input for gate " + self.getLabel() + "-->"))
- else:
- return self.pin.getFrom().getOutput()
- def setNextPin(self,source):
- if self.pin == None:
- self.pin = source
- else:
- raise RuntimeError("Error: NO EMPTY PINS")
- class AndGate(BinaryGate):
- def __init__(self, n):
- BinaryGate.__init__(self, n)
- def performGateLogic(self):
- a = self.getPinA()
- b = self.getPinB()
- if (a == 1 and b == 1):
- return 1
- else:
- return 0
- class OrGate(BinaryGate):
- def __init__(self, n):
- BinaryGate.__init__(self, n)
- def performGateLogic(self):
- a = self.getPinA()
- b = self.getPinB()
- if (a == 1 or b == 1):
- return 1
- else:
- return 0
- class NotOrGate(BinaryGate):
- def __init__(self, n):
- BinaryGate.__init__(self, n)
- def performGateLogic(self):
- a = self.getPinA()
- b = self.getPinB()
- if (a == 0 or b == 0):
- return 1
- else:
- return 0
- class NotAndGate(BinaryGate):
- def __init__(self, n):
- BinaryGate.__init__(self, n)
- def performGateLogic(self):
- a = self.getPinA()
- b = self.getPinB()
- if (a == 0 and a == 0):
- return 1
- else:
- return 0
- class ExclusiveOrGate(BinaryGate):
- def __init__(self, n):
- BinaryGate.__init__(self, n)
- def performGateLogic(self):
- a = self.getPinA()
- b = self.getPinB()
- if ((a == 1 and b == 0) or (a == 0 and b == 1)):
- return 1
- else:
- return 0
- class NotExclusiveOrGate(BinaryGate):
- def __init__(self, n):
- BinaryGate.__init__(self, n)
- def performGateLogic(self):
- a = self.getPinA()
- b = self.getPinB()
- if ((a == 1 and b == 0) or (a == 0 and b == 1)):
- return 0
- else:
- return 1
- class NotGate(UnaryGate):
- def __init__(self, n):
- UnaryGate.__init__(self, n)
- def performGateLogic(self):
- a = self.getPin()
- if a == 1:
- return 0
- else:
- return 1
- class Connector:
- def __init__(self, fgate, tgate):
- self.fromgate = fgate
- self.togate = tgate
- tgate.setNextPin(self)
- def getFrom(self):
- return self.fromgate
- def getTo(self):
- return self.togate
- class JKFlipFlop(BinaryGate):
- #ok, so I know that this is probably not what you want,
- #but I couldn't figure out how to do it another way :(
- def __init__(self, n):
- BinaryGate.__init__(self, n)
- self.q = 0
- self.qnext = 0
- self.visited = False
- def performGateLogic(self):
- if self.visited == False:
- self.visited = True
- self.q = self.qnext
- pinA = self.getPinA()
- pinB = self.getPinB()
- if self.q == 0:
- if pinA == 0:
- self.qnext = 0
- else:
- self.qnext = 1
- else:
- if pinB == 0:
- self.qnext = 1
- else:
- self.qnext = 0
- print(self.label, "qnext:", self.qnext)
- return self.q
- class Power(UnaryGate):
- def __init__(self, n):
- UnaryGate.__init__(self, n)
- def performGateLogic(self):
- self.pin = 1
- return self.pin
- class Switch(UnaryGate):
- def __init__(self, n):
- UnaryGate.__init__(self, n)
- self.visited = False
- def performGateLogic(self):
- if self.visited == False:
- if self.pin == None:
- self.pin = self.getPin()
- self.visited = True
- return self.pin
- def resetPin(self):
- self.pin = None
- def resetGate(gate):
- gate.visited = False
- and1 = AndGate("AND1")
- and2 = AndGate("AND2")
- and3 = AndGate("AND3")
- not1 = NotGate("NOT1")
- not2 = NotGate("NOT2")
- flipflop1 = JKFlipFlop("FLIPFLOP1")
- flipflop2 = JKFlipFlop("FLIPFLOP2")
- switch = Switch("SWITCH")
- power = Power("POWER")
- c1 = Connector(switch, and1)
- c2 = Connector(switch, and2)
- c3 = Connector(switch, not2)
- c4 = Connector(and1, flipflop1)
- c5 = Connector(not2, flipflop1)
- c6 = Connector(flipflop1, not1)
- c7 = Connector(not1, and3)
- c8 = Connector(not1, and2)
- c9 = Connector(and2, flipflop2)
- c10 = Connector(power, flipflop2)
- c11 = Connector(flipflop2, and1)
- c12 = Connector(flipflop2, and3)
- while True:
- if and3.getOutput() == 0:
- print("No pulse------------------------------------")
- else:
- print("Pulse sent----------------------------------")
- resetGate(flipflop1)
- resetGate(flipflop2)
- resetGate(switch)
- switch.resetPin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement