thermal_control_core_tb.v 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. );
  53. task next_bmc_epoch;
  54. begin
  55. @(negedge clk); bmc_epoch = bmc_epoch + 1'b1;
  56. repeat (3) @(negedge clk);
  57. end
  58. endtask
  59. task wait_commit_after;
  60. input integer old_count;
  61. integer timeout;
  62. begin
  63. timeout = 0;
  64. while (commits == old_count && timeout < 1000) begin @(negedge clk); timeout = timeout + 1; end
  65. if (timeout >= 1000) begin
  66. $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);
  67. $fatal;
  68. end
  69. end
  70. endtask
  71. task check_duty;
  72. input [4:0] address;
  73. input [7:0] expected;
  74. begin
  75. pwm_addr = address;
  76. repeat (2) @(negedge clk);
  77. if (pwm_data !== expected) begin
  78. $display("duty[%0d]=%0d expected=%0d", address, pwm_data, expected);
  79. $fatal;
  80. end
  81. end
  82. endtask
  83. initial begin
  84. for (i = 0; i < 24; i = i + 1) bmc_raw[i] = {8'd60, 8'd0};
  85. repeat (4) @(negedge clk);
  86. rst_n = 1;
  87. repeat (3) @(negedge clk);
  88. if (state !== 1 || global_fault) $fatal;
  89. ct_epoch = 1;
  90. repeat (3) @(negedge clk);
  91. if (state !== 2 || global_fault) $fatal;
  92. bmc_stale = 0;
  93. next_bmc_epoch();
  94. if (state !== 3) $fatal;
  95. next_bmc_epoch();
  96. next_bmc_epoch();
  97. next_bmc_epoch();
  98. wait_commit_after(0);
  99. if (state !== 4 || control_epoch == 0) $fatal;
  100. check_duty(0, 5);
  101. check_duty(24, 30);
  102. // 74 C is a soft reduction, not a shutdown: retain a reduced PTC
  103. // request and raise both fans for only the affected lane to 80%.
  104. bmc_raw[0] = {8'd74, 8'd0};
  105. i = commits;
  106. next_bmc_epoch();
  107. wait_commit_after(i);
  108. if (!safety[0] || state !== 4) $fatal;
  109. check_duty(0, 4);
  110. check_duty(24, 80);
  111. check_duty(28, 80);
  112. check_duty(25, 30);
  113. // 76 C shuts down only the matching PTC while retaining 80% lane
  114. // airflow; the 77 C lane latch below remains the 100% action.
  115. bmc_raw[0] = {8'd76, 8'd0};
  116. i = commits;
  117. next_bmc_epoch();
  118. wait_commit_after(i);
  119. if (!safety[0] || lane_fault[0] || state !== 5) $fatal;
  120. check_duty(0, 0);
  121. check_duty(24, 80);
  122. check_duty(28, 80);
  123. bmc_raw[0] = {8'd77, 8'd0};
  124. i = commits;
  125. next_bmc_epoch();
  126. wait_commit_after(i);
  127. if (!lane_fault[0] || state !== 5) $fatal;
  128. for (i = 0; i < 6; i = i + 1) check_duty(i[4:0], 0);
  129. check_duty(24, 100);
  130. check_duty(28, 100);
  131. i = commits;
  132. bmc_stale = 1;
  133. wait_commit_after(i);
  134. if (!global_fault) $fatal;
  135. check_duty(12, 0);
  136. check_duty(26, 100);
  137. // A legal target commit must not weaken an already-latched stale
  138. // fault or its 100% airflow fallback.
  139. i = commits;
  140. @(negedge clk); target = 65; target_commit = 1;
  141. @(negedge clk); target_commit = 0;
  142. wait_commit_after(i);
  143. if (!global_fault || state !== 5 || current_target !== 65) $fatal;
  144. check_duty(0, 0);
  145. check_duty(24, 100);
  146. check_duty(28, 100);
  147. // Recovery holds the global/lane clamps for two complete fresh
  148. // epochs and releases them only after the third safe epoch commits.
  149. for (i = 0; i < 24; i = i + 1) bmc_raw[i] = {8'd60, 8'd0};
  150. bmc_stale = 0;
  151. i = commits;
  152. next_bmc_epoch();
  153. wait_commit_after(i);
  154. if (!global_fault || !lane_fault[0] || state !== 5) $fatal;
  155. i = commits;
  156. next_bmc_epoch();
  157. wait_commit_after(i);
  158. if (!global_fault || !lane_fault[0] || state !== 5) $fatal;
  159. i = commits;
  160. next_bmc_epoch();
  161. wait_commit_after(i);
  162. if (global_fault || lane_fault != 0 || state !== 4) $fatal;
  163. // The release commit is still zero-duty: PI state was cleared while
  164. // clamped and may only ramp from the next fresh epoch.
  165. check_duty(0, 0);
  166. i = commits;
  167. next_bmc_epoch();
  168. wait_commit_after(i);
  169. check_duty(0, 4);
  170. $display("PASS: thermal_control_core_tb");
  171. $finish;
  172. end
  173. endmodule