From cabff0295de36f105a929ba66e7445da041a9a40 Mon Sep 17 00:00:00 2001 From: Daniel Jobson Date: Fri, 22 Nov 2024 13:40:43 +0100 Subject: [PATCH] tb: make timer core testbench selftesting - Compare against an expected result and count errors - Exit with the right error code - Clean up the output --- .../core/timer/tb/tb_timer_core.v | 75 ++++++++++++++++--- 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/hw/application_fpga/core/timer/tb/tb_timer_core.v b/hw/application_fpga/core/timer/tb/tb_timer_core.v index cdb65c80..44d05cea 100644 --- a/hw/application_fpga/core/timer/tb/tb_timer_core.v +++ b/hw/application_fpga/core/timer/tb/tb_timer_core.v @@ -122,14 +122,18 @@ module tb_timer_core (); //---------------------------------------------------------------- task reset_dut; begin + if (tb_monitor) begin $display("--- DUT before reset:"); - dump_dut_state(); + dump_dut_state(); + end $display("--- Toggling reset."); tb_reset_n = 0; #(2 * CLK_PERIOD); tb_reset_n = 1; + if (tb_monitor) begin $display("--- DUT after reset:"); - dump_dut_state(); + dump_dut_state(); + end end endtask // reset_dut @@ -182,28 +186,71 @@ module tb_timer_core (); //---------------------------------------------------------------- // test1() + // + // Set prescaler and timer and count until the timer returns expired. + // Check so the clock cycles passed adds up to timer * prescaler + 1. //---------------------------------------------------------------- task test1; - begin + begin : test1 + reg [31 : 0] test1_cycle_ctr_start; + reg [31 : 0] test1_counted_num_cycles; + reg [31 : 0] test1_expected_num_cycles; + tc_ctr = tc_ctr + 1; - tb_monitor = 1; - $display("--- test1 started."); - dump_dut_state(); + $display(""); + $display("--- test1: Run timer to set value started."); + $display("--- test1: prescaler: 6, timer: 9. Should take 6*9 + 1 = 55 cycles.."); + tb_prescaler_init = 32'h6; tb_timer_init = 32'h9; + test1_expected_num_cycles = tb_prescaler_init * tb_timer_init + 1; + #(CLK_PERIOD); tb_start = 1'h1; + test1_cycle_ctr_start = cycle_ctr; #(CLK_PERIOD); tb_start = 1'h0; - wait_done(); #(CLK_PERIOD); - tb_monitor = 0; - $display("--- test1 completed."); + + while (tb_running) begin + #(CLK_PERIOD); + end + test1_counted_num_cycles = cycle_ctr - test1_cycle_ctr_start; + + + if (test1_counted_num_cycles == test1_expected_num_cycles) begin + $display("--- test1: Correct number of cycles counted: %0d", test1_counted_num_cycles); + end + else begin + $display("--- test1: Error, expected %0d cycles, counted cycles: %0d", + test1_expected_num_cycles, test1_counted_num_cycles); + error_ctr = error_ctr + 1; + end + + + + $display("--- test1: Completed."); $display(""); end endtask // test1 + //---------------------------------------------------------------- + // exit_with_error_code() + // + // Exit with the right error code + //---------------------------------------------------------------- + task exit_with_error_code; + begin + if (error_ctr == 0) begin + $finish(0); + end + else begin + $fatal(1); + end + end + endtask // exit_with_error_code + //---------------------------------------------------------------- // timer_core_test @@ -211,7 +258,9 @@ module tb_timer_core (); // Test vectors from: //---------------------------------------------------------------- initial begin : timer_core_test - $display("--- Simulation of timer core started."); + $display(""); + $display(" -= Simulation of timer core started =-"); + $display(" ==============================="); $display(""); init_sim(); @@ -220,8 +269,10 @@ module tb_timer_core (); test1(); $display(""); - $display("--- Simulation of timer core completed."); - $finish; + $display(" -= Simulation of timer core completed =-"); + $display(" ==============================="); + $display(""); + exit_with_error_code(); end // timer_core_test endmodule // tb_timer_core