vt52-fpga  1.0.0 Initial
vt52-fpga is a serial terminal implemented on a FPGA
uart_tx.v
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2014-2017 Alex Forencich
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 
23 */
24 
25 // Language: Verilog 2001
26 
27 `timescale 1ns / 1ps
28 
29 /*
30  * AXI4-Stream UART
31  */
32 module uart_tx #
33 (
34  parameter DATA_WIDTH = 8
35 )
36 (
37  input wire clk,
38  input wire rst,
39 
40  /*
41  * AXI input
42  */
43  input wire [DATA_WIDTH-1:0] s_axis_tdata,
44  input wire s_axis_tvalid,
45  output wire s_axis_tready,
46 
47  /*
48  * UART interface
49  */
50  output wire txd,
51 
52  /*
53  * Status
54  */
55  output wire busy,
56 
57  /*
58  * Configuration
59  */
60  input wire [15:0] prescale
61 );
62 
63 reg s_axis_tready_reg = 0;
64 
65 reg txd_reg = 1;
66 
67 reg busy_reg = 0;
68 
69 reg [DATA_WIDTH:0] data_reg = 0;
70 reg [18:0] prescale_reg = 0;
71 reg [3:0] bit_cnt = 0;
72 
73 assign s_axis_tready = s_axis_tready_reg;
74 assign txd = txd_reg;
75 
76 assign busy = busy_reg;
77 
78 always @(posedge clk) begin
79  if (rst) begin
80  s_axis_tready_reg <= 0;
81  txd_reg <= 1;
82  prescale_reg <= 0;
83  bit_cnt <= 0;
84  busy_reg <= 0;
85  end else begin
86  if (prescale_reg > 0) begin
87  s_axis_tready_reg <= 0;
89  end else if (bit_cnt == 0) begin
90  s_axis_tready_reg <= 1;
91  busy_reg <= 0;
92 
93  if (s_axis_tvalid) begin
94  s_axis_tready_reg <= !s_axis_tready_reg;
95  prescale_reg <= (prescale << 3)-1;
96  bit_cnt <= DATA_WIDTH+1;
97  data_reg <= {1'b1, s_axis_tdata};
98  txd_reg <= 0;
99  busy_reg <= 1;
100  end
101  end else begin
102  if (bit_cnt > 1) begin
103  bit_cnt <= bit_cnt - 1;
104  prescale_reg <= (prescale << 3)-1;
105  {data_reg, txd_reg} <= {1'b0, data_reg};
106  end else if (bit_cnt == 1) begin
107  bit_cnt <= bit_cnt - 1;
108  prescale_reg <= (prescale << 3);
109  txd_reg <= 1;
110  end
111  end
112  end
113 end
114 
115 endmodule
module uart_tx(parameter DATA_WIDTH=8)(input wire clk
module input wire input wire< DATA_WIDTH-1:0 > input wire output wire output wire output wire input wire< 15:0 > prescale
Definition: uart_tx.v:61
module input wire input wire< DATA_WIDTH-1:0 > input wire s_axis_tvalid
Definition: uart_tx.v:44
module input wire input wire< DATA_WIDTH-1:0 > input wire output wire output wire txd
Definition: uart_tx.v:50
reg< 3:0 > bit_cnt
Definition: uart_tx.v:71
always(posedge clk)
Definition: uart_tx.v:78
module input wire rst
Definition: uart_tx.v:38
reg< 18:0 > prescale_reg
Definition: uart_tx.v:70
reg txd_reg
Definition: uart_tx.v:65
module input wire input wire< DATA_WIDTH-1:0 > input wire output wire s_axis_tready
Definition: uart_tx.v:45
reg busy_reg
Definition: uart_tx.v:67
reg< DATA_WIDTH:0 > data_reg
Definition: uart_tx.v:69
module input wire input wire< DATA_WIDTH-1:0 > s_axis_tdata
Definition: uart_tx.v:43
module input wire input wire< DATA_WIDTH-1:0 > input wire output wire output wire output wire busy
Definition: uart_tx.v:55