prod2u_p4_normal_v3_top_tb.v 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. `timescale 1ns/1ps
  2. module prod2u_p4_normal_v3_top_tb;
  3. reg clk = 1'b0;
  4. reg host_rst_n = 1'b0;
  5. reg host_tx_valid = 1'b0;
  6. reg [7:0] host_tx_data = 8'd0;
  7. wire host_tx_ready;
  8. wire host_to_dut;
  9. wire dut_to_host;
  10. wire host_rx_valid;
  11. wire [7:0] host_rx_data;
  12. reg x_limit_raw = 1'b0;
  13. reg y_limit_raw = 1'b0;
  14. wire [23:0] ptc_pwm_out;
  15. wire [3:0] fan_pwm_out;
  16. wire solenoid_out;
  17. wire motor_en_n;
  18. wire x_step;
  19. wire x_dir;
  20. wire y_step;
  21. wire y_dir;
  22. wire z_step;
  23. wire z_dir;
  24. reg [7:0] request_frame [0:49];
  25. reg [7:0] captured [0:511];
  26. integer request_length;
  27. integer captured_count = 0;
  28. integer read_index = 0;
  29. integer i;
  30. integer guard;
  31. integer failures = 0;
  32. integer x_rises = 0;
  33. reg [15:0] crc;
  34. reg [7:0] rx_frame_type;
  35. reg [7:0] rx_payload_len;
  36. reg [143:0] rx_payload;
  37. reg [191:0] held_ptc;
  38. reg [31:0] held_fan;
  39. reg [7:0] held_solenoid;
  40. reg [15:0] held_hash;
  41. reg held_disabled;
  42. reg held_timeout;
  43. uart_tx_8n1 #(.CLKS_PER_BIT(4)) u_host_tx (
  44. .clk(clk), .rst_n(host_rst_n), .tx_valid(host_tx_valid),
  45. .tx_data(host_tx_data), .tx_ready(host_tx_ready),
  46. .tx_o(host_to_dut), .busy()
  47. );
  48. uart_rx_8n1 #(.CLKS_PER_BIT(4)) u_host_rx (
  49. .clk(clk), .rst_n(host_rst_n), .rx_i(dut_to_host),
  50. .rx_valid(host_rx_valid), .rx_data(host_rx_data), .rx_ready(1'b1),
  51. .framing_error(), .break_detected(), .overflow()
  52. );
  53. prod2u_p4_normal_v3_top #(
  54. .CLK_HZ(4000000),
  55. .HOST_UART_CLKS_PER_BIT(4),
  56. .POR_CYCLES(4),
  57. .INTER_BYTE_TIMEOUT_MS(2),
  58. .PTC_STEP_CLKS(1),
  59. .FAN_STEP_CLKS(1),
  60. .SOLENOID_ACTIVE_HIGH(1)
  61. ) dut (
  62. .sys_clk(clk), .host_uart_rx(host_to_dut), .host_uart_tx(dut_to_host),
  63. .ptc_pwm_out(ptc_pwm_out), .fan_pwm_out(fan_pwm_out),
  64. .solenoid_out(solenoid_out), .motor_en_n(motor_en_n),
  65. .x_step(x_step), .x_dir(x_dir), .y_step(y_step), .y_dir(y_dir),
  66. .z_step(z_step), .z_dir(z_dir),
  67. .x_limit_raw(x_limit_raw), .y_limit_raw(y_limit_raw)
  68. );
  69. always #5 clk = ~clk;
  70. always @(posedge clk) begin
  71. if (host_rx_valid) begin
  72. captured[captured_count] <= host_rx_data;
  73. captured_count <= captured_count + 1;
  74. end
  75. end
  76. always @(posedge x_step) begin
  77. x_rises = x_rises + 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_command;
  94. input [7:0] frame_type;
  95. input integer payload_length;
  96. input [7:0] command;
  97. begin
  98. for (i = 0; i < 50; i = i + 1) request_frame[i] = 8'd0;
  99. request_frame[0] = 8'ha5;
  100. request_frame[1] = 8'h5a;
  101. request_frame[2] = 8'h02;
  102. request_frame[3] = 8'h04;
  103. request_frame[4] = frame_type;
  104. request_frame[13] = payload_length[7:0];
  105. request_frame[14] = command;
  106. request_length = 16 + payload_length;
  107. crc = 16'hffff;
  108. for (i = 2; i < 14 + payload_length; i = i + 1) begin
  109. crc = crc16_byte(crc, request_frame[i]);
  110. end
  111. request_frame[14 + payload_length] = crc[7:0];
  112. request_frame[15 + payload_length] = crc[15:8];
  113. end
  114. endtask
  115. task send_request;
  116. begin
  117. for (i = 0; i < request_length; i = i + 1) begin
  118. while (!host_tx_ready) @(negedge clk);
  119. host_tx_data = request_frame[i];
  120. host_tx_valid = 1'b1;
  121. @(negedge clk);
  122. host_tx_valid = 1'b0;
  123. end
  124. end
  125. endtask
  126. task build_jog;
  127. input [1:0] axis;
  128. input direction;
  129. input [1:0] speed_code;
  130. input [15:0] steps;
  131. begin
  132. build_command(8'h11, 8, 8'h35);
  133. request_frame[15] = {6'd0, axis};
  134. request_frame[16] = {7'd0, direction};
  135. request_frame[17] = {6'd0, speed_code};
  136. request_frame[18] = steps[7:0];
  137. request_frame[19] = steps[15:8];
  138. crc = 16'hffff;
  139. for (i = 2; i < 22; i = i + 1) begin
  140. crc = crc16_byte(crc, request_frame[i]);
  141. end
  142. request_frame[22] = crc[7:0];
  143. request_frame[23] = crc[15:8];
  144. end
  145. endtask
  146. task read_frame;
  147. integer frame_start;
  148. integer frame_bytes;
  149. integer scan_guard;
  150. begin
  151. frame_start = -1;
  152. scan_guard = 0;
  153. while (frame_start < 0 && scan_guard < 200000) begin
  154. while (captured_count < read_index + 2 && scan_guard < 200000) begin
  155. @(negedge clk);
  156. scan_guard = scan_guard + 1;
  157. end
  158. if (captured[read_index] == 8'ha5 && captured[read_index + 1] == 8'h5a) begin
  159. frame_start = read_index;
  160. end else begin
  161. read_index = read_index + 1;
  162. end
  163. end
  164. if (frame_start < 0 || captured[frame_start] !== 8'ha5 ||
  165. captured[frame_start + 1] !== 8'h5a) begin
  166. $display("TOP_FRAME_TIMEOUT captured=%0d read_index=%0d sys_rst_n=%0d frame_valid=%0d frame_ready=%0d serializer_busy=%0d serializer_done=%0d serializer_tx_valid=%0d serializer_byte=%02x uart_ready=%0d uart_busy=%0d uart_state=%0d uart_tx=%0d",
  167. captured_count, read_index, dut.sys_rst_n,
  168. dut.frame_valid, dut.frame_ready,
  169. dut.u_tx_frames.serializer_busy,
  170. dut.u_tx_frames.serializer_done,
  171. dut.u_tx_frames.tx_valid,
  172. dut.u_tx_frames.tx_data,
  173. dut.tx_stream_ready, dut.u_uart_tx.busy,
  174. dut.u_uart_tx.state, dut_to_host);
  175. $fatal;
  176. end
  177. while (captured_count < frame_start + 14) @(negedge clk);
  178. rx_frame_type = captured[frame_start + 4];
  179. rx_payload_len = captured[frame_start + 13];
  180. frame_bytes = 16 + rx_payload_len;
  181. while (captured_count < frame_start + frame_bytes) @(negedge clk);
  182. rx_payload = 144'd0;
  183. for (i = 0; i < rx_payload_len; i = i + 1) begin
  184. rx_payload[i * 8 +: 8] = captured[frame_start + 14 + i];
  185. end
  186. crc = 16'hffff;
  187. for (i = frame_start + 2; i < frame_start + 14 + rx_payload_len; i = i + 1) begin
  188. crc = crc16_byte(crc, captured[i]);
  189. end
  190. if (captured[frame_start + 14 + rx_payload_len] !== crc[7:0] ||
  191. captured[frame_start + 15 + rx_payload_len] !== crc[15:8]) $fatal;
  192. read_index = frame_start + frame_bytes;
  193. end
  194. endtask
  195. task expect_ack_without_trace;
  196. input [7:0] expected_command;
  197. input [7:0] expected_status;
  198. input expected_ok;
  199. reg first_frame;
  200. reg ack_seen;
  201. begin
  202. first_frame = 1'b1;
  203. ack_seen = 1'b0;
  204. guard = 0;
  205. while (!ack_seen && guard < 32) begin
  206. read_frame();
  207. if (first_frame && rx_frame_type !== 8'h06) begin
  208. $display("ACK_ONLY_FIRST_FRAME_FAIL command=%02x frame=%02x",
  209. expected_command, rx_frame_type);
  210. failures = failures + 1;
  211. end
  212. first_frame = 1'b0;
  213. if (rx_frame_type == 8'h0f) begin
  214. $display("ACK_ONLY_TRACE_FORBIDDEN command=%02x event=%02x seq=%0d",
  215. expected_command, rx_payload[39:32], rx_payload[31:0]);
  216. failures = failures + 1;
  217. end else if (rx_frame_type == 8'h06) begin
  218. ack_seen = 1'b1;
  219. if (rx_payload_len !== 8'd8 ||
  220. rx_payload[7:0] !== {7'd0, expected_ok} ||
  221. rx_payload[15:8] !== expected_command ||
  222. rx_payload[23:16] !== expected_status) begin
  223. $display("ACK_ONLY_ACK_PAYLOAD_FAIL command=%02x payload=%h",
  224. expected_command, rx_payload[63:0]);
  225. failures = failures + 1;
  226. end
  227. end else begin
  228. $display("ACK_ONLY_UNEXPECTED_BEFORE_ACK command=%02x frame=%02x",
  229. expected_command, rx_frame_type);
  230. failures = failures + 1;
  231. end
  232. guard = guard + 1;
  233. end
  234. if (!ack_seen) begin
  235. $display("ACK_ONLY_ACK_TIMEOUT command=%02x", expected_command);
  236. $fatal;
  237. end
  238. end
  239. endtask
  240. task expect_motion_status_without_trace;
  241. reg first_frame;
  242. reg status_seen;
  243. begin
  244. first_frame = 1'b1;
  245. status_seen = 1'b0;
  246. guard = 0;
  247. while (!status_seen && guard < 32) begin
  248. read_frame();
  249. if (first_frame && rx_frame_type !== 8'h12) begin
  250. $display("ACK_ONLY_STATUS_ORDER_FAIL frame=%02x", rx_frame_type);
  251. failures = failures + 1;
  252. end
  253. first_frame = 1'b0;
  254. if (rx_frame_type == 8'h0f) begin
  255. $display("ACK_ONLY_STATUS_TRACE_FORBIDDEN event=%02x seq=%0d",
  256. rx_payload[39:32], rx_payload[31:0]);
  257. failures = failures + 1;
  258. end else if (rx_frame_type == 8'h12) begin
  259. status_seen = 1'b1;
  260. if (rx_payload_len !== 8'd10) begin
  261. $display("ACK_ONLY_STATUS_LENGTH_FAIL len=%0d", rx_payload_len);
  262. failures = failures + 1;
  263. end
  264. end else begin
  265. $display("ACK_ONLY_UNEXPECTED_BEFORE_STATUS frame=%02x", rx_frame_type);
  266. failures = failures + 1;
  267. end
  268. guard = guard + 1;
  269. end
  270. if (!status_seen) begin
  271. $display("ACK_ONLY_STATUS_TIMEOUT");
  272. $fatal;
  273. end
  274. end
  275. endtask
  276. task expect_transaction_quiet;
  277. input [7:0] command;
  278. begin
  279. repeat (5000) @(negedge clk);
  280. while (captured_count >= read_index + 16) begin
  281. read_frame();
  282. if (rx_frame_type == 8'h0f) begin
  283. $display("ACK_ONLY_LATE_TRACE_FORBIDDEN command=%02x event=%02x seq=%0d",
  284. command, rx_payload[39:32], rx_payload[31:0]);
  285. end else begin
  286. $display("ACK_ONLY_LATE_FRAME_FORBIDDEN command=%02x frame=%02x",
  287. command, rx_frame_type);
  288. end
  289. failures = failures + 1;
  290. end
  291. if (captured_count !== read_index) begin
  292. $display("ACK_ONLY_LATE_PARTIAL_FRAME command=%02x bytes=%0d",
  293. command, captured_count - read_index);
  294. failures = failures + 1;
  295. read_index = captured_count;
  296. end
  297. end
  298. endtask
  299. initial begin
  300. repeat (3) @(negedge clk);
  301. host_rst_n = 1'b1;
  302. wait (dut.sys_rst_n === 1'b1);
  303. // There is no unsolicited BOOT or Trace frame. The first received
  304. // frame must be the ACK for the first host request below.
  305. repeat (200) @(negedge clk);
  306. if (captured_count != 0) begin
  307. $display("ACK_ONLY_UNSOLICITED_FRAME bytes=%0d first=%02x",
  308. captured_count, captured[0]);
  309. failures = failures + 1;
  310. end
  311. // Motor movement is independent of disabled/watchdog and LS inputs.
  312. x_limit_raw = 1'b1;
  313. y_limit_raw = 1'b1;
  314. build_jog(2'd0, 1'b1, 2'd3, 16'd1);
  315. send_request();
  316. expect_ack_without_trace(8'h35, 8'h00, 1'b1);
  317. if (x_rises != 1 || motor_en_n !== 1'b0 || !dut.disabled) begin
  318. $display("NO_GATES_TOP_FAIL rises=%0d en_n=%0d disabled=%0d",
  319. x_rises, motor_en_n, dut.disabled);
  320. failures = failures + 1;
  321. end
  322. wait (x_step === 1'b0);
  323. expect_transaction_quiet(8'h35);
  324. build_command(8'h05, 4, 8'h24);
  325. send_request();
  326. expect_ack_without_trace(8'h24, 8'h00, 1'b1);
  327. expect_transaction_quiet(8'h24);
  328. held_ptc = dut.ptc_percent;
  329. held_fan = dut.fan_percent;
  330. held_solenoid = dut.solenoid_percent;
  331. held_hash = dut.state_hash16;
  332. held_disabled = dut.disabled;
  333. held_timeout = dut.watchdog_timeout;
  334. build_command(8'h05, 4, 8'h29);
  335. send_request();
  336. expect_ack_without_trace(8'h29, 8'h01, 1'b0);
  337. expect_transaction_quiet(8'h29);
  338. if (dut.ptc_percent !== held_ptc || dut.fan_percent !== held_fan ||
  339. dut.solenoid_percent !== held_solenoid || dut.state_hash16 !== held_hash ||
  340. dut.disabled !== held_disabled || dut.watchdog_timeout !== held_timeout) $fatal;
  341. build_command(8'h11, 8, 8'h37);
  342. send_request();
  343. expect_ack_without_trace(8'h37, 8'h00, 1'b1);
  344. expect_motion_status_without_trace();
  345. expect_transaction_quiet(8'h37);
  346. held_ptc = dut.ptc_percent;
  347. held_fan = dut.fan_percent;
  348. held_solenoid = dut.solenoid_percent;
  349. held_hash = dut.state_hash16;
  350. held_disabled = dut.disabled;
  351. held_timeout = dut.watchdog_timeout;
  352. build_command(8'h11, 8, 8'h36);
  353. send_request();
  354. expect_ack_without_trace(8'h36, 8'h01, 1'b0);
  355. expect_transaction_quiet(8'h36);
  356. if (dut.ptc_percent !== held_ptc || dut.fan_percent !== held_fan ||
  357. dut.solenoid_percent !== held_solenoid || dut.state_hash16 !== held_hash ||
  358. dut.disabled !== held_disabled || dut.watchdog_timeout !== held_timeout ||
  359. x_step || y_step || z_step) begin
  360. $display("ACK_ONLY_MARK_UNSUPPORTED_SIDE_EFFECT");
  361. failures = failures + 1;
  362. end
  363. build_command(8'h05, 4, 8'h26);
  364. send_request();
  365. expect_ack_without_trace(8'h26, 8'h00, 1'b1);
  366. expect_transaction_quiet(8'h26);
  367. if (motor_en_n || x_step || y_step || z_step || solenoid_out) $fatal;
  368. if (!dut.disabled) $fatal;
  369. if (failures != 0) begin
  370. $display("ACK_ONLY_CONTRACT_RED failures=%0d", failures);
  371. $fatal;
  372. end
  373. $display("PASS: prod2u_p4_normal_v3_top_tb");
  374. $finish;
  375. end
  376. endmodule