I came across priority encoder design and found out a new way to do it using a case statement. The only thing that is confusing is, does a case statement give priority to cases? Example:
case(1'b1)
A[3]: Y<=4'b1000;
A[2]: Y<=4'b0100;
A[1]: Y<=4'b0010;
A[0]: Y<=4'b0001;
default:Y<=4'b0000;
endcase
Here if I give A
as 1111
Y
gets 1000
i.e it gives priority to the first case statement.
Why is this so?
Yes, there is a priority, based off of the order. According to the Verilog-2001 spec, section 9.5:
I would refactor this to :
Then there is no need to worry about priority, each match is unique.
From IEEE Std 1800-2009 (IEEE STANDARD FOR SYSTEMVERILOG)
I am not sure how well supported the priority case statements are supported by synthesis tools though.