thermal_control_core_tb.v 6.7 KB

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