host_uart_endpoint_tb.v 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. `timescale 1ns/1ps
  2. // Extended endpoint regression using integer 27-clock UART timing.
  3. // The nominal four-rate test independently checks baud and complete final stop.
  4. // This older fixture uses combinational request/reply TB RAM.
  5. // host_uart_endpoint testbench: drives UART bit timing (CLKS_PER_BIT=27
  6. // for 921600 baud at 25 MHz, plus one wrong-bit-period case and jittered
  7. // cases) into both RX lines and decodes both TX lines at the bit level.
  8. // All frame bytes below are the final 8-bit protocol images from the
  9. // independent Python codec (tools/crc16_ccitt_table.frame_bytes),
  10. // hand-embedded (see the vector table in the header comment).
  11. //
  12. // Vector table (hex on the wire):
  13. // SET32 zero : A5 5A 01 20 + 32x00 + 0D 8C
  14. // SET32 hot5 : A5 5A 01 20 + 00*5 64 00*26 + 32 5C
  15. // GET : A5 5A 02 00 6D 7B
  16. // GET payload0: (empty)
  17. // SETACK : A5 5A 81 00 A6 35
  18. // NACK QFULL : A5 5A C0 02 01 04 B1 2A
  19. // OVERSIZE 200: A5 5A 01 C8 + 200 payload bytes (00..C7 with A5 5A at
  20. // offsets 3,4) + 8F E1
  21. // EMBED6566 : payload bytes 3,4 of the oversize frame are A5 5A
  22. module host_uart_endpoint_tb;
  23. localparam integer CLKS = 27;
  24. localparam integer TB_TIMEOUT_CYCLES = 250000;
  25. reg clk = 1'b0;
  26. reg rst_n = 1'b0;
  27. reg rs232_rx = 1'b1;
  28. wire rs232_tx;
  29. reg ttl_rx = 1'b1;
  30. wire ttl_tx;
  31. // Request side: accept immediately by default; the oversize case
  32. // deselects ready to prove no partial write instead.
  33. reg req_ready = 1'b1;
  34. wire req_valid;
  35. wire [7:0] req_type;
  36. wire [7:0] req_length;
  37. wire req_ram_wr_en;
  38. wire [7:0] req_ram_wr_addr;
  39. wire [7:0] req_ram_wr_data;
  40. wire [7:0] req_ram_rd_addr;
  41. wire [7:0] req_ram_rd_data;
  42. // Reply side.
  43. reg tx_start = 1'b0;
  44. reg [7:0] tx_type = 8'd0;
  45. reg [7:0] tx_length = 8'd0;
  46. wire tx_busy;
  47. wire tx_done;
  48. wire [7:0] tx_ram_rd_addr;
  49. wire [7:0] tx_ram_rd_data;
  50. // Which physical RX line the stimulus drives (0=RS232, 1=TTL).
  51. // The idle line is always parked high; the TB asserts that on every
  52. // await_req so a stuck driver cannot hide behind the select.
  53. reg use_ttl = 1'b0;
  54. // Bit-period knob: 0 = nominal CLKS, 1 = deliberately wrong (+2).
  55. reg use_wrong_period = 1'b0;
  56. // Jitter knob: ±1 clock on each driven edge when set.
  57. reg use_jitter = 1'b0;
  58. always #20 clk = ~clk; // 25 MHz
  59. host_uart_endpoint #(
  60. .CLKS_PER_BIT(CLKS),
  61. .INTER_BYTE_TIMEOUT_CYCLES(TB_TIMEOUT_CYCLES),
  62. .MAX_PAYLOAD(128),
  63. .ADDR_WIDTH(8)
  64. ) dut (
  65. .clk(clk), .rst_n(rst_n),
  66. .rs232_rx(rs232_rx), .rs232_tx(rs232_tx),
  67. .ttl_rx(ttl_rx), .ttl_tx(ttl_tx),
  68. .req_valid(req_valid), .req_ready(req_ready),
  69. .req_type(req_type), .req_length(req_length),
  70. .req_ram_wr_en(req_ram_wr_en), .req_ram_wr_addr(req_ram_wr_addr),
  71. .req_ram_wr_data(req_ram_wr_data),
  72. .req_ram_rd_addr(req_ram_rd_addr), .req_ram_rd_data(req_ram_rd_data),
  73. .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length),
  74. .tx_busy(tx_busy), .tx_done(tx_done),
  75. .tx_ram_rd_addr(tx_ram_rd_addr), .tx_ram_rd_data(tx_ram_rd_data)
  76. );
  77. // Request RAM (128 B staging) and reply RAM (128 B staging).
  78. reg [7:0] req_mem [0:127];
  79. reg [7:0] rep_mem [0:127];
  80. integer init_i;
  81. initial begin
  82. for (init_i = 0; init_i < 128; init_i = init_i + 1) begin
  83. req_mem[init_i] = 8'hA9;
  84. rep_mem[init_i] = 8'h00;
  85. end
  86. end
  87. // Poison-guard: one extra sentinel cell proving the parser never
  88. // writes past the request RAM extent. It is re-armed before every
  89. // RX case because the oversize case legitimately fills 0..127.
  90. reg [7:0] req_guard = 8'hA9;
  91. task automatic req_guard_arm;
  92. begin
  93. req_guard = 8'hA9;
  94. end
  95. endtask
  96. always @(posedge clk) begin
  97. if (req_ram_wr_en) begin
  98. if (req_ram_wr_addr >= 128)
  99. req_guard <= req_ram_wr_data;
  100. else
  101. req_mem[req_ram_wr_addr] <= req_ram_wr_data;
  102. end
  103. end
  104. assign req_ram_rd_data = req_mem[req_ram_rd_addr];
  105. assign tx_ram_rd_data = rep_mem[tx_ram_rd_addr];
  106. // Count request-RAM writes per test case; any write with address past
  107. // the declared payload extent is a fatal protocol violation. The
  108. // counter is a clocked register reset by a one-cycle strobe so forked
  109. // stimulus/decode threads can never collide on it.
  110. reg [31:0] req_writes = 32'd0;
  111. reg req_writes_rst = 1'b0;
  112. always @(posedge clk) begin
  113. if (req_writes_rst)
  114. req_writes <= 32'd0;
  115. else if (req_ram_wr_en) begin
  116. req_writes <= req_writes + 1'b1;
  117. if (req_ram_wr_addr >= 128)
  118. $fatal(1, "request RAM address out of range %0d", req_ram_wr_addr);
  119. end
  120. end
  121. task automatic req_writes_reset;
  122. begin
  123. @(negedge clk);
  124. req_writes_rst = 1'b1;
  125. @(negedge clk);
  126. req_writes_rst = 1'b0;
  127. end
  128. endtask
  129. // ---- UART stimulus: real bit timing on the selected RX line ----
  130. task automatic uart_byte;
  131. input [7:0] value;
  132. integer uk;
  133. integer period;
  134. begin
  135. period = use_wrong_period ? (CLKS + 2) : CLKS;
  136. if (use_ttl) begin
  137. ttl_rx = 1'b0;
  138. #(period * 40 + (use_jitter ? 20 : 0));
  139. for (uk = 0; uk < 8; uk = uk + 1) begin
  140. ttl_rx = value[uk];
  141. #(period * 40 + (use_jitter ? ((uk % 2) ? 20 : -20) : 0));
  142. end
  143. ttl_rx = 1'b1;
  144. #(period * 40);
  145. end else begin
  146. rs232_rx = 1'b0;
  147. #(period * 40 + (use_jitter ? 20 : 0));
  148. for (uk = 0; uk < 8; uk = uk + 1) begin
  149. rs232_rx = value[uk];
  150. #(period * 40 + (use_jitter ? ((uk % 2) ? 20 : -20) : 0));
  151. end
  152. rs232_rx = 1'b1;
  153. #(period * 40);
  154. end
  155. end
  156. endtask
  157. // Byte-by-byte fragmented variant: one extra idle byte time between
  158. // every two frame bytes (still far below the inter-byte timeout).
  159. task automatic uart_byte_frag;
  160. input [7:0] value;
  161. begin
  162. uart_byte(value);
  163. #(CLKS * 40);
  164. end
  165. endtask
  166. // ---- TX decoders: real bit timing on both TX lines at once ----
  167. reg [7:0] rs232_stream [0:2047];
  168. integer rs232_count;
  169. reg [7:0] ttl_stream [0:2047];
  170. integer ttl_count;
  171. initial begin
  172. rs232_count = 0;
  173. ttl_count = 0;
  174. end
  175. task automatic decode_one;
  176. input which_ttl;
  177. output [7:0] decoded;
  178. integer b;
  179. begin
  180. if (which_ttl) begin
  181. // Mid-bit sampling: 1.5 bit periods after the start fall
  182. // lands in the middle of data bit 0 (sampling at exactly
  183. // one period would race the transmitter's bit boundary),
  184. // then one bit per sample and a stop check at 9.5 bits.
  185. @(negedge ttl_tx);
  186. #(CLKS * 40 + CLKS * 20);
  187. for (b = 0; b < 8; b = b + 1) begin
  188. decoded[b] = ttl_tx;
  189. if (b < 7) #1080;
  190. end
  191. #1080;
  192. if (ttl_tx !== 1'b1) $fatal(1, "TTL TX stop bit is low");
  193. ttl_stream[ttl_count] = decoded;
  194. ttl_count = ttl_count + 1;
  195. end else begin
  196. @(negedge rs232_tx);
  197. // Mid-bit sampling (see TTL branch note).
  198. #(CLKS * 40 + CLKS * 20);
  199. for (b = 0; b < 8; b = b + 1) begin
  200. decoded[b] = rs232_tx;
  201. if (b < 7) #1080;
  202. end
  203. #1080;
  204. if (rs232_tx !== 1'b1) $fatal(1, "RS232 TX stop bit is low");
  205. rs232_stream[rs232_count] = decoded;
  206. rs232_count = rs232_count + 1;
  207. end
  208. end
  209. endtask
  210. // Independent CRC-16/CCITT-FALSE over a decoded stream slice.
  211. // The stream arrays hold 8-bit bytes; the index math is sized
  212. // explicitly so Icarus never truncates the base+offset sum.
  213. function [15:0] stream_crc;
  214. input integer base;
  215. input integer nbody;
  216. integer ck, bitno;
  217. reg [15:0] c;
  218. begin
  219. c = 16'hFFFF;
  220. for (ck = 0; ck < nbody; ck = ck + 1) begin
  221. c = c ^ {rs232_stream[(base + ck) & 2047], 8'd0};
  222. for (bitno = 0; bitno < 8; bitno = bitno + 1)
  223. c = c[15] ? (c << 1) ^ 16'h1021 : c << 1;
  224. end
  225. stream_crc = c;
  226. end
  227. endfunction
  228. task check_decoded_frame;
  229. input integer base;
  230. input [7:0] etype;
  231. input integer elen;
  232. reg [15:0] c;
  233. reg [7:0] h0, h1, h2, h3, cl, ch;
  234. begin
  235. h0 = rs232_stream[(base) & 2047];
  236. h1 = rs232_stream[(base + 1) & 2047];
  237. h2 = rs232_stream[(base + 2) & 2047];
  238. h3 = rs232_stream[(base + 3) & 2047];
  239. cl = rs232_stream[(base + elen + 4) & 2047];
  240. ch = rs232_stream[(base + elen + 5) & 2047];
  241. if (h0 !== 8'hA5 || h1 !== 8'h5A)
  242. $fatal(1, "reply header magic mismatch");
  243. if (h2 !== etype || h3 !== elen[7:0])
  244. $fatal(1, "reply header mismatch type=%h len=%h", h2, h3);
  245. c = stream_crc(base + 2, elen + 2);
  246. if (c !== {ch, cl})
  247. $fatal(1, "reply CRC mismatch calc=%h wire=%h%h", c, ch, cl);
  248. end
  249. endtask
  250. // Wait for a parsed request with a bounded timeout (simulation-time
  251. // based: one UART byte at CLKS=27 takes 270 clocks).
  252. // Request-arrival sticky flag: req_valid is consumed by the accept
  253. // handshake, so cases that must prove a *fresh* parse arm this flag
  254. // after the link is free and poll it instead of level-polling valid.
  255. reg req_arrived = 1'b0;
  256. always @(posedge clk) begin
  257. if (req_valid)
  258. req_arrived <= 1'b1;
  259. end
  260. task automatic req_arrived_reset;
  261. begin
  262. @(negedge clk);
  263. req_arrived = 1'b0;
  264. @(negedge clk);
  265. end
  266. endtask
  267. // call_seq stamps every await_req call site: the failure log then
  268. // shows exactly which case returned a stale request.
  269. integer call_seq = 0;
  270. task automatic await_req;
  271. input [7:0] etype;
  272. input [7:0] elen;
  273. integer guard;
  274. reg [7:0] got_type;
  275. reg [7:0] got_len;
  276. integer my_seq;
  277. begin
  278. my_seq = call_seq;
  279. call_seq = call_seq + 1;
  280. // One UART byte at CLKS=27 costs 270 clocks; the longest
  281. // canned frame (oversize 200) needs ~56000 clocks on the
  282. // wire alone, plus CRC pacing.
  283. guard = 0;
  284. while (!req_valid && guard < 200000) begin
  285. @(posedge clk);
  286. guard = guard + 1;
  287. end
  288. if (!req_valid) $fatal(1, "request not parsed type=%h", etype);
  289. got_type = req_type;
  290. got_len = req_length;
  291. if (use_ttl) begin
  292. if (rs232_rx !== 1'b1) $fatal(1, "RS232 not idle during TTL case");
  293. end else begin
  294. if (ttl_rx !== 1'b1) $fatal(1, "TTL not idle during RS232 case");
  295. end
  296. if (got_type !== etype || got_len !== elen)
  297. $fatal(1, "request metadata mismatch type=%h len=%0d", got_type, got_len);
  298. end
  299. endtask
  300. task automatic accept_req;
  301. begin
  302. // Handshake the currently held request (ready stays up until
  303. // the endpoint drops valid). The owner release is polled
  304. // AFTER ready drops: the endpoint releases the link one cycle
  305. // after the accept handshake, and sampling earlier can catch
  306. // the stale owner and spin forever on the next claim edge.
  307. req_ready = 1'b1;
  308. while (req_valid) @(posedge clk);
  309. @(posedge clk);
  310. @(negedge clk);
  311. req_ready = 1'b0;
  312. repeat (2) @(posedge clk);
  313. while (dut.rx_owner != 2'd0) @(posedge clk);
  314. repeat (4) @(posedge clk);
  315. end
  316. endtask
  317. // ---- canned frames (8-bit protocol, independent vectors) ----
  318. task automatic send_set32_zero;
  319. integer tk;
  320. begin
  321. uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h01); uart_byte(8'h20);
  322. for (tk = 0; tk < 32; tk = tk + 1) uart_byte(8'h00);
  323. uart_byte(8'h0D); uart_byte(8'h8C);
  324. end
  325. endtask
  326. task automatic send_set32_hot5;
  327. integer tk;
  328. begin
  329. uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h01); uart_byte(8'h20);
  330. for (tk = 0; tk < 32; tk = tk + 1)
  331. uart_byte((tk == 5) ? 8'h64 : 8'h00);
  332. uart_byte(8'h32); uart_byte(8'h5C);
  333. end
  334. endtask
  335. task automatic send_get;
  336. begin
  337. uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h02); uart_byte(8'h00);
  338. uart_byte(8'h6D); uart_byte(8'h7B);
  339. end
  340. endtask
  341. task automatic send_bad_crc_get;
  342. begin
  343. uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h02); uart_byte(8'h00);
  344. uart_byte(8'h6D); uart_byte(8'h7C);
  345. end
  346. endtask
  347. // Declared length 200 (> 128-byte request RAM): consumed in full,
  348. // including CRC; embedded A5 5A at payload offsets 3,4 must not
  349. // resync. Payload byte k = k; CRC F2 BB (independent codec).
  350. task automatic send_oversize200;
  351. integer tk;
  352. begin
  353. uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h01); uart_byte(8'hC8);
  354. for (tk = 0; tk < 200; tk = tk + 1) begin
  355. if (tk == 3) uart_byte(8'hA5);
  356. else if (tk == 4) uart_byte(8'h5A);
  357. else uart_byte(tk[7:0]);
  358. end
  359. uart_byte(8'h8F); uart_byte(8'hE1);
  360. end
  361. endtask
  362. // Launch a reply of (tx_type, tx_length) with the reply RAM prefilled
  363. // from a pattern base. tx_start is held until the endpoint takes it
  364. // (tx_busy rises); a launch while a previous reply is still on the
  365. // wire waits for tx_done first.
  366. task automatic launch_reply;
  367. input [7:0] rtype;
  368. input [7:0] rlen;
  369. input [7:0] pat;
  370. integer tk;
  371. integer guard;
  372. begin
  373. guard = 0;
  374. while (tx_busy && guard < 200000) begin
  375. @(posedge clk);
  376. guard = guard + 1;
  377. end
  378. if (tx_busy) $fatal(1, "previous reply never finished");
  379. for (tk = 0; tk < 128; tk = tk + 1) rep_mem[tk] = pat + tk[7:0];
  380. // Fresh observation window for this reply: wait until the
  381. // endpoint is fully idle (frame source quiet, both line
  382. // drivers quiet, staging empty) so the previous reply's
  383. // done pulse has provably passed, then clear the sticky flag.
  384. begin
  385. integer clr_guard;
  386. clr_guard = 0;
  387. while (tx_busy && clr_guard < 400000) begin
  388. @(posedge clk);
  389. clr_guard = clr_guard + 1;
  390. end
  391. if (clr_guard >= 400000) $fatal(1, "TX never went idle");
  392. if (rs232_tx !== 1'b1 || ttl_tx !== 1'b1)
  393. $fatal(1, "public idle TX pins must both be high");
  394. end
  395. tx_done_reset;
  396. tx_type = rtype;
  397. tx_length = rlen;
  398. tx_start = 1'b1;
  399. while (!tx_busy) @(posedge clk);
  400. @(posedge clk);
  401. tx_start = 1'b0;
  402. end
  403. endtask
  404. // tx_done sticky flag: the one-cycle pulse is latched so bit-timed
  405. // stimulus threads can poll it without racing the clock edge. The
  406. // flag is cleared by launch_reply, so every reply gets a fresh
  407. // not-done observation window.
  408. reg tx_done_seen = 1'b0;
  409. always @(posedge clk) begin
  410. if (tx_done)
  411. tx_done_seen <= 1'b1;
  412. end
  413. task automatic tx_done_reset;
  414. begin
  415. @(negedge clk);
  416. tx_done_seen = 1'b0;
  417. @(negedge clk);
  418. end
  419. endtask
  420. task automatic await_tx_done;
  421. integer guard;
  422. begin
  423. guard = 0;
  424. while (!tx_done_seen && guard < 400000) begin
  425. @(posedge clk);
  426. guard = guard + 1;
  427. end
  428. if (!tx_done_seen) $fatal(1, "tx_done never arrived");
  429. @(posedge clk);
  430. end
  431. endtask
  432. // TX-line start-edge waiter: the DUT-side TX line under test falls
  433. // when the reply's first start bit goes on the wire. Sampling uses a
  434. // clocked poll (not a hierarchical edge control) so it survives the
  435. // forked stimulus branch on both simulators.
  436. // use_ttl selects the line: 0 = rs232_tx, 1 = ttl_tx.
  437. task automatic wait_for_tx_start_edge;
  438. integer guard;
  439. begin
  440. guard = 0;
  441. if (use_ttl) begin
  442. while (ttl_tx !== 1'b0 && guard < 200000) begin
  443. @(posedge clk);
  444. guard = guard + 1;
  445. end
  446. end else begin
  447. while (rs232_tx !== 1'b0 && guard < 200000) begin
  448. @(posedge clk);
  449. guard = guard + 1;
  450. end
  451. end
  452. if (guard >= 200000) $fatal(1, "TX start edge never came");
  453. end
  454. endtask
  455. // Decode exactly nframe bytes from the RS232 TX line (spawned in
  456. // parallel with stimulus so RX-during-TX is exercised for real).
  457. // Each invocation uses its own slot so concurrent calls (RS232 vs
  458. // TTL scenarios, never concurrent with each other) cannot collide.
  459. reg [7:0] dummy_rs232;
  460. // Forked-branch completion flags: polled, never @-waited, so no
  461. // event-control can leak onto a shared task variable.
  462. reg fork_decode_done;
  463. reg fork_stim_done;
  464. // Decode nframe bytes from each TX line in lockstep (same fork):
  465. // each byte is taken from whichever line falls first, then the other
  466. // line is decoded immediately after, proving broadcast equality.
  467. // Decode nframe bytes from BOTH TX lines at once: the two decoders
  468. // run in one fork so each broadcast byte is captured on both lines
  469. // concurrently, then compared. decode_one pushes into the shared
  470. // stream arrays; the byte outputs carry the compared values.
  471. task automatic decode_both;
  472. input integer nframe;
  473. integer dk2;
  474. reg [7:0] a_byte;
  475. reg [7:0] b_byte;
  476. begin
  477. for (dk2 = 0; dk2 < nframe; dk2 = dk2 + 1) begin
  478. fork
  479. decode_one(1'b0, a_byte);
  480. decode_one(1'b1, b_byte);
  481. join
  482. if (a_byte !== b_byte)
  483. $fatal(1, "broadcast TX diverged byte %0d: %h vs %h", dk2, a_byte, b_byte);
  484. end
  485. end
  486. endtask
  487. task automatic decode_n_ttl;
  488. input integer nframe;
  489. integer dk3;
  490. reg [7:0] throwaway;
  491. begin
  492. for (dk3 = 0; dk3 < nframe; dk3 = dk3 + 1) decode_one(1'b1, throwaway);
  493. end
  494. endtask
  495. function [15:0] ttl_stream_crc;
  496. input integer base;
  497. input integer nbody;
  498. integer ck2, bitno2;
  499. reg [15:0] c2;
  500. begin
  501. c2 = 16'hFFFF;
  502. for (ck2 = 0; ck2 < nbody; ck2 = ck2 + 1) begin
  503. c2 = c2 ^ {ttl_stream[(base + ck2) & 2047], 8'd0};
  504. for (bitno2 = 0; bitno2 < 8; bitno2 = bitno2 + 1)
  505. c2 = c2[15] ? (c2 << 1) ^ 16'h1021 : c2 << 1;
  506. end
  507. ttl_stream_crc = c2;
  508. end
  509. endfunction
  510. task automatic decode_n_rs232;
  511. input integer nframe;
  512. integer dk;
  513. begin
  514. for (dk = 0; dk < nframe; dk = dk + 1) decode_one(1'b0, dummy_rs232);
  515. end
  516. endtask
  517. // The scenario body, run once per active RX source. Per-invocation
  518. // state lives in these module slots so forked threads and repeated
  519. // invocations cannot collide on task statics.
  520. task automatic run_scenario;
  521. integer run_k;
  522. integer run_base;
  523. begin
  524. // 1. Legal SET on the active line: type/length right, payload in RAM.
  525. req_writes_reset;
  526. req_guard_arm;
  527. send_set32_zero();
  528. await_req(8'h01, 8'd32);
  529. if (req_writes !== 32) $fatal(1, "SET32 writes=%0d", req_writes);
  530. for (run_k = 0; run_k < 32; run_k = run_k + 1)
  531. if (req_mem[run_k] !== 8'h00) $fatal(1, "SET32 ram[%0d]=%h", run_k, req_mem[run_k]);
  532. if (req_guard !== 8'hA9) $fatal(1, "SET32 spilled past extent");
  533. // The validation read port mirrors the parser address: with a
  534. // synchronous request RAM it presented each staged byte one clock
  535. // behind its write; after the frame it still holds the last
  536. // staged byte (address 31 = last payload byte, value 00).
  537. accept_req();
  538. if (dut.req_ram_rd_addr !== 8'd31)
  539. $fatal(1, "validation read address %0d", dut.req_ram_rd_addr);
  540. if (dut.req_ram_rd_data !== 8'h00)
  541. $fatal(1, "validation read data %h", dut.req_ram_rd_data);
  542. // 2. GET with empty payload.
  543. req_writes_reset;
  544. send_get();
  545. await_req(8'h02, 8'd0);
  546. if (req_writes !== 0) $fatal(1, "GET writes=%0d", req_writes);
  547. accept_req();
  548. // 3. Bad-CRC frame discarded; later legal frame still parses.
  549. // The bad frame's 6 bytes take 6*270 clocks on the wire; the
  550. // parser needs up to two CRC_DONE + two wire bytes to abandon
  551. // it, so wait generously before asserting silence.
  552. req_writes_reset;
  553. send_bad_crc_get();
  554. repeat (6000) @(posedge clk);
  555. if (req_valid) $fatal(1, "bad-CRC frame published");
  556. while (dut.rx_owner != 2'd0) @(posedge clk);
  557. repeat (4) @(posedge clk);
  558. send_set32_hot5();
  559. await_req(8'h01, 8'd32);
  560. if (req_mem[5] !== 8'h64) $fatal(1, "hot5 ram[5]=%h", req_mem[5]);
  561. accept_req();
  562. // 4. Oversize declared length (200 > 128): fully consumed incl.
  563. // CRC, acknowledged as a frame, but no partial write into RAM.
  564. for (run_k = 0; run_k < 128; run_k = run_k + 1) req_mem[run_k] = 8'hA9;
  565. req_writes_reset;
  566. req_guard_arm;
  567. send_oversize200();
  568. await_req(8'h01, 8'd200);
  569. if (req_writes !== 128) $fatal(1, "oversize writes=%0d, want 128", req_writes);
  570. // Payload byte k holds k, except the embedded A5 5A at offsets
  571. // 3,4 which must land unescaped (and must not resync the frame).
  572. for (run_k = 0; run_k < 128; run_k = run_k + 1) begin
  573. if (run_k == 3) begin
  574. if (req_mem[run_k] !== 8'hA5) $fatal(1, "oversize ram[3]=%h", req_mem[run_k]);
  575. end else if (run_k == 4) begin
  576. if (req_mem[run_k] !== 8'h5A) $fatal(1, "oversize ram[4]=%h", req_mem[run_k]);
  577. end else if (req_mem[run_k] !== run_k[7:0])
  578. $fatal(1, "oversize ram[%0d]=%h", run_k, req_mem[run_k]);
  579. end
  580. accept_req();
  581. // 5. Two adjacent legal frames back-to-back both parse. The
  582. // second frame starts on the wire immediately after the first
  583. // frame's last stop bit (no idle gap beyond the UART stop bit).
  584. // The check afterwards proves the link was still owned across
  585. // the boundary (no release/re-claim glitch between frames).
  586. req_writes_reset;
  587. send_get();
  588. await_req(8'h02, 8'd0);
  589. if (req_writes !== 0) $fatal(1, "back-to-back GET writes=%0d", req_writes);
  590. accept_req();
  591. req_writes_reset;
  592. send_set32_zero();
  593. await_req(8'h01, 8'd32);
  594. if (dut.rx_owner == 2'd0)
  595. $fatal(1, "back-to-back SET parsed with no owner");
  596. if (req_writes !== 32) $fatal(1, "back-to-back SET writes=%0d", req_writes);
  597. accept_req();
  598. // 6. Byte-by-byte fragmented frame parses.
  599. req_writes_reset;
  600. uart_byte_frag(8'hA5); uart_byte_frag(8'h5A);
  601. uart_byte_frag(8'h02); uart_byte_frag(8'h00);
  602. uart_byte_frag(8'h6D); uart_byte_frag(8'h7B);
  603. await_req(8'h02, 8'd0);
  604. if (dut.rx_owner == 2'd0)
  605. $fatal(1, "frag GET parsed with no owner");
  606. if (req_writes !== 0) $fatal(1, "frag GET writes=%0d", req_writes);
  607. accept_req();
  608. // 7. RX stays live during TX: an 83-byte GET_DATA reply is
  609. // launched first (long on the wire: 89 bytes x 270 clocks); the
  610. // GET is injected mid-reply on the owned line and must parse
  611. // while the reply is still transmitting (tx_done_seen still low
  612. // at parse time, tx_busy still up). The reply bytes are decoded
  613. // live in the same fork and CRC-checked below.
  614. run_base = rs232_count;
  615. fork_decode_done = 1'b0;
  616. fork_stim_done = 1'b0;
  617. fork
  618. begin
  619. decode_n_rs232(89);
  620. fork_decode_done = 1'b1;
  621. end
  622. begin
  623. // The TTL scenario decodes the RS232 line too (broadcast
  624. // TX), but the request must arrive on the owned line, so
  625. // the injection edge waits on the line under test.
  626. // Launch a long reply (83-byte GET_DATA) so the GET
  627. // provably overlaps it: the reply needs ~23000 clocks on
  628. // the wire while the 6-byte GET needs ~1700.
  629. launch_reply(8'h42, 8'd83, 8'hA0);
  630. // Wait until the reply is really transmitting, then
  631. // inject the GET mid-reply. The head start is counted in
  632. // the testbench clock domain (40 ps timestep = 1 ps in
  633. // DUT time is wrong: uart_byte advances simulation time,
  634. // not DUT clocks), so instead of a fixed clock count, arm
  635. // on the live TX line: the first TX start edge proves the
  636. // reply owns the wire right now.
  637. wait_for_tx_start_edge();
  638. if (!tx_busy) $fatal(1, "reply not on wire at injection");
  639. if (tx_done || tx_done_seen)
  640. $fatal(1, "reply already done at injection");
  641. send_get();
  642. await_req(8'h02, 8'd0);
  643. // The GET parsed while the reply was still transmitting
  644. // iff the reply's wire is still busy at this point (the
  645. // frame source may already be quiet while the last bytes
  646. // are still shifting out).
  647. if (!tx_busy || tx_done || tx_done_seen)
  648. $fatal(1, "reply completed before overlapping GET was parsed");
  649. fork_stim_done = 1'b1;
  650. end
  651. join
  652. while (!fork_decode_done || !fork_stim_done) @(posedge clk);
  653. await_tx_done();
  654. check_decoded_frame(run_base, 8'h42, 83);
  655. // The RX side must have parsed the GET injected while the reply
  656. // was transmitting. The fork's stim branch already proved the
  657. // GET parsed (it awaited req_valid through the live parser while
  658. // tx_busy was up); here the request is still held because
  659. // req_ready stayed low through the whole reply, so accepting it
  660. // now proves the parse survived to the end of TX.
  661. if (!req_valid) $fatal(1, "during-TX GET lost");
  662. if (req_type !== 8'h02 || req_length !== 8'd0)
  663. $fatal(1, "during-TX GET metadata type=%h len=%0d", req_type, req_length);
  664. if (req_writes !== 0) $fatal(1, "during-TX GET writes=%0d", req_writes);
  665. accept_req();
  666. // 8. A longer reply (NACK, 2 payload bytes) decodes byte-exact
  667. // on the RS232 line, then the identical reply is re-emitted and
  668. // decoded on the TTL line: both pins provably carry the same
  669. // broadcast stream.
  670. run_base = rs232_count;
  671. fork_decode_done = 1'b0;
  672. fork_stim_done = 1'b0;
  673. fork
  674. begin
  675. decode_n_rs232(8);
  676. fork_decode_done = 1'b1;
  677. end
  678. begin
  679. launch_reply(8'hC0, 8'd2, 8'h01);
  680. fork_stim_done = 1'b1;
  681. end
  682. join
  683. while (!fork_decode_done || !fork_stim_done) @(posedge clk);
  684. await_tx_done();
  685. begin
  686. reg [7:0] n0, n1;
  687. n0 = rs232_stream[(run_base + 4) & 2047];
  688. n1 = rs232_stream[(run_base + 5) & 2047];
  689. if (n0 !== 8'h01 || n1 !== 8'h02)
  690. $fatal(1, "NACK payload mismatch %h %h", n0, n1);
  691. end
  692. check_decoded_frame(run_base, 8'hC0, 2);
  693. // Same reply again, decoded on the TTL pin this time.
  694. ttl_count = 0;
  695. fork
  696. begin
  697. decode_n_ttl(8);
  698. fork_decode_done = 1'b1;
  699. end
  700. begin
  701. launch_reply(8'hC0, 8'd2, 8'h01);
  702. fork_stim_done = 1'b1;
  703. end
  704. join
  705. while (!fork_decode_done || !fork_stim_done) @(posedge clk);
  706. await_tx_done();
  707. if (ttl_stream[2] !== 8'hC0 || ttl_stream[3] !== 8'h02 ||
  708. ttl_stream[4] !== 8'h01 || ttl_stream[5] !== 8'h02)
  709. $fatal(1, "TTL broadcast mismatch %h %h %h %h",
  710. ttl_stream[2], ttl_stream[3], ttl_stream[4], ttl_stream[5]);
  711. begin
  712. reg [7:0] t2, t3, t4, t5, t6, t7;
  713. reg [15:0] tcrc;
  714. t2 = ttl_stream[2]; t3 = ttl_stream[3];
  715. t4 = ttl_stream[4]; t5 = ttl_stream[5];
  716. t6 = ttl_stream[6]; t7 = ttl_stream[7];
  717. tcrc = ttl_stream_crc(2, 4);
  718. if (tcrc !== {t7, t6})
  719. $fatal(1, "TTL broadcast CRC mismatch calc=%h wire=%h%h", tcrc, t7, t6);
  720. end
  721. end
  722. endtask
  723. initial begin
  724. repeat (4) @(posedge clk);
  725. rst_n = 1'b1;
  726. repeat (4) @(posedge clk);
  727. // ---- RS232 as the active source ----
  728. use_ttl = 1'b0;
  729. req_ready = 1'b0;
  730. run_scenario;
  731. // ---- TTL as the active source: same scenario ----
  732. while (dut.rx_owner != 2'd0) @(posedge clk);
  733. repeat (4) @(posedge clk);
  734. use_ttl = 1'b1;
  735. // Fresh RX-side state for the TTL pass: the request/accept
  736. // handshake left req_ready low and the streams hold the RS232
  737. // pass bytes.
  738. req_ready = 1'b0;
  739. rs232_count = 0;
  740. ttl_count = 0;
  741. run_scenario;
  742. // ---- wrong bit period: frames must not parse ----
  743. // The TTL pass ends with the link owned (case 8 holds no request
  744. // but the owner releases only on transaction completion); force
  745. // the parser back to hunting with a bounded wait, then prove the
  746. // owner is free before switching lines.
  747. use_ttl = 1'b0;
  748. begin
  749. integer rel_guard;
  750. rel_guard = 0;
  751. while (dut.rx_owner != 2'd0 && rel_guard < 300000) begin
  752. @(posedge clk);
  753. rel_guard = rel_guard + 1;
  754. end
  755. if (dut.rx_owner != 2'd0) $fatal(1, "link never released, owner=%0d", dut.rx_owner);
  756. end
  757. repeat (4) @(posedge clk);
  758. use_wrong_period = 1'b1;
  759. send_get();
  760. repeat (6000) @(posedge clk);
  761. if (req_valid) $fatal(1, "wrong-period frame parsed");
  762. begin
  763. integer rel_guard2;
  764. rel_guard2 = 0;
  765. while (dut.rx_owner != 2'd0 && rel_guard2 < 300000) begin
  766. @(posedge clk);
  767. rel_guard2 = rel_guard2 + 1;
  768. end
  769. end
  770. repeat (4) @(posedge clk);
  771. use_wrong_period = 1'b0;
  772. // Link recovers at the nominal period.
  773. send_get();
  774. await_req(8'h02, 8'd0);
  775. accept_req();
  776. // ---- jittered edges at the nominal period: frames still parse ----
  777. use_jitter = 1'b1;
  778. send_get();
  779. await_req(8'h02, 8'd0);
  780. accept_req();
  781. send_set32_hot5();
  782. await_req(8'h01, 8'd32);
  783. if (req_mem[5] !== 8'h64) $fatal(1, "jittered hot5 ram[5]=%h", req_mem[5]);
  784. accept_req();
  785. use_jitter = 1'b0;
  786. $display("host_uart_endpoint_tb PASS");
  787. $finish;
  788. end
  789. initial begin
  790. #200000000;
  791. $fatal(1, "host_uart_endpoint deadline");
  792. end
  793. endmodule