Packages - vineethkumarv/SystemVerilog_Course GitHub Wiki

Packages are group of functions ,tasks ,classes that are shareable.Packages are meant for the code reusability similar to functions ,tasks and classes but all under one entity.

image

     Fig.Illustration of a Package
  • In a package we can have classes,functions and tasks but cannot have modules.
  • If there is a requirement of using the same class or function or task or all in multiple modules,instead of writing them again and again,we just put all of them under one identity,package and by importing that package we can use them anywhere in the module.

Syntax :

package <package_name>;
...    
...  
endpackage :<package_name>
  • To use a package in any module we should import the package using import keyword followed by package_name.

Syntax :

import <package_name>::<method_name>;
import <package_name>::*; //to import everything

Example code

Package code:

package one;
int a;
string k;

class details;
int age;
string name;
function new(int a,string b);
  age=a;
  name=b;
endfunction
function void getdetails();
  $display("name is  %0s",name," ,age is %0d",age);
endfunction
task t1;
  $display("it is in task of class");
endtask: t1
function void hi;
  $display("hi");
endfunction
endclass

function void pack_func;
 details d;
 d=new(40,"raj");
 a=d.age;
 k=d.name;
 $display("in package function");
 $display("name given is %0s",k,", age is %0d",a);
endfunction

task pack_task;
 input int a;
 output string k="it is odd";
 if(a%2==0)begin
  k="it is even";
 end
endtask
endpackage : one  

In the above code the package one contains a class details with few functions.And along with that we have a function in package pack_func and a task pack_task.
The code within the package can only be compiled.

Module code:

 import one::*;//importing package
 module mod1;
 string id1;
 details emp1=new(21,"kumar");//instantiation of package class
 initial begin
  string g;
  $display("details of id1 are : %0d",emp1.age);
  emp1.getdetails();
  emp1.t1();
  pack_func();
  pack_task(10,g);
  $display(g);
 end
 endmodule : mod1 

Output:

image

Fig.Output of the code   

Here, in the code of the module we would like to use the class details of the package one, and for that we import the package using import keyword.It simply means that all the code written in the package is a part of the module now and can be accessed.Here the class handle emp1 is used to access the class properties and class methods.The package functions and tasks can be accessed as it they were like module functions and tasks.

More about packages

  • We can have multiple packages in single file.
  • Package cannot be static,extern,virtual,pure.
  • We can import a package within a package but cannot write a package inside a package.
  • If two packages have similar functions,then we use package name followed by scope resolution operator to access that function.

Accessing a package without importing package.
We can access package elements using scope resolution operator '::',as follows,

Code:

import hi::*;                                                                                                                                                                           
                                                                                                
function ironman;                                                                                   
 $display("I am ironman of the module");                                                           
endfunction:ironman                                                                                 
                                                                                                
module one;                                                                                         
                                                                                                
initial begin                                                                                       
hi::ironman();                                                                                    
void'(pack::test());                                                                              
//assigning();throws runtime error as not a syntax fault assigning is a funcion of package 'pack' and  
// without scope resolution operator it is an error as package is not imported.                                          
end                                                                                                 
endmodule:one

Output:

lab link:https://github.com/vineethkumarv/vk/blob/master/packages/pack_without_import.sv