【问题标题】:VHDL arrays - How do i declare an array of unknown size and use itVHDL数组 - 我如何声明一个未知大小的数组并使用它
【发布时间】:2017-03-03 15:45:36
【问题描述】:

我的要求是从输入文件中读取数据位并将数据写入 vhdl 中的内存数组(输出)。 由于我不知道输入文件中的数据量,我需要声明未知大小的数组并在后期将数据分配给内存。

我的代码如下:

声明:

PACKAGE io IS
   type memory is array (natural range<>) of std_logic_vector(3 downto 0); 
END;

entity File_io is
 port (
     clk:             in  std_logic;
     Data_memory:     out   memory
 );
end entity;

architecture behav of File_io is
 signal mem: memory;
begin
 process(clk)
   begin
     for i in 0 to 15 loop  -- Took just 16 values for explaination purpose.
                             -- i may need more values to be written which is unknown.
      mem(i) <= std_logic_vector(to_unsigned(i, 4));
     end loop;
    Data_memory <= mem;
  end process;
end architecture behav;

我已将实际代码最小化为我的确切要求。如果有语法错误,请忽略。

问题是我无法为不受约束的数组赋值,即大小未定义。 我该怎么做呢 ?有什么建议或替代方案吗?指导我

【问题讨论】:

  • 只是想知道:您尝试过一些搜索引擎吗?我不是 VHDL 专家;但似乎有关于该主题的各种讨论......
  • 我已经在各种链接中进行了讨论,但我得到的只是,我应该用一些值声明大小(可能是最大可能值)。
  • 我想知道的是:VHDL 到底是不是关于构建“硬件”?那么“未知大小”的数组在真实硬件中的作用是什么?
  • @ghostCat,我需要它来进行模拟。
  • @GhostCat 在实际项目中,不仅设计使用 VHDL(或其他一些 HDL)编码,测试环境也使用 VHDL(或其他一些 HDL)编码。测试环境是非常软件:它只在计算机上执行。用 System-Verilog 编写的测试环境可能非常复杂,涉及 OO 设计和所有相关(继承、多态、设计模式等)。

标签: arrays vhdl


【解决方案1】:

查看您的其他问题 - How to Eliminate whitespaces while Reading a file in VHDL 提供了一个示例内存初始化文件以及读取它的问题。

合并该修复并使用方法方法 J.H. Bonarius 建议产生一个Minimal, Complete and Verifiable example

library ieee;
use ieee.std_logic_1164.all;

PACKAGE io IS
   type memory is array (natural range<>) of std_logic_vector(3 downto 0); 
   function iswhitespace (inpstr: in string) return boolean;
END;

package body io is
    function iswhitespace (inpstr:  in  string) return boolean is
        constant NBSP: character  := character'val(128); 
    begin
        for i in inpstr'range loop
            if inpstr(i) /= ' ' and inpstr(i) /= NBSP and inpstr(i) /= HT then
                exit;
            elsif i = inpstr'RIGHT then
                return TRUE;
            end if;
        end loop;
        return FALSE;
    end function;
end package body;

library ieee;
use ieee.std_logic_1164.all;
use work.io.all;

entity File_io is
    generic (
        constant MEMORY_SIZE: natural := 42;
        constant filename:  string := "C:\Users\ChowdaryS\Downloads\topo.bin"
    );
     port (
         clk:             in  std_logic;
         Data_memory:     out   memory (0 to MEMORY_SIZE - 1) 
     );
end entity;

architecture foo of File_io is
    signal mem: memory (0 to MEMORY_SIZE - 1); -- ADDED subtype indication
    use ieee.numeric_std.all;                  -- MISSING cntext item
    use std.textio.all;
    signal mem_inited:     boolean := FALSE;   -- ADDED
begin
    process(clk)
        file f:       text open read_mode is filename;
        variable L:   line;
        variable i:   integer:= 0;
        variable b:   bit_vector(3 downto 0);  
    begin
        if not mem_inited then
            i := 0;
            while not endfile(f) loop
                readline(f, L);
                while L.all'length >= b'length and not iswhitespace(L.all) loop
                    read(L, b);
                    mem(i) <= to_stdlogicvector(b);
                    i := i + 1;
                end loop;
            end loop;
            report "mem values loaded = " &  integer'image(i);
            mem_inited <= TRUE;
        end if;
    end process;
    Data_memory <= mem;
end architecture foo;

有一些变化。函数 iswhitespace 已添加到包 io。您可以查看上面引用的问题进行权衡以及是否需要。

对输出端口的分配已移到进程之外。假设您将在该过程中包含对内存的某种写入。

还有一个测试台实例化 file_io 并确定内存数组的大小,并将其作为泛型传递。

library ieee;
use ieee.std_logic_1164.all;
use work.io.all;

entity file_io_tb is
end entity;

architecture foo of file_io_tb is

    constant MEMSIZ:    natural := 16;
    constant filename:  string := "topo.bin";  -- found locally.
    signal clk:         std_logic := '0';
    signal Data_memory: memory (0 to MEMSIZ - 1);
    use std.textio.all;

    impure function getarraysize return natural is
        variable L:     Line;
        variable i:     natural;
        variable b:     bit_vector (3 downto 0);
        file f:         text  open read_mode is filename;
    begin
        i := 0;
        while not endfile(f) loop
            readline(f, L);
            while L.all'length >= b'length and not iswhitespace(L.all) loop
                read(L, b);
                i := i + 1;
            end loop;
        end loop;
        report " memory size = " & integer'image(i);
        return i;
    end function;
begin
DUT:
    entity work.file_io
        generic map (MEMORY_SIZE => getarraysize, filename => filename)
        port map (
            clk => clk,
            Data_memory => Data_memory
        );

CLOCK:
    process
    begin
        wait for 10 ns;
        clk <= not clk;
        if now > 50 ns then
            wait;
        end if;
    end process;

end architecture;

该功能是在制定时执行文件读取以设置通用。

我们看到这初始化了内存:

使用的 topo.bin 副本在第一行有四个尾随空格:

10101100 11010100 10101100 11010100   

11111110 10111001 11111110 10111001

波形中显示的值与 topo.bin 中的上述两行相匹配。

(所有这些都是为了找到另一个问题中的空格问题)。

【讨论】:

    【解决方案2】:

    两次读取文件并确定第一次的行数。例如

    编辑: 终于可以使用 vhdl 模拟器了。修复了代码。 最近我一直在 VHDL、C++、C#、matlab 和 python 之间切换,所以我一直在混合语法。

    library IEEE;
    use IEEE.std_logic_1164.all;
    use std.textio.all;
    
    entity file_io is
        generic (
            data_width : positive := 4;
            file_name  : string
        );
        port (
            clk         :  in std_logic;
            file_output : out std_logic_vector(data_width-1 downto 0) := (others => '0')
        );
    end entity file_io;
    
    architecture behaviour of file_io is
        impure function get_line_count return positive is
            file file_pointer : text;
            variable line_data : line;
            variable lineCount : natural := 0;
        begin
            file_open(file_pointer, file_name, read_mode);
            while not endfile(file_pointer) loop
                readline(file_pointer, line_data);
                lineCount := lineCount + 1;
            end loop;
            file_close(file_pointer);
            return lineCount;
        end function get_line_count;
    
        constant memory_size : positive := get_line_count;
    
        subtype data_type is std_logic_vector(data_width-1 downto 0);
        type memory_type is array (0 to memory_size-1) of data_type;
    
        impure function get_data return memory_type is
            file file_pointer : text;
            variable line_data : line;
            variable line_value : bit_vector(data_width-1 downto 0);
            variable memory : memory_type;
            variable index : natural := 0;
        begin
            file_open(file_pointer, file_name, read_mode);
            while not endfile(file_pointer) loop
                readline(file_pointer, line_data);
                read(line_data, line_value);
                memory(index) := to_stdlogicvector(line_value);
                index := index + 1;
            end loop;
            file_close(file_pointer);
            return memory;
        end function get_data;
    
        constant memory : memory_type := get_data;
        signal index : natural := 0;
    begin
        process(clk)
        begin
            if rising_edge(clk) then
                file_output <= memory(index);
                if (index < memory_size-1) then
                    index <= index + 1;
                else
                    index <= 0;
                end if;
            end if;
        end process;
    end architecture behaviour;
    

    和测试台:

    library IEEE;
    use IEEE.std_logic_1164.all;
    
    entity file_io_tb is
    end entity file_io_tb;
    
    architecture behaviour of file_io_tb is
        signal clk : std_logic := '0';
        constant data_width : positive := 4;
        signal data : std_logic_vector(data_width-1 downto 0);
    begin
        file_io_inst : entity work.file_io
            generic map(
                data_width => 4,
                file_name => "data_file.txt"
                )
            port map(
                clk => clk,
                file_output => data
                );
    
        clk_prc : process
        begin
            loop
                clk <= '0', '1' after 1 ns;
                wait for 2 ns;
            end loop;
        end process;
    end architecture behaviour;
    

    data_file.txt 只包含

    0101
    1010
    1100
    0011
    ...
    

    这对我来说很好......

    【讨论】:

    • mast中的参数接口列表至少有一个interface_element。 IEEE 标准 1076-2009 6.5.6.1,interface_list ::= interface_element { interface_element }。形式参数列表在子程序声明中是可选的。 4.2.1 function_specification ::= [ | 不纯 ] 函数指示符 subprogram_header [ [ parameter ] (formal_parameter_list ) ] return type_mark 。 1.2.1 方括号 [ ] 将可选项目括在产生式的右侧。
    猜你喜欢
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多