将 Verilog 中的二进制文件数据读入二维数组 [英] Read binary file data in Verilog into 2D Array

查看:78
本文介绍了将 Verilog 中的二进制文件数据读入二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要从二进制文件加载的数组:

I have an array that I want to load up from a binary file:

parameter c_ROWS = 8;
parameter c_COLS = 16;
reg [15:0]  r_Image_Raw[0:c_ROWS-1][0:c_COLS-1];

我的输入文件是二进制数据,256 字节长(与 r_Image_Raw 的总空间相同).我尝试使用 $fread 来完成此操作,但它仅适用于最后一行的第 4 列:

My input file is binary data, 256 bytes long (same total space as r_Image_Raw). I tried using $fread to accomplish this, but it only works through the 4th column of the last row:

n_File_ID = $fopen(s_File_Name, "r");
n_Temp = $fread(r_Image_Raw, n_File_ID);

为此我也尝试使用 $fscanf,但在打开综合工具时出现关于打包类型的错误:

I also tried using $fscanf for this, but I get an error about packed types when opening the synthesis tool:

while (!$feof(n_File_ID))
  n_Temp = $fscanf(n_File_ID, "%h", r_Image_Raw);

我觉得这应该很容易做到.我是否创建了一个 2D for 循环并遍历 r_Image_Raw 变量,一次读取 16 位?我觉得应该没那么复杂.

I feel like this should be easy to do. Do I have create a 2D for loop and loop through the r_Image_Raw variable, reading in 16 bits at a time? I feel like it should not be that complicated.

推荐答案

我意识到了我的错误.应该是:

I realized my mistake. It should be:

n_File_ID = $fopen(s_File_Name, "rb");
n_Temp = $fread(r_Image_Raw, n_File_ID);

我使用r"而不是rb"来指定它是一个二进制文件.有趣的是,r"确实适用于大部分数据,但无法从文件中读取最后约 13 个位置.

I was using "r" and not "rb" to specify that it was a binary file. Interestingly enough, "r" does work for the majority of the data, but it is unable read in the last ~13 locations from the file.

这篇关于将 Verilog 中的二进制文件数据读入二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆