// This file is part of the materials accompanying the book
// “The Elements of Computing Systems” by Nisan and Schocken,
// MIT Press. Book site: http://www.idc.ac.il/tecs
// File name: projects/02/ALU.hdl
/**
* The ALU. Computes one of the following functions:
* x+y, x-y, y뻳, 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.
* The bit-combinations that yield each function are
* documented in the book. In addition, the ALU
* computes two 1-bit outputs: if the ALU output
* is 0, zr is set to 1; otherwise zr is set to 0;
* If out<0, ng is set to 1; otherwise ng is set to 0.
*/
// Implementation: the ALU manipulates the x and y
// inputs and then 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 out = 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:
// Put your code here.
// X에 대한 조건.. zx, nx 에 대해서 처리하자…
Not16(in=x,out=notx); // x = ~x;
And16(a=x,b=notx,out=allzerox); // x= 0;
Or16(a=x,b=notx,out=allonex);
Not(in=x[0],out=notx0);
And(a=x[0],b=notx0,out=zero); // 0값
Or(a=x[0],b=notx0,out=one); //1값
Not16(in=y,out=noty); // y = ~y
And16(a=y,b=noty,out=allzeroy); // y = 0;
Mux16(a=x,b=allzerox,sel=zx,out=zxvalue); // zero the x input?
Not16(in=zxvalue,out=zxnotvalue);
Mux16(a=zxvalue,b=zxnotvalue,sel=nx,out=nxvalue); // negate the x input?
Mux16(a=y,b=allzeroy,sel=zy,out=zyvalue); // zero the y input?
Not16(in=zyvalue,out=zynotvalue);
Mux16(a=zyvalue,b=zynotvalue,sel=ny,out=nyvalue); // negate the y input?
Add16(a=nxvalue,b=nyvalue,out=addxy);
And16(a=nxvalue,b=nyvalue,out=andxy);
Mux16(a=andxy,b=addxy,sel=f,out=tout); // compute out (x+y) or (x&y)
Not16(in=tout,out=notout);
Mux16(a=tout,b=notout,sel=no, out=out, out=copyout); // negate out
And16(a=copyout,b=allonex,out[0..7]=frontout,out[8..15]=rearout);
Or8Way(in=frontout,out=zeroout1);
Or8Way(in=rearout,out=zeroout2);
Not(in=zeroout1,out=notzout1);
Not(in=zeroout2,out=notzout2);
And(a=notzout1,b=notzout2,out=zr); // 1 if out==0 , 0 otherwise
And16(a=allonex,b=copyout,out[15]=ng); // 1 if out < 0 , 0 otherwise
}
댓글 남기기