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

Subversion Repositories sd_card_controller

[/] [sd_card_controller/] [trunk/] [rtl/] [VHDL/] [convert_pack.vhd] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jclaytons
-- A package containing various conversion functions useful in testbenches,
2
-- especially when used with text file IO in reading and displaying
3
-- hexadecimal values.
4
--
5
-- Modifications by : John Clayton
6
--
7
 
8
library ieee;
9
use ieee.std_logic_1164.all;
10
use ieee.numeric_std.all;
11
use ieee.math_real.all;
12
use std.textio.all;
13
 
14
package convert_pack is
15
 
16
------------------------------------------------------------------------------------
17
-- function calls
18
------------------------------------------------------------------------------------
19
    function string_to_integer (in_string : string) return integer ;
20
    function vector_to_string (in_vector : std_logic_vector) return string ;
21
    function char_to_bit (in_char : character) return std_logic ;
22
    function char_to_hex (in_char : character) return std_logic_vector ;
23
 
24
    function slv2string(in_vector : std_logic_vector; nibbles : natural) return string ; -- Name changed by John Clayton
25
    function u2string(in_vector : unsigned; nibbles : natural) return string ; -- Added by John Clayton
26
    function u2asciichar(in_vector : unsigned(7 downto 0)) return character ; -- Added by John Clayton
27
    function asciichar2u(in_char : character) return unsigned; -- Added by John Clayton
28
 
29
    function hex_to_ascii(in_vector : std_logic_vector; nibbles : natural) return string ;
30
    function u2ascii(in_vector : unsigned; nibbles : natural) return string ; -- Added by John Clayton
31
    function hex_data_wrd(in_vector : std_logic_vector) return string ;
32
    function hex_data_dblwrd(in_vector : std_logic_vector) return string ;
33
    function hex_data_dblwrdz(in_vector : std_logic_vector) return string ;
34
 
35
    function is_hex(c : character) return boolean;    -- Added by John Clayton
36
    function is_std_logic(c : character) return boolean;    -- Added by John Clayton
37
    function is_space(c : character) return boolean;  -- Added by John Clayton
38
    function char2sl(in_char : character) return std_logic;  -- Added by John Clayton
39
    function char2slv(in_char : character) return std_logic_vector;
40
    function char2u(in_char : character) return unsigned;
41
    function slv2u(in_a : std_logic_vector) return unsigned; -- Added by John Clayton
42
    function u2slv(in_a : unsigned) return std_logic_vector; -- Added by John Clayton
43
    function slv2s(in_a : std_logic_vector) return signed; -- Added by John Clayton
44
    function s2slv(in_a : signed) return std_logic_vector; -- Added by John Clayton
45
    function str2u(in_string : string; out_size:integer) return unsigned; -- Added by John Clayton
46
    function str2s(in_string : string; out_size:integer) return   signed; -- Added by John Clayton
47
--    function "**"(in_a : natural; in_b : natural) return natural; -- Added by John Clayton
48
    function pow_2_u(in_a : natural; out_size:integer) return unsigned; -- Added by John Clayton
49
    function asr_function(in_vect : signed; in_a : natural) return signed; -- Added by John Clayton
50
 
51
    function slv_resize(in_vect : std_logic_vector; out_size : integer) return std_logic_vector; -- Added by John Clayton
52
    function slv_resize_l(in_vect : std_logic_vector; out_size : integer) return std_logic_vector; -- Added by John Clayton
53
    function slv_resize_se(in_vect : std_logic_vector; out_size : integer) return std_logic_vector; -- Added by John Clayton
54
    function s_resize(in_vect : signed; out_size : integer) return signed; -- Added by John Clayton
55
    function s_resize_l(in_vect : signed; out_size : integer) return signed; -- Added by John Clayton
56
    function s_resize_se(in_vect : signed; out_size : integer) return signed; -- Added by John Clayton
57
    function u_resize(in_vect : unsigned; out_size : integer) return unsigned; -- Added by John Clayton
58
    function u_resize_l(in_vect : unsigned; out_size : integer) return unsigned; -- Added by John Clayton
59
    function u_select(in_vect : unsigned; slice_num : integer; slice_size : integer) return unsigned; -- Added by John Clayton
60
    function u_reverse(in_vect : unsigned) return unsigned; -- Added by John Clayton
61
 
62
    function u_recursive_parity ( x : unsigned ) return std_logic; -- Added by John Clayton
63
 
64
    function bit_width (maxval : integer) return integer ; -- Added by John Clayton
65
    function bit_width (maxval : real) return integer ; -- Added by John Clayton
66
    function timer_width (maxval : integer) return integer ; -- Added by John Clayton
67
    function timer_width (maxval : real) return integer ; -- Added by John Clayton
68
 
69
    function num_words (num_bits : integer; bits_per_word : integer) return integer ;
70
    function asciichar2u2(d:character) return unsigned; -- Added by John Clayton.  Loosely based on code in Thesis by Rudi Rughoonundon, 11-1-1996
71
    function str2u(s: string) return unsigned; -- Added by John Clayton, Converts a string of ASCII characters into unsigned
72
    function u_ones(in_a : unsigned) return natural; -- Added by John Clayton, counts the number of '1' bits in an unsigned
73
 
74
 
75
------------------------------------------------------------------------------------
76
-- procedures
77
------------------------------------------------------------------------------------
78
 
79
 
80
end convert_pack;
81
 
82
 
83
package body convert_pack is
84
 
85
------------------------------------------------------------------------------------
86
-- 
87
------------------------------------------------------------------------------------
88
--* Title           :  TEST_PARITY
89
--* Filename & Ext  :  test_parity.vhdl
90
--* Author          :  David Bishop X-66788
91
--* Created         :  3/18/97
92
--* Version         :  1.2
93
--* Revision Date   :  97/04/15
94
--* SCCSid          :  1.2 04/15/97 test_parity.vhdl
95
--* WORK Library    :  testchip
96
--* Mod History     :  
97
--* Description     :  This is a parity generator which is written recursively
98
--*                 :  It is designed to test the ability of Simulation and
99
--*                 :  Synthesis tools to check this capability.
100
--* Known Bugs      :  
101
--* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
102
 
103
 
104
  function u_recursive_parity ( x : unsigned ) return std_logic is
105
    variable Upper, Lower : std_logic;
106
    variable Half : integer;
107
    variable BUS_int : unsigned( x'length-1 downto 0 );
108
    variable Result : std_logic;
109
  begin
110
    BUS_int := x;
111
    if ( BUS_int'length = 1 ) then
112
      Result := BUS_int ( BUS_int'left );
113
    elsif ( BUS_int'length = 2 ) then
114
      Result := BUS_int ( BUS_int'right ) xor BUS_int ( BUS_int'left );
115
    else
116
      Half := ( BUS_int'length + 1 ) / 2 + BUS_int'right;
117
      Upper := u_recursive_parity ( BUS_int ( BUS_int'left downto Half ));
118
      Lower := u_recursive_parity ( BUS_int ( Half - 1 downto BUS_int'right ));
119
      Result := Upper xor Lower;
120
    end if;
121
    return Result;
122
  end;
123
 
124
------------------------------------------------------------------------------------
125
-- 
126
------------------------------------------------------------------------------------
127
 
128
    function vector_to_string (in_vector : std_logic_vector) return string is
129
 
130
    variable out_string : string(32 downto 1);
131
 
132
    begin
133
 
134
        for i in in_vector'range loop
135
                if in_vector(i) = '1' then
136
                        out_string(i+1) := '1';
137
                elsif in_vector(i) = '0' then
138
                        out_string(i+1) := '0';
139
            else
140
                assert false
141
                report " illegal bit vector to bit string"
142
                severity note;
143
                end if;
144
        end loop;
145
        return out_string;
146
 
147
    end vector_to_string;
148
 
149
------------------------------------------------------------------------------------
150
-- 
151
------------------------------------------------------------------------------------
152
 
153
    function string_to_integer (in_string : string) return integer is
154
 
155
    variable int : integer := 0;
156
    begin
157
 
158
        for j in in_string'range loop
159
          case in_string(j) is
160
            when '0' => int := int;
161
            when '1' => int := int + (1 * 10**(j-1));
162
            when '2' => int := int + (2 * 10**(j-1));
163
            when '3' => int := int + (3 * 10**(j-1));
164
            when '4' => int := int + (4 * 10**(j-1));
165
            when '5' => int := int + (5 * 10**(j-1));
166
            when '6' => int := int + (6 * 10**(j-1));
167
            when '7' => int := int + (7 * 10**(j-1));
168
            when '8' => int := int + (8 * 10**(j-1));
169
            when '9' => int := int + (9 * 10**(j-1));
170
            when others =>
171
                assert false
172
                report " illegal character to integer"
173
                severity note;
174
          end case;
175
        end loop;
176
        return  int;
177
    end string_to_integer;
178
 
179
------------------------------------------------------------------------------------
180
-- 
181
------------------------------------------------------------------------------------
182
 
183
    function char_to_bit (in_char : character) return std_logic is
184
 
185
    variable out_bit : std_logic;
186
 
187
    begin
188
 
189
        if (in_char = '1') then
190
            out_bit := '1';
191
        elsif (in_char = '0') then
192
            out_bit := '0';
193
        else
194
            assert false
195
            report "illegal character to bit"
196
            severity note;
197
        end if;
198
 
199
        return out_bit;
200
    end char_to_bit;
201
 
202
------------------------------------------------------------------------------------
203
-- 
204
------------------------------------------------------------------------------------
205
    function char_to_hex (in_char : character) return std_logic_vector is
206
 
207
    variable out_vec : std_logic_vector(3 downto 0);
208
 
209
 
210
    begin
211
            case in_char is
212
              when '0' => out_vec := "0000";
213
              when '1' => out_vec := "0001";
214
              when '2' => out_vec := "0010";
215
              when '3' => out_vec := "0011";
216
              when '4' => out_vec := "0100";
217
              when '5' => out_vec := "0101";
218
              when '6' => out_vec := "0110";
219
              when '7' => out_vec := "0111";
220
              when '8' => out_vec := "1000";
221
              when '9' => out_vec := "1001";
222
              when 'A' | 'a' => out_vec := "1010";
223
              when 'B' | 'b' => out_vec := "1011";
224
              when 'C' | 'c' => out_vec := "1100";
225
              when 'D' | 'd' => out_vec := "1101";
226
              when 'E' | 'e' => out_vec := "1110";
227
              when 'F' | 'f' => out_vec := "1111";
228
              when others =>
229
                assert false
230
                report " illegal character to hex"
231
                severity note;
232
          end case;
233
        return out_vec;
234
    end char_to_hex;
235
 
236
------------------------------------------------------------------------------------
237
-- 
238
------------------------------------------------------------------------------------
239
 
240
    function slv2string(in_vector : std_logic_vector; nibbles : natural) return string is
241
    variable out_string : string(1 to nibbles);
242
    variable temp_in_vector : std_logic_vector(4*nibbles-1 downto 0);
243
    variable vector     : std_logic_vector(3 downto 0);
244
 
245
    begin
246
    temp_in_vector := in_vector(in_vector'length-1 downto in_vector'length-temp_in_vector'length);
247
        for i in 1 to nibbles loop
248
            vector := temp_in_vector((4*(nibbles-i)+3) downto 4*(nibbles-i));
249
            case vector is
250
                when "0000" => out_string(i) := '0';
251
                when "0001" => out_string(i) := '1';
252
                when "0010" => out_string(i) := '2';
253
                when "0011" => out_string(i) := '3';
254
                when "0100" => out_string(i) := '4';
255
                when "0101" => out_string(i) := '5';
256
                when "0110" => out_string(i) := '6';
257
                when "0111" => out_string(i) := '7';
258
                when "1000" => out_string(i) := '8';
259
                when "1001" => out_string(i) := '9';
260
                when "1010" => out_string(i) := 'A';
261
                when "1011" => out_string(i) := 'B';
262
                when "1100" => out_string(i) := 'C';
263
                when "1101" => out_string(i) := 'D';
264
                when "1110" => out_string(i) := 'E';
265
                when "1111" => out_string(i) := 'F';
266
                when others =>
267
                   out_string(i) := 'J';
268
                   assert false
269
                   report " illegal std_logic_vector to string"
270
                   severity note;
271
            end case;
272
        end loop;
273
        return out_string;
274
     end;
275
 
276
------------------------------------------------------------------------------------
277
-- 
278
------------------------------------------------------------------------------------
279
 
280
    function u2string(in_vector : unsigned; nibbles : natural) return string is
281
    variable out_string     : string(1 to nibbles);
282
    variable temp_in_vector : unsigned(4*nibbles-1 downto 0);
283
    variable vector         : unsigned(3 downto 0);
284
 
285
    begin
286
    temp_in_vector := in_vector(in_vector'length-1 downto in_vector'length-temp_in_vector'length);
287
        for i in 1 to nibbles loop
288
            vector := temp_in_vector((4*(nibbles-i)+3) downto 4*(nibbles-i));
289
            case vector is
290
                when "0000" => out_string(i) := '0';
291
                when "0001" => out_string(i) := '1';
292
                when "0010" => out_string(i) := '2';
293
                when "0011" => out_string(i) := '3';
294
                when "0100" => out_string(i) := '4';
295
                when "0101" => out_string(i) := '5';
296
                when "0110" => out_string(i) := '6';
297
                when "0111" => out_string(i) := '7';
298
                when "1000" => out_string(i) := '8';
299
                when "1001" => out_string(i) := '9';
300
                when "1010" => out_string(i) := 'A';
301
                when "1011" => out_string(i) := 'B';
302
                when "1100" => out_string(i) := 'C';
303
                when "1101" => out_string(i) := 'D';
304
                when "1110" => out_string(i) := 'E';
305
                when "1111" => out_string(i) := 'F';
306
                when others =>
307
                   out_string(i) := 'U';
308
                   assert false report " illegal unsigned to string" severity note;
309
            end case;
310
        end loop;
311
        return out_string;
312
     end;
313
 
314
------------------------------------------------------------------------------------
315
-- 
316
------------------------------------------------------------------------------------
317
 
318
    function u2asciichar(in_vector : unsigned(7 downto 0)) return character is
319
    variable out_char       : character;
320
 
321
    begin
322
      case in_vector is
323
          when "00001001" => out_char :=  HT; -- Horizontal Tab
324
          when "00100000" => out_char := ' ';
325
          when "00100001" => out_char := '!';
326
          when "00100010" => out_char := '"';
327
          when "00100011" => out_char := '#';
328
          when "00100100" => out_char := '$';
329
          when "00100101" => out_char := '%';
330
          when "00100110" => out_char := '&';
331
          when "00100111" => out_char := ''';
332
          when "00101000" => out_char := '(';
333
          when "00101001" => out_char := ')';
334
          when "00101010" => out_char := '*';
335
          when "00101011" => out_char := '+';
336
          when "00101100" => out_char := ',';
337
          when "00101101" => out_char := '-';
338
          when "00101110" => out_char := '.';
339
          when "00101111" => out_char := '/';
340
          when "00110000" => out_char := '0';
341
          when "00110001" => out_char := '1';
342
          when "00110010" => out_char := '2';
343
          when "00110011" => out_char := '3';
344
          when "00110100" => out_char := '4';
345
          when "00110101" => out_char := '5';
346
          when "00110110" => out_char := '6';
347
          when "00110111" => out_char := '7';
348
          when "00111000" => out_char := '8';
349
          when "00111001" => out_char := '9';
350
          when "00111010" => out_char := ':';
351
          when "00111011" => out_char := ';';
352
          when "00111100" => out_char := '<';
353
          when "00111101" => out_char := '=';
354
          when "00111110" => out_char := '>';
355
          when "00111111" => out_char := '?';
356
          when "01000000" => out_char := '@';
357
          when "01000001" => out_char := 'A';
358
          when "01000010" => out_char := 'B';
359
          when "01000011" => out_char := 'C';
360
          when "01000100" => out_char := 'D';
361
          when "01000101" => out_char := 'E';
362
          when "01000110" => out_char := 'F';
363
          when "01000111" => out_char := 'G';
364
          when "01001000" => out_char := 'H';
365
          when "01001001" => out_char := 'I';
366
          when "01001010" => out_char := 'J';
367
          when "01001011" => out_char := 'K';
368
          when "01001100" => out_char := 'L';
369
          when "01001101" => out_char := 'M';
370
          when "01001110" => out_char := 'N';
371
          when "01001111" => out_char := 'O';
372
          when "01010000" => out_char := 'P';
373
          when "01010001" => out_char := 'Q';
374
          when "01010010" => out_char := 'R';
375
          when "01010011" => out_char := 'S';
376
          when "01010100" => out_char := 'T';
377
          when "01010101" => out_char := 'U';
378
          when "01010110" => out_char := 'V';
379
          when "01010111" => out_char := 'W';
380
          when "01011000" => out_char := 'X';
381
          when "01011001" => out_char := 'Y';
382
          when "01011010" => out_char := 'Z';
383
          when "01011011" => out_char := '[';
384
          when "01011100" => out_char := '\';
385
          when "01011101" => out_char := ']';
386
          when "01011110" => out_char := '^';
387
          when "01011111" => out_char := '_';
388
          when "01100000" => out_char := '`';
389
          when "01100001" => out_char := 'a';
390
          when "01100010" => out_char := 'b';
391
          when "01100011" => out_char := 'c';
392
          when "01100100" => out_char := 'd';
393
          when "01100101" => out_char := 'e';
394
          when "01100110" => out_char := 'f';
395
          when "01100111" => out_char := 'g';
396
          when "01101000" => out_char := 'h';
397
          when "01101001" => out_char := 'i';
398
          when "01101010" => out_char := 'j';
399
          when "01101011" => out_char := 'k';
400
          when "01101100" => out_char := 'l';
401
          when "01101101" => out_char := 'm';
402
          when "01101110" => out_char := 'n';
403
          when "01101111" => out_char := 'o';
404
          when "01110000" => out_char := 'p';
405
          when "01110001" => out_char := 'q';
406
          when "01110010" => out_char := 'r';
407
          when "01110011" => out_char := 's';
408
          when "01110100" => out_char := 't';
409
          when "01110101" => out_char := 'u';
410
          when "01110110" => out_char := 'v';
411
          when "01110111" => out_char := 'w';
412
          when "01111000" => out_char := 'x';
413
          when "01111001" => out_char := 'y';
414
          when "01111010" => out_char := 'z';
415
          when "01111011" => out_char := '{';
416
          when "01111100" => out_char := '|';
417
          when "01111101" => out_char := '}';
418
          when "01111110" => out_char := '~';
419
          when others =>
420
             out_char := '*';
421
             --assert false report " illegal unsigned to ascii character" severity note;
422
      end case;
423
      return out_char;
424
     end;
425
 
426
------------------------------------------------------------------------------------
427
--
428
------------------------------------------------------------------------------------
429
 
430
    function asciichar2u(in_char : character) return unsigned is
431
    variable out_vect : unsigned(7 downto 0);
432
 
433
    begin
434
      case in_char is
435
          when  HT => out_vect := "00001001";
436
          when ' ' => out_vect := "00100000";
437
          when '!' => out_vect := "00100001";
438
          when '"' => out_vect := "00100010";
439
          when '#' => out_vect := "00100011";
440
          when '$' => out_vect := "00100100";
441
          when '%' => out_vect := "00100101";
442
          when '&' => out_vect := "00100110";
443
          when ''' => out_vect := "00100111";
444
          when '(' => out_vect := "00101000";
445
          when ')' => out_vect := "00101001";
446
          when '*' => out_vect := "00101010";
447
          when '+' => out_vect := "00101011";
448
          when ',' => out_vect := "00101100";
449
          when '-' => out_vect := "00101101";
450
          when '.' => out_vect := "00101110";
451
          when '/' => out_vect := "00101111";
452
          when '0' => out_vect := "00110000";
453
          when '1' => out_vect := "00110001";
454
          when '2' => out_vect := "00110010";
455
          when '3' => out_vect := "00110011";
456
          when '4' => out_vect := "00110100";
457
          when '5' => out_vect := "00110101";
458
          when '6' => out_vect := "00110110";
459
          when '7' => out_vect := "00110111";
460
          when '8' => out_vect := "00111000";
461
          when '9' => out_vect := "00111001";
462
          when ':' => out_vect := "00111010";
463
          when ';' => out_vect := "00111011";
464
          when '<' => out_vect := "00111100";
465
          when '=' => out_vect := "00111101";
466
          when '>' => out_vect := "00111110";
467
          when '?' => out_vect := "00111111";
468
          when '@' => out_vect := "01000000";
469
          when 'A' => out_vect := "01000001";
470
          when 'B' => out_vect := "01000010";
471
          when 'C' => out_vect := "01000011";
472
          when 'D' => out_vect := "01000100";
473
          when 'E' => out_vect := "01000101";
474
          when 'F' => out_vect := "01000110";
475
          when 'G' => out_vect := "01000111";
476
          when 'H' => out_vect := "01001000";
477
          when 'I' => out_vect := "01001001";
478
          when 'J' => out_vect := "01001010";
479
          when 'K' => out_vect := "01001011";
480
          when 'L' => out_vect := "01001100";
481
          when 'M' => out_vect := "01001101";
482
          when 'N' => out_vect := "01001110";
483
          when 'O' => out_vect := "01001111";
484
          when 'P' => out_vect := "01010000";
485
          when 'Q' => out_vect := "01010001";
486
          when 'R' => out_vect := "01010010";
487
          when 'S' => out_vect := "01010011";
488
          when 'T' => out_vect := "01010100";
489
          when 'U' => out_vect := "01010101";
490
          when 'V' => out_vect := "01010110";
491
          when 'W' => out_vect := "01010111";
492
          when 'X' => out_vect := "01011000";
493
          when 'Y' => out_vect := "01011001";
494
          when 'Z' => out_vect := "01011010";
495
          when '[' => out_vect := "01011011";
496
          when '\' => out_vect := "01011100";
497
          when ']' => out_vect := "01011101";
498
          when '^' => out_vect := "01011110";
499
          when '_' => out_vect := "01011111";
500
          when '`' => out_vect := "01100000";
501
          when 'a' => out_vect := "01100001";
502
          when 'b' => out_vect := "01100010";
503
          when 'c' => out_vect := "01100011";
504
          when 'd' => out_vect := "01100100";
505
          when 'e' => out_vect := "01100101";
506
          when 'f' => out_vect := "01100110";
507
          when 'g' => out_vect := "01100111";
508
          when 'h' => out_vect := "01101000";
509
          when 'i' => out_vect := "01101001";
510
          when 'j' => out_vect := "01101010";
511
          when 'k' => out_vect := "01101011";
512
          when 'l' => out_vect := "01101100";
513
          when 'm' => out_vect := "01101101";
514
          when 'n' => out_vect := "01101110";
515
          when 'o' => out_vect := "01101111";
516
          when 'p' => out_vect := "01110000";
517
          when 'q' => out_vect := "01110001";
518
          when 'r' => out_vect := "01110010";
519
          when 's' => out_vect := "01110011";
520
          when 't' => out_vect := "01110100";
521
          when 'u' => out_vect := "01110101";
522
          when 'v' => out_vect := "01110110";
523
          when 'w' => out_vect := "01110111";
524
          when 'x' => out_vect := "01111000";
525
          when 'y' => out_vect := "01111001";
526
          when 'z' => out_vect := "01111010";
527
          when '{' => out_vect := "01111011";
528
          when '|' => out_vect := "01111100";
529
          when '}' => out_vect := "01111101";
530
          when '~' => out_vect := "01111110";
531
          when others =>
532
             out_vect := "00101010";
533
             --assert false report " illegal char to unsigned" severity note;
534
      end case;
535
      return out_vect;
536
     end;
537
 
538
------------------------------------------------------------------------------------
539
--
540
------------------------------------------------------------------------------------
541
    function char2slv (in_char : character) return std_logic_vector is
542
 
543
    variable out_vec : std_logic_vector(3 downto 0);
544
 
545
    begin
546
      case in_char is
547
        when '0' => out_vec := "0000";
548
        when '1' => out_vec := "0001";
549
        when '2' => out_vec := "0010";
550
        when '3' => out_vec := "0011";
551
        when '4' => out_vec := "0100";
552
        when '5' => out_vec := "0101";
553
        when '6' => out_vec := "0110";
554
        when '7' => out_vec := "0111";
555
        when '8' => out_vec := "1000";
556
        when '9' => out_vec := "1001";
557
        when 'A' | 'a' => out_vec := "1010";
558
        when 'B' | 'b' => out_vec := "1011";
559
        when 'C' | 'c' => out_vec := "1100";
560
        when 'D' | 'd' => out_vec := "1101";
561
        when 'E' | 'e' => out_vec := "1110";
562
        when 'F' | 'f' => out_vec := "1111";
563
        when others =>
564
          out_vec := "0000";
565
      end case;
566
      return out_vec;
567
    end char2slv;
568
 
569
------------------------------------------------------------------------------------
570
--
571
------------------------------------------------------------------------------------
572
    function char2u(in_char : character) return unsigned is
573
 
574
    variable out_vec : unsigned(3 downto 0);
575
 
576
    begin
577
      case in_char is
578
        when '0' => out_vec := "0000";
579
        when '1' => out_vec := "0001";
580
        when '2' => out_vec := "0010";
581
        when '3' => out_vec := "0011";
582
        when '4' => out_vec := "0100";
583
        when '5' => out_vec := "0101";
584
        when '6' => out_vec := "0110";
585
        when '7' => out_vec := "0111";
586
        when '8' => out_vec := "1000";
587
        when '9' => out_vec := "1001";
588
        when 'A' | 'a' => out_vec := "1010";
589
        when 'B' | 'b' => out_vec := "1011";
590
        when 'C' | 'c' => out_vec := "1100";
591
        when 'D' | 'd' => out_vec := "1101";
592
        when 'E' | 'e' => out_vec := "1110";
593
        when 'F' | 'f' => out_vec := "1111";
594
        when others =>
595
          out_vec := "0000";
596
      end case;
597
      return out_vec;
598
    end char2u;
599
 
600
------------------------------------------------------------------------------------
601
--
602
------------------------------------------------------------------------------------
603
 
604
    function hex_to_ascii(in_vector : std_logic_vector; nibbles : natural) return string is
605
    variable out_string : string(1 to nibbles);
606
    variable temp_in_vector : std_logic_vector(4*nibbles-1 downto 0);
607
    variable vector     : std_logic_vector(3 downto 0);
608
 
609
    begin
610
    temp_in_vector := in_vector(in_vector'length-1 downto in_vector'length-temp_in_vector'length);
611
        for i in 1 to nibbles loop
612
            vector := temp_in_vector((4*(nibbles-i)+3) downto 4*(nibbles-i));
613
            case vector is
614
                when "0000" => out_string(i) := '0';
615
                when "0001" => out_string(i) := '1';
616
                when "0010" => out_string(i) := '2';
617
                when "0011" => out_string(i) := '3';
618
                when "0100" => out_string(i) := '4';
619
                when "0101" => out_string(i) := '5';
620
                when "0110" => out_string(i) := '6';
621
                when "0111" => out_string(i) := '7';
622
                when "1000" => out_string(i) := '8';
623
                when "1001" => out_string(i) := '9';
624
                when "1010" => out_string(i) := 'A';
625
                when "1011" => out_string(i) := 'B';
626
                when "1100" => out_string(i) := 'C';
627
                when "1101" => out_string(i) := 'D';
628
                when "1110" => out_string(i) := 'E';
629
                when "1111" => out_string(i) := 'F';
630
                when others =>
631
                   out_string(i) := 'J';
632
                   assert false
633
                   report " illegal std_logic_vector to string"
634
                   severity note;
635
            end case;
636
        end loop;
637
        return out_string;
638
     end;
639
 
640
------------------------------------------------------------------------------------
641
--
642
------------------------------------------------------------------------------------
643
 
644
    function u2ascii(in_vector : unsigned; nibbles : natural) return string is
645
    variable out_string     : string(1 to nibbles);
646
    variable temp_in_vector : unsigned(4*nibbles-1 downto 0);
647
    variable vector         : unsigned(3 downto 0);
648
 
649
    begin
650
    temp_in_vector := in_vector(in_vector'length-1 downto in_vector'length-temp_in_vector'length);
651
        for i in 1 to nibbles loop
652
            vector := temp_in_vector((4*(nibbles-i)+3) downto 4*(nibbles-i));
653
            case vector is
654
                when "0000" => out_string(i) := '0';
655
                when "0001" => out_string(i) := '1';
656
                when "0010" => out_string(i) := '2';
657
                when "0011" => out_string(i) := '3';
658
                when "0100" => out_string(i) := '4';
659
                when "0101" => out_string(i) := '5';
660
                when "0110" => out_string(i) := '6';
661
                when "0111" => out_string(i) := '7';
662
                when "1000" => out_string(i) := '8';
663
                when "1001" => out_string(i) := '9';
664
                when "1010" => out_string(i) := 'A';
665
                when "1011" => out_string(i) := 'B';
666
                when "1100" => out_string(i) := 'C';
667
                when "1101" => out_string(i) := 'D';
668
                when "1110" => out_string(i) := 'E';
669
                when "1111" => out_string(i) := 'F';
670
                when others =>
671
                   out_string(i) := 'U';
672
                   assert false report " illegal unsigned to string" severity note;
673
            end case;
674
        end loop;
675
        return out_string;
676
     end;
677
 
678
------------------------------------------------------------------------------------
679
--
680
------------------------------------------------------------------------------------
681
 
682
    function hex_data_wrd(in_vector : std_logic_vector) return string is
683
    variable out_string : string(1 to 8);
684
    variable temp_in_vector : std_logic_vector(31 downto 0);
685
    variable vector     : std_logic_vector(3 downto 0);
686
 
687
    begin
688
    temp_in_vector := in_vector;
689
        for i in 1 to 8 loop
690
            vector := temp_in_vector((35-(4*i)) downto (32-(4*i)));
691
            case vector is
692
                when "0000" => out_string(i) := '0';
693
                when "0001" => out_string(i) := '1';
694
                when "0010" => out_string(i) := '2';
695
                when "0011" => out_string(i) := '3';
696
                when "0100" => out_string(i) := '4';
697
                when "0101" => out_string(i) := '5';
698
                when "0110" => out_string(i) := '6';
699
                when "0111" => out_string(i) := '7';
700
                when "1000" => out_string(i) := '8';
701
                when "1001" => out_string(i) := '9';
702
                when "1010" => out_string(i) := 'A';
703
                when "1011" => out_string(i) := 'B';
704
                when "1100" => out_string(i) := 'C';
705
                when "1101" => out_string(i) := 'D';
706
                when "1110" => out_string(i) := 'E';
707
                when "1111" => out_string(i) := 'F';
708
                when others =>
709
                   out_string(i) := 'J';
710
                   assert false
711
                   report " illegal std_logic_vector to string"
712
                   severity note;
713
            end case;
714
        end loop;
715
        return out_string;
716
     end;
717
 
718
------------------------------------------------------------------------------------
719
--
720
------------------------------------------------------------------------------------
721
 
722
    function hex_data_dblwrd(in_vector : std_logic_vector) return string is
723
    variable out_string : string(1 to 16);
724
    variable temp_in_vector : std_logic_vector(63 downto 0);
725
    variable vector     : std_logic_vector(3 downto 0);
726
 
727
    begin
728
    temp_in_vector := in_vector;
729
        for i in 1 to 16 loop
730
            vector := temp_in_vector((67-(4*i)) downto (64-(4*i)));
731
            case vector is
732
                when "0000" => out_string(i) := '0';
733
                when "0001" => out_string(i) := '1';
734
                when "0010" => out_string(i) := '2';
735
                when "0011" => out_string(i) := '3';
736
                when "0100" => out_string(i) := '4';
737
                when "0101" => out_string(i) := '5';
738
                when "0110" => out_string(i) := '6';
739
                when "0111" => out_string(i) := '7';
740
                when "1000" => out_string(i) := '8';
741
                when "1001" => out_string(i) := '9';
742
                when "1010" => out_string(i) := 'A';
743
                when "1011" => out_string(i) := 'B';
744
                when "1100" => out_string(i) := 'C';
745
                when "1101" => out_string(i) := 'D';
746
                when "1110" => out_string(i) := 'E';
747
                when "1111" => out_string(i) := 'F';
748
                when others =>
749
                   out_string(i) := 'J';
750
                   assert false
751
                   report " illegal std_logic_vector to string"
752
                   severity note;
753
            end case;
754
        end loop;
755
        return out_string;
756
     end;
757
 
758
------------------------------------------------------------------------------------
759
--
760
------------------------------------------------------------------------------------
761
 
762
    function hex_data_dblwrdz(in_vector : std_logic_vector) return string is
763
    variable out_string : string(1 to 16);
764
    variable temp_in_vector : std_logic_vector(63 downto 0);
765
    variable vector     : std_logic_vector(3 downto 0);
766
 
767
    begin
768
    temp_in_vector := in_vector;
769
        for i in 1 to 16 loop
770
            vector := temp_in_vector((67-(4*i)) downto (64-(4*i)));
771
            case vector is
772
                when "0000" => out_string(i) := '0';
773
                when "0001" => out_string(i) := '1';
774
                when "0010" => out_string(i) := '2';
775
                when "0011" => out_string(i) := '3';
776
                when "0100" => out_string(i) := '4';
777
                when "0101" => out_string(i) := '5';
778
                when "0110" => out_string(i) := '6';
779
                when "0111" => out_string(i) := '7';
780
                when "1000" => out_string(i) := '8';
781
                when "1001" => out_string(i) := '9';
782
                when "1010" => out_string(i) := 'A';
783
                when "1011" => out_string(i) := 'B';
784
                when "1100" => out_string(i) := 'C';
785
                when "1101" => out_string(i) := 'D';
786
                when "1110" => out_string(i) := 'E';
787
                when "1111" => out_string(i) := 'F';
788
                when "ZZZZ" => out_string(i) := 'Z';
789
                when others =>
790
                   out_string(i) := 'J';
791
                   assert false
792
                   report " illegal std_logic_vector to string"
793
                   severity note;
794
            end case;
795
        end loop;
796
        return out_string;
797
     end;
798
 
799
------------------------------------------------------------------------------------
800
--
801
------------------------------------------------------------------------------------
802
    -- returns true if the character is a valid hexadecimal character.
803
    function is_hex(c : character) return boolean is
804
    begin
805
      case c is
806
        when '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
807
             'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' |
808
             'e' | 'f' =>
809
          return(true);
810
        when others =>
811
          return(false);
812
      end case;
813
    end is_hex;
814
 
815
------------------------------------------------------------------------------------
816
--
817
------------------------------------------------------------------------------------
818
    -- returns true if the character is a valid hexadecimal character.
819
    function is_std_logic(c : character) return boolean is
820
    begin
821
      case c is
822
        when '0' | '1' | 'u' | 'U' | 'x' | 'X' | 'z' | 'Z' =>
823
          return(true);
824
        when others =>
825
          return(false);
826
      end case;
827
    end is_std_logic;
828
 
829
------------------------------------------------------------------------------------
830
--
831
------------------------------------------------------------------------------------
832
    -- returns true if the character is whitespace.
833
    function is_space(c : character) return boolean is
834
    begin
835
      case c is
836
        when ' ' | HT =>
837
          return(true);
838
        when others =>
839
          return(false);
840
      end case;
841
    end is_space;
842
 
843
------------------------------------------------------------------------------------
844
--
845
------------------------------------------------------------------------------------
846
 
847
    function char2sl(in_char : character) return std_logic is
848
 
849
    variable out_bit : std_logic;
850
 
851
    begin
852
 
853
        if (in_char = '1') then
854
            out_bit := '1';
855
        elsif (in_char = '0') then
856
            out_bit := '0';
857
        elsif (in_char = 'x') then
858
            out_bit := 'X';
859
        elsif (in_char = 'X') then
860
            out_bit := 'X';
861
        elsif (in_char = 'u') then
862
            out_bit := 'U';
863
        elsif (in_char = 'U') then
864
            out_bit := 'U';
865
        elsif (in_char = 'z') then
866
            out_bit := 'Z';
867
        elsif (in_char = 'Z') then
868
            out_bit := 'Z';
869
        else
870
            assert false
871
            report "illegal character to std_logic"
872
            severity note;
873
        end if;
874
 
875
-- Mysteriously, the following code did not work in place of the
876
-- above chain of if-elsif-else logic... it seemed to always return '1'.
877
-- I cannot tell why. -- John Clayton
878
--        case in_char is
879
--          when '1' =>
880
--            out_bit:= '1';
881
--          when '0' =>
882
--            out_bit:= '1';
883
--          when 'u' | 'U' =>
884
--            out_bit:= 'U';
885
--          when 'x' | 'X' =>
886
--            out_bit:= 'X';
887
--          when 'z' | 'Z' =>
888
--            out_bit:= 'Z';
889
--          when others =>
890
--            assert false
891
--            report "illegal character to std_logic"
892
--            severity note;
893
--        end case;
894
 
895
        return out_bit;
896
    end char2sl;
897
 
898
 
899
 
900
------------------------------------------------------------------------------------
901
-- Converts Standard Logic Vectors to Unsigned
902
------------------------------------------------------------------------------------
903
 
904
  function slv2u(in_a : std_logic_vector) return unsigned is
905
  variable i : natural;
906
  variable o : unsigned(in_a'length-1 downto 0);
907
 
908
  begin
909
 
910
  o := (others=>'0');
911
  for i in 0 to in_a'length-1 loop
912
      o(i) := in_a(i);
913
  end loop;
914
 
915
  return(o);
916
 
917
  end;
918
 
919
------------------------------------------------------------------------------------
920
-- Converts Unsigned to Standard Logic Vector
921
------------------------------------------------------------------------------------
922
 
923
  function u2slv(in_a : unsigned) return std_logic_vector is
924
  variable i : natural;
925
  variable o : std_logic_vector(in_a'length-1 downto 0);
926
 
927
  begin
928
 
929
  o := (others=>'0');
930
  for i in 0 to in_a'length-1 loop
931
      o(i) := in_a(i);
932
  end loop;
933
 
934
  return(o);
935
 
936
  end;
937
 
938
------------------------------------------------------------------------------------
939
-- Converts Standard Logic Vectors to Signed
940
------------------------------------------------------------------------------------
941
 
942
  function slv2s(in_a : std_logic_vector) return signed is
943
  variable i : natural;
944
  variable o : signed(in_a'length-1 downto 0);
945
 
946
  begin
947
 
948
  o := (others=>'0');
949
  for i in 0 to in_a'length-1 loop
950
      o(i) := in_a(i);
951
  end loop;
952
 
953
  return(o);
954
 
955
  end;
956
 
957
------------------------------------------------------------------------------------
958
-- Converts Signed to Standard Logic Vector
959
------------------------------------------------------------------------------------
960
 
961
  function s2slv(in_a : signed) return std_logic_vector is
962
  variable i : natural;
963
  variable o : std_logic_vector(in_a'length-1 downto 0);
964
 
965
  begin
966
 
967
  o := (others=>'0');
968
  for i in 0 to in_a'length-1 loop
969
      o(i) := in_a(i);
970
  end loop;
971
 
972
  return(o);
973
 
974
  end;
975
 
976
------------------------------------------------------------------------------------
977
-- Resizes Standard Logic Vectors, "right justified" i.e. starts at LSB...
978
------------------------------------------------------------------------------------
979
 
980
  function slv_resize(in_vect : std_logic_vector; out_size : integer) return std_logic_vector is
981
  variable i      : integer;
982
  variable o_vect : std_logic_vector(out_size-1 downto 0);
983
 
984
  begin
985
 
986
  o_vect := (others=>'0');
987
  for i in 0 to in_vect'length-1 loop
988
    if (i<out_size) then
989
      o_vect(i) := in_vect(i);
990
    end if;
991
  end loop;
992
 
993
  return(o_vect);
994
 
995
  end slv_resize;
996
 
997
------------------------------------------------------------------------------------
998
-- Resizes Standard Logic Vectors, "left justified" i.e. starts at MSB...
999
------------------------------------------------------------------------------------
1000
 
1001
  function slv_resize_l(in_vect : std_logic_vector; out_size : integer) return std_logic_vector is
1002
  variable i      : integer;
1003
  variable j      : integer;
1004
  variable o_vect : std_logic_vector(out_size-1 downto 0);
1005
 
1006
  begin
1007
 
1008
  o_vect := (others=>'0');
1009
  j := out_size-1;
1010
  for i in in_vect'length-1 downto 0 loop
1011
    if (j>=0) then
1012
      o_vect(j) := in_vect(i);
1013
      j := j-1;
1014
    end if;
1015
  end loop;
1016
 
1017
  return(o_vect);
1018
 
1019
  end slv_resize_l;
1020
 
1021
------------------------------------------------------------------------------------
1022
-- Resizes Standard Logic Vectors, "right justified with sign extension"
1023
------------------------------------------------------------------------------------
1024
 
1025
  function slv_resize_se(in_vect : std_logic_vector; out_size : integer) return std_logic_vector is
1026
  variable i      : integer;
1027
  variable o_vect : std_logic_vector(out_size-1 downto 0);
1028
 
1029
  begin
1030
 
1031
  o_vect := (others=>in_vect(in_vect'length-1));
1032
  for i in 0 to in_vect'length-1 loop
1033
    if (i<out_size) then
1034
      o_vect(i) := in_vect(i);
1035
    end if;
1036
  end loop;
1037
 
1038
  return(o_vect);
1039
 
1040
  end slv_resize_se;
1041
 
1042
------------------------------------------------------------------------------------
1043
-- Resizes Signed, "right justified" i.e. starts at LSB...
1044
------------------------------------------------------------------------------------
1045
 
1046
  function s_resize(in_vect : signed; out_size : integer) return signed is
1047
  variable i      : integer;
1048
  variable o_vect : signed(out_size-1 downto 0);
1049
 
1050
  begin
1051
 
1052
  o_vect := (others=>'0');
1053
  for i in 0 to in_vect'length-1 loop
1054
    if (i<out_size) then
1055
      o_vect(i) := in_vect(i);
1056
    end if;
1057
  end loop;
1058
 
1059
  return(o_vect);
1060
 
1061
  end s_resize;
1062
 
1063
------------------------------------------------------------------------------------
1064
-- Resizes Signed, "left justified" i.e. starts at MSB...
1065
------------------------------------------------------------------------------------
1066
 
1067
  function s_resize_l(in_vect : signed; out_size : integer) return signed is
1068
  variable i      : integer;
1069
  variable j      : integer;
1070
  variable o_vect : signed(out_size-1 downto 0);
1071
 
1072
  begin
1073
 
1074
  o_vect := (others=>'0');
1075
  j := out_size-1;
1076
  for i in in_vect'length-1 downto 0 loop
1077
    if (j>=0) then
1078
      o_vect(j) := in_vect(i);
1079
      j := j-1;
1080
    end if;
1081
  end loop;
1082
 
1083
  return(o_vect);
1084
 
1085
  end s_resize_l;
1086
 
1087
------------------------------------------------------------------------------------
1088
-- Resizes Signed, "right justified with sign extension"
1089
------------------------------------------------------------------------------------
1090
 
1091
  function s_resize_se(in_vect : signed; out_size : integer) return signed is
1092
  variable i      : integer;
1093
  variable o_vect : signed(out_size-1 downto 0);
1094
 
1095
  begin
1096
 
1097
  o_vect := (others=>in_vect(in_vect'length-1));
1098
  for i in 0 to in_vect'length-1 loop
1099
    if (i<out_size) then
1100
      o_vect(i) := in_vect(i);
1101
    end if;
1102
  end loop;
1103
 
1104
  return(o_vect);
1105
 
1106
  end s_resize_se;
1107
 
1108
------------------------------------------------------------------------------------
1109
-- Resizes Unsigned, "right justified" i.e. starts at LSB...
1110
------------------------------------------------------------------------------------
1111
 
1112
  function u_resize(in_vect : unsigned; out_size : integer) return unsigned is
1113
  variable i      : integer;
1114
  variable i_vect : unsigned(in_vect'length-1 downto 0);
1115
  variable o_vect : unsigned(out_size-1 downto 0);
1116
 
1117
  begin
1118
  i_vect := in_vect;
1119
  o_vect := (others=>'0');
1120
  for i in 0 to in_vect'length-1 loop
1121
    if (i<out_size) then
1122
      o_vect(i) := i_vect(i);
1123
    end if;
1124
  end loop;
1125
 
1126
  return(o_vect);
1127
 
1128
  end u_resize;
1129
 
1130
------------------------------------------------------------------------------------
1131
-- Resizes Unsigned, "left justified" i.e. starts at MSB...
1132
------------------------------------------------------------------------------------
1133
 
1134
  function u_resize_l(in_vect : unsigned; out_size : integer) return unsigned is
1135
  variable i      : integer;
1136
  variable j      : integer;
1137
  variable o_vect : unsigned(out_size-1 downto 0);
1138
 
1139
  begin
1140
 
1141
  o_vect := (others=>'0');
1142
  j := out_size-1;
1143
  for i in in_vect'length-1 downto 0 loop
1144
    if (j>=0) then
1145
      o_vect(j) := in_vect(i);
1146
      j := j-1;
1147
    end if;
1148
  end loop;
1149
 
1150
  return(o_vect);
1151
 
1152
  end u_resize_l;
1153
 
1154
------------------------------------------------------------------------------------
1155
-- Selects a slice of the input vector, "right justified" i.e. slice 0 starts at LSB...
1156
------------------------------------------------------------------------------------
1157
 
1158
  function u_select(in_vect : unsigned; slice_num : integer; slice_size : integer) return unsigned is
1159
  variable i      : integer;
1160
  variable o_vect : unsigned(slice_size-1 downto 0);
1161
 
1162
  begin
1163
  o_vect := (others=>'0');
1164
  for i in 0 to o_vect'length-1 loop
1165
    o_vect(i) := in_vect(i+slice_num*slice_size);
1166
  end loop;
1167
 
1168
  return(o_vect);
1169
 
1170
  end u_select;
1171
 
1172
------------------------------------------------------------------------------------
1173
-- Bit Reverses the input vector
1174
------------------------------------------------------------------------------------
1175
 
1176
  function u_reverse(in_vect : unsigned) return unsigned is
1177
  variable i      : integer;
1178
  variable o_vect : unsigned(in_vect'length-1 downto 0);
1179
 
1180
  begin
1181
 
1182
  for i in in_vect'length-1 downto 0 loop
1183
    o_vect(in_vect'length-1-i) := in_vect(i);
1184
  end loop;
1185
 
1186
  return(o_vect);
1187
 
1188
  end u_reverse;
1189
 
1190
------------------------------------------------------------------------------------
1191
--
1192
------------------------------------------------------------------------------------
1193
 
1194
    function str2u(in_string : string; out_size:integer) return unsigned is
1195
 
1196
    variable uval   : unsigned(out_size-1 downto 0);
1197
    variable nibble : unsigned(3 downto 0);
1198
    begin
1199
      uval := (others=>'0');
1200
      for j in in_string'range loop
1201
        uval(uval'length-1 downto 4) := uval(uval'length-5 downto 0);
1202
        uval(3 downto 0) := char2u(in_string(j));
1203
      end loop;
1204
      return uval;
1205
    end str2u;
1206
 
1207
------------------------------------------------------------------------------------
1208
--
1209
------------------------------------------------------------------------------------
1210
 
1211
    function str2s(in_string : string; out_size:integer) return signed is
1212
 
1213
    variable uval   : signed(out_size-1 downto 0);
1214
    variable nibble : signed(3 downto 0);
1215
    begin
1216
      uval := (others=>'0');
1217
      for j in in_string'range loop
1218
        uval(uval'length-1 downto 4) := uval(uval'length-5 downto 0);
1219
        uval(3 downto 0) := signed(char2u(in_string(j)));
1220
      end loop;
1221
      return uval;
1222
    end str2s;
1223
 
1224
------------------------------------------------------------------------------------
1225
-- Power Function for naturals
1226
------------------------------------------------------------------------------------
1227
 
1228
--  function "**"(in_a : natural; in_b : natural) return natural is
1229
--  variable i : natural;
1230
--  variable o : natural;
1231
--
1232
--  begin
1233
--
1234
--  -- Coded with a for loop: works in simulation, but Synplify will not synthesize.
1235
----  if (in_b=0) then
1236
----    o := 1;
1237
----  else
1238
----    o := in_a;
1239
----    if (in_b>1) then
1240
----      for i in 2 to in_b loop
1241
----        o := o * in_a;
1242
----      end loop;
1243
----    end if;
1244
----  end if;
1245
----  return(o);
1246
--
1247
--  if (in_b=0) then
1248
--    o := 1;
1249
--  else
1250
--    o := in_a;
1251
--    i := 1;
1252
--    while (i<in_b) loop
1253
--      o := o * in_a;
1254
--      i := i+1;
1255
--    end loop;
1256
--  end if;
1257
--  return(o);
1258
--
1259
--  end;
1260
 
1261
------------------------------------------------------------------------------------
1262
-- Function for 2^(natural)
1263
------------------------------------------------------------------------------------
1264
 
1265
  function pow_2_u(in_a : natural; out_size:integer) return unsigned is
1266
  variable i : natural;
1267
  variable j : natural;
1268
  variable o : unsigned(out_size-1 downto 0);
1269
 
1270
  begin
1271
 
1272
  j := in_a;
1273
  o := to_unsigned(1,o'length);
1274
  for i in 0 to out_size-1 loop
1275
    if (j>0) then
1276
      o := o(out_size-2 downto 0) & '0';
1277
      j := j-1;
1278
    end if;
1279
  end loop;
1280
  return(o);
1281
 
1282
  end;
1283
 
1284
------------------------------------------------------------------------------------
1285
-- A sort of "barrel shifter."  Produces the ASR by in_a of the input...
1286
------------------------------------------------------------------------------------
1287
 
1288
  function asr_function(in_vect : signed; in_a : natural) return signed is
1289
  variable i      : natural;
1290
  variable j      : natural;
1291
  variable o_vect : signed(in_vect'length-1 downto 0);
1292
 
1293
  begin
1294
 
1295
  o_vect := in_vect;
1296
  j := in_a;
1297
  for i in 0 to in_vect'length-1 loop -- Now loop to fill in the actual results
1298
    if (j>0) then
1299
      o_vect := o_vect(o_vect'length-1) & o_vect(o_vect'length-1 downto 1);
1300
      j := j-1;
1301
    end if;
1302
  end loop;
1303
 
1304
  return(o_vect);
1305
 
1306
  end asr_function;
1307
 
1308
------------------------------------------------------------------------------------
1309
--
1310
------------------------------------------------------------------------------------
1311
 
1312
    function bit_width (maxval : integer) return integer is
1313
 
1314
    variable w : integer;
1315
    begin
1316
      if (maxval<2) then
1317
        w := 1;
1318
      else
1319
        w := integer(ceil(log2(real(maxval))));
1320
      end if;
1321
 
1322
      return  w;
1323
    end bit_width;
1324
 
1325
------------------------------------------------------------------------------------
1326
--
1327
------------------------------------------------------------------------------------
1328
 
1329
    function bit_width (maxval : real) return integer is
1330
 
1331
    variable w : integer;
1332
    begin
1333
      if (maxval<2.0) then
1334
        w := 1;
1335
      else
1336
        w := integer(ceil(log2(maxval)));
1337
      end if;
1338
 
1339
      return  w;
1340
    end bit_width;
1341
 
1342
------------------------------------------------------------------------------------
1343
-- Timer width differs from bit width in the following way:
1344
--   Bit width gives a vector large enough to have maxval different states, but
1345
--   timer width gives a vector large enough to hold the quantity maxval.
1346
--
1347
-- The difference is critical when using timers, since they often count down from
1348
-- the initial value, and trigger timeout at a value of 1...  So for maxval equal
1349
-- to a power of two, an extra bit must be reserved.  This is done by adding one
1350
-- to the maxval input...
1351
------------------------------------------------------------------------------------
1352
 
1353
    function timer_width (maxval : integer) return integer is
1354
 
1355
    variable w : integer;
1356
    begin
1357
      if (maxval<2) then
1358
        w := 1;
1359
      else
1360
        w := integer(ceil(log2(real(maxval+1))));
1361
      end if;
1362
 
1363
      return  w;
1364
    end timer_width;
1365
 
1366
------------------------------------------------------------------------------------
1367
--
1368
------------------------------------------------------------------------------------
1369
 
1370
    function timer_width (maxval : real) return integer is
1371
 
1372
    variable w : integer;
1373
    begin
1374
      if (maxval<2.0) then
1375
        w := 1;
1376
      else
1377
        w := integer(ceil(log2(maxval+1.0)));
1378
      end if;
1379
 
1380
      return  w;
1381
    end timer_width;
1382
 
1383
------------------------------------------------------------------------------------
1384
--
1385
------------------------------------------------------------------------------------
1386
 
1387
    function num_words (num_bits : integer; bits_per_word : integer) return integer is
1388
 
1389
    variable w : integer;
1390
    begin
1391
      if (num_bits mod bits_per_word /= 0) then
1392
        w := integer(ceil(real(num_bits) / real(bits_per_word)));
1393
      else
1394
        w := integer(floor(real(num_bits) / real(bits_per_word)));
1395
      end if;
1396
      return  w;
1397
    end num_words;
1398
 
1399
------------------------------------------------------------------------------------
1400
--
1401
------------------------------------------------------------------------------------
1402
 
1403
function asciichar2u2(d:character) return unsigned is
1404
  variable dout : unsigned(0 to 7);
1405
  variable ascii_int : integer := 0;
1406
  variable val : integer := 0;
1407
begin
1408
  -- Get integer value of the character
1409
  ascii_int := character'pos(d);
1410
  for index in dout'range loop
1411
    val := ascii_int rem 2;
1412
    ascii_int := ascii_int/2;
1413
    if val=0 then
1414
      dout(dout'high-index):='0';
1415
    else
1416
      dout(dout'high-index):='1';
1417
    end if;
1418
  end loop;
1419
 
1420
  return dout;
1421
end asciichar2u2;
1422
 
1423
------------------------------------------------------------------------------------
1424
--
1425
------------------------------------------------------------------------------------
1426
 
1427
-- converts a string into std_logic_vector
1428
 
1429
function str2u(s: string) return unsigned is
1430
  variable uv: unsigned(8*s'high-1 downto 0);
1431
  variable k: integer;
1432
begin
1433
   k := s'high-s'low;
1434
  for i in s'range loop
1435
--    uv(8*k+7 downto 8*k) := unsigned(chartobyte(s(i)));
1436
    uv(8*k+7 downto 8*k) := asciichar2u2(s(i));
1437
    k      := k - 1;
1438
  end loop;
1439
  return uv;
1440
end str2u;
1441
 
1442
------------------------------------------------------------------------------------
1443
-- Counts the number of ones in an unsigned input
1444
------------------------------------------------------------------------------------
1445
 
1446
  function u_ones(in_a : unsigned) return natural is
1447
  variable i,c : natural;
1448
 
1449
  begin
1450
  c := 0;
1451
  for i in 0 to in_a'length-1 loop
1452
    if (in_a(i)='1') then
1453
      c := c+1;
1454
    end if;
1455
  end loop;
1456
 
1457
  return(c);
1458
 
1459
  end;
1460
 
1461
------------------------------------------------------------------------------------
1462
--
1463
------------------------------------------------------------------------------------
1464
 
1465
end convert_pack;
1466
 
1467
 
1468
 

powered by: WebSVN 2.1.0

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