首页 > 解决方案 > Read and write from txt in Verilog

问题描述

First of all I want to say that I'm running the simulation in ADS (Advanced Design System 2017) through a Verilog model compiled in ModelSim.

My objective is loading data from a .txt file into the testbench as input in order to run the simulation, and afterwards save the results of this simulation in another .txt file.

Here is the content for the input test .txt file called "param.txt":

1
2
3
4
5

And here is my Verilog testbench code:

`include "disciplines.vams"


module resistor(p,n);
electrical p,n;
parameter real R=50.0;
integer file;
integer out;
real pwm_A[0:10];
integer i;
integer count;


analog begin

    @(initial_step) // Initial Conditions
    begin


////////////// Read

file=$fopen("param.txt","r");

    if (file)  $display("File was opened successfully : %0d", file);
    else       $display("File was NOT opened successfully : %0d", file);

    for (i=1; i<=5; i=i+1) begin  
         count = $fscanf(file,"%d",pwm_A[i]);
    end



////////////// Write


out=$fopen("out.txt","w");

    for (i=1; i<=5; i=i+1) begin  
        $fwrite(out,"%d\n",pwm_A[i]);
    end


$fclose(file);
$fclose(out);


end

// Simulation (doesnt matter)
V(p,n) <+ R * I(p,n);


end
endmodule

The simulation throws this error:

Error: Incorrect target supplied to integer-valued $fscanf field.

May anyone spot the problem?

Thanks in advance.

标签: filesaveverilog

解决方案


您已声明pwm为实数数组,同时使用%d 格式说明符读取和写入文件。你需要要么

i) 更改pwm为整数数组或

ii) 将%d格式说明符更改%f$fscanfand$fwrite系统函数调用。

格式说明%d符期望读取并将写入十进制整数。


推荐阅读