本文介绍了在Verilog中从txt读取和写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的目标是将.txt文件中的数据作为输入加载到测试台中,以运行模拟,然后将模拟结果保存到另一个.txt文件中.

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.

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

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

1
2
3
4
5

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

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

模拟抛出此错误:

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

有人可以发现问题吗?

谢谢.

推荐答案

您已声明pwm为实数数组,同时使用%d format指定符读取和写入文件.您需要

You have declared pwm as an array of reals whilst using the %d format specifier to both read and write the file. You need to either

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

i) change pwm to be an array of integers or

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

ii) change the %d format specifier to %f in the $fscanf and $fwrite system function calls.

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

The %d format specifier is expecting to read and will write a decimal integer.

这篇关于在Verilog中从txt读取和写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:32