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

Subversion Repositories 8051

[/] [8051/] [tags/] [rel_1/] [rtl/] [verilog/] [oc8051_uart.v] - Blame information for rev 186

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 82 simont
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  8051 cores serial interface                                 ////
4
////                                                              ////
5
////  This file is part of the 8051 cores project                 ////
6
////  http://www.opencores.org/cores/8051/                        ////
7
////                                                              ////
8
////  Description                                                 ////
9
////   uart for 8051 core                                         ////
10
////                                                              ////
11
////  To Do:                                                      ////
12
////   Nothing                                                    ////
13
////                                                              ////
14
////  Author(s):                                                  ////
15
////      - Simon Teran, simont@opencores.org                     ////
16
////                                                              ////
17
//////////////////////////////////////////////////////////////////////
18
////                                                              ////
19
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
20
////                                                              ////
21
//// This source file may be used and distributed without         ////
22
//// restriction provided that this copyright statement is not    ////
23
//// removed from the file and that any derivative work contains  ////
24
//// the original copyright notice and the associated disclaimer. ////
25
////                                                              ////
26
//// This source file is free software; you can redistribute it   ////
27
//// and/or modify it under the terms of the GNU Lesser General   ////
28
//// Public License as published by the Free Software Foundation; ////
29
//// either version 2.1 of the License, or (at your option) any   ////
30
//// later version.                                               ////
31
////                                                              ////
32
//// This source is distributed in the hope that it will be       ////
33
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
34
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
35
//// PURPOSE.  See the GNU Lesser General Public License for more ////
36
//// details.                                                     ////
37
////                                                              ////
38
//// You should have received a copy of the GNU Lesser General    ////
39
//// Public License along with this source; if not, download it   ////
40
//// from http://www.opencores.org/lgpl.shtml                     ////
41
////                                                              ////
42
//////////////////////////////////////////////////////////////////////
43
//
44
// CVS Revision History
45
//
46
// $Log: not supported by cvs2svn $
47 135 simont
// Revision 1.13  2003/04/10 08:57:16  simont
48
// remove signal sbuf_txd [12:11]
49
//
50 119 simont
// Revision 1.12  2003/04/07 14:58:02  simont
51
// change sfr's interface.
52
//
53 116 simont
// Revision 1.11  2003/04/07 13:29:16  simont
54
// change uart to meet timing.
55
//
56 115 simont
// Revision 1.10  2003/01/13 14:14:41  simont
57
// replace some modules
58
//
59 82 simont
// Revision 1.9  2002/09/30 17:33:59  simont
60
// prepared header
61
//
62
//
63
 
64
// synopsys translate_off
65
`include "oc8051_timescale.v"
66
// synopsys translate_on
67
 
68
`include "oc8051_defines.v"
69
 
70 115 simont
module oc8051_uart (rst, clk,
71
             bit_in, data_in,
72 116 simont
             wr_addr,
73 115 simont
             wr, wr_bit,
74 116 simont
             rxd, txd,
75 115 simont
             intr,
76
             brate2, t1_ow, pres_ow,
77 116 simont
             rclk, tclk,
78
//registers
79
             scon, pcon, sbuf);
80 82 simont
 
81 115 simont
input        rst,
82
             clk,
83
             bit_in,
84
             wr,
85
             rxd,
86
             wr_bit,
87
             t1_ow,
88
             brate2,
89
             pres_ow,
90
             rclk,
91
             tclk;
92 116 simont
input [7:0]  data_in,
93 115 simont
             wr_addr;
94 82 simont
 
95 115 simont
output       txd,
96 116 simont
             intr;
97
output [7:0] scon,
98
             pcon,
99
             sbuf;
100 82 simont
 
101
 
102 115 simont
reg t1_ow_buf;
103 82 simont
//
104 116 simont
reg [7:0] scon, pcon;
105 82 simont
 
106
 
107 115 simont
reg        txd,
108
           trans,
109
           receive,
110
           tx_done,
111
           rx_done,
112
           rxd_r,
113
           shift_tr,
114
           shift_re;
115
reg [1:0]  rx_sam;
116
reg [3:0]  tr_count,
117
           re_count;
118
reg [7:0]  sbuf_rxd;
119
reg [11:0] sbuf_rxd_tmp;
120 119 simont
reg [10:0] sbuf_txd;
121 115 simont
 
122 116 simont
assign sbuf = sbuf_rxd;
123 82 simont
assign intr = scon[1] | scon [0];
124
 
125
//
126
//serial port control register
127
//
128 115 simont
wire ren, tb8, rb8, ri;
129
assign ren = scon[4];
130
assign tb8 = scon[3];
131
assign rb8 = scon[2];
132
assign ri  = scon[0];
133
 
134 82 simont
always @(posedge clk or posedge rst)
135
begin
136
  if (rst)
137
    scon <= #1 `OC8051_RST_SCON;
138
  else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_SCON))
139
    scon <= #1 data_in;
140
  else if ((wr) & (wr_bit) & (wr_addr[7:3]==`OC8051_SFR_B_SCON))
141
    scon[wr_addr[2:0]] <= #1 bit_in;
142 115 simont
  else if (tx_done)
143 82 simont
    scon[1] <= #1 1'b1;
144 115 simont
  else if (!rx_done) begin
145
    if (scon[7:6]==2'b00) begin
146
      scon[0] <= #1 1'b1;
147
    end else if ((sbuf_rxd_tmp[11]) | !(scon[5])) begin
148
      scon[0] <= #1 1'b1;
149
      scon[2] <= #1 sbuf_rxd_tmp[11];
150
    end else
151
      scon[2] <= #1 sbuf_rxd_tmp[11];
152
  end
153
end
154
 
155
//
156
//power control register
157
//
158
wire smod;
159
assign smod = pcon[7];
160
always @(posedge clk or posedge rst)
161
begin
162
  if (rst)
163
  begin
164
    pcon <= #1 `OC8051_RST_PCON;
165
  end else if ((wr_addr==`OC8051_SFR_PCON) & (wr) & !(wr_bit))
166
    pcon <= #1 data_in;
167
end
168
 
169
 
170
//
171
//serial port buffer (transmit)
172
//
173
 
174
wire wr_sbuf;
175
assign wr_sbuf = (wr_addr==`OC8051_SFR_SBUF) & (wr) & !(wr_bit);
176
 
177
always @(posedge clk or posedge rst)
178
begin
179
  if (rst) begin
180
    txd      <= #1 1'b1;
181
    tr_count <= #1 4'd0;
182
    trans    <= #1 1'b0;
183
    sbuf_txd <= #1 11'h00;
184
    tx_done  <= #1 1'b0;
185
//
186
// start transmiting
187
//
188
  end else if (wr_sbuf) begin
189 82 simont
    case (scon[7:6])
190 115 simont
      2'b00: begin  // mode 0
191
        sbuf_txd <= #1 {3'b001, data_in};
192 82 simont
      end
193 115 simont
      2'b01: begin // mode 1
194
        sbuf_txd <= #1 {2'b01, data_in, 1'b0};
195
      end
196
      default: begin  // mode 2 and mode 3
197
        sbuf_txd <= #1 {1'b1, tb8, data_in, 1'b0};
198
      end
199 82 simont
    endcase
200 115 simont
    trans    <= #1 1'b1;
201
    tr_count <= #1 4'd0;
202
    tx_done  <= #1 1'b0;
203
//
204
// transmiting
205
//
206
  end else if (trans & (scon[7:6] == 2'b00) & pres_ow) // mode 0
207
  begin
208
    if (~|sbuf_txd[10:1]) begin
209
      trans   <= #1 1'b0;
210
      tx_done <= #1 1'b1;
211
    end else begin
212
      {sbuf_txd, txd} <= #1 {1'b0, sbuf_txd};
213
      tx_done         <= #1 1'b0;
214
    end
215
  end else if (trans & (scon[7:6] != 2'b00) & shift_tr) begin // mode 1, 2, 3
216
    tr_count <= #1 tr_count + 4'd1;
217
    if (~|tr_count) begin
218
      if (~|sbuf_txd[10:0]) begin
219
        trans   <= #1 1'b0;
220
        tx_done <= #1 1'b1;
221
        txd <= #1 1'b1;
222
      end else begin
223
        {sbuf_txd, txd} <= #1 {1'b0, sbuf_txd};
224
        tx_done         <= #1 1'b0;
225
      end
226
    end
227
  end else if (!trans) begin
228
    txd     <= #1 1'b1;
229
    tx_done <= #1 1'b0;
230 82 simont
  end
231
end
232
 
233
//
234
//
235 115 simont
reg sc_clk_tr, smod_clk_tr;
236
always @(brate2 or t1_ow or t1_ow_buf or scon[7:6] or tclk)
237
begin
238
  if (scon[7:6]==8'b10) begin //mode 2
239
    sc_clk_tr = 1'b1;
240
  end else if (tclk) begin //
241
    sc_clk_tr = brate2;
242
  end else begin //
243
    sc_clk_tr = !t1_ow_buf & t1_ow;
244
  end
245
end
246
 
247 82 simont
always @(posedge clk or posedge rst)
248
begin
249
  if (rst) begin
250 115 simont
    smod_clk_tr <= #1 1'b0;
251
    shift_tr    <= #1 1'b0;
252
  end else if (sc_clk_tr) begin
253
    if (smod) begin
254
      shift_tr <= #1 1'b1;
255
    end else begin
256
      shift_tr    <= #1  smod_clk_tr;
257
      smod_clk_tr <= #1 !smod_clk_tr;
258
    end
259
  end else begin
260
    shift_tr <= #1 1'b0;
261
  end
262 82 simont
end
263
 
264
 
265
//
266 115 simont
//serial port buffer (receive)
267 82 simont
//
268
always @(posedge clk or posedge rst)
269
begin
270 115 simont
  if (rst) begin
271
    re_count     <= #1 4'd0;
272
    receive      <= #1 1'b0;
273
    sbuf_rxd     <= #1 8'h00;
274
    sbuf_rxd_tmp <= #1 12'd0;
275
    rx_done      <= #1 1'b1;
276
    rxd_r        <= #1 1'b1;
277
    rx_sam       <= #1 2'b00;
278
  end else if (!rx_done) begin
279
    receive <= #1 1'b0;
280
    rx_done <= #1 1'b1;
281 116 simont
    sbuf_rxd <= #1 sbuf_rxd_tmp[10:3];
282 115 simont
  end else if (receive & (scon[7:6]==2'b00) & pres_ow) begin //mode 0
283
    {sbuf_rxd_tmp, rx_done} <= #1 {rxd, sbuf_rxd_tmp};
284
  end else if (receive & (scon[7:6]!=2'b00) & shift_re) begin //mode 1, 2, 3
285
    re_count <= #1 re_count + 4'd1;
286
    case (re_count)
287
      4'h7: rx_sam[0] <= #1 rxd;
288
      4'h8: rx_sam[1] <= #1 rxd;
289
      4'h9: begin
290
        {sbuf_rxd_tmp, rx_done} <= #1 {(rxd==rx_sam[0] ? rxd : rx_sam[1]), sbuf_rxd_tmp};
291
      end
292
    endcase
293
//
294
//start receiving
295
//
296
  end else if (scon[7:6]==2'b00) begin //start mode 0
297
    rx_done <= #1 1'b1;
298
    if (ren && !ri && !receive) begin
299
      receive      <= #1 1'b1;
300
      sbuf_rxd_tmp <= #1 10'h0ff;
301
    end
302
  end else if (ren & shift_re) begin
303
    rxd_r <= #1 rxd;
304
    rx_done <= #1 1'b1;
305
    re_count <= #1 4'h0;
306
    receive <= #1 (rxd_r & !rxd);
307
    sbuf_rxd_tmp <= #1 10'h1ff;
308 135 simont
  end else if (!ren) begin
309
    rxd_r <= #1 rxd;
310 115 simont
  end else
311
    rx_done <= #1 1'b1;
312 82 simont
end
313
 
314
//
315
//
316 115 simont
reg sc_clk_re, smod_clk_re;
317
always @(brate2 or t1_ow or t1_ow_buf or scon[7:6] or rclk)
318
begin
319
  if (scon[7:6]==8'b10) begin //mode 2
320
    sc_clk_re = 1'b1;
321
  end else if (rclk) begin //
322
    sc_clk_re = brate2;
323
  end else begin //
324
    sc_clk_re = !t1_ow_buf & t1_ow;
325
  end
326
end
327
 
328 82 simont
always @(posedge clk or posedge rst)
329
begin
330
  if (rst) begin
331 115 simont
    smod_clk_re <= #1 1'b0;
332
    shift_re    <= #1 1'b0;
333
  end else if (sc_clk_re) begin
334
    if (smod) begin
335
      shift_re <= #1 1'b1;
336
    end else begin
337
      shift_re    <= #1  smod_clk_re;
338
      smod_clk_re <= #1 !smod_clk_re;
339
    end
340
  end else begin
341
    shift_re <= #1 1'b0;
342
  end
343
end
344
 
345
 
346 82 simont
 
347
//
348
//
349
//
350
 
351
always @(posedge clk or posedge rst)
352
begin
353
  if (rst) begin
354
    t1_ow_buf <= #1 1'b0;
355
  end else begin
356
    t1_ow_buf <= #1 t1_ow;
357
  end
358
end
359
 
360 115 simont
 
361 82 simont
 
362
endmodule
363
 

powered by: WebSVN 2.1.0

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