Skip to content

Instantly share code, notes, and snippets.

@varunhuliyar
Last active June 7, 2024 14:03
Show Gist options
  • Select an option

  • Save varunhuliyar/edc0c63dd2e6eedd43d6803a16c09a62 to your computer and use it in GitHub Desktop.

Select an option

Save varunhuliyar/edc0c63dd2e6eedd43d6803a16c09a62 to your computer and use it in GitHub Desktop.
function void read_binary_file();
int fread;
bit [7:0] rdata;
int unsigned line_count;
line_count=0;
//Open file with binary read mode("rb"), b here specifies binary
fread=$fopen("binary_file.bin","rb");
if(fread==0)
`uvm_error("NO_FILE_FOUND","Couldn't open file binary_file.bin for reading")
//read until end of file is reached
while(!$feof(fread)) begin
//fgets reads one line at once and $fgetc reads a byte (a character) at once
$fgets(rdata,fread);
line_count+=1;
//Do what's required of data here
`uvm_info(get_name(),$sformatf("byte number %0d, Value= %08x",line_count,rdata),UVM_MEDIUM)
end
//close the opened binary file
$fclose(fread)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment