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 ------------------------------------------------------------------------------ 19 use ieee.std_logic_1164.
all;
25 --! @ingroup base_general 27 -- prints a message to the screen 30 -- prints the message when active 31 -- useful for debug switches 32 procedure print(
active : ; text : );
34 -- converts std_logic into a character 37 -- converts std_logic into a string (1 to 1) 40 -- converts std_logic_vector into a string (binary base) 43 -- converts boolean into a string 44 function str(b : )
return ;
46 -- converts an integer into a single character 47 -- (can also be used for hex conversion and other bases) 48 function chr(int : )
return ;
50 -- Converts a character into an integer 51 function int(c : )
return ;
53 -- converts integer into string using specified base 54 function str(int : ;
base : )
return ;
56 -- converts a string with specified base into an integer 57 function int(s : ;
base : )
return ;
59 -- converts integer to string, using base 10 60 function str(int : )
return ;
62 -- converts a time to a string 63 function str (tim : )
return ;
65 -- convert std_logic_vector into a string in hex format 68 ---------------------------------- 69 -- functions to manipulate strings 70 ----------------------------------- 72 -- convert a character to upper case 75 -- convert a character to lower case 78 -- convert a string to upper case 81 -- convert a string to lower case 84 -- checks if whitespace (JFF) 87 -- remove leading whitespace (JFF) 90 -- return first nonwhitespace substring (JFF) 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 ;
variable shead :
out );
98 -------------------------------------------------- 99 -- functions to convert strings into other formats 100 -------------------------------------------------- 102 -- converts a character into std_logic 105 -- converts a string into std_logic_vector 113 -- read variable length string from input file 117 -- print string to a file and start new line 118 procedure print(
file out_file : text;
121 -- print character to a file and start new line 122 procedure print(
file out_file : text;
131 -- prints text to the screen 133 variable msg_line : line;
135 write
(msg_line, text
);
136 writeline
(output, msg_line
);
139 -- prints text to the screen when active 140 procedure print(
active : ; text : )
is 147 -- converts std_logic into a character 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 := '-';
165 -- converts std_logic into a string (1 to 1) 167 variable s :
(1 to 1);
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) 178 variable result :
(1 to slv'
length);
182 for i
in slv'
range loop 183 result
(r
) := chr
(slv(i
));
189 -- Converts a boolean into "true" or "false" 190 function str(b : )
return is 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 : )
return is 244 when others => c := '?';
249 -- Convert a character into an integer. 250 function int (c : )
return 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;
295 -- convert integer to string using specified base 296 -- (adapted from Steve Vogwell's posting in comp.lang.vhdl) 297 function str(int : ;
base : )
return is 298 variable temp :
(1 to 10);
302 variable power : :=
1;
304 -- bug fix for negative numbers 308 while num >=
base loop -- Determine how many 309 len := len +
1;
-- characters required 310 num := num /
base;
-- to represent the 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 318 -- return result and add sign if required 320 return '-'& temp
(1 to len
);
322 return temp
(1 to len
);
327 -- Convert a string and base into an integer. 328 function int (s : ;
base : )
return is 333 for i
in s'
range loop 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." 339 ret := ret *
base + tmp;
345 -- convert integer to string, using base 10 346 function str(int : )
return is 351 -- convert a time to string 352 function str (tim : )
return is 357 -- converts a std_logic_vector into a hex string. 359 constant hexlen : := ite
(slv'
length mod 4 =
0,
slv'
length/
4,
slv'
length/
4 +
1);
360 variable longslv :
(slv'
length+
3 downto 0) :=
(others => '
0'
);
361 variable hex :
(1 to hexlen
);
362 variable fourbit :
(3 downto 0);
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));
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
) := '?';
390 -- print("HSTR Out: " & hex(1 to hexlen)); 391 return hex
(1 to hexlen
);
394 --------------------------------------------------------------------------------------------------------------------- 395 -- functions to manipulate strings 396 --------------------------------------------------------------------------------------------------------------------- 398 -- convert a character to upper case 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;
434 -- convert a character to lower case 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;
470 -- convert a string to upper case 472 variable uppercase :
(s'
range);
474 for i
in s'
range loop 475 uppercase
(i
) := toUpper
(s
(i
));
480 -- convert a string to lower case 482 variable lowercase :
(s'
range);
484 for i
in s'
range loop 485 lowercase
(i
) := toLower
(s
(i
));
490 --------------------------------------------------------------------------------------------------------------------- 493 -- checks if whitespace (JFF) 496 if (c = ' '
) or (c = HT
) then 503 -- remove leading whitespace (JFF) 505 variable stemp :
(s'
range);
506 variable j, k : positive :=
1;
508 -- fill stemp with blanks 509 for i
in s'
range loop 513 -- find first non-whitespace in s 514 for i
in s'
range loop 515 if isWhitespace
(s
(i
)) then 520 -- j points to first non-whitespace 522 -- copy remainder of s into stemp 524 for i
in j
to s'
length loop 534 -- return first non-whitespacesubstring (JFF) 536 variable stemp :
(s'
range);
537 variable s2 :
(s'
range);
539 -- fill s2 with blanks 540 for i
in s'
range loop 544 -- remove leading whitespace 547 -- copy until first whitespace 548 for i
in stemp'
range loop 549 if not isWhitespace
(stemp
(i
)) then 559 -- removes first non-whitespace string from a string (JFF) 560 procedure chomp(
variable s :
inout ;
variable shead :
out )
is 561 variable stemp :
(s'
range);
562 variable stemp2 :
(s'
range);
563 variable j : positive :=
1;
564 variable k : positive :=
1;
566 -- fill stemp and stemp2 with blanks 567 for i
in s'
range loop 568 stemp
(i
) := ' '; stemp2
(i
) := ' ';
572 shead := firstString
(stemp
);
574 -- find first whitespace in stemp 575 for i
in stemp'
range loop 576 if not isWhitespace
(stemp
(i
)) then 581 -- j points to first whitespace 583 -- copy remainder of stemp into stemp2 585 for i
in j
to stemp'
length loop 586 stemp2
(k
) := stemp
(i
);
595 -- functions to convert strings into other types 596 --------------------------------------------------------------------------------------------------------------------- 598 -- converts a character into a std_logic 628 -- converts a string into std_logic_vector 630 variable slv :
(s'
high-s'
low downto 0);
634 for i
in s'
range loop 635 slv(k
) := toSl
(s
(i
));
641 --------------------------------------------------------------------------------------------------------------------- 643 --------------------------------------------------------------------------------------------------------------------- 645 -- read variable length string from input file 647 res_string :
out )
is 650 variable is_string : ;
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
) := ' ';
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
);
662 if not is_string
then -- found end of line 669 -- print string to a file 670 procedure print(
file out_file : text;
674 write
(l, new_string
);
675 writeline
(out_file, l
);
679 -- print character to a file and start new line 680 procedure print(
file out_file : text;
685 writeline
(out_file, l
);
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;
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