You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vivado中VHDL读取TXT文件报错:文件未打开问题求助

Vivado下VHDL读取TXT文件报错的解决方案

问题描述

在Vivado环境下使用VHDL读取TXT文件时出现异常:测试代码在ModelSim中可正常运行,但在Vivado中报错ERROR: File in_textFile.txt is not open. Cannot call endfile on it,错误指向测试台的循环起始位置。尝试过调整文件打开位置、改用注释中的文件打开方式、将TXT文件加入Vivado设计源等操作,均未解决问题。

相关代码

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

use ieee.std_logic_textio.all;
library std;
use std.textio.all;

entity test is
end entity test;

architecture arch of test is
    
    constant input_width : natural := 36;
    signal  expected_in_data : std_logic_vector(input_width-1 downto 0) := x"123456789";
    

    constant v_file : string := "in_textFile.txt";
    --file open_v_file : text; 
begin

    process
        file  open_v_file   : text open read_mode is v_file;
        variable in_line    : line;
        variable good       : boolean;
        variable input_data : std_logic_vector(input_width-1 downto 0);
    begin
        --file_open(open_v_file, "in_textFile.txt", read_mode);
        while not endfile(open_v_file) loop
            readline(open_v_file, in_line);
            if in_line.all'length = 0 or in_line.all(1)='#' then
                next;
            end if;

            hread(in_line, input_data, good);
            assert good report "I/O error reading input_data" severity failure;

            assert expected_in_data=input_data report "Unexpected output. Error!" severity error;
        end loop;
        file_close(open_v_file);

        wait;
    end process;

end architecture arch;

TXT文件内容

123456789

原因分析

  • 路径解析差异:ModelSim默认在项目根目录或当前工作目录查找文件,而Vivado仿真的默认工作目录是sim_1/behav/xsim,若TXT文件不在该路径下,会导致无法找到文件。
  • 文件初始化时序问题:进程内直接声明并打开文件的方式,在Vivado仿真引擎中可能存在初始化延迟,导致文件未成功打开就调用endfile函数。

解决方案

1. 确保文件路径正确

  • 复制文件到仿真工作目录:将in_textFile.txt复制到Vivado仿真默认工作目录(路径示例:你的项目目录/sim_1/behav/xsim)。
  • 使用绝对路径:修改代码中文件路径为绝对路径,例如:
    constant v_file : string := "/home/user/your_project_dir/in_textFile.txt";
    
  • 修改仿真工作目录:在Vivado中点击Simulation Settings,将Working Directory设置为项目根目录,确保TXT文件与该目录同级。

2. 优化文件打开逻辑

改用file_open显式打开文件,并增加打开成功的断言判断,避免未打开文件就执行后续操作:

process
    file  open_v_file   : text;
    variable in_line    : line;
    variable good       : boolean;
    variable input_data : std_logic_vector(input_width-1 downto 0);
begin
    -- 显式打开文件并校验是否成功
    file_open(good, open_v_file, v_file, read_mode);
    assert good report "Failed to open file: " & v_file severity failure;

    while not endfile(open_v_file) loop
        readline(open_v_file, in_line);
        if in_line.all'length = 0 or in_line.all(1)='#' then
            next;
        end if;

        hread(in_line, input_data, good);
        assert good report "I/O error reading input_data" severity failure;

        assert expected_in_data=input_data report "Unexpected output. Error!" severity error;
    end loop;
    file_close(open_v_file);

    wait;
end process;

3. 配置Vivado仿真源属性

  • 在Vivado的Simulation Sources右键选择Add Sources,将in_textFile.txt添加到仿真源。
  • 选中该文件,在Properties面板设置File Type为Other,并勾选Copy to project选项,确保文件被复制到项目目录下。

内容的提问来源于stack exchange,提问作者Joel254

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:15:20