0% found this document useful (0 votes)
8 views

Files

Files allow storing and accessing different types of information. Key operations for files include declaring a file type, opening and closing files, and reading from and writing to files. Declaring a file type like "file of integer" specifies the format so the host environment can correctly interpret the stored data. Common procedures for I/O include FILE_OPEN to open a file, READ and WRITE for input/output, and FILE_CLOSE to close a file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Files

Files allow storing and accessing different types of information. Key operations for files include declaring a file type, opening and closing files, and reading from and writing to files. Declaring a file type like "file of integer" specifies the format so the host environment can correctly interpret the stored data. Common procedures for I/O include FILE_OPEN to open a file, READ and WRITE for input/output, and FILE_CLOSE to close a file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Files

•As with variable, signal, and constant objects, before we can use
a file object we must give it a name and declare its type.
• The type of a file is determined by the information provided in
the file.
• For example if a file contained a sequence of integers we might
naturally think of this file as being of type integer.
• As users we tend to create files with all types of information, for
example, strings, real numbers, and std_logic_vectors.
• Basic operations that we need for read-ing and writing files:
• Declaration of a file and its type
• Opening and closing a file of a specified type
• Reading and writing from a file
• type TEXT is file of string;
• type INTF is file of integer;·
• The first declaration defines a file type that can store ASCII data.
Files of this type contain human readable text.
• The second declaration defines a file type that can store a sequence
of integers. Files of this type are stored in binary form and are
machine read-able but not human readable.
• Both string and integer are predefined types of the lan-guage and
their definition can be found in the package STANDARD.
• Now we can declare a file object as being of a particular file type as
follows:
• file Integer_File: INTF;
• file Input _File : TEXT;
• We can think of Integer_File and Input _File as pointers to files that
contain sequences of integers and characters respectively.
• When VHDL procedures are used to read and write from these files
the file type enables the procedures to correctly interpret the values
being read from the files.
Opening and closing of files
• procedure FILE_OPEN (file file_handle : FILE_TYPE;
File_Name: in STRING;
Open_Kind: In FILE_OPEN_KIND:= READ_MODE);
• procedure FILE_OPEN (File_Status: out FILE_OPEN_STATUS;
file file_handle : FILE_TYPE;
File_Name: in STRING;
Open_Kind: in FILE_OPEN_KIND := READ_MODE);
• procedure FILE_CLOSE (file f : FILE_TYPE);
• In the file open call the name of the file can include a path in you local
machine hierarchy.
• type IntegerFile Type is file of integer; -- declare a file type in the
• -- architecture declarative region
• process is - a template for a process
• file datain :lntegerFileType; -- declare file handle
• variable fstatus :File_open_status; -- declare file status variable
• begin
• file_open(fstatus, dataout,"myfile.txt", read_mode);
• -- body of process: reading and writing files and
• -- performing computations
• ----
• End process;

• Files can be implicitly opened by providing the necessary
information in the file declara-tion as follows. There is no explicit
FILE_OPEN call.
• type IntegerFile Type is file of integer; -- declare a file type in the
• -- architecture declarative
region
• process is - a template for a process
• file datain:lntegerFileType open read_mode is "myfile.txt"; -- declare
file handle
• ---Other miscellaneous declarations
• begin
• -- body of process: reading and writing files and
• -- performing computations
• End process;
Reading and Writing Files
• procedure READ (file file_handle : FILE_ TYPE; value: out type);
• procedure WRITE (file file_handle : FILE_TYPE; value: in type);
function ENDFILE (file file_handle : FILE_TYPE) return boolean;
• The above I/O procedures are implicitly declared following a file type
declaration.
• This means that you do not have to declare these procedures prior
to their use.
• Whereas the procedures READ and WRITE are used for I/O
operations, the ENDFILE function is used to test for the end of file
when reading from files.
Basic binary file I/O
• entity io93 is -- this entity is empty
• end entity io93;
• architecture behavioral of io93 is
• begin
• process is
• type IntegerFileType is file of integer; -- file declarations
• file dataout :lntegerFileType;
• variable count: integer:= 0;
• variable fstatus: FILE_OPEN_STATUS;
• begin
• file_open(fstatus, dataout,"myfile.txt", write_mode); -- open the file
• for j in 1 to 8 loop
• write(dataout,count); -. some random values to write to the file
• count := count+2;
• end loop;
• wait; -- an artificial way to stop the process
• end process;
• end achitecture behavioral;
• To read the file "myfile.txt" we can imagine another VHDL model
with a block of code that looks exactly like the above for-loop but
with the write(dataout, count) procedure replaced by a read(dataout,
myinteger [i]) so that the sequence of values can be read into an
integer array myinteger[ ].
• The mode of the FILE_OPEN procedure would also have to be
read_mode rather than write_mode.
• The package TEXTIO is a standard package supported by all VHDL
simulators.
• The TEXTIO package provides a standard set of file types, data
types, and 110 functions.
• The TEXTIO package is in the library STD, which is implicitly
declared. The package TEXTIO defines a standard file type called
TEXT and provides the procedures for reading and writing the
predefined types of the language, such as bit, inte-ger, and
character.
• Normally, we expect that each data type will have a distinct
procedure for reading and writing the type to a file.
• For example, we could have read_string() and read_bitvector().
• However, given the large number of data types, we see that this
quickly becomes a rather tedious exercise in naming procedures.

• .
• Instead, it is convenient" for all of the procedures that perform the
same function, such as read(), to have the same name, and permit
the actual argument to identify the correct implementation: read ()
and write() are overloaded procedure names.
• We do not have to remember the exact name of the procedure to
read and write elements of type bit vector or string.
• We simply use the procedures read() and write(), and, depending on
whether the argument is a bit vector or string the appropriate
implementation is invoked. We see that read() and write()
pro-cedures are available for the predefined types bit, bit vector,
character, and string.
• Library ieee;
• Use ieee.std_logic_1164.all;
• use STD.Textio.all;
• entity formatted_io is -- this entity is empty
• end formatted_io;
• architecture behavioral of tormatted_io is
• begin
• process is
• file outfile: text; -- declare the file to be a text file
• variable tstatus : File_open_status;
• variable count: integer:= 5;
• variable value: bit _vector(3 downto 0):= X"6";
• variable buf: line; -- this is the buffer between the program and the file

• begin
• file_open(tstatus, outfile,"myfile.txt", write_mode); -- open the file for writing
• L 1: write(buf, "This is an example of formatted i0");
• L2: writeline(outfile, buf); --this procedure writes the buffer.JQ a line in the
file
• L3: write(buf, 'The First Parameter is =");
• L4: write(buf, count); -- this procedure writes to the next location in the
buffer
• L5: write(buf, ‘ ');
• L6: write(buf, 'The Second Parameter is = ");
• L7: write(buf, value); .
• L8: writeline(outfile, buf);
• L9: write(buf, " ... and so on");
• L 10: writeline( outfile, buf);
• L 11: file_close(outfile); -- flush the buffer to the file wait;
• end process;
• end architecture behavioral
• When viewed with a text editor the output file will appear as follows:
• This is an example of formatted i0
• The First Parameter is = 5
• The Second Parameter is = 0110
• ... and so on
• library IEEE;
• use IEEE.std_logic_1164.all;
• use STD.textio.all;
• package classio is
• procedure read_ v1 d (variable t: in text; v : out std_logic_ vector);
• procedure write_v1 d (variable t:out text; v: in std_logic_vector);
• end package classio;
• package body classio is
• procedure read_v 1 d (variable t:in text; v : out std_logic_vector) is
• variable but: line;
• variable c : character;
• begin
• readline(t, but);
• for i in v' range loop
• read(but, c);
• case c is
• when 'X' => v (i) := 'X';
• when 'U' => v (i) := 'U';
• when 'Z' => v (i) := 'Z';
• when '0' => v (i) := '0';
• when '1' => v (i) := '1';
• when '-' => v (i) := '-';
• when 'W' => v (i) := 'W';
• when 'L' => v (i) := 'L';
• when 'H' => v (i) := 'H';
• when others => v (i) := '0';
• end case;
• end loop;
• end procedure read_v1d;
• procedure write_ v1 d (variable f: out text; v : in std_logic_ vector) is
variable buf: line;
• variable c : character;
• begin
• for i in v'range loop case v(i) is

• when 'X' => write(buf, 'X');


• when 'U' => write(buf, 'U');
• when 'Z' => write(buf, 'Z');
• when '0' => write(buf, character' ('0'»;
• when '1' => write(buf, character'('1 '»;
• when '-' => write(buf, '-');

• when 'W' => write(buf, OW');


• when 'L' => write(buf, 'L');
• when 'H' => write(buf, 'H');
• when others => write(buf, character'('O'»;
• end case;
• end loop;
• writeline (f, buf);
• end procedure write_v1 d;
• end package body classio;
files
•To create and access information from files we would like a way
to state the type of information contained in these files and have
functions for reading and writing this information..
•we can identify the following basic operations that we need for
read-ing and writing files:
•Declaration of a file and its type
•Opening and closing a file of a specified type
•Reading and writing from a file
•If we want to declare a file we also wish to make sure that the
host environment can cor-rectly interpret the format of the data
stored in the file.
•This is achieved by first declaring the type of a file as follows:
•type TEXT is file of string;
• type INTF is file of integer;·
• The first declaration defines a file type that can store ASCII data.
Files of this type contain human readable text.
• The second declaration defines a file type that can store a sequence
of integers. Files of this type are stored in binary form and are
machine read-able but not human readable.
• Both string and integer are predefined types of the lan-guage and
their definition can be found in the package STANDARD.
• Now we can declare a file object as being of a particular file type as
follows:
• file Integer_File: INTF;
• file InpuCFile : TEXT;
• Once we have declared files of a specific type we must
open these files prior to use and close these files prior to
termination of the program..
• procedure FILE_OPEN (file file_handle : FILE_TYPE;
• File_Name: in STRING;
• Open_Kind: in FILE_OPEN_KIND:= READ_MODE);
• procedure FILE_OPEN (File_Status: out
FILE_OPEN_STATUS;
• file file_handle : FILE_TYPE;
• File_Name: in STRING;
• Open_Kind: in FILE_OPEN_KIND := READ_MODE);
• procedure FILE_CLOSE (file f : FILE_TYPE);
• Consider the first procedure. The first argument is the file pointer
• The second argument is the name of the file that will read or written.
For example you might have a file named input-file.txt in your
working directory.
• The third argument describes how this file is to be used, that is the
mode of the file.
• A file can be opened in three modes-READ_MODE, WRITE_MODE,
and APPEND_MODE. The default mode is READ_MODE.
• The second procedure is the same as the first with the addition of a
new parameter, File_Status. This parameter has a value returned by
the procedure that may have one of four values:
• OPEN_OK - file open operation was successful
• STATUS_ERROR - attempted to open an already open file
NAME_ERROR - file not found
• MODE_ERROR - file cannot be opened in this mode
Explicit file opening
• type IntegerFile Type is file of integer; -- declare a file type in the
• -- architecture declarative region
• process is - a template for a process
• file datain :lntegerFileType; -- declare file handle
• variable fstatus :File_open_status; -- declare file status variable
• begin
• file_open(fstatus, dataout,"myfile.txt", read_mode);
• -- body of process: reading and writing files and
• -- performing computations


Implicit file open
• Files can be implicitly opened by providing the necessary
information in the file declara-tion as follows. There is no explicit
FILE_OPEN call.
• _. architecture declarative region process is - a template for a
process
• file datain:lntegerFileType open read_mode is "myfile.txt"; -- declare
file han-
• -- body of process: reading and writing files and
• -- performing computations
• procedure READ (file file_handle : FILE~ TYPE; value: out type);
• procedure WRITE (file file_handle : FILE_TYPE; value: in type);
• function ENDFILE (file file_handle : FILE_TYPE) return boolean;
• The above I/O procedures are implicitly declared following a file type
declaration.
• This means that you do not have to declare these procedures prior
to their use.

You might also like