Monday, 28 December 2015

Package in SystemVerilog

First Example:
--------------------------------------------------------------------------------------------------------------------------------
package my_pkg1;
int unsigned a = 1;
endpackage : my_pkg1
module top1();
import my_pkg1 :: *;
initial begin
$display("my_pkg1::a = %0d", a);
end
endmodule : top1
//Output:
// my_pkg1::a = 1
view raw sv_pkg1.sv hosted with ❤ by GitHub

--------------------------------------------------------------------------------------------------------------------------------

Second Example:
--------------------------------------------------------------------------------------------------------------------------------
package my_pkg1;
int unsigned a = 1;
endpackage : my_pkg1
package my_pkg2;
import my_pkg1 :: *;
int unsigned b = 2;
endpackage : my_pkg2
module top2();
//import my_pkg1 :: *;
import my_pkg2 :: *;
initial begin
$display("top : my_pkg1::a = %0d, my_pkg2::b = %0d", a, b);
end
endmodule : top2
//Output:
// Identifier 'a' has not been declared yet. If this error is not expected, please check if you have set `default_nettype to none.
view raw sv_pkg2.sv hosted with ❤ by GitHub

--------------------------------------------------------------------------------------------------------------------------------

Third Example:
--------------------------------------------------------------------------------------------------------------------------------
package my_pkg1;
int unsigned a = 1;
endpackage : my_pkg1
package my_pkg2;
import my_pkg1 :: *;
int unsigned b = 2;
endpackage : my_pkg2
module top3();
import my_pkg1 :: *;
import my_pkg2 :: *;
initial begin
$display("top : my_pkg1::a = %0d, my_pkg2::b = %0d", a, b);
end
endmodule : top3
//Output:
// top : my_pkg1::a = 1, my_pkg2::b = 2
view raw sv_pkg3.sv hosted with ❤ by GitHub

--------------------------------------------------------------------------------------------------------------------------------
 
Fourth Example:
--------------------------------------------------------------------------------------------------------------------------------
package my_pkg1;
int unsigned a = 1;
endpackage : my_pkg1
package my_pkg2;
import my_pkg1 :: *;
int unsigned b = 2;
function void pkg2_print();
$display("my_pkg2 : pkg2_print : my_pkg1::a = %0d, my_pkg2::b = %0d", a, b);
endfunction : pkg2_print
endpackage : my_pkg2
module top4();
import my_pkg2 :: *;
initial begin
void '(pkg2_print());
end
endmodule : top4
//Output:
// my_pkg2 : pkg2_print : my_pkg1::a = 1, my_pkg2::b = 2
view raw sv_pkg4.sv hosted with ❤ by GitHub

--------------------------------------------------------------------------------------------------------------------------------

Fifth Example:
--------------------------------------------------------------------------------------------------------------------------------
package my_pkg1;
int unsigned a = 1;
endpackage : my_pkg1
package my_pkg2;
import my_pkg1 :: *;
int unsigned b = 2;
function void pkg2_print();
$display("my_pkg2 : pkg2_print : my_pkg1::a = %0d, my_pkg2::b = %0d", a, b);
endfunction : pkg2_print
endpackage : my_pkg2
package my_pkg3;
//import my_pkg1 :: *;
import my_pkg2 :: *;
int unsigned c = 3;
function void pkg3_print();
$display("my_pkg3 : pkg3_print : my_pkg1::a = %0d, my_pkg2::b = %0d, my_pkg3::c = %0d", a, b, c);
endfunction : pkg3_print
endpackage : my_pkg3
module top5();
import my_pkg3 :: *;
initial begin
void '(pkg3_print());
end
endmodule : top5
//Output:
// Identifier 'a' has not been declared yet. If this error is not expected, please check if you have set `default_nettype to none
view raw sv_pkg5.sv hosted with ❤ by GitHub

--------------------------------------------------------------------------------------------------------------------------------

Sixth Example:
--------------------------------------------------------------------------------------------------------------------------------
package my_pkg1;
int unsigned a = 1;
endpackage : my_pkg1
package my_pkg2;
import my_pkg1 :: *;
int unsigned b = 2;
function void pkg2_print();
$display("my_pkg2 : pkg2_print : my_pkg1::a = %0d, my_pkg2::b = %0d", a, b);
endfunction : pkg2_print
endpackage : my_pkg2
package my_pkg3;
import my_pkg1 :: *;
import my_pkg2 :: *;
int unsigned c = 3;
function void pkg3_print();
$display("my_pkg3 : pkg3_print : my_pkg1::a = %0d, my_pkg2::b = %0d, my_pkg3::c = %0d", a, b, c);
endfunction : pkg3_print
endpackage : my_pkg3
module top6();
import my_pkg3 :: *;
initial begin
void '(pkg3_print());
end
endmodule : top6
//Output:
// my_pkg3 : pkg3_print : my_pkg1::a = 1, my_pkg2::b = 2, my_pkg3::c = 3
view raw sv_pkg6.sv hosted with ❤ by GitHub

--------------------------------------------------------------------------------------------------------------------------------

Seventh Example:
--------------------------------------------------------------------------------------------------------------------------------
package my_pkg1;
int unsigned a = 1;
endpackage : my_pkg1
package my_pkg2;
import my_pkg1 :: *;
int unsigned b = 2;
function void pkg2_print();
$display("my_pkg2 : pkg2_print : my_pkg1::a = %0d, my_pkg2::b = %0d", a, b);
endfunction : pkg2_print
endpackage : my_pkg2
package my_pkg3;
//import my_pkg1 :: *;
import my_pkg2 :: *;
int unsigned c = 3;
function void pkg3_print();
$display("my_pkg3 : pkg3_print : my_pkg3::c = %0d", c);
$display("calling my_pkg2 :: pkg2_print");
pkg2_print();
endfunction : pkg3_print
endpackage : my_pkg3
module top7();
import my_pkg3 :: *;
initial begin
void '(pkg3_print());
end
endmodule : top7
//Output:
// my_pkg3 : pkg3_print : my_pkg3::c = 3
// calling my_pkg2 :: pkg2_print
// my_pkg2 : pkg2_print : my_pkg1::a = 1, my_pkg2::b = 2
view raw sv_pkg7.sv hosted with ❤ by GitHub

--------------------------------------------------------------------------------------------------------------------------------

Eighth Example: (package and module having variable with same name)
-------------------------------------------------------------------------------------------------------------------------------- 
package my_pkg1;
int unsigned a = 1;
endpackage : my_pkg1
module top();
int unsigned a = 2;
import my_pkg1 :: *;
initial begin
$display ("a=%0d", a);
$display ("my_pkg1::a=%0d", my_pkg1::a);
end
endmodule : top
//Output:
// a=2
// my_pkg1::a=1
view raw sv_pkg8.sv hosted with ❤ by GitHub

-------------------------------------------------------------------------------------------------------------------------------- 

Ninth Example: (package and module having variable with same name)
--------------------------------------------------------------------------------------------------------------------------------
package my_pkg1;
int unsigned a = 1;
endpackage : my_pkg1
module top();
// Not importing my_pkg1 here
int unsigned a = 2;
initial begin
$display ("a=%0d", a);
$display ("my_pkg1::a=%0d", my_pkg1::a);
end
endmodule : top
//Output:
// a=2
// my_pkg1::a=1
view raw sv_pkg9.sv hosted with ❤ by GitHub

--------------------------------------------------------------------------------------------------------------------------------

5 comments:

  1. thank you very much sagar,your examples are very very helpful in understanding the concepts

    ReplyDelete
  2. Very helpful for beginner

    ReplyDelete
  3. can package be used to store header files in SV testbench , If yes then how to solve the issue of file not found error arising ??

    ReplyDelete
  4. Very informative and simplified explanation for beginers

    ReplyDelete