Hardware Modeling Questions

Questions on designing Muxes, Decoders, and storage elements.

Beginner Level

Write code for a 2:1 Multiplexer.
assign out = (sel) ? b : a;
What is the difference between a Latch and a Flip-Flop?

Latch: Level-sensitive. Transparent when Enable is High.

Flip-Flop: Edge-sensitive. Captures data only on clock edge.

Intermediate Level

How do you infer a Latch unintentionally? Code snippet?

By not covering all conditions in an `if` or `case` statement inside a combinational block.

always @(*) begin
  if (en) out = in;
  // Missing else -> Latch inferred to hold value of 'out'
end
How do you avoid latches?

1. Always use `else` or `default` case.

2. assign default values at the top of the block.

always @(*) begin
  out = 0; // Default
  if (en) out = in;
end