frame_tx_uart_tx_tb.v 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. `timescale 1ns/1ps
  2. // frame_tx + uart_tx_byte at real bit timing (CLKS_PER_BIT=27 for 921600
  3. // baud at 25 MHz): a frame_tx with LENGTH_BYTES=1 streams final 8-bit
  4. // protocol frames into uart_tx_byte; every byte on the wire is decoded at
  5. // bit level and checked against the independent Python codec vectors
  6. // (tools/crc16_ccitt_table.frame_bytes), hand-embedded below.
  7. //
  8. // A second, deliberately wrong-period uart_rx_byte observes the same wire
  9. // and must NOT reproduce the image (proving the bit-level check is live),
  10. // while a nominal-period receiver proves the image end to end.
  11. module frame_tx_uart_tx_tb;
  12. localparam integer CLKS = 27;
  13. reg sys_clk = 1'b0;
  14. reg rst_n = 1'b0;
  15. reg start = 1'b0;
  16. reg [7:0] frame_type = 8'h82;
  17. reg [7:0] payload_length = 8'd27;
  18. wire busy;
  19. wire byte_valid;
  20. wire [7:0] byte_data;
  21. wire byte_ready;
  22. wire [15:0] payload_addr;
  23. // Payload ROM stand-in: addr0 -> 01, addr1 -> 04 (NACK shape), else FF.
  24. wire [7:0] payload_data = (payload_addr == 16'd0) ? 8'h01 :
  25. (payload_addr == 16'd1) ? 8'h04 : 8'hFF;
  26. wire done;
  27. wire tx_line;
  28. wire tx_busy;
  29. wire tx_byte_done;
  30. always #20 sys_clk = ~sys_clk; // 25 MHz
  31. frame_tx #(.PAYLOAD_IDX_BITS(8), .LENGTH_BYTES(1)) u_frame_tx (
  32. .clk(sys_clk), .rst_n(rst_n), .start(start), .frame_type(frame_type),
  33. .payload_length({8'd0, payload_length}), .busy(busy),
  34. .byte_valid(byte_valid), .byte_data(byte_data),
  35. .byte_ready(byte_ready), .payload_addr(payload_addr),
  36. .payload_data(payload_data), .done(done),
  37. .crc_start_o(), .crc_data_valid_o(), .crc_data_o(),
  38. .crc_busy_i(1'b0), .crc_data_done_i(1'b0), .crc_value_i(16'd0)
  39. );
  40. uart_tx_byte #(.CLKS_PER_BIT(CLKS)) u_uart_tx (
  41. .clk(sys_clk), .rst_n(rst_n),
  42. .byte_valid(byte_valid), .byte_data(byte_data),
  43. .tx_o(tx_line), .busy(tx_busy), .byte_done(tx_byte_done)
  44. );
  45. // Level handshake (same shape as the production endpoint): the frame
  46. // source may advance whenever the line driver is idle. A pulse
  47. // handshake on tx_byte_done races the producer/consumer and repeats
  48. // bytes (observed here 2026-09-20: byte 0 sent twice).
  49. assign byte_ready = !tx_busy;
  50. // Loop the wire back into two receivers: nominal period (must decode
  51. // the exact image) and wrong period (must NOT).
  52. wire rx_ok_valid;
  53. wire [7:0] rx_ok_data;
  54. wire rx_ok_err;
  55. uart_rx_byte #(.CLKS_PER_BIT(CLKS)) u_rx_ok (
  56. .clk(sys_clk), .rst_n(rst_n), .rx_i(tx_line),
  57. .byte_valid(rx_ok_valid), .byte_data(rx_ok_data),
  58. .framing_error(rx_ok_err)
  59. );
  60. wire rx_bad_valid;
  61. wire [7:0] rx_bad_data;
  62. wire rx_bad_err;
  63. uart_rx_byte #(.CLKS_PER_BIT(CLKS + 2)) u_rx_bad (
  64. .clk(sys_clk), .rst_n(rst_n), .rx_i(tx_line),
  65. .byte_valid(rx_bad_valid), .byte_data(rx_bad_data),
  66. .framing_error(rx_bad_err)
  67. );
  68. reg [7:0] ok_stream [0:63];
  69. reg [7:0] bad_stream [0:63];
  70. integer ok_count;
  71. integer bad_count;
  72. initial begin
  73. ok_count = 0;
  74. bad_count = 0;
  75. end
  76. always @(posedge sys_clk) begin
  77. if (rx_ok_valid) begin
  78. ok_stream[ok_count] <= rx_ok_data;
  79. ok_count <= ok_count + 1;
  80. end
  81. if (rx_bad_valid) begin
  82. bad_stream[bad_count] <= rx_bad_data;
  83. bad_count <= bad_count + 1;
  84. end
  85. end
  86. // Bit-level UART decoder for the wire image: one full bit period after
  87. // the start edge lands mid-d0 (the negedge fires up to one clock after
  88. // the fall and the transmitter holds every bit for a full CLKS
  89. // clocks); stepping CLKS per bit stays mid-bit.
  90. reg [7:0] decode_shift;
  91. integer decode_bit;
  92. task decode_byte;
  93. begin
  94. @(negedge tx_line);
  95. #(CLKS * 40);
  96. for (decode_bit = 0; decode_bit < 8; decode_bit = decode_bit + 1) begin
  97. #1080;
  98. decode_shift[decode_bit] = tx_line;
  99. end
  100. #1080;
  101. if (tx_line !== 1'b1) $fatal(1, "UART stop bit is low");
  102. end
  103. endtask
  104. task launch_start;
  105. input [7:0] ftype;
  106. input [7:0] flen;
  107. begin
  108. frame_type = ftype;
  109. payload_length = flen;
  110. @(negedge sys_clk);
  111. start = 1'b1;
  112. @(negedge sys_clk);
  113. start = 1'b0;
  114. end
  115. endtask
  116. reg [7:0] cap00, cap01, cap02, cap03, cap04, cap05, cap30, cap31, cap32;
  117. // GET_ACK, 27-byte payload (addr0 -> 01, addr1 -> 04, rest FF):
  118. // independently computed wire image A5 5A 82 1B 01 04 FF..FF 54 B9.
  119. task get_ack_frame;
  120. begin
  121. launch_start(8'h82, 8'd27);
  122. decode_byte; cap00 = decode_shift;
  123. decode_byte; cap01 = decode_shift;
  124. decode_byte; cap02 = decode_shift;
  125. decode_byte; cap03 = decode_shift;
  126. decode_byte; cap04 = decode_shift;
  127. decode_byte; cap05 = decode_shift;
  128. begin
  129. integer k;
  130. for (k = 0; k < 25; k = k + 1) begin
  131. if (k == 24) cap30 = decode_shift;
  132. decode_byte;
  133. end
  134. end
  135. decode_byte; cap31 = decode_shift;
  136. decode_byte; cap32 = decode_shift;
  137. end
  138. endtask
  139. initial begin
  140. repeat (4) @(posedge sys_clk);
  141. rst_n = 1'b1;
  142. repeat (4) @(posedge sys_clk);
  143. // ---- GET_ACK with a 27-byte payload ----
  144. get_ack_frame;
  145. if (cap00 !== 8'hA5 || cap01 !== 8'h5A ||
  146. cap02 !== 8'h82 || cap03 !== 8'h1B) begin
  147. $fatal(1, "UART frame header mismatch: %h %h %h %h",
  148. cap00, cap01, cap02, cap03);
  149. end
  150. if (cap04 !== 8'h01 || cap05 !== 8'h04 || cap30 !== 8'hFF) begin
  151. $fatal(1, "UART payload mismatch: %h %h %h", cap04, cap05, cap30);
  152. end
  153. if (cap31 !== 8'h54 || cap32 !== 8'hB9) begin
  154. $fatal(1, "UART CRC mismatch: %h %h", cap31, cap32);
  155. end
  156. // ---- SET_ACK, zero length: golden A5 5A 81 00 A6 35 ----
  157. launch_start(8'h81, 8'd0);
  158. decode_byte; cap00 = decode_shift;
  159. decode_byte; cap01 = decode_shift;
  160. decode_byte; cap02 = decode_shift;
  161. decode_byte; cap03 = decode_shift;
  162. decode_byte; cap04 = decode_shift;
  163. decode_byte; cap05 = decode_shift;
  164. if (cap00 !== 8'hA5 || cap01 !== 8'h5A ||
  165. cap02 !== 8'h81 || cap03 !== 8'h00 ||
  166. cap04 !== 8'hA6 || cap05 !== 8'h35) begin
  167. $fatal(1, "SET_ACK wire mismatch: %h %h %h %h %h %h",
  168. cap00, cap01, cap02, cap03, cap04, cap05);
  169. end
  170. // The wire idles high after the last stop bit: no 7th byte.
  171. repeat (CLKS * 12) begin
  172. @(posedge sys_clk);
  173. if (tx_line !== 1'b1) $fatal(1, "unexpected 7th UART byte");
  174. end
  175. // ---- loopback cross-checks (both receivers ran live above) ----
  176. $display("t=%0t loopback nominal=%0d wrong=%0d", $time, ok_count, bad_count);
  177. if (ok_count !== 39)
  178. $fatal(1, "nominal loopback byte count %0d, want 39", ok_count);
  179. if (ok_stream[0] !== 8'hA5 || ok_stream[1] !== 8'h5A ||
  180. ok_stream[2] !== 8'h82 || ok_stream[3] !== 8'h1B)
  181. $fatal(1, "nominal loopback header mismatch: %h %h %h %h",
  182. ok_stream[0], ok_stream[1], ok_stream[2], ok_stream[3]);
  183. if (ok_stream[33] !== 8'hA5 || ok_stream[34] !== 8'h5A ||
  184. ok_stream[35] !== 8'h81 || ok_stream[36] !== 8'h00 ||
  185. ok_stream[37] !== 8'hA6 || ok_stream[38] !== 8'h35)
  186. $fatal(1, "nominal loopback SET_ACK mismatch: %h %h %h %h %h %h",
  187. ok_stream[33], ok_stream[34], ok_stream[35],
  188. ok_stream[36], ok_stream[37], ok_stream[38]);
  189. // The wrong-period receiver must not reproduce the OK stream.
  190. if (bad_count === 39) begin
  191. if (bad_stream[0] === ok_stream[0] && bad_stream[1] === ok_stream[1] &&
  192. bad_stream[2] === ok_stream[2] && bad_stream[3] === ok_stream[3] &&
  193. bad_stream[33] === ok_stream[33] && bad_stream[38] === ok_stream[38])
  194. $fatal(1, "wrong-period receiver reproduced the image");
  195. end
  196. $display("frame_tx_uart_tx_tb PASS");
  197. $finish;
  198. end
  199. initial begin
  200. #100000000;
  201. $fatal(1, "UART frame timeout");
  202. end
  203. endmodule