0
0
Verilogprogramming~20 mins

Default case importance in Verilog - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Case Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Verilog code without a default case?

Consider the following Verilog always block that uses a case statement without a default case. What will be the value of out when sel is 2'b11?

Verilog
reg [1:0] sel;
reg [3:0] out;
always @(*) begin
  case(sel)
    2'b00: out = 4'b0001;
    2'b01: out = 4'b0010;
    2'b10: out = 4'b0100;
  endcase
end
Aout retains its previous value (no change)
BSimulation error occurs
Cout is set to 4'b0000
Dout is set to 4'b1000
Attempts:
2 left
💡 Hint

Think about what happens when none of the case conditions match and there is no default.

🧠 Conceptual
intermediate
1:30remaining
Why is a default case important in combinational logic?

In Verilog, why should you always include a default case in a case statement for combinational logic?

ATo prevent unintended latches by ensuring all outputs are assigned
BTo make the code run faster in simulation
CTo reduce the number of flip-flops used in the design
DTo allow synthesis tools to ignore unused inputs
Attempts:
2 left
💡 Hint

Think about what happens if some outputs are not assigned in all cases.

🔧 Debug
advanced
2:30remaining
Identify the problem caused by missing default in this code

Given this Verilog snippet, what problem can arise during synthesis?

Verilog
reg [2:0] sel;
reg [7:0] data_out;
always @(*) begin
  case(sel)
    3'b000: data_out = 8'hAA;
    3'b001: data_out = 8'hBB;
    3'b010: data_out = 8'hCC;
    3'b011: data_out = 8'hDD;
    3'b100: data_out = 8'hEE;
  endcase
end
Adata_out will always be zero regardless of sel
BSynthesis tool will optimize data_out to a constant value
CCompilation will fail with syntax error
DUnintended latch inferred because data_out is not assigned for all sel values
Attempts:
2 left
💡 Hint

Check if data_out is assigned for all possible sel values.

📝 Syntax
advanced
2:00remaining
Which option correctly adds a default case to avoid latches?

Choose the correct way to add a default case to this case statement to avoid unintended latches.

Verilog
reg [1:0] sel;
reg [3:0] out;
always @(*) begin
  case(sel)
    2'b00: out = 4'b0001;
    2'b01: out = 4'b0010;
    2'b10: out = 4'b0100;
  endcase
end
AAdd: else: out = 4'b0000; inside the case statement
BAdd: default out = 4'b0000; inside the case statement
CAdd: default: out = 4'b0000; inside the case statement
DAdd: default: out == 4'b0000; inside the case statement
Attempts:
2 left
💡 Hint

Remember the correct syntax for a default case in Verilog case statements.

🚀 Application
expert
3:00remaining
How many latches are inferred by this code and why?

Analyze this Verilog code. How many latches will synthesis infer and why?

Verilog
reg [1:0] sel;
reg [3:0] out1, out2;
always @(*) begin
  case(sel)
    2'b00: begin
      out1 = 4'b0001;
      out2 = 4'b1111;
    end
    2'b01: begin
      out1 = 4'b0010;
      // out2 not assigned here
    end
    2'b10: begin
      // out1 not assigned here
      out2 = 4'b0101;
    end
    default: begin
      out1 = 4'b0000;
      out2 = 4'b0000;
    end
  endcase
end
AOne latch for out2 only
BTwo latches: one for out1 and one for out2 due to missing assignments in some cases
COne latch for out1 only
DNo latches because default assigns both outputs
Attempts:
2 left
💡 Hint

Check if all outputs are assigned in every case including default.