在 VHDL 中,如何在打开文件之前检查文件是否存在

  • 本文关键字:文件 存在 是否 VHDL vhdl
  • 更新时间 :
  • 英文 :


在Verilog中,我可以通过打开文件然后检查文件描述符是否为零来检查文件是否存在,以及如果不是假设该文件不存在。 例如,如下所示:

    module testbench;
    function file_exists;
        input [80*8:0] filename;        
        integer        file;
        integer        error;
    begin
        file = $fopen(filename, "r");
        if (!file) begin
           $display("nFile Open Failed with Error Code = %x", error); 
           file_exists = 0;
        end
        else begin
           $fclose(file);
           file_exists = 1;
        end
    end
    endfunction
    integer x;
    initial begin
       x = file_exists("sdfsdf.txt");
       $display("x: %0b", x);          
    end
    endmodule

如何在 vhdl 中执行相同的操作?

例如,当您打开文件时:

file_open(status, file_handle, “my_file.txt”, read_mode);

您将获得类型 file_open_status 的状态。它可以有许多值:open_okstatus_errorname_errormode_error。如果找不到该文件,您将得到name_error

use ieee.std_logic_1164.all;
use std.textio.all;          
entity testebench is 
end entity;
architecture sim of testbench is 
    impure function file_exists(
        filename : in string
    ) return boolean is
        variable open_status :FILE_OPEN_STATUS;
        file     infile      :text;
    begin
        file_open(open_status, infile, filename, read_mode);
        if open_status /= open_ok then
            return false;
        else
            file_close(infile);
            return true;
        end if;
    end function;
begin
    process
        f1 :boolean;
    begin
        f1 = file_exists("fgsfgsdfg.txt")
        report "found: " & boolean'image(f1);
    end process;
end architecture;

最新更新