thermal_control_hard_race_tb.v 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. `timescale 1ns/1ps
  2. module thermal_control_hard_race_tb;
  3. reg clk = 0, rst_n = 0, tick = 0;
  4. reg manual_wr = 0, manual_commit = 0;
  5. reg [4:0] manual_addr = 0;
  6. reg [7:0] manual_data = 0;
  7. reg [15:0] bmc_epoch = 0;
  8. reg bmc_stale = 1;
  9. reg [23:0] bmc_mask = 24'hffffff;
  10. reg [15:0] bmc_raw [0:23];
  11. wire [4:0] bmc_index;
  12. reg pwm_boundary = 1;
  13. wire manual_done, manual_ok, active_bank, output_commit;
  14. wire [2:0] owner;
  15. wire [3:0] lane_fault;
  16. wire [7:0] pwm_data;
  17. reg [4:0] pwm_addr = 0;
  18. integer i, timeout, commit_count = 0;
  19. always #5 clk = ~clk;
  20. always @(posedge clk) if (output_commit) commit_count <= commit_count + 1;
  21. thermal_control_core #(.WATCHDOG_TICKS_OVERRIDE(4)) dut (
  22. .clk(clk), .rst_n(rst_n), .tick_1s(tick),
  23. .manual_wr_en(manual_wr), .manual_wr_addr(manual_addr),
  24. .manual_wr_data(manual_data), .manual_commit(manual_commit),
  25. .manual_commit_done(manual_done), .manual_commit_ok(manual_ok),
  26. .pid_target_commit(1'b0), .pid_target_celsius(8'd70),
  27. .bmc_epoch(bmc_epoch), .bmc_stale(bmc_stale), .bmc_valid_mask(bmc_mask),
  28. .bmc_rd_index(bmc_index), .bmc_rd_raw(bmc_raw[bmc_index]),
  29. .bmc_rd_valid(bmc_mask[bmc_index]), .ct_epoch(16'd0), .ct_stale(1'b1),
  30. .ct_valid_mask(27'd0), .snapshot_lock(1'b0), .snapshot_lock_bank(1'b0),
  31. .pwm_rd_addr(pwm_addr), .pwm_rd_data(pwm_data),
  32. .snapshot_output_bank(active_bank), .snapshot_rd_addr(5'd0),
  33. .snapshot_requested_data(), .snapshot_applied_data(),
  34. .active_output_bank(active_bank), .control_state(owner),
  35. .current_target_celsius(), .control_epoch(), .safety_override_mask(),
  36. .lane_fault_mask(lane_fault), .global_fault(), .output_commit(output_commit),
  37. .watchdog_active(), .watchdog_remaining(), .pid_running(), .manual_mode(),
  38. .hard_protect(), .pwm_phase_boundary(pwm_boundary), .pwm_scan_idle(1'b1),
  39. .boot_init_complete()
  40. );
  41. task check_duty;
  42. input [4:0] address;
  43. input [7:0] expected;
  44. begin
  45. pwm_addr = address;
  46. repeat (2) @(negedge clk);
  47. if (pwm_data !== expected)
  48. $fatal(1, "duty[%0d]=%0d expected=%0d", address, pwm_data, expected);
  49. end
  50. endtask
  51. initial begin
  52. for (i = 0; i < 24; i = i + 1) bmc_raw[i] = {8'd60, 8'd0};
  53. repeat (4) @(negedge clk); rst_n = 1;
  54. repeat (40) @(negedge clk);
  55. for (i = 0; i < 32; i = i + 1) begin
  56. @(negedge clk); manual_wr = 1; manual_addr = i[4:0];
  57. manual_data = (i < 24) ? 8'd80 : 8'd60;
  58. end
  59. @(negedge clk); manual_wr = 0;
  60. // Hold the atomic output boundary after the complete Manual Bank has
  61. // committed, then inject a new complete 77 C BMC epoch.
  62. pwm_boundary = 0;
  63. @(negedge clk); manual_commit = 1;
  64. @(negedge clk); manual_commit = 0;
  65. timeout = 0;
  66. while (dut.work_state != 9 && timeout < 300) begin
  67. @(negedge clk); timeout = timeout + 1;
  68. end
  69. if (timeout == 300) $fatal(1, "manual never reached output commit wait");
  70. bmc_stale = 0;
  71. bmc_raw[0] = {8'd77, 8'd0};
  72. bmc_epoch = 1;
  73. timeout = 0;
  74. while (!manual_done && timeout < 500) begin
  75. @(negedge clk); timeout = timeout + 1;
  76. end
  77. if (timeout == 500 || manual_ok)
  78. $fatal(1, "in-flight manual was not cancelled by new hard lock");
  79. if (owner == 1) $fatal(1, "cancelled manual gained ownership");
  80. check_duty(0, 0);
  81. check_duty(24, 20);
  82. pwm_boundary = 1;
  83. timeout = 0;
  84. while ((!lane_fault[0] || commit_count == 0) && timeout < 500) begin
  85. @(negedge clk); timeout = timeout + 1;
  86. end
  87. if (timeout == 500 || owner != 2) $fatal(1, "hard safe commit missing");
  88. check_duty(0, 0);
  89. check_duty(24, 100);
  90. check_duty(28, 100);
  91. check_duty(25, 20);
  92. $display("PASS: new hard epoch cancels in-flight Manual before output Bank apply");
  93. $finish;
  94. end
  95. endmodule