Write Pointer Generation and Gray Conversion
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_clkposedge - Reset type:
w_rst_nasynchronous - Reset polarity: active-low
- Output values on reset: Both
w_ptr_binandw_ptr_grayreset to 4'b0000 - Priority rules: Reset has highest priority. If
w_rst_nis 0, outputs remain 0 regardless ofw_en. - Registered outputs: Both
w_ptr_binandw_ptr_graymust 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_clkposedge,w_rst_nactive-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_binandw_ptr_graymust be flip-flop outputs, not combinational logic.
Topics
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