Codiode

Write Pointer Generation and Gray Conversion

MediumVerilog / SystemVerilogBuild

The write side of an async FIFO needs its pointer twice: in binary to address the memory, and in Gray code to send to the read side. A binary count can flip several bits at once, which is not safe to cross a clock boundary.

The module maintains a 4-bit write pointer. When the write enable signal is asserted, the binary pointer increments by one. The module simultaneously converts the incremented binary value into Gray code. Both the binary pointer and the Gray-coded pointer are registered on the clock edge to ensure clean, glitch-free outputs.

Timing and reset rules:

  • Clock edge: w_clk posedge
  • Reset type: w_rst_n asynchronous
  • Reset polarity: active-low
  • Output values on reset: Both w_ptr_bin and w_ptr_gray reset to 4'b0000
  • Priority rules: Reset has highest priority. If w_rst_n is 0, outputs remain 0 regardless of w_en.
  • Registered outputs: Both w_ptr_bin and w_ptr_gray must be updated exactly on the clock edge.

Cycle 1: w_rst_n=0 → w_ptr_bin=0, w_ptr_gray=0
Cycle 2: w_rst_n=1, w_en=1 → w_ptr_bin=1, w_ptr_gray=1
Cycle 3: w_en=1 → w_ptr_bin=2, w_ptr_gray=3
Cycle 4: w_en=0 → w_ptr_bin=2, w_ptr_gray=3 (hold)
Cycle 5: w_en=1 → w_ptr_bin=3, w_ptr_gray=2
Cycle 6: w_en=1 → w_ptr_bin=4, w_ptr_gray=6

Signal Direction Width Description
w_clk input 1 Positive-edge triggered write clock
w_rst_n input 1 Asynchronous active-low reset; drives outputs to 0
w_en input 1 Write enable; increments pointer when 1
w_ptr_bin output 4 Registered binary write pointer
w_ptr_gray output 4 Registered Gray-coded write pointer

Constraints

  • Clock edge and reset polarity: w_clk posedge, w_rst_n active-low asynchronous.
  • Output values on reset: Both outputs must be exactly 4'b0000.
  • Priority: Reset takes precedence over enable.
  • Bit widths: All pointers are exactly 4-bit.
  • Overflow: The counter must naturally wrap from 15 (4'b1111) back to 0 (4'b0000).
  • Registered outputs: Both w_ptr_bin and w_ptr_gray must be flip-flop outputs, not combinational logic.

Topics

FIFOGray CodeCDCSequential Logic

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 32 of 36 in the Clock Domain Crossing and Synchronization track.

Related problems

Browse all problems · Number system and binary code problems · Verilog practice problems · Learning tracks

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