1 |
2 |
vv_gulyaev |
-- Processor Common Library Package
|
2 |
|
|
-------------------------------------------------------------------------------
|
3 |
|
|
--
|
4 |
|
|
-- *************************************************************************
|
5 |
|
|
-- ** **
|
6 |
|
|
-- ** DISCLAIMER OF LIABILITY **
|
7 |
|
|
-- ** **
|
8 |
|
|
-- ** This text/file contains proprietary, confidential **
|
9 |
|
|
-- ** information of Xilinx, Inc., is distributed under **
|
10 |
|
|
-- ** license from Xilinx, Inc., and may be used, copied **
|
11 |
|
|
-- ** and/or disclosed only pursuant to the terms of a valid **
|
12 |
|
|
-- ** license agreement with Xilinx, Inc. Xilinx hereby **
|
13 |
|
|
-- ** grants you a license to use this text/file solely for **
|
14 |
|
|
-- ** design, simulation, implementation and creation of **
|
15 |
|
|
-- ** design files limited to Xilinx devices or technologies. **
|
16 |
|
|
-- ** Use with non-Xilinx devices or technologies is expressly **
|
17 |
|
|
-- ** prohibited and immediately terminates your license unless **
|
18 |
|
|
-- ** covered by a separate agreement. **
|
19 |
|
|
-- ** **
|
20 |
|
|
-- ** Xilinx is providing this design, code, or information **
|
21 |
|
|
-- ** "as-is" solely for use in developing programs and **
|
22 |
|
|
-- ** solutions for Xilinx devices, with no obligation on the **
|
23 |
|
|
-- ** part of Xilinx to provide support. By providing this design, **
|
24 |
|
|
-- ** code, or information as one possible implementation of **
|
25 |
|
|
-- ** this feature, application or standard, Xilinx is making no **
|
26 |
|
|
-- ** representation that this implementation is free from any **
|
27 |
|
|
-- ** claims of infringement. You are responsible for obtaining **
|
28 |
|
|
-- ** any rights you may require for your implementation. **
|
29 |
|
|
-- ** Xilinx expressly disclaims any warranty whatsoever with **
|
30 |
|
|
-- ** respect to the adequacy of the implementation, including **
|
31 |
|
|
-- ** but not limited to any warranties or representations that this **
|
32 |
|
|
-- ** implementation is free from claims of infringement, implied **
|
33 |
|
|
-- ** warranties of merchantability or fitness for a particular **
|
34 |
|
|
-- ** purpose. **
|
35 |
|
|
-- ** **
|
36 |
|
|
-- ** Xilinx products are not intended for use in life support **
|
37 |
|
|
-- ** appliances, devices, or systems. Use in such applications is **
|
38 |
|
|
-- ** expressly prohibited. **
|
39 |
|
|
-- ** **
|
40 |
|
|
-- ** Any modifications that are made to the Source Code are **
|
41 |
|
|
-- ** done at the user’s sole risk and will be unsupported. **
|
42 |
|
|
-- ** The Xilinx Support Hotline does not have access to source **
|
43 |
|
|
-- ** code and therefore cannot answer specific questions related **
|
44 |
|
|
-- ** to source HDL. The Xilinx Hotline support of original source **
|
45 |
|
|
-- ** code IP shall only address issues and questions related **
|
46 |
|
|
-- ** to the standard Netlist version of the core (and thus **
|
47 |
|
|
-- ** indirectly, the original core source). **
|
48 |
|
|
-- ** **
|
49 |
|
|
-- ** Copyright (c) 2001-2010 Xilinx, Inc. All rights reserved. **
|
50 |
|
|
-- ** **
|
51 |
|
|
-- ** This copyright and support notice must be retained as part **
|
52 |
|
|
-- ** of this text at all times. **
|
53 |
|
|
-- ** **
|
54 |
|
|
-- *************************************************************************
|
55 |
|
|
--
|
56 |
|
|
|
57 |
|
|
-------------------------------------------------------------------------------
|
58 |
|
|
-- Filename: lib_pkg.vhd
|
59 |
|
|
--
|
60 |
|
|
-------------------------------------------------------------------------------
|
61 |
|
|
-- Naming Conventions:
|
62 |
|
|
-- active low signals: "*_n"
|
63 |
|
|
-- clock signals: "clk", "clk_div#", "clk_#x"
|
64 |
|
|
-- reset signals: "rst", "rst_n"
|
65 |
|
|
-- generics: "C_*"
|
66 |
|
|
-- user defined types: "*_TYPE"
|
67 |
|
|
-- state machine next state: "*_ns"
|
68 |
|
|
-- state machine current state: "*_cs"
|
69 |
|
|
-- combinatorial signals: "*_com"
|
70 |
|
|
-- pipelined or register delay signals: "*_d#"
|
71 |
|
|
-- counter signals: "*cnt*"
|
72 |
|
|
-- clock enable signals: "*_ce"
|
73 |
|
|
-- internal version of output port "*_i"
|
74 |
|
|
-- device pins: "*_pin"
|
75 |
|
|
-- ports: - Names begin with Uppercase
|
76 |
|
|
-- processes: "*_PROCESS"
|
77 |
|
|
-- component instantiations: "<ENTITY_>I_<#|FUNC>
|
78 |
|
|
-------------------------------------------------------------------------------
|
79 |
|
|
library ieee;
|
80 |
|
|
use ieee.std_logic_1164.all;
|
81 |
|
|
-- need conversion function to convert reals/integers to std logic vectors
|
82 |
|
|
use ieee.std_logic_arith.conv_std_logic_vector;
|
83 |
|
|
use ieee.std_logic_arith.all;
|
84 |
|
|
use ieee.std_logic_unsigned.all;
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
package lib_pkg is
|
88 |
|
|
|
89 |
|
|
-------------------------------------------------------------------------------
|
90 |
|
|
-- Type Declarations
|
91 |
|
|
-------------------------------------------------------------------------------
|
92 |
|
|
type CHAR_TO_INT_TYPE is array (character) of integer;
|
93 |
|
|
-- type INTEGER_ARRAY_TYPE is array (natural range <>) of integer;
|
94 |
|
|
-- Type SLV64_ARRAY_TYPE is array (natural range <>) of std_logic_vector(0 to 63);
|
95 |
|
|
|
96 |
|
|
-------------------------------------------------------------------------------
|
97 |
|
|
-- Function and Procedure Declarations
|
98 |
|
|
-------------------------------------------------------------------------------
|
99 |
|
|
function max2 (num1, num2 : integer) return integer;
|
100 |
|
|
function min2 (num1, num2 : integer) return integer;
|
101 |
|
|
function Addr_Bits(x,y : std_logic_vector) return integer;
|
102 |
|
|
function clog2(x : positive) return natural;
|
103 |
|
|
function pad_power2 ( in_num : integer ) return integer;
|
104 |
|
|
function pad_4 ( in_num : integer ) return integer;
|
105 |
|
|
function log2(x : natural) return integer;
|
106 |
|
|
function pwr(x: integer; y: integer) return integer;
|
107 |
|
|
function String_To_Int(S : string) return integer;
|
108 |
|
|
function itoa (int : integer) return string;
|
109 |
|
|
-------------------------------------------------------------------------------
|
110 |
|
|
-- Constant Declarations
|
111 |
|
|
-------------------------------------------------------------------------------
|
112 |
|
|
-- the RESET_ACTIVE constant should denote the logic level of an active reset
|
113 |
|
|
constant RESET_ACTIVE : std_logic := '1';
|
114 |
|
|
|
115 |
|
|
-- table containing strings representing hex characters for conversion to
|
116 |
|
|
-- integers
|
117 |
|
|
constant STRHEX_TO_INT_TABLE : CHAR_TO_INT_TYPE :=
|
118 |
|
|
('0' => 0,
|
119 |
|
|
'1' => 1,
|
120 |
|
|
'2' => 2,
|
121 |
|
|
'3' => 3,
|
122 |
|
|
'4' => 4,
|
123 |
|
|
'5' => 5,
|
124 |
|
|
'6' => 6,
|
125 |
|
|
'7' => 7,
|
126 |
|
|
'8' => 8,
|
127 |
|
|
'9' => 9,
|
128 |
|
|
'A'|'a' => 10,
|
129 |
|
|
'B'|'b' => 11,
|
130 |
|
|
'C'|'c' => 12,
|
131 |
|
|
'D'|'d' => 13,
|
132 |
|
|
'E'|'e' => 14,
|
133 |
|
|
'F'|'f' => 15,
|
134 |
|
|
others => -1);
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
end lib_pkg;
|
138 |
|
|
|
139 |
|
|
package body lib_pkg is
|
140 |
|
|
-------------------------------------------------------------------------------
|
141 |
|
|
-- Function Definitions
|
142 |
|
|
-------------------------------------------------------------------------------
|
143 |
|
|
-------------------------------------------------------------------------------
|
144 |
|
|
-- Function max2
|
145 |
|
|
--
|
146 |
|
|
-- This function returns the greater of two numbers.
|
147 |
|
|
-------------------------------------------------------------------------------
|
148 |
|
|
function max2 (num1, num2 : integer) return integer is
|
149 |
|
|
begin
|
150 |
|
|
if num1 >= num2 then
|
151 |
|
|
return num1;
|
152 |
|
|
else
|
153 |
|
|
return num2;
|
154 |
|
|
end if;
|
155 |
|
|
end function max2;
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
-------------------------------------------------------------------------------
|
159 |
|
|
-- Function min2
|
160 |
|
|
--
|
161 |
|
|
-- This function returns the lesser of two numbers.
|
162 |
|
|
-------------------------------------------------------------------------------
|
163 |
|
|
function min2 (num1, num2 : integer) return integer is
|
164 |
|
|
begin
|
165 |
|
|
if num1 <= num2 then
|
166 |
|
|
return num1;
|
167 |
|
|
else
|
168 |
|
|
return num2;
|
169 |
|
|
end if;
|
170 |
|
|
end function min2;
|
171 |
|
|
|
172 |
|
|
|
173 |
|
|
-------------------------------------------------------------------------------
|
174 |
|
|
-- Function Addr_bits
|
175 |
|
|
--
|
176 |
|
|
-- function to convert an address range (base address and an upper address)
|
177 |
|
|
-- into the number of upper address bits needed for decoding a device
|
178 |
|
|
-- select signal. will handle slices and big or little endian
|
179 |
|
|
-------------------------------------------------------------------------------
|
180 |
|
|
function Addr_Bits(x,y : std_logic_vector) return integer is
|
181 |
|
|
variable addr_xor : std_logic_vector(x'range);
|
182 |
|
|
variable count : integer := 0;
|
183 |
|
|
begin
|
184 |
|
|
assert x'length = y'length and (x'ascending xnor y'ascending)
|
185 |
|
|
report "Addr_Bits: arguments are not the same type"
|
186 |
|
|
severity ERROR;
|
187 |
|
|
addr_xor := x xor y;
|
188 |
|
|
for i in x'range
|
189 |
|
|
loop
|
190 |
|
|
if addr_xor(i) = '1' then return count;
|
191 |
|
|
end if;
|
192 |
|
|
count := count + 1;
|
193 |
|
|
end loop;
|
194 |
|
|
return x'length;
|
195 |
|
|
end Addr_Bits;
|
196 |
|
|
|
197 |
|
|
|
198 |
|
|
--------------------------------------------------------------------------------
|
199 |
|
|
-- Function clog2 - returns the integer ceiling of the base 2 logarithm of x,
|
200 |
|
|
-- i.e., the least integer greater than or equal to log2(x).
|
201 |
|
|
--------------------------------------------------------------------------------
|
202 |
|
|
function clog2(x : positive) return natural is
|
203 |
|
|
variable r : natural := 0;
|
204 |
|
|
variable rp : natural := 1; -- rp tracks the value 2**r
|
205 |
|
|
begin
|
206 |
|
|
while rp < x loop -- Termination condition T: x <= 2**r
|
207 |
|
|
-- Loop invariant L: 2**(r-1) < x
|
208 |
|
|
r := r + 1;
|
209 |
|
|
if rp > integer'high - rp then exit; end if; -- If doubling rp overflows
|
210 |
|
|
-- the integer range, the doubled value would exceed x, so safe to exit.
|
211 |
|
|
rp := rp + rp;
|
212 |
|
|
end loop;
|
213 |
|
|
-- L and T <-> 2**(r-1) < x <= 2**r <-> (r-1) < log2(x) <= r
|
214 |
|
|
return r; --
|
215 |
|
|
end clog2;
|
216 |
|
|
|
217 |
|
|
-------------------------------------------------------------------------------
|
218 |
|
|
-- Function pad_power2
|
219 |
|
|
--
|
220 |
|
|
-- This function returns the next power of 2 from the input number. If the
|
221 |
|
|
-- input number is a power of 2, this function returns the input number.
|
222 |
|
|
--
|
223 |
|
|
-- This function is used to round up the number of masters to the next power
|
224 |
|
|
-- of 2 if the number of masters is not already a power of 2.
|
225 |
|
|
--
|
226 |
|
|
-- Input argument 0, which is not a power of two, is accepted and returns 0.
|
227 |
|
|
-- Input arguments less than 0 are not allowed.
|
228 |
|
|
-------------------------------------------------------------------------------
|
229 |
|
|
--
|
230 |
|
|
function pad_power2 (in_num : integer ) return integer is
|
231 |
|
|
begin
|
232 |
|
|
if in_num = 0 then
|
233 |
|
|
return 0;
|
234 |
|
|
else
|
235 |
|
|
return 2**(clog2(in_num));
|
236 |
|
|
end if;
|
237 |
|
|
end pad_power2;
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
-------------------------------------------------------------------------------
|
243 |
|
|
-- Function pad_4
|
244 |
|
|
--
|
245 |
|
|
-- This function returns the next multiple of 4 from the input number. If the
|
246 |
|
|
-- input number is a multiple of 4, this function returns the input number.
|
247 |
|
|
--
|
248 |
|
|
-------------------------------------------------------------------------------
|
249 |
|
|
--
|
250 |
|
|
function pad_4 (in_num : integer ) return integer is
|
251 |
|
|
|
252 |
|
|
variable out_num : integer;
|
253 |
|
|
|
254 |
|
|
begin
|
255 |
|
|
out_num := (((in_num-1)/4) + 1)*4;
|
256 |
|
|
return out_num;
|
257 |
|
|
|
258 |
|
|
end pad_4;
|
259 |
|
|
|
260 |
|
|
-------------------------------------------------------------------------------
|
261 |
|
|
-- Function log2 -- returns number of bits needed to encode x choices
|
262 |
|
|
-- x = 0 returns 0
|
263 |
|
|
-- x = 1 returns 0
|
264 |
|
|
-- x = 2 returns 1
|
265 |
|
|
-- x = 4 returns 2, etc.
|
266 |
|
|
-------------------------------------------------------------------------------
|
267 |
|
|
--
|
268 |
|
|
function log2(x : natural) return integer is
|
269 |
|
|
variable i : integer := 0;
|
270 |
|
|
variable val: integer := 1;
|
271 |
|
|
begin
|
272 |
|
|
if x = 0 then return 0;
|
273 |
|
|
else
|
274 |
|
|
for j in 0 to 29 loop -- for loop for XST
|
275 |
|
|
if val >= x then null;
|
276 |
|
|
else
|
277 |
|
|
i := i+1;
|
278 |
|
|
val := val*2;
|
279 |
|
|
end if;
|
280 |
|
|
end loop;
|
281 |
|
|
-- Fix per CR520627 XST was ignoring this anyway and printing a
|
282 |
|
|
-- Warning in SRP file. This will get rid of the warning and not
|
283 |
|
|
-- impact simulation.
|
284 |
|
|
-- synthesis translate_off
|
285 |
|
|
assert val >= x
|
286 |
|
|
report "Function log2 received argument larger" &
|
287 |
|
|
" than its capability of 2^30. "
|
288 |
|
|
severity failure;
|
289 |
|
|
-- synthesis translate_on
|
290 |
|
|
return i;
|
291 |
|
|
end if;
|
292 |
|
|
end function log2;
|
293 |
|
|
|
294 |
|
|
|
295 |
|
|
-------------------------------------------------------------------------------
|
296 |
|
|
-- Function pwr -- x**y
|
297 |
|
|
-- negative numbers not allowed for y
|
298 |
|
|
-------------------------------------------------------------------------------
|
299 |
|
|
|
300 |
|
|
function pwr(x: integer; y: integer) return integer is
|
301 |
|
|
variable z : integer := 1;
|
302 |
|
|
begin
|
303 |
|
|
if y = 0 then return 1;
|
304 |
|
|
else
|
305 |
|
|
for i in 1 to y loop
|
306 |
|
|
z := z * x;
|
307 |
|
|
end loop;
|
308 |
|
|
return z;
|
309 |
|
|
end if;
|
310 |
|
|
end function pwr;
|
311 |
|
|
|
312 |
|
|
-------------------------------------------------------------------------------
|
313 |
|
|
-- Function itoa
|
314 |
|
|
--
|
315 |
|
|
-- The itoa function converts an integer to a text string.
|
316 |
|
|
-- This function is required since `image doesn't work in Synplicity
|
317 |
|
|
-- Valid input range is -9999 to 9999
|
318 |
|
|
-------------------------------------------------------------------------------
|
319 |
|
|
--
|
320 |
|
|
function itoa (int : integer) return string is
|
321 |
|
|
type table is array (0 to 9) of string (1 to 1);
|
322 |
|
|
constant LUT : table :=
|
323 |
|
|
("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
|
324 |
|
|
variable str1 : string(1 to 1);
|
325 |
|
|
variable str2 : string(1 to 2);
|
326 |
|
|
variable str3 : string(1 to 3);
|
327 |
|
|
variable str4 : string(1 to 4);
|
328 |
|
|
variable str5 : string(1 to 5);
|
329 |
|
|
variable abs_int : natural;
|
330 |
|
|
|
331 |
|
|
variable thousands_place : natural;
|
332 |
|
|
variable hundreds_place : natural;
|
333 |
|
|
variable tens_place : natural;
|
334 |
|
|
variable ones_place : natural;
|
335 |
|
|
variable sign : integer;
|
336 |
|
|
|
337 |
|
|
begin
|
338 |
|
|
abs_int := abs(int);
|
339 |
|
|
if abs_int > int then sign := -1;
|
340 |
|
|
else sign := 1;
|
341 |
|
|
end if;
|
342 |
|
|
thousands_place := abs_int/1000;
|
343 |
|
|
hundreds_place := (abs_int-thousands_place*1000)/100;
|
344 |
|
|
tens_place := (abs_int-thousands_place*1000-hundreds_place*100)/10;
|
345 |
|
|
ones_place :=
|
346 |
|
|
(abs_int-thousands_place*1000-hundreds_place*100-tens_place*10);
|
347 |
|
|
|
348 |
|
|
if sign>0 then
|
349 |
|
|
if thousands_place>0 then
|
350 |
|
|
str4 := LUT(thousands_place) & LUT(hundreds_place) & LUT(tens_place) &
|
351 |
|
|
LUT(ones_place);
|
352 |
|
|
return str4;
|
353 |
|
|
elsif hundreds_place>0 then
|
354 |
|
|
str3 := LUT(hundreds_place) & LUT(tens_place) & LUT(ones_place);
|
355 |
|
|
return str3;
|
356 |
|
|
elsif tens_place>0 then
|
357 |
|
|
str2 := LUT(tens_place) & LUT(ones_place);
|
358 |
|
|
return str2;
|
359 |
|
|
else
|
360 |
|
|
str1 := LUT(ones_place);
|
361 |
|
|
return str1;
|
362 |
|
|
end if;
|
363 |
|
|
else
|
364 |
|
|
if thousands_place>0 then
|
365 |
|
|
str5 := "-" & LUT(thousands_place) & LUT(hundreds_place) &
|
366 |
|
|
LUT(tens_place) & LUT(ones_place);
|
367 |
|
|
return str5;
|
368 |
|
|
elsif hundreds_place>0 then
|
369 |
|
|
str4 := "-" & LUT(hundreds_place) & LUT(tens_place) & LUT(ones_place);
|
370 |
|
|
return str4;
|
371 |
|
|
elsif tens_place>0 then
|
372 |
|
|
str3 := "-" & LUT(tens_place) & LUT(ones_place);
|
373 |
|
|
return str3;
|
374 |
|
|
else
|
375 |
|
|
str2 := "-" & LUT(ones_place);
|
376 |
|
|
return str2;
|
377 |
|
|
end if;
|
378 |
|
|
end if;
|
379 |
|
|
end itoa;
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
|
383 |
|
|
-----------------------------------------------------------------------------
|
384 |
|
|
-- Function String_To_Int
|
385 |
|
|
--
|
386 |
|
|
-- Converts a string of hex character to an integer
|
387 |
|
|
-- accept negative numbers
|
388 |
|
|
-----------------------------------------------------------------------------
|
389 |
|
|
function String_To_Int(S : String) return Integer is
|
390 |
|
|
variable Result : integer := 0;
|
391 |
|
|
variable Temp : integer := S'Left;
|
392 |
|
|
variable Negative : integer := 1;
|
393 |
|
|
begin
|
394 |
|
|
for I in S'Left to S'Right loop
|
395 |
|
|
if (S(I) = '-') then
|
396 |
|
|
Temp := 0;
|
397 |
|
|
Negative := -1;
|
398 |
|
|
else
|
399 |
|
|
Temp := STRHEX_TO_INT_TABLE(S(I));
|
400 |
|
|
if (Temp = -1) then
|
401 |
|
|
assert false
|
402 |
|
|
report "Wrong value in String_To_Int conversion " & S(I)
|
403 |
|
|
severity error;
|
404 |
|
|
end if;
|
405 |
|
|
end if;
|
406 |
|
|
Result := Result * 16 + Temp;
|
407 |
|
|
end loop;
|
408 |
|
|
return (Negative * Result);
|
409 |
|
|
end String_To_Int;
|
410 |
|
|
|
411 |
|
|
end package body lib_pkg;
|
412 |
|
|
|
413 |
|
|
|