Advertisement
AquaBlitz11

uart_tx_simpler.sv

Mar 5th, 2025 (edited)
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `default_nettype none
  2.  
  3. module uart_tx_simpler #(
  4.     parameter CLOCK_FREQ_HZ = 0,
  5.     parameter BAUD_RATE = 0
  6. ) (
  7.     // axi inputs
  8.     input wire s_axis_aclk,
  9.     input wire s_axis_aresetn,
  10.     input wire s_axis_tvalid,
  11.     input wire [7:0] s_axis_tdata,
  12.     output logic s_axis_tready,
  13.     // uart outputs
  14.     output logic tx_bit
  15. );
  16.  
  17.   localparam IDLE = 0;
  18.   localparam START = 1;
  19.   localparam DATA_0 = 2;
  20.   localparam DATA_7 = 9;
  21.   localparam STOP = 10;
  22.   logic [3:0] state = IDLE;
  23.  
  24.   localparam UART_CYCLES = CLOCK_FREQ_HZ / BAUD_RATE;
  25.   localparam UART_CYCLES_BITS = $clog2(UART_CYCLES);
  26.   logic [UART_CYCLES_BITS-1:0] cycle_count;
  27.  
  28.   logic end_of_cycle;
  29.   assign end_of_cycle = cycle_count == UART_CYCLES - 1;
  30.  
  31.   logic [7:0] data_latched;
  32.  
  33.   always_ff @(posedge s_axis_aclk) begin
  34.     if (state == IDLE) begin
  35.       if (s_axis_tvalid && s_axis_tready) begin
  36.         data_latched <= s_axis_tdata;
  37.         state <= START;
  38.         cycle_count <= 0;
  39.       end
  40.     end else begin
  41.       if (end_of_cycle) begin
  42.         if (state >= DATA_0 && state <= DATA_7) data_latched <= data_latched >> 1;
  43.         state <= state == STOP ? IDLE : state + 1;
  44.         cycle_count <= 0;
  45.       end else begin
  46.         cycle_count <= cycle_count + 1;
  47.       end
  48.     end
  49.  
  50.     if (s_axis_aresetn == 0) begin
  51.       state <= IDLE;
  52.     end
  53.   end
  54.  
  55.   localparam IDLE_BIT = 1'b1;
  56.   localparam START_BIT = 1'b0;
  57.   localparam STOP_BIT = 1'b1;
  58.  
  59.   assign s_axis_tready = state == IDLE;
  60.   always_comb begin
  61.     tx_bit = data_latched[0];
  62.     if (state == IDLE) tx_bit = IDLE_BIT;
  63.     if (state == START) tx_bit = START_BIT;
  64.     if (state == STOP) tx_bit = STOP_BIT;
  65.   end
  66.  
  67. endmodule
  68.  
  69. `default_nettype wire
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement