Initialize Memory in Verilog

本文详细介绍了如何在Verilog中使用$readmemh和$readmemb函数初始化内存,包括语法、函数参数、内存文件格式,并提供了多个示例。同时讨论了在Yosys和Vivado中的具体用法。

想深耕嵌入式?这个专辑值得收藏

MCU、FPGA、工控、传感器一站式学习,实战项目直接抄

Initialize Memory in Verilog

t’s common for a simulation or firmware to need data loading into a memory array, ram, or rom. Fortunately, Verilog provides the $readmemh and $readmemb functions for this very purpose. Unfortunately, there is a dearth of good Verilog documentation online, so using them can be harder than it should be. This how to explains the syntax and provides plenty of examples, including how to do this in Yosys and Xilinx Vivado. This post was last updated in July 2021.

If you want to learn more about FPGA memory itself, see FPGA Memory Types.

Get in touch: GitHub Issues, 1BitSquared Discord, @WillFlux (Mastodon), @WillFlux (Twitter)

Verilog Syntax

Verilog allows you to initialize memory from a text file with either hex or binary values:

$readmemh("hex_memory_file.mem", memory_array, [start_address], [end_address])
$readmemb("bin_memory_file.mem", memory_array, [start_address], [end_address])

Function Arguments

  • hex_memory_file.mem - a text file containing hex values separated by whitespace
  • bin_memory_file.mem - a text file containing binary values separated by whitespace
  • memory_array - name of Verilog memory array of the form: reg [n:0] memory_array [0:m]
  • start_address - where in the memory array to start loading data (optional)
  • end_address - where in the memory array to stop loading data (optional)

The following shows a very simple simulation module using $readmemh:

module readmemh_tb();
    reg [7:0] test_memory [0:15];
    initial begin
        $display("Loading rom.");
        $readmemh("rom_image.mem", test_memory);
    end
endmodule

The test memory has 16 locations [0:15](depth) each of 8 bits [7:0](data width).

Memory File Syntax

The hex_memory_file.memor bin_memory_file.memfile consists of text hex/binary values separated by whitespace: space, tab, and newline all work. You can mix the whitespace types in one file. Comments are the same as regular Verilog files: // begins a comment.

The width of a data value in the file mustn’t be wider than the data width of the array; otherwise, that value will be truncated. In Vivado, you’ll see a warning in the log WARNING: Data truncated while reading Datafile.

Verilog Examples

The following examples show $readmemhand $readmembwith a range of different initialization files. Some older tools are really picky; if you have issues, you may need to avoid comments and mixing whitespace.

1) Four 16-bit data values in hex

reg [15:0] ex1_memory [0:3];
$readmemh("ex1.mem", ex1_memory);
dead
beef
0a0a
1234

2) Sixteen 8-bit data values in hex (mixing spaces and newlines)

reg [7:0] ex2_memory [0:15];
$readmemh("ex2.mem", ex2_memory);
ab cd ef 01  // this is a comment
ef 22 1e 00
9f ff 13 e6
ce b7 28 8f

3) Six 3-bit values in binary

reg [2:0] ex3_memory [0:5];
$readmemb("ex3.mem", ex3_memory);
001 101 111 111 101 001

4) Six 16-bit values in hex starting at array position 4

reg [15:0] ex4_memory [0:255];
$readmemh("ex4.mem", ex4_memory, 4);
dead beef 0a0a 1234 abab 987e

NB. As demonstrated by this example, the memory array can have more entries than the data file.

Yosys

Yosys automatically picks up $readmemhand $readmembfiles, but you need to include the path if the memory file isn’t in the same directory as the Verilog module. You can see examples of this in the iCEBreaker designs for Hardware Sprites.

Vivado

The easy way to get memory files working with Vivado is to give them the .memextension then add them to your project. You do this as you would for a design or simulation source using “Add Sources” then selecting “Files of type: Memory Initialization Files”. Vivado will automatically identify them as memory files and place them in the current working directory during simulation etc.

If you reference a file but don’t add it to the project, you’ll get an error of the form:

WARNING: File rom_image.mem referenced on acme.v at line 42 cannot be opened for reading. Please ensure that this file is available in the current working directory.

Once you’ve added the files to your project, they will show up in the Sources view under Design or Simulation Sources.

PS. The wonderful image of the Micron MT4C1024 DRAM used in the social media card for this post comes from Zeptobars and is licensed under a Creative Commons licence.
Get in touch: GitHub Issues, 1BitSquared Discord, @WillFlux (Mastodon), @WillFlux (Twitter)

想深耕嵌入式?这个专辑值得收藏

MCU、FPGA、工控、传感器一站式学习,实战项目直接抄

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值