fseek没有在系统verilog中工作

时间:2017-10-08 22:26:16

标签: system-verilog

我似乎无法读取hex文件中的第4个字节:

 module file_read();
   integer fd,file_char;
   logic [7:0] captured_data;

   initial begin
        fd = $fopen("README.lz4", "rb");
        while (!$feof(fd)) begin
               file_char=$fseek(fd,5,0);
               $display("file char is %h",file_char);
       end
    end
endmodule

文件内容是......

 00000000  04 22 4d 18 64 40 a7 43....
 .........

再次感谢!

1 个答案:

答案 0 :(得分:0)

$fseek不会从文件中读取字符,它只是指定文件中应该从哪里读取字符。相反,您可以使用$fgetc

module file_read();
   integer fd,file_char,status;
   logic [7:0] captured_data;

   initial begin
        fd = $fopen("README.lz4", "rb");
        status=$fseek(fd,4,0);
        while (!$feof(fd)) begin
          file_char=$fgetc(fd);
          $display("file char is %h",file_char);
       end
    end
endmodule

所以,我已经将调用移到了循环外的$fseek - 我们只想根据你描述的格式执行此操作。假设您想跳过00000000,我还将偏移量更改为4。我已将$fseek的返回值指定的变量更改为status,因为这是从$fseek返回的内容:

  

0表示成功,-1表示错误

此外,$fgetc会返回-1来表示文件的结尾。因此,您可能希望将while循环更改为do while循环,并在最后测试file_char的值(并删除$feof )例如

module file_read();
   integer fd,file_char,status;
   logic [7:0] captured_data;

   initial begin
        fd = $fopen("README.lz4", "rb");
        status=$fseek(fd,4,0);
        assert (status);
        do begin
          file_char=$fgetc(fd);
          $display("file char is %h",file_char);
        end
        while (file_char != -1); 
    end
endmodule

(鉴于缺少一个文件来测试它们,我没有测试过这两个代码块。)