`timescale 1ns/1ps module thermal_control_core_tb; reg clk = 0; reg rst_n = 0; reg tick = 0; reg target_commit = 0; reg [7:0] target = 70; reg [15:0] bmc_epoch = 0; reg bmc_stale = 1; reg [23:0] bmc_valid_mask = 24'hffffff; wire [4:0] bmc_index; reg [15:0] bmc_raw [0:23]; wire [15:0] bmc_data = bmc_raw[bmc_index]; wire bmc_valid = bmc_valid_mask[bmc_index]; reg [15:0] ct_epoch = 0; reg ct_stale = 0; reg [26:0] ct_valid_mask = 27'h7ffffff; reg [4:0] pwm_addr = 0; wire [7:0] pwm_data; reg [4:0] snapshot_addr = 0; wire [7:0] snapshot_req; wire [7:0] snapshot_app; wire active_bank; wire [2:0] state; wire [7:0] current_target; wire host_seen; wire [15:0] control_epoch; wire [23:0] safety; wire [3:0] lane_fault; wire global_fault; wire output_commit; wire stimulus_active; integer i; integer commits = 0; always #5 clk = ~clk; always @(posedge clk) if (output_commit) commits <= commits + 1; thermal_control_core dut ( .clk(clk), .rst_n(rst_n), .tick_1s(tick), .target_commit(target_commit), .target_celsius(target), .stimulus_wr_en(1'b0), .stimulus_wr_addr(5'd0), .stimulus_wr_data(8'd0), .stimulus_commit(1'b0), .stimulus_duration_seconds(8'd0), .bmc_epoch(bmc_epoch), .bmc_stale(bmc_stale), .bmc_valid_mask(bmc_valid_mask), .bmc_rd_index(bmc_index), .bmc_rd_raw(bmc_data), .bmc_rd_valid(bmc_valid), .ct_epoch(ct_epoch), .ct_stale(ct_stale), .ct_valid_mask(ct_valid_mask), .snapshot_lock(1'b0), .snapshot_lock_bank(1'b0), .pwm_rd_addr(pwm_addr), .pwm_rd_data(pwm_data), .snapshot_output_bank(active_bank), .snapshot_rd_addr(snapshot_addr), .snapshot_requested_data(snapshot_req), .snapshot_applied_data(snapshot_app), .active_output_bank(active_bank), .control_state(state), .current_target_celsius(current_target), .host_target_seen(host_seen), .control_epoch(control_epoch), .safety_override_mask(safety), .lane_fault_mask(lane_fault), .global_fault(global_fault), .output_commit(output_commit), .stimulus_active(stimulus_active) ); task next_bmc_epoch; begin @(negedge clk); bmc_epoch = bmc_epoch + 1'b1; repeat (3) @(negedge clk); end endtask task wait_commit_after; input integer old_count; integer timeout; begin timeout = 0; while (commits == old_count && timeout < 1000) begin @(negedge clk); timeout = timeout + 1; end if (timeout >= 1000) begin $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); $fatal; end end endtask task check_duty; input [4:0] address; input [7:0] expected; begin pwm_addr = address; repeat (2) @(negedge clk); if (pwm_data !== expected) begin $display("duty[%0d]=%0d expected=%0d", address, pwm_data, expected); $fatal; end end endtask initial begin for (i = 0; i < 24; i = i + 1) bmc_raw[i] = {8'd60, 8'd0}; repeat (4) @(negedge clk); rst_n = 1; repeat (3) @(negedge clk); if (state !== 1 || global_fault) $fatal; ct_epoch = 1; repeat (3) @(negedge clk); if (state !== 2 || global_fault) $fatal; bmc_stale = 0; next_bmc_epoch(); if (state !== 3) $fatal; next_bmc_epoch(); next_bmc_epoch(); next_bmc_epoch(); wait_commit_after(0); if (state !== 4 || control_epoch == 0) $fatal; check_duty(0, 5); check_duty(24, 30); // 74 C is a soft reduction, not a shutdown: retain a reduced PTC // request and raise both fans for only the affected lane to 80%. bmc_raw[0] = {8'd74, 8'd0}; i = commits; next_bmc_epoch(); wait_commit_after(i); if (!safety[0] || state !== 4) $fatal; check_duty(0, 4); check_duty(24, 80); check_duty(28, 80); check_duty(25, 30); // 76 C shuts down only the matching PTC while retaining 80% lane // airflow; the 77 C lane latch below remains the 100% action. bmc_raw[0] = {8'd76, 8'd0}; i = commits; next_bmc_epoch(); wait_commit_after(i); if (!safety[0] || lane_fault[0] || state !== 5) $fatal; check_duty(0, 0); check_duty(24, 80); check_duty(28, 80); bmc_raw[0] = {8'd77, 8'd0}; i = commits; next_bmc_epoch(); wait_commit_after(i); if (!lane_fault[0] || state !== 5) $fatal; for (i = 0; i < 6; i = i + 1) check_duty(i[4:0], 0); check_duty(24, 100); check_duty(28, 100); i = commits; bmc_stale = 1; wait_commit_after(i); if (!global_fault) $fatal; check_duty(12, 0); check_duty(26, 100); // A legal target commit must not weaken an already-latched stale // fault or its 100% airflow fallback. i = commits; @(negedge clk); target = 65; target_commit = 1; @(negedge clk); target_commit = 0; wait_commit_after(i); if (!global_fault || state !== 5 || current_target !== 65) $fatal; check_duty(0, 0); check_duty(24, 100); check_duty(28, 100); // Recovery holds the global/lane clamps for two complete fresh // epochs and releases them only after the third safe epoch commits. for (i = 0; i < 24; i = i + 1) bmc_raw[i] = {8'd60, 8'd0}; bmc_stale = 0; i = commits; next_bmc_epoch(); wait_commit_after(i); if (!global_fault || !lane_fault[0] || state !== 5) $fatal; i = commits; next_bmc_epoch(); wait_commit_after(i); if (!global_fault || !lane_fault[0] || state !== 5) $fatal; i = commits; next_bmc_epoch(); wait_commit_after(i); if (global_fault || lane_fault != 0 || state !== 4) $fatal; // The release commit is still zero-duty: PI state was cleared while // clamped and may only ramp from the next fresh epoch. check_duty(0, 0); i = commits; next_bmc_epoch(); wait_commit_after(i); check_duty(0, 4); $display("PASS: thermal_control_core_tb"); $finish; end endmodule