A case statement helps choose one option from many based on a value. It makes selecting signals easy in hardware design.
Case statement for multiplexing in Verilog
case (selector) value1: output = input1; value2: output = input2; ... default: output = default_value; endcase
The selector is the signal that decides which input to pick.
Always include a default case to handle unexpected values.
sel.case (sel) 2'b00: out = in0; 2'b01: out = in1; 2'b10: out = in2; 2'b11: out = in3; default: out = 1'b0; endcase
case (control) 3'd0: data_out = data0; 3'd1: data_out = data1; 3'd2: data_out = data2; default: data_out = 8'b00000000; endcase
This is a 4-to-1 multiplexer using a case statement. The selector sel picks which input signal goes to the output out. The testbench changes sel and prints the output.
module mux4to1( input wire [1:0] sel, input wire in0, in1, in2, in3, output reg out ); always @(*) begin case (sel) 2'b00: out = in0; 2'b01: out = in1; 2'b10: out = in2; 2'b11: out = in3; default: out = 1'b0; endcase end endmodule // Testbench module testbench; reg [1:0] sel; reg in0, in1, in2, in3; wire out; mux4to1 mux(.sel(sel), .in0(in0), .in1(in1), .in2(in2), .in3(in3), .out(out)); initial begin in0 = 0; in1 = 1; in2 = 0; in3 = 1; sel = 2'b00; #10 $display("sel=%b out=%b", sel, out); sel = 2'b01; #10 $display("sel=%b out=%b", sel, out); sel = 2'b10; #10 $display("sel=%b out=%b", sel, out); sel = 2'b11; #10 $display("sel=%b out=%b", sel, out); end endmodule
Case statements in Verilog are great for multiplexers because they clearly show all options.
Remember to use always @(*) for combinational logic to avoid latches.
Including a default case prevents unintended behavior if the selector has an unexpected value.
Case statements help pick one input from many based on a selector.
They make multiplexer design simple and clear.
Always include a default case and use always @(*) for combinational logic.
Practice
What is the main purpose of using a case statement in Verilog for multiplexing?
Solution
Step 1: Understand multiplexing concept
Multiplexing means choosing one input out of many based on a selector signal.Step 2: Role of case statement
Thecasestatement in Verilog is used to pick one input depending on the selector value.Final Answer:
To select one input from many based on a selector signal -> Option DQuick Check:
Multiplexer selection = case statement use [OK]
- Confusing case with loops
- Using case for variable declaration
- Thinking case defines timing
Which of the following is the correct syntax to start a case statement for a 2-bit selector sel in Verilog?
?
Solution
Step 1: Recall Verilog case syntax
The correct syntax to start a case statement iscase(expression)without extra keywords or braces.Step 2: Check options
case(sel) matches the correct syntax exactly:case(sel).Final Answer:
case(sel) -> Option CQuick Check:
Verilog case syntax = case(expression) [OK]
- Adding 'begin' after case
- Using braces {} like other languages
- Using 'switch' instead of 'case'
Given the following Verilog code, what is the value of out when sel = 2'b10?
module mux_example(input [1:0] sel, input a, b, c, d, output reg out);
always @(*) begin
case(sel)
2'b00: out = a;
2'b01: out = b;
2'b10: out = c;
default: out = d;
endcase
end
endmoduleSolution
Step 1: Identify selector value
The selectorselis2'b10, which matches the third case.Step 2: Match case to output
For2'b10, the outputoutis assigned the value of inputc.Final Answer:
Value of input c -> Option BQuick Check:
sel=2'b10 selects c [OK]
- Choosing default instead of matching case
- Mixing up input variables
- Ignoring binary selector format
Identify the error in this Verilog case statement used for multiplexing:
always @(*) begin
case(sel)
2'b00: out = a;
2'b01: out = b;
2'b10: out = c;
endcase
endSolution
Step 1: Check case completeness
The case statement covers 2'b00, 2'b01, and 2'b10 but misses 2'b11.Step 2: Importance of default case
Without adefaultcase, the outputoutmay hold previous value or be undefined for unmatched selectors.Final Answer:
Missing default case for unmatched selector values -> Option AQuick Check:
Always include default in case [OK]
- Forgetting default case
- Using wrong sensitivity list (@posedge instead of @(*) )
- Using '=' instead of '<=' in combinational logic
You want to design a 4-to-1 multiplexer using a case statement in Verilog. Which of the following code snippets correctly implements this with inputs a, b, c, d, selector sel, and output out?
?
Solution
Step 1: Check sensitivity list
always @(*) begin case(sel) 2'b00: out = a; 2'b01: out = b; 2'b10: out = c; 2'b11: out = d; default: out = 1'b0; endcase end usesalways @(*), which is correct for combinational logic multiplexers.Step 2: Verify case labels and default
always @(*) begin case(sel) 2'b00: out = a; 2'b01: out = b; 2'b10: out = c; 2'b11: out = d; default: out = 1'b0; endcase end covers all 4 selector values (2'b00 to 2'b11) and includes a default case to avoid latches.Step 3: Compare other options
always @(sel) begin case(sel) 2'b00: out = a; 2'b01: out = b; 2'b10: out = c; default: out = d; endcase end misses 2'b11 case explicitly and usesalways @(sel)which is less recommended. always @(*) begin if(sel == 2'b00) out = a; else if(sel == 2'b01) out = b; else if(sel == 2'b10) out = c; else out = d; end uses if-else, not case. always @(*) begin case(sel) 0: out = a; 1: out = b; 2: out = c; 3: out = d; endcase end uses decimal labels without binary format and misses default.Final Answer:
Option A code snippet -> Option AQuick Check:
Complete case with default and always @(*) = always @(*) begin case(sel) 2'b00: out = a; 2'b01: out = b; 2'b10: out = c; 2'b11: out = d; default: out = 1'b0; endcase end [OK]
- Omitting default case
- Using incomplete case labels
- Wrong sensitivity list
- Mixing if-else with case question
Ready to prove it?
Pass the test and this pattern moves unknown
