verilog从txt中读取_将Verilog中的二进制文件数据读入2D数组_百度文 ...

verilog从txt中读取_将Verilog中的⼆进制⽂件数据读⼊2D数组在 example 下⾯,⽤于从带有systemverilog的⼆进制⽂件中读取 .
如IEEE SV标准⽂档中所⽰, "nchar_code" 将返回读取的字节数/字符数 . 如果在上次读取时已经达到EOF,则该数字将为零 . 请注意,“nchar_code ”可以为零 but 尚未达到EOF,如果在数据⽂件的末尾有空格或返回,则会发⽣这种情况 .
您可以使⽤$ fread函数控制要读取的字节数 . 这是通过以下⽰例的"data_write_temp"或"mem"的 type definition 完成的 . 如果
"data_write_temp" 变量是16位长,那么每次调⽤$ fread时它将读取16位 . 此外,$ fread将返回"nchar_code=2",因为16位是
2bytes . 如果"data_write_temp"是⽰例中的32位,则$ fread将读取nchar_code = 4bytes(32bits) . 您还可以定义⼀个数组,$ fread 函数将尝试填充该数组 .
让我们定义⼀个多维数组 mem .
logic [31:0] mem [0:2][0:4][5:8];
在⽰例单词内容中, wzyx ,
-w shows the start of the word
-z corresponds to words of the [0:2] dimension (3 blocks).
-y corresponds to words of the [0:4] dimension (5 rows).
-x corresponds to words of the [5:8] dimension (4 columns).
该⽂件的结构如下(注意@z显⽰z维块):
棕蝠@0 w005 w006 w007 w008
w015 w016 w017 w018
w025 w026 w027 w028
w035 w036 w037 w038
w045 w046 w047 w048
@1 w105 w106 w107 w108
w115 w116 w117 w118
w125 w126 w127 w128
w135 w136 w137 w138
w145 w146 w147 w148
@2 w205 w206 w207 w208
w215 w216 w217 w218
w225 w226 w227 w228
w235 w236 w237 w238峨眉山暗岩事件
w245 w246 w247 w248
在前⾯的结构中,数字显⽰每个维度的索引 . 例如w048表⽰索引z = 0,索引y = 4和索引x = 8的字w(32位)值 .
现在, you have many ways to read this . 您可以使⽤上⾯声明的类型"mem"在 single shot 中读取所有内容,或者您可以执⾏while循环,直到EOF使⽤32位的"data_write_temp"变量读取32位的⽚段 . 如果你想对每个单词作品进⾏⼀些检查,那么循环就很有意思,你对内存值不感兴趣 .
如果选择了多维数组/单次读取,则可以使⽤ $fread 或使⽤SV标准中定义的特定函数$ readmemh .
$readmemh("mem.data", mem, 1, (3*5*4));
st天华
相当于
税务登记管理办法$readmemh("mem.data", mem);
$ readmemh使您⽆需打开/关闭⽂件 .
如果您使⽤ $fread 进⾏⼀次阅读
logic [31:0] mem [0:2][0:4][5:8];
register_init_id = $fopen("mem.data","rb");
nchar_code = $fread(mem, register_init_id);
if (nchar_code!=(3*5*4)*4)) begin
`uvm_error("do_read_file", $sformatf("Was not possible to read the whole expected bytes"));
end
$fclose(register_init_id);
如果你想⽤32b字读取做⼀个循环 . 然后看下⾯的例⼦ .
该⽰例使⽤从⽂件读取的数据使⽤AHB验证组件写⼊AHB总线 .
logic [31:0] data_write_temp;
...
//DO REGISTER FILE
register_init_id = $fopen("../../software/binary.bin","rb");
if (register_init_id==0) begin `uvm_error("do_read_file", $sformatf("Was not possible to open the register_init_id file")); end
count_32b_words=0;
while(!$feof(register_init_id)) begin
nchar_code = $fread(data_write_temp, register_init_id);
if ((nchar_code!=4)||(nchar_code==0)) begin
if (nchar_code!=0) begin
`uvm_error("do_read_file", $sformatf("Was not possible to read from file a whole 4bytes word:%0d",nchar_code));
end
end else begin
tmp_ahb_address = (pnio_pkg::conf_ahb_register_init_file_part1 + 4*count_32b_words);
data_write_temp = (data_write_temp << 8*( (tmp_ahb_address)%(DATAWIDTH/(8))));//bit shift if necessary not aligned to 4 bytes
`uvm_create_on(m_ahb_xfer,p_sequencer.ahb0_seqr);
assert(m_ahb_xfer.randomize(* solvefaildebug *) with {
write == 1;//perform a write
HADDR == tmp_ahb_address;
HSIZE == SIZE_32_BIT;
HBURST == HBURST_SINGLE;
HXDATA.size() == 1; //only one data for single bust HXDATA[0] == data_write_temp;
}) else $fatal (0, "Randomization failed"); //end assert `uvm_send(m_ahb_xfer);
count_32b_words++;
end //end if there is a word read
end //end while
福建交通厅厅长
曲面评价$fclose(register_init_id);

本文发布于:2024-09-22 03:30:40,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/651310.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:读取   数组   字节数   函数   数据   结构   数字
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议