HDL & Verification

always @(posedge clk) begin
    a <= b;
    b <= a;
end

In the code above, a = 0 and b = 1 just before a rising clock edge. What are a and b after the edge?

  1. Aa = 1, b = 0 (they swap)
  2. Ba = 1, b = 1
  3. Ca = 0, b = 0
Show answer and explanation

Answer: A. a = 1, b = 0 (they swap)

With nonblocking assignment, both right sides (b's old value 1 and a's old value 0) are evaluated first, then a gets 1 and b gets 0 simultaneously, so they swap. As a circuit, the two flip-flops' outputs are wired to each other's inputs. With blocking assignment (=), line 1 sets a to 1, and line 2 gives b the new a, 1, so both end up as 1.

  • A. Correct. Both right sides use the old values.
  • B. That is the result with "=".
  • C. No value is lost.