| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- `timescale 1ns/1ps
- module thermal_control_core_tb;
- reg clk = 0, rst_n = 0, tick = 0;
- reg manual_wr = 0, manual_commit = 0, pid_commit = 0;
- reg [4:0] manual_addr = 0;
- reg [7:0] manual_data = 0, pid_target = 70;
- reg [15:0] bmc_epoch = 0;
- reg bmc_stale = 1;
- reg [23:0] bmc_mask = 24'hffffff;
- wire [4:0] bmc_index;
- reg [15:0] bmc_raw [0:23];
- reg [4:0] pwm_addr = 0;
- wire [7:0] pwm_data;
- wire manual_done, manual_ok, active_bank, output_commit;
- wire [2:0] mode;
- wire [7:0] current_target, watchdog_remaining;
- wire [15:0] control_epoch;
- wire [23:0] safety;
- wire [3:0] lane_fault;
- wire global_fault, watchdog_active, pid_running, manual_mode, hard_protect;
- integer i, commits = 0;
- always #5 clk = ~clk;
- always @(posedge clk) if (output_commit) commits <= commits + 1;
- thermal_control_core #(.WATCHDOG_TICKS_OVERRIDE(10)) dut (
- .clk(clk), .rst_n(rst_n), .tick_1s(tick),
- .manual_wr_en(manual_wr), .manual_wr_addr(manual_addr),
- .manual_wr_data(manual_data), .manual_commit(manual_commit),
- .manual_commit_done(manual_done), .manual_commit_ok(manual_ok),
- .pid_target_commit(pid_commit), .pid_target_celsius(pid_target),
- .bmc_epoch(bmc_epoch),
- .bmc_stale(bmc_stale), .bmc_valid_mask(bmc_mask),
- .bmc_rd_index(bmc_index), .bmc_rd_raw(bmc_raw[bmc_index]),
- .bmc_rd_valid(bmc_mask[bmc_index]), .ct_epoch(0), .ct_stale(1'b0),
- .ct_valid_mask(27'h7ffffff), .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(0),
- .snapshot_requested_data(), .snapshot_applied_data(),
- .active_output_bank(active_bank), .control_state(mode),
- .current_target_celsius(current_target), .control_epoch(control_epoch),
- .safety_override_mask(safety), .lane_fault_mask(lane_fault),
- .global_fault(global_fault), .output_commit(output_commit),
- .watchdog_active(watchdog_active),
- .watchdog_remaining(watchdog_remaining), .pid_running(pid_running),
- .manual_mode(manual_mode), .hard_protect(hard_protect),
- .pwm_phase_boundary(1'b1), .pwm_scan_idle(1'b1), .boot_init_complete()
- );
- task one_tick;
- begin @(negedge clk); tick = 1; @(negedge clk); tick = 0; end
- endtask
- task wait_commit;
- input integer prior;
- integer timeout;
- begin
- timeout = 0;
- while (commits == prior && timeout < 500) begin @(negedge clk); timeout = timeout + 1; end
- if (timeout == 500) $fatal(1, "commit timeout mode=%0d work=%0d", mode, dut.work_state);
- end
- endtask
- task wait_manual_commit;
- integer timeout;
- begin
- timeout = 0;
- while (!manual_done && timeout < 500) begin @(negedge clk); timeout = timeout + 1; end
- if (timeout == 500) $fatal(1, "manual commit timeout mode=%0d work=%0d", mode, dut.work_state);
- @(negedge clk);
- 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) $fatal(1, "duty[%0d]=%0d expected=%0d", address, pwm_data, expected);
- end
- endtask
- task stage_manual;
- begin
- for (i = 0; i < 32; i = i + 1) begin
- @(negedge clk); manual_wr = 1; manual_addr = i[4:0];
- manual_data = (i < 24) ? 8'd50 : ((i[1:0] == 0) ? 0 : (i[1:0] == 1) ? 20 : (i[1:0] == 2) ? 50 : 100);
- end
- @(negedge clk); manual_wr = 0;
- 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 (40) @(negedge clk);
- if (mode != 0 || !watchdog_active || pid_running || manual_mode || current_target != 70) $fatal;
- check_duty(0, 0); check_duty(24, 20); check_duty(31, 20);
- // First boot timeout atomically commits safe and enters PID wait.
- i = commits;
- repeat (10) one_tick();
- wait_commit(i);
- if (mode != 2 || watchdog_remaining != 0 || pid_running) $fatal;
- check_duty(0, 0); check_duty(24, 20);
- // A new complete valid BMC epoch starts PID; absence never integrates.
- bmc_stale = 0; bmc_epoch = 1;
- i = commits; wait_commit(i);
- if (mode != 2 || !pid_running) $fatal;
- // SET_PID_TARGET changes only the next PID target.
- @(negedge clk); pid_target = 65; pid_commit = 1;
- @(negedge clk); pid_commit = 0;
- if (current_target != 65 || mode != 2) $fatal;
- // SET_ALL atomically enters Manual, freezes PI and reloads the test override.
- stage_manual();
- i = commits;
- @(negedge clk); manual_commit = 1;
- @(negedge clk); manual_commit = 0;
- wait_manual_commit();
- if (mode != 1 || !manual_mode || watchdog_remaining != 10) $fatal;
- check_duty(0, 50); check_duty(24, 0); check_duty(25, 20);
- check_duty(26, 50); check_duty(27, 100);
- // Same vector feeds again; SET_ALL wins over the final timeout tick.
- repeat (9) one_tick();
- if (watchdog_remaining != 1) $fatal;
- stage_manual();
- i = commits;
- @(negedge clk); manual_commit = 1; tick = 1;
- @(negedge clk); manual_commit = 0; tick = 0;
- wait_manual_commit();
- if (mode != 1 || watchdog_remaining != 10) $fatal;
- // Manual timeout returns atomically to safe PID_WAIT_SENSOR.
- i = commits;
- repeat (10) one_tick();
- wait_commit(i);
- if (mode != 2 || manual_mode) $fatal;
- check_duty(0, 0); check_duty(24, 20);
- // A valid 76 C channel is clamped without changing Manual ownership.
- stage_manual();
- i = commits;
- @(negedge clk); manual_commit = 1;
- @(negedge clk); manual_commit = 0;
- wait_manual_commit();
- bmc_raw[0] = {8'd76, 8'd0}; bmc_epoch = 2;
- i = commits; wait_commit(i);
- if (mode != 1 || hard_protect || !safety[0]) $fatal;
- check_duty(0, 0); check_duty(1, 50); check_duty(24, 0);
- // Only confirmed 77 C hard protection may force lane fans to 100.
- bmc_raw[0] = {8'd77, 8'd0}; bmc_epoch = 2;
- bmc_epoch = 3;
- i = commits; wait_commit(i);
- if (mode != 2 || !hard_protect || !lane_fault[0]) $fatal;
- check_duty(0, 0); check_duty(24, 100); check_duty(28, 100);
- check_duty(25, 20);
- $display("PASS: SAFE/MANUAL/PID watchdog, 76C clamp, 77C hard-protect");
- $finish;
- end
- endmodule
|