| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- `timescale 1ns/1ps
- module thermal_control_hard_race_tb;
- reg clk = 0, rst_n = 0, tick = 0;
- reg manual_wr = 0, manual_commit = 0;
- reg [4:0] manual_addr = 0;
- reg [7:0] manual_data = 0;
- reg [15:0] bmc_epoch = 0;
- reg bmc_stale = 1;
- reg [23:0] bmc_mask = 24'hffffff;
- reg [15:0] bmc_raw [0:23];
- wire [4:0] bmc_index;
- reg pwm_boundary = 1;
- wire manual_done, manual_ok, active_bank, output_commit;
- wire [2:0] owner;
- wire [3:0] lane_fault;
- wire [7:0] pwm_data;
- reg [4:0] pwm_addr = 0;
- integer i, timeout, commit_count = 0;
- always #5 clk = ~clk;
- always @(posedge clk) if (output_commit) commit_count <= commit_count + 1;
- thermal_control_core #(.WATCHDOG_TICKS_OVERRIDE(4)) 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(1'b0), .pid_target_celsius(8'd70),
- .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(16'd0), .ct_stale(1'b1),
- .ct_valid_mask(27'd0), .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(5'd0),
- .snapshot_requested_data(), .snapshot_applied_data(),
- .active_output_bank(active_bank), .control_state(owner),
- .current_target_celsius(), .control_epoch(), .safety_override_mask(),
- .lane_fault_mask(lane_fault), .global_fault(), .output_commit(output_commit),
- .watchdog_active(), .watchdog_remaining(), .pid_running(), .manual_mode(),
- .hard_protect(), .pwm_phase_boundary(pwm_boundary), .pwm_scan_idle(1'b1),
- .boot_init_complete()
- );
- 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
- 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);
- for (i = 0; i < 32; i = i + 1) begin
- @(negedge clk); manual_wr = 1; manual_addr = i[4:0];
- manual_data = (i < 24) ? 8'd80 : 8'd60;
- end
- @(negedge clk); manual_wr = 0;
- // Hold the atomic output boundary after the complete Manual Bank has
- // committed, then inject a new complete 77 C BMC epoch.
- pwm_boundary = 0;
- @(negedge clk); manual_commit = 1;
- @(negedge clk); manual_commit = 0;
- timeout = 0;
- while (dut.work_state != 9 && timeout < 300) begin
- @(negedge clk); timeout = timeout + 1;
- end
- if (timeout == 300) $fatal(1, "manual never reached output commit wait");
- bmc_stale = 0;
- bmc_raw[0] = {8'd77, 8'd0};
- bmc_epoch = 1;
- timeout = 0;
- while (!manual_done && timeout < 500) begin
- @(negedge clk); timeout = timeout + 1;
- end
- if (timeout == 500 || manual_ok)
- $fatal(1, "in-flight manual was not cancelled by new hard lock");
- if (owner == 1) $fatal(1, "cancelled manual gained ownership");
- check_duty(0, 0);
- check_duty(24, 20);
- pwm_boundary = 1;
- timeout = 0;
- while ((!lane_fault[0] || commit_count == 0) && timeout < 500) begin
- @(negedge clk); timeout = timeout + 1;
- end
- if (timeout == 500 || owner != 2) $fatal(1, "hard safe commit missing");
- check_duty(0, 0);
- check_duty(24, 100);
- check_duty(28, 100);
- check_duty(25, 20);
- $display("PASS: new hard epoch cancels in-flight Manual before output Bank apply");
- $finish;
- end
- endmodule
|