HDL Bits : [Solution] Always casez

In this quiz, we are required to create an 8-bit priority encoder.
We need to use the casez statement instead of case because it allows us to use Z as a wildcard value inside the always block.

The statement default: pos = 0; means that if none of the above conditions match, the output will be assigned to zero.

Here is my code for this quiz:

 
module top_module (
input [7:0] in,
output reg [2:0] pos
);

always @(*) begin
casez (in)
8’bzzzzzzz1: pos = 0;
8’bzzzzzz1z: pos = 1;
8’bzzzzz1zz: pos = 2;
8’bzzzz1zzz: pos = 3;
8’bzzz1zzzz: pos = 4;
8’bzz1zzzzz: pos = 5;
8’bz1zzzzzz: pos = 6;
8’b1zzzzzzz: pos = 7;
default: pos = 0;
endcase
end

endmodule

 

Reference: https://hdlbits.01xz.net/wiki/Always_casez

Leave a Reply

Your email address will not be published. Required fields are marked *