user1820066
user1820066

Reputation: 11

System verilog : instantiation of a package object

I have a package where classes param0 and param1 are declared. Could someone explain me why the instantiation of these classes

( ex:param0 = new() ) 

should be done on the top module and not directly inside the package itself?

The reason why I would like to do it inside the package was to use some parameters of the class param0 inside the class param1.

Upvotes: 1

Views: 3372

Answers (3)

dave_59
dave_59

Reputation: 42616

One issue with instantiating objects in a package is the question, if no one uses (imports) the package, does the object still get need to get constructed? The SystemVerilog standard does not answer this question and leaves it up to the tool vendors. So you are much safer leaving them out of your package.

Upvotes: 1

jclin
jclin

Reputation: 2549

I'm using VCS and I don't see why class objects cannot be allocated in a package. The following code is working in VCS and ncverilog.

package pkg;
    class Base;
        int x;
        function new();
            x=1;
        endfunction
        function int value();
            return x;
        endfunction
    endclass : Base
    class C;
      Base b;
      function new();
        b = new;
      endfunction
      function get();
        return b.value();
      endfunction
    endclass : C

    C d = new;
endpackage

pkg::C e = new;

module top;
    import pkg::*;
    C c;
    initial begin
        c = new;
        $display("c=%d", c.get());
        $display("d=%d", d.get());
        $display("e=%d", e.get());
    end
endmodule

But it is not a good coding style to declare such like global variable or instance in a package. The package should only be used to write declarations. Even you are not doing so in the package, but declare them outside the module/program/package, it belongs to $unit package. Of course, you can write class function body definition, functions or tasks, with any new operation for class instantiations. The idea of a package is the declaration only, so any classes, functions, tasks, net/variables are going to be imported to modules or programs.

Upvotes: 1

user597225
user597225

Reputation:

Packages are intended only for declarations and cannot contain any processes, outside of checkers. Since class new is a special function call, it must be used in a process context and thus is not allowed in a package.

You can directly reference class member parameters using class1::parameter1.

Upvotes: 0

Related Questions