`timescale 1ns/1ps module pwm_phase_scan_scheduler_tb; reg clk = 1'b0; reg rst_n = 1'b0; wire [4:0] target_rd_addr; reg [7:0] target_rd_data = 8'd0; wire [31:0] logical_pwm_on; wire scan_idle; reg [7:0] duties [0:31]; integer ch; integer high_count; integer period; always #1 clk = ~clk; always @(posedge clk) begin if (!rst_n) target_rd_data <= 8'd0; else target_rd_data <= duties[target_rd_addr]; end pwm_phase_scan_scheduler #( .CHANNEL_COUNT(32), .PHASE_COUNT(100), .PHASE_CLKS(500) ) dut ( .clk(clk), .rst_n(rst_n), .target_rd_addr(target_rd_addr), .target_rd_data(target_rd_data), .logical_pwm_on(logical_pwm_on), .scan_idle(scan_idle) ); initial begin for (ch = 0; ch < 32; ch = ch + 1) duties[ch] = 8'd0; duties[0] = 8'd0; duties[1] = 8'd1; duties[2] = 8'd50; duties[3] = 8'd99; duties[4] = 8'd100; duties[24] = 8'd10; repeat (2) @(posedge clk); rst_n = 1'b1; // Wait first scan, then measure two exact 50,000-clock periods. repeat (40) @(posedge clk); for (period = 0; period < 2; period = period + 1) begin high_count = 0; repeat (50000) begin @(posedge clk); if (logical_pwm_on[0] !== 1'b0) $fatal(1, "duty 0 went high"); if (logical_pwm_on[4] !== 1'b1) $fatal(1, "duty 100 went low"); if (logical_pwm_on[1]) high_count = high_count + 1; end if (high_count !== 500) $fatal(1, "duty 1 high_count=%0d period=%0d", high_count, period); end high_count = 0; repeat (50000) begin @(posedge clk); if (logical_pwm_on[2]) high_count = high_count + 1; end if (high_count !== 25000) $fatal(1, "duty 50 high_count=%0d", high_count); high_count = 0; repeat (50000) begin @(posedge clk); if (logical_pwm_on[3]) high_count = high_count + 1; end if (high_count !== 49500) $fatal(1, "duty 99 high_count=%0d", high_count); if (target_rd_addr > 5'd31) $fatal(1, "address overflow"); $display("pwm_phase_scan_scheduler_tb PASS"); $finish; end endmodule