prod2u_p4_pwm_outputs_tb.v 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. `timescale 1ns/1ps
  2. module prod2u_p4_pwm_outputs_tb;
  3. reg clk;
  4. reg rst_n;
  5. reg [191:0] ptc_percent;
  6. reg [31:0] fan_percent;
  7. reg [7:0] solenoid_percent;
  8. wire [23:0] ptc_pwm_out;
  9. wire [3:0] fan_pwm_out;
  10. wire solenoid_on;
  11. integer cycle;
  12. integer ptc_high;
  13. integer fan_raw_high;
  14. prod2u_p4_pwm_outputs #(
  15. .PTC_STEP_CLKS(1),
  16. .FAN_STEP_CLKS(1)
  17. ) dut (
  18. .clk(clk),
  19. .rst_n(rst_n),
  20. .ptc_percent(ptc_percent),
  21. .fan_percent(fan_percent),
  22. .solenoid_percent(solenoid_percent),
  23. .ptc_pwm_out(ptc_pwm_out),
  24. .fan_pwm_out(fan_pwm_out),
  25. .solenoid_on(solenoid_on)
  26. );
  27. initial begin
  28. clk = 1'b0;
  29. forever #5 clk = ~clk;
  30. end
  31. initial begin
  32. rst_n = 1'b0;
  33. ptc_percent = 192'd0;
  34. fan_percent = {8'd100, 8'd0, 8'd70, 8'd30};
  35. ptc_percent[7:0] = 8'd10;
  36. ptc_percent[15:8] = 8'd100;
  37. solenoid_percent = 8'd0;
  38. repeat (3) @(negedge clk);
  39. rst_n = 1'b1;
  40. ptc_high = 0;
  41. fan_raw_high = 0;
  42. for (cycle = 0; cycle < 100; cycle = cycle + 1) begin
  43. @(negedge clk);
  44. if (ptc_pwm_out[0]) ptc_high = ptc_high + 1;
  45. if (fan_pwm_out[0]) fan_raw_high = fan_raw_high + 1;
  46. if (ptc_pwm_out[1] !== 1'b1 || ptc_pwm_out[2] !== 1'b0 ||
  47. fan_pwm_out[1] !== (dut.fan_phase < 7'd30) ||
  48. fan_pwm_out[2] !== 1'b1 || fan_pwm_out[3] !== 1'b0) begin
  49. $display("PWM edge semantics mismatch cycle=%0d", cycle);
  50. $fatal;
  51. end
  52. end
  53. if (ptc_high !== 10 || fan_raw_high !== 70) begin
  54. $display("PWM duty mismatch ptc=%0d fan_raw=%0d", ptc_high, fan_raw_high);
  55. $fatal;
  56. end
  57. solenoid_percent = 8'd100;
  58. @(negedge clk);
  59. if (!solenoid_on) begin
  60. $display("solenoid logical output did not turn on at 100");
  61. $fatal;
  62. end
  63. solenoid_percent = 8'd0;
  64. @(negedge clk);
  65. if (solenoid_on) begin
  66. $display("solenoid logical output did not turn off at 0");
  67. $fatal;
  68. end
  69. $display("PASS: prod2u_p4_pwm_outputs_tb");
  70. $finish;
  71. end
  72. endmodule