【问题标题】:Read and write from txt in Verilog在 Verilog 中从 txt 读取和写入
【发布时间】:2019-03-07 22:16:53
【问题描述】:

首先我想说的是,我正在通过在 ModelSim 中编译的 Verilog 模型在 ADS(Advanced Design System 2017)中运行仿真。

我的目标是将 .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.

谁能发现问题?

提前致谢。

【问题讨论】:

  • 不是重复的,只是另一个问题!
  • 嗨 davmc!是的,但是您的源代码仍然包含与上一个问题相同的问题,其中 Matthew 建议将 pwm_A[i]=$fscanf(file,"%d",j); 更改为 count = $fscanf(file,"%d",pwm_A[i]);
  • 您将pwm_A 声明为real。您是否尝试将其声明为integer?根据您提供的错误消息,这可能是问题所在。如果您确实需要加载实数而不是整数,您可能需要更改 %d 说明符。有关几个 C 说明符,请参见 this reference
  • C 和 Verilog-AMS 格式的输入和输出之间存在差异。选择 Verilog-AMS 标准accellera.org/images/downloads/standards/v-ams/VAMS-LRM-2-4.pdf

标签: file save verilog


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2015-05-29
    • 1970-01-01
    • 2015-06-04
    • 2023-03-06
    相关资源
    最近更新 更多