自定义数据类型,各模块共享数据类型(VIVADO&VHDL)

有的时候要自定义一些数据类型,然后各个模块之间互用,或者说新定义的数据类型,在模块之间的信号传递也是需要的,那么就需要单独建立一个数据包来存放这些共用的数据格式。

那么就是新建一个VHDl文件

--
--	Package File Template
--
--	Purpose: This package defines supplemental types, subtypes, 
--		 constants, and functions 
--
--   To use any of the example code shown below, uncomment the lines and modify as necessary
--

library IEEE;
use IEEE.STD_LOGIC_1164.all;

package pack-temp is

-- type <new_type> is
--  record
--    <type_name>        : std_logic_vector( 7 downto 0);
--    <type_name>        : std_logic;
-- end record;
--
-- Declare constants
--
-- constant <constant_name>		: time := <time_unit> ns;
-- constant <constant_name>		: integer := <value;
--
-- Declare functions and procedure
--
-- function <function_name>  (signal <signal_name> : in <type_declaration>) return <type_declaration>;
-- procedure <procedure_name> (<type_declaration> <constant_name>	: in <type_declaration>);
--

end pack-temp;

package body pack-temp is

---- Example 1
--  function <function_name>  (signal <signal_name> : in <type_declaration>  ) return <type_declaration> is
--    variable <variable_name>     : <type_declaration>;
--  begin
--    <variable_name> := <signal_name> xor <signal_name>;
--    return <variable_name>; 
--  end <function_name>;

---- Example 2
--  function <function_name>  (signal <signal_name> : in <type_declaration>;
--                         signal <signal_name>   : in <type_declaration>  ) return <type_declaration> is
--  begin
--    if (<signal_name> = '1') then
--      return <signal_name>;
--    else
--      return 'Z';
--    end if;
--  end <function_name>;

---- Procedure Example
--  procedure <procedure_name>  (<type_declaration> <constant_name>  : in <type_declaration>) is
--    
--  begin
--    
--  end <procedure_name>;
 
end pack-temp;

特别是要定义一些二维数组,甚至多维数组都是一样的。先在包文件里面定义好,因为在ISE当中是有一个专门的文件类型去定义包的,在VIVADO里面没有,所以就直接定义一个VHDl文件,然后把内容改成包文件格式即可。

例如:

----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date: 2023/03/04 13:08:48
-- Design Name: 
-- Module Name: type_package - Behavioral
-- Project Name: 
-- Target Devices: 
-- Tool Versions: 
-- Description: 
-- 
-- Dependencies: 
-- 
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
-- 
----------------------------------------------------------------------------------


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;


package type_package is 
    type depth38_38_width16 is array  (0 to 37,0 to 37) of std_logic_vector(15 downto 0);   
    type depth38_width16 is array  (0 to 37) of std_logic_vector(15 downto 0);    
 --   type depth38_width16 is array  (0 to 2) of std_logic_vector(15 downto 0); 
    type depth112_112_width1 is array  (0 to 111,0 to 111) of STD_LOGIC_VECTOR(0 DOWNTO 0);    
    type depth38_38_width1 is array  (0 to 37,0 to 37) of std_logic_vector(0 downto 0);    
end type_package;

package body type_package is


end type_package;

其中:type depth38_38_width16 is array  (0 to 37,0 to 37) of std_logic_vector(15 downto 0);  就是一个二维数组,里面的元素是37*37个16bit的数据。type depth38_width16 is array  (0 to 37) of std_logic_vector(15 downto 0);  就是一个一维数组,里面的元素是37个16bit的数据。

然后在其他模块当中使用时,就可以直接用了,但是要在模块当中声明那个包文件:use work.type_package.all;   --数据类型库。还有就是这些模块和包文件要放在同一个库当中,vivado里面主要是work库和xi_defaultlib。方法如下:

 然后右击选择set library,将这些文件放在同一个里面就可以了。

然后就可以用这些数据类型了,包括模块端口也可以。

 或者在模块当中重新定义一个类型:比如在包文件当中定义了一个type depth38_width16 is array  (0 to 37) of std_logic_vector(15 downto 0);  那么在模块当中就还可以定义type depth38_2_width16 is array  (0 to 37) of depth38_width16 ;  signal    uncode_matrix_reg0  :  depth38_2_width16 ;但是这里要注意的是,这个和包文件定义的type depth38_38_width16 is array  (0 to 37,0 to 37) of std_logic_vector(15 downto 0); 是不一样的。虽然他们实质的内容一样,都是37*37个16bit的数据。但是depth38_2_width16里面的数据类型是depth38_width16,也就是在用depth38_2_width16是只能用A(i),i的范围是0——37.然后depth38_38_width16里面的数据类型是std_logic_vector(15 downto 0),也就是在用depth38_38_width16是只能用A(i,j),i和j的范围是0——37. 所以在使用信号时,一定要是同一数据类型的数据进行操作。

猜你喜欢

转载自blog.csdn.net/qq_43811597/article/details/129416350