System file I/O tasks

LRM §14.2.

System file I/O tasks are used for files on disk.

Syntax:

mcd = $fopen("file_name" [ , type ] );
$fclose(mcd);
$readmemb("file_name", memory_name [ , start_address [ , end_address ]] );
$readmemh("file_name", memory_name [ , start_address [ , end_address ]] );
c = $fgetc(mcd);
ungetc(c, mcd);
i = $fgets(str, mcd);
i = $fscanf(mcd, "text", signal, signal, ...);
i = $sscanf(str, "text", signal, signal, ...);
i = $fread(reg_or_mem, mcd [ , start_address [ , end_address ]] );i = $ftell(mcd);
i = $fseek(mcd, offset, operation);
i = $rewind(mcd);
$fflush( [ mcd ] );
i = $ferror(mcd, str);
$swrite(output_reg, signal, signal, ...));
$sformat(output_reg, text", signal, signal, ...); 

mcd = expression       // multi-channel descriptor; in Verilog-2001 file descriptor

Description:

$fopen opens a disk file for writing, and returns a 32-bit unsigned integer multi-channel descriptor pointer to the file. It returns a zero if the file could not be opened for writing. In Verilog-2001 the type indicates how the file is to be opened. The "b" distinguishes a binary file from a text file:

Type

Description

"r" or "rb"

Open for reading

"w" or "wb"

Create for writing

"a" or "ab"

Append

"r+" or "rb+" or "r+b"

Open for update (reading and writing)

"w+" or "wb+" or "w+b"

Create for update

"a+" or "ab+" or "a+b"

Append; open or create for update at end-of-file

$fclose closes a disk file that was opened by $fopen.

$readmemb and $readmemh initialize a memory array with the values from the file. The file must be an ASCII file with values represented in binary ($readmemb) or hexadecimal ($readmemh). The data values must be the same width as the memory array, and be separated by white spaces. The start and end address are hexadecimal numbers, even for $readmemb, preceded by @.

Verilog-2001 adds several new reading tasks: $fgetc reads a byte (a character) from the file. $fgets reads a line from the file. $fscanf and $sscanf reads data and interprets the data according to a format. $fscanf reads the data from a file whereas $sscanf reads the data from a reg variable. $fread reads binary data from the file. $ungetc inserts a specified character into a buffer specified by the file.

Other new Verilog-2001 file I/O tasks are: $ftell returns the offset from the beginning of the file. $fseek sets the position of the next input or output operation on the file. $rewind is the same as $fseek(0,0). $fflush writes any buffered output to the file. $ferror can be used to obtain more information about an error. $swrite and $sformat writes a string to a reg variable. $sformat interprets the string as a format string.

The system display tasks can be used to write to a file.

Example:

initial begin
  File = $fopen("Result.dat");
  if (!File)
    $display("Could not open \"result.dat\"");
  else begin
    $display(File, "Result is: %4b", A);
    $fclose(File);
  end
end

reg [7:0] Memory [15:0];
initial begin
  $readmemb("Stimulus.txt", Memory);
end

See also:

Display tasks