Last active
February 3, 2017 10:53
Revisions
-
imakin revised this gist
Feb 3, 2017 . 1 changed file with 145 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,17 +23,159 @@ def test_ff_one(self): print("test togle state") jkff.j.value = True jkff.k.value = True jkff.clock(False) self.assertFalse(jkff.q) self.assertTrue(jkff.qn) jkff.clock(False) self.assertTrue(jkff.q) self.assertFalse(jkff.qn) jkff.clock(False) self.assertFalse(jkff.q) self.assertTrue(jkff.qn) def test_ff_8bit_asynch(self): jkffs = [JKFF() for x in range(0,8)] for x in range(0,8): jkffs[x].j.value = True jkffs[x].k.value = True if x>0: jkffs[x].clk.add_connection( jkffs[x-1].q ) jkffs[0].clock() def test_ff_synch(self): jkff0 = JKFF() jkff1 = JKFF() jkff2 = JKFF() jkff3 = JKFF() n2 = AND() n3 = AND3() jkff0.clk.add_connection(jkff1.clk) jkff0.clk.add_connection(jkff2.clk) jkff0.clk.add_connection(jkff3.clk) jkff0.j.add_connection(jkff0.k) n2.a.add_connection(jkff0.q) n2.b.add_connection(jkff1.q) n3.a.add_connection(jkff0.q) n3.b.add_connection(jkff1.q) n3.c.add_connection(jkff2.q) jkff0.q.add_connection(jkff1.j) jkff0.q.add_connection(jkff1.k) n2.output.add_connection(jkff2.j) n2.output.add_connection(jkff2.k) n3.output.add_connection(jkff3.j) n3.output.add_connection(jkff3.k) jkff0.j.value = True jkff0.k.value = True first = True jkff3.clock(half=False) jkff2.clock(half=False) jkff1.clock(half=False) jkff0.clock(half=False) n2.clock() n3.clock() self.assertEqual(jkff3.q.value, True) self.assertEqual(jkff2.q.value, True) self.assertEqual(jkff1.q.value, True) self.assertEqual(jkff0.q.value, True) #~ print(jkff3.q.value, jkff2.q.value, jkff1.q.value, jkff0.q.value) jkff3.clock(half=False) jkff2.clock(half=False) jkff1.clock(half=False) jkff0.clock(half=False) n2.clock() n3.clock() self.assertEqual(jkff3.q.value, False) self.assertEqual(jkff2.q.value, False) self.assertEqual(jkff1.q.value, False) self.assertEqual(jkff0.q.value, False) #~ print(jkff3.q.value, jkff2.q.value, jkff1.q.value, jkff0.q.value) jkff3.clock(half=False) jkff2.clock(half=False) jkff1.clock(half=False) jkff0.clock(half=False) n2.clock() n3.clock() self.assertEqual(jkff3.q.value, False) self.assertEqual(jkff2.q.value, False) self.assertEqual(jkff1.q.value, False) self.assertEqual(jkff0.q.value, True) #~ print(jkff3.q.value, jkff2.q.value, jkff1.q.value, jkff0.q.value) jkff3.clock(half=False) jkff2.clock(half=False) jkff1.clock(half=False) jkff0.clock(half=False) n2.clock() n3.clock() self.assertEqual(jkff3.q.value, False) self.assertEqual(jkff2.q.value, False) self.assertEqual(jkff1.q.value, True) self.assertEqual(jkff0.q.value, False) #~ print(jkff3.q.value, jkff2.q.value, jkff1.q.value, jkff0.q.value) jkff3.clock(half=False) jkff2.clock(half=False) jkff1.clock(half=False) jkff0.clock(half=False) n2.clock() n3.clock() self.assertEqual(jkff3.q.value, False) self.assertEqual(jkff2.q.value, False) self.assertEqual(jkff1.q.value, True) self.assertEqual(jkff0.q.value, True) #~ print(jkff3.q.value, jkff2.q.value, jkff1.q.value, jkff0.q.value) jkff3.clock(half=False) jkff2.clock(half=False) jkff1.clock(half=False) jkff0.clock(half=False) n2.clock() n3.clock() self.assertEqual(jkff3.q.value, False) self.assertEqual(jkff2.q.value, True) self.assertEqual(jkff1.q.value, False) self.assertEqual(jkff0.q.value, False) #~ print(jkff3.q.value, jkff2.q.value, jkff1.q.value, jkff0.q.value) jkff3.clock(half=False) jkff2.clock(half=False) jkff1.clock(half=False) jkff0.clock(half=False) n2.clock() n3.clock() self.assertEqual(jkff3.q.value, False) self.assertEqual(jkff2.q.value, True) self.assertEqual(jkff1.q.value, False) self.assertEqual(jkff0.q.value, True) #~ print(jkff3.q.value, jkff2.q.value, jkff1.q.value, jkff0.q.value) def test_counter(self): c = Counter4Synch() while (c.counter!=8): c.clock() self.assertEqual(c.counter,8) if __name__=='__main__': unittest.main() -
imakin revised this gist
Feb 3, 2017 . 1 changed file with 105 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -54,11 +54,6 @@ def __bool__(self): self.clock() return self.value class AND(Device): """ Terminals: @@ -84,12 +79,12 @@ def __init__(self): self.outputs = [self.output] def clock(self): self.output.value = (self.a.value and self.b.value) self.output.clock() def __bool__(self): self.clock() return bool(self.output.value) class NAND(AND): def __init__(self): @@ -101,7 +96,7 @@ def clock(self): def __bool__(self): self.clock() return bool(self.output.value) class AND3(AND): c = None @@ -111,12 +106,16 @@ def __init__(self): self.inputs.append(self.c) def clock(self): try: bool(self.a) except TypeError as e: print("HAI", self.a, self.a.value) self.output.value = (bool(self.a) and bool(self.b) and bool(self.c)) self.output.clock() def __bool__(self): self.clock() return bool(self.output.value) class NAND3(AND3): def __init__(self): @@ -128,7 +127,7 @@ def clock(self): def __bool__(self): self.clock() return bool(self.output.value) class NOT(Device): a = None @@ -142,12 +141,12 @@ def __init__(self): self.outputs = [self.output] def clock(self): self.output.value = not (self.a.value) self.output.clock() def __bool__(self): self.output.value = not (self.a.value) return bool(self.output.value) class JKFF(Device): j = None @@ -228,15 +227,96 @@ def __init__(self): self.n6.output.add_connection(self.q) self.n7.output.add_connection(self.qn) def clock(self, half=False, half_value=None): """ clock the JKFF, if half is False or unset, will be full-clocked if half is True, will only clock to value half_value(True/False) if half_value is None, clk will be valued to its connections[0] value """ if not half: #clock start-High self.j.clock() self.k.clock() self.clk.value = True self.clk.clock() for gate in self.gates: gate.clock() self.clk.value = False self.clk.clock() for gate in self.gates: gate.clock() self.q.clock() self.qn.clock() else: if (half_value is not None): self.clk.value = half_value else: self.clk.value = ( self.clk.connections[0].value ) if self.clk.value: self.j.clock() self.k.clock() self.clk.clock() for gate in self.gates: gate.clock() if self.clk.value==False: self.q.clock() self.qn.clock() def __bool__(self): return self.q.value def __int__(self): return int(self.q.value==True) class Counter4Synch(Device): counter = 0 def __init__(self): self.counter = 0 self.jkff0 = JKFF() self.jkff1 = JKFF() self.jkff2 = JKFF() self.jkff3 = JKFF() self.n2 = AND() self.n3 = AND3() self.jkff0.clk.add_connection(self.jkff1.clk) self.jkff0.clk.add_connection(self.jkff2.clk) self.jkff0.clk.add_connection(self.jkff3.clk) self.jkff0.j.add_connection(self.jkff0.k) self.n2.a.add_connection(self.jkff0.q) self.n2.b.add_connection(self.jkff1.q) self.n3.a.add_connection(self.jkff0.q) self.n3.b.add_connection(self.jkff1.q) self.n3.c.add_connection(self.jkff2.q) self.jkff0.q.add_connection(self.jkff1.j) self.jkff0.q.add_connection(self.jkff1.k) self.n2.output.add_connection(self.jkff2.j) self.n2.output.add_connection(self.jkff2.k) self.n3.output.add_connection(self.jkff3.j) self.n3.output.add_connection(self.jkff3.k) self.jkff0.j.value = True self.jkff0.k.value = True def clock(self): self.jkff3.clock(half=False) self.jkff2.clock(half=False) self.jkff1.clock(half=False) self.jkff0.clock(half=False) self.n2.clock() self.n3.clock() self.counter = ( int(self.jkff3)<<3 | int(self.jkff2)<<2 | int(self.jkff1)<<1 | int(self.jkff0)<<0 ) -
imakin revised this gist
Feb 1, 2017 . 1 changed file with 18 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,18 +8,32 @@ def test_terminal(self): c = Terminal("c") na.a.add_connection(c) self.assertEqual(nb.a.connections,[]) def test_ff_one(self): print("test J terminal") jkff = JKFF() print("test set state") jkff.j.value = True jkff.k.value = False jkff.clock() self.assertTrue(jkff.q) self.assertFalse(jkff.qn) print("test togle state") jkff.j.value = True jkff.k.value = True jkff.clock() self.assertFalse(jkff.q) self.assertTrue(jkff.qn) jkff.clock() self.assertTrue(jkff.q) self.assertFalse(jkff.qn) jkff.clock() self.assertFalse(jkff.q) self.assertTrue(jkff.qn) if __name__=='__main__': unittest.main() -
imakin revised this gist
Feb 1, 2017 . 2 changed files with 30 additions and 45 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,6 +20,7 @@ def __init__(self, label="", connections_init = False): label is optional information connections_init is an array to be put in connections """ self.connections = [] self.label = label if connections_init: self.connections = connections_init @@ -28,19 +29,19 @@ def __init__(self, label="", connections_init = False): def add_connection(self, terminal): if terminal not in self.connections: if terminal is not self: self.connections.append(terminal) for t in terminal.connections: if not(t in self.connections): if t is not self: self.connections.append(t) if self not in terminal.connections: if terminal is not self: terminal.connections.append(self) #~ print(self.connections) @@ -84,15 +85,19 @@ def __init__(self): def clock(self): self.output.value = (self.a and self.b) self.output.clock() def __bool__(self): self.clock() return bool(self.output) class NAND(AND): def __init__(self): super().__init__() def clock(self): super().clock() self.output.value = not self.output.value self.output.clock() def __bool__(self): self.clock() @@ -107,15 +112,19 @@ def __init__(self): def clock(self): self.output.value = (self.a and self.b and self.c) self.output.clock() def __bool__(self): self.clock() return bool(self.output) class NAND3(AND3): def __init__(self): super().__init__() def clock(self): super().clock() self.output.value = not self.output.value self.output.clock() def __bool__(self): self.clock() @@ -134,6 +143,7 @@ def __init__(self): def clock(self): self.output.value = not (self.a) self.output.clock() def __bool__(self): self.output.value = not (self.a) @@ -188,13 +198,6 @@ def __init__(self): self.nt, ] self.nt.a.add_connection(self.clk) self.n0.a.add_connection(self.j) self.n0.b.add_connection(self.qn) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,42 +2,24 @@ from device import * class DeviceTest(unittest.TestCase): def test_terminal(self): na = NAND() nb = NAND() c = Terminal("c") na.a.add_connection(c) self.assertEqual(nb.a.connections,[]) print(nb.a.connections) def test_ff_one(self): print("test J terminal") jkff = JKFF() jkff.j.value = True jkff.k.value = False jkff.clock() self.assertTrue(jkff.n0.output) print(jkff.q.value) print(jkff.qn.value) if __name__=='__main__': unittest.main() -
imakin revised this gist
Feb 1, 2017 . 1 changed file with 43 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ import unittest from device import * class DeviceTest(unittest.TestCase): #~ def display(self, term): #~ for c in term.connections: #~ print(c.label) #~ print(bool(c)) #~ def test_terminal(self): #~ a = Terminal("a") #~ b = Terminal("b") #~ c = Terminal("c") #~ d = Terminal("d") #~ a.add_connection(c) #~ self.display(a) def test_ff_one(self): print("test J terminal") jkff = JKFF() jkff.j.value = True jkff.j.clock() #~ print(jkff.j.value) #~ print(jkff.n0.a.value) #~ for x in jkff.j.connections: #~ if x.label: #~ print(x.label) #~ print(jkff.j.connections) #~ def test_ff(self): #~ jkff = JKFF() #~ #~ jkff.j.value = True #~ jkff.k.value = False #~ #~ jkff.clock() #~ print(jkff.j.value) #~ self.display(jkff.q) #~ self.display(jkff.qn) if __name__=='__main__': unittest.main() -
imakin revised this gist
Feb 1, 2017 . 1 changed file with 38 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ #!python3 class Device(object): def clock(self): raise NotImplemented("unimplemented clock method") @@ -12,24 +14,36 @@ class Terminal(Device): """ connections = [] value = True label = "" def __init__(self, label="", connections_init = False): """ label is optional information connections_init is an array to be put in connections """ self.label = label if connections_init: self.connections = connections_init self.value = False def add_connection(self, terminal): if terminal not in self.connections: self.connections.append(terminal) for t in terminal.connections: if not(t in self.connections) and not(t is self): self.connections.append(t) if self not in terminal.connections: terminal.connections.append(self) print("adding") print(self) print("to") print(terminal) #~ print(self.connections) def clock(self): for c in self.connections: @@ -140,11 +154,11 @@ def __init__(self): n1 n3 n5 n7 nt """ self.j = Terminal("j") self.k = Terminal("k") self.clk = Terminal("clk") self.q = Terminal("q") self.qn = Terminal("not q") self.inputs = [self.j, self.k, self.clk] self.outputs = [self.q, self.qn] @@ -174,6 +188,13 @@ def __init__(self): self.nt, ] self.nt.a.add_connection(self.clk) print("after") print(self.nt.a) print(self.nt.a.connections) print("after") print(self.clk) print(self.clk) return self.n0.a.add_connection(self.j) self.n0.b.add_connection(self.qn) @@ -206,9 +227,13 @@ def __init__(self): def clock(self): #clock start-High self.j.clock() self.k.clock() self.clk.value = True self.clk.clock() for gate in self.gates: gate.clock() self.clk.value = False self.clk.clock() for gate in self.gates: gate.clock() -
imakin revised this gist
Jan 31, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -145,8 +145,8 @@ def __init__(self): self.clk = Terminal() self.q = Terminal() self.qn = Terminal() self.inputs = [self.j, self.k, self.clk] self.outputs = [self.q, self.qn] self.n0 = NAND3() self.n1 = NAND3() -
imakin revised this gist
Jan 31, 2017 . 1 changed file with 199 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,26 +1,214 @@ class Device(object): def clock(self): raise NotImplemented("unimplemented clock method") class Terminal(Device): """ wired object for any devices Members: connections: all connected Terminal/wire object array value: boolean value True(High/Set), False=(Low/Reset) usage: as boolean value """ connections = [] value = True def __init__(self, connections_init = False): """ connections_init is an array to be put in connections """ if connections_init: self.connections = connections_init self = False def add_connection(self, terminal): if terminal not in self.connections: self.connections.append(terminal) if self not in terminal.connections: terminal.connections.append(self) for t in terminal.connections: if t not in self.connections: self.connections.append(t) def clock(self): for c in self.connections: c.value = self.value def __bool__(self): self.clock() return self.value class Wire(Terminal): def __init_(self, connections_init = False): super().__init_(connections_init) class AND(Device): """ Terminals: a b output inputs: [a,b] outputs: [output] """ a = None b = None inputs = [] output = None outputs = [] def __init__(self): self.a = Terminal() self.b = Terminal() self.output = Terminal() self.inputs = [self.a,self.b] self.outputs = [self.output] def clock(self): self.output.value = (self.a and self.b) def __bool__(self): self.clock() return bool(self.output) class NAND(AND): def clock(self): super().clock() self.output.value = not self.output.value def __bool__(self): self.clock() return self.output.value class AND3(AND): c = None def __init__(self): super().__init__() self.c = Terminal() self.inputs.append(self.c) def clock(self): self.output.value = (self.a and self.b and self.c) def __bool__(self): self.clock() return bool(self.output) class NAND3(AND3): def clock(self): super().clock() self.output.value = not self.output.value def __bool__(self): self.clock() return self.output class NOT(Device): a = None output = None inputs = [] outputs = [] def __init__(self): self.a = Terminal() self.output = Terminal() self.inputs = [self.a] self.outputs = [self.output] def clock(self): self.output.value = not (self.a) def __bool__(self): self.output.value = not (self.a) return bool(self.output) class JKFF(Device): j = None k = None clk = None q = None qn= None inputs = [] outputs = [] def __init__(self): """ n0 n2 n4 n6 n1 n3 n5 n7 nt """ self.j = Terminal() self.k = Terminal() self.clk = Terminal() self.q = Terminal() self.qn = Terminal() inputs = [self.j, self.k, self.clk] outputs = [self.q, self.qn] self.n0 = NAND3() self.n1 = NAND3() self.n2 = NAND() self.n3 = NAND() self.n4 = NAND() self.n5 = NAND() self.n6 = NAND() self.n7 = NAND() self.nt = NOT() self.gates = [ self.n0, self.n1, self.n2, self.n3, self.n4, self.n5, self.n6, self.n7, self.nt, ] self.nt.a.add_connection(self.clk) self.n0.a.add_connection(self.j) self.n0.b.add_connection(self.qn) self.n0.c.add_connection(self.clk) self.n1.a.add_connection(self.clk) self.n1.b.add_connection(self.q) self.n1.c.add_connection(self.k) self.n2.a.add_connection(self.n0.output) self.n2.b.add_connection(self.n3.output) self.n3.a.add_connection(self.n2.output) self.n3.b.add_connection(self.n1.output) self.n4.a.add_connection(self.n2.output) self.n4.b.add_connection(self.nt.output) self.n5.a.add_connection(self.nt.output) self.n5.b.add_connection(self.n3.output) self.n6.a.add_connection(self.n4.output) self.n6.b.add_connection(self.n7.output) self.n7.a.add_connection(self.n6.output) self.n7.b.add_connection(self.n5.output) self.n6.output.add_connection(self.q) self.n7.output.add_connection(self.qn) def clock(self): #clock start-High self.clk = True for gate in self.gates: gate.clock() self.clk = False for gate in self.gates: gate.clock() -
imakin created this gist
Jan 30, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ class Terminal(object): """ wired object for any devices Members: connections: all connected wires object array value: boolean value True(High/Set), False=(Low/Reset) """ connections = [] value = True def __init__(self, connections_init = []): if connections_init: self.connections = connections_init class AND(object): """ Terminals: inputs["1"] inputs["2"] output """ inputs = { "1":Terminal(), "2":Terminal(), } output =