-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathuart_tx.v
75 lines (63 loc) · 1.18 KB
/
uart_tx.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* uart_tx.v
*
* Simple UART 8N1 TX
*
* Copyright (C) 2018 Sylvain Munaut
*
* vim: ts=4 sw=4
*/
`ifdef SIM
`default_nettype none
`endif
module uart_tx #(
parameter integer DIV_WIDTH = 8
)(
input wire [7:0] data,
input wire valid,
output reg ack,
output wire tx,
input wire [DIV_WIDTH-1:0] div,
input wire clk,
input wire rst
);
// Signals
wire go, done, ce;
reg active;
reg [9:0] shift;
reg [DIV_WIDTH:0] div_cnt;
reg [4:0] bit_cnt;
// Control
assign go = valid & ~active;
assign done = ce & bit_cnt[4];
always @(posedge clk)
if (rst)
active <= 1'b0;
else
active <= (active & ~done) | go;
// Baud rate generator
always @(posedge clk)
if (~active | div_cnt[DIV_WIDTH])
div_cnt <= { 1'b0, div };
else
div_cnt <= div_cnt - 1;
assign ce = div_cnt[DIV_WIDTH];
// Bit counter
always @(posedge clk)
if (~active)
bit_cnt <= 5'h08;
else if (ce)
bit_cnt <= bit_cnt - 1;
// Shift register
always @(posedge clk)
if (rst)
shift <= 10'h3ff;
else if (go)
shift <= { 1'b1, data, 1'b0 };
else if (ce)
shift <= { 1'b1, shift[9:1] };
// Outputs
always @(posedge clk)
ack <= go;
assign tx = shift[0];
endmodule // uart_tx