thermal_control_core_tb.v 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. `timescale 1ns/1ps
  2. module thermal_control_core_tb;
  3. reg clk = 0;
  4. reg rst_n = 0;
  5. reg tick = 0;
  6. reg target_commit = 0;
  7. reg [7:0] target = 70;
  8. reg [15:0] bmc_epoch = 0;
  9. reg bmc_stale = 1;
  10. reg [23:0] bmc_valid_mask = 24'hffffff;
  11. wire [4:0] bmc_index;
  12. reg [15:0] bmc_raw [0:23];
  13. wire [15:0] bmc_data = bmc_raw[bmc_index];
  14. wire bmc_valid = bmc_valid_mask[bmc_index];
  15. reg [15:0] ct_epoch = 0;
  16. reg ct_stale = 0;
  17. reg [26:0] ct_valid_mask = 27'h7ffffff;
  18. reg [4:0] pwm_addr = 0;
  19. wire [7:0] pwm_data;
  20. reg [4:0] snapshot_addr = 0;
  21. wire [7:0] snapshot_req;
  22. wire [7:0] snapshot_app;
  23. wire active_bank;
  24. wire [2:0] state;
  25. wire [7:0] current_target;
  26. wire host_seen;
  27. wire [15:0] control_epoch;
  28. wire [23:0] safety;
  29. wire [3:0] lane_fault;
  30. wire global_fault;
  31. wire output_commit;
  32. wire stimulus_active;
  33. integer i;
  34. integer commits = 0;
  35. always #5 clk = ~clk;
  36. always @(posedge clk) if (output_commit) commits <= commits + 1;
  37. thermal_control_core dut (
  38. .clk(clk), .rst_n(rst_n), .tick_1s(tick), .target_commit(target_commit),
  39. .target_celsius(target), .stimulus_wr_en(1'b0), .stimulus_wr_addr(5'd0),
  40. .stimulus_wr_data(8'd0), .stimulus_commit(1'b0), .stimulus_duration_seconds(8'd0),
  41. .bmc_epoch(bmc_epoch), .bmc_stale(bmc_stale), .bmc_valid_mask(bmc_valid_mask),
  42. .bmc_rd_index(bmc_index), .bmc_rd_raw(bmc_data), .bmc_rd_valid(bmc_valid),
  43. .ct_epoch(ct_epoch), .ct_stale(ct_stale), .ct_valid_mask(ct_valid_mask),
  44. .snapshot_lock(1'b0), .snapshot_lock_bank(1'b0), .pwm_rd_addr(pwm_addr),
  45. .pwm_rd_data(pwm_data), .snapshot_output_bank(active_bank),
  46. .snapshot_rd_addr(snapshot_addr), .snapshot_requested_data(snapshot_req),
  47. .snapshot_applied_data(snapshot_app), .active_output_bank(active_bank),
  48. .control_state(state), .current_target_celsius(current_target),
  49. .host_target_seen(host_seen), .control_epoch(control_epoch),
  50. .safety_override_mask(safety), .lane_fault_mask(lane_fault),
  51. .global_fault(global_fault), .output_commit(output_commit), .stimulus_active(stimulus_active),
  52. .pwm_phase_boundary(1'b1), .pwm_scan_idle(1'b1), .boot_init_complete()
  53. );
  54. task next_bmc_epoch;
  55. begin
  56. @(negedge clk); bmc_epoch = bmc_epoch + 1'b1;
  57. repeat (3) @(negedge clk);
  58. end
  59. endtask
  60. task wait_commit_after;
  61. input integer old_count;
  62. integer timeout;
  63. begin
  64. timeout = 0;
  65. while (commits == old_count && timeout < 1000) begin @(negedge clk); timeout = timeout + 1; end
  66. if (timeout >= 1000) begin
  67. $display("commit timeout state=%0d work=%0d index=%0d last_bmc=%0d input_epoch=%0d", state, dut.work_state, dut.work_index, dut.last_bmc_epoch, bmc_epoch);
  68. $fatal;
  69. end
  70. end
  71. endtask
  72. task check_duty;
  73. input [4:0] address;
  74. input [7:0] expected;
  75. begin
  76. pwm_addr = address;
  77. repeat (2) @(negedge clk);
  78. if (pwm_data !== expected) begin
  79. $display("duty[%0d]=%0d expected=%0d", address, pwm_data, expected);
  80. $fatal;
  81. end
  82. end
  83. endtask
  84. initial begin
  85. for (i = 0; i < 24; i = i + 1) bmc_raw[i] = {8'd60, 8'd0};
  86. repeat (4) @(negedge clk);
  87. rst_n = 1;
  88. // The RAM-backed controller deterministically initializes both output
  89. // banks and all PI-state words before leaving RESET_SAFE.
  90. repeat (36) @(negedge clk);
  91. if (state !== 1 || global_fault) $fatal;
  92. ct_epoch = 1;
  93. repeat (3) @(negedge clk);
  94. if (state !== 2 || global_fault) $fatal;
  95. bmc_stale = 0;
  96. next_bmc_epoch();
  97. if (state !== 3) $fatal;
  98. next_bmc_epoch();
  99. next_bmc_epoch();
  100. next_bmc_epoch();
  101. wait_commit_after(0);
  102. if (state !== 4 || control_epoch == 0) $fatal;
  103. check_duty(0, 5);
  104. check_duty(24, 30);
  105. // 74 C is a soft reduction, not a shutdown: retain a reduced PTC
  106. // request and raise both fans for only the affected lane to 80%.
  107. bmc_raw[0] = {8'd74, 8'd0};
  108. i = commits;
  109. next_bmc_epoch();
  110. wait_commit_after(i);
  111. if (!safety[0] || state !== 4) $fatal;
  112. check_duty(0, 4);
  113. check_duty(24, 80);
  114. check_duty(28, 80);
  115. check_duty(25, 30);
  116. // 76 C shuts down only the matching PTC while retaining 80% lane
  117. // airflow; the 77 C lane latch below remains the 100% action.
  118. bmc_raw[0] = {8'd76, 8'd0};
  119. i = commits;
  120. next_bmc_epoch();
  121. wait_commit_after(i);
  122. if (!safety[0] || lane_fault[0] || state !== 5) $fatal;
  123. check_duty(0, 0);
  124. check_duty(24, 80);
  125. check_duty(28, 80);
  126. bmc_raw[0] = {8'd77, 8'd0};
  127. i = commits;
  128. next_bmc_epoch();
  129. wait_commit_after(i);
  130. if (!lane_fault[0] || state !== 5) $fatal;
  131. for (i = 0; i < 6; i = i + 1) check_duty(i[4:0], 0);
  132. check_duty(24, 100);
  133. check_duty(28, 100);
  134. i = commits;
  135. bmc_stale = 1;
  136. wait_commit_after(i);
  137. if (!global_fault) $fatal;
  138. check_duty(12, 0);
  139. check_duty(26, 100);
  140. // A legal target commit must not weaken an already-latched stale
  141. // fault or its 100% airflow fallback.
  142. i = commits;
  143. @(negedge clk); target = 65; target_commit = 1;
  144. @(negedge clk); target_commit = 0;
  145. wait_commit_after(i);
  146. if (!global_fault || state !== 5 || current_target !== 65) $fatal;
  147. check_duty(0, 0);
  148. check_duty(24, 100);
  149. check_duty(28, 100);
  150. // Recovery holds the global/lane clamps for two complete fresh
  151. // epochs and releases them only after the third safe epoch commits.
  152. for (i = 0; i < 24; i = i + 1) bmc_raw[i] = {8'd60, 8'd0};
  153. bmc_stale = 0;
  154. i = commits;
  155. next_bmc_epoch();
  156. wait_commit_after(i);
  157. if (!global_fault || !lane_fault[0] || state !== 5) $fatal;
  158. i = commits;
  159. next_bmc_epoch();
  160. wait_commit_after(i);
  161. if (!global_fault || !lane_fault[0] || state !== 5) $fatal;
  162. i = commits;
  163. next_bmc_epoch();
  164. wait_commit_after(i);
  165. if (global_fault || lane_fault != 0 || state !== 4) $fatal;
  166. // The release commit is still zero-duty: PI state was cleared while
  167. // clamped and may only ramp from the next fresh epoch.
  168. check_duty(0, 0);
  169. i = commits;
  170. next_bmc_epoch();
  171. wait_commit_after(i);
  172. check_duty(0, 4);
  173. $display("PASS: thermal_control_core_tb");
  174. $finish;
  175. end
  176. endmodule