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

Subversion Repositories mips_enhanced

[/] [mips_enhanced/] [trunk/] [grlib-gpl-1.0.19-b3188/] [lib/] [fmf/] [utilities/] [conversions.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dimamali
--------------------------------------------------------------------------------
2
--  File Name: conversions_p.vhd
3
--------------------------------------------------------------------------------
4
--  Copyright (C) 1997, 1998, 2001 Free Model Foundry; http://eda.org/fmf/
5
-- 
6
--  This program is free software; you can redistribute it and/or modify
7
--  it under the terms of the GNU General Public License version 2 as
8
--  published by the Free Software Foundation.
9
-- 
10
--  This package was written by SEVA Technologies, Inc. and donated to the FMF.
11
--  www.seva.com
12
--
13
--  MODIFICATION HISTORY:
14
-- 
15
--  version: |  author:  | mod date: | changes made:
16
--    V1.0     R. Steele   97 DEC 05   Added header and formatting to SEVA file
17
--    V1.1     R. Munden   98 NOV 28   Corrected some comments
18
--                                     Corrected function b
19
--    V1.2     R. Munden   01 MAY 27   Corrected function to_nat for weak values
20
--                                     and combined into a single file
21
-- 
22
--------------------------------------------------------------------------------
23
 
24
LIBRARY IEEE;   USE IEEE.std_logic_1164.ALL;
25
 
26
--------------------------------------------------------------------------------
27
--  CONVERSION FUNCTION SELECTION TABLES
28
--------------------------------------------------------------------------------
29
-- 
30
--  FROM         TO: std_logic_vector std_logic  natural     time     string
31
--  -----------------|---------------|---------|---------|---------|-----------
32
--  std_logic_vector |      N/A      |   N/A   |  to_nat | combine | see below
33
--  std_logic        |      N/A      |   N/A   |  to_nat | combine | see below
34
--  natural          |     to_slv    |  to_sl  |   N/A   | to_time | see below
35
--  time             |      N/A      |   N/A   |  to_nat | N/A     | to_time_str
36
--  hex string       |       h       |   N/A   |    h    | combine | N/A
37
--  decimal string   |       d       |   N/A   |    d    | combine | N/A
38
--  octal string     |       o       |   N/A   |    o    | combine | N/A
39
--  binary string    |       b       |   N/A   |    b    | combine | N/A
40
--  -----------------|---------------|---------|---------|---------|-----------
41
--
42
--  FROM           TO: hex string decimal string octal string  binary string 
43
--  -----------------|------------|-------------|------------|----------------
44
--  std_logic_vector | to_hex_str | to_int_str  | to_oct_str |  to_bin_str
45
--  std_logic        |    N/A     |    N/A      |    N/A     |  to_bin_str
46
--  natural          | to_hex_str | to_int_str  | to_oct_str |  to_bin_str
47
--  -----------------|------------|-------------|------------|----------------
48
-- 
49
--------------------------------------------------------------------------------
50
 
51
PACKAGE conversions IS
52
 
53
    ----------------------------------------------------------------------------
54
    -- the conversions in this package are not guaranteed to be synthesizable.
55
    --
56
    -- others functions available
57
    -- fill         creates a variable length string of the fill character
58
    --
59
    -- 
60
    --
61
    -- input parameters of type natural or integer can be in the form:
62
    --    normal              -> 8, 99, 4_237
63
    --    base#value#         -> 2#0101#, 16#fa4C#,  8#6_734#
64
    --    with exponents(x10) -> 8e4, 16#2e#E4
65
    --
66
    -- input parameters of type string can be in the form:
67
    --    "99", "4_237", "0101", "1010_1010"
68
    --
69
    -- for bit/bit_vector <-> std_logic/std_logic_vector conversions use 
70
    --   package std_logic_1164
71
    --     to_bit(std_logic)
72
    --     to_bitvector(std_logic_vector)
73
    --     to_stdlogic(bit)
74
    --     to_stdlogicvector(bit_vector)
75
    --
76
    -- for "synthesizable" signed/unsigned/std_logic_vector/integer
77
    -- conversions use
78
    --   package std_logic_arith 
79
    --     conv_integer(signed/unsigned)
80
    --     conv_unsigned(integer/signed,size)
81
    --     conv_signed(integer/unsigned,size)
82
    --     conv_std_logic_vector(integer/signed/unsigned,size)
83
    --
84
    -- for "synthesizable" std_logic_vector -> integer conversions use
85
    --   package std_logic_unsigned/std_logic_signed
86
    --            <these packages are mutually exclusive>
87
    --     conv_integer(std_logic_vector)
88
    --            <except for this conversion, these packages are unnecessary)
89
    --     to minimize compile problems write:
90
    --       use std_logic_unsigned.conv_integer;
91
    --       use std_logic_signed.conv_integer;
92
    --
93
    -- std_logic_vector, signed and unsigned types are "closely related"
94
    -- no type conversion functions are needed, use type casting or qualified
95
    -- expressions
96
    --
97
    --   type1(object of type2)          <type casting>
98
    --   type1'(expression of type2)     <qualified expression>
99
    --
100
    -- most conversions have 4 parmeters:
101
    --   x         : value to be converted 
102
    --   rtn_len   : size of the return value
103
    --   justify   : justify value 'left' or 'right', default is right
104
    --   basespec  : print the base of the value - 'yes'/'no', default is yes
105
    --
106
    -- Typical ways to call these functions:
107
    --     simple, all defaults used
108
    --     to_bin_str(x)
109
    --         x will be converted to a string of minimum size with a 
110
    --           base specification appended for clarity
111
    --         if x is 10101 then return is b"10101" 
112
    --
113
    --   to control size of return string
114
    --     to_hex_str(x,                
115
    --                6)                
116
    --         length of string returned will be 6 characters
117
    --         value will be right justified in the field  
118
    --         if x is 10101 then return is ....h"15"
119
    --          where '.' represents a blank 
120
    --          if 'rtn_len' parm defaults or is set to 0 then
121
    --           return string will always be minimum size
122
    --
123
    --   to left justify and suppress base specification
124
    --     to_int_str(x,
125
    --                6,
126
    --                justify => left, 
127
    --                basespec => yes)
128
    --         length of return string will be 6 characters
129
    --         the base specification will be suppressed
130
    --         if x is 10101 then return is 21.... 
131
    --           where '.' represents a blank 
132
    --
133
    -- other usage notes
134
    --
135
    --   if rtn_len less than or equal to x'length then ignore 
136
    --      rtn_len and return string of x'length
137
    --   the 'justify' parm is effectively ignored in this case
138
    --
139
    --   if rtn_len greater than x'length then return string 
140
    --      of rtn_len with blanks based on 'justify' parm
141
    --
142
    -- these routines do not handle negative numbers
143
    ----------------------------------------------------------------------------
144
 
145
    type justify_side is (left, right);
146
    type b_spec       is (no  , yes);
147
 
148
    -- std_logic_vector to binary string
149
    function to_bin_str(x          : std_logic_vector;
150
                        rtn_len    : natural      := 0;
151
                        justify    : justify_side := right;
152
                        basespec   : b_spec       := yes)
153
      return string;
154
 
155
    -- std_logic to binary string
156
    function to_bin_str(x          : std_logic;
157
                        rtn_len    : natural      := 0;
158
                        justify    : justify_side := right;
159
                        basespec   : b_spec       := yes)
160
      return string;
161
 
162
    -- natural to binary string
163
    function to_bin_str(x          : natural;
164
                        rtn_len    : natural      := 0;
165
                        justify    : justify_side := right;
166
                        basespec   : b_spec       := yes)
167
      return string;
168
      -- see note above regarding possible formats for x
169
 
170
    -- std_logic_vector to hex string
171
    function to_hex_str(x          : std_logic_vector;
172
                        rtn_len    : natural      := 0;
173
                        justify    : justify_side := right;
174
                        basespec   : b_spec       := yes)
175
      return string;
176
 
177
    -- natural to hex string
178
    function to_hex_str(x          : natural;
179
                        rtn_len    : natural      := 0;
180
                        justify    : justify_side := right;
181
                        basespec   : b_spec       := yes)
182
      return string;
183
      -- see note above regarding possible formats for x
184
 
185
    -- std_logic_vector to octal string
186
    function to_oct_str(x          : std_logic_vector;
187
                        rtn_len    : natural      := 0;
188
                        justify    : justify_side := right;
189
                        basespec   : b_spec       := yes)
190
      return string;
191
 
192
    -- natural to octal string
193
    function to_oct_str(x          : natural;
194
                        rtn_len    : natural      := 0;
195
                        justify    : justify_side := right;
196
                        basespec   : b_spec       := yes)
197
      return string;
198
      -- see note above regarding possible formats for x
199
 
200
    -- natural to integer string
201
    function to_int_str(x          : natural;
202
                        rtn_len    : natural      := 0;
203
                        justify    : justify_side := right;
204
                        basespec   : b_spec       := yes)
205
      return string;
206
      -- see note above regarding possible formats for x
207
 
208
    -- std_logic_vector to integer string
209
    function to_int_str(x          : std_logic_vector;
210
                        rtn_len    : natural      := 0;
211
                        justify    : justify_side := right;
212
                        basespec   : b_spec       := yes)
213
      return string;
214
 
215
    -- time to string
216
    function to_time_str (x          : time)
217
      return string;
218
 
219
    -- add characters to a string
220
    function fill        (fill_char  : character    := '*';
221
                          rtn_len    : integer      := 1)
222
      return string;
223
      -- usage:
224
        -- fill
225
          -- returns *
226
        -- fill(' ',10)    
227
          -- returns ..........  when '.' represents a blank
228
        -- fill(lf) or fill(ht)  
229
          -- returns line feed character or tab character respectively
230
 
231
    -- std_logic_vector to natural
232
    function to_nat      (x        : std_logic_vector)
233
      return natural;
234
 
235
    -- std_logic to natural
236
    function to_nat      (x        : std_logic)
237
      return natural;
238
 
239
    -- time to natural
240
    function to_nat      (x        : time)
241
      return natural;
242
 
243
    -- hex string to std_logic_vector
244
    function h         (x          : string;
245
                        rtn_len    : positive range 1 to 32 := 32)
246
      return std_logic_vector;
247
    -- if rtn_len is < than x'length*4, result will be truncated on the left
248
    -- if x is other than characters 0 to 9 or a,A to f,F 
249
    --   or x,X,z,Z,u,U,-,w,W, result will be 0
250
 
251
    -- decimal string to std_logic_vector
252
    function d         (x          : string;
253
                        rtn_len    : positive range 1 to 32 := 32)
254
      return std_logic_vector;
255
    -- if rtn_len is < than x'length*4, result will be truncated on the left
256
    -- if x is other than characters 0 to 9 or x,X,z,Z,u,U,-,w,W,
257
    --   result will be 0
258
 
259
    -- octal string to std_logic_vector
260
    function o         (x          : string;
261
                        rtn_len    : positive range 1 to 32 := 32)
262
      return std_logic_vector;
263
    -- if rtn_len is < than x'length*4, result will be truncated on the left
264
    -- if x is other than characters 0 to 7 or x,X,z,Z,u,U,-,w,W,
265
    --   result will be 0
266
 
267
    -- binary string to std_logic_vector
268
    function b         (x          : string;
269
                        rtn_len    : positive range 1 to 32 := 32)
270
      return std_logic_vector;
271
    -- if rtn_len is < than x'length*4, result will be truncated on the left
272
    -- if x is other than characters 0 to 1 or x,X,z,Z,u,U,-,w,W, 
273
    --   result will be 0
274
 
275
    -- hex string to natural
276
    function h         (x          : string)
277
      return natural;
278
    -- if x is other than characters 0 to 9 or a,A to f,F, result will be 0
279
 
280
    -- decimal string to natural
281
    function d         (x          : string)
282
      return natural;
283
    -- if x is other than characters 0 to 9, result will be 0
284
 
285
    -- octal string to natural
286
    function o         (x          : string)
287
      return natural;
288
    -- if x is other than characters 0 to 7, result will be 0
289
 
290
    -- binary string to natural
291
    function b         (x          : string)
292
      return natural;
293
    -- if x is other than characters 0 to 1, result will be 0
294
 
295
    -- natural to std_logic_vector
296
    function to_slv    (x          : natural;
297
                        rtn_len    : positive range 1 to 32 := 32)
298
      return std_logic_vector;
299
      -- if rtn_len is < than sizeof(x), result will be truncated on the left
300
      -- see note above regarding possible formats for x
301
 
302
    -- natural to std_logic
303
    function to_sl     (x          : natural)
304
      return std_logic;
305
 
306
    -- natural to time
307
    function to_time   (x          : natural)
308
      return time;
309
      -- see note above regarding possible formats for x
310
 
311
END conversions;
312
-- 
313
--------------------------------------------------------------------------------
314
-- 
315
 
316
PACKAGE BODY conversions IS
317
 
318
    -- private declarations for this package 
319
    type basetype     is (binary, octal, decimal, hex);
320
 
321
    function max(x,y: integer) return integer is
322
    begin
323
      if x > y then return x; else return y; end if;
324
    end max;
325
 
326
    function min(x,y: integer) return integer is
327
    begin
328
      if x < y then return x; else return y; end if;
329
    end min;
330
 
331
    -- consider function sizeof for string/slv/???, return natural
332
 
333
    --  function size(len: natural) return natural is
334
    --  begin
335
    --    if len=0 then
336
    --      return 31;
337
    --    else return len;
338
    --    end if;
339
    --  end size;
340
 
341
      function nextmultof (x    : positive;
342
                           size : positive) return positive is
343
      begin
344
        case x mod size is
345
          when 0      => return size * x/size;
346
          when others => return size * (x/size + 1);
347
        end case;
348
      end nextmultof;
349
 
350
      function rtn_base (base : basetype) return character is
351
      begin
352
        case base is
353
          when binary  => return 'b';
354
          when octal   => return 'o';
355
          when decimal => return 'd';
356
          when hex     => return 'h';
357
        end case;
358
      end rtn_base;
359
 
360
      function format (r          : string;
361
                       base       : basetype;
362
                       rtn_len    : natural     ;
363
                       justify    : justify_side;
364
                       basespec   : b_spec) return string is
365
        variable int_rtn_len : integer;
366
      begin
367
        if basespec=yes then
368
          int_rtn_len := rtn_len - 3;
369
        else
370
          int_rtn_len := rtn_len;
371
        end if;
372
        if int_rtn_len <= r'length then
373
          case basespec is
374
            when no  => return r ;
375
            when yes => return rtn_base(base) & '"' & r & '"';
376
          end case;
377
        else
378
          case justify is
379
            when left  =>
380
              case basespec is
381
                when no  =>
382
                  return r & fill(' ',int_rtn_len - r'length);
383
                when yes  =>
384
                  return rtn_base(base) & '"' & r & '"' &
385
                         fill(' ',int_rtn_len - r'length);
386
              end case;
387
            when right =>
388
              case basespec is
389
                when no  =>
390
                  return fill(' ',int_rtn_len - r'length) & r ;
391
                when yes  =>
392
                  return fill(' ',int_rtn_len - r'length) &
393
                         rtn_base(base) & '"' & r & '"';
394
              end case;
395
          end case;
396
        end if;
397
      end format;
398
 
399
      -- convert numeric string of any base to natural
400
      function cnvt_base (x      : string;
401
                          inbase : natural range 2 to 16) return natural is
402
        -- assumes x is an unsigned number string of base 'inbase'
403
        -- values larger than natural'high are not supported
404
        variable r,t   : natural  := 0;
405
        variable place : positive := 1;
406
      begin
407
        for i in x'reverse_range loop
408
          case x(i) is
409
            when '0'     => t := 0;
410
            when '1'     => t := 1;
411
            when '2'     => t := 2;
412
            when '3'     => t := 3;
413
            when '4'     => t := 4;
414
            when '5'     => t := 5;
415
            when '6'     => t := 6;
416
            when '7'     => t := 7;
417
            when '8'     => t := 8;
418
            when '9'     => t := 9;
419
            when 'a'|'A' => t := 10;
420
            when 'b'|'B' => t := 11;
421
            when 'c'|'C' => t := 12;
422
            when 'd'|'D' => t := 13;
423
            when 'e'|'E' => t := 14;
424
            when 'f'|'F' => t := 15;
425
            when '_'     => t := 0;    -- ignore these characters
426
                            place := place / inbase;
427
            when others  =>
428
              assert false
429
                report lf &
430
                       "CNVT_BASE found input value larger than base: " & lf &
431
                       "Input value: " & x(i) &
432
                         " Base: " & to_int_str(inbase) & lf &
433
                       "converting input to integer 0"
434
                severity warning;
435
              return 0;
436
          end case;
437
          if t / inbase > 1 then       -- invalid value for base
438
            assert false
439
              report lf &
440
                     "CNVT_BASE found input value larger than base: " & lf &
441
                     "Input value: " & x(i) &
442
                       " Base: " & to_int_str(inbase) & lf &
443
                     "converting input to integer 0"
444
              severity warning;
445
            return 0;
446
          else
447
            r     := r + (t * place);
448
            place := place * inbase;
449
          end if;
450
        end loop;
451
        return r;
452
      end cnvt_base;
453
 
454
    function extend (x   : std_logic;
455
                     len : positive) return std_logic_vector is
456
      variable v : std_logic_vector(1 to len) := (others => x);
457
    begin
458
      return v;
459
    end extend;
460
 
461
 
462
    -- implementation of public declarations
463
 
464
    function to_bin_str(x          : std_logic_vector;
465
                        rtn_len    : natural      := 0;
466
                        justify    : justify_side := right;
467
                        basespec   : b_spec       := yes) return string is
468
 
469
      variable int : std_logic_vector(1 to x'length):=x;
470
      variable r   : string(1 to x'length):=(others=>'$');
471
    begin
472
      for i in int'range loop
473
        r(i to i) := to_bin_str(int(i),basespec=>no);
474
      end loop;
475
      return format (r,binary,rtn_len,justify,basespec);
476
    end to_bin_str;
477
 
478
    function to_bin_str(x          : std_logic;
479
                        rtn_len    : natural      := 0;
480
                        justify    : justify_side := right;
481
                        basespec   : b_spec       := yes) return string is
482
      variable r   : string(1 to 1);
483
    begin
484
        case x is
485
          when '0'    => r(1) := '0';
486
          when '1'    => r(1) := '1';
487
          when 'U'    => r(1) := 'U';
488
          when 'X'    => r(1) := 'X';
489
          when 'Z'    => r(1) := 'Z';
490
          when 'W'    => r(1) := 'W';
491
          when 'H'    => r(1) := 'H';
492
          when 'L'    => r(1) := 'L';
493
          when '-'    => r(1) := '-';
494
        end case;
495
      return format (r,binary,rtn_len,justify,basespec);
496
    end to_bin_str;
497
 
498
    function to_bin_str(x          : natural;
499
                        rtn_len    : natural      := 0;
500
                        justify    : justify_side := right;
501
                        basespec   : b_spec       := yes) return string is
502
      variable int : natural := x;
503
      variable ptr : positive range 2 to 32 := 32;
504
      variable r   : string(2 to 32):=(others=>'$');
505
    begin
506
      if int = 0 then
507
        return format ("0",binary,rtn_len,justify,basespec);
508
      end if;
509
 
510
      while int > 0 loop
511
        case int rem 2 is
512
          when 0 => r(ptr) := '0';
513
          when 1 => r(ptr) := '1';
514
          when others =>
515
            assert false report lf & "TO_BIN_STR, shouldn't happen"
516
            severity failure;
517
            return "$";
518
           null;
519
        end case;
520
        int := int / 2;
521
        ptr := ptr - 1;
522
      end loop;
523
      return format (r(ptr+1 to 32),binary,rtn_len,justify,basespec);
524
    end to_bin_str;
525
 
526
    function to_hex_str(x          : std_logic_vector;
527
                        rtn_len    : natural      := 0;
528
                        justify    : justify_side := right;
529
                        basespec   : b_spec       := yes) return string is
530
      -- will return x'length/4
531
      variable nxt : positive := nextmultof(x'length,4);
532
      variable int : std_logic_vector(1 to nxt):= (others => '0');
533
      variable ptr : positive range 1 to (nxt/4)+1 := 1;
534
      variable r   : string(1 to nxt/4):=(others=>'$');
535
      subtype slv4 is std_logic_vector(1 to 4);
536
    begin
537
      int(nxt-x'length+1 to nxt) := x;
538
      if nxt-x'length > 0 and x(x'left) /= '1' then
539
        int(1 to nxt-x'length) := extend(x(x'left),nxt-x'length);
540
      end if;
541
      for i in int'range loop
542
        next when i rem 4 /= 1;
543
        case slv4'(int(i to i+3)) is
544
          when "0000" => r(ptr) := '0';
545
          when "0001" => r(ptr) := '1';
546
          when "0010" => r(ptr) := '2';
547
          when "0011" => r(ptr) := '3';
548
          when "0100" => r(ptr) := '4';
549
          when "0101" => r(ptr) := '5';
550
          when "0110" => r(ptr) := '6';
551
          when "0111" => r(ptr) := '7';
552
          when "1000" => r(ptr) := '8';
553
          when "1001" => r(ptr) := '9';
554
          when "1010" => r(ptr) := 'A';
555
          when "1011" => r(ptr) := 'B';
556
          when "1100" => r(ptr) := 'C';
557
          when "1101" => r(ptr) := 'D';
558
          when "1110" => r(ptr) := 'E';
559
          when "1111" => r(ptr) := 'F';
560
          when "ZZZZ" => r(ptr) := 'Z';
561
          when "WWWW" => r(ptr) := 'W';
562
          when "LLLL" => r(ptr) := 'L';
563
          when "HHHH" => r(ptr) := 'H';
564
          when "UUUU" => r(ptr) := 'U';
565
          when "XXXX" => r(ptr) := 'X';
566
          when "----" => r(ptr) := '-';
567
          when others =>
568
            assert false
569
              report lf &
570
                     "TO_HEX_STR found illegal value: " &
571
                       to_bin_str(int(i to i+3)) & lf &
572
                     "converting input to '-'"
573
              severity warning;
574
            r(ptr) := '-';
575
        end case;
576
        ptr := ptr + 1;
577
      end loop;
578
      return format (r,hex,rtn_len,justify,basespec);
579
    end to_hex_str;
580
 
581
    function to_hex_str(x          : natural;
582
                        rtn_len    : natural      := 0;
583
                        justify    : justify_side := right;
584
                        basespec   : b_spec       := yes)     return string is
585
      variable int : natural := x;
586
      variable ptr : positive range 1 to 20 := 20;
587
      variable r   : string(1 to 20):=(others=>'$');
588
    begin
589
      if x=0 then return format ("0",hex,rtn_len,justify,basespec); end if;
590
      while int > 0 loop
591
        case int rem 16 is
592
          when 0  => r(ptr) := '0';
593
          when 1  => r(ptr) := '1';
594
          when 2  => r(ptr) := '2';
595
          when 3  => r(ptr) := '3';
596
          when 4  => r(ptr) := '4';
597
          when 5  => r(ptr) := '5';
598
          when 6  => r(ptr) := '6';
599
          when 7  => r(ptr) := '7';
600
          when 8  => r(ptr) := '8';
601
          when 9  => r(ptr) := '9';
602
          when 10 => r(ptr) := 'A';
603
          when 11 => r(ptr) := 'B';
604
          when 12 => r(ptr) := 'C';
605
          when 13 => r(ptr) := 'D';
606
          when 14 => r(ptr) := 'E';
607
          when 15 => r(ptr) := 'F';
608
          when others =>
609
            assert false report lf & "TO_HEX_STR, shouldn't happen"
610
            severity failure;
611
            return "$";
612
        end case;
613
        int := int / 16;
614
        ptr := ptr - 1;
615
      end loop;
616
      return format (r(ptr+1 to 20),hex,rtn_len,justify,basespec);
617
    end to_hex_str;
618
 
619
      function to_oct_str(x          : std_logic_vector;
620
                          rtn_len    : natural      := 0;
621
                          justify    : justify_side := right;
622
                          basespec   : b_spec       := yes) return string is
623
        -- will return x'length/3
624
        variable nxt : positive := nextmultof(x'length,3);
625
        variable int : std_logic_vector(1 to nxt):= (others => '0');
626
        variable ptr : positive range 1 to (nxt/3)+1 := 1;
627
        variable r   : string(1 to nxt/3):=(others=>'$');
628
        subtype slv3 is std_logic_vector(1 to 3);
629
      begin
630
        int(nxt-x'length+1 to nxt) := x;
631
        if nxt-x'length > 0 and x(x'left) /= '1' then
632
          int(1 to nxt-x'length) := extend(x(x'left),nxt-x'length);
633
        end if;
634
        for i in int'range loop
635
          next when i rem 3 /= 1;
636
          case slv3'(int(i to i+2)) is
637
            when "000" => r(ptr) := '0';
638
            when "001" => r(ptr) := '1';
639
            when "010" => r(ptr) := '2';
640
            when "011" => r(ptr) := '3';
641
            when "100" => r(ptr) := '4';
642
            when "101" => r(ptr) := '5';
643
            when "110" => r(ptr) := '6';
644
            when "111" => r(ptr) := '7';
645
            when "ZZZ" => r(ptr) := 'Z';
646
            when "WWW" => r(ptr) := 'W';
647
            when "LLL" => r(ptr) := 'L';
648
            when "HHH" => r(ptr) := 'H';
649
            when "UUU" => r(ptr) := 'U';
650
            when "XXX" => r(ptr) := 'X';
651
            when "---" => r(ptr) := '-';
652
            when others =>
653
              assert false
654
                report lf &
655
                       "TO_OCT_STR found illegal value: " &
656
                         to_bin_str(int(i to i+2)) & lf &
657
                       "converting input to '-'"
658
                severity warning;
659
              r(ptr) := '-';
660
          end case;
661
          ptr := ptr + 1;
662
        end loop;
663
        return format (r,octal,rtn_len,justify,basespec);
664
      end to_oct_str;
665
 
666
      function to_oct_str(x          : natural;
667
                          rtn_len    : natural      := 0;
668
                          justify    : justify_side := right;
669
                          basespec   : b_spec       := yes)  return string is
670
        variable int : natural := x;
671
        variable ptr : positive range 1 to 20 := 20;
672
        variable r   : string(1 to 20):=(others=>'$');
673
      begin
674
        if x=0 then return format ("0",octal,rtn_len,justify,basespec); end if;
675
        while int > 0 loop
676
          case int rem 8 is
677
            when 0  => r(ptr) := '0';
678
            when 1  => r(ptr) := '1';
679
            when 2  => r(ptr) := '2';
680
            when 3  => r(ptr) := '3';
681
            when 4  => r(ptr) := '4';
682
            when 5  => r(ptr) := '5';
683
            when 6  => r(ptr) := '6';
684
            when 7  => r(ptr) := '7';
685
            when others =>
686
              assert false report lf & "TO_OCT_STR, shouldn't happen"
687
              severity failure;
688
              return "$";
689
          end case;
690
          int := int / 8;
691
          ptr := ptr - 1;
692
        end loop;
693
        return format (r(ptr+1 to 20),octal,rtn_len,justify,basespec);
694
      end to_oct_str;
695
 
696
      function to_int_str(x          : natural;
697
                          rtn_len    : natural      := 0;
698
                          justify    : justify_side := right;
699
                          basespec   : b_spec       := yes) return string is
700
        variable int : natural := x;
701
        variable ptr : positive range 1 to 32 := 32;
702
        variable r   : string(1 to 32):=(others=>'$');
703
      begin
704
        if x=0 then return format ("0",hex,rtn_len,justify,basespec);
705
        else
706
        while int > 0 loop
707
          case int rem 10 is
708
            when 0 => r(ptr) := '0';
709
            when 1 => r(ptr) := '1';
710
            when 2 => r(ptr) := '2';
711
            when 3 => r(ptr) := '3';
712
            when 4 => r(ptr) := '4';
713
            when 5 => r(ptr) := '5';
714
            when 6 => r(ptr) := '6';
715
            when 7 => r(ptr) := '7';
716
            when 8 => r(ptr) := '8';
717
            when 9 => r(ptr) := '9';
718
            when others =>
719
              assert false report lf & "TO_INT_STR, shouldn't happen"
720
              severity failure;
721
              return "$";
722
          end case;
723
          int := int / 10;
724
          ptr := ptr - 1;
725
        end loop;
726
        return format (r(ptr+1 to 32),decimal,rtn_len,justify,basespec);
727
        end if;
728
      end to_int_str;
729
 
730
      function to_int_str(x          : std_logic_vector;
731
                          rtn_len    : natural      := 0;
732
                          justify    : justify_side := right;
733
                          basespec   : b_spec       := yes)
734
        return string is
735
      begin
736
        return to_int_str(to_nat(x),rtn_len,justify,basespec);
737
      end to_int_str;
738
 
739
 
740
      function to_time_str (x          : time)
741
        return string is
742
      begin
743
        return to_int_str(to_nat(x),basespec=>no) & " ns";
744
      end to_time_str;
745
 
746
      function fill        (fill_char  : character    := '*';
747
                            rtn_len    : integer      := 1)
748
        return string is
749
        variable r : string(1 to max(rtn_len,1)) := (others => fill_char);
750
        variable len : integer;
751
      begin
752
        if rtn_len < 2 then -- always returns at least 1 fill char 
753
          len := 1;
754
        else
755
          len := rtn_len;
756
        end if;
757
        return r(1 to len);
758
      end fill;
759
 
760
      function to_nat(x : std_logic_vector) return natural is
761
        -- assumes x is an unsigned number, lsb on right,
762
        -- more than 31 bits are truncated on left
763
        variable t     : std_logic_vector(1 to x'length) := x;
764
        variable int   : std_logic_vector(1 to 31) := (others => '0');
765
        variable r     : natural  := 0;
766
        variable place : positive := 1;
767
      begin
768
        if x'length < 32 then
769
          int(max(32-x'length,1) to 31) := t(1 to x'length);
770
        else -- x'length >= 32
771
          int(1 to 31) := t(x'length-30 to x'length);
772
        end if;
773
        for i in int'reverse_range loop
774
          case int(i) is
775
            when '1' | 'H'    => r := r + place;
776
            when '0' | 'L'    => null;
777
            when others =>
778
              assert false
779
                report lf &
780
                    "TO_NAT found illegal value: " & to_bin_str(int(i)) & lf &
781
                    "converting input to integer 0"
782
                severity warning;
783
              return 0;
784
          end case;
785
          exit when i=1;
786
          place := place * 2;
787
        end loop;
788
        return r;
789
      end to_nat;
790
 
791
      function to_nat      (x        : std_logic)
792
        return natural is
793
      begin
794
        case x is
795
          when '0' => return 0 ;
796
          when '1' => return 1 ;
797
          when others =>
798
              assert false
799
                report lf &
800
                       "TO_NAT found illegal value: " & to_bin_str(x) & lf &
801
                       "converting input to integer 0"
802
                severity warning;
803
              return 0;
804
        end case;
805
      end to_nat;
806
 
807
      function to_nat      (x        : time)
808
        return natural is
809
      begin
810
        return x / 1 ns;
811
      end to_nat;
812
 
813
      function h(x       : string;
814
                 rtn_len : positive range 1 to 32 := 32)
815
        return std_logic_vector is
816
      -- if rtn_len is < than x'length*4, result will be truncated on the left
817
      -- if x is other than characters 0 to 9 or a,A to f,F or 
818
      -- x,X,z,Z,u,U,-,w,W, 
819
      --   those result bits will be set to 0
820
        variable int : string(1 to x'length) := x;
821
        variable size: positive := max(x'length*4,rtn_len);
822
        variable ptr : integer range -3 to size := size;
823
        variable r   : std_logic_vector(1 to size) := (others=>'0');
824
      begin
825
        for i in int'reverse_range loop
826
          case int(i) is
827
            when '0'     => r(ptr-3 to ptr) := "0000";
828
            when '1'     => r(ptr-3 to ptr) := "0001";
829
            when '2'     => r(ptr-3 to ptr) := "0010";
830
            when '3'     => r(ptr-3 to ptr) := "0011";
831
            when '4'     => r(ptr-3 to ptr) := "0100";
832
            when '5'     => r(ptr-3 to ptr) := "0101";
833
            when '6'     => r(ptr-3 to ptr) := "0110";
834
            when '7'     => r(ptr-3 to ptr) := "0111";
835
            when '8'     => r(ptr-3 to ptr) := "1000";
836
            when '9'     => r(ptr-3 to ptr) := "1001";
837
            when 'a'|'A' => r(ptr-3 to ptr) := "1010";
838
            when 'b'|'B' => r(ptr-3 to ptr) := "1011";
839
            when 'c'|'C' => r(ptr-3 to ptr) := "1100";
840
            when 'd'|'D' => r(ptr-3 to ptr) := "1101";
841
            when 'e'|'E' => r(ptr-3 to ptr) := "1110";
842
            when 'f'|'F' => r(ptr-3 to ptr) := "1111";
843
            when 'U'     => r(ptr-3 to ptr) := "UUUU";
844
            when 'X'     => r(ptr-3 to ptr) := "XXXX";
845
            when 'Z'     => r(ptr-3 to ptr) := "ZZZZ";
846
            when 'W'     => r(ptr-3 to ptr) := "WWWW";
847
            when 'H'     => r(ptr-3 to ptr) := "HHHH";
848
            when 'L'     => r(ptr-3 to ptr) := "LLLL";
849
            when '-'     => r(ptr-3 to ptr) := "----";
850
            when '_'     => ptr := ptr + 4;
851
            when others  =>
852
              assert false
853
                report lf &
854
                    "O conversion found illegal input character: " &
855
                     int(i) & lf & "converting character to '----'"
856
                severity warning;
857
              r(ptr-3 to ptr) := "----";
858
         end case;
859
          ptr := ptr - 4;
860
        end loop;
861
        return r(size-rtn_len+1 to size);
862
      end h;
863
 
864
      function d         (x          : string;
865
                          rtn_len    : positive range 1 to 32 := 32)
866
        return std_logic_vector is
867
      -- if rtn_len is < than binary length of x, result will be truncated on 
868
      -- the left
869
      -- if x is other than characters 0 to 9, result will be 0
870
      begin
871
        return to_slv(cnvt_base(x,10),rtn_len);
872
      end d;
873
 
874
      function o         (x          : string;
875
                          rtn_len    : positive range 1 to 32 := 32)
876
        return std_logic_vector is
877
      -- if rtn_len is < than x'length*3, result will be truncated on the left
878
      -- if x is other than characters 0 to 7 or or x,X,z,Z,u,U,-,w,W,
879
      --   those result bits will be set to 0
880
        variable int : string(1 to x'length) := x;
881
        variable size: positive := max(x'length*3,rtn_len);
882
        variable ptr : integer range -2 to size := size;
883
        variable r   : std_logic_vector(1 to size) := (others=>'0');
884
      begin
885
        for i in int'reverse_range loop
886
          case int(i) is
887
            when '0'     => r(ptr-2 to ptr) := "000";
888
            when '1'     => r(ptr-2 to ptr) := "001";
889
            when '2'     => r(ptr-2 to ptr) := "010";
890
            when '3'     => r(ptr-2 to ptr) := "011";
891
            when '4'     => r(ptr-2 to ptr) := "100";
892
            when '5'     => r(ptr-2 to ptr) := "101";
893
            when '6'     => r(ptr-2 to ptr) := "110";
894
            when '7'     => r(ptr-2 to ptr) := "111";
895
            when 'U'     => r(ptr-2 to ptr) := "UUU";
896
            when 'X'     => r(ptr-2 to ptr) := "XXX";
897
            when 'Z'     => r(ptr-2 to ptr) := "ZZZ";
898
            when 'W'     => r(ptr-2 to ptr) := "WWW";
899
            when 'H'     => r(ptr-2 to ptr) := "HHH";
900
            when 'L'     => r(ptr-2 to ptr) := "LLL";
901
            when '-'     => r(ptr-2 to ptr) := "---";
902
            when '_'     => ptr := ptr + 3;
903
            when others  =>
904
              assert false
905
                report lf &
906
                    "O conversion found illegal input character: " &
907
                     int(i) & lf & "converting character to '---'"
908
                severity warning;
909
              r(ptr-2 to ptr) := "---";
910
         end case;
911
          ptr := ptr - 3;
912
        end loop;
913
        return r(size-rtn_len+1 to size);
914
      end o;
915
 
916
      function b         (x          : string;
917
                          rtn_len    : positive range 1 to 32 := 32)
918
        return std_logic_vector is
919
      -- if rtn_len is < than x'length, result will be truncated on the left
920
      -- if x is other than characters 0 to 1 or x,X,z,Z,u,U,-,w,W, 
921
      --   those result bits will be set to 0
922
        variable int : string(1 to x'length) := x;
923
        variable size: positive := max(x'length,rtn_len);
924
        variable ptr : integer range 0 to size+1 := size; -- csa
925
        variable r   : std_logic_vector(1 to size) := (others=>'0');
926
      begin
927
        for i in int'reverse_range loop
928
          case int(i) is
929
            when '0'     => r(ptr) := '0';
930
            when '1'     => r(ptr) := '1';
931
            when 'U'     => r(ptr) := 'U';
932
            when 'X'     => r(ptr) := 'X';
933
            when 'Z'     => r(ptr) := 'Z';
934
            when 'W'     => r(ptr) := 'W';
935
            when 'H'     => r(ptr) := 'H';
936
            when 'L'     => r(ptr) := 'L';
937
            when '-'     => r(ptr) := '-';
938
            when '_'     => ptr := ptr + 1;
939
            when others  =>
940
              assert false
941
                report lf &
942
                    "B conversion found illegal input character: " &
943
                     int(i) & lf &  "converting character to '-'"
944
                severity warning;
945
              r(ptr) := '-';
946
          end case;
947
          ptr := ptr - 1;
948
        end loop;
949
        return r(size-rtn_len+1 to size);
950
      end b;
951
 
952
      function h         (x          : string)
953
        return natural is
954
      -- only following characters are allowed, otherwise result will be 0
955
      --   0 to 9 
956
      --   a,A to f,F 
957
      --   blanks, underscore
958
      begin
959
        return cnvt_base(x,16);
960
      end h;
961
 
962
      function d         (x          : string)
963
        return natural is
964
      -- only following characters are allowed, otherwise result will be 0
965
      --   0 to 9 
966
      --   blanks, underscore
967
      begin
968
        return cnvt_base(x,10);
969
      end d;
970
 
971
      function o         (x          : string)
972
        return natural is
973
      -- only following characters are allowed, otherwise result will be 0
974
      --   0 to 7 
975
      --   blanks, underscore
976
      begin
977
        return cnvt_base(x,8);
978
      end o;
979
 
980
      function b         (x          : string)
981
        return natural is
982
      -- only following characters are allowed, otherwise result will be 0
983
      --   0 to 1 
984
      --   blanks, underscore
985
      begin
986
        return cnvt_base(x,2);
987
      end b;
988
 
989
      function to_slv(x       : natural;
990
                      rtn_len : positive range 1 to 32 := 32)
991
        return std_logic_vector is
992
        -- if rtn_len is < than sizeof(x), result will be truncated on the left
993
        variable int : natural  := x;
994
        variable ptr : positive := 32;
995
        variable r   : std_logic_vector(1 to 32) := (others=>'0');
996
      begin
997
        while int > 0 loop
998
          case int rem 2 is
999
            when 0 => r(ptr) := '0';
1000
            when 1 => r(ptr) := '1';
1001
            when others =>
1002
              assert false report lf & "TO_SLV, shouldn't happen"
1003
              severity failure;
1004
              return "0";
1005
          end case;
1006
          int := int / 2;
1007
          ptr := ptr - 1;
1008
        end loop;
1009
        return r(33-rtn_len to 32);
1010
      end to_slv;
1011
 
1012
      function to_sl(x       : natural)
1013
        return std_logic is
1014
        variable r   : std_logic := '0';
1015
      begin
1016
        case x is
1017
          when 0 => null;
1018
          when 1 => r := '1';
1019
          when others =>
1020
            assert false
1021
              report lf &
1022
                "TO_SL found illegal input character: " &
1023
                 to_int_str(x) & lf & "converting character to '-'"
1024
              severity warning;
1025
            return '-';
1026
        end case;
1027
        return r;
1028
      end to_sl;
1029
 
1030
      function to_time (x: natural) return time is
1031
      begin
1032
        return x * 1 ns;
1033
      end to_time;
1034
 
1035
END conversions;

powered by: WebSVN 2.1.0

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