在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中做同样的事情?
答案 0 :(得分:5)
打开文件时,例如:
file_open(status, file_handle, “my_file.txt”, read_mode);
您得到的状态为file_open_status
。它可以具有多个值:open_ok
,status_error
,name_error
和mode_error
。如果找不到文件,您将得到name_error
答案 1 :(得分:1)
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;