Files
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
•
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.