decode1: Implement obsolete dst, dstst, dss instructions as no-ops
[microwatt.git] / uart16550 / uart_receiver.v
1 //////////////////////////////////////////////////////////////////////
2 //// ////
3 //// uart_receiver.v ////
4 //// ////
5 //// ////
6 //// This file is part of the "UART 16550 compatible" project ////
7 //// http://www.opencores.org/cores/uart16550/ ////
8 //// ////
9 //// Documentation related to this project: ////
10 //// - http://www.opencores.org/cores/uart16550/ ////
11 //// ////
12 //// Projects compatibility: ////
13 //// - WISHBONE ////
14 //// RS232 Protocol ////
15 //// 16550D uart (mostly supported) ////
16 //// ////
17 //// Overview (main Features): ////
18 //// UART core receiver logic ////
19 //// ////
20 //// Known problems (limits): ////
21 //// None known ////
22 //// ////
23 //// To Do: ////
24 //// Thourough testing. ////
25 //// ////
26 //// Author(s): ////
27 //// - gorban@opencores.org ////
28 //// - Jacob Gorban ////
29 //// - Igor Mohor (igorm@opencores.org) ////
30 //// ////
31 //// Created: 2001/05/12 ////
32 //// Last Updated: 2001/05/17 ////
33 //// (See log for the revision history) ////
34 //// ////
35 //// ////
36 //////////////////////////////////////////////////////////////////////
37 //// ////
38 //// Copyright (C) 2000, 2001 Authors ////
39 //// ////
40 //// This source file may be used and distributed without ////
41 //// restriction provided that this copyright statement is not ////
42 //// removed from the file and that any derivative work contains ////
43 //// the original copyright notice and the associated disclaimer. ////
44 //// ////
45 //// This source file is free software; you can redistribute it ////
46 //// and/or modify it under the terms of the GNU Lesser General ////
47 //// Public License as published by the Free Software Foundation; ////
48 //// either version 2.1 of the License, or (at your option) any ////
49 //// later version. ////
50 //// ////
51 //// This source is distributed in the hope that it will be ////
52 //// useful, but WITHOUT ANY WARRANTY; without even the implied ////
53 //// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
54 //// PURPOSE. See the GNU Lesser General Public License for more ////
55 //// details. ////
56 //// ////
57 //// You should have received a copy of the GNU Lesser General ////
58 //// Public License along with this source; if not, download it ////
59 //// from http://www.opencores.org/lgpl.shtml ////
60 //// ////
61 //////////////////////////////////////////////////////////////////////
62 //
63 // CVS Revision History
64 //
65 // $Log: not supported by cvs2svn $
66 // Revision 1.29 2002/07/29 21:16:18 gorban
67 // The uart_defines.v file is included again in sources.
68 //
69 // Revision 1.28 2002/07/22 23:02:23 gorban
70 // Bug Fixes:
71 // * Possible loss of sync and bad reception of stop bit on slow baud rates fixed.
72 // Problem reported by Kenny.Tung.
73 // * Bad (or lack of ) loopback handling fixed. Reported by Cherry Withers.
74 //
75 // Improvements:
76 // * Made FIFO's as general inferrable memory where possible.
77 // So on FPGA they should be inferred as RAM (Distributed RAM on Xilinx).
78 // This saves about 1/3 of the Slice count and reduces P&R and synthesis times.
79 //
80 // * Added optional baudrate output (baud_o).
81 // This is identical to BAUDOUT* signal on 16550 chip.
82 // It outputs 16xbit_clock_rate - the divided clock.
83 // It's disabled by default. Define UART_HAS_BAUDRATE_OUTPUT to use.
84 //
85 // Revision 1.27 2001/12/30 20:39:13 mohor
86 // More than one character was stored in case of break. End of the break
87 // was not detected correctly.
88 //
89 // Revision 1.26 2001/12/20 13:28:27 mohor
90 // Missing declaration of rf_push_q fixed.
91 //
92 // Revision 1.25 2001/12/20 13:25:46 mohor
93 // rx push changed to be only one cycle wide.
94 //
95 // Revision 1.24 2001/12/19 08:03:34 mohor
96 // Warnings cleared.
97 //
98 // Revision 1.23 2001/12/19 07:33:54 mohor
99 // Synplicity was having troubles with the comment.
100 //
101 // Revision 1.22 2001/12/17 14:46:48 mohor
102 // overrun signal was moved to separate block because many sequential lsr
103 // reads were preventing data from being written to rx fifo.
104 // underrun signal was not used and was removed from the project.
105 //
106 // Revision 1.21 2001/12/13 10:31:16 mohor
107 // timeout irq must be set regardless of the rda irq (rda irq does not reset the
108 // timeout counter).
109 //
110 // Revision 1.20 2001/12/10 19:52:05 gorban
111 // Igor fixed break condition bugs
112 //
113 // Revision 1.19 2001/12/06 14:51:04 gorban
114 // Bug in LSR[0] is fixed.
115 // All WISHBONE signals are now sampled, so another wait-state is introduced on all transfers.
116 //
117 // Revision 1.18 2001/12/03 21:44:29 gorban
118 // Updated specification documentation.
119 // Added full 32-bit data bus interface, now as default.
120 // Address is 5-bit wide in 32-bit data bus mode.
121 // Added wb_sel_i input to the core. It's used in the 32-bit mode.
122 // Added debug interface with two 32-bit read-only registers in 32-bit mode.
123 // Bits 5 and 6 of LSR are now only cleared on TX FIFO write.
124 // My small test bench is modified to work with 32-bit mode.
125 //
126 // Revision 1.17 2001/11/28 19:36:39 gorban
127 // Fixed: timeout and break didn't pay attention to current data format when counting time
128 //
129 // Revision 1.16 2001/11/27 22:17:09 gorban
130 // Fixed bug that prevented synthesis in uart_receiver.v
131 //
132 // Revision 1.15 2001/11/26 21:38:54 gorban
133 // Lots of fixes:
134 // Break condition wasn't handled correctly at all.
135 // LSR bits could lose their values.
136 // LSR value after reset was wrong.
137 // Timing of THRE interrupt signal corrected.
138 // LSR bit 0 timing corrected.
139 //
140 // Revision 1.14 2001/11/10 12:43:21 gorban
141 // Logic Synthesis bugs fixed. Some other minor changes
142 //
143 // Revision 1.13 2001/11/08 14:54:23 mohor
144 // Comments in Slovene language deleted, few small fixes for better work of
145 // old tools. IRQs need to be fix.
146 //
147 // Revision 1.12 2001/11/07 17:51:52 gorban
148 // Heavily rewritten interrupt and LSR subsystems.
149 // Many bugs hopefully squashed.
150 //
151 // Revision 1.11 2001/10/31 15:19:22 gorban
152 // Fixes to break and timeout conditions
153 //
154 // Revision 1.10 2001/10/20 09:58:40 gorban
155 // Small synopsis fixes
156 //
157 // Revision 1.9 2001/08/24 21:01:12 mohor
158 // Things connected to parity changed.
159 // Clock devider changed.
160 //
161 // Revision 1.8 2001/08/23 16:05:05 mohor
162 // Stop bit bug fixed.
163 // Parity bug fixed.
164 // WISHBONE read cycle bug fixed,
165 // OE indicator (Overrun Error) bug fixed.
166 // PE indicator (Parity Error) bug fixed.
167 // Register read bug fixed.
168 //
169 // Revision 1.6 2001/06/23 11:21:48 gorban
170 // DL made 16-bit long. Fixed transmission/reception bugs.
171 //
172 // Revision 1.5 2001/06/02 14:28:14 gorban
173 // Fixed receiver and transmitter. Major bug fixed.
174 //
175 // Revision 1.4 2001/05/31 20:08:01 gorban
176 // FIFO changes and other corrections.
177 //
178 // Revision 1.3 2001/05/27 17:37:49 gorban
179 // Fixed many bugs. Updated spec. Changed FIFO files structure. See CHANGES.txt file.
180 //
181 // Revision 1.2 2001/05/21 19:12:02 gorban
182 // Corrected some Linter messages.
183 //
184 // Revision 1.1 2001/05/17 18:34:18 gorban
185 // First 'stable' release. Should be sythesizable now. Also added new header.
186 //
187 // Revision 1.0 2001-05-17 21:27:11+02 jacob
188 // Initial revision
189 //
190 //
191
192 `include "uart_defines.v"
193
194 module uart_receiver (clk, wb_rst_i, lcr, rf_pop, srx_pad_i, enable,
195 counter_t, rf_count, rf_data_out, rf_error_bit, rf_overrun, rx_reset, lsr_mask, rstate, rf_push_pulse);
196
197 input clk;
198 input wb_rst_i;
199 input [7:0] lcr;
200 input rf_pop;
201 input srx_pad_i;
202 input enable;
203 input rx_reset;
204 input lsr_mask;
205
206 output [9:0] counter_t;
207 output [`UART_FIFO_COUNTER_W-1:0] rf_count;
208 output [`UART_FIFO_REC_WIDTH-1:0] rf_data_out;
209 output rf_overrun;
210 output rf_error_bit;
211 output [3:0] rstate;
212 output rf_push_pulse;
213
214 reg [3:0] rstate;
215 reg [3:0] rcounter16;
216 reg [2:0] rbit_counter;
217 reg [7:0] rshift; // receiver shift register
218 reg rparity; // received parity
219 reg rparity_error;
220 reg rframing_error; // framing error flag
221 reg rparity_xor;
222 reg [7:0] counter_b; // counts the 0 (low) signals
223 reg rf_push_q;
224
225 // RX FIFO signals
226 reg [`UART_FIFO_REC_WIDTH-1:0] rf_data_in;
227 wire [`UART_FIFO_REC_WIDTH-1:0] rf_data_out;
228 wire rf_push_pulse;
229 reg rf_push;
230 wire rf_pop;
231 wire rf_overrun;
232 wire [`UART_FIFO_COUNTER_W-1:0] rf_count;
233 wire rf_error_bit; // an error (parity or framing) is inside the fifo
234 wire break_error = (counter_b == 0);
235
236 // RX FIFO instance
237 uart_rfifo #(`UART_FIFO_REC_WIDTH) fifo_rx(
238 .clk( clk ),
239 .wb_rst_i( wb_rst_i ),
240 .data_in( rf_data_in ),
241 .data_out( rf_data_out ),
242 .push( rf_push_pulse ),
243 .pop( rf_pop ),
244 .overrun( rf_overrun ),
245 .count( rf_count ),
246 .error_bit( rf_error_bit ),
247 .fifo_reset( rx_reset ),
248 .reset_status(lsr_mask)
249 );
250
251 wire rcounter16_eq_7 = (rcounter16 == 4'd7);
252 wire rcounter16_eq_0 = (rcounter16 == 4'd0);
253
254 wire [3:0] rcounter16_minus_1 = rcounter16 - 3'd1;
255
256 parameter sr_idle = 4'd0;
257 parameter sr_rec_start = 4'd1;
258 parameter sr_rec_bit = 4'd2;
259 parameter sr_rec_parity = 4'd3;
260 parameter sr_rec_stop = 4'd4;
261 parameter sr_check_parity = 4'd5;
262 parameter sr_rec_prepare = 4'd6;
263 parameter sr_end_bit = 4'd7;
264 parameter sr_ca_lc_parity = 4'd8;
265 parameter sr_wait1 = 4'd9;
266 parameter sr_push = 4'd10;
267
268
269 always @(posedge clk or posedge wb_rst_i)
270 begin
271 if (wb_rst_i)
272 begin
273 rstate <= sr_idle;
274 rcounter16 <= 0;
275 rbit_counter <= 0;
276 rparity_xor <= 1'b0;
277 rframing_error <= 1'b0;
278 rparity_error <= 1'b0;
279 rparity <= 1'b0;
280 rshift <= 0;
281 rf_push <= 1'b0;
282 rf_data_in <= 0;
283 end
284 else
285 if (enable)
286 begin
287 case (rstate)
288 sr_idle : begin
289 rf_push <= 1'b0;
290 rf_data_in <= 0;
291 rcounter16 <= 4'b1110;
292 if (srx_pad_i==1'b0 & ~break_error) // detected a pulse (start bit?)
293 begin
294 rstate <= sr_rec_start;
295 end
296 end
297 sr_rec_start : begin
298 rf_push <= 1'b0;
299 if (rcounter16_eq_7) // check the pulse
300 if (srx_pad_i==1'b1) // no start bit
301 rstate <= sr_idle;
302 else // start bit detected
303 rstate <= sr_rec_prepare;
304 rcounter16 <= rcounter16_minus_1;
305 end
306 sr_rec_prepare:begin
307 case (lcr[/*`UART_LC_BITS*/1:0]) // number of bits in a word
308 2'b00 : rbit_counter <= 3'b100;
309 2'b01 : rbit_counter <= 3'b101;
310 2'b10 : rbit_counter <= 3'b110;
311 2'b11 : rbit_counter <= 3'b111;
312 endcase
313 if (rcounter16_eq_0)
314 begin
315 rstate <= sr_rec_bit;
316 rcounter16 <= 4'b1110;
317 rshift <= 0;
318 end
319 else
320 rstate <= sr_rec_prepare;
321 rcounter16 <= rcounter16_minus_1;
322 end
323 sr_rec_bit : begin
324 if (rcounter16_eq_0)
325 rstate <= sr_end_bit;
326 if (rcounter16_eq_7) // read the bit
327 case (lcr[/*`UART_LC_BITS*/1:0]) // number of bits in a word
328 2'b00 : rshift[4:0] <= {srx_pad_i, rshift[4:1]};
329 2'b01 : rshift[5:0] <= {srx_pad_i, rshift[5:1]};
330 2'b10 : rshift[6:0] <= {srx_pad_i, rshift[6:1]};
331 2'b11 : rshift[7:0] <= {srx_pad_i, rshift[7:1]};
332 endcase
333 rcounter16 <= rcounter16_minus_1;
334 end
335 sr_end_bit : begin
336 if (rbit_counter==3'b0) // no more bits in word
337 if (lcr[`UART_LC_PE]) // choose state based on parity
338 rstate <= sr_rec_parity;
339 else
340 begin
341 rstate <= sr_rec_stop;
342 rparity_error <= 1'b0; // no parity - no error :)
343 end
344 else // else we have more bits to read
345 begin
346 rstate <= sr_rec_bit;
347 rbit_counter <= rbit_counter - 3'd1;
348 end
349 rcounter16 <= 4'b1110;
350 end
351 sr_rec_parity: begin
352 if (rcounter16_eq_7) // read the parity
353 begin
354 rparity <= srx_pad_i;
355 rstate <= sr_ca_lc_parity;
356 end
357 rcounter16 <= rcounter16_minus_1;
358 end
359 sr_ca_lc_parity : begin // rcounter equals 6
360 rcounter16 <= rcounter16_minus_1;
361 rparity_xor <= ^{rshift,rparity}; // calculate parity on all incoming data
362 rstate <= sr_check_parity;
363 end
364 sr_check_parity: begin // rcounter equals 5
365 case ({lcr[`UART_LC_EP],lcr[`UART_LC_SP]})
366 2'b00: rparity_error <= rparity_xor == 0; // no error if parity 1
367 2'b01: rparity_error <= ~rparity; // parity should sticked to 1
368 2'b10: rparity_error <= rparity_xor == 1; // error if parity is odd
369 2'b11: rparity_error <= rparity; // parity should be sticked to 0
370 endcase
371 rcounter16 <= rcounter16_minus_1;
372 rstate <= sr_wait1;
373 end
374 sr_wait1 : if (rcounter16_eq_0)
375 begin
376 rstate <= sr_rec_stop;
377 rcounter16 <= 4'b1110;
378 end
379 else
380 rcounter16 <= rcounter16_minus_1;
381 sr_rec_stop : begin
382 if (rcounter16_eq_7) // read the parity
383 begin
384 rframing_error <= !srx_pad_i; // no framing error if input is 1 (stop bit)
385 rstate <= sr_push;
386 end
387 rcounter16 <= rcounter16_minus_1;
388 end
389 sr_push : begin
390 ///////////////////////////////////////
391 // $display($time, ": received: %b", rf_data_in);
392 if(srx_pad_i | break_error)
393 begin
394 if(break_error)
395 rf_data_in <= {8'b0, 3'b100}; // break input (empty character) to receiver FIFO
396 else
397 rf_data_in <= {rshift, 1'b0, rparity_error, rframing_error};
398 rf_push <= 1'b1;
399 rstate <= sr_idle;
400 end
401 else if(~rframing_error) // There's always a framing before break_error -> wait for break or srx_pad_i
402 begin
403 rf_data_in <= {rshift, 1'b0, rparity_error, rframing_error};
404 rf_push <= 1'b1;
405 rcounter16 <= 4'b1110;
406 rstate <= sr_rec_start;
407 end
408
409 end
410 default : rstate <= sr_idle;
411 endcase
412 end // if (enable)
413 end // always of receiver
414
415 always @ (posedge clk or posedge wb_rst_i)
416 begin
417 if(wb_rst_i)
418 rf_push_q <= 0;
419 else
420 rf_push_q <= rf_push;
421 end
422
423 assign rf_push_pulse = rf_push & ~rf_push_q;
424
425
426 //
427 // Break condition detection.
428 // Works in conjuction with the receiver state machine
429
430 reg [9:0] toc_value; // value to be set to timeout counter
431
432 always @(lcr)
433 case (lcr[3:0])
434 4'b0000 : toc_value = 447; // 7 bits
435 4'b0100 : toc_value = 479; // 7.5 bits
436 4'b0001, 4'b1000 : toc_value = 511; // 8 bits
437 4'b1100 : toc_value = 543; // 8.5 bits
438 4'b0010, 4'b0101, 4'b1001 : toc_value = 575; // 9 bits
439 4'b0011, 4'b0110, 4'b1010, 4'b1101 : toc_value = 639; // 10 bits
440 4'b0111, 4'b1011, 4'b1110 : toc_value = 703; // 11 bits
441 4'b1111 : toc_value = 767; // 12 bits
442 endcase // case(lcr[3:0])
443
444 wire [7:0] brc_value; // value to be set to break counter
445 assign brc_value = toc_value[9:2]; // the same as timeout but 1 insead of 4 character times
446
447 always @(posedge clk or posedge wb_rst_i)
448 begin
449 if (wb_rst_i)
450 counter_b <= 8'd159;
451 else
452 if (srx_pad_i)
453 counter_b <= brc_value; // character time length - 1
454 else
455 if(enable & counter_b != 8'b0) // only work on enable times break not reached.
456 counter_b <= counter_b - 8'd1; // decrement break counter
457 end // always of break condition detection
458
459 ///
460 /// Timeout condition detection
461 reg [9:0] counter_t; // counts the timeout condition clocks
462
463 always @(posedge clk or posedge wb_rst_i)
464 begin
465 if (wb_rst_i)
466 counter_t <= 10'd639; // 10 bits for the default 8N1
467 else
468 if(rf_push_pulse || rf_pop || rf_count == 0) // counter is reset when RX FIFO is empty, accessed or above trigger level
469 counter_t <= toc_value;
470 else
471 if (enable && counter_t != 10'b0) // we don't want to underflow
472 counter_t <= counter_t - 10'd1;
473 end
474
475 endmodule