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

VIVADO仿真中VHDL读取文本文件bit值显示为'?'的问题排查

问题描述

在Vivado仿真环境中读取包含时间与bit值的文本文件,文件内容如下:

104 us 1
108 us 0
116 us 1

其中时间部分(如104 us)可正常读取,但第三列的bit值被识别为'?',而非预期的0或1。对应的VHDL测试代码如下:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use std.textio.all;

entity tb_input_from_txt is
end tb_input_from_txt;

architecture rtl_tb_input_from_txt of tb_input_from_txt is

  constant file_name_c : string := "input_data.txt";
  signal clk_100M_s : std_logic := '0';
  signal reset_s : std_logic := '0'; 
  signal dig_s : bit;
  
  file fptr: text;
  --signal line_sig : line;

begin

  clk_100M_s <= not clk_100M_s after 5 ns;
  reset_s <= '0', '1' after 40 us;

  GetData_proc: process
     variable fstatus       : file_open_status;
     
     variable file_line     : line;
     variable timewhen_v    : time;
     variable value_set_v   : bit;
     variable ok            : boolean;
        
  begin
    wait until reset_s = '1';
    file_open(fstatus, fptr, file_name_c, read_mode);
    while (not endfile(fptr)) loop
      readline(fptr, file_line);
      read(file_line, timewhen_v, ok);
      assert ok
        report "Read 'wait_time' failed for line: " & file_line.all
        severity failure;
        
      read(file_line, value_set_v, ok);
      assert ok
        report "Read 'value_set_v' failed for line: " & file_line.all
        severity failure;
        
      wait for timewhen_v -  NOW;
        dig_s <= value_set_v;
    end loop;
    wait for 5 ns;
  end process GetData_proc;

end architecture rtl_tb_input_from_txt;

仿真后dig_s信号在104us触发变化,但值为'?',需排查问题原因并解决。

问题原因

VHDL的read函数读取bit类型时,要求输入必须是带单引号的字符(如'1'或'0'),但文本文件中第三列是无引号的1/0,导致读取操作失败,value_set_v未被正确赋值,保持初始的'?'状态。

解决方法

有两种可行方案:

  • 方案1:修改文本文件格式
    将bit值改为带单引号的形式,修改后的文件内容如下:

    104 us '1'
    108 us '0'
    116 us '1'
    

    无需修改代码,原read函数可正常读取带引号的bit值。

  • 方案2:修改代码适配现有文件
    先读取整数类型的数值,再转换为bit类型,修改后的GetData_proc进程代码如下:

    GetData_proc: process
       variable fstatus       : file_open_status;
       
       variable file_line     : line;
       variable timewhen_v    : time;
       variable value_int_v   : integer; -- 新增整数变量用于中转
       variable value_set_v   : bit;
       variable ok            : boolean;
          
    begin
      wait until reset_s = '1';
      file_open(fstatus, fptr, file_name_c, read_mode);
      while (not endfile(fptr)) loop
        readline(fptr, file_line);
        read(file_line, timewhen_v, ok);
        assert ok
          report "Read 'wait_time' failed for line: " & file_line.all
          severity failure;
          
        read(file_line, value_int_v, ok); -- 先读取无引号的整数
        assert ok
          report "Read 'value_int_v' failed for line: " & file_line.all
          severity failure;
        
        -- 将整数转换为bit类型
        if value_int_v = 1 then
          value_set_v := '1';
        elsif value_int_v = 0 then
          value_set_v := '0';
        else
          assert false report "Invalid bit value: " & integer'image(value_int_v) severity failure;
        end if;
          
        wait for timewhen_v -  NOW;
          dig_s <= value_set_v;
      end loop;
      wait for 5 ns;
    end process GetData_proc;
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 09:57:44