我的要求是从输入文件读取数据位并将数据写入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;
我已将实际代码最小化为我的确切需求。如果有任何语法错误,请忽略。
问题是我无法为不受约束的数组赋值,即未定义的大小。 我该怎么做呢 ?任何建议或替代方案?指导我
答案 0 :(得分: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
...
这对我来说很好......
答案 1 :(得分:2)
查看您的其他问题 - 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;
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;
有变化。已将一个函数iswhitespace添加到包io中。你可以看到上面提到的问题需要权衡,是否需要。
输出端口的分配已移至进程外。假设您将在此过程中对mem进行某种写入。
还有一个测试平台,它实例化file_io并确定内存数组的大小,并将其作为通用传递。
10101100 11010100 10101100 11010100
11111110 10111001 11111110 10111001
该函数执行在精化时读取的文件以设置通用。
我们看到这会初始化mem:
使用的topo.bin副本在第一行有四个尾随空格:
#include<iostream>
#include<string>
#include<vector>
#include<cstddef>
#include<map>
using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::vector;
using std::map;
int main() {
cout << "Please enter a string: " << endl;
string str;
getline(cin, str, '\n');
size_t str_len = str.size();
cout << endl << endl;
size_t i = 0, j = 0;
bool pop = false;
map<string, int> myMap;
for (size_t k = 0; k < str_len-1; k++) {
if (((k == 0) && isalpha(str[0])) || (!(isalpha(str[k-1])) && isalpha(str[k])))
i = k;
if ( isalpha(str[k]) && !(isalpha(str[k+1])) ) {
j = k;
pop = true;
}
if ( (k == str_len-2) && isalpha(str[k+1]) ) {
j = k+1;
pop = true;
}
if ( (i <= j) && pop ) {
string tmp = str.substr(i, j-i+1);
cout << tmp << '\t';
myMap[tmp]++;
pop = false;
}
}
cout << endl << endl;
map<string, int>::iterator itr, end = myMap.end();
for (itr = myMap.begin(); itr != end; itr++)
cout << itr->first << "\t - - - - - \t" << itr->second << endl;
cout << endl;
return 0;
}
波形中显示的值与topo.bin中的上述两行匹配。
(所有这些都是为了在另一个问题中找到空白问题而写的。)