-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
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) |
This file contains 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 characters
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 |
This file contains 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 characters
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) |