library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_arith.all; Use STD.TEXTIO.ALL; -- for reading and writing a file -- IMPORTANT: the input file for decimal reading: there must be a space between -- to number and at the end of line -- for binary input: there must be space between each number and each number must be exactly -- 16 bit -- my own file , contains some function to read and write data -- and convert sdt_logic to integer. check read_write_package use work.read_write_library.all; -- you have to define input output file where you define entity. -- you cannot send file name as paramater, simulation gives -- error message. In this example I defined an input and an output file. -- It reads data from a file put them in array when start=1. -- when start=0, it counts the clock cycle and at each clock cycle -- it writes counted clock cycle and an array element to clk_data.txt -- output file. It writes them as integer. when all read data is written -- into output files, it stops. entity read_write is generic ( input_file: string := "input.txt"; decimal_file: string:="decimal_input.txt"; decimal_output: string:="decimal_out.txt"; output_bin_to_integer: string:="bin_to_integer.txt" ); end entity; architecture behav of read_write is -- at here file are opened to be read file input: TEXT open read_mode is input_file; file output1: TEXT open write_mode is output_bin_to_integer; file DEC_input: TEXT open read_mode is decimal_file; file DEC_output: TEXT open write_mode is decimal_output; signal clk, start:std_logic; component clock is generic (half_period:Time:=20 ns); port(clk_out:out std_logic); end component; begin C:clock port map(clk); -- SD 04/23/09: The clk part is not needed here start_proc: process is begin start <= '1'; wait for 20 ns; start <= '0'; wait; end process; -- the process below reads in integers in binary form and writes out -- data produced in decimal form read_binary_and_write_decimal: process is variable check:bit_vector(15 downto 0):=X"0000"; -- SD 04/23/09 variable mult1,mult2,result:integer; variable add_num1, add_num2, result:integer; variable ch : character; variable buf: line;-- this part need to be defined to able to write a file begin if start = '1' then while not endfile(input) loop readline(input,buf); read_1(buf,check); -- reads binary data from the buffer "buf" that -- contains a "line" of data add_num1:=convert_to_integer(check); read(buf,ch); -- skip space read_1(buf,check); -- reads binary data add_num2:=convert_to_integer(check); result:=add_num1+add_num2; write(buf,integer'image(add_num1)); -- take add_num1 and write each digit as character write(buf,string'(" + ")); write(buf,integer'image(add_num2)); write(buf,string'(" = ")); write(buf,integer'image(result)); -- take result and write each digit as character report("write file"); writeline(output1,buf); end loop; report(" read is done"); file_close(input); file_close(output); wait; end if; end process; -- this process reads in integers in decimal form and writes out -- data produced also in decimal form read_and_write_decimal :process is variable inp_buf,out_buf: line; variable c: character; variable add_num1,add_num2,result:integer; -- this procedure reads the decimal number until read character is space ot any other character. -- L is the line which is read from file -- n is the integer value begin if start = '1' then while not endfile(DEC_input) loop add_num1:=0; add_num2:=0; readline(DEC_input,inp_buf); -- at here we are reading 2 number. There is only read_integer(inp_buf,add_num1); -- one space between 2 number otherwise it will read_integer(inp_buf,add_num2); -- not read second one result:=add_num1+add_num2; write(out_buf,integer'image(add_num1)); write(out_buf,string'(" + ")); write(out_buf,integer'image(add_num2)); write(out_buf,string'(" = ")); write(out_buf,integer'image(result)); -- take result and write each digit as character writeline(DEC_output,out_buf); end loop; report("secon file is done"); file_close(DEC_output); -- standart textio library functions file_close(DEC_input); wait; end if; end process; end architecture;