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

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.hwp.communication/] [basic_tester/] [1.0/] [vhd/] [txt_util.vhd] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
-- -------------------------------------------------------------------
2
-- Design:
3
--
4
--    Package for VHDL text output
5
--
6
--     Note:
7
--     ----- 
8
--     This package uses the VHDL 95 standard.
9
--     If VHDL 95 is not supported by your simulator 
10
--     you need to comment out the file access functions.
11
--
12
--    The package provides a means to output text and
13
--    manipulate strings. 
14
--
15
--    The basic usage is like this: >> print(s); <<
16
--    (where s is any string)
17
--    To print something which is not a string it has to be converted
18
--    into a string first. For this purpose the package contains
19
--    conversion functions called >> str(...) <<.
20
--    For example a std_logic_vector slv would be printed like this:
21
--    >> print(str(slv)); <<. To print several items on one line the
22
--    items have to concatenated as strings with the "&" operator eg:
23
--    >> print("The value of slv is "& str(slv)); <<
24
--    The string functions can also be used in assert statements as shown
25
--    in the example below:
26
--    >> assert DIN = "0101"  <<
27
--    >>   report "DIN = "& str(DIN)& " expected 0101 "  <<
28
--    >>   severity Error;  <<
29
--
30
--
31
--
32
-- -------------------------------------------------------------------
33
 
34
-- MODIFIED by ak 22.02.2007, support for numeric std unsigned
35
 
36
library ieee;
37
use ieee.std_logic_1164.all;
38
use ieee.numeric_std.all;
39
use std.textio.all;
40
 
41
 
42
package txt_util is
43
 
44
    -- prints a message to the screen
45
    procedure print(text: string);
46
 
47
    -- prints the message when active
48
    -- useful for debug switches
49
    procedure print(active: boolean; text: string);
50
 
51
    -- converts std_logic into a character
52
    function chr(sl: std_logic) return character;
53
 
54
    -- converts std_logic into a string (1 to 1)
55
    function str(sl: std_logic) return string;
56
 
57
    -- converts std_logic_vector into a string (binary base)
58
    function str(slv: std_logic_vector) return string;
59
 
60
    -- converts std_logic_vector into a string (binary base)
61
    function str(slv: unsigned) return string;
62
 
63
    -- converts boolean into a string
64
    function str(b: boolean) return string;
65
 
66
    -- converts an integer into a single character
67
    -- (can also be used for hex conversion and other bases)
68
    function chr(int: integer) return character;
69
 
70
    -- converts integer into string using specified base
71
    function str(int: integer; base: integer) return string;
72
 
73
    -- converts integer to string, using base 10
74
    function str(int: integer) return string;
75
 
76
    -- convert std_logic_vector into a string in hex format
77
    function hstr(slv: std_logic_vector) return string;
78
 
79
 
80
    -- functions to manipulate strings
81
    -----------------------------------
82
 
83
    -- convert a character to upper case
84
    function to_upper(c: character) return character;
85
 
86
    -- convert a character to lower case
87
    function to_lower(c: character) return character;
88
 
89
    -- convert a string to upper case
90
    function to_upper(s: string) return string;
91
 
92
    -- convert a string to lower case
93
    function to_lower(s: string) return string;
94
 
95
 
96
 
97
    -- functions to convert strings into other formats
98
    --------------------------------------------------
99
 
100
    -- converts a character into std_logic
101
    function to_std_logic(c: character) return std_logic;
102
 
103
    -- converts a string into std_logic_vector
104
    function to_std_logic_vector(s: string) return std_logic_vector;
105
 
106
 
107
 
108
    -- file I/O
109
    -----------
110
 
111
    -- read variable length string from input file
112
    procedure str_read(file in_file: TEXT;
113
                       res_string: out string);
114
 
115
    -- print string to a file and start new line
116
    procedure print(file out_file: TEXT;
117
                    new_string: in  string);
118
 
119
    -- print character to a file and start new line
120
    procedure print(file out_file: TEXT;
121
                    char:       in  character);
122
 
123
end txt_util;
124
 
125
 
126
 
127
 
128
package body txt_util is
129
 
130
 
131
 
132
 
133
   -- prints text to the screen
134
 
135
   procedure print(text: string) is
136
     variable msg_line: line;
137
     begin
138
       write(msg_line, text);
139
       writeline(output, msg_line);
140
   end print;
141
 
142
 
143
 
144
 
145
   -- prints text to the screen when active
146
 
147
   procedure print(active: boolean; text: string)  is
148
     begin
149
      if active then
150
         print(text);
151
      end if;
152
   end print;
153
 
154
 
155
   -- converts std_logic into a character
156
 
157
   function chr(sl: std_logic) return character is
158
    variable c: character;
159
    begin
160
      case sl is
161
         when 'U' => c:= 'U';
162
         when 'X' => c:= 'X';
163
         when '0' => c:= '0';
164
         when '1' => c:= '1';
165
         when 'Z' => c:= 'Z';
166
         when 'W' => c:= 'W';
167
         when 'L' => c:= 'L';
168
         when 'H' => c:= 'H';
169
         when '-' => c:= '-';
170
      end case;
171
    return c;
172
   end chr;
173
 
174
 
175
 
176
   -- converts std_logic into a string (1 to 1)
177
 
178
   function str(sl: std_logic) return string is
179
    variable s: string(1 to 1);
180
    begin
181
        s(1) := chr(sl);
182
        return s;
183
   end str;
184
 
185
 
186
 
187
   -- converts std_logic_vector into a string (binary base)
188
   -- (this also takes care of the fact that the range of
189
   --  a string is natural while a std_logic_vector may
190
   --  have an integer range)
191
 
192
   function str(slv: std_logic_vector) return string is
193
     variable result : string (1 to slv'length);
194
     variable r : integer;
195
   begin
196
     r := 1;
197
     for i in slv'range loop
198
        result(r) := chr(slv(i));
199
        r := r + 1;
200
     end loop;
201
     return result;
202
   end str;
203
 
204
   -- converts std_logic_vector into a string (binary base)
205
   -- (this also takes care of the fact that the range of
206
   --  a string is natural while a std_logic_vector may
207
   --  have an integer range)
208
 
209
   function str(slv: unsigned) return string is
210
     variable result : string (1 to slv'length);
211
     variable r : integer;
212
   begin
213
     r := 1;
214
     for i in slv'range loop
215
        result(r) := chr(slv(i));
216
        r := r + 1;
217
     end loop;
218
     return result;
219
   end str;
220
 
221
   function str(b: boolean) return string is
222
 
223
    begin
224
       if b then
225
          return "true";
226
      else
227
        return "false";
228
       end if;
229
    end str;
230
 
231
 
232
   -- converts an integer into a character
233
   -- for 0 to 9 the obvious mapping is used, higher
234
   -- values are mapped to the characters A-Z
235
   -- (this is usefull for systems with base > 10)
236
   -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
237
 
238
   function chr(int: integer) return character is
239
    variable c: character;
240
   begin
241
        case int is
242
          when  0 => c := '0';
243
          when  1 => c := '1';
244
          when  2 => c := '2';
245
          when  3 => c := '3';
246
          when  4 => c := '4';
247
          when  5 => c := '5';
248
          when  6 => c := '6';
249
          when  7 => c := '7';
250
          when  8 => c := '8';
251
          when  9 => c := '9';
252
          when 10 => c := 'A';
253
          when 11 => c := 'B';
254
          when 12 => c := 'C';
255
          when 13 => c := 'D';
256
          when 14 => c := 'E';
257
          when 15 => c := 'F';
258
          when 16 => c := 'G';
259
          when 17 => c := 'H';
260
          when 18 => c := 'I';
261
          when 19 => c := 'J';
262
          when 20 => c := 'K';
263
          when 21 => c := 'L';
264
          when 22 => c := 'M';
265
          when 23 => c := 'N';
266
          when 24 => c := 'O';
267
          when 25 => c := 'P';
268
          when 26 => c := 'Q';
269
          when 27 => c := 'R';
270
          when 28 => c := 'S';
271
          when 29 => c := 'T';
272
          when 30 => c := 'U';
273
          when 31 => c := 'V';
274
          when 32 => c := 'W';
275
          when 33 => c := 'X';
276
          when 34 => c := 'Y';
277
          when 35 => c := 'Z';
278
          when others => c := '?';
279
        end case;
280
        return c;
281
    end chr;
282
 
283
 
284
 
285
   -- convert integer to string using specified base
286
   -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
287
 
288
   function str(int: integer; base: integer) return string is
289
 
290
    variable temp:      string(1 to 10);
291
    variable num:       integer;
292
    variable abs_int:   integer;
293
    variable len:       integer := 1;
294
    variable power:     integer := 1;
295
 
296
   begin
297
 
298
    -- bug fix for negative numbers
299
    abs_int := abs(int);
300
 
301
    num     := abs_int;
302
 
303
    while num >= base loop                     -- Determine how many
304
      len := len + 1;                          -- characters required
305
      num := num / base;                       -- to represent the
306
    end loop ;                                 -- number.
307
 
308
    for i in len downto 1 loop                 -- Convert the number to
309
      temp(i) := chr(abs_int/power mod base);  -- a string starting
310
      power := power * base;                   -- with the right hand
311
    end loop ;                                 -- side.
312
 
313
    -- return result and add sign if required
314
    if int < 0 then
315
       return '-'& temp(1 to len);
316
     else
317
       return temp(1 to len);
318
    end if;
319
 
320
   end str;
321
 
322
 
323
  -- convert integer to string, using base 10
324
  function str(int: integer) return string is
325
 
326
   begin
327
 
328
    return str(int, 10) ;
329
 
330
   end str;
331
 
332
 
333
 
334
   -- converts a std_logic_vector into a hex string.
335
   function hstr(slv: std_logic_vector) return string is
336
       variable hexlen: integer;
337
       variable longslv : std_logic_vector(67 downto 0) := (others => '0');
338
       variable hex : string(1 to 16);
339
       variable fourbit : std_logic_vector(3 downto 0);
340
     begin
341
       hexlen := (slv'left+1)/4;
342
       if (slv'left+1) mod 4 /= 0 then
343
         hexlen := hexlen + 1;
344
       end if;
345
       longslv(slv'left downto 0) := slv;
346
       for i in (hexlen -1) downto 0 loop
347
         fourbit := longslv(((i*4)+3) downto (i*4));
348
         case fourbit is
349
           when "0000" => hex(hexlen -I) := '0';
350
           when "0001" => hex(hexlen -I) := '1';
351
           when "0010" => hex(hexlen -I) := '2';
352
           when "0011" => hex(hexlen -I) := '3';
353
           when "0100" => hex(hexlen -I) := '4';
354
           when "0101" => hex(hexlen -I) := '5';
355
           when "0110" => hex(hexlen -I) := '6';
356
           when "0111" => hex(hexlen -I) := '7';
357
           when "1000" => hex(hexlen -I) := '8';
358
           when "1001" => hex(hexlen -I) := '9';
359
           when "1010" => hex(hexlen -I) := 'A';
360
           when "1011" => hex(hexlen -I) := 'B';
361
           when "1100" => hex(hexlen -I) := 'C';
362
           when "1101" => hex(hexlen -I) := 'D';
363
           when "1110" => hex(hexlen -I) := 'E';
364
           when "1111" => hex(hexlen -I) := 'F';
365
           when "ZZZZ" => hex(hexlen -I) := 'z';
366
           when "UUUU" => hex(hexlen -I) := 'u';
367
           when "XXXX" => hex(hexlen -I) := 'x';
368
           when others => hex(hexlen -I) := '?';
369
         end case;
370
       end loop;
371
       return hex(1 to hexlen);
372
     end hstr;
373
 
374
 
375
 
376
   -- functions to manipulate strings
377
   -----------------------------------
378
 
379
 
380
   -- convert a character to upper case
381
 
382
   function to_upper(c: character) return character is
383
 
384
      variable u: character;
385
 
386
    begin
387
 
388
       case c is
389
        when 'a' => u := 'A';
390
        when 'b' => u := 'B';
391
        when 'c' => u := 'C';
392
        when 'd' => u := 'D';
393
        when 'e' => u := 'E';
394
        when 'f' => u := 'F';
395
        when 'g' => u := 'G';
396
        when 'h' => u := 'H';
397
        when 'i' => u := 'I';
398
        when 'j' => u := 'J';
399
        when 'k' => u := 'K';
400
        when 'l' => u := 'L';
401
        when 'm' => u := 'M';
402
        when 'n' => u := 'N';
403
        when 'o' => u := 'O';
404
        when 'p' => u := 'P';
405
        when 'q' => u := 'Q';
406
        when 'r' => u := 'R';
407
        when 's' => u := 'S';
408
        when 't' => u := 'T';
409
        when 'u' => u := 'U';
410
        when 'v' => u := 'V';
411
        when 'w' => u := 'W';
412
        when 'x' => u := 'X';
413
        when 'y' => u := 'Y';
414
        when 'z' => u := 'Z';
415
        when others => u := c;
416
    end case;
417
 
418
      return u;
419
 
420
   end to_upper;
421
 
422
 
423
   -- convert a character to lower case
424
 
425
   function to_lower(c: character) return character is
426
 
427
      variable l: character;
428
 
429
    begin
430
 
431
       case c is
432
        when 'A' => l := 'a';
433
        when 'B' => l := 'b';
434
        when 'C' => l := 'c';
435
        when 'D' => l := 'd';
436
        when 'E' => l := 'e';
437
        when 'F' => l := 'f';
438
        when 'G' => l := 'g';
439
        when 'H' => l := 'h';
440
        when 'I' => l := 'i';
441
        when 'J' => l := 'j';
442
        when 'K' => l := 'k';
443
        when 'L' => l := 'l';
444
        when 'M' => l := 'm';
445
        when 'N' => l := 'n';
446
        when 'O' => l := 'o';
447
        when 'P' => l := 'p';
448
        when 'Q' => l := 'q';
449
        when 'R' => l := 'r';
450
        when 'S' => l := 's';
451
        when 'T' => l := 't';
452
        when 'U' => l := 'u';
453
        when 'V' => l := 'v';
454
        when 'W' => l := 'w';
455
        when 'X' => l := 'x';
456
        when 'Y' => l := 'y';
457
        when 'Z' => l := 'z';
458
        when others => l := c;
459
    end case;
460
 
461
      return l;
462
 
463
   end to_lower;
464
 
465
 
466
 
467
   -- convert a string to upper case
468
 
469
   function to_upper(s: string) return string is
470
 
471
     variable uppercase: string (s'range);
472
 
473
   begin
474
 
475
     for i in s'range loop
476
        uppercase(i):= to_upper(s(i));
477
     end loop;
478
     return uppercase;
479
 
480
   end to_upper;
481
 
482
 
483
 
484
   -- convert a string to lower case
485
 
486
   function to_lower(s: string) return string is
487
 
488
     variable lowercase: string (s'range);
489
 
490
   begin
491
 
492
     for i in s'range loop
493
        lowercase(i):= to_lower(s(i));
494
     end loop;
495
     return lowercase;
496
 
497
   end to_lower;
498
 
499
 
500
 
501
-- functions to convert strings into other types
502
 
503
 
504
-- converts a character into a std_logic
505
 
506
function to_std_logic(c: character) return std_logic is
507
    variable sl: std_logic;
508
    begin
509
      case c is
510
        when 'U' =>
511
           sl := 'U';
512
        when 'X' =>
513
           sl := 'X';
514
        when '0' =>
515
           sl := '0';
516
        when '1' =>
517
           sl := '1';
518
        when 'Z' =>
519
           sl := 'Z';
520
        when 'W' =>
521
           sl := 'W';
522
        when 'L' =>
523
           sl := 'L';
524
        when 'H' =>
525
           sl := 'H';
526
        when '-' =>
527
           sl := '-';
528
        when others =>
529
           sl := 'X';
530
    end case;
531
   return sl;
532
  end to_std_logic;
533
 
534
 
535
-- converts a string into std_logic_vector
536
 
537
function to_std_logic_vector(s: string) return std_logic_vector is
538
  variable slv: std_logic_vector(s'high-s'low downto 0);
539
  variable k: integer;
540
begin
541
   k := s'high-s'low;
542
  for i in s'range loop
543
     slv(k) := to_std_logic(s(i));
544
     k      := k - 1;
545
  end loop;
546
  return slv;
547
end to_std_logic_vector;
548
 
549
 
550
 
551
 
552
 
553
 
554
----------------
555
--  file I/O  --
556
----------------
557
 
558
 
559
 
560
-- read variable length string from input file
561
 
562
procedure str_read(file in_file: TEXT;
563
                   res_string: out string) is
564
 
565
       variable l:         line;
566
       variable c:         character;
567
       variable is_string: boolean;
568
 
569
   begin
570
 
571
     readline(in_file, l);
572
     -- clear the contents of the result string
573
     for i in res_string'range loop
574
         res_string(i) := ' ';
575
     end loop;
576
     -- read all characters of the line, up to the length  
577
     -- of the results string
578
     for i in res_string'range loop
579
        read(l, c, is_string);
580
        res_string(i) := c;
581
        if not is_string then -- found end of line
582
           exit;
583
        end if;
584
     end loop;
585
 
586
end str_read;
587
 
588
 
589
-- print string to a file
590
procedure print(file out_file: TEXT;
591
                new_string: in  string) is
592
 
593
       variable l: line;
594
 
595
   begin
596
 
597
     write(l, new_string);
598
     writeline(out_file, l);
599
 
600
end print;
601
 
602
 
603
-- print character to a file and start new line
604
procedure print(file out_file: TEXT;
605
                char: in  character) is
606
 
607
       variable l: line;
608
 
609
   begin
610
 
611
     write(l, char);
612
     writeline(out_file, l);
613
 
614
end print;
615
 
616
 
617
 
618
-- appends contents of a string to a file until line feed occurs
619
-- (LF is considered to be the end of the string)
620
 
621
procedure str_write(file out_file: TEXT;
622
                    new_string: in  string) is
623
 begin
624
 
625
   for i in new_string'range loop
626
      print(out_file, new_string(i));
627
      if new_string(i) = LF then -- end of string
628
         exit;
629
      end if;
630
   end loop;
631
 
632
end str_write;
633
 
634
 
635
 
636
 
637
end txt_util;
638
 
639
 
640
 
641
 

powered by: WebSVN 2.1.0

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