Verilog 有限狀態機設計_第1頁
Verilog 有限狀態機設計_第2頁
Verilog 有限狀態機設計_第3頁
Verilog 有限狀態機設計_第4頁
Verilog 有限狀態機設計_第5頁
已閱讀5頁,還剩31頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、第八章 有限狀態機設計l MOORE狀態機l MEALY狀態機l 有限狀態機的幾種描述方式l 有限狀態機的狀態編碼8.1 有限狀態機n 構成l 組合邏輯 狀態譯碼、產生輸出l 時序邏輯 存儲狀態n 分類lMoore 輸出是現態的函數lMealy 輸出是現態和輸入的函數摩爾型(摩爾型(Moore)狀態機)狀態機 米里型(米里型(Mealy)狀態機)狀態機 Moore型狀態圖現態次態輸入 / 輸出module fsm(clk,clr,z,qout);/模模5計數器計數器input clk,clr; output reg z; output reg2:0 qout;always (posedge c

2、lk or posedge clr) /此過程定義狀態轉換 begin if(clr) qout=0; /異步復位 else case(qout)3b000: qout=3b001;3b001: qout=3b010;3b010: qout=3b011;3b011: qout=3b100;3b100: qout=3b000;default: qout=3b000;/*default語句*/ endcase endalways (qout) /*此過程產生輸出邏輯*/ begin case(qout)3b100: z=1b1;default:z=1b0; endcase endendmodule

3、Mealy型狀態圖start = 0State0out = 001clr = 1step3 = 0State2out = 100State1out = 010start = 1step3 = 1State3out = 111step2 = 1step2 = 0【例10.7】狀態機設計舉例module FSM( clk, clr, out, start, step2, step3 );input clk, clr, start, step2, step3;output2:0 out;reg2:0 out;reg1:0 state, next_state;parameter state0 = 2b

4、00, state1 = 2b01, / 狀態編碼 state2 = 2b11, state3 = 2b10; / 格雷碼always ( posedge clk or posedge clr )beginif( clr ) state = state0; / 定義初態else state = next_state;endalways ( state or start or step2 or step3 ) / 狀態轉換begin case (state)state0: beginif( start ) next_state = state1;else next_state = state0;

5、endstate1:beginnext_state = state2;endstate2:beginif( step2 ) next_state = state3;else next_state = state0;endstate3:beginif( step3 ) next_state = state0;else next_state = state3;enddefault:next_state = state0; endcaseendalways ( state ) / 狀態譯碼及輸出begincase( state )state0:out = 3b001;state1:out = 3b0

6、10;state2:out = 3b100;state3:out = 3b111;default:out = 3b001;endcaseendendmodule(1)用三個過程描述:即現態(CS)、次態(NS)、輸出邏輯(OL)各用一個always過程描述。(2)雙過程描述(CS+NS、OL雙過程描述):使用兩個always過程來描述有限狀態機,一個過程描述現態和次態時序邏輯(CS+NS);另一個過程描述輸出邏輯(OL)。(3)雙過程描述(CS、NS+OL雙過程描述):一個過程用來描述現態(CS);另一個過程描述次態和輸出邏輯(NS+OL)。(4)單過程描述:在單過程描述方式中,將狀態機的現態

7、、次態和輸出邏輯(CS+NS+OL)放在一個always過程中進行描述。8.2 有限狀態機的Verilog描述“101”序列檢測器的Verilog描述(三個過程) module fsm1_seq101(clk,clr,x,z);input clk,clr,x; output reg z; reg1:0 state,next_state;parameter S0=2b00,S1=2b01,S2=2b11,S3=2b10; /*狀態編碼,采用格雷(Gray)編碼方式*/always (posedge clk or posedge clr) /*該過程定義當前狀態*/beginif(clr) sta

8、te=S0; /異步復位,s0為起始狀態else state=next_state;end“101”序列檢測器的Verilog描述(三個過程) always (state or x) /*該過程定義次態*/begincase (state)S0:begin if(x) next_state=S1; else next_state=S0; endS1:beginif(x) next_state=S1; else next_state=S2; endS2:beginif(x) next_state=S3;else next_state=S0; endS3:beginif(x) next_state

9、=S1;else next_state=S2; enddefault:next_state=S0; /*default語句*/endcaseendalways (state) /*該過程產生輸出邏輯*/ begin case(state)S3: z=1b1;default:z=1b0; endcase endendmodule“101”序列檢測器的Verilog描述(三個過程) “101”序列檢測器(單過程描述) module fsm4_seq101(clk,clr,x,z);input clk,clr,x;output reg z; reg1:0 state;parameter S0=2b0

10、0,S1=2b01,S2=2b11,S3=2b10;/*狀態編碼,采用格雷(Gray)編碼方式*/“101”序列檢測器(單過程描述) always (posedge clk or posedge clr)Begin if(clr) state=S0; /異步復位,s0為起始狀態else case(state)S0:begin if(x) begin state=S1; z=1b0;endelse begin state=S0; z=1b0;end endS1:begin if(x) begin state=S1; z=1b0;endelse begin state=S2; z=1b0;end

11、endS2:beginif(x) begin state=S3; z=1b0;endelse begin state=S0; z=1b0;end endS3:begin if(x) begin state=S1; z=1b1;endelse begin state=S2; z=1b1;end enddefault:begin state=S0; z=1b0;end /*default語句*/endcaseendendmodule8.3 狀 態 編 碼 順序編碼 格雷編碼 約翰遜編碼一位熱碼常用的編碼方式 一位熱碼編碼選擇對話框(Quartus ) 狀態編碼的定義在Verilog語言中,有兩種方

12、式可用于定義狀態編碼,分別用parameter和define語句實現,比如要為state0、state1、state2、state3四個狀態定義碼字為:00、01、11、10,可采用下面兩種方式。方式1:用parameter參數定義 parameter state1=2b00,state2=2b01,state3=2b11,state4=2b10; case(state) state1:; /調用 state2:; 要注意兩種方式定義與調用時的區別,一般情況下,更傾向于采用方式1來定義狀態編碼。一般使用case、casez和casex語句來描述狀態之間的轉換,用case語句表述比用if-els

13、e語句更清晰明了。 狀態編碼的定義方式2:用define語句定義define state1 2b00 /不要加分號“;”define state2 2b01define state3 2b11define state4 2b10case(state)state1:; /調用,不要漏掉符號“”state2:;狀態編碼的定義8.4 有限狀態機設計要點1起始狀態的選擇 : 起始狀態是指電路復位后所處的狀態,選擇一個合理的起始狀態將使整個系統簡潔、高效。多數EDA軟件會自動為基于狀態機的設計選擇一個最佳的起始狀態。2有限狀態機的同步復位 3有限狀態機的異步復位多余狀態的處理一般有如下兩種處理多余狀態的

14、方法:(1)在case語句中用default分支決定如果進入無效狀態所采取的措施;(2)編寫必要的Verilog源代碼明確定義進入無效狀態所采取的行為。頻率計控制器設計舉例 設計一個自動轉換量程的頻率計控制器,用Mealy狀態機實現。進入100K量程(狀態A)reset = 1; std_f_sel = 00100K量程測量(狀態B)reset = 0; std_f_sel = 00進入100K量程(狀態C)reset = 1; std_f_sel = 01100K量程測量(狀態D)reset = 0; std_f_sel = 01進入100K量程(狀態E)reset = 1; std_f_s

15、el = 111K量程測量(狀態D)reset = 0; std_f_sel = 11cntlow = 1cntlow = 1cntlow = 1cntlow = 1cntover = 1cntover = 1【例10.8】自動轉換量程頻率計控制器/* 信號定義clk: 時鐘輸入reset: 系統復位信號half_dollar: 投入5角硬幣one_dollar: 投入1元硬幣half_out: 找零信號dispense: 機器售出飲料collect: 提示取走飲料 */module FSM( clk, clr, out, start, step2, step3 );input clk, cl

16、r, start, step2, step3;output2:0 out;reg2:0 out;reg1:0 state, next_state;parameter state0 = 2b00, state1 = 2b01, / 狀態編碼 state2 = 2b11, state3 = 2b10; / 格雷碼always ( posedge clk or posedge clr )beginif( clr ) state = state0; / 定義初態else state = next_state;endalways ( state or start or step2 or step3 )

17、/ 狀態轉換begin case (state)state0: beginif( start ) next_state = state1;else next_state = state0;endstate1:beginnext_state = state2;endstate2:beginif( step2 ) next_state = state3;else next_state = state0;endstate3:beginif( step3 ) next_state = state0;else next_state = state3;enddefault:next_state = state0; endcaseendalways ( state ) / 狀態譯碼及輸出begincase( state )state0:out = 3b001;state1:out = 3b010;state2:out = 3b100;state3:out = 3b111;default:out = 3b001;endcaseendendmodule 對A/D器件進行采樣控制,用單片機完成編程簡單,控制

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論