UVM RAL complete guide cover: the register model, methods and tests on an APB example, The Art of Verification

UVM RAL: A Complete Guide With a Working APB Example

In short: UVM RAL (Register Abstraction Layer) is a model of your design’s registers that lives inside the testbench. Your test reads and writes registers by name, the model turns each call into real bus transfers through an adapter, and it keeps its own copy of what every register should hold, so it can check the design for you.

In this guide I build a complete RAL setup for a small APB register block and run every main method on Synopsys VCS: write, read, set, update, mirror, predict, peek, poke, reset, memories and callbacks. Then I plant four bugs. The built-in UVM register tests caught two of them. Two short targeted tests caught the other two.

Why do we need a register model at all?

A chip is configured through registers. Without a model:

  • Every test hard-codes addresses and bit positions: write 0x95 to 0x00, read 0x08, mask bit 2.
  • When a register moves or a field widens, every one of those tests breaks quietly.
  • Every check needs its expected value typed in again.

With RAL:

  • Names, not numbers. Tests say regmodel.ip.ctrl.prescale. Change the model once and every test follows.
  • Automatic checking. The model remembers what each register should hold, so a read can be checked for you.
  • Reuse. The same model works at block, subsystem and chip level. Only the address map and the bus change.

The picture: a bank passbook

Think of the register model as a bank passbook:

  • The bank’s ledger is the design. It holds the real balance.
  • Your passbook is the model. It is your copy.
  • write() is a deposit at the counter. The passbook gets the new line.
  • mirror() is printing the passbook at the machine and checking each line against the ledger.
  • predict() is writing a line in the passbook by hand, without visiting the bank.
  • A backdoor peek() is a bank employee reading the ledger directly, with no queue.
  • Interest the bank adds on its own leaves your passbook out of date until you print it again. That is exactly what a volatile register does, and it causes more RAL bugs than anything else.

What you will learn

  • The RAL layers: field, register, register file, block, map and sub-blocks, and every argument of configure().
  • All the UVM access policies, and what each one does to the model.
  • The three values RAL tracks (desired, mirrored and the real design value) and which method changes which one.
  • Every main method with real VCS output: write, read, field write, set, get, update, randomize, mirror, predict, peek, poke, reset.
  • Frontdoor vs backdoor access: how each works, when to use which, and what a backdoor skips.
  • The adapter, the three prediction modes, and how to wire RAL into an environment.
  • Memories, callbacks, user-defined frontdoors, coverage and model generation.
  • What the built-in register sequences catch, and the bugs they cannot see.

The register block in this guide

The design is a small APB slave with five registers and a 16-word memory. It is small on purpose: every RAL feature appears once, and you can hold the whole map in your head.

AddressNameFieldsAccessReset
0x00CTRLen [0], mode [2:1], prescale [7:4]. Bit 3 and [31:8] unusedRW0x40
0x04IDid [15:0]RO0x5A01
0x08INT_STATpending [3:0], set by hardware eventsW1C0x0
0x0CINT_ENenable [3:0]RW0x0
0x1CSCRATCHdata [31:0]RW0x0
0x40 to 0x7CBUF16 words of 32 bitsRW memorynone

Any other address reads 0 and answers with PSLVERR. Four bugs can be switched on, one per run:

OptionBug
+BUG=1CTRL resets to 0x00 instead of 0x40
+BUG=2INT_STAT acts as RW: a write stores the value instead of clearing bits
+BUG=3ID is writable
+BUG=4INT_EN decode ignores address bits [7:4], so a write to 0x1C also writes INT_EN

What are the layers of a UVM register model?

Inside a UVM register model: fields in the CTRL, ID, INT_STAT, INT_EN and SCRATCH registers drawn bit by bit, the BUF memory, all behind the address map; below it the predictor, the adapter and the APB agent (sequencer, driver, monitor) reaching the DUT.
Inside the register model: fields in registers, registers and memory behind the address map, then predictor, adapter and APB agent.

A register model is built from small pieces that nest inside each other, the same way the registers sit inside the design.

The RAL layers: fields inside registers, registers and memories inside a block, the map giving each one an address and a bus.
The layers as built in this guide: chip block, two IP blocks, register files, registers, memory.

Every layer below is real code from the bench, and every one of them runs on VCS.

1. Field (uvm_reg_field)

  • One or more bits that mean one thing: an enable bit, a 2-bit mode, a 4-bit prescaler.
  • The access policy lives here, not on the register.
  • You never extend it. You create it and call configure() once.
// INT_STAT has one field: 4 bits, W1C, set by hardware
pending = uvm_reg_field::type_id::create("pending");
// volatile = 1: hardware can set these bits at any time, so the mirror cannot predict them
pending.configure(this, 4, 0, "W1C", 1, 4'h0, 1, 0, 0);

Every field in our APB example, and the line that created it. The arguments are: parent, size, lsb, access, volatile, reset, has_reset, is_rand, individually_accessible.

RegisterFieldBitsAccessResetVolatileconfigure() call
CTRL 0x00en[0]RW0x0noen.configure(this, 1, 0, "RW", 0, 1'h0, 1, 1, 0)
CTRL 0x00mode[2:1]RW0x0nomode.configure(this, 2, 1, "RW", 0, 2'h0, 1, 1, 0)
CTRL 0x00prescale[7:4]RW0x4noprescale.configure(this, 4, 4, "RW", 0, 4'h4, 1, 1, 0)
ID 0x04id[15:0]RO0x5A01noid.configure(this, 16, 0, "RO", 0, 16'h5A01, 1, 0, 0)
INT_STAT 0x08pending[3:0]W1C0x0yespending.configure(this, 4, 0, "W1C", 1, 4'h0, 1, 0, 0)
INT_EN 0x0Cenable[3:0]RW0x0noenable.configure(this, 4, 0, "RW", 0, 4'h0, 1, 1, 0)
SCRATCH 0x1Cdata[31:0]RW0x0nodata.configure(this, 32, 0, "RW", 0, 32'h0, 1, 1, 0)
DMA chN.SRC 0x80 / 0x90data[31:0]RW0x0nodata.configure(this, 32, 0, "RW", 0, 32'h0, 1, 1, 0)
DMA chN.LEN 0x84 / 0x94len[15:0]RW0x0040nolen.configure(this, 16, 0, "RW", 0, 16'h0040, 1, 1, 0)
  • Bits that no field covers (CTRL bit 3 and [31:8], LEN [31:16]) are not in the model at all. The model expects them to read 0.
  • A register’s reset value is built from its fields: prescale reset 4 at lsb 4 gives CTRL reset 0x40.
  • Tour step 0 prints this same list back from the running model (section 8 below).
Bit layout of CTRL and DMA LEN: which bits each uvm_reg_field covers, its access and reset, and the configure() lines.
Where each field sits in CTRL and DMA LEN, and the configure() calls that put it there.

2. Register (uvm_reg)

  • A group of fields at one address.
  • Extend uvm_reg, declare the fields, create and configure them in build().
  • super.new() sets the width (32 here) and the coverage model.
class tav_ctrl_reg extends uvm_reg;
  `uvm_object_utils(tav_ctrl_reg)
  rand uvm_reg_field en, mode, prescale;
  function new(string name = "tav_ctrl_reg");
    super.new(name, 32, UVM_NO_COVERAGE);              // 32 bits wide, no coverage model
  endfunction
  virtual function void build();
    en       = uvm_reg_field::type_id::create("en");
    mode     = uvm_reg_field::type_id::create("mode");
    prescale = uvm_reg_field::type_id::create("prescale");
    //                  parent size lsb access volatile reset has_reset is_rand indiv
    en.configure      (this, 1,   0,  "RW",  0,       1'h0, 1,        1,      0);
    mode.configure    (this, 2,   1,  "RW",  0,       2'h0, 1,        1,      0);
    prescale.configure(this, 4,   4,  "RW",  0,       4'h4, 1,        1,      0);
  endfunction
endclass

3. Register file (uvm_reg_file)

  • A named group of registers that repeats, such as one set per DMA channel.
  • It adds one level to the name (regmodel.dma.ch1.len) and to the HDL path (tav_top.dut.u_ch1.len).
  • It has no address of its own. You place its registers in a map at an offset.
class tav_dma_chan_rf extends uvm_reg_file;
  `uvm_object_utils(tav_dma_chan_rf)
  rand tav_dma_src_reg src;
  rand tav_dma_len_reg len;
  function new(string name = "tav_dma_chan_rf");
    super.new(name);
  endfunction
  virtual function void build();
    src = tav_dma_src_reg::type_id::create("src");
    len = tav_dma_len_reg::type_id::create("len");
    src.configure(get_block(), this);  src.build();  src.add_hdl_path_slice("src", 0, 32);
    len.configure(get_block(), this);  len.build();  len.add_hdl_path_slice("len", 0, 16);
  endfunction
  // place both registers in a map, starting at this register file's offset
  function void tav_map(uvm_reg_map mp, uvm_reg_addr_t offset);
    mp.add_reg(src, offset + 'h0, "RW");
    mp.add_reg(len, offset + 'h4, "RW");
  endfunction
endclass

4. Memory (uvm_mem)

  • A range of words with one access policy and no fields, like a buffer or RAM.
  • Accessed by offset. RAL keeps no mirror of it.
class tav_buf_mem extends uvm_mem;
  `uvm_object_utils(tav_buf_mem)
  function new(string name = "tav_buf_mem");
    super.new(name, 16, 32, "RW", UVM_NO_COVERAGE);   // 16 words of 32 bits
  endfunction
endclass

5. Block (uvm_reg_block) and map (uvm_reg_map)

  • A block holds registers, register files, memories and other blocks. One block per IP is typical.
  • A map gives each register and memory an address, sets the bus width and byte order, and is what you connect to a sequencer.
  • lock_model() closes the block. After it, nothing can be added.
virtual function void build();     // tav_reg_block, the first IP
  // the map: base address 0, 4-byte bus, little endian
  default_map = create_map("default_map", 'h0, 4, UVM_LITTLE_ENDIAN);
  // where the design lives, for peek and poke
  add_hdl_path("tav_top.dut", "RTL");
  ...
  ctrl.configure(this);     ctrl.build();     ctrl.add_hdl_path_slice("ctrl", 0, 8);
  ...
  buffer.configure(this);   buffer.add_hdl_path_slice("mem", 0, 32);    // backdoor: mem[offset]

  default_map.add_reg(ctrl,     'h00, "RW");
  default_map.add_reg(id,       'h04, "RW");
  default_map.add_reg(int_stat, 'h08, "RW");
  default_map.add_reg(int_en,   'h0C, "RW");
  default_map.add_reg(scratch,  'h1C, "RW");
  default_map.add_mem(buffer,   'h40, "RW");          // words at 0x40, 0x44, ... 0x7C
  lock_model();                                      // no more changes; addresses are now fixed
endfunction

6. Two maps in one block

  • A block can have one map per bus that reaches it.
  • Here the DMA block has a CPU map and a debug map over the same four registers.
  • The first map you create becomes default_map.
class tav_dma_block extends uvm_reg_block;
  `uvm_object_utils(tav_dma_block)
  rand tav_dma_chan_rf ch0, ch1;
  uvm_reg_map          cpu_map, dbg_map;
  function new(string name = "tav_dma_block");
    super.new(name, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    cpu_map = create_map("cpu_map", 'h0, 4, UVM_LITTLE_ENDIAN);   // the first map is the default_map
    dbg_map = create_map("dbg_map", 'h0, 4, UVM_LITTLE_ENDIAN);
    add_hdl_path("tav_top.dut", "RTL");
    ch0 = tav_dma_chan_rf::type_id::create("ch0");
    ch1 = tav_dma_chan_rf::type_id::create("ch1");
    ch0.configure(this, null, "u_ch0");  ch0.build();
    ch1.configure(this, null, "u_ch1");  ch1.build();
    ch0.tav_map(cpu_map, 'h00);  ch1.tav_map(cpu_map, 'h10);
    ch0.tav_map(dbg_map, 'h00);  ch1.tav_map(dbg_map, 'h10);
  endfunction
endclass

7. Sub-blocks and sub-maps: the chip-level block

  • At chip level you do not write a new model. You put the IP blocks inside a top block.
  • Each IP map is added to the top map at the IP’s base address with add_submap().
  • Only the top block calls set_sequencer() and becomes the model your test uses.
class tav_chip_block extends uvm_reg_block;
  `uvm_object_utils(tav_chip_block)
  rand tav_reg_block ip;      // CTRL, ID, INT_STAT, INT_EN, SCRATCH, BUF at base 0x00
  rand tav_dma_block dma;     // two DMA channels at base 0x80
  uvm_reg_map        dbg_map;
  function new(string name = "tav_chip_block");
    super.new(name, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    default_map = create_map("default_map", 'h0, 4, UVM_LITTLE_ENDIAN);
    dbg_map     = create_map("dbg_map",     'h0, 4, UVM_LITTLE_ENDIAN);
    set_hdl_path_root("", "RTL");            // each IP block carries its own full HDL path
    ip  = tav_reg_block::type_id::create("ip");
    dma = tav_dma_block::type_id::create("dma");
    ip.configure(this);   ip.build();
    dma.configure(this);  dma.build();
    default_map.add_submap(ip.default_map, 'h00);
    default_map.add_submap(dma.cpu_map,    'h80);
    dbg_map.add_submap(dma.dbg_map,        'h80);
    lock_model();
  endfunction
endclass

8. What the finished map looks like: tour step 0

  • A map is not a class you write. It is the result of a few calls: create_map(), add_reg(), add_mem() and add_submap().
  • Here are all of them in this bench, from the three blocks, in one place:
// ip block (tav_reg_block)
default_map = create_map("default_map", 'h0, 4, UVM_LITTLE_ENDIAN);  // base 0, 4-byte bus
default_map.add_reg(ctrl,     'h00, "RW");
default_map.add_reg(id,       'h04, "RW");
default_map.add_reg(int_stat, 'h08, "RW");
default_map.add_reg(int_en,   'h0C, "RW");
default_map.add_reg(scratch,  'h1C, "RW");
default_map.add_mem(buffer,   'h40, "RW");

// dma block (tav_dma_block): tav_map() calls mp.add_reg(src, offset + 'h0) and mp.add_reg(len, offset + 'h4)
cpu_map = create_map("cpu_map", 'h0, 4, UVM_LITTLE_ENDIAN);
dbg_map = create_map("dbg_map", 'h0, 4, UVM_LITTLE_ENDIAN);
ch0.tav_map(cpu_map, 'h00);  ch1.tav_map(cpu_map, 'h10);
ch0.tav_map(dbg_map, 'h00);  ch1.tav_map(dbg_map, 'h10);

// chip block (tav_chip_block)
default_map = create_map("default_map", 'h0, 4, UVM_LITTLE_ENDIAN);
dbg_map     = create_map("dbg_map",     'h0, 4, UVM_LITTLE_ENDIAN);
default_map.add_submap(ip.default_map, 'h00);
default_map.add_submap(dma.cpu_map,    'h80);
dbg_map.add_submap(dma.dbg_map,        'h80);
  • Tour step 0 asks the running model what those calls built. It walks the chip default_map and prints each register, its address and its fields:
chip.default_map.get_registers(regs);
foreach (regs[i]) begin
  regs[i].get_fields(flds);
  // print regs[i].get_address(chip.default_map), regs[i].get_full_name(),
  // and for each field: get_name(), bits, get_access(), get_reset(), is_volatile()
end
chip.default_map.get_memories(mems);
@   45 [TOUR]    0x00  regmodel.ip.ctrl       | en [0:0] RW reset 0x0 | mode [2:1] RW reset 0x0 | prescale [7:4] RW reset 0x4
@   45 [TOUR]    0x04  regmodel.ip.id         | id [15:0] RO reset 0x5a01
@   45 [TOUR]    0x08  regmodel.ip.int_stat   | pending [3:0] W1C reset 0x0 volatile
@   45 [TOUR]    0x0c  regmodel.ip.int_en     | enable [3:0] RW reset 0x0
@   45 [TOUR]    0x1c  regmodel.ip.scratch    | data [31:0] RW reset 0x0
@   45 [TOUR]    0x80  regmodel.dma.ch0.src   | data [31:0] RW reset 0x0
@   45 [TOUR]    0x90  regmodel.dma.ch1.src   | data [31:0] RW reset 0x0
@   45 [TOUR]    0x84  regmodel.dma.ch0.len   | len [15:0] RW reset 0x40
@   45 [TOUR]    0x94  regmodel.dma.ch1.len   | len [15:0] RW reset 0x40
@   45 [TOUR]    0x40  regmodel.ip.buffer     | memory, 16 words of 32 bits, RW
@   45 [TOUR]    default_map: 4-byte bus, base 0x0; dbg_map holds 4 registers

How to read it:

  • Address = sub-map base + register offset. dma.ch1.len is at 0x94: 0x80 (add_submap of dma.cpu_map) + 0x10 (ch1) + 0x4 (len).
  • The full name shows the layers. regmodel.dma.ch1.len is chip block, IP block, register file, register.
  • Fields carry the policy. ID is the only RO field; INT_STAT is the only W1C and the only volatile one.
  • The order is the model’s, not the address order. 0x90 prints before 0x84. Sort by get_address() if you print a register map for people.
  • dbg_map holds 4 registers: only the four DMA registers. The IP block has no debug map, so it is not reachable through dbg_map.
  • Nothing went on the bus. All of this comes from the model, at 45 ns, before the first APB transfer.

What does each argument of uvm_reg_field::configure() mean?

This one line decides how the model treats a field for the rest of the simulation, so it is worth knowing every argument.

ArgumentIn CTRL.prescaleWhat it does
parentthisThe register the field belongs to
size4Width in bits
lsb_pos4Position of the lowest bit inside the register
access“RW”The access policy (next section). Decides how writes and reads change the model
volatile01 means hardware can change the field on its own. UVM then switches off the compare for that field
reset4’h4The value after a hard reset. regmodel.reset() loads it
has_reset10 means the field has no defined reset value, and the reset test skips it
is_rand11 lets randomize() pick a value for it. Read-only fields use 0
individually_accessible01 means the bus can write this field alone (needs byte enables and a field that fills whole bytes)

Three more calls matter just as much:

  • super.new(name, n_bits, has_coverage) in the register sets its width. All five registers here are 32 bits, even the ones with only 4 bits of fields, because the bus reads 32 bits.
  • create_map(name, base_addr, n_bytes, endian, byte_addressing) builds the map. 4 bytes wide and byte addressing means registers sit at 0x00, 0x04, 0x08 and so on.
  • add_reg(reg, offset, rights) places a register. The rights here are “RW” for every register, even ID. The field policy (RO) decides what a write does to the model; the map rights decide whether the model lets you send a write at all. I keep the map rights open so the built-in tests can try to write ID and prove the hardware ignores it.

lock_model() closes the model: after it, no register can be added and the address lookup tables are built. Forget it and the first access fails.

What are the UVM RAL access policies?

The access string on each field tells the model what a write or a read does to that field. Get it wrong and the model predicts the wrong value, so every check on that field fails, or worse, passes for the wrong reason. UVM 1.2 defines 25 policies. You will meet the first eight on almost every chip.

PolicyWrite doesRead doesTypical use
RWstores the valuenothingcontrol and configuration
ROnothingnothingstatus, ID, version
WOstores the valuereturns nothing usefulcommand or trigger registers
W1Ceach 1 clears that bit, 0 leaves itnothinginterrupt status (INT_STAT here)
W1Seach 1 sets that bitnothinginterrupt set or force registers
RCnothingclears the field after the readsticky status that clears on read
WRCstores the valueclears the field after the readstatus that software can also preload
W1Teach 1 toggles that bitnothingGPIO toggle registers

The rest are variations on the same ideas:

PolicyMeaning
RSread sets all bits
WRSwrite stores, read sets
WCany write clears all bits
WSany write sets all bits
WSRCany write sets, read clears
WCRSany write clears, read sets
W0C, W0S, W0Teach 0 clears, sets or toggles that bit
W1SRC, W1CRS1 sets (or clears) on write, read clears (or sets)
W0SRC, W0CRSthe same with 0 as the active value
WOC, WOSwrite-only, the write clears (or sets) all bits
W1, WO1only the first write after reset takes effect (WO1 is also write-only)

If a field behaves in a way none of these describe, you can define your own with uvm_reg_field::define_access(“NAME”) and model the behaviour in a callback.

Reserved bits, volatile fields and byte enables

Unused bits. CTRL bit 3 and bits [31:8] belong to no field. The model treats every bit outside a field as read-only zero, and uvm_reg_bit_bash_seq checks exactly that: it flips each of those bits and expects 0 back. If the design stores them by mistake, bit-bash will tell you.

Volatile fields. INT_STAT is set by hardware events the model never sees, so I configured it with volatile = 1. That tells UVM the mirror cannot be trusted for this field, and UVM switches its compare off. It is the correct model, and it has a cost: hw_reset, bit_bash and every mirror(UVM_CHECK) skip INT_STAT. A W1C bug there is invisible to all of them, as the bug runs below show.

Field writes and byte enables. individually_accessible = 1 tells UVM a field can be written alone. That only works if the bus has byte enables and the field fills whole bytes. APB3 has no byte strobes, so I set it to 0 and the adapter says supports_byte_enable = 0. A field write then becomes a full register write, using the mirrored values for the other fields. UVM 1.2 warns about it every time, and you will see that warning in the log below.

What are the desired, mirrored and actual values in RAL?

Every field in the model holds two numbers, and the design holds a third. Most RAL confusion comes from mixing them up.

  • Desired value. What you want the register to hold next. set() and randomize() change it. update() writes it to the design.
  • Mirrored value. What the model believes the design holds right now. It changes after every write and read the model knows about.
  • Actual value. What the flops in the design really hold. Only the design knows it for sure.
Desired, mirrored and design values for CTRL through set(), update() and peek(), using the values from the VCS run.
Desired, mirrored and design values of CTRL, step by step on VCS.
MethodDesiredMirroredBus trafficDesign
write(v)vv (after prediction)1 writechanges
read(v)read valueread value1 readno change
set(v)vno changenoneno change
get()returns itno changenoneno change
get_mirrored_value()no changereturns itnoneno change
update()no changebecomes desired1 write per register that needs itchanges
mirror(UVM_CHECK)read valueread value, after comparing1 readno change
predict(v)vvnoneno change
peek(v)read valueread valuenone (backdoor)no change
poke(v)vvnone (backdoor)changes
reset()reset valuereset valuenoneno change

How does each RAL method work? A tour on VCS

Phase 0 of the bench calls every method once. After each call it prints the desired value, the mirrored value and the real flop value (read straight from the RTL with uvm_hdl_read(), so the print itself does not disturb the model). It also counts the APB transfers each step caused. Here is the clean run, step by step.

Step 0: what does the model look like?

  • Before calling any method, the tour prints the whole model: every register in the chip default_map with its address and fields, the memory, and the map settings.
  • The output and the map calls that built it are in section 8 of the layers above. No APB traffic in this step.

Step 1: why does the model hold 0 after build()?

I expected the model to start at the reset values. It does not. build() creates the fields; only reset() loads the reset values into desired and mirrored.

@   45 [TOUR]    ctrl     desired=0x00000000  mirrored=0x00000000  design=0x00000040
@   45 [TOUR]    ctrl     desired=0x00000040  mirrored=0x00000040  design=0x00000040
@   45 [TOUR]    ctrl.get_reset() = 0x40

The first line is before regmodel.reset(), the second after. uvm_reg_hw_reset_seq calls reset() for you, but if your first test is your own sequence, call it yourself.

Step 2: write() and read()

rm.ctrl.write(status, 'h13);
@  106 [CALLBACK]    post_write: CTRL written with 0x13 through the frontdoor (write 1)
@  106 [TOUR]    ctrl     desired=0x00000013  mirrored=0x00000013  design=0x00000013
@  136 [TOUR]    APB traffic in this step: 1 write(s), 1 read(s)

One APB write, then one APB read for the check. All three values agree.

Step 3: writing one field

rm.ctrl.prescale.write(status, 'h9);
@  136 [RegModel] Individual field access not available for field 'regmodel.ip.ctrl.prescale'. Accessing complete register instead.
@  166 [CALLBACK]    post_write: CTRL written with 0x93 through the frontdoor (write 2)
@  166 [TOUR]    ctrl     desired=0x00000093  mirrored=0x00000093  design=0x00000093

APB cannot write 4 bits, so UVM wrote all of CTRL: prescale = 9 from my call, en and mode from their mirrored values (1 and 01). 0x13 became 0x93. The warning is UVM telling you it did that. This is why the mirror must be right before a field write: a stale mirror writes stale values into the other fields.

Step 4: set(), needs_update() and update()

rm.ctrl.mode.set(2'b10);
...
rm.ctrl.update(status);
@  196 [TOUR]    ctrl     desired=0x00000095  mirrored=0x00000093  design=0x00000093
@  196 [TOUR]    ctrl.needs_update() = 1
@  226 [CALLBACK]    post_write: CTRL written with 0x95 through the frontdoor (write 3)
@  226 [TOUR]    ctrl     desired=0x00000095  mirrored=0x00000095  design=0x00000095
@  226 [TOUR]    ctrl.needs_update() = 0
@  226 [TOUR]    APB traffic in this step: 1 write(s), 0 read(s)

set() moved only the desired value (0x93 to 0x95). Nothing reached the design until update(), which saw desired != mirrored and sent one write. This is the pattern for configuring many registers: set them all in the model, then one update() sends only what changed.

Step 5: does peek() change the model?

Yes, and it bit me while building this bench. peek() reads the design through the backdoor and then updates both desired and mirrored with what it found.

@  226 [TOUR]    ctrl     desired=0x00000093  mirrored=0x00000095  design=0x00000095
@  226 [TOUR]    ctrl.peek() = 0x95
@  226 [TOUR]    ctrl     desired=0x00000095  mirrored=0x00000095  design=0x00000095
@  226 [TOUR]    the set(2'b01) above is gone: peek() overwrote the desired value

I had set mode to 01 (desired 0x93). One peek() later, the set() was gone. In my first version of this bench I used peek() just to print the design value, and it silently wiped out every set() and randomize() before update() could send them. If you only want to look, use uvm_hdl_read() on the HDL path.

Step 6: randomize() and a block-level update()

if (!rm.randomize()) `uvm_error("TOUR", "randomize failed")
rm.update(status);
@  226 [TOUR]    ctrl     desired=0x00000076  mirrored=0x00000095  design=0x00000095
@  226 [TOUR]    int_en   desired=0x00000002  mirrored=0x00000000  design=0x00000000
@  226 [TOUR]    scratch  desired=0x185d80ef  mirrored=0x00000000  design=0x00000000
@  436 [TOUR]    ctrl     desired=0x00000076  mirrored=0x00000076  design=0x00000076
@  436 [TOUR]    int_en   desired=0x00000002  mirrored=0x00000002  design=0x00000002
@  436 [TOUR]    scratch  desired=0x185d80ef  mirrored=0x185d80ef  design=0x185d80ef
@  436 [TOUR]    APB traffic in this step: 4 write(s), 3 read(s)

Three registers changed, yet update() sent four writes. The fourth went to INT_STAT. In UVM, a volatile field always reports needs_update() = 1, so a block-level update() rewrites it every time. Here that is harmless, because writing 0 to a W1C field changes nothing. On a register where a write has a side effect, it is not harmless, so know which of your fields are volatile.

Step 7: mirror() on a register the hardware changed

The test pulses two interrupt events. The design sets INT_STAT bits 0 and 1. The model has no idea.

@  450 [TOUR]    int_stat desired=0x00000000  mirrored=0x00000000  design=0x00000003
@  476 [TOUR]    int_stat desired=0x00000003  mirrored=0x00000003  design=0x00000003
@  506 [TOUR]    int_stat desired=0x00000002  mirrored=0x00000002  design=0x00000002

Line 1: the mirror is stale. Line 2: after mirror(status, UVM_NO_CHECK), the model read the design and caught up. Line 3: after writing 1 to bit 0, the W1C policy predicted 0x2 and the design agrees. I used UVM_NO_CHECK because the field is volatile; with UVM_CHECK the compare would be skipped anyway.

For the full flow around a W1C interrupt status register (event, interrupt line, handler, clear), see how to handle interrupts in UVM.

Step 8: predict()

Another event sets bit 2. Instead of reading, the test tells the model what it knows:

void'(rm.int_stat.predict('h6));
@  550 [TOUR]    int_stat desired=0x00000006  mirrored=0x00000006  design=0x00000006
@  550 [TOUR]    APB traffic in this step: 0 write(s), 0 read(s)

No bus traffic at all. predict() is how a scoreboard or a callback keeps the model right when hardware changes a register and you know exactly how.

Step 9: poke(), peek() and a backdoor read

rm.scratch.poke(status, 'hDEAD_BEEF);
rm.id.peek(status, v);
rm.ctrl.read(status, v, UVM_BACKDOOR);
@  606 [TOUR]    scratch  desired=0xdeadbeef  mirrored=0xdeadbeef  design=0xdeadbeef
@  606 [TOUR]    id.peek() = 0x5a01
@  606 [TOUR]    ctrl.read(UVM_BACKDOOR) = 0x76
@  606 [TOUR]    APB traffic in this step: 0 write(s), 0 read(s)

Three accesses, zero APB transfers, zero simulation time (all three lines print at 606 ns). poke() wrote the flop and the model followed. A frontdoor read right after returned 0xDEADBEEF, so the backdoor really changed the design.

Frontdoor vs backdoor access: when should you use each?

Frontdoor: model, adapter, sequencer, driver, APB pins, decode, flop, taking bus cycles. Backdoor: model straight to the flop through its HDL path in zero time.
Frontdoor goes through the bus; backdoor goes straight to the flop.

Frontdoor is the real path. The model builds a generic request, the adapter turns it into an APB item, the driver puts it on the pins, the design decodes the address and the flop changes. It takes bus cycles (30 ns per transfer in this bench) and it is exactly what software will do on silicon.

Backdoor skips all of that. RAL uses the HDL path you gave it (tav_top.dut plus the slice name, for example ctrl) and reads or deposits the flop value directly through the simulator, in zero time. It needs those paths in the model and simulator access to the signals. On VCS that is the -debug_access+all compile option; without it, every peek and poke fails.

FrontdoorBackdoor
Pathadapter, sequencer, driver, bus, decodeHDL path straight to the flop
Timebus cycleszero
Tests the bus and the decodeyesno
Honours access policiesyes, the design applies themno: poke() forces the value, even into RO and W1C fields
Side effects (clear on read, W1C)happendo not happen
Needs HDL paths and simulator accessnoyes
Works on gate-level netlistsyesonly if the paths survive synthesis
Works on emulation and siliconyesno

Use frontdoor for:

  • Any check that a register is reachable and behaves right.
  • Every test of access policies and side effects (W1C, clear on read).
  • Every test of the bus and the address decode.
  • Anything that copies what software will do. All four bugs in this guide were found through the frontdoor.

Use backdoor for:

  • Loading a large configuration or memory image in zero time before the real test starts.
  • Putting a register into a state the bus cannot reach: a W1C status bit with no interrupt, an RO counter near its wrap value.
  • Checking a value without adding bus traffic that would disturb the scenario.
  • Reading a memory to check a DMA result.

The trap: backdoor tells you what the flop holds. It does not prove the bus can reach it. A register that is wired to the wrong address passes every backdoor check. uvm_reg_access_seq exists for exactly that reason: it writes through one door and reads through the other, and complains if they disagree.

Step 10: explicit prediction keeps the mirror right for traffic the model did not send

The test sends a plain APB write to 0x1C, not through the model, the way another bus master or embedded software would.

@  666 [TOUR]    scratch  desired=0x0000cafe  mirrored=0x0000cafe  design=0x0000cafe
@  666 [TOUR]    APB traffic in this step: 1 write(s), 0 read(s)

The model never called write(), yet the mirror says 0xCAFE. The uvm_reg_predictor saw the transfer on the APB monitor, looked up 0x1C in the map, and updated SCRATCH.

Step 11: hardware reset, then regmodel.reset()

@  690 [TOUR]    ctrl     desired=0x00000040  mirrored=0x00000040  design=0x00000040
@  690 [TOUR]    scratch  desired=0x00000000  mirrored=0x00000000  design=0x00000000

The design does not tell the model it was reset. When your test asserts reset, call regmodel.reset() at the same point, or every later check compares against values from before the reset.

Step 12: memories with uvm_mem

@  836 [TOUR]    buffer.write(3, 0x11110003), then buffer.read(3) = 0x11110003
@ 1076 [TOUR]    buffer.burst_write(8, 4 words), burst_read(8) = '{'ha0, 'ha1, 'ha2, 'ha3}
@ 1076 [TOUR]    buffer.poke(15, 0xBEEF), then buffer.peek(15) = 0xbeef
@ 1076 [TOUR]    APB traffic in this step: 5 write(s), 5 read(s)

A memory is accessed by offset: buffer.write(status, 3, value) went to 0x40 + 3 x 4 = 0x4C. Bursts on APB become one transfer per word, so the 4-word burst cost 4 writes and 4 reads. A memory has no mirror: RAL does not keep a copy of every word, so there is no mirror() and no automatic check. You compare reads yourself, or against a reference model. For big memories, uvm_mem_mam can hand out free regions, and uvm_vreg can lay virtual registers over a memory, but most benches never need either.

Step 13: callbacks

class tav_ctrl_cb extends uvm_reg_cbs;
  ...
  virtual task post_write(uvm_reg_item rw);
    n_writes++;
    ...
  endtask
endclass
// in the env:
uvm_reg_cb::add(regmodel.ip.ctrl, cb);
@  106 [CALLBACK]    post_write: CTRL written with 0x13 through the frontdoor (write 1)
@  166 [CALLBACK]    post_write: CTRL written with 0x93 through the frontdoor (write 2)
@  226 [CALLBACK]    post_write: CTRL written with 0x95 through the frontdoor (write 3)
@  256 [CALLBACK]    post_write: CTRL written with 0x76 through the frontdoor (write 4)
@ 1106 [TOUR]    CTRL writes seen by the callback so far: 4

uvm_reg_cbs gives you hooks around every access: pre_write, post_write, pre_read, post_read, and post_predict on fields. Use post_predict to model a side effect the access policies cannot describe, such as writing CTRL.en clearing a counter in another register. Use pre_write to block or log writes. Four CTRL writes happened in the tour, and the callback saw all four.

Step 14: a register file adds a level to the name and the HDL path

chip.dma.ch1.len.write(status, 'h0200);
chip.dma.ch1.len.get_full_hdl_path(hp);   // print hp[0].slices[0].path
@ 1136 [TOUR]    len      desired=0x00000200  mirrored=0x00000200  design=0x00000200
@ 1136 [TOUR]    full name: regmodel.dma.ch1.len
@ 1136 [TOUR]    HDL path:  tav_top.dut.u_ch1.len
@ 1166 [TOUR]    APB traffic in this step: 1 write(s), 1 read(s)
  • The name is chip block, IP block, register file, register.
  • The HDL path is joined the same way: the block path tav_top.dut, the register file path u_ch1, then the slice len.
  • One register file class serves both channels. Only the offset and the HDL path differ.

Step 15: one chip block holds both IP blocks

chip.get_registers(regs);
chip.dma.ch1.len.get_address(chip.default_map);
@ 1166 [TOUR]    registers in the chip model: 9
@ 1166 [TOUR]    ip.ctrl at 0x00, dma.ch0.src at 0x80, dma.ch1.len at 0x94 in default_map
  • 9 registers: 5 from the IP block and 4 from the DMA block. The memory is not a register, so it is not counted.
  • The addresses come from the sub-map bases. Neither IP block knows where it sits in the chip.
  • No bus traffic: this is all model data.

Step 16: the same register through two maps

chip.dma.ch0.src.write(status, 'hA5A5_0001, UVM_FRONTDOOR, chip.dbg_map);
chip.dma.ch0.src.read (status, v,           UVM_FRONTDOOR, chip.default_map);
@ 1226 [TOUR]    write via dbg_map, read via default_map = 0xa5a50001
@ 1226 [TOUR]    APB traffic in this step: 1 write(s), 1 read(s)
  • The map argument picks the path. Leave it out and RAL uses default_map.
  • Both maps use the same APB agent here, so this proves the wiring, not a second bus. On a real chip dbg_map would sit on a debug bus with its own sequencer and adapter.
  • uvm_reg_shared_access_seq (phase 3) does this for every register that is in more than one map.

Step 17: reset the design before uvm_reg_hw_reset_seq

@(negedge vif.pclk) vif.presetn = 0;
repeat (2) @(negedge vif.pclk);
vif.presetn = 1;
chip.reset();                                   // model back to reset values too
chip.dma.ch0.src.mirror(status, UVM_CHECK);
chip.dma.ch1.len.mirror(status, UVM_CHECK);
@ 1250 [TOUR]    src      desired=0x00000000  mirrored=0x00000000  design=0x00000000
@ 1250 [TOUR]    len      desired=0x00000040  mirrored=0x00000040  design=0x00000040
@ 1306 [TOUR]    APB traffic in this step: 0 write(s), 2 read(s)
  • uvm_reg_hw_reset_seq resets the model and reads the design. It never drives reset. It trusts that the design is already in reset state.
  • I learned this on an earlier version of this bench. Steps 14 and 16 wrote two DMA registers after the step 11 reset, and the clean run failed hw_reset with no bug switched on:
UVM_ERROR ... [RegModel] [phase 1] Register "regmodel.dma.ch0.src" value read from DUT (0x00000000a5a50001) does not match mirrored value (0x0000000000000000)
UVM_ERROR ... [RegModel] [phase 1] Register "regmodel.dma.ch1.len" value read from DUT (0x0000000000000200) does not match mirrored value (0x0000000000000040)
  • The values in those lines are exactly what steps 16 and 14 wrote. When hw_reset fails on a clean design, check what ran before it.

The same rule holds when you assert reset in the middle of a test: reset the design, then call reset() on the model. Reset testing with a phase jump in UVM shows one way to structure that.

What does the register adapter do?

The model speaks one generic language: a uvm_reg_bus_op with a kind (read or write), an address, data and a status. Your bus speaks APB, AXI or something in-house. The adapter translates both ways, and it is the only bus-specific piece of RAL.

class tav_apb_adapter extends uvm_reg_adapter;
  `uvm_object_utils(tav_apb_adapter)
  function new(string name = "tav_apb_adapter");
    super.new(name);
    supports_byte_enable = 0;          // APB3 has no byte strobes
    provides_responses   = 0;          // the driver puts read data into the same item
  endfunction
  virtual function uvm_sequence_item reg2bus(const ref uvm_reg_bus_op rw);
    tav_apb_item item = tav_apb_item::type_id::create("item");
    item.addr  = rw.addr;
    item.write = (rw.kind == UVM_WRITE || rw.kind == UVM_BURST_WRITE);   // bursts arrive as UVM_BURST_WRITE
    item.data  = rw.data;
    return item;
  endfunction
  virtual function void bus2reg(uvm_sequence_item bus_item, ref uvm_reg_bus_op rw);
    tav_apb_item item;
    if (!$cast(item, bus_item)) `uvm_fatal("ADAPTER", "not a tav_apb_item")
    rw.kind   = item.write ? UVM_WRITE : UVM_READ;
    rw.addr   = item.addr;
    rw.data   = item.data;
    rw.status = item.slverr ? UVM_NOT_OK : UVM_IS_OK;
  endfunction
endclass
  • reg2bus() turns a model request into a bus item. Note the burst line: memory bursts arrive as UVM_BURST_WRITE, not UVM_WRITE. My first version only checked UVM_WRITE, so on VCS the 4-word burst_write went out as 4 reads and burst_read returned ‘{‘h0, ‘h0, ‘h0, ‘h0}.
  • bus2reg() turns a completed bus item back into a response. The predictor uses it too, on every item the monitor sees. Map PSLVERR (or AXI SLVERR) to UVM_NOT_OK here, or bus errors vanish.
  • provides_responses is 0 when the driver writes read data back into the same request item, and 1 when the driver sends a separate response with put_response().
  • supports_byte_enable says whether the bus can write part of a word.

Auto, explicit or passive prediction: which one should you use?

Prediction is how the mirror learns what changed. UVM gives you three ways.

ModeHow the mirror is updatedSees accesses from other masters?Typical use
Auto (implicit)the model updates itself after each of its own read() and write()nofirst bring-up, quick unit benches
Explicita uvm_reg_predictor on the bus monitor updates it for every transfer on the busyesmost real benches, including this one
Passivethe same predictor, with no sequencer driving the busyesmonitoring a bus that only firmware or another agent drives

Step 10 of the tour shows why explicit is the usual choice: the raw APB write updated the mirror even though the model never sent it. With auto prediction, the mirror would still say 0xDEADBEEF and the next check would fail for the wrong reason.

One timing detail matters with explicit prediction. The monitor must publish the transfer before the sequence that sent it continues. My driver waits 1 ns after the last APB edge before item_done(), so the predictor has updated the mirror by the time write() returns. Without that, a check right after a write can compare against the old mirror.

How do you wire RAL into the environment?

How the register model connects: register spec and generator feed the model; sequences call the model; frontdoor goes model, adapter, APB sequencer and driver, DUT; the monitor feeds the predictor, which updates the mirror; backdoor goes straight from the model to the DUT flops through HDL paths; hardware events change INT_STAT inside the DUT.
How the register model connects to the testbench and the design.
  • Spec to model: a generator (or you, by hand) turns the register spec into uvm_reg classes.
  • Sequences to model: tests never touch the bus directly. They call write, read, update, mirror, peek and poke on the model.
  • Frontdoor: the map hands a register item to the adapter, reg2bus() turns it into an APB item, and the agent’s driver puts it on the bus.
  • Mirror: the monitor sees every transfer, from anyone. The predictor uses bus2reg() and updates the mirror.
  • Backdoor: peek and poke skip the bus and go straight to the flops through the HDL paths.
  • Hardware: event inputs change INT_STAT on their own. The model only learns about it through a read or mirror().

If the env itself is new to you, start with UVM testbench architecture. The virtual interface reaches the driver and monitor through uvm_config_db.

function void build_phase(uvm_phase phase);
  regmodel  = tav_chip_block::type_id::create("regmodel");
  regmodel.build();
  adapter   = tav_apb_adapter::type_id::create("adapter");
  predictor = uvm_reg_predictor#(tav_apb_item)::type_id::create("predictor", this);
  agent     = tav_apb_agent::type_id::create("agent", this);
endfunction
function void connect_phase(uvm_phase phase);
  regmodel.default_map.set_sequencer(agent.sqr, adapter);   // model accesses go to this sequencer
  regmodel.dbg_map.set_sequencer(agent.sqr, adapter);       // the debug map uses the same APB agent here
  regmodel.default_map.set_auto_predict(0);                 // the predictor keeps the mirror
  regmodel.dbg_map.set_auto_predict(0);
  predictor.map     = regmodel.default_map;
  predictor.adapter = adapter;
  agent.mon.ap.connect(predictor.bus_in);                   // every APB transfer, from anyone
  cb = tav_ctrl_cb::type_id::create("cb");
  uvm_reg_cb::add(regmodel.ip.ctrl, cb);                    // hook the callback onto CTRL
endfunction

Four connections: the map to the sequencer and adapter, auto prediction off, the predictor to the map and adapter, and the monitor to the predictor. Sequences then reach the model through a handle. In bigger benches the env puts it in uvm_config_db and each sequence fetches it, or the test sets a model handle on each sequence before starting it, as this bench does.

Testbench architecture: sequences call the register model; the default map sends requests through the adapter to the APB sequencer and driver; the monitor feeds the predictor, which updates the mirror; HDL paths give the backdoor.
The testbench used in this guide: test phases, register model, predictor, adapter, APB agent, DUT.

How does RAL scale to a full chip?

The chip-level block in layer 7 above is the whole answer:

  • Each IP keeps its own block, written once and reused.
  • The chip block instantiates them and adds each IP map with add_submap() at the IP’s base address. In this bench the DMA block’s registers sit at 0x80 plus their own offsets.
  • HDL paths stack the same way: chip path, then IP path, then register file, then register.
  • A block reachable from two buses gets two maps, each added to the matching chip map (dma.cpu_map into default_map, dma.dbg_map into dbg_map here).
  • Tour steps 14 to 16 and uvm_reg_mem_shared_access_seq run all of this on VCS.

For what else a chip-level bench has to check around blocks like these, see SoC verification: the key areas.

User-defined frontdoors and indirect registers

Some registers are not reached by one plain bus access:

  • Indirect registers. Write an index register, then read or write a data register. UVM ships uvm_reg_indirect_data for this pattern.
  • Registers behind a mailbox or a handshake. Extend uvm_reg_frontdoor, write the real access sequence in its body(), and attach it with map.set_frontdoor().
  • Either way, tests keep calling read() and write() as normal. This bench has neither, so this part is explained, not run.

Register coverage

  • Ask for coverage in super.new(): UVM_CVR_REG_BITS, UVM_CVR_ADDR_MAP or UVM_CVR_FIELD_VALS.
  • Switch it on with uvm_reg::include_coverage() before the model is built.
  • UVM_CVR_ADDR_MAP records which addresses were accessed. UVM_CVR_FIELD_VALS needs your own covergroup and sample_values().
  • Generators usually write these for you. This bench uses UVM_NO_COVERAGE to stay short.

Do you write the register model by hand?

On real projects, almost never:

  • The register map lives in a machine-readable spec: SystemRDL or IP-XACT XML.
  • A generator writes the RAL classes, the RTL register block and the documentation from it: open source PeakRDL, Synopsys ralgen, or the Cadence and Siemens equivalents.
  • Writing one by hand once, as here, is still the best way to understand what the generator produces, and to read its output when something looks wrong.

What are the UVM built-in register sequences?

UVM ships ready-made sequences that test a register model against the design with almost no code. You set their model handle and start them.

SequenceWhat it doesSkips
uvm_reg_hw_reset_seqcalls reset() on the model, then reads every register and compares it with its reset valuefields with no reset, volatile fields (compare off)
uvm_reg_bit_bash_seqfor every bit of every register, writes a 1 and a 0 and reads backvolatile fields, write-only fields
uvm_reg_access_seqwrites through the frontdoor and reads through the backdoor, then the other way roundregisters with only RO fields (a register with no HDL path gives an error)
uvm_reg_mem_hdl_paths_seqchecks that every HDL path in the model exists in the designnothing it can reach
uvm_mem_walk_seqwalks a memory: writes every word, reads it backread-only memories
uvm_mem_access_seqfrontdoor vs backdoor for every memory wordmemories with no HDL path
uvm_reg_mem_shared_access_seqfor registers in two maps, writes through one map and reads through the otherregisters in only one map
uvm_reg_mem_built_in_seqruns the set above, chosen by a test-type mask

To keep a register out of a built-in test, set a resource on its full name before you start the sequence:

uvm_resource_db#(bit)::set({"REG::", regmodel.ip.int_stat.get_full_name()},
                           "NO_REG_BIT_BASH_TEST", 1, this);

The other keys are NO_REG_TESTS (all register tests), NO_REG_HW_RESET_TEST, NO_REG_ACCESS_TEST, NO_REG_SHARED_ACCESS_TEST, NO_MEM_TESTS, NO_MEM_WALK_TEST and NO_MEM_ACCESS_TEST.

On this bench the built-ins ran in phases 1 to 3. bit_bash alone needed 824 APB writes and 824 APB reads, because it bashes each DMA register once per map (13 register-map pairs). In the clean run, uvm_reg_access_seq printed “Register ‘regmodel.ip.id’ has only RO fields in map regmodel.ip.default_map, skipping”. That warning is expected: there is nothing to write through either door.

Built-in vs targeted tests: which caught which bug?

After the 18-step tour (steps 0 to 17), the test runs five phases:

  • Phase 1: uvm_reg_hw_reset_seq.
  • Phase 2: uvm_reg_bit_bash_seq.
  • Phase 3: uvm_reg_mem_hdl_paths_seq, uvm_reg_access_seq, uvm_mem_walk_seq and uvm_reg_shared_access_seq.
  • Phase 4: tav_w1c_seq (targeted). It pulses interrupt events and checks INT_STAT sets and clears the right bits.
  • Phase 5: tav_alias_seq (targeted). It writes a different value to every RW register, then mirrors them all.
  • A report catcher counts errors per phase.
Results grid of five VCS runs: which phase caught each planted bug.
Which phase caught which planted bug, in five VCS runs.
RunTourhw_resetbit_bashpaths, access, walk, sharedW1C (targeted)alias (targeted)
cleanPASSPASSPASSPASSPASSPASS
BUG=1 reset value2 errors1 errorPASSPASSPASSPASS
BUG=2 W1C acts as RW2 errorsPASSPASSPASS5 errorsPASS
BUG=3 ID writablePASSPASS32 errorsPASSPASSPASS
BUG=4 address alias1 errorPASSPASSPASSPASS1 error

BUG=1, wrong reset value. hw_reset found it in one read, which is exactly its job:

UVM_ERROR ... [RegModel] [phase 1] Register "regmodel.ip.ctrl" value read from DUT (0x0000000000000000) does not match mirrored value (0x0000000000000040)
UVM_INFO  ... [RegModel] Field prescale (regmodel.ip.ctrl[7:4]) mismatch read=4'h0 mirrored=4'h4

The second line is the most useful line UVM prints: it names the field.

BUG=2, W1C behaving as RW. Every built-in passed. INT_STAT is volatile, so hw_reset, bit_bash and access all switch its compare off. Only the targeted W1C sequence, which compares the read values itself, saw it, 5 times:

UVM_ERROR ... [TAV_CHECK] [phase 4] after writing 1 to every bit: int_stat read 0xf, expected 0x0
UVM_ERROR ... [TAV_CHECK] [phase 4] after events 0 and 2: int_stat read 0xf, expected 0x5

The tour caught it too, at step 7: the model predicted 0x0000000e after the W1C write, and the design held 0x00000001.

BUG=3, RO register writable. This is what bit_bash is for. It wrote each of the 16 ID bits both ways and read back a changed value every time, 32 errors:

UVM_ERROR ... [uvm_reg_bit_bash_seq] [phase 2] Writing a 0 in bit #0 of register "regmodel.ip.id" with initial value 'h0000000000005a01 yielded 'h0000000000005a00 instead of 'h0000000000005a01

No other phase writes ID, so nothing else saw it.

BUG=4, address alias. bit_bash and access passed: each tests one register at a time, so a write to SCRATCH that also lands in INT_EN is never compared against INT_EN. The targeted alias sequence writes everything first and mirrors everything after:

UVM_ERROR ... [RegModel] [phase 5] Register "regmodel.ip.int_en" value read from DUT (0x0000000000000008) does not match mirrored value (0x000000000000000a)
  • INT_EN got 0x8, the low bits of the 0x12345678 written to SCRATCH.
  • The tour caught it too, at step 6. regmodel.update() wrote SCRATCH = 0x185d80ef, and the mirror check then read INT_EN as 0xf where the model held 0x2.
  • hw_reset passed in this run, and that is the honest result: a reset test does not look for decode bugs. In an earlier version of this bench, hw_reset also flagged BUG=4, but only by luck. The tour had left memory word 3 at 0x4C written, which under this bug also lands in INT_EN. Step 17 now resets the design before hw_reset, so that luck is gone.

What the grid says. The built-ins are cheap and they did their jobs: wrong reset values and wrong bit behaviour in a single register. They missed both bugs that involve state they do not compare (a volatile field) or more than one register at a time (an alias). Those need a short test that knows what the spec promises.

The full code

One file, 917 lines: the design, the RAL model, the APB agent, adapter and predictor, the tour and the five test phases. Paste it into testbench.sv on EDA Playground, leave design.sv empty, pick Synopsys VCS with UVM 1.2, set the top module to tav_top, and add -debug_access+all to the compile options. Add +BUG=1 to +BUG=4 to the run options to plant a bug.

The same file, the AI-check version and both expected outputs are in the free repo, folder bonus-uvm-ral.

// tav_ral_apb_uvm.sv : a complete UVM RAL example on an APB register block (UVM 1.2)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 25 Nov 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
// Paste this whole file into testbench.sv and leave design.sv empty.
// Compile options: add  -debug_access+all  (peek and poke reach the design through it).
//
// What is in here:
//   the design       tav_apb_regs: five registers, a 16-word memory and two DMA channels on an APB slave
//   the RAL model    every layer: fields, registers, a register file (one per DMA channel), a memory,
//                    two IP blocks inside a chip-level block, address maps (the DMA block has two:
//                    a CPU map and a debug map), sub-maps and HDL paths
//   the bus side     APB driver, APB monitor, adapter and an explicit predictor
//   the memory       a 16-word buffer modelled with uvm_mem
//   a callback       uvm_reg_cbs on CTRL, called after every write to it
//   the test         phase 0 walks through the RAL methods one by one (write, read, field write,
//                    set, get, update, randomize, mirror, predict, peek, poke, reset, memory
//                    access, callbacks, register files, sub-blocks, two maps), then phases
//                    1 to 5 hunt for planted bugs
//
// Register map (32-bit registers, byte addresses):
//   0x00 CTRL     en [0] RW, mode [2:1] RW, prescale [7:4] RW. Reset 0x40 (prescale = 4).
//                 Bit 3 and bits [31:8] are unused and read 0.
//   0x04 ID       id [15:0] RO, reads 0x5A01. Writes are ignored.
//   0x08 INT_STAT pending [3:0] W1C. Hardware sets a bit when its event input pulses;
//                 software writes 1 to clear it. Reset 0.
//   0x0C INT_EN   enable [3:0] RW. Reset 0.
//   0x1C SCRATCH  data [31:0] RW. Reset 0.
//   0x40-0x7C BUF 16 words x 32 bits of RAM, read and write. No reset value.
//   0x80, 0x84 DMA channel 0: SRC data [31:0] RW reset 0, LEN len [15:0] RW reset 0x40
//   0x90, 0x94 DMA channel 1: the same two registers
//   Any other address reads 0, ignores writes and answers with PSLVERR.
//
// Phases:
//   0  tav_tour_seq           every RAL method once, with the model and the design printed side by side
//   1  uvm_reg_hw_reset_seq   built in: reads every register after reset, checks the reset value
//   2  uvm_reg_bit_bash_seq   built in: flips every bit of every register and reads it back
//   3  built in: uvm_reg_mem_hdl_paths_seq (do the HDL paths exist?), uvm_reg_access_seq
//      (do frontdoor and backdoor agree?), uvm_mem_walk_seq (is every memory word there?) and
//      uvm_reg_mem_shared_access_seq (do the CPU map and the debug map reach the same DMA registers?)
//   4  tav_w1c_seq            targeted: pulses interrupt events, checks INT_STAT sets and clears right
//   5  tav_alias_seq          targeted: writes every RW register, then mirrors them all
//
// Run options (one per run):
//   (none)    the clean block
//   +BUG=1    CTRL resets to 0x00 instead of 0x40
//   +BUG=2    INT_STAT acts as RW: a write stores the value instead of clearing bits
//   +BUG=3    ID is writable
//   +BUG=4    INT_EN decode ignores address bits [7:4], so a write to 0x1C also writes INT_EN

`timescale 1ns/1ns
`include "uvm_macros.svh"
import uvm_pkg::*;

// ---------------------------------------------------------------------------
// The design: an APB register block
// ---------------------------------------------------------------------------
// One DMA channel's registers. Two instances, u_ch0 and u_ch1, give the register file its HDL level.
module tav_dma_chan (input logic pclk, presetn, wr_src, wr_len, input logic [31:0] wdata,
                     output logic [31:0] src, output logic [15:0] len);
  always_ff @(posedge pclk or negedge presetn)
    if (!presetn) begin src <= 32'h0; len <= 16'h0040; end
    else begin
      if (wr_src) src <= wdata;
      if (wr_len) len <= wdata[15:0];
    end
endmodule

module tav_apb_regs (input  logic        pclk, presetn,
                     input  logic        psel, penable, pwrite,
                     input  logic [7:0]  paddr,
                     input  logic [31:0] pwdata,
                     output logic [31:0] prdata,
                     output logic        pready, pslverr,
                     input  logic [3:0]  ev,
                     input  int          bug);
  logic [7:0]  ctrl;
  logic [15:0] id;
  logic [3:0]  int_stat, int_en;
  logic [31:0] scratch;
  logic [31:0] mem [16];                                                     // BUF
  logic        wr, hit_int_en, mapped;
  logic [31:0] ch0_src, ch1_src;
  logic [15:0] ch0_len, ch1_len;

  assign wr         = psel && penable && pwrite;
  assign hit_int_en = (bug == 4) ? (paddr[3:0] == 4'hC) : (paddr == 8'h0C);   // BUG=4: alias
  assign mapped     = paddr inside {8'h00, 8'h04, 8'h08, 8'h0C, 8'h1C, 8'h80, 8'h84, 8'h90, 8'h94}
                      || paddr[7:6] == 2'b01;
  assign pready     = 1'b1;                                                   // no wait states
  assign pslverr    = psel && penable && !mapped;

  tav_dma_chan u_ch0 (pclk, presetn, wr && paddr == 8'h80, wr && paddr == 8'h84, pwdata, ch0_src, ch0_len);
  tav_dma_chan u_ch1 (pclk, presetn, wr && paddr == 8'h90, wr && paddr == 8'h94, pwdata, ch1_src, ch1_len);

  always_ff @(posedge pclk or negedge presetn)
    if (!presetn) begin
      ctrl     <= (bug == 1) ? 8'h00 : 8'h40;                                 // BUG=1: reset value
      id       <= 16'h5A01;
      int_stat <= 4'h0;
      int_en   <= 4'h0;
      scratch  <= 32'h0;
    end
    else begin
      if (wr && paddr == 8'h00) ctrl <= {pwdata[7:4], 1'b0, pwdata[2:0]};
      if (wr && paddr == 8'h04 && bug == 3) id <= pwdata[15:0];             // BUG=3: RO is writable
      if (wr && hit_int_en)     int_en  <= pwdata[3:0];
      if (wr && paddr == 8'h1C) scratch <= pwdata;
      if (wr && paddr[7:6] == 2'b01) mem[paddr[5:2]] <= pwdata;
      if (wr && paddr == 8'h08)
        int_stat <= (bug == 2) ? (pwdata[3:0] | ev)                           // BUG=2: acts as RW
                               : ((int_stat & ~pwdata[3:0]) | ev);            // W1C; a new event wins
      else
        int_stat <= int_stat | ev;
    end

  always_comb
    case (paddr)
      8'h00:   prdata = {24'h0, ctrl};
      8'h04:   prdata = {16'h0, id};
      8'h08:   prdata = {28'h0, int_stat};
      8'h0C:   prdata = {28'h0, int_en};
      8'h1C:   prdata = scratch;
      8'h80:   prdata = ch0_src;
      8'h84:   prdata = {16'h0, ch0_len};
      8'h90:   prdata = ch1_src;
      8'h94:   prdata = {16'h0, ch1_len};
      default: prdata = (paddr[7:6] == 2'b01) ? mem[paddr[5:2]] : 32'h0;
    endcase
endmodule

// ---------------------------------------------------------------------------
// Interface
// ---------------------------------------------------------------------------
interface tav_apb_if (input logic pclk);
  logic        presetn, psel, penable, pwrite, pready, pslverr;
  logic [7:0]  paddr;
  logic [31:0] pwdata, prdata;
  logic [3:0]  ev;                     // hardware interrupt events, driven by the test
endinterface

// The test sets tav_phase so every error and every bus access knows which phase it belongs to.
int tav_phase = 0;
int tav_n_rd[6], tav_n_wr[6];          // APB reads and writes seen by the monitor, per phase

// ---------------------------------------------------------------------------
// RAL model: fields -> registers -> block -> map, written from the register map above
// ---------------------------------------------------------------------------
class tav_ctrl_reg extends uvm_reg;
  `uvm_object_utils(tav_ctrl_reg)
  rand uvm_reg_field en, mode, prescale;
  function new(string name = "tav_ctrl_reg");
    super.new(name, 32, UVM_NO_COVERAGE);              // 32 bits wide, no coverage model
  endfunction
  virtual function void build();
    en       = uvm_reg_field::type_id::create("en");
    mode     = uvm_reg_field::type_id::create("mode");
    prescale = uvm_reg_field::type_id::create("prescale");
    //                  parent size lsb access volatile reset has_reset is_rand indiv
    en.configure      (this, 1,   0,  "RW",  0,       1'h0, 1,        1,      0);
    mode.configure    (this, 2,   1,  "RW",  0,       2'h0, 1,        1,      0);
    prescale.configure(this, 4,   4,  "RW",  0,       4'h4, 1,        1,      0);
  endfunction
endclass

class tav_id_reg extends uvm_reg;
  `uvm_object_utils(tav_id_reg)
  uvm_reg_field id;
  function new(string name = "tav_id_reg");
    super.new(name, 32, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    id = uvm_reg_field::type_id::create("id");
    id.configure(this, 16, 0, "RO", 0, 16'h5A01, 1, 0, 0);
  endfunction
endclass

class tav_int_stat_reg extends uvm_reg;
  `uvm_object_utils(tav_int_stat_reg)
  uvm_reg_field pending;
  function new(string name = "tav_int_stat_reg");
    super.new(name, 32, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    pending = uvm_reg_field::type_id::create("pending");
    // volatile = 1: hardware can set these bits at any time, so the mirror cannot predict them
    pending.configure(this, 4, 0, "W1C", 1, 4'h0, 1, 0, 0);
  endfunction
endclass

class tav_int_en_reg extends uvm_reg;
  `uvm_object_utils(tav_int_en_reg)
  rand uvm_reg_field enable;
  function new(string name = "tav_int_en_reg");
    super.new(name, 32, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    enable = uvm_reg_field::type_id::create("enable");
    enable.configure(this, 4, 0, "RW", 0, 4'h0, 1, 1, 0);
  endfunction
endclass

class tav_scratch_reg extends uvm_reg;
  `uvm_object_utils(tav_scratch_reg)
  rand uvm_reg_field data;
  function new(string name = "tav_scratch_reg");
    super.new(name, 32, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    data = uvm_reg_field::type_id::create("data");
    data.configure(this, 32, 0, "RW", 0, 32'h0, 1, 1, 0);
  endfunction
endclass

class tav_buf_mem extends uvm_mem;
  `uvm_object_utils(tav_buf_mem)
  function new(string name = "tav_buf_mem");
    super.new(name, 16, 32, "RW", UVM_NO_COVERAGE);   // 16 words of 32 bits
  endfunction
endclass

class tav_reg_block extends uvm_reg_block;
  `uvm_object_utils(tav_reg_block)
  rand tav_ctrl_reg     ctrl;
  rand tav_id_reg       id;
  rand tav_int_stat_reg int_stat;
  rand tav_int_en_reg   int_en;
  rand tav_scratch_reg  scratch;
  tav_buf_mem           buffer;
  function new(string name = "tav_reg_block");
    super.new(name, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    // the map: base address 0, 4-byte bus, little endian
    default_map = create_map("default_map", 'h0, 4, UVM_LITTLE_ENDIAN);
    // where the design lives, for peek and poke
    add_hdl_path("tav_top.dut", "RTL");

    ctrl     = tav_ctrl_reg::type_id::create("ctrl");
    id       = tav_id_reg::type_id::create("id");
    int_stat = tav_int_stat_reg::type_id::create("int_stat");
    int_en   = tav_int_en_reg::type_id::create("int_en");
    scratch  = tav_scratch_reg::type_id::create("scratch");
    buffer   = tav_buf_mem::type_id::create("buffer");

    ctrl.configure(this);     ctrl.build();     ctrl.add_hdl_path_slice("ctrl", 0, 8);
    id.configure(this);       id.build();       id.add_hdl_path_slice("id", 0, 16);
    int_stat.configure(this); int_stat.build(); int_stat.add_hdl_path_slice("int_stat", 0, 4);
    int_en.configure(this);   int_en.build();   int_en.add_hdl_path_slice("int_en", 0, 4);
    scratch.configure(this);  scratch.build();  scratch.add_hdl_path_slice("scratch", 0, 32);
    buffer.configure(this);   buffer.add_hdl_path_slice("mem", 0, 32);    // backdoor: mem[offset]

    default_map.add_reg(ctrl,     'h00, "RW");
    default_map.add_reg(id,       'h04, "RW");
    default_map.add_reg(int_stat, 'h08, "RW");
    default_map.add_reg(int_en,   'h0C, "RW");
    default_map.add_reg(scratch,  'h1C, "RW");
    default_map.add_mem(buffer,   'h40, "RW");          // words at 0x40, 0x44, ... 0x7C
    lock_model();                                      // no more changes; addresses are now fixed
  endfunction
endclass

// ---------------------------------------------------------------------------
// A second IP block: two DMA channels, each a register file, reachable through two maps
// ---------------------------------------------------------------------------
class tav_dma_src_reg extends uvm_reg;
  `uvm_object_utils(tav_dma_src_reg)
  rand uvm_reg_field data;
  function new(string name = "tav_dma_src_reg");
    super.new(name, 32, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    data = uvm_reg_field::type_id::create("data");
    data.configure(this, 32, 0, "RW", 0, 32'h0, 1, 1, 0);
  endfunction
endclass

class tav_dma_len_reg extends uvm_reg;
  `uvm_object_utils(tav_dma_len_reg)
  rand uvm_reg_field len;
  function new(string name = "tav_dma_len_reg");
    super.new(name, 32, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    len = uvm_reg_field::type_id::create("len");
    len.configure(this, 16, 0, "RW", 0, 16'h0040, 1, 1, 0);
  endfunction
endclass

// A register file groups the registers that repeat once per channel. It adds one level to the
// register's name (dma.ch1.len) and to its HDL path (tav_top.dut.u_ch1.len).
class tav_dma_chan_rf extends uvm_reg_file;
  `uvm_object_utils(tav_dma_chan_rf)
  rand tav_dma_src_reg src;
  rand tav_dma_len_reg len;
  function new(string name = "tav_dma_chan_rf");
    super.new(name);
  endfunction
  virtual function void build();
    src = tav_dma_src_reg::type_id::create("src");
    len = tav_dma_len_reg::type_id::create("len");
    src.configure(get_block(), this);  src.build();  src.add_hdl_path_slice("src", 0, 32);
    len.configure(get_block(), this);  len.build();  len.add_hdl_path_slice("len", 0, 16);
  endfunction
  // place both registers in a map, starting at this register file's offset
  function void tav_map(uvm_reg_map mp, uvm_reg_addr_t offset);
    mp.add_reg(src, offset + 'h0, "RW");
    mp.add_reg(len, offset + 'h4, "RW");
  endfunction
endclass

// Two maps over the same registers: the CPU reaches them through cpu_map, a debug port through
// dbg_map. In this bench both maps end up on the same APB agent.
class tav_dma_block extends uvm_reg_block;
  `uvm_object_utils(tav_dma_block)
  rand tav_dma_chan_rf ch0, ch1;
  uvm_reg_map          cpu_map, dbg_map;
  function new(string name = "tav_dma_block");
    super.new(name, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    cpu_map = create_map("cpu_map", 'h0, 4, UVM_LITTLE_ENDIAN);   // the first map is the default_map
    dbg_map = create_map("dbg_map", 'h0, 4, UVM_LITTLE_ENDIAN);
    add_hdl_path("tav_top.dut", "RTL");
    ch0 = tav_dma_chan_rf::type_id::create("ch0");
    ch1 = tav_dma_chan_rf::type_id::create("ch1");
    ch0.configure(this, null, "u_ch0");  ch0.build();
    ch1.configure(this, null, "u_ch1");  ch1.build();
    ch0.tav_map(cpu_map, 'h00);  ch1.tav_map(cpu_map, 'h10);
    ch0.tav_map(dbg_map, 'h00);  ch1.tav_map(dbg_map, 'h10);
  endfunction
endclass

// ---------------------------------------------------------------------------
// The chip-level block: two IP blocks, each map added as a sub-map at its base address
// ---------------------------------------------------------------------------
class tav_chip_block extends uvm_reg_block;
  `uvm_object_utils(tav_chip_block)
  rand tav_reg_block ip;      // CTRL, ID, INT_STAT, INT_EN, SCRATCH, BUF at base 0x00
  rand tav_dma_block dma;     // two DMA channels at base 0x80
  uvm_reg_map        dbg_map;
  function new(string name = "tav_chip_block");
    super.new(name, UVM_NO_COVERAGE);
  endfunction
  virtual function void build();
    default_map = create_map("default_map", 'h0, 4, UVM_LITTLE_ENDIAN);
    dbg_map     = create_map("dbg_map",     'h0, 4, UVM_LITTLE_ENDIAN);
    set_hdl_path_root("", "RTL");            // each IP block carries its own full HDL path
    ip  = tav_reg_block::type_id::create("ip");
    dma = tav_dma_block::type_id::create("dma");
    ip.configure(this);   ip.build();
    dma.configure(this);  dma.build();
    default_map.add_submap(ip.default_map, 'h00);
    default_map.add_submap(dma.cpu_map,    'h80);
    dbg_map.add_submap(dma.dbg_map,        'h80);
    lock_model();
  endfunction
endclass

// ---------------------------------------------------------------------------
// APB agent: item, driver, monitor
// ---------------------------------------------------------------------------
class tav_apb_item extends uvm_sequence_item;
  `uvm_object_utils(tav_apb_item)
  bit [7:0]  addr;
  bit [31:0] data;
  bit        write;
  bit        slverr;
  function new(string name = "tav_apb_item");
    super.new(name);
  endfunction
endclass

class tav_apb_driver extends uvm_driver #(tav_apb_item);
  `uvm_component_utils(tav_apb_driver)
  virtual tav_apb_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 tav_apb_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no virtual interface")
  endfunction
  task run_phase(uvm_phase phase);
    vif.psel <= 0; vif.penable <= 0; vif.pwrite <= 0; vif.paddr <= 0; vif.pwdata <= 0;
    forever begin
      seq_item_port.get_next_item(req);
      @(posedge vif.pclk);                                   // SETUP: select, address, direction
      vif.psel   <= 1; vif.penable <= 0; vif.pwrite <= req.write;
      vif.paddr  <= req.addr; vif.pwdata <= req.write ? req.data : 32'h0;
      @(posedge vif.pclk);                                   // ACCESS: enable
      vif.penable <= 1;
      do @(posedge vif.pclk); while (vif.pready !== 1'b1);   // transfer ends when PREADY is high
      if (!req.write) req.data = vif.prdata;
      req.slverr = vif.pslverr;
      vif.psel <= 0; vif.penable <= 0;
      #1 seq_item_port.item_done();                          // after the monitor has seen it
    end
  endtask
endclass

class tav_apb_monitor extends uvm_monitor;
  `uvm_component_utils(tav_apb_monitor)
  virtual tav_apb_if vif;
  uvm_analysis_port #(tav_apb_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 tav_apb_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no virtual interface")
  endfunction
  task run_phase(uvm_phase phase);
    tav_apb_item t;
    forever begin
      @(posedge vif.pclk);
      if (vif.presetn === 1'b1 && vif.psel === 1'b1 && vif.penable === 1'b1 && vif.pready === 1'b1) begin
        t = tav_apb_item::type_id::create("t");
        t.addr   = vif.paddr;
        t.write  = vif.pwrite;
        t.data   = vif.pwrite ? vif.pwdata : vif.prdata;
        t.slverr = vif.pslverr;
        if (t.write) tav_n_wr[tav_phase]++; else tav_n_rd[tav_phase]++;
        ap.write(t);
      end
    end
  endtask
endclass

class tav_apb_agent extends uvm_agent;
  `uvm_component_utils(tav_apb_agent)
  uvm_sequencer #(tav_apb_item) sqr;
  tav_apb_driver                drv;
  tav_apb_monitor               mon;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    sqr = uvm_sequencer#(tav_apb_item)::type_id::create("sqr", this);
    drv = tav_apb_driver::type_id::create("drv", this);
    mon = tav_apb_monitor::type_id::create("mon", this);
  endfunction
  function void connect_phase(uvm_phase phase);
    drv.seq_item_port.connect(sqr.seq_item_export);
  endfunction
endclass

// ---------------------------------------------------------------------------
// Adapter: RAL's generic read/write <-> one APB transfer
// ---------------------------------------------------------------------------
class tav_apb_adapter extends uvm_reg_adapter;
  `uvm_object_utils(tav_apb_adapter)
  function new(string name = "tav_apb_adapter");
    super.new(name);
    supports_byte_enable = 0;          // APB3 has no byte strobes
    provides_responses   = 0;          // the driver puts read data into the same item
  endfunction
  virtual function uvm_sequence_item reg2bus(const ref uvm_reg_bus_op rw);
    tav_apb_item item = tav_apb_item::type_id::create("item");
    item.addr  = rw.addr;
    item.write = (rw.kind == UVM_WRITE || rw.kind == UVM_BURST_WRITE);   // bursts arrive as UVM_BURST_WRITE
    item.data  = rw.data;
    return item;
  endfunction
  virtual function void bus2reg(uvm_sequence_item bus_item, ref uvm_reg_bus_op rw);
    tav_apb_item item;
    if (!$cast(item, bus_item)) `uvm_fatal("ADAPTER", "not a tav_apb_item")
    rw.kind   = item.write ? UVM_WRITE : UVM_READ;
    rw.addr   = item.addr;
    rw.data   = item.data;
    rw.status = item.slverr ? UVM_NOT_OK : UVM_IS_OK;
  endfunction
endclass

// ---------------------------------------------------------------------------
// Report catcher: counts errors per phase and shows only the first two of each phase
// ---------------------------------------------------------------------------
class tav_err_catcher extends uvm_report_catcher;
  int n_err[6], shown[6];
  function new(string name = "tav_err_catcher");
    super.new(name);
  endfunction
  function action_e catch();
    if (get_severity() != UVM_ERROR) return THROW;
    n_err[tav_phase]++;
    if (shown[tav_phase] < 2) begin
      shown[tav_phase]++;
      set_message({$sformatf("[phase %0d] ", tav_phase), get_message()});
      return THROW;
    end
    return CAUGHT;
  endfunction
endclass

// ---------------------------------------------------------------------------
// Register callback: UVM calls post_write() after every write to the register it is added to
// ---------------------------------------------------------------------------
class tav_ctrl_cb extends uvm_reg_cbs;
  `uvm_object_utils(tav_ctrl_cb)
  static int n_writes;
  function new(string name = "tav_ctrl_cb");
    super.new(name);
  endfunction
  virtual task post_write(uvm_reg_item rw);
    n_writes++;
    if (tav_phase == 0)
      `uvm_info("CALLBACK", $sformatf("   post_write: CTRL written with 0x%0h through the %s (write %0d)",
                rw.value[0], rw.path == UVM_BACKDOOR ? "backdoor" : "frontdoor", n_writes), UVM_NONE)
  endtask
endclass

// ---------------------------------------------------------------------------
// Register sequences
// ---------------------------------------------------------------------------
class tav_base_reg_seq extends uvm_reg_sequence;
  `uvm_object_utils(tav_base_reg_seq)
  tav_reg_block      rm;
  virtual tav_apb_if vif;
  function new(string name = "tav_base_reg_seq");
    super.new(name);
  endfunction
  // frontdoor read, compared with what the spec says
  task tav_expect(uvm_reg r, uvm_reg_data_t exp, string why);
    uvm_status_e   status;
    uvm_reg_data_t got;
    r.read(status, got);
    if (got !== exp)
      `uvm_error("TAV_CHECK", $sformatf("%s: %s read 0x%0h, expected 0x%0h", why, r.get_name(), got, exp))
  endtask
  // one-cycle pulse on the hardware event inputs
  task pulse(bit [3:0] e);
    @(negedge vif.pclk) vif.ev = e;
    @(negedge vif.pclk) vif.ev = 0;
  endtask
endclass

// A plain APB write that does not go through the register model,
// like a write from another bus master or from embedded software
class tav_apb_raw_write_seq extends uvm_sequence #(tav_apb_item);
  `uvm_object_utils(tav_apb_raw_write_seq)
  bit [7:0]  addr;
  bit [31:0] data;
  function new(string name = "tav_apb_raw_write_seq");
    super.new(name);
  endfunction
  task body();
    req = tav_apb_item::type_id::create("req");
    start_item(req);
    req.addr = addr; req.data = data; req.write = 1;
    finish_item(req);
  endtask
endclass

// Phase 0: every RAL method once. Each step prints the model (desired and mirrored values)
// and the design value read straight from the RTL with uvm_hdl_read().
class tav_tour_seq extends tav_base_reg_seq;
  `uvm_object_utils(tav_tour_seq)
  uvm_sequencer #(tav_apb_item) sqr;
  tav_chip_block                chip;                  // the whole model: ip, dma and both maps
  int w0, r0;                                          // bus counters at the start of a step
  function new(string name = "tav_tour_seq");
    super.new(name);
  endfunction
  function void step(string title);
    w0 = tav_n_wr[0]; r0 = tav_n_rd[0];
    `uvm_info("TOUR", {"---- ", title}, UVM_NONE)
  endfunction
  function int n_dbg_regs();
    uvm_reg r[$];
    chip.dbg_map.get_registers(r);
    return r.size();
  endfunction
  function void bus();
    `uvm_info("TOUR", $sformatf("   APB traffic in this step: %0d write(s), %0d read(s)",
              tav_n_wr[0] - w0, tav_n_rd[0] - r0), UVM_NONE)
  endfunction
  // Prints the model and the design side by side. The design value comes from uvm_hdl_read(),
  // not peek(): peek() would also update the model (step 5 shows why that matters).
  function void show(uvm_reg r, string hdl = "");
    uvm_hdl_data_t dut;
    if (hdl == "") hdl = {"tav_top.dut.", r.get_name()};
    if (!uvm_hdl_read(hdl, dut)) `uvm_error("TOUR", "uvm_hdl_read failed")
    `uvm_info("TOUR", $sformatf("   %-8s desired=0x%08h  mirrored=0x%08h  design=0x%08h",
              r.get_name(), r.get(), r.get_mirrored_value(), dut[31:0]), UVM_NONE)
  endfunction
  task body();
    uvm_status_e          status;
    uvm_reg_data_t        v;
    uvm_reg_data_t        wburst[], rburst[];
    tav_apb_raw_write_seq raw;
    uvm_hdl_path_concat   hp[$];
    uvm_reg               regs[$];
    uvm_mem               mems[$];

    step("0. What the model looks like: every register in the chip's default_map, with every field");
    chip.default_map.get_registers(regs);
    foreach (regs[i]) begin
      uvm_reg_field flds[$];
      string        line;
      regs[i].get_fields(flds);
      line = $sformatf("   0x%02h  %-22s", regs[i].get_address(chip.default_map), regs[i].get_full_name());
      foreach (flds[f])
        line = {line, $sformatf(" | %s [%0d:%0d] %s reset 0x%0h%s", flds[f].get_name(),
                flds[f].get_lsb_pos() + flds[f].get_n_bits() - 1, flds[f].get_lsb_pos(),
                flds[f].get_access(), flds[f].get_reset(), flds[f].is_volatile() ? " volatile" : "")};
      `uvm_info("TOUR", line, UVM_NONE)
    end
    chip.default_map.get_memories(mems);
    foreach (mems[i])
      `uvm_info("TOUR", $sformatf("   0x%02h  %-22s | memory, %0d words of %0d bits, %s", mems[i].get_address(0, chip.default_map),
                mems[i].get_full_name(), mems[i].get_size(), mems[i].get_n_bits(), mems[i].get_access()), UVM_NONE)
    `uvm_info("TOUR", $sformatf("   default_map: %0d-byte bus, base 0x%0h; dbg_map holds %0d registers",
              chip.default_map.get_n_bytes(), chip.default_map.get_base_addr(), n_dbg_regs()), UVM_NONE)

    step("1. Right after build() the model holds 0, not the reset values. regmodel.reset() loads them");
    show(rm.ctrl);
    rm.reset();
    show(rm.ctrl);
    `uvm_info("TOUR", $sformatf("   ctrl.get_reset() = 0x%0h", rm.ctrl.get_reset()), UVM_NONE)
    tav_expect(rm.ctrl, 'h40, "step 1, read after reset");
    bus();

    step("2. write() and read(): one APB transfer each, the mirror follows");
    rm.ctrl.write(status, 'h13);
    show(rm.ctrl);
    tav_expect(rm.ctrl, 'h13, "step 2, read back");
    bus();

    step("3. Field write: ctrl.prescale.write(9). APB cannot write part of a register, so RAL writes all of CTRL");
    rm.ctrl.prescale.write(status, 'h9);
    show(rm.ctrl);
    tav_expect(rm.ctrl, 'h93, "step 3, read back");
    bus();

    step("4. set() changes only the model (desired); ctrl.update() writes it when desired != mirrored");
    rm.ctrl.mode.set(2'b10);
    show(rm.ctrl);
    `uvm_info("TOUR", $sformatf("   ctrl.needs_update() = %0d", rm.ctrl.needs_update()), UVM_NONE)
    rm.ctrl.update(status);
    show(rm.ctrl);
    `uvm_info("TOUR", $sformatf("   ctrl.needs_update() = %0d", rm.ctrl.needs_update()), UVM_NONE)
    bus();

    step("5. Careful: peek() is not only a look. It also updates desired and mirrored from the design");
    rm.ctrl.mode.set(2'b01);
    show(rm.ctrl);
    rm.ctrl.peek(status, v);
    `uvm_info("TOUR", $sformatf("   ctrl.peek() = 0x%0h", v), UVM_NONE)
    show(rm.ctrl);
    `uvm_info("TOUR", (rm.ctrl.mode.get() == 2'b01) ? "   the set(2'b01) above survived the peek()"
                       : "   the set(2'b01) above is gone: peek() overwrote the desired value", UVM_NONE)
    bus();

    step("6. randomize() the model, then regmodel.update(): writes every register whose desired value changed");
    if (!rm.randomize()) `uvm_error("TOUR", "randomize failed")
    show(rm.ctrl); show(rm.int_en); show(rm.scratch);
    rm.update(status);
    rm.ctrl.mirror(status, UVM_CHECK);
    rm.int_en.mirror(status, UVM_CHECK);
    rm.scratch.mirror(status, UVM_CHECK);
    show(rm.ctrl); show(rm.int_en); show(rm.scratch);
    bus();

    step("7. Hardware sets INT_STAT behind the model's back; mirror() reads it and refreshes the mirror");
    pulse(4'b0011);
    show(rm.int_stat);
    rm.int_stat.mirror(status, UVM_NO_CHECK);
    show(rm.int_stat);
    rm.int_stat.write(status, 'h1);                    // W1C: clears bit 0 only
    show(rm.int_stat);
    tav_expect(rm.int_stat, 'h2, "step 7, after writing 1 to bit 0");
    bus();

    step("8. predict() tells the model a value without touching the bus");
    pulse(4'b0100);
    void'(rm.int_stat.predict('h6));
    show(rm.int_stat);
    bus();
    rm.int_stat.write(status, 'h6);                    // clear both
    tav_expect(rm.int_stat, 'h0, "step 8, after writing 1 to bits 1 and 2");

    step("9. Backdoor: poke() and peek() reach the design directly, in zero time, no APB");
    rm.scratch.poke(status, 'hDEAD_BEEF);
    show(rm.scratch);
    rm.id.peek(status, v);
    `uvm_info("TOUR", $sformatf("   id.peek() = 0x%0h", v), UVM_NONE)
    rm.ctrl.read(status, v, UVM_BACKDOOR);
    `uvm_info("TOUR", $sformatf("   ctrl.read(UVM_BACKDOOR) = 0x%0h", v), UVM_NONE)
    bus();
    tav_expect(rm.scratch, 'hDEAD_BEEF, "step 9, frontdoor read of the poked value");

    step("10. Explicit prediction: a raw APB write, not from the model, still updates the mirror");
    raw = tav_apb_raw_write_seq::type_id::create("raw");
    raw.addr = 'h1C; raw.data = 'h0000_CAFE;
    raw.start(sqr);
    show(rm.scratch);
    bus();

    step("11. Hardware reset, then regmodel.reset(): model and design both back to reset values");
    @(negedge vif.pclk) vif.presetn = 0;
    repeat (2) @(negedge vif.pclk);
    vif.presetn = 1;
    rm.reset();
    show(rm.ctrl); show(rm.scratch);
    rm.ctrl.mirror(status, UVM_CHECK);
    rm.int_en.mirror(status, UVM_CHECK);
    rm.scratch.mirror(status, UVM_CHECK);
    bus();

    step("12. Memory (uvm_mem): read and write by offset, burst access, peek and poke. A memory has no mirror");
    rm.buffer.write(status, 3, 'h1111_0003);
    rm.buffer.read(status, 3, v);
    `uvm_info("TOUR", $sformatf("   buffer.write(3, 0x11110003), then buffer.read(3) = 0x%0h", v), UVM_NONE)
    wburst = '{'hA0, 'hA1, 'hA2, 'hA3};
    rburst = new[4];
    rm.buffer.burst_write(status, 8, wburst);
    rm.buffer.burst_read(status, 8, rburst);
    `uvm_info("TOUR", $sformatf("   buffer.burst_write(8, 4 words), burst_read(8) = %p", rburst), UVM_NONE)
    rm.buffer.poke(status, 15, 'h0000_BEEF);
    rm.buffer.peek(status, 15, v);
    `uvm_info("TOUR", $sformatf("   buffer.poke(15, 0xBEEF), then buffer.peek(15) = 0x%0h", v), UVM_NONE)
    bus();
    rm.buffer.read(status, 15, v);
    if (v !== 'h0000_BEEF) `uvm_error("TAV_CHECK", $sformatf("step 12: buffer[15] read 0x%0h, expected 0xbeef", v))

    step("13. Callbacks: tav_ctrl_cb::post_write() ran after every CTRL write above (the CALLBACK lines)");
    `uvm_info("TOUR", $sformatf("   CTRL writes seen by the callback so far: %0d", tav_ctrl_cb::n_writes), UVM_NONE)

    step("14. Register file: dma.ch1 groups SRC and LEN, adding a level to the name and the HDL path");
    chip.dma.ch1.len.write(status, 'h0200);
    show(chip.dma.ch1.len, "tav_top.dut.u_ch1.len");
    `uvm_info("TOUR", $sformatf("   full name: %s", chip.dma.ch1.len.get_full_name()), UVM_NONE)
    chip.dma.ch1.len.get_full_hdl_path(hp);
    `uvm_info("TOUR", $sformatf("   HDL path:  %s", hp[0].slices[0].path), UVM_NONE)
    tav_expect(chip.dma.ch1.len, 'h0200, "step 14, read back");
    bus();

    step("15. Sub-blocks: one chip block holds both IP blocks; each IP map sits in the chip map at its base");
    regs.delete();
    chip.get_registers(regs);
    `uvm_info("TOUR", $sformatf("   registers in the chip model: %0d", regs.size()), UVM_NONE)
    `uvm_info("TOUR", $sformatf("   ip.ctrl at 0x%02h, dma.ch0.src at 0x%02h, dma.ch1.len at 0x%02h in default_map",
              chip.ip.ctrl.get_address(chip.default_map), chip.dma.ch0.src.get_address(chip.default_map),
              chip.dma.ch1.len.get_address(chip.default_map)), UVM_NONE)

    step("16. Two maps: write DMA ch0 SRC through the debug map, read it back through the CPU map");
    chip.dma.ch0.src.write(status, 'hA5A5_0001, UVM_FRONTDOOR, chip.dbg_map);
    chip.dma.ch0.src.read(status, v, UVM_FRONTDOOR, chip.default_map);
    `uvm_info("TOUR", $sformatf("   write via dbg_map, read via default_map = 0x%0h", v), UVM_NONE)
    if (v !== 'hA5A5_0001) `uvm_error("TAV_CHECK", $sformatf("step 16: read 0x%0h, expected 0xa5a50001", v))
    bus();

    step("17. hw_reset_seq only compares, it never resets the design. So reset design and model before it runs");
    @(negedge vif.pclk) vif.presetn = 0;
    repeat (2) @(negedge vif.pclk);
    vif.presetn = 1;
    chip.reset();
    show(chip.dma.ch0.src, "tav_top.dut.u_ch0.src");
    show(chip.dma.ch1.len, "tav_top.dut.u_ch1.len");
    chip.dma.ch0.src.mirror(status, UVM_CHECK);
    chip.dma.ch1.len.mirror(status, UVM_CHECK);
    bus();
  endtask
endclass

// Phase 4: does INT_STAT really set on events and clear only the bits written as 1?
class tav_w1c_seq extends tav_base_reg_seq;
  `uvm_object_utils(tav_w1c_seq)
  function new(string name = "tav_w1c_seq");
    super.new(name);
  endfunction
  task body();
    uvm_status_e status;
    rm.int_stat.write(status, 4'hF);                   // start clean
    tav_expect(rm.int_stat, 4'h0, "after writing 1 to every bit");
    pulse(4'b0101);
    tav_expect(rm.int_stat, 4'h5, "after events 0 and 2");
    rm.int_stat.write(status, 4'b0001);
    tav_expect(rm.int_stat, 4'h4, "after writing 1 to bit 0 only");
    rm.int_stat.write(status, 4'b0000);
    tav_expect(rm.int_stat, 4'h4, "after writing 0 (must change nothing)");
    rm.int_stat.write(status, 4'b0100);
    tav_expect(rm.int_stat, 4'h0, "after writing 1 to bit 2");
  endtask
endclass

// Phase 5: does any write land somewhere it should not?
class tav_alias_seq extends tav_base_reg_seq;
  `uvm_object_utils(tav_alias_seq)
  function new(string name = "tav_alias_seq");
    super.new(name);
  endfunction
  task body();
    uvm_status_e status;
    uvm_reg      regs[$];
    rm.ctrl.write(status,    32'h35);
    rm.int_en.write(status,  32'hA);
    rm.scratch.write(status, 32'h1234_5678);
    rm.get_registers(regs);
    foreach (regs[i]) regs[i].mirror(status, UVM_CHECK);   // every register, against the model
  endtask
endclass

// ---------------------------------------------------------------------------
// Environment and test
// ---------------------------------------------------------------------------
class tav_env extends uvm_env;
  `uvm_component_utils(tav_env)
  tav_chip_block                    regmodel;
  tav_apb_adapter                   adapter;
  uvm_reg_predictor #(tav_apb_item) predictor;
  tav_apb_agent                     agent;
  tav_ctrl_cb                       cb;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    regmodel  = tav_chip_block::type_id::create("regmodel");
    regmodel.build();
    adapter   = tav_apb_adapter::type_id::create("adapter");
    predictor = uvm_reg_predictor#(tav_apb_item)::type_id::create("predictor", this);
    agent     = tav_apb_agent::type_id::create("agent", this);
  endfunction
  function void connect_phase(uvm_phase phase);
    regmodel.default_map.set_sequencer(agent.sqr, adapter);   // model accesses go to this sequencer
    regmodel.dbg_map.set_sequencer(agent.sqr, adapter);       // the debug map uses the same APB agent here
    regmodel.default_map.set_auto_predict(0);                 // the predictor keeps the mirror
    regmodel.dbg_map.set_auto_predict(0);
    predictor.map     = regmodel.default_map;
    predictor.adapter = adapter;
    agent.mon.ap.connect(predictor.bus_in);                   // every APB transfer, from anyone
    cb = tav_ctrl_cb::type_id::create("cb");
    uvm_reg_cb::add(regmodel.ip.ctrl, cb);                        // hook the callback onto CTRL
  endfunction
endclass

class tav_ral_test extends uvm_test;
  `uvm_component_utils(tav_ral_test)
  tav_env            env;
  tav_err_catcher    catcher;
  virtual tav_apb_if vif;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    env = tav_env::type_id::create("env", this);
    catcher = new("catcher");
    uvm_report_cb::add(null, catcher);
    if (!uvm_config_db#(virtual tav_apb_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no virtual interface")
  endfunction
  task run_phase(uvm_phase phase);
    tav_tour_seq         s0 = tav_tour_seq::type_id::create("s0");
    uvm_reg_hw_reset_seq s1 = uvm_reg_hw_reset_seq::type_id::create("s1");
    uvm_reg_bit_bash_seq s2 = uvm_reg_bit_bash_seq::type_id::create("s2");
    uvm_reg_mem_hdl_paths_seq s3a = uvm_reg_mem_hdl_paths_seq::type_id::create("s3a");
    uvm_reg_access_seq        s3b = uvm_reg_access_seq::type_id::create("s3b");
    uvm_mem_walk_seq          s3c = uvm_mem_walk_seq::type_id::create("s3c");
    uvm_reg_mem_shared_access_seq s3d = uvm_reg_mem_shared_access_seq::type_id::create("s3d");
    tav_w1c_seq          s4 = tav_w1c_seq::type_id::create("s4");
    tav_alias_seq        s5 = tav_alias_seq::type_id::create("s5");
    phase.raise_objection(this);
    wait (vif.presetn === 1'b1);
    repeat (2) @(posedge vif.pclk);
    s0.rm = env.regmodel.ip; s0.chip = env.regmodel; s0.vif = vif; s0.sqr = env.agent.sqr;
    s1.model = env.regmodel;
    s2.model = env.regmodel;
    s3a.model = env.regmodel; s3b.model = env.regmodel; s3c.model = env.regmodel; s3d.model = env.regmodel;
    s4.rm = env.regmodel.ip; s4.vif = vif;
    s5.rm = env.regmodel.ip; s5.vif = vif;
    tav_phase = 0; s0.start(null);
    tav_phase = 1; s1.start(null);
    tav_phase = 2; s2.start(null);
    tav_phase = 3; s3a.start(null); s3b.start(null); s3c.start(null); s3d.start(null);
    tav_phase = 4; s4.start(null);
    tav_phase = 5; s5.start(null);
    repeat (2) @(posedge vif.pclk);
    tav_print_results();
    phase.drop_objection(this);
  endtask
  function void tav_print_results();
    string nm[6] = '{"tour of the methods      ", "hw_reset (built-in)      ",
                     "bit_bash (built-in)      ", "paths/access/walk/shared ",
                     "W1C (targeted)           ", "alias (targeted)         "};
    for (int p = 0; p <= 5; p++)
      `uvm_info("RESULT", $sformatf("Phase %0d %s: APB reads=%0d writes=%0d errors=%0d -> %s",
                p, nm[p], tav_n_rd[p], tav_n_wr[p], catcher.n_err[p],
                catcher.n_err[p] == 0 ? "PASS" : "FAIL"), UVM_NONE)
  endfunction
endclass

// ---------------------------------------------------------------------------
// Top
// ---------------------------------------------------------------------------
module tav_top;
  logic pclk = 0;
  int   BUG = 0;
  always #5 pclk = ~pclk;

  tav_apb_if vif (pclk);
  tav_apb_regs dut (.pclk(pclk), .presetn(vif.presetn), .psel(vif.psel), .penable(vif.penable),
                    .pwrite(vif.pwrite), .paddr(vif.paddr), .pwdata(vif.pwdata),
                    .prdata(vif.prdata), .pready(vif.pready), .pslverr(vif.pslverr),
                    .ev(vif.ev), .bug(BUG));

  initial begin
    vif.presetn = 0; vif.ev = 0;
    void'($value$plusargs("BUG=%d", BUG));
    `uvm_info("TOP", $sformatf("Running with BUG=%0d", BUG), UVM_NONE)
    uvm_config_db#(virtual tav_apb_if)::set(null, "uvm_test_top*", "vif", vif);
    repeat (3) @(negedge pclk);
    vif.presetn = 1;
  end

  initial run_test("tav_ral_test");
endmodule

Expected output

The clean run ends with these lines (the tour lines are shown step by step above):

UVM_INFO testbench.sv(887) @ 54525: uvm_test_top [RESULT] Phase 0 tour of the methods      : APB reads=23 writes=17 errors=0 -> PASS
UVM_INFO testbench.sv(887) @ 54525: uvm_test_top [RESULT] Phase 1 hw_reset (built-in)      : APB reads=13 writes=0 errors=0 -> PASS
UVM_INFO testbench.sv(887) @ 54525: uvm_test_top [RESULT] Phase 2 bit_bash (built-in)      : APB reads=824 writes=824 errors=0 -> PASS
UVM_INFO testbench.sv(887) @ 54525: uvm_test_top [RESULT] Phase 3 paths/access/walk/shared : APB reads=44 writes=51 errors=0 -> PASS
UVM_INFO testbench.sv(887) @ 54525: uvm_test_top [RESULT] Phase 4 W1C (targeted)           : APB reads=5 writes=4 errors=0 -> PASS
UVM_INFO testbench.sv(887) @ 54525: uvm_test_top [RESULT] Phase 5 alias (targeted)         : APB reads=5 writes=3 errors=0 -> PASS
  • The whole run takes 54,525 ns of simulated time, with 0 errors.
  • The report summary shows four warnings, all expected:
    • two UVM/RSRC/NOREGEX lines at time 0 naming regmodel.ip and regmodel.dma. They appeared once the model got sub-blocks and do not affect any check;
    • the field-write warning from tour step 3;
    • the access test skipping ID.

Common mistakes with UVM RAL

  1. Not calling regmodel.reset() after build(). The model starts at 0. Step 1 of the tour shows desired = mirrored = 0x00000000 while the design holds 0x40. Every check before the first reset() is wrong.
  2. Using peek() just to look. peek() also overwrites desired and mirrored. In my first version, a debug print that used peek() erased every set() and randomize() before update() could send them. Use uvm_hdl_read() for a look that changes nothing.
  3. An adapter that only knows UVM_WRITE. Memory bursts arrive as UVM_BURST_WRITE. My first adapter turned a 4-word burst write into 4 reads, and burst_read returned all zeros.
  4. The wrong access policy. Model a W1C status bit as RW and every write is predicted wrong. Model an RO counter as RW and bit_bash fails on a correct design.
  5. Forgetting volatile, or forgetting what it costs. A field that hardware changes must be volatile, or you get false errors. Once it is volatile, the built-ins stop checking it, so give it a targeted test, like tav_w1c_seq here.
  6. Field writes with a stale mirror. On a bus without byte enables, a field write sends the mirrored values of every other field in the register. If the mirror is stale, you just wrote stale values back.
  7. Not telling the model about a reset. Assert reset mid-test without calling regmodel.reset() and every later check compares against the old values.
  8. Auto prediction with more than one master. The mirror only follows the model’s own accesses. Use a predictor on the monitor, and make sure the monitor publishes before the driver calls item_done().
  9. Expecting the built-ins to find decode bugs. They test one register at a time. An alias that writes two registers at once passed bit_bash and access here.
  10. Peek and poke failing with no clear reason. On VCS, backdoor access needs -debug_access+all (or at least read and write access to those signals). Run uvm_reg_mem_hdl_paths_seq first: it tells you if a path is wrong before a test depends on it.
  11. Running uvm_reg_hw_reset_seq after other traffic. It resets the model, not the design. In an earlier version of this bench the clean run failed it with 2 errors, both registers the tour had just written. Reset the design first, as tour step 17 does.

How do you debug a RAL mismatch?

  1. Read the field line, not just the register line. “Field prescale (regmodel.ip.ctrl[7:4]) mismatch read=4’h0 mirrored=4’h4” tells you which bits and both values.
  2. Ask whether the mirror or the design is wrong. Peek the flop (or uvm_hdl_read it). If the flop matches the mirror but the frontdoor read does not, suspect the read path or the decode. If the flop matches the frontdoor read, the mirror is stale or the model is wrong.
  3. Find who last wrote it. Turn on the predictor and adapter messages (UVM_HIGH or UVM_FULL) and look for a write the model did not expect, from another master, a backdoor poke or an alias.
  4. Check the field’s policy and volatile setting against the spec. A wrong policy gives a consistent, repeatable mismatch on every access.
  5. Check reset. A mismatch on the very first read usually means reset() was not called, or the spec and RTL disagree on the reset value.
  6. For bit_bash failures, read the bit number. “Writing a 0 in bit #0 of register regmodel.ip.id” names the exact bit. The same failure on every bit of a field points at the field’s policy; one bit points at the RTL.
  7. Use the same habit outside RAL. A mirror mismatch is a model-versus-design mismatch, like any scoreboard error. How to find the real cause of a scoreboard mismatch walks through the same steps.

Can AI write a UVM register model? I tested it

The task. Write the RAL model (register classes, memory class and block) from the same register map I used, then drop it into my bench unedited and run the same five VCS runs.

The tool. Claude, in a fresh chat with no access to my bench. I pasted only the prompt below.

This is a smaller version of my AI UVM testbench experiment, this time on the register model alone.

The full prompt.

I need a UVM 1.2 register model (RAL) for this APB register block. Write the register classes, the memory class and the register block in SystemVerilog.

Requirements:
- The block class is named tav_reg_block and extends uvm_reg_block. It must have register handles named ctrl, id, int_stat, int_en and scratch, a memory handle named buffer, and its address map must be default_map (byte addressing, 4-byte bus, little endian, base 0). Call lock_model() at the end of build().
- Every class name starts with tav_.
- Add HDL paths so peek() and poke() work. The design instance is tav_top.dut. The flops are named ctrl [7:0], id [15:0], int_stat [3:0], int_en [3:0] and scratch [31:0], and the memory is an array logic [31:0] mem [16].
- My bench calls regmodel.build(), connects default_map to an APB sequencer through an adapter, and keeps the mirror with a uvm_reg_predictor on the APB monitor (auto prediction off). It uses write, read, field write, set/update, randomize, mirror, predict, peek, poke, memory read/write/burst, a uvm_reg_cbs callback on ctrl, and runs uvm_reg_hw_reset_seq, uvm_reg_bit_bash_seq, uvm_reg_mem_hdl_paths_seq, uvm_reg_access_seq and uvm_mem_walk_seq.

Register map (32-bit registers, byte addresses):
  0x00 CTRL     en [0] RW, mode [2:1] RW, prescale [7:4] RW. Reset 0x40 (prescale = 4).
                Bit 3 and bits [31:8] are unused and read 0.
  0x04 ID       id [15:0] RO, reads 0x5A01. Writes are ignored.
  0x08 INT_STAT pending [3:0] W1C. Hardware sets a bit when its event input pulses;
                software writes 1 to clear it. Reset 0.
  0x0C INT_EN   enable [3:0] RW. Reset 0.
  0x1C SCRATCH  data [31:0] RW. Reset 0.
  0x40-0x7C BUF 16 words x 32 bits of RAM, read and write. No reset value.
  Any other address reads 0, ignores writes and answers with PSLVERR.

The real output. A 179-line package. The field setup matched mine line for line: the same widths, positions, policies and reset values, INT_STAT volatile, ID not randomizable. The block was a little different from mine:

ctrl.configure(this, null, "ctrl");          // tav_top.dut.ctrl[7:0]
...
buffer.configure(this, "mem");               // tav_top.dut.mem[i]
...
// uvm_reg_access_seq cannot test a register with RO fields (it warns and skips)
uvm_resource_db#(bit)::set({"REG::", id.get_full_name()},
                           "NO_REG_ACCESS_TEST", 1, this);

It gave each register a whole-signal HDL path instead of a slice, and it excluded ID from the access test up front. It also added a uvm_register_cb macro I did not need. Its notes were useful: it explained that volatile turns the compare off, and suggested set_compare(UVM_CHECK) on INT_STAT during a window when no events can fire.

How I validated it. I pasted the package into tav_ral_ai_check.sv unedited, with the same design, bench and tests, and ran the same five VCS runs.

The result. I ran the AI model on the previous version of the bench (v5, before step 17 existed). Step 17 only resets the design and the model, so it does not depend on whose model is plugged in.

  • The RESULT lines matched my model’s on that same version in all five runs: the same errors in the same phases, 824 reads and 824 writes in bit_bash, 54,445 ns per run.
  • Both models also showed the same phase 1 failure on the clean run, the one that led me to add step 17. That failure came from the tour order, not from either model.
  • The only difference in the logs: my run prints “has only RO fields … skipping” for ID, and the AI’s does not, because it had already excluded ID.

Where it fell short. Two places, both small for the model and big for the lesson.

  • Its comment says the access test “cannot test a register with RO fields”. The UVM 1.2 source you compile on VCS skips a register only when all of its fields are RO. A register that mixes RO and RW fields is tested. On this map it made no difference; on a real chip, excluding every register with one RO field would quietly drop access testing for many registers.
  • A correct model did not catch more bugs. With its model, the built-ins still missed the W1C bug and the alias, exactly as with mine. The model describes the registers; it does not plan the tests. The AI did not mention that bit_bash and access cannot see an alias between two registers, and that is the gap that let BUG=4 through.

So AI is a good fit for typing out a model from a clear register map, and its output passed every run I gave it. Check it the same way you would check a generator: run the built-ins on a clean design, plant a few bugs, and keep writing the targeted tests yourself.

Interview questions on UVM RAL

  1. What is the difference between the desired and the mirrored value? Desired is what you want the register to hold next; set() and randomize() change it. Mirrored is what the model believes the design holds now; it follows every write, read and prediction.
  2. What is the difference between write() and update()? write() sends a value now. update() sends the desired value only for registers where desired differs from mirrored, so many set() calls cost one bus write per changed register.
  3. What does mirror() do, and when does it not check? It reads the register and updates the mirror. With UVM_CHECK it compares first, except for fields whose compare is off, such as volatile fields.
  4. When would you use backdoor access, and what does it not prove? To load state in zero time, reach states the bus cannot, or check without bus traffic. It does not prove the register is reachable through the bus or that the decode is right.
  5. What are auto, explicit and passive prediction? Auto updates the mirror from the model’s own accesses. Explicit uses a uvm_reg_predictor on the bus monitor, so it sees every master. Passive is the predictor with no sequencer driving.
  6. Why does uvm_reg_bit_bash_seq skip some fields? It skips volatile fields (their compare is off) and write-only fields (you cannot read them back), plus any register you exclude with NO_REG_BIT_BASH_TEST.
  7. What does the adapter do, and what is provides_responses? It converts between uvm_reg_bus_op and the bus item, both ways. provides_responses tells RAL whether the driver returns a separate response item.
  8. How do you reuse a block’s register model at chip level? Instantiate the block inside the chip block, set its HDL path, and add its map to the chip map with add_submap() at its base address.

Practice challenge

  1. Add a new phase that calls regmodel.ip.int_stat.pending.set_compare(UVM_CHECK) and reruns bit_bash on INT_STAT only. Does it now catch BUG=2? Why is this safe here and risky on a real chip?
  2. Add +BUG=5: the SCRATCH register drops bit 31 on writes. Which phase catches it first?
  3. Change INT_STAT to non-volatile and rerun the clean test. Which steps of the tour fail, and why?
  4. In tour step 10, switch to set_auto_predict(1) and disconnect the predictor. What does the mirror say after the raw APB write?
  5. Write a uvm_reg_cbs post_predict callback that models a side effect: writing CTRL.en = 0 clears INT_EN. Test it with mirror(UVM_CHECK).

Frequently asked questions

What is UVM RAL in simple words?

It is a copy of the design’s register map inside the testbench. Tests read and write registers by name, and the copy remembers what each register should hold so reads can be checked automatically.

What is the difference between frontdoor and backdoor access?

Frontdoor goes through the real bus, takes bus cycles and tests the decode. Backdoor goes straight to the flop through its HDL path in zero time and skips the bus, the decode and any side effects.

Why does my register model show 0 right after build()?

build() creates the fields but does not load reset values. Call regmodel.reset(), or run uvm_reg_hw_reset_seq, which calls it for you.

Does peek() change the register model?

Yes. peek() updates both the desired and mirrored values with what it reads. To look at a flop without touching the model, use uvm_hdl_read() on its HDL path.

Why do the built-in register tests skip my interrupt status register?

It is probably volatile. UVM switches the compare off for volatile fields, so hw_reset, bit_bash and access do not check it. Test it with a targeted sequence that compares the read values itself.

Do I need explicit prediction?

If anything other than the model can write the registers, such as a CPU, a DMA or a second bus agent, yes. Otherwise the mirror goes stale and checks fail for the wrong reason.

References

Related lessons

12 Comments

  1. I have gone through most of the topics from “The Art of Verification” where your answers and explanation is very clear. Good work.. Suggested to many of my friends.

  2. HI Im new to UVM but got around 10 years of exp in Val. Would like to start on this. What is hte best way to start assuming Im really bad in OOP?
    Thanks for the suggestions and help

    1. Hi Hafiz,

      The best way to start is to understand SV constructs and how it used in Testbench architecture. UVM having better features but if you know SV you can easily compare those things in UVM Testbench architecture.

      I hope I can able to help !!

      Regards,
      Hardik

Comments are closed.