prod2u_p4_normal_v3_rx_tb.v 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. `timescale 1ns/1ps
  2. module prod2u_p4_normal_v3_rx_tb;
  3. localparam [7:0] HOST_CMD = 8'h05;
  4. localparam [7:0] HOST_BATCH_CMD = 8'h07;
  5. localparam [7:0] MOTION_CMD = 8'h11;
  6. reg clk;
  7. reg rst_n;
  8. reg rx_valid;
  9. wire rx_ready;
  10. reg [7:0] rx_data;
  11. wire request_valid;
  12. reg request_ready;
  13. wire [7:0] request_type;
  14. wire [5:0] request_length;
  15. wire [271:0] request_payload;
  16. wire [15:0] request_crc16;
  17. wire fast_jog_valid;
  18. wire [1:0] fast_jog_axis;
  19. wire fast_jog_dir;
  20. wire [15:0] fast_jog_steps;
  21. wire [15:0] crc_error_count;
  22. wire [15:0] timeout_count;
  23. wire [15:0] format_error_count;
  24. reg [7:0] frame [0:49];
  25. reg [271:0] expected_payload;
  26. reg [15:0] count_before;
  27. reg [7:0] held_request_type;
  28. reg [5:0] held_request_length;
  29. reg [271:0] held_request_payload;
  30. reg [15:0] held_request_crc16;
  31. reg [15:0] held_crc_error_count;
  32. reg [15:0] held_timeout_count;
  33. reg [15:0] held_format_error_count;
  34. reg check_held_request;
  35. integer frame_length;
  36. integer i;
  37. integer wait_cycles;
  38. reg timeout_probe_valid;
  39. reg [7:0] timeout_probe_data;
  40. wire timeout1_probe_ready;
  41. wire timeout3_probe_ready;
  42. wire [15:0] timeout1_probe_count;
  43. wire [15:0] timeout3_probe_count;
  44. prod2u_p4_normal_v3_rx #(
  45. .CLK_HZ(10000),
  46. .INTER_BYTE_TIMEOUT_MS(2)
  47. ) dut (
  48. .clk(clk),
  49. .rst_n(rst_n),
  50. .rx_valid(rx_valid),
  51. .rx_ready(rx_ready),
  52. .rx_data(rx_data),
  53. .request_valid(request_valid),
  54. .request_ready(request_ready),
  55. .request_type(request_type),
  56. .request_length(request_length),
  57. .request_payload(request_payload),
  58. .request_crc16(request_crc16),
  59. .fast_jog_valid(fast_jog_valid),
  60. .fast_jog_axis(fast_jog_axis),
  61. .fast_jog_dir(fast_jog_dir),
  62. .fast_jog_steps(fast_jog_steps),
  63. .crc_error_count(crc_error_count),
  64. .timeout_count(timeout_count),
  65. .format_error_count(format_error_count)
  66. );
  67. prod2u_p4_normal_v3_rx #(
  68. .CLK_HZ(1000),
  69. .INTER_BYTE_TIMEOUT_MS(1)
  70. ) timeout1_dut (
  71. .clk(clk),
  72. .rst_n(rst_n),
  73. .rx_valid(timeout_probe_valid),
  74. .rx_ready(timeout1_probe_ready),
  75. .rx_data(timeout_probe_data),
  76. .request_valid(),
  77. .request_ready(1'b1),
  78. .request_type(),
  79. .request_length(),
  80. .request_payload(),
  81. .request_crc16(),
  82. .fast_jog_valid(),
  83. .fast_jog_axis(),
  84. .fast_jog_dir(),
  85. .fast_jog_steps(),
  86. .crc_error_count(),
  87. .timeout_count(timeout1_probe_count),
  88. .format_error_count()
  89. );
  90. prod2u_p4_normal_v3_rx #(
  91. .CLK_HZ(1000),
  92. .INTER_BYTE_TIMEOUT_MS(3)
  93. ) timeout3_dut (
  94. .clk(clk),
  95. .rst_n(rst_n),
  96. .rx_valid(timeout_probe_valid),
  97. .rx_ready(timeout3_probe_ready),
  98. .rx_data(timeout_probe_data),
  99. .request_valid(),
  100. .request_ready(1'b1),
  101. .request_type(),
  102. .request_length(),
  103. .request_payload(),
  104. .request_crc16(),
  105. .fast_jog_valid(),
  106. .fast_jog_axis(),
  107. .fast_jog_dir(),
  108. .fast_jog_steps(),
  109. .crc_error_count(),
  110. .timeout_count(timeout3_probe_count),
  111. .format_error_count()
  112. );
  113. initial begin
  114. clk = 1'b0;
  115. forever #5 clk = ~clk;
  116. end
  117. always @(posedge clk) begin
  118. if (check_held_request &&
  119. (!request_valid || request_type !== held_request_type ||
  120. request_length !== held_request_length ||
  121. request_payload !== held_request_payload ||
  122. request_crc16 !== held_request_crc16)) begin
  123. $display(
  124. "held request changed valid=%0d type=%02x/%02x len=%0d/%0d crc=%04x/%04x payload_match=%0d",
  125. request_valid,
  126. request_type,
  127. held_request_type,
  128. request_length,
  129. held_request_length,
  130. request_crc16,
  131. held_request_crc16,
  132. request_payload === held_request_payload
  133. );
  134. $fatal;
  135. end
  136. end
  137. function [15:0] crc16_byte;
  138. input [15:0] crc_in;
  139. input [7:0] data;
  140. reg [15:0] value;
  141. integer bit_index;
  142. begin
  143. value = crc_in ^ {data, 8'h00};
  144. for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
  145. if (value[15]) begin
  146. value = (value << 1) ^ 16'h1021;
  147. end else begin
  148. value = value << 1;
  149. end
  150. end
  151. crc16_byte = value;
  152. end
  153. endfunction
  154. task begin_frame;
  155. input [7:0] frame_type;
  156. input [7:0] payload_length;
  157. begin
  158. for (i = 0; i < 50; i = i + 1) begin
  159. frame[i] = 8'h00;
  160. end
  161. frame[0] = 8'ha5;
  162. frame[1] = 8'h5a;
  163. frame[2] = 8'h02;
  164. frame[3] = 8'h04;
  165. frame[4] = frame_type;
  166. frame[13] = payload_length;
  167. frame_length = 16 + payload_length;
  168. end
  169. endtask
  170. task finish_frame;
  171. reg [15:0] crc;
  172. integer last_body_byte;
  173. begin
  174. crc = 16'hffff;
  175. last_body_byte = frame_length - 3;
  176. for (i = 2; i <= last_body_byte; i = i + 1) begin
  177. crc = crc16_byte(crc, frame[i]);
  178. end
  179. frame[frame_length - 2] = crc[7:0];
  180. frame[frame_length - 1] = crc[15:8];
  181. end
  182. endtask
  183. task build_snapshot;
  184. begin
  185. begin_frame(HOST_BATCH_CMD, 8'd34);
  186. frame[14] = 8'h2a;
  187. frame[15] = 8'h00;
  188. for (i = 0; i < 24; i = i + 1) begin
  189. frame[16 + i] = i[7:0];
  190. end
  191. frame[40] = 8'd30;
  192. frame[41] = 8'd40;
  193. frame[42] = 8'd50;
  194. frame[43] = 8'd60;
  195. frame[44] = 8'd100;
  196. frame[45] = 8'd0;
  197. frame[46] = 8'd100;
  198. frame[47] = 8'd0;
  199. finish_frame();
  200. end
  201. endtask
  202. task build_host_command;
  203. input [7:0] command;
  204. begin
  205. begin_frame(HOST_CMD, 8'd4);
  206. frame[14] = command;
  207. finish_frame();
  208. end
  209. endtask
  210. task build_motion_command;
  211. input [7:0] opcode;
  212. begin
  213. begin_frame(MOTION_CMD, 8'd8);
  214. frame[14] = opcode;
  215. frame[15] = 8'd1;
  216. frame[16] = 8'd0;
  217. frame[17] = 8'd3;
  218. frame[18] = 8'ha0;
  219. frame[19] = 8'h00;
  220. finish_frame();
  221. end
  222. endtask
  223. task send_byte;
  224. input [7:0] value;
  225. begin
  226. while (!rx_ready) begin
  227. @(negedge clk);
  228. end
  229. rx_data = value;
  230. rx_valid = 1'b1;
  231. @(posedge clk);
  232. #1;
  233. @(negedge clk);
  234. rx_valid = 1'b0;
  235. end
  236. endtask
  237. task send_built_frame;
  238. begin
  239. for (i = 0; i < frame_length; i = i + 1) begin
  240. send_byte(frame[i]);
  241. end
  242. end
  243. endtask
  244. task expect_request;
  245. input [7:0] expected_type;
  246. input [5:0] expected_length;
  247. input [271:0] expected_bytes;
  248. begin
  249. wait_cycles = 0;
  250. while (!request_valid && wait_cycles < 20) begin
  251. @(negedge clk);
  252. wait_cycles = wait_cycles + 1;
  253. end
  254. if (!request_valid) begin
  255. $display("request timeout type=%02x", expected_type);
  256. $fatal;
  257. end
  258. if (request_type !== expected_type ||
  259. request_length !== expected_length ||
  260. request_payload !== expected_bytes ||
  261. request_crc16 !== {frame[frame_length - 1], frame[frame_length - 2]}) begin
  262. $display(
  263. "request mismatch type=%02x/%02x length=%0d/%0d",
  264. request_type,
  265. expected_type,
  266. request_length,
  267. expected_length
  268. );
  269. $fatal;
  270. end
  271. @(posedge clk);
  272. #1;
  273. if (request_valid) begin
  274. $display("request_valid did not clear after ready handshake");
  275. $fatal;
  276. end
  277. end
  278. endtask
  279. task expect_format_rejection;
  280. begin
  281. count_before = format_error_count;
  282. send_built_frame();
  283. repeat (3) @(negedge clk);
  284. if (request_valid || format_error_count !== count_before + 16'd1) begin
  285. $display(
  286. "format rejection mismatch count=%0d/%0d valid=%0d",
  287. format_error_count,
  288. count_before + 16'd1,
  289. request_valid
  290. );
  291. $fatal;
  292. end
  293. end
  294. endtask
  295. initial begin
  296. rst_n = 1'b0;
  297. rx_valid = 1'b0;
  298. rx_data = 8'h00;
  299. request_ready = 1'b1;
  300. check_held_request = 1'b0;
  301. timeout_probe_valid = 1'b0;
  302. timeout_probe_data = 8'h00;
  303. repeat (4) @(negedge clk);
  304. rst_n = 1'b1;
  305. if ($bits(timeout1_dut.timeout_counter) != 1 ||
  306. $bits(timeout3_dut.timeout_counter) != 2) begin
  307. $display(
  308. "timeout counter widths mismatch one=%0d three=%0d",
  309. $bits(timeout1_dut.timeout_counter),
  310. $bits(timeout3_dut.timeout_counter)
  311. );
  312. $fatal;
  313. end
  314. // A parser with timeout N expires on the Nth idle cycle after a byte.
  315. timeout_probe_data = 8'ha5;
  316. timeout_probe_valid = 1'b1;
  317. @(posedge clk);
  318. #1;
  319. @(negedge clk);
  320. timeout_probe_valid = 1'b0;
  321. @(posedge clk);
  322. #1;
  323. if (timeout1_probe_count !== 16'd1 || timeout3_probe_count !== 16'd0 ||
  324. timeout3_dut.timeout_counter !== 2'd1) begin
  325. $display(
  326. "timeout first-idle mismatch t1=%0d t3=%0d counter3=%0d",
  327. timeout1_probe_count,
  328. timeout3_probe_count,
  329. timeout3_dut.timeout_counter
  330. );
  331. $fatal;
  332. end
  333. @(posedge clk);
  334. #1;
  335. if (timeout3_probe_count !== 16'd0 || timeout3_dut.timeout_counter !== 2'd2) begin
  336. $display(
  337. "timeout second-idle mismatch t3=%0d counter3=%0d",
  338. timeout3_probe_count,
  339. timeout3_dut.timeout_counter
  340. );
  341. $fatal;
  342. end
  343. @(posedge clk);
  344. #1;
  345. if (timeout3_probe_count !== 16'd1 || timeout3_dut.timeout_counter !== 2'd0) begin
  346. $display(
  347. "timeout third-idle mismatch t3=%0d counter3=%0d",
  348. timeout3_probe_count,
  349. timeout3_dut.timeout_counter
  350. );
  351. $fatal;
  352. end
  353. // The first complete command is accepted immediately after reset.
  354. build_snapshot();
  355. expected_payload = 272'd0;
  356. for (i = 0; i < 34; i = i + 1) begin
  357. expected_payload[i * 8 +: 8] = frame[14 + i];
  358. end
  359. send_built_frame();
  360. expect_request(HOST_BATCH_CMD, 6'd34, expected_payload);
  361. build_host_command(8'h29);
  362. expected_payload = 272'd0;
  363. expected_payload[7:0] = 8'h29;
  364. send_built_frame();
  365. expect_request(HOST_CMD, 6'd4, expected_payload);
  366. build_host_command(8'h26);
  367. expected_payload = 272'd0;
  368. expected_payload[7:0] = 8'h26;
  369. send_built_frame();
  370. expect_request(HOST_CMD, 6'd4, expected_payload);
  371. build_motion_command(8'h35);
  372. send_built_frame();
  373. if (!fast_jog_valid || fast_jog_axis !== 2'd1 ||
  374. fast_jog_dir !== 1'b0 || fast_jog_steps !== 16'd160 ||
  375. request_valid) begin
  376. $display("legal JOG did not use the fast exit");
  377. $fatal;
  378. end
  379. @(posedge clk);
  380. #1;
  381. if (fast_jog_valid) begin
  382. $display("fast JOG event was not one cycle");
  383. $fatal;
  384. end
  385. build_host_command(8'h24);
  386. frame[2] = 8'h03;
  387. expect_format_rejection();
  388. build_host_command(8'h24);
  389. frame[3] = 8'h03;
  390. expect_format_rejection();
  391. build_host_command(8'h24);
  392. frame[4] = 8'h06;
  393. expect_format_rejection();
  394. build_host_command(8'h24);
  395. frame[5] = 8'h01;
  396. expect_format_rejection();
  397. build_host_command(8'h24);
  398. frame[9] = 8'h01;
  399. expect_format_rejection();
  400. build_snapshot();
  401. frame[13] = 8'd33;
  402. expect_format_rejection();
  403. build_host_command(8'h24);
  404. frame[13] = 8'd8;
  405. expect_format_rejection();
  406. build_motion_command(8'h37);
  407. frame[13] = 8'd4;
  408. expect_format_rejection();
  409. build_host_command(8'h26);
  410. frame[frame_length - 1] = frame[frame_length - 1] ^ 8'h01;
  411. count_before = crc_error_count;
  412. send_built_frame();
  413. repeat (3) @(negedge clk);
  414. if (request_valid || crc_error_count !== count_before + 16'd1) begin
  415. $display("CRC rejection mismatch count=%0d valid=%0d", crc_error_count, request_valid);
  416. $fatal;
  417. end
  418. // A truncated frame expires without producing a request.
  419. build_snapshot();
  420. for (i = 0; i < 6; i = i + 1) begin
  421. send_byte(frame[i]);
  422. end
  423. repeat (19) @(negedge clk);
  424. if (timeout_count !== 16'd0) begin
  425. $display("timeout asserted before boundary count=%0d", timeout_count);
  426. $fatal;
  427. end
  428. @(negedge clk);
  429. if (timeout_count !== 16'd1 || request_valid) begin
  430. $display("timeout mismatch count=%0d valid=%0d", timeout_count, request_valid);
  431. $fatal;
  432. end
  433. // The one-entry ordinary slot stays stable while RX continues parsing.
  434. request_ready = 1'b0;
  435. build_motion_command(8'h37);
  436. send_built_frame();
  437. wait_cycles = 0;
  438. while (!request_valid && wait_cycles < 20) begin
  439. @(negedge clk);
  440. wait_cycles = wait_cycles + 1;
  441. end
  442. if (!request_valid) begin
  443. $display("ordinary request was not held under backpressure");
  444. $fatal;
  445. end
  446. held_request_type = request_type;
  447. held_request_length = request_length;
  448. held_request_payload = request_payload;
  449. held_request_crc16 = request_crc16;
  450. held_crc_error_count = crc_error_count;
  451. held_timeout_count = timeout_count;
  452. held_format_error_count = format_error_count;
  453. check_held_request = 1'b1;
  454. repeat (8) begin
  455. @(posedge clk);
  456. #1;
  457. if (!rx_ready || crc_error_count !== held_crc_error_count ||
  458. timeout_count !== held_timeout_count ||
  459. format_error_count !== held_format_error_count) begin
  460. $display("held request blocked RX or changed parser counters");
  461. $fatal;
  462. end
  463. end
  464. build_host_command(8'h29);
  465. send_built_frame();
  466. repeat (2) @(posedge clk);
  467. #1;
  468. if (!request_valid || request_type !== held_request_type ||
  469. request_length !== held_request_length ||
  470. request_payload !== held_request_payload ||
  471. request_crc16 !== held_request_crc16) begin
  472. $display("ordinary overflow replaced the held request");
  473. $fatal;
  474. end
  475. check_held_request = 1'b0;
  476. request_ready = 1'b1;
  477. @(posedge clk);
  478. #1;
  479. if (request_valid || !rx_ready) begin
  480. $display("request handshake did not retire the held slot");
  481. $fatal;
  482. end
  483. build_host_command(8'h26);
  484. expected_payload = 272'd0;
  485. expected_payload[7:0] = 8'h26;
  486. send_built_frame();
  487. expect_request(HOST_CMD, 6'd4, expected_payload);
  488. $display("PASS: prod2u_p4_normal_v3_rx_tb");
  489. $finish;
  490. end
  491. endmodule