host_uart_endpoint_tb.v 32 KB

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