利用VHDL读写file文件


library ieee;
    use std.textio.all;
    use ieee.std_logic_textio.all;
    use ieee.std_logic_1164.all;
    use ieee.std_logic_unsigned.all;
entity testin is
end entity testin;
architecture rtl of testin is
    begin
        process is
            file file_out1,filein: text; --定义text类型的文件句柄;
            variable fstatus1,fstatus2:FILE_OPEN_STATUS;   --定义文件状态指示变量;
            variable count:integer:=5;           --定义integer型写入数据;
            variable stringdata:string(5 downto 1):="whwnh";--定义string型写入数据;
            variable vectordata:bit_vector(5 downto 0):="001000";--定义bit_vector型的写入数据;
            variable value:std_logic_vector(3 downto 0):="1111";--定义std_logic_vector型的写入数据;
            variable BUF,BUF1:LINE;
            begin
                    file_open(fstatus1,file_out1,"datain.txt",write_mode);  --打开文件“datain.txt"
                    write(file_out1,string'("the first parameter is="));    --通过write()函数直接向文件中写入对应类型数据。
                    readline(input,buf);                                    --从控制台输入字符串输入文件;
                    write(buf,count);
                    writeline(file_out1,buf);                                      --向文件中输入integer类型
                    wait for 20 ns;
                    write(buf,string'("the second parameter is="));
                    write(buf,value);
                    writeline(file_out1,buf);   
                    wait for 20 ns;                                     --向文件中输入std_logic_vector类型数据;
                    write(buf,string'("the third parameter is="));
                    write(buf,vectordata);
                    writeline(file_out1,buf); 
                    wait for 20 ns;                                  --向文件中输入bit_vector类型数据;
                    write(buf,string'("the forth parameter is="));
                    write(buf,stringdata);                                  --向文件中输入string类型数据。
                    writeline(file_out1,buf);
                    write(file_out1,string'("end of file"));
                    file_close(file_out1); 
                    wait for 100 ns;                                --关闭文件
                    file_open(fstatus1,file_out1,"datain.txt",read_mode);  --以读取模式打开文件
                    readline(file_out1,buf);                               --读取文件数据并输出到控制台界面。
                    writeline(output,buf);               
                    file_close(file_out1);
                    wait for 100 ns;
                    file_open(fstatus1,filein,"STD_INPUT",read_mode);      --以控制台作为文件输入
                    file_open(fstatus2,file_out1,"STD_OUTPUT",write_mode);  --以控制台作为文件输出
                    readline(filein,BUF);
                    writeline(file_out1,BUF);
         wait;  
        end process;
    end rtl;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM