Skip to content

Commit

Permalink
[Project 2] Implement ALU
Browse files Browse the repository at this point in the history
  • Loading branch information
indragiek committed Dec 28, 2014
1 parent c686c9c commit ceaa9c0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
68 changes: 68 additions & 0 deletions Project2/ALU.hdl
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);
}
18 changes: 18 additions & 0 deletions Project2/ZeroNeg16.hdl
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);
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

These are my projects for the book [*The Elements of Computing Systems*](http://www.nand2tetris.org) by Noam Nisan and Shimon Schocken.

* **Project 1** — Implementing basic logic gates tested with the supplied HDL simulator
* **Project 1** — Primitive and composite logic gates
* **Project 2** — Adder circuits and ALU

0 comments on commit ceaa9c0

Please sign in to comment.