fan_tach_domain_tb.v 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. `timescale 1ns/1ps
  2. module fan_tach_domain_tb;
  3. reg clk;
  4. reg rst_n;
  5. reg global_enable;
  6. reg fault_latched;
  7. reg [3:0] fault_code;
  8. reg [5:0] fan_tach_in;
  9. wire fan_enable;
  10. wire [17:0] fan_duty_or_level;
  11. wire fan_force_off;
  12. wire [3:0] fan_reason;
  13. wire [5:0] fan_pwm_enable;
  14. wire [5:0] fan_pwm_logic_high;
  15. wire [5:0] fan_pwm_drive_low;
  16. wire [6*16-1:0] fan_tach_count;
  17. wire [6*16-1:0] fan_tach_window_edges;
  18. wire [6*16-1:0] fan_rpm_estimate;
  19. fan_tach_domain #(
  20. .PWM_PERIOD_TICKS(1000),
  21. .PWM_COUNTER_WIDTH(10),
  22. .TACH_WINDOW_TICKS(1000),
  23. .TACH_COUNTER_WIDTH(10)
  24. ) dut (
  25. .clk(clk),
  26. .rst_n(rst_n),
  27. .global_enable(global_enable),
  28. .fault_latched(fault_latched),
  29. .fault_code(fault_code),
  30. .fan_tach_in(fan_tach_in),
  31. .fan_enable(fan_enable),
  32. .fan_duty_or_level(fan_duty_or_level),
  33. .fan_force_off(fan_force_off),
  34. .fan_reason(fan_reason),
  35. .fan_pwm_enable(fan_pwm_enable),
  36. .fan_pwm_logic_high(fan_pwm_logic_high),
  37. .fan_pwm_drive_low(fan_pwm_drive_low),
  38. .fan_tach_count(fan_tach_count),
  39. .fan_tach_window_edges(fan_tach_window_edges),
  40. .fan_rpm_estimate(fan_rpm_estimate)
  41. );
  42. initial begin
  43. clk = 1'b0;
  44. forever #5 clk = ~clk;
  45. end
  46. task wait_cycles;
  47. input integer cycles;
  48. integer n;
  49. begin
  50. for (n = 0; n < cycles; n = n + 1) begin
  51. @(negedge clk);
  52. end
  53. end
  54. endtask
  55. initial begin
  56. rst_n = 1'b0;
  57. global_enable = 1'b1;
  58. fault_latched = 1'b0;
  59. fault_code = 4'd0;
  60. fan_tach_in = 6'd0;
  61. repeat (4) @(negedge clk);
  62. rst_n = 1'b1;
  63. fan_tach_in[0] = 1'b1;
  64. @(negedge clk);
  65. fan_tach_in[0] = 1'b0;
  66. @(negedge clk);
  67. fan_tach_in[0] = 1'b1;
  68. @(negedge clk);
  69. if (fan_tach_count[15:0] !== 16'd2) begin
  70. $display("fan tach domain should count rising tach edges");
  71. $fatal;
  72. end
  73. if (!fan_enable || fan_force_off || fan_duty_or_level !== 18'd38 || fan_pwm_enable !== 6'h3f) begin
  74. $display("fan tach domain should run fixed low background duty when safe");
  75. $fatal;
  76. end
  77. if (!fan_pwm_logic_high[0] || fan_pwm_drive_low[0]) begin
  78. $display("fan pwm should start high/high-z for normal 4-wire pwm");
  79. $fatal;
  80. end
  81. wait_cycles(299);
  82. if (fan_pwm_logic_high[0] || !fan_pwm_drive_low[0]) begin
  83. $display("fan pwm normal duty should pull low after 30 percent high-time");
  84. $fatal;
  85. end
  86. wait_cycles(701);
  87. if (!fan_pwm_logic_high[0] || fan_pwm_drive_low[0]) begin
  88. $display("fan pwm should restart high at 25 kHz period");
  89. $fatal;
  90. end
  91. fault_latched = 1'b1;
  92. fault_code = 4'd5;
  93. @(negedge clk);
  94. if (!fan_enable || fan_force_off || fan_duty_or_level !== 18'd127 ||
  95. fan_pwm_enable !== 6'h3f || fan_reason !== 4'd5) begin
  96. $display("fan tach domain should enable fallback on overtemp");
  97. $fatal;
  98. end
  99. wait_cycles(1000);
  100. if (!fan_pwm_logic_high[0] || fan_pwm_drive_low[0]) begin
  101. $display("fan fallback 100 percent should stay high/high-z for full speed request");
  102. $fatal;
  103. end
  104. repeat (100) begin
  105. fan_tach_in[1] = 1'b1;
  106. @(negedge clk);
  107. fan_tach_in[1] = 1'b0;
  108. @(negedge clk);
  109. end
  110. wait_cycles(1000);
  111. if (fan_tach_window_edges[(1*16) +: 16] !== 16'd100 || fan_rpm_estimate[(1*16) +: 16] !== 16'd3000) begin
  112. $display("fan tach window should convert 100 edges per second to 3000 rpm");
  113. $fatal;
  114. end
  115. fault_latched = 1'b0;
  116. global_enable = 1'b0;
  117. fault_code = 4'd1;
  118. @(negedge clk);
  119. if (!fan_enable || fan_force_off || fan_duty_or_level !== 18'd38 || fan_pwm_enable !== 6'h3f) begin
  120. $display("fan tach domain should keep background duty on host disable");
  121. $fatal;
  122. end
  123. global_enable = 1'b1;
  124. fault_latched = 1'b1;
  125. fault_code = 4'd7;
  126. @(negedge clk);
  127. if (!fan_enable || fan_force_off || fan_duty_or_level !== 18'd38 || fan_pwm_enable !== 6'h3f) begin
  128. $display("fan tach domain should not stop fan for motor interlock");
  129. $fatal;
  130. end
  131. $display("PASS: fan_tach_domain_tb");
  132. $finish;
  133. end
  134. endmodule