`timescale 1ns/1ps // Extended endpoint regression using integer 27-clock UART timing. // The nominal four-rate test independently checks baud and complete final stop. // This older fixture uses combinational request/reply TB RAM. // host_uart_endpoint testbench: drives UART bit timing (CLKS_PER_BIT=27 // for 921600 baud at 25 MHz, plus one wrong-bit-period case and jittered // cases) into both RX lines and decodes both TX lines at the bit level. // All frame bytes below are the final 8-bit protocol images from the // independent Python codec (tools/crc16_ccitt_table.frame_bytes), // hand-embedded (see the vector table in the header comment). // // Vector table (hex on the wire): // SET32 zero : A5 5A 01 20 + 32x00 + 0D 8C // SET32 hot5 : A5 5A 01 20 + 00*5 64 00*26 + 32 5C // GET : A5 5A 02 00 6D 7B // GET payload0: (empty) // SETACK : A5 5A 81 00 A6 35 // NACK QFULL : A5 5A C0 02 01 04 B1 2A // OVERSIZE 200: A5 5A 01 C8 + 200 payload bytes (00..C7 with A5 5A at // offsets 3,4) + 8F E1 // EMBED6566 : payload bytes 3,4 of the oversize frame are A5 5A module host_uart_endpoint_tb; localparam integer CLKS = 27; localparam integer TB_TIMEOUT_CYCLES = 250000; reg clk = 1'b0; reg rst_n = 1'b0; reg rs232_rx = 1'b1; wire rs232_tx; reg ttl_rx = 1'b1; wire ttl_tx; // Request side: accept immediately by default; the oversize case // deselects ready to prove no partial write instead. reg req_ready = 1'b1; wire req_valid; wire [7:0] req_type; wire [7:0] req_length; wire req_ram_wr_en; wire [7:0] req_ram_wr_addr; wire [7:0] req_ram_wr_data; wire [7:0] req_ram_rd_addr; wire [7:0] req_ram_rd_data; // Reply side. reg tx_start = 1'b0; reg [7:0] tx_type = 8'd0; reg [7:0] tx_length = 8'd0; wire tx_busy; wire tx_done; wire [7:0] tx_ram_rd_addr; wire [7:0] tx_ram_rd_data; // Which physical RX line the stimulus drives (0=RS232, 1=TTL). // The idle line is always parked high; the TB asserts that on every // await_req so a stuck driver cannot hide behind the select. reg use_ttl = 1'b0; // Bit-period knob: 0 = nominal CLKS, 1 = deliberately wrong (+2). reg use_wrong_period = 1'b0; // Jitter knob: ±1 clock on each driven edge when set. reg use_jitter = 1'b0; always #20 clk = ~clk; // 25 MHz host_uart_endpoint #( .CLKS_PER_BIT(CLKS), .INTER_BYTE_TIMEOUT_CYCLES(TB_TIMEOUT_CYCLES), .MAX_PAYLOAD(128), .ADDR_WIDTH(8) ) dut ( .clk(clk), .rst_n(rst_n), .rs232_rx(rs232_rx), .rs232_tx(rs232_tx), .ttl_rx(ttl_rx), .ttl_tx(ttl_tx), .req_valid(req_valid), .req_ready(req_ready), .req_type(req_type), .req_length(req_length), .req_ram_wr_en(req_ram_wr_en), .req_ram_wr_addr(req_ram_wr_addr), .req_ram_wr_data(req_ram_wr_data), .req_ram_rd_addr(req_ram_rd_addr), .req_ram_rd_data(req_ram_rd_data), .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length), .tx_busy(tx_busy), .tx_done(tx_done), .tx_ram_rd_addr(tx_ram_rd_addr), .tx_ram_rd_data(tx_ram_rd_data) ); // Request RAM (128 B staging) and reply RAM (128 B staging). reg [7:0] req_mem [0:127]; reg [7:0] rep_mem [0:127]; integer init_i; initial begin for (init_i = 0; init_i < 128; init_i = init_i + 1) begin req_mem[init_i] = 8'hA9; rep_mem[init_i] = 8'h00; end end // Poison-guard: one extra sentinel cell proving the parser never // writes past the request RAM extent. It is re-armed before every // RX case because the oversize case legitimately fills 0..127. reg [7:0] req_guard = 8'hA9; task automatic req_guard_arm; begin req_guard = 8'hA9; end endtask always @(posedge clk) begin if (req_ram_wr_en) begin if (req_ram_wr_addr >= 128) req_guard <= req_ram_wr_data; else req_mem[req_ram_wr_addr] <= req_ram_wr_data; end end assign req_ram_rd_data = req_mem[req_ram_rd_addr]; assign tx_ram_rd_data = rep_mem[tx_ram_rd_addr]; // Count request-RAM writes per test case; any write with address past // the declared payload extent is a fatal protocol violation. The // counter is a clocked register reset by a one-cycle strobe so forked // stimulus/decode threads can never collide on it. reg [31:0] req_writes = 32'd0; reg req_writes_rst = 1'b0; always @(posedge clk) begin if (req_writes_rst) req_writes <= 32'd0; else if (req_ram_wr_en) begin req_writes <= req_writes + 1'b1; if (req_ram_wr_addr >= 128) $fatal(1, "request RAM address out of range %0d", req_ram_wr_addr); end end task automatic req_writes_reset; begin @(negedge clk); req_writes_rst = 1'b1; @(negedge clk); req_writes_rst = 1'b0; end endtask // ---- UART stimulus: real bit timing on the selected RX line ---- task automatic uart_byte; input [7:0] value; integer uk; integer period; begin period = use_wrong_period ? (CLKS + 2) : CLKS; if (use_ttl) begin ttl_rx = 1'b0; #(period * 40 + (use_jitter ? 20 : 0)); for (uk = 0; uk < 8; uk = uk + 1) begin ttl_rx = value[uk]; #(period * 40 + (use_jitter ? ((uk % 2) ? 20 : -20) : 0)); end ttl_rx = 1'b1; #(period * 40); end else begin rs232_rx = 1'b0; #(period * 40 + (use_jitter ? 20 : 0)); for (uk = 0; uk < 8; uk = uk + 1) begin rs232_rx = value[uk]; #(period * 40 + (use_jitter ? ((uk % 2) ? 20 : -20) : 0)); end rs232_rx = 1'b1; #(period * 40); end end endtask // Byte-by-byte fragmented variant: one extra idle byte time between // every two frame bytes (still far below the inter-byte timeout). task automatic uart_byte_frag; input [7:0] value; begin uart_byte(value); #(CLKS * 40); end endtask // ---- TX decoders: real bit timing on both TX lines at once ---- reg [7:0] rs232_stream [0:2047]; integer rs232_count; reg [7:0] ttl_stream [0:2047]; integer ttl_count; initial begin rs232_count = 0; ttl_count = 0; end task automatic decode_one; input which_ttl; output [7:0] decoded; integer b; begin if (which_ttl) begin // Mid-bit sampling: 1.5 bit periods after the start fall // lands in the middle of data bit 0 (sampling at exactly // one period would race the transmitter's bit boundary), // then one bit per sample and a stop check at 9.5 bits. @(negedge ttl_tx); #(CLKS * 40 + CLKS * 20); for (b = 0; b < 8; b = b + 1) begin decoded[b] = ttl_tx; if (b < 7) #1080; end #1080; if (ttl_tx !== 1'b1) $fatal(1, "TTL TX stop bit is low"); ttl_stream[ttl_count] = decoded; ttl_count = ttl_count + 1; end else begin @(negedge rs232_tx); // Mid-bit sampling (see TTL branch note). #(CLKS * 40 + CLKS * 20); for (b = 0; b < 8; b = b + 1) begin decoded[b] = rs232_tx; if (b < 7) #1080; end #1080; if (rs232_tx !== 1'b1) $fatal(1, "RS232 TX stop bit is low"); rs232_stream[rs232_count] = decoded; rs232_count = rs232_count + 1; end end endtask // Independent CRC-16/CCITT-FALSE over a decoded stream slice. // The stream arrays hold 8-bit bytes; the index math is sized // explicitly so Icarus never truncates the base+offset sum. function [15:0] stream_crc; input integer base; input integer nbody; integer ck, bitno; reg [15:0] c; begin c = 16'hFFFF; for (ck = 0; ck < nbody; ck = ck + 1) begin c = c ^ {rs232_stream[(base + ck) & 2047], 8'd0}; for (bitno = 0; bitno < 8; bitno = bitno + 1) c = c[15] ? (c << 1) ^ 16'h1021 : c << 1; end stream_crc = c; end endfunction task check_decoded_frame; input integer base; input [7:0] etype; input integer elen; reg [15:0] c; reg [7:0] h0, h1, h2, h3, cl, ch; begin h0 = rs232_stream[(base) & 2047]; h1 = rs232_stream[(base + 1) & 2047]; h2 = rs232_stream[(base + 2) & 2047]; h3 = rs232_stream[(base + 3) & 2047]; cl = rs232_stream[(base + elen + 4) & 2047]; ch = rs232_stream[(base + elen + 5) & 2047]; if (h0 !== 8'hA5 || h1 !== 8'h5A) $fatal(1, "reply header magic mismatch"); if (h2 !== etype || h3 !== elen[7:0]) $fatal(1, "reply header mismatch type=%h len=%h", h2, h3); c = stream_crc(base + 2, elen + 2); if (c !== {ch, cl}) $fatal(1, "reply CRC mismatch calc=%h wire=%h%h", c, ch, cl); end endtask // Wait for a parsed request with a bounded timeout (simulation-time // based: one UART byte at CLKS=27 takes 270 clocks). // Request-arrival sticky flag: req_valid is consumed by the accept // handshake, so cases that must prove a *fresh* parse arm this flag // after the link is free and poll it instead of level-polling valid. reg req_arrived = 1'b0; always @(posedge clk) begin if (req_valid) req_arrived <= 1'b1; end task automatic req_arrived_reset; begin @(negedge clk); req_arrived = 1'b0; @(negedge clk); end endtask // call_seq stamps every await_req call site: the failure log then // shows exactly which case returned a stale request. integer call_seq = 0; task automatic await_req; input [7:0] etype; input [7:0] elen; integer guard; reg [7:0] got_type; reg [7:0] got_len; integer my_seq; begin my_seq = call_seq; call_seq = call_seq + 1; // One UART byte at CLKS=27 costs 270 clocks; the longest // canned frame (oversize 200) needs ~56000 clocks on the // wire alone, plus CRC pacing. guard = 0; while (!req_valid && guard < 200000) begin @(posedge clk); guard = guard + 1; end if (!req_valid) $fatal(1, "request not parsed type=%h", etype); got_type = req_type; got_len = req_length; if (use_ttl) begin if (rs232_rx !== 1'b1) $fatal(1, "RS232 not idle during TTL case"); end else begin if (ttl_rx !== 1'b1) $fatal(1, "TTL not idle during RS232 case"); end if (got_type !== etype || got_len !== elen) $fatal(1, "request metadata mismatch type=%h len=%0d", got_type, got_len); end endtask task automatic accept_req; begin // Handshake the currently held request (ready stays up until // the endpoint drops valid). The owner release is polled // AFTER ready drops: the endpoint releases the link one cycle // after the accept handshake, and sampling earlier can catch // the stale owner and spin forever on the next claim edge. req_ready = 1'b1; while (req_valid) @(posedge clk); @(posedge clk); @(negedge clk); req_ready = 1'b0; repeat (2) @(posedge clk); while (dut.rx_owner != 2'd0) @(posedge clk); repeat (4) @(posedge clk); end endtask // ---- canned frames (8-bit protocol, independent vectors) ---- task automatic send_set32_zero; integer tk; begin uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h01); uart_byte(8'h20); for (tk = 0; tk < 32; tk = tk + 1) uart_byte(8'h00); uart_byte(8'h0D); uart_byte(8'h8C); end endtask task automatic send_set32_hot5; integer tk; begin uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h01); uart_byte(8'h20); for (tk = 0; tk < 32; tk = tk + 1) uart_byte((tk == 5) ? 8'h64 : 8'h00); uart_byte(8'h32); uart_byte(8'h5C); end endtask task automatic send_get; begin uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h02); uart_byte(8'h00); uart_byte(8'h6D); uart_byte(8'h7B); end endtask task automatic send_bad_crc_get; begin uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h02); uart_byte(8'h00); uart_byte(8'h6D); uart_byte(8'h7C); end endtask // Declared length 200 (> 128-byte request RAM): consumed in full, // including CRC; embedded A5 5A at payload offsets 3,4 must not // resync. Payload byte k = k; CRC F2 BB (independent codec). task automatic send_oversize200; integer tk; begin uart_byte(8'hA5); uart_byte(8'h5A); uart_byte(8'h01); uart_byte(8'hC8); for (tk = 0; tk < 200; tk = tk + 1) begin if (tk == 3) uart_byte(8'hA5); else if (tk == 4) uart_byte(8'h5A); else uart_byte(tk[7:0]); end uart_byte(8'h8F); uart_byte(8'hE1); end endtask // Launch a reply of (tx_type, tx_length) with the reply RAM prefilled // from a pattern base. tx_start is held until the endpoint takes it // (tx_busy rises); a launch while a previous reply is still on the // wire waits for tx_done first. task automatic launch_reply; input [7:0] rtype; input [7:0] rlen; input [7:0] pat; integer tk; integer guard; begin guard = 0; while (tx_busy && guard < 200000) begin @(posedge clk); guard = guard + 1; end if (tx_busy) $fatal(1, "previous reply never finished"); for (tk = 0; tk < 128; tk = tk + 1) rep_mem[tk] = pat + tk[7:0]; // Fresh observation window for this reply: wait until the // endpoint is fully idle (frame source quiet, both line // drivers quiet, staging empty) so the previous reply's // done pulse has provably passed, then clear the sticky flag. begin integer clr_guard; clr_guard = 0; while (tx_busy && clr_guard < 400000) begin @(posedge clk); clr_guard = clr_guard + 1; end if (clr_guard >= 400000) $fatal(1, "TX never went idle"); if (rs232_tx !== 1'b1 || ttl_tx !== 1'b1) $fatal(1, "public idle TX pins must both be high"); end tx_done_reset; tx_type = rtype; tx_length = rlen; tx_start = 1'b1; while (!tx_busy) @(posedge clk); @(posedge clk); tx_start = 1'b0; end endtask // tx_done sticky flag: the one-cycle pulse is latched so bit-timed // stimulus threads can poll it without racing the clock edge. The // flag is cleared by launch_reply, so every reply gets a fresh // not-done observation window. reg tx_done_seen = 1'b0; always @(posedge clk) begin if (tx_done) tx_done_seen <= 1'b1; end task automatic tx_done_reset; begin @(negedge clk); tx_done_seen = 1'b0; @(negedge clk); end endtask task automatic await_tx_done; integer guard; begin guard = 0; while (!tx_done_seen && guard < 400000) begin @(posedge clk); guard = guard + 1; end if (!tx_done_seen) $fatal(1, "tx_done never arrived"); @(posedge clk); end endtask // TX-line start-edge waiter: the DUT-side TX line under test falls // when the reply's first start bit goes on the wire. Sampling uses a // clocked poll (not a hierarchical edge control) so it survives the // forked stimulus branch on both simulators. // use_ttl selects the line: 0 = rs232_tx, 1 = ttl_tx. task automatic wait_for_tx_start_edge; integer guard; begin guard = 0; if (use_ttl) begin while (ttl_tx !== 1'b0 && guard < 200000) begin @(posedge clk); guard = guard + 1; end end else begin while (rs232_tx !== 1'b0 && guard < 200000) begin @(posedge clk); guard = guard + 1; end end if (guard >= 200000) $fatal(1, "TX start edge never came"); end endtask // Decode exactly nframe bytes from the RS232 TX line (spawned in // parallel with stimulus so RX-during-TX is exercised for real). // Each invocation uses its own slot so concurrent calls (RS232 vs // TTL scenarios, never concurrent with each other) cannot collide. reg [7:0] dummy_rs232; // Forked-branch completion flags: polled, never @-waited, so no // event-control can leak onto a shared task variable. reg fork_decode_done; reg fork_stim_done; // Decode nframe bytes from each TX line in lockstep (same fork): // each byte is taken from whichever line falls first, then the other // line is decoded immediately after, proving broadcast equality. // Decode nframe bytes from BOTH TX lines at once: the two decoders // run in one fork so each broadcast byte is captured on both lines // concurrently, then compared. decode_one pushes into the shared // stream arrays; the byte outputs carry the compared values. task automatic decode_both; input integer nframe; integer dk2; reg [7:0] a_byte; reg [7:0] b_byte; begin for (dk2 = 0; dk2 < nframe; dk2 = dk2 + 1) begin fork decode_one(1'b0, a_byte); decode_one(1'b1, b_byte); join if (a_byte !== b_byte) $fatal(1, "broadcast TX diverged byte %0d: %h vs %h", dk2, a_byte, b_byte); end end endtask task automatic decode_n_ttl; input integer nframe; integer dk3; reg [7:0] throwaway; begin for (dk3 = 0; dk3 < nframe; dk3 = dk3 + 1) decode_one(1'b1, throwaway); end endtask function [15:0] ttl_stream_crc; input integer base; input integer nbody; integer ck2, bitno2; reg [15:0] c2; begin c2 = 16'hFFFF; for (ck2 = 0; ck2 < nbody; ck2 = ck2 + 1) begin c2 = c2 ^ {ttl_stream[(base + ck2) & 2047], 8'd0}; for (bitno2 = 0; bitno2 < 8; bitno2 = bitno2 + 1) c2 = c2[15] ? (c2 << 1) ^ 16'h1021 : c2 << 1; end ttl_stream_crc = c2; end endfunction task automatic decode_n_rs232; input integer nframe; integer dk; begin for (dk = 0; dk < nframe; dk = dk + 1) decode_one(1'b0, dummy_rs232); end endtask // The scenario body, run once per active RX source. Per-invocation // state lives in these module slots so forked threads and repeated // invocations cannot collide on task statics. task automatic run_scenario; integer run_k; integer run_base; begin // 1. Legal SET on the active line: type/length right, payload in RAM. req_writes_reset; req_guard_arm; send_set32_zero(); await_req(8'h01, 8'd32); if (req_writes !== 32) $fatal(1, "SET32 writes=%0d", req_writes); for (run_k = 0; run_k < 32; run_k = run_k + 1) if (req_mem[run_k] !== 8'h00) $fatal(1, "SET32 ram[%0d]=%h", run_k, req_mem[run_k]); if (req_guard !== 8'hA9) $fatal(1, "SET32 spilled past extent"); // The validation read port mirrors the parser address: with a // synchronous request RAM it presented each staged byte one clock // behind its write; after the frame it still holds the last // staged byte (address 31 = last payload byte, value 00). accept_req(); if (dut.req_ram_rd_addr !== 8'd31) $fatal(1, "validation read address %0d", dut.req_ram_rd_addr); if (dut.req_ram_rd_data !== 8'h00) $fatal(1, "validation read data %h", dut.req_ram_rd_data); // 2. GET with empty payload. req_writes_reset; send_get(); await_req(8'h02, 8'd0); if (req_writes !== 0) $fatal(1, "GET writes=%0d", req_writes); accept_req(); // 3. Bad-CRC frame discarded; later legal frame still parses. // The bad frame's 6 bytes take 6*270 clocks on the wire; the // parser needs up to two CRC_DONE + two wire bytes to abandon // it, so wait generously before asserting silence. req_writes_reset; send_bad_crc_get(); repeat (6000) @(posedge clk); if (req_valid) $fatal(1, "bad-CRC frame published"); while (dut.rx_owner != 2'd0) @(posedge clk); repeat (4) @(posedge clk); send_set32_hot5(); await_req(8'h01, 8'd32); if (req_mem[5] !== 8'h64) $fatal(1, "hot5 ram[5]=%h", req_mem[5]); accept_req(); // 4. Oversize declared length (200 > 128): fully consumed incl. // CRC, acknowledged as a frame, but no partial write into RAM. for (run_k = 0; run_k < 128; run_k = run_k + 1) req_mem[run_k] = 8'hA9; req_writes_reset; req_guard_arm; send_oversize200(); await_req(8'h01, 8'd200); if (req_writes !== 128) $fatal(1, "oversize writes=%0d, want 128", req_writes); // Payload byte k holds k, except the embedded A5 5A at offsets // 3,4 which must land unescaped (and must not resync the frame). for (run_k = 0; run_k < 128; run_k = run_k + 1) begin if (run_k == 3) begin if (req_mem[run_k] !== 8'hA5) $fatal(1, "oversize ram[3]=%h", req_mem[run_k]); end else if (run_k == 4) begin if (req_mem[run_k] !== 8'h5A) $fatal(1, "oversize ram[4]=%h", req_mem[run_k]); end else if (req_mem[run_k] !== run_k[7:0]) $fatal(1, "oversize ram[%0d]=%h", run_k, req_mem[run_k]); end accept_req(); // 5. Two adjacent legal frames back-to-back both parse. The // second frame starts on the wire immediately after the first // frame's last stop bit (no idle gap beyond the UART stop bit). // The check afterwards proves the link was still owned across // the boundary (no release/re-claim glitch between frames). req_writes_reset; send_get(); await_req(8'h02, 8'd0); if (req_writes !== 0) $fatal(1, "back-to-back GET writes=%0d", req_writes); accept_req(); req_writes_reset; send_set32_zero(); await_req(8'h01, 8'd32); if (dut.rx_owner == 2'd0) $fatal(1, "back-to-back SET parsed with no owner"); if (req_writes !== 32) $fatal(1, "back-to-back SET writes=%0d", req_writes); accept_req(); // 6. Byte-by-byte fragmented frame parses. req_writes_reset; uart_byte_frag(8'hA5); uart_byte_frag(8'h5A); uart_byte_frag(8'h02); uart_byte_frag(8'h00); uart_byte_frag(8'h6D); uart_byte_frag(8'h7B); await_req(8'h02, 8'd0); if (dut.rx_owner == 2'd0) $fatal(1, "frag GET parsed with no owner"); if (req_writes !== 0) $fatal(1, "frag GET writes=%0d", req_writes); accept_req(); // 7. RX stays live during TX: an 83-byte GET_DATA reply is // launched first (long on the wire: 89 bytes x 270 clocks); the // GET is injected mid-reply on the owned line and must parse // while the reply is still transmitting (tx_done_seen still low // at parse time, tx_busy still up). The reply bytes are decoded // live in the same fork and CRC-checked below. run_base = rs232_count; fork_decode_done = 1'b0; fork_stim_done = 1'b0; fork begin decode_n_rs232(89); fork_decode_done = 1'b1; end begin // The TTL scenario decodes the RS232 line too (broadcast // TX), but the request must arrive on the owned line, so // the injection edge waits on the line under test. // Launch a long reply (83-byte GET_DATA) so the GET // provably overlaps it: the reply needs ~23000 clocks on // the wire while the 6-byte GET needs ~1700. launch_reply(8'h42, 8'd83, 8'hA0); // Wait until the reply is really transmitting, then // inject the GET mid-reply. The head start is counted in // the testbench clock domain (40 ps timestep = 1 ps in // DUT time is wrong: uart_byte advances simulation time, // not DUT clocks), so instead of a fixed clock count, arm // on the live TX line: the first TX start edge proves the // reply owns the wire right now. wait_for_tx_start_edge(); if (!tx_busy) $fatal(1, "reply not on wire at injection"); if (tx_done || tx_done_seen) $fatal(1, "reply already done at injection"); send_get(); await_req(8'h02, 8'd0); // The GET parsed while the reply was still transmitting // iff the reply's wire is still busy at this point (the // frame source may already be quiet while the last bytes // are still shifting out). if (!tx_busy || tx_done || tx_done_seen) $fatal(1, "reply completed before overlapping GET was parsed"); fork_stim_done = 1'b1; end join while (!fork_decode_done || !fork_stim_done) @(posedge clk); await_tx_done(); check_decoded_frame(run_base, 8'h42, 83); // The RX side must have parsed the GET injected while the reply // was transmitting. The fork's stim branch already proved the // GET parsed (it awaited req_valid through the live parser while // tx_busy was up); here the request is still held because // req_ready stayed low through the whole reply, so accepting it // now proves the parse survived to the end of TX. if (!req_valid) $fatal(1, "during-TX GET lost"); if (req_type !== 8'h02 || req_length !== 8'd0) $fatal(1, "during-TX GET metadata type=%h len=%0d", req_type, req_length); if (req_writes !== 0) $fatal(1, "during-TX GET writes=%0d", req_writes); accept_req(); // 8. A longer reply (NACK, 2 payload bytes) decodes byte-exact // on the RS232 line, then the identical reply is re-emitted and // decoded on the TTL line: both pins provably carry the same // broadcast stream. run_base = rs232_count; fork_decode_done = 1'b0; fork_stim_done = 1'b0; fork begin decode_n_rs232(8); fork_decode_done = 1'b1; end begin launch_reply(8'hC0, 8'd2, 8'h01); fork_stim_done = 1'b1; end join while (!fork_decode_done || !fork_stim_done) @(posedge clk); await_tx_done(); begin reg [7:0] n0, n1; n0 = rs232_stream[(run_base + 4) & 2047]; n1 = rs232_stream[(run_base + 5) & 2047]; if (n0 !== 8'h01 || n1 !== 8'h02) $fatal(1, "NACK payload mismatch %h %h", n0, n1); end check_decoded_frame(run_base, 8'hC0, 2); // Same reply again, decoded on the TTL pin this time. ttl_count = 0; fork begin decode_n_ttl(8); fork_decode_done = 1'b1; end begin launch_reply(8'hC0, 8'd2, 8'h01); fork_stim_done = 1'b1; end join while (!fork_decode_done || !fork_stim_done) @(posedge clk); await_tx_done(); if (ttl_stream[2] !== 8'hC0 || ttl_stream[3] !== 8'h02 || ttl_stream[4] !== 8'h01 || ttl_stream[5] !== 8'h02) $fatal(1, "TTL broadcast mismatch %h %h %h %h", ttl_stream[2], ttl_stream[3], ttl_stream[4], ttl_stream[5]); begin reg [7:0] t2, t3, t4, t5, t6, t7; reg [15:0] tcrc; t2 = ttl_stream[2]; t3 = ttl_stream[3]; t4 = ttl_stream[4]; t5 = ttl_stream[5]; t6 = ttl_stream[6]; t7 = ttl_stream[7]; tcrc = ttl_stream_crc(2, 4); if (tcrc !== {t7, t6}) $fatal(1, "TTL broadcast CRC mismatch calc=%h wire=%h%h", tcrc, t7, t6); end end endtask initial begin repeat (4) @(posedge clk); rst_n = 1'b1; repeat (4) @(posedge clk); // ---- RS232 as the active source ---- use_ttl = 1'b0; req_ready = 1'b0; run_scenario; // ---- TTL as the active source: same scenario ---- while (dut.rx_owner != 2'd0) @(posedge clk); repeat (4) @(posedge clk); use_ttl = 1'b1; // Fresh RX-side state for the TTL pass: the request/accept // handshake left req_ready low and the streams hold the RS232 // pass bytes. req_ready = 1'b0; rs232_count = 0; ttl_count = 0; run_scenario; // ---- wrong bit period: frames must not parse ---- // The TTL pass ends with the link owned (case 8 holds no request // but the owner releases only on transaction completion); force // the parser back to hunting with a bounded wait, then prove the // owner is free before switching lines. use_ttl = 1'b0; begin integer rel_guard; rel_guard = 0; while (dut.rx_owner != 2'd0 && rel_guard < 300000) begin @(posedge clk); rel_guard = rel_guard + 1; end if (dut.rx_owner != 2'd0) $fatal(1, "link never released, owner=%0d", dut.rx_owner); end repeat (4) @(posedge clk); use_wrong_period = 1'b1; send_get(); repeat (6000) @(posedge clk); if (req_valid) $fatal(1, "wrong-period frame parsed"); begin integer rel_guard2; rel_guard2 = 0; while (dut.rx_owner != 2'd0 && rel_guard2 < 300000) begin @(posedge clk); rel_guard2 = rel_guard2 + 1; end end repeat (4) @(posedge clk); use_wrong_period = 1'b0; // Link recovers at the nominal period. send_get(); await_req(8'h02, 8'd0); accept_req(); // ---- jittered edges at the nominal period: frames still parse ---- use_jitter = 1'b1; send_get(); await_req(8'h02, 8'd0); accept_req(); send_set32_hot5(); await_req(8'h01, 8'd32); if (req_mem[5] !== 8'h64) $fatal(1, "jittered hot5 ram[5]=%h", req_mem[5]); accept_req(); use_jitter = 1'b0; $display("host_uart_endpoint_tb PASS"); $finish; end initial begin #200000000; $fatal(1, "host_uart_endpoint deadline"); end endmodule