forked from indragiek/tecs
-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
88 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// This file is part of www.nand2tetris.org | ||
// and the book "The Elements of Computing Systems" | ||
// by Nisan and Schocken, MIT Press. | ||
// File name: projects/02/ALU.hdl | ||
|
||
/** | ||
* The ALU (Arithmetic Logic Unit). | ||
* Computes one of the following functions: | ||
* x+y, x-y, y-x, 0, 1, -1, x, y, -x, -y, !x, !y, | ||
* x+1, y+1, x-1, y-1, x&y, x|y on two 16-bit inputs, | ||
* according to 6 input bits denoted zx,nx,zy,ny,f,no. | ||
* In addition, the ALU computes two 1-bit outputs: | ||
* if the ALU output == 0, zr is set to 1; otherwise zr is set to 0; | ||
* if the ALU output < 0, ng is set to 1; otherwise ng is set to 0. | ||
*/ | ||
|
||
// Implementation: the ALU logic manipulates the x and y inputs | ||
// and operates on the resulting values, as follows: | ||
// if (zx == 1) set x = 0 // 16-bit constant | ||
// if (nx == 1) set x = !x // bitwise not | ||
// if (zy == 1) set y = 0 // 16-bit constant | ||
// if (ny == 1) set y = !y // bitwise not | ||
// if (f == 1) set out = x + y // integer 2's complement addition | ||
// if (f == 0) set out = x & y // bitwise and | ||
// if (no == 1) set out = !out // bitwise not | ||
// if (out == 0) set zr = 1 | ||
// if (out < 0) set ng = 1 | ||
|
||
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: | ||
// Process x and y inputs based on the negative and zero control bits. | ||
ZeroNeg16(in=x, z=zx, n=nx, out=vx); | ||
ZeroNeg16(in=y, z=zy, n=ny, out=vy); | ||
|
||
// Chips for the arithemtic operations | ||
And16(a=vx, b=vy, out=andin); | ||
Add16(a=vx, b=vy, out=addin); | ||
|
||
// Choose between and and add based on the selection bit f | ||
Mux16(a=andin, b=addin, sel=f, out=fout); | ||
|
||
// Process the output based on the no control bit | ||
ZeroNeg16(in=fout, z=false, n=no, out=out, out[0..7]=lout, out[8..15]=mout, out[15]=sout); | ||
|
||
// Combine 2 8-bit ORs to check if the 16-bit out value is 0. | ||
Or8Way(in=lout, out=orl); | ||
Or8Way(in=mout, out=orm); | ||
Or(a=orl, b=orm, out=nzr); | ||
Not(in=nzr, out=zr); | ||
|
||
// Check if the sign bit (MSB) of the output value is 1 to determine whether | ||
// it is negative. | ||
And(a=sout, b=true, out=ng); | ||
} |
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,18 @@ | ||
// Created by Indragie Karunaratne | ||
|
||
/** | ||
* Manipulates a 16-bit input based on control inputs. | ||
* if (z == 1) set out = 0 | ||
* else set out = in | ||
* if (n == 1) set out = !out | ||
*/ | ||
|
||
CHIP ZeroNeg16 { | ||
IN in[16], z, n; | ||
OUT out[16]; | ||
|
||
PARTS: | ||
Mux16(a=in, b=false, sel=z, out=zin); | ||
Not16(in=zin, out=nin); | ||
Mux16(a=zin, b=nin, sel=n, out=out); | ||
} |
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