/var/log/messages

debugging with sixth sense

Zero Determination in Boolean Arithmetic

I did not think it would be possible to write like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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:
   Not16(in=x, out=notx);
   Mux4Way16(a=x, b=notx, c[0..15]=false, d[0..15]=true, sel[1]=zx, sel[0]=nx, out=inx);

   Not16(in=y, out=noty);
   Mux4Way16(a=y, b=noty, c[0..15]=false, d[0..15]=true, sel[1]=zy, sel[0]=ny, out=iny);

   Add16(a=inx, b=iny, out=addxy);
   And16(a=inx, b=iny, out=andxy);

   Mux16(a=andxy, b=addxy, sel=f, out=outtmp);
   Not16(in=outtmp, out=notout);

   Mux16(a=outtmp, b=notout, sel=no, out=out, out[15]=ng, out[0..7]=zout1, out[8..15]=zout2);

   Or8Way(in=zout1, out=zr1);
   Or8Way(in=zout2, out=zr2);
   Or(a=zr1, b=zr2, out=zr3);
   Not(in=zr3, out=zr);
}

Comments