`timescale 1ns/1ps module mrdimm_pwm_commit_race_tb; reg clk = 0; always #20 clk = ~clk; // 25 MHz reg rst_n = 0; reg tick_1s = 0; reg wr_en = 0; reg [2:0] wr_addr = 0; reg [7:0] wr_data = 0; reg commit = 0; wire commit_done; wire commit_ok; reg keepalive_reload = 0; wire [31:0] applied_fan, applied_ptc; wire [31:0] requested_fan, requested_ptc; wire [3:0] manual_active; wire lease_active; wire [7:0] watchdog_remaining; wire [3:0] fan_pwm_on; wire [3:0] ptc_pwm_on; mrdimm_output_core #( .BOOT_FAN_PERCENT(8'd0), .BOOT_PTC_PERCENT(8'd0), .PWM_PERIOD_CLKS(2000), .PWM_CLKS_PER_PERCENT(20) ) dut ( .clk(clk), .rst_n(rst_n), .tick_1s(tick_1s), .wr_en(wr_en), .wr_addr(wr_addr), .wr_data(wr_data), .commit(commit), .commit_done(commit_done), .commit_ok(commit_ok), .keepalive_reload(keepalive_reload), .applied_fan(applied_fan), .applied_ptc(applied_ptc), .requested_fan(requested_fan), .requested_ptc(requested_ptc), .manual_active(manual_active), .lease_active(lease_active), .watchdog_remaining(watchdog_remaining), .fan_pwm_on(fan_pwm_on), .ptc_pwm_on(ptc_pwm_on) ); wire [7:0] combined_bitmap = {ptc_pwm_on, fan_pwm_on}; integer sweep_offset; integer mixed_generation_errors = 0; reg [7:0] captured_bitmap; initial begin $display("=== Starting MRDIMM PWM Commit Race Reproduction ==="); // Sweep commit arrival across all 500 clocks of a PWM phase for (sweep_offset = 0; sweep_offset < 50; sweep_offset = sweep_offset + 1) begin // Reset DUT rst_n = 0; commit = 0; wr_en = 0; repeat (5) @(posedge clk); rst_n = 1; repeat (20) @(posedge clk); // Pre-stage all 8 channels to 100% before the target phase starts for (int ch = 0; ch < 8; ch = ch + 1) begin wr_en = 1; wr_addr = ch; wr_data = 8'd100; @(posedge clk); end wr_en = 0; // Wait until phase boundary (phase_clk == 0) while (dut.u_pwm.phase_clk != 0) @(posedge clk); // Advance to the exact target clock offset within the scan window repeat (sweep_offset) @(posedge clk); // Commit at the exact clock offset commit = 1; @(posedge clk); commit = 0; // Wait until commit_done while (!commit_done) @(posedge clk); // Check bitmap over the next 2 full phases (1000 clocks) repeat (1000) begin @(posedge clk); captured_bitmap = combined_bitmap; // Duty was 0 (bitmap 8'h00) -> 100 (bitmap 8'hFF) // A valid bitmap MUST be either 8'h00 or 8'hFF! // Any intermediate value (like 8'hE0, 8'hF8, 8'h0F) indicates channel mixing! if (captured_bitmap != 8'h00 && captured_bitmap != 8'hFF) begin mixed_generation_errors = mixed_generation_errors + 1; $display("RACE DETECTED at sweep offset %0d: mixed bitmap = 0x%02X at phase %0d, phase_clk %0d", sweep_offset, captured_bitmap, dut.u_pwm.phase, dut.u_pwm.phase_clk); end end end if (mixed_generation_errors > 0) begin $display("REPRODUCTION CONFIRMED: Found %0d mixed generation bitmap events!", mixed_generation_errors); end else begin $display("CLEAN: No mixed generation bitmap events observed."); end $finish; end endmodule