Skip to content

Commit

Permalink
WIP: alu
Browse files Browse the repository at this point in the history
  • Loading branch information
mlouielu committed Jul 3, 2017
1 parent b4897d0 commit 71b65da
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions nand2vm/gate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
from .xor import Xor
from .mux import Mux, Mux16, Mux4Way16, Mux8Way16
from .adder import fa, ha, add16
from .alu import ALU
3 changes: 2 additions & 1 deletion nand2vm/gate/alu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@


def ALU(x, y, zx, nx, zy, ny, f, no) -> Tuple[List[bool], bool, bool]:
pass
# print(x, y, zx, nx, zy, ny, f, no)
return (None, None, None)
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
30 changes: 30 additions & 0 deletions test/test_alu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# 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):
@classmethod
def setUpClass(cls):
with open(os.path.join(DATA_DIRECTORY, 'ALU.cmp'), newline='') as f:
reader = csv.reader(f, delimiter='|')
Data = namedtuple('Data', next(reader))
cls.cases = [case for case in map(Data._make, reader)]

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

0 comments on commit 71b65da

Please sign in to comment.