Lesson 14: UVM Register Layer (RAL) Basics

The UVM register layer, often called RAL, is a model of a design's registers inside the testbench: one object per register, each with fields that know their width, position, access policy and reset value. A test calls write() and read() on the model by register name, and the model turns each call into a bus transfer through an adapter and a bus agent, so the same test works whatever the bus is. The model also keeps a mirror, its best knowledge of each register's value, and can check the hardware against it.

Learning objectives

  • Model registers and fields with uvm_reg, uvm_reg_field and uvm_reg_block, from a register map
  • Connect the model to a bus agent with a map and an adapter, and read and write registers by name
  • Tell the desired, mirrored and hardware values apart, and use update(), mirror() and predict
  • Run the built-in reset check and write a register test that catches a status bug

Prerequisites

  • Lesson 11: agents, sequencers, drivers and the FIFO environment
  • Lesson 13: the factory and uvm_config_db, used to build and wire the model
  • The running example is the synchronous FIFO: 16 entries of 8 bits, one clock, flags full and empty and an occupancy count; writes when full are dropped, reads when empty are ignored, and read data appears one clock after the read. Its shared testbench code is in the site's repository under scripts/verification/fifo, and every example runs in AMD Vivado xsim 2025.2.

The register block

The FIFO's data path is unchanged; a small block of four 8-bit registers sits beside it on a simple register bus. Reads are combinational: rdata is valid while req is high and we is low. Writes take effect at the rising edge.

fifo/fifo_regs.v: the register map
//   0x0 CTRL    RW  bit 0 EN (writes into the FIFO are ignored while 0), bit 1 FLUSH (write 1 to empty the FIFO;
//                   reads back 0)                                                            reset 0x01
//   0x1 STATUS  RO  bit 0 EMPTY, bit 1 FULL, bit 2 ALMOST_FULL (count >= THRESH)             reset 0x01
//   0x2 LEVEL   RO  bits 4:0 the number of entries                                           reset 0x00
//   0x3 THRESH  RW  bits 4:0 the ALMOST_FULL threshold                                       reset 0x0E

Registers, fields and the block

Each register class creates its fields in build() and configures each one: its size, its lowest bit, its access policy (RW, RO, WO and others), whether the hardware changes it on its own (volatile), and its reset value. The block creates the registers, places them in a map at their addresses, and locks the model.

fifo/fifo_ral.svh: THRESH, one register
class fifo_thresh_reg extends uvm_reg;
  `uvm_object_utils(fifo_thresh_reg)
  rand uvm_reg_field value;
  function new(string name = "thresh"); super.new(name, 8, UVM_NO_COVERAGE); endfunction
  virtual function void build();
    value = uvm_reg_field::type_id::create("value");
    value.configure(this, 5, 0, "RW", 0, 5'd14, 1, 1, 0);
  endfunction
endclass
fifo/fifo_ral.svh: the block and its map
// The block: the four registers at their addresses in one map (byte addresses, one byte per register).
class fifo_reg_block extends uvm_reg_block;
  `uvm_object_utils(fifo_reg_block)
  rand fifo_ctrl_reg ctrl;
  fifo_status_reg status;
  fifo_level_reg level;
  rand fifo_thresh_reg thresh;
  function new(string name = "fifo_regs"); super.new(name, UVM_NO_COVERAGE); endfunction
  virtual function void build();
    default_map = create_map("map", 0, 1, UVM_LITTLE_ENDIAN);
    ctrl = fifo_ctrl_reg::type_id::create("ctrl");       ctrl.configure(this);   ctrl.build();
    status = fifo_status_reg::type_id::create("status"); status.configure(this); status.build();
    level = fifo_level_reg::type_id::create("level");    level.configure(this);  level.build();
    thresh = fifo_thresh_reg::type_id::create("thresh"); thresh.configure(this); thresh.build();
    default_map.add_reg(ctrl, 'h0, "RW");
    default_map.add_reg(status, 'h1, "RO");
    default_map.add_reg(level, 'h2, "RO");
    default_map.add_reg(thresh, 'h3, "RW");
    lock_model();
  endfunction
endclass

The adapter and the bus agent

The model speaks in generic bus operations: read or write, address and data. The adapter's reg2bus turns one into the bus agent's item, and bus2reg turns a completed item back. The map is told which sequencer and adapter to use, and with auto-predict on, the model updates its mirror from every read and write it makes itself.

fifo/fifo_ral.svh: the adapter
// The adapter turns the register layer's generic read or write into a bus item and back.
class reg_bus_adapter extends uvm_reg_adapter;
  `uvm_object_utils(reg_bus_adapter)
  function new(string name = "reg_bus_adapter"); super.new(name); endfunction
  virtual function uvm_sequence_item reg2bus(const ref uvm_reg_bus_op rw);
    reg_bus_item it = reg_bus_item::type_id::create("it");
    it.we = (rw.kind == UVM_WRITE);
    it.addr = rw.addr[1:0];
    it.data = rw.data[7:0];
    return it;
  endfunction
  virtual function void bus2reg(uvm_sequence_item bus_item, ref uvm_reg_bus_op rw);
    reg_bus_item it;
    if (!$cast(it, bus_item)) `uvm_fatal("ADAPT", "not a reg_bus_item")
    rw.kind = it.we ? UVM_WRITE : UVM_READ;
    rw.addr = it.addr;
    rw.data = it.data;
    rw.status = UVM_IS_OK;
  endfunction
endclass
fifo/fifo_ral.svh: the environment
// The environment: the bus agent's sequencer and driver, the register model, and the map tied to the sequencer.
class fifo_ral_env extends uvm_env;
  `uvm_component_utils(fifo_ral_env)
  uvm_sequencer #(reg_bus_item) sqr;
  reg_bus_driver drv;
  fifo_reg_block regs;
  reg_bus_adapter adapter;
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  function void build_phase(uvm_phase phase);
    sqr = uvm_sequencer#(reg_bus_item)::type_id::create("sqr", this);
    drv = reg_bus_driver::type_id::create("drv", this);
    regs = fifo_reg_block::type_id::create("regs");
    regs.build();
    adapter = reg_bus_adapter::type_id::create("adapter");
  endfunction
  function void connect_phase(uvm_phase phase);
    drv.seq_item_port.connect(sqr.seq_item_export);
    regs.default_map.set_sequencer(sqr, adapter);
    regs.default_map.set_auto_predict(1);       // update the mirror from every read and write the model makes
  endfunction
endclass
UVM register model, adapter, bus sequencer and driver reaching the FIFO's register block
The path from a test to the hardware. A write() on the model becomes a bus operation in the map, a bus item in the adapter, and pin activity from the bus driver on reg_if.
Waveform of a register bus write of 12 to THRESH and a read that returns 12
One write and one read on the register bus, from the run. At 115 ns THRESH (address 3) is written with 12, and rdata changes from 14 to 12; at 135 ns a read of address 3 returns 12.

Desired, mirrored and actual values

Each field has three values. The desired value is what the test wants; set() changes only that. The mirrored value is what the model believes the hardware holds; reads, writes and predict() update it. The actual value is in the design. update() writes any register whose desired value differs from its mirror, and mirror() reads the hardware, and with UVM_CHECK reports an error if it differs from the mirror. Volatile fields, such as the status bits, are not compared, because the hardware changes them.

Worked example: the register test

The test runs UVM's built-in uvm_reg_hw_reset_seq, which reads every register and compares it with its reset value in the model. Then it writes THRESH, fills the FIFO to the threshold through its data pins and reads LEVEL and STATUS, and finally turns writes off with CTRL.EN.

fifo_ral_tb.sv: the test
  task run_phase(uvm_phase phase);
    uvm_status_e st;
    uvm_reg_data_t v;
    uvm_reg_hw_reset_seq reset_seq = uvm_reg_hw_reset_seq::type_id::create("reset_seq");
    phase.raise_objection(this);
    wait (fifo_vif.rst === 1'b0);
    // 1. The built-in sequence reads every register and compares it with its reset value in the model.
    reset_seq.model = env.regs;
    reset_seq.start(null);
    `uvm_info("RAL", "hardware reset values checked", UVM_NONE)
    // 2. Write THRESH through the model, read it back, and look at the model's mirror.
    env.regs.thresh.write(st, 12);
    env.regs.thresh.read(st, v);
    `uvm_info("RAL", $sformatf("THRESH written 12, read back %0d, mirror %0d", v, env.regs.thresh.get_mirrored_value()), UVM_NONE)
    // 3. Fill the FIFO to the threshold through the data pins, then read LEVEL and STATUS.
    push(12);
    env.regs.level.read(st, v);
    `uvm_info("RAL", $sformatf("after 12 writes LEVEL reads %0d", v), UVM_NONE)
    env.regs.status.read(st, v);
    `uvm_info("RAL", $sformatf("STATUS reads 0x%0h: empty %0d, full %0d, almost_full %0d", v, v[0], v[1], v[2]), UVM_NONE)
    // 4. Turn writes off with CTRL.EN, push again, and check that LEVEL did not move.
    env.regs.ctrl.en.set(0);
    env.regs.ctrl.update(st);
    push(3);
    env.regs.level.read(st, v);
    `uvm_info("RAL", $sformatf("EN = 0, 3 more writes: LEVEL reads %0d", v), UVM_NONE)
    phase.drop_objection(this);
  endtask
xsim output: the real register block
UVM_INFO fifo_ral_tb.sv(37) @ 100000: uvm_test_top [RAL] hardware reset values checked
UVM_INFO fifo_ral_tb.sv(41) @ 140000: uvm_test_top [RAL] THRESH written 12, read back 12, mirror 12
UVM_INFO fifo_ral_tb.sv(45) @ 290000: uvm_test_top [RAL] after 12 writes LEVEL reads 12
UVM_INFO fifo_ral_tb.sv(47) @ 310000: uvm_test_top [RAL] STATUS reads 0x4: empty 0, full 0, almost_full 1
UVM_INFO fifo_ral_tb.sv(53) @ 390000: uvm_test_top [RAL] EN = 0, 3 more writes: LEVEL reads 12
UVM_ERROR :    0
[RAL]     5

Planted register bug 1 makes THRESH reset to 15 instead of 14. The reset sequence finds it on its first pass, before any test traffic, and names the register and both values:

xsim output: built with -d REG_BUG=1
UVM_ERROR uvm_pkg @ 80000: reporter [RegModel] Register "regs.thresh" value read from DUT (0x000000000000000f) does not match mirrored value (0x000000000000000e)
UVM_ERROR :    1

Common mistakes

  • Forgetting to call the block's reset(), so the mirror starts at 0 instead of the reset values and every later check compares against the wrong value.
  • Writing a register directly on the bus, round the model: its mirror goes stale, and the next mirror(UVM_CHECK) reports a mismatch (E3). Use the model, or a monitor with an explicit predictor.
  • Marking a field that the hardware changes as non-volatile, so mirror checks fail as soon as the design moves it.
  • Calling set() and expecting the hardware to change: set() only changes the desired value until update() or write().
  • Getting reg2bus and bus2reg out of step, for example not copying read data back, so every read returns 0.

Quick check

1. What is the difference between the desired and the mirrored value?

The desired value is what the test wants the register to hold; the mirrored value is what the model believes the hardware holds now. update() writes the register when they differ.

2. What does uvm_reg_hw_reset_seq check?

It reads every register through the bus and compares the value with the reset value in the model, which found THRESH resetting to 15 instead of 14.

3. Why are STATUS and LEVEL volatile?

The FIFO changes them on its own as entries are written and read, so a mirror check against a value the model wrote or predicted would be meaningless.

Exercises

Graded from easy to hard. Try each one before you open the solution; every solution was run in xsim 2025.2, and its output is shown.

E1 (easy). Read every register once after reset and print its address and value.

get_registers lists the block's registers; read() on each returns the hardware value.

fifo_ral_ex_tb.sv: E1
// E1: read every register once after reset.
class fifo_e1_rtest extends fifo_ral_base_test;
  `uvm_component_utils(fifo_e1_rtest)
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  task body();
    uvm_reg regs[$];
    env.regs.get_registers(regs);
    foreach (regs[i]) begin
      regs[i].read(st, v);
      `uvm_info("E1", $sformatf("%s at 0x%0h reads 0x%02h", regs[i].get_name(), regs[i].get_address(), v), UVM_NONE)
    end
  endtask
endclass
xsim output
UVM_INFO fifo_ral_ex_tb.sv(47) @ 40000: uvm_test_top [E1] ctrl at 0x0 reads 0x01
UVM_INFO fifo_ral_ex_tb.sv(47) @ 60000: uvm_test_top [E1] level at 0x2 reads 0x00
UVM_INFO fifo_ral_ex_tb.sv(47) @ 80000: uvm_test_top [E1] thresh at 0x3 reads 0x0e
UVM_INFO fifo_ral_ex_tb.sv(47) @ 100000: uvm_test_top [E1] status at 0x1 reads 0x01
E2 (easy). Change THRESH with set() and update(), and print the desired and mirrored values after each.

set(9) changes only the desired value; update() writes the register, and the mirror follows.

fifo_ral_ex_tb.sv: E2
// E2: set() changes only the desired value; update() writes it, and then the mirror follows.
class fifo_e2_rtest extends fifo_ral_base_test;
  `uvm_component_utils(fifo_e2_rtest)
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  task body();
    env.regs.thresh.set(9);
    `uvm_info("E2", $sformatf("after set(9): desired %0d, mirrored %0d", env.regs.thresh.get(), env.regs.thresh.get_mirrored_value()), UVM_NONE)
    env.regs.thresh.update(st);
    `uvm_info("E2", $sformatf("after update(): desired %0d, mirrored %0d", env.regs.thresh.get(), env.regs.thresh.get_mirrored_value()), UVM_NONE)
  endtask
endclass
xsim output
UVM_INFO fifo_ral_ex_tb.sv(58) @ 20000: uvm_test_top [E2] after set(9): desired 9, mirrored 14
UVM_INFO fifo_ral_ex_tb.sv(60) @ 40000: uvm_test_top [E2] after update(): desired 9, mirrored 9
E3 (medium). Write THRESH directly on the bus, round the model, and show that mirror(UVM_CHECK) finds it.

The model never saw the write, so its mirror still says 14. mirror() reads 7, reports the mismatch and updates the mirror.

fifo_ral_ex_tb.sv: E3
// E3's raw bus write: a sequence that writes one register directly, without the register model.
class reg_raw_write_seq extends uvm_sequence #(reg_bus_item);
  `uvm_object_utils(reg_raw_write_seq)
  bit [1:0] addr;
  bit [7:0] data;
  function new(string name = "reg_raw_write_seq"); super.new(name); endfunction
  task body();
    reg_bus_item it = reg_bus_item::type_id::create("it");
    start_item(it);
    it.we = 1; it.addr = addr; it.data = data;
    finish_item(it);
  endtask
endclass

// E3: a write that goes round the register model leaves its mirror stale; mirror(UVM_CHECK) finds it.
class fifo_e3_rtest extends fifo_ral_base_test;
  `uvm_component_utils(fifo_e3_rtest)
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  task body();
    reg_raw_write_seq raw = reg_raw_write_seq::type_id::create("raw");
    raw.addr = 3; raw.data = 7;                                   // THRESH = 7, written straight on the bus
    raw.start(env.sqr);
    `uvm_info("E3", $sformatf("bus write of 7 to THRESH; the model's mirror still says %0d", env.regs.thresh.get_mirrored_value()), UVM_NONE)
    env.regs.thresh.mirror(st, UVM_CHECK);
    `uvm_info("E3", $sformatf("after mirror(): the mirror says %0d", env.regs.thresh.get_mirrored_value()), UVM_NONE)
  endtask
endclass
xsim output
UVM_INFO fifo_ral_ex_tb.sv(86) @ 40000: uvm_test_top [E3] bus write of 7 to THRESH; the model's mirror still says 14
UVM_ERROR uvm_pkg @ 60000: reporter [RegModel] Register "regs.thresh" value read from DUT (0x0000000000000007) does not match mirrored value (0x000000000000000e)
UVM_INFO fifo_ral_ex_tb.sv(88) @ 60000: uvm_test_top [E3] after mirror(): the mirror says 7
E4 (hard). Check ALMOST_FULL against THRESH one entry below and at the threshold, and catch planted register bug 2.

Write THRESH, flush, push one entry fewer than the threshold and expect ALMOST_FULL low, then one more and expect it high. Planted bug 2 compares with > instead of >=, so the flag is still low at the threshold.

fifo_ral_ex_tb.sv: E4
// E4: check ALMOST_FULL against THRESH at two thresholds, one entry below and at the threshold.
class fifo_e4_rtest extends fifo_ral_base_test;
  `uvm_component_utils(fifo_e4_rtest)
  function new(string name, uvm_component parent); super.new(name, parent); endfunction
  task expect_af(bit want, int level);
    env.regs.status.read(st, v);
    if (v[2] !== want) `uvm_error("E4", $sformatf("THRESH %0d, LEVEL %0d: ALMOST_FULL is %0d, expected %0d", env.regs.thresh.get_mirrored_value(), level, v[2], want))
    else `uvm_info("E4", $sformatf("THRESH %0d, LEVEL %0d: ALMOST_FULL %0d as expected", env.regs.thresh.get_mirrored_value(), level, v[2]), UVM_NONE)
  endtask
  task body();
    int ts[2] = '{4, 10};
    foreach (ts[k]) begin
      env.regs.thresh.write(st, ts[k]);
      env.regs.ctrl.write(st, 'b11);                            // FLUSH, keep EN
      push(ts[k] - 1);
      expect_af(0, ts[k] - 1);
      push(1);
      expect_af(1, ts[k]);
    end
  endtask
endclass
xsim output: the real block, then built with -d REG_BUG=2
UVM_ERROR :    0
UVM_ERROR :    0
UVM_ERROR :    1
UVM_INFO fifo_ral_ex_tb.sv(99) @ 120000: uvm_test_top [E4] THRESH 4, LEVEL 3: ALMOST_FULL 0 as expected
UVM_INFO fifo_ral_ex_tb.sv(99) @ 160000: uvm_test_top [E4] THRESH 4, LEVEL 4: ALMOST_FULL 1 as expected
UVM_INFO fifo_ral_ex_tb.sv(99) @ 320000: uvm_test_top [E4] THRESH 10, LEVEL 9: ALMOST_FULL 0 as expected
UVM_INFO fifo_ral_ex_tb.sv(99) @ 360000: uvm_test_top [E4] THRESH 10, LEVEL 10: ALMOST_FULL 1 as expected
UVM_ERROR :    0
UVM_INFO fifo_ral_ex_tb.sv(99) @ 120000: uvm_test_top [E4] THRESH 4, LEVEL 3: ALMOST_FULL 0 as expected
UVM_ERROR fifo_ral_ex_tb.sv(98) @ 160000: uvm_test_top [E4] THRESH 4, LEVEL 4: ALMOST_FULL is 0, expected 1
UVM_INFO fifo_ral_ex_tb.sv(99) @ 320000: uvm_test_top [E4] THRESH 10, LEVEL 9: ALMOST_FULL 0 as expected
UVM_ERROR fifo_ral_ex_tb.sv(98) @ 360000: uvm_test_top [E4] THRESH 10, LEVEL 10: ALMOST_FULL is 0, expected 1
UVM_ERROR :    2

Interview questions

  • Q1. Why use a register model instead of writing bus transfers by hand? Answer: tests name registers and fields instead of addresses and bit masks, the same test runs over any bus through a different adapter, and the model's mirror and built-in sequences check reset values and accesses for free.
  • Q2. What is the mirror, and how does it stay correct? Answer: the model's copy of what the hardware holds. It is updated from the model's own reads and writes (auto-predict here) or, in a fuller testbench, from a bus monitor through a predictor, which also catches writes that did not go through the model.
  • Q3. A reset value is wrong in the RTL. How do you catch it? Answer: run uvm_reg_hw_reset_seq right after reset. It reads every register and compares it with the reset value in the model, which is written from the register specification.

Summary

  • The register layer models each register and field with its width, access policy and reset value.
  • A map and an adapter turn read() and write() by name into transfers on any bus agent.
  • Desired, mirrored and actual values differ; set, update, mirror and predict move between them.
  • The FIFO's register block had two planted bugs: the reset sequence found one, and a directed register test found the other.

Related

Sources

Last verified

Run in Vivado xsim 2025.2 with its bundled UVM 1.2 on 27 November 2026: the register test on the real block and on both planted register bugs, and all four exercise tests. Outputs are copied from those runs and kept in scripts/verification/results; lib/verificationCourse.test.js checks them against this page.

Keep going