systemverilog function的一点小case

关于function的应用无论是在systemverilog还是verilog中都有很广泛的应用,但是一直有一个模糊的概念困扰着我,今天刚好有时间来搞清楚并记录下来。

关于fucntion的返回值的问题:

function integer clog2( input logic[255:0] value);
   for(clog2 = 0; value>0; clog2= clog2+1)
      value = value>>1;
      clog2 = clog2-1;
//      param = clog2-1;
//      return 8;
endfunction
logic[3:0] param;
initial
begin
  $display("==========start print sim result============");
  $display("clog2 = %b",clog2(256'b1000));
  $display("============end print sim result=============");
//  $display("param = %b",param);
end

上述function vcs编译之后打印的结果为:

==========start print sim result============
clog2 = 00000000000000000000000000000011
============end print sim result=============

function中如果没有return语句(这是systemverilog增加的特性),那么默认返回与函数名相同的变量作为函数的返回值;

如果以下面的方式code:

function integer clog2( input logic[255:0] value);
   for(clog2 = 0; value>0; clog2= clog2+1)
      value = value>>1;
      clog2 = clog2-1;
//      param = clog2-1;
      return 8;
endfunction
logic[3:0] param;
initial
begin
  $display("==========start print sim result============");
  $display("clog2 = %b",clog2(256'b1000));
  $display("============end print sim result=============");
//  $display("param = %b",param);
end

那么编译之后打印的结果是什么呢?

==========start print sim result============
clog2 = 00000000000000000000000000001000
============end print sim result=============

看到了吧,function会以return语句声明的值作为函数的返回值;

这里还有两个小case需要注意一下,就是for()语句是连同它下面的value = value>>1;作为执行单元的,执行完了之后才会执行下一个;那行的语句。

还有这里要另外注意:

function 返回值的类型:

void:如果你想调用函数并且忽略它的返回值,可以使用void进行声明函数类型,比如函数只用来打印一些想要的信息:

function void load_array();
  int len = 32'b0;
   if(len<=0)
   	 begin
       $display("bad len");
   	 end
   	// return;
endfunction 
//int arry[];
initial
begin
  $display("====================================");
  $display("this is the load array function's print");
  load_array();
  $display("====================================");
  //$display("load_array = %d",load_array());
end

打印结果:

====================================
this is the load array function's print
bad len
====================================

但是你如果把这句解开:

 $display("load_array = %d",load_array());

那就会报错了,因为这里使用了load_array()的返回值,但是void函数是没有返回值的。

报错:

Void functions cannot be used in contexts which require return values

 函数类型还有logic,int,static,automatic,数组,结构体等等类型;

Systemverilog中static、automatic区别_automatic变量-CSDN博客

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值