The case statement is a decision instruction that chooses one statement for execution. The statement chosen is one with a value that matches that of the case statement.
case (expression)
expression : statement
expression {, expression} : statement
default : statement
endcase
casez (expression)
expression : statement
expression {, expression} : statement
default : statement
endcase
casex (expression)
expression : statement
expression {, expression} : statement
default : statement
endcase
The case statement starts with a case or casex or casez keyword followed by the case expression (in parenthesis) and case items or default statement. It ends with the endcase keyword. The default statement is optional and should be used only once. A case item contains a list of one or more case item expressions, separated by comma, and the case item statement. The case item expression and the case item statement should be separated by a colon.
During the evaluation of the case statement, all case item expressions are evaluated and compared in the order in which they are given. If the first case item expression matches the case expression, then the statement which is associated with that expression is executed and the execution of the case statement is terminated. If comparison fails, then the next case item expression is evaluated and compared with the case expression. If all comparisons fail and the default section is given, then its statements are executed. Otherwise none of the case items will be executed.
Both case expression and case item expressions should have the same bit length. None of the expressions are required to be a constant expression.
The case expression comparison is effective when all compared bits are identical. Therefore, special types of case statement are provided, which can contain don't-care values in the case expression and in the case item expression. These statements can be used in the same way as the case statement, but they begin with the keywords casex and casez.
The casez statement treats high-impedance (z) values as don't-care values and the casex statement treats high-impedance and unknown (x) values as don't care values. If any of the bits in the case expression or case item expression is a don't-care value then that bit position will be ignored.
The don't-care value can be also specified by the question mark (?), which is equal to z value.
Example 1
reg [1:0] address;
case (address)
2'b00 : statement1;
2'b01, 2'b10 : statement2;
default : statement3;
endcase
If the address value is 2'b00 then statement1 will be executed. Statement2 is executed when address value equals 2'b01 or 2'b10. Otherwise statement3 is executed.
Example 2
reg a;
case (a)
1'b0 : statement1;
1'b1 : statement2;
1'bx : statement3;
1'bz : statement4;
endcase
In Example 2, the statements will be executed depending on the value of the 'a' variable (if a = 1'b0 then statement1 will be executed, etc). If we assign a question mark (?) to the 'a' variable, then statement4 will be executed because the syntax concerning numbers defines the question mark as equal to the z value.
Example 3
reg a;
casez (a)
1'b0 : statement1;
1'b1 : statement2;
1'bx : statement3;
1'bz : statement4;
endcase
If value of variable 'a' is 1'b0 or 1'b1 or 1'bx then statement1, statement2 or statement3 will be executed respectively. If 'a' equals 1'bz or 1'b? then statement1 will be executed because the casez statement treats z and ? as the don't-care values. Statement4 will never be executed because only the first case item, which matches with the case expression, is executed.
Example 4
reg a;
casex (a)
1'b0 : statement1;
1'b1 : statement2;
1'bx : statement3;
1'bz : statement4;
endcase
If variable 'a' is 1'b0 or 1'b1 then statement1 and statement2 will be executed respectively. If 'a' equals 1'bx or 1'bz or 1'b? then statement1 will be executed (x, z and ? are don't care values for the casex statement). Statement3 and statement4 will never be executed.
Example 5
reg a;
case (1'b1)
a : statement1;
endcase
The case expression can be a constant expression. In Example 5, statement1 will be executed only if 'a' is equal to 1'b1.
The default statement is optional and it can appear only once.
The parenthesis (?) can appear in expressions and it is equal to high-impedance (z) value.
If don't care values are to be ignored, then the casex or casez statements should be used.
The case statement can be a group of statements in the begin - end block.
Both the case expression and case item expressions should have the same length.
The case statement can appear only within structured procedures.
The CHM file was converted to HTML by chm2web software. |