BitFlow

Asynchronous FIFO (Clock Domain Crossing Safe)

advanced · memory · 320 XP

Sign in to write your solution, run it in the simulator and submit it for grading.

Sign in or create a free account →

Problem

Asynchronous FIFO with Clock Domain Crossing A FIFO that safely bridges two independent clock domains. Write and read clocks may have **no frequency relationship** (e.g., 10 MHz write, 7 MHz read). Pointers crossing between domains use **Gray code** + **2-flop synchronizers** to prevent metastability. **Key Insight:** Gray code ensures only 1 bit changes between consecutive values. This guarantees that even with synchronizer latency, comparisons stay safe. **Interface** | Port | Direction | Clock | Description | |-------------|-----------|----------|--------------------------------| | `wr_clk` | input | write | Write clock | | `rd_clk` | input | read | Read clock (independent freq) | | `rst` | input | - | Async reset | | `wr_en` | input | wr_clk | Write enable | | `wr_data[7:0]`| input | wr_clk | Write data (8-bit) | | `rd_en` | input | rd_clk | Read enable | | `rd_data[7:0]`| output | rd_clk | Read data (8-bit) | | `wr_full` | output | wr_clk | Write side: FIFO full flag | | `rd_empty` | output | rd_clk | Read side: FIFO empty flag | **Depth: 8 entries (3-bit pointers)** **Write side:** - `wr_ptr[2:0]` advances on valid writes - Convert `wr_ptr` to Gray code: `wr_ptr_gray = wr_ptr ^ (wr_ptr >> 1)` - Synchronize `wr_ptr_gray` to read clock via 2-flop sync → `wr_ptr_gray_sync` - Convert back to binary: `wr_ptr_sync` (in read domain) - `wr_full` asserted in write domain when `wr_ptr_next == rd_ptr_sync_gray` (after Gray decode) **Read side:** - `rd_ptr[2:0]` advances on valid reads - Synchronize `rd_ptr_gray` to write clock via 2-flop sync → `rd_ptr_gray_sync` - `rd_empty` asserted in read domain when `rd_ptr == wr_ptr_sync_gray` (after Gray decode) **Conservative behavior:** - `wr_full` may assert one cycle early (conservative: prevents overflow) - `rd_empty` may assert one cycle late (conservative: allows extra read) **Gray code properties:** - Binary to Gray: `gray = binary ^ (binary >> 1)` - Gray to Binary: requires a loop or ripple-carry style (harder; often just compare Gray directly)

Constraints

  • Use Gray code for pointer synchronization across clock domains.
  • 2-flop synchronizers on both pointer directions (write→read and read→write).
  • FIFO depth = 8 (3-bit pointers, MSB used for wrap detection).
  • full/empty flags are conservative (safe against metastability but may be off by 1 cycle).
  • No data corruption even with simultaneous high-speed read and write in different domains.

Examples

Input: wr_clk 10ns, rd_clk 7ns, write all 8 entries continuously

Output: wr_full asserts after 8 writes; rd_data streams out asynchronously

Non-integer clock ratio

Input: Write stops, read continues

Output: rd_empty asserts after reading 8 entries

Safe empty detection

Input: Simultaneous high-speed read and write (10k transactions)

Output: Zero data corruption, flags stay consistent

CDC safety verified

Common questions

What does the Asynchronous FIFO (Clock Domain Crossing Safe) problem ask you to build?
Asynchronous FIFO with Clock Domain Crossing A FIFO that safely bridges two independent clock domains. Write and read clocks may have **no frequency relationship** (e.g., 10 MHz write, 7 MHz read).
What constraints does Asynchronous FIFO (Clock Domain Crossing Safe) have?
Use Gray code for pointer synchronization across clock domains.; 2-flop synchronizers on both pointer directions (write→read and read→write).; FIFO depth = 8 (3-bit pointers, MSB used for wrap detection).; full/empty flags are conservative (safe against metastability but may be off by 1 cycle).; No data corruption even with simultaneous high-speed read and write in different domains.
What is an example input and output for Asynchronous FIFO (Clock Domain Crossing Safe)?
Input wr_clk 10ns, rd_clk 7ns, write all 8 entries continuously gives output wr_full asserts after 8 writes; rd_data streams out asynchronously. Non-integer clock ratio
Which Verilog concepts does Asynchronous FIFO (Clock Domain Crossing Safe) practise?
memory, FIFO, CDC, Gray-code, interview.