pid_contract_integration_tb.v 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. `timescale 1ns/1ps
  2. module pid_contract_integration_tb;
  3. reg clk;
  4. reg rst_n;
  5. reg sensor_valid;
  6. reg sensor_fault;
  7. reg [4:0] sensor_channel;
  8. reg signed [17:0] sensor_temp_q;
  9. reg [15:0] sensor_sample_seq;
  10. reg host_enable;
  11. reg signed [17:0] host_target_q;
  12. reg signed [17:0] host_a0;
  13. reg signed [17:0] host_a1;
  14. reg signed [17:0] host_a2;
  15. reg [1:0] host_mode;
  16. reg [15:0] host_config_seq;
  17. wire sensor_ready;
  18. wire pid_valid;
  19. wire [4:0] pid_channel;
  20. wire signed [17:0] pid_error_q;
  21. wire signed [17:0] pid_ctrl_q;
  22. wire pid_done;
  23. wire pid_fault;
  24. wire [15:0] pid_sample_seq;
  25. wire signed [17:0] pid_ctrl_prev_q;
  26. wire signed [17:0] pid_delta_q;
  27. wire pid_clamped;
  28. wire pid_history_cleared;
  29. wire [1:0] pid_state;
  30. wire [2:0] pid_cycle_phase;
  31. wire pid_busy;
  32. wire clear_history_all = 1'b0;
  33. wire act_enable;
  34. wire [4:0] act_channel;
  35. wire [17:0] act_duty_or_level;
  36. wire act_force_off;
  37. wire [1:0] act_reason;
  38. pid_24ch_scheduler #(
  39. .DW(18),
  40. .CHW(5),
  41. .CHANNEL_COUNT(24),
  42. .OUT_MIN(18'sd0),
  43. .OUT_MAX(18'sd127)
  44. ) u_pid_24ch_scheduler (
  45. .clk(clk),
  46. .rst_n(rst_n),
  47. .sensor_valid(sensor_valid),
  48. .sensor_ready(sensor_ready),
  49. .sensor_fault(sensor_fault),
  50. .sensor_channel(sensor_channel),
  51. .sensor_temp_q(sensor_temp_q),
  52. .sensor_sample_seq(sensor_sample_seq),
  53. .host_enable(host_enable),
  54. .host_target_q(host_target_q),
  55. .host_a0(host_a0),
  56. .host_a1(host_a1),
  57. .host_a2(host_a2),
  58. .host_mode(host_mode),
  59. .host_config_seq(host_config_seq),
  60. .clear_history_all(clear_history_all),
  61. .pid_valid(pid_valid),
  62. .pid_channel(pid_channel),
  63. .pid_error_q(pid_error_q),
  64. .pid_ctrl_q(pid_ctrl_q),
  65. .pid_done(pid_done),
  66. .pid_fault(pid_fault),
  67. .pid_sample_seq(pid_sample_seq),
  68. .pid_ctrl_prev_q(pid_ctrl_prev_q),
  69. .pid_delta_q(pid_delta_q),
  70. .pid_clamped(pid_clamped),
  71. .pid_history_cleared(pid_history_cleared),
  72. .pid_state(pid_state),
  73. .pid_cycle_phase(pid_cycle_phase),
  74. .busy(pid_busy)
  75. );
  76. actuator_sink u_actuator_sink (
  77. .clk(clk),
  78. .rst_n(rst_n),
  79. .host_enable(host_enable),
  80. .pid_valid(pid_valid),
  81. .pid_channel(pid_channel),
  82. .pid_ctrl_q(pid_ctrl_q),
  83. .pid_fault(pid_fault),
  84. .act_enable(act_enable),
  85. .act_channel(act_channel),
  86. .act_duty_or_level(act_duty_or_level),
  87. .act_force_off(act_force_off),
  88. .act_reason(act_reason)
  89. );
  90. initial begin
  91. clk = 1'b0;
  92. forever #5 clk = ~clk;
  93. end
  94. task send_sample;
  95. input [4:0] channel;
  96. input signed [17:0] temp_q;
  97. input fault;
  98. begin
  99. @(negedge clk);
  100. sensor_channel = channel;
  101. sensor_temp_q = temp_q;
  102. sensor_fault = fault;
  103. sensor_sample_seq = sensor_sample_seq + 16'd1;
  104. sensor_valid = 1'b1;
  105. @(negedge clk);
  106. sensor_valid = 1'b0;
  107. wait(pid_done == 1'b1);
  108. @(posedge clk);
  109. @(negedge clk);
  110. end
  111. endtask
  112. task send_sample_while_busy;
  113. input [4:0] channel;
  114. input signed [17:0] temp_q;
  115. input fault;
  116. begin
  117. @(negedge clk);
  118. sensor_channel = 5'd7;
  119. sensor_temp_q = 18'sd90;
  120. sensor_fault = 1'b0;
  121. sensor_sample_seq = sensor_sample_seq + 16'd1;
  122. sensor_valid = 1'b1;
  123. @(negedge clk);
  124. sensor_channel = channel;
  125. sensor_temp_q = temp_q;
  126. sensor_fault = fault;
  127. sensor_sample_seq = sensor_sample_seq + 16'd1;
  128. expect_ready(1'b0);
  129. repeat (2) @(negedge clk);
  130. expect_ready(1'b1);
  131. expect_actuator(1'b1, 1'b0, 2'd0);
  132. expect_pid(5'd7, 18'sd10, 18'sd60, 1'b0);
  133. @(negedge clk);
  134. sensor_valid = 1'b0;
  135. wait(pid_done == 1'b1);
  136. @(posedge clk);
  137. @(negedge clk);
  138. end
  139. endtask
  140. task expect_ready;
  141. input expected_ready;
  142. begin
  143. if (sensor_ready !== expected_ready) begin
  144. $display("sensor_ready mismatch ready=%0b expected=%0b", sensor_ready, expected_ready);
  145. $fatal;
  146. end
  147. end
  148. endtask
  149. task expect_actuator;
  150. input expected_enable;
  151. input expected_force_off;
  152. input [1:0] expected_reason;
  153. begin
  154. if (act_enable !== expected_enable || act_force_off !== expected_force_off || act_reason !== expected_reason) begin
  155. $display("actuator mismatch enable=%0b force_off=%0b reason=%0d", act_enable, act_force_off, act_reason);
  156. $fatal;
  157. end
  158. end
  159. endtask
  160. task expect_pid;
  161. input [4:0] expected_channel;
  162. input signed [17:0] expected_error_q;
  163. input signed [17:0] expected_ctrl_q;
  164. input expected_fault;
  165. begin
  166. if (pid_channel !== expected_channel || pid_error_q !== expected_error_q ||
  167. pid_ctrl_q !== expected_ctrl_q || pid_fault !== expected_fault) begin
  168. $display(
  169. "pid mismatch ch=%0d err=%0d ctrl=%0d fault=%0b expected ch=%0d err=%0d ctrl=%0d fault=%0b",
  170. pid_channel,
  171. pid_error_q,
  172. pid_ctrl_q,
  173. pid_fault,
  174. expected_channel,
  175. expected_error_q,
  176. expected_ctrl_q,
  177. expected_fault
  178. );
  179. $fatal;
  180. end
  181. end
  182. endtask
  183. initial begin
  184. rst_n = 1'b0;
  185. sensor_valid = 1'b0;
  186. sensor_fault = 1'b0;
  187. sensor_channel = 5'd0;
  188. sensor_temp_q = 18'sd0;
  189. sensor_sample_seq = 16'd0;
  190. host_enable = 1'b1;
  191. host_target_q = 18'sd100;
  192. host_a0 = 18'sd6;
  193. host_a1 = -18'sd2;
  194. host_a2 = 18'sd1;
  195. host_mode = 2'd0;
  196. host_config_seq = 16'd1;
  197. repeat (4) @(negedge clk);
  198. rst_n = 1'b1;
  199. expect_ready(1'b1);
  200. send_sample(5'd0, 18'sd80, 1'b0);
  201. expect_actuator(1'b1, 1'b0, 2'd0);
  202. expect_pid(5'd0, 18'sd20, 18'sd120, 1'b0);
  203. expect_ready(1'b1);
  204. send_sample(5'd1, 18'sd95, 1'b0);
  205. expect_actuator(1'b1, 1'b0, 2'd0);
  206. expect_pid(5'd1, 18'sd5, 18'sd30, 1'b0);
  207. send_sample(5'd0, 18'sd80, 1'b0);
  208. expect_actuator(1'b1, 1'b0, 2'd0);
  209. expect_pid(5'd0, 18'sd20, 18'sd127, 1'b0);
  210. send_sample(5'd23, 18'sd114, 1'b0);
  211. expect_actuator(1'b1, 1'b0, 2'd0);
  212. expect_pid(5'd23, -18'sd14, 18'sd0, 1'b0);
  213. send_sample(5'd3, 18'sd0, 1'b1);
  214. expect_actuator(1'b0, 1'b1, 2'd2);
  215. expect_pid(5'd3, 18'sd100, 18'sd0, 1'b1);
  216. send_sample(5'd24, 18'sd88, 1'b0);
  217. expect_actuator(1'b0, 1'b1, 2'd3);
  218. expect_pid(5'd24, 18'sd12, 18'sd0, 1'b1);
  219. send_sample(5'd4, 18'sd80, 1'b0);
  220. expect_actuator(1'b1, 1'b0, 2'd0);
  221. expect_pid(5'd4, 18'sd20, 18'sd120, 1'b0);
  222. host_enable = 1'b0;
  223. send_sample(5'd4, 18'sd80, 1'b0);
  224. expect_actuator(1'b0, 1'b1, 2'd1);
  225. expect_pid(5'd4, 18'sd20, 18'sd0, 1'b0);
  226. // host disable clears channel history before the next enable.
  227. host_enable = 1'b1;
  228. send_sample(5'd4, 18'sd80, 1'b0);
  229. expect_actuator(1'b1, 1'b0, 2'd0);
  230. expect_pid(5'd4, 18'sd20, 18'sd120, 1'b0);
  231. // sensor fault clears channel history before the next valid sample.
  232. send_sample(5'd5, 18'sd90, 1'b0);
  233. expect_actuator(1'b1, 1'b0, 2'd0);
  234. expect_pid(5'd5, 18'sd10, 18'sd60, 1'b0);
  235. send_sample(5'd5, 18'sd90, 1'b0);
  236. expect_actuator(1'b1, 1'b0, 2'd0);
  237. expect_pid(5'd5, 18'sd10, 18'sd100, 1'b0);
  238. send_sample(5'd5, 18'sd0, 1'b1);
  239. expect_actuator(1'b0, 1'b1, 2'd2);
  240. expect_pid(5'd5, 18'sd100, 18'sd0, 1'b1);
  241. send_sample(5'd5, 18'sd90, 1'b0);
  242. expect_actuator(1'b1, 1'b0, 2'd0);
  243. expect_pid(5'd5, 18'sd10, 18'sd60, 1'b0);
  244. // sensor_valid must be held while busy and accepted when ready returns.
  245. send_sample_while_busy(5'd6, 18'sd90, 1'b0);
  246. expect_actuator(1'b1, 1'b0, 2'd0);
  247. expect_pid(5'd6, 18'sd10, 18'sd60, 1'b0);
  248. $display("PASS: pid_contract_integration_tb");
  249. $finish;
  250. end
  251. endmodule