-- Copyright 1992-1997 Future Parallel -- Source: T:/IC_Design/generic/VHDL/string_plus.bdy -- VLSI Design Lab -- Designer: Tim Pagden -- Description: Extensions to the std.standard.string package to allow -- conversion functions and mixed operators -- Revision history: T:/IC_Design/generic/etc/string_plus_bdy.dnh -- 29.12.97 TP 0.0 created from string_plus.pkg library IEEE; library vfp; package body string_plus is use std.textio.all; use IEEE.std_logic_1164.all; -- extensions to std.standard.character and std.standard.string -- character and string defined in std.standard -- text operators -- defined in std.standard -- general purpose functions -- none yet defined -- software functions -- none yet defined -- text functions -- function split (a: string; str_array: string_list; separator: string) return integer; -- 15.11.95 -- function is_vowel (a: character) return boolean; -- 15.11.95 -- function ordinate (a: character) return integer; -- 15.11.95 -- conversion functions --function to_string (a: std_ulogic_vector) return string; -- 29.11.95 --function to_string (a: std_logic_vector) return string; -- 03.02.95 -- function to_string (a: twos_complement) return string; -- 29.11.95 --function to_character (a: std_ulogic) return character; -- 29.12.97 --function to_character (a: std_logic) return character; -- 29.12.97 -- mixed operators --function "&" (a: string; b: integer) return string; --function "&" (a: string; b: std_logic_vector) return string; function "&" (a: string; b: integer) return string is use std.textio.all; use vfp.integer_class.all; --variable intline : line; variable b_string : string(1 to string_length(b)); variable op_string : string(1 to a'LENGTH+string_length(b)); begin -- write(intline, b); -- read(intline, b_string); b_string := to_string(b); op_string := a & b_string; -- deallocate(intline); return op_string; end "&"; function to_string (a: integer) return string is use std.textio.all; use vfp.integer_class.all; variable a_int : integer; variable int_line : line; variable result : string(1 to integer_charlength(a)); -- variable result : string(1 to string_length(a)); -- variable result : string(1 to string_length(a)+3); variable read_error : boolean; begin a_int := a; write(int_line, a_int); read(int_line, result, read_error); -- result := real_line.all; assert read_error report "Error reading result from int_line" -- , read back --" & result & "--" severity error; deallocate(int_line); -- return result; -- end to_string; -- function to_string (a: real) return string is use std.textio.all; use vfp.real_class.all; variable a_real : real; variable real_line : line; -- variable a_string : string(1 to integer_charlength(b)); -- by defining a variable dependent upon a variable, you get to see -- the behaviour of the string_length function in the simulation variable num_chars : integer := string_length(a); variable result : string(1 to num_chars); -- variable result : string(1 to string_length(a)+3); variable read_error : boolean; -- variable num_chars : integer := 0; variable num_char_length : line; -- variable num_chars_as_string : string(1 to 2); begin a_real := a; write(real_line, a_real); -- write(real_line, a_real); -- num_chars := real_line'LENGTH; -- writeline(OUTPUT, real_line); -- write(num_char_length, num_chars); -- writeline(OUTPUT, num_char_length); result := real_line.all; -- read(real_line, result, read_error); -- assert FALSE -- report "result is " & result -- severity note; -- result := real_line.all; -- assert read_error -- report "Error reading result from real_line" -- , read back --" & result & "--" -- severity error; -- deallocate(real_line); -- return result; -- end to_string; function to_string (a: time) return string is use std.textio.all; use vfp.time_class.all; variable a_time : time; variable time_line : line; -- variable a_string : string(1 to integer_charlength(b)); variable result : string(1 to string_length(a)); -- (a)+2 variable read_error : boolean; begin a_time := a; write(time_line, a_time); read(time_line, result, read_error); -- result := real_line.all; deallocate(time_line); return result; end to_string; function to_character ( -- a: std_ulogic -- ) return character is -- variable a_char : character; -- begin -- case a is -- when 'U' => a_char := 'U'; -- when 'X' => a_char := 'X'; -- when '0' => a_char := '0'; -- when '1' => a_char := '1'; -- when 'W' => a_char := 'W'; -- when 'L' => a_char := 'L'; -- when 'H' => a_char := 'H'; -- when 'Z' => a_char := 'Z'; -- when '-' => a_char := '-'; -- end case; -- return a_char; -- end to_character; -- -- function to_character ( -- b: std_logic -- ) return character is -- variable a_char : character; -- begin -- case b is -- when 'U' => a_char := 'U'; -- when 'X' => a_char := 'X'; -- when '0' => a_char := '0'; -- when '1' => a_char := '1'; -- when 'W' => a_char := 'W'; -- when 'L' => a_char := 'L'; -- when 'H' => a_char := 'H'; -- when 'Z' => a_char := 'Z'; -- when '-' => a_char := '-'; -- end case; -- return a_char; -- end to_character; -- procedure write ( -- l: inout line; -- value : in std_logic_vector; -- justified: in side := right; -- field: in width := 0 -- ) is -- variable a: std_logic_vector(1 to value'length) := value; -- variable value_str: string(1 to value'length); -- begin -- for i in value_str'range loop -- value_str(i) := to_character(std_logic(value(i))); -- end loop; -- write (l, value_str, justified, field); -- end write; function to_string ( -- 29.11.95 a: std_ulogic_vector ) return string is constant a_length : integer := a'length; variable a_str: string(1 to a'length); variable a_copy: std_ulogic_vector(a'length-1 to 0) := a; -- a copy is made of a because a may not use a 0-based index, -- it could be 7 downto 4, for example begin -- convert each bit of the vector to a char for i in 1 to a'length loop -- conversion is done by assigning from the msb of a_copy from the -- left downto the lsb so that the textual "image" of a_str follows -- that of a_copy and thus a itself. Without the length-i trick -- a_copy would effectively be reversed onto a_str as a_str is declared -- with a "to" range as opposed to a_copy's downto range. a_str(i) := to_character(std_ulogic(a_copy(a_length-i))); end loop; return a_str; end; procedure write ( l : inout line; value : in std_ulogic_vector; justified : in side := right; field : in width := 0 ) is variable a_str: string(1 to value'length); begin a_str := to_string(value); write (l, a_str, justified, field); end write; end string_plus;