library IEEE; use IEEE.std_logic_1164.all; use STD.textio.all; package read_write_library is procedure write_1(variable f:out text;v:in bit_vector); function convert_to_integer ( bv : in bit_vector ) return integer; procedure read_1(buf :inout line ;v:out bit_vector) ; procedure read_integer(L : inout line; n : out integer); end package read_write_library; --reads binary data from a file (f: is file name) package body read_write_library is procedure read_integer(L : inout line; n : out integer) is variable result : integer := 0; variable ll:integer:=0; variable ch: character; variable negative:integer:=1;-- if input is negative, during reading this value will be negative begin while(ll=0) loop -- until not decimal number read(L, ch); if(ch='-') then -- check it is sign character negative:=-1; else if('0' <= ch and ch <= '9') then -- convert ascii to integer result := result*10 + character'pos(ch) - character'pos('0'); else ll:=1; -- not valid character may be it is space end if; end if; end loop; n := negative*result; -- if negative, convert positive number to negative. end read_integer; function convert_to_integer ( bv : in bit_vector ) return integer is -- function assumes that bv's range is defined in "n-1 downto 0" style variable temp : bit_vector(bv'range); variable result : integer := 0; begin if bv(bv'left) = '1' then -- negative number temp := not bv; else temp := bv; end if; for index in bv'range loop result := result * 2 + bit'pos( temp(index) ); -- bit'pos( temp(index) ) is the position (an integer starting from 0: -- left most position is 0) of the bit value "temp(index)" -- in enumerated type "bit". So bit value '0''s position is the integer 0 -- and bit value '1''s position is the integer 1. -- this conversion is needed from bit -> integer to perform the above -- arithmetic operations. end loop; if bv(bv'left) = '1' then result := (-result) - 1; end if; return result; end function convert_to_integer; procedure read_1(buf :inout line ;v:out bit_vector) is variable c: character; begin for i in v'range loop -- do character to bit conversion read(buf,c); case c is when '1' => v(i):='1'; when '0' => v(i):='0'; when others => v(i) :='0'; end case; end loop; end procedure read_1; --writes binary data to a file procedure write_1(variable f:out text;v:in bit_vector) is variable buf: line; variable c: character; begin for i in v'range loop case v(i) is when '1' => write(buf,character'('1')); when '0' => write(buf,character'('0')); when others => write(buf,character'('0')); end case; end loop; writeline(f,buf); end procedure write_1; end package body read_write_library ;