finished chapter 2
This commit is contained in:
@@ -42,5 +42,24 @@ CHIP ALU {
|
||||
ng; // 1 if (out < 0), 0 otherwise
|
||||
|
||||
PARTS:
|
||||
// Put you code here:
|
||||
|
||||
Mux16(a=x, b[0..15]=false, sel=zx, out=zxo);
|
||||
Not16(in=zxo, out=nxi);
|
||||
Mux16(a=zxo, b=nxi, sel=nx, out=nxo);
|
||||
|
||||
Mux16(a=y, b[0..15]=false, sel=zy, out=zyo);
|
||||
Not16(in=zyo, out=nyi);
|
||||
Mux16(a=zyo, b=nyi, sel=ny, out=nyo);
|
||||
|
||||
Add16(a=nxo, b=nyo, out=addo);
|
||||
And16(a=nxo, b=nyo, out=ando);
|
||||
Mux16(a=ando, b=addo, sel=f, out=opo);
|
||||
|
||||
Not16(in=opo, out=noi);
|
||||
Mux16(a=opo, b=noi, sel=no, out=out, out[15]=ng, out[0..7]=zri0, out[8..15]=zri1);
|
||||
|
||||
Or8Way(in=zri0, out=o0);
|
||||
Or8Way(in=zri1, out=o1);
|
||||
Or(a=o0, b=o1, out=nzr);
|
||||
Not(in=nzr, out=zr);
|
||||
}
|
||||
68
projects/02/ALU.hdl~
Normal file
68
projects/02/ALU.hdl~
Normal file
@@ -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:
|
||||
|
||||
Mux16(a=x, b=0, sel=zx, out=zx_o);
|
||||
Not16(in=zx_o, out=nx_i);
|
||||
Mux16(a=zx_o, b=nx_i, sel=nx, out=nx_o);
|
||||
|
||||
Mux16(a=y, b=0, sel=zy, out=zy_o);
|
||||
Not16(in=zy_o, out=ny_i);
|
||||
Mux16(a=zy_o, b=ny_i, sel=ny, out=ny_o);
|
||||
|
||||
Add16(a=nx_o, b=ny_o, sum=add_o, carry=carry);
|
||||
And16(a=nx_o, b=ny_o, out=and_o);
|
||||
Mux16(a=and_o, b=add_o, sel=f, out=op_o);
|
||||
|
||||
Not16(in=op_o, out=no_i);
|
||||
Mux16(a=op_o, b=no_i, sel=no, out=out);
|
||||
|
||||
Or8Way(a=out[0], b=out[1], c=out[2], d=out[3],
|
||||
e=out[4], f=out[5], g=out[6], h=out[7], out=zr_o0);
|
||||
Or8Way(a=out[8], b=out[9], c=out[10], d=out[11],
|
||||
e=out[12], f=out[13], g=out[14], h=out[15], out=zr_o1);
|
||||
Or(a=zr_o0, b=zr_o1, out=zr);
|
||||
|
||||
Mux(a=op_o[15], b=no_i[15], sel=no, out=ng);
|
||||
}
|
||||
37
projects/02/ALU.out
Executable file
37
projects/02/ALU.out
Executable file
@@ -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 |
|
||||
Reference in New Issue
Block a user