Lesson 6: What Are the Basic Parts of a UVM Testbench?
UVM, the Universal Verification Methodology, is a SystemVerilog class library that gives every testbench the same parts with the same names: a sequence item describes one transaction, a sequence makes a stream of them, a driver turns them into pin wiggles, a monitor turns pins back into transactions, and a scoreboard checks them. An agent groups the driver, monitor and sequencer for one interface, an environment holds the agents and scoreboards, and a test configures it all. This lesson rebuilds the Lesson 2 ALU testbench in those parts.
What you'll learn
- The UVM component tree, and why it looks the way it does
- Phases: build, connect, run and report
- Sequences, the sequencer and the driver handshake
- Analysis ports from the monitor to the scoreboard
- The factory and the config database, briefly
Conventions and assumptions
Every lesson tests the same small design: a registered 8-bit ALU (add, sub, and, or, xor) whose 9-bit result appears one clock after in_valid; the final project moves on to the synchronous FIFO. All code was compiled and run in AMD Vivado xsim 2025.2, and the output shown is copied from those runs. The complete files are in the site's repository under scripts/verification.
Step 1: the transaction
A uvm_sequence_item holds the randomizable fields of one request, plus the result once the monitor sees it. The utils macro registers it with the factory, so a test can later swap it for a subclass without editing any other file.
class alu_item extends uvm_sequence_item;
rand bit [2:0] op;
rand bit [7:0] a, b;
bit [8:0] y;
constraint legal_op { op inside {[0:4]}; }
`uvm_object_utils(alu_item)
function new(string name = "alu_item"); super.new(name); endfunction
function string convert2string(); return $sformatf("op=%0d a=%0d b=%0d y=%0d", op, a, b, y); endfunction
endclassStep 2: sequence, sequencer and driver
The sequence's body() creates items and hands them over with start_item and finish_item. The sequencer arbitrates between sequences; the driver pulls the next item with get_next_item, drives the interface, and calls item_done. The driver finds the interface through uvm_config_db, which the top module filled in.
class alu_seq extends uvm_sequence #(alu_item);
`uvm_object_utils(alu_seq)
int unsigned n = 200;
function new(string name = "alu_seq"); super.new(name); endfunction
task body();
repeat (n) begin
alu_item it = alu_item::type_id::create("it");
start_item(it);
if (!it.randomize()) `uvm_fatal("SEQ", "randomize failed")
finish_item(it);
end
endtask
endclass
class alu_driver extends uvm_driver #(alu_item);
`uvm_component_utils(alu_driver)
virtual alu_if vif;
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void build_phase(uvm_phase phase);
if (!uvm_config_db#(virtual alu_if)::get(this, "", "vif", vif)) `uvm_fatal("DRV", "no vif")
endfunction
task run_phase(uvm_phase phase);
vif.in_valid <= 0;
@(posedge vif.clk iff vif.rst_n);
forever begin
seq_item_port.get_next_item(req);
@(negedge vif.clk);
vif.in_valid <= 1; vif.op <= req.op; vif.a <= req.a; vif.b <= req.b;
@(negedge vif.clk);
vif.in_valid <= 0;
seq_item_port.item_done();
end
endtask
endclassStep 3: monitor and scoreboard
The monitor only watches: it records each request and, when out_valid rises, writes the completed transaction to its analysis port. Anything connected to that port receives it; here the scoreboard compares it with a reference model. Keeping the checker out of the driver means the same monitor and scoreboard work for any stimulus.
class alu_monitor extends uvm_monitor;
`uvm_component_utils(alu_monitor)
virtual alu_if vif;
uvm_analysis_port #(alu_item) ap;
function new(string name, uvm_component parent); super.new(name, parent); ap = new("ap", this); endfunction
function void build_phase(uvm_phase phase);
if (!uvm_config_db#(virtual alu_if)::get(this, "", "vif", vif)) `uvm_fatal("MON", "no vif")
endfunction
task run_phase(uvm_phase phase);
alu_item pending;
forever begin
@(posedge vif.clk);
if (pending != null && vif.out_valid) begin
pending.y = vif.y;
ap.write(pending);
pending = null;
end
if (vif.in_valid) begin
pending = alu_item::type_id::create("seen");
pending.op = vif.op; pending.a = vif.a; pending.b = vif.b;
end
end
endtask
endclass
class alu_scoreboard extends uvm_scoreboard;
`uvm_component_utils(alu_scoreboard)
uvm_analysis_imp #(alu_item, alu_scoreboard) imp;
int checked, errors;
function new(string name, uvm_component parent); super.new(name, parent); imp = new("imp", this); endfunction
function bit [8:0] model(alu_item t);
case (t.op)
0: return t.a + t.b;
1: return {1'b0, t.a} - {1'b0, t.b};
2: return {1'b0, t.a & t.b};
3: return {1'b0, t.a | t.b};
4: return {1'b0, t.a ^ t.b};
default: return '0;
endcase
endfunction
function void write(alu_item t);
checked++;
if (t.y !== model(t)) begin
errors++;
`uvm_error("SCB", $sformatf("mismatch: %s, expected %0d", t.convert2string(), model(t)))
end
endfunction
function void report_phase(uvm_phase phase);
`uvm_info("SCB", $sformatf("%0d results checked, %0d errors", checked, errors), UVM_NONE)
endfunction
endclassStep 4: agent, environment and test
build_phase creates children top-down with type_id::create (through the factory); connect_phase wires ports; run_phase is the only phase that consumes time; report_phase prints results. The test raises an objection while its sequence runs, and the run phase ends when every objection is dropped.
class alu_test extends uvm_test;
`uvm_component_utils(alu_test)
alu_env env;
function new(string name, uvm_component parent); super.new(name, parent); endfunction
function void build_phase(uvm_phase phase);
env = alu_env::type_id::create("env", this);
endfunction
task run_phase(uvm_phase phase);
alu_seq seq = alu_seq::type_id::create("seq");
phase.raise_objection(this);
seq.start(env.agt.sqr);
repeat (3) @(posedge env.agt.drv.vif.clk);
phase.drop_objection(this);
endtask
endclassWhat the run showed
Compiled with xvlog -sv -L uvm and elaborated with xelab -L uvm (Vivado ships UVM 1.2), the test ran 200 random transactions:
UVM_INFO lesson6_tb.sv(105) @ 4045000: uvm_test_top.env.scb [SCB] 200 results checked, 0 errors
** Report counts by severity
UVM_INFO : 6
UVM_WARNING : 0
UVM_ERROR : 0
UVM_FATAL : 0That is the same result Lesson 2 got from about a third of the code. UVM's payoff comes later: a second test can run a different sequence, or override alu_item with a constrained subclass through the factory, without touching the driver, monitor or scoreboard.
Exercises
- E1. Write a sequence that sends only subtractions with a < b.
- E2. Where would you add the covergroup from Lesson 4?
- E3. Why does the monitor create a new item for each request instead of reusing one?
Solutions
- E1. Extend alu_seq and randomize with an inline constraint: it.randomize() with { op == 1; a < b; }.
- E2. In a coverage subscriber connected to the same analysis port as the scoreboard, so coverage sees exactly the transactions that were checked.
- E3. The analysis port passes a handle; if the monitor reused one object, a subscriber holding it would see its fields change under it.
Common mistakes
- Calling new() on components instead of type_id::create, which bypasses the factory.
- Forgetting to raise an objection, so the run phase ends at time 0.
- Doing checking in the driver.
- Getting the virtual interface from config_db in the constructor instead of build_phase.
- Copying a large UVM template before understanding the five or six parts that matter.
Check your understanding
- Q1. Which phase consumes simulation time? Answer: run_phase (and its sub-phases); build, connect and report take no time.
- Q2. What connects the monitor to the scoreboard? Answer: an analysis port on the monitor connected to an analysis imp on the scoreboard.
- Q3. What ends the test? Answer: when all raised objections in the run phase have been dropped.
The course
Related
Last verified
Run in Vivado xsim 2025.2 with its bundled UVM 1.2 on 16 November 2026; output copied from that run.