-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathuartrx.v
81 lines (72 loc) · 1.56 KB
/
uartrx.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
module uartrx #(parameter CLKDIV = (100000000/115200)) (
input clk,
input rst,
input rx,
output reg [7:0] q,
output reg strobe,
output [1:0] leds);
localparam
Idle = 0, ReadingStartBit = 1,
ReadingData = 2, ReadingStopBit = 3;
reg [1:0] state, nextstate;
reg [15:0] bitclock;
reg [2:0] bitcount;
wire fullbittime = bitclock == CLKDIV-1;
wire halfbittime = bitclock == (CLKDIV/2);
wire [2:0]maxbit = 3'd7;
wire lastbit = bitcount == 0;
assign leds = state;
always @(*) begin
nextstate = state;
case (state)
Idle:
if (~rx) nextstate = ReadingStartBit;
ReadingStartBit:
if (rx) nextstate = Idle;
else if (halfbittime) nextstate = ReadingData;
ReadingData:
if (fullbittime & lastbit) nextstate = ReadingStopBit;
ReadingStopBit:
if (fullbittime) nextstate = Idle;
endcase
end
always @(posedge clk)
if (rst)
state <= Idle;
else
state <= nextstate;
always @(posedge clk)
case (state)
ReadingStartBit:
bitcount <= maxbit;
ReadingData:
if (fullbittime) bitcount <= bitcount - 1'd1;
default:
bitcount <= 0;
endcase
always @(posedge clk)
case (state)
Idle:
bitclock <= 0;
ReadingStartBit:
if (halfbittime) bitclock <= 0;
else bitclock <= bitclock + 1'd1;
default:
if (fullbittime) bitclock <= 0;
else bitclock <= bitclock + 1'd1;
endcase
always @(posedge clk)
if (rst) begin
q <= 0;
strobe <= 0;
end else case (state)
ReadingData:
if (fullbittime) begin
q <= {rx, q[7:1]};
if (lastbit)
strobe <= 1'b1;
end
default:
strobe <= 0;
endcase
endmodule