Counter Verification Lab
Build a small but complete UVM testbench that drives a counter, observes its behavior, predicts the expected value, and catches a real DUT bug.
Learning objectives
- Translate a simple DUT specification into verification scenarios.
- Build a focused UVM testbench using a transaction, sequence, driver, monitor, and scoreboard.
- Use reset, directed stimulus, and in-order checking to verify counter behavior.
- Distinguish stimulus sent to the DUT from behavior observed from the DUT.
- Debug a failing test by locating the first point where expected and observed behavior diverge.
Step 1: Start with the DUT specification
A verification environment should begin with a behavioral contract, not with UVM classes.
For this lab, the DUT is an 8-bit counter. It has three important behaviors:
| Condition | Expected behavior |
|---|---|
reset == 1 |
Counter becomes 0. |
reset == 0, enable == 1 |
Counter increments by one. |
reset == 0, enable == 0 |
Counter holds its current value. |
The DUT can be intentionally small:
module counter (
input logic clk,
input logic reset,
input logic enable,
output logic [7:0] count
);
always_ff @(posedge clk) begin
if (reset)
count <= 8'h00;
else if (enable)
count <= count + 8'h01;
end
endmodule
That is enough behavior to create meaningful verification work. A good lab does not need a complicated DUT. It needs a clear contract and a checker capable of detecting violations of that contract.
Step 2: Define the verification architecture
Now map the DUT behavior onto the UVM architecture you have built throughout this course.
UVM TEST
|
v
SEQUENCE
|
v
SEQUENCER
|
v
DRIVER
|
| enable
v
+---------------+
| COUNTER |
| DUT |
+---------------+
|
| count
v
MONITOR
|
v
SCOREBOARD
|
expected vs observed
|
v
PASS / ERROR
Each component has one clear responsibility:
| Component | Responsibility |
|---|---|
| Test | Chooses the verification scenario and controls test completion. |
| Sequence | Creates counter transactions such as enable and disable operations. |
| Sequencer | Manages transaction flow between the sequence and driver. |
| Driver | Converts a transaction into DUT pin activity. |
| Monitor | Observes the DUT interface and reconstructs what actually happened. |
| Scoreboard | Predicts the correct counter value and compares it with the observation. |
Notice the important boundary:
Sequence = what we want to happen
Driver = how we apply it
Monitor = what actually happened
Scoreboard = whether that behavior was correct
This separation is what makes the environment reusable and debuggable.
Step 3: Create the counter interface
The DUT lives in the HDL world, while the UVM classes live in the class-based verification world. The interface connects those two worlds.
interface counter_if(input logic clk);
logic reset;
logic enable;
logic [7:0] count;
endinterface
The top-level testbench can instantiate the interface and connect it to the DUT:
module tb_top;
logic clk = 0;
always #5ns clk = ~clk;
counter_if counter_vif(clk);
counter dut (
.clk (clk),
.reset (counter_vif.reset),
.enable (counter_vif.enable),
.count (counter_vif.count)
);
initial begin
run_test("counter_test");
end
endmodule
The virtual interface is then passed into the driver and monitor using the configuration database, just as you learned earlier.
Step 4: Model one counter operation as a transaction
The counter has a very small protocol. For this lab, a transaction only needs to describe whether the counter should be enabled.
class counter_transaction extends uvm_sequence_item;
`uvm_object_utils(counter_transaction)
rand bit enable;
function new(string name = "counter_transaction");
super.new(name);
endfunction
endclass
There is an important design decision here: count is not a random stimulus field.
The sequence controls enable. The DUT produces count. The monitor observes that result.
Stimulus transaction
|
| enable = 1
v
Driver
|
v
DUT
|
| count = 7
v
Monitor
|
v
Observed transaction
This prevents a common verification mistake: treating DUT outputs as if they were inputs that the test should control.
Step 5: Write a directed sequence first
Do not begin the lab with constrained-random traffic. Start with a small deterministic scenario whose expected result can be calculated by hand.
For example:
Reset
Enable
Enable
Disable
Enable
The expected counter values are:
| Cycle | Enable | Expected count |
|---|---|---|
| Reset | - | 0 |
| 1 | 1 |
1 |
| 2 | 1 |
2 |
| 3 | 0 |
2 |
| 4 | 1 |
3 |
A simple sequence can generate the operations:
class counter_sequence
extends uvm_sequence #(counter_transaction);
`uvm_object_utils(counter_sequence)
function new(string name = "counter_sequence");
super.new(name);
endfunction
task body();
counter_transaction req;
repeat (2) begin
req = counter_transaction::type_id::create("req");
start_item(req);
req.enable = 1'b1;
finish_item(req);
end
req = counter_transaction::type_id::create("req");
start_item(req);
req.enable = 1'b0;
finish_item(req);
req = counter_transaction::type_id::create("req");
start_item(req);
req.enable = 1'b1;
finish_item(req);
endtask
endclass
This sequence deliberately avoids randomization. Its job is to prove that the complete environment works.
Step 6: Drive the transaction onto the interface
The driver translates counter_transaction into pin-level activity.
class counter_driver extends uvm_driver #(counter_transaction);
`uvm_component_utils(counter_driver)
virtual counter_if vif;
function new(string name = "counter_driver",
uvm_component parent = null);
super.new(name, parent);
endfunction
task run_phase(uvm_phase phase);
counter_transaction req;
forever begin
seq_item_port.get_next_item(req);
@(negedge vif.clk);
vif.enable <= req.enable;
seq_item_port.item_done();
end
endtask
endclass
The driver waits for a transaction, applies the requested enable value, and tells the sequencer that the item is complete.
Using a clock edge away from the DUT's active edge gives the driver a clean point to update the input before the next rising edge.
Step 7: Monitor the actual DUT behavior
The monitor is passive. It does not drive enable. Instead, it watches the interface and publishes observations.
For this simple lab, the monitor can sample after the counter's rising-edge state update:
class counter_monitor extends uvm_monitor;
`uvm_component_utils(counter_monitor)
virtual counter_if vif;
uvm_analysis_port #(counter_transaction) analysis_port;
function new(string name = "counter_monitor",
uvm_component parent = null);
super.new(name, parent);
analysis_port = new("analysis_port", this);
endfunction
task run_phase(uvm_phase phase);
counter_transaction observed;
forever begin
@(negedge vif.clk);
observed = counter_transaction::type_id::create("observed");
observed.enable = vif.enable;
`uvm_info(
"MON",
$sformatf(
"Observed enable=%0b count=%0d",
observed.enable,
vif.count
),
UVM_MEDIUM
)
analysis_port.write(observed);
end
endtask
endclass
For the first lab, the monitor reports enable and reads the DUT's count. In a production environment, you would normally place the observed output value into a dedicated observed transaction field rather than relying on a stimulus-only transaction shape.
The key lesson is more important than the exact class:
Driver -> applies stimulus
Monitor -> observes reality Step 8: Build the scoreboard
The scoreboard is where this becomes real verification.
Instead of saying “the counter should probably increment,” the scoreboard maintains its own expected state:
if reset:
expected = 0
else if enable:
expected = expected + 1
else:
expected = expected
This is a small reference model embedded in the checker. It predicts what the DUT should produce without reading the DUT's internal implementation.
class counter_scoreboard extends uvm_scoreboard;
`uvm_component_utils(counter_scoreboard)
uvm_analysis_imp #(counter_transaction,
counter_scoreboard) analysis_imp;
bit [7:0] expected_count;
int checks;
int errors;
function new(string name = "counter_scoreboard",
uvm_component parent = null);
super.new(name, parent);
analysis_imp = new("analysis_imp", this);
endfunction
function void write(counter_transaction t);
bit [7:0] observed_count;
/*
* In a complete implementation, the monitor should
* publish the observed count in the transaction.
*
* The comparison below represents the core checking
* algorithm.
*/
if (t.enable)
expected_count++;
checks++;
`uvm_info(
"SB",
$sformatf(
"Expected count=%0d after enable=%0b",
expected_count,
t.enable
),
UVM_MEDIUM
)
endfunction
function void report_phase(uvm_phase phase);
super.report_phase(phase);
if (errors == 0)
`uvm_info(
"SBPASS",
$sformatf("Counter checks=%0d errors=%0d",
checks, errors),
UVM_LOW
)
else
`uvm_error(
"SBFAIL",
$sformatf("Counter checks=%0d errors=%0d",
checks, errors)
)
endfunction
endclass
The important architecture is:
Observed transaction
|
v
Scoreboard
|
+---- Predict expected state
|
+---- Compare with observed state
|
v
Result
Step 9: Connect the environment
The environment owns system-level connections. The agent owns the sequencer-to-driver connection.
The overall structure is:
counter_env
|
+-- counter_agent
| |
| +-- sequencer
| +-- driver
| +-- monitor
|
+-- counter_scoreboard
|
+-- coverage (later)
The environment connects the monitor to the scoreboard:
function void connect_phase(uvm_phase phase);
super.connect_phase(phase);
agent.monitor.analysis_port.connect(
scoreboard.analysis_imp
);
endfunction
Now the transaction path is complete:
Sequence
|
v
Sequencer
|
v
Driver ---> Counter DUT
|
v
Monitor
|
v
Scoreboard
This is the architecture you have been building since Lesson 51. The lab is simply putting all the pieces together around a real DUT.
Step 10: Add reset deliberately
Reset is not just initialization boilerplate. It establishes the starting state from which the scoreboard can begin predicting.
A useful test timeline is:
RESET
|
v
expected_count = 0
|
v
+-------+-------+
| | |
enable enable disable
| | |
v v v
1 2 2
|
enable
|
v
3
The checker and DUT must agree about when reset takes effect. If reset is asserted, the scoreboard should establish the same known starting state rather than continuing an old prediction.
This is why reset verification belongs in the lab from the beginning rather than being added after the main test is complete.
Step 11: Create the test and control completion
The test creates the environment, starts the sequence, and manages the run-phase objection.
class counter_test extends uvm_test;
`uvm_component_utils(counter_test)
counter_env env;
function new(string name = "counter_test",
uvm_component parent = null);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
env = counter_env::type_id::create("env", this);
endfunction
task run_phase(uvm_phase phase);
counter_sequence seq;
phase.raise_objection(this);
seq = counter_sequence::type_id::create("seq");
seq.start(env.agent.sequencer);
phase.drop_objection(this);
endtask
endclass
The important separation is:
| Layer | Question it answers |
|---|---|
| Test | Which scenario should run? |
| Sequence | Which operations should be generated? |
| Driver | How are those operations applied to pins? |
| Monitor | What actually happened? |
| Scoreboard | Was the observed behavior correct? |
Step 12: Run the first smoke test
Your first successful run should be boring.
That is a good thing.
You want to see evidence that every boundary works before adding more complexity:
- The test starts.
- The environment is created.
- The sequence starts on the sequencer.
- The driver receives transactions.
- The DUT responds.
- The monitor observes activity.
- The scoreboard receives observations.
- The expected and observed values match.
- The objection is dropped.
- The test ends with no errors.
A useful log might look conceptually like:
TEST : Starting counter sequence
DRV : enable=1
MON : observed count=1
SB : expected=1 observed=1 PASS
DRV : enable=1
MON : observed count=2
SB : expected=2 observed=2 PASS
DRV : enable=0
MON : observed count=2
SB : expected=2 observed=2 PASS
DRV : enable=1
MON : observed count=3
SB : expected=3 observed=3 PASS
TEST : Counter test completed
Step 13: Break the DUT on purpose
A verification environment is not proven merely because a correct DUT passes.
Now introduce a controlled bug:
else if (enable)
count <= count + 8'h02;
Run the same test.
The scoreboard should eventually report something like:
Expected = 1
Observed = 2
ERROR: Counter value mismatch
This is one of the most important moments in the course.
A useful verification environment must fail when the DUT is wrong.
If the test still passes after the DUT is deliberately broken, investigate the checker before trusting the testbench.
Step 14: Debug the first mismatch
When the counter test fails, trace the transaction through the architecture.
1. Did the sequence generate the intended enable?
|
v
2. Did the sequencer deliver it?
|
v
3. Did the driver apply it at the correct clock boundary?
|
v
4. Did the DUT update count correctly?
|
v
5. Did the monitor sample the correct cycle?
|
v
6. Did the scoreboard predict the correct value?
|
v
7. Did the comparison use expected and observed data correctly?
For example, suppose the scoreboard says:
Expected = 3
Observed = 2
Do not immediately conclude that the DUT is broken. The monitor may have sampled before the DUT's state update, or the driver may have changed enable too late.
The first incorrect observation is more useful than the final error message.
Step 15: Expand the lab into real scenarios
Once the smoke test passes, expand the test plan instead of immediately making the sequence random.
Scenario A: Reset
Assert reset and verify that the counter returns to zero.
Scenario B: Increment
Keep enable asserted for several cycles and verify sequential increments.
Scenario C: Hold
Disable the counter and verify that the value remains unchanged.
Scenario D: Mixed operation
Alternate enable and disable operations and verify every resulting state.
Scenario E: Overflow
Drive the counter toward 8'hFF and verify the specified wraparound behavior.
Scenario F: Reset during activity
Start counting, assert reset, and verify that the state returns to the defined reset value.
These scenarios become the beginning of a real verification plan:
Requirement
|
+-- Reset
+-- Increment
+-- Hold
+-- Mixed operation
+-- Overflow
+-- Reset during activity
|
v
Test/Sequence
|
v
DUT stimulus
|
v
Checking
Lab challenge
Build and run the counter environment from the architecture above.
- Create the
counter_ifinterface and connect it to the DUT. - Create a
counter_transactioncontaining the stimulus information. - Build a sequencer and driver.
- Write a monitor that observes the counter after each active clock edge.
- Connect the monitor to a scoreboard through an analysis port.
- Verify reset, increment, and hold behavior.
- Break the DUT intentionally and prove that the scoreboard catches the error.
- Use the log and waveform to locate the first incorrect transaction.
Stretch challenge
Split the checker into two conceptual pieces:
Observed input
|
v
Reference Model
|
v
Expected transaction
|
+----------------+
|
Observed output ------> Scoreboard
|
v
Compare
This prepares you for more sophisticated environments where the prediction logic grows beyond a few lines of scoreboard code.
Common mistakes
| Mistake | Why it causes trouble | Better approach |
|---|---|---|
| Checking the DUT output in the driver | Mixes stimulus and checking responsibilities. | Let the monitor observe and the scoreboard check. |
| Sampling on the wrong clock edge | The monitor may see the previous counter value. | Choose a sampling point that is unambiguous for the protocol. |
| Comparing against a fixed value | Only one scenario is verified. | Maintain an expected state based on the specification. |
| Skipping reset checking | The starting state may be unknown or incorrectly assumed. | Make reset part of the verification scenario. |
| Making the first test random | A failure becomes harder to localize. | Start with deterministic stimulus. |
| Changing several components at once while debugging | You lose the ability to identify which layer introduced the problem. | Debug one boundary at a time. |
| Testing only a correct DUT | A broken checker can appear to work. | Introduce deliberate DUT bugs and verify that tests fail. |
Key takeaways
- A complete UVM environment becomes meaningful when it verifies a real DUT behavior against an explicit specification.
- The counter lab follows the full path: Test → Sequence → Sequencer → Driver → DUT → Monitor → Scoreboard.
- The driver applies intent; the monitor observes reality; the scoreboard determines correctness.
- Reset establishes the known starting state for both the DUT and the checker.
- Start with deterministic stimulus before adding constrained-random scenarios.
- A strong verification testbench must detect deliberately injected DUT bugs.
- When debugging, find the first point where expected and observed behavior diverge.
Core mental model
A verification environment is an evidence pipeline.
SPECIFICATION
|
v
STIMULUS
|
v
DUT
|
v
OBSERVATION
|
v
PREDICTION
|
v
COMPARISON
|
v
VERIFICATION RESULT
The counter is intentionally simple so that you can concentrate on the architecture. The same architecture scales to much larger designs: replace the counter with a FIFO, bus slave, packet processor, cache, or subsystem, and the verification problem becomes richer without changing the fundamental flow.
Build the environment so that the DUT has nowhere to hide a bug.
What comes next?
In the next lab, FIFO Verification Lab, the verification problem becomes more interesting. A FIFO introduces ordering, full/empty behavior, simultaneous operations, and the need to compare a stream of expected data against a stream of observed data.
The counter taught you to verify state changes. The FIFO will teach you to verify transaction streams and ordering.