frame_rx_tb.v 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. `timescale 1ns/1ps
  2. module frame_rx_tb;
  3. reg clk = 1'b0;
  4. reg rst_n = 1'b0;
  5. reg byte_valid = 1'b0;
  6. reg [7:0] byte_data = 8'd0;
  7. reg accept_enable = 1'b1;
  8. reg frame_ready = 1'b0;
  9. wire frame_valid;
  10. wire [7:0] frame_type;
  11. wire [15:0] frame_length;
  12. reg [7:0] payload_read_addr = 8'd0;
  13. wire [7:0] payload_read_data;
  14. wire payload_wr_en;
  15. wire [15:0] payload_wr_addr;
  16. wire [7:0] payload_wr_data;
  17. integer i;
  18. always #1 clk = ~clk;
  19. // 6400 SET_ALL is 32 bytes; the request RAM keeps that production size.
  20. // Final 8-bit protocol: single LENGTH byte on the wire.
  21. frame_rx #(.MAX_PAYLOAD(32), .INTER_BYTE_TIMEOUT_CYCLES(16), .LENGTH_BYTES(1)) dut (
  22. .clk(clk), .rst_n(rst_n), .byte_valid(byte_valid), .byte_data(byte_data),
  23. .byte_error(1'b0),
  24. .accept_enable(accept_enable), .frame_valid(frame_valid), .frame_ready(frame_ready),
  25. .frame_type(frame_type), .frame_length(frame_length),
  26. .payload_read_addr({8'd0, payload_read_addr}), .payload_read_data(payload_read_data),
  27. .payload_wr_en(payload_wr_en), .payload_wr_addr(payload_wr_addr), .payload_wr_data(payload_wr_data),
  28. .crc_start_o(), .crc_data_valid_o(), .crc_data_o(),
  29. .crc_busy_i(1'b0), .crc_data_done_i(1'b0), .crc_value_i(16'd0)
  30. );
  31. reg [7:0] ram [0:255];
  32. always @(posedge clk) begin
  33. if (payload_wr_en) ram[payload_wr_addr[7:0]] <= payload_wr_data;
  34. end
  35. assign payload_read_data = ram[payload_read_addr[7:0]];
  36. task push;
  37. input [7:0] value;
  38. begin
  39. @(negedge clk);
  40. byte_data = value;
  41. byte_valid = 1'b1;
  42. @(negedge clk);
  43. byte_valid = 1'b0;
  44. // Give the serial CRC time to absorb the byte before the next
  45. // one arrives (matches production 921600 byte spacing).
  46. repeat (10) @(posedge clk);
  47. $display("push %02h t=%0t", value, $time);
  48. end
  49. endtask
  50. task ack_frame;
  51. begin
  52. frame_ready = 1'b1;
  53. @(posedge clk);
  54. frame_ready = 1'b0;
  55. end
  56. endtask
  57. task expect_frame;
  58. input [7:0] etype;
  59. input [15:0] elen;
  60. begin
  61. repeat (4) @(posedge clk);
  62. $display("expect t=%0t valid=%b type=%h len=%0d state=%0d idx=%0d crc=%h",
  63. $time, frame_valid, frame_type, frame_length, dut.state, dut.payload_index, dut.crc_value);
  64. if (!frame_valid) $fatal(1, "valid frame missing");
  65. if (frame_type !== etype || frame_length !== elen)
  66. $fatal(1, "frame metadata mismatch type=%h len=%0d", frame_type, frame_length);
  67. end
  68. endtask
  69. initial begin
  70. repeat (3) @(posedge clk);
  71. rst_n = 1'b1;
  72. // Fixed contract vector: GET_ALL A5 5A 02 00 6D 7B.
  73. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  74. push(8'h6D); push(8'h7B);
  75. expect_frame(8'h02, 16'd0);
  76. ack_frame();
  77. // Bad CRC (contract vector with corrupted CRC low byte) stays silent.
  78. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  79. push(8'h6C); push(8'h7B);
  80. repeat (4) @(posedge clk);
  81. if (frame_valid) $fatal(1, "bad CRC published");
  82. // SET_ALL, 32 zero bytes (8-bit length header 20, CRC 0D 8C).
  83. push(8'hA5); push(8'h5A); push(8'h01); push(8'h20);
  84. for (i = 0; i < 32; i = i + 1) push(8'h00);
  85. push(8'h0D); push(8'h8C);
  86. expect_frame(8'h01, 16'd32);
  87. for (i = 0; i < 32; i = i + 1) begin
  88. payload_read_addr = i[7:0];
  89. @(posedge clk);
  90. @(negedge clk);
  91. if (payload_read_data !== 8'h00)
  92. $fatal(1, "SET_ALL payload[%0d]=%h", i, payload_read_data);
  93. end
  94. ack_frame();
  95. // Mixed values: 0A 14 1E 28 / 00 19 32 64 / 01 02 00 + zeros (CRC 49 E2).
  96. push(8'hA5); push(8'h5A); push(8'h01); push(8'h20);
  97. push(8'h0A); push(8'h14); push(8'h1E); push(8'h28);
  98. push(8'h00); push(8'h19); push(8'h32); push(8'h64);
  99. push(8'h01); push(8'h02); push(8'h00);
  100. for (i = 11; i < 32; i = i + 1) push(8'h00);
  101. push(8'h49); push(8'hE2);
  102. expect_frame(8'h01, 16'd32);
  103. ack_frame();
  104. // A complete oversized frame (declared 34 bytes over a 32-byte RAM)
  105. // remains one trusted request: consumed in full, CRC checked, with
  106. // embedded A5 5A unable to resync. CRC value 0x69EC from the
  107. // independent Python codec; wire CRC is u16le, so EC then 69.
  108. push(8'hA5); push(8'h5A); push(8'h01); push(8'h22);
  109. for (i = 0; i < 34; i = i + 1) begin
  110. if (i == 3) push(8'hA5);
  111. else if (i == 4) push(8'h5A);
  112. else push(i[7:0]);
  113. end
  114. push(8'hEC); push(8'h69);
  115. expect_frame(8'h01, 16'd34);
  116. ack_frame();
  117. // The same complete oversized request with a bad CRC is silent and
  118. // the next real frame still parses (length extent is trusted).
  119. push(8'hA5); push(8'h5A); push(8'h01); push(8'h22);
  120. for (i = 0; i < 34; i = i + 1) begin
  121. if (i == 3) push(8'hA5);
  122. else if (i == 4) push(8'h5A);
  123. else push(i[7:0]);
  124. end
  125. push(8'hEC); push(8'h96);
  126. repeat (4) @(posedge clk);
  127. if (frame_valid) $fatal(1, "bad-CRC oversized frame published");
  128. push(8'hA5); push(8'h5A); push(8'h05); push(8'h00);
  129. push(8'hFA); push(8'hE2);
  130. expect_frame(8'h05, 16'd0);
  131. ack_frame();
  132. accept_enable = 1'b0;
  133. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
  134. push(8'h6D); push(8'h7B);
  135. repeat (4) @(posedge clk);
  136. if (frame_valid) $fatal(1, "busy input was accepted");
  137. $display("frame_rx_tb PASS");
  138. $finish;
  139. end
  140. endmodule