frame_rx_tb.v 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 [15:0] payload_read_addr = 16'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. frame_rx #(.MAX_PAYLOAD(32), .INTER_BYTE_TIMEOUT_CYCLES(16)) dut (
  21. .clk(clk), .rst_n(rst_n), .byte_valid(byte_valid), .byte_data(byte_data),
  22. .accept_enable(accept_enable), .frame_valid(frame_valid), .frame_ready(frame_ready),
  23. .frame_type(frame_type), .frame_length(frame_length),
  24. .payload_read_addr(payload_read_addr), .payload_read_data(payload_read_data),
  25. .payload_wr_en(payload_wr_en), .payload_wr_addr(payload_wr_addr), .payload_wr_data(payload_wr_data)
  26. );
  27. payload_ram #(.DEPTH(32), .ADDR_WIDTH(8)) payload_store (
  28. .clk(clk), .wr_en(payload_wr_en), .wr_addr(payload_wr_addr), .wr_data(payload_wr_data),
  29. .rd_addr(payload_read_addr), .rd_data(payload_read_data)
  30. );
  31. task push;
  32. input [7:0] value;
  33. begin
  34. @(negedge clk);
  35. byte_data = value;
  36. byte_valid = 1'b1;
  37. @(negedge clk);
  38. byte_valid = 1'b0;
  39. // Give the serial CRC time to absorb the byte before the next
  40. // one arrives (matches production 921600 byte spacing).
  41. repeat (10) @(posedge clk);
  42. $display("push %02h t=%0t", value, $time);
  43. end
  44. endtask
  45. task ack_frame;
  46. begin
  47. frame_ready = 1'b1;
  48. @(posedge clk);
  49. frame_ready = 1'b0;
  50. end
  51. endtask
  52. task expect_frame;
  53. input [7:0] etype;
  54. input [15:0] elen;
  55. begin
  56. repeat (4) @(posedge clk);
  57. $display("expect t=%0t valid=%b type=%h len=%0d state=%0d idx=%0d crc=%h",
  58. $time, frame_valid, frame_type, frame_length, dut.state, dut.payload_index, dut.crc_value);
  59. if (!frame_valid) $fatal(1, "valid frame missing");
  60. if (frame_type !== etype || frame_length !== elen)
  61. $fatal(1, "frame metadata mismatch type=%h len=%0d", frame_type, frame_length);
  62. end
  63. endtask
  64. initial begin
  65. repeat (3) @(posedge clk);
  66. rst_n = 1'b1;
  67. // Fixed contract vector: GET_ALL A5 5A 02 00 00 FC A2.
  68. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h00);
  69. push(8'hFC); push(8'hA2);
  70. expect_frame(8'h02, 16'd0);
  71. ack_frame();
  72. // Bad CRC (contract vector with corrupted CRC low byte) stays silent.
  73. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h00);
  74. push(8'hFC); push(8'hA3);
  75. repeat (4) @(posedge clk);
  76. if (frame_valid) $fatal(1, "bad CRC published");
  77. // SET_ALL, 32 zero bytes (16-bit length header 20 00, CRC 04 5D).
  78. push(8'hA5); push(8'h5A); push(8'h01); push(8'h20); push(8'h00);
  79. for (i = 0; i < 32; i = i + 1) push(8'h00);
  80. push(8'h04); push(8'h5D);
  81. expect_frame(8'h01, 16'd32);
  82. for (i = 0; i < 32; i = i + 1) begin
  83. payload_read_addr = i[15:0];
  84. @(posedge clk);
  85. @(negedge clk);
  86. if (payload_read_data !== 8'h00)
  87. $fatal(1, "SET_ALL payload[%0d]=%h", i, payload_read_data);
  88. end
  89. ack_frame();
  90. // Mixed values: 0A 14 1E 28 / 00 19 32 64 / 01 02 00 + zeros.
  91. push(8'hA5); push(8'h5A); push(8'h01); push(8'h20); push(8'h00);
  92. push(8'h0A); push(8'h14); push(8'h1E); push(8'h28);
  93. push(8'h00); push(8'h19); push(8'h32); push(8'h64);
  94. push(8'h01); push(8'h02); push(8'h00);
  95. for (i = 11; i < 32; i = i + 1) push(8'h00);
  96. push(8'h40); push(8'h33);
  97. expect_frame(8'h01, 16'd32);
  98. ack_frame();
  99. // A complete oversized frame (declared 34 bytes over a 32-byte RAM)
  100. // remains one trusted request: consumed in full, CRC checked, with
  101. // embedded A5 5A unable to resync. CRC 42 3B from the independent
  102. // Python codec.
  103. push(8'hA5); push(8'h5A); push(8'h01); push(8'h22); push(8'h00);
  104. for (i = 0; i < 34; i = i + 1) begin
  105. if (i == 3) push(8'hA5);
  106. else if (i == 4) push(8'h5A);
  107. else push(i[7:0]);
  108. end
  109. push(8'h42); push(8'h3B);
  110. expect_frame(8'h01, 16'd34);
  111. ack_frame();
  112. // The same complete oversized request with a bad CRC is silent and
  113. // the next real frame still parses (length extent is trusted).
  114. push(8'hA5); push(8'h5A); push(8'h01); push(8'h22); push(8'h00);
  115. for (i = 0; i < 34; i = i + 1) begin
  116. if (i == 3) push(8'hA5);
  117. else if (i == 4) push(8'h5A);
  118. else push(i[7:0]);
  119. end
  120. push(8'h42); push(8'h3C);
  121. repeat (4) @(posedge clk);
  122. if (frame_valid) $fatal(1, "bad-CRC oversized frame published");
  123. push(8'hA5); push(8'h5A); push(8'h05); push(8'h00); push(8'h00);
  124. push(8'h6C); push(8'h27);
  125. expect_frame(8'h05, 16'd0);
  126. ack_frame();
  127. accept_enable = 1'b0;
  128. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h00);
  129. push(8'hFC); push(8'hA2);
  130. repeat (4) @(posedge clk);
  131. if (frame_valid) $fatal(1, "busy input was accepted");
  132. $display("frame_rx_tb PASS");
  133. $finish;
  134. end
  135. endmodule