Mastering $display and $write in Verilog and SystemVerilog

In this blog post, we delve into the world of Verilog and SystemVerilog, focusing on the $display and $write tasks, as well as the unique concept of escape sequences. This tutorial will offer insights and practical examples to help you master these tasks and sequences effectively.

Understanding the Basics: $display and $write Tasks

Both Verilog and SystemVerilog endorse the $display and $write tasks, paving the way for the formulation of personalized messages in your simulation logs.

Here is an illustration of a rudimentary $display task in SystemVerilog:

module DisplayTask;
  initial begin
    $display("Hello, world!");
  end
endmodule

In this scenario, the $display task will output the string "Hello, world!" onto the console as soon as the simulation initiates.

Differences Between $display and $write Tasks: Spotting the Discrepancies

Though they may seem identical at first glance, $display and $write tasks possess slight variations. The $display task automatically adds a newline character at the end of the output, whereas the $write task refrains from doing so.

module WriteTask;
  initial begin
    $write("Hello, world!");
  end
endmodule

In the above example, the $write task will output "Hello, world!", however, contrary to the $display task, it will not add a newline character.

Unraveling Arguments: Comprehending $display and $write Tasks

Both tasks are capable of accepting multiple arguments. The subsequent example exemplifies the display of an integer variable.

module DisplayVariable;
  reg [7:0] myVariable;
  
  initial begin
    myVariable = 8'hA5;
    $display("The value of myVariable is: %h", myVariable);
  end
endmodule

This example will output "The value of myVariable is: A5" onto the console.

Understanding Escape Sequences and Special Characters

Escape sequences are pretty handy in Verilog and SystemVerilog. They help us represent specific characters, like literal or non-printable ones, in our strings. Basically, they give us a way to include characters that we normally can't type out.

When you see the special character \, know that the character right after it should be viewed as a literal or non-printable character. Escape sequences, when mixed into a string argument, create special characters. Here are some common escape sequences you'll come across:

  • \n: This stands for a newline character.
  • \t: This represents a tab character.
  • \\: This is for the \ character.
  • \": This is the " character.
  • \v: This is a vertical tab.
  • \f: This stands for a form feed.
  • \a: This is a bell.
  • \ddd: This is a character specified in 1 to 3 octal digits, where 0 ≤ d ≤ 7. If you use less than three characters, the next character shouldn't be an octal digit.
  • \xdd: This is a character specified in 2 hexadecimal digits, where 0 ≤ d ≤ F.

If an escaped character isn't part of the list above, it will just be printed as it is. For example, if you have a string argument \b, it will simply print out as b.

Up Next: Delving Deeper into Format Specifications

In our subsequent piece, we'll be deep-diving into the realm of format specifications in Verilog and SystemVerilog. Explore this advanced guide as we continue to unravel these powerful languages.


As we conclude this tutorial, you should now have a better grasp of the $display and $write tasks, as well as escape sequences in Verilog and SystemVerilog. Remember that practice is what makes you better. So, keep practicing these tasks and sequences, and before you know it, you'll be using them with ease in your coding projects.