The Verilog language has a rich set of system functions to write files ($fdisplay, $fwrite, etc.) but only reads files with a single, fixed format ($readmem). In the past if you wanted to read a file that was not in $readmem format, you would have to learn the Programming Language Interface (PLI) and the C language, write C code to read the file and pass values into Verilog, then debug the combined C and Verilog code. In addition, the Verilog is limited to 32 open files at a time.
To Write into a file ->
module tb;
integer fl1;
initial begin
fl1 = $fopen("file.xyz");
forever begin
@(posedge clk)
$fdisplay(fl1, "value to print = %h", value);
end
$fclose(fl1);
end
endmodule
Read a file in Verilog.
file = $fopen("filename");
Reading data for per clock cycle -> (In System Verilog)
integer data_file ; // file handler
integer scan_file ; // file handler
logic signed [21:0] capture_data;
`define NULL 0
initial begin
data_file = $fopen("data_file.txt", "r");
if (data_file == `NULL) begin
$display("data_file handle was NULL");
$finish;
end
end
always @(posedge clk) begin
scan_file = $fscanf(data_file, "%d\n", capture_data);
if (!$feof(data_file)) begin //use capture_data;
end
end
NEXT