System Tasks and Compiler Directives
System Tasks and Compiler Directives
In this section, we introduce two special concepts used in Verilog: system tasks and
compiler directives.
Verilog provides standard system tasks for certain routine operations. All system tasks
appear in the form $<keyword>. Operations such as displaying on the screen, monitoring
values of nets, stopping, and finishing are done by system tasks. We will discuss only the
most useful system tasks. Other tasks are listed in Verilog manuals provided by your
simulator vendor or in the IEEE Standard Verilog Hardware Description Language
specification.
Displaying information
$display is the main system task for displaying values of variables or strings or
expressions. This is one of the most useful tasks in Verilog.
p1, p2, p3,..., pn can be quoted strings or variables or expressions. The format of $display
is very similar to printf in C. A $display inserts a newline at the end of the string by
default. A $display without any arguments produces a newline.
Strings can be formatted using the specifications listed in Table 3-4. For more detailed
specifications, see IEEE Standard Verilog Hardware Description Language specification.
Format Display
%d or %D Display variable in decimal
%b or %B Display variable in binary
%s or %S Display string
%h or %H Display variable in hex
53
%c or %C Display ASCII character
%m or %M Display hierarchical name (no argument required)
%v or %V Display strength
%o or %O Display variable in octal
%t or %T Display in current time format
%e or %E Display real number in scientific format (e.g., 3e10)
%f or %F Display real number in decimal format (e.g., 2.13)
%g or %G Display real number in scientific or decimal, whichever is shorter
Example 3-3 shows some examples of the $display task. If variables contain x or z
values, they are printed in the displayed string as "x" or "z".
//Display x characters
//Display value of 4-bit bus 10xx (signal contention) in binary
reg [3:0] bus;
$display("Bus value is %b", bus);
-- Bus value is 10xx
54
Special characters are discussed in Section 3.2.9, Strings. Examples of displaying special
characters in strings as discussed are shown in Example 3-4.
Monitoring information
Verilog provides a mechanism to monitor a signal when its value changes. This facility is
provided by the $monitor task.
Usage: $monitor(p1,p2,p3,....,pn);
The parameters p1, p2, ... , pn can be variables, signal names, or quoted strings. A format
similar to the $display task is used in the $monitor task. $monitor continuously monitors
the values of the variables or signals specified in the parameter list and displays all
parameters in the list whenever the value of any one variable or signal changes. Unlike
$display, $monitor needs to be invoked only once.
Only one monitoring list can be active at a time. If there is more than one $monitor
statement in your simulation, the last $monitor statement will be the active statement.
The earlier $monitor statements will be overridden.
Usage:
$monitoron;
$monitoroff;
The $monitoron tasks enables monitoring, and the $monitoroff task disables monitoring
during a simulation. Monitoring is turned on by default at the beginning of the simulation
and can be controlled during the simulation with the $monitoron and $monitoroff tasks.
Examples of monitoring statements are given in Example 3-5. Note the use of $time in
the $monitor statement.
55
Example 3-5 Monitor Statement
Usage: $stop;
The $stop task puts the simulation in an interactive mode. The designer can then debug
the design from the interactive mode. The $stop task is used whenever the designer wants
to suspend the simulation and examine the values of signals in the design.
Usage: $finish;
56
3.3.2 Compiler Directives
Compiler directives are provided in Verilog. All compiler directives are defined by using
the `<keyword> construct. We deal with the two most useful compiler directives.
`define
The `define directive is used to define text macros in Verilog (see Example 3-7). The
Verilog compiler substitutes the text of the macro wherever it encounters a
`<macro_name>. This is similar to the #define construct in C. The defined constants or
text macros are used in the Verilog code by preceding them with a ` (back tick).
`include
The `include directive allows you to include entire contents of a Verilog source file in
another Verilog file during compilation. This works similarly to the #include in the C
programming language. This directive is typically used to include header files, which
typically contain global or commonly used definitions (see Example 3-8).
Two other directives, `ifdef and `timescale, are used frequently. They are discussed in
Chapter 9, Useful Modeling Techniques.
57
3.4 Summary
We discussed the basic concepts of Verilog in this chapter. These concepts lay the
foundation for the material discussed in the further chapters.
• Various data types are available in Verilog. There are four logic values, each with
different strength levels. Available data types include nets, registers, vectors,
numbers, simulation time, arrays, memories, parameters, and strings. Data types
represent actual hardware elements very closely.
• Compiler directive `define is used to define text macros, and `include is used to
include other Verilog files.
58