Tutorials

Learn More

System Tasks in Verilog

The system tasks are used to perform some operations like displaying the messages, terminating simulation, generating random numbers, etc.

Syntax: $<keyword>

Display system tasks

System tasks

Description

$display

To display strings, variables, and expressions immediately in the active region.

$monitor

To monitor signal values upon its changes and executes in the postpone region.

$write

To display strings, variables, and expressions without appending the newline at the end of the message and executing in the active region.

$strobe

To display strings, variables, and expressions at the end of the current time slot i.e. in the postpone region.

The display system tasks use various format specifiers to print the values

Format specifiers

Description

%d or %D

To display variables in decimal

%b or %B

To display variables in binary

%h or %H

To display variables in hexadecimal

%o or %O

To display variables in octal

%c or %C

To display ASCII character

%s or %S

To display string

%t or %T

To display the current time

%f or %F

To display real numbers in decimal format. (Ex. 3.14)

%e or %E

To display real numbers in scientific format. (Ex. 2e20)

The signal or variable monitoring can be switched on or off using the below two tasks

System tasks

Description

$monitoron

To turn on monitoring during simulation.

$monitoroff

To turn off monitoring during simulation.

Note: The monitoring for the signals is by default turned on from the start of the simulation. Hence, it is not required to call at the beginning of the simulation. If monitoring is turned off (using $monitoroff), then to restart the monitoring $monitoron has to be called.

Difference between $display and $monitor

  1. The $monitor can continuously monitor the changes in mentioned variables or signals values whereas $display prints mentioned variables or signals values when it is called.
  2. $monitor should be invoked only once whereas $display can be invoked multiple times.
  3. In case multiple $monitor tasks have called only the last $monitor statement will be active and previous statements will be overridden.
module display_tb;
  reg [3:0] d1, d2;
  
  initial begin
    d1 = 4; d2 = 5;
    
    #5 d1 = 2; d2 = 3;
  end
  
  initial begin
    $display("At time %0t: {$display A} -> d1 = %0d, d2 = %0d", $time, d1, d2);
    $monitor("At time %0t: {$monitor A} -> d1 = %0d, d2 = %0d", $time, d1, d2);
    $write("At time %0t: {$write A} -> d1 = %0d, d2 = %0d", $time, d1, d2);
    $strobe("At time %0t: {$strobe A} -> d1 = %0d, d2 = %0d", $time, d1, d2);
    
    #5;
    
    $display("At time %0t: {$display B} -> d1 = %0d, d2 = %0d", $time, d1, d2);
    // $monitor is missing -> Observe print for $monitor A
    $write("At time %0t: {$write B} -> d1 = %0d, d2 = %0d", $time, d1, d2);
    $strobe("At time %0t: {$strobe B} -> d1 = %0d, d2 = %0d", $time, d1, d2);
  end
endmodule

Output:

At time 0: {$display A} -> d1 = 4, d2 = 5
At time 0: {$write A} -> d1 = 4, d2 = 5At time 0: {$monitor A} -> d1 = 4, d2 = 5
At time 0: {$strobe A} -> d1 = 4, d2 = 5
At time 5: {$display B} -> d1 = 2, d2 = 3
At time 5: {$write B} -> d1 = 2, d2 = 3At time 5: {$strobe B} -> d1 = 2, d2 = 3
At time 5: {$monitor A} -> d1 = 2, d2 = 3

Simulation controlling system tasks

$stop – It is used to stop or suspend the running simulation. It is generally used for debugging purposes and puts the simulation mode in interactive mode so that designers can examine signal values.

$finish – It is used to terminate the simulation.

Random number generator system task

$random – It returns a 32-bit signed integer i.e. it can be positive or negative integer.

Example:

reg [7:0] = $random;
integer value = $random;