I am trying to create a task that reads in a hex file (raw video data) and injects it axis stream. I am looking for some guidance on the System Verilog file operations work. My pixels are 16-bits but for now, all I am trying to do is read single bytes in from the file. Here is the code I have so far:
task task_file_to_axis(
input [1024*8-1:0] file_name,
input [31:0] frame_width,
input [31:0] frame_height,
input [31:0] num_frames,
output error
);
integer file_handler;
logic [31:0] pixel;
begin
file_handler = $fopen (file_name, "rb");
if(file_handler == 0) begin
$display("Failed to open file: %0s", file_name);
$stop;
end
$fread(pixel, file_handler);
$display("pixel read: %h", pixel);
end
endtask
All this does is read the first 4 bytes (because I made the pixel [31:0]). I am wondering how I can modify this to work in a loop. Is there someway to put an offset into the $fread command?
Here I tried to use $fscanf in a loop to store the values in a 2d array, but this doesn’t work:
reg [15:0] frame[0:18432];
begin
file_handler = $fopen (file_name, "rb");
if(file_handler == 0) begin
$display("Failed to open file: %0s", file_name);
$stop;
end
for(int i = 0; i < 5; i++) begin
pixel = $fscanf(file_handler, "%h", frame[i] );
$display("pixel read: %h", frame[i]);
end
Just a quick update because I realized at least one of my problems. I was using “%h” in the $fscanf function, which does not work. I changed it to %u so now its reading the 16 bits into my array. Also changed the variable it outputs into to “status”. I assume status = 1 is good, and status = 0 means something didn’t work?
integer file_handler;
int status;
reg [15:0] frame[0:18432];
begin
file_handler = $fopen (file_name, "rb");
if(file_handler == 0) begin
$display("Failed to open file: %0s", file_name);
$stop;
end
for(int i = 0; i < 5; i++) begin
status = $fscanf(file_handler, "%u", frame[i] );
$display("pixel read: %h", frame[i]);
$display("read status: %d", status);
end
The $fscanf system function can be used to format data as it is read from a file. For example: integer code ;
code = $fscanf ( fd, format, args );
$fscanf reads from the files specified by the file descriptor fd.
The number of successfully matched and assigned input items is returned in code; this number can be 0 in the event of an early matching failure between an input character and the control string. If the input ends before the first matching failure or conversion, EOF (-1) is returned. Applications can call $ferror to determine the cause of the most recent error (see 21.3.7).