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 16

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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