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

Subversion Repositories rv01_riscv_core

[/] [rv01_riscv_core/] [trunk/] [VHDL/] [RV01_funcs_pkg.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 madsilicon
-----------------------------------------------------------------
2
--                                                             --
3
-----------------------------------------------------------------
4
--                                                             --
5
-- Copyright (C) 2015 Stefano Tonello                          --
6
--                                                             --
7
-- This source file may be used and distributed without        --
8
-- restriction provided that this copyright statement is not   --
9
-- removed from the file and that any derivative work contains --
10
-- the original copyright notice and the associated disclaimer.--
11
--                                                             --
12
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY         --
13
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   --
14
-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   --
15
-- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      --
16
-- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         --
17
-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    --
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   --
19
-- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        --
20
-- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  --
21
-- LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  --
22
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  --
23
-- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         --
24
-- POSSIBILITY OF SUCH DAMAGE.                                 --
25
--                                                             --
26
-----------------------------------------------------------------
27
 
28
---------------------------------------------------------------
29
-- RV01 functions package
30
---------------------------------------------------------------
31
 
32
library IEEE;
33
use IEEE.std_logic_1164.all;
34
use IEEE.numeric_std.all;
35
 
36
library WORK;
37
use WORK.RV01_CONSTS_PKG.all;
38
use WORK.RV01_TYPES_PKG.all;
39
 
40
package RV01_FUNCS_PKG is
41
 
42
  -- log2()
43
  function log2(VAL : integer range 1 to 2**20-1) return integer;
44
 
45
  ----------------------------------------------------
46
  -- Convert hex string
47
  ----------------------------------------------------
48
 
49
  -- convert hex string to natural
50
  function hex_to_natural(XSTR : string) return natural;
51
 
52
  -- convert hex string to std_logic_vector
53
  function hex_to_std_logic(XSTR : string) return std_logic_vector;
54
 
55
  -- convert hex string to unsigned
56
  function hex_to_unsigned(XSTR : string) return unsigned;
57
 
58
  ----------------------------------------------------
59
  -- Convert bit vector
60
  ----------------------------------------------------
61
 
62
  -- convert unsigned to std_logic_vector
63
  function to_std_logic_vector(U : unsigned) return std_logic_vector;
64
 
65
  -- convert signed to std_logic_vector
66
  function to_std_logic_vector(S : signed) return std_logic_vector;
67
 
68
  -- convert std_logic_vector to unsigned
69
  function to_unsigned(V : std_logic_vector) return unsigned;
70
 
71
  -- convert signed to unsigned
72
  function to_unsigned(S : signed) return unsigned;
73
 
74
  -- convert std_logic_vector to signed
75
  function to_signed(V : std_logic_vector) return signed;
76
 
77
  -- convert unsigned to signed
78
  function to_signed(U : unsigned) return signed;
79
 
80
  ----------------------------------------------------
81
  -- C/verilog-style ternary operator
82
  ----------------------------------------------------
83
 
84
  function qmark(C : std_logic; A,B : std_logic) return std_logic;
85
 
86
  function qmark(C : boolean; A,B : std_logic) return std_logic;
87
 
88
end package;
89
 
90
package body RV01_FUNCS_PKG is
91
 
92
  function log2(VAL : integer range 1 to 2**20-1) return integer is
93
    variable LOG2 : integer range 0 to 20 := 0;
94
  begin
95
    while (VAL > 2**LOG2) loop
96
      LOG2 := LOG2 + 1;
97
    end loop;
98
    return(LOG2);
99
  end function;
100
 
101
  ----------------------------------------------------
102
  -- Convert hex string
103
  ----------------------------------------------------
104
 
105
  function hex_to_natural(XSTR : string) return natural is
106
    variable VAL : natural := 0;
107
    variable DGT : natural range 0 to 15;
108
  begin
109
    for i in 1 to XSTR'length loop
110
      case XSTR(i) is
111
        when '0' => DGT := 0;
112
        when '1' => DGT := 1;
113
        when '2' => DGT := 2;
114
        when '3' => DGT := 3;
115
        when '4' => DGT := 4;
116
        when '5' => DGT := 5;
117
        when '6' => DGT := 6;
118
        when '7' => DGT := 7;
119
        when '8' => DGT := 8;
120
        when '9' => DGT := 9;
121
        when 'a' => DGT := 10;
122
        when 'b' => DGT := 11;
123
        when 'c' => DGT := 12;
124
        when 'd' => DGT := 13;
125
        when 'e' => DGT := 14;
126
        when others => DGT := 15;
127
      end case;
128
      VAL := VAL + DGT*(16**(XSTR'length-i));
129
    end loop;
130
    return(VAL);
131
  end function;
132
 
133
  function hex_to_std_logic(XSTR : string) return std_logic_vector is
134
    variable VEC : unsigned(XSTR'length*4-1 downto 0);
135
    variable DGT : unsigned(4-1 downto 0);
136
  begin
137
    for i in 1 to XSTR'length loop
138
      case XSTR(i) is
139
        when '0' => DGT := "0000";
140
        when '1' => DGT := "0001";
141
        when '2' => DGT := "0010";
142
        when '3' => DGT := "0011";
143
        when '4' => DGT := "0100";
144
        when '5' => DGT := "0101";
145
        when '6' => DGT := "0110";
146
        when '7' => DGT := "0111";
147
        when '8' => DGT := "1000";
148
        when '9' => DGT := "1001";
149
        when 'a' => DGT := "1010";
150
        when 'b' => DGT := "1011";
151
        when 'c' => DGT := "1100";
152
        when 'd' => DGT := "1101";
153
        when 'e' => DGT := "1110";
154
        when others => DGT := "1111";
155
      end case;
156
      VEC(XSTR'length*4-1 downto 4) := VEC(XSTR'length*4-1-4 downto 0);
157
      VEC(4-1 downto 0) := DGT;
158
    end loop;
159
    return(to_std_logic_vector(VEC));
160
  end function;
161
 
162
  function hex_to_unsigned(XSTR : string) return unsigned is
163
    variable VEC : unsigned(XSTR'length*4-1 downto 0);
164
    variable DGT : unsigned(4-1 downto 0);
165
  begin
166
    for i in 1 to XSTR'length loop
167
      case XSTR(i) is
168
        when '0' => DGT := "0000";
169
        when '1' => DGT := "0001";
170
        when '2' => DGT := "0010";
171
        when '3' => DGT := "0011";
172
        when '4' => DGT := "0100";
173
        when '5' => DGT := "0101";
174
        when '6' => DGT := "0110";
175
        when '7' => DGT := "0111";
176
        when '8' => DGT := "1000";
177
        when '9' => DGT := "1001";
178
        when 'a' => DGT := "1010";
179
        when 'b' => DGT := "1011";
180
        when 'c' => DGT := "1100";
181
        when 'd' => DGT := "1101";
182
        when 'e' => DGT := "1110";
183
        when others => DGT := "1111";
184
      end case;
185
      VEC(XSTR'length*4-1 downto 4) := VEC(XSTR'length*4-1-4 downto 0);
186
      VEC(4-1 downto 0) := DGT;
187
    end loop;
188
    return(VEC);
189
  end function;
190
 
191
  ----------------------------------------------------
192
  -- Convert bit vector
193
  ----------------------------------------------------
194
 
195
  function to_std_logic_vector(U : unsigned) return std_logic_vector is
196
    variable V : std_logic_vector(U'high downto U'low);
197
  begin
198
    for i in U'low to U'high loop
199
      V(i) := U(i);
200
    end loop;
201
    return(V);
202
  end function;
203
 
204
  function to_std_logic_vector(S : signed) return std_logic_vector is
205
     variable V : std_logic_vector(S'high downto S'low);
206
  begin
207
    for i in S'low to S'high loop
208
      V(i) := S(i);
209
    end loop;
210
    return(V);
211
  end function;
212
 
213
  function to_unsigned(V : std_logic_vector) return unsigned is
214
    variable U : unsigned(V'high downto V'low);
215
  begin
216
    for i in V'low to V'high loop
217
      U(i) := V(i);
218
    end loop;
219
    return(U);
220
  end function;
221
 
222
  function to_unsigned(S : signed) return unsigned is
223
    variable U : unsigned(S'high downto S'low);
224
  begin
225
    for i in S'low to S'high loop
226
      U(i) := S(i);
227
    end loop;
228
    return(U);
229
  end function;
230
 
231
  function to_signed(V : std_logic_vector) return signed is
232
    variable S : signed(V'high downto V'low);
233
  begin
234
    for i in V'low to V'high loop
235
      S(i) := V(i);
236
    end loop;
237
    return(S);
238
  end function;
239
 
240
  function to_signed(U : unsigned) return signed is
241
    variable S : signed(U'high downto U'low);
242
  begin
243
    for i in U'low to U'high loop
244
      S(i) := U(i);
245
    end loop;
246
    return(S);
247
  end function;
248
 
249
  ----------------------------------------------------
250
  -- C/verilog-style ternary operator
251
  ----------------------------------------------------
252
 
253
  function qmark(C : std_logic; A,B : std_logic) return std_logic is
254
  begin
255
    if(C = '1') then
256
      return(A);
257
    else
258
      return(B);
259
    end if;
260
  end function;
261
 
262
  function qmark(C : boolean; A,B : std_logic) return std_logic is
263
  begin
264
    if(C) then
265
      return(A);
266
    else
267
      return(B);
268
    end if;
269
  end function;
270
 
271
end package body;

powered by: WebSVN 2.1.0

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