在 Verilog 中读取和写入 txt

Read and write from txt in Verilog

首先我想说的是,我正在 运行通过在 ModelSim 中编译的 Verilog 模型在 ADS(高级设计系统 2017)中进行仿真。

我的 objective 正在将 .txt 文件中的数据作为输入加载到测试台中,以便 运行 进行模拟,然后将此模拟的结果保存在另一个 .txt 文件中。

这是名为 "param.txt" 的输入测试 .txt 文件的内容:

1
2
3
4
5

这是我的 Verilog 测试平台代码:

`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

模拟抛出这个错误:

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

有人能发现问题吗?

提前致谢。

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

i) 将 pwm 更改为整数数组或

ii) 在 $fscanf$fwrite 系统函数调用中将 %d 格式说明符更改为 %f

%d 格式说明符需要读取和写入一个十进制整数。