prod2u_p4_temperature_short_tx_tb.v 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. `timescale 1ns/1ps
  2. module prod2u_p4_temperature_short_tx_tb;
  3. reg clk = 1'b0;
  4. reg rst_n = 1'b0;
  5. reg start = 1'b0;
  6. reg force_ff = 1'b0;
  7. wire rd_req;
  8. wire [4:0] rd_addr;
  9. reg rd_valid = 1'b0;
  10. reg [7:0] rd_data = 8'd0;
  11. wire tx_valid;
  12. reg tx_ready = 1'b1;
  13. wire [7:0] tx_data;
  14. wire busy;
  15. wire done;
  16. reg [7:0] memory [0:26];
  17. reg [7:0] captured [0:34];
  18. reg [15:0] expected_crc;
  19. reg [7:0] stalled_data;
  20. integer capture_count;
  21. integer read_count;
  22. integer index;
  23. integer wait_cycles;
  24. prod2u_p4_temperature_short_tx dut (
  25. .clk(clk),
  26. .rst_n(rst_n),
  27. .start(start),
  28. .force_ff(force_ff),
  29. .rd_req(rd_req),
  30. .rd_addr(rd_addr),
  31. .rd_valid(rd_valid),
  32. .rd_data(rd_data),
  33. .tx_valid(tx_valid),
  34. .tx_ready(tx_ready),
  35. .tx_data(tx_data),
  36. .busy(busy),
  37. .done(done)
  38. );
  39. always #5 clk = ~clk;
  40. always @(posedge clk) begin
  41. if (!rst_n) begin
  42. rd_valid <= 1'b0;
  43. rd_data <= 8'd0;
  44. read_count <= 0;
  45. end else begin
  46. rd_valid <= rd_req;
  47. if (rd_req) begin
  48. if (rd_addr > 5'd26 || rd_addr !== read_count[4:0]) begin
  49. $display(
  50. "STORE_READ_ORDER_MISMATCH count=%0d addr=%0d",
  51. read_count,
  52. rd_addr
  53. );
  54. $fatal;
  55. end
  56. rd_data <= memory[rd_addr];
  57. read_count <= read_count + 1;
  58. end
  59. end
  60. end
  61. function [15:0] crc16_byte;
  62. input [15:0] crc_in;
  63. input [7:0] value_in;
  64. reg [15:0] value;
  65. integer bit_index;
  66. begin
  67. value = crc_in ^ {value_in, 8'h00};
  68. for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
  69. if (value[15]) value = (value << 1) ^ 16'h1021;
  70. else value = value << 1;
  71. end
  72. crc16_byte = value;
  73. end
  74. endfunction
  75. task pulse_start;
  76. input next_force_ff;
  77. begin
  78. @(negedge clk);
  79. force_ff = next_force_ff;
  80. start = 1'b1;
  81. @(posedge clk);
  82. #1;
  83. start = 1'b0;
  84. end
  85. endtask
  86. task capture_frame;
  87. input expected_force_ff;
  88. begin
  89. capture_count = 0;
  90. wait_cycles = 0;
  91. while (tx_valid !== 1'b1 && wait_cycles < 100) begin
  92. @(posedge clk);
  93. #1;
  94. wait_cycles = wait_cycles + 1;
  95. end
  96. if (!tx_valid) begin
  97. $display("TX_VALID_TIMEOUT force_ff=%0d", expected_force_ff);
  98. $fatal;
  99. end
  100. @(negedge clk);
  101. tx_ready = 1'b0;
  102. stalled_data = tx_data;
  103. repeat (4) begin
  104. @(posedge clk);
  105. #1;
  106. if (!tx_valid || tx_data !== stalled_data) begin
  107. $display(
  108. "TX_CHANGED_UNDER_BACKPRESSURE valid=%0d data=%02x expected=%02x",
  109. tx_valid,
  110. tx_data,
  111. stalled_data
  112. );
  113. $fatal;
  114. end
  115. end
  116. @(negedge clk);
  117. tx_ready = 1'b1;
  118. wait_cycles = 0;
  119. while (capture_count < 35 && wait_cycles < 500) begin
  120. @(posedge clk);
  121. if (tx_valid && tx_ready) begin
  122. captured[capture_count] = tx_data;
  123. capture_count = capture_count + 1;
  124. end
  125. #1;
  126. wait_cycles = wait_cycles + 1;
  127. end
  128. if (capture_count !== 35) begin
  129. $display("TX_FRAME_TIMEOUT captured=%0d", capture_count);
  130. $fatal;
  131. end
  132. if (captured[0] !== 8'ha5 || captured[1] !== 8'h5a ||
  133. captured[2] !== 8'h02 || captured[3] !== 8'h04 ||
  134. captured[4] !== 8'h10 || captured[5] !== 8'h1b) begin
  135. $display("SHORT_HEADER_MISMATCH");
  136. $fatal;
  137. end
  138. for (index = 0; index < 27; index = index + 1) begin
  139. if (captured[6 + index] !==
  140. (expected_force_ff ? 8'hff : index[7:0])) begin
  141. $display(
  142. "SHORT_PAYLOAD_MISMATCH index=%0d data=%02x expected=%02x",
  143. index,
  144. captured[6 + index],
  145. (expected_force_ff ? 8'hff : index[7:0])
  146. );
  147. $fatal;
  148. end
  149. end
  150. expected_crc = 16'hffff;
  151. for (index = 2; index < 33; index = index + 1) begin
  152. expected_crc = crc16_byte(expected_crc, captured[index]);
  153. end
  154. if (captured[33] !== expected_crc[7:0] ||
  155. captured[34] !== expected_crc[15:8]) begin
  156. $display(
  157. "SHORT_CRC_MISMATCH got=%02x%02x expected_lo_hi=%02x%02x",
  158. captured[33],
  159. captured[34],
  160. expected_crc[7:0],
  161. expected_crc[15:8]
  162. );
  163. $fatal;
  164. end
  165. wait_cycles = 0;
  166. while (done !== 1'b1 && wait_cycles < 5) begin
  167. @(posedge clk);
  168. #1;
  169. wait_cycles = wait_cycles + 1;
  170. end
  171. if (!done || busy) begin
  172. $display("TX_DONE_MISMATCH done=%0d busy=%0d", done, busy);
  173. $fatal;
  174. end
  175. end
  176. endtask
  177. initial begin
  178. for (index = 0; index < 27; index = index + 1) begin
  179. memory[index] = index[7:0];
  180. end
  181. repeat (4) @(negedge clk);
  182. rst_n = 1'b1;
  183. pulse_start(1'b0);
  184. if (!busy) begin
  185. $display("TX_BUSY_NOT_ASSERTED_AFTER_START");
  186. $fatal;
  187. end
  188. capture_frame(1'b0);
  189. if (read_count !== 27) begin
  190. $display("STORE_READ_COUNT_MISMATCH count=%0d", read_count);
  191. $fatal;
  192. end
  193. if ({captured[34], captured[33]} !== 16'hc91f) begin
  194. $display("GOLDEN_CRC_MISMATCH crc=%02x%02x", captured[33], captured[34]);
  195. $fatal;
  196. end
  197. @(negedge clk);
  198. rst_n = 1'b0;
  199. repeat (2) @(negedge clk);
  200. rst_n = 1'b1;
  201. pulse_start(1'b1);
  202. capture_frame(1'b1);
  203. if (read_count !== 0) begin
  204. $display("FORCE_FF_READ_STORE count=%0d", read_count);
  205. $fatal;
  206. end
  207. if ({captured[34], captured[33]} !== 16'hc41b) begin
  208. $display("ALL_FF_CRC_MISMATCH crc=%02x%02x", captured[33], captured[34]);
  209. $fatal;
  210. end
  211. $display("PASS: prod2u_p4_temperature_short_tx_tb");
  212. $finish;
  213. end
  214. endmodule