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

Subversion Repositories p9813_rgb_led_string_driver

[/] [p9813_rgb_led_string_driver/] [trunk/] [rtl/] [VHDL/] [testbench/] [txt_util.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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