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 27

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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