Cannot pass ALU test : 2's complement issue ?

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

Cannot pass ALU test : 2's complement issue ?

thibaut
This post was updated on .
Hello,
I spent quite a long time trying to build my ALU, and after a few problems I can't seem to be able to fix this one... I get comparison errors in multiple tests. I then tried multiple codes available on gitHubs etc.. here is one of those that was exactly the same as mine (I mixed my files and cannot find my own anymore) :

PARTS:
    // Design details deleted.
}

My out file :

|        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 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 1111111111111111 | 0 | 1 |

The cmp lines corresponding :

|        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 | 0000000000000001 | 0 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 |

I had the feeling that my issue came from negative additions. I (believe that I) fully understand the concept of 2's complement. It looks like my first three lines, using the f as an "Add", give me an opposite (in the mathematical way) of what I should get. Whereas, when I use the "and" function, in line 4 and 5, I get the right result.
Thank you for any clue or any answer :)


ADMIN EDIT: Details of design removed once issue resolved.
Reply | Threaded
Open this post in threaded view
|

Re: Cannot pass ALU test : 2's complement issue ?

WBahn
Administrator
Let's just focus on the first comparison fail, which is the second line.

All of the control signals are 1 which, looking at the table of instructions, means that the output should be 1 regardless of the x or y inputs. Your ALU is putting out -1.

The first step is to walk the logic of the ALU and see if you agree that it should output 1.

Since both zx and zy are 1, the x and y inputs are both replaced with all zeroes.

Since both nx and ny are 1, these zeroes are flipped to back the inputs to the add/and circuits all ones.

Since f is 1 the function selected is addition. Adding 111...111 to 111...111 results in 111...110.

Since no is 1 the output of is flipped, yielding 000...001, which is the desired output.

Now go through your logic and follow this same progression and see where your design goes off the path.
Reply | Threaded
Open this post in threaded view
|

Re: Cannot pass ALU test : 2's complement issue ?

thibaut
Thank you,
First, I paid very close attention to every part of the comparison, as to see where my ALU was off, as you said. I then realized the problem was arround my "Add16" function.
So, it actually was not off anywhere : in fact, I had created my "Add16.hdl" file in another directory, leaving the empty one inside the directory "projects/02". The software tried to use it instead of the BuiltIn one, therefore returning "0" for any "Add16" written in the program.
I took the bad file away and everything goes smooth.
I explain all this in case someone falls in the same trap as I did, sorry for the chitchat.
Thanks a lot WBahn :)
Reply | Threaded
Open this post in threaded view
|

Re: Cannot pass ALU test : 2's complement issue ?

WBahn
Administrator
Glad you found it!

I've removed the details of your implementation from your initial post since the issue appears resolved.