story · 2020年05月09日

SystemVerilog教程之Task和Functions

SystemVerilog在Verilog 2001的Task和Function的基础上增加了在static
Task和Function中声明automatic 变量的能力,以及在automatic Task和Function中声明static变量的能力。 

Tasks&Functions

Default Port Direction:除非声明为其他类型,否则任何端口都被视为input

Default Data TYpe:除非声明为其他类型,否则端口的数据类型是logic类型。

begin..end:当使用多个语句时,没有必要使用begin..end

automatic :Systemverilog允许使用本地static或dynamic 变量。

life time:SystemVerilog中的Task和Function可以是static或dynamic的。

wire:Wire数据类型不能在端口列表中使用;

Tasks

module task_intro ();

initial begin
  #1 doInit(4,5);
  #1 doInit(9,6);
  #1 $finish;
end

task doInit (input bit [3:0] count, delay);
  automatic reg [7:0] a;
  if (count > 5) begin
    $display ("@%g Returning from task", $time);
    return;
  end
  #(delay) $display ("@%g Value passed is %d", $time, count);
endtask

endmodule

输出

@6 Value passed is  4
@7 Returning from task

Functions

module task_intro ();

bit a ;

initial begin
  #1 a = doInit(4,5);
  #1 a = doInit(9,6);
  #1 $finish;
end

function bit unsigned doInit (bit [3:0] count, add);
  automatic reg [7:0] b;
  if (count > 5) begin
    $display ("@%g Returning from function", $time);
    return 0;
  end
  b = add;
  $display ("@%g Value passed is %d", $time, count + b);
  doInit = 1;
endfunction

endmodule

输出

@1 Value passed is   9
@2 Returning from function

Argument Passing

SystemVerilog提供了通过valuereference将参数传递给Task 和function 的方法。

Pass by value

在Verilog 1995/2001中,可以通过值将一个参数传递给Task 和function 。此时仿真工具会对参数值进行复制。

module function_by_value ();

reg [7:0] data      ;
reg       parity_out;
integer   i         ;

function parity;
 input [31:0] data;
 integer i;
 begin
  parity = 0;
  for (i= 0; i < 32; i = i + 1) begin
    parity = parity ^ data[i];
  end
 end
endfunction

initial begin
  parity_out = 0;
  data = 0;
  for (i=250; i<256; i = i + 1) begin
   #5 data = i;
   parity_out = parity (data);
   $display ("Data = %b, Parity = %b", data, parity_out);
  end
  #10 $finish;
end

endmodule

输出

 Data = 11111010, Parity = 0
 Data = 11111011, Parity = 1
 Data = 11111100, Parity = 0
 Data = 11111101, Parity = 1
 Data = 11111110, Parity = 1
 Data = 11111111, Parity = 0

Pass by reference

Systemverilog可以通过reference传递参数,此时仿真器不会复制参数,而是将原始参数的reference传递给Task 和function。

当参数通过reference传递时,调用者所做的任何更改都会影响原来的参数值。

当参数声明为const ref时,调用者就不能改变参数值。

module function_by_ref ();

reg [7:0] data      ;
reg       parity_out;

time      ltime;

function reg parity (ref reg [7:0] idata, const ref time tdata);
 parity = 0;
 for (int i= 0; i < 8; i ++) begin
    parity = parity ^ idata[i];
 end
 // We can modify the data passed through reference
 idata ++ ;
 // Something that is passed as const  ref, can  not be modified
 // tdata ++ ; This is wrong
endfunction

initial begin
  parity_out = 0;
  data = 0;
  for (int i=250; i<256; i ++) begin
   #5 data = i;
   ltime = $time;
   parity_out = parity (data, ltime);
   $display ("Data = %00000000b, Parity = %b, Modified data : %b",
      i, parity_out, data);
  end
  #10 $finish;
end

endmodule

输出

 Data = 11111010, Parity = 0, Modified data : 11111011
 Data = 11111011, Parity = 1, Modified data : 11111100
 Data = 11111100, Parity = 0, Modified data : 11111101
 Data = 11111101, Parity = 1, Modified data : 11111110
 Data = 11111110, Parity = 1, Modified data : 11111111
 Data = 11111111, Parity = 0, Modified data : 00000000



本文转载自公众号:芯片数字实验室
原文链接:
https://mp.weixin.qq.com/s?\_\_biz=MzU4ODY5MzkzOA==&mid=2247485226&idx=1&sn=4f456ff6dfdc8a113f559d0a4e21fc58&chksm=fdd9aff7caae26e1677173ab6c13ab186a4827eb1ffe03a8fdc14d2d412af97829b0745cba25&mpshare=1&scene=1&srcid=&sharer\_sharetime=1588754985133&sharer\_shareid=8ae9fe2db3774940c41ec158014781a8#rd
未经作者同意,请勿转载!

推荐阅读

想了解更多内容,欢迎关注芯片数字实验室专栏
推荐阅读
关注数
12315
内容数
220
前瞻性的眼光,和持之以恒的学习~
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息