1 //==============================================================================
2 // File: $URL: svn+ssh://repositorypub@repository.eecs.berkeley.edu/public/Projects/GateLib/branches/dev/GateLibCore/Hardware/Library/Const.v $
3 // Version: $Revision: 16355 $
4 // Author: Greg Gibeling (http://gdgib.gotdns.com/~gdgib/)
5 // Copyright: Copyright 2003-2008 UC Berkeley
6 //==============================================================================
8 //==============================================================================
10 //==============================================================================
11 // Copyright (c) 2005-2008, Regents of the University of California
12 // All rights reserved.
14 // Redistribution and use in source and binary forms, with or without modification,
15 // are permitted provided that the following conditions are met:
17 // - Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimer.
19 // - Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer
21 // in the documentation and/or other materials provided with the
23 // - Neither the name of the University of California, Berkeley nor the
24 // names of its contributors may be used to endorse or promote
25 // products derived from this software without specific prior
26 // written permission.
28 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
29 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
32 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
35 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //==============================================================================
40 //------------------------------------------------------------------------------
41 // Section: Simulation Flag
42 // Desc: This little nebulous block will define the flags:
43 // -SIMULATION Simulating
44 // -MODELSIM Simulating using ModelSim
45 // -XST Synthesizing with XST
46 // -SYNPLIFY Synthesizing with Synplify
47 // -SYNTHESIS Synthesizing
48 // -MACROSAFE Safe to use macros (Synplify or ModelSim)
50 // YOU SHOULD DEFINE THE "MODELSIM" FLAG FOR SIMULATION!!!!
51 //------------------------------------------------------------------------------
52 `ifdef synthesis // if Synplify
56 `else // if not Synplify
62 // synthesis translate_off // if XST then stop compiling
66 // synthesis translate_on // if XST then resume compiling
73 //------------------------------------------------------------------------------
75 //------------------------------------------------------------------------------
76 // Section: Log2 Macro
77 // Desc: A macro to take the log base 2 of any number. Useful for
78 // calculating bitwidths. Warning, this actually calculates
79 // log2(x-1), not log2(x).
80 //------------------------------------------------------------------------------
82 `define log2(x) ((((x) > 1) ? 1 : 0) + \
83 (((x) > 2) ? 1 : 0) + \
84 (((x) > 4) ? 1 : 0) + \
85 (((x) > 8) ? 1 : 0) + \
86 (((x) > 16) ? 1 : 0) + \
87 (((x) > 32) ? 1 : 0) + \
88 (((x) > 64) ? 1 : 0) + \
89 (((x) > 128) ? 1 : 0) + \
90 (((x) > 256) ? 1 : 0) + \
91 (((x) > 512) ? 1 : 0) + \
92 (((x) > 1024) ? 1 : 0) + \
93 (((x) > 2048) ? 1 : 0) + \
94 (((x) > 4096) ? 1 : 0) + \
95 (((x) > 8192) ? 1 : 0) + \
96 (((x) > 16384) ? 1 : 0) + \
97 (((x) > 32768) ? 1 : 0) + \
98 (((x) > 65536) ? 1 : 0) + \
99 (((x) > 131072) ? 1 : 0) + \
100 (((x) > 262144) ? 1 : 0) + \
101 (((x) > 524288) ? 1 : 0) + \
102 (((x) > 1048576) ? 1 : 0) + \
103 (((x) > 2097152) ? 1 : 0) + \
104 (((x) > 4194304) ? 1 : 0) + \
105 (((x) > 8388608) ? 1 : 0) + \
106 (((x) > 16777216) ? 1 : 0) + \
107 (((x) > 33554432) ? 1 : 0) + \
108 (((x) > 67108864) ? 1 : 0) + \
109 (((x) > 134217728) ? 1 : 0) + \
110 (((x) > 268435456) ? 1 : 0) + \
111 (((x) > 536870912) ? 1 : 0) + \
112 (((x) > 1073741824) ? 1 : 0))
114 //------------------------------------------------------------------------------
116 //------------------------------------------------------------------------------
117 // Section: Log2 Floor Macro
118 // Desc: A macro to take the floor of the log base 2 of any number.
119 //------------------------------------------------------------------------------
121 `define log2f(x) ((((x) >= 2) ? 1 : 0) + \
122 (((x) >= 4) ? 1 : 0) + \
123 (((x) >= 8) ? 1 : 0) + \
124 (((x) >= 16) ? 1 : 0) + \
125 (((x) >= 32) ? 1 : 0) + \
126 (((x) >= 64) ? 1 : 0) + \
127 (((x) >= 128) ? 1 : 0) + \
128 (((x) >= 256) ? 1 : 0) + \
129 (((x) >= 512) ? 1 : 0) + \
130 (((x) >= 1024) ? 1 : 0) + \
131 (((x) >= 2048) ? 1 : 0) + \
132 (((x) >= 4096) ? 1 : 0) + \
133 (((x) >= 8192) ? 1 : 0) + \
134 (((x) >= 16384) ? 1 : 0) + \
135 (((x) >= 32768) ? 1 : 0) + \
136 (((x) >= 65536) ? 1 : 0) + \
137 (((x) >= 131072) ? 1 : 0) + \
138 (((x) >= 262144) ? 1 : 0) + \
139 (((x) >= 524288) ? 1 : 0) + \
140 (((x) >= 1048576) ? 1 : 0) + \
141 (((x) >= 2097152) ? 1 : 0) + \
142 (((x) >= 4194304) ? 1 : 0) + \
143 (((x) >= 8388608) ? 1 : 0) + \
144 (((x) >= 16777216) ? 1 : 0) + \
145 (((x) >= 33554432) ? 1 : 0) + \
146 (((x) >= 67108864) ? 1 : 0) + \
147 (((x) >= 134217728) ? 1 : 0) + \
148 (((x) >= 268435456) ? 1 : 0) + \
149 (((x) >= 536870912) ? 1 : 0) + \
150 (((x) >= 1073741824) ? 1 : 0))
152 //------------------------------------------------------------------------------
154 //------------------------------------------------------------------------------
155 // Section: Pow2 Macro
156 // Desc: A macro to take the 2 to the power of any number. Useful for
157 // calculating bitwidths.
158 //------------------------------------------------------------------------------
160 `define pow2(x) ((((x) >= 1) ? 2 : 1) * \
161 (((x) >= 2) ? 2 : 1) * \
162 (((x) >= 3) ? 2 : 1) * \
163 (((x) >= 4) ? 2 : 1) * \
164 (((x) >= 5) ? 2 : 1) * \
165 (((x) >= 6) ? 2 : 1) * \
166 (((x) >= 7) ? 2 : 1) * \
167 (((x) >= 8) ? 2 : 1) * \
168 (((x) >= 9) ? 2 : 1) * \
169 (((x) >= 10) ? 2 : 1) * \
170 (((x) >= 11) ? 2 : 1) * \
171 (((x) >= 12) ? 2 : 1) * \
172 (((x) >= 13) ? 2 : 1) * \
173 (((x) >= 14) ? 2 : 1) * \
174 (((x) >= 15) ? 2 : 1) * \
175 (((x) >= 16) ? 2 : 1) * \
176 (((x) >= 17) ? 2 : 1) * \
177 (((x) >= 18) ? 2 : 1) * \
178 (((x) >= 19) ? 2 : 1) * \
179 (((x) >= 20) ? 2 : 1) * \
180 (((x) >= 21) ? 2 : 1) * \
181 (((x) >= 22) ? 2 : 1) * \
182 (((x) >= 23) ? 2 : 1) * \
183 (((x) >= 24) ? 2 : 1) * \
184 (((x) >= 25) ? 2 : 1) * \
185 (((x) >= 26) ? 2 : 1) * \
186 (((x) >= 27) ? 2 : 1) * \
187 (((x) >= 28) ? 2 : 1) * \
188 (((x) >= 29) ? 2 : 1) * \
189 (((x) >= 30) ? 2 : 1) * \
190 (((x) >= 31) ? 2 : 1))
192 //------------------------------------------------------------------------------
194 //------------------------------------------------------------------------------
195 // Section: Max/Min Macros
196 // Desc: Standard binary max/min macros
197 //------------------------------------------------------------------------------
199 `define max(x,y) ((x) > (y) ? (x) : (y))
200 `define min(x,y) ((x) < (y) ? (x) : (y))
202 //------------------------------------------------------------------------------
204 //------------------------------------------------------------------------------
205 // Section: Integer Division Macros
206 // Desc: Rounding and ceiling for integer division
207 //------------------------------------------------------------------------------
209 `define divceil(x,y) (((x) + ((y) - 1)) / (y))
210 `define divrnd(x,y) (((x) + ((y) >> 1)) / (y))
212 //------------------------------------------------------------------------------