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

Subversion Repositories fpu100

[/] [fpu100/] [branches/] [avendor/] [test_bench/] [txt_util.vhd] - Blame information for rev 21

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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