What is the output of the following Verilog code snippet after one clock cycle?
reg [3:0] a, b; always @(posedge clk) begin a <= b; b <= a + 1; end
reg [3:0] a = 0, b = 0; always @(posedge clk) begin a <= b; b <= a + 1; end // After one positive clock edge, what are the values of a and b?
Remember that non-blocking assignments update all variables simultaneously after the always block finishes.
Non-blocking assignments (<=) schedule updates to happen at the end of the time step. So, 'a' gets the old value of 'b' (which is 0), and 'b' gets the old value of 'a' plus 1 (0 + 1 = 1). Thus, after the clock edge, a=0 and b=1.
Which reason best explains why non-blocking assignments (<=) are preferred in sequential always blocks triggered by clock edges?
Think about how multiple assignments inside a clocked block should behave simultaneously.
Non-blocking assignments schedule all updates to happen simultaneously after evaluating the right-hand sides. This prevents race conditions and ensures correct sequential logic behavior.
Consider this Verilog code inside a clocked always block:
always @(posedge clk) begin a = b; b = a + 1; end
What problem will this cause?
Think about how blocking assignments execute in order inside an always block.
Blocking assignments (=) update variables immediately. So 'a = b;' updates 'a' before 'b = a + 1;' executes, causing 'b' to use the new 'a' value, not the old one. This creates unintended behavior and race conditions.
Which of the following always blocks correctly uses non-blocking assignments for sequential logic?
Non-blocking assignments are used in clocked always blocks, not combinational ones.
Option A uses non-blocking assignments (<=) inside a clocked always block (posedge clk), which is the correct style for sequential logic. Options C and D use combinational always blocks (@(*)). Option A uses blocking assignments (=) in a clocked block, which is discouraged.
Given the following Verilog code, what are the values of a and b after 3 positive clock edges?
reg [3:0] a = 0, b = 0; always @(posedge clk) begin a <= b; b <= a + 1; end
Trace the values step-by-step, remembering non-blocking assignments update simultaneously after each clock.
Cycle 1: a=0, b=1
Cycle 2: a=1, b=2
Cycle 3: a=2, b=3
Each cycle, 'a' takes previous 'b', and 'b' takes previous 'a' + 1.