nx=1 and zx=1?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

nx=1 and zx=1?

leal
For the ALU, what should the output of X be if both nx and zx are set? That is, should x first be negated, and then zeroed, or zeroed, and then negated? Is this behaviour specified, or have I simply overlooked it?
Reply | Threaded
Open this post in threaded view
|

Re: nx=1 and zx=1?

cadet1620
Administrator
Follow the steps in order as written in the comment in ALU.hdl

    if zx=1
        set x = 0
    if nx=1
        set x = !x
    etc.

Note that in hardware you cannot replace the signal on a wire so this is implemented as

    if zx=1
        set x1 = 0
    else
        set x1 = x
    if nx=1
        set x2 = !x1
    else
        set x2 = x1
    etc.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: nx=1 and zx=1?

leal
Thank you very much. I realize now I could have also figured that out from the truth table on the opposite page of the ALU specification, too!
Reply | Threaded
Open this post in threaded view
|

Re: nx=1 and zx=1?

cadet1620
Administrator
ybakos also wrote a worksheet for the ALU if you want to learn how the various control combinations cause the ALU to compute the specific operations.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: nx=1 and zx=1?

Dimenus
I'm having trouble understanding the 2nd and 3rd rows of that truth table. I have no idea how the builtin ALU is getting that LSB set to just one on its own.

My ALU is stuck on the following:

|        x         |        y         |zx |nx |zy |ny | f |no |       out        |zr |ng |
| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 1111111111111111 | 0 | 1 |


Reply | Threaded
Open this post in threaded view
|

Re: nx=1 and zx=1?

cadet1620
Administrator
Dimenus wrote
I'm having trouble understanding the 2nd and 3rd rows of that truth table. I have no idea how the builtin ALU is getting that LSB set to just one on its own.

My ALU is stuck on the following:

|        x         |        y         |zx |nx |zy |ny | f |no |       out        |zr |ng |
| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 1111111111111111 | 0 | 1 |
Follow the steps in ALU.hdl's comments:

// 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
    x = 0000000000000000
// if (nx == 1) set x = !x       // bitwise not
    x = 1111111111111111 (-1)
// if (zy == 1) set y = 0        // 16-bit constant
    y = 0000000000000000
// if (ny == 1) set y = !y       // bitwise not
    y = 1111111111111111 (-1)
// if (f == 1)  set out = x + y  // integer 2's complement addition
    out = 1111111111111110 (-2)
// if (f == 0)  set out = x & y  // bitwise and

// if (no == 1) set out = !out   // bitwise not
    out = 0000000000000001 (1)
// if (out == 0) set zr = 1
    zr = 0
// if (out < 0) set ng = 1
    ng = 0

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: nx=1 and zx=1?

Dimenus
Thanks Mark.

I forgot that I was on another computer that didn't have Add16 implemented. :( So it was just doing nothing. My clue was that:

   //F flag
   And16(a=x1, b=y1, out=xandy);
   Add16(a=x1, b=y1, out=xaddy);
   Mux16(a=xandy, b=xaddy, sel=f, out=xy0);


would always return 0......Once I implemented that it worked fine.