Welcome ! This website will help YOU (recent graduates/professionals) learn verification languages like SystemVerilog and UVM. Register for free and access more content !

It's not uncommon for testbenches to have the ability to dump information into files that other verification teams can work on. For example, a graphics engine might process data and write into a memory model, which can be captured by the testbench and dumped into a file. Another team that works on the verification of a display controller can use this valid data to functionally verify their block.

Opening and closing files

  fd = $fopen (filename, type)

The function $fopen opens the file specified as the filename argument and returns a 32-bit file descriptor. filename is a string expression that names the file to be opened. type is also a string expression that indicates how the file should be opened. If type is omitted, the file is opened for writing, and a multichannel descriptor is returned. If type is specified, the file is opened in the given mode, and a file descriptor fd is returned.

Argument Description
"r" Open for reading
"w" Create for writing, overwrite if it exists
"a" Create if file does not exist, else append; open for writing at end of file
"r+" Open for update (reading and writing)
"w+" Truncate or create for update
"a+" Append, open or create for update at EOF

  $fclose (fd)

This system function will close the file descriptor fd opened with $fopen. It is always recommended to close any existing open file descriptors after requirement. No further reads or writes to the file descriptor is allowed once it is closed. Any active $fmonitor or $fstrobe operations are implicitly cancelled by an $fclose operation.


module tb;
  int fd;
  initial begin
`ifdef ERROR
    fd = $fopen ("nofile", "r");
    fd = $fopen ("sample.txt", "w");
    if (fd)
      $display ("Opened successfully");
      $display ("Oops ! four oh four ...");
    $fclose (fd);
Simulation Log
ncsim> run
Opened successfully
ncsim: *W,RNQUIE: Simulation is complete.

Note that if a file cannot be opened, a zero is returned for the mcd or fd. Let's use the +define+ERROR compile time option to see what we'll get.

ncsim> run
Open failed on file "nofile". No such file or directory
Oops ! four oh four ...
ncsim: *W,RNQUIE: Simulation is complete.

What are multichannel file descriptors ?

The mcd is a 32-bit packed array value in which a single bit is set indicating which file is opened. The LSB of an mcd always refers to the standard output. Output is directed to two or more files opened with multichannel descriptors by bitwise OR-ing together their mcds, and writing to the resultant value.

Was this article helpful ?

We use cookies to personalize content and ads, to provide social media features and to analyze our traffic. You consent to our cookies if you continue to use our website. To find out more about the cookies we use and how to delete them, see our privacy policy.

  I accept cookies from this site.
EU Cookie Directive plugin by www.channeldigital.co.uk