Why does synchronizing a 4-bit binary counter with two separate 2-FF synchronizers cause intermittent silicon glitches?
We have a write counter running at 200 MHz being sampled into a 66 MHz clock domain. We passed each bit of wptr[3:0] through its own two-stage flip-flop synchronizer. In simulation it works fine, but on FPGA hardware we intermittently observe invalid jump values (e.g. jumping from 7 to 0 then back to 8). What is the root cause?
// Buggy RTL: Bit-by-bit synchronization of binary counter
genvar i;
generate
for (i = 0; i < 4; i = i + 1) begin : sync_bits
(* ASYNC_REG = "TRUE" *) reg [1:0] sync_reg;
always @(posedge rclk) sync_reg <= {sync_reg[0], wptr_bin[i]};
assign rclk_wptr[i] = sync_reg[1];
end
endgenerate
The root cause is asynchronous wire skew across multi-bit transitions. When transitioning from 7 (0111) to 8 (1000), all 4 bits toggle at the same time. Because of slight differences in physical routing delays and standard-cell threshold variations, the destination clock rclk may sample bit 3 after it changed, but sample bits 2, 1, and 0 before they finished propagating!
This results in sampling intermediate phantom codes such as 1111 (15) or 0000 (0). To fix this, you must convert to reflected Gray code before crossing, ensuring that strictly 1 bit changes per clock cycle.
// Verified Fix: Convert to Gray code in source domain, then sync
wire [3:0] wptr_gray = wptr_bin ^ (wptr_bin >> 1);
// Synchronize wptr_gray, then convert back to binary in rclk domain!