ALU
算术逻辑单元(ALU),是*处理器CPU的一部分,用以计算机指令集中的执行算术与逻辑操作。
Chip name: ALU
Inputs: x[16], y[16], // Two 16-bit data inputs
zx, // Zero the x input
nx, // Negate the x input
zy, // Zero the y input
ny, // Negate the y input
f, // Function code: 1 for Add, 0 for And
no // Negate the out output
Outputs: out[16], // 16-bit output
zr, // True iff out=0
ng // True iff out<0
Function:
if zx then x = 0 // 16-bit zero constant
if nx then x = !x // Bit-wise negation
if zy then y = 0 // 16-bit zero constant
if ny then y = !y // Bit-wise negation
if f then out = x + y // Integer 2's complement addition
else out = x & y // Bit-wise And
if no then out = !out // Bit-wise negation
if out=0 then zr = 1 else zr = 0 // 16-bit eq. comparison
if out<0 then ng = 1 else ng = 0 // 16-bit neg. comparison
Comment: Overflow is neither detected nor handled.
// 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:
// Put you code here:
// ----- Zero x (zx bit)
// If the zx bit is set, the muxer will select 16 bits of 0,
// otherwise it will select x
Mux16(a=x, b=false, sel=zx, out=zxOut);
// ----- Negate x (nx bit)
// Negate x with Not16. Then use Mux16 to select between negated x
// or the previous output (zxOut).
Not16(in=zxOut, out=notX);
Mux16(a=zxOut, b=notX, sel=nx, out=nxOut);
// ----- Zero y (zy bit)
// Exactly the same as zero x.
Mux16(a=y, b=false, sel=zy, out=zyOut);
// ----- Negate y (zy bit)
// Behaves exactly the same as Negate x.
Not16(in=zyOut, out=notY);
Mux16(a=zyOut, b=notY, sel=ny, out=nyOut);
// ----- Compute out (f bit)
// x & y
And16(a=nxOut, b=nyOut, out=andOut);
// x + y
Add16(a=nxOut, b=nyOut, out=addOut);
// Return the output that the f bit expects
Mux16(a=andOut, b=addOut, sel=f, out=fOut);
// ----- Negate output (no bit)
// Simply negate the output if this bit is set. This operation returns
// the final output.
Not16(in=fOut, out=noOut);
Mux16(a=fOut, b=noOut, sel=no, out=final);
// Need out final mux so that we can save the final output and run a few
// tests on it below.
Mux16(a=final, b=false, sel=false, out=out);
// ----- Return true if the output is zero (zr)
// Or over all 16 bits. This will return false if the number is zero,
// so flip the bit so that zr is true
Or16Way(in=final, out=zrOut);
Not(in=zrOut, out=zr);
// ----- Return true if the output is negative (ng)
// The most significant bit will be 1 if the number is negative thanks to
// 2's complement. But I abstracted all of that out into another chip.
IsNeg(in=final, out=ng);
}