在仿真中,定义队列,使用任务进行函数传递,并传递文件名,传递队列,进行打印
$sformat(filename, "./data_log/%0d_%0d_%0d_0.txt", f_num, lane_num,dt);
使用此函数可以自定义字符串,在仿真的时候进行文件命名和传递数据的场景很多。
python
int file;
task writeQueueToFile(string filename, reg [7:0] queue[$]);
if (filename == "") begin
$display("Error: Invalid file name");
return;
end
file = $fopen(filename, "w");
if (file == 0) begin
$display("Error: Unable to open file '%s'", filename);
return;
end
foreach (queue[i]) begin
$fwrite(file, "%d\n", queue[i]);
end
$fclose(file);
$display("Queue contents written to file '%s'", filename);
endtask
创建一个data_log文件夹,存入文件,数据可以传入数字类型
python
$sformat(filename, "./data_log/%0d_%0d_%0d_0.txt", f_num, lane_num,dt);
writeQueueToFile(filename, dl0_data);
完整的测试方案
python
`timescale 1ns/1ns
module test_q_w (
);
reg [7:0]dl0_data[$];
string filename;
bit [1:0] lane_num;
bit [10:0] f_num;
reg [07:00] dt ;
reg [31:00] data_in;
initial begin
dl0_data.delete();
end
int file;
task writeQueueToFile(string filename, reg [7:0] queue[$]);
if (filename == "") begin
$display("Error: Invalid file name");
return;
end
file = $fopen(filename, "w");
if (file == 0) begin
$display("Error: Unable to open file '%s'", filename);
return;
end
foreach (queue[i]) begin
$fwrite(file, "%d\n", queue[i]);
end
$fclose(file);
$display("Queue contents written to file '%s'", filename);
endtask
bit clk;
bit wr_en;
int wc= 'd500;
initial begin
clk = 0;
forever begin
#10ns; clk = ~clk;
end
end
always @(posedge clk) begin
if(wr_en=='d1)begin
dl0_data.push_back(data_in);
data_in =data_in+1'b1;
if (data_in== wc)begin
wr_en='d0;
end
end
end
always @(negedge wr_en) begin
$sformat(filename, "./data_log/%0d_%0d_%0d_0.txt", f_num, lane_num,dt);
writeQueueToFile(filename, dl0_data);
dl0_data.delete();
end
initial begin
wr_en =1;
data_in =0;
// #10us
f_num='d1;
lane_num ='d1;
dt = 'd1;
end
initial begin
$display("fsdbDumpfilrs is start at %d",$time);
$fsdbDumpfile("verilog.fsdb");
$fsdbDumpvars();
#1e9;
$finish;
end
endmodule
仿真结果
往文本中写入了500个数据