0
0
Verilogprogramming~20 mins

Latch inference and how to avoid it in Verilog - Practice Problems & Coding Challenges

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

Consider the following Verilog code snippet:

module latch_example(input wire clk, input wire en, input wire d, output reg q);
  always @(posedge clk) begin
    if (en)
      q <= d;
  end
endmodule

What is the behavior of the output q?

Verilog
module latch_example(input wire clk, input wire en, input wire d, output reg q);
  always @(posedge clk) begin
    if (en)
      q <= d;
  end
endmodule
Aq updates only on clock edge when en is high; no latch inferred.
Bq holds previous value when en is low; latch inferred due to incomplete assignment.
Cq updates asynchronously with d; latch inferred.
Dq is always zero; no update occurs.
Attempts:
2 left
💡 Hint

Think about whether q is assigned in all cases inside the clocked block.

Predict Output
intermediate
2:00remaining
What error does this Verilog code cause?

Examine this Verilog code:

module latch_infer(input wire a, b, output reg y);
  always @(*) begin
    if (a)
      y = b;
  end
endmodule

What happens when this code is synthesized?

Verilog
module latch_infer(input wire a, b, output reg y);
  always @(*) begin
    if (a)
      y = b;
  end
endmodule
ANo latch inferred; y is combinational.
BA flip-flop is inferred because of the always @(*) block.
CSynthesis error due to missing else statement.
DA latch is inferred because y is not assigned in all paths.
Attempts:
2 left
💡 Hint

Check if y is assigned a value in every possible condition.

🔧 Debug
advanced
2:00remaining
Identify the latch inference in this code and fix it

Look at this Verilog code snippet:

module latch_bug(input wire a, b, output reg y);
  always @(*) begin
    if (a)
      y = b;
  end
endmodule

Which fix will prevent latch inference?

Verilog
module latch_bug(input wire a, b, output reg y);
  always @(*) begin
    if (a)
      y = b;
  end
endmodule
ARemove the if statement and assign y = b always;
BChange always @(*) to always @(posedge a);
CAdd an else clause: else y = 1'b0;
DDeclare y as wire instead of reg.
Attempts:
2 left
💡 Hint

Think about how to assign y in all cases inside the always block.

🧠 Conceptual
advanced
2:00remaining
Why does latch inference occur in combinational always blocks?

In Verilog, why does latch inference happen when using always @(*) blocks?

ABecause <code>always @(*)</code> blocks are only for sequential logic and require flip-flops.
BBecause the output variable is not assigned in all possible paths, so the synthesizer infers storage to hold the last value.
CBecause the sensitivity list is incomplete, causing synthesis errors.
DBecause the output variable is declared as wire instead of reg.
Attempts:
2 left
💡 Hint

Think about what happens if a variable is not assigned a value in every condition inside a combinational block.

🚀 Application
expert
3:00remaining
How to rewrite this code to avoid latch inference and keep intended behavior?

Given this Verilog code that infers a latch:

module latch_problem(input wire a, b, output reg y);
  always @(*) begin
    if (a)
      y = b;
  end
endmodule

Rewrite the always block to avoid latch inference but keep the same behavior: y should be b when a is true, and 0 otherwise.

A
always @(*) begin
  if (a)
    y = b;
  else
    y = 1'b0;
end
B
always @(posedge a) begin
  y &lt;= b;
end
Cassign y = a & b;
Dalways @(*) y = a ? b : 1'b0;
Attempts:
2 left
💡 Hint

Make sure y is assigned in all paths inside the always block.