`timescale 1ns/1ps // frame_tx + uart_tx_byte at real bit timing (CLKS_PER_BIT=27 for 921600 // baud at 25 MHz): a frame_tx with LENGTH_BYTES=1 streams final 8-bit // protocol frames into uart_tx_byte; every byte on the wire is decoded at // bit level and checked against the independent Python codec vectors // (tools/crc16_ccitt_table.frame_bytes), hand-embedded below. // // A second, deliberately wrong-period uart_rx_byte observes the same wire // and must NOT reproduce the image (proving the bit-level check is live), // while a nominal-period receiver proves the image end to end. module frame_tx_uart_tx_tb; localparam integer CLKS = 27; reg sys_clk = 1'b0; reg rst_n = 1'b0; reg start = 1'b0; reg [7:0] frame_type = 8'h82; reg [7:0] payload_length = 8'd27; wire busy; wire byte_valid; wire [7:0] byte_data; wire byte_ready; wire [15:0] payload_addr; // Payload ROM stand-in: addr0 -> 01, addr1 -> 04 (NACK shape), else FF. wire [7:0] payload_data = (payload_addr == 16'd0) ? 8'h01 : (payload_addr == 16'd1) ? 8'h04 : 8'hFF; wire done; wire tx_line; wire tx_busy; wire tx_byte_done; always #20 sys_clk = ~sys_clk; // 25 MHz frame_tx #(.PAYLOAD_IDX_BITS(8), .LENGTH_BYTES(1)) u_frame_tx ( .clk(sys_clk), .rst_n(rst_n), .start(start), .frame_type(frame_type), .payload_length({8'd0, payload_length}), .busy(busy), .byte_valid(byte_valid), .byte_data(byte_data), .byte_ready(byte_ready), .payload_addr(payload_addr), .payload_data(payload_data), .done(done), .crc_start_o(), .crc_data_valid_o(), .crc_data_o(), .crc_busy_i(1'b0), .crc_data_done_i(1'b0), .crc_value_i(16'd0) ); uart_tx_byte #(.CLKS_PER_BIT(CLKS)) u_uart_tx ( .clk(sys_clk), .rst_n(rst_n), .byte_valid(byte_valid), .byte_data(byte_data), .tx_o(tx_line), .busy(tx_busy), .byte_done(tx_byte_done) ); // Level handshake (same shape as the production endpoint): the frame // source may advance whenever the line driver is idle. A pulse // handshake on tx_byte_done races the producer/consumer and repeats // bytes (observed here 2026-09-20: byte 0 sent twice). assign byte_ready = !tx_busy; // Loop the wire back into two receivers: nominal period (must decode // the exact image) and wrong period (must NOT). wire rx_ok_valid; wire [7:0] rx_ok_data; wire rx_ok_err; uart_rx_byte #(.CLKS_PER_BIT(CLKS)) u_rx_ok ( .clk(sys_clk), .rst_n(rst_n), .rx_i(tx_line), .byte_valid(rx_ok_valid), .byte_data(rx_ok_data), .framing_error(rx_ok_err) ); wire rx_bad_valid; wire [7:0] rx_bad_data; wire rx_bad_err; uart_rx_byte #(.CLKS_PER_BIT(CLKS + 2)) u_rx_bad ( .clk(sys_clk), .rst_n(rst_n), .rx_i(tx_line), .byte_valid(rx_bad_valid), .byte_data(rx_bad_data), .framing_error(rx_bad_err) ); reg [7:0] ok_stream [0:63]; reg [7:0] bad_stream [0:63]; integer ok_count; integer bad_count; initial begin ok_count = 0; bad_count = 0; end always @(posedge sys_clk) begin if (rx_ok_valid) begin ok_stream[ok_count] <= rx_ok_data; ok_count <= ok_count + 1; end if (rx_bad_valid) begin bad_stream[bad_count] <= rx_bad_data; bad_count <= bad_count + 1; end end // Bit-level UART decoder for the wire image: one full bit period after // the start edge lands mid-d0 (the negedge fires up to one clock after // the fall and the transmitter holds every bit for a full CLKS // clocks); stepping CLKS per bit stays mid-bit. reg [7:0] decode_shift; integer decode_bit; task decode_byte; begin @(negedge tx_line); #(CLKS * 40); for (decode_bit = 0; decode_bit < 8; decode_bit = decode_bit + 1) begin #1080; decode_shift[decode_bit] = tx_line; end #1080; if (tx_line !== 1'b1) $fatal(1, "UART stop bit is low"); end endtask task launch_start; input [7:0] ftype; input [7:0] flen; begin frame_type = ftype; payload_length = flen; @(negedge sys_clk); start = 1'b1; @(negedge sys_clk); start = 1'b0; end endtask reg [7:0] cap00, cap01, cap02, cap03, cap04, cap05, cap30, cap31, cap32; // GET_ACK, 27-byte payload (addr0 -> 01, addr1 -> 04, rest FF): // independently computed wire image A5 5A 82 1B 01 04 FF..FF 54 B9. task get_ack_frame; begin launch_start(8'h82, 8'd27); decode_byte; cap00 = decode_shift; decode_byte; cap01 = decode_shift; decode_byte; cap02 = decode_shift; decode_byte; cap03 = decode_shift; decode_byte; cap04 = decode_shift; decode_byte; cap05 = decode_shift; begin integer k; for (k = 0; k < 25; k = k + 1) begin if (k == 24) cap30 = decode_shift; decode_byte; end end decode_byte; cap31 = decode_shift; decode_byte; cap32 = decode_shift; end endtask initial begin repeat (4) @(posedge sys_clk); rst_n = 1'b1; repeat (4) @(posedge sys_clk); // ---- GET_ACK with a 27-byte payload ---- get_ack_frame; if (cap00 !== 8'hA5 || cap01 !== 8'h5A || cap02 !== 8'h82 || cap03 !== 8'h1B) begin $fatal(1, "UART frame header mismatch: %h %h %h %h", cap00, cap01, cap02, cap03); end if (cap04 !== 8'h01 || cap05 !== 8'h04 || cap30 !== 8'hFF) begin $fatal(1, "UART payload mismatch: %h %h %h", cap04, cap05, cap30); end if (cap31 !== 8'h54 || cap32 !== 8'hB9) begin $fatal(1, "UART CRC mismatch: %h %h", cap31, cap32); end // ---- SET_ACK, zero length: golden A5 5A 81 00 A6 35 ---- launch_start(8'h81, 8'd0); decode_byte; cap00 = decode_shift; decode_byte; cap01 = decode_shift; decode_byte; cap02 = decode_shift; decode_byte; cap03 = decode_shift; decode_byte; cap04 = decode_shift; decode_byte; cap05 = decode_shift; if (cap00 !== 8'hA5 || cap01 !== 8'h5A || cap02 !== 8'h81 || cap03 !== 8'h00 || cap04 !== 8'hA6 || cap05 !== 8'h35) begin $fatal(1, "SET_ACK wire mismatch: %h %h %h %h %h %h", cap00, cap01, cap02, cap03, cap04, cap05); end // The wire idles high after the last stop bit: no 7th byte. repeat (CLKS * 12) begin @(posedge sys_clk); if (tx_line !== 1'b1) $fatal(1, "unexpected 7th UART byte"); end // ---- loopback cross-checks (both receivers ran live above) ---- $display("t=%0t loopback nominal=%0d wrong=%0d", $time, ok_count, bad_count); if (ok_count !== 39) $fatal(1, "nominal loopback byte count %0d, want 39", ok_count); if (ok_stream[0] !== 8'hA5 || ok_stream[1] !== 8'h5A || ok_stream[2] !== 8'h82 || ok_stream[3] !== 8'h1B) $fatal(1, "nominal loopback header mismatch: %h %h %h %h", ok_stream[0], ok_stream[1], ok_stream[2], ok_stream[3]); if (ok_stream[33] !== 8'hA5 || ok_stream[34] !== 8'h5A || ok_stream[35] !== 8'h81 || ok_stream[36] !== 8'h00 || ok_stream[37] !== 8'hA6 || ok_stream[38] !== 8'h35) $fatal(1, "nominal loopback SET_ACK mismatch: %h %h %h %h %h %h", ok_stream[33], ok_stream[34], ok_stream[35], ok_stream[36], ok_stream[37], ok_stream[38]); // The wrong-period receiver must not reproduce the OK stream. if (bad_count === 39) begin if (bad_stream[0] === ok_stream[0] && bad_stream[1] === ok_stream[1] && bad_stream[2] === ok_stream[2] && bad_stream[3] === ok_stream[3] && bad_stream[33] === ok_stream[33] && bad_stream[38] === ok_stream[38]) $fatal(1, "wrong-period receiver reproduced the image"); end $display("frame_tx_uart_tx_tb PASS"); $finish; end initial begin #100000000; $fatal(1, "UART frame timeout"); end endmodule