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

Subversion Repositories test_project

[/] [test_project/] [trunk/] [rtl/] [verilog/] [components/] [uart16550/] [uart_transmitter.v] - Blame information for rev 21

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

Line No. Rev Author Line
1 18 unneback
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  uart_transmitter.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 transmitter 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: uart_transmitter.v,v $
66
// Revision 1.19  2002/07/29 21:16:18  gorban
67
// The uart_defines.v file is included again in sources.
68
//
69
// Revision 1.18  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.16  2002/01/08 11:29:40  mohor
86
// tf_pop was too wide. Now it is only 1 clk cycle width.
87
//
88
// Revision 1.15  2001/12/17 14:46:48  mohor
89
// overrun signal was moved to separate block because many sequential lsr
90
// reads were preventing data from being written to rx fifo.
91
// underrun signal was not used and was removed from the project.
92
//
93
// Revision 1.14  2001/12/03 21:44:29  gorban
94
// Updated specification documentation.
95
// Added full 32-bit data bus interface, now as default.
96
// Address is 5-bit wide in 32-bit data bus mode.
97
// Added wb_sel_i input to the core. It's used in the 32-bit mode.
98
// Added debug interface with two 32-bit read-only registers in 32-bit mode.
99
// Bits 5 and 6 of LSR are now only cleared on TX FIFO write.
100
// My small test bench is modified to work with 32-bit mode.
101
//
102
// Revision 1.13  2001/11/08 14:54:23  mohor
103
// Comments in Slovene language deleted, few small fixes for better work of
104
// old tools. IRQs need to be fix.
105
//
106
// Revision 1.12  2001/11/07 17:51:52  gorban
107
// Heavily rewritten interrupt and LSR subsystems.
108
// Many bugs hopefully squashed.
109
//
110
// Revision 1.11  2001/10/29 17:00:46  gorban
111
// fixed parity sending and tx_fifo resets over- and underrun
112
//
113
// Revision 1.10  2001/10/20 09:58:40  gorban
114
// Small synopsis fixes
115
//
116
// Revision 1.9  2001/08/24 21:01:12  mohor
117
// Things connected to parity changed.
118
// Clock devider changed.
119
//
120
// Revision 1.8  2001/08/23 16:05:05  mohor
121
// Stop bit bug fixed.
122
// Parity bug fixed.
123
// WISHBONE read cycle bug fixed,
124
// OE indicator (Overrun Error) bug fixed.
125
// PE indicator (Parity Error) bug fixed.
126
// Register read bug fixed.
127
//
128
// Revision 1.6  2001/06/23 11:21:48  gorban
129
// DL made 16-bit long. Fixed transmission/reception bugs.
130
//
131
// Revision 1.5  2001/06/02 14:28:14  gorban
132
// Fixed receiver and transmitter. Major bug fixed.
133
//
134
// Revision 1.4  2001/05/31 20:08:01  gorban
135
// FIFO changes and other corrections.
136
//
137
// Revision 1.3  2001/05/27 17:37:49  gorban
138
// Fixed many bugs. Updated spec. Changed FIFO files structure. See CHANGES.txt file.
139
//
140
// Revision 1.2  2001/05/21 19:12:02  gorban
141
// Corrected some Linter messages.
142
//
143
// Revision 1.1  2001/05/17 18:34:18  gorban
144
// First 'stable' release. Should be sythesizable now. Also added new header.
145
//
146
// Revision 1.0  2001-05-17 21:27:12+02  jacob
147
// Initial revision
148
//
149
//
150
 
151
// synopsys translate_off
152
`include "timescale.v"
153
// synopsys translate_on
154
 
155
`include "uart_defines.v"
156
 
157
module uart_transmitter (clk, wb_rst_i, lcr, tf_push, wb_dat_i, enable, stx_pad_o, tstate, tf_count, tx_reset, lsr_mask);
158
 
159
input                                                                           clk;
160
input                                                                           wb_rst_i;
161
input [7:0]                                                              lcr;
162
input                                                                           tf_push;
163
input [7:0]                                                              wb_dat_i;
164
input                                                                           enable;
165
input                                                                           tx_reset;
166
input                                                                           lsr_mask; //reset of fifo
167
output                                                                          stx_pad_o;
168
output [2:0]                                                             tstate;
169
output [`UART_FIFO_COUNTER_W-1:0]        tf_count;
170
 
171
reg [2:0]                                                                        tstate;
172
reg [4:0]                                                                        counter;
173
reg [2:0]                                                                        bit_counter;   // counts the bits to be sent
174
reg [6:0]                                                                        shift_out;      // output shift register
175
reg                                                                                     stx_o_tmp;
176
reg                                                                                     parity_xor;  // parity of the word
177
reg                                                                                     tf_pop;
178
reg                                                                                     bit_out;
179
 
180
// TX FIFO instance
181
//
182
// Transmitter FIFO signals
183
wire [`UART_FIFO_WIDTH-1:0]                      tf_data_in;
184
wire [`UART_FIFO_WIDTH-1:0]                      tf_data_out;
185
wire                                                                                    tf_push;
186
wire                                                                                    tf_overrun;
187
wire [`UART_FIFO_COUNTER_W-1:0]          tf_count;
188
 
189
assign                                                                          tf_data_in = wb_dat_i;
190
 
191
uart_tfifo fifo_tx(     // error bit signal is not used in transmitter FIFO
192
        .clk(           clk             ),
193
        .wb_rst_i(      wb_rst_i        ),
194
        .data_in(       tf_data_in      ),
195
        .data_out(      tf_data_out     ),
196
        .push(          tf_push         ),
197
        .pop(           tf_pop          ),
198
        .overrun(       tf_overrun      ),
199
        .count(         tf_count        ),
200
        .fifo_reset(    tx_reset        ),
201
        .reset_status(lsr_mask)
202
);
203
 
204
`ifdef UART_LOG_TX
205
   integer file_handler;
206
 
207
initial
208
  begin
209
     file_handler = $fopen("uart_tx.txt");
210
  end
211
 
212
always @(negedge tf_push)
213
  begin
214
   $fwrite(file_handler, "%s", tf_data_out);
215
   $display("UART: %s", tf_data_out);
216
  end
217
`endif
218
 
219
// TRANSMITTER FINAL STATE MACHINE
220
 
221
parameter s_idle        = 3'd0;
222
parameter s_send_start  = 3'd1;
223
parameter s_send_byte   = 3'd2;
224
parameter s_send_parity = 3'd3;
225
parameter s_send_stop   = 3'd4;
226
parameter s_pop_byte    = 3'd5;
227
 
228
always @(posedge clk or posedge wb_rst_i)
229
begin
230
  if (wb_rst_i)
231
  begin
232
        tstate       <= #1 s_idle;
233
        stx_o_tmp       <= #1 1'b1;
234
        counter   <= #1 5'b0;
235
        shift_out   <= #1 7'b0;
236
        bit_out     <= #1 1'b0;
237
        parity_xor  <= #1 1'b0;
238
        tf_pop      <= #1 1'b0;
239
        bit_counter <= #1 3'b0;
240
  end
241
  else
242
  if (enable)
243
  begin
244
        case (tstate)
245
        s_idle   :      if (~|tf_count) // if tf_count==0
246
                        begin
247
                                tstate <= #1 s_idle;
248
                                stx_o_tmp <= #1 1'b1;
249
                        end
250
                        else
251
                        begin
252
                                tf_pop <= #1 1'b0;
253
                                stx_o_tmp  <= #1 1'b1;
254
                                tstate  <= #1 s_pop_byte;
255
                        end
256
        s_pop_byte :    begin
257
                                tf_pop <= #1 1'b1;
258
                                case (lcr[/*`UART_LC_BITS*/1:0])  // number of bits in a word
259
                                2'b00 : begin
260
                                        bit_counter <= #1 3'b100;
261
                                        parity_xor  <= #1 ^tf_data_out[4:0];
262
                                     end
263
                                2'b01 : begin
264
                                        bit_counter <= #1 3'b101;
265
                                        parity_xor  <= #1 ^tf_data_out[5:0];
266
                                     end
267
                                2'b10 : begin
268
                                        bit_counter <= #1 3'b110;
269
                                        parity_xor  <= #1 ^tf_data_out[6:0];
270
                                     end
271
                                2'b11 : begin
272
                                        bit_counter <= #1 3'b111;
273
                                        parity_xor  <= #1 ^tf_data_out[7:0];
274
                                     end
275
                                endcase
276
                                {shift_out[6:0], bit_out} <= #1 tf_data_out;
277
                                tstate <= #1 s_send_start;
278
                        end
279
        s_send_start :  begin
280
                                tf_pop <= #1 1'b0;
281
                                if (~|counter)
282
                                        counter <= #1 5'b01111;
283
                                else
284
                                if (counter == 5'b00001)
285
                                begin
286
                                        counter <= #1 0;
287
                                        tstate <= #1 s_send_byte;
288
                                end
289
                                else
290
                                        counter <= #1 counter - 1'b1;
291
                                stx_o_tmp <= #1 1'b0;
292
                        end
293
        s_send_byte :   begin
294
                                if (~|counter)
295
                                        counter <= #1 5'b01111;
296
                                else
297
                                if (counter == 5'b00001)
298
                                begin
299
                                        if (bit_counter > 3'b0)
300
                                        begin
301
                                                bit_counter <= #1 bit_counter - 1'b1;
302
                                                {shift_out[5:0],bit_out  } <= #1 {shift_out[6:1], shift_out[0]};
303
                                                tstate <= #1 s_send_byte;
304
                                        end
305
                                        else   // end of byte
306
                                        if (~lcr[`UART_LC_PE])
307
                                        begin
308
                                                tstate <= #1 s_send_stop;
309
                                        end
310
                                        else
311
                                        begin
312
                                                case ({lcr[`UART_LC_EP],lcr[`UART_LC_SP]})
313
                                                2'b00:  bit_out <= #1 ~parity_xor;
314
                                                2'b01:  bit_out <= #1 1'b1;
315
                                                2'b10:  bit_out <= #1 parity_xor;
316
                                                2'b11:  bit_out <= #1 1'b0;
317
                                                endcase
318
                                                tstate <= #1 s_send_parity;
319
                                        end
320
                                        counter <= #1 0;
321
                                end
322
                                else
323
                                        counter <= #1 counter - 1'b1;
324
                                stx_o_tmp <= #1 bit_out; // set output pin
325
                        end
326
        s_send_parity : begin
327
                                if (~|counter)
328
                                        counter <= #1 5'b01111;
329
                                else
330
                                if (counter == 5'b00001)
331
                                begin
332
                                        counter <= #1 4'b0;
333
                                        tstate <= #1 s_send_stop;
334
                                end
335
                                else
336
                                        counter <= #1 counter - 1'b1;
337
                                stx_o_tmp <= #1 bit_out;
338
                        end
339
        s_send_stop :  begin
340
                                if (~|counter)
341
                                  begin
342
                                                casex ({lcr[`UART_LC_SB],lcr[`UART_LC_BITS]})
343
                                                3'b0xx:   counter <= #1 5'b01101;     // 1 stop bit ok igor
344
                                                3'b100:   counter <= #1 5'b10101;     // 1.5 stop bit
345
                                                default:          counter <= #1 5'b11101;     // 2 stop bits
346
                                                endcase
347
                                        end
348
                                else
349
                                if (counter == 5'b00001)
350
                                begin
351
                                        counter <= #1 0;
352
                                        tstate <= #1 s_idle;
353
                                end
354
                                else
355
                                        counter <= #1 counter - 1'b1;
356
                                stx_o_tmp <= #1 1'b1;
357
                        end
358
 
359
                default : // should never get here
360
                        tstate <= #1 s_idle;
361
        endcase
362
  end // end if enable
363
  else
364
    tf_pop <= #1 1'b0;  // tf_pop must be 1 cycle width
365
end // transmitter logic
366
 
367
assign stx_pad_o = lcr[`UART_LC_BC] ? 1'b0 : stx_o_tmp;    // Break condition
368
 
369
endmodule

powered by: WebSVN 2.1.0

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