Skip to content

Commit

Permalink
Add or8way gate
Browse files Browse the repository at this point in the history
  • Loading branch information
mlouielu committed Jul 3, 2017
1 parent b4897d0 commit 768624e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nand2vm/gate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .nand import Nand
from .and_g import And, And16
from .not_g import Not, Not16
from .or_g import Or, Or16
from .or_g import Or, Or16, Or8Way
from .xor import Xor
from .mux import Mux, Mux16, Mux4Way16, Mux8Way16
from .adder import fa, ha, add16
13 changes: 13 additions & 0 deletions nand2vm/gate/or_g.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,16 @@ def Or16(a: List[bool], b: List[bool]) -> List[bool]:
gate.Or(a[14], b[14]),
gate.Or(a[15], b[15])
], endian=False)


def Or8Way(a: List[bool]) -> bool:
assert len(a) == 8

a2 = gate.Or(a[0], a[1])
a4 = gate.Or(a[2], a[3])
a6 = gate.Or(a[4], a[5])
a8 = gate.Or(a[6], a[7])
a24 = gate.Or(a2, a4)
a68 = gate.Or(a6, a8)

return gate.Or(a24, a68)
13 changes: 13 additions & 0 deletions test/test_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ def test_not_16_gate(self):
self.assertEqual(gate.Not16(a), ~a)
self.assertEqual(gate.Not16(a), r)

def test_or8way_gate(self):
a = BitArray('00001000')
self.assertEqual(gate.Or8Way(a), True)

a = BitArray('00000000')
self.assertEqual(gate.Or8Way(a), False)

a = BitArray('11111111')
self.assertEqual(gate.Or8Way(a), True)

a = BitArray('10101100')
self.assertEqual(gate.Or8Way(a), True)


class AdderTest(unittest.TestCase):
def test_half_adder(self):
Expand Down

0 comments on commit 768624e

Please sign in to comment.