Packages & Imports

Share types, parameters and functions between the design and its testbench from one place.

A package is a named collection of declarations: parameters, typedefs, enums, structs, functions, tasks and classes. Modules and testbenches import it, so every file uses the same definitions instead of each keeping its own copy.

An ALU sharing a package with its testbench

import alu_pkg::*; makes every name in the package visible.

alu_pkg::W refers to one name without importing (the scope operator ::).

compute() is an automatic function, synthesizable inside the module and callable from the testbench.

Packages vs `include

module alu(input op_t op,...); import alu_pkg::*; // too late for the ports module alu import alu_pkg::*; (input op_t op,...);

An import inside the module body comes after the port list, so the ports cannot use the package's types. Import in the module header.

What is the difference between import pkg::*; and import pkg::name;?

The wildcard import makes every name in the package available when it is used (if not declared locally); an explicit import brings in just that one name. Explicit imports avoid name clashes between packages.