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 4

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 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
----                                                              ---- 
10 2 sinx
----  To Do:                                                      ---- 
11 4 sinx
----   -                                                          ---- 
12 2 sinx
----                                                              ---- 
13
----  Author(s):                                                  ---- 
14 4 sinx
----      - Sinx, sinx@opencores.org                              ---- 
15 2 sinx
----                                                              ---- 
16
---------------------------------------------------------------------- 
17 4 sinx
----    SVN information
18
----
19
----      $URL:  $
20
---- $Revision:  $
21
----     $Date:  $
22
----   $Author:  $
23
----       $Id:  $
24
---------------------------------------------------------------------- 
25 2 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
  FUNCTION to_string(int             : integer; base : integer := 10; length : integer := 0) RETURN string;
62
  FUNCTION to_string(slv             : std_logic_vector; base : integer; length : integer) RETURN string;
63
 
64
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 2 sinx
  FUNCTION to_string(int : integer; base : integer := 10; length : integer := 0) RETURN string IS
125
 
126
    VARIABLE temp    : string(1 TO 1000);
127
    VARIABLE num     : integer;
128
    VARIABLE abs_int : integer;
129
    VARIABLE len     : integer := 1;
130
    VARIABLE power   : integer := 1;
131
 
132
  BEGIN
133
    abs_int := ABS(int);
134
    num     := abs_int;
135
    --
136
    IF (length = 0) THEN                -- automatic length detection 
137
      WHILE num >= base LOOP            -- Determine how many
138
        len := len + 1;                 -- characters required
139
        num := num / base;              -- to represent the
140
      END LOOP;  -- number.
141
    ELSE
142
      len := ABS(length);               -- 
143
    END IF;
144
 
145
    IF (base /= 10) THEN
146
      len := len + (len-1) / 4;                       -- increase for underlines
147
    END IF;
148
    --
149
    FOR i IN len DOWNTO 1 LOOP                        -- Convert the number to
150
      IF (((len-i) MOD 5 = 4) AND (base /= 10)) THEN  -- every fith char shell be an underline
151
        temp(i) := '_';
152
      ELSE
153
        temp(i) := to_char(abs_int/power MOD base);   -- a string starting
154
        power   := power * base;                      -- with the right hand
155
      END IF;
156
    END LOOP;  -- side.
157
    --
158
    -- return result and add sign if required
159
    IF (base = 16) THEN
160
      IF (int < 0) THEN
161
        CASE temp(len) IS
162
          WHEN '0'    => temp(len) := 'F';
163
          WHEN '1'    => temp(len) := '0';
164
          WHEN '2'    => temp(len) := '1';
165
          WHEN '3'    => temp(len) := '2';
166
          WHEN '4'    => temp(len) := '3';
167
          WHEN '5'    => temp(len) := '4';
168
          WHEN '6'    => temp(len) := '5';
169
          WHEN '7'    => temp(len) := '6';
170
          WHEN '8'    => temp(len) := '7';
171
          WHEN '9'    => temp(len) := '8';
172
          WHEN 'A'    => temp(len) := '9';
173
          WHEN 'B'    => temp(len) := 'A';
174
          WHEN 'C'    => temp(len) := 'B';
175
          WHEN 'D'    => temp(len) := 'C';
176
          WHEN 'E'    => temp(len) := 'D';
177
          WHEN 'F'    => temp(len) := 'E';
178
          WHEN OTHERS => NULL;
179
        END CASE;
180
        FOR i IN len DOWNTO 1 LOOP
181
          CASE temp(i) IS
182
            WHEN '0'    => temp(i) := 'F';
183
            WHEN '1'    => temp(i) := 'E';
184
            WHEN '2'    => temp(i) := 'D';
185
            WHEN '3'    => temp(i) := 'C';
186
            WHEN '4'    => temp(i) := 'B';
187
            WHEN '5'    => temp(i) := 'A';
188
            WHEN '6'    => temp(i) := '9';
189
            WHEN '7'    => temp(i) := '8';
190
            WHEN '8'    => temp(i) := '7';
191
            WHEN '9'    => temp(i) := '6';
192
            WHEN 'A'    => temp(i) := '5';
193
            WHEN 'B'    => temp(i) := '4';
194
            WHEN 'C'    => temp(i) := '3';
195
            WHEN 'D'    => temp(i) := '2';
196
            WHEN 'E'    => temp(i) := '1';
197
            WHEN 'F'    => temp(i) := '0';
198
            WHEN OTHERS => NULL;
199
          END CASE;
200
        END LOOP;  -- i
201
      END IF;
202
      RETURN temp(1 TO len);
203
    ELSE
204
      IF (int < 0) THEN
205
        RETURN '-'& temp(1 TO len);
206
      ELSE
207
        RETURN temp(1 TO len);
208
      END IF;
209
    END IF;
210
  END to_string;
211 4 sinx
  ----------------------------------------------------------------------
212 2 sinx
  FUNCTION to_string(slv : std_logic_vector) RETURN string IS
213
 
214
    VARIABLE hexlen  : integer;
215
    VARIABLE longslv : std_logic_vector(131 DOWNTO 0) := (OTHERS => '0');
216
    VARIABLE hex     : string(1 TO 32);
217
    VARIABLE fourbit : std_logic_vector(3 DOWNTO 0);
218
 
219
  BEGIN
220
    hexlen := ((slv'high - slv'low) + 1) / 4;
221
    IF (((slv'high - slv'low) + 1) MOD 4 /= 0) THEN
222
      hexlen := hexlen + 1;
223
    END IF;
224
    --
225
    longslv((slv'high - slv'low) DOWNTO 0) := slv;
226
    --
227
    FOR i IN (hexlen -1) DOWNTO 0 LOOP
228
      fourbit := longslv(((i*4)+3) DOWNTO (i*4));
229
      CASE fourbit IS
230
        WHEN "0000" => hex(hexlen -I) := '0';
231
        WHEN "0001" => hex(hexlen -I) := '1';
232
        WHEN "0010" => hex(hexlen -I) := '2';
233
        WHEN "0011" => hex(hexlen -I) := '3';
234
        WHEN "0100" => hex(hexlen -I) := '4';
235
        WHEN "0101" => hex(hexlen -I) := '5';
236
        WHEN "0110" => hex(hexlen -I) := '6';
237
        WHEN "0111" => hex(hexlen -I) := '7';
238
        WHEN "1000" => hex(hexlen -I) := '8';
239
        WHEN "1001" => hex(hexlen -I) := '9';
240
        WHEN "1010" => hex(hexlen -I) := 'A';
241
        WHEN "1011" => hex(hexlen -I) := 'B';
242
        WHEN "1100" => hex(hexlen -I) := 'C';
243
        WHEN "1101" => hex(hexlen -I) := 'D';
244
        WHEN "1110" => hex(hexlen -I) := 'E';
245
        WHEN "1111" => hex(hexlen -I) := 'F';
246
        WHEN "ZZZZ" => hex(hexlen -I) := 'z';
247
        WHEN "UUUU" => hex(hexlen -I) := 'u';
248
        WHEN "XXXX" => hex(hexlen -I) := 'x';
249
        WHEN OTHERS => hex(hexlen -I) := '?';
250
      END CASE;
251
    END LOOP;
252
    RETURN hex(1 TO hexlen);
253
  END to_string;
254 4 sinx
  ----------------------------------------------------------------------
255 2 sinx
  FUNCTION to_string(slv : std_logic_vector; base : integer; length : integer) RETURN string IS
256
 
257
  BEGIN
258
    RETURN to_string(to_integer(slv), base, length);
259
  END to_string;
260 4 sinx
  ----------------------------------------------------------------------
261 2 sinx
end package body;
262
----------------------------------------------------------------------
263
---- end of file                                                  ---- 
264
----------------------------------------------------------------------

powered by: WebSVN 2.1.0

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