frame_tx_len1_tb.v 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. `timescale 1ns/1ps
  2. // frame_tx with LENGTH_BYTES=1: single LENGTH byte on the wire; the CRC
  3. // still covers TYPE, LENGTH and PAYLOAD. Expected bytes below are the
  4. // final 8-bit protocol frames from the independent Python codec
  5. // (tools/crc16_ccitt_table.frame_bytes), hand-embedded.
  6. module frame_tx_len1_tb;
  7. reg clk = 1'b0;
  8. reg rst_n = 1'b0;
  9. reg start = 1'b0;
  10. reg [7:0] frame_type = 8'h82;
  11. reg [7:0] payload_length = 8'd27;
  12. wire busy;
  13. wire byte_valid;
  14. wire [7:0] byte_data;
  15. reg byte_ready = 1'b1;
  16. wire [15:0] payload_addr;
  17. // Two-byte NACK payload ROM stand-in: addr0 -> request TYPE 01,
  18. // addr1 -> reason 04; every other address reads FF.
  19. wire [7:0] payload_data = (payload_addr == 16'd0) ? 8'h01 :
  20. (payload_addr == 16'd1) ? 8'h04 : 8'hFF;
  21. wire done;
  22. // Independent TX CRC model: bit-serial like the production
  23. // crc16_serial (not its code, but the same shift/XOR definition),
  24. // stepped only while crc_data_valid_o is high and pulsed done one
  25. // cycle after the eighth bit. crc_busy_i mirrors the model so the
  26. // DUT's WAIT states pace bytes exactly like the real path.
  27. wire crc_start_o, crc_data_valid_o;
  28. wire [7:0] crc_data_o;
  29. reg crc_busy_r = 1'b0;
  30. reg crc_done_pulse_r = 1'b0;
  31. reg [15:0] crc_model_value_r = 16'hFFFF;
  32. reg [7:0] crc_shift = 8'd0;
  33. reg [3:0] crc_bit = 4'd0;
  34. wire crc_done_pulse = crc_done_pulse_r;
  35. wire [15:0] crc_model_value = crc_model_value_r;
  36. wire crc_busy_model = crc_busy_r;
  37. integer count;
  38. reg [7:0] captured [0:63];
  39. always #1 clk = ~clk;
  40. frame_tx #(.PAYLOAD_IDX_BITS(8), .EXTERNAL_CRC(1), .LENGTH_BYTES(1)) dut (
  41. .clk(clk), .rst_n(rst_n), .start(start), .frame_type(frame_type),
  42. .payload_length({8'd0, payload_length}), .busy(busy), .byte_valid(byte_valid),
  43. .byte_data(byte_data), .byte_ready(byte_ready), .payload_addr(payload_addr),
  44. .payload_data(payload_data), .done(done),
  45. .crc_start_o(crc_start_o), .crc_data_valid_o(crc_data_valid_o), .crc_data_o(crc_data_o),
  46. .crc_busy_i(crc_busy_model), .crc_data_done_i(crc_done_pulse), .crc_value_i(crc_model_value)
  47. );
  48. always @(posedge clk or negedge rst_n) begin
  49. if (!rst_n) begin
  50. crc_model_value_r <= 16'hFFFF;
  51. crc_busy_r <= 1'b0;
  52. crc_done_pulse_r <= 1'b0;
  53. crc_shift <= 8'd0;
  54. crc_bit <= 4'd0;
  55. end else begin
  56. crc_done_pulse_r <= 1'b0;
  57. if (crc_start_o) begin
  58. crc_model_value_r <= 16'hFFFF;
  59. crc_busy_r <= 1'b0;
  60. end else if (!crc_busy_r) begin
  61. if (crc_data_valid_o) begin
  62. crc_shift <= crc_data_o;
  63. crc_bit <= 4'd0;
  64. crc_busy_r <= 1'b1;
  65. end
  66. end else begin
  67. // MSB-first bit select over the latched byte.
  68. crc_model_value_r <= (crc_model_value_r[15] ^ crc_shift[7 - crc_bit[2:0]]) ?
  69. ({crc_model_value_r[14:0], 1'b0} ^ 16'h1021) :
  70. {crc_model_value_r[14:0], 1'b0};
  71. if (crc_bit == 4'd7) begin
  72. crc_bit <= 4'd0;
  73. crc_busy_r <= 1'b0;
  74. crc_done_pulse_r <= 1'b1;
  75. end else begin
  76. crc_bit <= crc_bit + 1'b1;
  77. end
  78. end
  79. end
  80. end
  81. // Sample on the rising edge like a real downstream ready consumer:
  82. // byte_valid/byte_data are launched by the DUT on this edge.
  83. always @(posedge clk) begin
  84. if (byte_valid && byte_ready) begin
  85. captured[count] = byte_data;
  86. count = count + 1;
  87. end
  88. end
  89. task run_frame;
  90. input [7:0] ftype;
  91. input [7:0] flen;
  92. begin
  93. count = 0;
  94. frame_type = ftype;
  95. payload_length = flen;
  96. @(negedge clk);
  97. start = 1'b1;
  98. @(negedge clk);
  99. start = 1'b0;
  100. wait (done);
  101. @(negedge clk);
  102. end
  103. endtask
  104. initial begin
  105. count = 0;
  106. repeat (3) @(posedge clk);
  107. rst_n = 1'b1;
  108. repeat (2) @(posedge clk);
  109. // GET_ACK with a 27-byte payload sourced from the shared ROM
  110. // (addr0 -> 01, addr1 -> 04, rest FF): the independently computed
  111. // wire image is A5 5A 82 1B 01 04 FF..FF 54 B9.
  112. run_frame(8'h82, 8'd27);
  113. if (count !== 33) $fatal(1, "TX byte count mismatch: %0d", count);
  114. if (captured[0] !== 8'hA5 || captured[1] !== 8'h5A ||
  115. captured[2] !== 8'h82 || captured[3] !== 8'h1B)
  116. $fatal(1, "TX header mismatch %h %h %h %h",
  117. captured[0], captured[1], captured[2], captured[3]);
  118. if (captured[4] !== 8'h01 || captured[5] !== 8'h04 || captured[30] !== 8'hFF)
  119. $fatal(1, "TX payload source mismatch %h %h %h",
  120. captured[4], captured[5], captured[30]);
  121. if (captured[31] !== 8'h54 || captured[32] !== 8'hB9)
  122. $fatal(1, "TX CRC mismatch: %h %h", captured[31], captured[32]);
  123. // SET_ACK, zero length: A5 5A 81 00 A6 35 (golden vector).
  124. run_frame(8'h81, 8'd0);
  125. if (count !== 6) $fatal(1, "SET_ACK byte count mismatch: %0d", count);
  126. if (captured[0] !== 8'hA5 || captured[1] !== 8'h5A ||
  127. captured[2] !== 8'h81 || captured[3] !== 8'h00)
  128. $fatal(1, "SET_ACK header mismatch");
  129. if (captured[4] !== 8'hA6 || captured[5] !== 8'h35)
  130. $fatal(1, "SET_ACK CRC mismatch: %h %h", captured[4], captured[5]);
  131. // Queue-full NACK: A5 5A C0 02 01 04 B1 2A (golden vector).
  132. run_frame(8'hC0, 8'd2);
  133. if (count !== 8) $fatal(1, "NACK byte count mismatch: %0d", count);
  134. if (captured[0] !== 8'hA5 || captured[1] !== 8'h5A ||
  135. captured[2] !== 8'hC0 || captured[3] !== 8'h02)
  136. $fatal(1, "NACK header mismatch");
  137. if (captured[4] !== 8'h01 || captured[5] !== 8'h04)
  138. $fatal(1, "NACK payload mismatch: %h %h", captured[4], captured[5]);
  139. if (captured[6] !== 8'hB1 || captured[7] !== 8'h2A)
  140. $fatal(1, "NACK CRC mismatch: %h %h", captured[6], captured[7]);
  141. $display("frame_tx_len1_tb PASS");
  142. $finish;
  143. end
  144. initial begin
  145. #200000;
  146. $fatal(1, "TX timeout state=%0d busy=%b valid=%b count=%0d",
  147. dut.state, busy, byte_valid, count);
  148. end
  149. endmodule