25 use ieee.std_logic_1164.
all;
39 procedure print(
active:
boolean; text:
string);
49 function chr(sl:
std_logic)
return character;
52 function str(sl:
std_logic)
return string;
55 function str(slv:
std_logic_vector)
return string;
58 function str(b:
boolean)
return string;
62 function chr(int:
integer)
return character;
65 function str(int:
integer;
base:
integer)
return string;
68 function str(int:
integer)
return string;
71 function hstr(slv:
std_logic_vector)
return string;
76 function to_upper(c:
character)
return character;
79 function to_lower(c:
character)
return character;
98 procedure chomp(
variable s:
inout string;
variable shead:
out string);
125 res_string:
out string
136 new_string:
in string
157 variable msg_line : line;
159 write
(msg_line, text
);
160 writeline
(output, msg_line
);
165 procedure print(
active:
boolean; text:
string)
is
176 function chr(sl:
std_logic)
return character is
177 variable c :
character;
208 function str(sl:
std_logic)
return string is
209 variable s :
string(1 to 1);
221 function str(slv:
std_logic_vector)
return string is
222 variable result :
string (1 to slv'
length);
223 variable r :
integer;
226 for i
in slv'
range loop
227 result
(r
) := chr
(slv
(i
));
235 function str(b:
boolean)
return string is
254 function chr(int:
integer)
return character is
255 variable c :
character;
344 function str(int:
integer;
base:
integer)
return string is
346 variable temp :
string(1 to 10);
347 variable num :
integer;
348 variable abs_int :
integer;
349 variable len :
integer :=
1;
350 variable power :
integer :=
1;
359 while num >=
base loop -- Determine how many
360 len := len +
1;
-- characters required
361 num := num /
base;
-- to represent the
364 for i
in len
downto 1 loop -- Convert the number to
365 temp
(i
) := chr
(abs_int / power
mod base);
-- a string starting
366 power := power *
base;
-- with the right hand
371 return '-'& temp
(1 to len
);
373 return temp
(1 to len
);
380 function str(int:
integer)
return string is
390 function hstr(slv:
std_logic_vector)
return string is
391 variable hexlen :
integer;
392 variable longslv :
std_logic_vector(67 downto 0) :=
(others => '
0'
);
393 variable hex :
string(1 to 16);
394 variable fourbit :
std_logic_vector(3 downto 0);
396 hexlen :=
(slv'
left +
1) /
4;
398 if (slv'
left +
1) mod 4 /=
0 then
399 hexlen := hexlen +
1;
402 longslv
(slv'
left downto 0) := slv;
403 for i
in (hexlen -
1) downto 0 loop
404 fourbit := longslv
(((i *
4) +
3) downto (i *
4));
409 hex
(hexlen - I
) := '
0';
411 hex
(hexlen - I
) := '
1';
413 hex
(hexlen - I
) := '
2';
415 hex
(hexlen - I
) := '
3';
417 hex
(hexlen - I
) := '
4';
419 hex
(hexlen - I
) := '
5';
421 hex
(hexlen - I
) := '
6';
423 hex
(hexlen - I
) := '
7';
425 hex
(hexlen - I
) := '
8';
427 hex
(hexlen - I
) := '
9';
429 hex
(hexlen - I
) := 'A';
431 hex
(hexlen - I
) := 'B';
433 hex
(hexlen - I
) := 'C';
435 hex
(hexlen - I
) := 'D';
437 hex
(hexlen - I
) := 'E';
439 hex
(hexlen - I
) := 'F';
441 hex
(hexlen - I
) := 'z';
443 hex
(hexlen - I
) := 'u';
445 hex
(hexlen - I
) := 'x';
447 hex
(hexlen - I
) := '?';
452 return hex
(1 to hexlen
);
459 function to_upper(c:
character)
return character is
461 variable u :
character;
530 function to_lower(c:
character)
return character is
532 variable l :
character;
603 variable uppercase :
string (s'
range);
607 for i
in s'
range loop
608 uppercase
(i
) := to_upper
(s
(i
));
618 variable lowercase :
string (s'
range);
622 for i
in s'
range loop
623 lowercase
(i
) := to_lower
(s
(i
));
635 if (c = ' '
) or (c = HT
) then
647 variable stemp :
string (s'
range);
648 variable j, k :
positive :=
1;
653 for i
in s'
range loop
658 for i
in s'
range loop
660 if (is_whitespace
(s
(i
))) then
671 for i
in j
to s'
length loop
678 end strip_whitespace;
684 variable stemp, s2 :
string (s'
range);
689 for i
in s'
range loop
694 stemp := strip_whitespace
(s
);
697 for i
in stemp'
range loop
699 if (not is_whitespace
(stemp
(i
))) then
713 procedure chomp(
variable s:
inout string;
variable shead:
out string)
is
715 variable stemp, stemp2 :
string (s'
range);
716 variable j, k :
positive :=
1;
721 for i
in s'
range loop
722 stemp
(i
) := ' '; stemp2
(i
) := ' ';
725 stemp := strip_whitespace
(s
);
727 shead := first_string
(stemp
);
730 for i
in stemp'
range loop
732 if (not is_whitespace
(stemp
(i
))) then
743 for i
in j
to stemp'
length loop
744 stemp2
(k
) := stemp
(i
);
757 variable sl :
std_logic;
791 variable slv :
std_logic_vector(3 downto 0);
839 variable x :
integer;
875 variable slv :
std_logic_vector(s'
high - s'
low downto 0);
876 variable k :
integer;
879 for i
in s'
range loop
880 slv
(k
) := to_std_logic
(s
(i
));
884 end to_std_logic_vector;
889 variable slv :
std_logic_vector(((s'
length*
4)-
1) downto 0) :=
(others => '
0'
);
890 variable k :
integer;
892 for i
in s'
range loop
893 slv := slv
((slv'
length -
5) downto 0) & chr_to_slv
(s
(i
));
901 variable k :
integer;
904 for i
in s'
range loop
905 k :=
(k *
10) + chr_to_int
(s
(i
));
915 res_string:
out string)
is
918 variable c :
character;
919 variable is_string :
boolean;
923 readline
(in_file, l
);
925 for i
in res_string'
range loop
926 res_string
(i
) := ' ';
930 for i
in res_string'
range loop
934 read
(l, c, is_string
);
956 procedure print(
file out_file: TEXT;
957 new_string:
in string)
is
963 write
(l, new_string
);
964 writeline
(out_file, l
);
969 procedure print(
file out_file: TEXT;
970 char:
in character)
is
977 writeline
(out_file, l
);
984 procedure str_write(
file out_file: TEXT;
985 new_string:
in string)
is
988 for i
in new_string'
range loop
989 print
(out_file, new_string
(i
));
991 if (new_string
(i
) = LF
) then -- end of string
Defines useful functions an procedures for text handling text in VHDL.
integer chr_to_intc,
converts a character into int (JFF)
std_logic_vector chr_to_slvc,
converts a hex character into std_logic_vector(JFF)
string strsl,
converts std_logic into a string (1 to 1)
boolean is_whitespacec,
checks if whitespace (JFF)
chomps,shead,
finds the first non-whitespace substring in a string and (JFF)
integer str_to_ints,
converts a decimal string into an integer (JFF)
std_logic to_std_logicc,
converts a character into std_logic
character chrsl,
converts std_logic into a character
string first_strings,
return first nonwhitespace substring (JFF)
character to_upperc,
convert a character to upper case
std_logic_vector to_std_logic_vectors,
converts a binary string into std_logic_vector
string hstrslv,
convert std_logic_vector into a string in hex format
character to_lowerc,
convert a character to lower case
string strip_whitespaces,
remove leading whitespace (JFF)
str_readres_string,
read variable length string from input file
std_logic_vector hstr_to_slvs,
converts a hex string into std_logic_vector(JFF)
printactive,text,
prints the message when active