updates for ml410 board
[fleet.git] / src / edu / berkeley / fleet / fpga / sasc_brg.v
1 /////////////////////////////////////////////////////////////////////
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.2 2002/11/08 15:22:49 rudi Exp $
42 //
43 //  $Date: 2002/11/08 15:22:49 $
44 //  $Revision: 1.2 $
45 //  $Author: rudi $
46 //  $Locker:  $
47 //  $State: Exp $
48 //
49 // Change History:
50 //               $Log: sasc_brg.v,v $
51 //               Revision 1.2  2002/11/08 15:22:49  rudi
52 //
53 //               Fixed a typo in brg
54 //
55 //               Revision 1.1.1.1  2002/09/16 16:16:40  rudi
56 //               Initial Checkin
57 //
58 //
59 //
60 //
61 //
62 //
63 //
64 //
65
66 `include "timescale.v"
67
68 /*
69         Baud rate Generator
70         ==================
71
72         div0 -  is the first stage divider
73                 Set this to the desired number of cycles less two
74         div1 -  is the second stage divider
75                 Set this to the actual number of cycles
76
77         Remember you have to generate a baud rate that is 4 higher than what
78         you really want. This is because of the DPLL in the RX section ...
79
80         Example:
81         If your system clock is 50MHz and you want to generate a 9.6 Kbps baud rate:
82         9600*4 = 38400KHz
83         50MHz/38400KHz=1302 or 6*217
84         set div0=4 (6-2) and set div1=217
85
86         100MHz/38400KHz=2604 or 12*217
87         set div0=10 (12-2) and set div1=217
88
89 */
90
91 module sasc_brg(clk, rst, div0, div1, sio_ce, sio_ce_x4);
92 input           clk;
93 input           rst;
94 input   [7:0]   div0, div1;
95 output          sio_ce, sio_ce_x4;
96
97 ///////////////////////////////////////////////////////////////////
98 //
99 // Local Wires and Registers
100 //
101
102 reg     [7:0]   ps;
103 reg             ps_clr;
104 reg     [7:0]   br_cnt;
105 reg             br_clr;
106 reg             sio_ce_x4_r;
107 reg     [1:0]   cnt;
108 reg             sio_ce, sio_ce_x4;
109 reg             sio_ce_r ;
110 reg             sio_ce_x4_t;
111
112 ///////////////////////////////////////////////////////////////////
113 //
114 // Boud Rate Generator
115 //
116
117 // -----------------------------------------------------
118 // Prescaler
119 always @(posedge clk)
120         if(!rst)        ps <= #1 8'h0;
121         else
122         if(ps_clr)      ps <= #1 8'h0;
123         else            ps <= #1 ps + 8'h1;
124
125 always @(posedge clk)
126         ps_clr <= #1 (ps == div0);      // Desired number of cycles less 2
127
128 // -----------------------------------------------------
129 // Oversampled Boud Rate (x4)
130 always @(posedge clk)
131         if(!rst)        br_cnt <= #1 8'h0;
132         else
133         if(br_clr)      br_cnt <= #1 8'h0;
134         else
135         if(ps_clr)      br_cnt <= #1 br_cnt + 8'h1;
136
137 always @(posedge clk)
138         br_clr <= #1 (br_cnt == div1); // Prciese number of PS cycles
139
140 always @(posedge clk)
141         sio_ce_x4_r <= #1 br_clr;
142
143 always @(posedge clk)
144         sio_ce_x4_t <= #1 !sio_ce_x4_r & br_clr;
145
146 always @(posedge clk)
147         sio_ce_x4 <= #1 sio_ce_x4_t;
148
149 // -----------------------------------------------------
150 // Actual Boud rate
151 always @(posedge clk)
152         if(!rst)                        cnt <= #1 2'h0;
153         else
154         if(!sio_ce_x4_r & br_clr)       cnt <= #1 cnt + 2'h1;
155
156 always @(posedge clk)
157         sio_ce_r <= #1 (cnt == 2'h0);
158
159 always @(posedge clk)
160         sio_ce <= #1 !sio_ce_r & (cnt == 2'h0);
161
162 endmodule
163