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
|
`include "defs.svh"
module rs232_tx
( input bit clock
, input bit reset
, output bit out_ready
, input bit out_valid
, input uart_byte_t out_data
, output bit tx
);
bit hold_valid;
uart_byte_t hold;
bit parity;
(* syn_encoding = "one-hot" *) enum int unsigned
{ START
, DATA
, PARITY
, STOP1
, STOP2
} state;
bit [$clog2(`UART_BYTE_BITS):0] data_bits;
always @(posedge clock) begin
if (reset) begin
out_ready = 0;
tx = 1;
hold_valid = 0;
parity = 0;
state = state.first;
data_bits = 0;
end else begin
if (out_ready && out_valid) begin
hold_valid = 1;
hold = out_data;
parity = 0;
state = state.first;
data_bits = 0;
end
if (hold_valid) begin
case (state)
START: begin
tx = 0;
state = state.next;
end
DATA: begin
tx = hold[data_bits];
parity = parity ^ tx;
if (data_bits == `UART_BYTE_BITS-1)
state = state.next;
else
++data_bits;
end
PARITY: begin
tx = parity;
state = state.next;
end
STOP1: begin
tx = 1;
state = state.next;
end
STOP2: begin
hold_valid = 0;
tx = 1;
state = state.next;
end
endcase
end else begin
tx = 1;
end
out_ready = !hold_valid;
end
end
endmodule
module rs232_rx
( input bit clock
, input bit reset
, input bit in_ready
, output bit in_valid
, output uart_byte_t in_data
, input bit rx
);
(* syn_encoding = "one-hot" *) enum int unsigned
{ START
, DATA
, PARITY
, STOP
} state;
uart_byte_t buffer;
bit [$clog2(`UART_BYTE_BITS):0] data_bits;
bit parity;
always @(posedge clock) begin
if (reset) begin
in_valid = 0;
state = state.first;
buffer = 0;
data_bits = 0;
parity = 0;
end else begin
if (in_ready && in_valid)
in_valid = 0;
case (state)
START: begin
if (rx == 0) begin
state = state.next;
buffer = 0;
data_bits = 0;
parity = 0;
end
end
DATA: begin
buffer[data_bits] = rx;
parity = parity ^ rx;
if (data_bits == `UART_BYTE_BITS-1)
state = state.next;
else
++data_bits;
end
PARITY: begin
parity = parity ^ rx;
if (parity == 0)
state = state.next;
else
state = state.first;
end
STOP: begin
if (!in_valid && rx == 1) begin
in_valid = 1;
in_data = buffer;
end
state = state.next;
end
endcase
end
end
endmodule
|