/* * Top-level testbench */ module cpu_tb_top; import cpu_tb_pkg::*; parameter int DataSize = DATA_W; parameter int AddrSize = XBAR_AW; string requested_seed = "default"; int assertion_errors = 0; logic clk = 0; always #5 clk = ~clk; // IF instantiation cpu_if #( .DataSize(DataSize), .AddrSize(AddrSize) ) cpu_if ( .clk(clk) ); // DUT instantiation chip_top dut ( .clk_i(clk), .rst_i(cpu_if.cpu.reset), .en_cpu_i(cpu_if.cpu.en_cpu), .halt_cpu_i(cpu_if.cpu.halt_cpu), .cpu_halted_o(cpu_if.cpu.cpu_halted), .addr_i(cpu_if.cpu.addr), .wdata_i(cpu_if.cpu.wdata), .w_en_i(cpu_if.cpu.w_en), .r_en_i(cpu_if.cpu.r_en), .rdata_o(cpu_if.cpu.rdata), .rready_o(cpu_if.cpu.rready) ); // Observation signals from the DUT memory path assign cpu_if.cpu_active = dut.cpu_enable_q; assign cpu_if.dsram_en = dut.dsram_en; assign cpu_if.dsram_write_en = dut.dsram_write_en; assign cpu_if.dsram_addr = dut.dsram_addr; assign cpu_if.dsram_wdata = dut.dsram_wdata; assign cpu_if.dsram_rdata = dut.dsram_rdata; cpu_tb_pkg::cpu_sequencer #(.DataSize(DataSize), .AddrSize(AddrSize)) seqr; cpu_tb_pkg::cpu_driver #(.DataSize(DataSize), .AddrSize(AddrSize)) drv; cpu_tb_pkg::cpu_monitor #(.DataSize(DataSize), .AddrSize(AddrSize)) mon; cpu_tb_pkg::cpu_sb #(.DataSize(DataSize), .AddrSize(AddrSize)) sb; // Record failed assertion function void assertion_fail(input string message); assertion_errors++; $error($stime, " ASSERT: %s\n", message); endfunction : assertion_fail // Initialize both memories through the DUT's external memory port task automatic init_memories(); $display($stime, " TB: Initializing instruction and data memories\n"); mon.log_transactions = 1'b0; sb.log_transactions = 1'b0; for (int i = 0; i < ISRAM_WORDS; i++) begin seqr.send_write_trans(word_addr(ISRAM_BASE, i), EBREAK_WORD); end for (int i = 0; i < DSRAM_WORDS; i++) begin seqr.send_write_trans(word_addr(DSRAM_BASE, i), $urandom()); end seqr.wait_for_empty(); repeat (2) @(cpu_if.cb); mon.log_transactions = 1'b1; sb.log_transactions = 1'b1; $display($stime, " TB: Memory initialization complete\n"); endtask : init_memories // Queue external reads on a memory window task automatic readback(input int window, input int num_words, input int start_word = 0); logic [AddrSize-1:0] base; case (window) 0: base = ISRAM_BASE; 1: base = DSRAM_BASE; 2: base = REG_BASE; default: base = REG_BASE; endcase for (int i = 0; i < num_words; i++) begin seqr.send_read_trans(word_addr(base, start_word + i)); end seqr.wait_for_empty(); repeat (4) @(cpu_if.cb); endtask : readback task automatic preload_dmem(input logic [31:0] values[$], input int start_word = 0); foreach (values[i]) begin seqr.send_write_trans(word_addr(DSRAM_BASE, start_word + i), values[i]); end seqr.wait_for_empty(); repeat (2) @(cpu_if.cb); endtask : preload_dmem // Run a directed test task automatic run_directed(input string name, const ref logic [31:0] program_words[$], input int max_cycles = 2000, input int dmem_words = 16); $display(""); $display("================================================================="); $display(" TEST: %s", name); $display("================================================================="); sb.set_test(name); drv.reset_task(); seqr.load_program(program_words); 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 endtask : run_directed // Basic smoke test for loading, running, and observing a program that halts task automatic run_smoke_test(); logic [31:0] smoke_prog[$]; smoke_prog = '{EBREAK_WORD}; run_directed("smoke_ebreak", smoke_prog, 100, 1); endtask : run_smoke_test // Smoke test for stopping the CPU from outside the program // Readback is skipped because the CPU stops before the program completes task automatic run_external_halt_smoke(); logic [31:0] long_prog[$]; for (int i = 0; i < 32; i++) begin long_prog.push_back(asm_instr(.op(OP_ADDI), .rd(1), .rs1(1), .imm(1))); end long_prog.push_back(EBREAK_WORD); $display(""); $display("================================================================="); $display(" TEST: smoke_external_halt"); $display("================================================================="); sb.set_test("smoke_external_halt"); drv.reset_task(); seqr.load_program(long_prog); seqr.wait_for_empty(); repeat (2) @(cpu_if.cb); drv.start_cpu(); repeat (12) @(cpu_if.cb); drv.halt_cpu_now(); drv.reset_task(); 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(); sb.set_test("random_xbar_traffic"); 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 logic [31:0] prog[$]; logic [31:0] data[$]; initial begin int total_errors; mon = new(cpu_if); sb = new(mon.mon_box); seqr = new(); drv = new(cpu_if, seqr.xbar_box); void'($value$plusargs("NUM_RANDOM_PROGRAMS=%d", NUM_RANDOM_PROGRAMS)); void'($value$plusargs("NUM_RANDOM_XBAR=%d", NUM_RANDOM_XBAR)); if ($test$plusargs("REF_TRACE")) begin sb.ref_model.trace = 1'b1; end fork mon.run(); sb.run(); drv.run(); join_none drv.reset_task(); init_memories(); run_smoke_test(); run_external_halt_smoke(); // DIRECTED TESTS drv.reset_task(); init_memories(); // Add directed test cases from the test plan // Use the existing helpers // Example Test: addi with a positive immediate prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(5)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(1), .imm(100)), EBREAK_WORD }; run_directed("addi_positive", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADD), .rd(2), .rs1(1), .rs2(2)), EBREAK_WORD }; run_directed("add_overflow", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADDI), .rd(1), .rs1(1), .imm(1)), EBREAK_WORD }; run_directed("addi_overflow", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(1)), asm_instr(.op(OP_ADDI), .rd(1), .rs1(1), .imm(-1)), EBREAK_WORD }; run_directed("addi_negative", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm('h123)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm('h123)), asm_instr(.op(OP_ADD), .rd(2), .rs1(1), .rs2(2)), EBREAK_WORD }; run_directed("add_typical", prog); prog = '{asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm('h123)), EBREAK_WORD}; run_directed("addi_typical", prog); prog = '{asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), EBREAK_WORD}; run_directed("addi_negative_overflow", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(1)), asm_instr(.op(OP_SUB), .rd(1), .rs1(0), .rs2(1)), EBREAK_WORD }; run_directed("sub_overflow", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm('h123)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm('h23)), asm_instr(.op(OP_SUB), .rd(2), .rs1(1), .rs2(2)), EBREAK_WORD }; run_directed("sub_typical", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(32)), asm_instr(.op(OP_SRL), .rd(1), .rs1(1), .rs2(2)), EBREAK_WORD }; run_directed("srl_oob", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(32)), asm_instr(.op(OP_SLL), .rd(1), .rs1(1), .rs2(2)), EBREAK_WORD }; run_directed("sll_oob", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_SLL), .rd(1), .rs1(1), .rs2(0)), EBREAK_WORD }; run_directed("sll_zero", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_SRL), .rd(1), .rs1(1), .rs2(0)), EBREAK_WORD }; run_directed("srl_zero", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(1)), asm_instr(.op(OP_SLL), .rd(1), .rs1(1), .rs2(2)), EBREAK_WORD }; run_directed("sll_one", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(1)), asm_instr(.op(OP_SRL), .rd(1), .rs1(1), .rs2(2)), EBREAK_WORD }; run_directed("srl_one", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(4)), asm_instr(.op(OP_SLL), .rd(1), .rs1(1), .rs2(2)), EBREAK_WORD }; run_directed("sll_typical", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(4)), asm_instr(.op(OP_SRL), .rd(1), .rs1(1), .rs2(2)), EBREAK_WORD }; run_directed("srl_typical", prog); prog = '{asm_instr(.op(OP_LW), .rd(1), .rs1(0), .imm(0)), EBREAK_WORD}; run_directed("lw_zero_offset", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_SW), .rs1(0), .rs2(1), .imm(0)), EBREAK_WORD }; run_directed("sw_zero_offset", prog); prog = '{asm_instr(.op(OP_LW), .rd(1), .rs1(0), .imm(4)), EBREAK_WORD}; run_directed("lw_positive", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_SW), .rs1(0), .rs2(1), .imm(4)), EBREAK_WORD }; run_directed("sw_positive", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(64)), asm_instr(.op(OP_LW), .rd(2), .rs1(1), .imm(-4)), EBREAK_WORD }; run_directed("lw_negative", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(64)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(-1)), asm_instr(.op(OP_SW), .rs1(1), .rs2(2), .imm(-4)), EBREAK_WORD }; run_directed("sw_negative", prog); prog = '{asm_instr(.op(OP_LW), .rd(0), .rs1(0), .imm(0)), EBREAK_WORD}; run_directed("lw_x0", prog); prog = '{ asm_instr(.op(OP_BEQ), .rs1(0), .rs2(0), .imm(8)), asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(1)), asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(2)), EBREAK_WORD }; run_directed("beq_x0", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_BEQ), .rs1(0), .rs2(1), .imm(8)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(-1)), EBREAK_WORD }; run_directed("beq_not_taken", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1)), asm_instr(.op(OP_ADDI), .rd(2), .rs1(0), .imm(-1)), asm_instr(.op(OP_BEQ), .rs1(1), .rs2(2), .imm(8)), asm_instr(.op(OP_ADDI), .rd(3), .rs1(0), .imm(1)), asm_instr(.op(OP_ADDI), .rd(3), .rs1(0), .imm(2)), EBREAK_WORD }; run_directed("beq_typical", prog); prog = '{EBREAK_WORD, asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(-1))}; run_directed("ebreak_immediately", prog); prog = '{ asm_instr(.op(OP_ADDI), .rd(1), .rs1(0), .imm(3)), asm_instr(.op(OP_ADDI), .rd(1), .rs1(1), .imm(-1)), asm_instr(.op(OP_BEQ), .rs1(1), .rs2(0), .imm(8)), asm_instr(.op(OP_BEQ), .rs1(0), .rs2(0), .imm(-8)), EBREAK_WORD }; run_directed("beq_backward_loop", prog); // 1.33: bulk register readback inside run_directed already reads x0 // and x31 (index 0 and REG_COUNT-1), so no dedicated task is needed. prog = '{asm_instr(.op(OP_ADDI), .rd(31), .rs1(0), .imm(-1)), EBREAK_WORD}; run_directed("external_register_boundary_reads", prog); // 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 repeat (20) @(cpu_if.cb); total_errors = sb.errors + drv.timeouts + mon.errors + assertion_errors; if (sb.checks == 0) begin $error("Testbench performed zero checks; no results were verified"); end $display("================================================================="); $display(" Scoreboard mismatches : %0d", sb.errors); $display(" Driver timeouts : %0d", drv.timeouts); $display(" Monitor protocol errs : %0d", mon.errors); $display(" Assertion failures : %0d", assertion_errors); $display("================================================================="); $display(); if (total_errors == 0) begin $display("================================================================="); $display(" TEST PASSED!"); $display("================================================================="); end else begin $display("================================================================="); $display(" TEST FAILED: %0d error(s)", total_errors); $display("================================================================="); end $finish; end // Stop a broken DUT or testbench instead of letting the simulation run forever initial begin #10ms; $display(" TEST FAILED - global timeout"); $fatal(1, "global timeout"); end initial begin void'($value$plusargs("SEED_LABEL=%s", requested_seed)); if ($test$plusargs("BUG_HUNT_WAVES")) begin $shm_open("bug_hunt_waves.shm"); end else begin $shm_open("waves.shm"); end $shm_probe("AC"); end wire rst = cpu_if.reset; // Example assertion // It checks one interface rule and sends failures through the testbench counter NO_SIMULTANEOUS_READ_WRITE : assert property (@(posedge clk) disable iff (rst) !(cpu_if.w_en && cpu_if.r_en)) else assertion_fail("w_en_i and r_en_i were asserted together"); X0_REMAINS_0 : assert property (@(posedge clk) disable iff (rst) dut.register_crossbar[0] == 32'b0) else assertion_fail("x0 is nonzero"); STABLE_WHEN_DISABLED : assert property (@(posedge clk) disable iff (rst) !cpu_if.cpu_active |=> $stable( dut.register_crossbar )) else assertion_fail("register file changed while cpu inactive"); endmodule : cpu_tb_top