`timescale 1ns / 1ps

module tb_ecg_r_peak_detector;
    reg clk;
    reg rst;
    reg [11:0] sample_in;
    reg [11:0] threshold;

    wire peak_out;

    integer errors = 0;
    integer tests_run = 0;
    integer i;

// Generated by generate_golden.py. Do not edit by hand.
localparam integer NUM_TESTS = 186;
reg test_rst [0:NUM_TESTS-1];
reg [11:0] test_sample [0:NUM_TESTS-1];
reg [11:0] test_threshold [0:NUM_TESTS-1];
reg expected_peak [0:NUM_TESTS-1];
reg [255:0] test_tag [0:NUM_TESTS-1];

initial begin
    test_rst[0] = 1'b1;
    test_sample[0] = 12'd0;
    test_threshold[0] = 12'd500;
    expected_peak[0] = 1'b0;
    test_tag[0] = "startup_reset";
    test_rst[1] = 1'b1;
    test_sample[1] = 12'd0;
    test_threshold[1] = 12'd500;
    expected_peak[1] = 1'b0;
    test_tag[1] = "startup_reset";
    test_rst[2] = 1'b0;
    test_sample[2] = 12'd20;
    test_threshold[2] = 12'd500;
    expected_peak[2] = 1'b0;
    test_tag[2] = "startup_fill";
    test_rst[3] = 1'b0;
    test_sample[3] = 12'd20;
    test_threshold[3] = 12'd500;
    expected_peak[3] = 1'b0;
    test_tag[3] = "startup_fill";
    test_rst[4] = 1'b0;
    test_sample[4] = 12'd20;
    test_threshold[4] = 12'd500;
    expected_peak[4] = 1'b0;
    test_tag[4] = "startup_fill";
    test_rst[5] = 1'b0;
    test_sample[5] = 12'd20;
    test_threshold[5] = 12'd500;
    expected_peak[5] = 1'b0;
    test_tag[5] = "startup_fill";
    test_rst[6] = 1'b1;
    test_sample[6] = 12'd0;
    test_threshold[6] = 12'd500;
    expected_peak[6] = 1'b0;
    test_tag[6] = "valid_reset";
    test_rst[7] = 1'b0;
    test_sample[7] = 12'd30;
    test_threshold[7] = 12'd500;
    expected_peak[7] = 1'b0;
    test_tag[7] = "valid_prefill";
    test_rst[8] = 1'b0;
    test_sample[8] = 12'd30;
    test_threshold[8] = 12'd500;
    expected_peak[8] = 1'b0;
    test_tag[8] = "valid_prefill";
    test_rst[9] = 1'b0;
    test_sample[9] = 12'd100;
    test_threshold[9] = 12'd500;
    expected_peak[9] = 1'b0;
    test_tag[9] = "valid_equal_threshold";
    test_rst[10] = 1'b0;
    test_sample[10] = 12'd300;
    test_threshold[10] = 12'd500;
    expected_peak[10] = 1'b0;
    test_tag[10] = "valid_equal_threshold";
    test_rst[11] = 1'b0;
    test_sample[11] = 12'd600;
    test_threshold[11] = 12'd600;
    expected_peak[11] = 1'b0;
    test_tag[11] = "valid_equal_threshold";
    test_rst[12] = 1'b0;
    test_sample[12] = 12'd250;
    test_threshold[12] = 12'd500;
    expected_peak[12] = 1'b0;
    test_tag[12] = "valid_equal_threshold";
    test_rst[13] = 1'b0;
    test_sample[13] = 12'd120;
    test_threshold[13] = 12'd500;
    expected_peak[13] = 1'b1;
    test_tag[13] = "valid_equal_threshold";
    test_rst[14] = 1'b0;
    test_sample[14] = 12'd40;
    test_threshold[14] = 12'd500;
    expected_peak[14] = 1'b0;
    test_tag[14] = "valid_post";
    test_rst[15] = 1'b0;
    test_sample[15] = 12'd40;
    test_threshold[15] = 12'd500;
    expected_peak[15] = 1'b0;
    test_tag[15] = "valid_post";
    test_rst[16] = 1'b0;
    test_sample[16] = 12'd40;
    test_threshold[16] = 12'd500;
    expected_peak[16] = 1'b0;
    test_tag[16] = "valid_post";
    test_rst[17] = 1'b1;
    test_sample[17] = 12'd0;
    test_threshold[17] = 12'd500;
    expected_peak[17] = 1'b0;
    test_tag[17] = "threshold_block_reset";
    test_rst[18] = 1'b0;
    test_sample[18] = 12'd30;
    test_threshold[18] = 12'd500;
    expected_peak[18] = 1'b0;
    test_tag[18] = "threshold_block_prefill";
    test_rst[19] = 1'b0;
    test_sample[19] = 12'd30;
    test_threshold[19] = 12'd500;
    expected_peak[19] = 1'b0;
    test_tag[19] = "threshold_block_prefill";
    test_rst[20] = 1'b0;
    test_sample[20] = 12'd100;
    test_threshold[20] = 12'd500;
    expected_peak[20] = 1'b0;
    test_tag[20] = "threshold_block_center";
    test_rst[21] = 1'b0;
    test_sample[21] = 12'd300;
    test_threshold[21] = 12'd500;
    expected_peak[21] = 1'b0;
    test_tag[21] = "threshold_block_center";
    test_rst[22] = 1'b0;
    test_sample[22] = 12'd600;
    test_threshold[22] = 12'd601;
    expected_peak[22] = 1'b0;
    test_tag[22] = "threshold_block_center";
    test_rst[23] = 1'b0;
    test_sample[23] = 12'd250;
    test_threshold[23] = 12'd500;
    expected_peak[23] = 1'b0;
    test_tag[23] = "threshold_block_center";
    test_rst[24] = 1'b0;
    test_sample[24] = 12'd120;
    test_threshold[24] = 12'd500;
    expected_peak[24] = 1'b0;
    test_tag[24] = "threshold_block_center";
    test_rst[25] = 1'b0;
    test_sample[25] = 12'd40;
    test_threshold[25] = 12'd500;
    expected_peak[25] = 1'b0;
    test_tag[25] = "threshold_block_post";
    test_rst[26] = 1'b0;
    test_sample[26] = 12'd40;
    test_threshold[26] = 12'd500;
    expected_peak[26] = 1'b0;
    test_tag[26] = "threshold_block_post";
    test_rst[27] = 1'b0;
    test_sample[27] = 12'd40;
    test_threshold[27] = 12'd500;
    expected_peak[27] = 1'b0;
    test_tag[27] = "threshold_block_post";
    test_rst[28] = 1'b1;
    test_sample[28] = 12'd0;
    test_threshold[28] = 12'd500;
    expected_peak[28] = 1'b0;
    test_tag[28] = "plateau_left_reset";
    test_rst[29] = 1'b0;
    test_sample[29] = 12'd25;
    test_threshold[29] = 12'd500;
    expected_peak[29] = 1'b0;
    test_tag[29] = "plateau_left_prefill";
    test_rst[30] = 1'b0;
    test_sample[30] = 12'd25;
    test_threshold[30] = 12'd500;
    expected_peak[30] = 1'b0;
    test_tag[30] = "plateau_left_prefill";
    test_rst[31] = 1'b0;
    test_sample[31] = 12'd100;
    test_threshold[31] = 12'd500;
    expected_peak[31] = 1'b0;
    test_tag[31] = "plateau_left_equal";
    test_rst[32] = 1'b0;
    test_sample[32] = 12'd700;
    test_threshold[32] = 12'd500;
    expected_peak[32] = 1'b0;
    test_tag[32] = "plateau_left_equal";
    test_rst[33] = 1'b0;
    test_sample[33] = 12'd700;
    test_threshold[33] = 12'd500;
    expected_peak[33] = 1'b0;
    test_tag[33] = "plateau_left_equal";
    test_rst[34] = 1'b0;
    test_sample[34] = 12'd300;
    test_threshold[34] = 12'd500;
    expected_peak[34] = 1'b0;
    test_tag[34] = "plateau_left_equal";
    test_rst[35] = 1'b0;
    test_sample[35] = 12'd100;
    test_threshold[35] = 12'd500;
    expected_peak[35] = 1'b0;
    test_tag[35] = "plateau_left_equal";
    test_rst[36] = 1'b0;
    test_sample[36] = 12'd40;
    test_threshold[36] = 12'd500;
    expected_peak[36] = 1'b0;
    test_tag[36] = "plateau_left_post";
    test_rst[37] = 1'b0;
    test_sample[37] = 12'd40;
    test_threshold[37] = 12'd500;
    expected_peak[37] = 1'b0;
    test_tag[37] = "plateau_left_post";
    test_rst[38] = 1'b0;
    test_sample[38] = 12'd40;
    test_threshold[38] = 12'd500;
    expected_peak[38] = 1'b0;
    test_tag[38] = "plateau_left_post";
    test_rst[39] = 1'b1;
    test_sample[39] = 12'd0;
    test_threshold[39] = 12'd500;
    expected_peak[39] = 1'b0;
    test_tag[39] = "plateau_right_reset";
    test_rst[40] = 1'b0;
    test_sample[40] = 12'd25;
    test_threshold[40] = 12'd500;
    expected_peak[40] = 1'b0;
    test_tag[40] = "plateau_right_prefill";
    test_rst[41] = 1'b0;
    test_sample[41] = 12'd25;
    test_threshold[41] = 12'd500;
    expected_peak[41] = 1'b0;
    test_tag[41] = "plateau_right_prefill";
    test_rst[42] = 1'b0;
    test_sample[42] = 12'd100;
    test_threshold[42] = 12'd500;
    expected_peak[42] = 1'b0;
    test_tag[42] = "plateau_right_equal";
    test_rst[43] = 1'b0;
    test_sample[43] = 12'd300;
    test_threshold[43] = 12'd500;
    expected_peak[43] = 1'b0;
    test_tag[43] = "plateau_right_equal";
    test_rst[44] = 1'b0;
    test_sample[44] = 12'd700;
    test_threshold[44] = 12'd500;
    expected_peak[44] = 1'b0;
    test_tag[44] = "plateau_right_equal";
    test_rst[45] = 1'b0;
    test_sample[45] = 12'd700;
    test_threshold[45] = 12'd500;
    expected_peak[45] = 1'b0;
    test_tag[45] = "plateau_right_equal";
    test_rst[46] = 1'b0;
    test_sample[46] = 12'd100;
    test_threshold[46] = 12'd500;
    expected_peak[46] = 1'b0;
    test_tag[46] = "plateau_right_equal";
    test_rst[47] = 1'b0;
    test_sample[47] = 12'd40;
    test_threshold[47] = 12'd500;
    expected_peak[47] = 1'b0;
    test_tag[47] = "plateau_right_post";
    test_rst[48] = 1'b0;
    test_sample[48] = 12'd40;
    test_threshold[48] = 12'd500;
    expected_peak[48] = 1'b0;
    test_tag[48] = "plateau_right_post";
    test_rst[49] = 1'b0;
    test_sample[49] = 12'd40;
    test_threshold[49] = 12'd500;
    expected_peak[49] = 1'b0;
    test_tag[49] = "plateau_right_post";
    test_rst[50] = 1'b1;
    test_sample[50] = 12'd0;
    test_threshold[50] = 12'd500;
    expected_peak[50] = 1'b0;
    test_tag[50] = "refractory_block_reset";
    test_rst[51] = 1'b0;
    test_sample[51] = 12'd20;
    test_threshold[51] = 12'd500;
    expected_peak[51] = 1'b0;
    test_tag[51] = "refractory_block_prefill";
    test_rst[52] = 1'b0;
    test_sample[52] = 12'd20;
    test_threshold[52] = 12'd500;
    expected_peak[52] = 1'b0;
    test_tag[52] = "refractory_block_prefill";
    test_rst[53] = 1'b0;
    test_sample[53] = 12'd100;
    test_threshold[53] = 12'd500;
    expected_peak[53] = 1'b0;
    test_tag[53] = "refractory_block_first_peak";
    test_rst[54] = 1'b0;
    test_sample[54] = 12'd300;
    test_threshold[54] = 12'd500;
    expected_peak[54] = 1'b0;
    test_tag[54] = "refractory_block_first_peak";
    test_rst[55] = 1'b0;
    test_sample[55] = 12'd900;
    test_threshold[55] = 12'd500;
    expected_peak[55] = 1'b0;
    test_tag[55] = "refractory_block_first_peak";
    test_rst[56] = 1'b0;
    test_sample[56] = 12'd250;
    test_threshold[56] = 12'd500;
    expected_peak[56] = 1'b0;
    test_tag[56] = "refractory_block_first_peak";
    test_rst[57] = 1'b0;
    test_sample[57] = 12'd100;
    test_threshold[57] = 12'd500;
    expected_peak[57] = 1'b1;
    test_tag[57] = "refractory_block_first_peak";
    test_rst[58] = 1'b0;
    test_sample[58] = 12'd15;
    test_threshold[58] = 12'd500;
    expected_peak[58] = 1'b0;
    test_tag[58] = "refractory_block_gap";
    test_rst[59] = 1'b0;
    test_sample[59] = 12'd15;
    test_threshold[59] = 12'd500;
    expected_peak[59] = 1'b0;
    test_tag[59] = "refractory_block_gap";
    test_rst[60] = 1'b0;
    test_sample[60] = 12'd15;
    test_threshold[60] = 12'd500;
    expected_peak[60] = 1'b0;
    test_tag[60] = "refractory_block_gap";
    test_rst[61] = 1'b0;
    test_sample[61] = 12'd15;
    test_threshold[61] = 12'd500;
    expected_peak[61] = 1'b0;
    test_tag[61] = "refractory_block_gap";
    test_rst[62] = 1'b0;
    test_sample[62] = 12'd15;
    test_threshold[62] = 12'd500;
    expected_peak[62] = 1'b0;
    test_tag[62] = "refractory_block_gap";
    test_rst[63] = 1'b0;
    test_sample[63] = 12'd15;
    test_threshold[63] = 12'd500;
    expected_peak[63] = 1'b0;
    test_tag[63] = "refractory_block_gap";
    test_rst[64] = 1'b0;
    test_sample[64] = 12'd15;
    test_threshold[64] = 12'd500;
    expected_peak[64] = 1'b0;
    test_tag[64] = "refractory_block_gap";
    test_rst[65] = 1'b0;
    test_sample[65] = 12'd15;
    test_threshold[65] = 12'd500;
    expected_peak[65] = 1'b0;
    test_tag[65] = "refractory_block_gap";
    test_rst[66] = 1'b0;
    test_sample[66] = 12'd15;
    test_threshold[66] = 12'd500;
    expected_peak[66] = 1'b0;
    test_tag[66] = "refractory_block_gap";
    test_rst[67] = 1'b0;
    test_sample[67] = 12'd15;
    test_threshold[67] = 12'd500;
    expected_peak[67] = 1'b0;
    test_tag[67] = "refractory_block_gap";
    test_rst[68] = 1'b0;
    test_sample[68] = 12'd15;
    test_threshold[68] = 12'd500;
    expected_peak[68] = 1'b0;
    test_tag[68] = "refractory_block_gap";
    test_rst[69] = 1'b0;
    test_sample[69] = 12'd15;
    test_threshold[69] = 12'd500;
    expected_peak[69] = 1'b0;
    test_tag[69] = "refractory_block_gap";
    test_rst[70] = 1'b0;
    test_sample[70] = 12'd15;
    test_threshold[70] = 12'd500;
    expected_peak[70] = 1'b0;
    test_tag[70] = "refractory_block_gap";
    test_rst[71] = 1'b0;
    test_sample[71] = 12'd15;
    test_threshold[71] = 12'd500;
    expected_peak[71] = 1'b0;
    test_tag[71] = "refractory_block_gap";
    test_rst[72] = 1'b0;
    test_sample[72] = 12'd15;
    test_threshold[72] = 12'd500;
    expected_peak[72] = 1'b0;
    test_tag[72] = "refractory_block_gap";
    test_rst[73] = 1'b0;
    test_sample[73] = 12'd15;
    test_threshold[73] = 12'd500;
    expected_peak[73] = 1'b0;
    test_tag[73] = "refractory_block_gap";
    test_rst[74] = 1'b0;
    test_sample[74] = 12'd15;
    test_threshold[74] = 12'd500;
    expected_peak[74] = 1'b0;
    test_tag[74] = "refractory_block_gap";
    test_rst[75] = 1'b0;
    test_sample[75] = 12'd15;
    test_threshold[75] = 12'd500;
    expected_peak[75] = 1'b0;
    test_tag[75] = "refractory_block_gap";
    test_rst[76] = 1'b0;
    test_sample[76] = 12'd15;
    test_threshold[76] = 12'd500;
    expected_peak[76] = 1'b0;
    test_tag[76] = "refractory_block_gap";
    test_rst[77] = 1'b0;
    test_sample[77] = 12'd15;
    test_threshold[77] = 12'd500;
    expected_peak[77] = 1'b0;
    test_tag[77] = "refractory_block_gap";
    test_rst[78] = 1'b0;
    test_sample[78] = 12'd15;
    test_threshold[78] = 12'd500;
    expected_peak[78] = 1'b0;
    test_tag[78] = "refractory_block_gap";
    test_rst[79] = 1'b0;
    test_sample[79] = 12'd15;
    test_threshold[79] = 12'd500;
    expected_peak[79] = 1'b0;
    test_tag[79] = "refractory_block_gap";
    test_rst[80] = 1'b0;
    test_sample[80] = 12'd15;
    test_threshold[80] = 12'd500;
    expected_peak[80] = 1'b0;
    test_tag[80] = "refractory_block_gap";
    test_rst[81] = 1'b0;
    test_sample[81] = 12'd15;
    test_threshold[81] = 12'd500;
    expected_peak[81] = 1'b0;
    test_tag[81] = "refractory_block_gap";
    test_rst[82] = 1'b0;
    test_sample[82] = 12'd15;
    test_threshold[82] = 12'd500;
    expected_peak[82] = 1'b0;
    test_tag[82] = "refractory_block_gap";
    test_rst[83] = 1'b0;
    test_sample[83] = 12'd120;
    test_threshold[83] = 12'd500;
    expected_peak[83] = 1'b0;
    test_tag[83] = "refractory_block_second_peak";
    test_rst[84] = 1'b0;
    test_sample[84] = 12'd350;
    test_threshold[84] = 12'd500;
    expected_peak[84] = 1'b0;
    test_tag[84] = "refractory_block_second_peak";
    test_rst[85] = 1'b0;
    test_sample[85] = 12'd950;
    test_threshold[85] = 12'd500;
    expected_peak[85] = 1'b0;
    test_tag[85] = "refractory_block_second_peak";
    test_rst[86] = 1'b0;
    test_sample[86] = 12'd260;
    test_threshold[86] = 12'd500;
    expected_peak[86] = 1'b0;
    test_tag[86] = "refractory_block_second_peak";
    test_rst[87] = 1'b0;
    test_sample[87] = 12'd110;
    test_threshold[87] = 12'd500;
    expected_peak[87] = 1'b0;
    test_tag[87] = "refractory_block_second_peak";
    test_rst[88] = 1'b0;
    test_sample[88] = 12'd20;
    test_threshold[88] = 12'd500;
    expected_peak[88] = 1'b0;
    test_tag[88] = "refractory_block_post";
    test_rst[89] = 1'b0;
    test_sample[89] = 12'd20;
    test_threshold[89] = 12'd500;
    expected_peak[89] = 1'b0;
    test_tag[89] = "refractory_block_post";
    test_rst[90] = 1'b0;
    test_sample[90] = 12'd20;
    test_threshold[90] = 12'd500;
    expected_peak[90] = 1'b0;
    test_tag[90] = "refractory_block_post";
    test_rst[91] = 1'b1;
    test_sample[91] = 12'd0;
    test_threshold[91] = 12'd500;
    expected_peak[91] = 1'b0;
    test_tag[91] = "refractory_boundary_reset";
    test_rst[92] = 1'b0;
    test_sample[92] = 12'd20;
    test_threshold[92] = 12'd500;
    expected_peak[92] = 1'b0;
    test_tag[92] = "refractory_boundary_prefill";
    test_rst[93] = 1'b0;
    test_sample[93] = 12'd20;
    test_threshold[93] = 12'd500;
    expected_peak[93] = 1'b0;
    test_tag[93] = "refractory_boundary_prefill";
    test_rst[94] = 1'b0;
    test_sample[94] = 12'd100;
    test_threshold[94] = 12'd500;
    expected_peak[94] = 1'b0;
    test_tag[94] = "refractory_boundary_first_peak";
    test_rst[95] = 1'b0;
    test_sample[95] = 12'd300;
    test_threshold[95] = 12'd500;
    expected_peak[95] = 1'b0;
    test_tag[95] = "refractory_boundary_first_peak";
    test_rst[96] = 1'b0;
    test_sample[96] = 12'd900;
    test_threshold[96] = 12'd500;
    expected_peak[96] = 1'b0;
    test_tag[96] = "refractory_boundary_first_peak";
    test_rst[97] = 1'b0;
    test_sample[97] = 12'd250;
    test_threshold[97] = 12'd500;
    expected_peak[97] = 1'b0;
    test_tag[97] = "refractory_boundary_first_peak";
    test_rst[98] = 1'b0;
    test_sample[98] = 12'd100;
    test_threshold[98] = 12'd500;
    expected_peak[98] = 1'b1;
    test_tag[98] = "refractory_boundary_first_peak";
    test_rst[99] = 1'b0;
    test_sample[99] = 12'd15;
    test_threshold[99] = 12'd500;
    expected_peak[99] = 1'b0;
    test_tag[99] = "refractory_boundary_gap";
    test_rst[100] = 1'b0;
    test_sample[100] = 12'd15;
    test_threshold[100] = 12'd500;
    expected_peak[100] = 1'b0;
    test_tag[100] = "refractory_boundary_gap";
    test_rst[101] = 1'b0;
    test_sample[101] = 12'd15;
    test_threshold[101] = 12'd500;
    expected_peak[101] = 1'b0;
    test_tag[101] = "refractory_boundary_gap";
    test_rst[102] = 1'b0;
    test_sample[102] = 12'd15;
    test_threshold[102] = 12'd500;
    expected_peak[102] = 1'b0;
    test_tag[102] = "refractory_boundary_gap";
    test_rst[103] = 1'b0;
    test_sample[103] = 12'd15;
    test_threshold[103] = 12'd500;
    expected_peak[103] = 1'b0;
    test_tag[103] = "refractory_boundary_gap";
    test_rst[104] = 1'b0;
    test_sample[104] = 12'd15;
    test_threshold[104] = 12'd500;
    expected_peak[104] = 1'b0;
    test_tag[104] = "refractory_boundary_gap";
    test_rst[105] = 1'b0;
    test_sample[105] = 12'd15;
    test_threshold[105] = 12'd500;
    expected_peak[105] = 1'b0;
    test_tag[105] = "refractory_boundary_gap";
    test_rst[106] = 1'b0;
    test_sample[106] = 12'd15;
    test_threshold[106] = 12'd500;
    expected_peak[106] = 1'b0;
    test_tag[106] = "refractory_boundary_gap";
    test_rst[107] = 1'b0;
    test_sample[107] = 12'd15;
    test_threshold[107] = 12'd500;
    expected_peak[107] = 1'b0;
    test_tag[107] = "refractory_boundary_gap";
    test_rst[108] = 1'b0;
    test_sample[108] = 12'd15;
    test_threshold[108] = 12'd500;
    expected_peak[108] = 1'b0;
    test_tag[108] = "refractory_boundary_gap";
    test_rst[109] = 1'b0;
    test_sample[109] = 12'd15;
    test_threshold[109] = 12'd500;
    expected_peak[109] = 1'b0;
    test_tag[109] = "refractory_boundary_gap";
    test_rst[110] = 1'b0;
    test_sample[110] = 12'd15;
    test_threshold[110] = 12'd500;
    expected_peak[110] = 1'b0;
    test_tag[110] = "refractory_boundary_gap";
    test_rst[111] = 1'b0;
    test_sample[111] = 12'd15;
    test_threshold[111] = 12'd500;
    expected_peak[111] = 1'b0;
    test_tag[111] = "refractory_boundary_gap";
    test_rst[112] = 1'b0;
    test_sample[112] = 12'd15;
    test_threshold[112] = 12'd500;
    expected_peak[112] = 1'b0;
    test_tag[112] = "refractory_boundary_gap";
    test_rst[113] = 1'b0;
    test_sample[113] = 12'd15;
    test_threshold[113] = 12'd500;
    expected_peak[113] = 1'b0;
    test_tag[113] = "refractory_boundary_gap";
    test_rst[114] = 1'b0;
    test_sample[114] = 12'd15;
    test_threshold[114] = 12'd500;
    expected_peak[114] = 1'b0;
    test_tag[114] = "refractory_boundary_gap";
    test_rst[115] = 1'b0;
    test_sample[115] = 12'd15;
    test_threshold[115] = 12'd500;
    expected_peak[115] = 1'b0;
    test_tag[115] = "refractory_boundary_gap";
    test_rst[116] = 1'b0;
    test_sample[116] = 12'd15;
    test_threshold[116] = 12'd500;
    expected_peak[116] = 1'b0;
    test_tag[116] = "refractory_boundary_gap";
    test_rst[117] = 1'b0;
    test_sample[117] = 12'd15;
    test_threshold[117] = 12'd500;
    expected_peak[117] = 1'b0;
    test_tag[117] = "refractory_boundary_gap";
    test_rst[118] = 1'b0;
    test_sample[118] = 12'd15;
    test_threshold[118] = 12'd500;
    expected_peak[118] = 1'b0;
    test_tag[118] = "refractory_boundary_gap";
    test_rst[119] = 1'b0;
    test_sample[119] = 12'd15;
    test_threshold[119] = 12'd500;
    expected_peak[119] = 1'b0;
    test_tag[119] = "refractory_boundary_gap";
    test_rst[120] = 1'b0;
    test_sample[120] = 12'd15;
    test_threshold[120] = 12'd500;
    expected_peak[120] = 1'b0;
    test_tag[120] = "refractory_boundary_gap";
    test_rst[121] = 1'b0;
    test_sample[121] = 12'd15;
    test_threshold[121] = 12'd500;
    expected_peak[121] = 1'b0;
    test_tag[121] = "refractory_boundary_gap";
    test_rst[122] = 1'b0;
    test_sample[122] = 12'd15;
    test_threshold[122] = 12'd500;
    expected_peak[122] = 1'b0;
    test_tag[122] = "refractory_boundary_gap";
    test_rst[123] = 1'b0;
    test_sample[123] = 12'd15;
    test_threshold[123] = 12'd500;
    expected_peak[123] = 1'b0;
    test_tag[123] = "refractory_boundary_gap";
    test_rst[124] = 1'b0;
    test_sample[124] = 12'd15;
    test_threshold[124] = 12'd500;
    expected_peak[124] = 1'b0;
    test_tag[124] = "refractory_boundary_gap";
    test_rst[125] = 1'b0;
    test_sample[125] = 12'd15;
    test_threshold[125] = 12'd500;
    expected_peak[125] = 1'b0;
    test_tag[125] = "refractory_boundary_gap";
    test_rst[126] = 1'b0;
    test_sample[126] = 12'd15;
    test_threshold[126] = 12'd500;
    expected_peak[126] = 1'b0;
    test_tag[126] = "refractory_boundary_gap";
    test_rst[127] = 1'b0;
    test_sample[127] = 12'd15;
    test_threshold[127] = 12'd500;
    expected_peak[127] = 1'b0;
    test_tag[127] = "refractory_boundary_gap";
    test_rst[128] = 1'b0;
    test_sample[128] = 12'd15;
    test_threshold[128] = 12'd500;
    expected_peak[128] = 1'b0;
    test_tag[128] = "refractory_boundary_gap";
    test_rst[129] = 1'b0;
    test_sample[129] = 12'd15;
    test_threshold[129] = 12'd500;
    expected_peak[129] = 1'b0;
    test_tag[129] = "refractory_boundary_gap";
    test_rst[130] = 1'b0;
    test_sample[130] = 12'd15;
    test_threshold[130] = 12'd500;
    expected_peak[130] = 1'b0;
    test_tag[130] = "refractory_boundary_gap";
    test_rst[131] = 1'b0;
    test_sample[131] = 12'd15;
    test_threshold[131] = 12'd500;
    expected_peak[131] = 1'b0;
    test_tag[131] = "refractory_boundary_gap";
    test_rst[132] = 1'b0;
    test_sample[132] = 12'd15;
    test_threshold[132] = 12'd500;
    expected_peak[132] = 1'b0;
    test_tag[132] = "refractory_boundary_gap";
    test_rst[133] = 1'b0;
    test_sample[133] = 12'd15;
    test_threshold[133] = 12'd500;
    expected_peak[133] = 1'b0;
    test_tag[133] = "refractory_boundary_gap";
    test_rst[134] = 1'b0;
    test_sample[134] = 12'd15;
    test_threshold[134] = 12'd500;
    expected_peak[134] = 1'b0;
    test_tag[134] = "refractory_boundary_gap";
    test_rst[135] = 1'b0;
    test_sample[135] = 12'd15;
    test_threshold[135] = 12'd500;
    expected_peak[135] = 1'b0;
    test_tag[135] = "refractory_boundary_gap";
    test_rst[136] = 1'b0;
    test_sample[136] = 12'd15;
    test_threshold[136] = 12'd500;
    expected_peak[136] = 1'b0;
    test_tag[136] = "refractory_boundary_gap";
    test_rst[137] = 1'b0;
    test_sample[137] = 12'd15;
    test_threshold[137] = 12'd500;
    expected_peak[137] = 1'b0;
    test_tag[137] = "refractory_boundary_gap";
    test_rst[138] = 1'b0;
    test_sample[138] = 12'd15;
    test_threshold[138] = 12'd500;
    expected_peak[138] = 1'b0;
    test_tag[138] = "refractory_boundary_gap";
    test_rst[139] = 1'b0;
    test_sample[139] = 12'd15;
    test_threshold[139] = 12'd500;
    expected_peak[139] = 1'b0;
    test_tag[139] = "refractory_boundary_gap";
    test_rst[140] = 1'b0;
    test_sample[140] = 12'd15;
    test_threshold[140] = 12'd500;
    expected_peak[140] = 1'b0;
    test_tag[140] = "refractory_boundary_gap";
    test_rst[141] = 1'b0;
    test_sample[141] = 12'd15;
    test_threshold[141] = 12'd500;
    expected_peak[141] = 1'b0;
    test_tag[141] = "refractory_boundary_gap";
    test_rst[142] = 1'b0;
    test_sample[142] = 12'd15;
    test_threshold[142] = 12'd500;
    expected_peak[142] = 1'b0;
    test_tag[142] = "refractory_boundary_gap";
    test_rst[143] = 1'b0;
    test_sample[143] = 12'd15;
    test_threshold[143] = 12'd500;
    expected_peak[143] = 1'b0;
    test_tag[143] = "refractory_boundary_gap";
    test_rst[144] = 1'b0;
    test_sample[144] = 12'd120;
    test_threshold[144] = 12'd500;
    expected_peak[144] = 1'b0;
    test_tag[144] = "refractory_boundary_second_peak";
    test_rst[145] = 1'b0;
    test_sample[145] = 12'd350;
    test_threshold[145] = 12'd500;
    expected_peak[145] = 1'b0;
    test_tag[145] = "refractory_boundary_second_peak";
    test_rst[146] = 1'b0;
    test_sample[146] = 12'd950;
    test_threshold[146] = 12'd500;
    expected_peak[146] = 1'b0;
    test_tag[146] = "refractory_boundary_second_peak";
    test_rst[147] = 1'b0;
    test_sample[147] = 12'd260;
    test_threshold[147] = 12'd500;
    expected_peak[147] = 1'b0;
    test_tag[147] = "refractory_boundary_second_peak";
    test_rst[148] = 1'b0;
    test_sample[148] = 12'd110;
    test_threshold[148] = 12'd500;
    expected_peak[148] = 1'b1;
    test_tag[148] = "refractory_boundary_second_peak";
    test_rst[149] = 1'b0;
    test_sample[149] = 12'd20;
    test_threshold[149] = 12'd500;
    expected_peak[149] = 1'b0;
    test_tag[149] = "refractory_boundary_post";
    test_rst[150] = 1'b0;
    test_sample[150] = 12'd20;
    test_threshold[150] = 12'd500;
    expected_peak[150] = 1'b0;
    test_tag[150] = "refractory_boundary_post";
    test_rst[151] = 1'b0;
    test_sample[151] = 12'd20;
    test_threshold[151] = 12'd500;
    expected_peak[151] = 1'b0;
    test_tag[151] = "refractory_boundary_post";
    test_rst[152] = 1'b1;
    test_sample[152] = 12'd0;
    test_threshold[152] = 12'd500;
    expected_peak[152] = 1'b0;
    test_tag[152] = "reset_clears_reset";
    test_rst[153] = 1'b0;
    test_sample[153] = 12'd20;
    test_threshold[153] = 12'd500;
    expected_peak[153] = 1'b0;
    test_tag[153] = "reset_clears_prefill";
    test_rst[154] = 1'b0;
    test_sample[154] = 12'd20;
    test_threshold[154] = 12'd500;
    expected_peak[154] = 1'b0;
    test_tag[154] = "reset_clears_prefill";
    test_rst[155] = 1'b0;
    test_sample[155] = 12'd100;
    test_threshold[155] = 12'd500;
    expected_peak[155] = 1'b0;
    test_tag[155] = "reset_clears_first_peak";
    test_rst[156] = 1'b0;
    test_sample[156] = 12'd300;
    test_threshold[156] = 12'd500;
    expected_peak[156] = 1'b0;
    test_tag[156] = "reset_clears_first_peak";
    test_rst[157] = 1'b0;
    test_sample[157] = 12'd900;
    test_threshold[157] = 12'd500;
    expected_peak[157] = 1'b0;
    test_tag[157] = "reset_clears_first_peak";
    test_rst[158] = 1'b0;
    test_sample[158] = 12'd250;
    test_threshold[158] = 12'd500;
    expected_peak[158] = 1'b0;
    test_tag[158] = "reset_clears_first_peak";
    test_rst[159] = 1'b0;
    test_sample[159] = 12'd100;
    test_threshold[159] = 12'd500;
    expected_peak[159] = 1'b1;
    test_tag[159] = "reset_clears_first_peak";
    test_rst[160] = 1'b0;
    test_sample[160] = 12'd15;
    test_threshold[160] = 12'd500;
    expected_peak[160] = 1'b0;
    test_tag[160] = "reset_clears_gap";
    test_rst[161] = 1'b0;
    test_sample[161] = 12'd15;
    test_threshold[161] = 12'd500;
    expected_peak[161] = 1'b0;
    test_tag[161] = "reset_clears_gap";
    test_rst[162] = 1'b0;
    test_sample[162] = 12'd15;
    test_threshold[162] = 12'd500;
    expected_peak[162] = 1'b0;
    test_tag[162] = "reset_clears_gap";
    test_rst[163] = 1'b0;
    test_sample[163] = 12'd15;
    test_threshold[163] = 12'd500;
    expected_peak[163] = 1'b0;
    test_tag[163] = "reset_clears_gap";
    test_rst[164] = 1'b0;
    test_sample[164] = 12'd15;
    test_threshold[164] = 12'd500;
    expected_peak[164] = 1'b0;
    test_tag[164] = "reset_clears_gap";
    test_rst[165] = 1'b0;
    test_sample[165] = 12'd15;
    test_threshold[165] = 12'd500;
    expected_peak[165] = 1'b0;
    test_tag[165] = "reset_clears_gap";
    test_rst[166] = 1'b0;
    test_sample[166] = 12'd15;
    test_threshold[166] = 12'd500;
    expected_peak[166] = 1'b0;
    test_tag[166] = "reset_clears_gap";
    test_rst[167] = 1'b0;
    test_sample[167] = 12'd15;
    test_threshold[167] = 12'd500;
    expected_peak[167] = 1'b0;
    test_tag[167] = "reset_clears_gap";
    test_rst[168] = 1'b0;
    test_sample[168] = 12'd15;
    test_threshold[168] = 12'd500;
    expected_peak[168] = 1'b0;
    test_tag[168] = "reset_clears_gap";
    test_rst[169] = 1'b0;
    test_sample[169] = 12'd15;
    test_threshold[169] = 12'd500;
    expected_peak[169] = 1'b0;
    test_tag[169] = "reset_clears_gap";
    test_rst[170] = 1'b0;
    test_sample[170] = 12'd15;
    test_threshold[170] = 12'd500;
    expected_peak[170] = 1'b0;
    test_tag[170] = "reset_clears_gap";
    test_rst[171] = 1'b0;
    test_sample[171] = 12'd15;
    test_threshold[171] = 12'd500;
    expected_peak[171] = 1'b0;
    test_tag[171] = "reset_clears_gap";
    test_rst[172] = 1'b0;
    test_sample[172] = 12'd15;
    test_threshold[172] = 12'd500;
    expected_peak[172] = 1'b0;
    test_tag[172] = "reset_clears_gap";
    test_rst[173] = 1'b0;
    test_sample[173] = 12'd15;
    test_threshold[173] = 12'd500;
    expected_peak[173] = 1'b0;
    test_tag[173] = "reset_clears_gap";
    test_rst[174] = 1'b0;
    test_sample[174] = 12'd15;
    test_threshold[174] = 12'd500;
    expected_peak[174] = 1'b0;
    test_tag[174] = "reset_clears_gap";
    test_rst[175] = 1'b1;
    test_sample[175] = 12'd0;
    test_threshold[175] = 12'd500;
    expected_peak[175] = 1'b0;
    test_tag[175] = "reset_clears_mid_reset";
    test_rst[176] = 1'b0;
    test_sample[176] = 12'd20;
    test_threshold[176] = 12'd500;
    expected_peak[176] = 1'b0;
    test_tag[176] = "reset_clears_prefill_after";
    test_rst[177] = 1'b0;
    test_sample[177] = 12'd20;
    test_threshold[177] = 12'd500;
    expected_peak[177] = 1'b0;
    test_tag[177] = "reset_clears_prefill_after";
    test_rst[178] = 1'b0;
    test_sample[178] = 12'd120;
    test_threshold[178] = 12'd500;
    expected_peak[178] = 1'b0;
    test_tag[178] = "reset_clears_second_peak";
    test_rst[179] = 1'b0;
    test_sample[179] = 12'd350;
    test_threshold[179] = 12'd500;
    expected_peak[179] = 1'b0;
    test_tag[179] = "reset_clears_second_peak";
    test_rst[180] = 1'b0;
    test_sample[180] = 12'd950;
    test_threshold[180] = 12'd500;
    expected_peak[180] = 1'b0;
    test_tag[180] = "reset_clears_second_peak";
    test_rst[181] = 1'b0;
    test_sample[181] = 12'd260;
    test_threshold[181] = 12'd500;
    expected_peak[181] = 1'b0;
    test_tag[181] = "reset_clears_second_peak";
    test_rst[182] = 1'b0;
    test_sample[182] = 12'd110;
    test_threshold[182] = 12'd500;
    expected_peak[182] = 1'b1;
    test_tag[182] = "reset_clears_second_peak";
    test_rst[183] = 1'b0;
    test_sample[183] = 12'd20;
    test_threshold[183] = 12'd500;
    expected_peak[183] = 1'b0;
    test_tag[183] = "reset_clears_post";
    test_rst[184] = 1'b0;
    test_sample[184] = 12'd20;
    test_threshold[184] = 12'd500;
    expected_peak[184] = 1'b0;
    test_tag[184] = "reset_clears_post";
    test_rst[185] = 1'b0;
    test_sample[185] = 12'd20;
    test_threshold[185] = 12'd500;
    expected_peak[185] = 1'b0;
    test_tag[185] = "reset_clears_post";
end


    ecg_r_peak_detector uut (
        .clk(clk),
        .rst(rst),
        .sample_in(sample_in),
        .threshold(threshold),
        .peak_out(peak_out)
    );

    initial begin
        clk = 1'b0;
        forever #5 clk = ~clk;
    end

    task apply_cycle;
        input integer cycle_idx;
        begin
            rst = test_rst[cycle_idx];
            sample_in = test_sample[cycle_idx];
            threshold = test_threshold[cycle_idx];

            @(posedge clk);
            #1;

            tests_run = tests_run + 1;

            if (peak_out !== expected_peak[cycle_idx]) begin
                $display("ERROR [%s cycle %0d]: peak_out mismatch. expected=%b got=%b rst=%b sample=%0d threshold=%0d",
                         test_tag[cycle_idx], cycle_idx, expected_peak[cycle_idx], peak_out,
                         rst, sample_in, threshold);
                errors = errors + 1;
            end
        end
    endtask

    initial begin
        rst = 1'b0;
        sample_in = 12'd0;
        threshold = 12'd0;

        $display("===========================================");
        $display("      ECG R-Peak Detector Testbench");
        $display("===========================================");

        for (i = 0; i < NUM_TESTS; i = i + 1)
            apply_cycle(i);

        $display("");
        $display("===========================================");
        $display("  Tests Run: %0d", tests_run);
        $display("===========================================");

        if (errors == 0) begin
            $display("TEST_RESULT: PASS");
        end else begin
            $display("TEST_RESULT: FAIL (%0d errors)", errors);
        end

        $finish;
    end
endmodule
