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

Subversion Repositories sd_mmc_emulator

[/] [sd_mmc_emulator/] [trunk/] [rtl/] [convert_pack.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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