📄 stringlib.vhd
字号:
-- -- _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/-- _/ _/ ____________________________________________ -- _/ _/ / / -- _/_/ _/ / NAND01GR3B / -- _/_/_/ _/ / / -- _/_/ _/ / 1Gbit / -- _/_/ _/ / 8 bit, 2112 Byte Page, 1.8 V, NAND / -- _/ _/ / / -- _/ _/ / VHDL Behavioral Model / -- _/ _/ / Version 3.0 / -- _/_/ _/ / /-- _/_/_/ _/ / Copyright (c) 2006 STMicroelectronics / -- _/_/_/ _/ /___________________________________________/ -- _/_/_/_/_/ _/ -- ---- String Package Utility -- Author: Giampaolo Giacopelli-- beta release 0.4 28/10/2002LIBRARY IEEE; Use IEEE.std_logic_1164.all; Use IEEE.std_logic_TextIO.all; Use IEEE.Std_Logic_Arith.conv_std_logic_vector;Library Std; Use STD.TextIO.right; Use STD.TextIO.WRITE; Use STD.TextIO.LINE; Use STD.TextIO.OUTPUT; Use STD.TextIO.DEALLOCATE;Package StringLib is Constant MAXCHARTIME : Integer := 20; Constant MAXCHARINTEGER : Integer := 20; Constant MAXCHARIDENTIFIER : Integer := 32; Constant MAXCHARSUBSTRING : Integer := 80; Function SpaceString(size: Natural) Return string; Procedure PrintString(str: string); Function StrLen(str: String) Return Integer; Function StrPos(str: String; i: Integer) Return Character; Procedure StrCat(str: String; substr: inout String; l: Integer; r: Integer); Procedure Line2String (Variable L: in line; Variable s: out string); Function CharNoCase(c: Character) Return Character; Function StrCmpNoCase(d: String; s: String) Return Integer; Function StrCmp(d: String; s: String) Return Integer; Function SearchChar(Str:String; Init: Integer; ch: Character) Return Integer; Function SearchBackChar(Str:String; Init: Integer; ch: Character) Return Integer; Function SearchPosNumber(Str:String; Init: Integer) Return Integer; Function SearchBackPosNumber(Str:String; Init: Integer) Return Integer; Function SearchPosIdentifier(Str:String; Init: Integer) Return Integer; Function SearchBackPosIdentifier(Str:String; Init: Integer) Return Integer; Procedure SearchNumber(Str:string; Sout: inout string;Init: integer); Procedure SearchIdentifier(Str:string; Sout: inout string;Init: integer); Function Slv2Str(value : Std_Logic_Vector) return String; Function Str2Hex(value : String) return String; Function Slv2Hex(value :Std_Logic_Vector) return String; Function Hex2Slv(str : String) Return Std_Logic_Vector; Function Time2Str(value: Time) Return String; Function Str2Time(str: String) Return Time; Function Int2Str(value: Integer) Return String; Function Str2Int(str: String) Return Integer; Function int2bit(value: integer) return bit; Function bit2int(b: bit) return integer; Function int2vbit(value: integer; size: Integer) return bit_vector; Function vbit2int(b: bit_vector) return integer;-------------------------------------------------------------------------------- -- Utility Function Function int2charHex(i: Integer) return character; Function char2quad_bits(c: character) return std_logic_vector; Function slv2int(value: std_logic_vector) return integer; Function Int2Hex(Value: Integer) return string; Function Int2Hexdata(Value: Integer) return string;--------------------------------------------------------------------------------End StringLib; -- Declaritive Parte of StringUtilPackage body StringLib is ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Function int2charHex(i: Integer) return character is Variable result : character ; Begin case i is when 0 => result := '0'; when 1 => result := '1'; when 2 => result := '2'; when 3 => result := '3'; when 4 => result := '4'; when 5 => result := '5'; when 6 => result := '6'; when 7 => result := '7'; when 8 => result := '8'; when 9 => result := '9'; when 10 => result := 'A'; when 11 => result := 'B'; when 12 => result := 'C'; when 13 => result := 'D'; when 14 => result := 'E'; when 15 => result := 'F'; when others => result := 'X'; end case; return result; end int2charHex; --This function converts ascii-hex character into a std_logic_vector-- function char2quad_bits(c: character) return std_logic_vector is variable result : std_logic_vector(3 downto 0); begin case c is when '0' => result := "0000"; when '1' => result := "0001"; when '2' => result := "0010"; when '3' => result := "0011"; when '4' => result := "0100"; when '5' => result := "0101"; when '6' => result := "0110"; when '7' => result := "0111"; when '8' => result := "1000"; when '9' => result := "1001"; when 'A' => result := "1010"; when 'B' => result := "1011"; when 'C' => result := "1100"; when 'D' => result := "1101"; when 'E' => result := "1110"; when 'F' => result := "1111"; when 'a' => result := "1010"; when 'b' => result := "1011"; when 'c' => result := "1100"; when 'd' => result := "1101"; when 'e' => result := "1110"; when 'f' => result := "1111"; when 'x' => result := "XXXX"; when 'X' => result := "XXXX"; when 'z' => result := "ZZZZ"; when 'Z' => result := "ZZZZ"; when others => result := "0000"; end case; return result; end char2quad_bits; -- This function converts std_logic_vector into integer value. function slv2int(value: std_logic_vector) return integer is variable sum : integer := 0; begin sum := 0; for i in value'range loop if value(i) = '1' then sum := (sum * 2) + 1; elsif value(i) = '0' then sum:= sum*2; end if; end loop; return (sum); end slv2int; Function Int2Hex(Value: Integer) return string is Constant NUMCHARHEXADDR : Natural := (Value / 16) + 1; Variable divValue, modValue, i: Integer; Variable HexString: String(1 to NUMCHARHEXADDR):=(Others => '0' ); Variable revHexString: String(1 to NUMCHARHEXADDR); Variable HexLine: Line; begin divValue := Value; while (divValue>15) loop modValue := divValue mod 16; Std.TextIO.WRITE(HexLine, int2charHex(modValue),right); divValue := divValue / 16; end loop; Std.TextIO.WRITE(HexLine, int2charHex(divValue)); HexString(HexLine.all'Range) := HexLine.all; i:=1; while (i<=HexString'length) loop revHexString(i):=HexString(HexString'length - i + 1); i := i + 1; end loop; Deallocate(HexLine); Return revHexString; end Int2Hex; Function Int2Hexdata(Value: Integer) return string is Constant NUMCHARHEXDATA : Natural := (Value / 16) + 1; Variable divValue, modValue, i: Integer; Variable HexString: String(1 to NUMCHARHEXDATA):=(Others => '0' ); Variable revHexString: String(1 to NUMCHARHEXDATA); Variable HexLine: Line; begin divValue := Value; while (divValue>15) loop modValue := divValue mod 16; Std.TextIO.WRITE(HexLine, int2charHex(modValue),right); divValue := divValue / 16; end loop; Std.TextIO.WRITE(HexLine, int2charHex(divValue)); HexString(HexLine.all'Range) := HexLine.all; i:=1; while (i<=HexString'length) loop revHexString(i):=HexString(HexString'length - i + 1); i := i + 1; end loop; Deallocate(HexLine); Return revHexString; end Int2Hexdata; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Function SpaceString(size: Natural) Return string is Variable str: string(1 to size); begin str:=(Others => ' '); Return str; end SpaceString; Procedure PrintString(str: String) is Variable OutLine: Line; Begin Std.TextIO.WRITE(OutLine, str); Std.TextIO.WriteLine(Output, OutLine); deallocate(OutLine); End PrintString; Function StrLen(str: String) Return Integer is Variable i:Integer; Begin i:=str'length; if (str(i)/=NUL) then Return i;end if; i:=1; while (str(i) /= NUL) loop i := i + 1; end loop; Return i-1; End StrLen; Function StrPos(str: String; i: Integer) Return Character is Begin if (i>=str'left) and (i<=str'right) then Return str(i); else Return NUL; -- PrintString(String'("Error in StrPos: index out of range")); End If; end StrPos; Procedure StrCat(str: String; substr: inout String; l: Integer; r: Integer) is Variable i,li,ri: Integer; Begin li:=l;ri:=r; if (li < str'left) then li := str'left; end if; if (ri > str'right) then ri := str'right; end if; if (ri > li) then i := li; while (i<=ri) loop if i-li+1 > substr'right then exit; end if; substr(i - li + 1):=str(i); i:=i+1; end loop; if ri-li+1 < substr'right then substr(ri-li+2) := NUL; end if; end if; end StrCat; Function CharNoCase (c: Character) Return Character is Begin If character'pos(c) >= character'pos('A') and character'pos(c) <= character'pos('Z') Then Return character'val(character'pos(c) + (character'pos('a') - character'pos('A'))); End If; Return c; End CharNoCase; Function StrCmpNoCase(d: String; s: String) Return Integer is Variable i: integer:=1; Variable dc,sc: Character ; Begin Loop If i<=d'right Then dc:=d(i); Else dc:=NUL; End If; If i<=s'right Then sc:=s(i); Else sc:=NUL; End If; If CharNoCase(dc)/=CharNoCase(sc) or dc=NUL Then Return (character'pos(dc) - character'pos(sc)); Else i:=i+1; End If; End Loop; End StrCmpNoCase; Function StrCmp(d: String; s: String) Return Integer is Variable i: integer:=1; Variable dc,sc: Character ; Begin Loop
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -