0
0
Verilogprogramming~5 mins

ROM (Read-Only Memory) in Verilog

Choose your learning style9 modes available
Introduction

ROM stores fixed data that does not change during operation. It helps keep important information safe and always available.

When you need to store fixed lookup tables like character fonts.
When you want to keep configuration data that never changes.
When you need to store program instructions in simple hardware.
When you want to save constants that your circuit uses repeatedly.
Syntax
Verilog
module rom_example(
  input wire [3:0] addr,
  output reg [7:0] data
);

  always @(*) begin
    case(addr)
      4'h0: data = 8'hA1;
      4'h1: data = 8'hB2;
      4'h2: data = 8'hC3;
      4'h3: data = 8'hD4;
      default: data = 8'h00;
    endcase
  end

endmodule

ROM is often implemented using a case statement inside a combinational always block.

The addr input selects which fixed data to output on data.

Examples
Simple ROM with 3 fixed values and a default.
Verilog
case(addr)
  4'h0: data = 8'hFF;
  4'h1: data = 8'h00;
  4'h2: data = 8'hAA;
  default: data = 8'h55;
endcase
Using an array and initial block to store ROM contents.
Verilog
reg [7:0] rom_mem [0:15];

initial begin
  rom_mem[0] = 8'h10;
  rom_mem[1] = 8'h20;
  rom_mem[2] = 8'h30;
  // ... initialize other addresses
end

always @(*) begin
  data = rom_mem[addr];
end
Sample Program

This program defines a small ROM with 4 fixed values. The testbench prints the data for each address.

Verilog
module rom_demo(
  input wire [1:0] addr,
  output reg [7:0] data
);

  always @(*) begin
    case(addr)
      2'b00: data = 8'h12;
      2'b01: data = 8'h34;
      2'b10: data = 8'h56;
      2'b11: data = 8'h78;
      default: data = 8'h00;
    endcase
  end

endmodule

// Testbench to show ROM output
module testbench;
  reg [1:0] addr;
  wire [7:0] data;

  rom_demo rom(.addr(addr), .data(data));

  initial begin
    $display("Addr | Data");
    for (addr = 0; addr < 4; addr = addr + 1) begin
      #1; // wait for data to update
      $display("%b    | %h", addr, data);
    end
    $finish;
  end
endmodule
OutputSuccess
Important Notes

ROM contents are fixed and cannot be changed during simulation or hardware operation.

Use initial blocks or case statements to define ROM data.

ROM is useful for storing constants, lookup tables, or program code in hardware.

Summary

ROM holds fixed data that your circuit reads but never changes.

Use case or arrays with initial to define ROM contents.

ROM is great for lookup tables, constants, and program instructions.