OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [orp/] [orp_soc/] [rtl/] [verilog/] [uart16550.old/] [uart_receiver.v] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 746 lampret
//////////////////////////////////////////////////////////////////////
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.27  2001/12/30 20:39:13  mohor
67
// More than one character was stored in case of break. End of the break
68
// was not detected correctly.
69
//
70
// Revision 1.26  2001/12/20 13:28:27  mohor
71
// Missing declaration of rf_push_q fixed.
72
//
73
// Revision 1.25  2001/12/20 13:25:46  mohor
74
// rx push changed to be only one cycle wide.
75
//
76
// Revision 1.24  2001/12/19 08:03:34  mohor
77
// Warnings cleared.
78
//
79
// Revision 1.23  2001/12/19 07:33:54  mohor
80
// Synplicity was having troubles with the comment.
81
//
82
// Revision 1.22  2001/12/17 14:46:48  mohor
83
// overrun signal was moved to separate block because many sequential lsr
84
// reads were preventing data from being written to rx fifo.
85
// underrun signal was not used and was removed from the project.
86
//
87
// Revision 1.21  2001/12/13 10:31:16  mohor
88
// timeout irq must be set regardless of the rda irq (rda irq does not reset the
89
// timeout counter).
90
//
91
// Revision 1.20  2001/12/10 19:52:05  gorban
92
// Igor fixed break condition bugs
93
//
94
// Revision 1.19  2001/12/06 14:51:04  gorban
95
// Bug in LSR[0] is fixed.
96
// All WISHBONE signals are now sampled, so another wait-state is introduced on all transfers.
97
//
98
// Revision 1.18  2001/12/03 21:44:29  gorban
99
// Updated specification documentation.
100
// Added full 32-bit data bus interface, now as default.
101
// Address is 5-bit wide in 32-bit data bus mode.
102
// Added wb_sel_i input to the core. It's used in the 32-bit mode.
103
// Added debug interface with two 32-bit read-only registers in 32-bit mode.
104
// Bits 5 and 6 of LSR are now only cleared on TX FIFO write.
105
// My small test bench is modified to work with 32-bit mode.
106
//
107
// Revision 1.17  2001/11/28 19:36:39  gorban
108
// Fixed: timeout and break didn't pay attention to current data format when counting time
109
//
110
// Revision 1.16  2001/11/27 22:17:09  gorban
111
// Fixed bug that prevented synthesis in uart_receiver.v
112
//
113
// Revision 1.15  2001/11/26 21:38:54  gorban
114
// Lots of fixes:
115
// Break condition wasn't handled correctly at all.
116
// LSR bits could lose their values.
117
// LSR value after reset was wrong.
118
// Timing of THRE interrupt signal corrected.
119
// LSR bit 0 timing corrected.
120
//
121
// Revision 1.14  2001/11/10 12:43:21  gorban
122
// Logic Synthesis bugs fixed. Some other minor changes
123
//
124
// Revision 1.13  2001/11/08 14:54:23  mohor
125
// Comments in Slovene language deleted, few small fixes for better work of
126
// old tools. IRQs need to be fix.
127
//
128
// Revision 1.12  2001/11/07 17:51:52  gorban
129
// Heavily rewritten interrupt and LSR subsystems.
130
// Many bugs hopefully squashed.
131
//
132
// Revision 1.11  2001/10/31 15:19:22  gorban
133
// Fixes to break and timeout conditions
134
//
135
// Revision 1.10  2001/10/20 09:58:40  gorban
136
// Small synopsis fixes
137
//
138
// Revision 1.9  2001/08/24 21:01:12  mohor
139
// Things connected to parity changed.
140
// Clock devider changed.
141
//
142
// Revision 1.8  2001/08/23 16:05:05  mohor
143
// Stop bit bug fixed.
144
// Parity bug fixed.
145
// WISHBONE read cycle bug fixed,
146
// OE indicator (Overrun Error) bug fixed.
147
// PE indicator (Parity Error) bug fixed.
148
// Register read bug fixed.
149
//
150
// Revision 1.6  2001/06/23 11:21:48  gorban
151
// DL made 16-bit long. Fixed transmission/reception bugs.
152
//
153
// Revision 1.5  2001/06/02 14:28:14  gorban
154
// Fixed receiver and transmitter. Major bug fixed.
155
//
156
// Revision 1.4  2001/05/31 20:08:01  gorban
157
// FIFO changes and other corrections.
158
//
159
// Revision 1.3  2001/05/27 17:37:49  gorban
160
// Fixed many bugs. Updated spec. Changed FIFO files structure. See CHANGES.txt file.
161
//
162
// Revision 1.2  2001/05/21 19:12:02  gorban
163
// Corrected some Linter messages.
164
//
165
// Revision 1.1  2001/05/17 18:34:18  gorban
166
// First 'stable' release. Should be sythesizable now. Also added new header.
167
//
168
// Revision 1.0  2001-05-17 21:27:11+02  jacob
169
// Initial revision
170
//
171
//
172
 
173
// synopsys translate_off
174
`include "timescale.v"
175
// synopsys translate_on
176
 
177
`include "uart_defines.v"
178
 
179
module uart_receiver (clk, wb_rst_i, lcr, rf_pop, srx_pad_i, enable,
180
        counter_t, rf_count, rf_data_out, rf_error_bit, rf_overrun, rx_reset, lsr_mask, rstate, rf_push_pulse);
181
 
182
input                           clk;
183
input                           wb_rst_i;
184
input   [7:0]    lcr;
185
input                           rf_pop;
186
input                           srx_pad_i;
187
input                           enable;
188
input                           rx_reset;
189
input       lsr_mask;
190
 
191
output  [9:0]                    counter_t;
192
output  [`UART_FIFO_COUNTER_W-1:0]       rf_count;
193
output  [`UART_FIFO_REC_WIDTH-1:0]       rf_data_out;
194
output                          rf_overrun;
195
output                          rf_error_bit;
196
output [3:0]             rstate;
197
output                          rf_push_pulse;
198
 
199
reg     [3:0]    rstate;
200
reg     [3:0]    rcounter16;
201
reg     [2:0]    rbit_counter;
202
reg     [7:0]    rshift;                 // receiver shift register
203
reg             rparity;                // received parity
204
reg             rparity_error;
205
reg             rframing_error;         // framing error flag
206
reg             rbit_in;
207
reg             rparity_xor;
208
reg     [7:0]    counter_b;      // counts the 0 (low) signals
209
reg   rf_push_q;
210
 
211
// RX FIFO signals
212
reg     [`UART_FIFO_REC_WIDTH-1:0]       rf_data_in;
213
wire    [`UART_FIFO_REC_WIDTH-1:0]       rf_data_out;
214
wire      rf_push_pulse;
215
reg                             rf_push;
216
wire                            rf_pop;
217
wire                            rf_overrun;
218
wire    [`UART_FIFO_COUNTER_W-1:0]       rf_count;
219
wire                            rf_error_bit; // an error (parity or framing) is inside the fifo
220
wire                            break_error = (counter_b == 0);
221
 
222
// RX FIFO instance
223
uart_fifo #(`UART_FIFO_REC_WIDTH) fifo_rx(
224
        .clk(           clk             ),
225
        .wb_rst_i(      wb_rst_i        ),
226
        .data_in(       rf_data_in      ),
227
        .data_out(      rf_data_out     ),
228
        .push(          rf_push_pulse           ),
229
        .pop(           rf_pop          ),
230
        .overrun(       rf_overrun      ),
231
        .count(         rf_count        ),
232
        .error_bit(     rf_error_bit    ),
233
        .fifo_reset(    rx_reset        ),
234
        .reset_status(lsr_mask)
235
);
236
 
237
wire            rcounter16_eq_7 = (rcounter16 == 4'd7);
238
wire            rcounter16_eq_0 = (rcounter16 == 4'd0);
239
wire            rcounter16_eq_1 = (rcounter16 == 4'd1);
240
 
241
wire [3:0] rcounter16_minus_1 = rcounter16 - 1'b1;
242
 
243
parameter  sr_idle                                      = 4'd0;
244
parameter  sr_rec_start                         = 4'd1;
245
parameter  sr_rec_bit                           = 4'd2;
246
parameter  sr_rec_parity                        = 4'd3;
247
parameter  sr_rec_stop                          = 4'd4;
248
parameter  sr_check_parity              = 4'd5;
249
parameter  sr_rec_prepare                       = 4'd6;
250
parameter  sr_end_bit                           = 4'd7;
251
parameter  sr_ca_lc_parity            = 4'd8;
252
parameter  sr_wait1                                     = 4'd9;
253
parameter  sr_push                                      = 4'd10;
254
parameter  sr_last                                      = 4'd11;
255
 
256
 
257
always @(posedge clk or posedge wb_rst_i)
258
begin
259
  if (wb_rst_i)
260
  begin
261
     rstate                     <= #1 sr_idle;
262
          rbit_in                               <= #1 1'b0;
263
          rcounter16                    <= #1 0;
264
          rbit_counter          <= #1 0;
265
          rparity_xor           <= #1 1'b0;
266
          rframing_error        <= #1 1'b0;
267
          rparity_error                 <= #1 1'b0;
268
          rparity                               <= #1 1'b0;
269
          rshift                                <= #1 0;
270
          rf_push                               <= #1 1'b0;
271
          rf_data_in                    <= #1 0;
272
  end
273
  else
274
  if (enable)
275
  begin
276
        case (rstate)
277
        sr_idle : begin
278
                        rf_push                           <= #1 1'b0;
279
                        rf_data_in        <= #1 0;
280
                        if (srx_pad_i==1'b0 & ~break_error)   // detected a pulse (start bit?)
281
                        begin
282
                                rstate            <= #1 sr_rec_start;
283
                                rcounter16        <= #1 4'b1110;
284
                        end
285
                end
286
        sr_rec_start :  begin
287
                                if (rcounter16_eq_7)    // check the pulse
288
                                        if (srx_pad_i==1'b1)   // no start bit
289
                                                rstate <= #1 sr_idle;
290
                                        else            // start bit detected
291
                                                rstate <= #1 sr_rec_prepare;
292
                                rcounter16 <= #1 rcounter16_minus_1;
293
                        end
294
        sr_rec_prepare:begin
295
                                case (lcr[/*`UART_LC_BITS*/1:0])  // number of bits in a word
296
                                2'b00 : rbit_counter <= #1 3'b100;
297
                                2'b01 : rbit_counter <= #1 3'b101;
298
                                2'b10 : rbit_counter <= #1 3'b110;
299
                                2'b11 : rbit_counter <= #1 3'b111;
300
                                endcase
301
                                if (rcounter16_eq_0)
302
                                begin
303
                                        rstate          <= #1 sr_rec_bit;
304
                                        rcounter16      <= #1 4'b1110;
305
                                        rshift          <= #1 0;
306
                                end
307
                                else
308
                                        rstate <= #1 sr_rec_prepare;
309
                                rcounter16 <= #1 rcounter16_minus_1;
310
                        end
311
        sr_rec_bit :    begin
312
                                if (rcounter16_eq_0)
313
                                        rstate <= #1 sr_end_bit;
314
                                if (rcounter16_eq_7) // read the bit
315
                                        case (lcr[/*`UART_LC_BITS*/1:0])  // number of bits in a word
316
                                        2'b00 : rshift[4:0]  <= #1 {srx_pad_i, rshift[4:1]};
317
                                        2'b01 : rshift[5:0]  <= #1 {srx_pad_i, rshift[5:1]};
318
                                        2'b10 : rshift[6:0]  <= #1 {srx_pad_i, rshift[6:1]};
319
                                        2'b11 : rshift[7:0]  <= #1 {srx_pad_i, rshift[7:1]};
320
                                        endcase
321
                                rcounter16 <= #1 rcounter16_minus_1;
322
                        end
323
        sr_end_bit :   begin
324
                                if (rbit_counter==3'b0) // no more bits in word
325
                                        if (lcr[`UART_LC_PE]) // choose state based on parity
326
                                                rstate <= #1 sr_rec_parity;
327
                                        else
328
                                        begin
329
                                                rstate <= #1 sr_rec_stop;
330
                                                rparity_error <= #1 1'b0;  // no parity - no error :)
331
                                        end
332
                                else            // else we have more bits to read
333
                                begin
334
                                        rstate <= #1 sr_rec_bit;
335
                                        rbit_counter <= #1 rbit_counter - 1'b1;
336
                                end
337
                                rcounter16 <= #1 4'b1110;
338
                        end
339
        sr_rec_parity: begin
340
                                if (rcounter16_eq_7)    // read the parity
341
                                begin
342
                                        rparity <= #1 srx_pad_i;
343
                                        rstate <= #1 sr_ca_lc_parity;
344
                                end
345
                                rcounter16 <= #1 rcounter16_minus_1;
346
                        end
347
        sr_ca_lc_parity : begin    // rcounter equals 6
348
                                rcounter16  <= #1 rcounter16_minus_1;
349
                                rparity_xor <= #1 ^{rshift,rparity}; // calculate parity on all incoming data
350
                                rstate      <= #1 sr_check_parity;
351
                          end
352
        sr_check_parity: begin    // rcounter equals 5
353
                                case ({lcr[`UART_LC_EP],lcr[`UART_LC_SP]})
354
                                        2'b00: rparity_error <= #1  rparity_xor == 0;  // no error if parity 1
355
                                        2'b01: rparity_error <= #1 ~rparity;      // parity should sticked to 1
356
                                        2'b10: rparity_error <= #1  rparity_xor == 1;   // error if parity is odd
357
                                        2'b11: rparity_error <= #1  rparity;      // parity should be sticked to 0
358
                                endcase
359
                                rcounter16 <= #1 rcounter16_minus_1;
360
                                rstate <= #1 sr_wait1;
361
                          end
362
        sr_wait1 :      if (rcounter16_eq_0)
363
                        begin
364
                                rstate <= #1 sr_rec_stop;
365
                                rcounter16 <= #1 4'b1110;
366
                        end
367
                        else
368
                                rcounter16 <= #1 rcounter16_minus_1;
369
        sr_rec_stop :   begin
370
                                if (rcounter16_eq_7)    // read the parity
371
                                begin
372
                                        rframing_error <= #1 !srx_pad_i; // no framing error if input is 1 (stop bit)
373
                                        rstate <= #1 sr_push;
374
                                end
375
                                rcounter16 <= #1 rcounter16_minus_1;
376
                        end
377
        sr_push :       begin
378
///////////////////////////////////////
379
//                              $display($time, ": received: %b", rf_data_in);
380
        if(srx_pad_i | break_error)
381
          begin
382
            if(break_error)
383
                          rf_data_in    <= #1 {8'b0, 3'b100}; // break input (empty character) to receiver FIFO
384
            else
385
                                rf_data_in  <= #1 {rshift, 1'b0, rparity_error, rframing_error};
386
                  rf_push                 <= #1 1'b1;
387
                                rstate        <= #1 sr_last;
388
          end
389
 
390
                        end
391
        sr_last :       begin
392
                                if (rcounter16_eq_1 & srx_pad_i | break_error)
393
                                        rstate <= #1 sr_idle;
394
                                rcounter16 <= #1 rcounter16_minus_1;
395
                                rf_push    <= #1 1'b0;
396
                        end
397
        default : rstate <= #1 sr_idle;
398
        endcase
399
  end  // if (enable)
400
end // always of receiver
401
 
402
always @ (posedge clk or posedge wb_rst_i)
403
begin
404
  if(wb_rst_i)
405
    rf_push_q <= 0;
406
  else
407
    rf_push_q <= #1 rf_push;
408
end
409
 
410
assign rf_push_pulse = rf_push & ~rf_push_q;
411
 
412
 
413
//
414
// Break condition detection.
415
// Works in conjuction with the receiver state machine
416
 
417
reg     [9:0]    toc_value; // value to be set to timeout counter
418
 
419
always @(lcr)
420
        case (lcr[3:0])
421
                4'b0000                                                                         : toc_value = 447; // 7 bits
422
                4'b0100                                                                         : toc_value = 479; // 7.5 bits
423
                4'b0001,        4'b1000                                                 : toc_value = 511; // 8 bits
424
                4'b1100                                                                         : toc_value = 543; // 8.5 bits
425
                4'b0010, 4'b0101, 4'b1001                               : toc_value = 575; // 9 bits
426
                4'b0011, 4'b0110, 4'b1010, 4'b1101      : toc_value = 639; // 10 bits
427
                4'b0111, 4'b1011, 4'b1110                               : toc_value = 703; // 11 bits
428
                4'b1111                                                                         : toc_value = 767; // 12 bits
429
        endcase // case(lcr[3:0])
430
 
431
wire [7:0]       brc_value; // value to be set to break counter
432
assign          brc_value = toc_value[9:2]; // the same as timeout but 1 insead of 4 character times
433
 
434
always @(posedge clk or posedge wb_rst_i)
435
begin
436
        if (wb_rst_i)
437
                counter_b <= #1 8'd159;
438
        else
439
        if (srx_pad_i)
440
                counter_b <= #1 brc_value; // character time length - 1
441
        else
442
        if(enable & counter_b != 8'b0)            // only work on enable times  break not reached.
443
                counter_b <= #1 counter_b - 1;  // decrement break counter
444
end // always of break condition detection
445
 
446
///
447
/// Timeout condition detection
448
reg     [9:0]    counter_t;      // counts the timeout condition clocks
449
 
450
always @(posedge clk or posedge wb_rst_i)
451
begin
452
        if (wb_rst_i)
453
                counter_t <= #1 10'd639; // 10 bits for the default 8N1
454
        else
455
                if(rf_push_pulse || rf_pop || rf_count == 0) // counter is reset when RX FIFO is empty, accessed or above trigger level
456
                        counter_t <= #1 toc_value;
457
                else
458
                if (enable && counter_t != 10'b0)  // we don't want to underflow
459
                        counter_t <= #1 counter_t - 1;
460
end
461
 
462
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.