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

Subversion Repositories uart16550

[/] [uart16550/] [trunk/] [rtl/] [verilog/] [uart_regs.v] - Blame information for rev 79

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 mohor
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  uart_regs.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
////  Registers of the uart 16550 core                            ////
19
////                                                              ////
20
////  Known problems (limits):                                    ////
21
////  Inserts 1 wait state in all WISHBONE transfers              ////
22
////                                                              ////
23
////  To Do:                                                      ////
24
////  Nothing or verification.                                    ////
25
////                                                              ////
26
////  Author(s):                                                  ////
27
////      - gorban@opencores.org                                  ////
28
////      - Jacob Gorban                                          ////
29 29 mohor
////      - Igor Mohor (igorm@opencores.org)                      ////
30 27 mohor
////                                                              ////
31
////  Created:        2001/05/12                                  ////
32
////  Last Updated:   (See log for the revision history           ////
33
////                                                              ////
34
////                                                              ////
35
//////////////////////////////////////////////////////////////////////
36
////                                                              ////
37 29 mohor
//// Copyright (C) 2000, 2001 Authors                             ////
38 27 mohor
////                                                              ////
39
//// This source file may be used and distributed without         ////
40
//// restriction provided that this copyright statement is not    ////
41
//// removed from the file and that any derivative work contains  ////
42
//// the original copyright notice and the associated disclaimer. ////
43
////                                                              ////
44
//// This source file is free software; you can redistribute it   ////
45
//// and/or modify it under the terms of the GNU Lesser General   ////
46
//// Public License as published by the Free Software Foundation; ////
47
//// either version 2.1 of the License, or (at your option) any   ////
48
//// later version.                                               ////
49
////                                                              ////
50
//// This source is distributed in the hope that it will be       ////
51
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
52
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
53
//// PURPOSE.  See the GNU Lesser General Public License for more ////
54
//// details.                                                     ////
55
////                                                              ////
56
//// You should have received a copy of the GNU Lesser General    ////
57
//// Public License along with this source; if not, download it   ////
58
//// from http://www.opencores.org/lgpl.shtml                     ////
59
////                                                              ////
60
//////////////////////////////////////////////////////////////////////
61
//
62
// CVS Revision History
63
//
64
// $Log: not supported by cvs2svn $
65 79 gorban
// Revision 1.37  2001/12/27 13:24:09  mohor
66
// lsr[7] was not showing overrun errors.
67
//
68 68 mohor
// Revision 1.36  2001/12/20 13:25:46  mohor
69
// rx push changed to be only one cycle wide.
70
//
71 66 mohor
// Revision 1.35  2001/12/19 08:03:34  mohor
72
// Warnings cleared.
73
//
74 64 mohor
// Revision 1.34  2001/12/19 07:33:54  mohor
75
// Synplicity was having troubles with the comment.
76
//
77 63 mohor
// Revision 1.33  2001/12/17 10:14:43  mohor
78
// Things related to msr register changed. After THRE IRQ occurs, and one
79
// character is written to the transmit fifo, the detection of the THRE bit in the
80
// LSR is delayed for one character time.
81
//
82 60 mohor
// Revision 1.32  2001/12/14 13:19:24  mohor
83
// MSR register fixed.
84
//
85 59 mohor
// Revision 1.31  2001/12/14 10:06:58  mohor
86
// After reset modem status register MSR should be reset.
87
//
88 58 mohor
// Revision 1.30  2001/12/13 10:09:13  mohor
89
// thre irq should be cleared only when being source of interrupt.
90
//
91 56 mohor
// Revision 1.29  2001/12/12 09:05:46  mohor
92
// LSR status bit 0 was not cleared correctly in case of reseting the FCR (rx fifo).
93
//
94 54 mohor
// Revision 1.28  2001/12/10 19:52:41  gorban
95
// Scratch register added
96
//
97 52 gorban
// Revision 1.27  2001/12/06 14:51:04  gorban
98
// Bug in LSR[0] is fixed.
99
// All WISHBONE signals are now sampled, so another wait-state is introduced on all transfers.
100
//
101 50 gorban
// Revision 1.26  2001/12/03 21:44:29  gorban
102
// Updated specification documentation.
103
// Added full 32-bit data bus interface, now as default.
104
// Address is 5-bit wide in 32-bit data bus mode.
105
// Added wb_sel_i input to the core. It's used in the 32-bit mode.
106
// Added debug interface with two 32-bit read-only registers in 32-bit mode.
107
// Bits 5 and 6 of LSR are now only cleared on TX FIFO write.
108
// My small test bench is modified to work with 32-bit mode.
109
//
110 48 gorban
// Revision 1.25  2001/11/28 19:36:39  gorban
111
// Fixed: timeout and break didn't pay attention to current data format when counting time
112
//
113 47 gorban
// Revision 1.24  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 45 gorban
// Revision 1.23  2001/11/12 21:57:29  gorban
122
// fixed more typo bugs
123
//
124 44 gorban
// Revision 1.22  2001/11/12 15:02:28  mohor
125
// lsr1r error fixed.
126
//
127 43 mohor
// Revision 1.21  2001/11/12 14:57:27  mohor
128
// ti_int_pnd error fixed.
129
//
130 42 mohor
// Revision 1.20  2001/11/12 14:50:27  mohor
131
// ti_int_d error fixed.
132
//
133 41 mohor
// Revision 1.19  2001/11/10 12:43:21  gorban
134 63 mohor
// Logic Synthesis bugs fixed. Some other minor changes
135 41 mohor
//
136 40 gorban
// Revision 1.18  2001/11/08 14:54:23  mohor
137
// Comments in Slovene language deleted, few small fixes for better work of
138
// old tools. IRQs need to be fix.
139
//
140 39 mohor
// Revision 1.17  2001/11/07 17:51:52  gorban
141
// Heavily rewritten interrupt and LSR subsystems.
142
// Many bugs hopefully squashed.
143
//
144 37 gorban
// Revision 1.16  2001/11/02 09:55:16  mohor
145
// no message
146
//
147 36 mohor
// Revision 1.15  2001/10/31 15:19:22  gorban
148
// Fixes to break and timeout conditions
149
//
150 35 gorban
// Revision 1.14  2001/10/29 17:00:46  gorban
151
// fixed parity sending and tx_fifo resets over- and underrun
152
//
153 34 gorban
// Revision 1.13  2001/10/20 09:58:40  gorban
154
// Small synopsis fixes
155
//
156 33 gorban
// Revision 1.12  2001/10/19 16:21:40  gorban
157
// Changes data_out to be synchronous again as it should have been.
158
//
159 32 gorban
// Revision 1.11  2001/10/18 20:35:45  gorban
160
// small fix
161
//
162 31 gorban
// Revision 1.10  2001/08/24 21:01:12  mohor
163
// Things connected to parity changed.
164
// Clock devider changed.
165
//
166 29 mohor
// Revision 1.9  2001/08/23 16:05:05  mohor
167
// Stop bit bug fixed.
168
// Parity bug fixed.
169
// WISHBONE read cycle bug fixed,
170
// OE indicator (Overrun Error) bug fixed.
171
// PE indicator (Parity Error) bug fixed.
172
// Register read bug fixed.
173
//
174 27 mohor
// Revision 1.10  2001/06/23 11:21:48  gorban
175
// DL made 16-bit long. Fixed transmission/reception bugs.
176
//
177
// Revision 1.9  2001/05/31 20:08:01  gorban
178
// FIFO changes and other corrections.
179
//
180
// Revision 1.8  2001/05/29 20:05:04  gorban
181
// Fixed some bugs and synthesis problems.
182
//
183
// Revision 1.7  2001/05/27 17:37:49  gorban
184
// Fixed many bugs. Updated spec. Changed FIFO files structure. See CHANGES.txt file.
185
//
186
// Revision 1.6  2001/05/21 19:12:02  gorban
187
// Corrected some Linter messages.
188
//
189
// Revision 1.5  2001/05/17 18:34:18  gorban
190
// First 'stable' release. Should be sythesizable now. Also added new header.
191
//
192
// Revision 1.0  2001-05-17 21:27:11+02  jacob
193
// Initial revision
194
//
195
//
196
 
197 33 gorban
// synopsys translate_off
198 27 mohor
`include "timescale.v"
199 33 gorban
// synopsys translate_on
200
 
201 79 gorban
//`include "uart_defines.v"
202 27 mohor
 
203
`define UART_DL1 7:0
204
`define UART_DL2 15:8
205
 
206
module uart_regs (clk,
207
        wb_rst_i, wb_addr_i, wb_dat_i, wb_dat_o, wb_we_i, wb_re_i,
208
 
209
// additional signals
210
        modem_inputs,
211
        stx_pad_o, srx_pad_i,
212 48 gorban
 
213
`ifdef DATA_BUS_WIDTH_8
214
`else
215
// debug interface signals      enabled
216
ier, iir, fcr, mcr, lcr, msr, lsr, rf_count, tf_count, tstate, rstate,
217
`endif
218 27 mohor
        rts_pad_o, dtr_pad_o, int_o
219 79 gorban
`ifdef UART_HAS_BAUDRATE_OUTPUT
220
        , baud_o
221
`endif
222
 
223 27 mohor
        );
224
 
225 37 gorban
input                                                                   clk;
226
input                                                                   wb_rst_i;
227
input [`UART_ADDR_WIDTH-1:0]             wb_addr_i;
228
input [7:0]                                                      wb_dat_i;
229
output [7:0]                                                     wb_dat_o;
230
input                                                                   wb_we_i;
231
input                                                                   wb_re_i;
232 27 mohor
 
233 37 gorban
output                                                                  stx_pad_o;
234
input                                                                   srx_pad_i;
235 27 mohor
 
236 37 gorban
input [3:0]                                                      modem_inputs;
237
output                                                                  rts_pad_o;
238
output                                                                  dtr_pad_o;
239
output                                                                  int_o;
240 79 gorban
`ifdef UART_HAS_BAUDRATE_OUTPUT
241
output  baud_o;
242
`endif
243 27 mohor
 
244 48 gorban
`ifdef DATA_BUS_WIDTH_8
245
`else
246
// if 32-bit databus and debug interface are enabled
247
output [3:0]                                                     ier;
248
output [3:0]                                                     iir;
249
output [1:0]                                                     fcr;  /// bits 7 and 6 of fcr. Other bits are ignored
250
output [4:0]                                                     mcr;
251
output [7:0]                                                     lcr;
252
output [7:0]                                                     msr;
253
output [7:0]                                                     lsr;
254
output [`UART_FIFO_COUNTER_W-1:0]        rf_count;
255
output [`UART_FIFO_COUNTER_W-1:0]        tf_count;
256
output [2:0]                                                     tstate;
257
output [3:0]                                                     rstate;
258
 
259
`endif
260
 
261 37 gorban
wire [3:0]                                                               modem_inputs;
262
reg                                                                             enable;
263 79 gorban
`ifdef UART_HAS_BAUDRATE_OUTPUT
264
assign baud_o = enable; // baud_o is actually the enable signal
265
`endif
266
 
267
 
268 37 gorban
wire                                                                            stx_pad_o;              // received from transmitter module
269
wire                                                                            srx_pad_i;
270 27 mohor
 
271 37 gorban
reg [7:0]                                                                wb_dat_o;
272 27 mohor
 
273 37 gorban
wire [`UART_ADDR_WIDTH-1:0]              wb_addr_i;
274
wire [7:0]                                                               wb_dat_i;
275 27 mohor
 
276
 
277 37 gorban
reg [3:0]                                                                ier;
278
reg [3:0]                                                                iir;
279
reg [1:0]                                                                fcr;  /// bits 7 and 6 of fcr. Other bits are ignored
280
reg [4:0]                                                                mcr;
281
reg [7:0]                                                                lcr;
282
reg [7:0]                                                                msr;
283
reg [15:0]                                                               dl;  // 32-bit divisor latch
284 52 gorban
reg [7:0]                                                                scratch; // UART scratch register
285 37 gorban
reg                                                                             start_dlc; // activate dlc on writing to UART_DL1
286
reg                                                                             lsr_mask_d; // delay for lsr_mask condition
287
reg                                                                             msi_reset; // reset MSR 4 lower bits indicator
288 40 gorban
//reg                                                                           threi_clear; // THRE interrupt clear flag
289 37 gorban
reg [15:0]                                                               dlc;  // 32-bit divisor latch counter
290
reg                                                                             int_o;
291 27 mohor
 
292 37 gorban
reg [3:0]                                                                trigger_level; // trigger level of the receiver FIFO
293
reg                                                                             rx_reset;
294
reg                                                                             tx_reset;
295 27 mohor
 
296 37 gorban
wire                                                                            dlab;                      // divisor latch access bit
297
wire                                                                            cts_pad_i, dsr_pad_i, ri_pad_i, dcd_pad_i; // modem status bits
298
wire                                                                            loopback;                  // loopback bit (MCR bit 4)
299 60 mohor
wire                                                                            cts, dsr, ri, dcd;         // effective signals
300 59 mohor
wire                    cts_c, dsr_c, ri_c, dcd_c; // Complement effective signals (considering loopback)
301 37 gorban
wire                                                                            rts_pad_o, dtr_pad_o;              // modem control outputs
302 27 mohor
 
303 37 gorban
// LSR bits wires and regs
304
wire [7:0]                                                               lsr;
305
wire                                                                            lsr0, lsr1, lsr2, lsr3, lsr4, lsr5, lsr6, lsr7;
306
reg                                                                             lsr0r, lsr1r, lsr2r, lsr3r, lsr4r, lsr5r, lsr6r, lsr7r;
307
wire                                                                            lsr_mask; // lsr_mask
308
 
309 27 mohor
//
310
// ASSINGS
311
//
312
 
313 37 gorban
assign                                                                  lsr[7:0] = { lsr7r, lsr6r, lsr5r, lsr4r, lsr3r, lsr2r, lsr1r, lsr0r };
314 27 mohor
 
315 37 gorban
assign                                                                  {cts_pad_i, dsr_pad_i, ri_pad_i, dcd_pad_i} = modem_inputs;
316 60 mohor
assign                                                                  {cts, dsr, ri, dcd} = ~{cts_pad_i,dsr_pad_i,ri_pad_i,dcd_pad_i};
317 37 gorban
 
318 59 mohor
assign                  {cts_c, dsr_c, ri_c, dcd_c} = loopback ? {mcr[`UART_MC_RTS],mcr[`UART_MC_DTR],mcr[`UART_MC_OUT1],mcr[`UART_MC_OUT2]}
319 60 mohor
                                                               : {cts_pad_i,dsr_pad_i,ri_pad_i,dcd_pad_i};
320 59 mohor
 
321 37 gorban
assign                                                                  dlab = lcr[`UART_LC_DL];
322
assign                                                                  loopback = mcr[4];
323
 
324 27 mohor
// assign modem outputs
325 37 gorban
assign                                                                  rts_pad_o = mcr[`UART_MC_RTS];
326
assign                                                                  dtr_pad_o = mcr[`UART_MC_DTR];
327 27 mohor
 
328
// Interrupt signals
329 37 gorban
wire                                                                            rls_int;  // receiver line status interrupt
330
wire                                                                            rda_int;  // receiver data available interrupt
331
wire                                                                            ti_int;   // timeout indicator interrupt
332
wire                                                                            thre_int; // transmitter holding register empty interrupt
333
wire                                                                            ms_int;   // modem status interrupt
334 27 mohor
 
335
// FIFO signals
336 37 gorban
reg                                                                             tf_push;
337
reg                                                                             rf_pop;
338
wire [`UART_FIFO_REC_WIDTH-1:0]  rf_data_out;
339
wire                                                                            rf_error_bit; // an error (parity or framing) is inside the fifo
340
wire [`UART_FIFO_COUNTER_W-1:0]  rf_count;
341
wire [`UART_FIFO_COUNTER_W-1:0]  tf_count;
342 48 gorban
wire [2:0]                                                               tstate;
343
wire [3:0]                                                               rstate;
344 37 gorban
wire [9:0]                                                               counter_t;
345 27 mohor
 
346 60 mohor
wire                      thre_set_en; // THRE status is delayed one character time when a character is written to fifo.
347
reg  [7:0]                block_cnt;   // While counter counts, THRE status is blocked (delayed one character cycle)
348
reg  [7:0]                block_value; // One character length minus stop bit
349 37 gorban
 
350 27 mohor
// Transmitter Instance
351 79 gorban
wire serial_out;
352 27 mohor
 
353 79 gorban
uart_transmitter transmitter(clk, wb_rst_i, lcr, tf_push, wb_dat_i, enable, serial_out, tstate, tf_count, tx_reset, lsr_mask);
354
 
355
// handle loopback
356
wire serial_in = loopback ? serial_out : srx_pad_i;
357
assign stx_pad_o = loopback ? 1'b1 : serial_out;
358
 
359 27 mohor
// Receiver Instance
360 79 gorban
uart_receiver receiver(clk, wb_rst_i, lcr, rf_pop, serial_in, enable,
361 66 mohor
        counter_t, rf_count, rf_data_out, rf_error_bit, rf_overrun, rx_reset, lsr_mask, rstate, rf_push_pulse);
362 27 mohor
 
363 32 gorban
 
364 48 gorban
// Asynchronous reading here because the outputs are sampled in uart_wb.v file 
365 52 gorban
always @(dl or dlab or ier or iir or scratch
366 48 gorban
                        or lcr or lsr or msr or rf_data_out or wb_addr_i or wb_re_i)   // asynchrounous reading
367 27 mohor
begin
368 52 gorban
        case (wb_addr_i)
369
                `UART_REG_RB   : wb_dat_o = dlab ? dl[`UART_DL1] : rf_data_out[10:3];
370
                `UART_REG_IE    : wb_dat_o = dlab ? dl[`UART_DL2] : ier;
371
                `UART_REG_II    : wb_dat_o = {4'b1100,iir};
372
                `UART_REG_LC    : wb_dat_o = lcr;
373
                `UART_REG_LS    : wb_dat_o = lsr;
374
                `UART_REG_MS    : wb_dat_o = msr;
375
                `UART_REG_SR    : wb_dat_o = scratch;
376
                default:  wb_dat_o = 8'b0; // ??
377
        endcase // case(wb_addr_i)
378
end // always @ (dl or dlab or ier or iir or scratch...
379 27 mohor
 
380 52 gorban
 
381 27 mohor
// rf_pop signal handling
382
always @(posedge clk or posedge wb_rst_i)
383
begin
384
        if (wb_rst_i)
385
                rf_pop <= #1 0;
386
        else
387
        if (rf_pop)     // restore the signal to 0 after one clock cycle
388
                rf_pop <= #1 0;
389
        else
390
        if (wb_re_i && wb_addr_i == `UART_REG_RB && !dlab)
391
                rf_pop <= #1 1; // advance read pointer
392
end
393
 
394 37 gorban
wire    lsr_mask_condition;
395
wire    iir_read;
396
wire  msr_read;
397
wire    fifo_read;
398 45 gorban
wire    fifo_write;
399 37 gorban
 
400
assign lsr_mask_condition = (wb_re_i && wb_addr_i == `UART_REG_LS && !dlab);
401
assign iir_read = (wb_re_i && wb_addr_i == `UART_REG_II && !dlab);
402
assign msr_read = (wb_re_i && wb_addr_i == `UART_REG_MS && !dlab);
403
assign fifo_read = (wb_re_i && wb_addr_i == `UART_REG_RB && !dlab);
404 45 gorban
assign fifo_write = (wb_we_i && wb_addr_i == `UART_REG_TR && !dlab);
405 37 gorban
 
406
// lsr_mask_d delayed signal handling
407 27 mohor
always @(posedge clk or posedge wb_rst_i)
408
begin
409
        if (wb_rst_i)
410 37 gorban
                lsr_mask_d <= #1 0;
411
        else // reset bits in the Line Status Register
412
                lsr_mask_d <= #1 lsr_mask_condition;
413 27 mohor
end
414
 
415 37 gorban
// lsr_mask is rise detected
416
assign lsr_mask = lsr_mask_condition && ~lsr_mask_d;
417 27 mohor
 
418
// msi_reset signal handling
419
always @(posedge clk or posedge wb_rst_i)
420
begin
421
        if (wb_rst_i)
422 58 mohor
                msi_reset <= #1 1;
423 27 mohor
        else
424
        if (msi_reset)
425
                msi_reset <= #1 0;
426
        else
427 47 gorban
        if (msr_read)
428 27 mohor
                msi_reset <= #1 1; // reset bits in Modem Status Register
429
end
430
 
431
 
432
//
433
//   WRITES AND RESETS   //
434
//
435
// Line Control Register
436
always @(posedge clk or posedge wb_rst_i)
437
        if (wb_rst_i)
438
                lcr <= #1 8'b00000011; // 8n1 setting
439
        else
440
        if (wb_we_i && wb_addr_i==`UART_REG_LC)
441
                lcr <= #1 wb_dat_i;
442
 
443
// Interrupt Enable Register or UART_DL2
444
always @(posedge clk or posedge wb_rst_i)
445
        if (wb_rst_i)
446
        begin
447
                ier <= #1 4'b0000; // no interrupts after reset
448
                dl[`UART_DL2] <= #1 8'b0;
449
        end
450
        else
451
        if (wb_we_i && wb_addr_i==`UART_REG_IE)
452
                if (dlab)
453
                begin
454
                        dl[`UART_DL2] <= #1 wb_dat_i;
455
                end
456
                else
457
                        ier <= #1 wb_dat_i[3:0]; // ier uses only 4 lsb
458
 
459
 
460
// FIFO Control Register and rx_reset, tx_reset signals
461
always @(posedge clk or posedge wb_rst_i)
462
        if (wb_rst_i) begin
463
                fcr <= #1 2'b11;
464
                rx_reset <= #1 0;
465
                tx_reset <= #1 0;
466
        end else
467
        if (wb_we_i && wb_addr_i==`UART_REG_FC) begin
468
                fcr <= #1 wb_dat_i[7:6];
469
                rx_reset <= #1 wb_dat_i[1];
470
                tx_reset <= #1 wb_dat_i[2];
471 37 gorban
        end else begin
472 27 mohor
                rx_reset <= #1 0;
473
                tx_reset <= #1 0;
474
        end
475
 
476
// Modem Control Register
477
always @(posedge clk or posedge wb_rst_i)
478
        if (wb_rst_i)
479
                mcr <= #1 5'b0;
480
        else
481
        if (wb_we_i && wb_addr_i==`UART_REG_MC)
482
                        mcr <= #1 wb_dat_i[4:0];
483
 
484 52 gorban
// Scratch register
485
// Line Control Register
486
always @(posedge clk or posedge wb_rst_i)
487
        if (wb_rst_i)
488
                scratch <= #1 0; // 8n1 setting
489
        else
490
        if (wb_we_i && wb_addr_i==`UART_REG_SR)
491
                scratch <= #1 wb_dat_i;
492
 
493 27 mohor
// TX_FIFO or UART_DL1
494
always @(posedge clk or posedge wb_rst_i)
495
        if (wb_rst_i)
496
        begin
497
                dl[`UART_DL1]  <= #1 8'b0;
498
                tf_push   <= #1 1'b0;
499
                start_dlc <= #1 1'b0;
500
        end
501
        else
502
        if (wb_we_i && wb_addr_i==`UART_REG_TR)
503
                if (dlab)
504
                begin
505
                        dl[`UART_DL1] <= #1 wb_dat_i;
506
                        start_dlc <= #1 1'b1; // enable DL counter
507
                        tf_push <= #1 1'b0;
508
                end
509
                else
510
                begin
511
                        tf_push   <= #1 1'b1;
512
                        start_dlc <= #1 1'b0;
513 37 gorban
                end // else: !if(dlab)
514 27 mohor
        else
515
        begin
516
                start_dlc <= #1 1'b0;
517
                tf_push   <= #1 1'b0;
518 37 gorban
        end // else: !if(dlab)
519 27 mohor
 
520
// Receiver FIFO trigger level selection logic (asynchronous mux)
521 31 gorban
always @(fcr)
522 27 mohor
        case (fcr[`UART_FC_TL])
523
                2'b00 : trigger_level = 1;
524
                2'b01 : trigger_level = 4;
525
                2'b10 : trigger_level = 8;
526
                2'b11 : trigger_level = 14;
527 37 gorban
        endcase // case(fcr[`UART_FC_TL])
528 27 mohor
 
529
//
530
//  STATUS REGISTERS  //
531
//
532
 
533
// Modem Status Register
534 59 mohor
reg [3:0] delayed_modem_signals;
535 27 mohor
always @(posedge clk or posedge wb_rst_i)
536
begin
537
        if (wb_rst_i)
538 64 mohor
          begin
539
                msr <= #1 0;
540
                delayed_modem_signals[3:0] <= #1 0;
541
          end
542 27 mohor
        else begin
543
                msr[`UART_MS_DDCD:`UART_MS_DCTS] <= #1 msi_reset ? 4'b0 :
544 59 mohor
                        msr[`UART_MS_DDCD:`UART_MS_DCTS] | ({dcd, ri, dsr, cts} ^ delayed_modem_signals[3:0]);
545
                msr[`UART_MS_CDCD:`UART_MS_CCTS] <= #1 {dcd_c, ri_c, dsr_c, cts_c};
546
                delayed_modem_signals[3:0] <= #1 {dcd, ri, dsr, cts};
547 27 mohor
        end
548
end
549
 
550 58 mohor
 
551 27 mohor
// Line Status Register
552 37 gorban
 
553
// activation conditions
554 66 mohor
assign lsr0 = (rf_count==0 && rf_push_pulse);  // data in receiver fifo available set condition
555 37 gorban
assign lsr1 = rf_overrun;     // Receiver overrun error
556
assign lsr2 = rf_data_out[1]; // parity error bit
557
assign lsr3 = rf_data_out[0]; // framing error bit
558 45 gorban
assign lsr4 = rf_data_out[2]; // break error in the character
559 60 mohor
assign lsr5 = (tf_count==5'b0 && thre_set_en);  // transmitter fifo is empty
560
assign lsr6 = (tf_count==5'b0 && thre_set_en && (tstate == /*`S_IDLE */ 0)); // transmitter empty
561 68 mohor
assign lsr7 = rf_error_bit | rf_overrun;
562 37 gorban
 
563
// lsr bit0 (receiver data available)
564 45 gorban
reg      lsr0_d;
565
 
566 27 mohor
always @(posedge clk or posedge wb_rst_i)
567 45 gorban
        if (wb_rst_i) lsr0_d <= #1 0;
568
        else lsr0_d <= #1 lsr0;
569
 
570
always @(posedge clk or posedge wb_rst_i)
571 37 gorban
        if (wb_rst_i) lsr0r <= #1 0;
572 54 mohor
        else lsr0r <= #1 (rf_count==1 && fifo_read || rx_reset) ? 0 : // deassert condition
573 45 gorban
                                          lsr0r || (lsr0 && ~lsr0_d); // set on rise of lsr0 and keep asserted until deasserted 
574 27 mohor
 
575 37 gorban
// lsr bit 1 (receiver overrun)
576
reg lsr1_d; // delayed
577 29 mohor
 
578 37 gorban
always @(posedge clk or posedge wb_rst_i)
579
        if (wb_rst_i) lsr1_d <= #1 0;
580
        else lsr1_d <= #1 lsr1;
581
 
582
always @(posedge clk or posedge wb_rst_i)
583 43 mohor
        if (wb_rst_i) lsr1r <= #1 0;
584 45 gorban
        else    lsr1r <= #1     lsr_mask ? 0 : lsr1r || (lsr1 && ~lsr1_d); // set on rise
585 37 gorban
 
586
// lsr bit 2 (parity error)
587
reg lsr2_d; // delayed
588
 
589
always @(posedge clk or posedge wb_rst_i)
590
        if (wb_rst_i) lsr2_d <= #1 0;
591
        else lsr2_d <= #1 lsr2;
592
 
593
always @(posedge clk or posedge wb_rst_i)
594 44 gorban
        if (wb_rst_i) lsr2r <= #1 0;
595 45 gorban
        else lsr2r <= #1 lsr_mask ? 0 : lsr2r || (lsr2 && ~lsr2_d); // set on rise
596 37 gorban
 
597
// lsr bit 3 (framing error)
598
reg lsr3_d; // delayed
599
 
600
always @(posedge clk or posedge wb_rst_i)
601
        if (wb_rst_i) lsr3_d <= #1 0;
602
        else lsr3_d <= #1 lsr3;
603
 
604
always @(posedge clk or posedge wb_rst_i)
605 44 gorban
        if (wb_rst_i) lsr3r <= #1 0;
606 45 gorban
        else lsr3r <= #1 lsr_mask ? 0 : lsr3r || (lsr3 && ~lsr3_d); // set on rise
607 37 gorban
 
608
// lsr bit 4 (break indicator)
609
reg lsr4_d; // delayed
610
 
611
always @(posedge clk or posedge wb_rst_i)
612
        if (wb_rst_i) lsr4_d <= #1 0;
613
        else lsr4_d <= #1 lsr4;
614
 
615
always @(posedge clk or posedge wb_rst_i)
616 44 gorban
        if (wb_rst_i) lsr4r <= #1 0;
617 45 gorban
        else lsr4r <= #1 lsr_mask ? 0 : lsr4r || (lsr4 && ~lsr4_d);
618 37 gorban
 
619
// lsr bit 5 (transmitter fifo is empty)
620
reg lsr5_d;
621
 
622
always @(posedge clk or posedge wb_rst_i)
623 45 gorban
        if (wb_rst_i) lsr5_d <= #1 1;
624 37 gorban
        else lsr5_d <= #1 lsr5;
625
 
626
always @(posedge clk or posedge wb_rst_i)
627 45 gorban
        if (wb_rst_i) lsr5r <= #1 1;
628 50 gorban
        else lsr5r <= #1 (fifo_write) ? 0 :  lsr5r || (lsr5 && ~lsr5_d);
629 37 gorban
 
630
// lsr bit 6 (transmitter empty indicator)
631
reg lsr6_d;
632
 
633
always @(posedge clk or posedge wb_rst_i)
634 45 gorban
        if (wb_rst_i) lsr6_d <= #1 1;
635 37 gorban
        else lsr6_d <= #1 lsr6;
636
 
637
always @(posedge clk or posedge wb_rst_i)
638 45 gorban
        if (wb_rst_i) lsr6r <= #1 1;
639 50 gorban
        else lsr6r <= #1 (fifo_write) ? 0 : lsr6r || (lsr6 && ~lsr6_d);
640 37 gorban
 
641
// lsr bit 7 (error in fifo)
642
reg lsr7_d;
643
 
644
always @(posedge clk or posedge wb_rst_i)
645
        if (wb_rst_i) lsr7_d <= #1 0;
646
        else lsr7_d <= #1 lsr7;
647
 
648
always @(posedge clk or posedge wb_rst_i)
649 44 gorban
        if (wb_rst_i) lsr7r <= #1 0;
650 45 gorban
        else lsr7r <= #1 lsr_mask ? 0 : lsr7r || (lsr7 && ~lsr7_d);
651 37 gorban
 
652 29 mohor
// Frequency divider
653 37 gorban
always @(posedge clk or posedge wb_rst_i)
654 29 mohor
begin
655
        if (wb_rst_i)
656
                dlc <= #1 0;
657
        else
658 37 gorban
                if (start_dlc | ~ (|dlc))
659
                        dlc <= #1 dl - 1;               // preset counter
660
                else
661
                        dlc <= #1 dlc - 1;              // decrement counter
662 29 mohor
end
663
 
664 27 mohor
// Enable signal generation logic
665
always @(posedge clk or posedge wb_rst_i)
666
begin
667
        if (wb_rst_i)
668
                enable <= #1 1'b0;
669
        else
670 37 gorban
                if (|dl & ~(|dlc))     // dl>0 & dlc==0
671
                        enable <= #1 1'b1;
672
                else
673
                        enable <= #1 1'b0;
674 27 mohor
end
675
 
676 60 mohor
// Delaying THRE status for one character cycle after a character is written to an empty fifo.
677
always @(lcr)
678
  case (lcr[3:0])
679
    4'b0000                             : block_value =  95; // 6 bits
680
    4'b0100                             : block_value = 103; // 6.5 bits
681
    4'b0001, 4'b1000                    : block_value = 111; // 7 bits
682
    4'b1100                             : block_value = 119; // 7.5 bits
683
    4'b0010, 4'b0101, 4'b1001           : block_value = 127; // 8 bits
684
    4'b0011, 4'b0110, 4'b1010, 4'b1101  : block_value = 143; // 9 bits
685
    4'b0111, 4'b1011, 4'b1110           : block_value = 159; // 10 bits
686
    4'b1111                             : block_value = 175; // 11 bits
687
  endcase // case(lcr[3:0])
688
 
689
// Counting time of one character minus stop bit
690
always @(posedge clk or posedge wb_rst_i)
691
begin
692
  if (wb_rst_i)
693
    block_cnt <= #1 8'd0;
694
  else
695
  if(lsr5r & fifo_write)  // THRE bit set & write to fifo occured
696
    block_cnt <= #1 block_value;
697
  else
698
  if (enable & block_cnt != 8'b0)  // only work on enable times
699
    block_cnt <= #1 block_cnt - 1;  // decrement break counter
700
end // always of break condition detection
701
 
702
// Generating THRE status enable signal
703
assign thre_set_en = ~(|block_cnt);
704
 
705
 
706 37 gorban
//
707
//      INTERRUPT LOGIC
708
//
709 29 mohor
 
710 37 gorban
assign rls_int  = ier[`UART_IE_RLS] && (lsr[`UART_LS_OE] || lsr[`UART_LS_PE] || lsr[`UART_LS_FE] || lsr[`UART_LS_BI]);
711
assign rda_int  = ier[`UART_IE_RDA] && (rf_count >= {1'b0,trigger_level});
712 40 gorban
assign thre_int = ier[`UART_IE_THRE] && lsr[`UART_LS_TFE];
713 37 gorban
assign ms_int   = ier[`UART_IE_MS] && (| msr[3:0]);
714
assign ti_int   = ier[`UART_IE_RDA] && (counter_t == 10'b0);
715 29 mohor
 
716 37 gorban
reg      rls_int_d;
717
reg      thre_int_d;
718
reg      ms_int_d;
719
reg      ti_int_d;
720 45 gorban
reg      rda_int_d;
721 29 mohor
 
722 37 gorban
// delay lines
723
always  @(posedge clk or posedge wb_rst_i)
724
        if (wb_rst_i) rls_int_d <= #1 0;
725
        else rls_int_d <= #1 rls_int;
726 29 mohor
 
727 37 gorban
always  @(posedge clk or posedge wb_rst_i)
728 45 gorban
        if (wb_rst_i) rda_int_d <= #1 0;
729
        else rda_int_d <= #1 rda_int;
730
 
731
always  @(posedge clk or posedge wb_rst_i)
732 37 gorban
        if (wb_rst_i) thre_int_d <= #1 0;
733
        else thre_int_d <= #1 thre_int;
734 29 mohor
 
735 37 gorban
always  @(posedge clk or posedge wb_rst_i)
736
        if (wb_rst_i) ms_int_d <= #1 0;
737
        else ms_int_d <= #1 ms_int;
738 29 mohor
 
739 37 gorban
always  @(posedge clk or posedge wb_rst_i)
740 41 mohor
        if (wb_rst_i) ti_int_d <= #1 0;
741 37 gorban
        else ti_int_d <= #1 ti_int;
742 27 mohor
 
743 37 gorban
// rise detection signals
744
 
745
wire     rls_int_rise;
746
wire     thre_int_rise;
747
wire     ms_int_rise;
748
wire     ti_int_rise;
749 45 gorban
wire     rda_int_rise;
750 37 gorban
 
751 45 gorban
assign rda_int_rise    = rda_int & ~rda_int_d;
752 37 gorban
assign rls_int_rise       = rls_int & ~rls_int_d;
753
assign thre_int_rise   = thre_int & ~thre_int_d;
754
assign ms_int_rise        = ms_int & ~ms_int_d;
755
assign ti_int_rise        = ti_int & ~ti_int_d;
756
 
757
// interrupt pending flags
758
reg     rls_int_pnd;
759 45 gorban
reg     rda_int_pnd;
760 37 gorban
reg     thre_int_pnd;
761
reg     ms_int_pnd;
762
reg     ti_int_pnd;
763
 
764
// interrupt pending flags assignments
765
always  @(posedge clk or posedge wb_rst_i)
766
        if (wb_rst_i) rls_int_pnd <= #1 0;
767
        else
768
                rls_int_pnd <= #1 lsr_mask ? 0 :                                                 // reset condition
769
                                                        rls_int_rise ? 1 :                                              // latch condition
770
                                                        rls_int_pnd && ier[`UART_IE_RLS];       // default operation: remove if masked
771
 
772
always  @(posedge clk or posedge wb_rst_i)
773 45 gorban
        if (wb_rst_i) rda_int_pnd <= #1 0;
774
        else
775
                rda_int_pnd <= #1 ((rf_count == {1'b0,trigger_level}) && fifo_read) ? 0 :        // reset condition
776
                                                        rda_int_rise ? 1 :                                              // latch condition
777
                                                        rda_int_pnd && ier[`UART_IE_RDA];       // default operation: remove if masked
778
 
779
always  @(posedge clk or posedge wb_rst_i)
780 37 gorban
        if (wb_rst_i) thre_int_pnd <= #1 0;
781
        else
782 56 mohor
                thre_int_pnd <= #1 fifo_write || (iir_read & ~iir[`UART_II_IP] & iir[`UART_II_II] == `UART_II_THRE)? 0 :
783 37 gorban
                                                        thre_int_rise ? 1 :
784
                                                        thre_int_pnd && ier[`UART_IE_THRE];
785
 
786
always  @(posedge clk or posedge wb_rst_i)
787
        if (wb_rst_i) ms_int_pnd <= #1 0;
788
        else
789
                ms_int_pnd <= #1 msr_read ? 0 :
790
                                                        ms_int_rise ? 1 :
791
                                                        ms_int_pnd && ier[`UART_IE_MS];
792
 
793
always  @(posedge clk or posedge wb_rst_i)
794 42 mohor
        if (wb_rst_i) ti_int_pnd <= #1 0;
795 37 gorban
        else
796
                ti_int_pnd <= #1 fifo_read ? 0 :
797
                                                        ti_int_rise ? 1 :
798
                                                        ti_int_pnd && ier[`UART_IE_RDA];
799
// end of pending flags
800
 
801
// INT_O logic
802 27 mohor
always @(posedge clk or posedge wb_rst_i)
803
begin
804
        if (wb_rst_i)
805
                int_o <= #1 1'b0;
806
        else
807 37 gorban
                int_o <= #1
808
                                        rls_int_pnd             ?       ~lsr_mask                                       :
809 45 gorban
                                        rda_int_pnd             ? 1                                                             :
810 37 gorban
                                        ti_int_pnd              ? ~fifo_read                                    :
811 45 gorban
                                        thre_int_pnd    ? !(fifo_write & iir_read) :
812 37 gorban
                                        ms_int_pnd              ? ~msr_read                                             :
813
                                        0;       // if no interrupt are pending
814 27 mohor
end
815
 
816
 
817
// Interrupt Identification register
818
always @(posedge clk or posedge wb_rst_i)
819
begin
820
        if (wb_rst_i)
821
                iir <= #1 1;
822
        else
823 37 gorban
        if (rls_int_pnd)  // interrupt is pending
824 27 mohor
        begin
825
                iir[`UART_II_II] <= #1 `UART_II_RLS;    // set identification register to correct value
826
                iir[`UART_II_IP] <= #1 1'b0;            // and clear the IIR bit 0 (interrupt pending)
827 37 gorban
        end else // the sequence of conditions determines priority of interrupt identification
828 27 mohor
        if (rda_int)
829
        begin
830
                iir[`UART_II_II] <= #1 `UART_II_RDA;
831
                iir[`UART_II_IP] <= #1 1'b0;
832
        end
833 37 gorban
        else if (ti_int_pnd)
834 27 mohor
        begin
835
                iir[`UART_II_II] <= #1 `UART_II_TI;
836
                iir[`UART_II_IP] <= #1 1'b0;
837
        end
838 37 gorban
        else if (thre_int_pnd)
839 27 mohor
        begin
840
                iir[`UART_II_II] <= #1 `UART_II_THRE;
841
                iir[`UART_II_IP] <= #1 1'b0;
842
        end
843 37 gorban
        else if (ms_int_pnd)
844 27 mohor
        begin
845
                iir[`UART_II_II] <= #1 `UART_II_MS;
846
                iir[`UART_II_IP] <= #1 1'b0;
847 37 gorban
        end else        // no interrupt is pending
848 27 mohor
        begin
849 40 gorban
                iir[`UART_II_II] <= #1 0;
850 27 mohor
                iir[`UART_II_IP] <= #1 1'b1;
851
        end
852
end
853
 
854
endmodule

powered by: WebSVN 2.1.0

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