• No results found

File Types

In document VHDL Programming by Example pdf (Page 121-124)

A file type allows declarations of objects that have a type FILE. A file object type is actually a subset of the variable object type. A variable object can be assigned with a variable assignment statement, while a file object cannot be assigned. A file object can be read from, written to, and checked for end of file only with special procedures and functions.

Files consist of sequential streams of a particular type. A file whose base object type is INTEGER consists of a sequential stream of integers. This is shown in Figure 4-10.

A file whose object type is a complex record type consists of a sequential stream of complex records. An example of how this might look is shown in Figure 4-11.

At the end of the stream of data is an end-of-file mark. Two procedures and one function allow operations on file objects:

READ(file, data)Procedure

Stack Element Data NXT Stack Element Data NXT Stack Element Data NXT List_Head Temp_Elem Figure 4-8

Move Head Pointer to Next Element. Stack Element Data NXT Stack Element Data NXT List_Head Temp_Elem Figure 4-9 Deallocate Element.

WRITE(file, data)Procedure

ENDFILE(file)Function, returns boolean

Procedure READreads an object from the file and returns the object in argument data. Procedure WRITEwrites argument data to the file specified by the file argument. Finally, function ENDFILEreturns true when the file is currently at the end-of-file mark.

Using these procedures and functions requires a file type declaration and a file object declaration.

FILE TYPE DECLARATION A file type declaration specifies the

name of the file type and the base type of the file. Following is an example of a file type declaration:

TYPE integer_file IS FILE OF INTEGER;

This declaration specifies a file type whose name is integer_fileand is of type INTEGER. This declaration corresponds to the file in Figure 4-10.

FILE OBJECT DECLARATION A file object makes use of a file type

and declares an object of type FILE. The file object declaration specifies the name of the file object, the mode of the file, and the physical disk path name. The file mode can be INor OUT. If the mode is IN, then the file can be read with the READprocedure. If the mode is OUT, then the file can be written with the WRITEprocedure. Here is an example:

FILE myfile : integer_file IS IN

“/doug/test/examples/data_file”;

This declaration declares a file object called myfilethat is an input file of type integer_file. The last argument is the path name on the physical

Integer 1 Integer 2 ... Integer N End of File

Figure 4-10

Pictorial Representa- tion of Integer File.

OPCODE ADDRMODE SRC DST OPCODE ADDRMODE SRC DST OPCODE ADDRMODE SRC DST ... ... ... ... END OF FILE MARK Figure 4-11 Pictorial Representa- tion of Complex File.

disk where the file is located. (In most implementations this is true, but it is not necessarily true.)

FILE TYPE EXAMPLES To read the contents of a file, you can call the READprocedure within a loop statement. The loop statement can perform read operations until an end of file is reached, at which time the loop is terminated. Following is an example of a file read operation:

LIBRARY IEEE;

USE IEEE.std_logic_1164.ALL; ENTITY rom IS

PORT(addr : IN INTEGER; PORT(cs : IN std_logic; PORT(data : OUT INTEGER); END rom;

ARCHITECTURE rom OF rom IS BEGIN

PROCESS(addr, cs)

VARIABLE rom_init : BOOLEAN := FALSE; --line 1 TYPE rom_data_file_t IS FILE OF INTEGER; --line 2

FILE rom_data_file : rom_data_file_t IS IN

“/doug/dlp/test1.dat”; --line 3

TYPE dtype IS ARRAY(0 TO 63) OF INTEGER;

VARIABLE rom_data : dtype; --line 4 VARIABLE i : INTEGER := 0; --line 5 BEGIN

IF (rom_init = false) THEN --line 6 WHILE NOT ENDFILE(rom_data_file) --line 7

AND (i < 64) LOOP

READ(rom_data_file, rom_data(i)); --line 8 i := i 1; --line 9 END LOOP;

rom_init := true; --line 10 END IF;

IF (cs = ‘1’) THEN --line 11 data <= rom_data(addr); --line 12 ELSE

data <= -1; --line 13 END IF;

END PROCESS; END rom;

This example shows how a romcan be initialized from a file the first time the model is executed and never again. A variable called rom_initis used to keep track of whether the romhas been initialized or not. If false, the rom has not been initialized; if true, the romhas already been initialized.

Line 2 of the example declares a file type rom_data_file_tthat is used to declare a file object. In line 3, a rom_data_fileobject is declared. In this example, the physical disk path name was hard-coded into the model, but a generic could have been used to pass a different path name for each instance of the rom.

Line 6 of the example tests variable rom_initfor true or false. If false, the initialization loop is executed. Line 7 is the start of the initialization loop. The loop test makes use of the predefined function ENDFILE. The loop executes until there is no more data in the file or when the romstorage area has been filled.

Each pass through the loop calls the predefined procedure READ. This procedure reads one integer at a time and places it in the element of rom_datathat is currently being accessed. Each time through the loop, the index iis incremented to the next element position.

Finally, when the loop finishes, the variable rom_initis set to true. The next time the process is invoked, variable rom_init will be true, so the initialization loop will not be invoked again.

Writing a file is analogous to reading, except that the loop does not test every time through for an end-of-file condition. Each time a loop writing data is executed, the new object is appended to the end of the file. When the model is writing to a file, the file must have been declared with mode OUT.

In document VHDL Programming by Example pdf (Page 121-124)