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

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [rtl/] [commonlib/] [types_common.vhd] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sergeykhbr
-----------------------------------------------------------------------------
2
--! @file
3
--! @copyright Copyright 2015 GNSS Sensor Ltd. All right reserved.
4
--! @author    Sergey Khabarov - sergeykhbr@gmail.com
5
--! @brief     Declaration and implementation of the types_common package.
6
------------------------------------------------------------------------------
7
 
8
--! Standard library
9
library ieee;
10
use ieee.std_logic_1164.all;
11
use ieee.numeric_std.all;
12
use std.textio.all;
13
 
14
--! @brief   Definition of the generic VHDL methods and constants.
15
--! @details This package defines common mathematical methods and
16
--!          utility methods for the VHDL types conversions.
17
package types_common is
18
 
19
--! @brief Array declaration of the pre-computed log2 values.
20
type log2arr is array(0 to 512) of integer;
21
 
22
--! @brief Array definition of the pre-computed log2 values.
23
--! @details These values are used as an argument in bus width
24
--!          declaration. 
25
--!
26
--! Example usage:
27
--! @code 
28
--!   component foo_component is
29
--!   generic (
30
--!     max_clients  : integer := 8
31
--!   );
32
--!   port (
33
--!     foo : inout  std_logic_vector(log2(max_clients)-1 downto 0)
34
--!   );
35
--!   end component;
36
--! @endcode 
37
constant log2   : log2arr := (
38
0,0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
39
  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
40
  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
41
  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
42
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
43
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
44
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
45
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
46
  others => 9);
47
 
48
constant log2x  : log2arr := (
49
0,1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
50
  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
51
  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
52
  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
53
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
54
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
55
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
56
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
57
  others => 9);
58
 
59
constant zero32 : std_logic_vector(31 downto 0) := X"00000000";
60
constant zero64 : std_logic_vector(63 downto 0) := zero32 & zero32;
61
 
62
 
63
function "-" (i : integer; d : std_logic_vector) return std_logic_vector;
64
function "-" (d : std_logic_vector; i : integer) return std_logic_vector;
65
function "-" (a, b : std_logic_vector) return std_logic_vector;
66
function "+" (d : std_logic_vector; i : integer) return std_logic_vector;
67
function "+" (a, b : std_logic_vector) return std_logic_vector;
68
function "+" (d : std_logic_vector; i : std_ulogic) return std_logic_vector;
69
function "*" (a, b : std_logic_vector) return std_logic_vector;
70
 
71
function conv_integer(v : std_logic_vector) return integer;
72
function conv_integer(v : std_logic) return integer;
73
function conv_std_logic_vector(i : integer; w : integer) return std_logic_vector;
74
function conv_std_logic_vector_signed(i : integer; w : integer) return std_logic_vector;
75
function conv_std_logic(b : boolean) return std_ulogic;
76
 
77
end;
78
 
79
 
80
package body types_common is
81
 
82
 
83
function conv_integer(v : std_logic_vector) return integer is
84
begin
85
  if not is_x(v) then return(to_integer(unsigned(v)));
86
  else return(0); end if;
87
end;
88
 
89
function conv_integer(v : std_logic) return integer is
90
begin
91
  if not is_x(v) then
92
    if v = '1' then return(1);
93
    else return(0); end if;
94
  else return(0); end if;
95
end;
96
 
97
function conv_std_logic_vector(i : integer; w : integer) return std_logic_vector is
98
variable tmp : std_logic_vector(w-1 downto 0);
99
begin
100
  tmp := std_logic_vector(to_unsigned(i, w));
101
  return(tmp);
102
end;
103
 
104
function conv_std_logic_vector_signed(i : integer; w : integer) return std_logic_vector is
105
variable tmp : std_logic_vector(w-1 downto 0);
106
begin
107
  tmp := std_logic_vector(to_signed(i, w));
108
  return(tmp);
109
end;
110
 
111
function conv_std_logic(b : boolean) return std_ulogic is
112
begin
113
  if b then return('1'); else return('0'); end if;
114
end;
115
 
116
function "+" (d : std_logic_vector; i : integer) return std_logic_vector is
117
variable x : std_logic_vector(d'length-1 downto 0);
118
begin
119
-- pragma translate_off
120
  if not is_x(d) then
121
-- pragma translate_on
122
    return(std_logic_vector(unsigned(d) + i));
123
-- pragma translate_off
124
  else x := (others =>'X'); return(x);
125
  end if;
126
-- pragma translate_on
127
end;
128
 
129
function "+" (a, b : std_logic_vector) return std_logic_vector is
130
variable x : std_logic_vector(a'length-1 downto 0);
131
variable y : std_logic_vector(b'length-1 downto 0);
132
begin
133
-- pragma translate_off
134
  if not is_x(a&b) then
135
-- pragma translate_on
136
    return(std_logic_vector(unsigned(a) + unsigned(b)));
137
-- pragma translate_off
138
  else
139
     x := (others =>'X'); y := (others =>'X');
140
     if (x'length > y'length) then return(x); else return(y); end if;
141
  end if;
142
-- pragma translate_on
143
end;
144
 
145
function "+" (d : std_logic_vector; i : std_ulogic) return std_logic_vector is
146
variable x : std_logic_vector(d'length-1 downto 0);
147
variable y : std_logic_vector(0 downto 0);
148
begin
149
  y(0) := i;
150
-- pragma translate_off
151
  if not is_x(d) then
152
-- pragma translate_on
153
    return(std_logic_vector(unsigned(d) + unsigned(y)));
154
-- pragma translate_off
155
  else x := (others =>'X'); return(x);
156
  end if;
157
-- pragma translate_on
158
end;
159
 
160
 
161
function "-" (i : integer; d : std_logic_vector) return std_logic_vector is
162
variable x : std_logic_vector(d'length-1 downto 0);
163
begin
164
-- pragma translate_off
165
  if not is_x(d) then
166
-- pragma translate_on
167
    return(std_logic_vector(i - unsigned(d)));
168
-- pragma translate_off
169
  else x := (others =>'X'); return(x);
170
  end if;
171
-- pragma translate_on
172
end;
173
 
174
function "-" (d : std_logic_vector; i : integer) return std_logic_vector is
175
variable x : std_logic_vector(d'length-1 downto 0);
176
begin
177
-- pragma translate_off
178
  if not is_x(d) then
179
-- pragma translate_on
180
    return(std_logic_vector(unsigned(d) - i));
181
-- pragma translate_off
182
  else x := (others =>'X'); return(x);
183
  end if;
184
-- pragma translate_on
185
end;
186
 
187
function "-" (a, b : std_logic_vector) return std_logic_vector is
188
variable x : std_logic_vector(a'length-1 downto 0);
189
variable y : std_logic_vector(b'length-1 downto 0);
190
begin
191
-- pragma translate_off
192
  if not is_x(a&b) then
193
-- pragma translate_on
194
    return(std_logic_vector(unsigned(a) - unsigned(b)));
195
-- pragma translate_off
196
  else
197
     x := (others =>'X'); y := (others =>'X');
198
     if (x'length > y'length) then return(x); else return(y); end if;
199
  end if;
200
-- pragma translate_on
201
end;
202
 
203
function "*" (a, b : std_logic_vector) return std_logic_vector is
204
variable z : std_logic_vector(a'length+b'length-1 downto 0);
205
begin
206
-- pragma translate_off
207
  if not is_x(a&b) then
208
-- pragma translate_on
209
    return(std_logic_vector(unsigned(a) * unsigned(b)));
210
-- pragma translate_off
211
  else
212
     z := (others =>'X'); return(z);
213
  end if;
214
-- pragma translate_on
215
end;
216
 
217
 
218
end;
219
 
220
 

powered by: WebSVN 2.1.0

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