用VHDL中的文本文件内容替换代码段

用VHDL中的文本文件内容替换代码段,vhdl,Vhdl,你好,谢谢你阅读我的问题 我不知道我想做的是可能的,还是愚蠢的,还有更简单的解决办法 我有一堆FIR滤波器,它们都有不同的系数。所以我创建了一个包文件,声明如下 package coeff_list is Type Coeff_type is array (0 to (((FIR_length - 1)/2))) of STD_LOGIC_VECTOR(17 downto 0); CONSTANT Coeff_50_100

你好,谢谢你阅读我的问题

我不知道我想做的是可能的,还是愚蠢的,还有更简单的解决办法

我有一堆FIR滤波器,它们都有不同的系数。所以我创建了一个包文件,声明如下

        package coeff_list is
            Type Coeff_type is array (0 to (((FIR_length - 1)/2))) of STD_LOGIC_VECTOR(17 downto 0);    
            CONSTANT Coeff_50_100 : Coeff_type := ("list of coefficients");
            CONSTANT Coeff_100_150 : Coeff_type := ( "other list of coefficients");
            ---- and many other lists similarly declared
        end package coeff_list; 
通过这种方式,我必须从文本文件中复制文件中的所有系数。。。而且很长很无聊,特别是如果我以后需要修改它们的话

所以我的问题是。。。是否有命令或其他东西可以获取文本文件并将其视为VHDL代码块

我知道如何在testbench中读取文件,并在模拟过程中加载参数将很容易,但我希望从一开始就预加载我的系数


我试着用谷歌搜索,但没有找到任何相关的东西,也许我问的问题不对。不管怎样,我来了。感谢您的帮助,我希望这个问题有意义。

您可以使用一个不纯函数提供数组类型Coeff_type的常量值,该函数使用TextIO从主机文件系统中的文件读取:

library ieee;
use ieee.std_logic_1164.all;

package coeff_list is
    constant FIR_length:    natural := 17;  -- ADDED demo purposes

    Type Coeff_type is array (0 to (((FIR_length - 1)/2))) of 
                                            STD_LOGIC_VECTOR(17 downto 0);  

    --ADDED:
    impure function InitRomFromFile (RomFileNAme:  in string)  
            return Coeff_type;

    -- CONSTANT Coeff_50_100 : Coeff_type := ("list of coefficients");

    constant Coeff_50_100: Coeff_type := InitRomFromFile("file_path_name1");

    -- CONSTANT Coeff_100_150 : Coeff_type := ( "other list of coefficients");

    -- constant Coeff_100_150: Coeff_type := ("file_path_name2");

    ---- and many other lists similarly declared

end package coeff_list; 

package body coeff_list is

    impure function InitRomFromFile ( RomFileName: in string)  -- ADDED
            return Coeff_type is
        use std.TextIO.all;
        FILE romfile:  text open read_mode is romfileName; 
        variable RomFileLine:   line;
        variable rom:           Coeff_type;
        variable rom_value:     bit_vector(17 downto 0);
    begin 
        for i in 0 to (FIR_Length - 1)/2 loop  
            if ENDFILE(romfile) then  -- can get ordered shorter list
                rom(i) := (others => '0');
            else
                readline(romfile, RomFileLine);
                read(RomFileLine, rom_value);
                rom(i) := to_stdlogicvector(rom_value);
            end if;
        end loop;
        return rom;
    end function;
end package body;
对于名称由不纯函数InitRomFromFile的字符串RomFileName找到的文件:

您可以通过以下方式进行演示:

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

entity foo is
end entity;

architecture fum of foo is
    -- if not VHDL-2008:
    function to_string (inp: std_logic_vector) return string is
        variable image_str: string (1 to inp'length);
        alias input_str:  std_logic_vector (1 to inp'length) is inp;
    begin
        for i in input_str'range loop
            image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
        end loop;
        return image_str;
    end function;
begin
    process
    begin
        report "Coeff_50_100 contains";
        for i in Coeff_50_100'RANGE loop
            report HT & to_string(Coeff_50_100(i));
        end loop;
        wait;
    end process;
end architecture;
在分析、阐述和模拟时产生:

注意,系数是从0开始按数组顺序从文件加载的,如果提供的值的数量不够,您可以通过评估ENDFILEromfile是否返回true来填充默认值


您还可以将中的系数作为数字文字读取,并将其转换为函数中的目标类型元素类型。

非常感谢,您的代码使用起来更方便。可以肯定的是,如果我尝试在我的FPGA上实现代码,那么系数也会被实现,或者这仅仅是为了模拟目的吗?常数的值在细化时被分配。大多数合成工具都支持TextIO来细化常量数组的值。您的合成供应商的工具文档是权威。主题是初始化内存,它可以包括RAM和ROM。。。。或等等。使用[vhdl]不纯函数textio进行搜索会发现一个候选项不是这个问题,并且答案不会显示textio。如果没有不纯函数,或者在VHDL中使用不纯函数提供相同类型的另一个对象的值,则无法从文件中为常量提供值。上述评论中提供的重复候选人和参考均未提议使用不纯正函数,在执行过程语句期间派生对象的值。由于我明确指出我要查找的内容与用于测试台的代码不兼容,因此我不认为在VHDL测试台中读取文本文件在这种情况下会有什么帮助。也许不纯函数的使用对每个人来说都是显而易见的,但对我来说却不是。因此,非常感谢用户1155120实际阅读了问题并提供了有效的解释,而不仅仅是粘贴到不同问题答案的链接。问题的答案建议是重复的,使用不同的方法从文件中获取ROM内容。这个函数使用一个不纯的初始化函数在其声明中提供ROM数组的值。“复制”在process语句中提供rom内容,不初始化rom声明。
library ieee;
use ieee.std_logic_1164.all;
use work.coeff_list.all;

entity foo is
end entity;

architecture fum of foo is
    -- if not VHDL-2008:
    function to_string (inp: std_logic_vector) return string is
        variable image_str: string (1 to inp'length);
        alias input_str:  std_logic_vector (1 to inp'length) is inp;
    begin
        for i in input_str'range loop
            image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
        end loop;
        return image_str;
    end function;
begin
    process
    begin
        report "Coeff_50_100 contains";
        for i in Coeff_50_100'RANGE loop
            report HT & to_string(Coeff_50_100(i));
        end loop;
        wait;
    end process;
end architecture;
coeff_list.vhdl:70:9:@0ms:(report note): Coeff_50_100 contains
coeff_list.vhdl:72:13:@0ms:(report note):     100000000000000000
coeff_list.vhdl:72:13:@0ms:(report note):     000000000111111111
coeff_list.vhdl:72:13:@0ms:(report note):     001111111111111000
coeff_list.vhdl:72:13:@0ms:(report note):     010101010101010101
coeff_list.vhdl:72:13:@0ms:(report note):     110011001100110011
coeff_list.vhdl:72:13:@0ms:(report note):     001110011100111001
coeff_list.vhdl:72:13:@0ms:(report note):     000000000000000000
coeff_list.vhdl:72:13:@0ms:(report note):     000000000000000000
coeff_list.vhdl:72:13:@0ms:(report note):     000000000000000000