From 768624ef00cc1187db4377f9a164ab805037de10 Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Mon, 3 Jul 2017 23:32:18 +0800 Subject: [PATCH] Add or8way gate --- nand2vm/gate/__init__.py | 2 +- nand2vm/gate/or_g.py | 13 +++++++++++++ test/test_gate.py | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/nand2vm/gate/__init__.py b/nand2vm/gate/__init__.py index 137d6d5..ecd2034 100644 --- a/nand2vm/gate/__init__.py +++ b/nand2vm/gate/__init__.py @@ -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 diff --git a/nand2vm/gate/or_g.py b/nand2vm/gate/or_g.py index 6668532..f29b7b7 100644 --- a/nand2vm/gate/or_g.py +++ b/nand2vm/gate/or_g.py @@ -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) diff --git a/test/test_gate.py b/test/test_gate.py index e590c5d..793f2e8 100644 --- a/test/test_gate.py +++ b/test/test_gate.py @@ -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):