Double-Buffered FPGA LED Framebuffer With Block RAM

An LED scanner reads pixels continuously while a CPU, DMA engine, or animation core writes the next image. If both sides touch the displayed image, the panel can show half of one frame and half of another. A double buffer fixes tearing: one memory bank is scanned while the other is updated, and ownership changes only at a frame boundary.

The architecture

  • Front bank: read by the real-time display scanner.
  • Back bank: written by the producer.
  • Swap request: says the back bank contains a complete frame.
  • Frame boundary: the only place the scanner changes banks.

The producer must not write the bank currently being scanned. The scanner must not switch until the last row and last bitplane have completed.

Infer two synchronous memories

localparam int PIXELS = WIDTH * HEIGHT;

logic [23:0] bank0 [0:PIXELS-1];
logic [23:0] bank1 [0:PIXELS-1];
logic        front_bank;
logic [23:0] scan_pixel;

always_ff @(posedge write_clk) begin
    if (write_en) begin
        if (front_bank) bank0[write_addr] <= write_data;
        else            bank1[write_addr] <= write_data;
    end
end

always_ff @(posedge scan_clk) begin
    if (front_bank) scan_pixel <= bank1[scan_addr];
    else            scan_pixel <= bank0[scan_addr];
end

This example writes the bank opposite front_bank. If the clocks are unrelated, do not pass front_bank directly into write logic without a synchronization and ownership protocol. A safer system acknowledges each swap back to the producer before accepting more writes.

Swap only on a complete frame

always_ff @(posedge scan_clk) begin
    if (rst) begin
        front_bank <= 1'b0;
        swap_ack   <= 1'b0;
    end else if (frame_done && swap_pending) begin
        front_bank <= ~front_bank;
        swap_ack   <= ~swap_ack;
    end
end

A toggle acknowledgement is easier to cross between clock domains than a one-cycle pulse. The producer holds the back bank stable after requesting a swap and waits until it sees the acknowledgement toggle.

Account for memory latency

Block RAM normally has a synchronous read. Request pixel N one clock before its bits are needed, and pipeline the column counter, plane index, and row metadata alongside the data. An off-by-one memory pipeline often appears as a one-column horizontal shift.

Capacity example

A 64×32 RGB framebuffer at 24 bits per pixel contains 49,152 bits. Double buffering requires 98,304 bits before padding and parity. Compare that requirement with the block-RAM geometry of the target FPGA, not only its headline memory total.

Official reference

AMD UG901 RAM HDL Coding Techniques documents portable coding patterns for distributed and block-memory inference.


FPGA LED controller tutorial series

This article is part of the FPGA LED Controller Tutorials learning path. Continue with PWM vs. BCM, the HUB75 timing calculator, or SystemVerilog verification.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *