1. 程式人生 > >3.VHDL的基本結構和語法(二)

3.VHDL的基本結構和語法(二)

目錄

IF語句

1.順序語句

順序語句僅出現在程序和子程式中。順序語句綜合後 , 對映為實際的閘電路,系統一上電, 閘電路開始工作 。電路可實現邏輯上 的順序執行,實際上所有閘電路是並行工作的。

賦值語句要點:

  • 賦值語句
  • 流程控制語句
  • 空操作語句
  • 等待語句
  • 子程式呼叫語句
  • 返回語句

1.1賦值語句:

例子:

程式如下所示:

library ieee;
use ieee.std_logic_1164.all;
entity test_signal is
port
(Reset,Clock :in std_logic;
 NumA,NumB   :out integer range 0 to 255);
end test_signal;
architecture test of test_signal is
	signal A,B:integer range 0 to 255;
	begin 
		process(Reset,Clock)
		variable C: integer range 0 to 255;
	begin
		if Reset='1' THEN
			A<=0;B<=2;C:=0;
		elsif rising_edge(Clock) THEN
			C:=C+1;A<=C+1;B<=A+2;
		end if;
		end process;
		NumA<=A;NumB<=B;
end test;

進行模擬測試:

1.2 .流程控制語句

  • IF語句

方式一:

IF 條件式 THEN      順序語句END IF; 

方式二:

IF 條件式  THEN      順序語句ELSE 順序語句END IF

方式三:有優先順序

IF 條件式 THEN     順序語句ELSEIF 條件式2   THEN      順序語句ELSE      順序語句END IF; 

注意:用 IF 語句描述組合邏輯電路時 , 務必涵蓋所有的情況 , 否則綜合後將引入鎖存器 !

例子1:使用IF語句實現8-3譯碼器

程式碼如下:

library ieee;
use ieee.std_logic_1164.all;
entity Encoder8_3 is
port
(En :in std_logic;
 I  :in std_logic_vector(7 downto 0);
 A  :out std_logic_vector(2 downto 0);
 Idel:out std_logic
);
end Encoder8_3;
architecture EncoderBhv of Encoder8_3 is
begin
	process(En,I)
begin
	if En='1' THEN
	   if    I="10000000" THEN
		A<="111";Idel<='0';
	   elsif I="01000000" THEN
	    A<="110";Idel<='0';
	   elsif I="00100000" THEN
	    A<="101";Idel<='0';
	   elsif I="00010000" THEN
	    A<="100";Idel<='0';
	   elsif I="00001000" THEN
	    A<="011";Idel<='0';
	   elsif I="00000100" THEN
	    A<="010";Idel<='0';
	   elsif I="00000010" THEN
	    A<="001";Idel<='0';
	   elsif I="00000001" THEN
	    A<="000";Idel<='0';
	   else
	    A<="000";Idel<='1';
	   end if;
	 end if;
	 end process;
end EncoderBhv;	    	    

解釋:En為使能位,只有當使能位為1時,才會根據輸入改變輸出的值,I是輸入的為,寬度為8為,Idel檢測狀態是否正確,如果輸入不正確,Idel位置1.

模擬波形如下:

例子2:不完整條件語句時序電路

程式如下:

library ieee;
use ieee.std_logic_1164.all;
entity comp_bad is
port
(a1 :in bit;
 b1 :in bit;
 q1 :out bit
);
end comp_bad;
architecture oneBhv of comp_bad is
begin
	process(a1,b1)
begin
	if    a1>b1 THEN q1<='1';
	elsif a1<b1 THEN q1<='0';   --這裡少了一種情況,a1 = b1
	end if;
	end process;
end oneBhv;

那麼綜合的結果就會有鎖存器:

例子三:完整條件語句

程式如下:

library ieee;
use ieee.std_logic_1164.all;
entity comp_bad is
port
(a1 :in bit;
 b1 :in bit;
 q1 :out bit
);
end comp_bad;
architecture oneBhv of comp_bad is
begin
	process(a1,b1)
begin
	if    a1>b1 THEN q1<='1';
	else  
          q1<='0';   --涵蓋所有的情況
	end if;
	end process;
end oneBhv;

綜合結果是:

  • case語句

語法

CASE  表示式 IS            WHEN   選擇值 [ 選擇值 ]=>順序語句;            WHEN   選擇值 [ 選擇值 ]=>順序語句;            WHEN OTHERS=>順序語句;END CASE;

注意:

1.選擇值不可重複或重疊; 

2.當CASE 語句的選擇值無法覆蓋所有的情況時情況時,要用OTHERS指定未能列出的其他所有情況的輸出值。

例子:使用CASE實現4選1四路選擇器

程式碼如下:

library ieee;
use ieee.std_logic_1164.all;
entity mux is
port
(Data0,Data1,Data2,Data3 :in std_logic_vector(7 downto 0);
 Sel :in std_logic_vector(1 downto 0);
 Dout :out std_logic_vector(7 downto 0)
);
end mux;
architecture DataFlow of mux is
begin
	process(Sel)
begin
	case Sel is
		WHEN"00"=>Dout<=Data0;
		WHEN"01"=>Dout<=Data1;
		WHEN"10"=>Dout<=Data2;
		WHEN"11"=>Dout<=Data3;
		WHEN OTHERS=>Dout<="00000000";
	end case;
	end process;	
end DataFlow;

模擬結果如下:

  • LOOP語句

語法:

[LOOP 標號:] FOR 迴圈變數  IN 迴圈次數範圍 LOOP                                 順序語句END LOOP [LOOP 標號];

注意:

1.迴圈變數是一個臨時變數。

2.….TO….….DOWNTO…..從初值開始 , 每執行完一次後遞增(遞減),  直到終值為止 。

例子1:

Sum:=0;     FOR i IN 0 TO 9 LOOP          Sum:=Sum+i;     END LOOP;

例子2:

VARIABLE Length :Integer RANGE 0 TO 15;

               .

               . FOR i IN 0 TO Length LOOP

               .

               . END LOOP;