`timescale 1ns/1ps module pwm_scheduler_tb; reg clk = 1'b0; reg rst_n = 1'b0; reg target_loaded = 1'b0; reg [1:0] target_rd_addr; wire [1:0] target_rd_data; wire [7:0] ram_rd_data; reg [1:0] target_wr_addr = 2'd0; reg [7:0] target_wr_data = 8'd0; reg target_wr_en = 1'b0; wire [1:0] ptc_pwm_out; wire fan_pwm_out; integer event_index; integer expected_phase; integer connector_high_count; always #1 clk = ~clk; output_target_ram #(.DEPTH(3), .ADDR_WIDTH(2)) ram ( .clk(clk), .wr_en(target_wr_en), .wr_addr(target_wr_addr), .wr_data(target_wr_data), .rd_addr(target_rd_addr), .rd_data(ram_rd_data) ); assign target_rd_data = ram_rd_data[1:0]; pwm_scheduler #( .PTC_COUNT(2), .FAN_COUNT(1), .PWM_BITS(2), .PWM_SLOT_CLKS(2), .PTC_DEFAULT_TARGET(8'h00), .FAN_DEFAULT_TARGET(8'h02), .PTC_ACTIVE_HIGH(1), .FAN_ACTIVE_HIGH(0) ) dut ( .clk(clk), .rst_n(rst_n), .target_loaded(target_loaded), .target_rd_addr(target_rd_addr), .target_rd_data(target_rd_data), .ptc_pwm_out(ptc_pwm_out), .fan_pwm_out(fan_pwm_out) ); task write_target; input [1:0] address; input [7:0] value; begin @(negedge clk); target_wr_addr = address; target_wr_data = value; target_wr_en = 1'b1; @(negedge clk); target_wr_en = 1'b0; end endtask initial begin #0.1; rst_n = 1'b1; #0.1; rst_n = 1'b0; #0.1; if (ptc_pwm_out !== 2'b00) $fatal(1, "PTC reset output is active"); if (fan_pwm_out !== 1'b1) $fatal(1, "FAN reset output is not inactive"); repeat (2) @(posedge clk); rst_n = 1'b1; repeat (2) @(posedge clk); if (dut.current_channel !== 2'd1) $fatal(1, "channel did not advance at slot boundary"); @(posedge clk); if (dut.current_channel !== 2'd1) $fatal(1, "channel advanced before slot completed"); @(posedge clk); if (dut.current_channel !== 2'd2) $fatal(1, "channel did not advance after second slot"); repeat (2) @(posedge clk); if (dut.current_channel !== 2'd0) $fatal(1, "last channel did not wrap"); if (dut.phase !== 2'd1) $fatal(1, "phase did not increment exactly once"); rst_n = 1'b0; @(posedge clk); if (ptc_pwm_out !== 2'b00 || fan_pwm_out !== 1'b1) $fatal(1, "reset polarity mismatch"); rst_n = 1'b1; connector_high_count = 0; for (event_index = 0; event_index < 12; event_index = event_index + 1) begin repeat (2) @(posedge clk); @(negedge clk); if (event_index % 3 == 2) begin expected_phase = event_index / 3; if ((!fan_pwm_out) !== (2 > expected_phase)) begin $fatal(1, "default FAN connector polarity/duty mismatch phase=%0d out=%b", expected_phase, fan_pwm_out); end if (!fan_pwm_out) connector_high_count = connector_high_count + 1; end if (ptc_pwm_out !== 2'b00) $fatal(1, "default PTC is not continuously off"); end if (connector_high_count !== 2) $fatal(1, "default FAN connector duty mismatch: %0d", connector_high_count); write_target(0, 8'h03); write_target(1, 8'h00); write_target(2, 8'h00); target_loaded = 1'b1; repeat (2) @(posedge clk); @(negedge clk); if (ptc_pwm_out[0] !== 1'b1) $fatal(1, "loaded PTC target was not used"); repeat (4) @(posedge clk); @(negedge clk); if (ptc_pwm_out[1] !== 1'b0) $fatal(1, "loaded PTC zero target was not used"); repeat (2) @(posedge clk); @(negedge clk); if (fan_pwm_out !== 1'b1) $fatal(1, "loaded FAN zero target polarity mismatch"); $display("pwm_scheduler_tb PASS"); $finish; end initial begin #2000; $fatal(1, "PWM timeout channel=%0d phase=%0d slot=%0d", dut.current_channel, dut.phase, dut.slot_counter); end endmodule