由于平時(shí)比較忙,沒有時(shí)間系統(tǒng)的學(xué)習(xí),所以一般情況下只能周末稍微系統(tǒng)的學(xué)習(xí)一下。前幾周主要學(xué)習(xí)了一下如何用VHDL來實(shí)現(xiàn)一個(gè)狀態(tài)機(jī)。因?yàn)闋顟B(tài)機(jī)的應(yīng)用實(shí)在是太廣泛了,例如各種存儲(chǔ)器的控制,AD的控制外部器件的控制,也包括內(nèi)部電路的控制,到了非學(xué)不可的地步了。 對(duì)于狀態(tài)機(jī)的理論沒有涉及太多,只有幾點(diǎn)需要注意: (1)moore和mealy的區(qū)別在于輸出是否只和當(dāng)前狀態(tài)有關(guān)。 (2)狀態(tài)機(jī)的兩種基本操作:一是狀態(tài)機(jī)內(nèi)部狀態(tài)的轉(zhuǎn)換,另一是產(chǎn)生輸出信號(hào)序列。 (3)狀態(tài)機(jī)的分析可以從狀態(tài)圖入手,同樣,狀態(tài)機(jī)的設(shè)計(jì)也可以從狀態(tài)圖入手。 在集成電路設(shè)計(jì)時(shí),通?梢詫⒄麄(gè)系統(tǒng)劃分為兩部分,一部分是數(shù)據(jù)單元,另一部分是控制單元。數(shù)據(jù)單元包含保存運(yùn)算數(shù)據(jù)和運(yùn)算結(jié)果的數(shù)據(jù)寄存器,也包括完成數(shù)據(jù)運(yùn)算的組合邏輯?刂茊卧脕懋a(chǎn)生信號(hào)序列,以決定何時(shí)進(jìn)行何種數(shù)據(jù)運(yùn)算,控制單元要從數(shù)據(jù)單元得到條件信號(hào),以決定繼續(xù)進(jìn)行那些數(shù)據(jù)運(yùn)算。數(shù)據(jù)單元要產(chǎn)生輸出信號(hào),數(shù)據(jù)運(yùn)算狀態(tài)等有用信號(hào)。數(shù)據(jù)單元和控制單元中,有兩個(gè)非常重要的信號(hào),即復(fù)位信號(hào)和時(shí)鐘信號(hào)。復(fù)位信號(hào)保證了系統(tǒng)初始狀態(tài)的確定性,時(shí)鐘信號(hào)則是時(shí)序系統(tǒng)工作的必要條件。狀態(tài)機(jī)通常在復(fù)位信號(hào)到來的時(shí)候恢復(fù)到初始狀態(tài),每個(gè)時(shí)鐘到來的時(shí)候內(nèi)部狀態(tài)發(fā)生變化。 正如上面的(3)提到的,設(shè)計(jì)狀態(tài)機(jī)時(shí)一般先構(gòu)造出狀態(tài)圖。構(gòu)造狀態(tài)圖的一般方法是從一個(gè)比較容易描述的狀態(tài)開始,通常初始態(tài)是一個(gè)很好開始的狀態(tài),也就是狀態(tài)機(jī)復(fù)位以后開始的狀態(tài)。在建立每個(gè)狀態(tài)時(shí)最好都清楚的寫出關(guān)于這個(gè)狀態(tài)的文字描述,為硬件設(shè)計(jì)過程提供清晰的參考資料,也為最后完成的設(shè)計(jì)提供完整的設(shè)計(jì)文檔。 下面給出一個(gè)用VHDL實(shí)現(xiàn)ADC0804控制器的完整設(shè)計(jì)過程。 首先根據(jù)ADC0804的時(shí)序圖分析所有可能的狀態(tài),并且建立起來狀態(tài)圖。 時(shí)序圖: http://pic13.album.tom.com/album_pic/2005/09/12/b15fe2073e48e1bb3780e6431f695e8c?%75%4e%6f%44%6a%77%49%71%6d%74 4個(gè)狀態(tài)如下: idle: CS="0",WR=0,RD=1 啟動(dòng)AD0804開始轉(zhuǎn)換 convert:CS=1,WR=1,RD=1,AD0804進(jìn)行數(shù)據(jù)轉(zhuǎn)換 read1: CS="1",WR=1,RD=1,INTR,轉(zhuǎn)換結(jié)束,開始讀 read2: CS="1",WR=1,RD=0,讀取數(shù)據(jù)。 狀態(tài)圖: http://pic13.album.tom.com/album_pic/2005/09/12/8427a2ae7a7a9c03018d4f83dfce23a4?%75%4e%6f%44%6a%77%4b%73%69%72 VHDL程序如下,所用的綜合器是XST -------------------------------------------------------------------------------- -- Design Name: skycanny -- Module Name: ad_controller - Behavioral -- Description: This VHDL design is created to implement a state machine -- to control AD0804 -------------------------------------------------------------------------------- library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity ad_controller is port( reset : in std_logic; clk : in std_logic; intr : in std_logic; data_i : in std_logic_vector(7 downto 0); data_o : out std_logic_vector(7 downto 0); cs : out std_logic; wr : out std_logic; rd : out std_logic ); end ad_controller; architecture Behavioral of ad_controller is type state is (start, convert, read1, read2); signal current_state, next_state : state; signal data_r : std_logic_vector(7 downto 0); signal read_data : std_logic; begin sync :process(reset,clk) begin if(reset = '0') then current_state <= start; elsif(clk'event and clk = '1') then current_state <= next_state; end if; end process sync; comb :process(current_state, intr) begin case current_state is when start => next_state <= convert; cs <= '0'; wr <= '0'; rd <= '1'; read_data <= '0'; when convert => if(intr = '0') then next_state <= read1; else next_state <= convert; end if; cs <= '1'; wr <= '1'; rd <= '1'; read_data <= '0'; when read1 => next_state <= read2; cs <= '0'; wr <= '1'; rd <= '0'; read_data <= '1'; when read2 => next_state <= start; cs <= '1'; wr <= '1'; rd <= '1'; read_data <= '0'; when others => next_state <= start; end case; end process comb; get_data: process(reset,clk) begin if(reset = '0') then data_r <= X"00"; elsif(clk'event and clk = '1') then if(read_data = '1') then data_r <= data_i; end if; end if; end process; data_o <= data_r; end Behavioral;
功能仿真圖: http://pic13.album.tom.com/album_pic/2005/09/12/da734f0b3c9a730aed2a367e13d9f833?%75%4e%6f%44%6a%77%4b%75%6d%73 從仿真圖可以看出,該控制器工作正常。 RTL原理圖: http://pic13.album.tom.com/album_pic/2005/09/12/8427a2ae7a7a9c03018d4f83dfce23a4?%75%4e%6f%44%6a%77%4b%73%69%72 總結(jié):對(duì)于時(shí)序電路中用到的狀態(tài)機(jī),分析時(shí)序電路中間經(jīng)歷的狀態(tài)可以很快得出狀態(tài)機(jī)的整體結(jié)構(gòu),然后用VHDL實(shí)現(xiàn)就可以。另外ISE提供了StateCad,方便了狀態(tài)機(jī)的設(shè)計(jì),仿真等等。 |