ptc_output_bank_tb.v 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. `timescale 1ns/1ps
  2. module ptc_output_bank_tb;
  3. reg clk;
  4. reg rst_n;
  5. reg pid_valid;
  6. reg [4:0] pid_channel;
  7. reg signed [17:0] pid_ctrl_q;
  8. reg pid_fault;
  9. reg global_enable;
  10. reg fault_latched;
  11. reg [3:0] fault_code;
  12. wire ptc_enable;
  13. wire [4:0] ptc_channel;
  14. wire [17:0] ptc_duty_or_level;
  15. wire ptc_force_off;
  16. wire [3:0] ptc_reason;
  17. wire [23:0] ptc_bank_enable;
  18. wire [23:0] ptc_bank_force_off;
  19. wire [24*18-1:0] ptc_bank_duty_or_level;
  20. wire [23:0] ptc_pwm_out;
  21. integer high_count;
  22. integer sample_idx;
  23. ptc_output_bank #(
  24. .PWM_PERIOD_TICKS(127),
  25. .PWM_COUNTER_WIDTH(7)
  26. ) dut (
  27. .clk(clk),
  28. .rst_n(rst_n),
  29. .pid_valid(pid_valid),
  30. .pid_channel(pid_channel),
  31. .pid_ctrl_q(pid_ctrl_q),
  32. .pid_fault(pid_fault),
  33. .global_enable(global_enable),
  34. .fault_latched(fault_latched),
  35. .fault_code(fault_code),
  36. .ptc_enable(ptc_enable),
  37. .ptc_channel(ptc_channel),
  38. .ptc_duty_or_level(ptc_duty_or_level),
  39. .ptc_force_off(ptc_force_off),
  40. .ptc_reason(ptc_reason),
  41. .ptc_bank_enable(ptc_bank_enable),
  42. .ptc_bank_force_off(ptc_bank_force_off),
  43. .ptc_bank_duty_or_level(ptc_bank_duty_or_level),
  44. .ptc_pwm_out(ptc_pwm_out)
  45. );
  46. initial begin
  47. clk = 1'b0;
  48. forever #5 clk = ~clk;
  49. end
  50. task pulse_pid;
  51. input [4:0] channel;
  52. input signed [17:0] ctrl;
  53. begin
  54. pid_channel = channel;
  55. pid_ctrl_q = ctrl;
  56. pid_valid = 1'b1;
  57. @(negedge clk);
  58. pid_valid = 1'b0;
  59. @(negedge clk);
  60. end
  61. endtask
  62. task wait_cycles;
  63. input integer cycles;
  64. integer n;
  65. begin
  66. for (n = 0; n < cycles; n = n + 1) begin
  67. @(negedge clk);
  68. end
  69. end
  70. endtask
  71. task wait_pwm_rollover;
  72. begin
  73. while (dut.pwm_counter != 0) begin
  74. @(negedge clk);
  75. end
  76. end
  77. endtask
  78. initial begin
  79. rst_n = 1'b0;
  80. pid_valid = 1'b0;
  81. pid_channel = 5'd0;
  82. pid_ctrl_q = 18'sd0;
  83. pid_fault = 1'b0;
  84. global_enable = 1'b1;
  85. fault_latched = 1'b0;
  86. fault_code = 4'd0;
  87. repeat (4) @(negedge clk);
  88. rst_n = 1'b1;
  89. if (ptc_pwm_out !== 24'd0) begin
  90. $display("ptc pwm outputs must reset off");
  91. $fatal;
  92. end
  93. pulse_pid(5'd5, 18'sd200);
  94. if (!ptc_enable || ptc_duty_or_level !== 18'd127 || !ptc_bank_enable[5] ||
  95. ptc_bank_duty_or_level[(5*18) +: 18] !== 18'd127) begin
  96. $display("ptc output bank should clamp and store channel duty");
  97. $fatal;
  98. end
  99. if (!ptc_pwm_out[5]) begin
  100. $display("ptc pwm at 100 percent should request external switch on");
  101. $fatal;
  102. end
  103. pulse_pid(5'd6, 18'sd64);
  104. wait_pwm_rollover();
  105. high_count = 0;
  106. for (sample_idx = 0; sample_idx < 127; sample_idx = sample_idx + 1) begin
  107. if (ptc_pwm_out[6]) begin
  108. high_count = high_count + 1;
  109. end
  110. @(negedge clk);
  111. end
  112. if (high_count !== 64) begin
  113. $display("ptc pwm at duty 64 should have 64 high ticks per 127 tick period, got %0d", high_count);
  114. $fatal;
  115. end
  116. pid_fault = 1'b1;
  117. pulse_pid(5'd5, 18'sd80);
  118. if (!ptc_force_off || ptc_reason !== 4'd2 || ptc_bank_enable[5] || !ptc_bank_force_off[5] || ptc_pwm_out[5]) begin
  119. $display("ptc output bank should force off channel on pid fault");
  120. $fatal;
  121. end
  122. pid_fault = 1'b0;
  123. fault_latched = 1'b1;
  124. fault_code = 4'd5;
  125. @(negedge clk);
  126. if (!ptc_force_off || ptc_bank_enable !== 24'd0 || ptc_bank_force_off !== 24'hffffff || ptc_pwm_out !== 24'd0) begin
  127. $display("ptc output bank should force all PTC outputs off directly from central safety state");
  128. $fatal;
  129. end
  130. $display("PASS: ptc_output_bank_tb");
  131. $finish;
  132. end
  133. endmodule