RTL Design / Module 9: Lint · CDC · Timing · Area · QoR
Lesson 9.7

What not to do in RTL — bus tearing, Gray vs binary

A multi-bit bus sampled directly across clock domains can arrive as an impossible hybrid value — bits from one clock edge mixed with bits from another. This is bus tearing. Single-bit synchronizers do not fix it. You need a protocol: Gray counters, handshake, or FIFO.

Bus tearing

When a multi-bit signal changes while the receiver samples it, different bits may reflect different source cycles. The sampled value may never have existed on the source — corrupting pointers, status words, or configuration buses.

1

Why 2-FF sync is not enough for buses

Lesson 9.6 taught synchronizer chains for single control bits. Stretching that to a 32-bit counter by placing 2-FF on every bit is a classic CDC failure — each bit settles at a different time.

Bus tearing across clock domains

Binary rollover flips all bits — maximum tearing risk. Gray code changes one bit per increment.

Trap

Never write assign count_b = count_a_sync; with per-bit 2-FF on a binary counter used as a pointer or level indicator. Lint tools flag this; silicon may pass sim then fail in corner silicon.

2

Gray code — when one-bit-at-a-time saves you

Gray encoding guarantees only one bit changes per increment. If the receiver samples during a transition, the value is either the old Gray word or the new one — both valid, never a third impossible state.

BinaryGray (4-bit)Bits changed
00000000—
000100011
001000111
001100101
010001101
011101001
100011001
bin2gray.v
// Binary to Gray — one combinational xor tree
function automatic [WIDTH-1:0] bin2gray(input [WIDTH-1:0] bin);
  bin2gray = bin ^ (bin >> 1);
endfunction

// Gray pointer in write domain, sync to read domain
always @(posedge wr_clk) begin
  wr_ptr_gray <= bin2gray(wr_ptr_bin);
end
// Each bit through 2-FF in rd_clk — safe because only 1 bit changes/cycle
gray2bin.v
// Gray to binary on receive side
function automatic [WIDTH-1:0] gray2bin(input [WIDTH-1:0] gray);
  integer i;
  gray2bin[WIDTH-1] = gray[WIDTH-1];
  for (i = WIDTH-2; i >= 0; i = i - 1)
    gray2bin[i] = gray2bin[i+1] ^ gray[i];
endfunction

wire [PTR_W-1:0] rd_ptr_sync_gray /* 2-FF per bit */;
wire [PTR_W-1:0] rd_ptr_sync_bin = gray2bin(rd_ptr_sync_gray);

Gray is for monotonic counters/pointers — not arbitrary data buses. A 32-bit status register with random field updates still needs handshake or FIFO.

Gray code single bit transition

Gray does not eliminate metastability — you still need 2-FF per bit. It eliminates impossible data values.

3

Do / don't decision map

ScenarioDon'tDo
Single control pulseStretch pulse with combo logic in receiverToggle sync or pulse stretcher in source domain
FIFO read/write pointersSync binary pointers directlyGray pointers + 2-FF + full/empty math in safe domain
32-bit config registerSample bus with 2-FF per bitHandshake (req/ack) or shadow register + toggle
Streaming dataWide bus + hopeAsync FIFO (Lesson 9.5) or AXI CDC bridge
Level indicator (credit count)Binary counter cross-domainGray if monotonic; else handshake
Raw multi-bit wire✗ 2-FF each bit (binary)✗ Gray ptr✓ FIFO✓ Handshake
bad_bus_sync.v — never ship this
// ANTI-PATTERN: binary status bus "synchronized" per bit
reg [31:0] status_a;
reg [31:0] status_b_meta, status_b;

always @(posedge clk_b) begin
  status_b_meta <= status_a;   // each bit from different clk_a cycle!
  status_b      <= status_b_meta;
end
// Consumer may decode impossible mode bits → FSM escape
good_toggle_handshake.v
// Safe config update: latch in src, pulse toggle, ack in dst
always @(posedge clk_a) begin
  if (cfg_wr) begin
    cfg_shadow <= cfg_wdata;  // stable before toggle
    toggle_a   <= ~toggle_a;
  end
end
// toggle_a → 2-FF → toggle_b_sync
// Edge on toggle_b_sync means "sample cfg_shadow (held on bus or registered)"
4

FIFO pointers — Gray in practice

Async FIFOs (Lesson 6.8 overview, CDC course) cross pointers, not data buses directly. Write pointer increments in wr_clk, converts to Gray, syncs to rd_clk, converts back to binary for full/empty math.

async_fifo_gray_ptr.v
// Pointer sync — the pattern behind every async FIFO
reg [PTR_W:0] wr_ptr_bin, wr_ptr_gray;  // extra bit for full/empty

always @(posedge wr_clk)
  wr_ptr_bin <= wr_ptr_bin + (wr_en && !full);

always @(posedge wr_clk)
  wr_ptr_gray <= bin2gray(wr_ptr_bin);

// wr_ptr_gray → 2FF per bit in rd_clk → gray2bin → rd_ptr_sync
// Full when (wr_ptr_sync - rd_ptr) == DEPTH (Gray math in safe domain)
Trap

Gray pointers must be one step monotonic. Skipping values or decrementing by more than one per cycle reintroduces multi-bit transitions. Also: pointer width is ADDR_W+1 bits to distinguish full from empty.

5

Review checklist before signoff

  • Every cross-domain multi-bit path has a documented protocol (Gray, FIFO, or handshake)
  • No binary counters synchronized bit-by-bit for pointer or level semantics
  • Gray used only where count is strictly monotonic (increment/decrement one step)
  • Full/empty for FIFOs computed with Gray sync + correct MSB handling
  • Lint/CDC tool run; waivers have engineer name and technical justification
  • Simulation includes metastability injection or formal CDC where available
  • XOR-based "bus sync" is never acceptable for multi-bit data — same tearing risk
gray_full_empty.v
// Full/empty uses Gray-synced pointers — compare in same domain
wire [PTR_W:0] wr_ptr_gray_rd;  // wr_ptr synced to rd_clk via 2FF each bit
wire [PTR_W:0] rd_ptr_bin_rd = gray2bin(rd_ptr_gray);
wire [PTR_W:0] wr_ptr_bin_rd = gray2bin(wr_ptr_gray_rd);

assign empty = (rd_ptr_bin_rd == wr_ptr_bin_rd);  // same clock domain now
// Full computed symmetrically in wr_clk domain
// MSB trick: (PTR_W+1)-bit pointers disambiguate full vs empty
6

Common bus-sync anti-patterns in review

Anti-patternWhy it failsReplacement
XOR "balance" sync on busStill multi-bit, still tearsFIFO or handshake
Sample bus on slow clock without holdSource may change mid-sampleReq/ack or registered shadow
Gray on non-monotonic counterMulti-bit transitions returnHandshake only
Waive CDC_MULTI_BIT without protocolSignoff debtFix RTL or document FIFO

🎯 What to remember

  • Bus tearing = multi-bit sample mixes old and new bits — value never existed on source.
  • 2-FF per bit fixes metastability on one bit, not atomicity on a bus.
  • Gray code for monotonic pointers; handshake or FIFO for arbitrary data.
  • If you cannot name the CDC protocol on a signal, it is not ready for tapeout.
Advertisement