frame_rx_len1_tb.v 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. `timescale 1ns/1ps
  2. // frame_rx with LENGTH_BYTES=1: single LENGTH byte, frame_length is
  3. // {8'h00, length_byte}, CRC covers TYPE then the single LENGTH byte.
  4. // Vectors below are the final 8-bit protocol frames from the independent
  5. // Python codec (tools/crc16_ccitt_table.frame_bytes), hand-embedded.
  6. module frame_rx_len1_tb;
  7. reg clk = 1'b0;
  8. reg rst_n = 1'b0;
  9. reg byte_valid = 1'b0;
  10. reg [7:0] byte_data = 8'd0;
  11. reg byte_error = 1'b0;
  12. reg accept_enable = 1'b1;
  13. reg frame_ready = 1'b0;
  14. wire frame_valid;
  15. wire [7:0] frame_type;
  16. wire [15:0] frame_length;
  17. reg [7:0] payload_read_addr_8 = 8'd0;
  18. wire [7:0] payload_read_data;
  19. wire payload_wr_en;
  20. wire [15:0] payload_wr_addr;
  21. wire [7:0] payload_wr_data;
  22. integer i;
  23. integer writes;
  24. // External-CRC handshake observation (RX accumulator is separate).
  25. wire crc_start_o, crc_data_valid_o;
  26. wire [7:0] crc_data_o;
  27. // crc_done_pulse follows one cycle behind the absorbed byte, like the
  28. // serial CRC's pipelined data_done, so the DUT observes a stable crc
  29. // accumulator for the state it is comparing against.
  30. reg crc_pending = 1'b0;
  31. reg crc_done_pulse_r = 1'b0;
  32. reg [15:0] crc_model_value_r = 16'hFFFF;
  33. wire crc_done_pulse = crc_done_pulse_r;
  34. wire [15:0] crc_model_value = crc_model_value_r;
  35. wire crc_absorb_now = crc_data_valid_o && !crc_start_o;
  36. always #1 clk = ~clk;
  37. frame_rx #(
  38. .MAX_PAYLOAD(32), .INTER_BYTE_TIMEOUT_CYCLES(32),
  39. .EXTERNAL_CRC(1), .UART_ERRORS_ENABLED(1), .LENGTH_BYTES(1)
  40. ) dut (
  41. .clk(clk), .rst_n(rst_n), .byte_valid(byte_valid), .byte_data(byte_data),
  42. .byte_error(byte_error),
  43. .accept_enable(accept_enable), .frame_valid(frame_valid), .frame_ready(frame_ready),
  44. .frame_type(frame_type), .frame_length(frame_length),
  45. .payload_read_addr({8'd0, payload_read_addr_8}), .payload_read_data(payload_read_data),
  46. .payload_wr_en(payload_wr_en), .payload_wr_addr(payload_wr_addr), .payload_wr_data(payload_wr_data),
  47. .crc_start_o(crc_start_o), .crc_data_valid_o(crc_data_valid_o), .crc_data_o(crc_data_o),
  48. .crc_busy_i(1'b0), .crc_data_done_i(crc_done_pulse), .crc_value_i(crc_model_value)
  49. );
  50. // Independent CRC-16/CCITT-FALSE model inside the testbench (nibble
  51. // formulation, not the DUT's bit-serial form): only fed from the
  52. // observed crc_data_valid_o/crc_data_o handshake.
  53. reg [15:0] crc_table [0:255];
  54. integer t_init, t_bit;
  55. initial begin
  56. for (t_init = 0; t_init < 256; t_init = t_init + 1) begin
  57. crc_table[t_init] = {t_init[7:0], 8'd0};
  58. for (t_bit = 0; t_bit < 8; t_bit = t_bit + 1)
  59. crc_table[t_init] = crc_table[t_init][15] ?
  60. (crc_table[t_init] << 1) ^ 16'h1021 : crc_table[t_init] << 1;
  61. end
  62. end
  63. always @(posedge clk or negedge rst_n) begin
  64. if (!rst_n) begin
  65. crc_model_value_r <= 16'hFFFF;
  66. crc_done_pulse_r <= 1'b0;
  67. crc_pending <= 1'b0;
  68. end else begin
  69. crc_done_pulse_r <= crc_pending && !crc_absorb_now;
  70. crc_pending <= 1'b0;
  71. if (crc_start_o)
  72. crc_model_value_r <= 16'hFFFF;
  73. else if (crc_data_valid_o) begin
  74. // Table-driven MSB-first CRC-16/CCITT-FALSE step over one
  75. // byte: index = old_crc[15:8] ^ byte, then shift out the
  76. // consumed top byte and fold the table word in.
  77. crc_model_value_r <= {crc_model_value_r[7:0], 8'd0} ^
  78. crc_table[(crc_model_value_r[15:8] ^ crc_data_o)];
  79. crc_pending <= 1'b1;
  80. end
  81. end
  82. end
  83. payload_ram #(.DEPTH(32), .ADDR_WIDTH(8)) payload_store (
  84. .clk(clk), .wr_en(payload_wr_en), .wr_addr(payload_wr_addr[7:0]),
  85. .wr_data(payload_wr_data),
  86. .rd_addr(payload_read_addr_8), .rd_data(payload_read_data)
  87. );
  88. // Sample on the falling edge: payload_wr_en is a registered pulse
  89. // that is stable through the whole low half-cycle.
  90. always @(negedge clk) if (payload_wr_en) begin
  91. writes = writes + 1;
  92. if (payload_wr_addr >= 32) $fatal(1, "request RAM overflow at %0d", payload_wr_addr);
  93. end
  94. task push;
  95. input [7:0] value;
  96. begin
  97. @(negedge clk);
  98. byte_data = value;
  99. byte_valid = 1'b1;
  100. byte_error = 1'b0;
  101. @(negedge clk);
  102. byte_valid = 1'b0;
  103. repeat (10) @(posedge clk);
  104. end
  105. endtask
  106. task push_err;
  107. begin
  108. @(negedge clk);
  109. byte_data = 8'h00;
  110. byte_valid = 1'b0;
  111. byte_error = 1'b1;
  112. @(negedge clk);
  113. byte_error = 1'b0;
  114. repeat (10) @(posedge clk);
  115. end
  116. endtask
  117. task ack_frame;
  118. begin
  119. frame_ready = 1'b1;
  120. @(posedge clk);
  121. @(negedge clk);
  122. frame_ready = 1'b0;
  123. end
  124. endtask
  125. // expect_frame checks metadata, samples the running write counter
  126. // for the payload assertions, then accepts the frame. The write
  127. // counter is reset at the start of the *next* case instead of here so
  128. // an assertion failure cannot hide which run produced the count.
  129. integer writes_at_accept;
  130. task expect_frame;
  131. input [7:0] etype;
  132. input [15:0] elen;
  133. begin
  134. repeat (4) @(posedge clk);
  135. if (!frame_valid) $fatal(1, "valid frame missing");
  136. if (frame_type !== etype || frame_length !== elen)
  137. $fatal(1, "frame metadata mismatch type=%h len=%0d", frame_type, frame_length);
  138. writes_at_accept = writes;
  139. end
  140. endtask
  141. task expect_silent;
  142. begin
  143. repeat (4) @(posedge clk);
  144. if (frame_valid) $fatal(1, "corrupt frame published");
  145. end
  146. endtask
  147. initial begin
  148. writes = 0;
  149. repeat (3) @(posedge clk);
  150. rst_n = 1'b1;
  151. // GET_ALL, 8-bit length: A5 5A 02 00 6D 7B (golden vector).
  152. writes = 0;
  153. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  154. push(8'h6D); push(8'h7B);
  155. expect_frame(8'h02, 16'd0);
  156. ack_frame();
  157. // Bad CRC (corrupted CRC high byte) stays silent.
  158. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  159. push(8'h6D); push(8'h7C);
  160. expect_silent();
  161. // A later legal frame still parses (recovery).
  162. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  163. push(8'h6D); push(8'h7B);
  164. expect_frame(8'h02, 16'd0);
  165. ack_frame();
  166. // SET_ALL 32 zero bytes: A5 5A 01 20 + 32x00 + 0D 8C.
  167. writes = 0;
  168. push(8'hA5); push(8'h5A); push(8'h01); push(8'h20);
  169. for (i = 0; i < 32; i = i + 1) push(8'h00);
  170. push(8'h0D); push(8'h8C);
  171. expect_frame(8'h01, 16'd32);
  172. if (writes_at_accept !== 32) $fatal(1, "payload writes=%0d", writes_at_accept);
  173. for (i = 0; i < 32; i = i + 1) begin
  174. payload_read_addr_8 = i[7:0];
  175. @(posedge clk);
  176. @(negedge clk);
  177. if (payload_read_data !== 8'h00)
  178. $fatal(1, "SET_ALL payload[%0d]=%h", i, payload_read_data);
  179. end
  180. ack_frame();
  181. // Mixed 11-byte payload (0A 14 1E 28 00 19 32 64 01 02 00 + zeros,
  182. // CRC 49 E2).
  183. writes = 0;
  184. push(8'hA5); push(8'h5A); push(8'h01); push(8'h20);
  185. push(8'h0A); push(8'h14); push(8'h1E); push(8'h28);
  186. push(8'h00); push(8'h19); push(8'h32); push(8'h64);
  187. push(8'h01); push(8'h02); push(8'h00);
  188. for (i = 11; i < 32; i = i + 1) push(8'h00);
  189. push(8'h49); push(8'hE2);
  190. expect_frame(8'h01, 16'd32);
  191. if (writes_at_accept !== 32) $fatal(1, "payload writes=%0d", writes_at_accept);
  192. ack_frame();
  193. // A declared length beyond the 32-byte RAM (34) is trusted and
  194. // consumed in full: CRC checked over all 34 payload bytes, embedded
  195. // A5 5A (bytes 3,4) unable to resync; CRC EC 69.
  196. writes = 0;
  197. push(8'hA5); push(8'h5A); push(8'h01); push(8'h22);
  198. push(8'h00); push(8'h01); push(8'h02);
  199. push(8'hA5); push(8'h5A);
  200. for (i = 5; i < 34; i = i + 1) push(i[7:0]);
  201. push(8'hEC); push(8'h69);
  202. expect_frame(8'h01, 16'd34);
  203. if (writes_at_accept !== 32) $fatal(1, "payload writes=%0d", writes_at_accept);
  204. ack_frame();
  205. // Same oversized request with corrupted CRC is silent, then the
  206. // next real frame still parses.
  207. push(8'hA5); push(8'h5A); push(8'h01); push(8'h22);
  208. push(8'h00); push(8'h01); push(8'h02);
  209. push(8'hA5); push(8'h5A);
  210. for (i = 5; i < 34; i = i + 1) push(i[7:0]);
  211. push(8'hEC); push(8'h96);
  212. expect_silent();
  213. push(8'hA5); push(8'h5A); push(8'h05); push(8'h00);
  214. push(8'hFA); push(8'hE2);
  215. expect_frame(8'h05, 16'd0);
  216. ack_frame();
  217. // A UART error byte anywhere in the frame poisons it.
  218. push(8'hA5); push(8'h5A); push(8'h01); push(8'h20);
  219. push(8'h00); push(8'h00); push_err();
  220. for (i = 3; i < 32; i = i + 1) push(8'h00);
  221. push(8'h0D); push(8'h8C);
  222. expect_silent();
  223. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  224. push(8'h6D); push(8'h7B);
  225. expect_frame(8'h02, 16'd0);
  226. ack_frame();
  227. // A half frame is abandoned only after the inter-byte timeout;
  228. // the parser then returns to hunting and parses the next frame.
  229. push(8'hA5); push(8'h5A); push(8'h01);
  230. repeat (64) @(posedge clk);
  231. if (frame_valid) $fatal(1, "truncated frame published");
  232. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  233. push(8'h6D); push(8'h7B);
  234. expect_frame(8'h02, 16'd0);
  235. ack_frame();
  236. // While accept is withdrawn the parser drops everything.
  237. accept_enable = 1'b0;
  238. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  239. push(8'h6D); push(8'h7B);
  240. repeat (4) @(posedge clk);
  241. if (frame_valid) $fatal(1, "busy input was accepted");
  242. accept_enable = 1'b1;
  243. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  244. push(8'h6D); push(8'h7B);
  245. expect_frame(8'h02, 16'd0);
  246. ack_frame();
  247. $display("frame_rx_len1_tb PASS");
  248. $finish;
  249. end
  250. initial begin
  251. #5000000;
  252. $fatal(1, "frame_rx_len1 deadline");
  253. end
  254. endmodule