thermal_control_core.v 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. `timescale 1ns/1ps
  2. // One shared incremental-PI datapath updates 24 logical loops once per new BMC
  3. // epoch. Requested/applied duties commit by bank flip after the sequential pass.
  4. module thermal_control_core #(
  5. parameter integer ENABLE_STIMULUS = 0,
  6. parameter signed [15:0] SOFT_LIMIT = 16'sd7400,
  7. parameter signed [15:0] PRETRIP_LIMIT = 16'sd7600,
  8. parameter signed [15:0] HARD_LIMIT = 16'sd7700,
  9. parameter signed [15:0] DEFAULT_TARGET = 16'sd7000,
  10. parameter signed [15:0] RECOVERY_MARGIN = 16'sd200,
  11. parameter [1:0] RECOVERY_EPOCHS = 2'd3,
  12. parameter signed [15:0] EDGE_KP = 16'sd32,
  13. parameter signed [15:0] EDGE_KI = 16'sd4,
  14. parameter signed [15:0] CENTER_KP = 16'sd24,
  15. parameter signed [15:0] CENTER_KI = 16'sd3,
  16. parameter integer SLEW_Q8_8 = 1280
  17. ) (
  18. input wire clk,
  19. input wire rst_n,
  20. input wire tick_1s,
  21. input wire target_commit,
  22. input wire [7:0] target_celsius,
  23. input wire stimulus_wr_en,
  24. input wire [4:0] stimulus_wr_addr,
  25. input wire [7:0] stimulus_wr_data,
  26. input wire stimulus_commit,
  27. input wire [7:0] stimulus_duration_seconds,
  28. input wire [15:0] bmc_epoch,
  29. input wire bmc_stale,
  30. input wire [23:0] bmc_valid_mask,
  31. output reg [4:0] bmc_rd_index,
  32. input wire [15:0] bmc_rd_raw,
  33. input wire bmc_rd_valid,
  34. input wire [15:0] ct_epoch,
  35. input wire ct_stale,
  36. input wire [26:0] ct_valid_mask,
  37. input wire snapshot_lock,
  38. input wire snapshot_lock_bank,
  39. input wire [4:0] pwm_rd_addr,
  40. output reg [7:0] pwm_rd_data,
  41. input wire snapshot_output_bank,
  42. input wire [4:0] snapshot_rd_addr,
  43. output reg [7:0] snapshot_requested_data,
  44. output reg [7:0] snapshot_applied_data,
  45. output reg active_output_bank,
  46. output reg [2:0] control_state,
  47. output reg [7:0] current_target_celsius,
  48. output reg host_target_seen,
  49. output reg [15:0] control_epoch,
  50. output reg [23:0] safety_override_mask,
  51. output reg [3:0] lane_fault_mask,
  52. output reg global_fault,
  53. output reg output_commit,
  54. output wire stimulus_active
  55. );
  56. localparam [2:0] RESET_SAFE = 3'd0;
  57. localparam [2:0] WAIT_CT_VALID = 3'd1;
  58. localparam [2:0] PREHEAT_CT = 3'd2;
  59. localparam [2:0] WAIT_BMC_STABLE = 3'd3;
  60. localparam [2:0] AUTO_BMC = 3'd4;
  61. localparam [2:0] SENSOR_DEGRADED = 3'd5;
  62. localparam [2:0] STOPPED = 3'd6;
  63. localparam [3:0] W_IDLE = 4'd0;
  64. localparam [3:0] W_PREP = 4'd1;
  65. localparam [3:0] W_KP = 4'd2;
  66. localparam [3:0] W_KI = 4'd3;
  67. localparam [3:0] W_UPDATE = 4'd4;
  68. localparam [3:0] W_FANS = 4'd5;
  69. localparam [3:0] W_APPLY = 4'd6;
  70. localparam [3:0] W_SAFE_FILL = 4'd7;
  71. reg [3:0] work_state;
  72. reg [4:0] work_index;
  73. reg [15:0] last_bmc_epoch;
  74. reg [15:0] last_ct_epoch;
  75. reg [1:0] stable_epochs;
  76. reg epoch_safe;
  77. reg [2:0] invalid_count0;
  78. reg [2:0] invalid_count1;
  79. reg [2:0] invalid_count2;
  80. reg [2:0] invalid_count3;
  81. reg [15:0] target_centi;
  82. reg [7:0] req0 [0:31];
  83. reg [7:0] req1 [0:31];
  84. reg [7:0] app0 [0:31];
  85. reg [7:0] app1 [0:31];
  86. reg signed [15:0] previous_error [0:23];
  87. reg signed [15:0] previous_output [0:23];
  88. reg [7:0] stimulus_mem [0:31];
  89. reg stimulus_running;
  90. reg [7:0] stimulus_remaining;
  91. reg [7:0] safe_fan_target;
  92. // safety_override_mask is Host telemetry for every per-channel safety
  93. // action. Keep shutdown and airflow intent separate so the 74 C soft
  94. // action can reduce (rather than zero) PTC duty while still raising both
  95. // fans for the affected lane.
  96. reg [23:0] ptc_shutdown_mask;
  97. reg [23:0] thermal_protection_mask;
  98. reg sample_valid;
  99. reg signed [15:0] sample_error;
  100. reg signed [15:0] sample_temperature;
  101. reg signed [31:0] kp_product;
  102. reg signed [31:0] ki_product;
  103. integer i;
  104. wire [15:0] bmc_integer_scaled = ({8'd0, bmc_rd_raw[15:8]} << 6) +
  105. ({8'd0, bmc_rd_raw[15:8]} << 5) + ({8'd0, bmc_rd_raw[15:8]} << 2);
  106. wire signed [15:0] bmc_temperature = $signed(bmc_integer_scaled + {8'd0, bmc_rd_raw[7:0]});
  107. wire signed [15:0] live_error = $signed(target_centi) - bmc_temperature;
  108. wire edge_channel = (work_index < 6) || (work_index >= 18);
  109. wire signed [15:0] selected_kp = edge_channel ? EDGE_KP : CENTER_KP;
  110. wire signed [15:0] selected_ki = edge_channel ? EDGE_KI : CENTER_KI;
  111. reg signed [15:0] mult_a;
  112. reg signed [15:0] mult_b;
  113. wire signed [31:0] mult_result = mult_a * mult_b;
  114. wire signed [31:0] pi_sum = kp_product + ki_product;
  115. wire signed [31:0] pi_delta = (pi_sum >= 0) ? ((pi_sum + 8) >>> 4) : -(((-pi_sum) + 8) >>> 4);
  116. wire signed [31:0] previous_output_ext = {{16{previous_output[work_index][15]}}, previous_output[work_index]};
  117. wire signed [31:0] pi_candidate = previous_output_ext + pi_delta;
  118. wire signed [31:0] slew_low = previous_output_ext - SLEW_Q8_8;
  119. wire signed [31:0] slew_high = previous_output_ext + SLEW_Q8_8;
  120. wire signed [31:0] slew_candidate = (pi_candidate < slew_low) ? slew_low : ((pi_candidate > slew_high) ? slew_high : pi_candidate);
  121. wire signed [15:0] clamped_output = (slew_candidate < 0) ? 16'sd0 : ((slew_candidate > 25600) ? 16'sd25600 : slew_candidate[15:0]);
  122. wire signed [15:0] reduced_output = previous_output[work_index] - 16'sd256;
  123. wire [7:0] clamped_duty = (clamped_output >= 16'sd25600) ? 8'd100 : q8_8_to_duty(clamped_output);
  124. wire [7:0] reduced_duty = (previous_output[work_index] > 16'sd256) ? q8_8_to_duty(reduced_output) : 8'd0;
  125. wire [7:0] inactive_request = active_output_bank ? req0[work_index] : req1[work_index];
  126. wire signed [15:0] recovery_threshold = $signed(target_centi) + RECOVERY_MARGIN;
  127. assign stimulus_active = stimulus_running;
  128. function [1:0] lane_for_channel;
  129. input [4:0] channel;
  130. begin
  131. if (channel < 6) lane_for_channel = 0;
  132. else if (channel < 12) lane_for_channel = 1;
  133. else if (channel < 18) lane_for_channel = 2;
  134. else lane_for_channel = 3;
  135. end
  136. endfunction
  137. function [7:0] q8_8_to_duty;
  138. input signed [15:0] value;
  139. /* verilator lint_off UNUSEDSIGNAL */
  140. reg signed [15:0] rounded;
  141. /* verilator lint_on UNUSEDSIGNAL */
  142. begin
  143. rounded = (value + 16'sd128) >>> 8;
  144. q8_8_to_duty = rounded[7:0];
  145. end
  146. endfunction
  147. function lane_fault_for_output;
  148. input [4:0] address;
  149. reg [1:0] lane;
  150. begin
  151. if (address < 24) lane = lane_for_channel(address);
  152. else lane = address[1:0];
  153. lane_fault_for_output = lane_fault_mask[lane];
  154. end
  155. endfunction
  156. function [1:0] lane_for_output;
  157. input [4:0] address;
  158. begin
  159. if (address < 24)
  160. lane_for_output = lane_for_channel(address);
  161. else
  162. lane_for_output = address[1:0];
  163. end
  164. endfunction
  165. function lane_invalid;
  166. input [1:0] lane;
  167. begin
  168. case (lane)
  169. 0: lane_invalid = invalid_count0 >= 2;
  170. 1: lane_invalid = invalid_count1 >= 2;
  171. 2: lane_invalid = invalid_count2 >= 2;
  172. default: lane_invalid = invalid_count3 >= 2;
  173. endcase
  174. end
  175. endfunction
  176. function lane_thermal_protection;
  177. input [1:0] lane;
  178. begin
  179. case (lane)
  180. 0: lane_thermal_protection = |thermal_protection_mask[5:0];
  181. 1: lane_thermal_protection = |thermal_protection_mask[11:6];
  182. 2: lane_thermal_protection = |thermal_protection_mask[17:12];
  183. default: lane_thermal_protection = |thermal_protection_mask[23:18];
  184. endcase
  185. end
  186. endfunction
  187. wire [7:0] apply_ptc_value =
  188. (global_fault || control_state == STOPPED ||
  189. lane_fault_for_output(work_index) ||
  190. lane_invalid(lane_for_channel(work_index)) ||
  191. ptc_shutdown_mask[work_index]) ? 8'd0 : inactive_request;
  192. wire [7:0] apply_fan_value =
  193. (global_fault || lane_fault_for_output(work_index) ||
  194. lane_invalid(lane_for_output(work_index))) ? 8'd100 :
  195. (lane_thermal_protection(lane_for_output(work_index)) ? 8'd80 : inactive_request);
  196. wire [7:0] safe_fill_value = (work_index < 24) ? 8'd0 :
  197. ((global_fault || lane_fault_for_output(work_index) ||
  198. lane_invalid(lane_for_output(work_index))) ? 8'd100 :
  199. (lane_thermal_protection(lane_for_output(work_index)) ? 8'd80 : safe_fan_target));
  200. // BMC age intentionally resets stale. That is an acquisition-startup
  201. // condition, not a runtime sensor failure: CT qualification and preheat
  202. // must still run before BMC stability is evaluated.
  203. wire bmc_stale_runtime = bmc_stale &&
  204. (control_state == AUTO_BMC || control_state == SENSOR_DEGRADED);
  205. always @* begin
  206. mult_a = 0;
  207. mult_b = 0;
  208. if (work_state == W_KP) begin
  209. mult_a = live_error - previous_error[work_index[4:0]];
  210. mult_b = selected_kp;
  211. end else if (work_state == W_KI) begin
  212. mult_a = sample_error;
  213. mult_b = selected_ki;
  214. end
  215. if (snapshot_output_bank) begin
  216. snapshot_requested_data = req1[snapshot_rd_addr];
  217. snapshot_applied_data = app1[snapshot_rd_addr];
  218. end else begin
  219. snapshot_requested_data = req0[snapshot_rd_addr];
  220. snapshot_applied_data = app0[snapshot_rd_addr];
  221. end
  222. end
  223. always @(posedge clk or negedge rst_n) begin
  224. if (!rst_n) begin
  225. active_output_bank <= 0;
  226. control_state <= RESET_SAFE;
  227. current_target_celsius <= 70;
  228. target_centi <= DEFAULT_TARGET;
  229. host_target_seen <= 0;
  230. control_epoch <= 0;
  231. safety_override_mask <= 0;
  232. ptc_shutdown_mask <= 0;
  233. thermal_protection_mask <= 0;
  234. lane_fault_mask <= 0;
  235. global_fault <= 0;
  236. output_commit <= 0;
  237. pwm_rd_data <= 0;
  238. work_state <= W_IDLE;
  239. work_index <= 0;
  240. bmc_rd_index <= 0;
  241. last_bmc_epoch <= 0;
  242. last_ct_epoch <= 0;
  243. stable_epochs <= 0;
  244. epoch_safe <= 0;
  245. invalid_count0 <= 0;
  246. invalid_count1 <= 0;
  247. invalid_count2 <= 0;
  248. invalid_count3 <= 0;
  249. stimulus_running <= 0;
  250. stimulus_remaining <= 0;
  251. safe_fan_target <= 10;
  252. sample_valid <= 0;
  253. sample_error <= 0;
  254. sample_temperature <= 0;
  255. kp_product <= 0;
  256. ki_product <= 0;
  257. for (i = 0; i < 32; i = i + 1) begin
  258. req0[i] <= (i < 24) ? 0 : 10;
  259. req1[i] <= (i < 24) ? 0 : 10;
  260. app0[i] <= (i < 24) ? 0 : 10;
  261. app1[i] <= (i < 24) ? 0 : 10;
  262. stimulus_mem[i] <= 0;
  263. end
  264. for (i = 0; i < 24; i = i + 1) begin
  265. previous_error[i] <= 0;
  266. previous_output[i] <= 0;
  267. end
  268. end else begin
  269. output_commit <= 0;
  270. if (active_output_bank)
  271. pwm_rd_data <= app1[pwm_rd_addr];
  272. else
  273. pwm_rd_data <= app0[pwm_rd_addr];
  274. if (stimulus_wr_en && (ENABLE_STIMULUS != 0))
  275. stimulus_mem[stimulus_wr_addr] <= stimulus_wr_data;
  276. if (stimulus_commit && (ENABLE_STIMULUS != 0) && stimulus_duration_seconds != 0) begin
  277. stimulus_running <= 1;
  278. stimulus_remaining <= stimulus_duration_seconds;
  279. end
  280. if (tick_1s && stimulus_running) begin
  281. if (stimulus_remaining <= 1) begin
  282. stimulus_running <= 0;
  283. stimulus_remaining <= 0;
  284. end else stimulus_remaining <= stimulus_remaining - 1'b1;
  285. end
  286. if (target_commit) begin
  287. current_target_celsius <= target_celsius;
  288. target_centi <= ({8'd0, target_celsius} << 6) +
  289. ({8'd0, target_celsius} << 5) + ({8'd0, target_celsius} << 2);
  290. host_target_seen <= 1;
  291. stable_epochs <= 0;
  292. stimulus_running <= 0;
  293. work_index <= 0;
  294. work_state <= W_SAFE_FILL;
  295. safe_fan_target <= (global_fault || bmc_stale_runtime) ? 8'd100 :
  296. ((target_celsius == 0) ? 8'd30 : 8'd10);
  297. global_fault <= global_fault || bmc_stale_runtime;
  298. if (target_celsius == 0)
  299. control_state <= STOPPED;
  300. else if (global_fault || bmc_stale_runtime)
  301. control_state <= SENSOR_DEGRADED;
  302. else
  303. control_state <= WAIT_CT_VALID;
  304. for (i = 0; i < 24; i = i + 1) begin
  305. previous_error[i] <= 0;
  306. previous_output[i] <= 0;
  307. end
  308. end else if (bmc_stale_runtime && !global_fault) begin
  309. global_fault <= 1;
  310. control_state <= SENSOR_DEGRADED;
  311. stable_epochs <= 0;
  312. work_index <= 0;
  313. work_state <= W_SAFE_FILL;
  314. safe_fan_target <= 100;
  315. stimulus_running <= 0;
  316. end else begin
  317. case (work_state)
  318. W_IDLE: begin
  319. if (control_state == RESET_SAFE) begin
  320. control_state <= WAIT_CT_VALID;
  321. end else if (control_state == WAIT_CT_VALID) begin
  322. if (!ct_stale && (&ct_valid_mask) && ct_epoch != last_ct_epoch) begin
  323. last_ct_epoch <= ct_epoch;
  324. control_state <= PREHEAT_CT;
  325. end
  326. end else if (control_state == PREHEAT_CT) begin
  327. if (bmc_epoch != last_bmc_epoch) begin
  328. last_bmc_epoch <= bmc_epoch;
  329. stable_epochs <= 0;
  330. control_state <= WAIT_BMC_STABLE;
  331. end
  332. end else if (control_state == WAIT_BMC_STABLE) begin
  333. if (bmc_epoch != last_bmc_epoch) begin
  334. last_bmc_epoch <= bmc_epoch;
  335. if (&bmc_valid_mask) begin
  336. if (stable_epochs == 2) begin
  337. stable_epochs <= 0;
  338. control_state <= AUTO_BMC;
  339. work_index <= 0;
  340. work_state <= W_PREP;
  341. end else stable_epochs <= stable_epochs + 1'b1;
  342. end else stable_epochs <= 0;
  343. end
  344. end else if ((control_state == AUTO_BMC || control_state == SENSOR_DEGRADED) &&
  345. !bmc_stale && bmc_epoch != last_bmc_epoch &&
  346. !(snapshot_lock && ((~active_output_bank) == snapshot_lock_bank))) begin
  347. last_bmc_epoch <= bmc_epoch;
  348. work_index <= 0;
  349. work_state <= W_PREP;
  350. end
  351. end
  352. W_PREP: begin
  353. bmc_rd_index <= work_index;
  354. if (work_index == 0) begin
  355. epoch_safe <= 1;
  356. invalid_count0 <= 0;
  357. invalid_count1 <= 0;
  358. invalid_count2 <= 0;
  359. invalid_count3 <= 0;
  360. end
  361. work_state <= W_KP;
  362. end
  363. W_KP: begin
  364. sample_valid <= bmc_rd_valid;
  365. sample_temperature <= bmc_temperature;
  366. sample_error <= live_error;
  367. kp_product <= mult_result;
  368. work_state <= W_KI;
  369. end
  370. W_KI: begin
  371. ki_product <= mult_result;
  372. work_state <= W_UPDATE;
  373. end
  374. W_UPDATE: begin
  375. if (!sample_valid) begin
  376. if (active_output_bank) begin req0[work_index] <= 0; end else begin req1[work_index] <= 0; end
  377. safety_override_mask[work_index] <= 1;
  378. ptc_shutdown_mask[work_index] <= 1;
  379. thermal_protection_mask[work_index] <= 0;
  380. previous_error[work_index] <= 0;
  381. previous_output[work_index] <= 0;
  382. epoch_safe <= 0;
  383. control_state <= SENSOR_DEGRADED;
  384. case (lane_for_channel(work_index))
  385. 0: invalid_count0 <= invalid_count0 + 1'b1;
  386. 1: invalid_count1 <= invalid_count1 + 1'b1;
  387. 2: invalid_count2 <= invalid_count2 + 1'b1;
  388. 3: invalid_count3 <= invalid_count3 + 1'b1;
  389. endcase
  390. end else if (sample_temperature >= HARD_LIMIT) begin
  391. if (active_output_bank) req0[work_index] <= 0; else req1[work_index] <= 0;
  392. safety_override_mask[work_index] <= 1;
  393. ptc_shutdown_mask[work_index] <= 1;
  394. thermal_protection_mask[work_index] <= 1;
  395. previous_error[work_index] <= 0;
  396. previous_output[work_index] <= 0;
  397. lane_fault_mask[lane_for_channel(work_index)] <= 1;
  398. epoch_safe <= 0;
  399. control_state <= SENSOR_DEGRADED;
  400. stimulus_running <= 0;
  401. end else if (sample_temperature >= PRETRIP_LIMIT) begin
  402. if (active_output_bank) req0[work_index] <= 0; else req1[work_index] <= 0;
  403. safety_override_mask[work_index] <= 1;
  404. ptc_shutdown_mask[work_index] <= 1;
  405. thermal_protection_mask[work_index] <= 1;
  406. previous_error[work_index] <= 0;
  407. previous_output[work_index] <= 0;
  408. epoch_safe <= 0;
  409. control_state <= SENSOR_DEGRADED;
  410. end else if (global_fault || lane_fault_for_output(work_index)) begin
  411. // A global/lane clamp must also clear the hidden PI
  412. // state. Otherwise recovery could expose duty that
  413. // accumulated while the physical output was forced
  414. // off, violating bumpless limited-duty restart.
  415. if (active_output_bank) req0[work_index] <= 0; else req1[work_index] <= 0;
  416. safety_override_mask[work_index] <= 0;
  417. ptc_shutdown_mask[work_index] <= 0;
  418. thermal_protection_mask[work_index] <= 0;
  419. previous_error[work_index] <= 0;
  420. previous_output[work_index] <= 0;
  421. end else begin
  422. // Safety precedes identification stimulus. The
  423. // soft limit reduces the autonomous PI state by
  424. // one duty point and requests protection airflow,
  425. // but deliberately does not assert PTC shutdown.
  426. if (sample_temperature >= SOFT_LIMIT) begin
  427. if (active_output_bank)
  428. req0[work_index] <= reduced_duty;
  429. else
  430. req1[work_index] <= reduced_duty;
  431. if (previous_output[work_index] > 16'sd256) previous_output[work_index] <= previous_output[work_index] - 16'sd256; else previous_output[work_index] <= 0;
  432. previous_error[work_index] <= sample_error;
  433. safety_override_mask[work_index] <= 1;
  434. ptc_shutdown_mask[work_index] <= 0;
  435. thermal_protection_mask[work_index] <= 1;
  436. end else if (stimulus_running) begin
  437. if (active_output_bank) req0[work_index] <= stimulus_mem[work_index]; else req1[work_index] <= stimulus_mem[work_index];
  438. safety_override_mask[work_index] <= 0;
  439. ptc_shutdown_mask[work_index] <= 0;
  440. thermal_protection_mask[work_index] <= 0;
  441. end else begin
  442. if (active_output_bank) req0[work_index] <= clamped_duty; else req1[work_index] <= clamped_duty;
  443. previous_output[work_index] <= clamped_output;
  444. previous_error[work_index] <= sample_error;
  445. safety_override_mask[work_index] <= 0;
  446. ptc_shutdown_mask[work_index] <= 0;
  447. thermal_protection_mask[work_index] <= 0;
  448. end
  449. if (sample_temperature > recovery_threshold) epoch_safe <= 0;
  450. end
  451. if (work_index == 23) begin
  452. work_index <= 24;
  453. work_state <= W_FANS;
  454. if (control_state == SENSOR_DEGRADED) begin
  455. if (epoch_safe && sample_valid && sample_temperature <= recovery_threshold) begin
  456. if (stable_epochs == RECOVERY_EPOCHS - 1'b1) begin
  457. stable_epochs <= 0;
  458. lane_fault_mask <= 0;
  459. global_fault <= 0;
  460. control_state <= AUTO_BMC;
  461. end else stable_epochs <= stable_epochs + 1'b1;
  462. end else stable_epochs <= 0;
  463. end
  464. end else begin
  465. work_index <= work_index + 1'b1;
  466. work_state <= W_PREP;
  467. end
  468. end
  469. W_FANS: begin
  470. if (active_output_bank)
  471. req0[work_index] <= stimulus_running ? stimulus_mem[work_index] : 8'd30;
  472. else
  473. req1[work_index] <= stimulus_running ? stimulus_mem[work_index] : 8'd30;
  474. if (work_index == 31) begin work_index <= 0; work_state <= W_APPLY; end
  475. else work_index <= work_index + 1'b1;
  476. end
  477. W_APPLY: begin
  478. if (active_output_bank) begin
  479. if (work_index < 24)
  480. app0[work_index] <= apply_ptc_value;
  481. else
  482. app0[work_index] <= apply_fan_value;
  483. end else begin
  484. if (work_index < 24)
  485. app1[work_index] <= apply_ptc_value;
  486. else
  487. app1[work_index] <= apply_fan_value;
  488. end
  489. if (work_index == 31) begin
  490. active_output_bank <= ~active_output_bank;
  491. control_epoch <= control_epoch + 1'b1;
  492. output_commit <= 1;
  493. work_index <= 0;
  494. work_state <= W_IDLE;
  495. end else work_index <= work_index + 1'b1;
  496. end
  497. W_SAFE_FILL: begin
  498. if (active_output_bank) begin
  499. req0[work_index] <= safe_fill_value;
  500. app0[work_index] <= safe_fill_value;
  501. end else begin
  502. req1[work_index] <= safe_fill_value;
  503. app1[work_index] <= safe_fill_value;
  504. end
  505. if (work_index == 31) begin
  506. active_output_bank <= ~active_output_bank;
  507. control_epoch <= control_epoch + 1'b1;
  508. output_commit <= 1;
  509. work_index <= 0;
  510. work_state <= W_IDLE;
  511. end else work_index <= work_index + 1'b1;
  512. end
  513. default: work_state <= W_IDLE;
  514. endcase
  515. end
  516. end
  517. end
  518. `ifdef FORMAL
  519. reg formal_past_valid;
  520. (* anyconst *) reg [5:0] formal_duty_index;
  521. (* anyconst *) reg [4:0] formal_ptc_index;
  522. wire [7:0] formal_active_applied = active_output_bank ?
  523. app1[formal_duty_index] : app0[formal_duty_index];
  524. wire [7:0] formal_inactive_applied = active_output_bank ?
  525. app0[formal_duty_index] : app1[formal_duty_index];
  526. wire [7:0] formal_safe_fill_value = (formal_duty_index < 24) ? 8'd0 :
  527. ((global_fault || lane_fault_for_output(formal_duty_index) ||
  528. lane_invalid(lane_for_output(formal_duty_index))) ?
  529. 8'd100 :
  530. (lane_thermal_protection(lane_for_output(formal_duty_index)) ?
  531. 8'd80 : safe_fan_target));
  532. wire formal_stale_preempt = bmc_stale_runtime && !global_fault;
  533. function [7:0] formal_work_rank;
  534. input [3:0] state_value;
  535. input [4:0] index_value;
  536. begin
  537. case (state_value)
  538. W_PREP: formal_work_rank = 8'd136 - ({3'd0, index_value} << 2);
  539. W_KP: formal_work_rank = 8'd135 - ({3'd0, index_value} << 2);
  540. W_KI: formal_work_rank = 8'd134 - ({3'd0, index_value} << 2);
  541. W_UPDATE: formal_work_rank = 8'd133 - ({3'd0, index_value} << 2);
  542. W_FANS: formal_work_rank = 8'd64 - index_value;
  543. W_APPLY: formal_work_rank = 8'd32 - index_value;
  544. W_SAFE_FILL: formal_work_rank = 8'd64 - index_value;
  545. default: formal_work_rank = 8'd0;
  546. endcase
  547. end
  548. endfunction
  549. wire [7:0] formal_scheduler_rank = formal_work_rank(work_state, work_index);
  550. initial formal_past_valid = 0;
  551. always @(posedge clk) begin
  552. formal_past_valid <= 1;
  553. if (stimulus_wr_en) assume(stimulus_wr_data <= 100);
  554. if (target_commit)
  555. assume(target_celsius == 0 || (target_celsius >= 20 && target_celsius <= 70));
  556. assume(formal_duty_index < 32);
  557. assume(formal_ptc_index < 24);
  558. // P01: every requested and applied logical duty remains in 0..100.
  559. assert(req0[formal_duty_index] <= 100);
  560. assert(req1[formal_duty_index] <= 100);
  561. assert(app0[formal_duty_index] <= 100);
  562. assert(app1[formal_duty_index] <= 100);
  563. assert(stimulus_mem[formal_duty_index] <= 100);
  564. assert(safe_fan_target <= 100);
  565. assert(control_state <= STOPPED);
  566. assert(current_target_celsius == 0 ||
  567. (current_target_celsius >= 20 && current_target_celsius <= 70));
  568. assert(target_centi <= 16'd7000);
  569. if (formal_duty_index < 24) begin
  570. assert(previous_output[formal_duty_index] >= 0);
  571. assert(previous_output[formal_duty_index] <= 25600);
  572. end
  573. if (!rst_n) begin
  574. // P17: reset state is safe and deterministic.
  575. assert(control_state == RESET_SAFE);
  576. assert(current_target_celsius == 70);
  577. assert(safety_override_mask == 0);
  578. assert(ptc_shutdown_mask == 0);
  579. assert(thermal_protection_mask == 0);
  580. assert(lane_fault_mask == 0);
  581. assert(!global_fault);
  582. if (formal_duty_index < 24) begin
  583. assert(app0[formal_duty_index] == 0);
  584. assert(app1[formal_duty_index] == 0);
  585. end else begin
  586. assert(app0[formal_duty_index] == 10);
  587. assert(app1[formal_duty_index] == 10);
  588. end
  589. end else begin
  590. // P10: an admitted scheduler pass has a finite rank that strictly
  591. // decreases every clock unless target/stale safety preempts it.
  592. assert(formal_scheduler_rank <= 136);
  593. assert(work_state <= W_SAFE_FILL);
  594. if (work_state == W_PREP || work_state == W_KP ||
  595. work_state == W_KI || work_state == W_UPDATE)
  596. assert(work_index < 24);
  597. if (work_state == W_FANS)
  598. assert(work_index >= 24);
  599. // Reset-age stale must not bypass CT/preheat startup sequencing.
  600. if (bmc_stale && (control_state == RESET_SAFE ||
  601. control_state == WAIT_CT_VALID ||
  602. control_state == PREHEAT_CT ||
  603. control_state == WAIT_BMC_STABLE))
  604. assert(!formal_stale_preempt);
  605. // P02/P03/P04/P05/P16: the production apply datapath gives every
  606. // hard safety condition priority over an ordinary PTC request.
  607. if (work_state == W_APPLY && work_index == formal_ptc_index &&
  608. (global_fault || control_state == STOPPED ||
  609. ptc_shutdown_mask[formal_ptc_index] ||
  610. lane_fault_mask[lane_for_channel(formal_ptc_index)] ||
  611. lane_invalid(lane_for_channel(formal_ptc_index))))
  612. assert(apply_ptc_value == 0);
  613. // Inductive sweep invariants: every address below work_index has
  614. // already been written into the inactive bank by this pass.
  615. if (work_state == W_SAFE_FILL &&
  616. formal_duty_index < work_index)
  617. assert(formal_inactive_applied == formal_safe_fill_value);
  618. if (work_state == W_APPLY &&
  619. formal_duty_index < work_index) begin
  620. if (formal_duty_index < 24 &&
  621. (global_fault || control_state == STOPPED ||
  622. ptc_shutdown_mask[formal_duty_index] ||
  623. lane_fault_mask[lane_for_channel(formal_duty_index)] ||
  624. lane_invalid(lane_for_channel(formal_duty_index))))
  625. assert(formal_inactive_applied == 0);
  626. if (formal_duty_index >= 24 &&
  627. (global_fault ||
  628. lane_fault_for_output(formal_duty_index) ||
  629. lane_invalid(lane_for_output(formal_duty_index))))
  630. assert(formal_inactive_applied == 100);
  631. if (formal_duty_index >= 24 &&
  632. !global_fault &&
  633. !lane_fault_for_output(formal_duty_index) &&
  634. !lane_invalid(lane_for_output(formal_duty_index)) &&
  635. lane_thermal_protection(lane_for_output(formal_duty_index)))
  636. assert(formal_inactive_applied == 80);
  637. end
  638. // Once a safety pass has committed, the active bank itself (not
  639. // merely the combinational apply value) is safe. W_IDLE is the
  640. // post-commit quiescent point, so these assertions compose the
  641. // detection, fill, bank flip, and externally visible duty state.
  642. if (work_state == W_IDLE) begin
  643. if (formal_duty_index < 24 &&
  644. (global_fault || control_state == STOPPED ||
  645. ptc_shutdown_mask[formal_duty_index] ||
  646. lane_fault_mask[lane_for_channel(formal_duty_index)] ||
  647. lane_invalid(lane_for_channel(formal_duty_index))))
  648. assert(formal_active_applied == 0);
  649. if (formal_duty_index >= 24 &&
  650. (global_fault ||
  651. lane_fault_for_output(formal_duty_index) ||
  652. lane_invalid(lane_for_output(formal_duty_index))))
  653. assert(formal_active_applied == 100);
  654. if (formal_duty_index >= 24 &&
  655. !global_fault &&
  656. !lane_fault_for_output(formal_duty_index) &&
  657. !lane_invalid(lane_for_output(formal_duty_index)) &&
  658. lane_thermal_protection(lane_for_output(formal_duty_index)))
  659. assert(formal_active_applied == 80);
  660. end
  661. if (formal_past_valid && $past(rst_n)) begin
  662. if ($past(formal_scheduler_rank) != 0 &&
  663. !$past(target_commit) && !$past(formal_stale_preempt))
  664. assert(formal_scheduler_rank < $past(formal_scheduler_rank));
  665. // P03: a newly observed stale source starts the bounded safe
  666. // fill at address zero and latches the global fault. The rank
  667. // invariant above forces that fill to reach the safe commit.
  668. if ($past(formal_stale_preempt && !target_commit)) begin
  669. assert(global_fault);
  670. assert(control_state == SENSOR_DEGRADED);
  671. assert(work_state == W_SAFE_FILL);
  672. assert(work_index == 0);
  673. assert(safe_fan_target == 100);
  674. end
  675. // P04: an invalid sample records both the zero request and the
  676. // channel override before the common apply/commit sequence.
  677. if ($past(work_state == W_UPDATE &&
  678. work_index == formal_ptc_index && !sample_valid &&
  679. !target_commit && !formal_stale_preempt)) begin
  680. assert(safety_override_mask[formal_ptc_index]);
  681. assert(ptc_shutdown_mask[formal_ptc_index]);
  682. assert(!thermal_protection_mask[formal_ptc_index]);
  683. if ($past(active_output_bank))
  684. assert(req0[formal_ptc_index] == 0);
  685. else
  686. assert(req1[formal_ptc_index] == 0);
  687. end
  688. // P05: a sampled hard limit latches the lane fault before the
  689. // common apply pass zeros every PTC belonging to that lane.
  690. if ($past(work_state == W_UPDATE &&
  691. work_index == formal_ptc_index && sample_valid &&
  692. sample_temperature >= HARD_LIMIT &&
  693. !target_commit && !formal_stale_preempt)) begin
  694. assert(lane_fault_mask[lane_for_channel(formal_ptc_index)]);
  695. assert(ptc_shutdown_mask[formal_ptc_index]);
  696. assert(thermal_protection_mask[formal_ptc_index]);
  697. end
  698. // Soft and pretrip telemetry share the public override bit,
  699. // but only pretrip shuts the PTC down. Both request 80%
  700. // airflow until a normal sample clears the channel masks.
  701. if ($past(work_state == W_UPDATE &&
  702. work_index == formal_ptc_index && sample_valid &&
  703. sample_temperature >= PRETRIP_LIMIT &&
  704. sample_temperature < HARD_LIMIT &&
  705. !target_commit && !formal_stale_preempt)) begin
  706. assert(safety_override_mask[formal_ptc_index]);
  707. assert(ptc_shutdown_mask[formal_ptc_index]);
  708. assert(thermal_protection_mask[formal_ptc_index]);
  709. end
  710. if ($past(work_state == W_UPDATE &&
  711. work_index == formal_ptc_index && sample_valid &&
  712. sample_temperature >= SOFT_LIMIT &&
  713. sample_temperature < PRETRIP_LIMIT &&
  714. !global_fault &&
  715. !lane_fault_mask[lane_for_channel(formal_ptc_index)] &&
  716. !target_commit && !formal_stale_preempt)) begin
  717. assert(safety_override_mask[formal_ptc_index]);
  718. assert(!ptc_shutdown_mask[formal_ptc_index]);
  719. assert(thermal_protection_mask[formal_ptc_index]);
  720. end
  721. // Fault-clamped control passes cannot accumulate hidden PI
  722. // state or a nonzero request for later release.
  723. if ($past(work_state == W_UPDATE &&
  724. work_index == formal_ptc_index && sample_valid &&
  725. sample_temperature < PRETRIP_LIMIT &&
  726. (global_fault ||
  727. lane_fault_mask[lane_for_channel(formal_ptc_index)]) &&
  728. !target_commit && !formal_stale_preempt)) begin
  729. assert(previous_error[formal_ptc_index] == 0);
  730. assert(previous_output[formal_ptc_index] == 0);
  731. if ($past(active_output_bank))
  732. assert(req0[formal_ptc_index] == 0);
  733. else
  734. assert(req1[formal_ptc_index] == 0);
  735. end
  736. // P12: absence of a Host target commit cannot create STOPPED.
  737. if (!$past(target_commit) && $past(control_state) != STOPPED)
  738. assert(control_state != STOPPED);
  739. // P13: stimulus has a strictly decreasing per-tick rank and
  740. // reaches inactive when the remaining rank is one.
  741. if ($past(stimulus_running && tick_1s && !stimulus_commit)) begin
  742. if ($past(stimulus_remaining) <= 1)
  743. assert(!stimulus_running);
  744. else if (stimulus_running)
  745. assert(stimulus_remaining == $past(stimulus_remaining) - 1'b1);
  746. end
  747. end
  748. // Reachability witnesses are discharged by the dedicated cover
  749. // tasks; they prevent the safety antecedents from being vacuous.
  750. cover(global_fault && work_state == W_IDLE &&
  751. formal_ptc_index < 24 &&
  752. (active_output_bank ? app1[formal_ptc_index] :
  753. app0[formal_ptc_index]) == 0);
  754. cover(ptc_shutdown_mask[formal_ptc_index] &&
  755. work_state == W_IDLE &&
  756. (active_output_bank ? app1[formal_ptc_index] :
  757. app0[formal_ptc_index]) == 0);
  758. cover(lane_fault_mask[lane_for_channel(formal_ptc_index)] &&
  759. work_state == W_IDLE &&
  760. (active_output_bank ? app1[formal_ptc_index] :
  761. app0[formal_ptc_index]) == 0);
  762. cover(control_state == STOPPED && work_state == W_IDLE &&
  763. (active_output_bank ? app1[formal_ptc_index] :
  764. app0[formal_ptc_index]) == 0);
  765. cover(stimulus_running && stimulus_remaining != 0);
  766. cover(formal_past_valid &&
  767. $past(stimulus_running && tick_1s &&
  768. stimulus_remaining <= 1 && !stimulus_commit) &&
  769. !stimulus_running);
  770. cover(control_state == AUTO_BMC && work_state == W_UPDATE);
  771. end
  772. end
  773. `endif
  774. endmodule