Building systems · Lesson 12 of 14

FPGA memory and DSP: block RAM, FIFOs and MACs

Infer block RAM, distributed RAM and ROM in SystemVerilog, build a synchronous FIFO, and write multiply-accumulate and fixed-point code that maps to DSP slices.

intermediate50 minUpdated Free

What you will learn

  • Choose between registers, distributed RAM, block RAM and large on-chip memories for a given buffer
  • Write single-port and simple dual-port RAM and ROM templates that synthesis tools map to block RAM
  • Build and verify a synchronous FIFO with correct full, empty and count logic
  • Write a pipelined multiply-accumulate that packs into a DSP slice
  • Multiply Q1.15 fixed-point numbers with correct rounding and saturation

Before you start

  • Sequential logic (lesson 7)
  • Testbenches (lesson 9)
  • Static timing analysis and timing closure (lesson 10)

After this lesson you can write RAM, ROM and FIFO code that synthesis turns into the FPGA’s dedicated memory blocks, and arithmetic that lands in the DSP slices instead of thousands of LUTs. You will also be able to work in fixed point, which is how most FPGA signal processing represents fractional numbers.

The dedicated blocks were introduced in FPGA architecture. The tools use them only when your code matches what the hardware can do. That is the theme of this lesson: write the hardware’s behaviour, and the tool recognises it.

Where data can live

StorageBuilt fromReadTypical size per instanceGood for
RegistersFlip-flopsAny time, many portsA few wordsPipeline stages, small register files
Distributed RAMLUTs configured as memoryAsynchronous or synchronousTens to a few hundred wordsSmall buffers, coefficient tables, shallow FIFOs
Block RAMDedicated RAM blocksSynchronous onlyHundreds to thousands of wordsLine buffers, FIFOs, packet memory
Large on-chip RAMLarger dedicated blocks, in some familiesSynchronous onlyMuch larger than one block RAMBig frame or lookup buffers

Distributed RAM uses LUTs as tiny memories. AMD calls the LUTs that can do this SLICEM LUTs; Intel uses MLAB blocks. It is fast and can be read asynchronously, but each bit of storage costs logic.

Block RAM is a hard memory block spread in columns across the die. In AMD 7-series devices each block holds 36 Kb and can be split into two 18 Kb halves. Intel names its blocks by size: M9K in MAX 10, M10K in Cyclone V, M20K in Arria 10, Stratix 10 and Agilex. A block RAM has two ports, a configurable width and depth, an optional output register, and initial contents loaded with the bitstream. Its read is always synchronous: the address is registered on a clock edge and the data appears after it.

Large memories such as AMD UltraRAM (in some UltraScale+ devices) trade flexibility for capacity. They have fewer width options and more restrictions on clocking and initialisation. Check the memory guide for your family before relying on one.

The rule that follows: if you want block RAM, write a synchronous read. An asynchronous read can only be built from LUTs or flip-flops.

Inferring RAM

Synthesis tools recognise a few coding patterns as RAM. Write the memory as an array, write to it in a clocked block, read it in a clocked block, and do not reset the array. A reset on the memory contents rules out block RAM, because the hardware cannot clear all its words in one cycle.

Single-port RAM

One address is used for both reading and writing:

module sp_ram #(
    parameter int DW = 16,   // data width
    parameter int AW = 10    // address width, depth = 2**AW
) (
    input  logic          clk,
    input  logic          en,     // port enable: no read or write when low
    input  logic          we,     // write enable
    input  logic [AW-1:0] addr,
    input  logic [DW-1:0] din,
    output logic [DW-1:0] dout
);
    logic [DW-1:0] mem [0:(1<<AW)-1];

    always_ff @(posedge clk) begin
        if (en) begin
            if (we) begin
                mem[addr] <= din;
            end
            dout <= mem[addr];   // nonblocking: reads the value before this write
        end
    end
endmodule
Download sp_ram.sv

Read-during-write behaviour

When a port writes and reads the same address on the same edge, the output can show three different things. Block RAMs support all three as port modes, and the code you write selects the mode:

  • Read-first: dout shows the old contents. This is the code above. Because both assignments are nonblocking, mem[addr] on the right-hand side is the value before the write.
  • Write-first (also called read-through): dout shows the new data.
  • No-change: dout keeps its previous value during writes. This can save power.

Write-first looks like this:

always_ff @(posedge clk) begin
    if (en) begin
        if (we) begin
            mem[addr] <= din;
            dout      <= din;      // new data appears on dout
        end else begin
            dout      <= mem[addr];
        end
    end
end

If the tool cannot match your read-during-write behaviour to a mode the hardware supports, it adds bypass logic around the RAM or uses LUTs instead, and the synthesis log says so. Read the log for every memory you infer.

Simple dual-port RAM

A simple dual-port RAM has one write port and one read port with separate addresses. This is the structure behind most FIFOs and buffers.

always_ff @(posedge clk) begin
    if (we) begin
        mem[waddr] <= din;
    end
end

always_ff @(posedge clk) begin
    if (re) begin
        rd_q <= mem[raddr];
    end
end

The full module adds a parameter OUT_REG. When it is 1, a second register follows the read, so the read latency is two cycles. Block RAMs have this register built in. It shortens the path from the RAM output to the logic that uses the data, because the slow RAM access time is no longer in the same cycle as that logic. When block RAM paths fail timing, turning on the output register is usually the first fix.

Download sdp_ram.sv

For two clock domains, clock the write block with the write clock and the read block with the read clock. The RAM itself is then the crossing, and the address and control signals still need the treatment in clock domain crossing.

When the write and read addresses are equal on the same edge, the value the read port returns depends on the device and the port mode. If your design can produce that collision and depends on the result, avoid the collision in the control logic or add explicit bypass logic, and check the device memory guide.

ROM from a file

A ROM is a RAM without a write port, with contents loaded at configuration. $readmemh in an initial block reads a hex file at synthesis time. Vivado, Quartus and Yosys all support it.

module rom #(
    parameter int    DW        = 16,
    parameter int    AW        = 6,
    parameter string INIT_FILE = "sine_q15.hex"
) (
    input  logic          clk,
    input  logic [AW-1:0] addr,
    output logic [DW-1:0] dout
);
    logic [DW-1:0] mem [0:(1<<AW)-1];

    initial begin
        $readmemh(INIT_FILE, mem);
    end

    always_ff @(posedge clk) begin
        dout <= mem[addr];
    end
endmodule

The hex file holds one word per line. The example file holds one cycle of a sine wave in 64 samples, in the Q1.15 format explained below. Add the file to the project (or give the path relative to where the tool runs). If synthesis cannot find the file, the ROM contents will be wrong, often with only a warning in the log, so check for it.

Download rom.sv Download sine_q15.hex

Resource hints

You can ask for a specific memory type with an attribute on the array. These are hints: the tool may still refuse if the code does not fit the requested resource.

(* ram_style = "block" *)   logic [15:0] buf_a [0:1023];   // Vivado: "block", "distributed", "registers", "ultra"
(* ramstyle  = "M20K" *)    logic [15:0] buf_b [0:1023];   // Quartus: "M20K" (or M10K, M9K), "MLAB", "logic"

Vivado also has use_dsp for arithmetic, and Quartus has multstyle. Use attributes when the tool’s default choice is wrong, for example a small table that went into block RAM when you need the block RAMs elsewhere.

Synchronous FIFO

A FIFO (first in, first out) buffer passes data between a producer and a consumer that do not run in lock step. The synchronous FIFO here uses one clock for both sides. It is a simple dual-port RAM plus a write pointer, a read pointer and status flags.

The pointers have one more bit than the RAM address. The low bits address the RAM. The extra top bit flips each time a pointer wraps around, which separates the two cases where the low bits are equal:

  • Empty: the pointers are equal, including the top bit. Every word written has been read.
  • Full: the low bits are equal and the top bits differ. The write pointer is exactly one lap ahead.
  • Count: wr_ptr - rd_ptr, in modular arithmetic. The extra bit lets it reach the full depth.
Three rows of an eight-entry FIFO. Normal: read pointer at entry 2, write pointer at entry 6, entries 2 to 5 hold data, pointer values 0010 and 0110, count 4. Empty: both pointers at entry 5 with value 0101 and no data. Full: both pointers at entry 3, all entries hold data, read pointer 0011 and write pointer 1011, only the top bit differs.
Pointers of a depth-8 synchronous FIFO. The pointers carry one extra bit, so equal pointers mean empty, and pointers that differ only in the top bit mean full.
assign do_wr = wr_en && !full;
assign do_rd = rd_en && !empty;

// Storage: no reset, so the tool can use RAM primitives.
always_ff @(posedge clk) begin
    if (do_wr) begin
        mem[wr_ptr[AW-1:0]] <= wr_data;
    end
    if (do_rd) begin
        rd_data <= mem[rd_ptr[AW-1:0]];
    end
end

// Pointers.
always_ff @(posedge clk) begin
    if (rst) begin
        wr_ptr <= '0;
        rd_ptr <= '0;
    end else begin
        if (do_wr) wr_ptr <= wr_ptr + 1'b1;
        if (do_rd) rd_ptr <= rd_ptr + 1'b1;
    end
end

assign count = wr_ptr - rd_ptr;
assign empty = (wr_ptr == rd_ptr);
assign full  = (wr_ptr[AW] != rd_ptr[AW]) &&
               (wr_ptr[AW-1:0] == rd_ptr[AW-1:0]);

Points worth checking in any FIFO:

  • Writes when full and reads when empty are ignored. The do_wr and do_rd gates protect the pointers, so a misbehaving producer or consumer cannot corrupt the FIFO.
  • The read has one cycle of latency. rd_data is valid on the cycle after rd_en was accepted. This keeps the read synchronous, so the storage maps to block RAM. A “first-word fall-through” FIFO shows the next word before it is requested; it needs an extra output register stage and more control logic.
  • The read and write addresses cannot collide. They are equal only when the FIFO is empty (no read accepted) or full (no write accepted), so there is no read-during-write case to worry about.
  • The depth is a power of two, so the pointers wrap naturally.

The testbench keeps a SystemVerilog queue as a reference model. It runs 2000 cycles each of mostly writes, balanced traffic and mostly reads, with random enables. Every cycle it checks full, empty and count against the model, and every read against the expected word. It also checks that the FIFO actually reached full and empty, so a test that never exercised them cannot pass by accident.

Download sync_fifo.sv Download tb_sync_fifo.sv
iverilog -g2012 -o sim tb_sync_fifo.sv sync_fifo.sv && vvp sim
# writes 2073, reads 2072, cycles full 1409, cycles empty 1386
# PASS

DSP slices

A DSP slice is a hard arithmetic block: a multiplier, an adder/accumulator behind it, often a pre-adder in front, and pipeline registers at each stage. In AMD 7-series devices the slice (DSP48E1) has a 25 x 18-bit signed multiplier and a 48-bit accumulator. Intel’s variable-precision DSP blocks support several multiplier sizes depending on the family, including a 27 x 27 mode and smaller multipliers around 18 bits wide. A multiply in LUTs costs hundreds of LUTs and is slow; in a DSP slice it costs one block and runs much faster.

The tools put * into DSP slices automatically when the operand widths fit. The challenge is to get the registers into the slice too. The slice’s internal registers sit at fixed points: after the inputs, after the multiplier, and after the adder. If your RTL has registers in the same places, synthesis absorbs them into the slice. If it does not, the multiplier’s full delay ends up in a fabric path, and the design runs far below what the slice can do.

Multiply-accumulate

A multiply-accumulate (MAC) computes acc←acc+a⋅b\text{acc} \leftarrow \text{acc} + a \cdot b every cycle. It is the core of FIR filters, dot products and matrix multiplication.

// Stage 1: input registers.         -> slice input registers
// Stage 2: product register.        -> slice multiplier register
// Stage 3: accumulator register.    -> slice output register
always_ff @(posedge clk) begin
    a_r    <= a;
    b_r    <= b;
    first1 <= first;
    p_r    <= a_r * b_r;
    first2 <= first1;
    if (v2) begin
        acc <= first2 ? ACCW'(p_r) : acc + ACCW'(p_r);
    end
end

The first input starts a new sum: the accumulator loads the product instead of adding it. A separate block, with a reset, moves the valid flags v1, v2 and out_valid along with the data. The latency from inputs to acc is three cycles.

Design choices in this code:

  • Data registers have no reset. DSP slice registers typically support only synchronous resets, so an asynchronous reset keeps a register out of the slice. Any reset also adds a load to the reset net. The valid flags tell downstream logic when data is meaningful, so the data does not need a known start value.
  • The accumulator has guard bits. A 16 x 16 signed product needs 32 bits. Summing N products needs about ⌈log⁡2N⌉\lceil \log_2 N \rceil more bits. With ACCW = 40, the 8 guard bits cover at least 256 worst-case products without overflow.
  • The operands are declared signed. Mixing signed and unsigned operands makes the whole expression unsigned, as covered in Verilog basics.

The testbench sends 200 dot products of random length (1 to 64 terms) with random idle cycles. It includes the extreme values -32768 and 32767 often, and checks every intermediate accumulator value against a 64-bit reference.

Download mac.sv Download tb_mac.sv
iverilog -g2012 -o sim tb_mac.sv mac.sv && vvp sim
# PASS: 200 sums, 6180 accumulator values checked

After synthesis, check the utilisation report: the MAC should use one DSP slice and almost no LUTs. If it uses LUTs or more slices, look at the operand widths and the register placement first.

Fixed-point arithmetic

FPGAs process fractional numbers mostly in fixed point: an integer with an implied binary point. The notation Qm.n means m integer bits (including the sign bit) and n fractional bits. A Qm.n number is the stored integer divided by 2n2^n.

Q1.15 uses 16 bits: a sign bit and 15 fractional bits. Its range is −1-1 to 1−2−151 - 2^{-15}, and its resolution is 2−15≈3.05×10−52^{-15} \approx 3.05 \times 10^{-5}. It is common for audio samples and filter coefficients.

Multiplying Q1.15 numbers

When two fixed-point numbers are multiplied, the integer bits add and the fractional bits add. Q1.15 times Q1.15 gives Q2.30 in 32 bits. To get back to Q1.15 you drop 15 fractional bits, and how you drop them matters.

Worked example: 0.6×(−0.35)0.6 \times (-0.35).

  1. Convert to Q1.15 by multiplying by 215=327682^{15} = 32768 and rounding: a=round(0.6×32768)=19661a = \text{round}(0.6 \times 32768) = 19661 (16'h4CCD), and b=round(−0.35×32768)=−11469b = \text{round}(-0.35 \times 32768) = -11469 (16'hD333).
  2. Multiply the integers: 19661×(−11469)=−225 492 00919661 \times (-11469) = -225\,492\,009. In Q2.30 that is −225 492 009/230≈−0.2100058-225\,492\,009 / 2^{30} \approx -0.2100058. The small difference from −0.21-0.21 comes from rounding the inputs.
  3. Truncate: an arithmetic shift right by 15 gives ⌊−225 492 009/32768⌋=⌊−6881.47⌋=−6882\lfloor -225\,492\,009 / 32768 \rfloor = \lfloor -6881.47 \rfloor = -6882 (16'hE51E), which is −0.2100220-0.2100220. Truncation always rounds towards minus infinity. Across many operations, that adds a negative bias.
  4. Round: add half of the result’s LSB, 214=163842^{14} = 16384, before the shift. ⌊(−225 492 009+16384)/32768⌋=⌊−6880.97⌋=−6881\lfloor (-225\,492\,009 + 16384) / 32768 \rfloor = \lfloor -6880.97 \rfloor = -6881 (16'hE51F), which is −0.2099915-0.2099915. The error against the exact −0.21-0.21 is about 8.5×10−68.5 \times 10^{-6}, compared with 2.2×10−52.2 \times 10^{-5} for truncation.

One case overflows: −1×−1=+1-1 \times -1 = +1, which Q1.15 cannot represent. The product 16'h8000 * 16'h8000 is 2302^{30}, and after the shift it is 32768, one more than the largest Q1.15 value. Saturate it to 16'h7FFF rather than letting it wrap to −1-1:

// Q1.15 x Q1.15 -> Q1.15, round half up, saturate
function automatic logic signed [15:0] q15_mul(input logic signed [15:0] a,
                                               input logic signed [15:0] b);
    logic signed [31:0] p;
    p = a * b;                 // Q2.30, exact
    p = p + 32'sd16384;        // add half of the result LSB (2^14)
    p = p >>> 15;              // back to 15 fractional bits
    if (p > 32'sd32767)       return 16'sh7FFF;   // only -1 x -1 gets here
    else if (p < -32'sd32768) return 16'sh8000;
    else                      return p[15:0];
endfunction

Use >>> (arithmetic shift) on a signed value, so the sign bit is copied in from the left. >> would shift in zeros and turn a negative product into a large positive one.

In a filter, do not round after every multiply. Keep the full-precision products in a wide accumulator, as in the MAC above, and round and saturate once at the output. That is both more accurate and cheaper.

Exercise

Q1.15 by hand

Compute 0.75×(−0.5)0.75 \times (-0.5) in Q1.15. Give the input integers, the Q2.30 product, and the rounded Q1.15 result in decimal and hex. Then compute (−1)×0.5(-1) \times 0.5 and check whether it needs saturation.

Show solution

0.75×32768=245760.75 \times 32768 = 24576 (16'h6000) and −0.5×32768=−16384-0.5 \times 32768 = -16384 (16'hC000). Both are exact.

Product: 24576×(−16384)=−402 653 18424576 \times (-16384) = -402\,653\,184, which is −0.375-0.375 in Q2.30.

Rounded: ⌊(−402 653 184+16384)/32768⌋=⌊−12287.5⌋=−12288\lfloor (-402\,653\,184 + 16384) / 32768 \rfloor = \lfloor -12287.5 \rfloor = -12288 (16'hD000), exactly −0.375-0.375. No precision is lost because the product’s low 15 bits are zero.

(−1)×0.5(-1) \times 0.5: −32768×16384=−536 870 912-32768 \times 16384 = -536\,870\,912; after rounding and the shift, −16384-16384 (16'hC000), which is −0.5-0.5. No saturation is needed. Only (−1)×(−1)(-1) \times (-1) overflows.

Exercise

Add an almost-full flag to the FIFO

Add an output almost_full to sync_fifo that is high when the FIFO holds DEPTH - 2 words or more, where DEPTH = 2**AW. A producer with two cycles of pipeline delay uses it to stop in time. Extend the testbench to check the flag every cycle.

Show solution

The count is already available, so the flag is a comparison:

output logic almost_full,
...
assign almost_full = (count >= (AW+1)'((1 << AW) - 2));

The cast to AW+1 bits matches the width of count and avoids a width warning. For better timing, register the flag: compute it from the next count (current count, plus one if do_wr, minus one if do_rd) and register the comparison, so the output comes straight from a flip-flop.

In the testbench, add a check next to the existing flag checks:

if (almost_full !== (model.size() >= DEPTH - 2)) begin
    $display("ERROR %0t: almost_full=%b, model size %0d", $time, almost_full, model.size());
    errors++;
end

Run it with the three traffic phases. The mostly-write phase drives the count through DEPTH - 2, DEPTH - 1 and DEPTH often, which tests the threshold.

Summary

  • Block RAM needs a synchronous read and an array without reset. Asynchronous reads map to distributed RAM or flip-flops.
  • The way you write read-during-write selects read-first, write-first or no-change mode. Read the synthesis log to confirm each memory went where you expected.
  • A synchronous FIFO is a simple dual-port RAM plus pointers with one extra bit: equal pointers mean empty, a difference only in the top bit means full.
  • Put registers before and after the multiplier and after the accumulator so the tool can pack a MAC into one DSP slice. Leave data registers without reset and give the accumulator guard bits.
  • Q1.15 times Q1.15 is Q2.30. Round by adding 2142^{14} before an arithmetic shift of 15, and saturate the single overflow case, −1×−1-1 \times -1.
  • Attributes such as ram_style, ramstyle, use_dsp and multstyle are hints when the tool’s default resource choice does not suit the design.

Check your understanding

Pick an answer for each question, then check. Answers stay hidden until you submit.

1. You write a 1024 x 16 RAM with an asynchronous read (assign dout = mem[addr]). What will the synthesis tool most likely build?
2. A single-port RAM is written in read-first mode. On a clock edge with we = 1, addr = 5, din = 8'hAA, what appears on dout after the edge?
3. In the synchronous FIFO with 4-bit pointers for a depth-8 memory, wr_ptr = 4'b1010 and rd_ptr = 4'b0010. What is the state?
4. Two Q1.15 numbers are multiplied. In what format is the full-precision 32-bit product?
5. Why does the MAC example leave the data path registers without a reset?

Found a mistake or something unclear?Report a correction.