Codiode

Synchronizing Read Pointer to Write Domain

MediumVerilog / SystemVerilogBuild

The full flag lives in the write clock domain, but it depends on the read pointer. The Gray-coded read pointer has to pass through a synchronizer before the write side can compare against it.

This module synchronizes a Gray-coded read pointer into the write clock domain using a standard two stage flip-flop synchronizer. The input pointer is captured on the positive edge of the write clock and passed through two sequential registers. Because Gray code guarantees that only one bit changes at a time during normal counting, passing it through a multi-bit synchronizer is safe and prevents the write domain from seeing corrupted intermediate pointer values.

Timing and Reset Rules:

  • Clock edge: posedge wclk
  • Reset type: Asynchronous
  • Reset polarity: Active low (wrst_n)
  • Reset behavior: All internal synchronizer stages and the output wq_rptr_gray must reset to 0 immediately when wrst_n is 0.

Worked Trace:
Cycle 1: wrst_n=0 → internal_stage=0, wq_rptr_gray=0
Cycle 2: wrst_n=1, rptr_gray=5 → internal_stage=5, wq_rptr_gray=0
Cycle 3: rptr_gray=5 → internal_stage=5, wq_rptr_gray=5
Cycle 4: rptr_gray=12 → internal_stage=12, wq_rptr_gray=5
Cycle 5: rptr_gray=12 → internal_stage=12, wq_rptr_gray=12

Module Parameters:

Parameter Type Default Description
PTR_WIDTH integer 5 Bit width of the read pointer

Port Table:

Signal Direction Width Description
wclk input 1 Write domain positive-edge triggered clock
wrst_n input 1 Write domain asynchronous active-low reset
rptr_gray input PTR_WIDTH Gray-coded read pointer from the read clock domain
wq_rptr_gray output PTR_WIDTH Synchronized Gray-coded read pointer in the write clock domain

Constraints

  • The module must be parameterized with PTR_WIDTH defaulting to 5.
  • The synchronizer must consist of exactly two flip-flop stages in series.
  • No combinational logic may be placed between the first and second synchronizer stages.
  • All stages must be cleared asynchronously when wrst_n is low.

Topics

FIFOGray CodeCDCSynchronizer

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Clock Domain Crossing and Synchronization

Problem 20 of 36 in the Clock Domain Crossing and Synchronization track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.