Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.28 KB | None | 0 0
  1. class LogicGate:
  2.     def __init__(self, n):
  3.         self.label = n
  4.         self.output = None
  5.         self.visited = False
  6.  
  7.     def getLabel(self):
  8.         return self.label
  9.  
  10.     def getOutput(self):
  11.         print("Visiting:",self.label)
  12.         self.output = self.performGateLogic()
  13.         print(self.label, "Output:", self.output)
  14.         return self.output
  15.  
  16. class BinaryGate(LogicGate):
  17.     def __init__(self, n):
  18.         LogicGate.__init__(self, n)
  19.  
  20.         self.pinA = None
  21.         self.pinB = None
  22.  
  23.     def getPinA(self):
  24.         if self.pinA == None:
  25.             return int(input("Enter Pin A input for gate " + self.getLabel() + "-->"))
  26.         else:
  27.             a_output = self.pinA.getFrom().getOutput()
  28.             print(self.label, "Pin A:", a_output)
  29.             return a_output
  30.  
  31.     def getPinB(self):
  32.         if self.pinB == None:
  33.             return int(input("Enter Pin B input for gate " + self.getLabel() + "-->"))
  34.         else:
  35.             b_output = self.pinB.getFrom().getOutput()
  36.             print(self.label, "Pin B:", b_output)
  37.             return b_output
  38.          
  39.     def setNextPin(self,source):
  40.       if self.pinA == None:
  41.         self.pinA = source
  42.       else:
  43.         if self.pinB == None:
  44.             self.pinB = source
  45.         else:
  46.            raise RuntimeError("Error: NO EMPTY PINS")
  47.  
  48. class UnaryGate(LogicGate):
  49.     def __init__(self, n):
  50.         LogicGate.__init__(self, n)
  51.  
  52.         self.pin = None
  53.  
  54.     def getPin(self):
  55.         if self.pin == None:
  56.             return int(input("Enter Pin input for gate " + self.getLabel() + "-->"))
  57.         else:
  58.             return self.pin.getFrom().getOutput()
  59.          
  60.     def setNextPin(self,source):
  61.       if self.pin == None:
  62.         self.pin = source
  63.       else:
  64.            raise RuntimeError("Error: NO EMPTY PINS")
  65.  
  66. class AndGate(BinaryGate):
  67.     def __init__(self, n):
  68.         BinaryGate.__init__(self, n)
  69.  
  70.     def performGateLogic(self):
  71.         a = self.getPinA()
  72.         b = self.getPinB()
  73.         if (a == 1 and b == 1):
  74.             return 1
  75.         else:
  76.             return 0
  77.  
  78. class OrGate(BinaryGate):
  79.     def __init__(self, n):
  80.         BinaryGate.__init__(self, n)
  81.  
  82.     def performGateLogic(self):
  83.         a = self.getPinA()
  84.         b = self.getPinB()
  85.         if (a == 1 or b == 1):
  86.             return 1
  87.         else:
  88.             return 0
  89.  
  90. class NotOrGate(BinaryGate):
  91.     def __init__(self, n):
  92.         BinaryGate.__init__(self, n)
  93.  
  94.     def performGateLogic(self):
  95.         a = self.getPinA()
  96.         b = self.getPinB()
  97.         if (a == 0 or b == 0):
  98.             return 1
  99.         else:
  100.             return 0
  101.  
  102. class NotAndGate(BinaryGate):
  103.     def __init__(self, n):
  104.         BinaryGate.__init__(self, n)
  105.  
  106.     def performGateLogic(self):
  107.         a = self.getPinA()
  108.         b = self.getPinB()
  109.         if (a == 0 and a == 0):
  110.             return 1
  111.         else:
  112.             return 0
  113.  
  114. class ExclusiveOrGate(BinaryGate):
  115.     def __init__(self, n):
  116.         BinaryGate.__init__(self, n)
  117.  
  118.     def performGateLogic(self):
  119.         a = self.getPinA()
  120.         b = self.getPinB()
  121.         if ((a == 1 and b == 0) or (a == 0 and b == 1)):
  122.             return 1
  123.         else:
  124.             return 0
  125.  
  126. class NotExclusiveOrGate(BinaryGate):
  127.     def __init__(self, n):
  128.         BinaryGate.__init__(self, n)
  129.  
  130.     def performGateLogic(self):
  131.         a = self.getPinA()
  132.         b = self.getPinB()
  133.         if ((a == 1 and b == 0) or (a == 0 and b == 1)):
  134.             return 0
  135.         else:
  136.             return 1
  137.  
  138. class NotGate(UnaryGate):
  139.     def __init__(self, n):
  140.         UnaryGate.__init__(self, n)
  141.  
  142.     def performGateLogic(self):
  143.         a = self.getPin()
  144.         if a == 1:
  145.             return 0
  146.         else:
  147.             return 1
  148.  
  149. class Connector:
  150.     def __init__(self, fgate, tgate):
  151.         self.fromgate = fgate
  152.         self.togate = tgate
  153.  
  154.         tgate.setNextPin(self)
  155.  
  156.     def getFrom(self):
  157.         return self.fromgate
  158.  
  159.     def getTo(self):
  160.         return self.togate
  161.  
  162. class JKFlipFlop(BinaryGate):
  163.   #ok, so I know that this is probably not what you want,
  164.   #but I couldn't figure out how to do it another way :(
  165.  
  166.     def __init__(self, n):
  167.         BinaryGate.__init__(self, n)
  168.         self.q = 0
  169.         self.qnext = 0
  170.         self.visited = False
  171.  
  172.     def performGateLogic(self):
  173.         if self.visited == False:
  174.           self.visited = True
  175.           self.q = self.qnext
  176.           pinA = self.getPinA()
  177.           pinB = self.getPinB()
  178.        
  179.           if self.q == 0:
  180.             if pinA == 0:
  181.               self.qnext = 0
  182.             else:
  183.               self.qnext = 1
  184.           else:
  185.             if pinB == 0:
  186.               self.qnext = 1
  187.             else:
  188.               self.qnext = 0
  189.           print(self.label, "qnext:", self.qnext)
  190.         return self.q
  191.      
  192. class Power(UnaryGate):
  193.     def __init__(self, n):
  194.       UnaryGate.__init__(self, n)
  195.    
  196.     def performGateLogic(self):
  197.       self.pin = 1
  198.       return self.pin
  199.    
  200. class Switch(UnaryGate):
  201.     def __init__(self, n):
  202.       UnaryGate.__init__(self, n)
  203.       self.visited = False
  204.      
  205.     def performGateLogic(self):
  206.       if self.visited == False:
  207.         if self.pin == None:
  208.           self.pin = self.getPin()
  209.         self.visited = True
  210.       return self.pin
  211.    
  212.     def resetPin(self):
  213.       self.pin = None
  214.  
  215. def resetGate(gate):
  216.   gate.visited = False
  217.    
  218. and1 = AndGate("AND1")
  219. and2 = AndGate("AND2")
  220. and3 = AndGate("AND3")
  221. not1 = NotGate("NOT1")
  222. not2 = NotGate("NOT2")
  223. flipflop1 = JKFlipFlop("FLIPFLOP1")
  224. flipflop2 = JKFlipFlop("FLIPFLOP2")
  225. switch = Switch("SWITCH")
  226. power = Power("POWER")
  227.  
  228. c1 = Connector(switch, and1)
  229. c2 = Connector(switch, and2)
  230. c3 = Connector(switch, not2)
  231. c4 = Connector(and1, flipflop1)
  232. c5 = Connector(not2, flipflop1)
  233. c6 = Connector(flipflop1, not1)
  234. c7 = Connector(not1, and3)
  235. c8 = Connector(not1, and2)
  236. c9 = Connector(and2, flipflop2)
  237. c10 = Connector(power, flipflop2)
  238. c11 = Connector(flipflop2, and1)
  239. c12 = Connector(flipflop2, and3)
  240.  
  241. while True:    
  242.   if and3.getOutput() == 0:
  243.     print("No pulse------------------------------------")
  244.   else:
  245.     print("Pulse sent----------------------------------")
  246.   resetGate(flipflop1)
  247.   resetGate(flipflop2)
  248.   resetGate(switch)
  249.   switch.resetPin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement