update constrained random testing + final directed tests

This commit is contained in:
2026-10-06 01:57:27 -04:00
parent 08a816478b
commit 69bb88630e
3 changed files with 187 additions and 27 deletions
+1
View File
@@ -76,6 +76,7 @@ class cpu_sb #(int DataSize=32, int AddrSize=14);
end end
expected_stores.delete(); expected_stores.delete();
program_running = 1'b0; program_running = 1'b0;
checking_regs = 1'b1;
end end
// Track writes made through the DUT's external memory port // Track writes made through the DUT's external memory port
+163 -12
View File
@@ -158,15 +158,163 @@ module cpu_tb_top;
drv.reset_task(); drv.reset_task();
endtask : run_external_halt_smoke endtask : run_external_halt_smoke
// 1.29: rst_i asserted mid-program must clear the register file even
// though the program never reaches ebreak
task automatic run_reset_while_running();
logic [31:0] reset_prog[$];
reset_prog = '{
asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(1)),
asm_instr(.op(OP_ADDI), .rd(1), .rs1(1), .imm(1)),
asm_instr(.op(OP_ADDI), .rd(1), .rs1(1), .imm(1)),
asm_instr(.op(OP_ADDI), .rd(1), .rs1(1), .imm(1)),
EBREAK_WORD
};
$display("");
$display("=================================================================");
$display(" TEST: reset_while_running");
$display("=================================================================");
sb.set_test("reset_while_running");
drv.reset_task();
seqr.load_program(reset_prog);
seqr.wait_for_empty();
repeat (2) @(cpu_if.cb);
drv.start_cpu();
repeat (2) @(cpu_if.cb);
cpu_if.cb.reset <= 1;
repeat (4) @(cpu_if.cb);
cpu_if.cb.reset <= 0;
repeat (2) @(cpu_if.cb);
readback(2, 1, 1);
endtask : run_reset_while_running
// 1.30: halt_cpu_i pulsed mid-program must stop the CPU short of
// completion. x1's partial count and cpu_active can't go through the
// scoreboard's exact-match checker (the ref model only predicts a
// completed run), so this test reads the external port directly and
// checks the result itself.
task automatic run_external_halt();
logic [31:0] halt_prog[$];
logic [31:0] x1_value;
for (int i = 0; i < 32; i++) begin
halt_prog.push_back(asm_instr(.op(OP_ADDI), .rd(1), .rs1(1), .imm(1)));
end
halt_prog.push_back(EBREAK_WORD);
$display("");
$display("=================================================================");
$display(" TEST: external_halt");
$display("=================================================================");
sb.set_test("external_halt");
drv.reset_task();
seqr.load_program(halt_prog);
seqr.wait_for_empty();
repeat (2) @(cpu_if.cb);
drv.start_cpu();
repeat (12) @(cpu_if.cb);
drv.halt_cpu_now();
if (cpu_if.cpu_active) begin
assertion_fail("external_halt: cpu_active did not deassert after halt_cpu_now()");
end
drv.xbar_read(word_addr(REG_BASE, 1));
x1_value = cpu_if.cb.rdata;
$display($stime, " TB: external_halt observed x1 = 0x%08x\n", x1_value);
if (x1_value >= 32) begin
assertion_fail($sformatf("external_halt: x1 = 0x%08x did not stop short of 32", x1_value));
end
drv.reset_task();
endtask : run_external_halt
// 1.31: external access to the first and last ISRAM words
task automatic run_isram_boundary_words();
$display("");
$display("=================================================================");
$display(" TEST: isram_boundary_words");
$display("=================================================================");
sb.set_test("isram_boundary_words");
drv.reset_task();
seqr.send_write_trans(word_addr(ISRAM_BASE, 0), asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(5)
));
seqr.send_write_trans(word_addr(ISRAM_BASE, 1023), EBREAK_WORD);
seqr.wait_for_empty();
repeat (2) @(cpu_if.cb);
readback(0, 1, 0);
readback(0, 1, 1023);
endtask : run_isram_boundary_words
// 1.32: external access to the first and last DSRAM words
task automatic run_dsram_boundary_words();
$display("");
$display("=================================================================");
$display(" TEST: dsram_boundary_words");
$display("=================================================================");
sb.set_test("dsram_boundary_words");
drv.reset_task();
seqr.send_write_trans(word_addr(DSRAM_BASE, 0), 32'hA5A5A5A5);
seqr.send_write_trans(word_addr(DSRAM_BASE, 1023), 32'h5A5A5A5A);
seqr.wait_for_empty();
repeat (2) @(cpu_if.cb);
readback(1, 1, 0);
readback(1, 1, 1023);
endtask : run_dsram_boundary_words
int NUM_RANDOM_PROGRAMS = 50;
int NUM_RANDOM_XBAR = 300;
// 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.
task automatic run_constrained_random_tests();
int min_len = 2;
int max_len = 50;
int max_cycles = 2000;
int dmem_words = 16;
drv.reset_task();
init_memories();
seqr.gen_xbar(NUM_RANDOM_XBAR);
seqr.wait_for_empty();
repeat (2) @(cpu_if.cb);
for (int i = 0; i < NUM_RANDOM_PROGRAMS; i++) begin
int len = $urandom_range(min_len, max_len);
string name = $sformatf("random_gen_%0d_len_%0d", i, len);
sb.set_test(name);
drv.reset_task();
init_memories();
seqr.gen_program(len);
seqr.wait_for_empty();
repeat (2) @(cpu_if.cb);
drv.run_program(max_cycles);
if (drv.last_run_completed) begin
readback(2, REG_COUNT);
readback(1, dmem_words);
end else begin
$display($stime, " TB: Skipping %s readback after CPU timeout\n", name);
drv.reset_task();
end
end
endtask : run_constrained_random_tests
// TEST PROGRAM // TEST PROGRAM
logic [31:0] prog[$]; logic [31:0] prog[$];
logic [31:0] data[$]; logic [31:0] data[$];
int NUM_RANDOM_PROGRAMS = 20;
int NUM_RANDOM_XBAR = 300;
initial begin initial begin
int total_errors; int total_errors;
@@ -398,16 +546,19 @@ module cpu_tb_top;
}; };
run_directed("beq_backward_loop", prog); run_directed("beq_backward_loop", prog);
// CONSTRAINED-RANDOM TESTS // 1.33: bulk register readback inside run_directed already reads x0
// TODO: Add constrained-randomized testing for cpu_seq_item and cpu_xbar_item // and x31 (index 0 and REG_COUNT-1), so no dedicated task is needed.
// For every random CPU program: prog = '{asm_instr(.op(OP_ADDI), .rd(31), .rs1(0), .imm(-1)), EBREAK_WORD};
// 1. print an iteration label so the seed and failure are reproducible, run_directed("external_register_boundary_reads", prog);
// 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.
// CHIP-LEVEL DIRECTED TESTS
run_reset_while_running();
run_external_halt();
run_isram_boundary_words();
run_dsram_boundary_words();
// CONSTRAINED-RANDOM TESTS
run_constrained_random_tests();
// TEST FINISH // TEST FINISH
repeat (20) @(cpu_if.cb); repeat (20) @(cpu_if.cb);
+15 -7
View File
@@ -1250,18 +1250,25 @@ biases and why the resulting programs remain useful and terminating.
**Implementation outline:** **Implementation outline:**
```systemverilog ```systemverilog
int min_len = 2; task automatic run_constrained_random_tests();
int max_len = 50; int min_len = 2;
int random_progs = 50; int max_len = 50;
int max_cycles = 2000; int max_cycles = 2000;
int dmem_words = 16; int dmem_words = 16;
for (int i = 0; i < random_progs; i++) begin drv.reset_task();
init_memories();
seqr.gen_xbar(NUM_RANDOM_XBAR);
seqr.wait_for_empty();
repeat (2) @(cpu_if.cb);
for (int i = 0; i < NUM_RANDOM_PROGRAMS; i++) begin
int len = $urandom_range(min_len, max_len); int len = $urandom_range(min_len, max_len);
string name = $sformatf("random_gen_%0d_len_%0d", i, len); string name = $sformatf("random_gen_%0d_len_%0d", i, len);
sb.set_test(name); sb.set_test(name);
drv.reset_task(); drv.reset_task();
init_memories();
seqr.gen_program(len); seqr.gen_program(len);
seqr.wait_for_empty(); seqr.wait_for_empty();
repeat (2) @(cpu_if.cb); repeat (2) @(cpu_if.cb);
@@ -1274,7 +1281,8 @@ for (int i = 0; i < random_progs; i++) begin
$display($stime, " TB: Skipping %s readback after CPU timeout\n", name); $display($stime, " TB: Skipping %s readback after CPU timeout\n", name);
drv.reset_task(); drv.reset_task();
end end
end end
endtask : run_constrained_random_tests
``` ```
## 3. Assertions ## 3. Assertions