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