RTL Studio Open IDE

1.9 · SYSTEMVERILOG CLASS

package and import

Define and import packages to centralize shared types and constants across design and testbench.

Watch on YouTube

What you practice

package and import (1.9). Define and import packages to centralize shared types and constants across design and testbench. It is in Packages — Part 1 · Design in SV of the SystemVerilog Masterclass. Synthesizable SystemVerilog that removes Verilog ambiguity and cuts boilerplate.

The same IDE is the SystemVerilog simulator. Slang lints the file, Verilator runs it, and the waveform viewer sits next to the source.

What you will learn

Today you will learn how to define shared types and constants in a SystemVerilog package and import them into modules and testbenches to avoid duplication and improve maintainability.

Learning objectives

  • Define a package containing enums, structs, and parameters.
  • Import package contents into a module using import pkg::*;.
  • Ensure the package is compiled before modules that use it.
  • Use package-defined types in state machines and data structures.

Theory

SystemVerilog packages centralize reusable declarations such as enums, typedefs, parameters, and functions. A package is defined once in a .sv file and imported into any module or testbench that needs it. This avoids redefining the same types in multiple files and prevents inconsistencies.

ConceptPurpose
packageEncapsulates reusable declarations
import pkg::*;Brings all package contents into local scope
golden/rtl/pkg.svSingle source of truth for shared definitions

The package must be compiled before modules that import it, so rtl/pkg.sv must appear before rtl/design.sv in the RTL filelist.

### Simulation vs synthesis tooling

import pkg::*; is valid SystemVerilog. Verilator (RtlStudio Simulation) supports it, which is why this chapter practices import in the DUT and testbench. Yosys (RtlStudio Synthesis) does not parse import yet — you will see a frontend error like unexpected TOK_IMPORT. That is a Yosys SV coverage limit, not illegal HDL. This chapter therefore focuses on simulation (has_synthesis is off).

Practice

  1. Open this chapter in RtlStudio — files appear automatically in Explore.
  2. Open golden/rtl/pkg.sv to see the package definition (reference only).
  3. Open student/rtl/design.sv and fill the blank import statement inside the DUT.
  4. Open student/tb/tb.sv and fill the blank import statement inside the testbench.
  5. Run Simulation from the sidebar and check the Log panel for expected output.
  6. Do not expect Synthesis to pass on the import-based golden in this chapter — see the tooling note above.

Summary

You practiced centralizing shared definitions like parameters, enums, and structs in a SystemVerilog package and importing them into both the DUT and testbench. This approach ensures consistency and avoids duplication by defining the package once in a single file and referencing it via import statements. You confirmed correct simulation by including the package file before the design file in the filelist. Remember that Verilator accepts import, while Yosys currently needs pkg:: qualified names in synthesizable RTL. The key takeaway is that packages provide a clean, scalable way to share type and constant definitions across multiple modules and testbenches.

Run it in the Verilog IDE

  1. Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
  2. Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
  3. Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
  4. Run Yosys when the lesson is synthesizable RTL, and keep testbench-only constructs out of that module.

The lesson is done when the simulation matches package and import. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.