-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsimpleuart.v
162 lines (133 loc) · 4.8 KB
/
simpleuart.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// iceZ0mb1e - FPGA 8-Bit TV80 SoC for Lattice iCE40
// with complete open-source toolchain flow using yosys and SDCC
//
// Copyright (c) 2021 Franz Neumann ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
module simpleuart(
input clk,
input reset,
input[15:0] baud_divider,
input[7:0] data_write,
input transmit,
output ready,
output[7:0] data_read,
output received,
output tx,
input rx
);
localparam STATE_IDLE = 0;
localparam STATE_RX_STARTBIT = 1;
localparam STATE_RX_RECEIVE = 2;
localparam STATE_RX_STOPBIT = 3;
localparam STATE_TX_STARTBIT = 4;
localparam STATE_TX_SEND = 5;
localparam STATE_TX_STOPBIT = 6;
reg [2:0] fsm_state = STATE_IDLE;
wire ready = (fsm_state == STATE_IDLE) ? 1'b 1 : 1'b 0;
reg received;
reg [3:0] bitcount;
wire bitcount_last = (bitcount == 8'd 1) ? 1'b 1 : 1'b 0;
reg [7:0] data_read;
reg [7:0] shiftreg;
wire clken_baud;
wire clken_sample;
reg tx;
reg rx_sampled;
reg [1:0] sync_rx;
reg [1:0] sync_transmit;
reg req_start_transmit = 1'b 0;
clk_enable baud_clock (
.reset( (fsm_state == STATE_IDLE) ),
.divider(baud_divider),
.clk_in(clk),
.clk_en(clken_baud)
);
clk_enable sample_clock (
.reset(clken_baud),
.divider( {1'b 0, baud_divider[15:1] } ),
.clk_in(clk),
.clk_en(clken_sample)
);
always @(posedge clk) begin
if (reset == 1'b1) begin
fsm_state <= STATE_IDLE;
end else begin
sync_rx <= {sync_rx[0], rx};
sync_transmit <= {sync_transmit[0], transmit};
if(sync_transmit == 2'b 01)begin
req_start_transmit <= 1'b 1;
end
if( clken_sample == 1'b 1) begin
rx_sampled <= rx;
end
if( clken_baud == 1'b 1) begin
shiftreg <= { rx_sampled, shiftreg[7:1] };
bitcount <= bitcount - 8'd 1;
end
if( fsm_state == STATE_IDLE )begin
bitcount <= 8'd 9;
tx <= 1'b 1;
received <= 1'b 0;
if( sync_rx[1:0] == 2'b 10 ) begin
fsm_state <= STATE_RX_STARTBIT;
end
if( req_start_transmit == 1'b 1 ) begin
req_start_transmit <= 1'b 0;
tx <= 1'b 0; // Startbit
shiftreg <= data_write;
fsm_state <= STATE_TX_STARTBIT;
end
end
if( clken_baud == 1'b 1 ) begin
case(fsm_state)
STATE_IDLE: begin
// done without clk_en
end
STATE_RX_STARTBIT: begin
fsm_state <= STATE_RX_RECEIVE;
end
STATE_RX_RECEIVE: begin
if( bitcount_last ) begin
fsm_state <= STATE_RX_STOPBIT;
end
end
STATE_RX_STOPBIT: begin
received <= 1'b 1;
data_read <= shiftreg;
fsm_state <= STATE_IDLE;
end
STATE_TX_STARTBIT: begin
tx <= shiftreg[0];
fsm_state <= STATE_TX_SEND;
end
STATE_TX_SEND: begin
tx <= shiftreg[0];
if( bitcount_last ) begin
tx <= 1'b 1; // Stopbit
fsm_state <= STATE_TX_STOPBIT;
end
end
STATE_TX_STOPBIT: begin
fsm_state <= STATE_IDLE;
end
endcase
end
end
end
endmodule