0

The difference between class and struct is straightforward, but where does package fit in wrt the whole scheme. Most of the packages I see have imports from library files and include other sv files in the directory. To be more specific, when are packages typically used in a optimal coding scheme, Is it a better practice to use header files instead if no specific variables are added at that level ? Thanks

2 Answers 2

3

Packages provide a compilation scope in which types, classes, function, tasks and other declarations can be made. Packages can then be imported in other scopes (either other packages or the top-level compilation unit scope).

I think of packages much like namespaces in C++. They provide an area to compile code in a "packaged" manner which can then be imported and used in other places.

Whether the package consists of declarations immediately within the package file, or come from header files which get `included within the package declaration is just a matter of preference.

The following two are equivalent:

// File my_pkg.sv
package my_pkg;
    class my_class;
    endclass
endpackage

--

// File my_class.sv
class my_class;
endclass

// File my_pkg.sv
package my_pkg;
    `include "my_class.sv"
endpackage
Sign up to request clarification or add additional context in comments.

Comments

2

Packages, like C++ namepsaces can be used to avoid variable name colisions.

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

Here two classes refer to global variables of their own packages, both named 'value'. and a third class chooses to use a global variable from a previous package using 'import'.

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

Also note that structs are treated somewhat like scalar or array variables. A class variable holds just a reference and a new call is required to create a class object. But struct variables like array variables directly hold values. These can also be used in synthesisable RTL code.

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.