Advertisement
Guest User

HDL for ALU

a guest
Aug 28th, 2010
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. // This file is part of the materials accompanying the book
  2.  
  3. // "The Elements of Computing Systems" by Nisan and Schocken,
  4.  
  5. // MIT Press. Book site: www.idc.ac.il/tecs
  6.  
  7. // File name: projects/02/ALU.hdl
  8.  
  9.  
  10.  
  11. /**
  12.  
  13.  * The ALU.  Computes a pre-defined set of functions out = f(x,y)
  14.  
  15.  * where x and y are two 16-bit inputs. The function f is selected
  16.  
  17.  * by a set of 6 control bits denoted zx, nx, zy, ny, f, no.
  18.  
  19.  * The ALU operation can be described using the following pseudocode:
  20.  
  21.  *     if zx=1 set x = 0       // 16-bit zero constant
  22.  
  23.  *     if nx=1 set x = !x      // Bit-wise negation
  24.  
  25.  *     if zy=1 set y = 0       // 16-bit zero constant
  26.  
  27.  *     if ny=1 set y = !y      // Bit-wise negation
  28.  
  29.  *     if f=1  set out = x + y // Integer 2's complement addition
  30.  
  31.  *     else    set out = x & y // Bit-wise And
  32.  
  33.  *     if no=1 set out = !out  // Bit-wise negation
  34.  
  35.  *
  36.  
  37.  * In addition to computing out, the ALU computes two 1-bit outputs:
  38.  
  39.  *     if out=0 set zr = 1 else zr = 0 // 16-bit equality comparison
  40.  
  41.  *     if out<0 set ng = 1 else ng = 0 // 2's complement comparison
  42.  
  43.  */
  44.  
  45.  
  46.  
  47. CHIP ALU {
  48.  
  49.  
  50.  
  51.     IN  // 16-bit inputs:
  52.  
  53.         x[16], y[16],
  54.  
  55.         // Control bits:
  56.  
  57.         zx, // Zero the x input
  58.  
  59.         nx, // Negate the x input
  60.  
  61.         zy, // Zero the y input
  62.  
  63.         ny, // Negate the y input
  64.  
  65.         f,  // Function code: 1 for add, 0 for and
  66.  
  67.         no; // Negate the out output
  68.  
  69.  
  70.  
  71.     OUT // 16-bit output
  72.  
  73.         out[16],
  74.  
  75.  
  76.  
  77.         // ALU output flags
  78.  
  79.         zr, // 1 if out=0, 0 otherwise
  80.  
  81.         ng; // 1 if out<0, 0 otherwise
  82.  
  83.  
  84.  
  85.     PARTS:
  86.  
  87.     Not(a=zx,out=notzx);
  88.     And16(a=x,b=notzx,out=x1);
  89.     Not(a=zy,out=notzy);
  90.     And16(a=y,b=notzy,out=y1);
  91.     Not(a=nx,out=notnx);
  92.     Xor(a=x1,b=notnx,out=x2);
  93.     Not(a=ny,out=notny);
  94.     Xor(a=y1,b=notny,out=y2);
  95.     Add16(a=x2,b=y2,out=addout);
  96.     And16(a=x2,b=y2,out=andout);
  97.     Mux16(a=andout,b=addout,sel=f,out=out1);
  98.     Not(a=no,out=notno);
  99.     And16(a=out,b=notno,out=out);
  100.     Not(a=out[15],out=ng);
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement