`timescale 1ns/1ps // UART-level end-to-end test for the 6400 Host wire path over the final // 8-bit-length protocol, driven through the shared host_uart_endpoint // (real 921600/25 MHz bit timing: CLKS_PER_BIT=27) with the product's own // frame sizes: SET_ALL 32-byte payloads and an 83-byte GET_DATA reply. // // Request frames, the reply image and the expected CRCs are the vectors // from the independent Python codec (tools/crc16_ccitt_table.frame_bytes), // hand-embedded here; the TB verifies the reply CRC with its own // bit-serial CRC-16/CCITT-FALSE over the decoded wire bytes, never with // the DUT's accumulator. // // NOTE (protocol worker, 2026-09-20): this bench deliberately covers the // shared transport primitives plus the 6400 frame sizes. The product // command core (thermal_host_command) is mid-migration to the frozen // 8-bit macro set by its owner; when it lands, its own host-level bench // re-attaches the command semantics. Everything this bench drives is in // this worker's scope. module prod2u_6400_uart_e2e_tb; localparam integer CLKS = 27; localparam integer 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; reg req_ready = 1'b0; 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; 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; integer i; integer k; always #20 clk = ~clk; // 25 MHz host_uart_endpoint #( .CLKS_PER_BIT(CLKS), .INTER_BYTE_TIMEOUT_CYCLES(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 staging RAM (128 bytes), parser owns the write port; the // endpoint's read address drives the synchronous read port for // validation/commit. reg [7:0] req_mem [0:127]; initial begin for (i = 0; i < 128; i = i + 1) req_mem[i] = 8'hA9; end always @(posedge clk) begin if (req_ram_wr_en) req_mem[req_ram_wr_addr] <= req_ram_wr_data; end assign req_ram_rd_data = req_mem[req_ram_rd_addr]; integer req_writes; initial req_writes = 0; always @(negedge clk) begin if (req_ram_wr_en) begin req_writes = req_writes + 1; if (req_ram_wr_addr >= 128) $fatal(1, "request RAM address out of range %0d", req_ram_wr_addr); end end // Reply staging RAM (128 bytes, synchronous read) feeds frame_tx. reg rep_wr_en = 1'b0; reg [7:0] rep_wr_addr = 8'd0; reg [7:0] rep_wr_data = 8'd0; reply_ram #(.DEPTH(128), .ADDR_WIDTH(8)) u_reply ( .clk(clk), .wr_en(rep_wr_en), .wr_addr(rep_wr_addr), .wr_data(rep_wr_data), .rd_addr(tx_ram_rd_addr), .rd_data(tx_ram_rd_data) ); // ---- independent CRC-16/CCITT-FALSE over decoded wire bytes ---- reg [7:0] rx_snapshot [0:511]; integer rx_snapshot_n; initial rx_snapshot_n = 0; // CRC over the framed body (TYPE, LENGTH, PAYLOAD): starts at the // byte after the A5 5A header, exactly as the wire contract defines. function [15:0] tb_crc; 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 ^ {rx_snapshot[(2 + ck) & 511], 8'd0}; for (bitno = 0; bitno < 8; bitno = bitno + 1) c = c[15] ? (c << 1) ^ 16'h1021 : c << 1; end tb_crc = c; end endfunction // ---- real UART stimulus on the RS232 RX line ---- task send_byte; input [7:0] value; integer kk; begin rs232_rx = 1'b0; #1080; for (kk = 0; kk < 8; kk = kk + 1) begin rs232_rx = value[kk]; #1080; end rs232_rx = 1'b1; #1080; end endtask task send_get; begin send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h02); send_byte(8'h00); send_byte(8'h6D); send_byte(8'h7B); end endtask task send_set32_zero; integer kk; begin send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01); send_byte(8'h20); for (kk = 0; kk < 32; kk = kk + 1) send_byte(8'h00); send_byte(8'h0D); send_byte(8'h8C); end endtask task send_set32_hot5; integer kk; begin send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01); send_byte(8'h20); for (kk = 0; kk < 32; kk = kk + 1) send_byte((kk == 5) ? 8'h64 : 8'h00); send_byte(8'h32); send_byte(8'h5C); end endtask task await_req; input [7:0] etype; input [7:0] elen; integer guard; begin 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); if (req_type !== etype || req_length !== elen) $fatal(1, "request metadata mismatch type=%h len=%0d", req_type, req_length); end endtask task accept_req; begin 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 // ---- TX decoder: samples RS232 and TTL at the same bit centres, so // every decoded byte also proves the broadcast copies agree bit for // bit. Records into rx_snapshot for the independent CRC. ---- reg [7:0] dec_byte; task decode_byte_both; integer b; begin // Byte assembled in a scalar first, then stored: the same // shape the proven host_uart_endpoint decoder uses (direct // bit-by-bit stores into a task-written array read back as // zeros under Icarus, 2026-09-20). // Mid-bit sampling: 1.5 bit periods after the start fall // lands in the middle of data bit 0 (the transmitter changes // the line every CLKS clocks; sampling at exactly one bit // period races that edge), then one bit per sample. @(negedge rs232_tx); #(CLKS * 40 + CLKS * 20); for (b = 0; b < 8; b = b + 1) begin if (rs232_tx !== ttl_tx) $fatal(1, "TX broadcast diverged at bit %0d", b); dec_byte[b] = rs232_tx; if (b < 7) #1080; end rx_snapshot[rx_snapshot_n & 511] = dec_byte; rx_snapshot_n = rx_snapshot_n + 1; #1080; if (rs232_tx !== 1'b1 || ttl_tx !== 1'b1) $fatal(1, "TX stop bit is low"); end endtask task decode_and_check; input [7:0] rtype; input integer rlen; integer n; reg [15:0] c; begin rx_snapshot_n = 0; for (n = 0; n < rlen + 6; n = n + 1) decode_byte_both; if (rx_snapshot[0] !== 8'hA5 || rx_snapshot[1] !== 8'h5A) $fatal(1, "reply magic mismatch"); if (rx_snapshot[2] !== rtype || rx_snapshot[3] !== rlen[7:0]) $fatal(1, "reply header mismatch type=%h len=%0d", rx_snapshot[2], rx_snapshot[3]); c = tb_crc(rlen + 2); if (c !== {rx_snapshot[rlen + 5], rx_snapshot[rlen + 4]}) $fatal(1, "reply CRC mismatch calc=%h wire=%h%h", c, rx_snapshot[rlen + 5], rx_snapshot[rlen + 4]); end endtask task prefill_reply_pattern; input [7:0] base; input [7:0] step; integer n2; begin // Drive the reply-RAM write port at the falling edge: a value // set right after a rising edge is sampled by that same edge // in Icarus's event order, which silently dropped writes // (observed 2026-09-20). for (n2 = 0; n2 < 128; n2 = n2 + 1) begin @(negedge clk); rep_wr_en = 1'b1; rep_wr_addr = n2[7:0]; rep_wr_data = base + (step * n2[7:0]); end @(negedge clk); rep_wr_en = 1'b0; repeat (4) @(posedge clk); end endtask task launch_reply; input [7:0] rtype; input [7:0] rlen; 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"); tx_type = rtype; tx_length = rlen; tx_start = 1'b1; while (!tx_busy) @(posedge clk); @(posedge clk); tx_start = 1'b0; end endtask initial begin repeat (4) @(posedge clk); rst_n = 1'b1; repeat (4) @(posedge clk); // ---- 1. SET_ALL 32 zero bytes ---- // Wire image A5 5A 01 20 + 32x00 + 0D 8C (independent codec). req_writes = 0; send_set32_zero(); await_req(8'h01, 8'd32); if (req_writes !== 32) $fatal(1, "SET32 writes=%0d", req_writes); for (k = 0; k < 32; k = k + 1) if (req_mem[k] !== 8'h00) $fatal(1, "SET32 ram[%0d]=%h", k, req_mem[k]); if (req_mem[32] !== 8'hA9) $fatal(1, "SET32 spilled past extent"); // the validation read port holds the last staged byte if (req_ram_rd_addr !== 8'd31) $fatal(1, "validation read address %0d", req_ram_rd_addr); if (req_ram_rd_data !== 8'h00) $fatal(1, "validation read data %h", req_ram_rd_data); accept_req(); // ---- 2. SET_ALL with index 5 = 100 (0x64), wire CRC 32 5C ---- req_writes = 0; send_set32_hot5(); await_req(8'h01, 8'd32); if (req_writes !== 32) $fatal(1, "hot5 writes=%0d", req_writes); if (req_mem[5] !== 8'h64) $fatal(1, "hot5 ram[5]=%h", req_mem[5]); if (req_mem[6] !== 8'h00) $fatal(1, "hot5 ram[6]=%h", req_mem[6]); accept_req(); // ---- 3. GET_ALL with an empty payload (golden A5 5A 02 00 6D 7B) ---- req_writes = 0; send_get(); await_req(8'h02, 8'd0); if (req_writes !== 0) $fatal(1, "GET writes=%0d", req_writes); accept_req(); // ---- 4. GET_DATA reply, 83-byte payload through reply_ram ---- prefill_reply_pattern(8'h03, 8'h07); // byte k = 0x03 + 7k (mod 256) launch_reply(8'h42, 8'd83); decode_and_check(8'h42, 83); for (k = 0; k < 83; k = k + 1) if (rx_snapshot[4 + k] !== (8'h03 + 8'h07 * k[7:0])) $fatal(1, "GET_DATA payload[%0d]=%h", k, rx_snapshot[4 + k]); // ---- 5. SET_ACK reply, golden vector A5 5A 81 00 A6 35 ---- prefill_reply_pattern(8'h00, 8'h00); launch_reply(8'h81, 8'd0); decode_and_check(8'h81, 0); if (rx_snapshot[4] !== 8'hA6 || rx_snapshot[5] !== 8'h35) $fatal(1, "SET_ACK wire mismatch %h %h", rx_snapshot[4], rx_snapshot[5]); // ---- 6. NACK reply, golden vector A5 5A C0 02 01 04 B1 2A ---- @(negedge clk); rep_wr_en = 1'b1; rep_wr_addr = 8'd0; rep_wr_data = 8'h01; @(negedge clk); rep_wr_addr = 8'd1; rep_wr_data = 8'h04; @(negedge clk); rep_wr_en = 1'b0; repeat (4) @(posedge clk); launch_reply(8'hC0, 8'd2); decode_and_check(8'hC0, 2); if (rx_snapshot[4] !== 8'h01 || rx_snapshot[5] !== 8'h04 || rx_snapshot[6] !== 8'hB1 || rx_snapshot[7] !== 8'h2A) $fatal(1, "NACK wire mismatch %h %h %h %h", rx_snapshot[4], rx_snapshot[5], rx_snapshot[6], rx_snapshot[7]); // ---- 7. the link still parses a legal request after all replies ---- send_get(); await_req(8'h02, 8'd0); accept_req(); $display("prod2u_6400_uart_e2e_tb PASS"); $finish; end initial begin #400000000; $fatal(1, "6400 e2e deadline"); end endmodule