Contents Up << >>

Files in VHDL'93

The file type is used with the file declaration to declare a file object that can be read from and written to. The file type definition specifies what type of objects the file will contain. The syntax for the file type declaration and file declaration is: The subtype in the file declaration must be a file type.

The opening and closing of files can be managed automatically or by the designer. If the file-open-specification is present then the file will automatically be opened and closed, otherwise the designer is responsible for opening and closing the file.

There are two parts of the file-open-specification that give the mode of the file and the name of the file. The file-kind is an expression of the built-in type file_open_kind. This type describes the access mode of the file and is defined by If no file kind is given, READ_MODE is the default. Files opened with the READ_MODE or APPEND_MODE kind must refer to already existing files. Files opened with the WRITE_MODE will create a new file of the given name, if it does not already exist, or otherwise overwrite the existing file.

If a file with a file-open-specification is declared within a subprogram then that file is automatically opened each time the subprogram is called, and automatically closed when the subprogram returns. For files with a file-open-specification that are declared elsewhere, the file is opened at the start of simulation and closed when simulation is complete.

Files that do not have a file-open-specification are opened and closed by the designer using the file operations file_open and file_close. These two operations are defined as the following built-in procedures

procedure file_open(file f: FT; name: string, 
                    kind: FILE_OPEN_KIND:=READ_MODE);
procedure file_open(status: out FILE_OPEN_STATUS;
                    file f: FT; name: string, 
                    kind: FILE_OPEN_KIND:=READ_MODE);
procedure file_close(file f: FT);
In these definitions, FT refers to the file type of file f. The file open procedures open the given file with the supplied name and access mode specified by kind. Additionally, the first open procedure has an extra parameter, status, which indicates the success of the procedure. The FILE_OPEN_STATUS type of this parameter is a built-in type defined by

type FILE_OPEN_STATUS is (
    OPEN_OK,        -- File successfully opened.
    STATUS_ERROR,   -- File is already open.
    NAME_ERROR,     -- File is open if READ_MODE or APPEND_MODE, or 
                    --  can not create file if WRITE_MODE.
    MODE_ERROR);    -- File can not be opened with the specified mode.
The second version of file_open causes a run-time error is the file can not be opened.

The following subprograms are also built-in and can be used with any open file. If the file is not open then a run-time error will occur.

procedure read(file f: FT; value: out TM);
procedure write(file f: FT; value: in TM);
function endfile(file f: FT) return boolean;
The read procedure reads the next value of the appropriate type from a file. When the file is first opened, the next value is the first value in the file. If there are no more values in the file, an error will be reported. The write procedure writes a value of the appropriate type to the end of the file. The endfile function returns true if there are no more values to be read from a file of mode in.

A special package named textio, described in this chapter, provides input and output with text files and the screen and keyboard.