VH
Basic
EL 3
Erkay
Sabancı U
HDL
c I/O
310
Savaş
University
1
Basic I/O and It
• Objects of file type
– it is a special type that s
between the VHDL progr
environment.
• Motivation
– how file objects are crea
within VHDL simulations
• To get it right
– File input and output can
– I/O operations do not re
chips
ts Applications
serve as an interface
rams and the host
ated, read, written and used
nnot be synthesized
efer to I/O pins of FPGA
2
Test Benches w
• Test bench
– VHDL programs for test
• A typical test bench
– reads test inputs from a
– applies them to the VHD
– and records model outpu
with File Object
ting VHDL models
a file
DL model under test
uts for analysis
3
Basic I/O O
• Type of a file object
– depends what sort of da
– can be anything: integer,
std_logic_vector, etc.
• Three types of basic op
– Declaration of a file and
– opening and closing a file
– reading and writing a file
Operations
ata is stored in them
, string, real number,
perations
d its type
e of a specified type
e.
4
File Decl
• Syntax
– type TEXT is file o
– type INTF is file o
– The first types of files c
that form human readab
– The second can store seq
stored in binary form.
– string and integer ar
language and can be foun
STANDARD.
– file integer_file:
– file input_file: TE
larations
of string; -- ASCII
of integer;
contain ASCII characters,
ble text.
quence of integers that are
re predefined types of the
nd in the package
INTF;
EXT;
5
File Decl
• TEXT is also a predefin
TEXTIO.
– we will learn more about
• The files integer_fil
can be thought of point
sequences of integers a
respectively.
– they are also being called
larations
ned type in the package
t it.
le and input_file
ters to files contains
and characters,
d as file handles.
6
Opening and C
• After declaration, files m
use
• After use the files must
• We have procedures for
• procedure FILE_OPEN(file fil
File_Name: in STRING;
Open_Kind: in FILE_OPEN_K
• procedure FILE_OPEN(File_Sta
file file_handle: FILE_TY
File_Name: in STRING;
Open_Kind: in FILE_OPEN_K
• procedure FILE_CLOSE(file fi
Closing Files
must be opened prior to
be closed
opening and closing files:
le_handle: FILE_TYPE;
KIND:=READ_MODE);
atus: out FILE_OPEN_STATUS;
YPE;
KIND:=READ_MODE);
ile_handle: FILE_TYPE);
7
Opening and C
• file file_handle: FILE_TYP
• File_Name: in STRING –- nam
• Open_Kind: in FILE_OPEN_KI
mode the file is to be opened.
• The opening modes for a fil
– READ_MODE -- default mod
– WRITE_MODE
– APPEND_MODE
• File_Status: out FILE_OPEN
procedure FILE_OPEN. Four val
– OPEN_OK
– STATUS_ERROR
– NAME_ERROR
– MODE_ERROR
Closing Files
PE; -- pointer to the file
me of the file
IND:=READ_MODE –- in which
le:
de
N_STATUS -- the result of the
lues:
8
Exam
-- declare a file type in the a
type IntegerFileType is file
process is
file data_in: IntegerFileTy
variable fstatus: file_open
-- status var
-- other declarations
begin
file_open(fstatus, datain, “m
--
-- body of process; reading a
-- performing computations
--
end process;
-- termination implicitly cause
mple
architecture declarative region
of integer;
ype; -- declare the file handle
n_status; -- declare file
riable of file_open_status type
myfile.txt”, read_mode);
and writing files and
es a call to FILE_CLOSE
9
Example: Impl
-- declare a file type in the a
type IntegerFileType is file
process is
-- implicitly open a file i
file data_in: IntegerFileTy
“my_file.txt”;
-- other declarations
begin
--
-- body of process; reading
-- performing computations
--
end process;
-- termination implicitly cau
licit File Open
architecture declarative region
of integer;
in file declaration
ype open read_mode is
g and writing files and
uses a call to FILE_CLOSE
10
Reading & W
• The standard VHDL sub
– procedure READ(file fil
out type);
– procedure WRITE(file fi
value: in type);
– function ENDFILE(file f
return Boolean;
• The read/write functions c
opened.
• The language supports read
the predefined types of the
• For other types, you need t
procedures built on these b
Writing Files
bprograms
le_handle: FILE_TYPE; value:
ile_handle: FILE_TYPE;
file_handle: FILE_TYPE)
can be used after the file is
ding and writing from files of
e language.
to write your own I/O
basic procedures.
11
VHDL 1
• VHDL 1993
– file infile: text open rea
– file outfile: text open wr
• VHDL 1987
– file infile: text in is “i
– file outfile: text out is
– procedure READ(file fil
value: out type);
– procedure WRITE(file fi
value: in type);
– function ENDFILE(file f
return Boolean;
– No explicit FILE_OPEN an
1987 I/O
ad_mode is “inputdata.txt”;
rite_mode is “outputdata.txt”;
inputdata.txt”;
“outputdata.txt”;
le_handle: FILE_TYPE;
ile_handle: FILE_TYPE;
file_handle: FILE_TYPE)
nd FILE_CLOSE
12
Exam
entity io93 is
end entity io93;
architecture beh of io93 is
begin
process is
type IntegerFileType is fil
file data_out: IntegerFileT
variable fstatus: FILE_OPEN
variable count: integer:= 0
begin
file_open(fstatus, data_out
for i in 1 to 8 loop
write(data_out, count);
count := count + 2;
end loop;
wait;
end process;
end architecture beh;
mple
le of integer;
Type;
N_STATUS;
0;
t, “myfile.txt”, write_mode);
13
The Packag
• A standard package sup
simulators.
• It provides a standard
types, and I/O function
• TEXTIO is in the librar
• The library STD does n
declared.
• However, the packages
order to use the packag
– use STD.textio.all;
ge TEXTIO
pported by all VHDL
set of file types, data
ns.
ry STD;
not have to be explicitly
must be declared in
ge content
;
14
The Packag
• Standard file type: TEX
– package provides the pro
writing the predefined t
bit, integer, and cha
– See Appendix F in the te
• Several lines
– -- A LINE is a pointer
type LINE is access STR
– -- A file of variable-l
type TEXT is file of ST
– procedure READLINE(file
– procedure READ(L: inout
– procedure WRITELINE(fil
– procedure WRITE(L: inou
ge TEXTIO
XT
ocedures for reading and
types of the language such as
aracter.
extbook.
to a string
RING;
length ASCII records
TRING;
e F: TEXT; L: out LINE);
t LINE; value: out bit);
le F: TEXT; L: inout LINE);
ut LINE; value: out bit);
15
TEXTIO M
• LINE serve as a buffer
writing
– read() and write() pr
operate on this buffer
– They are overloaded and
bit_vector, and strin
– readline() and writel
the contents of this buf
• Access types are simila
and C languages.
• There are two special f
input and output that
package TEXTIO.
Mechanism
r area for reading and
rocedures access and
d defined for bit,
ng.
line() procedures move
ffer to and from files.
ar to pointers in Pascal
file handles called
t are defined in the
16
TEXTIO: input
• Two special predefined
• input and output
• they are mapped to the
std_output of the ho
the console window of t
– file INPUT: TEXT op
“STD_INPUT”;
– file OUTPUT: TEXT o
“STD_OUTPUT”;
t and output
file handles
e std_input and
ost environment that is
the VHDL simulator.
pen READ_MODE is
open WRITE_MODE is
17
Example:
use STD.textio.all;
entity formatted_io is
end entity;
architecture beh of formatted_i
begin
process is
file outfile: text;
variable f_status: FILE_OPEN_
variable count: integer := 5;
variable value: bit_vector(3
variable buf: LINE; -- buffer
begin
file_open(fstatus, outfile, “
...
end process;
end architecture beh;
TEXTIO
io is
_STATUS;
;
downto 0) := x”6”;
r between the program and file
“myfile.txt”, write_mode);
18
Example:
...
process is
begin
file_open(f_status, outfile, “myf
L1: write(buf, string'("This is a
L2: writeline(outfile, buf);
L3: write(buf, string'("The first
L4: write(buf, count);
L5: write(buf, ' ');
L6: write(buf, string'("The secon
L7: write(buf, value);
L8: writeline(outfile, buf);
L9: write(buf, string'(“... and s
L10: writeline(outfile, buf);
L11: file_close(outfile);
wait;
end process;
end architecture beh;
TEXTIO
file.txt”, write_mode);
an example of formatted IO"));
t parameter is="));
nd parameter is="));
so on"));
19
ModelSi
• A common error when c
– WRITE (L, "hello");
– WRITE (L, "010111")
– will cause the following e
– ERROR: Subprogram "
• In the TextIO package
is overloaded for the ty
BIT_VECTOR.
– procedure WRITE(L:
BIT_VECTOR; JUSTIFI
FIELD: in WIDTH :=
– procedure WRITE(L:
STRING; JUSTIFIED:
FIELD: in WIDTH :=
im Note
calling
; or
);
error
"WRITE" is ambiguous.
e, the WRITE procedure
ypes STRING and
inout LINE; VALUE: in
IED: in SIDE:= RIGHT;
0);
inout LINE; VALUE: in
in SIDE:= RIGHT;
0); 20
ModelSi
• "hello" could be interpre
• Use the following syntax:
WRITE (L, string’("
WRITE (L, bit_vecto
im Note
eted as a string or a bit
"hello"));
or’(" 010111 "));
21
Output of Th
• buf of LINE type can b
write and read proced
• The file “myfile.txt” wil
This is an example of format
The first parameter is=5 The
... and so on
• If you write “STD_OUTPUT
then the output will be writ
(ModelSim’s Main Window )
• Note that “STD_OUTPUT”
he Program
be accessed by only
dures.
ll contain
tted IO
e second parameter is=0110
T” instead of “myfile.txt”
tten to simulator console
)
must be in capital letters.
22
Yet Anothe
use STD.textio.all;
entity formatted_io_02 is
end entity;
architecture beh of formatted_i
begin
process is
file infile, outfile: text;
variable f_status: FILE_OPE
variable buf_in, buf_out: L
variable count: integer;
begin
file_open(f_status, infile,
file_open(f_status, outfile
...
end process;
end architecture beh;
er Example
io_02 is
; -- two files
EN_STATUS;
LINE; -- buffers between the
-- program and file
, "STD_INPUT", read_mode);
e, "STD_OUTPUT", write_mode);
23
Yet Anothe
...
process is
...
begin
L1: write(buf_out, string'("
L2: writeline(outfile, buf_o
L3: write(buf_out, string'("
L4: writeline(outfile, buf_o
L5: readline(infile, buf_in)
L6: read(buf_in, count);
L7: write(buf_out, string'("
L8: write(buf_out, count);
L9: writeline(outfile, buf_o
L10: file_close(outfile);
wait;
end process;
end architecture beh;
er Example
"This is an example of
formatted IO"));
out);
"Enter the parameter"));
out);
);
"The parameter is="));
out);
24
Yet Anothe
er Example
25