Metastability happens when a signal changes near a clock edge and the circuit can't decide if it's a 0 or 1. Synchronizers help avoid errors by giving the signal time to settle before using it.
Metastability and synchronizer design in Verilog
module synchronizer(
input wire async_signal,
input wire clk,
output reg sync_signal
);
reg sync_stage1;
always @(posedge clk) begin
sync_stage1 <= async_signal;
sync_signal <= sync_stage1;
end
endmoduleThis example uses two flip-flops in series to reduce metastability risk.
The signal passes through two stages before being considered stable.
module simple_sync(
input wire async_in,
input wire clk,
output reg sync_out
);
reg stage1;
always @(posedge clk) begin
stage1 <= async_in;
sync_out <= stage1;
end
endmodulemodule three_stage_sync(
input wire async_in,
input wire clk,
output reg sync_out
);
reg stage1, stage2;
always @(posedge clk) begin
stage1 <= async_in;
stage2 <= stage1;
sync_out <= stage2;
end
endmoduleThis testbench shows how an asynchronous signal is synchronized to the clock using a 2-stage synchronizer. The monitor prints the signals over time.
module testbench;
reg async_signal;
reg clk;
wire sync_signal;
synchronizer uut(
.async_signal(async_signal),
.clk(clk),
.sync_signal(sync_signal)
);
initial begin
clk = 0;
async_signal = 0;
#5 async_signal = 1; // Change async signal asynchronously
#100 $finish;
end
always #5 clk = ~clk; // 10 time units clock period
initial begin
$monitor($time, " ns: async_signal=%b, sync_signal=%b", async_signal, sync_signal);
end
endmodule
module synchronizer(
input wire async_signal,
input wire clk,
output reg sync_signal
);
reg sync_stage1;
always @(posedge clk) begin
sync_stage1 <= async_signal;
sync_signal <= sync_stage1;
end
endmoduleMetastability can't be completely eliminated but can be made very unlikely with synchronizers.
Using more stages in the synchronizer reduces risk but adds delay.
Always synchronize signals crossing clock domains to avoid errors.
Metastability happens when signals change near clock edges and cause uncertainty.
Synchronizers use multiple flip-flops to give signals time to settle.
Use synchronizers when signals come from different clocks or asynchronous sources.
Practice
Solution
Step 1: Understand metastability
Metastability occurs when signals change close to clock edges, causing uncertain outputs.Step 2: Role of synchronizers
Synchronizers use multiple flip-flops in series to give signals time to stabilize and avoid metastability effects.Final Answer:
To reduce metastability by allowing signals to settle before use -> Option BQuick Check:
Synchronizer purpose = reduce metastability [OK]
- Thinking synchronizers speed up clocks
- Confusing synchronizers with reset generators
- Assuming synchronizers convert signal types
async_in?Solution
Step 1: Identify correct synchronizer structure
A 2-stage synchronizer uses two flip-flops in series clocked by the same clock.Step 2: Check code correctness
reg sync1, sync2; always @(posedge clk) begin sync1 <= async_in; sync2 <= sync1; end correctly assignssync1first, thensync2on the clock's rising edge, matching the synchronizer design.Final Answer:
reg sync1, sync2; always @(posedge clk) begin sync1 <= async_in; sync2 <= sync1; end -> Option AQuick Check:
Two flip-flops in series = reg sync1, sync2; always @(posedge clk) begin sync1 <= async_in; sync2 <= sync1; end [OK]
- Using negedge clock instead of posedge
- Assigning signals in wrong order
- Using wires instead of regs for flip-flops
sync_out after two clock cycles if async_in changes from 0 to 1 just before the first clock edge?reg sync1, sync2; always @(posedge clk) begin sync1 <= async_in; sync2 <= sync1; end assign sync_out = sync2;
Solution
Step 1: Trace signal through synchronizer stages
At first clock edge,sync1capturesasync_in=1, butsync2still holds oldsync1value (0).Step 2: After second clock edge
At second clock edge,sync2capturessync1=1, sosync_outbecomes 1.Final Answer:
sync_out becomes 1 after two clock cycles -> Option AQuick Check:
Two flip-flop delay = output after two clocks [OK]
- Assuming output changes immediately
- Ignoring flip-flop delay stages
- Confusing sync1 and sync2 roles
reg sync1, sync2; always @(posedge clk) begin sync2 <= async_in; sync1 <= sync2; end
Solution
Step 1: Analyze assignment order
The first flip-flop should captureasync_in, then the second captures the first flip-flop's output.Step 2: Identify reversed assignments
Here,sync2capturesasync_infirst, which is incorrect;sync1should captureasync_infirst.Final Answer:
The flip-flop assignments are reversed; sync1 should capture async_in first -> Option DQuick Check:
Correct order: sync1 <= async_in; sync2 <= sync1 [OK]
- Swapping flip-flop assignment order
- Forgetting non-blocking assignments
- Assuming reset is mandatory for synchronizers
Solution
Step 1: Understand metastability risk with fast toggling signals
Fast toggling signals crossing clock domains increase metastability risk; more synchronization stages help reduce it.Step 2: Choose proper synchronizer design
A 3-stage synchronizer clocked by the receiving clock provides more settling time and reduces metastability better than fewer stages or wrong clock domains.Final Answer:
Use a 3-stage synchronizer with flip-flops clocked by the receiving clock -> Option CQuick Check:
More stages + receiving clock = better metastability reduction [OK]
- Using source clock instead of receiving clock
- Using too few synchronizer stages
- Sampling asynchronously with combinational logic
Ready to prove it?
Pass the test and this pattern moves unknown
