Module 2: UVM Foundations | Beginner UVM

The UVM Factory

Learn why UVM uses a factory, how factory-based creation works, and why it makes verification environments easier to reuse and customize.

Learning objectives

By the end of this lesson, you will be able to:

  • Explain why UVM provides a factory.
  • Understand the difference between new() and type_id::create().
  • Register a UVM object with the factory.
  • Create a sequence item through the UVM factory.
  • Explain how factory creation enables testbench reuse.
  • Recognize when factory-based creation should be used.

Step 1: What problem does the factory solve?

Before learning the UVM Factory, start with a simple question:

What happens if a testbench creates every object directly with new()?

Direct construction works perfectly well in small SystemVerilog programs. For example:

packet pkt;


pkt = new();

The problem appears when we build a reusable UVM environment. A test may want to replace one implementation with another without modifying the environment itself.

For example, imagine that an environment normally uses:

normal_packet

but one test wants to use:

error_packet

If the environment directly contains:

pkt = new();

the class being constructed is fixed in the source code. Replacing it becomes difficult.

The UVM Factory introduces another level of indirection:

Key idea: Instead of asking an object to directly construct a specific class, UVM can ask the factory to construct an object of the requested type.

This allows a test to customize which implementation is actually created. That mechanism becomes especially powerful when we introduce factory overrides.

Factory overrides are covered in detail in the next lesson.

Step 2: Direct construction with new()

Let's start with normal SystemVerilog object construction.

class packet extends uvm_sequence_item;


rand bit [7:0] addr;
rand bit       write;

function new(string name = "packet");
super.new(name);
endfunction

endclass

We can create the object directly:

packet pkt;


pkt = new("pkt");

This means the code explicitly requests a packet. SystemVerilog constructs that exact type.

Think of new() as: "Create exactly this class."

Direct construction is not inherently wrong. There are places where using new() is appropriate.

However, many UVM objects and components should be created through the factory when we want the testbench to support factory overrides.

Step 3: Register a class with the factory

For the factory to work with a class, the class must be registered with the UVM factory.

For a sequence item, a common way to do this is:

class packet extends uvm_sequence_item;


rand bit [7:0] addr;
rand bit       write;

`uvm_object_utils_begin(packet)
    `uvm_field_int(addr,  UVM_DEFAULT)
`uvm_field_int(write, UVM_DEFAULT)
  `uvm_object_utils_end

function new(string name = "packet");
super.new(name);
endfunction

endclass

The `uvm_object_utils family of macros provides the factory registration and supporting utilities for UVM objects.

You do not need to memorize everything generated by the macro yet. For now, remember this:

Remember: If you want to create a UVM object through the factory, the type must be registered with the factory.

We will study the registration macros in more detail later. The important concept here is the relationship:

UVM object
↓


registered with factory
↓
factory can create it
↓
factory can potentially replace it

Step 4: Create the object through the factory

Once packet is registered, we can create it through the factory:

packet pkt;


pkt = packet::type_id::create("pkt");

At first glance, this looks more complicated than:

pkt = new("pkt");

So why use it?

The important difference is not what happens in the simplest case. Both approaches can produce a packet. The important difference is that the factory-based version gives UVM an opportunity to apply factory rules before the object is created.

Key idea: type_id::create() asks the UVM Factory to create the object. It does not simply hard-code direct construction with new().

This is what makes factory overrides possible.

Step 5: Understand type_id

You will see code such as:

packet::type_id::create("pkt");

This notation is generated and supported by the UVM factory registration mechanism.

For a beginner, it is useful to break the expression into pieces:

packet


↓
the class we want

packet::type_id
↓
the factory registration associated with that class

create("pkt")
↓
request an object from the factory

The exact internal implementation is more advanced than we need at this point. What matters is the programming model:

Factory programming model: Register the type, then request its creation through type_id::create().

Step 6: new() vs type_id::create()

This is one of the most important comparisons in beginner UVM.

Direct construction Factory construction
new() type_id::create()
Directly constructs the requested class Requests creation through the UVM Factory
Cannot use UVM factory overrides for that creation Can participate in factory overrides
Simple and explicit More flexible and reusable

A useful mental model is:

new()


↓
"Give me this exact type."

type_id::create()
↓
"Factory, please create this type for me."

The second approach gives the factory an opportunity to substitute another registered implementation.

Step 7: See why this matters for reuse

Imagine that we have a base transaction:

class packet extends uvm_sequence_item;


rand bit [7:0] addr;
rand bit       write;

`uvm_object_utils(packet)

function new(string name = "packet");
super.new(name);
endfunction

endclass

Now imagine a specialized transaction:

class error_packet extends packet;


`uvm_object_utils(error_packet)

function new(string name = "error_packet");
super.new(name);
endfunction

endclass

A reusable sequence might create the base type:

packet pkt;


pkt = packet::type_id::create("pkt");

The sequence does not need to know whether a test wants a specialized implementation.

A test can later configure a factory override so that requests for packet produce an error_packet.

Important: The sequence did not change. The reusable environment did not change. The factory controls the substitution.

This is one of the central ideas behind UVM reuse:

Reusable code
  ↓


requests a base type
↓
factory decides what implementation to create
↓
test can customize behavior

We will build this exact idea with real factory overrides in Lesson 18: Factory Creation Patterns.

Step 8: A complete small example

Here is a minimal example that puts the important pieces together.

class packet extends uvm_sequence_item;


rand bit [7:0] addr;
rand bit       write;

`uvm_object_utils_begin(packet)
    `uvm_field_int(addr,  UVM_DEFAULT)
`uvm_field_int(write, UVM_DEFAULT)
  `uvm_object_utils_end

function new(string name = "packet");
super.new(name);
endfunction

endclass

module example;

packet pkt;

initial begin


// Create through the UVM factory
pkt = packet::type_id::create("pkt");

// Give the transaction some values
pkt.addr  = 8'hA5;
pkt.write = 1'b1;

// Display the object
$display("%s", pkt.sprint());


end

endmodule

Trace the example in this order:

  1. packet extends uvm_sequence_item.
  2. The UVM object registration macro registers the type.
  3. packet::type_id::create() requests an object from the factory.
  4. The returned object is stored in pkt.
  5. The transaction is populated and printed.
Debugging tip: If factory creation fails, first check that the class is registered correctly and that the class has an appropriate constructor.

Step 9: What the factory is really buying you

The factory does not make a transaction faster, smaller, or more random.

Its main value is flexibility.

Without the factory:

Test


↓
Environment
↓
Sequence
↓
new(packet)

The requested type is tightly connected to the source code.

With factory-based creation:

Test


↓
Environment / Sequence
↓
type_id::create()
↓
UVM Factory
↓
requested type
OR
override type

This separation allows tests to customize an environment without rewriting the reusable components.

Professional mindset: The UVM Factory is primarily a reuse and substitution mechanism, not just another way to call a constructor.

Step 10: Common beginner mistakes

Mistake 1: Thinking the factory is required for every object

Not every SystemVerilog object must be created through the factory. The factory is useful when you want UVM's type substitution and override capabilities.

Mistake 2: Using new() everywhere

If a UVM object or component is intended to be replaceable through the factory, creating it directly with new() bypasses the factory for that creation.

Mistake 3: Thinking type_id::create() is magic

It is not magic. The class is registered with the factory, and the factory uses that registration to create the requested type and apply any applicable override.

Mistake 4: Learning override syntax before understanding the problem

Factory override APIs are much easier to understand once you know why the factory exists.

First learn:

Why?


↓
Factory
↓
type_id::create()
↓
Substitution

Then learn the different override patterns.

Quick check

Question 1

What is the main reason UVM provides a factory?

Show answer

The factory allows UVM code to request objects or components by type while giving tests the ability to substitute different implementations through factory overrides.

Question 2

Which statement requests an object through the factory?

A. packet pkt = new("pkt");


B. packet pkt;
pkt = packet::type_id::create("pkt");

C. packet pkt;
pkt = packet.create();
Show answer

B. packet::type_id::create("pkt") requests the object through the UVM factory.

Question 3

Why can new() prevent a factory override from affecting a particular creation?

Show answer

new() directly constructs the specified class. It does not ask the UVM factory to resolve the requested type.

Try it yourself

Create a small packet class that extends uvm_sequence_item.

  1. Register the class with the UVM factory.
  2. Add an addr field.
  3. Create the object using packet::type_id::create().
  4. Assign 8'h55 to addr.
  5. Print the transaction using sprint().
Challenge: Change the creation from type_id::create() to new(). Ask yourself: what capability did you lose by bypassing the factory?

Key takeaways

  • The UVM Factory provides a mechanism for flexible object and component creation.
  • new() directly constructs the requested class.
  • type_id::create() requests creation through the UVM Factory.
  • A type must be registered appropriately to participate in factory-based creation.
  • Factory-based creation enables factory overrides and makes reusable verification environments easier to customize.
  • The factory is valuable because of reuse and substitution, not because it is simply another constructor.

What's next?

You now understand the reason the UVM Factory exists and how type_id::create() differs from direct construction.

In the next lesson, we will go one step further and learn how to actually use the factory to substitute one implementation for another.

Next lesson: Factory Creation Patterns — type overrides, instance overrides, and practical factory customization.