VHDL测试文件的读入和写出

相关库

use std.textio.all;
use IEEE.STD_LOGIC_TEXTIO.ALL;

相关参数

file output_file        :text;				//fid
variable fstatus        :file_open_status;
variable buf            :line;				//buffer

相关函数

//文件打开函数
file_open(file_open_status, fid, file_name, file_permission);
//从文件中读入一行内容存入buffer当中
readline(fid, buffer);
//每次从buffer中读取一个数据,以空格作为结束符
read(buffer, variable);
//将buffer中的内容写入文件的一行
writeline(fid, buffer);
//将变量的内容写入到buffer当中
write(buffer, variable);

//当fid 设置为 input 或者 output 时,表示从控制台读入或输出内容,比如
write(output,"Hello");	

//当写入内容为字符串常量时,可以直接用
write(fid, string);

实例

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_TEXTIO.ALL;
use std.textio.all;
use IEEE.numeric_std.ALL;

entity sim_test2 is
end sim_test2;

architecture Behavioral of sim_test2 is
        signal  data    :std_logic_vector(11 downto 0)   := x"000";
        
begin
        process
        file output_file        :text;
        variable fstatus        :file_open_status;
        variable buf            :line;
        variable temp1,temp2,temp3      :integer;
        begin
                file_open(fstatus, output_file, "output.txt",read_mode);
                readline(output_file, buf);
                read(buf, temp1);
                read(buf, temp2);
                read(buf, temp3);
                data<=std_logic_vector(to_unsigned(temp1, 4) & to_unsigned(temp2, 4) & to_unsigned(temp3, 4));    
                file_close(output_file);
                wait;
        end process;

end Behavioral;

实现功能:

  1. 以 read_mode 形式打开文件output.txt
  2. 从文件output.txt中读取一行数据,得到“11 12 13”,存入buf当中
  3. 读出buf中的第一个数据“11”,赋值给temp1
  4. 读出buf中的第一个数据“12”,赋值给temp2
  5. 读出buf中的第一个数据“13”,赋值给temp3
  6. 将temp1、temp2、temp3转换成数据宽度为4的无符号数,在从高位到低位依次写入data信号
  7. 最终 data = 0xbcd
  8. 关闭文件,将缓冲区中的数据写入到文件中
  9. 进程进入等待状态

你可能感兴趣的:(FPGA,算法,嵌入式硬件)