-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_transfer.v
108 lines (98 loc) · 2.85 KB
/
uart_transfer.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
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
`timescale 1ns / 1ps
module uart_transfer(
input wire clk,
input wire reset_n,
input wire tx_start,
input wire b_tick,
input wire [7:0] d_in,
output reg tx_done,
output wire tx
);
localparam [1:0] idle_st = 2'b00;
localparam [1:0] start_st = 2'b01;
localparam [1:0] data_st = 2'b11;
localparam [1:0] stop_st = 2'b10;
reg [1:0] current_state;
reg [1:0] next_state;
reg [3:0] b_reg;
reg [3:0] b_next;
reg [2:0] count_reg;
reg [2:0] count_next;
reg [7:0] data_reg;
reg [7:0] data_next;
reg tx_reg;
reg tx_next;
always @(posedge clk or negedge reset_n) begin
if(!reset_n) begin
current_state <= idle_st;
b_reg <= 0;
count_reg <= 0;
data_reg <= 0;
tx_reg <= 1'b1;
end else begin
current_state <= next_state;
b_reg <= b_next;
count_reg <= count_next;
data_reg <= data_next;
tx_reg <= tx_next;
end
end
always @(*) begin
next_state = current_state;
tx_done = 1'b0;
b_next = b_reg;
count_next = count_reg;
data_next = data_reg;
tx_next = tx_reg;
case (current_state)
idle_st : begin
tx_next = 1'b1;
if(tx_start) begin
next_state = start_st;
b_next = 0;
data_next = d_in;
end
end
start_st : begin
tx_next = 1'b0;
if(b_tick) begin
if(b_reg == 15) begin
next_state = data_st;
b_next = 0;
count_next = 0;
end else begin
b_next = b_reg + 1;
end
end
end
data_st : begin
tx_next = data_reg[0];
if(b_tick) begin
if(b_reg == 15) begin
b_next = 0;
data_next = data_reg >> 1;
if(count_reg == 7) begin
next_state = stop_st;
end else begin
count_next = count_reg + 1;
end
end else begin
b_next = b_reg + 1;
end
end
end
stop_st : begin
tx_next = 1'b1;
if(b_tick) begin
if(b_reg == 15) begin
next_state = idle_st;
tx_done = 1'b1;
end else begin
b_next = b_reg + 1;
end
end
end
endcase
end
assign tx = tx_reg;
endmodule