systemverilog中输入/输出系统任务和系统函数---$sformat和$sformatf使用方法及其区别

文件I/O任务和函数(IEEE Standard for SystemVerilog—21)
将数据格式化为字符串(IEEE Standard for SystemVerilog—21.3.3)

variable_format_string_output_task
$sformat ( output_var , format_string [ , list_of_arguments ] )
1、系统任务$sformat与系统任务$write相似,但是有一个主要的不同。
2、与输出系统任务$display和$write不同的是,$sformat只把第二个参数作为格式字符串。
3、格式字符串可以是字符串常量,例如"data is %d",或者是整数表达式,非合并字节数组,字符串数据类型。
4、$sformat支持和$display一样所有支持的格式。
5、如果没有为格式说明符提供足够的参数或提供了太多的参数,那么应用程序将发出警告并继续执行。

variable_format_string_output_function
$sformatf ( format_string [ , list_of_arguments ] )
系统函数$sformatf的行为类似于$sformat,只是字符串结果作为$sformatf的函数结果值传回,而不是像$sformat那样放在第一个参数中。

string_output_task_name
$swrite | $swriteb | $swriteh | $swriteo
1、任务的$swrite家族基于任务的$fwrite家族,并且接受与它所基于的任务相同类型的参数;
2、有一个例外:$swrite的第一个参数应该是一个整型、非合并字节数组或字符串数据类型的变量,而不是指定将生成的字符串写入到哪个文件的变量。
3、非合并字节数组字符顺序是从左向右。

例子:

string     string0;
string     string1;
$sformat(string0,  “string0_inst==%0d” , 100);
$display(%0s”, string0);		//string0_inst==100
string1=$sformatf(“string1_inst==%0d” , 100);
$display(%0s”, string1);		//string1_inst==100

这两个函数就是整理字符串的格式,按照函数里给的格式,把相应的变量填进去。

$sformat和$sformatf的不同点:
1、$sformat()比$sformatf()多了第一个参数,第一个参数就是放整理好的字符串的容器。$sformat()会直接把整理好的字符串放到第一个字符串类型的参数中。
2、$sformatf()没有$sformat()第一个参数。$sformatf()返回的值就是整理好的字符串。
比如我们在UVM验证平台常用的uvm_info宏的打印方式:

uvm中的应用:

`uvm_info(“driver”,”driver=100,UVM_LOW)
`uvm_info(“driver”,$sformatf(“driver==%0d” , 100),UVM_LOW)

其他可以写更少代码的应用:

//简便的方法
bit test[32];
foreach(test[i]) 
begin
	if($test$plusargs($sformatf(%0d_in”,i)))
     	test[i]=1;
end
./run_option +1_in
./run_option +5_in

おすすめ

転載: blog.csdn.net/Michael177/article/details/120940013