📄 txt_util.vhd
字号:
function to_upper(s: string) return string is
variable uppercase: string (s'range);
begin
for i in s'range loop
uppercase(i):= to_upper(s(i));
end loop;
return uppercase;
end to_upper;
-- convert a string to lower case
function to_lower(s: string) return string is
variable lowercase: string (s'range);
begin
for i in s'range loop
lowercase(i):= to_lower(s(i));
end loop;
return lowercase;
end to_lower;
-- functions to convert strings into other types
-- converts a character into a std_logic
function to_std_logic(c: character) return std_logic is
variable sl: std_logic;
begin
case c is
when 'U' =>
sl := 'U';
when 'X' =>
sl := 'X';
when '0' =>
sl := '0';
when '1' =>
sl := '1';
when 'Z' =>
sl := 'Z';
when 'W' =>
sl := 'W';
when 'L' =>
sl := 'L';
when 'H' =>
sl := 'H';
when '-' =>
sl := '-';
when others =>
sl := 'X';
end case;
return sl;
end to_std_logic;
-- converts a string into std_logic_vector
function to_std_logic_vector(s: string) return std_logic_vector is
variable slv: std_logic_vector(s'high-s'low downto 0);
variable k: integer;
begin
k := s'high-s'low;
for i in s'range loop
slv(k) := to_std_logic(s(i));
k := k - 1;
end loop;
return slv;
end to_std_logic_vector;
-- converts an integer to a string
function int_to_str (value : integer; len : POSITIVE) return string is
variable ivalue : integer := 0;
variable index : integer := 0;
variable digit : integer := 0;
variable line_no: string(16 downto 1) := " ";
begin
assert len <= 16
report "ERROR : The length of the output string can not be great than 16 !"
severity ERROR;
if value > 0 THEN
ivalue := value;
elsif value < 0 THEN
ivalue := - value;
else
ivalue := 0;
end if;
index := 0;
while (ivalue > 0) loop
index := index + 1;
digit := ivalue MOD 10;
ivalue := ivalue/10;
case digit is
when 0 => line_no(index) := '0';
when 1 => line_no(index) := '1';
when 2 => line_no(index) := '2';
when 3 => line_no(index) := '3';
when 4 => line_no(index) := '4';
when 5 => line_no(index) := '5';
when 6 => line_no(index) := '6';
when 7 => line_no(index) := '7';
when 8 => line_no(index) := '8';
when 9 => line_no(index) := '9';
when others =>
ASSERT FALSE
REPORT "Illegal number!"
SEVERITY ERROR;
end case;
end loop;
if value = 0 THEN
index := 1;
line_no(index) := '0';
elsif value < 0 THEN
index := index + 1;
line_no(index) := '-';
end if;
assert len >= index
report "ERROR : The length of the output string is too small !"
severity ERROR;
return line_no(len downto 1);
end int_to_str;
-- converts a string to an integer
function str_to_int ( str : string ) return integer is
variable len : integer := str'length;
variable ivalue : integer := 0;
variable digit : integer := 0;
variable isign : integer := 0;
begin
for i in str'RANGE loop
case str(i) is
when '0' =>
digit := 0;
if isign=0 then
isign := 1;
end if;
when '1' =>
digit := 1;
if isign=0 then
isign := 1;
end if;
when '2' =>
digit := 2;
if isign=0 then
isign := 1;
end if;
when '3' =>
digit := 3;
if isign=0 then
isign := 1;
end if;
when '4' =>
digit := 4;
if isign=0 then
isign := 1;
end if;
when '5' =>
digit := 5;
if isign=0 then
isign := 1;
end if;
when '6' =>
digit := 6;
if isign=0 then
isign := 1;
end if;
when '7' =>
digit := 7;
if isign=0 then
isign := 1;
end if;
when '8' =>
digit := 8;
if isign=0 then
isign := 1;
end if;
when '9' =>
digit := 9;
if isign=0 then
isign := 1;
end if;
when ' ' | ',' =>
next;
when '+' =>
if isign=0 then
isign := 1;
else
ASSERT FALSE
REPORT "ERROR : Illegal Character '" & str(i) & "' in string parameter ! "
SEVERITY ERROR;
end if;
when '-' =>
if isign=0 then
isign := -1;
else
ASSERT FALSE
REPORT "ERROR : Illegal Character '" & str(i) & "' in string parameter ! "
SEVERITY ERROR;
end if;
when others =>
ASSERT FALSE
REPORT "ERROR : Illegal Character '" & str(i) & "' in string parameter ! "
SEVERITY ERROR;
end case;
ivalue := ivalue * 10 + digit;
end loop;
if isign=0 then
ASSERT FALSE
REPORT "ERROR : The input string is not a number string ! "
SEVERITY ERROR;
END IF;
ivalue := ivalue * isign;
return ivalue;
end str_to_int;
-- converts an integer to a hexadecimal number string
function int_to_hex_str (value : integer; len : POSITIVE) return string is
variable ivalue : integer := 0;
variable index : integer := 0;
variable digit : integer := 0;
variable line_no: string(16 downto 1) := "0000000000000000";
variable len_line : integer;
begin
assert len <= 16
report "ERROR : The length of the output string can not be great than 16 ! "
severity ERROR;
assert value >= 0
report "ERROR : The value input can not be negative ! "
severity ERROR;
ivalue := value;
index := 0;
while (ivalue > 0) loop
index := index + 1;
digit := ivalue MOD 16;
ivalue := ivalue/16;
case digit is
when 0 => line_no(index) := '0';
when 1 => line_no(index) := '1';
when 2 => line_no(index) := '2';
when 3 => line_no(index) := '3';
when 4 => line_no(index) := '4';
when 5 => line_no(index) := '5';
when 6 => line_no(index) := '6';
when 7 => line_no(index) := '7';
when 8 => line_no(index) := '8';
when 9 => line_no(index) := '9';
when 10 => line_no(index) := 'A';
when 11 => line_no(index) := 'B';
when 12 => line_no(index) := 'C';
when 13 => line_no(index) := 'D';
when 14 => line_no(index) := 'E';
when 15 => line_no(index) := 'F';
when others =>
ASSERT FALSE
REPORT "Illegal number ! "
SEVERITY ERROR;
end case;
end loop;
if ivalue=0 then
index := 1;
end if;
assert len >= index
report "ERROR : The length of the output string is too small ! "
severity ERROR;
return line_no(len downto 1);
end int_to_hex_str;
-- converts a hexadecimal number string to a integer
function hex_str_to_int (str : string) return integer is
variable len : integer := str'length;
variable ivalue : integer := 0;
variable digit : integer := 0;
begin
for i in str'RANGE loop
case str(i) is
when '0' => digit := 0;
when '1' => digit := 1;
when '2' => digit := 2;
when '3' => digit := 3;
when '4' => digit := 4;
when '5' => digit := 5;
when '6' => digit := 6;
when '7' => digit := 7;
when '8' => digit := 8;
when '9' => digit := 9;
when 'A' => digit := 10;
when 'a' => digit := 10;
when 'B' => digit := 11;
when 'b' => digit := 11;
when 'C' => digit := 12;
when 'c' => digit := 12;
when 'D' => digit := 13;
when 'd' => digit := 13;
when 'E' => digit := 14;
when 'e' => digit := 14;
when 'F' => digit := 15;
when 'f' => digit := 15;
when ' ' | ',' =>
next;
when others =>
ASSERT FALSE
REPORT "Illegal character '" & str(i) & "' in Intel Hex File ! "
SEVERITY ERROR;
end case;
ivalue := ivalue * 16 + digit;
end loop;
return ivalue;
end hex_str_to_int;
----------------
-- file I/O --
----------------
-- read variable length string from input file
procedure str_read(file in_file: TEXT;
res_string: out string) is
variable l: line;
variable c: character;
variable is_string: boolean;
begin
readline(in_file, l);
-- clear the contents of the result string
for i in res_string'range loop
res_string(i) := ' ';
end loop;
-- read all characters of the line, up to the length
-- of the results string
for i in res_string'range loop
read(l, c, is_string);
res_string(i) := c;
if not is_string then -- found end of line
exit;
end if;
end loop;
end str_read;
-- print string to a file
procedure print(file out_file: TEXT;
new_string: in string) is
variable l: line;
begin
write(l, new_string);
writeline(out_file, l);
end print;
-- print character to a file and start new line
procedure print(file out_file: TEXT;
char: in character) is
variable l: line;
begin
write(l, char);
writeline(out_file, l);
end print;
-- appends contents of a string to a file until line feed occurs
-- (LF is considered to be the end of the string)
procedure str_write(file out_file: TEXT;
new_string: in string) is
begin
for i in new_string'range loop
print(out_file, new_string(i));
if new_string(i) = LF then -- end of string
exit;
end if;
end loop;
end str_write;
end txt_util;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -