Skip to content

Commit

Permalink
Chapter 2 - Boolean Arithmetic
Browse files Browse the repository at this point in the history
HDL for HalfAdder, FullAdder, Add16, Inc16, ALU circuits.
  • Loading branch information
roboryman committed Mar 11, 2019
1 parent ec68678 commit 4fda0ed
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
73 changes: 73 additions & 0 deletions 02/ALU.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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.
*
* PINS:
* + IN: x[16], y[16], zx, nx, zy, ny, f, no
* + OUT: out[16], zr, ng
*/

// 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:
// zx/nx logic
Mux16(a=x, b=false, sel=zx, out=zxLogic);
Not16(in=zxLogic, out=notZxLogic);
Mux16(a=zxLogic, b=notZxLogic, sel=nx, out=nxLogic);

// zy/ny logic
Mux16(a=y, b=false, sel=zy, out=zyLogic);
Not16(in=zyLogic, out=notZyLogic);
Mux16(a=zyLogic, b=notZyLogic, sel=ny, out=nyLogic);

// f Logic
And16(a=nxLogic, b=nyLogic, out=nxAndNyLogic);
Add16(a=nxLogic, b=nyLogic, out=nxAddNyLogic);
Mux16(a=nxAndNyLogic, b=nxAddNyLogic, sel=f, out=fLogic);

// out/ng Logic
Not16(in=fLogic, out=notFLogic);
Mux16(a=fLogic, b=notFLogic, sel=no, out=out, out[15]=ng, out[0..7]=outLogicFirstHalf, out[8..15]=outLogicSecondHalf);

// zr logic
Or8Way(in=outLogicFirstHalf, out=zrLogicFirstHalf);
Or8Way(in=outLogicSecondHalf, out=zrLogicSecondHalf);
Or(a=zrLogicFirstHalf, b=zrLogicSecondHalf, out=zrLogic);
Not(in=zrLogic, out=zr);
}
37 changes: 37 additions & 0 deletions 02/Add16.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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/Adder16.hdl

/**
* 16-Bit Adder:
* Adds two 16-bit values.
* The most significant carry bit is ignored.
*
* PINS:
* + IN: a[16], b[16]
* + OUT: out[16]
*/

CHIP Add16 {
IN a[16], b[16];
OUT out[16];

PARTS:
HalfAdder(a=a[0], b=b[0], sum=out[0], carry=carry1);
FullAdder(a=carry1, b=a[1], c=b[1], sum=out[1], carry=carry2);
FullAdder(a=carry2, b=a[2], c=b[2], sum=out[2], carry=carry3);
FullAdder(a=carry3, b=a[3], c=b[3], sum=out[3], carry=carry4);
FullAdder(a=carry4, b=a[4], c=b[4], sum=out[4], carry=carry5);
FullAdder(a=carry5, b=a[5], c=b[5], sum=out[5], carry=carry6);
FullAdder(a=carry6, b=a[6], c=b[6], sum=out[6], carry=carry7);
FullAdder(a=carry7, b=a[7], c=b[7], sum=out[7], carry=carry8);
FullAdder(a=carry8, b=a[8], c=b[8], sum=out[8], carry=carry9);
FullAdder(a=carry9, b=a[9], c=b[9], sum=out[9], carry=carry10);
FullAdder(a=carry10, b=a[10], c=b[10], sum=out[10], carry=carry11);
FullAdder(a=carry11, b=a[11], c=b[11], sum=out[11], carry=carry12);
FullAdder(a=carry12, b=a[12], c=b[12], sum=out[12], carry=carry13);
FullAdder(a=carry13, b=a[13], c=b[13], sum=out[13], carry=carry14);
FullAdder(a=carry14, b=a[14], c=b[14], sum=out[14], carry=carry15);
FullAdder(a=carry15, b=a[15], c=b[15], sum=out[15]);
}
24 changes: 24 additions & 0 deletions 02/FullAdder.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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/FullAdder.hdl

/**
* Full-Adder:
* Computes the sum of three bits.
*
* PINS:
* + IN: a, b, c
* + OUT: sum, carry
*/

CHIP FullAdder {
IN a, b, c; // 1-bit inputs
OUT sum, // Right bit of a + b + c
carry; // Left bit of a + b + c

PARTS:
HalfAdder(a=b, b=c, sum=sumBC, carry=carryBC);
HalfAdder(a=a, b=sumBC, sum=sum, carry=carryASumBC);
Or(a=carryBC, b=carryASumBC, out=carry);
}
23 changes: 23 additions & 0 deletions 02/HalfAdder.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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/HalfAdder.hdl

/**
* Half-Adder:
* Computes the sum of two bits.
*
* PINS:
* + IN: a, b
* + OUT: sum, carry
*/

CHIP HalfAdder {
IN a, b; // 1-bit inputs
OUT sum, // Right bit of a + b
carry; // Left bit of a + b

PARTS:
Xor(a=a, b=b, out=sum);
And(a=a, b=b, out=carry);
}
21 changes: 21 additions & 0 deletions 02/Inc16.hdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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/Inc16.hdl

/**
* 16-Bit Incrementer:
* out = in + 1 (arithmetic addition)
*
* PINS:
* + IN: in[16]
* + OUT: out[16]
*/

CHIP Inc16 {
IN in[16];
OUT out[16];

PARTS:
Add16(a=in, b[0]=true, out=out);
}

0 comments on commit 4fda0ed

Please sign in to comment.