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

Subversion Repositories common_pkg

[/] [common_pkg/] [trunk/] [common_str_pkg.vhd] - Blame information for rev 6

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

Line No. Rev Author Line
1 6 danv
-------------------------------------------------------------------------------
2
--
3
-- Copyright (C) 2012
4
-- ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
5
-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
6
--
7
-- This program is free software: you can redistribute it and/or modify
8
-- it under the terms of the GNU General Public License as published by
9
-- the Free Software Foundation, either version 3 of the License, or
10
-- (at your option) any later version.
11
--
12
-- This program is distributed in the hope that it will be useful,
13
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
14
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
-- GNU General Public License for more details.
16
--
17
-- You should have received a copy of the GNU General Public License
18
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
--
20
-------------------------------------------------------------------------------
21
 
22
LIBRARY IEEE;
23
USE IEEE.STD_LOGIC_1164.ALL;
24
USE IEEE.NUMERIC_STD.ALL;
25
USE STD.TEXTIO.ALL;
26
USE IEEE.STD_LOGIC_TEXTIO.ALL;
27
USE work.common_pkg.ALL;
28
 
29
PACKAGE common_str_pkg IS
30
 
31
  TYPE t_str_4_arr IS ARRAY (INTEGER RANGE <>) OF STRING(1 TO 4);
32
 
33
  FUNCTION nof_digits(number: NATURAL) RETURN NATURAL;
34
  FUNCTION nof_digits_int(number: INTEGER) RETURN NATURAL;
35
 
36
  FUNCTION time_to_str(in_time : TIME) RETURN STRING;
37
  FUNCTION str_to_time(in_str : STRING) RETURN TIME;
38
  FUNCTION slv_to_str(slv : STD_LOGIC_VECTOR) RETURN STRING;
39
  FUNCTION str_to_hex(str : STRING) RETURN STRING;
40
  FUNCTION slv_to_hex(slv : STD_LOGIC_VECTOR) RETURN STRING;
41
  FUNCTION hex_to_slv(str : STRING) RETURN STD_LOGIC_VECTOR;
42
 
43
  Function hex_nibble_to_slv(c: character) return std_logic_vector;
44
 
45
  FUNCTION int_to_str(int: INTEGER) RETURN STRING;
46
  FUNCTION real_to_str(re: REAL; width : INTEGER; digits : INTEGER) RETURN STRING;
47
 
48
  PROCEDURE print_str(str : STRING);
49
 
50
  FUNCTION str_to_ascii_integer_arr(s: STRING) RETURN t_integer_arr;
51
  FUNCTION str_to_ascii_slv_8_arr(  s: STRING) RETURN t_slv_8_arr;
52
  FUNCTION str_to_ascii_slv_32_arr( s: STRING) RETURN t_slv_32_arr;
53
  FUNCTION str_to_ascii_slv_32_arr( s: STRING; arr_size : NATURAL) RETURN t_slv_32_arr;
54
 
55
END common_str_pkg;
56
 
57
PACKAGE BODY common_str_pkg IS
58
 
59
  FUNCTION nof_digits(number: NATURAL) RETURN NATURAL IS
60
  -- Returns number of digits in a natural number. Only used in string processing, so defined here.
61
  -- log10(0) is not allowed so:
62
  -- . nof_digits(0) = 1
63
  -- We're adding 1 so:
64
  -- . nof_digits(1) = 1
65
  -- . nof_digits(9) = 1
66
  -- . nof_digits(10) = 2
67
  BEGIN
68
    IF number>0 THEN
69
      RETURN floor_log10(number)+1;
70
    ELSE
71
      RETURN 1;
72
    END IF;
73
  END;
74
 
75
  FUNCTION nof_digits_int(number: INTEGER) RETURN NATURAL IS
76
  -- Returns number of digits in a natural number. Only used in string processing, so defined here.
77
  -- log10(0) is not allowed so:
78
  -- . nof_digits(0) = 1
79
  -- We're adding 1 so:
80
  -- . nof_digits(1) = 1
81
  -- . nof_digits(9) = 1
82
  -- . nof_digits(10) = 2
83
  -- . nof_digits(1) = 2
84
  BEGIN
85
    IF number=0 THEN
86
      RETURN 1;
87
    ELSE
88
      IF number > 0 THEN
89
        RETURN floor_log10(number)+1;
90
      ELSE
91
        RETURN floor_log10(-1*number)+2;
92
      END IF;
93
    END IF;
94
  END;
95
 
96
  FUNCTION time_to_str(in_time : TIME) RETURN STRING IS
97
    CONSTANT c_max_len_time : NATURAL := 20;
98
        VARIABLE v_line         : LINE;
99
        VARIABLE v_str          : STRING(1 TO c_max_len_time):= (OTHERS => ' ');
100
  BEGIN
101
    write(v_line, in_time);
102
        v_str(v_line.ALL'RANGE) := v_line.ALL;
103
        deallocate(v_line);
104
        RETURN v_str;
105
  END;
106
 
107
  FUNCTION str_to_time(in_str : STRING) RETURN TIME IS
108
  BEGIN
109
    RETURN TIME'VALUE(in_str);
110
  END;
111
 
112
  FUNCTION slv_to_str(slv : STD_LOGIC_VECTOR) RETURN STRING IS
113
    VARIABLE v_line : LINE;
114
    VARIABLE v_str  : STRING(1 TO slv'LENGTH) := (OTHERS => ' ');
115
  BEGIN
116
     write(v_line, slv);
117
     v_str(v_line.ALL'RANGE) := v_line.ALL;
118
     deallocate(v_line);
119
     RETURN v_str;
120
  END;
121
 
122
  FUNCTION str_to_hex(str : STRING) RETURN STRING IS
123
    CONSTANT c_nof_nibbles : NATURAL := ceil_div(str'LENGTH, c_nibble_w);
124
    VARIABLE v_nibble_arr  : t_str_4_arr(0 TO c_nof_nibbles-1) := (OTHERS=>(OTHERS=>'0'));
125
    VARIABLE v_hex         : STRING(1 TO c_nof_nibbles) := (OTHERS => '0');
126
  BEGIN
127
    FOR i IN 0 TO v_hex'RIGHT-1 LOOP
128
      v_nibble_arr(i) := slice_up(str, c_nibble_w, i, '0');
129
 
130
      CASE v_nibble_arr(i) IS
131
        WHEN "0000" => v_hex(i+1) := '0';
132
        WHEN "0001" => v_hex(i+1) := '1';
133
        WHEN "0010" => v_hex(i+1) := '2';
134
        WHEN "0011" => v_hex(i+1) := '3';
135
        WHEN "0100" => v_hex(i+1) := '4';
136
        WHEN "0101" => v_hex(i+1) := '5';
137
        WHEN "0110" => v_hex(i+1) := '6';
138
        WHEN "0111" => v_hex(i+1) := '7';
139
        WHEN "1000" => v_hex(i+1) := '8';
140
        WHEN "1001" => v_hex(i+1) := '9';
141
        WHEN "1010" => v_hex(i+1) := 'A';
142
        WHEN "1011" => v_hex(i+1) := 'B';
143
        WHEN "1100" => v_hex(i+1) := 'C';
144
        WHEN "1101" => v_hex(i+1) := 'D';
145
        WHEN "1110" => v_hex(i+1) := 'E';
146
        WHEN "1111" => v_hex(i+1) := 'F';
147
        WHEN OTHERS => v_hex(i+1) := 'X';
148
      END CASE;
149
    END LOOP;
150
    RETURN v_hex;
151
  END;
152
 
153
  FUNCTION slv_to_hex(slv :STD_LOGIC_VECTOR) RETURN STRING IS
154
  BEGIN
155
    RETURN str_to_hex(slv_to_str(slv));
156
  END;
157
 
158
  FUNCTION hex_to_slv(str: STRING) RETURN STD_LOGIC_VECTOR IS
159
  CONSTANT c_length : NATURAL := str'LENGTH;
160
  VARIABLE v_str    : STRING(1 TO str'LENGTH) := str; -- Keep local copy of str to prevent range mismatch
161
  VARIABLE v_result : STD_LOGIC_VECTOR(c_length * 4 - 1 DOWNTO 0);
162
  BEGIN
163
   FOR i IN c_length DOWNTO 1 LOOP
164
       v_result(3 +(c_length - i)*4  DOWNTO (c_length-i)*4) := hex_nibble_to_slv(v_str(i));
165
   END LOOP;
166
   RETURN v_result;
167
  END;
168
 
169
  FUNCTION hex_nibble_to_slv(c: CHARACTER) RETURN STD_LOGIC_VECTOR IS
170
    VARIABLE v_result : STD_LOGIC_VECTOR(3 DOWNTO 0);
171
  BEGIN
172
    CASE c IS
173
      WHEN '0' => v_result :=  "0000";
174
      WHEN '1' => v_result :=  "0001";
175
      WHEN '2' => v_result :=  "0010";
176
      WHEN '3' => v_result :=  "0011";
177
      WHEN '4' => v_result :=  "0100";
178
      WHEN '5' => v_result :=  "0101";
179
      WHEN '6' => v_result :=  "0110";
180
      WHEN '7' => v_result :=  "0111";
181
      WHEN '8' => v_result :=  "1000";
182
      WHEN '9' => v_result :=  "1001";
183
      WHEN 'A' => v_result :=  "1010";
184
      WHEN 'B' => v_result :=  "1011";
185
      WHEN 'C' => v_result :=  "1100";
186
      WHEN 'D' => v_result :=  "1101";
187
      WHEN 'E' => v_result :=  "1110";
188
      WHEN 'F' => v_result :=  "1111";
189
      WHEN 'a' => v_result :=  "1010";
190
      WHEN 'b' => v_result :=  "1011";
191
      WHEN 'c' => v_result :=  "1100";
192
      WHEN 'd' => v_result :=  "1101";
193
      WHEN 'e' => v_result :=  "1110";
194
      WHEN 'f' => v_result :=  "1111";
195
      WHEN 'x' => v_result :=  "XXXX";
196
      WHEN 'X' => v_result :=  "XXXX";
197
      WHEN 'z' => v_result :=  "ZZZZ";
198
      WHEN 'Z' => v_result :=  "ZZZZ";
199
 
200
            WHEN OTHERS => v_result := "0000";
201
     END CASE;
202
   RETURN v_result;
203
  END hex_nibble_to_slv;
204
 
205
  FUNCTION int_to_str(int: INTEGER) RETURN STRING IS
206
--    CONSTANT c_max_len_int : NATURAL := 20;
207
    VARIABLE v_line: LINE;
208
    VARIABLE v_str: STRING(1 TO nof_digits_int(int)):= (OTHERS => ' ');
209
  BEGIN
210
    STD.TEXTIO.WRITE(v_line, int);
211
    v_str(v_line.ALL'RANGE) := v_line.ALL;
212
    deallocate(v_line);
213
    RETURN v_str;
214
  END;
215
 
216
  FUNCTION real_to_str(re: REAL; width : INTEGER; digits : INTEGER) RETURN STRING IS
217
    VARIABLE v_line: LINE;
218
    VARIABLE v_str: STRING(1 TO width):= (OTHERS => ' ');
219
  BEGIN
220
    STD.TEXTIO.WRITE(v_line, re, right, width, digits);
221
    v_str(v_line.ALL'RANGE) := v_line.ALL;
222
    deallocate(v_line);
223
    RETURN v_str;
224
  END;
225
 
226
  PROCEDURE print_str(str: STRING) IS
227
  VARIABLE v_line: LINE;
228
  BEGIN
229
    write(v_line, str);
230
    writeline(output, v_line);
231
    deallocate(v_line);
232
  END;
233
 
234
  FUNCTION str_to_ascii_integer_arr(s: STRING) RETURN t_integer_arr IS
235
    VARIABLE r: t_integer_arr(0 TO s'RIGHT-1);
236
  BEGIN
237
    FOR i IN s'RANGE LOOP
238
      r(i-1) := CHARACTER'POS(s(i));
239
    END LOOP;
240
    RETURN r;
241
  END;
242
 
243
  FUNCTION str_to_ascii_slv_8_arr(s: STRING) RETURN t_slv_8_arr IS
244
    VARIABLE r: t_slv_8_arr(0 TO s'RIGHT-1);
245
  BEGIN
246
    FOR i IN s'RANGE LOOP
247
      r(i-1) := TO_UVEC(str_to_ascii_integer_arr(s)(i-1), 8);
248
    END LOOP;
249
    RETURN r;
250
  END;
251
 
252
  -- Returns minimum array size required to fit the string
253
  FUNCTION str_to_ascii_slv_32_arr(s: STRING) RETURN t_slv_32_arr IS
254
    CONSTANT c_slv_8: t_slv_8_arr(0 TO s'RIGHT-1) := str_to_ascii_slv_8_arr(s);
255
    CONSTANT c_bytes_per_word : NATURAL := 4;
256
    -- Initialize all elements to (OTHERS=>'0') so any unused bytes become a NULL character
257
    VARIABLE r: t_slv_32_arr(0 TO ceil_div(s'RIGHT * c_byte_w, c_word_w)-1) := (OTHERS=>(OTHERS=>'0'));
258
  BEGIN
259
    FOR word IN r'RANGE  LOOP --0, 1
260
      FOR byte IN 0 TO c_bytes_per_word-1 LOOP -- 0,1,2,3
261
        IF byte+c_bytes_per_word*word<=c_slv_8'RIGHT THEN
262
          r(word)(byte*c_byte_w+c_byte_w-1 DOWNTO byte*c_byte_w) := c_slv_8(byte+c_bytes_per_word*word);
263
        END IF;
264
      END LOOP;
265
    END LOOP;
266
 
267
    RETURN r;
268
  END;
269
 
270
  -- Overloaded version to match array size to arr_size
271
  FUNCTION str_to_ascii_slv_32_arr(s: STRING; arr_size: NATURAL) RETURN t_slv_32_arr IS
272
    CONSTANT slv_32: t_slv_32_arr(0 TO ceil_div(s'RIGHT * c_byte_w, c_word_w)-1) := str_to_ascii_slv_32_arr(s);
273
    VARIABLE r: t_slv_32_arr(0 TO arr_size-1) := (OTHERS=>(OTHERS=>'0'));
274
  BEGIN
275
    FOR word IN slv_32'RANGE  LOOP
276
      r(word) := slv_32(word);
277
    END LOOP;
278
    RETURN r;
279
  END;
280
 
281
END common_str_pkg;
282
 

powered by: WebSVN 2.1.0

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