thermal_control_hard_race_tb.v 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. `timescale 1ns/1ps
  2. module thermal_control_hard_race_tb;
  3. reg clk = 0, rst_n = 0, tick = 0;
  4. reg manual_wr = 0, manual_commit = 0;
  5. reg [4:0] manual_addr = 0;
  6. reg [7:0] manual_data = 0;
  7. reg [15:0] bmc_epoch = 0;
  8. reg bmc_stale = 1;
  9. reg [23:0] bmc_mask = 24'hffffff;
  10. reg [15:0] bmc_raw [0:23];
  11. wire [4:0] bmc_index;
  12. reg pwm_boundary = 1;
  13. wire manual_done, manual_ok, active_bank, output_commit;
  14. wire [7:0] watchdog_remaining;
  15. wire [2:0] owner;
  16. wire [3:0] lane_fault;
  17. wire [7:0] pwm_data;
  18. reg [4:0] pwm_addr = 0;
  19. integer i, timeout, commit_count = 0;
  20. always #5 clk = ~clk;
  21. always @(posedge clk) if (output_commit) commit_count <= commit_count + 1;
  22. thermal_control_core #(.WATCHDOG_TICKS_OVERRIDE(4)) dut (
  23. .clk(clk), .rst_n(rst_n), .tick_1s(tick),
  24. .manual_wr_en(manual_wr), .manual_wr_addr(manual_addr),
  25. .manual_wr_data(manual_data), .manual_commit(manual_commit),
  26. .manual_commit_done(manual_done), .manual_commit_ok(manual_ok),
  27. .pid_target_commit(1'b0), .pid_target_celsius(8'd70),
  28. .bmc_epoch(bmc_epoch), .bmc_stale(bmc_stale), .bmc_valid_mask(bmc_mask),
  29. .bmc_rd_index(bmc_index), .bmc_rd_raw(bmc_raw[bmc_index]),
  30. .bmc_rd_valid(bmc_mask[bmc_index]), .ct_epoch(16'd0), .ct_stale(1'b1),
  31. .ct_valid_mask(27'd0), .snapshot_lock(1'b0), .snapshot_lock_bank(1'b0),
  32. .pwm_rd_addr(pwm_addr), .pwm_rd_data(pwm_data),
  33. .snapshot_output_bank(active_bank), .snapshot_rd_addr(5'd0),
  34. .snapshot_requested_data(), .snapshot_applied_data(),
  35. .active_output_bank(active_bank), .control_state(owner),
  36. .current_target_celsius(), .control_epoch(), .safety_override_mask(),
  37. .lane_fault_mask(lane_fault), .global_fault(), .output_commit(output_commit),
  38. .watchdog_active(), .watchdog_remaining(watchdog_remaining),
  39. .pid_running(), .manual_mode(),
  40. .hard_protect(), .pwm_phase_boundary(pwm_boundary), .pwm_scan_idle(1'b1),
  41. .boot_init_complete()
  42. );
  43. task check_duty;
  44. input [4:0] address;
  45. input [7:0] expected;
  46. begin
  47. pwm_addr = address;
  48. repeat (2) @(negedge clk);
  49. if (pwm_data !== expected)
  50. $fatal(1, "duty[%0d]=%0d expected=%0d", address, pwm_data, expected);
  51. end
  52. endtask
  53. initial begin
  54. for (i = 0; i < 24; i = i + 1) bmc_raw[i] = {8'd60, 8'd0};
  55. repeat (4) @(negedge clk); rst_n = 1;
  56. repeat (40) @(negedge clk);
  57. // Put the lease on its final tick. Staging a SET_ALL must not feed it;
  58. // only the later, safely-applied output commit may reload it.
  59. repeat (3) begin
  60. @(negedge clk); tick = 1;
  61. @(negedge clk); tick = 0;
  62. end
  63. if (watchdog_remaining != 1)
  64. $fatal(1, "watchdog setup failed: remaining=%0d", watchdog_remaining);
  65. for (i = 0; i < 32; i = i + 1) begin
  66. @(negedge clk); manual_wr = 1; manual_addr = i[4:0];
  67. manual_data = (i < 24) ? 8'd80 : 8'd60;
  68. end
  69. @(negedge clk); manual_wr = 0;
  70. // Hold the atomic output boundary after the complete Manual Bank has
  71. // committed, then inject a new complete 77 C BMC epoch.
  72. pwm_boundary = 0;
  73. @(negedge clk); manual_commit = 1;
  74. @(negedge clk); manual_commit = 0;
  75. repeat (4) @(negedge clk);
  76. if (watchdog_remaining != 1)
  77. $fatal(1, "staged/cancelable SET_ALL fed watchdog: remaining=%0d",
  78. watchdog_remaining);
  79. timeout = 0;
  80. while (dut.work_state != 9 && timeout < 300) begin
  81. @(negedge clk); timeout = timeout + 1;
  82. end
  83. if (timeout == 300) $fatal(1, "manual never reached output commit wait");
  84. bmc_stale = 0;
  85. bmc_raw[0] = {8'd77, 8'd0};
  86. bmc_epoch = 1;
  87. timeout = 0;
  88. while (!manual_done && timeout < 500) begin
  89. @(negedge clk); timeout = timeout + 1;
  90. end
  91. if (timeout == 500 || manual_ok)
  92. $fatal(1, "in-flight manual was not cancelled by new hard lock");
  93. if (owner == 1) $fatal(1, "cancelled manual gained ownership");
  94. check_duty(0, 0);
  95. check_duty(24, 20);
  96. pwm_boundary = 1;
  97. timeout = 0;
  98. while ((!lane_fault[0] || commit_count == 0) && timeout < 500) begin
  99. @(negedge clk); timeout = timeout + 1;
  100. end
  101. if (timeout == 500 || owner != 2) $fatal(1, "hard safe commit missing");
  102. check_duty(0, 0);
  103. check_duty(24, 100);
  104. check_duty(28, 100);
  105. check_duty(25, 20);
  106. $display("PASS: new hard epoch cancels in-flight Manual before output Bank apply");
  107. $finish;
  108. end
  109. endmodule