casez, and Why Not casex
Standard case statements require exact bit-pattern matches. Decoding partial bit fields or priority request vectors requires don’t-care wildcard matching. Verilog provides two wildcard constructs: casez and casex. This article explains how to use casez safely and why industry standards forbid casex.
Wildcard Matching with casez
In a casez statement, the character ? (and high-impedance z) represents a “don’t-care” condition that matches either 0 or 1:
always @(*) begin
casez (req)
4'b1???: grant = 2'd3; // Matches any pattern with bit 3 asserted
4'b01??: grant = 2'd2; // Matches bit 2 asserted when bit 3 is low
4'b001?: grant = 2'd1; // Matches bit 1 asserted
4'b0001: grant = 2'd0; // Matches bit 0 asserted
default: grant = 2'd0;
endcase
end
The casez construct implements a priority encoder cleanly. The synthesis tool optimizes the wildcard patterns into a minimal logic gate netlist.
The Simulation Hazard of casex
The casex keyword treats unknown states (x) as wildcards in addition to z and ?. This behavior causes severe simulation-synthesis mismatches.
During simulation initialization before reset deassertion, uninitialized registers hold x. Under casex, an x bit matches any branch that contains an x or ?. The simulator executes the first matching branch, masking the unknown state. In physical silicon, uninitialized registers resolve to 0 or 1, taking an unverified hardware path.
| Construct | Treatment of ? and z |
Treatment of x |
Industry Usage Rule |
|---|---|---|---|
case |
Exact comparison | Exact comparison | Standard synthesizable matching |
casez |
Don’t-care wildcard | Exact comparison | Standard wildcard decoding |
casex |
Don’t-care wildcard | Don’t-care wildcard | Prohibited (masks bugs) |
Never use casex in RTL code. Design style guides and static linters reject casex.
Instruction Opcode Decoding Example
Microprocessors use casez to decode instruction fields where specific sub-bits determine instruction format:
always @(*) begin
casez (instr[6:0])
7'b0110011: instr_type = R_TYPE;
7'b0010011: instr_type = I_TYPE;
7'b0000011: instr_type = LOAD;
7'b0100011: instr_type = STORE;
7'b1100011: instr_type = BRANCH;
default: instr_type = ILLEGAL;
endcase
end
Using casez preserves strict evaluation of unknown values during reset. An uninitialized x input will not match opcode branches, forcing the default branch to assert ILLEGAL.
Practice Exercise
Refactor the following defective casex block to standard casez. Explain how both constructs evaluate during time zero when req equals 4'bxxxx:
always @(*) begin
casex (req)
4'b1xxx: grant = 2'd3;
4'b01xx: grant = 2'd2;
4'b001x: grant = 2'd1;
4'b0001: grant = 2'd0;
default: grant = 2'd0;
endcase
end
Solution
Refactored implementation using casez and ? wildcards:
always @(*) begin
casez (req)
4'b1???: grant = 2'd3;
4'b01??: grant = 2'd2;
4'b001?: grant = 2'd1;
4'b0001: grant = 2'd0;
default: grant = 2'd0;
endcase
end
- Under
casex: Whenreqequals4'bxxxx, bit 3 isx. Becausecasextreatsxas a wildcard, the first branch4'b1xxxmatches immediately. Outputgrantbecomes 3, erroneously claiming an active request on channel 3. - Under
casez: Unknownxbits do not match1or0. No branch matches4'bxxxx. The simulator executes thedefaultbranch, drivinggrant = 0.
The next article examines synthesis pragmas full_case and parallel_case and explains why modern coding guidelines avoid them.