I am trying to write some simple verilog code for a comparator of two 4 bit two's complement 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 A is less than B. There is also a third input named sign, which if 0 means that the numbers are unsigned, and if 1, the numbers are signed.
So i know that two signed two's complement numbers can be compared by subtracting them, but i cannot get that to work properly in my design. Here is what i have 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 as though this should work. I concatenate the sign bit to the front of the 4 bit numbers, subtract them, and then check to see if they are greater than or equal to zero. If A-B<0, then B is less than A because they are both negative numbers.
However when i simulate this design, it is correct whenever A=B, but shows AgtB in every other case, never AltB.
Any ideas on what i am doing wrong?