| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807 |
- `timescale 1ns/1ps
- // One shared incremental-PI datapath updates 24 logical loops once per new BMC
- // epoch. Requested/applied duties commit by bank flip after the sequential pass.
- module thermal_control_core #(
- parameter integer ENABLE_STIMULUS = 0,
- parameter signed [15:0] SOFT_LIMIT = 16'sd7400,
- parameter signed [15:0] PRETRIP_LIMIT = 16'sd7600,
- parameter signed [15:0] HARD_LIMIT = 16'sd7700,
- parameter signed [15:0] DEFAULT_TARGET = 16'sd7000,
- parameter signed [15:0] RECOVERY_MARGIN = 16'sd200,
- parameter [1:0] RECOVERY_EPOCHS = 2'd3,
- parameter signed [15:0] EDGE_KP = 16'sd32,
- parameter signed [15:0] EDGE_KI = 16'sd4,
- parameter signed [15:0] CENTER_KP = 16'sd24,
- parameter signed [15:0] CENTER_KI = 16'sd3,
- parameter integer SLEW_Q8_8 = 1280
- ) (
- input wire clk,
- input wire rst_n,
- input wire tick_1s,
- input wire target_commit,
- input wire [7:0] target_celsius,
- input wire stimulus_wr_en,
- input wire [4:0] stimulus_wr_addr,
- input wire [7:0] stimulus_wr_data,
- input wire stimulus_commit,
- input wire [7:0] stimulus_duration_seconds,
- input wire [15:0] bmc_epoch,
- input wire bmc_stale,
- input wire [23:0] bmc_valid_mask,
- output reg [4:0] bmc_rd_index,
- input wire [15:0] bmc_rd_raw,
- input wire bmc_rd_valid,
- input wire [15:0] ct_epoch,
- input wire ct_stale,
- input wire [26:0] ct_valid_mask,
- input wire snapshot_lock,
- input wire snapshot_lock_bank,
- input wire [4:0] pwm_rd_addr,
- output reg [7:0] pwm_rd_data,
- input wire snapshot_output_bank,
- input wire [4:0] snapshot_rd_addr,
- output reg [7:0] snapshot_requested_data,
- output reg [7:0] snapshot_applied_data,
- output reg active_output_bank,
- output reg [2:0] control_state,
- output reg [7:0] current_target_celsius,
- output reg host_target_seen,
- output reg [15:0] control_epoch,
- output reg [23:0] safety_override_mask,
- output reg [3:0] lane_fault_mask,
- output reg global_fault,
- output reg output_commit,
- output wire stimulus_active
- );
- localparam [2:0] RESET_SAFE = 3'd0;
- localparam [2:0] WAIT_CT_VALID = 3'd1;
- localparam [2:0] PREHEAT_CT = 3'd2;
- localparam [2:0] WAIT_BMC_STABLE = 3'd3;
- localparam [2:0] AUTO_BMC = 3'd4;
- localparam [2:0] SENSOR_DEGRADED = 3'd5;
- localparam [2:0] STOPPED = 3'd6;
- localparam [3:0] W_IDLE = 4'd0;
- localparam [3:0] W_PREP = 4'd1;
- localparam [3:0] W_KP = 4'd2;
- localparam [3:0] W_KI = 4'd3;
- localparam [3:0] W_UPDATE = 4'd4;
- localparam [3:0] W_FANS = 4'd5;
- localparam [3:0] W_APPLY = 4'd6;
- localparam [3:0] W_SAFE_FILL = 4'd7;
- reg [3:0] work_state;
- reg [4:0] work_index;
- reg [15:0] last_bmc_epoch;
- reg [15:0] last_ct_epoch;
- reg [1:0] stable_epochs;
- reg epoch_safe;
- reg [2:0] invalid_count0;
- reg [2:0] invalid_count1;
- reg [2:0] invalid_count2;
- reg [2:0] invalid_count3;
- reg [15:0] target_centi;
- reg [7:0] req0 [0:31];
- reg [7:0] req1 [0:31];
- reg [7:0] app0 [0:31];
- reg [7:0] app1 [0:31];
- reg signed [15:0] previous_error [0:23];
- reg signed [15:0] previous_output [0:23];
- reg [7:0] stimulus_mem [0:31];
- reg stimulus_running;
- reg [7:0] stimulus_remaining;
- reg [7:0] safe_fan_target;
- // safety_override_mask is Host telemetry for every per-channel safety
- // action. Keep shutdown and airflow intent separate so the 74 C soft
- // action can reduce (rather than zero) PTC duty while still raising both
- // fans for the affected lane.
- reg [23:0] ptc_shutdown_mask;
- reg [23:0] thermal_protection_mask;
- reg sample_valid;
- reg signed [15:0] sample_error;
- reg signed [15:0] sample_temperature;
- reg signed [31:0] kp_product;
- reg signed [31:0] ki_product;
- integer i;
- wire [15:0] bmc_integer_scaled = ({8'd0, bmc_rd_raw[15:8]} << 6) +
- ({8'd0, bmc_rd_raw[15:8]} << 5) + ({8'd0, bmc_rd_raw[15:8]} << 2);
- wire signed [15:0] bmc_temperature = $signed(bmc_integer_scaled + {8'd0, bmc_rd_raw[7:0]});
- wire signed [15:0] live_error = $signed(target_centi) - bmc_temperature;
- wire edge_channel = (work_index < 6) || (work_index >= 18);
- wire signed [15:0] selected_kp = edge_channel ? EDGE_KP : CENTER_KP;
- wire signed [15:0] selected_ki = edge_channel ? EDGE_KI : CENTER_KI;
- reg signed [15:0] mult_a;
- reg signed [15:0] mult_b;
- wire signed [31:0] mult_result = mult_a * mult_b;
- wire signed [31:0] pi_sum = kp_product + ki_product;
- wire signed [31:0] pi_delta = (pi_sum >= 0) ? ((pi_sum + 8) >>> 4) : -(((-pi_sum) + 8) >>> 4);
- wire signed [31:0] previous_output_ext = {{16{previous_output[work_index][15]}}, previous_output[work_index]};
- wire signed [31:0] pi_candidate = previous_output_ext + pi_delta;
- wire signed [31:0] slew_low = previous_output_ext - SLEW_Q8_8;
- wire signed [31:0] slew_high = previous_output_ext + SLEW_Q8_8;
- wire signed [31:0] slew_candidate = (pi_candidate < slew_low) ? slew_low : ((pi_candidate > slew_high) ? slew_high : pi_candidate);
- wire signed [15:0] clamped_output = (slew_candidate < 0) ? 16'sd0 : ((slew_candidate > 25600) ? 16'sd25600 : slew_candidate[15:0]);
- wire signed [15:0] reduced_output = previous_output[work_index] - 16'sd256;
- wire [7:0] clamped_duty = (clamped_output >= 16'sd25600) ? 8'd100 : q8_8_to_duty(clamped_output);
- wire [7:0] reduced_duty = (previous_output[work_index] > 16'sd256) ? q8_8_to_duty(reduced_output) : 8'd0;
- wire [7:0] inactive_request = active_output_bank ? req0[work_index] : req1[work_index];
- wire signed [15:0] recovery_threshold = $signed(target_centi) + RECOVERY_MARGIN;
- assign stimulus_active = stimulus_running;
- function [1:0] lane_for_channel;
- input [4:0] channel;
- begin
- if (channel < 6) lane_for_channel = 0;
- else if (channel < 12) lane_for_channel = 1;
- else if (channel < 18) lane_for_channel = 2;
- else lane_for_channel = 3;
- end
- endfunction
- function [7:0] q8_8_to_duty;
- input signed [15:0] value;
- /* verilator lint_off UNUSEDSIGNAL */
- reg signed [15:0] rounded;
- /* verilator lint_on UNUSEDSIGNAL */
- begin
- rounded = (value + 16'sd128) >>> 8;
- q8_8_to_duty = rounded[7:0];
- end
- endfunction
- function lane_fault_for_output;
- input [4:0] address;
- reg [1:0] lane;
- begin
- if (address < 24) lane = lane_for_channel(address);
- else lane = address[1:0];
- lane_fault_for_output = lane_fault_mask[lane];
- end
- endfunction
- function [1:0] lane_for_output;
- input [4:0] address;
- begin
- if (address < 24)
- lane_for_output = lane_for_channel(address);
- else
- lane_for_output = address[1:0];
- end
- endfunction
- function lane_invalid;
- input [1:0] lane;
- begin
- case (lane)
- 0: lane_invalid = invalid_count0 >= 2;
- 1: lane_invalid = invalid_count1 >= 2;
- 2: lane_invalid = invalid_count2 >= 2;
- default: lane_invalid = invalid_count3 >= 2;
- endcase
- end
- endfunction
- function lane_thermal_protection;
- input [1:0] lane;
- begin
- case (lane)
- 0: lane_thermal_protection = |thermal_protection_mask[5:0];
- 1: lane_thermal_protection = |thermal_protection_mask[11:6];
- 2: lane_thermal_protection = |thermal_protection_mask[17:12];
- default: lane_thermal_protection = |thermal_protection_mask[23:18];
- endcase
- end
- endfunction
- wire [7:0] apply_ptc_value =
- (global_fault || control_state == STOPPED ||
- lane_fault_for_output(work_index) ||
- lane_invalid(lane_for_channel(work_index)) ||
- ptc_shutdown_mask[work_index]) ? 8'd0 : inactive_request;
- wire [7:0] apply_fan_value =
- (global_fault || lane_fault_for_output(work_index) ||
- lane_invalid(lane_for_output(work_index))) ? 8'd100 :
- (lane_thermal_protection(lane_for_output(work_index)) ? 8'd80 : inactive_request);
- wire [7:0] safe_fill_value = (work_index < 24) ? 8'd0 :
- ((global_fault || lane_fault_for_output(work_index) ||
- lane_invalid(lane_for_output(work_index))) ? 8'd100 :
- (lane_thermal_protection(lane_for_output(work_index)) ? 8'd80 : safe_fan_target));
- // BMC age intentionally resets stale. That is an acquisition-startup
- // condition, not a runtime sensor failure: CT qualification and preheat
- // must still run before BMC stability is evaluated.
- wire bmc_stale_runtime = bmc_stale &&
- (control_state == AUTO_BMC || control_state == SENSOR_DEGRADED);
- always @* begin
- mult_a = 0;
- mult_b = 0;
- if (work_state == W_KP) begin
- mult_a = live_error - previous_error[work_index[4:0]];
- mult_b = selected_kp;
- end else if (work_state == W_KI) begin
- mult_a = sample_error;
- mult_b = selected_ki;
- end
- if (snapshot_output_bank) begin
- snapshot_requested_data = req1[snapshot_rd_addr];
- snapshot_applied_data = app1[snapshot_rd_addr];
- end else begin
- snapshot_requested_data = req0[snapshot_rd_addr];
- snapshot_applied_data = app0[snapshot_rd_addr];
- end
- end
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- active_output_bank <= 0;
- control_state <= RESET_SAFE;
- current_target_celsius <= 70;
- target_centi <= DEFAULT_TARGET;
- host_target_seen <= 0;
- control_epoch <= 0;
- safety_override_mask <= 0;
- ptc_shutdown_mask <= 0;
- thermal_protection_mask <= 0;
- lane_fault_mask <= 0;
- global_fault <= 0;
- output_commit <= 0;
- pwm_rd_data <= 0;
- work_state <= W_IDLE;
- work_index <= 0;
- bmc_rd_index <= 0;
- last_bmc_epoch <= 0;
- last_ct_epoch <= 0;
- stable_epochs <= 0;
- epoch_safe <= 0;
- invalid_count0 <= 0;
- invalid_count1 <= 0;
- invalid_count2 <= 0;
- invalid_count3 <= 0;
- stimulus_running <= 0;
- stimulus_remaining <= 0;
- safe_fan_target <= 10;
- sample_valid <= 0;
- sample_error <= 0;
- sample_temperature <= 0;
- kp_product <= 0;
- ki_product <= 0;
- for (i = 0; i < 32; i = i + 1) begin
- req0[i] <= (i < 24) ? 0 : 10;
- req1[i] <= (i < 24) ? 0 : 10;
- app0[i] <= (i < 24) ? 0 : 10;
- app1[i] <= (i < 24) ? 0 : 10;
- stimulus_mem[i] <= 0;
- end
- for (i = 0; i < 24; i = i + 1) begin
- previous_error[i] <= 0;
- previous_output[i] <= 0;
- end
- end else begin
- output_commit <= 0;
- if (active_output_bank)
- pwm_rd_data <= app1[pwm_rd_addr];
- else
- pwm_rd_data <= app0[pwm_rd_addr];
- if (stimulus_wr_en && (ENABLE_STIMULUS != 0))
- stimulus_mem[stimulus_wr_addr] <= stimulus_wr_data;
- if (stimulus_commit && (ENABLE_STIMULUS != 0) && stimulus_duration_seconds != 0) begin
- stimulus_running <= 1;
- stimulus_remaining <= stimulus_duration_seconds;
- end
- if (tick_1s && stimulus_running) begin
- if (stimulus_remaining <= 1) begin
- stimulus_running <= 0;
- stimulus_remaining <= 0;
- end else stimulus_remaining <= stimulus_remaining - 1'b1;
- end
- if (target_commit) begin
- current_target_celsius <= target_celsius;
- target_centi <= ({8'd0, target_celsius} << 6) +
- ({8'd0, target_celsius} << 5) + ({8'd0, target_celsius} << 2);
- host_target_seen <= 1;
- stable_epochs <= 0;
- stimulus_running <= 0;
- work_index <= 0;
- work_state <= W_SAFE_FILL;
- safe_fan_target <= (global_fault || bmc_stale_runtime) ? 8'd100 :
- ((target_celsius == 0) ? 8'd30 : 8'd10);
- global_fault <= global_fault || bmc_stale_runtime;
- if (target_celsius == 0)
- control_state <= STOPPED;
- else if (global_fault || bmc_stale_runtime)
- control_state <= SENSOR_DEGRADED;
- else
- control_state <= WAIT_CT_VALID;
- for (i = 0; i < 24; i = i + 1) begin
- previous_error[i] <= 0;
- previous_output[i] <= 0;
- end
- end else if (bmc_stale_runtime && !global_fault) begin
- global_fault <= 1;
- control_state <= SENSOR_DEGRADED;
- stable_epochs <= 0;
- work_index <= 0;
- work_state <= W_SAFE_FILL;
- safe_fan_target <= 100;
- stimulus_running <= 0;
- end else begin
- case (work_state)
- W_IDLE: begin
- if (control_state == RESET_SAFE) begin
- control_state <= WAIT_CT_VALID;
- end else if (control_state == WAIT_CT_VALID) begin
- if (!ct_stale && (&ct_valid_mask) && ct_epoch != last_ct_epoch) begin
- last_ct_epoch <= ct_epoch;
- control_state <= PREHEAT_CT;
- end
- end else if (control_state == PREHEAT_CT) begin
- if (bmc_epoch != last_bmc_epoch) begin
- last_bmc_epoch <= bmc_epoch;
- stable_epochs <= 0;
- control_state <= WAIT_BMC_STABLE;
- end
- end else if (control_state == WAIT_BMC_STABLE) begin
- if (bmc_epoch != last_bmc_epoch) begin
- last_bmc_epoch <= bmc_epoch;
- if (&bmc_valid_mask) begin
- if (stable_epochs == 2) begin
- stable_epochs <= 0;
- control_state <= AUTO_BMC;
- work_index <= 0;
- work_state <= W_PREP;
- end else stable_epochs <= stable_epochs + 1'b1;
- end else stable_epochs <= 0;
- end
- end else if ((control_state == AUTO_BMC || control_state == SENSOR_DEGRADED) &&
- !bmc_stale && bmc_epoch != last_bmc_epoch &&
- !(snapshot_lock && ((~active_output_bank) == snapshot_lock_bank))) begin
- last_bmc_epoch <= bmc_epoch;
- work_index <= 0;
- work_state <= W_PREP;
- end
- end
- W_PREP: begin
- bmc_rd_index <= work_index;
- if (work_index == 0) begin
- epoch_safe <= 1;
- invalid_count0 <= 0;
- invalid_count1 <= 0;
- invalid_count2 <= 0;
- invalid_count3 <= 0;
- end
- work_state <= W_KP;
- end
- W_KP: begin
- sample_valid <= bmc_rd_valid;
- sample_temperature <= bmc_temperature;
- sample_error <= live_error;
- kp_product <= mult_result;
- work_state <= W_KI;
- end
- W_KI: begin
- ki_product <= mult_result;
- work_state <= W_UPDATE;
- end
- W_UPDATE: begin
- if (!sample_valid) begin
- if (active_output_bank) begin req0[work_index] <= 0; end else begin req1[work_index] <= 0; end
- safety_override_mask[work_index] <= 1;
- ptc_shutdown_mask[work_index] <= 1;
- thermal_protection_mask[work_index] <= 0;
- previous_error[work_index] <= 0;
- previous_output[work_index] <= 0;
- epoch_safe <= 0;
- control_state <= SENSOR_DEGRADED;
- case (lane_for_channel(work_index))
- 0: invalid_count0 <= invalid_count0 + 1'b1;
- 1: invalid_count1 <= invalid_count1 + 1'b1;
- 2: invalid_count2 <= invalid_count2 + 1'b1;
- 3: invalid_count3 <= invalid_count3 + 1'b1;
- endcase
- end else if (sample_temperature >= HARD_LIMIT) begin
- if (active_output_bank) req0[work_index] <= 0; else req1[work_index] <= 0;
- safety_override_mask[work_index] <= 1;
- ptc_shutdown_mask[work_index] <= 1;
- thermal_protection_mask[work_index] <= 1;
- previous_error[work_index] <= 0;
- previous_output[work_index] <= 0;
- lane_fault_mask[lane_for_channel(work_index)] <= 1;
- epoch_safe <= 0;
- control_state <= SENSOR_DEGRADED;
- stimulus_running <= 0;
- end else if (sample_temperature >= PRETRIP_LIMIT) begin
- if (active_output_bank) req0[work_index] <= 0; else req1[work_index] <= 0;
- safety_override_mask[work_index] <= 1;
- ptc_shutdown_mask[work_index] <= 1;
- thermal_protection_mask[work_index] <= 1;
- previous_error[work_index] <= 0;
- previous_output[work_index] <= 0;
- epoch_safe <= 0;
- control_state <= SENSOR_DEGRADED;
- end else if (global_fault || lane_fault_for_output(work_index)) begin
- // A global/lane clamp must also clear the hidden PI
- // state. Otherwise recovery could expose duty that
- // accumulated while the physical output was forced
- // off, violating bumpless limited-duty restart.
- if (active_output_bank) req0[work_index] <= 0; else req1[work_index] <= 0;
- safety_override_mask[work_index] <= 0;
- ptc_shutdown_mask[work_index] <= 0;
- thermal_protection_mask[work_index] <= 0;
- previous_error[work_index] <= 0;
- previous_output[work_index] <= 0;
- end else begin
- // Safety precedes identification stimulus. The
- // soft limit reduces the autonomous PI state by
- // one duty point and requests protection airflow,
- // but deliberately does not assert PTC shutdown.
- if (sample_temperature >= SOFT_LIMIT) begin
- if (active_output_bank)
- req0[work_index] <= reduced_duty;
- else
- req1[work_index] <= reduced_duty;
- if (previous_output[work_index] > 16'sd256) previous_output[work_index] <= previous_output[work_index] - 16'sd256; else previous_output[work_index] <= 0;
- previous_error[work_index] <= sample_error;
- safety_override_mask[work_index] <= 1;
- ptc_shutdown_mask[work_index] <= 0;
- thermal_protection_mask[work_index] <= 1;
- end else if (stimulus_running) begin
- if (active_output_bank) req0[work_index] <= stimulus_mem[work_index]; else req1[work_index] <= stimulus_mem[work_index];
- safety_override_mask[work_index] <= 0;
- ptc_shutdown_mask[work_index] <= 0;
- thermal_protection_mask[work_index] <= 0;
- end else begin
- if (active_output_bank) req0[work_index] <= clamped_duty; else req1[work_index] <= clamped_duty;
- previous_output[work_index] <= clamped_output;
- previous_error[work_index] <= sample_error;
- safety_override_mask[work_index] <= 0;
- ptc_shutdown_mask[work_index] <= 0;
- thermal_protection_mask[work_index] <= 0;
- end
- if (sample_temperature > recovery_threshold) epoch_safe <= 0;
- end
- if (work_index == 23) begin
- work_index <= 24;
- work_state <= W_FANS;
- if (control_state == SENSOR_DEGRADED) begin
- if (epoch_safe && sample_valid && sample_temperature <= recovery_threshold) begin
- if (stable_epochs == RECOVERY_EPOCHS - 1'b1) begin
- stable_epochs <= 0;
- lane_fault_mask <= 0;
- global_fault <= 0;
- control_state <= AUTO_BMC;
- end else stable_epochs <= stable_epochs + 1'b1;
- end else stable_epochs <= 0;
- end
- end else begin
- work_index <= work_index + 1'b1;
- work_state <= W_PREP;
- end
- end
- W_FANS: begin
- if (active_output_bank)
- req0[work_index] <= stimulus_running ? stimulus_mem[work_index] : 8'd30;
- else
- req1[work_index] <= stimulus_running ? stimulus_mem[work_index] : 8'd30;
- if (work_index == 31) begin work_index <= 0; work_state <= W_APPLY; end
- else work_index <= work_index + 1'b1;
- end
- W_APPLY: begin
- if (active_output_bank) begin
- if (work_index < 24)
- app0[work_index] <= apply_ptc_value;
- else
- app0[work_index] <= apply_fan_value;
- end else begin
- if (work_index < 24)
- app1[work_index] <= apply_ptc_value;
- else
- app1[work_index] <= apply_fan_value;
- end
- if (work_index == 31) begin
- active_output_bank <= ~active_output_bank;
- control_epoch <= control_epoch + 1'b1;
- output_commit <= 1;
- work_index <= 0;
- work_state <= W_IDLE;
- end else work_index <= work_index + 1'b1;
- end
- W_SAFE_FILL: begin
- if (active_output_bank) begin
- req0[work_index] <= safe_fill_value;
- app0[work_index] <= safe_fill_value;
- end else begin
- req1[work_index] <= safe_fill_value;
- app1[work_index] <= safe_fill_value;
- end
- if (work_index == 31) begin
- active_output_bank <= ~active_output_bank;
- control_epoch <= control_epoch + 1'b1;
- output_commit <= 1;
- work_index <= 0;
- work_state <= W_IDLE;
- end else work_index <= work_index + 1'b1;
- end
- default: work_state <= W_IDLE;
- endcase
- end
- end
- end
- `ifdef FORMAL
- reg formal_past_valid;
- (* anyconst *) reg [5:0] formal_duty_index;
- (* anyconst *) reg [4:0] formal_ptc_index;
- wire [7:0] formal_active_applied = active_output_bank ?
- app1[formal_duty_index] : app0[formal_duty_index];
- wire [7:0] formal_inactive_applied = active_output_bank ?
- app0[formal_duty_index] : app1[formal_duty_index];
- wire [7:0] formal_safe_fill_value = (formal_duty_index < 24) ? 8'd0 :
- ((global_fault || lane_fault_for_output(formal_duty_index) ||
- lane_invalid(lane_for_output(formal_duty_index))) ?
- 8'd100 :
- (lane_thermal_protection(lane_for_output(formal_duty_index)) ?
- 8'd80 : safe_fan_target));
- wire formal_stale_preempt = bmc_stale_runtime && !global_fault;
- function [7:0] formal_work_rank;
- input [3:0] state_value;
- input [4:0] index_value;
- begin
- case (state_value)
- W_PREP: formal_work_rank = 8'd136 - ({3'd0, index_value} << 2);
- W_KP: formal_work_rank = 8'd135 - ({3'd0, index_value} << 2);
- W_KI: formal_work_rank = 8'd134 - ({3'd0, index_value} << 2);
- W_UPDATE: formal_work_rank = 8'd133 - ({3'd0, index_value} << 2);
- W_FANS: formal_work_rank = 8'd64 - index_value;
- W_APPLY: formal_work_rank = 8'd32 - index_value;
- W_SAFE_FILL: formal_work_rank = 8'd64 - index_value;
- default: formal_work_rank = 8'd0;
- endcase
- end
- endfunction
- wire [7:0] formal_scheduler_rank = formal_work_rank(work_state, work_index);
- initial formal_past_valid = 0;
- always @(posedge clk) begin
- formal_past_valid <= 1;
- if (stimulus_wr_en) assume(stimulus_wr_data <= 100);
- if (target_commit)
- assume(target_celsius == 0 || (target_celsius >= 20 && target_celsius <= 70));
- assume(formal_duty_index < 32);
- assume(formal_ptc_index < 24);
- // P01: every requested and applied logical duty remains in 0..100.
- assert(req0[formal_duty_index] <= 100);
- assert(req1[formal_duty_index] <= 100);
- assert(app0[formal_duty_index] <= 100);
- assert(app1[formal_duty_index] <= 100);
- assert(stimulus_mem[formal_duty_index] <= 100);
- assert(safe_fan_target <= 100);
- assert(control_state <= STOPPED);
- assert(current_target_celsius == 0 ||
- (current_target_celsius >= 20 && current_target_celsius <= 70));
- assert(target_centi <= 16'd7000);
- if (formal_duty_index < 24) begin
- assert(previous_output[formal_duty_index] >= 0);
- assert(previous_output[formal_duty_index] <= 25600);
- end
- if (!rst_n) begin
- // P17: reset state is safe and deterministic.
- assert(control_state == RESET_SAFE);
- assert(current_target_celsius == 70);
- assert(safety_override_mask == 0);
- assert(ptc_shutdown_mask == 0);
- assert(thermal_protection_mask == 0);
- assert(lane_fault_mask == 0);
- assert(!global_fault);
- if (formal_duty_index < 24) begin
- assert(app0[formal_duty_index] == 0);
- assert(app1[formal_duty_index] == 0);
- end else begin
- assert(app0[formal_duty_index] == 10);
- assert(app1[formal_duty_index] == 10);
- end
- end else begin
- // P10: an admitted scheduler pass has a finite rank that strictly
- // decreases every clock unless target/stale safety preempts it.
- assert(formal_scheduler_rank <= 136);
- assert(work_state <= W_SAFE_FILL);
- if (work_state == W_PREP || work_state == W_KP ||
- work_state == W_KI || work_state == W_UPDATE)
- assert(work_index < 24);
- if (work_state == W_FANS)
- assert(work_index >= 24);
- // Reset-age stale must not bypass CT/preheat startup sequencing.
- if (bmc_stale && (control_state == RESET_SAFE ||
- control_state == WAIT_CT_VALID ||
- control_state == PREHEAT_CT ||
- control_state == WAIT_BMC_STABLE))
- assert(!formal_stale_preempt);
- // P02/P03/P04/P05/P16: the production apply datapath gives every
- // hard safety condition priority over an ordinary PTC request.
- if (work_state == W_APPLY && work_index == formal_ptc_index &&
- (global_fault || control_state == STOPPED ||
- ptc_shutdown_mask[formal_ptc_index] ||
- lane_fault_mask[lane_for_channel(formal_ptc_index)] ||
- lane_invalid(lane_for_channel(formal_ptc_index))))
- assert(apply_ptc_value == 0);
- // Inductive sweep invariants: every address below work_index has
- // already been written into the inactive bank by this pass.
- if (work_state == W_SAFE_FILL &&
- formal_duty_index < work_index)
- assert(formal_inactive_applied == formal_safe_fill_value);
- if (work_state == W_APPLY &&
- formal_duty_index < work_index) begin
- if (formal_duty_index < 24 &&
- (global_fault || control_state == STOPPED ||
- ptc_shutdown_mask[formal_duty_index] ||
- lane_fault_mask[lane_for_channel(formal_duty_index)] ||
- lane_invalid(lane_for_channel(formal_duty_index))))
- assert(formal_inactive_applied == 0);
- if (formal_duty_index >= 24 &&
- (global_fault ||
- lane_fault_for_output(formal_duty_index) ||
- lane_invalid(lane_for_output(formal_duty_index))))
- assert(formal_inactive_applied == 100);
- if (formal_duty_index >= 24 &&
- !global_fault &&
- !lane_fault_for_output(formal_duty_index) &&
- !lane_invalid(lane_for_output(formal_duty_index)) &&
- lane_thermal_protection(lane_for_output(formal_duty_index)))
- assert(formal_inactive_applied == 80);
- end
- // Once a safety pass has committed, the active bank itself (not
- // merely the combinational apply value) is safe. W_IDLE is the
- // post-commit quiescent point, so these assertions compose the
- // detection, fill, bank flip, and externally visible duty state.
- if (work_state == W_IDLE) begin
- if (formal_duty_index < 24 &&
- (global_fault || control_state == STOPPED ||
- ptc_shutdown_mask[formal_duty_index] ||
- lane_fault_mask[lane_for_channel(formal_duty_index)] ||
- lane_invalid(lane_for_channel(formal_duty_index))))
- assert(formal_active_applied == 0);
- if (formal_duty_index >= 24 &&
- (global_fault ||
- lane_fault_for_output(formal_duty_index) ||
- lane_invalid(lane_for_output(formal_duty_index))))
- assert(formal_active_applied == 100);
- if (formal_duty_index >= 24 &&
- !global_fault &&
- !lane_fault_for_output(formal_duty_index) &&
- !lane_invalid(lane_for_output(formal_duty_index)) &&
- lane_thermal_protection(lane_for_output(formal_duty_index)))
- assert(formal_active_applied == 80);
- end
- if (formal_past_valid && $past(rst_n)) begin
- if ($past(formal_scheduler_rank) != 0 &&
- !$past(target_commit) && !$past(formal_stale_preempt))
- assert(formal_scheduler_rank < $past(formal_scheduler_rank));
- // P03: a newly observed stale source starts the bounded safe
- // fill at address zero and latches the global fault. The rank
- // invariant above forces that fill to reach the safe commit.
- if ($past(formal_stale_preempt && !target_commit)) begin
- assert(global_fault);
- assert(control_state == SENSOR_DEGRADED);
- assert(work_state == W_SAFE_FILL);
- assert(work_index == 0);
- assert(safe_fan_target == 100);
- end
- // P04: an invalid sample records both the zero request and the
- // channel override before the common apply/commit sequence.
- if ($past(work_state == W_UPDATE &&
- work_index == formal_ptc_index && !sample_valid &&
- !target_commit && !formal_stale_preempt)) begin
- assert(safety_override_mask[formal_ptc_index]);
- assert(ptc_shutdown_mask[formal_ptc_index]);
- assert(!thermal_protection_mask[formal_ptc_index]);
- if ($past(active_output_bank))
- assert(req0[formal_ptc_index] == 0);
- else
- assert(req1[formal_ptc_index] == 0);
- end
- // P05: a sampled hard limit latches the lane fault before the
- // common apply pass zeros every PTC belonging to that lane.
- if ($past(work_state == W_UPDATE &&
- work_index == formal_ptc_index && sample_valid &&
- sample_temperature >= HARD_LIMIT &&
- !target_commit && !formal_stale_preempt)) begin
- assert(lane_fault_mask[lane_for_channel(formal_ptc_index)]);
- assert(ptc_shutdown_mask[formal_ptc_index]);
- assert(thermal_protection_mask[formal_ptc_index]);
- end
- // Soft and pretrip telemetry share the public override bit,
- // but only pretrip shuts the PTC down. Both request 80%
- // airflow until a normal sample clears the channel masks.
- if ($past(work_state == W_UPDATE &&
- work_index == formal_ptc_index && sample_valid &&
- sample_temperature >= PRETRIP_LIMIT &&
- sample_temperature < HARD_LIMIT &&
- !target_commit && !formal_stale_preempt)) begin
- assert(safety_override_mask[formal_ptc_index]);
- assert(ptc_shutdown_mask[formal_ptc_index]);
- assert(thermal_protection_mask[formal_ptc_index]);
- end
- if ($past(work_state == W_UPDATE &&
- work_index == formal_ptc_index && sample_valid &&
- sample_temperature >= SOFT_LIMIT &&
- sample_temperature < PRETRIP_LIMIT &&
- !global_fault &&
- !lane_fault_mask[lane_for_channel(formal_ptc_index)] &&
- !target_commit && !formal_stale_preempt)) begin
- assert(safety_override_mask[formal_ptc_index]);
- assert(!ptc_shutdown_mask[formal_ptc_index]);
- assert(thermal_protection_mask[formal_ptc_index]);
- end
- // Fault-clamped control passes cannot accumulate hidden PI
- // state or a nonzero request for later release.
- if ($past(work_state == W_UPDATE &&
- work_index == formal_ptc_index && sample_valid &&
- sample_temperature < PRETRIP_LIMIT &&
- (global_fault ||
- lane_fault_mask[lane_for_channel(formal_ptc_index)]) &&
- !target_commit && !formal_stale_preempt)) begin
- assert(previous_error[formal_ptc_index] == 0);
- assert(previous_output[formal_ptc_index] == 0);
- if ($past(active_output_bank))
- assert(req0[formal_ptc_index] == 0);
- else
- assert(req1[formal_ptc_index] == 0);
- end
- // P12: absence of a Host target commit cannot create STOPPED.
- if (!$past(target_commit) && $past(control_state) != STOPPED)
- assert(control_state != STOPPED);
- // P13: stimulus has a strictly decreasing per-tick rank and
- // reaches inactive when the remaining rank is one.
- if ($past(stimulus_running && tick_1s && !stimulus_commit)) begin
- if ($past(stimulus_remaining) <= 1)
- assert(!stimulus_running);
- else if (stimulus_running)
- assert(stimulus_remaining == $past(stimulus_remaining) - 1'b1);
- end
- end
- // Reachability witnesses are discharged by the dedicated cover
- // tasks; they prevent the safety antecedents from being vacuous.
- cover(global_fault && work_state == W_IDLE &&
- formal_ptc_index < 24 &&
- (active_output_bank ? app1[formal_ptc_index] :
- app0[formal_ptc_index]) == 0);
- cover(ptc_shutdown_mask[formal_ptc_index] &&
- work_state == W_IDLE &&
- (active_output_bank ? app1[formal_ptc_index] :
- app0[formal_ptc_index]) == 0);
- cover(lane_fault_mask[lane_for_channel(formal_ptc_index)] &&
- work_state == W_IDLE &&
- (active_output_bank ? app1[formal_ptc_index] :
- app0[formal_ptc_index]) == 0);
- cover(control_state == STOPPED && work_state == W_IDLE &&
- (active_output_bank ? app1[formal_ptc_index] :
- app0[formal_ptc_index]) == 0);
- cover(stimulus_running && stimulus_remaining != 0);
- cover(formal_past_valid &&
- $past(stimulus_running && tick_1s &&
- stimulus_remaining <= 1 && !stimulus_commit) &&
- !stimulus_running);
- cover(control_state == AUTO_BMC && work_state == W_UPDATE);
- end
- end
- `endif
- endmodule
|