You are on page 1of 37

Finite State Machine

-- Moore Machine
-- Mealy Machine
• 要設計一個數位電路,通常會將系統分為
兩個部分:控制單元(control unit)與資料運
算單元(datapath) 。
• 控制單元產生控制訊號(control signals)用以
控制資料運算單元;而資料運算單元產生
的狀態訊號(status signals)則會送至控制單
元當成部分的輸入訊號。
數位電路的基本架構
控制單元與資料運算單元的內部電路
控制單元
• Microprogramming control方式。
• 此種方式會把要送至datapath的所有狀態控制訊號,
儲存於唯讀記憶體(ROM)中,再依據每次的狀態
(state),從ROM中讀出相對應的控制訊號。此種
實現方式,彈性較大,若需要更改狀態控制訊號,
只需將ROM的內容更新即可。
• 缺點是速度較慢,針對不同狀態到唯讀記憶體取
出所需的控制訊號,需要很長的延遲時間(delay)。
Microprogramming control電路
控制單元
• Hard-wired control方式:也可稱為有限狀態機
(finite state machine, FSM),通常分成三個部分:
• 次狀態邏輯:組合電路,根據現在輸入(control
inputs)與目前電路狀態(current state),產生下一個
電路狀態(next state)。
• 目前狀態暫存器:記憶單元,儲存目前的電路狀
態(需使用脈波clock來同步)。
• 輸出邏輯:組合電路,根據現在輸入與目前電路
狀態,產生輸出控制訊號來控制datapath單元進行
所需運算。
FSM
• 此種實現方式,彈性較小,若要更改狀態
控制訊號,電路需重新設計。但是因為產
生控制訊號所需的延遲時間很短(比
microprogramming control快很多),故常被
電路設計者所採用,以達到較快的電路速
度。
FSM電路
Moore機
• 名字來自這個狀態機概念的提出者E. F. Moore。
• Moore機的輸出,只與目前的電路狀態有關,與
目前的輸入信號(current inputs)無關。
• Moore有限狀態機最重要的特點就是將輸入與輸
出信號隔離開來。在時鐘脈衝有效邊緣的有限個
延遲後,輸出達到穩定值。即使在一個時鐘週期
內輸入信號發生變化,輸出也會在一個完整的時
鐘週期內保持穩定值而不變。輸入對輸出的影響
要到下一個時鐘週期才會反應出來。
Mealy機
• 名字來自這個狀態機概念的提出者G. H.
Mealy。
• Mealy機的輸出,與目前的電路狀態和目前
的輸入信號都有關。即輸入信號一改變,
輸出會馬上跟著改變(前述的Moore機則是
輸入改變後,輸出會延遲一個時鐘週期之
後才變化)。
Moore 機與Mealy機
資料運算單元
•資料運算單元通常包含:算術邏輯單元、加
減法器、乘法器、比較器、選擇器、暫存
器、記憶體等。
•,資料運算單元主要進行所規劃設計電路
的運算行為。資料運算單元依據控制單元
傳來的控制訊號,依序執行(一個狀態接著
另一個狀態)所設計好的運算動作。
• 資料運算單元設計時,可針對成本資源(resource)
或是時間/時序(timing)來做最佳化(optimization)。
• 資源最佳化(resource optimization)可從下列四個地
方進行:
(1)storage sharing。
(2)functional sharing。
(3)bus sharing。
(4)register merging。
• 時序最佳化(timing optimization)則從下列四個地方
來進行:
(1)chaining or multicycling。
(2)functional unit pipelining。
(3)datapath pipelining;
(4)control path pipelining。
管線(pipeline)技術
• 在資料運算單元最佳化中,最常使用的是
管線技術,它會在組合邏輯中插入暫存器,
將邏輯電路分成一個階段一個階段(stage)。
• 在每次的時脈週期(clock period)中,各個階
段都能輸出該部分的運算結果,如此可減
少critical path的傳遞延遲時間。此外,在第
一個事件完成後,每個時脈週期都能輸出
一個運算事件的結果。
無管線化電路
設計一個產生(X1+X2+X3+X4+X5)結果的電路
管線化電路

設計一個產生(X1+X2+X3+X4+X5)結果的電路
Moore機範例
//File Name: Moore_FSM.v
module Moore_FSM(Clock, Reset, In, Out, State);
input Clock, Reset, In;
output Out;
output [1:0] State;
reg Out;
reg [1:0] State, NextState;
parameter S0=2’b00, S1=2’b01, S2=2’b10, S3=2’b11;
//Current state register (F/F)
always @(posedge Clock or posedge Reset)
begin
if(Reset)
State <=S0;
else
State <=NextState;
end
//Next state logic (combinational CKT)
always @(In or State)
begin
case (State)
S0: begin
if (In==1)
NextState=S2;
else
NextState=S0;
end
S1: begin
if (In==1)
NextState=S2;
else
NextState=S0;
end
S2: begin
if (In==1)
NextState=S3;
else
NextState=S2;
end
S3: begin
if (In==1)
NextState=S1;
else
NextState=S3;
end
endcase
end

//Output logic (combinational CKT)


always @(State)
begin
case (State)
S0: Out=0;
S1: Out=1;
S2: Out=1;
S3: Out=0;
endcase
end
module stimulus;
reg Clock, Reset, In;
wire [1:0] Out,State;
Moore_FSM MSM(Clock, Reset, In, Out,State);

initial clock=1’b0;
always #20 clock=~clock;

initial begin
Reset=1’b0;
#20 Reset=1’b1; In=1’b0;
#20 Reset=1’b0;
#20 In=1’b1; //S1
#40 In=1’b1; //S3
#40 In=1’b1; //S1
#40 In=1’b1; //S2
#200 $finish;
end
initial $monitor($stime,“In=%b, Out=%b, State=%b”, In, Out, State);
//dumpvars;
endmodule
Mealy機範例
//File Name: Mealy_FSM.v
module Mealy_FSM(Clock, Reset, In, Out);
input Clock, Reset, In;
output Out;
reg Out;
reg [1:0] State, NextState;
parameter S0=2’b00, S1=2’b01, S2=2’b10, S3=2’b11;

//Current state register (F/F)


always @(posedge Clock or posedge Reset)
begin
if(Reset)
State <=S0;
else
State <=NextState;
end
//Next state logic (combinational CKT)
always @(In or State)
begin
case (State)
S0: begin
if (In==1)
begin
NextState=S1;
Out=1;
end
else
begin
NextState=S0;
Out=0;
end
end
S1: begin
if (In==1)
begin
NextState=S2;
Out=0;
end
else
begin
NextState=S1;
Out=1;
end
end
S2: begin
if (In==1)
begin
NextState=S0;
Out=1;
end
else
begin
NextState=S2;
Out=0;
end
end
S3: begin
if (In==1)
begin
NextState=S1;
Out=1;
end
else
begin
NextState=S3;
Out=0;
end
end
endcase
end
endmodule
紅綠燈控制器
• 實作出紅綠燈控制器,在一定數目的clock cycle
之後做切換燈號的動作。假設希望綠燈維持3個
clock cycle之後切換成黃燈,黃燈在1個clock
cycle之後切換成紅燈,紅燈維持5個clock cycle。
紅綠燈範例
//File name: trafficLight.v
module trafficLight(Clock, Reset, Red, Green, Yellow);
input Clock, Reset;
output Red, Green, Yellow;
Wire Recount;
Traffic_Control myTraffic_Control(.Clock(Clock), .Reset(Reset), .Recount(Recount),
.Red(Red), .Green(Green), .Yellow(Yellow)); //FSM
Datapath myDatapath(.Clock(Clock), .Reset(Reset), .RGY({Red, Green,
Yellow}), .Recount(Recount));
endmodule
module Datapath(Clock, Reset, RGY, Recount);
input Clock, Reset;
input [2:0] RGY;
output Recount;
wire [3:0] Counter_Number;

Compare mycompare(.current_times(Counter_Number), .RGY(RGY), .Recount(Recount));


Counter16 myCounter16(.Clock(Clock), .Reset(Reset), .Recount(Recount),
.Count_Out(Counter_Number));

endmodule
module Traffic_Control(Clock, Reset, Recount, Red, Green, Yellow);
input Clock, Reset, Recount;
output Red, Green, Yellow;
reg Red, Green, Yellow;
reg [1:0] currentState, nextState;
parameter [1:0] Red_Light=0, Green_Light=1, Yellow_Light=2;

//state register (sequential)


always @(posedge Clock)
begin
if(Reset)
currentState <=Red_Light;
else
currentState <=nextState;
end
//next state logic (combinational)
always @(currentState or Recount)
begin
case(currentState)
Red_Light:
begin
if (Recount)
nextState=Green_Light;
else
nextState=Red_Light;
end
Green_Light:
begin
if (Recount)
nextState=Yellow_Light;
else
nextState=Green_Light;
end
Yellow_Light:
begin
if (Recount)
nextState=Red_Light;
else
nextState=Yellow_Light;
end
default: nextState=Red_Light;
endcase
end

//Output logic (combinational)


always @(currentState)
begin
case(currentState)
Red_Light:
begin
Red=1’b1;
Green=1’b0;
Yellow=1’b0;
end
Green_Light:
begin
Red=1’b0;
Green=1’b1;
Yellow=1’b0;
end
Yellow_Light:
begin
Red=1’b0;
Green=1’b0;
Yellow=1’b1;
end
default:
begin
Red=1’b0;
Green=1’b0;
Yellow=1’b0;
end
endcase
endmodule
module Counter16(Clock, Reset, Recount, Count_Out);
input Clock, Reset, Recount;
output[3:0] Count_Out;
reg [3:0] Count_Out;

always @(posedge Clock)


begin
if (Reset)
Count_out <=0;
else
begin
if (Recount)
Count_Out <=0;
else
Count_Out <=Count_Out+1;
end
end
endmodule
module compare(current_times, RGY, Recount);
input [2:0] RGY;
input [3:0] current_times;
output Recount;
reg Recount;
parameter R_times=4, G_times=2, Y_times=0;

always @(RGY or current_times)


begin
case (RGY)
3’b100: begin
if (current_times== R_times)
Recount=1;
else
Recount=0;
end
3’b001: begin
if (current_times== Y_times)
Recount=1;
else
Recount=0;
end
3’b010: begin
if (current_times== G_times)
Recount=1;
else
Recount=0;
end
default: Recount=1;
endcase
end
endmodule

You might also like