-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart_tx.v
More file actions
120 lines (107 loc) · 2.98 KB
/
uart_tx.v
File metadata and controls
120 lines (107 loc) · 2.98 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
module uart_tx #(
parameter integer DATA_BIT = 8
) (
// clocks & ticks
input wire clk,
input wire rst_n,
input wire tick,
// transmit handshake
input wire tx_valid,
output reg tx_ready,
// data to send
input wire [DATA_BIT-1:0] tx_data,
// serial output Idle: 1 Start:0 Stop:1
output reg tx_serial
);
localparam integer bit_cnt_size = 3;
/*
IDLE: 4'd0
START: 4'd1
DATA_0-DATA_7: 4'd2-4'd9
STOP: 4'd10
*/
localparam IDLE=0, START=1, DATA=2, STOP=3;
reg [bit_cnt_size-1:0] bit_cnt,next_bit_cnt;
reg [DATA_BIT-1:0] shift,next_shift;
reg [3:0] state, next_state;
reg next_tx_ready, next_tx_serial;
//FSM
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
state <= IDLE;
shift <= 0;
tx_ready <= 1;
tx_serial <= 1;
bit_cnt <= 0;
end else begin
state <= next_state;
shift <= next_shift;
tx_ready <= next_tx_ready;
tx_serial <= next_tx_serial;
bit_cnt <= next_bit_cnt;
end
end
//Combinational Logic
always @(*) begin
//Default
next_state = state;
next_shift = shift;
next_tx_serial = 1;
next_tx_ready = 0;
next_bit_cnt = bit_cnt;
case (state)
//IDLE
IDLE: begin
next_tx_ready = 1;
if (tx_valid) begin
next_shift = tx_data;
next_state = START;
next_tx_ready = 0;
end
end
//Start
START: begin
next_tx_serial = 0;
if (tick) begin
next_state = DATA;
next_bit_cnt = 0;
end
end
//data 0 - 7
DATA: begin
next_tx_ready = 0;
next_tx_serial = shift[0];
if(tick) begin
next_shift = shift >> 1;
if (bit_cnt == DATA_BIT-1) begin
next_state = STOP;
next_bit_cnt = 0;
end else begin
next_state = DATA;
next_bit_cnt = bit_cnt + 1;
end
end
end
//Stop
STOP: begin
next_tx_ready = 0;
next_tx_serial = 1;
if (tick)
next_state = IDLE;
end
endcase
end
//Log 2
// function integer log2;
// input integer value;
// integer v;
// begin
// log2 = 0;
// v = value - 1;
// while (v > 0) begin
// log2 = log2 + 1;
// v = v >> 1;
// end
// end
// endfunction
endmodule