7.1 Verilog Display Tasks
Category Advanced Verilog Tutorial
Keywords: $display, $write, $strobe, $monitor
In Verilog, the following four system tasks are primarily used to display (print) debugging information: $display, $write, $strobe, $monitor.
$display
The usage of $display is very similar to the printf function in C language. It can directly print strings and also specify the format of variables in the string to print related variables. For example:
$display("This is a test."); //Directly print a string
$display("This is a test number: %b.", num); //Print the variable num in binary format
If no display format for the variable is specified, the variable value will be displayed according to its position in the string, which is equivalent to string concatenation. For example:
$display("This is a test number: ", num, "!!!");
If no format is specified, $display defaults to decimal display. $displayb, $displayo, $displayh display formats are binary, octal, and hexadecimal, respectively. Similarly, there are $writeb, $writeo, $writeh, $strobeb, etc.
The following table shows common format specifiers.
| %h or %H | Hexadecimal format output | %c or %C | ASCII code format output | | %d or %D | Decimal format output | %e or %E | Exponential format output | | %o or %O | Octal format output | %f or %F | Floating point (real type) format output | | %b or %B | Binary format output | %t or %T | Current time format output | | %s or %S | String format output | %m or %M | Current hierarchical access path output |
You can also use escape characters to display special characters, for example:
| \n | Line feed | %% | Percentage sign "%" | | \t | Tab character (Tab key) | \0 | Character represented by octal | | \ | Backslash "\" character | \0x | Character represented by hexadecimal | | \" | Double quote | | |
$write
The usage of $write is exactly the same as $display, except that the former does not automatically line break after each display, while the latter does. When no line break is needed after the output, you can use the display task $write.
$write("This is a test");
$write("number: %b", num);
$write("!!!\n");
$strobe
$strobe is a strobed display task. The usage of $strobe is consistent with $display, but the timing of the printed information is different from $display.
When many statements are executed at the same time as the $display task, the execution order of these statements and $display is uncertain, generally executed according to the program's sequential structure.
$strobe, on the other hand, performs the display task after all other statements have been executed. For example:
Example
reg [3:0] a;
initial begin
a = 1;
#1;
a <= a + 1;
//First display
$display("$display executing result: %d.", a);
$strobe("$strobe executing result: %d.", a);
#1;
$display();
//Second display
$display("$display executing result: %d.", a);
$strobe("$strobe executing result: %d.", a);
end
The execution results are as follows.
When the first display task is executed, the non-blocking assignment and $display are executed simultaneously. $display displays the variable value before the assignment, while $strobe displays the variable value after the assignment. This reflects the strobed display characteristic of $strobe.
Let's look at another example:
Example
integer i;
initial begin
for (i=0; i<4; i=i+1) begin
$display("Run times of $display: %d.", i);
$strobe("Run times of $strobe: %d.", i);
end
end
$display performs the display operation 4 times according to the program structure. And this loop statement is executed at time 0, so the variable value displayed by $strobe is the result of the variable after the loop ends, that is, i=4 exits the loop and $strobe will be executed. This reflects