Random Instruction Streams

A model in lockstep checks any program, so random programs become free tests. But a random program dies easily, and it reaches some corners a thousand times and others never. This page shows the guard rails that keep it alive and the cover points that tell you what it reached.

You Will Be Able To

  • Name the guard rails that keep a random program alive, and what each prevents.
  • Define hazard cover points on retire records and count them.
  • Read coverage from recorded random programs and find the holes.
  • Say where verifying a core from scratch begins, and which open tools do it.
Lesson
V5.4
Time
28 min
Runs on
Your browser
Check
5 questions

The lockstep gate of this chip ran 2,000 random programs through ap_soc with no mismatch. Good news, if the programs did anything that matters. Did any of them load a word right after storing it? Not in twenty of them.

A Random Program and Its Guard RailsStep through the parts of one generated program and the rule that keeps each part alive.
  • Processor
  • Debug
  • Peripheral
  • Data
  • Control
  • Interrupt
A Random Program and Its Guard RailsStep through the parts of one generated program and the rule that keeps each part alive. processor Prologue main entered; pads,UART, GPIO, ap_plicset up processor Random blocks 429 words for seed41 debug Guard rails reserved registers,forward branches, afixed handler peripheral Real lines MSIP, the timer,UART0 and GPIOthrough ap_plic processor Epilogue TB_STATUS =0x900D, a jump toitself thenkeep it aliveinterrupt italways reaches
  1. processor Prologuemain entered; pads, UART, GPIO, ap_plic set up
    • then → Random blocks
  2. processor Random blocks429 words for seed 41
    • always reaches → Epilogue
  3. debug Guard railsreserved registers, forward branches, a fixed handler
    • keep it alive → Random blocks
  4. peripheral Real linesMSIP, the timer, UART0 and GPIO through ap_plic
    • interrupt it → Random blocks
  5. processor EpilogueTB_STATUS = 0x900D, a jump to itself
  1. Prologue

    Fixed set-up: "main entered", the UART pads, and random priorities and enables at ap_plic.

  2. Random blocks

    Arithmetic, memory, CSRs, branches, loops, waits and excursions, drawn by the seed.

  3. Guard rails

    Rules that no random choice can break: what may be written, where a branch may go, what a trap does.

  4. Real interrupt lines

    Blocks that raise and drop lines through the chip's own devices.

  5. Epilogue

    Every program ends with the pass word, so every record of it is compared.

Read the diagram as text
  • Prologue: main entered; pads, UART, GPIO, ap_plic set up
  • Random blocks: 429 words for seed 41
  • Guard rails: reserved registers, forward branches, a fixed handler
  • Real lines: MSIP, the timer, UART0 and GPIO through ap_plic
  • Epilogue: TB_STATUS = 0x900D, a jump to itself
  • Prologue → Random blocks: then
  • Guard rails → Random blocks: keep it alive
  • Real lines → Random blocks: interrupt it
  • Random blocks → Epilogue: always reaches

random-chip.mjs: the core's generator with the chip's blocks around it.

Before You Start

Read Lockstep With an Instruction-Level Model and Data Hazards and Forwarding first. Load-Use Stalls and the Hazard Unit explains the pipeline side of the bins.

Five questions this page answers
Question
1Why are random programs worth running at chip level?
2What kills a random program, and which guard rail stops each?
3What is a hazard cover point, and how is it counted?
4What did the recorded random programs miss?
5Where does verifying the core itself begin?

The Idea: Free Tests, If They Survive

A directed test needs someone to think of the case and its expected values. A random program needs neither: the model of the last two lessons checks every record. So a generator can produce thousands of programs. Each one checks the core in the chip, with real interrupts from real devices.

Random words die fast, though. One write to the register that holds an address kills it. So does one branch back into a loop, or one jump into empty memory. Guard rails are the rules a generator keeps so that no random choice can kill the program.

Surviving is not the same as reaching. A random stream hits back-to-back dependencies constantly and rare pairs almost never. Cover points count what the stream reached, so you know where to aim.

New Words

New words on this page
WordMeaning
Random program generatorA program that writes programs from a seed: the same seed gives the same program.
Guard railA rule of the generator that keeps every program alive whatever the random choices.
HazardA dependency between neighbouring instructions that a pipeline must handle: forwarding, a stall or a flush.
Cover pointA named situation that is counted when a run reaches it.
BinOne counted case of a cover point.
Coverage holeA bin no run reached.

The Smallest Example: Count Three Bins by Hand

A bin is a pair of neighbouring records. In raw1, the second reads a register the first wrote. In raw2, it reads one written two records back. In load_use, the first is a load and the second reads what it loaded. Count these three over the records below.

Twelve records of random program 41
pcinstructionwritesreads
00000368addi a0, a0, -1912x10x10
0000036ccsrrs zero, mie, a0x10
00000370lui a3, 0x200cx13
00000374addi a3, a3, -8x13x13
00000378lw a0, 0(a3)x10x13
0000037caddi a0, a0, 149x10x10
00000380lui a3, 0x2004x13
00000384addi a3, a3, 0x13x13
00000388sw a0, 0(a3)x13, x10
0000038csw zero, 4(a3)x13
00000390wfi
00000394trap 2147483655

From the retire records of the recorded run.

Walk the pairs top to bottom. The slice at the end of the page counts them the same way. It prints: records 12: raw at distance 1 6, at distance 2 1, load-use 1.

The Rules

  1. Rule 1: Reserve what keeps the program alive.

    Loop counters and address registers are never random destinations.

  2. Rule 2: Branch only forward, to a known place.

    A loop counts down in a reserved register; no jump lands inside it.

  3. Rule 3: Make every trap survivable.

    A fixed handler steps over a faulting instruction and returns from an interrupt with interrupts off.

  4. Rule 4: End every program the same way.

    A pass word and a jump to itself, so every record is compared.

  5. Rule 5: Count what the stream reached.

    Cover points on the records; a hole gets a directed sequence, not more seeds.

Sources for These Rules

The guard rails: tools/soc/rv32/random-program.mjs (its header lists them) and, for the chip, tools/soc/ap_soc/bench/soc_tb/random-chip.mjs. The lockstep gate (plan 3.10 E7, docs/soc/ap_soc-facts.md 27.12) ran 2,000 programs. It compared 1,128,404 records with 8,234 interrupts, and found 0 mismatches.

The bins: defined on retire records in content/modules/v3/socv/v5-labs.mjs (hazardBins). The chip's coverage build counts the same eight bins on the core's retire port, and grades problem V5.4.2 (facts 27.22). Its x0 bin counts only arithmetic writes, opcodes 13 and 33 in hex. Forwarding and load-use stalls on ap_rv32: modules S2.8 and S2.9.

Open tools for verifying a core from scratch, as their own pages describe them (read again on 2026-11-28). They are riscv-dv (github.com/chipsalliance/riscv-dv) and riscv-formal (github.com/YosysHQ/riscv-formal). Then riscv-arch-test (github.com/riscv-non-isa/riscv-arch-test) and the Sail RISC-V model (github.com/riscv/sail-riscv).

Now Run It: One Random Program, Twenty Seeds

Seed 41 makes 429 words. On ap_soc with the model on, it ran 1651 cycles: 731 records, 33 traps, 1 interrupt, 0 mismatches.

Its first six records: the prologue writes "main entered" before any random block.
 0 jal zero, 0x00000098
 1 lui t3, 0x80001
 2 addi t3, t3, -2048
 3 lui t4, 0x10002
 4 addi t4, t4, -2048
 5 lui t0, 0x10004
The eight bins over all 731 records of seed 41.
raw1         278   An instruction reads a register the record just before it wrote (not x0)
raw2          45   An instruction reads a register written two records before it, and not by the record just before
load_use       4   An instruction reads the register a load just before it wrote
load_store     3   A store writes, as its data (rs2), the register a load just before it wrote
store_load     0   A load reads the same word that a store just before it wrote
branch_raw    76   A branch compares a register the record just before it wrote
csr_raw        0   A CSR instruction accesses the CSR that the record just before it wrote
x0_write       0   An arithmetic instruction writes x0 (other than the nop addi x0, x0, 0), and the next one reads x0

Seed 41 left 3 bins empty: store_load, csr_raw, x0_write. Is that bad luck? Seeds 1 to 20, rerun the same way, passed with 0 mismatches in 10,731 records:

Twenty seeds
BinHitsSeeds that hit it
raw1422520
raw255620
load_use8518
load_store3012
store_load00
branch_raw104220
csr_raw11
x0_write22

Recorded on ap_soc v1.5, the model compare on.

The bin raw1 is everywhere: the generator draws from a small pool of registers on purpose. The bin store_load is a hole in all twenty. Only 1 seed hit csr_raw, and 2 hit x0_write. More seeds would barely move these; a directed pair of two instructions closes each. That is rule 5, and the practice problem does it.

Why It Is Built This Way

The generator is the core's own, with the chip around it. The core's bench had a fuzz device and a control device for its interrupt pins. The chip has real blocks instead: x29 points into the GPIO page. Lines are raised through ap_clint, the timer block, UART0 and GPIO. So the same programs check the core and its integration at once.

The bins are defined on retire records, not on pipeline signals. A record is the same on the chip and on the model. So a bin counted on either means the same thing, and the model alone can grade it. What they cannot say is whether the pipeline forwarded or stalled: that is the core team's coverage, on the core's own signals.

Here is where this module stops. Verifying a core from scratch means every instruction against the ISA and every hazard path in the pipeline. It means every exception rule, and proofs where simulation cannot reach. Open tools exist for each part. The generator riscv-dv, written in SystemVerilog and UVM, makes random instruction streams. Its page lists the commercial simulators it was checked with, and four instruction-level models it compares against. The framework riscv-formal checks a core formally through its RVFI port, on the SymbiYosys flow. The suite riscv-arch-test holds architectural tests whose expected results come from the Sail model. Its own page says they are not verification tests. Sail itself is a formal specification of RISC-V that also builds into a simulator.

The Trap: Ten Thousand Seeds of the Same Thing

A regression adds seeds every night, and the pass count climbs. The coverage report does not move. The same bins fill and the same holes stay empty, because the generator's weights never make the rare pair. More runs of a blind generator buy time, not coverage.

Read the holes, then aim. Raise a weight, add a block that makes the pair on purpose, or write a directed test. The worksheet reads the twenty seeds. The practice problem aims by hand: assembly that fills all eight bins inside a cycle budget.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc's generator is a few hundred lines with eight bins.

  1. Instruction streams by the million

    Core teams run random streams for weeks on emulators and farms, with thousands of cover points over instructions, operands, hazards and exceptions.

  2. Coverage-driven generation

    Generators read the coverage back and change their weights to aim at holes.

  3. At chip level

    The integrated core is checked again with the same streams, now with real devices and real interrupts, as this chip's gate does.

Try It

Run the slice: it counts raw1, raw2 and load_use over the twelve records of the table.

Remove the load from the list. Which count falls?

The predict question runs it on the program's first twelve records.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: records 12: raw at distance 1 6, at distance 2 1, load-use 1

Check Yourself

Predict the outputThe slice now counts the first twelve records of program 41, its prologue. What is the last line it prints?
// Decoded records of a random program on ap_soc v1.5: the register each one wrote, the registers it read,
// and whether it was a load. Three hazard bins counted the way the cover points of this module count them.
module tb;
  localparam int N = 12;
  int rd [N], rs1 [N], rs2 [N];
  bit load [N];
  int raw1 = 0, raw2 = 0, load_use = 0;
  function automatic bit reads(int i, int r);
    return r != 0 && (rs1[i] == r || rs2[i] == r);
  endfunction
  initial begin
    rd[0] = 0; rs1[0] = 0; rs2[0] = 0; load[0] = 0;
    rd[1] = 28; rs1[1] = 0; rs2[1] = 0; load[1] = 0;
    rd[2] = 28; rs1[2] = 28; rs2[2] = 0; load[2] = 0;
    rd[3] = 29; rs1[3] = 0; rs2[3] = 0; load[3] = 0;
    rd[4] = 29; rs1[4] = 29; rs2[4] = 0; load[4] = 0;
    rd[5] = 5; rs1[5] = 0; rs2[5] = 0; load[5] = 0;
    rd[6] = 5; rs1[6] = 5; rs2[6] = 0; load[6] = 0;
    rd[7] = 6; rs1[7] = 0; rs2[7] = 0; load[7] = 0;
    rd[8] = 6; rs1[8] = 6; rs2[8] = 0; load[8] = 0;
    rd[9] = 0; rs1[9] = 5; rs2[9] = 6; load[9] = 0;
    rd[10] = 6; rs1[10] = 0; rs2[10] = 0; load[10] = 0;
    rd[11] = 6; rs1[11] = 6; rs2[11] = 0; load[11] = 0;
    for (int i = 1; i < N; i++) begin
      if (reads(i, rd[i-1])) begin raw1++; if (load[i-1]) load_use++; end
      else if (i > 1 && reads(i, rd[i-2])) raw2++;
    end
    $display("records %0d: raw at distance 1 %0d, at distance 2 %0d, load-use %0d", N, raw1, raw2, load_use);
  end
endmodule
ConceptWhy are random programs worth running at chip level?
ConceptA random add writes x28, the register that points into SRAM. What happens next?
ConceptWhat did twenty recorded random programs never reach?
ConceptWhere does verifying a core from scratch begin?

Common Mistakes

Wrong Let random instructions write any register.

One write to an address register sends every later access anywhere.

Fix Reserve pointers and loop counters; draw from a pool.

Wrong Add seeds to fill a hole.

A rare pair stays rare with a blind generator.

Fix Aim: a weight, a directed block, or a directed test.

Wrong Read the chip-level bins as pipeline coverage.

They count records, not forwarding paths or stalls.

Fix Leave pipeline coverage to the core's own bench.

Remember

  • With a model in lockstep, random programs are tests without expected values.
  • Guard rails keep them alive: reserved registers, forward branches, a fixed handler, a fixed end.
  • Cover points on retire records say what a stream reached; holes need aimed sequences, not more seeds.
  • Core verification proper is the core team's: riscv-dv, riscv-formal, riscv-arch-test and the Sail model are its open tools.

Practice

Report a problem with this page