Keywords: case, selector
The case statement is a form of multi-way conditional branching, which can solve the inconvenience of using an if statement when there are multiple conditional options.
case statement
The format of the case statement is as follows:
case(case_expr)
condition1 : true_statement1 ;
condition2 : true_statement2 ;
……
default : default_statement ;
endcase
When the case statement is executed, if condition1 is true, execute true_statement1; if condition1 is false and condition2 is true, execute true_statement2; and so on. If none of the conditions is true, execute the default_statement.
The default statement is optional, and there cannot be multiple default statements in one case statement.
There can be multiple conditional options, not limited to condition1, condition2, etc., and these conditional options are not required to be mutually exclusive. Although these conditional options are compared concurrently, the execution effect is that the one that comes first and whose condition is true is executed.
Execution statements such as ture_statement1 can be a single statement or multiple statements. If there are multiple execution statements, they need to be enclosed with the begin and end keywords.
case statements support nested usage.
In the following, a case statement is used instead of an if statement to implement the function of a 4-way selector. The simulation results and testbench can be referred toConditional Statementschapter, and the two are exactly the same.
Example
input [1:0] sel ,
input [1:0] p0 ,
input [1:0] p1 ,
input [1:0] p2 ,
input [1:0] p3 ,
output [1:0] sout);
reg [1:0] sout_t ;
always @(*)
case(sel)
2'b00: begin
sout_t = p0 ;
end
2'b01: sout_t = p1 ;
2'b10: sout_t = p2 ;
default: sout_t = p3 ;
endcase
assign sout = sout_t ;
endmodule
The conditional option expressions in a case statement do not have to be constants; they can also be x values or z values.
When multiple conditional options need to execute the same statement, the multiple conditional options can be separated by commas and placed in the same statement block's alternatives.
However, the comparison logic of x or z in a case statement is not synthesizable, so it is generally not recommended to use x or z as comparison values in a case statement.
For example, extend the case statement of the 4-way selector, as follows:
Example
2'b00: sout_t = p0 ;
2'b01: sout_t = p1 ;
2'b10: sout_t = p2 ;
2'b11: sout_t = p3 ;
2'bx0, 2'bx1, 2'bxz, 2'bxx, 2'b0x, 2'b1x, 2'bzx :
sout_t = 2'bxx ;
2'bz0, 2'bz1, 2'bzz, 2'b0z, 2'b1z :
sout_t = 2'bzz ;
default: $display("Unexpected input control!!!");
endcase
casex/casez statements
The casex and casez statements are variants of the case statement, used to represent don't-care terms in conditional options.
casex uses "x" to represent don't-care values, and casez uses the question mark "?" to represent don't-care values.
The functions implemented by the two are exactly the same, and the syntax is also exactly the same as the case statement.
However, casex and casez are generally not synthesizable and are mostly used in simulation.
For example, use a casez statement to implement a 4-way selector with a 4-bit control input.
Example
input [3:0] sel ,
input [1:0] p0 ,
input [1:0] p1 ,
input [1:0] p2 ,
input [1:0] p3 ,
output [1:0] sout);
reg [1:0] sout_t ;
always @(*)
casez(sel)
4'b???1: sout_t = p0 ;
4'b??1?: sout_t = p1 ;
4'b?1??: sout_t = p2 ;
4'b1???: sout_t = p3 ;
default: sout_t = 2'b0 ;
endcase
assign sout = sout_t ;
endmodule
Source code download
Download