The case statement executes one of several sequences of statements, depending on the value of a single expression. The syntax follows.
case expression is
when choices =>
{ sequential_statement }
{ when choices =>
{ sequential_statement } }
end case;
Expression must evaluate to an INTEGER or an enumerated type or an array of enumerated types, such as BIT_VECTOR. Each of the choices must be in the following form.
choice { | choice}
Each choice can be either a static expression (such as 3) or a static range (such as 1 to 3). The type of choice_expression determines the type of each choice. Each value in the range of the choice_expression type must be covered by one choice.
The final choice can be others, which matches all remaining (unchosen) values in the range of the expression type. The others choice, if present, matches expression only if no other choices match.
The case statement evaluates expression and compares that value to each choice value. The statements following each when clause is evaluated only if the choice value matches the expression value.
The following restrictions are placed on choices.
The following example shows a case statement that selects one of four signal assignment statements by using an enumerated expression type. The resulting circuit is shown in the figure that follows the example.
type ENUM is (PICK_A, PICK_B, PICK_C, PICK_D);
signal VALUE: ENUM;
signal A, B, C, D, Z: BIT;
case VALUE is
when PICK_A =>
Z <= A;
when PICK_B =>
Z <= B;
when PICK_C =>
Z <= C;
when PICK_D =>
Z <= D;
end case;
Figure 6.3 Circuit for case Statement that Uses an Enumerated Type |
The example below shows a case statement again used to select one of four signal assignment statements, this time by using an integer expression type with multiple choices. The resulting circuit is shown in the figure following the example.
signal VALUE is INTEGER range 0 to 15;
signal Z1, Z2, Z3, Z4: BIT;
Z1 <= '0';
Z2 <= '0';
Z3 <= '0';
Z4 <= '0';
case VALUE is
when 0 => -- Matches 0
Z1 <= '1';
when 1 | 3 => -- Matches 1 or 3
Z2 <= '1';
when 4 to 7 | 2 => -- Matches 2, 4, 5, 6, or 7
Z3 <= '1';
when others => -- Matches remaining values,
-- 8 through 15
Z4 <= '1';
end case;
Figure 6.4 Circuit for case Statement with Integers |
The following example shows invalid case statements.
signal VALUE: INTEGER range 0 to 15;
signal OUT_1: BIT;
case VALUE is -- Must have at least one when
end case; -- clause
case VALUE is -- Values 2 to 15 are not
when 0 => -- covered by choices
OUT_1 <= '1';
when 1 =>
OUT_1 <= '0';
end case;
case VALUE is -- Choices 5 to 10 overlap
when 0 to 10 =>
OUT_1 <= '1';
when 5 to 15 =>
OUT_1 <= '0';
end case;