sv package

1. package 的用法:

包提供了一个编译范围,可以在其中进行类型、类、函数、任务和其他声明。然后可以在其他作用域(其他包或顶级编译单元作用域)中导入包。

包非常类似于C++中的命名空间。它们提供了一个以“打包”方式编译代码的区域,然后可以导入并在其他地方使用。

package 提供了一种在多个module/interface/programs中共享数据,方法,参数的方法

package my_pkg;
  typedef enum bit [1:0] { RED, YELLOW, GREEN, RSVD } e_signal;
  typedef struct { bit [3:0] signal_id;
                     bit       active;
                     bit [1:0] timeout; 
                   } e_sig_param;
 
  function common ();
      $display ("Called from somewhere");
     endfunction
 
    task run ( ... );
      ...
    endtask
endpackage
// Import the package defined above to use e_signal
import my_pkg::*;
 
class myClass;
  e_signal   my_sig;
endclass
 
module tb;
  myClass cls;
 
  initial begin
    cls = new ();
    cls.my_sig = GREEN;
    $display ("my_sig = %s", cls.my_sig.name());
    common ();
  end
endmodule

2.Passing parameters to package

 上面的参数化parameter P_MAX_DATA_WIDTH 可以由宏·得到,然后通过-D 编译过程中直接改变。

 

3.Packages, like C++ namepsaces

package a_pkg;
int value = 100;
class a_class;
  task print;
    $display ("value is %d", value);
  endtask
endclass
endpackage

package b_pkg;
int value = 200;
class b_class;
  task print;
    $display ("value is %d", value);
  endtask
endclass
endpackage

package c_pkg;
import a_pkg::value;
class c_class;
  task print;
    $display ("value is %d", value);
  endtask
endclass
endpackage
//Without packages the situation would be like this:
int value = 100;
class a_class;
  task print;
    $display ("value is %d", value);
  endtask
endclass

// int value = 100; // can't do... compilation error
class b_class;
  task print; // prints 100
    $display ("value is %d", value);
  endtask
endclass

class c_class;
  task print;
    $display ("value is %d", value);
  endtask
endclass
endpackage

Note that packages cannot be instantiated and there is only one instance of whatever is declared in a package.

They could be seen as the TB equivalent of top-level modules in RTL, i.e. modules which aren't instantiated.

 

posted on 2018-04-01 14:58  hematologist  阅读(776)  评论(0编辑  收藏  举报

导航