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$fopen
fd = $fopen (filename, type)
$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.
|"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|
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
$fstrobe operations are implicitly cancelled by an
module tb; int fd; initial begin `ifdef ERROR fd = $fopen ("nofile", "r"); `else fd = $fopen ("sample.txt", "w"); `endif if (fd) $display ("Opened successfully"); else $display ("Oops ! four oh four ..."); $fclose (fd); end endmoduleSimulation 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.