SURF  1.0
TextUtilPkg.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 -- File : TextUtilPkg.vhd
3 -- Company : SLAC National Accelerator Laboratory
4 -- Created : 2013-05-01
5 -- Last update: 2017-02-23
6 -------------------------------------------------------------------------------
7 -- Description: Provides functions for handling text.
8 ------------------------------------------------------------------------------
9 -- This file is part of 'SLAC Firmware Standard Library'.
10 -- It is subject to the license terms in the LICENSE.txt file found in the
11 -- top-level directory of this distribution and at:
12 -- https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
13 -- No part of 'SLAC Firmware Standard Library', including this file,
14 -- may be copied, modified, propagated, or distributed except according to
15 -- the terms contained in the LICENSE.txt file.
16 ------------------------------------------------------------------------------
17 
18 library ieee;
19 use ieee.std_logic_1164.all;
20 use std.textio.all;
21 use work.StdRtlPkg.all;
22 
23 package TextUtilPkg is
24 --! @file
25  --! @ingroup base_general
26 
27  -- prints a message to the screen
28  procedure print(text : string);
29 
30  -- prints the message when active
31  -- useful for debug switches
32  procedure print(active : boolean; text : string);
33 
34  -- converts std_logic into a character
35  function chr(sl : std_logic) return character;
36 
37  -- converts std_logic into a string (1 to 1)
38  function str(sl : std_logic) return string;
39 
40  -- converts std_logic_vector into a string (binary base)
41  function str(slv : std_logic_vector) return string;
42 
43  -- converts boolean into a string
44  function str(b : boolean) return string;
45 
46  -- converts an integer into a single character
47  -- (can also be used for hex conversion and other bases)
48  function chr(int : integer) return character;
49 
50  -- Converts a character into an integer
51  function int(c : character) return integer;
52 
53  -- converts integer into string using specified base
54  function str(int : integer; base : integer) return string;
55 
56  -- converts a string with specified base into an integer
57  function int(s : string; base : integer) return integer;
58 
59  -- converts integer to string, using base 10
60  function str(int : integer) return string;
61 
62  -- converts a time to a string
63  function str (tim : time) return string;
64 
65  -- convert std_logic_vector into a string in hex format
66  function hstr(slv : std_logic_vector) return string;
67 
68  ----------------------------------
69  -- functions to manipulate strings
70  -----------------------------------
71 
72  -- convert a character to upper case
73  function toUpper(c : character) return character;
74 
75  -- convert a character to lower case
76  function toLower(c : character) return character;
77 
78  -- convert a string to upper case
79  function toUpper(s : string) return string;
80 
81  -- convert a string to lower case
82  function toLower(s : string) return string;
83 
84  -- checks if whitespace (JFF)
85  function isWhitespace(c : character) return boolean;
86 
87  -- remove leading whitespace (JFF)
88  function strip(s : string) return string;
89 
90  -- return first nonwhitespace substring (JFF)
91  function firstString(s : string) return string;
92 
93  -- finds the first non-whitespace substring in a string and (JFF)
94  -- returns both the substring and the original with the substring removed
95  procedure chomp(variable s : inout string; variable shead : out string);
96 
97 
98  --------------------------------------------------
99  -- functions to convert strings into other formats
100  --------------------------------------------------
101 
102  -- converts a character into std_logic
103  function toSl(c : character) return std_logic;
104 
105  -- converts a string into std_logic_vector
106  function toSlv(s : string) return std_logic_vector;
107 
108 
109  -----------
110  -- file I/O
111  -----------
112 
113  -- read variable length string from input file
114  procedure strRead(file in_file : text;
115  res_string : out string);
116 
117  -- print string to a file and start new line
118  procedure print(file out_file : text;
119  new_string : in string);
120 
121  -- print character to a file and start new line
122  procedure print(file out_file : text;
123  char : in character);
124 
125 
126 
127 end TextUtilPkg;
128 
129 package body TextUtilPkg is
130 
131  -- prints text to the screen
132  procedure print(text : string) is
133  variable msg_line : line;
134  begin
135  write(msg_line, text);
136  writeline(output, msg_line);
137  end print;
138 
139  -- prints text to the screen when active
140  procedure print(active : boolean; text : string) is
141  begin
142  if active then
143  print(text);
144  end if;
145  end print;
146 
147  -- converts std_logic into a character
148  function chr(sl : std_logic) return character is
149  variable c : character;
150  begin
151  case sl is
152  when 'U' => c := 'U';
153  when 'X' => c := 'X';
154  when '0' => c := '0';
155  when '1' => c := '1';
156  when 'Z' => c := 'Z';
157  when 'W' => c := 'W';
158  when 'L' => c := 'L';
159  when 'H' => c := 'H';
160  when '-' => c := '-';
161  end case;
162  return c;
163  end chr;
164 
165  -- converts std_logic into a string (1 to 1)
166  function str(sl : std_logic) return string is
167  variable s : string(1 to 1);
168  begin
169  s(1) := chr(sl);
170  return s;
171  end str;
172 
173  -- converts std_logic_vector into a string (binary base)
174  -- (this also takes care of the fact that the range of
175  -- a string is natural while a std_logic_vector may
176  -- have an integer range)
177  function str(slv : std_logic_vector) return string is
178  variable result : string (1 to slv'length);
179  variable r : integer;
180  begin
181  r := 1;
182  for i in slv'range loop
183  result(r) := chr(slv(i));
184  r := r + 1;
185  end loop;
186  return result;
187  end str;
188 
189  -- Converts a boolean into "true" or "false"
190  function str(b : boolean) return string is
191  begin
192  if b then
193  return "true";
194  else
195  return "false";
196  end if;
197  end str;
198 
199  -- converts an integer into a character
200  -- for 0 to 9 the obvious mapping is used, higher
201  -- values are mapped to the characters A-Z
202  -- (this is usefull for systems with base > 10)
203  -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
204  function chr(int : integer) return character is
205  variable c : character;
206  begin
207  case int is
208  when 0 => c := '0';
209  when 1 => c := '1';
210  when 2 => c := '2';
211  when 3 => c := '3';
212  when 4 => c := '4';
213  when 5 => c := '5';
214  when 6 => c := '6';
215  when 7 => c := '7';
216  when 8 => c := '8';
217  when 9 => c := '9';
218  when 10 => c := 'A';
219  when 11 => c := 'B';
220  when 12 => c := 'C';
221  when 13 => c := 'D';
222  when 14 => c := 'E';
223  when 15 => c := 'F';
224  when 16 => c := 'G';
225  when 17 => c := 'H';
226  when 18 => c := 'I';
227  when 19 => c := 'J';
228  when 20 => c := 'K';
229  when 21 => c := 'L';
230  when 22 => c := 'M';
231  when 23 => c := 'N';
232  when 24 => c := 'O';
233  when 25 => c := 'P';
234  when 26 => c := 'Q';
235  when 27 => c := 'R';
236  when 28 => c := 'S';
237  when 29 => c := 'T';
238  when 30 => c := 'U';
239  when 31 => c := 'V';
240  when 32 => c := 'W';
241  when 33 => c := 'X';
242  when 34 => c := 'Y';
243  when 35 => c := 'Z';
244  when others => c := '?';
245  end case;
246  return c;
247  end chr;
248 
249  -- Convert a character into an integer.
250  function int (c : character) return integer is
251  variable tmp : character;
252  begin
253  tmp := toUpper(c);
254  case tmp is
255  when '0' => return 0;
256  when '1' => return 1;
257  when '2' => return 2;
258  when '3' => return 3;
259  when '4' => return 4;
260  when '5' => return 5;
261  when '6' => return 6;
262  when '7' => return 7;
263  when '8' => return 8;
264  when '9' => return 9;
265  when 'A' => return 10;
266  when 'B' => return 11;
267  when 'C' => return 12;
268  when 'D' => return 13;
269  when 'E' => return 14;
270  when 'F' => return 15;
271  when 'G' => return 16;
272  when 'H' => return 17;
273  when 'I' => return 18;
274  when 'J' => return 19;
275  when 'K' => return 20;
276  when 'L' => return 21;
277  when 'M' => return 22;
278  when 'N' => return 23;
279  when 'O' => return 24;
280  when 'P' => return 25;
281  when 'Q' => return 26;
282  when 'R' => return 27;
283  when 'S' => return 28;
284  when 'T' => return 29;
285  when 'U' => return 30;
286  when 'V' => return 31;
287  when 'W' => return 32;
288  when 'X' => return 33;
289  when 'Y' => return 34;
290  when 'Z' => return 35;
291  when others => return -1;
292  end case;
293  end function int;
294 
295  -- convert integer to string using specified base
296  -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
297  function str(int : integer; base : integer) return string is
298  variable temp : string(1 to 10);
299  variable num : integer;
300  variable abs_int : integer;
301  variable len : integer := 1;
302  variable power : integer := 1;
303  begin
304  -- bug fix for negative numbers
305  abs_int := abs(int);
306  num := abs_int;
307 
308  while num >= base loop -- Determine how many
309  len := len + 1; -- characters required
310  num := num / base; -- to represent the
311  end loop; -- number.
312 
313  for i in len downto 1 loop -- Convert the number to
314  temp(i) := chr(abs_int/power mod base); -- a string starting
315  power := power * base; -- with the right hand
316  end loop; -- side.
317 
318  -- return result and add sign if required
319  if int < 0 then
320  return '-'& temp(1 to len);
321  else
322  return temp(1 to len);
323  end if;
324 
325  end str;
326 
327  -- Convert a string and base into an integer.
328  function int (s : string; base : integer) return integer is
329  variable ret : integer;
330  variable tmp : integer;
331  begin
332  ret := 0;
333  for i in s'range loop
334  tmp := int(s(i));
335  assert (tmp < base and tmp >= 0) report
336  "TextUtilPkg::int(string, integer): Input string (" & s & ") " &
337  "has character (" & s(i) & ") outside of base (" & str(base) & ") character set."
338  severity error;
339  ret := ret * base + tmp;
340  end loop;
341  return ret;
342  end function int;
343 
344 
345  -- convert integer to string, using base 10
346  function str(int : integer) return string is
347  begin
348  return str(int, 10);
349  end str;
350 
351  -- convert a time to string
352  function str (tim : time) return string is
353  begin
354  return time'image(tim);
355  end str;
356 
357  -- converts a std_logic_vector into a hex string.
358  function hstr(slv : std_logic_vector) return string is
359  constant hexlen : integer := ite(slv'length mod 4 = 0, slv'length/4, slv'length/4 +1);
360  variable longslv : std_logic_vector(slv'length+3 downto 0) := (others => '0');
361  variable hex : string(1 to hexlen);
362  variable fourbit : std_logic_vector(3 downto 0);
363  begin
364  longslv(slv'length-1 downto 0) := slv;
365  for i in (hexlen -1) downto 0 loop
366  fourbit := longslv(((i*4)+3) downto (i*4));
367  case fourbit is
368  when "0000" => hex(hexlen -I) := '0';
369  when "0001" => hex(hexlen -I) := '1';
370  when "0010" => hex(hexlen -I) := '2';
371  when "0011" => hex(hexlen -I) := '3';
372  when "0100" => hex(hexlen -I) := '4';
373  when "0101" => hex(hexlen -I) := '5';
374  when "0110" => hex(hexlen -I) := '6';
375  when "0111" => hex(hexlen -I) := '7';
376  when "1000" => hex(hexlen -I) := '8';
377  when "1001" => hex(hexlen -I) := '9';
378  when "1010" => hex(hexlen -I) := 'A';
379  when "1011" => hex(hexlen -I) := 'B';
380  when "1100" => hex(hexlen -I) := 'C';
381  when "1101" => hex(hexlen -I) := 'D';
382  when "1110" => hex(hexlen -I) := 'E';
383  when "1111" => hex(hexlen -I) := 'F';
384  when "ZZZZ" => hex(hexlen -I) := 'z';
385  when "UUUU" => hex(hexlen -I) := 'u';
386  when "XXXX" => hex(hexlen -I) := 'x';
387  when others => hex(hexlen -I) := '?';
388  end case;
389  end loop;
390 -- print("HSTR Out: " & hex(1 to hexlen));
391  return hex(1 to hexlen);
392  end hstr;
393 
394  ---------------------------------------------------------------------------------------------------------------------
395  -- functions to manipulate strings
396  ---------------------------------------------------------------------------------------------------------------------
397 
398  -- convert a character to upper case
399  function toUpper(c : character) return character is
400  variable u : character;
401  begin
402  case c is
403  when 'a' => u := 'A';
404  when 'b' => u := 'B';
405  when 'c' => u := 'C';
406  when 'd' => u := 'D';
407  when 'e' => u := 'E';
408  when 'f' => u := 'F';
409  when 'g' => u := 'G';
410  when 'h' => u := 'H';
411  when 'i' => u := 'I';
412  when 'j' => u := 'J';
413  when 'k' => u := 'K';
414  when 'l' => u := 'L';
415  when 'm' => u := 'M';
416  when 'n' => u := 'N';
417  when 'o' => u := 'O';
418  when 'p' => u := 'P';
419  when 'q' => u := 'Q';
420  when 'r' => u := 'R';
421  when 's' => u := 'S';
422  when 't' => u := 'T';
423  when 'u' => u := 'U';
424  when 'v' => u := 'V';
425  when 'w' => u := 'W';
426  when 'x' => u := 'X';
427  when 'y' => u := 'Y';
428  when 'z' => u := 'Z';
429  when others => u := c;
430  end case;
431  return u;
432  end toUpper;
433 
434  -- convert a character to lower case
435  function toLower(c : character) return character is
436  variable l : character;
437  begin
438  case c is
439  when 'A' => l := 'a';
440  when 'B' => l := 'b';
441  when 'C' => l := 'c';
442  when 'D' => l := 'd';
443  when 'E' => l := 'e';
444  when 'F' => l := 'f';
445  when 'G' => l := 'g';
446  when 'H' => l := 'h';
447  when 'I' => l := 'i';
448  when 'J' => l := 'j';
449  when 'K' => l := 'k';
450  when 'L' => l := 'l';
451  when 'M' => l := 'm';
452  when 'N' => l := 'n';
453  when 'O' => l := 'o';
454  when 'P' => l := 'p';
455  when 'Q' => l := 'q';
456  when 'R' => l := 'r';
457  when 'S' => l := 's';
458  when 'T' => l := 't';
459  when 'U' => l := 'u';
460  when 'V' => l := 'v';
461  when 'W' => l := 'w';
462  when 'X' => l := 'x';
463  when 'Y' => l := 'y';
464  when 'Z' => l := 'z';
465  when others => l := c;
466  end case;
467  return l;
468  end toLower;
469 
470  -- convert a string to upper case
471  function toUpper(s : string) return string is
472  variable uppercase : string (s'range);
473  begin
474  for i in s'range loop
475  uppercase(i) := toUpper(s(i));
476  end loop;
477  return uppercase;
478  end toUpper;
479 
480  -- convert a string to lower case
481  function toLower(s : string) return string is
482  variable lowercase : string (s'range);
483  begin
484  for i in s'range loop
485  lowercase(i) := toLower(s(i));
486  end loop;
487  return lowercase;
488  end toLower;
489 
490  ---------------------------------------------------------------------------------------------------------------------
491 
492 
493  -- checks if whitespace (JFF)
494  function isWhitespace(c : character) return boolean is
495  begin
496  if (c = ' ') or (c = HT) then
497  return true;
498  else return false;
499  end if;
500  end isWhitespace;
501 
502 
503  -- remove leading whitespace (JFF)
504  function strip(s : string) return string is
505  variable stemp : string (s'range);
506  variable j, k : positive := 1;
507  begin
508  -- fill stemp with blanks
509  for i in s'range loop
510  stemp(i) := ' ';
511  end loop;
512 
513  -- find first non-whitespace in s
514  for i in s'range loop
515  if isWhitespace(s(i)) then
516  j := j + 1;
517  else exit;
518  end if;
519  end loop;
520  -- j points to first non-whitespace
521 
522  -- copy remainder of s into stemp
523  -- starting at 1
524  for i in j to s'length loop
525  stemp(k) := s(i);
526  k := k + 1;
527  end loop;
528 
529  return stemp;
530  end strip;
531 
532 
533 
534  -- return first non-whitespacesubstring (JFF)
535  function firstString(s : string) return string is
536  variable stemp : string (s'range);
537  variable s2 : string(s'range);
538  begin
539  -- fill s2 with blanks
540  for i in s'range loop
541  s2(i) := ' ';
542  end loop;
543 
544  -- remove leading whitespace
545  stemp := strip(s);
546 
547  -- copy until first whitespace
548  for i in stemp'range loop
549  if not isWhitespace(stemp(i)) then
550  s2(i) := stemp(i);
551  else exit;
552  end if;
553  end loop;
554 
555  return s2;
556  end firstString;
557 
558 
559  -- removes first non-whitespace string from a string (JFF)
560  procedure chomp(variable s : inout string; variable shead : out string) is
561  variable stemp : string (s'range);
562  variable stemp2 : string (s'range);
563  variable j : positive := 1;
564  variable k : positive := 1;
565  begin
566  -- fill stemp and stemp2 with blanks
567  for i in s'range loop
568  stemp(i) := ' '; stemp2(i) := ' ';
569  end loop;
570 
571  stemp := strip(s);
572  shead := firstString(stemp);
573 
574  -- find first whitespace in stemp
575  for i in stemp'range loop
576  if not isWhitespace(stemp(i)) then
577  j := j + 1;
578  else exit;
579  end if;
580  end loop;
581  -- j points to first whitespace
582 
583  -- copy remainder of stemp into stemp2
584  -- starting at 1
585  for i in j to stemp'length loop
586  stemp2(k) := stemp(i);
587  k := k + 1;
588  end loop;
589 
590  s := stemp2;
591  end chomp;
592 
593 
594 
595  -- functions to convert strings into other types
596  ---------------------------------------------------------------------------------------------------------------------
597 
598  -- converts a character into a std_logic
599  function toSl(c : character) return std_logic is
600  variable sl : std_logic;
601  begin
602  case c is
603  when 'U' =>
604  sl := 'U';
605  when 'X' =>
606  sl := 'X';
607  when '0' =>
608  sl := '0';
609  when '1' =>
610  sl := '1';
611  when 'Z' =>
612  sl := 'Z';
613  when 'W' =>
614  sl := 'W';
615  when 'L' =>
616  sl := 'L';
617  when 'H' =>
618  sl := 'H';
619  when '-' =>
620  sl := '-';
621  when others =>
622  sl := 'X';
623  end case;
624  return sl;
625  end toSl;
626 
627 
628  -- converts a string into std_logic_vector
629  function toSlv(s : string) return std_logic_vector is
630  variable slv : std_logic_vector(s'high-s'low downto 0);
631  variable k : integer;
632  begin
633  k := s'high-s'low;
634  for i in s'range loop
635  slv(k) := toSl(s(i));
636  k := k - 1;
637  end loop;
638  return slv;
639  end toSlv;
640 
641  ---------------------------------------------------------------------------------------------------------------------
642  -- file I/O --
643  ---------------------------------------------------------------------------------------------------------------------
644 
645  -- read variable length string from input file
646  procedure strRead(file in_file : text;
647  res_string : out string) is
648  variable l : line;
649  variable c : character;
650  variable is_string : boolean;
651  begin
652  readline(in_file, l);
653  -- clear the contents of the result string
654  for i in res_string'range loop
655  res_string(i) := ' ';
656  end loop;
657  -- read all characters of the line, up to the length
658  -- of the results string
659  for i in res_string'range loop
660  read(l, c, is_string);
661  res_string(i) := c;
662  if not is_string then -- found end of line
663  exit;
664  end if;
665  end loop;
666  end strRead;
667 
668 
669  -- print string to a file
670  procedure print(file out_file : text;
671  new_string : in string) is
672  variable l : line;
673  begin
674  write(l, new_string);
675  writeline(out_file, l);
676  end print;
677 
678 
679  -- print character to a file and start new line
680  procedure print(file out_file : text;
681  char : in character) is
682  variable l : line;
683  begin
684  write(l, char);
685  writeline(out_file, l);
686  end print;
687 
688  -- appends contents of a string to a file until line feed occurs
689  -- (LF is considered to be the end of the string)
690  procedure strWrite(file out_file : text;
691  new_string : in string) is
692  begin
693  for i in new_string'range loop
694  print(out_file, new_string(i));
695  if new_string(i) = LF then -- end of string
696  exit;
697  end if;
698  end loop;
699  end strWrite;
700 
701 
702 end TextUtilPkg;
703 
704 
705 
706 
string hstrslv,
Definition: TextUtilPkg.vhd:66
std_logic sl
Definition: StdRtlPkg.vhd:28
integer intc,
Definition: TextUtilPkg.vhd:51
strReadres_string,
chomps,shead,
Definition: TextUtilPkg.vhd:95
std_logic toSlc,
character toUpperc,
Definition: TextUtilPkg.vhd:73
character chrsl,
Definition: TextUtilPkg.vhd:35
character toLowerc,
Definition: TextUtilPkg.vhd:76
string strips,
Definition: TextUtilPkg.vhd:88
std_logic_vector toSlvs,
string strsl,
Definition: TextUtilPkg.vhd:38
boolean isWhitespacec,
Definition: TextUtilPkg.vhd:85
string firstStrings,
Definition: TextUtilPkg.vhd:91
std_logic_vector slv
Definition: StdRtlPkg.vhd:29