Module 8: Protocol Projects | Intermediate UVM

SPI Verification Project

Build a reusable UVM environment that verifies SPI transfers, clock modes, chip-select behavior, data ordering, and protocol errors.

Learning objectives

  • Model SPI transfers as transactions rather than individual signal transitions.
  • Explain how CPOL and CPHA determine the SPI clocking mode.
  • Build and adapt SPI sequences, drivers, monitors, and a scoreboard.
  • Verify chip-select timing, bit ordering, full-duplex data, and transfer boundaries.
  • Use assertions and functional coverage to measure protocol correctness.
  • Debug an SPI failure by connecting transaction-level evidence to waveform activity.

Step 1: Understand the SPI protocol

SPI is a synchronous serial protocol commonly organized around four signals: SCLK, MOSI, MISO, and CS (or chip select). Unlike UART, SPI does not define one universal frame format. The device specification determines details such as clock polarity, clock phase, bit order, word length, and chip-select behavior.

A useful verification plan therefore starts by turning the DUT specification into explicit protocol requirements:

  • Clock polarity (CPOL): determine whether SCLK is idle low or idle high.
  • Clock phase (CPHA): determine which clock edge captures data and which edge changes it.
  • Bit ordering: verify MSB-first or LSB-first behavior as required by the DUT.
  • Word length: exercise the supported transfer sizes.
  • Chip select: verify assertion, deassertion, and transfer boundaries.
  • Full-duplex behavior: check MOSI and MISO independently during the same clock transfer.
  • Clock timing: verify the supported SCLK frequency and required setup/hold relationships.
  • Back-to-back transfers: verify whether CS must toggle between words or may remain asserted.

The most important abstraction is the SPI transaction. A sequence should describe the data and configuration required for a transfer. The driver owns the clock and signal timing, while the monitor independently reconstructs what actually appeared on MOSI and MISO.

Key idea: CPOL and CPHA define four common SPI modes. Do not assume that a driver written for one mode can be reused unchanged for another; the sampling and driving edges must be modeled explicitly.

Step 2: Model an SPI transaction

The transaction should describe what should be transferred, not how the wires should toggle.

class spi_item extends uvm_sequence_item;


rand bit [7:0] tx_data;
bit [7:0] rx_data;

rand bit       cpol;
rand bit       cpha;
rand bit       lsb_first;

`uvm_object_utils(spi_item)

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

The exact fields depend on the DUT. For a fixed 8-bit SPI peripheral, for example, CPOL, CPHA, and bit order might be configuration rather than randomized transaction fields.

Notice that rx_data is not randomized. It represents information observed from the DUT. Separating expected inputs from observed outputs prevents the testbench from accidentally treating its own prediction as evidence.

Step 3: Build the SPI driver

The driver converts the transaction into SCLK, MOSI, and CS activity. The critical part is implementing the correct active edge and sampling edge for the selected CPOL/CPHA mode.

class spi_driver extends uvm_driver #(spi_item);


virtual spi_if vif;

`uvm_component_utils(spi_driver)

function new(string name = "spi_driver",
uvm_component parent = null);
super.new(name, parent);
endfunction

task run_phase(uvm_phase phase);
spi_item req;


forever begin
  seq_item_port.get_next_item(req);

  drive_transfer(req);

  seq_item_port.item_done();
end


endtask

task drive_transfer(spi_item req);
// Assert CS for the transfer.
vif.cs <= 1'b0;


// Generate the configured SCLK edges.
// Drive MOSI and sample MISO according
// to CPOL/CPHA and bit-order configuration.

repeat (8) begin
  drive_next_bit(req);
  clock_next_edge(req);
  sample_return_bit(req);
end

// End the transaction.
vif.cs <= 1'b1;


endtask
endclass

The helper tasks are intentionally abstract. A real implementation must define the exact timing of every SCLK transition and data sample.

Common mistake: implementing SPI as “toggle clock, then shift data” without defining which edge changes data and which edge samples it. That shortcut often works in one mode and silently fails in another.

Step 4: Build an independent monitor

The monitor should observe the interface rather than copying values from the driver. It should detect chip-select assertion, observe SCLK edges, sample MOSI and MISO at the correct points, and publish a completed transaction.

class spi_monitor extends uvm_monitor;


virtual spi_if vif;
uvm_analysis_port #(spi_item) ap;

`uvm_component_utils(spi_monitor)

function new(string name = "spi_monitor",
uvm_component parent = null);
super.new(name, parent);
ap = new("ap", this);
endfunction

task run_phase(uvm_phase phase);
spi_item item;


forever begin
  wait (vif.cs == 1'b0);

  item = spi_item::type_id::create("item");

  // Decode MOSI and MISO from observed SCLK edges.
  // Determine frame length and transfer mode.

  wait (vif.cs == 1'b1);

  ap.write(item);
end


endtask
endclass

The monitor is particularly important in SPI because a transaction can look correct from the driver's perspective while the DUT is sampling on the wrong edge or interpreting the bit order differently.

Step 5: Check both directions

SPI is commonly full-duplex: a bit may be transmitted on MOSI while another bit is simultaneously returned on MISO. Therefore, a scoreboard should not only check the transmitted command. It should also compare the expected response with the observed MISO data.

function void write_actual(spi_item actual);


spi_item expected;

if (expected_fifo.size() == 0) begin
`uvm_error("SPI_SB",
"Observed SPI transfer without an expected transaction")
return;
end

expected = expected_fifo.pop_front();

if (actual.tx_data !== expected.tx_data) begin
`uvm_error("SPI_SB",
$sformatf("MOSI mismatch: expected=0x%02h actual=0x%02h",
expected.tx_data, actual.tx_data))
end

if (actual.rx_data !== expected.rx_data) begin
`uvm_error("SPI_SB",
$sformatf("MISO mismatch: expected=0x%02h actual=0x%02h",
expected.rx_data, actual.rx_data))
end
endfunction

For a memory-mapped SPI peripheral, the expected response may depend on the command, address, register contents, or previous transaction. In that case, the scoreboard should model the expected protocol behavior rather than simply comparing against a fixed byte.

Step 6: Verify chip-select behavior

CS is part of the protocol, not merely a signal used to enable the DUT. Many SPI devices interpret CS transitions as transaction boundaries.

Test cases should answer questions such as:

  1. Does CS assert before the first SCLK edge?
  2. Does CS remain active for the complete transfer?
  3. Does CS deassert after the final required clock edge?
  4. Can multiple words be transferred while CS remains asserted?
  5. Does the DUT reset its bit counter when CS is deasserted?
  6. What happens if CS is asserted for an incomplete frame?

These checks are especially useful for finding off-by-one errors in shift counters and frame-state machines.

Step 7: Exercise all supported SPI modes

If the DUT supports multiple SPI modes, make the mode part of the verification matrix rather than testing only the default configuration.

class spi_mode_seq extends uvm_sequence #(spi_item);


`uvm_object_utils(spi_mode_seq)

task body();
spi_item req;


for (int mode = 0; mode < 4; mode++) begin
  req = spi_item::type_id::create($sformatf("req_%0d", mode));

  start_item(req);

  req.cpol = (mode >= 2);
  req.cpha = (mode == 1 || mode == 3);
  req.tx_data = 8'hA5;

  finish_item(req);
end


endtask
endclass

This gives the testbench an explicit way to exercise the four common CPOL/CPHA combinations. Only enable modes that are actually supported by the DUT.

Step 8: Add assertions and coverage

Assertions are useful for checking signal-level protocol rules continuously.

// Conceptual example:


// SCLK should not toggle while CS is inactive.
property sclk_stable_when_cs_inactive;
@(posedge vif.clk)
vif.cs |-> $stable(vif.sclk);
endproperty

assert property (sclk_stable_when_cs_inactive)
else `uvm_error("SPI_ASSERT",
"SCLK changed while chip select was inactive");

Functional coverage should capture meaningful combinations rather than simply counting transfers.

covergroup spi_cg;


mode_cp: coverpoint spi_mode {
bins mode0 = {0};
bins mode1 = {1};
bins mode2 = {2};
bins mode3 = {3};
}

bit_order_cp: coverpoint lsb_first;

data_cp: coverpoint tx_data {
bins zero = {8'h00};
bins ones = {8'hFF};
bins alternating = {8'h55, 8'hAA};
}

mode_x_order:
cross mode_cp, bit_order_cp;
endgroup

Coverage should answer a verification question such as: “Have all supported SPI modes been exercised with both important data patterns and bit-order configurations?”

Step 9: Add negative and boundary scenarios

Once normal transfers pass, deliberately exercise conditions that expose protocol-state bugs.

  • Transfer all-zero and all-one data.
  • Use alternating patterns such as 8'h55 and 8'hAA.
  • Exercise the minimum and maximum supported transfer lengths.
  • Change CPOL/CPHA configuration between tests.
  • Exercise MSB-first and LSB-first modes when supported.
  • Terminate a transfer earlier than expected.
  • Assert CS for an incomplete frame.
  • Attempt back-to-back transfers with the required CS behavior.
  • Exercise the fastest and slowest supported SCLK rates.

For every negative test, define the expected DUT response. A negative test is successful only when the DUT detects or handles the abnormal condition as specified.

Step 10: Assemble the reusable environment

A production-oriented SPI environment should have clear ownership of protocol behavior:

  • Sequence: defines transfer scenarios and expected behavior.
  • Driver: generates SPI signal activity and timing.
  • Monitor: independently reconstructs observed MOSI/MISO transfers.
  • Scoreboard: checks expected versus observed transactions.
  • Assertions: enforce signal-level protocol rules.
  • Coverage: measures SPI modes, data patterns, lengths, and meaningful combinations.
  • Environment: connects and configures the reusable components.
class spi_env extends uvm_env;


spi_agent      agent;
spi_scoreboard scoreboard;

`uvm_component_utils(spi_env)

function new(string name = "spi_env",
uvm_component parent = null);
super.new(name, parent);
endfunction

function void build_phase(uvm_phase phase);
super.build_phase(phase);


agent = spi_agent::type_id::create("agent", this);
scoreboard =
  spi_scoreboard::type_id::create("scoreboard", this);


endfunction

function void connect_phase(uvm_phase phase);
agent.monitor.ap.connect(scoreboard.actual_export);
endfunction
endclass

The goal is not simply to make one SPI test pass. The goal is to create an agent that can be reused for different SPI peripherals while keeping device-specific expectations in the appropriate test or reference model.

Step 11: Debug an SPI failure systematically

When a scoreboard reports an SPI mismatch, do not immediately modify the DUT or driver. First determine which layer introduced the discrepancy.

  1. Record the test name, seed, SPI mode, and configuration.
  2. Identify the first failing transaction.
  3. Compare expected and observed MOSI/MISO values.
  4. Check whether CS boundaries were decoded correctly.
  5. Inspect the SCLK idle level and active/sample edges.
  6. Check bit ordering and transfer length.
  7. Compare monitor decoding against the waveform.
  8. Only after verifying the testbench observation should you investigate the DUT state machine.

A useful debugging technique is to start with a single byte such as 8'hA5. Its alternating bit pattern makes bit-order and sampling mistakes easier to see than a value containing mostly zeros.

Try it yourself

Build a deterministic SPI smoke test that transfers 8'hA5. Verify MOSI, MISO, CS, and SCLK behavior with the monitor and scoreboard.

Then expand the project:

  1. Run the transfer in each SPI mode supported by the DUT.
  2. Test 8'h00, 8'hFF, 8'h55, and 8'hAA.
  3. Exercise the supported bit ordering and transfer lengths.
  4. Add at least one CS-boundary test.
  5. Add assertions for CS and SCLK behavior.
  6. Review coverage and identify one untested combination.
  7. Introduce a controlled protocol error and verify the DUT's documented response.

For every scenario, record the expected transaction, observed transaction, configuration, pass/fail result, assertion status, coverage result, and—when randomized—the test name and seed.

Key takeaways

  • Model SPI transfers as transactions and keep signal timing inside the driver and monitor.
  • CPOL and CPHA must be modeled explicitly because they determine SPI clocking behavior.
  • Verify MOSI and MISO independently because SPI commonly operates full-duplex.
  • Treat CS as a protocol boundary and verify its relationship with SCLK.
  • Use an independent monitor so the testbench checks what the DUT actually did.
  • Use scoreboards for transaction-level checking and assertions for signal-level rules.
  • Use functional coverage to demonstrate that important SPI modes, data patterns, and configurations were exercised.
  • Preserve seeds and transaction context so randomized failures can be reproduced and minimized.