1 |
2 |
thorn_aitc |
/////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// Simple Baud Rate Generator ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// ////
|
6 |
|
|
//// Author: Rudolf Usselmann ////
|
7 |
|
|
//// rudi@asics.ws ////
|
8 |
|
|
//// ////
|
9 |
|
|
//// ////
|
10 |
|
|
//// Downloaded from: http://www.opencores.org/cores/sasc/ ////
|
11 |
|
|
//// ////
|
12 |
|
|
/////////////////////////////////////////////////////////////////////
|
13 |
|
|
//// ////
|
14 |
|
|
//// Copyright (C) 2000-2002 Rudolf Usselmann ////
|
15 |
|
|
//// www.asics.ws ////
|
16 |
|
|
//// rudi@asics.ws ////
|
17 |
|
|
//// ////
|
18 |
|
|
//// This source file may be used and distributed without ////
|
19 |
|
|
//// restriction provided that this copyright statement is not ////
|
20 |
|
|
//// removed from the file and that any derivative work contains ////
|
21 |
|
|
//// the original copyright notice and the associated disclaimer.////
|
22 |
|
|
//// ////
|
23 |
|
|
//// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ////
|
24 |
|
|
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ////
|
25 |
|
|
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ////
|
26 |
|
|
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ////
|
27 |
|
|
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ////
|
28 |
|
|
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ////
|
29 |
|
|
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ////
|
30 |
|
|
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ////
|
31 |
|
|
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ////
|
32 |
|
|
//// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ////
|
33 |
|
|
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ////
|
34 |
|
|
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ////
|
35 |
|
|
//// POSSIBILITY OF SUCH DAMAGE. ////
|
36 |
|
|
//// ////
|
37 |
|
|
/////////////////////////////////////////////////////////////////////
|
38 |
|
|
|
39 |
|
|
// CVS Log
|
40 |
|
|
//
|
41 |
|
|
// $Id: sasc_brg.v,v 1.1.1.1 2003-07-12 13:19:55 thorn_aitch Exp $
|
42 |
|
|
//
|
43 |
|
|
// $Date: 2003-07-12 13:19:55 $
|
44 |
|
|
// $Revision: 1.1.1.1 $
|
45 |
|
|
// $Author: thorn_aitch $
|
46 |
|
|
// $Locker: $
|
47 |
|
|
// $State: Exp $
|
48 |
|
|
//
|
49 |
|
|
// Change History:
|
50 |
|
|
// $Log: not supported by cvs2svn $
|
51 |
|
|
// Revision 1.1.1.1 2002/09/16 16:16:40 rudi
|
52 |
|
|
// Initial Checkin
|
53 |
|
|
//
|
54 |
|
|
//
|
55 |
|
|
//
|
56 |
|
|
//
|
57 |
|
|
//
|
58 |
|
|
//
|
59 |
|
|
//
|
60 |
|
|
//
|
61 |
|
|
|
62 |
|
|
`include "timescale.v"
|
63 |
|
|
|
64 |
|
|
/*
|
65 |
|
|
Baud rate Generator
|
66 |
|
|
==================
|
67 |
|
|
|
68 |
|
|
div0 - is the first stage divider
|
69 |
|
|
Set this to the desired number of cycles less two
|
70 |
|
|
div1 - is the second stage divider
|
71 |
|
|
Set this to the actual number of cycles
|
72 |
|
|
|
73 |
|
|
Remember you have to generate a baud rate that is 4 higher than what
|
74 |
|
|
you really want. This is because of the DPLL in the RX section ...
|
75 |
|
|
|
76 |
|
|
Example:
|
77 |
|
|
If your system clock is 50MHz and you want to generate a 9.6 Kbps baud rate:
|
78 |
|
|
9600*4 = 38400KHz
|
79 |
|
|
50MHz/38400KHz=1302 or 6*217
|
80 |
|
|
set div0=4 (6-2) and set div1=217
|
81 |
|
|
|
82 |
|
|
*/
|
83 |
|
|
|
84 |
|
|
// Following Comments are added by Thorn Aitch, Nov.02,2002
|
85 |
|
|
//=========================================================
|
86 |
|
|
// <Actual Baud Rate>
|
87 |
|
|
//
|
88 |
|
|
// Baud Rate = freq/[(div0 + 2)*(div1 + 1)*4]
|
89 |
|
|
//
|
90 |
|
|
// ex)9600bps : 20MHz, div0=3, div1=103
|
91 |
|
|
// 20MHz/5*105*4=9524bps (error= 0.79%)
|
92 |
|
|
//=========================================================
|
93 |
|
|
|
94 |
|
|
module sasc_brg(clk, rst, div0, div1, sio_ce, sio_ce_x4);
|
95 |
|
|
input clk;
|
96 |
|
|
input rst;
|
97 |
|
|
input [7:0] div0, div1;
|
98 |
|
|
output sio_ce, sio_ce_x4;
|
99 |
|
|
|
100 |
|
|
///////////////////////////////////////////////////////////////////
|
101 |
|
|
//
|
102 |
|
|
// Local Wires and Registers
|
103 |
|
|
//
|
104 |
|
|
|
105 |
|
|
reg [7:0] ps;
|
106 |
|
|
reg ps_clr;
|
107 |
|
|
reg [7:0] br_cnt;
|
108 |
|
|
reg br_clr;
|
109 |
|
|
reg sio_ce_x4_r;
|
110 |
|
|
reg [1:0] cnt;
|
111 |
|
|
reg sio_ce, sio_ce_x4;
|
112 |
|
|
reg sio_ce_r ;
|
113 |
|
|
reg sio_ce_x4_t;
|
114 |
|
|
|
115 |
|
|
///////////////////////////////////////////////////////////////////
|
116 |
|
|
//
|
117 |
|
|
// Boud Rate Generator
|
118 |
|
|
//
|
119 |
|
|
|
120 |
|
|
// -----------------------------------------------------
|
121 |
|
|
// Prescaler
|
122 |
|
|
always @(posedge clk)
|
123 |
|
|
if(!rst) ps <= #1 8'h0;
|
124 |
|
|
else
|
125 |
|
|
if(ps_clr) ps <= #1 8'h0;
|
126 |
|
|
else ps <= #1 ps + 8'h1;
|
127 |
|
|
|
128 |
|
|
always @(posedge clk)
|
129 |
|
|
ps_clr <= #1 (ps == div0); // Desired number of cycles less 2
|
130 |
|
|
|
131 |
|
|
// -----------------------------------------------------
|
132 |
|
|
// Oversampled Boud Rate (x4)
|
133 |
|
|
always @(posedge clk)
|
134 |
|
|
if(!rst) br_cnt <= #1 8'h0;
|
135 |
|
|
else
|
136 |
|
|
if(br_clr) br_cnt <= #1 8'h0;
|
137 |
|
|
// else br_cnt <= #1 br_cnt + 8'h1; // *** Deleted *** Oct.30,2002 Thorn Aitch
|
138 |
|
|
else if (ps_clr) br_cnt <= #1 br_cnt + 8'h1; // *** Added *** Oct.30,2002 Thorn Aitch
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
always @(posedge clk)
|
142 |
|
|
br_clr <= #1 (br_cnt == div1); // Prciese number of PS cycles
|
143 |
|
|
|
144 |
|
|
always @(posedge clk)
|
145 |
|
|
sio_ce_x4_r <= #1 br_clr;
|
146 |
|
|
|
147 |
|
|
always @(posedge clk)
|
148 |
|
|
sio_ce_x4_t <= #1 !sio_ce_x4_r & br_clr;
|
149 |
|
|
|
150 |
|
|
always @(posedge clk)
|
151 |
|
|
sio_ce_x4 <= #1 sio_ce_x4_t;
|
152 |
|
|
|
153 |
|
|
// -----------------------------------------------------
|
154 |
|
|
// Actual Boud rate
|
155 |
|
|
always @(posedge clk)
|
156 |
|
|
if(!rst) cnt <= #1 2'h0;
|
157 |
|
|
else
|
158 |
|
|
if(!sio_ce_x4_r & br_clr) cnt <= #1 cnt + 2'h1;
|
159 |
|
|
|
160 |
|
|
always @(posedge clk)
|
161 |
|
|
sio_ce_r <= #1 (cnt == 2'h0);
|
162 |
|
|
|
163 |
|
|
always @(posedge clk)
|
164 |
|
|
sio_ce <= #1 !sio_ce_r & (cnt == 2'h0);
|
165 |
|
|
|
166 |
|
|
endmodule
|
167 |
|
|
|