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

Replace code segment by text file content in 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 : 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 代码块?

我知道如何在测试台中读取文件并在模拟期间加载参数很容易,但我希望从一开始就预加载我的系数。

我试着用谷歌搜索这个东西,但没有找到任何相关的东西,也许我没有问正确的问题。无论如何,我在这里。感谢您的帮助,我希望这个问题是有道理的。

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

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 找到名称的文件:

100000000000000000
000000000111111111
001111111111111000
010101010101010101
110011001100110011
001110011100111001

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

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

注意系数是从文件中按数组顺序从 0 加载的,如果提供的值的数量不够,您可以使用默认值填充,这要感谢评估是否有 ENDFILE(romfile) returns 正确。

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