pwm_phase_scan_scheduler_tb.v 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. `timescale 1ns/1ps
  2. module pwm_phase_scan_scheduler_tb;
  3. reg clk = 1'b0;
  4. reg rst_n = 1'b0;
  5. wire [4:0] target_rd_addr;
  6. reg [7:0] target_rd_data = 8'd0;
  7. wire [31:0] logical_pwm_on;
  8. wire scan_idle;
  9. reg [7:0] duties [0:31];
  10. integer ch;
  11. integer high_count;
  12. integer period;
  13. always #1 clk = ~clk;
  14. always @(posedge clk) begin
  15. if (!rst_n)
  16. target_rd_data <= 8'd0;
  17. else
  18. target_rd_data <= duties[target_rd_addr];
  19. end
  20. pwm_phase_scan_scheduler #(
  21. .CHANNEL_COUNT(32), .PHASE_COUNT(100), .PHASE_CLKS(500)
  22. ) dut (
  23. .clk(clk), .rst_n(rst_n),
  24. .target_rd_addr(target_rd_addr), .target_rd_data(target_rd_data),
  25. .logical_pwm_on(logical_pwm_on), .scan_idle(scan_idle)
  26. );
  27. initial begin
  28. for (ch = 0; ch < 32; ch = ch + 1) duties[ch] = 8'd0;
  29. duties[0] = 8'd0;
  30. duties[1] = 8'd1;
  31. duties[2] = 8'd50;
  32. duties[3] = 8'd99;
  33. duties[4] = 8'd100;
  34. duties[24] = 8'd10;
  35. repeat (2) @(posedge clk);
  36. rst_n = 1'b1;
  37. // Wait first scan, then measure two exact 50,000-clock periods.
  38. repeat (40) @(posedge clk);
  39. for (period = 0; period < 2; period = period + 1) begin
  40. high_count = 0;
  41. repeat (50000) begin
  42. @(posedge clk);
  43. if (logical_pwm_on[0] !== 1'b0)
  44. $fatal(1, "duty 0 went high");
  45. if (logical_pwm_on[4] !== 1'b1)
  46. $fatal(1, "duty 100 went low");
  47. if (logical_pwm_on[1]) high_count = high_count + 1;
  48. end
  49. if (high_count !== 500)
  50. $fatal(1, "duty 1 high_count=%0d period=%0d", high_count, period);
  51. end
  52. high_count = 0;
  53. repeat (50000) begin
  54. @(posedge clk);
  55. if (logical_pwm_on[2]) high_count = high_count + 1;
  56. end
  57. if (high_count !== 25000)
  58. $fatal(1, "duty 50 high_count=%0d", high_count);
  59. high_count = 0;
  60. repeat (50000) begin
  61. @(posedge clk);
  62. if (logical_pwm_on[3]) high_count = high_count + 1;
  63. end
  64. if (high_count !== 49500)
  65. $fatal(1, "duty 99 high_count=%0d", high_count);
  66. if (target_rd_addr > 5'd31)
  67. $fatal(1, "address overflow");
  68. $display("pwm_phase_scan_scheduler_tb PASS");
  69. $finish;
  70. end
  71. endmodule