frame_rx_tb.v 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Legacy length 0x0E must be rejected under MAX_PAYLOAD=11.
  94. push(8'hA5); push(8'h5A); push(8'h01); push(8'h0E);
  95. for (i = 0; i < 14; i = i + 1) push(8'h00);
  96. push(8'h00); push(8'h00);
  97. repeat (4) @(posedge clk);
  98. if (frame_valid) $fatal(1, "legacy 14-byte SET_ALL published");
  99. accept_enable = 1'b0;
  100. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h6D); push(8'h7B);
  101. repeat (4) @(posedge clk);
  102. if (frame_valid) $fatal(1, "busy input was accepted");
  103. $display("frame_rx_tb PASS");
  104. $finish;
  105. end
  106. endmodule