Add state-aware random program scaffolding

This commit is contained in:
Huang
2026-09-29 21:29:06 -04:00
committed by Benzi
parent e1ff376357
commit 2bb9ccd45e
5 changed files with 131 additions and 18 deletions
+10 -2
View File
@@ -101,6 +101,13 @@ biases and why the resulting programs remain useful and terminating.
| `c_no_branch_at_end` | Provided | | No member change |
| `c_branch_taken_bias` | Open | | |
<!--
For c_mem_align, explain why alignment of the encoded immediate is not enough.
Identify the runtime inputs to an effective address, the legal DSRAM byte-address
range for a whole word, and how your constraint preserves useful rs1 choices.
Do not reserve a base register merely to make the constraint easier.
-->
### External-port constraints
| Constraint | Provided, partial, or open | Behavior and purpose | Final implementation summary |
@@ -121,7 +128,9 @@ biases and why the resulting programs remain useful and terminating.
**Implementation outline:**
```systemverilog
// Write the intended loop and lifecycle using seqr.gen_program(...).
// Sketch the loop using seqr.gen_program(...). Include the iteration label,
// CPU reset, memory reinitialization, bounded run, result reads, and recovery
// behavior after a timeout.
```
## 3. Assertions
@@ -229,4 +238,3 @@ Include a screenshot showing the offending instruction and the first incorrect a
**Behavioral characterization:**
<!-- One precise sentence. Do not speculate about hidden RTL. -->
+32 -6
View File
@@ -100,7 +100,7 @@ scoreboard must not assume that driver intent became DUT behavior.
| `src/verilog/tb/cpu_if.sv` | Groups chip signals and supplies a clocking block that prevents testbench/RTL races. | Provided |
| `src/verilog/tb/cpu_seq_item.svh` | Represents one randomized instruction and encodes it into a program word. | Complete constraints |
| `src/verilog/tb/cpu_xbar_item.svh` | Represents external accesses and monitor observations. | Complete constraints |
| `src/verilog/tb/cpu_sequencer.svh` | Generates programs/accesses and sends them to the driver through a mailbox. | Provided |
| `src/verilog/tb/cpu_sequencer.svh` | Generates programs/accesses, tracks the reachable generation state, and sends requests through a mailbox. | Provided |
| `src/verilog/tb/cpu_driver.svh` | Converts high-level requests into cycle-accurate chip pin activity. | Complete protocol tasks |
| `src/verilog/tb/cpu_monitor.svh` | Samples the interface and turns observed behavior into transactions. | Complete delayed-read and CPU-memory reconstruction |
| `src/verilog/tb/cpu_ref_model.svh` | Executes the supported architecture without modeling pipeline timing. | Provided; may be extended |
@@ -108,7 +108,7 @@ scoreboard must not assume that driver intent became DUT behavior.
| `src/verilog/tb/cpu_tb_pkg.sv` | Holds shared types, constants, helpers, and class includes. | Provided |
| `src/verilog/tb/cpu_tb_top.sv` | Instantiates the DUT and components, runs tests, and contains assertions. | Add tests and assertions |
| `Fall-26-Onboarding-TestPlan-Template.md` | Plan organized like the testbench: functional, chip-level, random, assertion, coverage, and bug-hunt sections. | Copy into initial and final plans |
| `sim/behav/Include/member.include` | Lists the your CPU and testbench sources compiled by Xcelium. | Update only if filenames differ |
| `sim/behav/Include/member.include` | Lists your CPU and testbench sources compiled by Xcelium. | Update only if filenames differ |
| `sim/behav/Makefile` | Provides the simulation, waveform, coverage, and bug-hunt commands. | Provided |
### How the components cooperate
@@ -116,6 +116,12 @@ scoreboard must not assume that driver intent became DUT behavior.
The **sequencer** decides what operation should be attempted. It sends transaction objects through
a mailbox to the **driver**, which is the only class allowed to drive DUT inputs.
Some instruction constraints depend on architectural state produced by earlier generated
instructions. For that purpose, the supplied sequencer advances a private reference model along
the program's reachable path and places the current register values in each instruction item
before randomization. This model supplies generation context only: it never observes the DUT and
never decides whether a test passed.
The **monitor** independently samples signals at the chip boundary. It converts observed requests,
responses, CPU memory operations, reset, start, and halt into transactions for the **scoreboard**.
The monitor never asks the driver what it intended to do.
@@ -157,7 +163,8 @@ rewrite the original reasoning when results contradict it; the revision history
When planning constrained-random tests, look through `src/verilog/tb/cpu_seq_item.svh` for the
instruction constraints, and `src/verilog/tb/cpu_xbar_item.svh` for the external-port constraints.
Use those constraint blocks to guide the plan for what stimulus is already covered and
what still needs to be completed.
what still needs to be completed. In particular, distinguish a field that is encoded in an
instruction from a value that depends on the state in which that instruction executes.
Do not make a checklist containing only one friendly example per operation. Think about value
classes, limits, state transitions, dependencies between adjacent operations, ownership changes,
@@ -269,6 +276,22 @@ unproductive combinations. The constraint exercises intentionally use three leve
For each constraint you complete, document the invalid state it removes, the useful distribution
or dependency it encourages, and any legal behavior it makes unreachable.
#### State-aware memory constraints
For `lw` and `sw`, the immediate is only one input to the effective byte address. Earlier
instructions may leave the selected base register aligned or unaligned, so constraining the
immediate alone cannot guarantee a legal access. Before each instruction is randomized, the
provided sequencer fills `reg_snapshot` with the register state at that instruction's reachable
execution point. Use that context to express both requirements in the `c_mem_align` TODO:
- the effective byte address is word-aligned, and
- the entire word lies inside data SRAM.
Keep `rs1` genuinely random. Forcing it to `x0`, reserving one address register, or requiring base
register values themselves to be aligned removes legal combinations and hides dependencies between
instructions. The sequencer includes a diagnostic check that stops at the first illegal generated
memory operation; it is feedback for completing the constraint, not a substitute for it.
Random stimulus is valuable only when it is reproducible and checked. Generated instructions are
printed by default. The Makefile also controls Xcelium's seed:
@@ -277,9 +300,12 @@ make xrun SEED=1 # repeat a known run
make xrun SEED=random # ask Xcelium to choose a new seed
```
For a random run, record the numeric `SVSEED` printed by Xcelium and rerun with that number. Reset
between random programs, label each iteration, and ensure the monitor/scoreboard path checks every
relevant result. A seed without the generated program and failing iteration is weak evidence.
For a random run, record the numeric `SVSEED` printed by Xcelium and rerun with that number. Label
each iteration, reset the CPU, and call `init_memories()` before generating each new program. That
reinitialization gives the DUT, scoreboard, and sequencer's generation model the same starting
memory image; it also prevents a timed-out or store-heavy program from contaminating the next
iteration. Run the CPU with a bounded timeout, then queue the register and relevant memory reads
needed for checking. A seed without the generated program and failing iteration is weak evidence.
### 7. Report DUT code coverage and revise the plan
+13 -3
View File
@@ -19,6 +19,12 @@ class cpu_seq_item #(int DataSize=32, int AddrSize=14);
int idx; // Position of this instruction in the program
int prog_len; // Length used by branch constraints
// Architectural register values when this instruction would execute.
// The sequencer supplies this context before randomize(). rs1 is still
// randomized; this array tells a constraint the value of the selected
// base register without reserving or forcing an address register.
logic [REG_COUNT-1:0][31:0] reg_snapshot;
// Keep generated instructions within the supported instruction set
constraint c_opcode {
op inside {OP_ADD, OP_ADDI, OP_SUB, OP_SLL, OP_SRL, OP_LW, OP_SW, OP_BEQ};
@@ -36,9 +42,12 @@ class cpu_seq_item #(int DataSize=32, int AddrSize=14);
imm >= -2048;
}
// TODO: Keep load/store offsets word-aligned. Addresses depend on
// runtime rs1 values, so tests must initialize address registers
// such that accesses stay inside data memory range.
// TODO: Keep each load/store effective byte address word-aligned and
// inside data SRAM. Checking imm by itself is not sufficient because the
// address also depends on the runtime value of the randomized rs1.
// Use reg_snapshot to describe the legal relationship while preserving
// the existing rs1 distribution; do not solve this by forcing rs1 to x0
// or to a dedicated address register.
constraint c_mem_align {
1;
}
@@ -68,6 +77,7 @@ class cpu_seq_item #(int DataSize=32, int AddrSize=14);
function new();
idx = 0;
prog_len = 1;
reg_snapshot = '0;
endfunction: new
// Encode the instruction fields into an instruction-memory word
+66 -3
View File
@@ -7,9 +7,17 @@ class cpu_sequencer #(int DataSize=32, int AddrSize=14);
mailbox #(cpu_xbar_item #(DataSize, AddrSize)) xbar_box;
// Mirror data-memory initialization writes. gen_program starts a private
// reference model from this image and advances it only along the generated
// program's reachable path. This is generation context, not DUT checking.
logic [31:0] shadow_dmem [DSRAM_WORDS];
// Constructor
function new();
xbar_box = new();
foreach (shadow_dmem[i]) begin
shadow_dmem[i] = '0;
end
endfunction: new
// Build a random program and enqueue frontdoor instruction writes.
@@ -17,21 +25,59 @@ class cpu_sequencer #(int DataSize=32, int AddrSize=14);
// constraints depend on the instruction's position.
task gen_program(input int num, input bit verbose=1'b1);
cpu_seq_item #(DataSize, AddrSize) instr;
cpu_ref_model shadow;
logic [31:0] program_words [$];
logic [31:0] effective_addr;
bit reachable;
int prog_len;
if ((num < 0) || (num >= ISRAM_WORDS)) begin
$fatal(1,
"%0t Seq: program needs 0..%0d random instructions, got %0d",
$time, ISRAM_WORDS - 1, num);
end
prog_len = num + 1; // +1 for the ebreak
shadow = new();
shadow.load_dmem(shadow_dmem);
for (int i = 0; i < num; i++) begin
instr = new();
instr.idx = i;
instr.prog_len = prog_len;
foreach (shadow.regs[r]) begin
instr.reg_snapshot[r] = shadow.regs[r];
end
if (!instr.randomize()) begin
$error($stime, " Seq: randomization failed for instruction %0d\n", i);
$fatal(1, "%0t Seq: state-aware randomization failed for instruction %0d",
$time, i);
end
// Keep this independent diagnostic while c_mem_align is a member
// TODO. If it fires, the generated instruction violated the
// effective-address contract described on that constraint.
if (instr.op inside {OP_LW, OP_SW}) begin
effective_addr = instr.reg_snapshot[instr.rs1] + instr.imm;
if ((effective_addr[1:0] != 2'b00) ||
(effective_addr > ((DSRAM_WORDS * 4) - 4))) begin
$fatal(1,
"%0t Seq: illegal effective byte address 0x%08x for %s; complete c_mem_align",
$time, effective_addr, instr.asm());
end
end
if (verbose) begin
instr.display();
end
send_write_trans(word_addr(ISRAM_BASE, i), instr.encode());
program_words.push_back(instr.encode());
// Taken forward branches may skip generated slots. They still
// belong in instruction memory, but skipped instructions must not
// change the state supplied to a later reachable instruction.
reachable = !shadow.halted && (shadow.pc[11:2] == i);
shadow.imem[i] = instr.encode();
if (reachable) begin
shadow.step();
end
end
// Append ebreak explicitly so a random program cannot halt early
@@ -46,7 +92,21 @@ class cpu_sequencer #(int DataSize=32, int AddrSize=14);
if (verbose) begin
instr.display();
end
send_write_trans(word_addr(ISRAM_BASE, num), instr.encode());
program_words.push_back(instr.encode());
shadow.imem[num] = instr.encode();
if (!shadow.halted && (shadow.pc[11:2] == num)) begin
shadow.step();
end
if (!shadow.halted) begin
$fatal(1,
"%0t Seq: generated control flow missed the final ebreak (pc=0x%08x); complete c_branch_target",
$time, shadow.pc);
end
foreach (program_words[i]) begin
send_write_trans(word_addr(ISRAM_BASE, i), program_words[i]);
end
endtask: gen_program
// Stage one external write transaction
@@ -58,6 +118,9 @@ class cpu_sequencer #(int DataSize=32, int AddrSize=14);
trans.addr = addr;
trans.data = data;
trans.is_write = 1'b1;
if ((window_of(addr) == WIN_DSRAM) && (addr[1:0] == 2'b00)) begin
shadow_dmem[addr[11:2]] = data;
end
xbar_box.put(trans);
endtask: send_write_trans
+7 -1
View File
@@ -208,7 +208,13 @@ module cpu_tb_top;
// CONSTRAINED-RANDOM TESTS
// TODO: Add constrained-randomized testing for cpu_seq_item and cpu_xbar_item
// Use the existing helpers
// For every random CPU program:
// 1. print an iteration label so the seed and failure are reproducible,
// 2. reset the CPU and call init_memories() before gen_program(),
// 3. wait for program writes, run with the supplied bounded helper,
// 4. queue the register and relevant memory reads needed by the scoreboard.
// Keep randomized external-port traffic in a phase where the CPU is stopped.
// The README explains why memory must be reinitialized every iteration.
// TEST FINISH