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

Subversion Repositories esoc

[/] [esoc/] [trunk/] [Sources/] [logixa/] [package_txt_utilities.vhd] - Blame information for rev 46

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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