pwm_scheduler_tb.v 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. `timescale 1ns/1ps
  2. module pwm_scheduler_tb;
  3. reg clk = 1'b0;
  4. reg rst_n = 1'b0;
  5. reg target_loaded = 1'b0;
  6. reg [1:0] target_rd_addr;
  7. wire [1:0] target_rd_data;
  8. wire [7:0] ram_rd_data;
  9. reg [1:0] target_wr_addr = 2'd0;
  10. reg [7:0] target_wr_data = 8'd0;
  11. reg target_wr_en = 1'b0;
  12. wire [1:0] ptc_pwm_out;
  13. wire fan_pwm_out;
  14. integer event_index;
  15. integer expected_phase;
  16. integer connector_high_count;
  17. always #1 clk = ~clk;
  18. output_target_ram #(.DEPTH(3), .ADDR_WIDTH(2)) ram (
  19. .clk(clk), .wr_en(target_wr_en), .wr_addr(target_wr_addr), .wr_data(target_wr_data),
  20. .rd_addr(target_rd_addr), .rd_data(ram_rd_data)
  21. );
  22. assign target_rd_data = ram_rd_data[1:0];
  23. pwm_scheduler #(
  24. .PTC_COUNT(2), .FAN_COUNT(1), .PWM_BITS(2), .PWM_SLOT_CLKS(2),
  25. .PTC_DEFAULT_TARGET(8'h00), .FAN_DEFAULT_TARGET(8'h02),
  26. .PTC_ACTIVE_HIGH(1), .FAN_ACTIVE_HIGH(0)
  27. ) dut (
  28. .clk(clk), .rst_n(rst_n), .target_loaded(target_loaded),
  29. .target_rd_addr(target_rd_addr), .target_rd_data(target_rd_data),
  30. .ptc_pwm_out(ptc_pwm_out), .fan_pwm_out(fan_pwm_out)
  31. );
  32. task write_target;
  33. input [1:0] address;
  34. input [7:0] value;
  35. begin
  36. @(negedge clk);
  37. target_wr_addr = address;
  38. target_wr_data = value;
  39. target_wr_en = 1'b1;
  40. @(negedge clk);
  41. target_wr_en = 1'b0;
  42. end
  43. endtask
  44. initial begin
  45. #0.1;
  46. rst_n = 1'b1;
  47. #0.1;
  48. rst_n = 1'b0;
  49. #0.1;
  50. if (ptc_pwm_out !== 2'b00) $fatal(1, "PTC reset output is active");
  51. if (fan_pwm_out !== 1'b1) $fatal(1, "FAN reset output is not inactive");
  52. repeat (2) @(posedge clk);
  53. rst_n = 1'b1;
  54. repeat (2) @(posedge clk);
  55. if (dut.current_channel !== 2'd1) $fatal(1, "channel did not advance at slot boundary");
  56. @(posedge clk);
  57. if (dut.current_channel !== 2'd1) $fatal(1, "channel advanced before slot completed");
  58. @(posedge clk);
  59. if (dut.current_channel !== 2'd2) $fatal(1, "channel did not advance after second slot");
  60. repeat (2) @(posedge clk);
  61. if (dut.current_channel !== 2'd0) $fatal(1, "last channel did not wrap");
  62. if (dut.phase !== 2'd1) $fatal(1, "phase did not increment exactly once");
  63. rst_n = 1'b0;
  64. @(posedge clk);
  65. if (ptc_pwm_out !== 2'b00 || fan_pwm_out !== 1'b1) $fatal(1, "reset polarity mismatch");
  66. rst_n = 1'b1;
  67. connector_high_count = 0;
  68. for (event_index = 0; event_index < 12; event_index = event_index + 1) begin
  69. repeat (2) @(posedge clk);
  70. @(negedge clk);
  71. if (event_index % 3 == 2) begin
  72. expected_phase = event_index / 3;
  73. if ((!fan_pwm_out) !== (2 > expected_phase)) begin
  74. $fatal(1, "default FAN connector polarity/duty mismatch phase=%0d out=%b", expected_phase, fan_pwm_out);
  75. end
  76. if (!fan_pwm_out) connector_high_count = connector_high_count + 1;
  77. end
  78. if (ptc_pwm_out !== 2'b00) $fatal(1, "default PTC is not continuously off");
  79. end
  80. if (connector_high_count !== 2) $fatal(1, "default FAN connector duty mismatch: %0d", connector_high_count);
  81. write_target(0, 8'h03);
  82. write_target(1, 8'h00);
  83. write_target(2, 8'h00);
  84. target_loaded = 1'b1;
  85. repeat (2) @(posedge clk);
  86. @(negedge clk);
  87. if (ptc_pwm_out[0] !== 1'b1) $fatal(1, "loaded PTC target was not used");
  88. repeat (4) @(posedge clk);
  89. @(negedge clk);
  90. if (ptc_pwm_out[1] !== 1'b0) $fatal(1, "loaded PTC zero target was not used");
  91. repeat (2) @(posedge clk);
  92. @(negedge clk);
  93. if (fan_pwm_out !== 1'b1) $fatal(1, "loaded FAN zero target polarity mismatch");
  94. $display("pwm_scheduler_tb PASS");
  95. $finish;
  96. end
  97. initial begin
  98. #2000;
  99. $fatal(1, "PWM timeout channel=%0d phase=%0d slot=%0d", dut.current_channel, dut.phase, dut.slot_counter);
  100. end
  101. endmodule