actuator_domain_tb.v 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. `timescale 1ns/1ps
  2. module actuator_domain_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. reg motor_open_req;
  13. reg motor_close_req;
  14. reg button_open_req;
  15. reg button_close_req;
  16. reg limit_open;
  17. reg limit_close;
  18. reg [5:0] fan_tach_in;
  19. reg interlock;
  20. wire ptc_enable;
  21. wire [4:0] ptc_channel;
  22. wire [17:0] ptc_duty_or_level;
  23. wire ptc_force_off;
  24. wire [3:0] ptc_reason;
  25. wire fan_enable;
  26. wire [17:0] fan_duty_or_level;
  27. wire fan_force_off;
  28. wire [3:0] fan_reason;
  29. wire motor_enable;
  30. wire [1:0] motor_dir;
  31. wire motor_force_off;
  32. wire [3:0] motor_reason;
  33. wire [23:0] ptc_bank_enable;
  34. wire [23:0] ptc_bank_force_off;
  35. wire [24*18-1:0] ptc_bank_duty_or_level;
  36. wire [23:0] ptc_pwm_out;
  37. wire [5:0] fan_pwm_enable;
  38. wire [5:0] fan_pwm_logic_high;
  39. wire [5:0] fan_pwm_drive_low;
  40. wire [6*16-1:0] fan_tach_count;
  41. wire [6*16-1:0] fan_tach_window_edges;
  42. wire [6*16-1:0] fan_rpm_estimate;
  43. wire [1:0] motor_state;
  44. wire [1:0] limit_status;
  45. wire [1:0] button_status;
  46. actuator_domain dut (
  47. .clk(clk),
  48. .rst_n(rst_n),
  49. .pid_valid(pid_valid),
  50. .pid_channel(pid_channel),
  51. .pid_ctrl_q(pid_ctrl_q),
  52. .pid_fault(pid_fault),
  53. .global_enable(global_enable),
  54. .fault_latched(fault_latched),
  55. .fault_code(fault_code),
  56. .motor_open_req(motor_open_req),
  57. .motor_close_req(motor_close_req),
  58. .button_open_req(button_open_req),
  59. .button_close_req(button_close_req),
  60. .limit_open(limit_open),
  61. .limit_close(limit_close),
  62. .fan_tach_in(fan_tach_in),
  63. .interlock(interlock),
  64. .ptc_enable(ptc_enable),
  65. .ptc_channel(ptc_channel),
  66. .ptc_duty_or_level(ptc_duty_or_level),
  67. .ptc_force_off(ptc_force_off),
  68. .ptc_reason(ptc_reason),
  69. .fan_enable(fan_enable),
  70. .fan_duty_or_level(fan_duty_or_level),
  71. .fan_force_off(fan_force_off),
  72. .fan_reason(fan_reason),
  73. .motor_enable(motor_enable),
  74. .motor_dir(motor_dir),
  75. .motor_force_off(motor_force_off),
  76. .motor_reason(motor_reason),
  77. .ptc_bank_enable(ptc_bank_enable),
  78. .ptc_bank_force_off(ptc_bank_force_off),
  79. .ptc_bank_duty_or_level(ptc_bank_duty_or_level),
  80. .ptc_pwm_out(ptc_pwm_out),
  81. .fan_pwm_enable(fan_pwm_enable),
  82. .fan_pwm_logic_high(fan_pwm_logic_high),
  83. .fan_pwm_drive_low(fan_pwm_drive_low),
  84. .fan_tach_count(fan_tach_count),
  85. .fan_tach_window_edges(fan_tach_window_edges),
  86. .fan_rpm_estimate(fan_rpm_estimate),
  87. .motor_state(motor_state),
  88. .limit_status(limit_status),
  89. .button_status(button_status)
  90. );
  91. initial begin
  92. clk = 1'b0;
  93. forever #5 clk = ~clk;
  94. end
  95. task pulse_pid;
  96. input [4:0] channel;
  97. input signed [17:0] ctrl;
  98. begin
  99. pid_channel = channel;
  100. pid_ctrl_q = ctrl;
  101. pid_valid = 1'b1;
  102. @(negedge clk);
  103. pid_valid = 1'b0;
  104. @(negedge clk);
  105. end
  106. endtask
  107. initial begin
  108. rst_n = 1'b0;
  109. pid_valid = 1'b0;
  110. pid_channel = 5'd0;
  111. pid_ctrl_q = 18'sd0;
  112. pid_fault = 1'b0;
  113. global_enable = 1'b1;
  114. fault_latched = 1'b0;
  115. fault_code = 4'd0;
  116. motor_open_req = 1'b0;
  117. motor_close_req = 1'b0;
  118. button_open_req = 1'b0;
  119. button_close_req = 1'b0;
  120. limit_open = 1'b0;
  121. limit_close = 1'b0;
  122. fan_tach_in = 6'd0;
  123. interlock = 1'b0;
  124. repeat (4) @(negedge clk);
  125. rst_n = 1'b1;
  126. pulse_pid(5'd2, 18'sd90);
  127. if (!ptc_enable || ptc_force_off || ptc_duty_or_level !== 18'd90 || ptc_reason !== 4'd0 ||
  128. !ptc_bank_enable[2] || ptc_bank_force_off[2] || ptc_bank_duty_or_level[(2*18) +: 18] !== 18'd90 ||
  129. !ptc_pwm_out[2]) begin
  130. $display("normal PTC route mismatch");
  131. $fatal;
  132. end
  133. if (!fan_enable || fan_duty_or_level !== 18'd38 || !fan_pwm_logic_high[0] || fan_pwm_drive_low[0]) begin
  134. $display("safe actuator domain should drive fixed low fan pwm");
  135. $fatal;
  136. end
  137. pulse_pid(5'd24, 18'sd90);
  138. if (!ptc_force_off || ptc_reason !== 4'd6) begin
  139. $display("invalid channel should force off PTC");
  140. $fatal;
  141. end
  142. fault_latched = 1'b1;
  143. fault_code = 4'd5;
  144. pulse_pid(5'd1, 18'sd80);
  145. if (!ptc_force_off || ptc_reason !== 4'd5) begin
  146. $display("fault should force off PTC");
  147. $fatal;
  148. end
  149. if (!fan_enable || fan_force_off || fan_duty_or_level !== 18'd127 || fan_reason !== 4'd5) begin
  150. $display("overtemp should drive fan fallback");
  151. $fatal;
  152. end
  153. if (fan_pwm_enable !== 6'h3f || !fan_pwm_logic_high[0] || fan_pwm_drive_low[0]) begin
  154. $display("overtemp fan fallback should enable all fan pwm outputs");
  155. $fatal;
  156. end
  157. if (ptc_pwm_out !== 24'd0) begin
  158. $display("overtemp should force all ptc pwm outputs off");
  159. $fatal;
  160. end
  161. fault_latched = 1'b0;
  162. fault_code = 4'd0;
  163. global_enable = 1'b1;
  164. motor_open_req = 1'b1;
  165. @(negedge clk);
  166. if (!motor_enable || motor_dir !== 2'd1 || motor_force_off || motor_state !== 2'd1) begin
  167. $display("motor should open when safe and not at limit");
  168. $fatal;
  169. end
  170. limit_open = 1'b1;
  171. @(negedge clk);
  172. if (motor_enable || motor_dir !== 2'd0) begin
  173. $display("motor should stop at open limit");
  174. $fatal;
  175. end
  176. fault_latched = 1'b1;
  177. fault_code = 4'd7;
  178. interlock = 1'b1;
  179. @(negedge clk);
  180. if (!motor_force_off || motor_reason !== 4'd7) begin
  181. $display("interlock should force off motor");
  182. $fatal;
  183. end
  184. $display("PASS: actuator_domain_tb");
  185. $finish;
  186. end
  187. endmodule