1. PROCEDURE(過程語句)和FUNCTION(函數語句)的區別
PROCEDURE | FUNCTION | |
返回值 | 多個返回值、不提供返回值 | 單個返回值 |
參數 | 輸入、輸出、雙向參數 | 輸入參數(信號,常量) |
結構 | 過程首、過程體 | 函數首、函數體 |
位置 | 程序包、結構體、進程 | 程序包、結構體、進程 |
相似 | 順序語句 | 順序語句 |
2. PROCEDURE(過程語句)
1 library ieee; 2 use ieee.std_logic_1164.all; 3 use ieee.std_logic_arith.all; 4 use ieee.std_logic_unsigned.all; 5 6 PACKAGE TEST1 IS 7 PROCEDURE OPERATE1 (a : in integer;b : out integer; c : INOUT integer); 8 END PACKAGE TEST1; 9 10 PACKAGE BODY TEST1 IS 11 PROCEDURE OPERATE1 (a : in integer;b : out integer;c : INOUT integer)IS 12 BEGIN 13 b := 0; 14 c := 0; 15 if a>10 then 16 b := a+a+a; 17 c := 0; 18 else 19 c := a+a; 20 b := 0; 21 end if; 22 end OPERATE1; 23 END PACKAGE BODY TEST1;
1 library ieee; 2 use ieee.std_logic_1164.all; 3 use work.TEST1.ALL; 4 use ieee.std_logic_arith.all; 5 use ieee.std_logic_unsigned.all; 6 7 8 entity testa is 9 port( 10 sysclk : in std_logic; 11 RST_N : in std_logic; 12 r1 : out integer; 13 r2 : out integer 14 15 ); 16 end entity; 17 18 architecture behav of testa is 19 20 21 22 signal a1 : integer; 23 begin 24 25 process(sysclk,RST_N) 26 begin 27 if RST_N = '0' then 28 a1 <= 0; 29 elsif rising_edge(sysclk) then 30 if a1 = 20 then 31 a1 <= 0; 32 else 33 a1 <= a1 + 1; 34 end if; 35 end if; 36 end process; 37 38 process(a1) 39 40 variable r3 : integer:=0; 41 variable r4 : integer:=0; 42 begin 43 OPERATE1(a1,r3,r4); 44 r1 <= r3; 45 r2 <= r4; 46 end process; 47 48 end behav;
3. FUNCTION(函數語句)
1 library ieee; 2 use ieee.std_logic_1164.all; 3 --use work.TEST1.ALL; 4 use ieee.std_logic_arith.all; 5 use ieee.std_logic_unsigned.all; 6 7 8 entity testa is 9 port( 10 sysclk : in std_logic; 11 RST_N : in std_logic; 12 r1 : out integer; 13 r2 : out integer 14 15 ); 16 end entity; 17 18 architecture behav of testa is 19 signal a1 : integer RANGE 0 TO 15; 20 begin 21 22 process(sysclk,RST_N) 23 begin 24 if RST_N = '0' then 25 a1 <= 0; 26 elsif rising_edge(sysclk) then 27 if a1 = 13 then 28 a1 <= 0; 29 else 30 a1 <= a1 + 1; 31 end if; 32 end if; 33 end process; 34 35 process(a1) 36 function OPERATE1(a: integer RANGE 0 TO 15) RETURN INTEGER IS 37 begin 38 if (a>10) then 39 return 5; 40 else 41 return a; 42 end IF; 43 end OPERATE1; 44 begin 45 r1 <= OPERATE1(a1); 46 end process; 47 end behav;
備注:以上是兩個較為簡單理解的小例子,不能完全映射語句語法的各種情況;例如函數與過程語句的不同之處,如上文所說,就是輸入參數的信號類型,輸出參數的個數,定義和調用位置等;這里重點備注說明的是函數定義,函數可以程序包、結構體和進程中被定義,當在結構體和進程中可以不必定義函數首,當在程序包中定義時,要定義函數首;值得注意的是,函數的輸入參數數據類型必須定義范圍。
4. 參考博文
【FPGA學習筆記】VHDL語言學習筆記(四)並行語句:並行賦值、process、子程序(procedure、function)_yang_jiangning的博客-CSDN博客