Hardware interview practice
Eight-entry asynchronous FIFO
A 32-bit producer and consumer use unrelated clocks. Implement an eight-entry asynchronous FIFO with local full/empty flags, Gray-pointer crossings, and reset-release gating in both domains.
Starting point
Question code
input logic wclk, wrst_n, wvalid;
output logic wready;
input logic [31:0] wdata;
input logic rclk, rrst_n, rready;
output logic rvalid;
output logic [31:0] rdata;Reviewed example
Work through one case
Input
After reset, write A once and hold rready=1Expected output
rvalid eventually rises and the first accepted read returns AThe write Gray pointer crosses two rclk stages before rempty clears; the data bus remains in dual-port memory and FIFO order is preserved.
What to cover
Requirements
- Use 4-bit binary read and write pointers for eight entries, convert each next pointer to Gray code, and synchronize only Gray pointers through two flops into the opposite domain.
- Accept a write only on wvalid and wready; assert local full when the next write Gray pointer equals the synchronized read Gray pointer with its two most-significant bits inverted.
- Accept a read only on rvalid and rready; assert local empty when the next read Gray pointer equals the synchronized write Gray pointer, and preserve FIFO order.
- For initialization or flush, assert both resets together; each local reset clears its binary pointer, Gray pointer, and flag, and permit no traffic until both resets have been released and synchronized.
