| |
包是SystemVerilog语言结构,它使相关的声明和定义能够在包名称空间中组合在一起。包可能包含类型定义,常量声明,函数和类模板。为了能在一个范围内使用包,必须先导入它,然后才能引用其内容。
包是组织代码的一种有用方法,也是确保对类型、类等的引用是一致的有用方法。UVM基类库包含在一个名为“uvm_pkg”的包中。在开发UVM测试平台时,应该使用包来收集和组织各种类定义,这些类定义是为实现agent、envs、sequence libraries、 test libraries等而开发的。
UVM Package Coding Guidelines
Package naming and file naming conventions:
应使用_pkg 后缀命名包。包含该包的文件的名称应反映包的名称,并具有.sv扩展名。【如YnrrubysimTestTop.sv文件中包含一个包,名字是package YnrrubysimTestTop】
例如:文件spi_env_pkg.sv将包含spi_env_pkg包。
Justification:.sv扩展名是一种约定,表示包文件是独立的编译单元。_pkg 后缀表示该文件包含一个包。这两个约定对人类和机器解析脚本都很有用。
Classes contained within a package should be `included
在包范围内声明的类模板应该分成具有.svh扩展名的单个文件。这些文件应该按照需要编译的顺序包含在包中【使用`include】。包文件是唯一应该使用`includes的地方,被'include的文件中不应该包含其他`include语句。
Justification:.将类声明在单独的文件中使它们更易于维护,并且还使包内容更清晰。
Imports from other packages should be declared at the head of the package
包的内容可能需要引用另一个包的内容。在这种情况下,外部包应该在包代码体的开头声明。单个文件(例如可能被`include的类模板)不应单独导入。
Justification:.将所有imports分组到一个地方可以清楚地了解包依赖那些文件或外部包。将imports放置在包的其他部分或内部被include的文件中,这可能会导致排序和潜在的类型冲突。
All the files used by a package should be collected together in one directory
要包含在包中的所有文件应该一起收集在一个目录文件夹中。这对于agents来说,它的目录结构需要是完整的独立包,这点尤为重要。
Justification:.这使编译更容易,因为只有一个include目录,它还有助于重用,因为包的所有文件可以很容易地收集在一起。
下面是UVM env的包文件示例。这个env包含两个agent(spi和apb)和一个寄存器模型,这些作为子包导入。与env相关的类模板被`include。
// Note that this code is contained in a file called spi_env_pkg.sv
//
// In Questa it would be compiled using:
// vlog +incdir+$UVM_HOME/src+<path_to_spi_env> <path_to_spi_env>
//
// Package Description:
//
package spi_env_pkg;
import uvm_pkg::*;
`include "uvm_macros.svh"
// Any further package imports:
import apb_agent_pkg::*;
import spi_agent_pkg::*;
import spi_register_pkg::*;
// Includes:
`include "spi_env_config.svh"
`include "spi_virtual_sequencer.svh"
`include "spi_env.svh"
endpackage: spi_env_pkg
Package Scopes
经常让用户感到困惑的是SystemVerilog package是一个范围。这意味着在package中声明的所有内容以及导入到package中的其他package的内容仅在本package的范围内可见。如果将包导入另一个范围(即另一个包或模块),则只能看到包的内容,而不是它导入的任何包的内容。如果新范围中需要这些其他包的内容,则需要单独导入它们。
//
// Package Scope Example
// ----------------------------------------------------
//
package spi_test_pkg;
// The UVM package has to be imported, even though it is imported
// in the spi_env package. This is because the import of the uvm_pkg
// is only visible within the current scope
import uvm_pkg::*;
// The same is true of the `include of the uvm_macros
`include "uvm_macros.svh"
// Import of uvm_pkg inside the spi_env package is not
// visible within the scope of the spi_test package
import spi_env_pkg::*;
// Other imports
// Other `includes
`include spi_test_base.svh
endpackage: spi_test_pkg