U18.8.2 Peek Through the Scrambler

HardFixUVM · serverReview the topic

A memory stores every byte scrambled, and its backdoor relies on decode and encode callbacks. Fix the backdoor so peek and poke agree with the bus.

Hardware context: APB register slice with an aliased register pair and a scrambled memory. Write a backdoor for a scrambled memory.

The register slice's 16-byte memory at 40-7C stores each byte with its nibbles swapped and then XORed with its address. The bus hides this: a write stores the scrambled byte, a read unscrambles it. A backdoor sees the stored bytes.

A colleague wrote mem_backdoor to copy the stored bytes and attached an unscramble callback, as the UVM standard describes. peek still returns scrambled bytes. Fix mem_backdoor so that peek and poke use plain values, exactly like read and write through the bus.

Full specification
The code to fix
// The memory's backdoor: the bytes as stored, with an unscramble callback attached.
class unscramble_cb extends uvm_reg_cbs;
  `uvm_object_utils(unscramble_cb)
  int offset;
  function new(string name = "unscramble_cb"); super.new(name); endfunction
  function bit [7:0] swap(bit [7:0] v); return {v[3:0], v[7:4]}; endfunction
  virtual function void decode(ref uvm_reg_data_t data[]); data[0] = swap(8'(data[0]) ^ 8'('h40 + 4 * offset)); endfunction
  virtual function void encode(ref uvm_reg_data_t data[]); data[0] = swap(8'(data[0])) ^ 8'('h40 + 4 * offset); endfunction
endclass

class mem_backdoor extends uvm_reg_backdoor;
  `uvm_object_utils(mem_backdoor)
  unscramble_cb cb;
  function new(string name = "mem_backdoor");
    super.new(name);
    cb = new("unscramble");
    uvm_reg_bd_cb::add(this, cb);
  endfunction
  virtual task write(uvm_reg_item rw);
    cb.offset = rw.get_offset();
    do_pre_write(rw);                          // runs encode
    tb.d0.mem[rw.get_offset()] = rw.get_value(0);
    rw.set_status(UVM_IS_OK);
    do_post_write(rw);
  endtask
  virtual function void read_func(uvm_reg_item rw);
    cb.offset = rw.get_offset();               // the base read runs decode afterwards
    rw.set_value(tb.d0.mem[rw.get_offset()], 0);
    rw.set_status(UVM_IS_OK);
  endfunction
endclass
GivenMeaning
tb.d0.mem[i]The stored byte of word i (byte address 40 + 4i)
Scramblingstored = swap(plain) ^ (40 + 4i), where swap exchanges the two nibbles; so plain = swap(stored ^ (40 + 4i))
rw.get_offset()The word offset of the access, 0 to 15
rw.get_value(0), rw.set_value(v, 0)The value written, or the value to return

The model's memory starts plain 00 everywhere.

Examples

Example 1

Poke, Then Read Through the Bus
Input
poke(2, 5e); read(2); peek(2)
Output
value_0 = 0x5e, value_1 = 0x5e, value_2 = 0x0, value_3 = 0x0, value_4 = 0x0, value_5 = 0x0, value_6 = 0x0, value_7 = 0x0, failed_accesses = 0

poke stores swap(5e) ^ 48 = e5 ^ 48 = ad. The bus read unscrambles it to 5e, and peek must return 5e too.

Example 2

Write Through the Bus, Then Peek
Input
write(5, 3c); peek(5); read(5)
Output
value_0 = 0x3c, value_1 = 0x3c, value_2 = 0x0, value_3 = 0x0, value_4 = 0x0, value_5 = 0x0, value_6 = 0x0, value_7 = 0x0, failed_accesses = 0

The bus stores swap(3c) ^ 54. peek undoes both steps with the same key and returns 3c.

Example 3

Peek Memory Nobody Wrote
Input
peek(0); peek(9); read(15)
Output
value_0 = 0x0, value_1 = 0x0, value_2 = 0x0, value_3 = 0x0, value_4 = 0x0, value_5 = 0x0, value_6 = 0x0, value_7 = 0x0, failed_accesses = 0

Every word holds plain 00, stored as its own key. Unused value slots read 00.

Constraints and Contract

  • Keep the class name; the bench installs it with regs.mem.set_backdoor.
  • Set the status to UVM_IS_OK on every access.

Hints

Hint 1

In uvm-core 2020.3.1, decode and encode receive a copy of the value array. Does the copy go back?

Hint 2

The key is the low byte of the byte address, not the word offset.

Hint 3

The base read calls read_func and then post_read. Unscramble in exactly one place.

What the grader checks: Your backdoor serves a real register model of the memory through hidden mixes of peek, poke, bus reads and bus writes. The checks read every value returned and every failed status.

6 checks on UVM · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 problems on this path

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Poke, Then Read Through the Bus

Input
poke(2, 5e); read(2); peek(2)
Expected output
value_0 = 0x5e, value_1 = 0x5e, value_2 = 0x0, value_3 = 0x0, value_4 = 0x0, value_5 = 0x0, value_6 = 0x0, value_7 = 0x0, failed_accesses = 0

poke stores swap(5e) ^ 48 = e5 ^ 48 = ad. The bus read unscrambles it to 5e, and peek must return 5e too.

Run samples executes all 3 public checks. Check all tests runs 6 checks on the server.

UVM · server