Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1.
2.
3.
4.
Packages
Top Level Modules
Nested Modules
Extern Modules
Introduction - Packages
parameters
data
type
task
function
sequence
property
Rules :
Items within packages cannot have
hierarchical references.
Packages can not contain any assign
statement.
Variable declaration assignments within the
package shall occur before any initial,
always, always_comb, always_latch, or
always_ff blocks
Accessing data,functions or
types in packages
By class scope resolution operator ::
By import statement : The import statement
provides direct visibility of identifiers within
packages.
Example :
package definesPkg;
typedef enum {FALSE, TRUE} bool;
typedef struct {
endtask
endpackage
`include "definesPkg.sv
import definesPkg::bool
import definesPkg::* // all
module simple_package();
bool value = definesPkg::FALSE;
initial begin
msgPkg::msg_info("Testing Packages");
#10 msgPkg::msg_info("Testing Packages");
if (value ! = definesPkg::TRUE)
#10 msgPkg::msg_fatal("Value is FALSE");
End
endmodule
Module child ()
task print();
$display(in child module);
endtask
initial begin
$root.top.U.U.print();
end
endmodule
Module parent();
task print();
$display(in parent module);
endtask
child u();
initial begin
$root.top.u2.print();
$root.top.u.print();
end
endmodule
Module top();
child u2();
parent u();
task print();
$display(in top);
endtask
endmodule
Nested Modules
In Verilog 2001 and 1995, module declaration
inside another module is not allowed, only
modules can be instanciated. But in
SystemVerilog, module declaration can be nested
The outer name space is visible to the inner
module so that any name declared there can be
used, unless hidden by a local name, provided the
module is declared and instantiated in the same
scope.
One purpose of nesting modules is to show the
logical partitioning of a module without using
ports.
Module nested_module();
Module counter(input clk,reset,enable,
output logic [3:0]data);
always@(posedge clk)
if(reset)
data <= 0;
else if(enable)
data <= data + 1;
Endmodule
Logic clk = 0;
Always #1 clk = ~clk;
Logic enable,reset;
Wire [3:0] data;
Counter c1(clk,reset,enable,data);
Initial begin
$monitor($time,reset,enable,data);
#10 reset <= 0;
#1 enable <= 1;
#10 enable <= 0;
end
endmodule
Extern module
To support separate compilation, extern
declarations of a module can be used to
declare the ports on a module without
defining the module itself.
An extern module declaration consists of
the keyword externfollowed by the module
name and the list of ports for the module
InFo:
System verilog package globals instead of
`include
There are 2 files
globals2.svh (our include file)
example.sv (the main module)
Globals2.svh
const int I_PORTS_NUM2 = 1;
const real R_CONSTX2 = 1.66;
typedef enum {
E_MODE_X2 = 0,
E_MODE_Y2 = 1,
E_MODE_Z2 = 2
} e_modes2;
Example.sv
package globals;
const int I_PORTS_NUM = 0;
const real R_CONSTX = 1.33;
typedef enum {
E_MODE_X = 0,
E_MODE_Y = 1,
E_MODE_Z = 2
} e_modes;
endpackage // globals
module x;
`include "globals2.svh"
initial begin
$display("I_PORTS_NUM : %h", globals::I_PORTS_NUM);
$display("R_CONSTX : %f", globals::R_CONSTX);
$display("E_MODE_X : %h", globals::E_MODE_X);
$display("E_MODE_Y : %h", globals::E_MODE_Y);
$display("E_MODE_Z : %h", globals::E_MODE_Z);
$display("--");
$display("I_PORTS_NUM2: %h", I_PORTS_NUM2);
$display("R_CONSTX2 : %f", R_CONSTX2);
$display("E_MODE_X2 : %h", E_MODE_X2);
$display("E_MODE_Y2 : %h", E_MODE_Y2);
$display("E_MODE_Z2 : %h", E_MODE_Z2);
end
endmodule // x
#
#
#
#
#
#
#
#
#
#
#
I_PORTS_NUM : 00000000
R_CONSTX : 1.330000
E_MODE_X : 00000000
E_MODE_Y : 00000001
E_MODE_Z : 00000002
-I_PORTS_NUM2: 00000001
R_CONSTX2 : 1.660000
E_MODE_X2 : 00000000
E_MODE_Y2 : 00000001
E_MODE_Z2 : 00000002
Conclusion
We create a package called"globals"which has constants
defined in it - the same ones with a slightly different name
than in globals2.svh. Then, we define a module"x"and
have$displaystatements to show how to use those
variables the package way and the`includeway.
You have a choice to use the scope of
the"globals::"package, which I think is very nice for things
like constants - you get to know where the constant came
from. Or, you could choose to import everything"import
globals::*"and have those variables in your name space.
Or lastly, you could choose to only import certain
constants"import globals::I_PORTS_NUM". The choice is up
to you. But, youdohave a choice and some of those
possibilities might lead to easier code to reuse or read.
Quiz
What is $root?
Ans:$root refers to the top level instance in
SystemVerilog
1.packageABC;
2.$root.A;// top level instance A
3.$root.A.B.C;// item C within instance B
within top level instance A
Thank You