Add state-aware random program scaffolding
This commit is contained in:
@@ -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
|
||||
@@ -118,4 +128,4 @@ class cpu_seq_item #(int DataSize=32, int AddrSize=14);
|
||||
$display($stime, " Seq: [%0d] 0x%08x %s\n", idx, encode(), asm());
|
||||
endfunction: display
|
||||
|
||||
endclass: cpu_seq_item
|
||||
endclass: cpu_seq_item
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -103,4 +166,4 @@ class cpu_sequencer #(int DataSize=32, int AddrSize=14);
|
||||
end
|
||||
endtask: wait_for_empty
|
||||
|
||||
endclass: cpu_sequencer
|
||||
endclass: cpu_sequencer
|
||||
|
||||
@@ -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
|
||||
@@ -265,4 +271,4 @@ module cpu_tb_top;
|
||||
!(cpu_if.w_en && cpu_if.r_en))
|
||||
else assertion_fail("w_en_i and r_en_i were asserted together");
|
||||
|
||||
endmodule: cpu_tb_top
|
||||
endmodule: cpu_tb_top
|
||||
|
||||
Reference in New Issue
Block a user