📄 stringlib.vhd
字号:
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 dc/=sc or dc=NUL Then Return (character'pos(dc) - character'pos(sc)); Else i:=i+1; End If; End Loop; End StrCmp; Function SearchChar(Str:String; Init: Integer; ch: Character) Return Integer is Variable Len : Integer; Variable i : Integer; begin Len:=StrLen(Str); i:=Init; while (i<=Len) loop if Str(i)=ch then return i; -- found end if; i:=i+1; end loop; return 0; -- not found end SearchChar; -- Search Char into a String from position Init downto position 1 Function SearchBackChar(Str:String; Init: Integer; ch: Character) Return Integer is Variable i : Integer; begin if Init<=StrLen(Str) then i:=Init; while (i>=1) loop if Str(i)=ch then return i; end if; -- found i:=i-1; end loop; end if; Return 0; end SearchBackChar; Function SearchPosNumber(Str:String; Init: Integer) Return Integer is Variable Len : Integer; Variable i : Integer; begin Len:=StrLen(Str); i:=Init; while (i<=Len) loop if (character'pos(Str(i))>=character'pos('0') and character'pos(Str(i))<=character'pos('9')) then Return i; end if; i:=i+1; end loop; return 0; -- not found end SearchPosNumber; Function SearchBackPosNumber(Str:String; Init: Integer) Return Integer is Variable i : Integer; begin if Init<=StrLen(Str) then i:=Init; while (i>=1) loop if (character'pos(Str(i))>=character'pos('0') and character'pos(Str(i))<=character'pos('9')) then Return i; end if; i:=i-1; end loop; end if; Return 0; -- not found end SearchBackPosNumber; Function SearchPosIdentifier(Str:String; Init: Integer) Return Integer is Variable Len : Integer; Variable i : Integer; begin Len:=StrLen(Str); i:=Init; while (i<=Len) loop if (character'pos(Str(i))>=character'pos('a') and character'pos(Str(i))<=character'pos('z')) or (character'pos(Str(i))>=character'pos('A') and character'pos(Str(i))<=character'pos('Z')) or (character'pos(Str(i))>=character'pos('_')) then Return i; End If; i:=i+1; end loop; return 0; -- not found end SearchPosIdentifier; Function SearchBackPosIdentifier(Str:String; Init: Integer) Return Integer is Variable i : Integer; begin if Init<=StrLen(Str) then i:=Init; while (i>=1) loop if (character'pos(Str(i))>=character'pos('a') and character'pos(Str(i))<=character'pos('z')) or (character'pos(Str(i))>=character'pos('A') and character'pos(Str(i))<=character'pos('Z')) or (character'pos(Str(i))>=character'pos('_')) then Return i; End If; i:=i-1; end loop; end if; Return 0; -- not found end SearchBackPosIdentifier; -- Search Number in a String -- non considera gli spazi trovati all'inizio e si ferma quando trova un carattere diverso -- da un numero Procedure SearchNumber(Str:string; Sout: inout string;Init: integer) is Variable i, iSout : Integer; Variable Len : Integer; Begin Len:=StrLen(Str); iSout:=0; If Len>0 then i:=Init; While i<Len loop if character'pos(Str(i)) = character'pos(' ') then i := i + 1; else exit; end if; End Loop; While i<=Len loop if (character'pos(Str(i))>=character'pos('0') and character'pos(Str(i))<=character'pos('9')) then iSout := iSout + 1; if iSout>Sout'right then exit;end if; Sout(iSout) := Str(i); i:=i+1; else exit; end if; End Loop; If (iSout>0 and iSout<Sout'right) then Sout(iSout+1):=NUL; End If; End if; End SearchNumber; -- Search Identifier in a String -- non considera gli spazi trovati all'inizio e si ferma quando trova un carattere diverso -- dall'identificatore Procedure SearchIdentifier(Str:string; Sout: inout string;Init: integer) is Variable i, iSout : Integer; Variable Len : Integer; Begin Len:=StrLen(Str); iSout:=0; If Len>0 then i:=Init; While i<Len loop if character'pos(Str(i)) = character'pos(' ') then i := i + 1; else exit; end if; End Loop; While i<=Len loop if (character'pos(Str(i))>=character'pos('a') and character'pos(Str(i))<=character'pos('z')) or (character'pos(Str(i))>=character'pos('A') and character'pos(Str(i))<=character'pos('Z')) or (character'pos(Str(i))>=character'pos('_')) then iSout := iSout + 1; if iSout>Sout'right then exit;end if; Sout(iSout) := Str(i); i:=i+1; else exit; end if; End Loop; If (iSout>0 and iSout<Sout'right) then Sout(iSout+1):=NUL; End If; End if; End SearchIdentifier; Function Slv2Str(Value : Std_Logic_Vector) return String is variable L : Line; -- access type variable W : String(1 to value'length) := (others => ' '); begin IEEE.Std_Logic_TextIO.WRITE(L, value); W(L.all'range) := L.all; Deallocate(L); return W; end Slv2Str; function Str2Hex(value : String) return String is subtype Int03_Typ is Integer range 0 to 3; variable Result : string(1 to ((value'length - 1)/4)+1) := (others => '0'); variable StrTo4 : string(1 to Result'length * 4) := (others => '0'); variable MTspace : Int03_Typ; -- Empty space to fill in variable Str4 : String(1 to 4); variable Group_v : Natural := 0; begin MTspace := Result'length * 4 - value'length; StrTo4(MTspace + 1 to StrTo4'length) := value; -- padded with '0' Cnvrt_Lbl : for I in Result'range loop Group_v := Group_v + 4; -- identifies end of bit # in a group of 4 Str4 := StrTo4(Group_v - 3 to Group_v); -- get next 4 characters case Str4 is when "0000" => Result(I) := '0'; when "0001" => Result(I) := '1'; when "0010" => Result(I) := '2'; when "0011" => Result(I) := '3'; when "0100" => Result(I) := '4'; when "0101" => Result(I) := '5'; when "0110" => Result(I) := '6'; when "0111" => Result(I) := '7'; when "1000" => Result(I) := '8'; when "1001" => Result(I) := '9'; when "1010" => Result(I) := 'A'; when "1011" => Result(I) := 'B'; when "1100" => Result(I) := 'C'; when "1101" => Result(I) := 'D'; when "1110" => Result(I) := 'E'; when "1111" => Result(I) := 'F'; when others => Result(I) := 'X'; end case; -- Str4 end loop Cnvrt_Lbl; return Result; end Str2Hex; Function Hex2Slv(str: string) return Std_Logic_vector is Constant Len : Natural := str'length; Variable Result: Std_Logic_vector(Len * 4 - 1 downto 0); begin for i in Len downto 1 loop Result(3 +(Len - i)*4 downto (Len-i)*4) := char2quad_bits(str(i)); end loop; return Result; end; Function Slv2Hex(value :Std_Logic_Vector) return String is begin return Str2Hex(Slv2Str(value)); end Slv2Hex; Function Time2Str(value: Time) Return String is Variable L: Line; Variable W: String(1 to MAXCHARTIME):= (Others => ' '); Begin Std.TextIO.WRITE(L, value); W(L.all'range) := L.all; DeAllocate(L); Return W; End Time2Str; Function Str2Time(str: String) Return Time is Variable T: time; Begin if StrLen(str)>0 then T:=time'value(str); -- Only VHDL version 93 Return T; end if; End Str2Time; Function Str2Int(str: String) Return Integer is Variable i:Integer; Begin if StrLen(str)>0 then i:=Integer'value(str); Return i; end if; Return 0; End Str2Int; Function Int2Str(value: Integer) Return String is Variable L: Line; Variable LowBound, HighBound : natural; Variable W: String(1 to MAXCHARINTEGER):= (Others => ' '); Begin Std.TextIO.WRITE(L, value); LowBound := L.all'low; HighBound := L.all'high; W(LowBound to HighBound) := L.all; DeAllocate(L); return W(LowBound to HighBound); End Int2Str; Procedure Line2String ( Variable L: in Line; Variable S: out String ) is Begin if (L.all'length > 0) then S(L.all'range) := L.all; end if; End Line2String; Function int2bit(value: integer) return bit is begin if value=0 then return '0'; else return '1'; end if; end int2bit; Function bit2int(b: bit) return integer is begin if b='0' then return 0; else return 1; end if; end bit2Int; Function int2vbit(value: Integer; size :Integer) Return bit_vector is begin Return to_bitvector(conv_std_logic_vector(value,size)); end int2vbit; Function vbit2int(b: bit_vector) Return Integer is begin Return slv2int(to_stdlogicvector(b)); end vbit2int; End StringLib;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -