2016-12-14 44 views
0

我想實現一個通用(可參數化)的矩陣加法器。傳遞泛型到VHDL中的包

到目前爲止,我只有兩個3x3矩陣的矩陣加法器。這裏是matrix_add:

LIBRARY IEEE; 
USE IEEE.std_logic_1164.all; 
USE IEEE.STD_LOGIC_ARITH.ALL; 
USE IEEE.std_logic_UNSIGNED.all; 
USE ieee.numeric_std.all; 

LIBRARY work; 
USE work.matrix_pack.all; 

ENTITY matrix_add is 
    PORT (
    t_clk_i   : IN STD_LOGIC; -- System Clock (66.6667 MHz) (
    s_rst_l_i  : IN STD_LOGIC; -- Reset input 
    d_mat1_i   : IN matrix_t; -- Matrix 1 
    d_mat2_i   : IN matrix_t; -- Matrix 2 
    d_result_o  : OUT matrix_t -- Addition Result 
); 
END matrix_add; 

ARCHITECTURE rtl_matrix_add OF matrix_add IS 

BEGIN 
    p_add : PROCESS(t_clk_i, s_rst_l_i) 
    BEGIN 
    IF s_rst_l_i = '0' THEN 
     d_result_o <= (OTHERS => (OTHERS => (OTHERS => '1'))); 
    ELSIF RISING_EDGE(t_clk_i) THEN 
     FOR i IN 0 TO (d_mat1_i'LENGTH(1)-1) LOOP 
     FOR j IN 0 TO (d_mat1_i'LENGTH(2)-1) LOOP 
      d_result_o(i, j) <= d_mat1_i(i, j) + d_mat2_i(i, j); 
     END LOOP; 
     END LOOP; 
    END IF; 
    END PROCESS p_add; 

END rtl_matrix_add; 

這裏是包:

LIBRARY IEEE; 
USE IEEE.std_logic_1164.all; 
USE IEEE.STD_LOGIC_ARITH.ALL; 
USE IEEE.std_logic_UNSIGNED.all; 
USE ieee.numeric_std.all; 

PACKAGE matrix_pack IS 
    TYPE matrix_t is ARRAY (0 TO 2, 0 TO 2) OF STD_LOGIC_VECTOR(7 DOWNTO 0); 
END matrix_pack; 

我怎樣才能使矩陣的大小一般?不幸的是,我必須使用一個封裝,因爲Quartus將不接受作爲信號輸入類型的ARRAY(0 TO 2,0 TO 2)STD_LOGIC_VECTOR(7 DOWNTO 0),否則我不需要一個封裝,我的問題就會解決。

我聽說VHDL 2008中可以做到這一點,但Quartus或Modelsim會如何如此以至於會接受這一點?

謝謝

+0

爲什麼不使用'matrix_pack'中聲明的常量或包含所有常量的另一個包的一部分,並在數組聲明中使用它。 –

+0

有趣的答案,唯一的缺點是如果我需要在同一個設計中實例化兩個不同維度的矩陣加法器。 – Renato

+0

我不知道下面的符號是否會起作用... 'type typename是類型的數組(自然範圍<>)...嘗試它並讓我們知道。有一個很好的例子[這裏](http://stackoverflow.com/questions/15641751/multidimensional-memory-with-generic-data-width-vhdl)。如果你可以使用類型作爲輸入,那麼你可以使用泛型來限制數組。 –

回答

0

謝謝你的答案fpga_magik。有效。果然如代碼如下:

LIBRARY IEEE; 
USE IEEE.std_logic_1164.all; 
USE IEEE.STD_LOGIC_ARITH.ALL; 
USE IEEE.std_logic_UNSIGNED.all; 
USE ieee.numeric_std.all; 

LIBRARY work; 
USE work.matrix_pack.all; 

ENTITY matrix_add is 
    GENERIC (
    M : INTEGER := 3; 
    N : INTEGER := 2 
    ); 
    PORT (
    t_clk_i   : IN STD_LOGIC;     -- System Clock (66.6667 MHz) (
    s_rst_l_i  : IN STD_LOGIC;     -- Reset input 
    d_mat1_i   : IN matrix_t(0 TO M-1, 0 TO N-1); -- Matrix 1 
    d_mat2_i   : IN matrix_t(0 TO M-1, 0 TO N-1); -- Matrix 2 
    d_result_o  : OUT matrix_t(0 TO M-1, 0 TO N-1) -- Addition Result 
); 
END matrix_add; 

ARCHITECTURE rtl_matrix_add OF matrix_add IS 

BEGIN 
    p_add : PROCESS(t_clk_i, s_rst_l_i) 
    BEGIN 
    IF s_rst_l_i = '0' THEN 
     d_result_o <= (OTHERS => (OTHERS => (OTHERS => '1'))); 
    ELSIF RISING_EDGE(t_clk_i) THEN 
     FOR i IN 0 TO (d_mat1_i'LENGTH(1)-1) LOOP 
     FOR j IN 0 TO (d_mat1_i'LENGTH(2)-1) LOOP 
      d_result_o(i, j) <= d_mat1_i(i, j) + d_mat2_i(i, j); 
     END LOOP; 
     END LOOP; 
    END IF; 
    END PROCESS p_add; 

END rtl_matrix_add; 

和封裝如下:

LIBRARY IEEE; 
USE IEEE.std_logic_1164.all; 
USE IEEE.STD_LOGIC_ARITH.ALL; 
USE IEEE.std_logic_UNSIGNED.all; 
USE ieee.numeric_std.all; 

PACKAGE matrix_pack IS 
    TYPE matrix_t is ARRAY (natural range <>, natural range <>) OF STD_LOGIC_VECTOR(7 DOWNTO 0); 
END matrix_pack; 

非常感謝! :)

+0

輝煌,真棒工作夥計。 –