当前位置:网站首页>System tasks (display / print class) in Verilog - $display, $write, $strobe, $monitor

System tasks (display / print class) in Verilog - $display, $write, $strobe, $monitor

2022-06-26 13:03:00 Lonely single blade

summary

         During verification and debugging , Sometimes it is very helpful to print some information on the terminal .

         For example, you are verifying the loopback module of a serial port , The sender will send... At regular intervals 1 individual BYTE Data to the receiving end . If you don't want to verify whether the sending and receiving are consistent by comparing the waveforms one by one , You can choose to print each sent value and received value directly to the terminal .

         Another example is your RTL A parameter in the has a value that is not within the expected range , You can print an error message to the terminal at this time , In this way, we can know RTL Is there a problem , Instead of staring at your oscillogram all the time .

        Verilog Grammar provides us with 4 System functions , Variable information can be displayed on the terminal , According to its use method, it can be divided into 3 class :

  1. $display, $write
  2. $strobe
  3. $monitor

$display and $write

        $display You can print a text message directly , And every time $display Post implementation meeting Word wrap , such as :

`timescale 1ns/1ns
module test_tb();

initial begin
	$display("China NO1!");
	$display("USA NO2!");
end

endmodule 

        stay vivado Print result of window observation :

        $write The usage and $display Agreement , The difference lies in , One $write After statement execution , No line wrapping . Consider the following code :

`timescale 1ns/1ns
module test_tb();

initial begin
	$write("China NO1!");
	$write("USA NO2!");
end

endmodule 

        The print result is :


        These two system functions can print text directly , You can also print the values of variables , The format for ( With $display For example ):

                                                                $display("%b %b",a,b) ;

         and C The language is the same , among a,b Is the variable value of the expected output ,%b Format for variable output , It is expressed as 2 Binary output . Other output formats are as follows :

%h or %H Hex format output %c or %CASCII Code format output
%d or %D Decimal format output %e or %E Output in exponential format
%o or %O Output in octal format %f or %F Floating point numbers (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 hierarchy access path output

        In addition to this , Use escape characters , for example :

\n A newline %% Percent sign "%"
\t tabs (Tab key )\0 Octal characters
\\ The backslash "\" operator \0x Hexadecimal characters
\" Double quotes

        such as , Output 2 The process and result of adding numbers :

`timescale 1ns/1ns
module test_tb();

	reg	a;
	reg	b;
	reg [1:0]sum;
	
initial begin
	a = 0;
	b = 0;
	sum = a + b;	
	$display("%b + %b = %b",a,b,sum);

	#10	
	a = 1;
	b = 1;
	sum = a + b;	
	$display("%b + %b = %b",a,b,sum);
end

endmodule 

          The printed results are displayed twice 1bit Binary addition process and result of :


$strobe

        $strobe Show tasks for gating .$strobe Usage and $display Agreement , But the time it takes to print the message is $display There are differences ( You can also print text directly ).

         When many sentences are related to $display When a task is executed at the same time , These statements and $display The execution order of is uncertain , Generally, it is executed according to the sequence structure of the program . The difference between the two is :$strobe The command will be completed at the end of the current time ; and $display Yes, as long as the emulator sees it, it will execute immediately .

        $strobe It is in the After his statement is executed , To execute the display task . for example :

`timescale 1ns/1ns
module test_tb();

reg [3:0]  a ;
initial begin
	$strobe("begin!");
	a = 1 ;
	#1 ;
	a <= a + 1 ;
	// For the first time 
	$display("$display excuting result: %d.", a);
	$strobe("$strobe excuting result: %d.", a);
	#1 ;
	$display();
	// The second time it shows 
	$display("$display excuting result: %d.", a);
	$strobe("$strobe excuting result: %d.", a);
	$strobe("end!");
end

endmodule 

        The printing results are as follows : 

         You can see ,$strobe And $display The print content of is not consistent .

         This is because the statement :    a <= a + 1 ; in other words a The second assignment of is a non blocking assignment , Non blocking assignment takes time .

  • When printing for the first time ,$display I won't care about you a Block assignment or non block assignment , It just prints a The current value 1. and $strobe Will wait until the non blocking assignment is completed before printing , So the printed value is 2.
  • At the second printing , It's delayed again 1ns, So the non blocking assignment is completed , that $strobe And $display The printed contents of all are 2 了 .

       

         therefore $strobe This system task is usually used to print the variable value of the current non blocking assignment .


$monitor

        $monitor For monitoring tasks , For continuous monitoring of variables . As long as the variable changes ,$monitor The corresponding information will be printed and displayed . How to use it and $display Agreement .

        The following code uses $monitor To monitor a,b,c this 3 A variable , As long as one of them changes , Will immediately print on the terminal .

`timescale 1ns/1ns

module test_tb();

reg [1:0]  a ;
reg [1:0]  b ;
reg [1:0]  c ;

initial begin
	a = 0 ;
	b = 0 ;
	c = 0 ;

	$monitor("a=%d b=%d c=%d",a,b,c);
	#50 $finish;	//50ns Stop after 
end

always #10 begin		// Every time 10ns, Random generation a,b,c
	a = {$random}%4;
	b = {$random}%4;
	c = {$random}%4;
end

endmodule 

        The terminal print results are as follows : 

原网站

版权声明
本文为[Lonely single blade]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261212475436.html