当前位置:   article > 正文

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

vivado vhdl package

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

那么就是新建一个VHDl文件

  1. --
  2. -- Package File Template
  3. --
  4. -- Purpose: This package defines supplemental types, subtypes,
  5. -- constants, and functions
  6. --
  7. -- To use any of the example code shown below, uncomment the lines and modify as necessary
  8. --
  9. library IEEE;
  10. use IEEE.STD_LOGIC_1164.all;
  11. package pack-temp is
  12. -- type <new_type> is
  13. -- record
  14. -- <type_name> : std_logic_vector( 7 downto 0);
  15. -- <type_name> : std_logic;
  16. -- end record;
  17. --
  18. -- Declare constants
  19. --
  20. -- constant <constant_name> : time := <time_unit> ns;
  21. -- constant <constant_name> : integer := <value;
  22. --
  23. -- Declare functions and procedure
  24. --
  25. -- function <function_name> (signal <signal_name> : in <type_declaration>) return <type_declaration>;
  26. -- procedure <procedure_name> (<type_declaration> <constant_name> : in <type_declaration>);
  27. --
  28. end pack-temp;
  29. package body pack-temp is
  30. ---- Example 1
  31. -- function <function_name> (signal <signal_name> : in <type_declaration> ) return <type_declaration> is
  32. -- variable <variable_name> : <type_declaration>;
  33. -- begin
  34. -- <variable_name> := <signal_name> xor <signal_name>;
  35. -- return <variable_name>;
  36. -- end <function_name>;
  37. ---- Example 2
  38. -- function <function_name> (signal <signal_name> : in <type_declaration>;
  39. -- signal <signal_name> : in <type_declaration> ) return <type_declaration> is
  40. -- begin
  41. -- if (<signal_name> = '1') then
  42. -- return <signal_name>;
  43. -- else
  44. -- return 'Z';
  45. -- end if;
  46. -- end <function_name>;
  47. ---- Procedure Example
  48. -- procedure <procedure_name> (<type_declaration> <constant_name> : in <type_declaration>) is
  49. --
  50. -- begin
  51. --
  52. -- end <procedure_name>;
  53. end pack-temp;

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

例如:

  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date: 2023/03/04 13:08:48
  6. -- Design Name:
  7. -- Module Name: type_package - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool Versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20. library IEEE;
  21. use IEEE.STD_LOGIC_1164.ALL;
  22. use ieee.std_logic_unsigned.all;
  23. use ieee.std_logic_arith.all;
  24. package type_package is
  25. type depth38_38_width16 is array (0 to 37,0 to 37) of std_logic_vector(15 downto 0);
  26. type depth38_width16 is array (0 to 37) of std_logic_vector(15 downto 0);
  27. -- type depth38_width16 is array (0 to 2) of std_logic_vector(15 downto 0);
  28. type depth112_112_width1 is array (0 to 111,0 to 111) of STD_LOGIC_VECTOR(0 DOWNTO 0);
  29. type depth38_38_width1 is array (0 to 37,0 to 37) of std_logic_vector(0 downto 0);
  30. end type_package;
  31. package body type_package is
  32. 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. 所以在使用信号时,一定要是同一数据类型的数据进行操作。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/879508
推荐阅读
相关标签
  

闽ICP备14008679号