uart_baud_candidates_tb.v 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. `timescale 1ns/1ps
  2. module uart_baud_candidates_tb;
  3. reg clk = 0;
  4. always #20 clk = ~clk;
  5. reg rst_n = 0;
  6. reg rx_fixed = 1;
  7. reg rx_fractional = 1;
  8. wire valid_fixed;
  9. wire [7:0] data_fixed;
  10. wire error_fixed;
  11. wire valid_fractional;
  12. wire [7:0] data_fractional;
  13. wire error_fractional;
  14. reg tx_fixed_valid = 0;
  15. reg [7:0] tx_fixed_data = 0;
  16. wire tx_fixed_line;
  17. wire tx_fixed_busy;
  18. wire tx_fixed_done;
  19. reg tx_fractional_valid = 0;
  20. reg [7:0] tx_fractional_data = 0;
  21. wire tx_fractional_line;
  22. wire tx_fractional_busy;
  23. wire tx_fractional_done;
  24. wire loop_fixed_valid;
  25. wire [7:0] loop_fixed_data;
  26. wire loop_fractional_valid;
  27. wire [7:0] loop_fractional_data;
  28. reg [7:0] expected_fixed [0:255];
  29. reg [7:0] expected_fractional [0:255];
  30. reg [7:0] expected_loop_fixed [0:31];
  31. reg [7:0] expected_loop_fractional [0:31];
  32. integer expected_fixed_count = 0;
  33. integer expected_fractional_count = 0;
  34. integer expected_loop_fixed_count = 0;
  35. integer expected_loop_fractional_count = 0;
  36. integer got_fixed_count = 0;
  37. integer got_fractional_count = 0;
  38. integer got_loop_fixed_count = 0;
  39. integer got_loop_fractional_count = 0;
  40. integer fixed_errors = 0;
  41. integer fractional_errors = 0;
  42. integer i;
  43. integer cycles;
  44. uart_rx_byte #(.CLKS_PER_BIT(27)) u_fixed_rx (
  45. .clk(clk), .rst_n(rst_n), .rx_i(rx_fixed),
  46. .byte_valid(valid_fixed), .byte_data(data_fixed), .framing_error(error_fixed)
  47. );
  48. uart_rx_byte_fractional u_fractional_rx (
  49. .clk(clk), .rst_n(rst_n), .rx_i(rx_fractional),
  50. .byte_valid(valid_fractional), .byte_data(data_fractional), .framing_error(error_fractional)
  51. );
  52. uart_tx_byte #(.CLKS_PER_BIT(27)) u_fixed_tx (
  53. .clk(clk), .rst_n(rst_n), .byte_valid(tx_fixed_valid), .byte_data(tx_fixed_data),
  54. .tx_o(tx_fixed_line), .busy(tx_fixed_busy), .byte_done(tx_fixed_done)
  55. );
  56. uart_tx_byte_fractional u_fractional_tx (
  57. .clk(clk), .rst_n(rst_n), .byte_valid(tx_fractional_valid), .byte_data(tx_fractional_data),
  58. .tx_o(tx_fractional_line), .busy(tx_fractional_busy), .byte_done(tx_fractional_done)
  59. );
  60. uart_rx_byte #(.CLKS_PER_BIT(27)) u_fixed_loop_rx (
  61. .clk(clk), .rst_n(rst_n), .rx_i(tx_fixed_line),
  62. .byte_valid(loop_fixed_valid), .byte_data(loop_fixed_data), .framing_error()
  63. );
  64. uart_rx_byte_fractional u_fractional_loop_rx (
  65. .clk(clk), .rst_n(rst_n), .rx_i(tx_fractional_line),
  66. .byte_valid(loop_fractional_valid), .byte_data(loop_fractional_data), .framing_error()
  67. );
  68. always @(posedge clk) begin
  69. if (valid_fixed) begin
  70. if (got_fixed_count >= expected_fixed_count || data_fixed !== expected_fixed[got_fixed_count]) begin
  71. $display("fixed RX mismatch index=%0d data=%02x", got_fixed_count, data_fixed);
  72. $fatal(1);
  73. end
  74. got_fixed_count = got_fixed_count + 1;
  75. end
  76. if (valid_fractional) begin
  77. if (got_fractional_count >= expected_fractional_count || data_fractional !== expected_fractional[got_fractional_count]) begin
  78. $display("fractional RX mismatch index=%0d data=%02x", got_fractional_count, data_fractional);
  79. $fatal(1);
  80. end
  81. got_fractional_count = got_fractional_count + 1;
  82. end
  83. if (error_fixed) fixed_errors = fixed_errors + 1;
  84. if (error_fractional) fractional_errors = fractional_errors + 1;
  85. if (loop_fixed_valid) begin
  86. if (loop_fixed_data !== expected_loop_fixed[got_loop_fixed_count]) $fatal(1, "fixed TX loopback mismatch");
  87. got_loop_fixed_count = got_loop_fixed_count + 1;
  88. end
  89. if (loop_fractional_valid) begin
  90. if (loop_fractional_data !== expected_loop_fractional[got_loop_fractional_count]) $fatal(1, "fractional TX loopback mismatch");
  91. got_loop_fractional_count = got_loop_fractional_count + 1;
  92. end
  93. end
  94. task hold_clocks;
  95. input integer count;
  96. integer n;
  97. begin
  98. for (n = 0; n < count; n = n + 1) @(posedge clk);
  99. end
  100. endtask
  101. task send_fixed_peer;
  102. input [7:0] value;
  103. input integer nominal;
  104. input integer jitter;
  105. input integer tag;
  106. integer bit_number;
  107. integer period;
  108. begin
  109. expected_fixed[expected_fixed_count] = value;
  110. expected_fixed_count = expected_fixed_count + 1;
  111. rx_fixed = 0;
  112. period = nominal + ((((tag + 0) & 1) != 0) ? jitter : -jitter);
  113. hold_clocks(period);
  114. for (bit_number = 0; bit_number < 8; bit_number = bit_number + 1) begin
  115. rx_fixed = value[bit_number];
  116. period = nominal + ((((tag + bit_number + 1) & 1) != 0) ? jitter : -jitter);
  117. hold_clocks(period);
  118. end
  119. rx_fixed = 1;
  120. period = nominal + ((((tag + 9) & 1) != 0) ? jitter : -jitter);
  121. hold_clocks(period);
  122. end
  123. endtask
  124. task send_fractional_peer;
  125. input [7:0] value;
  126. input integer nominal;
  127. input integer jitter;
  128. input integer tag;
  129. integer bit_number;
  130. integer period;
  131. begin
  132. expected_fractional[expected_fractional_count] = value;
  133. expected_fractional_count = expected_fractional_count + 1;
  134. rx_fractional = 0;
  135. period = nominal + ((((tag + 0) & 1) != 0) ? jitter : -jitter);
  136. hold_clocks(period);
  137. for (bit_number = 0; bit_number < 8; bit_number = bit_number + 1) begin
  138. rx_fractional = value[bit_number];
  139. period = nominal + ((((tag + bit_number + 1) & 1) != 0) ? jitter : -jitter);
  140. hold_clocks(period);
  141. end
  142. rx_fractional = 1;
  143. period = nominal + ((((tag + 9) & 1) != 0) ? jitter : -jitter);
  144. hold_clocks(period);
  145. end
  146. endtask
  147. task transmit_fixed;
  148. input [7:0] value;
  149. begin
  150. expected_loop_fixed[expected_loop_fixed_count] = value;
  151. expected_loop_fixed_count = expected_loop_fixed_count + 1;
  152. @(negedge clk);
  153. tx_fixed_data = value;
  154. tx_fixed_valid = 1;
  155. @(negedge clk);
  156. tx_fixed_valid = 0;
  157. cycles = 0;
  158. while (tx_fixed_busy) begin
  159. @(negedge clk);
  160. cycles = cycles + 1;
  161. end
  162. if (cycles != 270) $fatal(1, "fixed TX duration %0d, expected 270", cycles);
  163. end
  164. endtask
  165. task transmit_fractional;
  166. input [7:0] value;
  167. begin
  168. expected_loop_fractional[expected_loop_fractional_count] = value;
  169. expected_loop_fractional_count = expected_loop_fractional_count + 1;
  170. @(negedge clk);
  171. tx_fractional_data = value;
  172. tx_fractional_valid = 1;
  173. @(negedge clk);
  174. tx_fractional_valid = 0;
  175. cycles = 0;
  176. while (tx_fractional_busy) begin
  177. @(negedge clk);
  178. cycles = cycles + 1;
  179. end
  180. if (cycles != 272) $fatal(1, "fractional TX duration %0d, expected 272", cycles);
  181. end
  182. endtask
  183. initial begin
  184. repeat (8) @(posedge clk);
  185. rst_n = 1;
  186. #7;
  187. for (i = 0; i < 16; i = i + 1) send_fixed_peer(8'h20 + i, 26, 0, i);
  188. for (i = 0; i < 16; i = i + 1) send_fixed_peer(8'h40 + i, 28, 0, i);
  189. for (i = 0; i < 64; i = i + 1) send_fixed_peer((i * 37) ^ 8'ha5, 27, 1, i);
  190. for (i = 0; i < 16; i = i + 1) send_fractional_peer(8'h60 + i, 26, 0, i);
  191. for (i = 0; i < 16; i = i + 1) send_fractional_peer(8'h80 + i, 28, 0, i);
  192. for (i = 0; i < 64; i = i + 1) send_fractional_peer((i * 53) ^ 8'h5a, 27, 1, i);
  193. hold_clocks(20);
  194. if (got_fixed_count != expected_fixed_count || got_fractional_count != expected_fractional_count) begin
  195. $fatal(1, "RX count mismatch fixed=%0d/%0d frac=%0d/%0d", got_fixed_count, expected_fixed_count, got_fractional_count, expected_fractional_count);
  196. end
  197. if (fixed_errors != 0 || fractional_errors != 0) $fatal(1, "unexpected framing errors");
  198. for (i = 0; i < 16; i = i + 1) begin
  199. transmit_fixed(8'h55 ^ i);
  200. transmit_fractional(8'haa ^ i);
  201. end
  202. hold_clocks(20);
  203. if (got_loop_fixed_count != expected_loop_fixed_count || got_loop_fractional_count != expected_loop_fractional_count) begin
  204. $fatal(1, "TX loopback count mismatch");
  205. end
  206. $display("PASS: uart_baud_candidates_tb fixed=27 fractional=27/28 peer=26..28 jitter=+/-1 long=64");
  207. $finish;
  208. end
  209. endmodule