Case statement¶
Information of case statement¶
Case statements in Verilog are nearly equivalent to a sequence of if-elseif-else that compares one expression to a list of others. Its syntax and functionality differs from the switch
statement in C.
Verilog | |
---|---|
- The case statement begins with
case
and each "case item" ends with a colon. There is no "switch". - Each case item can execute exactly one statement. This makes the "break" used in C unnecessary. But this means that if you need more than one statement, you must use
begin ... end
. - Duplicate (and partially overlapping) case items are permitted. The first one that matches is used. C does not allow duplicate case items.
Practice¶
Problem statement¶
Case statements are more convenient than if statements if there are a large number of cases. So, in this exercise, create a 6-to-1 multiplexer. When
sel
is between 0 and 5, choose the corresponding data input. Otherwise, output 0. The data inputs and outputs are all 4 bits wide.Be careful of inferring latches (See.always_if2)