thermal_control_core_tb.v 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. `timescale 1ns/1ps
  2. module thermal_control_core_tb;
  3. reg clk = 0, rst_n = 0, tick = 0;
  4. reg manual_wr = 0, manual_commit = 0, pid_commit = 0;
  5. reg [4:0] manual_addr = 0;
  6. reg [7:0] manual_data = 0, pid_target = 70;
  7. reg [15:0] bmc_epoch = 0;
  8. reg bmc_stale = 1;
  9. reg [23:0] bmc_mask = 24'hffffff;
  10. wire [4:0] bmc_index;
  11. reg [15:0] bmc_raw [0:23];
  12. reg [4:0] pwm_addr = 0;
  13. wire [7:0] pwm_data;
  14. wire manual_done, manual_ok, active_bank, output_commit;
  15. wire [2:0] mode;
  16. wire [7:0] current_target, watchdog_remaining;
  17. wire [15:0] control_epoch;
  18. wire [23:0] safety;
  19. wire [3:0] lane_fault;
  20. wire global_fault, watchdog_active, pid_running, manual_mode, hard_protect;
  21. integer i, commits = 0;
  22. always #5 clk = ~clk;
  23. always @(posedge clk) if (output_commit) commits <= commits + 1;
  24. thermal_control_core #(.WATCHDOG_TICKS_OVERRIDE(10)) dut (
  25. .clk(clk), .rst_n(rst_n), .tick_1s(tick),
  26. .manual_wr_en(manual_wr), .manual_wr_addr(manual_addr),
  27. .manual_wr_data(manual_data), .manual_commit(manual_commit),
  28. .manual_commit_done(manual_done), .manual_commit_ok(manual_ok),
  29. .pid_target_commit(pid_commit), .pid_target_celsius(pid_target),
  30. .bmc_epoch(bmc_epoch),
  31. .bmc_stale(bmc_stale), .bmc_valid_mask(bmc_mask),
  32. .bmc_rd_index(bmc_index), .bmc_rd_raw(bmc_raw[bmc_index]),
  33. .bmc_rd_valid(bmc_mask[bmc_index]), .ct_epoch(0), .ct_stale(1'b0),
  34. .ct_valid_mask(27'h7ffffff), .snapshot_lock(1'b0),
  35. .snapshot_lock_bank(1'b0), .pwm_rd_addr(pwm_addr), .pwm_rd_data(pwm_data),
  36. .snapshot_output_bank(active_bank), .snapshot_rd_addr(0),
  37. .snapshot_requested_data(), .snapshot_applied_data(),
  38. .active_output_bank(active_bank), .control_state(mode),
  39. .current_target_celsius(current_target), .control_epoch(control_epoch),
  40. .safety_override_mask(safety), .lane_fault_mask(lane_fault),
  41. .global_fault(global_fault), .output_commit(output_commit),
  42. .watchdog_active(watchdog_active),
  43. .watchdog_remaining(watchdog_remaining), .pid_running(pid_running),
  44. .manual_mode(manual_mode), .hard_protect(hard_protect),
  45. .pwm_phase_boundary(1'b1), .pwm_scan_idle(1'b1), .boot_init_complete()
  46. );
  47. task one_tick;
  48. begin @(negedge clk); tick = 1; @(negedge clk); tick = 0; end
  49. endtask
  50. task wait_commit;
  51. input integer prior;
  52. integer timeout;
  53. begin
  54. timeout = 0;
  55. while (commits == prior && timeout < 500) begin @(negedge clk); timeout = timeout + 1; end
  56. if (timeout == 500) $fatal(1, "commit timeout mode=%0d work=%0d", mode, dut.work_state);
  57. end
  58. endtask
  59. task wait_manual_commit;
  60. integer timeout;
  61. begin
  62. timeout = 0;
  63. while (!manual_done && timeout < 500) begin @(negedge clk); timeout = timeout + 1; end
  64. if (timeout == 500) $fatal(1, "manual commit timeout mode=%0d work=%0d", mode, dut.work_state);
  65. @(negedge clk);
  66. end
  67. endtask
  68. task check_duty;
  69. input [4:0] address;
  70. input [7:0] expected;
  71. begin
  72. pwm_addr = address;
  73. repeat (2) @(negedge clk);
  74. if (pwm_data !== expected) $fatal(1, "duty[%0d]=%0d expected=%0d", address, pwm_data, expected);
  75. end
  76. endtask
  77. task stage_manual;
  78. begin
  79. for (i = 0; i < 32; i = i + 1) begin
  80. @(negedge clk); manual_wr = 1; manual_addr = i[4:0];
  81. manual_data = (i < 24) ? 8'd50 : ((i[1:0] == 0) ? 0 : (i[1:0] == 1) ? 20 : (i[1:0] == 2) ? 50 : 100);
  82. end
  83. @(negedge clk); manual_wr = 0;
  84. end
  85. endtask
  86. initial begin
  87. for (i = 0; i < 24; i = i + 1) bmc_raw[i] = {8'd60, 8'd0};
  88. repeat (4) @(negedge clk); rst_n = 1;
  89. repeat (40) @(negedge clk);
  90. if (mode != 0 || !watchdog_active || pid_running || manual_mode || current_target != 70) $fatal;
  91. check_duty(0, 0); check_duty(24, 20); check_duty(31, 20);
  92. // First boot timeout atomically commits safe and enters PID wait.
  93. i = commits;
  94. repeat (10) one_tick();
  95. wait_commit(i);
  96. if (mode != 2 || watchdog_remaining != 0 || pid_running) $fatal;
  97. check_duty(0, 0); check_duty(24, 20);
  98. // A new complete valid BMC epoch starts PID; absence never integrates.
  99. bmc_stale = 0; bmc_epoch = 1;
  100. i = commits; wait_commit(i);
  101. if (mode != 2 || !pid_running) $fatal;
  102. // SET_PID_TARGET changes only the next PID target.
  103. @(negedge clk); pid_target = 65; pid_commit = 1;
  104. @(negedge clk); pid_commit = 0;
  105. if (current_target != 65 || mode != 2) $fatal;
  106. // SET_ALL atomically enters Manual, freezes PI and reloads the test override.
  107. stage_manual();
  108. i = commits;
  109. @(negedge clk); manual_commit = 1;
  110. @(negedge clk); manual_commit = 0;
  111. wait_manual_commit();
  112. if (mode != 1 || !manual_mode || watchdog_remaining != 10) $fatal;
  113. check_duty(0, 50); check_duty(24, 0); check_duty(25, 20);
  114. check_duty(26, 50); check_duty(27, 100);
  115. // Same vector feeds again; SET_ALL wins over the final timeout tick.
  116. repeat (9) one_tick();
  117. if (watchdog_remaining != 1) $fatal;
  118. stage_manual();
  119. i = commits;
  120. @(negedge clk); manual_commit = 1; tick = 1;
  121. @(negedge clk); manual_commit = 0; tick = 0;
  122. wait_manual_commit();
  123. if (mode != 1 || watchdog_remaining != 10) $fatal;
  124. // Manual timeout returns atomically to safe PID_WAIT_SENSOR.
  125. i = commits;
  126. repeat (10) one_tick();
  127. wait_commit(i);
  128. if (mode != 2 || manual_mode) $fatal;
  129. check_duty(0, 0); check_duty(24, 20);
  130. // A valid 76 C channel is clamped without changing Manual ownership.
  131. stage_manual();
  132. i = commits;
  133. @(negedge clk); manual_commit = 1;
  134. @(negedge clk); manual_commit = 0;
  135. wait_manual_commit();
  136. bmc_raw[0] = {8'd76, 8'd0}; bmc_epoch = 2;
  137. i = commits; wait_commit(i);
  138. if (mode != 1 || hard_protect || !safety[0]) $fatal;
  139. check_duty(0, 0); check_duty(1, 50); check_duty(24, 0);
  140. // Only confirmed 77 C hard protection may force lane fans to 100.
  141. bmc_raw[0] = {8'd77, 8'd0}; bmc_epoch = 2;
  142. bmc_epoch = 3;
  143. i = commits; wait_commit(i);
  144. if (mode != 2 || !hard_protect || !lane_fault[0]) $fatal;
  145. check_duty(0, 0); check_duty(24, 100); check_duty(28, 100);
  146. check_duty(25, 20);
  147. $display("PASS: SAFE/MANUAL/PID watchdog, 76C clamp, 77C hard-protect");
  148. $finish;
  149. end
  150. endmodule