prod2u_p4_temperature_top_integration_tb.v 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. `timescale 1ns/1ps
  2. module prod2u_p4_temperature_top_integration_tb;
  3. reg clk = 1'b0;
  4. reg host_rst_n = 1'b0;
  5. reg host_to_dut = 1'b1;
  6. wire dut_to_host;
  7. wire host_rx_valid;
  8. wire [7:0] host_rx_data;
  9. reg force_sda_low = 1'b0;
  10. tri1 temp_scl;
  11. tri1 temp_sda;
  12. reg [7:0] request_frame [0:23];
  13. reg [7:0] captured [0:127];
  14. integer request_length;
  15. integer captured_count = 0;
  16. integer read_index = 0;
  17. integer index;
  18. integer guard;
  19. integer accepted_requests = 0;
  20. integer host_bytes_launched = 0;
  21. integer dut_bytes_received = 0;
  22. reg [7:0] last_accepted_command = 8'd0;
  23. reg [15:0] crc;
  24. reg early_i2c_seen = 1'b0;
  25. assign temp_sda = force_sda_low ? 1'b0 : 1'bz;
  26. uart_rx_8n1 #(.CLKS_PER_BIT(4)) u_host_rx (
  27. .clk(clk),
  28. .rst_n(host_rst_n),
  29. .rx_i(dut_to_host),
  30. .rx_valid(host_rx_valid),
  31. .rx_data(host_rx_data),
  32. .rx_ready(1'b1),
  33. .framing_error(),
  34. .break_detected(),
  35. .overflow()
  36. );
  37. prod2u_p4_normal_v3_top #(
  38. .CLK_HZ(10000),
  39. .HOST_UART_CLKS_PER_BIT(4),
  40. .POR_CYCLES(4),
  41. .INTER_BYTE_TIMEOUT_MS(8),
  42. .PTC_STEP_CLKS(1),
  43. .FAN_STEP_CLKS(1),
  44. .SOLENOID_ACTIVE_HIGH(1)
  45. ) dut (
  46. .sys_clk(clk),
  47. .host_uart_rx(host_to_dut),
  48. .host_uart_tx(dut_to_host),
  49. .ptc_pwm_out(),
  50. .fan_pwm_out(),
  51. .solenoid_out(),
  52. .motor_en_n(),
  53. .x_step(),
  54. .x_dir(),
  55. .y_step(),
  56. .y_dir(),
  57. .z_step(),
  58. .z_dir(),
  59. .x_limit_raw(1'b0),
  60. .y_limit_raw(1'b0),
  61. .temp_scl(temp_scl),
  62. .temp_sda(temp_sda)
  63. );
  64. always #5 clk = ~clk;
  65. always @(posedge clk) begin
  66. if (host_rx_valid) begin
  67. captured[captured_count] <= host_rx_data;
  68. captured_count <= captured_count + 1;
  69. end
  70. if (!dut.temperature_powerup_ready && dut.temperature_i2c_request_valid)
  71. early_i2c_seen <= 1'b1;
  72. if (dut.request_valid && dut.request_ready) begin
  73. accepted_requests <= accepted_requests + 1;
  74. last_accepted_command <= dut.request_payload[7:0];
  75. end
  76. if (dut.uart_rx_valid && dut.uart_rx_ready)
  77. dut_bytes_received <= dut_bytes_received + 1;
  78. end
  79. function [15:0] crc16_byte;
  80. input [15:0] crc_in;
  81. input [7:0] data;
  82. reg [15:0] value;
  83. integer bit_index;
  84. begin
  85. value = crc_in ^ {data, 8'h00};
  86. for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
  87. if (value[15]) value = (value << 1) ^ 16'h1021;
  88. else value = value << 1;
  89. end
  90. crc16_byte = value;
  91. end
  92. endfunction
  93. task build_host_command;
  94. input [7:0] command;
  95. begin
  96. for (index = 0; index < 24; index = index + 1)
  97. request_frame[index] = 8'd0;
  98. request_frame[0] = 8'ha5;
  99. request_frame[1] = 8'h5a;
  100. request_frame[2] = 8'h02;
  101. request_frame[3] = 8'h04;
  102. request_frame[4] = 8'h05;
  103. request_frame[13] = 8'd4;
  104. request_frame[14] = command;
  105. request_length = 20;
  106. crc = 16'hffff;
  107. for (index = 2; index < 18; index = index + 1)
  108. crc = crc16_byte(crc, request_frame[index]);
  109. request_frame[18] = crc[7:0];
  110. request_frame[19] = crc[15:8];
  111. end
  112. endtask
  113. task send_request;
  114. integer byte_index;
  115. integer bit_index;
  116. begin
  117. for (byte_index = 0; byte_index < request_length; byte_index = byte_index + 1) begin
  118. @(negedge clk);
  119. host_to_dut = 1'b0;
  120. repeat (4) @(posedge clk);
  121. for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
  122. @(negedge clk);
  123. host_to_dut = request_frame[byte_index][bit_index];
  124. repeat (4) @(posedge clk);
  125. end
  126. @(negedge clk);
  127. host_to_dut = 1'b1;
  128. repeat (4) @(posedge clk);
  129. host_bytes_launched = host_bytes_launched + 1;
  130. end
  131. end
  132. endtask
  133. task wait_bytes;
  134. input integer count;
  135. begin
  136. guard = 0;
  137. while (captured_count < read_index + count && guard < 200000) begin
  138. @(negedge clk);
  139. guard = guard + 1;
  140. end
  141. if (captured_count < read_index + count) begin
  142. $display(
  143. "TOP_TEMPERATURE_UART_TIMEOUT have=%0d need=%0d host_bytes=%0d dut_bytes=%0d accepted=%0d last_cmd=%02x crc_err=%0d timeout_err=%0d format_err=%0d req_valid=%0d req_ready=%0d core_state=%0d inflight=%0d read_state=%0d read_done=%0d bus_error=%0d i2c_state=%0d i2c_valid=%0d i2c_error=%0d frame_valid=%0d tx_owner=%0d",
  144. captured_count,
  145. read_index + count,
  146. host_bytes_launched,
  147. dut_bytes_received,
  148. accepted_requests,
  149. last_accepted_command,
  150. dut.parser_crc_error_count,
  151. dut.parser_timeout_count,
  152. dut.parser_format_error_count,
  153. dut.request_valid,
  154. dut.request_ready,
  155. dut.u_core.state,
  156. dut.u_core.command_inflight,
  157. dut.u_temperature_read.state,
  158. dut.temperature_read_done,
  159. dut.temperature_bus_error,
  160. dut.u_temperature_i2c.state,
  161. dut.temperature_i2c_response_valid,
  162. dut.temperature_i2c_error,
  163. dut.frame_valid,
  164. dut.u_tx_frames.owner
  165. );
  166. $fatal;
  167. end
  168. end
  169. endtask
  170. task check_crc;
  171. input integer frame_length;
  172. begin
  173. crc = 16'hffff;
  174. for (index = read_index + 2; index < read_index + frame_length - 2; index = index + 1)
  175. crc = crc16_byte(crc, captured[index]);
  176. if (captured[read_index + frame_length - 2] !== crc[7:0] ||
  177. captured[read_index + frame_length - 1] !== crc[15:8]) begin
  178. $display("TOP_TEMPERATURE_CRC_MISMATCH");
  179. $fatal;
  180. end
  181. end
  182. endtask
  183. task expect_all_ff_temperature;
  184. begin
  185. wait_bytes(35);
  186. if (captured[read_index] !== 8'ha5 || captured[read_index + 1] !== 8'h5a ||
  187. captured[read_index + 2] !== 8'h02 || captured[read_index + 3] !== 8'h04 ||
  188. captured[read_index + 4] !== 8'h10 || captured[read_index + 5] !== 8'h1b)
  189. $fatal;
  190. for (index = 0; index < 27; index = index + 1)
  191. if (captured[read_index + 6 + index] !== 8'hff) $fatal;
  192. check_crc(35);
  193. read_index = read_index + 35;
  194. end
  195. endtask
  196. task expect_ack;
  197. input [7:0] command;
  198. input [7:0] status;
  199. input expected_ok;
  200. begin
  201. wait_bytes(24);
  202. if (captured[read_index + 4] !== 8'h06 ||
  203. captured[read_index + 13] !== 8'd8 ||
  204. captured[read_index + 14] !== {7'd0, expected_ok} ||
  205. captured[read_index + 15] !== command ||
  206. captured[read_index + 16] !== status) begin
  207. $display("TOP_TEMPERATURE_ACK_MISMATCH");
  208. $fatal;
  209. end
  210. check_crc(24);
  211. read_index = read_index + 24;
  212. end
  213. endtask
  214. initial begin
  215. repeat (4) @(negedge clk);
  216. host_rst_n = 1'b1;
  217. wait (dut.sys_rst_n === 1'b1);
  218. build_host_command(8'h28);
  219. send_request();
  220. expect_all_ff_temperature();
  221. if (early_i2c_seen) begin
  222. $display("EARLY_GET_ACCESSED_I2C");
  223. $fatal;
  224. end
  225. wait (dut.temperature_powerup_ready === 1'b1);
  226. force_sda_low = 1'b1;
  227. build_host_command(8'h28);
  228. send_request();
  229. expect_ack(8'h28, 8'h0a, 1'b0);
  230. force_sda_low = 1'b0;
  231. repeat (8) @(negedge clk);
  232. if (dut.temperature_scl_drive_low || dut.temperature_sda_drive_low) begin
  233. $display("TOP_TEMPERATURE_BUS_NOT_RELEASED_AFTER_ERROR");
  234. $fatal;
  235. end
  236. build_host_command(8'h24);
  237. send_request();
  238. expect_ack(8'h24, 8'h00, 1'b1);
  239. $display("PASS: prod2u_p4_temperature_top_integration_tb");
  240. $finish;
  241. end
  242. endmodule