summaryrefslogtreecommitdiff
path: root/hdl/rs232.sv
diff options
context:
space:
mode:
Diffstat (limited to 'hdl/rs232.sv')
-rw-r--r--hdl/rs232.sv79
1 files changed, 49 insertions, 30 deletions
diff --git a/hdl/rs232.sv b/hdl/rs232.sv
index 9050464..31beb1e 100644
--- a/hdl/rs232.sv
+++ b/hdl/rs232.sv
@@ -87,6 +87,8 @@ module rs232_tx
87endmodule 87endmodule
88 88
89module rs232_rx 89module rs232_rx
90 #( OVERSAMPLE = 0
91 )
90 ( input bit clock 92 ( input bit clock
91 , input bit reset 93 , input bit reset
92 94
@@ -108,6 +110,8 @@ module rs232_rx
108 bit [$clog2(`UART_BYTE_BITS):0] data_bits; 110 bit [$clog2(`UART_BYTE_BITS):0] data_bits;
109 bit parity; 111 bit parity;
110 112
113 bit [$clog2(OVERSAMPLE+1):0] sample;
114
111 always @(posedge clock, posedge reset) begin 115 always @(posedge clock, posedge reset) begin
112 if (reset) begin 116 if (reset) begin
113 in_valid = 0; 117 in_valid = 0;
@@ -115,47 +119,62 @@ module rs232_rx
115 buffer = 0; 119 buffer = 0;
116 data_bits = 0; 120 data_bits = 0;
117 parity = 0; 121 parity = 0;
122 sample = 0;
118 end else begin 123 end else begin
124 automatic bit ok = 0;
125
119 if (in_ready && in_valid) 126 if (in_ready && in_valid)
120 in_valid = 0; 127 in_valid = 0;
121 128
122 case (state) 129 if (state == state.first) begin
130 ok = 1;
131 end else begin
132 ++sample;
133 if (sample == OVERSAMPLE+1) begin
134 sample = 0;
135 ok = 1;
136 end
137 end
138
139 if (ok) begin
140 case (state)
123 141
124 START: begin 142 START: begin
125 if (rx == 0) begin 143 if (rx == 0) begin
126 state = state.next; 144 state = state.next;
127 buffer = 0; 145 buffer = 0;
128 data_bits = 0; 146 data_bits = 0;
129 parity = 0; 147 parity = 0;
148 end
130 end 149 end
131 end
132 150
133 DATA: begin 151 DATA: begin
134 buffer[data_bits] = rx; 152 buffer[data_bits] = rx;
135 parity = parity ^ rx; 153 parity = parity ^ rx;
136 if (data_bits == `UART_BYTE_BITS-1) 154 if (data_bits == `UART_BYTE_BITS-1)
137 state = state.next; 155 state = state.next;
138 else 156 else
139 ++data_bits; 157 ++data_bits;
140 end 158 end
141 159
142 PARITY: begin 160 PARITY: begin
143 parity = parity ^ rx; 161 parity = parity ^ rx;
144 if (parity == 0) 162 if (parity == 0)
145 state = state.next; 163 state = state.next;
146 else 164 else
147 state = state.first; 165 state = state.first;
148 end 166 end
149 167
150 STOP: begin 168 STOP: begin
151 if (!in_valid && rx == 1) begin 169 if (!in_valid && rx == 1) begin
152 in_valid = 1; 170 in_valid = 1;
153 in_data = buffer; 171 in_data = buffer;
172 end
173 state = state.next;
154 end 174 end
155 state = state.next;
156 end
157 175
158 endcase 176 endcase
177 end
159 end 178 end
160 end 179 end
161 180