gpt4 book ai didi

arrays - VHDL 数组 - 我如何声明一个未知大小的数组并使用它

转载 作者:行者123 更新时间:2023-12-01 15:25:06 27 4
gpt4 key购买 nike

我的要求是从输入文件中读取数据位并将数据写入 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;

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

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

最佳答案

查看您的其他问题 - 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 中。您可以查看上面引用的权衡问题以及是否需要。

输出端口的分配已移出流程。假定您将在该过程中对 mem 进行某种写入。

还有一个测试台实例化 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;

该功能是在细化时执行文件读取以设置泛型。

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

file_io_tb.png

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

10101100 11010100 10101100 11010100   

11111110 10111001 11111110 10111001

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

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

关于arrays - VHDL 数组 - 我如何声明一个未知大小的数组并使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42583061/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com