mrdimm_pwm_commit_race_tb.v 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. `timescale 1ns/1ps
  2. module mrdimm_pwm_commit_race_tb;
  3. reg clk = 0;
  4. always #20 clk = ~clk; // 25 MHz
  5. reg rst_n = 0;
  6. reg tick_1s = 0;
  7. reg wr_en = 0;
  8. reg [2:0] wr_addr = 0;
  9. reg [7:0] wr_data = 0;
  10. reg commit = 0;
  11. wire commit_done;
  12. wire commit_ok;
  13. reg keepalive_reload = 0;
  14. wire [31:0] applied_fan, applied_ptc;
  15. wire [31:0] requested_fan, requested_ptc;
  16. wire [3:0] manual_active;
  17. wire lease_active;
  18. wire [7:0] watchdog_remaining;
  19. wire [3:0] fan_pwm_on;
  20. wire [3:0] ptc_pwm_on;
  21. mrdimm_output_core #(
  22. .BOOT_FAN_PERCENT(8'd0),
  23. .BOOT_PTC_PERCENT(8'd0),
  24. .PWM_PERIOD_CLKS(2000),
  25. .PWM_CLKS_PER_PERCENT(20)
  26. ) dut (
  27. .clk(clk),
  28. .rst_n(rst_n),
  29. .tick_1s(tick_1s),
  30. .wr_en(wr_en),
  31. .wr_addr(wr_addr),
  32. .wr_data(wr_data),
  33. .commit(commit),
  34. .commit_done(commit_done),
  35. .commit_ok(commit_ok),
  36. .keepalive_reload(keepalive_reload),
  37. .applied_fan(applied_fan),
  38. .applied_ptc(applied_ptc),
  39. .requested_fan(requested_fan),
  40. .requested_ptc(requested_ptc),
  41. .manual_active(manual_active),
  42. .lease_active(lease_active),
  43. .watchdog_remaining(watchdog_remaining),
  44. .fan_pwm_on(fan_pwm_on),
  45. .ptc_pwm_on(ptc_pwm_on)
  46. );
  47. wire [7:0] combined_bitmap = {ptc_pwm_on, fan_pwm_on};
  48. integer sweep_offset;
  49. integer mixed_generation_errors = 0;
  50. reg [7:0] captured_bitmap;
  51. initial begin
  52. $display("=== Starting MRDIMM PWM Commit Race Reproduction ===");
  53. // Sweep commit arrival across all 500 clocks of a PWM phase
  54. for (sweep_offset = 0; sweep_offset < 50; sweep_offset = sweep_offset + 1) begin
  55. // Reset DUT
  56. rst_n = 0;
  57. commit = 0;
  58. wr_en = 0;
  59. repeat (5) @(posedge clk);
  60. rst_n = 1;
  61. repeat (20) @(posedge clk);
  62. // Pre-stage all 8 channels to 100% before the target phase starts
  63. for (int ch = 0; ch < 8; ch = ch + 1) begin
  64. wr_en = 1; wr_addr = ch; wr_data = 8'd100;
  65. @(posedge clk);
  66. end
  67. wr_en = 0;
  68. // Wait until phase boundary (phase_clk == 0)
  69. while (dut.u_pwm.phase_clk != 0) @(posedge clk);
  70. // Advance to the exact target clock offset within the scan window
  71. repeat (sweep_offset) @(posedge clk);
  72. // Commit at the exact clock offset
  73. commit = 1;
  74. @(posedge clk);
  75. commit = 0;
  76. // Wait until commit_done
  77. while (!commit_done) @(posedge clk);
  78. // Check bitmap over the next 2 full phases (1000 clocks)
  79. repeat (1000) begin
  80. @(posedge clk);
  81. captured_bitmap = combined_bitmap;
  82. // Duty was 0 (bitmap 8'h00) -> 100 (bitmap 8'hFF)
  83. // A valid bitmap MUST be either 8'h00 or 8'hFF!
  84. // Any intermediate value (like 8'hE0, 8'hF8, 8'h0F) indicates channel mixing!
  85. if (captured_bitmap != 8'h00 && captured_bitmap != 8'hFF) begin
  86. mixed_generation_errors = mixed_generation_errors + 1;
  87. $display("RACE DETECTED at sweep offset %0d: mixed bitmap = 0x%02X at phase %0d, phase_clk %0d",
  88. sweep_offset, captured_bitmap, dut.u_pwm.phase, dut.u_pwm.phase_clk);
  89. end
  90. end
  91. end
  92. if (mixed_generation_errors > 0) begin
  93. $display("REPRODUCTION CONFIRMED: Found %0d mixed generation bitmap events!", mixed_generation_errors);
  94. end else begin
  95. $display("CLEAN: No mixed generation bitmap events observed.");
  96. end
  97. $finish;
  98. end
  99. endmodule