📄 txt_util2.vhd
字号:
when 'p' => u := 'P'; when 'q' => u := 'Q'; when 'r' => u := 'R'; when 's' => u := 'S'; when 't' => u := 'T'; when 'u' => u := 'U'; when 'v' => u := 'V'; when 'w' => u := 'W'; when 'x' => u := 'X'; when 'y' => u := 'Y'; when 'z' => u := 'Z'; when others => u := c; end case; return u; end to_upper; -- convert a character to lower case function to_lower(c: character) return character is variable l: character; begin case c is when 'A' => l := 'a'; when 'B' => l := 'b'; when 'C' => l := 'c'; when 'D' => l := 'd'; when 'E' => l := 'e'; when 'F' => l := 'f'; when 'G' => l := 'g'; when 'H' => l := 'h'; when 'I' => l := 'i'; when 'J' => l := 'j'; when 'K' => l := 'k'; when 'L' => l := 'l'; when 'M' => l := 'm'; when 'N' => l := 'n'; when 'O' => l := 'o'; when 'P' => l := 'p'; when 'Q' => l := 'q'; when 'R' => l := 'r'; when 'S' => l := 's'; when 'T' => l := 't'; when 'U' => l := 'u'; when 'V' => l := 'v'; when 'W' => l := 'w'; when 'X' => l := 'x'; when 'Y' => l := 'y'; when 'Z' => l := 'z'; when others => l := c; end case; return l; end to_lower; -- convert a string to upper case 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_logicfunction 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_vectorfunction 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; ------------------ 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 fileprocedure 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 lineprocedure 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;--======= EXTENSION ===========================================================---------------------------------------------------- a package of utilities for example testbench-------------------------------------------------- ------------------------------------------- -- convert a std_logic value to a character ------------------------------------------- type stdlogic_to_char_t is array(std_logic) of character; constant to_char : stdlogic_to_char_t := ( 'U' => 'U', 'X' => 'X', '0' => '0', '1' => '1', 'Z' => 'Z', 'W' => 'W', 'L' => 'L', 'H' => 'H', '-' => '-'); ----------------------------------------- -- convert a std_logic_vector to a string ----------------------------------------- function slv_to_string(inp : std_logic_vector) return string is alias vec : std_logic_vector(1 to inp'length) is inp; variable result : string(vec'range); begin for i in vec'range loop result(i) := to_char(vec(i)); end loop; return result; end; ------------------------------------------- -- convert a character to a std_logic value ------------------------------------------- type char_to_stdlogic_t is array(character) of std_logic; constant to_std_logic2 : char_to_stdlogic_t := ( 'U' => 'U', 'X' => 'X', '0' => '0', '1' => '1', 'Z' => 'Z', 'W' => 'W', 'L' => 'L', 'H' => 'H', '-' => '-', others => 'X'); --------------------------------------- -- convert a string to std_logic_vector --------------------------------------- function string_to_slv(s : string) return std_logic_vector is variable result : std_logic_vector(s'length - 1 downto 0); variable j : integer; begin j := s'length -1; for i in s'range loop result(j) := to_std_logic2(s(i)); j := j -1; end loop; return result; end function; -------------------------------------------------------------- -- convert a super-hex digit to a 4-bit std_logic_vector slice -------------------------------------------------------------- type hex_to_slv4_t is array(character) of std_logic_vector(3 downto 0); constant hex_to_slv4 : hex_to_slv4_t := ( '0' => "0000", '1' => "0001", '2' => "0010", '3' => "0011", '4' => "0100", '5' => "0101", '6' => "0110", '7' => "0111", '8' => "1000", '9' => "1001", 'a' | 'A' => "1010", 'b' | 'B' => "1011", 'c' | 'C' => "1100", 'd' | 'D' => "1101", 'e' | 'E' => "1110", 'f' | 'F' => "1111", 'x' | 'X' => "XXXX", 'z' | 'Z' => "ZZZZ", '-' => "----", others => "XXXX"); ------------------------------------------- -- convert a hex string to std_logic_vector ------------------------------------------- function hex_string_to_slv(s : string) return std_logic_vector is variable lbd : natural := (s'length * 4) -1; variable result : std_logic_vector(lbd downto 0); variable j : integer; begin j := lbd; for i in s'range loop result(j downto j - 3) := hex_to_slv4(s(i)); j := j - 4; end loop; return result; end function; --------------------------------- -- skip white space on input line --------------------------------- procedure skip_white( variable lp : inout line ) is variable ch : character; begin while (lp(lp'left) = ' ') or (lp(lp'left) = HT) loop read(lp, ch); end loop; end procedure skip_white;--========================================================================end txt_util2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -