Skip to content

Commit

Permalink
Merge 4e9bcf5 into 768624e
Browse files Browse the repository at this point in the history
  • Loading branch information
mlouielu committed Jul 3, 2017
2 parents 768624e + 4e9bcf5 commit d01aff1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
2 changes: 2 additions & 0 deletions nand2vm/gate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
from .xor import Xor
from .mux import Mux, Mux16, Mux4Way16, Mux8Way16
from .adder import fa, ha, add16
from .alu import ALU
from .const import TRUE, FALSE
29 changes: 27 additions & 2 deletions nand2vm/gate/alu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,32 @@

from typing import List, Tuple
from .. import gate
from ..bitarray import BitArray


def ALU(x, y, zx, nx, zy, ny, f, no) -> Tuple[List[bool], bool, bool]:
pass
def ALU(x: BitArray, y: BitArray,
zx: bool, nx: bool, zy: bool, ny: bool,
f: bool, no: bool) -> Tuple[BitArray, bool, bool]:
negx = gate.Not16(x)
negy = gate.Not16(y)

outx = gate.Mux4Way16(x, negx, gate.FALSE, gate.TRUE, select=BitArray([zx, nx]))
outy = gate.Mux4Way16(y, negy, gate.FALSE, gate.TRUE, select=BitArray([zy, ny]))

anded = gate.And16(outx, outy)
added = gate.add16(outx, outy)
result = gate.Mux16(anded, added, f)

negresult = gate.Not16(result)
out = gate.Mux16(result, negresult, no)

out7 = out[:8]
out14 = out[8: 16]
ng = out[15]

or7 = gate.Or8Way(out7)
or14 = gate.Or8Way(out14)
or714 = gate.Or(or7, or14)
zr = gate.Not(or714)

return (out, zr, ng)
11 changes: 11 additions & 0 deletions nand2vm/gate/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Copyright (c) 2017 Louie Lu. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#

from .. import gate
from ..bitarray import BitArray

TRUE = BitArray(2 ** BitArray.DEFAULT_BITS - 1)
FALSE = BitArray(0)
37 changes: 37 additions & 0 deletions test/data/ALU.cmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
x|y|zx|nx|zy|ny|f|no|out|zr|ng
0000000000000000|1111111111111111|1|0|1|0|1|0|0000000000000000|1|0
0000000000000000|1111111111111111|1|1|1|1|1|1|0000000000000001|0|0
0000000000000000|1111111111111111|1|1|1|0|1|0|1111111111111111|0|1
0000000000000000|1111111111111111|0|0|1|1|0|0|0000000000000000|1|0
0000000000000000|1111111111111111|1|1|0|0|0|0|1111111111111111|0|1
0000000000000000|1111111111111111|0|0|1|1|0|1|1111111111111111|0|1
0000000000000000|1111111111111111|1|1|0|0|0|1|0000000000000000|1|0
0000000000000000|1111111111111111|0|0|1|1|1|1|0000000000000000|1|0
0000000000000000|1111111111111111|1|1|0|0|1|1|0000000000000001|0|0
0000000000000000|1111111111111111|0|1|1|1|1|1|0000000000000001|0|0
0000000000000000|1111111111111111|1|1|0|1|1|1|0000000000000000|1|0
0000000000000000|1111111111111111|0|0|1|1|1|0|1111111111111111|0|1
0000000000000000|1111111111111111|1|1|0|0|1|0|1111111111111110|0|1
0000000000000000|1111111111111111|0|0|0|0|1|0|1111111111111111|0|1
0000000000000000|1111111111111111|0|1|0|0|1|1|0000000000000001|0|0
0000000000000000|1111111111111111|0|0|0|1|1|1|1111111111111111|0|1
0000000000000000|1111111111111111|0|0|0|0|0|0|0000000000000000|1|0
0000000000000000|1111111111111111|0|1|0|1|0|1|1111111111111111|0|1
0000000000010001|0000000000000011|1|0|1|0|1|0|0000000000000000|1|0
0000000000010001|0000000000000011|1|1|1|1|1|1|0000000000000001|0|0
0000000000010001|0000000000000011|1|1|1|0|1|0|1111111111111111|0|1
0000000000010001|0000000000000011|0|0|1|1|0|0|0000000000010001|0|0
0000000000010001|0000000000000011|1|1|0|0|0|0|0000000000000011|0|0
0000000000010001|0000000000000011|0|0|1|1|0|1|1111111111101110|0|1
0000000000010001|0000000000000011|1|1|0|0|0|1|1111111111111100|0|1
0000000000010001|0000000000000011|0|0|1|1|1|1|1111111111101111|0|1
0000000000010001|0000000000000011|1|1|0|0|1|1|1111111111111101|0|1
0000000000010001|0000000000000011|0|1|1|1|1|1|0000000000010010|0|0
0000000000010001|0000000000000011|1|1|0|1|1|1|0000000000000100|0|0
0000000000010001|0000000000000011|0|0|1|1|1|0|0000000000010000|0|0
0000000000010001|0000000000000011|1|1|0|0|1|0|0000000000000010|0|0
0000000000010001|0000000000000011|0|0|0|0|1|0|0000000000010100|0|0
0000000000010001|0000000000000011|0|1|0|0|1|1|0000000000001110|0|0
0000000000010001|0000000000000011|0|0|0|1|1|1|1111111111110010|0|1
0000000000010001|0000000000000011|0|0|0|0|0|0|0000000000000001|0|0
0000000000010001|0000000000000011|0|1|0|1|0|1|0000000000010011|0|0
51 changes: 51 additions & 0 deletions test/test_alu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Copyright (c) 2017 Louie Lu. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#

import csv
import os
import unittest
from collections import namedtuple
from nand2vm import gate, BitArray


PACKAGE_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
DATA_DIRECTORY = os.path.join(PACKAGE_DIRECTORY, 'data')


class ALUTest(unittest.TestCase):
DATA = namedtuple('Data', ['x', 'y', 'zx', 'nx', 'zy', 'ny', 'f', 'no', 'out', 'zr', 'ng'])

@classmethod
def make_data(cls, data):
data[0] = BitArray(data[0])
data[1] = BitArray(data[1])
data[2] = bool(int(data[2]))
data[3] = bool(int(data[3]))
data[4] = bool(int(data[4]))
data[5] = bool(int(data[5]))
data[6] = bool(int(data[6]))
data[7] = bool(int(data[7]))
data[8] = BitArray(data[8])
data[9] = bool(int(data[9]))
data[10] = bool(int(data[10]))

return cls.DATA(*data)

@classmethod
def setUpClass(cls):
with open(os.path.join(DATA_DIRECTORY, 'ALU.cmp'), newline='') as f:
reader = csv.reader(f, delimiter='|')
next(reader)
cls.cases = [case for case in map(cls.make_data, reader)]

def test_alu(self):
for index, case in enumerate(self.cases):
with self.subTest(caseid=index, zr=case.zr, ng=case.ng, out=case.out):
out, zr, ng = gate.ALU(case.x, case.y, case.zx, case.nx,
case.zy, case.ny, case.f, case.no)
self.assertEqual(out, case.out)
self.assertEqual(zr, case.zr)
self.assertEqual(ng, case.ng)

0 comments on commit d01aff1

Please sign in to comment.