在Verilog中从txt读取和写入数组 [英] Read and write array from txt in Verilog

查看:359
本文介绍了在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
6
7
8
9
10

这是我的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:50];
integer i;
integer j=1;


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<50; i=i+1) begin  
        pwm_A[i]=$fscanf(file,"%d",j);
        j = j+1;
    end



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


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

    for (i=1; i<=15; i=i+1) begin  

        $fwrite(out,"%d\n",pwm_A[i]);

    end

// Trying to manually display data
$fdisplay(out,123);


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


end

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


end
endmodule

模拟日志中没有错误或警告:

The simulation log has no errors or warnings:

File was opened successfully : -32

但是名为"out.txt"的输出.txt文件会生成以下内容:

But the output .txt file called "out.txt" generates this:

1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
123 

代替原始数据.

值得注意的是,手动引入的'123'值没有问题,但是其余数据为'0'或'1'.

It's noticeable that the '123' manually introduced value has no problems to be written, but the rest of the data are '0' or '1'.

有人可以发现问题吗?

谢谢.

推荐答案

此行

pwm_A[i]=$fscanf(file,"%d",j);

应阅读

$fscanf(file,"%d",pwm_A[i]);

$fscanf 系统函数$fdisplay的opposisite-函数输出到其参数列表中的变量中.它返回从行中成功读取的项目数. (希望)前十行为1,下一行为0.因此,您应该真正检查该数字,如果不是您期望的数字,请执行一些操作,例如:

The $fscanf system function is the opposisite of $fdisplay - the function outputs into variables in its argument list. It returns the number of items successfully read from the line. This will (hopefully) be 1 for the first ten lines and will be 0 for the next lines. So, you should really check that number and do something if it's not what you expect, eg:

count = $fscanf(file,"%d",pwm_A[i]);
if (count != 1) 
  // whatever

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆