是否可以检查输入文本文件的长度? [英] Is it possible to check the length of an input text file?

查看:31
本文介绍了是否可以检查输入文本文件的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 VHDL 项目中,我的输入将从包含 n 位 1 和 0 的文本文件中提取.我试图使它尽可能通用.我熟悉如何使用 test-bench 在文本文件上读写,但我不知道如何检查它的长度.

In my VHDL project, my input is going to be extracted from a text file containing n bits of 1's and 0's. I am trying to make it as general as possible. I am familiar with how to read and write on a text file using test-bench, but I don't know how to check its length.

我的代码通常采用 64 位作为输入,将它们传递给所有块并生成输出.如果剩余位长度小于 64,则它通过特定块.

My code normally takes 64 bit as input, pass them through all the blocks and generate an output. If the remaining bits length is less than 64 then it passes through a specific block.

假设文本文件包含 1000 位.15 x 64 = 960.960 位将通过所有块,其余 40 位将通过特定块.这看起来很简单,但为了让我执行此类操作,我需要知道文本文件的长度.如果有人可以提供帮助,那将是非常有益的.

Let's say the text file contains 1000 bits. 15 x 64 = 960. 960 bits will pass through all blocks, the remaining 40 will pass by a specific block. This looks straight forward but in order for me to do such operations i need to know the length of the text file. If anyone can help that would be very beneficial.

推荐答案

应该考虑 VHDL 数据结构的长度,而不是文件长度,因为这是特定于实现的而不是 VHDL 指定的.

The VHDL data structure length should be considered, not the file length since that is implementation specific and not VHDL specified.

如果位在一个长字符串中,该字符串将被切成带有余数的 64 位片段,则可以将整个字符串读入 VHDL line 类型,并从中读取到 std_logic_vector 类型的行然后可以依赖于行中的剩余位(字符).

If the bits are in one long string that is to be chopped up into 64-bit pieces with a remainder, then the entire string can be read into a VHDL line type, and reading from that line to a std_logic_vector type can then depend on the remaining bits (characters) in the line.

以下是这样做的代码示例:

Below is a code example doing so:

library ieee;
use std.textio.all;
use ieee.std_logic_textio.all;  -- Synopsys package; required for VHDL-2002 only

architecture syn of tb is
begin
  process is
    variable myl_v : line;
    file txt_file : text;
    variable slv_v : std_logic_vector(63 downto 0);
  begin
    file_open(txt_file, "input.txt", read_mode);
    readline(txt_file, myl_v);
    while myl_v'length > 0 loop
      if myl_v'length >= slv_v'length then  -- Full slv_v
        report "Full...: " & myl_v.all(1 to slv_v'length);
        read(myl_v, slv_v);
      else  -- Reduced slv_v
        report "Reduced: " & myl_v.all(1 to myl_v'length);
        read(myl_v, slv_v(myl_v'length - 1 downto 0));  -- Place reduced at LSBs
      end if;
    end loop;
    file_close(txt_file);
    wait;
  end process;
end architecture;

顺便说一句,要回答输入文本文件的长度"的问题,那么可以通过从文件中读取尽可能多的字符来确定字符长度,例如使用如下代码:

Btw, to answer the question of "length of an input text file", then the length in characters can be determined by reading as many characters from the file as possible, for example with code like:

impure function file_length_in_characters(filename : string) return natural is
  type char_file_t is file of character;
  file char_file : char_file_t;
  variable char_v : character;
  variable res_v : natural;
begin
  res_v := 0;
  file_open(char_file, filename, read_mode);
  while not endfile(char_file) loop
    read(char_file, char_v);
    res_v := res_v + 1;
  end loop;
  file_close(char_file);
  return res_v;
end function;

这篇关于是否可以检查输入文本文件的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆