First-in, first-out queue (FIFO)
Trace the read pointer, write pointer and occupancy of a single-clock FIFO. Specify which transfers are allowed when empty or full and when enqueue and dequeue coincide. Use an independent reference queue to check output ordering after the pointers wrap.
Four readings arrive while the receiver is busy. Preserving arrival order takes more than four storage slots: the circuit tracks where to write next and which byte must leave first.
The RTL
This single-clock FIFO has depth four. Its two-bit write and read pointers wrap from 3 to 0. Equal pointers can mean empty or full, so a three-bit occupancy count distinguishes the two states.
module sync_fifo4(
input logic clk,
input logic rst_n,
input logic push,
input logic pop,
input logic [7:0] din,
output logic [2:0] level,
output logic [1:0] rptr,
output logic [1:0] wptr,
output logic [7:0] head
);
logic [7:0] mem [0:3];
logic put, get;
assign put = push && (level < 4);
assign get = pop && (level != 0);
assign head = (level != 0) ? mem[rptr] : 8'h00;
always_ff @(posedge clk) begin
if (!rst_n) begin
wptr <= 0;
rptr <= 0;
level <= 0;
end else begin
if (put) begin
mem[wptr] <= din;
wptr <= wptr + 1'b1;
end
if (get) rptr <= rptr + 1'b1;
case ({put,get})
2'b10: level <= level + 1'b1;
2'b01: level <= level - 1'b1;
default: level <= level;
endcase
end
end
endmodule
Trace the outputs
After inserting 11, 22, 33 and 44, the FIFO is full. A simultaneous pop and push of 55 uses the pre-edge limits: only the pop of 11 succeeds. On the next edge the same requests remove 22 and write 55 at wrapped address 0.
| Rising edge | Inputs (hex) | Observed after edge (hex) |
|---|---|---|
| E0 | rst_n=0, push=0, pop=0, din=00 | level=0, rptr=0, wptr=0, head=00 |
| E1 | rst_n=1, push=1, pop=1, din=11 | level=1, rptr=0, wptr=1, head=11 |
| E2 | rst_n=1, push=1, pop=0, din=22 | level=2, rptr=0, wptr=2, head=11 |
| E3 | rst_n=1, push=1, pop=0, din=33 | level=3, rptr=0, wptr=3, head=11 |
| E4 | rst_n=1, push=1, pop=0, din=44 | level=4, rptr=0, wptr=0, head=11 |
| E5 | rst_n=1, push=1, pop=1, din=55 | level=3, rptr=1, wptr=0, head=22 |
| E6 | rst_n=1, push=1, pop=1, din=55 | level=3, rptr=2, wptr=1, head=33 |
| E7 | rst_n=1, push=0, pop=1, din=00 | level=2, rptr=3, wptr=1, head=44 |
| E8 | rst_n=1, push=0, pop=1, din=00 | level=1, rptr=0, wptr=1, head=55 |
| E9 | rst_n=1, push=0, pop=1, din=00 | level=0, rptr=1, wptr=1, head=00 |
Design choices
The remaining order must be 33, 44, 55. Starting empty, a concurrent push and pop accepts only the push. There is no fall-through transfer. Read the boundary contract before connecting another FIFO implementation.
Yosys synth_ice40 mapped this module to the following cell counts: SB_CARRY: 2, SB_DFFE: 32, SB_DFFESR: 7, SB_DFFSR: 2, SB_LUT4: 49. The download records the tool versions and the commands to reproduce this mapping.
Interactive lab
Reset, then enqueue 11, 22, 33 and 44. Starting full, request a pop and a push of 55 on two successive edges. Watch level and head.
- level
- rptr
- wptr
- head
Show the last eight observations
Teaching film
Run the RTL yourself
Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.
Download First-in, first-out queue (FIFO) RTL (.zip)