在 Verilog 中从 txt 读取和写入数组
Read and write array from txt in Verilog
首先我想说的是,我正在 运行通过在 ModelSim 中编译的 Verilog 模型在 ADS(高级设计系统 2017)中进行仿真。
我的 objective 正在将数据从 .txt 文件加载到测试平台作为输入,以便 运行 模拟,然后将模拟结果保存在另一个 .txt 文件中。
这是名为 "param.txt" 的输入测试 .txt 文件的内容:
1
2
3
4
5
6
7
8
9
10
这是我的 Verilog 测试平台代码:
`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
模拟日志没有错误或警告:
File was opened successfully : -32
但是名为 "out.txt" 的输出 .txt 文件生成了这个:
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
123
代替原始数据。
值得注意的是,手动引入的值'123'写入没有问题,但其余数据为'0'或'1'。
有人能发现问题吗?
提前致谢。
这行在这里
pwm_A[i]=$fscanf(file,"%d",j);
应该阅读
$fscanf(file,"%d",pwm_A[i]);
$fscanf
系统函数 与 $fdisplay
相反 - 该函数输出到其参数列表中的变量。它 returns 从行中成功读取的项目数。这将(希望)前十行为 1,下一行为 0。所以,你真的应该检查那个数字,如果它不是你期望的,就做点什么,例如:
count = $fscanf(file,"%d",pwm_A[i]);
if (count != 1)
// whatever
首先我想说的是,我正在 运行通过在 ModelSim 中编译的 Verilog 模型在 ADS(高级设计系统 2017)中进行仿真。
我的 objective 正在将数据从 .txt 文件加载到测试平台作为输入,以便 运行 模拟,然后将模拟结果保存在另一个 .txt 文件中。
这是名为 "param.txt" 的输入测试 .txt 文件的内容:
1
2
3
4
5
6
7
8
9
10
这是我的 Verilog 测试平台代码:
`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
模拟日志没有错误或警告:
File was opened successfully : -32
但是名为 "out.txt" 的输出 .txt 文件生成了这个:
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
123
代替原始数据。
值得注意的是,手动引入的值'123'写入没有问题,但其余数据为'0'或'1'。
有人能发现问题吗?
提前致谢。
这行在这里
pwm_A[i]=$fscanf(file,"%d",j);
应该阅读
$fscanf(file,"%d",pwm_A[i]);
$fscanf
系统函数 与 $fdisplay
相反 - 该函数输出到其参数列表中的变量。它 returns 从行中成功读取的项目数。这将(希望)前十行为 1,下一行为 0。所以,你真的应该检查那个数字,如果它不是你期望的,就做点什么,例如:
count = $fscanf(file,"%d",pwm_A[i]);
if (count != 1)
// whatever