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

Subversion Repositories vhdl_wb_tb

[/] [vhdl_wb_tb/] [trunk/] [rtl/] [vhdl/] [packages/] [convert_pkg.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sinx
---------------------------------------------------------------------- 
2
----                                                              ---- 
3
----  VHDL Wishbone TESTBENCH                                     ---- 
4
----                                                              ---- 
5
----  This is a universal type conversion library for VHDL. With  ----
6
----  the contained overloaded functions conversions from any to  ----
7
----  any of the following data types are possible:               ----
8
----                                                              ----
9
----    std_logic_vector                                          ----
10
----    std_ulogic_vector                                         ----
11
----    unsigned                                                  ----
12
----    signed                                                    ----
13
----    bit_vector                                                ----
14
----    integer                                                   ----
15
----    string                                                    ----
16
----                                                              ----
17
----  To use them just add the prefix "to_" to the desired result ----
18
----  type with the source type in braces.                        ----
19
----  E.g. conversion from integer to std_logic_vector:           ----
20
----    destination<=to_std_logic_vector(source);                 ----
21
----                                                              ---- 
22
----  This file is part of the vhdl_wb_tb project                 ---- 
23
----  http://www.opencores.org/cores/vhdl_wb_tb/                  ---- 
24
----                                                              ---- 
25
----  To Do:                                                      ---- 
26
----    -                                                         ---- 
27
----                                                              ---- 
28
----  Author(s):                                                  ---- 
29
----      - First & Last Name, email@opencores.org                ---- 
30
----                                                              ---- 
31
----------------------------------------------------------------------
32
--    SVN information
33
--
34
--      $URL:  $
35
-- $Revision:  $
36
--     $Date:  $
37
--   $Author:  $
38
--       $Id:  $
39
--
40
---------------------------------------------------------------------- 
41
----                                                              ---- 
42
---- Copyright (C) 2018 Authors and OPENCORES.ORG                 ---- 
43
----                                                              ---- 
44
---- This source file may be used and distributed without         ---- 
45
---- restriction provided that this copyright statement is not    ---- 
46
---- removed from the file and that any derivative work contains  ---- 
47
---- the original copyright notice and the associated disclaimer. ---- 
48
----                                                              ---- 
49
---- This source file is free software; you can redistribute it   ---- 
50
---- and/or modify it under the terms of the GNU Lesser General   ---- 
51
---- Public License as published by the Free Software Foundation; ---- 
52
---- either version 2.1 of the License, or (at your option) any   ---- 
53
---- later version.                                               ---- 
54
----                                                              ---- 
55
---- This source is distributed in the hope that it will be       ---- 
56
---- useful, but WITHOUT ANY WARRANTY; without even the implied   ---- 
57
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ---- 
58
---- PURPOSE.  See the GNU Lesser General Public License for more ---- 
59
---- details.                                                     ---- 
60
----                                                              ---- 
61
---- You should have received a copy of the GNU Lesser General    ---- 
62
---- Public License along with this source; if not, download it   ---- 
63
---- from http://www.opencores.org/lgpl.shtml                     ---- 
64
----                                                              ---- 
65
----------------------------------------------------------------------
66
--============================================================================
67
--============================================================================
68
LIBRARY ieee;
69
USE ieee.std_logic_1164.ALL;
70
USE ieee.std_logic_arith.ALL;
71
--============================================================================
72
 
73
 
74
--============================================================================
75
PACKAGE convert_pkg IS
76
 
77
  FUNCTION to_std_logic_vector(input : integer; length : integer) RETURN std_logic_vector;
78
 
79
  FUNCTION to_integer(input : std_logic_vector) RETURN integer;
80
  FUNCTION to_string(int             : integer; base : integer := 10; length : integer := 0) RETURN string;
81
  FUNCTION to_string(slv             : std_logic_vector; base : integer; length : integer) RETURN string;
82
 
83
END convert_pkg;
84
--============================================================================
85
 
86
--============================================================================
87
PACKAGE BODY convert_pkg IS
88
  --==========================================================================
89
  FUNCTION to_std_logic_vector(input : integer; length : integer) RETURN std_logic_vector IS
90
  BEGIN
91
    RETURN std_logic_vector(conv_unsigned(input, length));
92
  END;
93
 
94
  FUNCTION to_integer(input : std_logic_vector) RETURN integer IS
95
  BEGIN
96
    RETURN conv_integer(unsigned(input));
97
  END;
98
 
99
  --==========================================================================
100
  FUNCTION to_char(int : integer) RETURN character IS
101
    VARIABLE c : character;
102
  BEGIN
103
    CASE int IS
104
      WHEN 0      => c := '0';
105
      WHEN 1      => c := '1';
106
      WHEN 2      => c := '2';
107
      WHEN 3      => c := '3';
108
      WHEN 4      => c := '4';
109
      WHEN 5      => c := '5';
110
      WHEN 6      => c := '6';
111
      WHEN 7      => c := '7';
112
      WHEN 8      => c := '8';
113
      WHEN 9      => c := '9';
114
      WHEN 10     => c := 'A';
115
      WHEN 11     => c := 'B';
116
      WHEN 12     => c := 'C';
117
      WHEN 13     => c := 'D';
118
      WHEN 14     => c := 'E';
119
      WHEN 15     => c := 'F';
120
      WHEN 16     => c := 'G';
121
      WHEN 17     => c := 'H';
122
      WHEN 18     => c := 'I';
123
      WHEN 19     => c := 'J';
124
      WHEN 20     => c := 'K';
125
      WHEN 21     => c := 'L';
126
      WHEN 22     => c := 'M';
127
      WHEN 23     => c := 'N';
128
      WHEN 24     => c := 'O';
129
      WHEN 25     => c := 'P';
130
      WHEN 26     => c := 'Q';
131
      WHEN 27     => c := 'R';
132
      WHEN 28     => c := 'S';
133
      WHEN 29     => c := 'T';
134
      WHEN 30     => c := 'U';
135
      WHEN 31     => c := 'V';
136
      WHEN 32     => c := 'W';
137
      WHEN 33     => c := 'X';
138
      WHEN 34     => c := 'Y';
139
      WHEN 35     => c := 'Z';
140
      WHEN OTHERS => c := '?';
141
    END CASE;
142
    RETURN c;
143
  END to_char;
144
  --========================================================================
145
  -- convert integer to string using specified base
146
  -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
147
  -- if base=0 convert to 32 bit hex
148
  FUNCTION to_string(int : integer; base : integer := 10; length : integer := 0) RETURN string IS
149
 
150
    VARIABLE temp    : string(1 TO 1000);
151
    VARIABLE num     : integer;
152
    VARIABLE abs_int : integer;
153
    VARIABLE len     : integer := 1;
154
    VARIABLE power   : integer := 1;
155
 
156
  BEGIN
157
    abs_int := ABS(int);
158
    num     := abs_int;
159
    --
160
    IF (length = 0) THEN                -- automatic length detection 
161
      WHILE num >= base LOOP            -- Determine how many
162
        len := len + 1;                 -- characters required
163
        num := num / base;              -- to represent the
164
      END LOOP;  -- number.
165
    ELSE
166
      len := ABS(length);               -- 
167
    END IF;
168
 
169
    IF (base /= 10) THEN
170
      len := len + (len-1) / 4;                       -- increase for underlines
171
    END IF;
172
    --
173
    FOR i IN len DOWNTO 1 LOOP                        -- Convert the number to
174
      IF (((len-i) MOD 5 = 4) AND (base /= 10)) THEN  -- every fith char shell be an underline
175
        temp(i) := '_';
176
      ELSE
177
        temp(i) := to_char(abs_int/power MOD base);   -- a string starting
178
        power   := power * base;                      -- with the right hand
179
      END IF;
180
    END LOOP;  -- side.
181
    --
182
    -- return result and add sign if required
183
    IF (base = 16) THEN
184
      IF (int < 0) THEN
185
        CASE temp(len) IS
186
          WHEN '0'    => temp(len) := 'F';
187
          WHEN '1'    => temp(len) := '0';
188
          WHEN '2'    => temp(len) := '1';
189
          WHEN '3'    => temp(len) := '2';
190
          WHEN '4'    => temp(len) := '3';
191
          WHEN '5'    => temp(len) := '4';
192
          WHEN '6'    => temp(len) := '5';
193
          WHEN '7'    => temp(len) := '6';
194
          WHEN '8'    => temp(len) := '7';
195
          WHEN '9'    => temp(len) := '8';
196
          WHEN 'A'    => temp(len) := '9';
197
          WHEN 'B'    => temp(len) := 'A';
198
          WHEN 'C'    => temp(len) := 'B';
199
          WHEN 'D'    => temp(len) := 'C';
200
          WHEN 'E'    => temp(len) := 'D';
201
          WHEN 'F'    => temp(len) := 'E';
202
          WHEN OTHERS => NULL;
203
        END CASE;
204
        FOR i IN len DOWNTO 1 LOOP
205
          CASE temp(i) IS
206
            WHEN '0'    => temp(i) := 'F';
207
            WHEN '1'    => temp(i) := 'E';
208
            WHEN '2'    => temp(i) := 'D';
209
            WHEN '3'    => temp(i) := 'C';
210
            WHEN '4'    => temp(i) := 'B';
211
            WHEN '5'    => temp(i) := 'A';
212
            WHEN '6'    => temp(i) := '9';
213
            WHEN '7'    => temp(i) := '8';
214
            WHEN '8'    => temp(i) := '7';
215
            WHEN '9'    => temp(i) := '6';
216
            WHEN 'A'    => temp(i) := '5';
217
            WHEN 'B'    => temp(i) := '4';
218
            WHEN 'C'    => temp(i) := '3';
219
            WHEN 'D'    => temp(i) := '2';
220
            WHEN 'E'    => temp(i) := '1';
221
            WHEN 'F'    => temp(i) := '0';
222
            WHEN OTHERS => NULL;
223
          END CASE;
224
        END LOOP;  -- i
225
      END IF;
226
      RETURN temp(1 TO len);
227
    ELSE
228
      IF (int < 0) THEN
229
        RETURN '-'& temp(1 TO len);
230
      ELSE
231
        RETURN temp(1 TO len);
232
      END IF;
233
    END IF;
234
  END to_string;
235
 
236
  --========================================================================
237
  FUNCTION to_string(slv : std_logic_vector) RETURN string IS
238
 
239
    VARIABLE hexlen  : integer;
240
    VARIABLE longslv : std_logic_vector(131 DOWNTO 0) := (OTHERS => '0');
241
    VARIABLE hex     : string(1 TO 32);
242
    VARIABLE fourbit : std_logic_vector(3 DOWNTO 0);
243
 
244
  BEGIN
245
    hexlen := ((slv'high - slv'low) + 1) / 4;
246
    IF (((slv'high - slv'low) + 1) MOD 4 /= 0) THEN
247
      hexlen := hexlen + 1;
248
    END IF;
249
    --
250
    longslv((slv'high - slv'low) DOWNTO 0) := slv;
251
    --
252
    FOR i IN (hexlen -1) DOWNTO 0 LOOP
253
      fourbit := longslv(((i*4)+3) DOWNTO (i*4));
254
      CASE fourbit IS
255
        WHEN "0000" => hex(hexlen -I) := '0';
256
        WHEN "0001" => hex(hexlen -I) := '1';
257
        WHEN "0010" => hex(hexlen -I) := '2';
258
        WHEN "0011" => hex(hexlen -I) := '3';
259
        WHEN "0100" => hex(hexlen -I) := '4';
260
        WHEN "0101" => hex(hexlen -I) := '5';
261
        WHEN "0110" => hex(hexlen -I) := '6';
262
        WHEN "0111" => hex(hexlen -I) := '7';
263
        WHEN "1000" => hex(hexlen -I) := '8';
264
        WHEN "1001" => hex(hexlen -I) := '9';
265
        WHEN "1010" => hex(hexlen -I) := 'A';
266
        WHEN "1011" => hex(hexlen -I) := 'B';
267
        WHEN "1100" => hex(hexlen -I) := 'C';
268
        WHEN "1101" => hex(hexlen -I) := 'D';
269
        WHEN "1110" => hex(hexlen -I) := 'E';
270
        WHEN "1111" => hex(hexlen -I) := 'F';
271
        WHEN "ZZZZ" => hex(hexlen -I) := 'z';
272
        WHEN "UUUU" => hex(hexlen -I) := 'u';
273
        WHEN "XXXX" => hex(hexlen -I) := 'x';
274
        WHEN OTHERS => hex(hexlen -I) := '?';
275
      END CASE;
276
    END LOOP;
277
    RETURN hex(1 TO hexlen);
278
  END to_string;
279
 
280
  --========================================================================
281
  FUNCTION to_string(slv : std_logic_vector; base : integer; length : integer) RETURN string IS
282
 
283
  BEGIN
284
    RETURN to_string(to_integer(slv), base, length);
285
  END to_string;
286
 
287
end package body;
288
----------------------------------------------------------------------
289
---- end of file                                                  ---- 
290
----------------------------------------------------------------------

powered by: WebSVN 2.1.0

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