Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This file is part of the materials accompanying the book
- // "The Elements of Computing Systems" by Nisan and Schocken,
- // MIT Press. Book site: www.idc.ac.il/tecs
- // File name: projects/02/ALU.hdl
- /**
- * The ALU. Computes a pre-defined set of functions out = f(x,y)
- * where x and y are two 16-bit inputs. The function f is selected
- * by a set of 6 control bits denoted zx, nx, zy, ny, f, no.
- * The ALU operation can be described using the following pseudocode:
- * if zx=1 set x = 0 // 16-bit zero constant
- * if nx=1 set x = !x // Bit-wise negation
- * if zy=1 set y = 0 // 16-bit zero constant
- * if ny=1 set y = !y // Bit-wise negation
- * if f=1 set out = x + y // Integer 2's complement addition
- * else set out = x & y // Bit-wise And
- * if no=1 set out = !out // Bit-wise negation
- *
- * In addition to computing out, the ALU computes two 1-bit outputs:
- * if out=0 set zr = 1 else zr = 0 // 16-bit equality comparison
- * if out<0 set ng = 1 else ng = 0 // 2's complement comparison
- */
- CHIP ALU {
- IN // 16-bit inputs:
- x[16], y[16],
- // Control bits:
- 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
- OUT // 16-bit output
- out[16],
- // ALU output flags
- zr, // 1 if out=0, 0 otherwise
- ng; // 1 if out<0, 0 otherwise
- PARTS:
- Not(a=zx,out=notzx);
- And16(a=x,b=notzx,out=x1);
- Not(a=zy,out=notzy);
- And16(a=y,b=notzy,out=y1);
- Not(a=nx,out=notnx);
- Xor(a=x1,b=notnx,out=x2);
- Not(a=ny,out=notny);
- Xor(a=y1,b=notny,out=y2);
- Add16(a=x2,b=y2,out=addout);
- And16(a=x2,b=y2,out=andout);
- Mux16(a=andout,b=addout,sel=f,out=out1);
- Not(a=no,out=notno);
- And16(a=out,b=notno,out=out);
- Not(a=out[15],out=ng);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement