Chapter 4 - Behavioral Descriptions

Section 4 - Program Output

In most programming languages there is a mechanism for printing text on the monitor and getting input from the user through the keyboard. Even though your simulator will let you monitor the value of signals and variables in your design, it is also nice to be able to output certain information during simulation. It is not provided as a language feature in VHDL, but rather as a standard library that comes with every VHDL language system. In VHDL, common code can be put in a separate file to be used by many designs. This common code is called a library. In order to use the library that provides input and output capabilities you must add the statement
use textio.all;
immediately before every architecture that uses input and output. The name of the library is textio and this statement indicates that you wish to use everything or all of the textio library. Once you have done that, you may use any of the features discussed in this section. Note that although it is not part of the language, the library is standard and will be the same regardless of the VHDL tools you are using.

Text is input and output using textio via a variable of the type line. Since variables are used for textio, input and output is done in processes. The procedure for outputting information is to first place it in text form into the variable of type line and then to request that the line be output. This is shown in the following example.

use textio.all;
architecture behavior of check is
  process (x)
    variable s : line;
    variable cnt : integer:=0;
    if (x='1' and x'last_value='0') then
      if (cnt>MAX_COUNT) then
        write(s,"Counter overflow - ");
      end if;
    end if;
  end process;
end behavior;
The write function is used to append text information at the end of a line variable which is empty when the simulator is initialized. The function takes two arguments, the first is the name of the line to append to, and the second is the information to be appended. In the example, s is set to "Counter overflow - ", and then the current value of cnt is converted to text and added to the end of that. The writeline function outputs the current value of a line to the monitor, and empties the line for re-use. The first argument of the writeline function just indicates that the text should be output to the screen. If MAX_COUNT were a constant equal to 15 and more than 15 rising edges occur on the signal x, then the message
Counter overflow - 16
would be printed on the screen.

The write statement can also be used to append constant values and the value of variables and signals of the types bit, bit_vector, time, integer, and real. Keyboard input is more complex than output, and is not discussed in this tutorial.

The previous section is Behavioral Descriptions - Signals and Processes.
The next section is The End.

Copyright 1995, Green Mountain Computing Systems.
Copying this document is strictly prohibited. Making any non-volatile or semi-permanent copies of this document is a violation of international copyright laws.