从阵列中选择VHDL列

时间:2018-04-07 13:47:31

标签: vhdl fpga

type matrixsignal is array (LEVELS downto 0) of std_logic_vector(NBIT-1 downto 0);
signal p_matrix, g_matrix: matrixsignal;
signal col_temp_g, col_temp_p : std_logic_vector(LEVELS downto 0);

...

col_temp_p<=p_matrix(LEVELS downto 0)(j-1);
col_temp_g<=g_matrix(LEVELS downto 0)(j-1);

大家好! 我想选择并复制2个数组的整个列(j-1)......但编译器告诉我这种方式不正确。 怎么可能这样做?

P.S。 LEVELS,NBIT,j是初始化参数......我没有报告它们的初始化。

2 个答案:

答案 0 :(得分:2)

你应该将matrixsignal定义为一个二维数组,而不是一个嵌套另一个一维数组的一维数组。

type matrixsignal is array(LEVELS downto 0, NBIT - 1 downto 0) of std_logic;

PoC-Library提供类型为T_SLM(std_logic_matrix)的大量操作函数和程序包PoC.vectors。例如。 PoC定义了get_col函数,如下所示:

function get_col(slm : T_SLM; ColIndex : natural) return std_logic_vector is
  variable slv      : std_logic_vector(slm'range(1));
begin
  for i in slm'range(1) loop
    slv(i)  := slm(i, ColIndex);
  end loop;
  return slv;
end function;

用法:

subtype matrixsignal is T_SLM(LEVELS downto 0, NBIT - 1 downto 0);
signal p_matrix, g_matrix     : matrixsignal;
signal col_temp_g, col_temp_p : std_logic_vector(LEVELS downto 0);

...

col_temp_p <= get_col(p_matrix, j - 1);
col_temp_g <= get_col(g_matrix, j - 1);

可以合成包PoC.vectors。

提供了更多功能,如:

  • 切割整行
  • 切片子矩阵
  • 展平/序列化
  • 从vector / deserialization创建矩阵
  • 重载布尔运算符
  • 行/列分配
  • 矩阵合并
  • 转换为包含另一个一维数组类型的一维数组类型
  • ...

答案 1 :(得分:0)

您没有为您的问题提供Minimal, Complete and Verifiable example,特别是不提供编译器错误消息。

你的作业:

col_temp_p<=p_matrix(LEVELS downto 0)(j-1);
col_temp_g<=g_matrix(LEVELS downto 0)(j-1);

不要做你想要的。例如,p_matrix(LEVELS downto 0)是一个切片名称,恰好包含p_matrix的整个值。

p_matrix(LEVELS downto 0)(j-1)是该片名称的索引名称(IEEE Std 1076-2008 8.名称,索引名称可以具有作为片名称的前缀)并且将返回pmatrix的元素(std_logic_vector(NBIT) -1 downto 0)。级别必须等于NBIT - 1以避免分配错误,并提供一行而不是一列。

可以使用函数访问列,而不需要Patricks PoC库的开销(其中Poc.vectors包具有PoC.utils和PoC.config的使用子句,后者非常大且涉及但不关联您的问题代码片段)。

虽然独立创作,但该功能看起来非常像Paebbels所表达的那样,而不要求你将类型matrixsignal改为子类型:

function col (inp: matrixsignal; col: natural) return 
                      std_logic_vector is
        variable retval: std_logic_vector(inp'RANGE);
    begin
        for i in inp'RANGE loop
            retval(i) := inp(i)(col);
        end loop;
        return retval;
    end function;

还需要类似于Paebbels回答的函数调用,其形式遵循函数:

    col_temp_p <= col(p_matrix, j - 1);
    col_temp_g <= col(g_matrix, j - 1);

但是,它不需要您将信号矩阵转换为二维数组类型,如此MCve中所示:

library ieee;
use ieee.std_logic_1164.all;

entity hacked is
end entity;

architecture fumble of hacked is
    constant LEVELS:    natural := 4;
    constant NBIT:      natural := 4;

    constant j:         natural := 2;

    type matrixsignal is array (LEVELS downto 0) of 
                std_logic_vector(NBIT - 1 downto 0);
    -- signal p_matrix, g_matrix: matrixsignal;
    signal p_matrix: matrixsignal := ("0101", "1010", "0000", "1111", "1100");
    signal g_matrix: matrixsignal := ("1100", "1111", "0000", "1010", "0101");

    signal col_temp_g, col_temp_p: std_logic_vector(LEVELS downto 0);

    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;

    function col (inp: matrixsignal; col: natural) return 
                      std_logic_vector is
        variable retval: std_logic_vector(inp'RANGE);
    begin
        for i in inp'RANGE loop
            retval(i) := inp(i)(col);
        end loop;
        return retval;
    end function;

begin
    col_temp_p <= col(p_matrix, j - 1);
    col_temp_g <= col(g_matrix, j - 1);

    MONITORp:
    process
    begin
        wait for 0 ns; -- skip initial value wait for delta cycle
        report "col_temp_p = " & to_string(col_temp_p);
        wait;
    end process;

MONITORg:
    process
    begin
        wait for 0 ns; -- skip initial value wait for delta cycle
        report "col_temp_g = " & to_string(col_temp_g);
        wait;
    end process;

end architecture;

包含to_string函数以使示例VHDL修订版独立。

经过详细分析并运行:

ghdl -a hack.vhdl
ghdl -e hacked
ghdl -r hacked
hack.vhdl:57:9:@0ms:(report note): col_temp_g = 01010
hack.vhdl:49:9:@0ms:(report note): col_temp_p = 01010

该示例生成的结果演示了p_matrix或gmatrix的每个std_logic_vector元素(行)的每个std_ulogic列子元素对相应的col_temp_g或col_temp_p的贡献。