I am trying to write some simple verilog code for a comparator of two four-bit two add-on numbers. I have two 4-bit inputs (A [3: 0], B [3: 0]) and 3 outputs (AeqB, AgtB, AltB) to show if A and B are equal if A is greater than B, or less B. There is also a third input character with a name, which, if 0 means unsigned numbers, and if 1, numbers are signed.
So, I know that the two signed two add-on numbers can be compared by subtracting them, but I cannot get this to work properly in my design. Here is what I tried:
if(sign==0)
begin
if(({sign,A}-{sign,B})==0)
AeqB = 1;
else if(({sign,A}-{sign,B}) > 0)
AgtB = 1;
else if (({sign,A}-{sign,B}) < 0
AltB = 1;
end
It seems like this should work. I concatenate the sign bit in front of the four bit numbers, subtract them, and then check to see if they are greater than or equal to zero. If AB <0, then B is less than A, since they are both negative numbers.
However, when I simulate this construct, it is correct whenever A = B, but shows AgtB in every other case, never AltB.
Any ideas on what I'm doing wrong?
source
share