HDL & Verification

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

always @(posedge clk) begin
    a <= b;
    b <= a;
end
  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.
Practice this topic Back to topic

More HDL & Verification questions

  1. Intending a two-stage shift register, someone wrote the code above with blocking assignments (=). What goes wrong?
  2. What is the common guideline for choosing assignment types in Verilog?
  3. What does writing "always @(*)" mean in Verilog?
  4. Intending combinational logic, someone wrote the code above. What problem can occur?
  5. Intending combinational logic, someone writes the code above. What does logic synthesis produce?