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 21

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

Line No. Rev Author Line
1 21 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 21 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: 21 $
21
----     $Date: 2018-08-01 12:05:41 +0200 (Wed, 01 Aug 2018) $
22
----   $Author: sinx $
23
----       $Id: convert_pkg.vhd 21 2018-08-01 10:05:41Z sinx $
24 2 sinx
----------------------------------------------------------------------
25 21 sinx
----                                                              ----
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
----------------------------------------------------------------------
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 17 sinx
  FUNCTION to_string(int : integer; base : integer range 1 to 16 := 16; length : integer range 1 to 8:= 8) RETURN string;
62
  FUNCTION to_string(slv : std_logic_vector; base : integer range 1 to 16 := 16; length : integer range 1 to 8 := 8) RETURN string;
63 21 sinx
 
64 2 sinx
END convert_pkg;
65
 
66 4 sinx
-- package body ------------------------------------------------------
67 2 sinx
PACKAGE BODY convert_pkg IS
68 4 sinx
  ----------------------------------------------------------------------
69 2 sinx
  FUNCTION to_std_logic_vector(input : integer; length : integer) RETURN std_logic_vector IS
70
  BEGIN
71
    RETURN std_logic_vector(conv_unsigned(input, length));
72
  END;
73 4 sinx
  ----------------------------------------------------------------------
74 2 sinx
  FUNCTION to_integer(input : std_logic_vector) RETURN integer IS
75
  BEGIN
76
    RETURN conv_integer(unsigned(input));
77
  END;
78 4 sinx
  ----------------------------------------------------------------------
79 2 sinx
  FUNCTION to_char(int : integer) RETURN character IS
80
    VARIABLE c : character;
81
  BEGIN
82
    CASE int IS
83
      WHEN 0      => c := '0';
84
      WHEN 1      => c := '1';
85
      WHEN 2      => c := '2';
86
      WHEN 3      => c := '3';
87
      WHEN 4      => c := '4';
88
      WHEN 5      => c := '5';
89
      WHEN 6      => c := '6';
90
      WHEN 7      => c := '7';
91
      WHEN 8      => c := '8';
92
      WHEN 9      => c := '9';
93
      WHEN 10     => c := 'A';
94
      WHEN 11     => c := 'B';
95
      WHEN 12     => c := 'C';
96
      WHEN 13     => c := 'D';
97
      WHEN 14     => c := 'E';
98
      WHEN 15     => c := 'F';
99
      WHEN 16     => c := 'G';
100
      WHEN 17     => c := 'H';
101
      WHEN 18     => c := 'I';
102
      WHEN 19     => c := 'J';
103
      WHEN 20     => c := 'K';
104
      WHEN 21     => c := 'L';
105
      WHEN 22     => c := 'M';
106
      WHEN 23     => c := 'N';
107
      WHEN 24     => c := 'O';
108
      WHEN 25     => c := 'P';
109
      WHEN 26     => c := 'Q';
110
      WHEN 27     => c := 'R';
111
      WHEN 28     => c := 'S';
112
      WHEN 29     => c := 'T';
113
      WHEN 30     => c := 'U';
114
      WHEN 31     => c := 'V';
115
      WHEN 32     => c := 'W';
116
      WHEN 33     => c := 'X';
117
      WHEN 34     => c := 'Y';
118
      WHEN 35     => c := 'Z';
119
      WHEN OTHERS => c := '?';
120
    END CASE;
121
    RETURN c;
122
  END to_char;
123 4 sinx
  ----------------------------------------------------------------------
124 17 sinx
  FUNCTION to_string(int : integer;
125
                     base : integer range 1 to 16 := 16;
126
                     length : integer range 1 to 8 := 8
127
                     ) RETURN string IS
128 2 sinx
 
129
    VARIABLE temp    : string(1 TO 1000);
130
    VARIABLE num     : integer;
131
    VARIABLE abs_int : integer;
132
    VARIABLE len     : integer := 1;
133
    VARIABLE power   : integer := 1;
134
 
135
  BEGIN
136
    abs_int := ABS(int);
137
    num     := abs_int;
138
    --
139 21 sinx
    IF (length = 0) THEN                -- automatic length detection
140 2 sinx
      WHILE num >= base LOOP            -- Determine how many
141
        len := len + 1;                 -- characters required
142
        num := num / base;              -- to represent the
143
      END LOOP;  -- number.
144
    ELSE
145 21 sinx
      len := ABS(length);               --
146 2 sinx
    END IF;
147
 
148
    IF (base /= 10) THEN
149
      len := len + (len-1) / 4;                       -- increase for underlines
150
    END IF;
151
    --
152
    FOR i IN len DOWNTO 1 LOOP                        -- Convert the number to
153
      IF (((len-i) MOD 5 = 4) AND (base /= 10)) THEN  -- every fith char shell be an underline
154
        temp(i) := '_';
155
      ELSE
156
        temp(i) := to_char(abs_int/power MOD base);   -- a string starting
157
        power   := power * base;                      -- with the right hand
158
      END IF;
159
    END LOOP;  -- side.
160
    --
161
    -- return result and add sign if required
162
    IF (base = 16) THEN
163
      IF (int < 0) THEN
164
        CASE temp(len) IS
165
          WHEN '0'    => temp(len) := 'F';
166
          WHEN '1'    => temp(len) := '0';
167
          WHEN '2'    => temp(len) := '1';
168
          WHEN '3'    => temp(len) := '2';
169
          WHEN '4'    => temp(len) := '3';
170
          WHEN '5'    => temp(len) := '4';
171
          WHEN '6'    => temp(len) := '5';
172
          WHEN '7'    => temp(len) := '6';
173
          WHEN '8'    => temp(len) := '7';
174
          WHEN '9'    => temp(len) := '8';
175
          WHEN 'A'    => temp(len) := '9';
176
          WHEN 'B'    => temp(len) := 'A';
177
          WHEN 'C'    => temp(len) := 'B';
178
          WHEN 'D'    => temp(len) := 'C';
179
          WHEN 'E'    => temp(len) := 'D';
180
          WHEN 'F'    => temp(len) := 'E';
181
          WHEN OTHERS => NULL;
182
        END CASE;
183
        FOR i IN len DOWNTO 1 LOOP
184
          CASE temp(i) IS
185
            WHEN '0'    => temp(i) := 'F';
186
            WHEN '1'    => temp(i) := 'E';
187
            WHEN '2'    => temp(i) := 'D';
188
            WHEN '3'    => temp(i) := 'C';
189
            WHEN '4'    => temp(i) := 'B';
190
            WHEN '5'    => temp(i) := 'A';
191
            WHEN '6'    => temp(i) := '9';
192
            WHEN '7'    => temp(i) := '8';
193
            WHEN '8'    => temp(i) := '7';
194
            WHEN '9'    => temp(i) := '6';
195
            WHEN 'A'    => temp(i) := '5';
196
            WHEN 'B'    => temp(i) := '4';
197
            WHEN 'C'    => temp(i) := '3';
198
            WHEN 'D'    => temp(i) := '2';
199
            WHEN 'E'    => temp(i) := '1';
200
            WHEN 'F'    => temp(i) := '0';
201
            WHEN OTHERS => NULL;
202
          END CASE;
203
        END LOOP;  -- i
204
      END IF;
205
      RETURN temp(1 TO len);
206
    ELSE
207
      IF (int < 0) THEN
208
        RETURN '-'& temp(1 TO len);
209
      ELSE
210
        RETURN temp(1 TO len);
211
      END IF;
212
    END IF;
213
  END to_string;
214 4 sinx
  ----------------------------------------------------------------------
215 2 sinx
  FUNCTION to_string(slv : std_logic_vector) RETURN string IS
216
 
217
    VARIABLE hexlen  : integer;
218
    VARIABLE longslv : std_logic_vector(131 DOWNTO 0) := (OTHERS => '0');
219
    VARIABLE hex     : string(1 TO 32);
220
    VARIABLE fourbit : std_logic_vector(3 DOWNTO 0);
221
 
222
  BEGIN
223
    hexlen := ((slv'high - slv'low) + 1) / 4;
224
    IF (((slv'high - slv'low) + 1) MOD 4 /= 0) THEN
225
      hexlen := hexlen + 1;
226
    END IF;
227
    --
228
    longslv((slv'high - slv'low) DOWNTO 0) := slv;
229
    --
230
    FOR i IN (hexlen -1) DOWNTO 0 LOOP
231
      fourbit := longslv(((i*4)+3) DOWNTO (i*4));
232
      CASE fourbit IS
233
        WHEN "0000" => hex(hexlen -I) := '0';
234
        WHEN "0001" => hex(hexlen -I) := '1';
235
        WHEN "0010" => hex(hexlen -I) := '2';
236
        WHEN "0011" => hex(hexlen -I) := '3';
237
        WHEN "0100" => hex(hexlen -I) := '4';
238
        WHEN "0101" => hex(hexlen -I) := '5';
239
        WHEN "0110" => hex(hexlen -I) := '6';
240
        WHEN "0111" => hex(hexlen -I) := '7';
241
        WHEN "1000" => hex(hexlen -I) := '8';
242
        WHEN "1001" => hex(hexlen -I) := '9';
243
        WHEN "1010" => hex(hexlen -I) := 'A';
244
        WHEN "1011" => hex(hexlen -I) := 'B';
245
        WHEN "1100" => hex(hexlen -I) := 'C';
246
        WHEN "1101" => hex(hexlen -I) := 'D';
247
        WHEN "1110" => hex(hexlen -I) := 'E';
248
        WHEN "1111" => hex(hexlen -I) := 'F';
249
        WHEN "ZZZZ" => hex(hexlen -I) := 'z';
250
        WHEN "UUUU" => hex(hexlen -I) := 'u';
251
        WHEN "XXXX" => hex(hexlen -I) := 'x';
252
        WHEN OTHERS => hex(hexlen -I) := '?';
253
      END CASE;
254
    END LOOP;
255
    RETURN hex(1 TO hexlen);
256
  END to_string;
257 4 sinx
  ----------------------------------------------------------------------
258 21 sinx
  FUNCTION to_string( slv : std_logic_vector;
259
                      base : integer range 1 to 16 := 16;
260 17 sinx
                      length : integer range 1 to 8 := 8
261
                      ) RETURN string IS
262 2 sinx
 
263
  BEGIN
264
    RETURN to_string(to_integer(slv), base, length);
265
  END to_string;
266 4 sinx
  ----------------------------------------------------------------------
267 2 sinx
end package body;
268
----------------------------------------------------------------------
269 21 sinx
---- end of file                                                  ----
270 2 sinx
----------------------------------------------------------------------

powered by: WebSVN 2.1.0

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