// Echo top level: every byte received on uart_rxd is sent back on uart_txd. // Connect uart_rxd/uart_txd to the board's USB-UART bridge pins. `timescale 1ns / 1ps module uart_echo_top #( parameter int CLK_FREQ = 100_000_000, // set to your board's clock parameter int BAUD = 115_200 ) ( input logic clk, input logic rst_n, // push button, active low (invert if yours is active high) input logic uart_rxd, // from the PC (bridge TX pin) output logic uart_txd, // to the PC (bridge RX pin) output logic led_err // lights after a framing error, cleared by reset ); // Reset synchroniser. FPGA flip-flops load their initial value when the // device is configured, so the design starts in reset for two cycles. // The lint tool warns about initial values because ASIC flip-flops have // none. On an FPGA they are defined, so the warning is switched off here. /* verilator lint_off PROCASSINIT */ logic [1:0] rst_pipe = 2'b11; /* verilator lint_on PROCASSINIT */ logic rst; always_ff @(posedge clk) rst_pipe <= {rst_pipe[0], ~rst_n}; assign rst = rst_pipe[1]; logic [7:0] rx_data; logic rx_valid; logic rx_frame_err; logic [7:0] hold_data; // one-byte buffer between RX and TX logic hold_full; logic tx_ready; uart_rx #(.CLK_FREQ(CLK_FREQ), .BAUD(BAUD)) u_rx ( .clk(clk), .rst(rst), .rxd(uart_rxd), .rx_data(rx_data), .rx_valid(rx_valid), .rx_frame_err(rx_frame_err) ); uart_tx #(.CLK_FREQ(CLK_FREQ), .BAUD(BAUD)) u_tx ( .clk(clk), .rst(rst), .tx_data(hold_data), .tx_valid(hold_full), .tx_ready(tx_ready), .txd(uart_txd) ); always_ff @(posedge clk) begin if (rst) begin hold_data <= 8'h00; hold_full <= 1'b0; led_err <= 1'b0; end else begin if (rx_valid) begin hold_data <= rx_data; // a new byte always wins hold_full <= 1'b1; end else if (hold_full && tx_ready) begin hold_full <= 1'b0; // TX took the byte this cycle end if (rx_frame_err) led_err <= 1'b1; end end endmodule