frame_rx_tb.v 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 [7: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 [7:0] payload_wr_addr;
  16. wire [7:0] payload_wr_data;
  17. integer i;
  18. always #1 clk = ~clk;
  19. // Production boundary uses 11-byte SET_ALL max payload.
  20. frame_rx #(.MAX_PAYLOAD(11), .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(11), .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. repeat (10) @(posedge clk);
  40. end
  41. endtask
  42. task ack_frame;
  43. begin
  44. frame_ready = 1'b1;
  45. @(posedge clk);
  46. frame_ready = 1'b0;
  47. end
  48. endtask
  49. task get_frame;
  50. begin
  51. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h6D); push(8'h7B);
  52. repeat (4) @(posedge clk);
  53. if (!frame_valid) $fatal(1, "valid GET frame missing");
  54. if (frame_type !== 8'h02 || frame_length !== 8'd0) $fatal(1, "frame metadata mismatch");
  55. ack_frame();
  56. end
  57. endtask
  58. initial begin
  59. repeat (3) @(posedge clk);
  60. rst_n = 1'b1;
  61. get_frame();
  62. // Bad CRC must not publish (atomicity of frame accept).
  63. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h6D); push(8'h7A);
  64. repeat (4) @(posedge clk);
  65. if (frame_valid) $fatal(1, "bad CRC published");
  66. // Golden SET_ALL all-zero 11-byte vector.
  67. // A55A010B00000000000000000000005873
  68. push(8'hA5); push(8'h5A); push(8'h01); push(8'h0B);
  69. for (i = 0; i < 11; i = i + 1) push(8'h00);
  70. push(8'h58); push(8'h73);
  71. repeat (4) @(posedge clk);
  72. if (!frame_valid) $fatal(1, "golden SET_ALL not published");
  73. if (frame_type !== 8'h01 || frame_length !== 8'd11)
  74. $fatal(1, "SET_ALL metadata mismatch type=%h len=%0d", frame_type, frame_length);
  75. for (i = 0; i < 11; i = i + 1) begin
  76. payload_read_addr = i[7:0];
  77. @(posedge clk);
  78. @(negedge clk);
  79. if (payload_read_data !== 8'h00)
  80. $fatal(1, "SET_ALL payload[%0d]=%h", i, payload_read_data);
  81. end
  82. ack_frame();
  83. // Mixed golden: A55A010B0A141E280019326401020087D5
  84. push(8'hA5); push(8'h5A); push(8'h01); push(8'h0B);
  85. push(8'h0A); push(8'h14); push(8'h1E); push(8'h28);
  86. push(8'h00); push(8'h19); push(8'h32); push(8'h64);
  87. push(8'h01); push(8'h02); push(8'h00);
  88. push(8'h87); push(8'hD5);
  89. repeat (4) @(posedge clk);
  90. if (!frame_valid) $fatal(1, "mixed golden SET_ALL not published");
  91. if (frame_length !== 8'd11) $fatal(1, "mixed golden length");
  92. ack_frame();
  93. // A complete oversized frame remains one trusted request. Only the
  94. // first MAX_PAYLOAD bytes enter RAM; embedded A5 5A cannot resync it.
  95. // CRC covers TYPE=01, LENGTH=34 and the complete generated payload.
  96. push(8'hA5); push(8'h5A); push(8'h01); push(8'h22);
  97. for (i = 0; i < 34; i = i + 1) begin
  98. if (i == 3) push(8'hA5);
  99. else if (i == 4) push(8'h5A);
  100. else push(i[7:0]);
  101. end
  102. push(8'hEC); push(8'h69);
  103. repeat (4) @(posedge clk);
  104. if (!frame_valid) $fatal(1, "trusted oversized frame metadata missing");
  105. if (frame_type !== 8'h01 || frame_length !== 8'd34)
  106. $fatal(1, "oversized metadata mismatch type=%h len=%0d", frame_type, frame_length);
  107. ack_frame();
  108. // The same complete oversized request with a bad CRC is silent.
  109. push(8'hA5); push(8'h5A); push(8'h01); push(8'h22);
  110. for (i = 0; i < 34; i = i + 1) begin
  111. if (i == 3) push(8'hA5);
  112. else if (i == 4) push(8'h5A);
  113. else push(i[7:0]);
  114. end
  115. push(8'hED); push(8'h69);
  116. repeat (4) @(posedge clk);
  117. if (frame_valid) $fatal(1, "bad-CRC oversized frame published");
  118. accept_enable = 1'b0;
  119. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h6D); push(8'h7B);
  120. repeat (4) @(posedge clk);
  121. if (frame_valid) $fatal(1, "busy input was accepted");
  122. $display("frame_rx_tb PASS");
  123. $finish;
  124. end
  125. endmodule