用VHDL

时间:2017-10-31 01:32:32

标签: 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代码块?

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

我尝试使用谷歌搜索但未发现任何相关内容,也许我没有问正确的问题。无论如何,我在这里。感谢您的帮助,我希望这个问题有道理。

1 个答案:

答案 0 :(得分:4)

您可以使用不正确的函数提供数组类型为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;

对于一个文件,其名称由字符串RomFileName找到不纯函数InitRomFromFile:

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

注意系数是从数组按顺序从文件加载的,如果提供的数量不足,则可以填写默认值,这要归功于评估ENDFILE(romfile)是否返回true。

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