如何在verilog中逐行读取文本文件?

问题描述 投票:0回答:3

我有一个 SREC 文件,它是一个简单的文本文件,我想在 verilog 中逐行读取它。我怎样才能做到这一点?

file-io verilog
3个回答
21
投票

以下内容读取文件,每个时钟周期读取 1 行:预期数据格式为每行一个十进制数。

integer               data_file    ; // file handler
integer               scan_file    ; // file handler
logic   signed [21:0] captured_data;
`define NULL 0    

initial begin
  data_file = $fopen("data_file.dat", "r");
  if (data_file == `NULL) begin
    $display("data_file handle was NULL");
    $finish;
  end
end

always @(posedge clk) begin
  scan_file = $fscanf(data_file, "%d\n", captured_data); 
  if (!$feof(data_file)) begin
    //use captured_data as you would any other wire or reg value;
  end
end

4
投票

谢谢您的解决方案。 我对其进行了一些修改,以使用每行包含 32 个十六进制数字的 2 个 .txt 文件,并发现了一些困难,因为我不明白每行代码的作用。我的发现如下。

仅 vars 和 reg 声明

////////I'm using inputs.txt and outputs.txt to read both lines at the same time
module Decryption_Top_Testbench;
////////TEXT DOC variables

integer               file_outputs    ; // var to see if file exists 
integer               scan_outputs    ; // captured text handler
integer               file_inputs     ; // var to see if file exists
integer               scan_inputs     ; // captured text handler

//TXT
reg [127:0] captured_outputs; ///Actual text obtained from outputs.txt lines
reg [127:0] captured_inputs;  ///Actual text obtained from inputs.txt lines

打开两个文件

initial 
begin

 // TEXT FILE outputs///////////////////////

  file_outputs = $fopen("C:/outputs.txt", "r"); //Opening text file

//you should use the full path if you don't want to get in the trouble 
//of using environment vars 

    if (file_outputs == 0) begin               // If outputs file is not found
      $display("data_file handle was NULL"); //simulation monitor command
      $finish;
    end
  // TEXT FILE inputs///////////////////////
    file_inputs = $fopen("C:/inputs.txt", "r"); //Opening text file (inputs)
      if (file_inputs == 0) begin               //If inputs file is not found
        $display("data_file handle was NULL");
        $finish;
      end
end

在这部分,我将以十六进制格式逐行读取并将其存储在“captured_outputs”寄存器和“captured_inputs”寄存器中。

///Since I'm using it just to simulate I'm not interested on a clock pulse,
/// I want it to happen all at the same time with whatever comes first

always @(* )
begin

   if (!$feof(file_outputs)) 
   begin
   ///!$feof means if not reaching the end of file
   ///file_outputs is always returning a different number other than "0" if the doc 
   ///has not ended. When reaching "0" it means the doc is over.
   ///Since both of my docs are the same length I'm only validating one of them
   ///but if you have different lenghts you should verify each doc you're reading

   ///

   scan_inputs = $fscanf(file_inputs, "%h\n", captured_inputs);        //Inputs Line text
   scan_outputs = $fscanf(file_outputs, "%h\n", captured_outputs);     //Outputs line text

   $display ("Line :[inputs: %h _ outputs: %h ]" captured_inputs, captured_outputs);  
   // Displaying each line at the simulation monitor

   ///$fscanf means formatted text, $scanf would read text ignoring the format
   /// %h\n means it should expect HEX numbers and the end of line character, that means 
   /// the line is over, but if you want to use a diff criteria 
   /// you can replace \n to whatever you may need 



   end

   else
   begin

   $finish;
   $fclose(file_outputs); //Closing files just in case to prevent wasting memory
   $fclose(file_inputs);

   end

end

我只是想贡献一些东西,让任何开始用 Verilog 编码的人都能理解并将这个伟大的功能附加到他/她的项目中。

享受吧!


0
投票
integer               data_file    ; // file handler
integer               scan_file    ; // file handler
logic   signed [21:0] captured_data;
`define NULL 0    

initial begin
  data_file = $fopen("data_file.dat", "r");
  if (data_file == `NULL) begin
    $display("data_file handle was NULL");
    $finish;
  end
end

always @(posedge clk) begin
  scan_file = $fscanf(data_file, "%d\n", captured_data); 
  if (!$feof(data_file)) begin
    //use captured_data as you would any other wire or reg value;
  end
end

一旦我们在always块之后定义了scanf函数,那么我们需要在eof(文件结尾)的if块中放入什么,您能解释一下吗

就像我正在做 18 位 2 输入乘法,并且我有 10 个不同的 a 和 b(输入)输入,那么我应该如何使用

© www.soinside.com 2019 - 2024. All rights reserved.