feat: UNIT 2 HDL
This commit is contained in:
BIN
project-1-boolean-logic/.DS_Store
vendored
BIN
project-1-boolean-logic/.DS_Store
vendored
Binary file not shown.
BIN
project-2-boolean-arithmetic/.DS_Store
vendored
Normal file
BIN
project-2-boolean-arithmetic/.DS_Store
vendored
Normal file
Binary file not shown.
90
project-2-boolean-arithmetic/hdl/ALU.hdl
Executable file
90
project-2-boolean-arithmetic/hdl/ALU.hdl
Executable file
@@ -0,0 +1,90 @@
|
|||||||
|
// 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/2/ALU.hdl
|
||||||
|
/**
|
||||||
|
* ALU (Arithmetic Logic Unit):
|
||||||
|
* Computes out = one of the following functions:
|
||||||
|
* 0, 1, -1,
|
||||||
|
* x, y, !x, !y, -x, -y,
|
||||||
|
* x + 1, y + 1, x - 1, y - 1,
|
||||||
|
* x + y, x - y, y - x,
|
||||||
|
* x & y, x | y
|
||||||
|
* on the 16-bit inputs x, y,
|
||||||
|
* according to the input bits zx, nx, zy, ny, f, no.
|
||||||
|
* In addition, computes the two output bits:
|
||||||
|
* if (out == 0) zr = 1, else zr = 0
|
||||||
|
* if (out < 0) ng = 1, else ng = 0
|
||||||
|
*/
|
||||||
|
// Implementation: Manipulates the x and y inputs
|
||||||
|
// and operates on the resulting values, as follows:
|
||||||
|
// if (zxxx // 16-bit constant
|
||||||
|
// if (nx == 1) sets x = !x // bitwise not
|
||||||
|
// if (zy == 1) sets y = 0 // 16-bit constant
|
||||||
|
// if (ny == 1) sets y = !y // bitwise not
|
||||||
|
// if (f == 1) sets out = x + y // integer 2's complement addition
|
||||||
|
// if (f == 0) sets out = x & y // bitwise and
|
||||||
|
// if (no == 1) sets out = !out // bitwise not
|
||||||
|
|
||||||
|
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) or (out = x & y)?
|
||||||
|
no; // negate the out output?
|
||||||
|
OUT
|
||||||
|
out[16], // 16-bit output
|
||||||
|
zr, // if (out == 0) equals 1, else 0
|
||||||
|
ng; // if (out < 0) equals 1, else 0
|
||||||
|
|
||||||
|
PARTS:
|
||||||
|
|
||||||
|
|
||||||
|
// zx
|
||||||
|
Mux16(a=x, b[0..15]=false , sel=zx, out=zeroedX);
|
||||||
|
|
||||||
|
// nz
|
||||||
|
Not16(in=zeroedX, out=notX);
|
||||||
|
Mux16(a=zeroedX,b=notX,sel=nx, out=processedX);
|
||||||
|
|
||||||
|
//zy
|
||||||
|
Mux16(a=y, b[0..15]=false , sel=zy, out=zeroedY);
|
||||||
|
|
||||||
|
//ny
|
||||||
|
Not16(in=zeroedY, out=notY);
|
||||||
|
Mux16(a=zeroedY,b=notY,sel=ny, out=processedY);
|
||||||
|
|
||||||
|
// f
|
||||||
|
And16(a=processedX , b=processedY , out=andXY);
|
||||||
|
Add16(a=processedX , b=processedY , out=addXY);
|
||||||
|
Mux16(a=andXY, b=addXY,sel=f, out= fOut );
|
||||||
|
|
||||||
|
// no
|
||||||
|
Not16(in=fOut, out=notF);
|
||||||
|
Mux16(a=fOut, b=notF, sel=no,
|
||||||
|
// variable wire
|
||||||
|
out[0..7]=finalResultp1,
|
||||||
|
out[8..15]=finalResultp2,
|
||||||
|
// out
|
||||||
|
out=out,
|
||||||
|
out[15]=ng
|
||||||
|
);
|
||||||
|
//zr
|
||||||
|
Or8Way(in=finalResultp1 , out=zr8wayoutp1);
|
||||||
|
Or8Way(in=finalResultp2 , out=zr8wayoutp2);
|
||||||
|
|
||||||
|
Or(a=zr8wayoutp1, b= zr8wayoutp2, out=zrOut);
|
||||||
|
|
||||||
|
Not(in= zrOut, out= zr);
|
||||||
|
|
||||||
|
//ng
|
||||||
|
// output from Mux16 in no
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//// Replace this comment with your code.
|
||||||
|
|
||||||
|
}
|
||||||
35
project-2-boolean-arithmetic/hdl/Add16.hdl
Executable file
35
project-2-boolean-arithmetic/hdl/Add16.hdl
Executable file
@@ -0,0 +1,35 @@
|
|||||||
|
// 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/2/Add16.hdl
|
||||||
|
/**
|
||||||
|
* 16-bit adder: Adds two 16-bit two's complement values.
|
||||||
|
* The most significant carry bit is ignored.
|
||||||
|
*/
|
||||||
|
CHIP Add16 {
|
||||||
|
IN a[16], b[16];
|
||||||
|
OUT out[16];
|
||||||
|
|
||||||
|
PARTS:
|
||||||
|
|
||||||
|
HalfAdder(a=a[0] , b=b[0] , sum=out[0] , carry=ab0carry);
|
||||||
|
|
||||||
|
FullAdder(a=a[1] , b=b[1] , c=ab0carry , sum=out[1], carry=abc1cary);
|
||||||
|
FullAdder(a=a[2] , b=b[2] , c=abc1cary , sum=out[2], carry=abc2cary);
|
||||||
|
FullAdder(a=a[3] , b=b[3] , c=abc2cary , sum=out[3], carry=abc3cary);
|
||||||
|
FullAdder(a=a[4] , b=b[4] , c=abc3cary , sum=out[4], carry=abc4cary);
|
||||||
|
FullAdder(a=a[5] , b=b[5] , c=abc4cary , sum=out[5], carry=abc5cary);
|
||||||
|
FullAdder(a=a[6] , b=b[6] , c=abc5cary , sum=out[6], carry=abc6cary);
|
||||||
|
FullAdder(a=a[7] , b=b[7] , c=abc6cary , sum=out[7], carry=abc7cary);
|
||||||
|
FullAdder(a=a[8] , b=b[8] , c=abc7cary , sum=out[8], carry=abc8cary);
|
||||||
|
FullAdder(a=a[9] , b=b[9] , c=abc8cary , sum=out[9], carry=abc9cary);
|
||||||
|
FullAdder(a=a[10] , b=b[10] , c=abc9cary , sum=out[10], carry=abc10cary);
|
||||||
|
FullAdder(a=a[11] , b=b[11] , c=abc10cary , sum=out[11], carry=abc11carry);
|
||||||
|
FullAdder(a=a[12] , b=b[12] , c=abc11carry , sum=out[12], carry=abc12cary);
|
||||||
|
FullAdder(a=a[13] , b=b[13] , c=abc12cary , sum=out[13], carry=abc13cary);
|
||||||
|
FullAdder(a=a[14] , b=b[14] , c=abc13cary , sum=out[14], carry=abc14cary);
|
||||||
|
FullAdder(a=a[15] , b=b[15] , c=abc14cary , sum=out[15], carry=abc15cary);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
22
project-2-boolean-arithmetic/hdl/FullAdder.hdl
Executable file
22
project-2-boolean-arithmetic/hdl/FullAdder.hdl
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
// 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/2/FullAdder.hdl
|
||||||
|
/**
|
||||||
|
* Computes the sum of three bits.
|
||||||
|
*/
|
||||||
|
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:
|
||||||
|
|
||||||
|
// 1st half adder
|
||||||
|
HalfAdder(a=a , b=b , sum=abSum , carry=abCarry);
|
||||||
|
// 2nd half adder
|
||||||
|
HalfAdder(a=abSum , b=c , sum=sum , carry=abSumCCarry);
|
||||||
|
// Carry Or
|
||||||
|
Or(a= abCarry, b=abSumCCarry , out=carry );
|
||||||
|
|
||||||
|
}
|
||||||
20
project-2-boolean-arithmetic/hdl/HalfAdder.hdl
Executable file
20
project-2-boolean-arithmetic/hdl/HalfAdder.hdl
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
// 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/2/HalfAdder.hdl
|
||||||
|
/**
|
||||||
|
* Computes the sum of two bits.
|
||||||
|
*/
|
||||||
|
CHIP HalfAdder {
|
||||||
|
IN a, b; // 1-bit inputs
|
||||||
|
OUT sum, // Right bit of a + b
|
||||||
|
carry; // Left bit of a + b
|
||||||
|
|
||||||
|
PARTS:
|
||||||
|
|
||||||
|
// A + B Calculation
|
||||||
|
Xor(a=a,b=b,out=sum);
|
||||||
|
|
||||||
|
// The Carry
|
||||||
|
And(a=a,b=b,out=carry);
|
||||||
|
}
|
||||||
19
project-2-boolean-arithmetic/hdl/Inc16.hdl
Executable file
19
project-2-boolean-arithmetic/hdl/Inc16.hdl
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
// 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/2/Inc16.hdl
|
||||||
|
/**
|
||||||
|
* 16-bit incrementer:
|
||||||
|
* out = in + 1
|
||||||
|
*/
|
||||||
|
CHIP Inc16 {
|
||||||
|
IN in[16];
|
||||||
|
OUT out[16];
|
||||||
|
|
||||||
|
|
||||||
|
PARTS:
|
||||||
|
|
||||||
|
|
||||||
|
Add16(a=in ,b[0]=true, b[1..15]=false, out=out );
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user