HDL & Verification
Why does the code above create a latch, and what is the fix?
always @(*) begin
case (sel)
2'b00: y = a;
2'b01: y = b;
2'b10: y = c;
endcase
end
- AThree inputs are used in the case; reduce them to two
- BNo value is assigned to y when sel = 2'b11; add a default clause so every case assigns a value
- Calways @(*) is used; change it to always @(posedge clk)
Show answer and explanation
Answer: B. No value is assigned to y when sel = 2'b11; add a default clause so every case assigns a value
sel is 2 bits with four possible values, but 2'b11 is not handled, so y must keep its previous value in that case, creating a latch. Specifying a value for every remaining case, such as "default: y = 0;", or assigning y a default value before the case statement yields combinational logic. Latches from incomplete conditions are a common mistake for HDL beginners.
- A. The number of inputs is not the cause.
- B. Correct. Every case must be covered.
- C. That would create flip-flops, a different circuit.