John Mavrick's Garden

Search IconIcon to open search

Last updated Unknown

Status: Links: Nand2Tetris Project 2


ALU Gate

Notes

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
CHIP ALU {
    IN  
        x[16], y[16],  // 16-bit inputs        
        zx, // zero the x input?
        nx, // negate the x input?
        zy, // zero the y input?
        ny, // negate the y input?
        f,  // compute out = x + y (if 1) or x & y (if 0)
        no; // negate the out output?

    OUT 
        out[16], // 16-bit output
        zr, // 1 if (out == 0), 0 otherwise
        ng; // 1 if (out < 0),  0 otherwise

    PARTS:
	//zero x
	Mux16(a=x, sel=zx, out=x1);
	
	//negate x
	Not16(in=x1, out=xNegated);
	Mux16(a=x1, b=xNegated, sel=nx, out=x2);
	
	//zero y
	Mux16(a=y, sel=zy, out=y1);
	
	//negate y
	Not16(in=y1, out=yNegated);
	Mux16(a=y1, b=yNegated, sel=ny, out=y2);
	
	//f function
	Add16(a=x2, b=y2, out=added);
	And16(a=x2, b=y2, out=anded);
	Mux16(a=anded, b=added, sel=f, out=fresult);
	
	//negate output
	Not16(in=fresult, out=fnegated);
	Mux16(a=fresult, b=fnegated, sel=no, out=out, out[15]=firstDigit, out[0..7]=finalRight, out[8..15]=finalLeft);
	
	//zr
	Or8Way(in=finalLeft, out=zrl);
    Or8Way(in=finalRight, out=zrr);
    Or(a=zrl, b=zrr, out=nzr);
    Not(in=nzr, out=zr);
	
	//ng
	And(a=firstDigit, b=true, out=ng);
	
}

Truth Table

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


Interactive Graph