Skip to content

Instantly share code, notes, and snippets.

@varunhuliyar
Last active February 6, 2022 04:31
Show Gist options
  • Select an option

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

Select an option

Save varunhuliyar/f1af230f768fc66233f23b453df5ef0a to your computer and use it in GitHub Desktop.
function void writing_binary_file(int unsigned total_bytes_to_write=32);
int fwrite;
bit [7:0] wdata;
//Open file with binary write mode("wb"), b here specifies binary
fwrite=$fopen("binary_file.bin","wb");
if(fwrite==0)
`uvm_error("NO_FILE_FOUND","Couldn't open file binary_file.bin for writing")
//iterate until all intended bytes are written
for(int i=0,i<total_bytes_to_write,i++) begin
//generate current random byte to write
wdata=$urandom_range(0,255);
//fwrite with %c writes only 1 byte at a time, use "%u" format for writing 32bit data
$fwrite(fwrite,"%c",wdata);//Note that fwrite doesn't introduce newline character unless added with \n
end
//close the file
$fclose(fwrite)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment