system中的“ref”是什么意思?

时间:2015-06-24 04:28:58

标签: system-verilog

我在public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> { private ArrayList<Note> mNotes; private int lastPosition = -1; private Context context; public HomeAdapter(ArrayList<Note> list, Context context) { this.mNotes = list; this.context = context; } public void clear() { mNotes.clear(); notifyDataSetChanged(); } public void addItem(Note n) { System.out.println("add"); mNotes.add(n); notifyDataSetChanged(); } public static class ViewHolder extends RecyclerView.ViewHolder { public View view; public ViewHolder(View itemView) { super(itemView); view = itemView; } } public HomeAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { //create new view View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_card, viewGroup, false); ViewHolder vh = new ViewHolder(v); return vh; } public TextView title; public TextView subtext; @Override public void onBindViewHolder(HomeAdapter.ViewHolder viewHolder, int i) { //Replace contents of a view, called by layout manager title = (TextView) viewHolder.view.findViewById(R.id.title); subtext = (TextView) viewHolder.view.findViewById(R.id.subtext); title.setText(mNotes.get(i).getTitle()); subtext.setText(mNotes.get(i).getBody()); setAnimation(viewHolder.view, i); } private void setAnimation(View viewToAnimate, int position) { // If the bound view wasn't previously displayed on screen, it's animated if (position > lastPosition) { Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left); viewToAnimate.startAnimation(animation); lastPosition = position; } } @Override public int getItemCount() { return mNotes.size(); } } 中找到了这个:

systemverilog

我想知道task automatic xxx(ref xxxpackage bus,input interface ift); 的用法。有什么好处?

3 个答案:

答案 0 :(得分:12)

通常,声明为input的任务和函数参数在进入例程时按值复制,声明为output的参数在从例程返回时按值复制。 inout个参数在进入和从例程返回时都被复制。使用ref声明的参数不会被复制,而是对调用例程时使用的实际参数的引用。使用ref参数时,有更严格的数据类型兼容性规则。

在耗费时间的任务中,可以使用ref而不是inout来捕获任务处于活动状态时发生的值更改。请记住,inout参数在调用时会复制到任务中,并在任务返回时复制出来。这是一个你应该尝试的例子。

module top;
logic A,B;
task automatic mytask(inout logic arg1, ref logic arg2);
  #0 $display("%m %t arg1 %b arg2 %b",$time,arg1,arg2);
  // actual arguments have been set to 0
  #5 $display("%m %t arg1 %b arg2 %b",$time,arg1,arg2);
  #0 arg1 = 1; arg2 = 1;
  #5 $display("%m %t arg1 %b arg2 %b",$time,arg1,arg2);
endtask
initial #1 mytask(A,B);
initial begin
       A = 'z; B ='z;
       #2 A = 0; B = 0; // after call 
       // arguments have been set to 1
       #5 $display("%m %t A %b B %b",$time,A ,B);
       #5 $display("%m %t A %b B %b",$time,A ,B);
end
endmodule

查看inout和传递ref参数之间的差异。

请注意,类变量已经是对类句柄的引用,因此通过引用传递类变量很少有任何好处。此外,在函数中,ref参数的唯一好处可能是在传递大型数据结构(如数组)时的性能,而不是使用inputoutputinout

答案 1 :(得分:3)

ref 参数是通过引用传递的变量。这种类型的参数不是副本,而是对原始变量的引用。

  

通过引用传递的参数不会复制到子例程区域中,而是将对原始参数的引用传递给子例程。然后子程序可以通过引用访问参数数据。

来自IEEE Std 1800-2012中的第13.5.2节。

答案 2 :(得分:1)

嘿,这是对DAVE给出的例子的解释。非常感谢Dave的例子。

module top;
logic A,B;
task automatic mytask(inout logic arg1, ref logic arg2);
  #0 $display("%m %t arg1 %b arg2 %b",$time,arg1,arg2);
  // actual arguments have been set to 0
  #5 $display("%m %t arg1 %b arg2 %b",$time,arg1,arg2);
  #0 arg1 = 1; arg2 = 1;
  #5 $display("%m %t arg1 %b arg2 %b",$time,arg1,arg2);
endtask
initial #1 mytask(A,B);
initial begin
       A = 'z; B ='z;
       #2 A = 0; B = 0; // after call 
       // arguments have been set to 1
       #5 $display("%m %t A %b B %b",$time,A ,B);
       #5 $display("%m %t A %b B %b",$time,A ,B);
end
endmodule

/*Both the two 'initial' statements are running simultaneously*/
/* 1) At time t=0 A and B are set to z by second initial statement
   2) At time t=1 mytask(A,B) is called by first initial 
      statement, 
      the first display statements displays arg1 and arg2 =z as 
      set by A and B.
   3) t=3 the second initial statement sets A=0 and B=0, but only  
      A=0 is passed to arg 1 in the ongoing task since it is
      passed by reference, whereas B=0 can only be passed at the
      starting or the end of the task since it is passed by value 
      hence arg2 remains z.
   4) inside the task--At t=6 values of arg1 and arg2 are 
      displayed
   5) at t=6 the values of arg1 and arg2 are made 1.
   6) in the second initial statement at t=7 values of A and B
      is displayed, since arg2 is passed through reference
      therefore it becomes 1, whereas A remains zero until the end of
   the task.
   7) at t=11 the values of arg1 and arg2 are displayed. -- task 
      ends.
   8) Since the task is ended arg2 value is passed to B and is 
      displayed by the second initial statement at t=12. 
   */

我已根据内核中显示的输出进行了解释,并显示希望这会有所帮助。