host_uart_endpoint_nominal_tb.v 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. `timescale 1ns/1ps
  2. // Independent, wire-level endpoint regression. The sender and monitor use
  3. // exactly 1e9 / BAUD ns, not the DUT's integer divider. A 25 MHz clock is
  4. // supplied to the real production endpoint, UARTs, frame codecs and CRCs.
  5. // No DUT state is forced or used to calculate expected bytes or completion.
  6. module host_uart_endpoint_nominal_case #(
  7. parameter integer BAUD = 921600
  8. ) (output reg done);
  9. localparam integer DUT_DIVIDER = (25000000 + BAUD/2) / BAUD;
  10. localparam real BIT_NS = 1000000000.0 / BAUD;
  11. reg clk = 0;
  12. always #20 clk = ~clk;
  13. reg rst_n = 0;
  14. reg rs232_rx = 1, ttl_rx = 1;
  15. wire rs232_tx, ttl_tx;
  16. wire req_valid;
  17. reg req_ready = 0;
  18. wire [7:0] req_type, req_length;
  19. wire req_wr_en;
  20. wire [7:0] req_wr_addr, req_wr_data, req_rd_addr;
  21. reg [7:0] req_rd_data;
  22. reg tx_start = 0;
  23. reg [7:0] tx_type = 0, tx_length = 0;
  24. wire tx_busy, tx_done;
  25. wire [7:0] tx_rd_addr;
  26. reg [7:0] tx_rd_data;
  27. reg [7:0] req_mem [0:255];
  28. reg [7:0] tx_mem [0:255];
  29. reg [7:0] request_payload [0:127];
  30. reg [7:0] expected_tx [0:255];
  31. integer expected_tx_count = 0;
  32. integer captured_tx_count = 0;
  33. integer tx_done_count = 0;
  34. integer next_done_count = 0;
  35. integer accepted_count = 0;
  36. integer reply_count = 0;
  37. reg checking_tx = 0;
  38. reg previous_tx_done = 0;
  39. realtime measured_bit_ns = 0.0;
  40. realtime final_stop_end_ns = 0.0;
  41. realtime byte_start_ns;
  42. reg [7:0] captured_byte;
  43. integer bit_number;
  44. integer i, axis;
  45. host_uart_endpoint #(
  46. .CLKS_PER_BIT(DUT_DIVIDER),
  47. .INTER_BYTE_TIMEOUT_CYCLES(250000),
  48. .MAX_PAYLOAD(128), .ADDR_WIDTH(8)
  49. ) dut (
  50. .clk(clk), .rst_n(rst_n),
  51. .rs232_rx(rs232_rx), .rs232_tx(rs232_tx),
  52. .ttl_rx(ttl_rx), .ttl_tx(ttl_tx),
  53. .req_valid(req_valid), .req_ready(req_ready),
  54. .req_type(req_type), .req_length(req_length),
  55. .req_ram_wr_en(req_wr_en), .req_ram_wr_addr(req_wr_addr),
  56. .req_ram_wr_data(req_wr_data),
  57. .req_ram_rd_addr(req_rd_addr), .req_ram_rd_data(req_rd_data),
  58. .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length),
  59. .tx_busy(tx_busy), .tx_done(tx_done),
  60. .tx_ram_rd_addr(tx_rd_addr), .tx_ram_rd_data(tx_rd_data)
  61. );
  62. // Real one-cycle synchronous memory semantics, including undefined
  63. // contents before a write. The test never assumes power-on RAM zeros.
  64. always @(posedge clk) begin
  65. if (req_wr_en) req_mem[req_wr_addr] <= req_wr_data;
  66. req_rd_data <= req_mem[req_rd_addr];
  67. tx_rd_data <= tx_mem[tx_rd_addr];
  68. if (rst_n && req_valid && req_ready)
  69. accepted_count = accepted_count + 1;
  70. end
  71. function [15:0] reference_crc_byte;
  72. input [15:0] prior;
  73. input [7:0] value;
  74. reg [15:0] result;
  75. integer b;
  76. begin
  77. result = prior ^ {value, 8'h00};
  78. for (b = 0; b < 8; b = b + 1)
  79. result = result[15] ? ((result << 1) ^ 16'h1021)
  80. : (result << 1);
  81. reference_crc_byte = result;
  82. end
  83. endfunction
  84. task automatic set_rx;
  85. input integer port_number;
  86. input value;
  87. begin
  88. if (port_number == 0) rs232_rx = value;
  89. else ttl_rx = value;
  90. end
  91. endtask
  92. task automatic send_byte;
  93. input integer port_number;
  94. input [7:0] value;
  95. integer b;
  96. begin
  97. set_rx(port_number, 0);
  98. #(BIT_NS);
  99. for (b = 0; b < 8; b = b + 1) begin
  100. set_rx(port_number, value[b]);
  101. #(BIT_NS);
  102. end
  103. set_rx(port_number, 1);
  104. #(BIT_NS);
  105. end
  106. endtask
  107. task automatic send_request;
  108. input integer port_number;
  109. input [7:0] kind;
  110. input integer count;
  111. reg [15:0] crc;
  112. integer p;
  113. begin
  114. crc = reference_crc_byte(16'hffff, kind);
  115. crc = reference_crc_byte(crc, count[7:0]);
  116. send_byte(port_number, 8'ha5);
  117. send_byte(port_number, 8'h5a);
  118. send_byte(port_number, kind);
  119. send_byte(port_number, count[7:0]);
  120. for (p = 0; p < count; p = p + 1) begin
  121. send_byte(port_number, request_payload[p]);
  122. crc = reference_crc_byte(crc, request_payload[p]);
  123. end
  124. send_byte(port_number, crc[7:0]);
  125. send_byte(port_number, crc[15:8]);
  126. end
  127. endtask
  128. task automatic accept_request;
  129. input [7:0] kind;
  130. input integer count;
  131. integer p, wait_cycles;
  132. begin
  133. wait_cycles = 0;
  134. while (!req_valid && wait_cycles < 1000) begin
  135. @(negedge clk);
  136. wait_cycles = wait_cycles + 1;
  137. end
  138. if (!req_valid || req_type !== kind || req_length !== count[7:0])
  139. $fatal(1, "baud=%0d bad request valid=%b type=%h length=%0d expected=%h/%0d",
  140. BAUD, req_valid, req_type, req_length, kind, count);
  141. for (p = 0; p < count; p = p + 1)
  142. if (req_mem[p] !== request_payload[p])
  143. $fatal(1, "baud=%0d request payload[%0d]=%h expected=%h",
  144. BAUD, p, req_mem[p], request_payload[p]);
  145. @(negedge clk);
  146. req_ready = 1;
  147. @(negedge clk);
  148. req_ready = 0;
  149. repeat (5) @(negedge clk);
  150. if (req_valid) $fatal(1, "baud=%0d duplicate request publication", BAUD);
  151. end
  152. endtask
  153. task automatic begin_reply;
  154. input [7:0] kind;
  155. input integer count;
  156. reg [15:0] crc;
  157. integer p;
  158. begin
  159. if (tx_busy || checking_tx)
  160. $fatal(1, "baud=%0d test attempted overlapping reply frames", BAUD);
  161. expected_tx[0] = 8'ha5;
  162. expected_tx[1] = 8'h5a;
  163. expected_tx[2] = kind;
  164. expected_tx[3] = count[7:0];
  165. crc = reference_crc_byte(16'hffff, kind);
  166. crc = reference_crc_byte(crc, count[7:0]);
  167. for (p = 0; p < count; p = p + 1) begin
  168. expected_tx[4+p] = tx_mem[p];
  169. crc = reference_crc_byte(crc, tx_mem[p]);
  170. end
  171. expected_tx[4+count] = crc[7:0];
  172. expected_tx[5+count] = crc[15:8];
  173. // Published protocol golden values are independent sanity
  174. // anchors for the reference implementation itself.
  175. if (kind == 8'h82 && count == 0 && crc !== 16'h60f5)
  176. $fatal(1, "GET_ACK golden CRC mismatch in test reference");
  177. if (kind == 8'h81 && count == 0 && crc !== 16'h35a6)
  178. $fatal(1, "SET_ACK golden CRC mismatch in test reference");
  179. expected_tx_count = count + 6;
  180. captured_tx_count = 0;
  181. final_stop_end_ns = 0.0;
  182. measured_bit_ns = 0.0;
  183. next_done_count = tx_done_count + 1;
  184. checking_tx = 1;
  185. reply_count = reply_count + 1;
  186. @(negedge clk);
  187. tx_type = kind;
  188. tx_length = count[7:0];
  189. tx_start = 1;
  190. @(negedge clk);
  191. tx_start = 0;
  192. end
  193. endtask
  194. task automatic finish_reply;
  195. integer wait_cycles;
  196. begin
  197. wait_cycles = 0;
  198. while (tx_done_count < next_done_count && wait_cycles < 300000) begin
  199. @(negedge clk);
  200. wait_cycles = wait_cycles + 1;
  201. end
  202. if (tx_done_count !== next_done_count ||
  203. captured_tx_count !== expected_tx_count || tx_busy)
  204. $fatal(1, "baud=%0d incomplete reply done=%0d target=%0d bytes=%0d/%0d busy=%b",
  205. BAUD, tx_done_count, next_done_count,
  206. captured_tx_count, expected_tx_count, tx_busy);
  207. checking_tx = 0;
  208. repeat (5) @(negedge clk);
  209. end
  210. endtask
  211. // Independent UART decoder. It samples at nominal receiver baud.
  212. // Only for the physical-stop completion assertion, measure the start
  213. // bit directly from the first 0xa5 byte (data bit 0 is high). This
  214. // permits integer-divider baud error without assuming its bit length.
  215. initial begin
  216. forever begin
  217. @(negedge rs232_tx);
  218. if (rst_n) begin
  219. if (!checking_tx || captured_tx_count >= expected_tx_count)
  220. $fatal(1, "baud=%0d unexpected TX byte", BAUD);
  221. byte_start_ns = $realtime;
  222. if (captured_tx_count == 0) begin
  223. @(posedge rs232_tx);
  224. measured_bit_ns = $realtime - byte_start_ns;
  225. if (measured_bit_ns < BIT_NS * 0.98 ||
  226. measured_bit_ns > BIT_NS * 1.02)
  227. $fatal(1, "baud=%0d TX bit duration %0.3f ns outside nominal tolerance", BAUD, measured_bit_ns);
  228. #(1.5 * BIT_NS - measured_bit_ns);
  229. end else begin
  230. #(1.5 * BIT_NS);
  231. end
  232. for (bit_number = 0; bit_number < 8; bit_number = bit_number + 1) begin
  233. captured_byte[bit_number] = rs232_tx;
  234. #(BIT_NS);
  235. end
  236. if (rs232_tx !== 1'b1)
  237. $fatal(1, "baud=%0d TX missing stop bit, byte=%0d", BAUD, captured_tx_count);
  238. if (captured_byte !== expected_tx[captured_tx_count])
  239. $fatal(1, "baud=%0d TX byte[%0d]=%h expected=%h",
  240. BAUD, captured_tx_count, captured_byte, expected_tx[captured_tx_count]);
  241. captured_tx_count = captured_tx_count + 1;
  242. if (captured_tx_count == expected_tx_count)
  243. final_stop_end_ns = byte_start_ns + 10.0 * measured_bit_ns;
  244. end
  245. end
  246. end
  247. // Observe after NBA updates, so the completion pulse and pins belong
  248. // to the same edge. One measured full stop bit must have elapsed.
  249. always @(posedge clk) begin
  250. #1;
  251. if (rst_n) begin
  252. if (rs232_tx !== ttl_tx)
  253. $fatal(1, "baud=%0d RS232/TTL broadcast diverged", BAUD);
  254. if (tx_done) begin
  255. if (previous_tx_done || !checking_tx ||
  256. captured_tx_count != expected_tx_count || final_stop_end_ns <= 0.0)
  257. $fatal(1, "baud=%0d premature/duplicate tx_done", BAUD);
  258. if ($realtime + 0.001 < final_stop_end_ns)
  259. $fatal(1, "baud=%0d tx_done precedes full final stop: now=%0.3f deadline=%0.3f",
  260. BAUD, $realtime, final_stop_end_ns);
  261. if ($realtime > final_stop_end_ns + 161.0)
  262. $fatal(1, "baud=%0d completion delayed more than four clocks", BAUD);
  263. if (tx_busy || rs232_tx !== 1'b1)
  264. $fatal(1, "baud=%0d tx_done while wire/serializer busy", BAUD);
  265. tx_done_count = tx_done_count + 1;
  266. end
  267. previous_tx_done = tx_done;
  268. end else begin
  269. previous_tx_done = 0;
  270. end
  271. end
  272. always @(rs232_tx or ttl_tx) begin
  273. #0.001;
  274. if (rst_n && rs232_tx !== ttl_tx)
  275. $fatal(1, "baud=%0d TX broadcast differs between clock samples", BAUD);
  276. end
  277. initial begin
  278. done = 0;
  279. if (reference_crc_byte(reference_crc_byte(16'hffff, 8'h02), 8'h00) !== 16'h7b6d)
  280. $fatal(1, "GET request golden CRC mismatch in test reference");
  281. repeat (10) @(negedge clk);
  282. rst_n = 1;
  283. #(2.37 * BIT_NS); // unrelated phase to the 25 MHz clock
  284. // RS232 GET; full GET_ACK followed by a real-length 83-byte DATA.
  285. send_request(0, 8'h02, 0);
  286. accept_request(8'h02, 0);
  287. begin_reply(8'h82, 0);
  288. finish_reply;
  289. for (i = 0; i < 83; i = i + 1)
  290. tx_mem[i] = (i * 29 + 37) & 255;
  291. tx_mem[32] = 0;
  292. tx_mem[55] = 8'hff;
  293. tx_mem[82] = 8'hff;
  294. for (i = 0; i < 32; i = i + 1)
  295. request_payload[i] = (i * 11 + 3) % 101;
  296. begin_reply(8'h42, 83);
  297. // RX and TX CRC streams intentionally overlap. RX uses nominal
  298. // wire timing and a different payload from the TX synchronous RAM.
  299. #(3.23 * BIT_NS);
  300. send_request(1, 8'h01, 32);
  301. accept_request(8'h01, 32);
  302. finish_reply;
  303. begin_reply(8'h81, 0);
  304. finish_reply;
  305. // RS232 full 68-byte MRDIMM SET, with valid finite axis slots and
  306. // embedded A5 5A in a step count. The endpoint must preserve it.
  307. for (i = 0; i < 8; i = i + 1)
  308. request_payload[i] = (i * 13 + 7) % 101;
  309. for (axis = 0; axis < 12; axis = axis + 1) begin
  310. request_payload[8 + axis*5] = (axis % 2) ? 8'h02 : 8'h01;
  311. request_payload[9 + axis*5] = 8'ha5 - axis;
  312. request_payload[10 + axis*5] = 8'h5a;
  313. request_payload[11 + axis*5] = 20 + axis;
  314. request_payload[12 + axis*5] = 0;
  315. end
  316. #(1.19 * BIT_NS);
  317. send_request(0, 8'h01, 68);
  318. accept_request(8'h01, 68);
  319. begin_reply(8'h81, 0);
  320. finish_reply;
  321. send_request(1, 8'h02, 0);
  322. accept_request(8'h02, 0);
  323. begin_reply(8'h82, 0);
  324. finish_reply;
  325. // A lone sync byte must time out in WAIT_5A and release ownership.
  326. // The next complete request comes from the other physical port.
  327. // Use the actual production 250000-clock (10 ms) gap, with only a
  328. // short external guard; no parser/owner state is inspected or forced.
  329. send_byte(0, 8'ha5);
  330. repeat (250500) @(negedge clk);
  331. send_request(1, 8'h02, 0);
  332. accept_request(8'h02, 0);
  333. begin_reply(8'h82, 0);
  334. finish_reply;
  335. send_byte(1, 8'ha5);
  336. repeat (250500) @(negedge clk);
  337. send_request(0, 8'h02, 0);
  338. accept_request(8'h02, 0);
  339. begin_reply(8'h82, 0);
  340. finish_reply;
  341. #(30 * BIT_NS);
  342. if (accepted_count != 6 || tx_done_count != 7 || reply_count != 7)
  343. $fatal(1, "baud=%0d wrong totals requests=%0d completed=%0d replies=%0d",
  344. BAUD, accepted_count, tx_done_count, reply_count);
  345. if (req_valid || tx_busy || tx_done || rs232_tx !== 1 || ttl_tx !== 1)
  346. $fatal(1, "baud=%0d endpoint did not return idle", BAUD);
  347. $display("PASS nominal UART baud=%0d divider=%0d RX=RS232+TTL requests=6 replies=7 full_stop_done=7 broadcast=identical synchronous_RAM=checked concurrent_CRC=checked half_A5_timeout=RS232_to_TTL+TTL_to_RS232",
  348. BAUD, DUT_DIVIDER);
  349. done = 1;
  350. end
  351. endmodule
  352. module host_uart_endpoint_nominal_tb;
  353. wire [3:0] done;
  354. host_uart_endpoint_nominal_case #(.BAUD(921600)) t921600(.done(done[0]));
  355. host_uart_endpoint_nominal_case #(.BAUD(460800)) t460800(.done(done[1]));
  356. host_uart_endpoint_nominal_case #(.BAUD(230400)) t230400(.done(done[2]));
  357. host_uart_endpoint_nominal_case #(.BAUD(115200)) t115200(.done(done[3]));
  358. initial begin
  359. wait (&done);
  360. $display("PASS host_uart_endpoint_nominal_tb: four nominal baud rates, both RX ports, full final stop, independent CRC and synchronous RAM");
  361. $finish;
  362. end
  363. initial begin
  364. #100000000;
  365. $fatal(1, "host_uart_endpoint_nominal_tb timeout");
  366. end
  367. endmodule