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.

Both pointers indicate address zero after filling. Occupancy distinguishes full from empty.
Both pointers indicate address zero after filling. Occupancy distinguishes full from empty.

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.

Recorded simulation. Dashed lines mark rising edges; bus values are hexadecimal. Open the figure to enlarge it.
Recorded simulation. Dashed lines mark rising edges; bus values are hexadecimal. Open the figure to enlarge it.
Rising edgeInputs (hex)Observed after edge (hex)
E0rst_n=0, push=0, pop=0, din=00level=0, rptr=0, wptr=0, head=00
E1rst_n=1, push=1, pop=1, din=11level=1, rptr=0, wptr=1, head=11
E2rst_n=1, push=1, pop=0, din=22level=2, rptr=0, wptr=2, head=11
E3rst_n=1, push=1, pop=0, din=33level=3, rptr=0, wptr=3, head=11
E4rst_n=1, push=1, pop=0, din=44level=4, rptr=0, wptr=0, head=11
E5rst_n=1, push=1, pop=1, din=55level=3, rptr=1, wptr=0, head=22
E6rst_n=1, push=1, pop=1, din=55level=3, rptr=2, wptr=1, head=33
E7rst_n=1, push=0, pop=1, din=00level=2, rptr=3, wptr=1, head=44
E8rst_n=1, push=0, pop=1, din=00level=1, rptr=0, wptr=1, head=55
E9rst_n=1, push=0, pop=1, din=00level=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

    Read the transcript

    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)

    Check your understanding

    1. Enqueue 11, 22, 33 and 44 in order. Which word is at the head?
    2. After filling, the pointers match. What distinguishes this from empty?
    3. What is accepted when both operations are requested from a full FIFO here?
    4. At level three a push and pop both succeed. What is the new level?
    5. When empty, can a simultaneous push and pop deliver the arriving byte on that edge?