Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 37 ยท Sequential Logic

Register Files, RAM and ROM in Verilog

Model storage in Verilog: register files, synchronous single and dual-port RAM, ROM with initial contents, read-during-write behavior, latches versus flip-flops and a small FIFO, with C models.

Beyond single registers, designs need arrays of stored words: a CPU's register file, a data buffer, a lookup table. Verilog describes these as arrays of regs, and synthesis tools infer the right hardware (flip-flops, FPGA block RAM or ASIC memory macros) from the shape of your code. This lesson shows the common patterns and the pitfalls, and finishes with latches and a small FIFO.

Register file

A register file is a small array with several read ports and one write port, where reads are combinational and the write is clocked. A CPU-style 8-entry file of 16-bit words:

VERILOG
module regfile (
    input  wire        clk,
    input  wire        we,
    input  wire [2:0]  waddr, raddr1, raddr2,
    input  wire [15:0] wdata,
    output wire [15:0] rdata1, rdata2
);
    reg [15:0] regs [0:7];

    always @(posedge clk)
        if (we) regs[waddr] <= wdata;

    assign rdata1 = regs[raddr1];
    assign rdata2 = regs[raddr2];
endmodule

Many processors hardwire register 0 to zero; you would add assign rdata1 = (raddr1 == 0) ? 16'd0 : regs[raddr1];. Small arrays with asynchronous (combinational) reads usually become flip-flops and multiplexers.

Synchronous single-port RAM

When the read is registered (the data appears one clock after the address), tools can use dedicated block RAM:

VERILOG
module ram_sp #(parameter AW = 8, DW = 8) (
    input  wire          clk,
    input  wire          we,
    input  wire [AW-1:0] addr,
    input  wire [DW-1:0] din,
    output reg  [DW-1:0] dout
);
    reg [DW-1:0] mem [0:(1<<AW)-1];

    always @(posedge clk) begin
        if (we) mem[addr] <= din;
        dout <= mem[addr];
    end
endmodule

Here dout shows the old contents of mem[addr] on a write cycle (read-first behavior), because the non-blocking write has not yet happened when the read samples. Other block RAM modes are write-first and no-change; the style of your code selects one, so check your device's documentation.

Dual-port RAM

A simple dual-port RAM has one write port and one read port with independent addresses (and optionally independent clocks):

VERILOG
always @(posedge wclk)
    if (we) mem[waddr] <= din;

always @(posedge rclk)
    dout <= mem[raddr];

It is the storage inside a FIFO, a video line buffer or a cache.

ROM

A ROM holds constants. Describe it with a case statement or load it from a file with $readmemh:

VERILOG
reg [7:0] rom [0:15];
initial $readmemh("sine_table.hex", rom);   // FPGA flows load this into the bitstream

always @(posedge clk)
    data <= rom[addr];

A case ROM is portable because it is plain logic:

VERILOG
always @* begin
    case (addr)
        2'd0: data = 8'h3A;
        2'd1: data = 8'h7F;
        2'd2: data = 8'hC1;
        default: data = 8'h00;
    endcase
end

$readmemh and initial memory loading are synthesizable only for FPGA and some ASIC memory compilers. Treat them as a tool-specific feature.

Latches versus flip-flops

A latch is level-sensitive: while its enable is high it passes d to q, and when the enable falls it holds. A flip-flop samples only at an edge.

VERILOG
// Latch: transparent while en is high
always @*
    if (en) q = d;

Latches are legitimate (clock gating cells, some custom designs) but most FPGA fabrics lack them, they complicate timing analysis, and they are nearly always created by accident when a combinational block misses an assignment. If you see "inferred latch" in a synthesis report, find the missing else or default.

A small FIFO idea

A FIFO (first in, first out) buffer needs a RAM, a write pointer, a read pointer and full/empty logic.

VERILOG
module fifo #(parameter AW = 3, DW = 8) (
    input  wire          clk, rst_n,
    input  wire          push, pop,
    input  wire [DW-1:0] din,
    output wire [DW-1:0] dout,
    output wire          empty, full
);
    reg [DW-1:0] mem [0:(1<<AW)-1];
    reg [AW:0]   wptr, rptr;          // one extra bit tells full from empty

    assign empty = (wptr == rptr);
    assign full  = (wptr[AW-1:0] == rptr[AW-1:0]) && (wptr[AW] != rptr[AW]);
    assign dout  = mem[rptr[AW-1:0]];

    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin wptr <= 0; rptr <= 0; end
        else begin
            if (push && !full) begin mem[wptr[AW-1:0]] <= din; wptr <= wptr + 1'b1; end
            if (pop && !empty) rptr <= rptr + 1'b1;
        end
    end
endmodule

The trick: pointers have one more bit than the address needs. If all bits match the FIFO is empty; if only the top bit differs it is full (the writer has lapped the reader).

Stand-in: FIFO pointers in C

This stand-in (not Verilog) models the FIFO with the extra-bit pointer trick and prints status during pushes and pops, so you can confirm the full/empty logic.

C Stand-in: FIFO with wrap bit pointers (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The fifth push is ignored because the FIFO is full after four, and the fifth pop returns -1 (nothing to pop).

Stand-in: read-first behavior (C)

C Stand-in: RAM read-first on a write cycle (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

On cycle 1 the write to address 2 shows the old value 33; the new value 99 appears on the next read.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a registered read help synthesis infer block RAM?

    FPGA block RAMs register the read address or output, so code with a clocked read maps onto them.
  2. 2. With non-blocking writes, what does dout show on a write cycle to the same address?

    The read samples mem[addr] before the non-blocking write updates.
  3. 3. How does the FIFO tell full from empty?

    Equal pointers are empty; equal addresses with different wrap bits are full.
  4. 4. What usually causes an inferred latch?

    Missing assignments in combinational logic require holding the previous value.

Summary

  • Register files are small arrays with a clocked write and combinational reads; they map to flip-flops and muxes.
  • A clocked read lets the tool infer block RAM; non-blocking writes give read-first behavior.
  • ROMs use case statements or $readmemh (tool dependent); dual-port RAMs have independent read and write.
  • Latches are level-sensitive and usually accidental; flip-flops sample on a clock edge.
  • A FIFO combines a RAM, two pointers with an extra wrap bit, and full/empty logic.

Next: Your First Testbench