0
0
Verilogprogramming~20 mins

Case statement for multiplexing in Verilog - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Case Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a 4-to-1 Multiplexer Using Case Statement
What is the output y of this Verilog code when sel = 2'b10?
Verilog
module mux4to1(input [1:0] sel, input [3:0] d, output reg y);
  always @(*) begin
    case(sel)
      2'b00: y = d[0];
      2'b01: y = d[1];
      2'b10: y = d[2];
      2'b11: y = d[3];
      default: y = 1'b0;
    endcase
  end
endmodule

// Inputs: sel = 2'b10, d = 4'b1011
Ay = 1
By = 0
Cy = 1'bz (high impedance)
Dy = 2'b10
Attempts:
2 left
💡 Hint
Look at which bit of d is selected by sel = 2'b10.
Predict Output
intermediate
2:00remaining
Behavior of Case Statement with Default in Multiplexer
What will be the output y if sel = 2'b11 and d = 4'b0101 in this Verilog code?
Verilog
module mux4to1(input [1:0] sel, input [3:0] d, output reg y);
  always @(*) begin
    case(sel)
      2'b00: y = d[0];
      2'b01: y = d[1];
      2'b10: y = d[2];
      default: y = 1'b0;
    endcase
  end
endmodule
Ay = 0
By = 1
Cy = d[3]
Dy = 1'bx (unknown)
Attempts:
2 left
💡 Hint
Check what the default case does when sel is not explicitly matched.
🔧 Debug
advanced
2:00remaining
Identify the Error in Multiplexer Case Statement
What error will this Verilog code produce when synthesized or simulated?
Verilog
module mux4to1(input [1:0] sel, input [3:0] d, output reg y);
  always @(*) begin
    case(sel)
      2'b00: y = d[0];
      2'b01: y = d[1];
      2'b10: y = d[2];
      2'b11: y = d[3];
    endcase
  end
endmodule
AType mismatch error between y and d bits
BSyntax error: missing parentheses around case expression
CNo error, code works fine
DLatch inferred due to missing default case
Attempts:
2 left
💡 Hint
Recall the exact syntax for a case statement in Verilog.
📝 Syntax
advanced
2:00remaining
Syntax Error in Case Statement for Multiplexer
Which option contains the correct syntax for the case statement in Verilog?
Verilog
always @(*) begin
  case(sel)
    2'b00: y = d[0];
    2'b01 y = d[1];
    2'b10: y = d[2];
    2'b11: y = d[3];
  endcase
end
Acase(sel) 2'b00: y = d[0]; 2'b01: y = d[1]; 2'b10: y = d[2]; 2'b11: y = d[3]; endcase
Bcase sel ( 2'b00: y = d[0]; 2'b01: y = d[1]; 2'b10: y = d[2]; 2'b11: y = d[3]; ) endcase
Ccase(sel): 2'b00: y = d[0]; 2'b01: y = d[1]; 2'b10: y = d[2]; 2'b11: y = d[3]; endcase
Dcase(sel) 2'b00 y = d[0]; 2'b01 y = d[1]; 2'b10 y = d[2]; 2'b11 y = d[3]; endcase
Attempts:
2 left
💡 Hint
Check for colons after each case label and correct case statement syntax.
🚀 Application
expert
3:00remaining
Number of Output Changes in a 3-to-8 Decoder Using Case Statement
Consider a 3-to-8 decoder implemented with a case statement on a 3-bit input sel. How many output lines will be set to 1 for any single input value?
Verilog
module decoder3to8(input [2:0] sel, output reg [7:0] out);
  always @(*) begin
    out = 8'b00000000;
    case(sel)
      3'b000: out[0] = 1'b1;
      3'b001: out[1] = 1'b1;
      3'b010: out[2] = 1'b1;
      3'b011: out[3] = 1'b1;
      3'b100: out[4] = 1'b1;
      3'b101: out[5] = 1'b1;
      3'b110: out[6] = 1'b1;
      3'b111: out[7] = 1'b1;
    endcase
  end
endmodule
AAll output bits are set to 1
BTwo output bits are set to 1
CExactly one output bit is set to 1
DNo output bits are set to 1
Attempts:
2 left
💡 Hint
Look at how the output out is assigned before and inside the case statement.