Readmemh behaviour when file is not accessible

From the 2017 LRM section 21.4

I can see the following “When $readmemh or $readmemb is given a file with address entries, initialization of the specified highest
dimension words is done. If the file contains insufficient words to completely fill a highest dimension word,
then the remaining subwords are left unchanged.”

But in the LRM there is no specification of what should happen when the file is not present or no permissions are given, since this is probably OS dependant probably is not specified but there is some clarification for $writememh

“If filename exists at the time $writememb or $writememh is called, the file will be overwritten (i.e., there
is no append mode).”

So what would be the expected behaviour if the file cannot be read (I’d expect error but some tools just issue a warning) and why the LRM has no mention about it. or maybe having a return value would be an improvement.

Regards,
-Ronald

In reply to rgarcia07:

Any of the file I/O system routines can use
$ferror
get the return code and string message from the last routine executed.

In reply to dave_59:

In reply to rgarcia07:
Any of the file I/O system routines can use
$ferror
get the return code and string message from the last routine executed.

Hi Dave is possible to share a quick snippet showing how to use $ferror along with $readmem*, from the LRM I see it requires a file descriptor but $readmem* system functions/tasks do not seem to return any compared to $fopen, ideally I don’t want to use $fopen + $fclose and then $readmemh just to have a “tool independent” mechanism for ensuring that if the file does not exist an error is raised.

For example:


module test();
  
  integer fd;
  integer errno;
  string error_msg;
  bit [31:0] mem_array[];
  string filename = "mem.hex";
  
  
  initial begin
    //     fd = $readmemh(filename, mem_array); // Compile error
    $readmemh(filename, mem_array); //Tool dependant some give warning others error
    //     fd = $fopen("mem.hex", "r"); //This works as the file is not existing 
    errno = $ferror(fd, error_msg); // what to pass as first argument after using $readmem*
    $display("ERROR NUM = %0d MSG = %s", errno, error_msg);
    
  end
  
endmodule

Apologies if my requirement is not clear

-R

In reply to rgarcia07:

It looks like there is an outstanding issue in the LRM trying to use $ferror for file opening issues.

Most tools have tool message severity handler so you can promote specific warnings to errors or fatals. Otherwise you would have to write some OS-specific DPI code to get information about the existence of a file and its permissions.