유한 상태 기계로, 디지털 시스템의 제어 로직을 설계하는 핵심 기법
FSM 에는 무어 머신(Moore Machine) 과 밀리 머신(Mealy Machine)이 존재한다.
Binary 인코딩 방식은 아래와 같다.
parameter IDLE = 2'b00;
parameter RUN = 2'b01;
parameter STOP = 2'b10;
parameter ERROR = 2'b11;
One-Hot 인코딩 방식은 아래와 같다.
parameter IDLE = 4'b0001;
parameter RUN = 4'b0010;
parameter STOP = 4'b0100;
parameter ERROR = 4'b1000;
Gray 인코딩 방식은 아래와 같다.
parameter IDLE = 2'b00;
parameter RUN = 2'b01;
parameter STOP = 2'b11;
parameter ERROR = 2'b10;
module fsm_3process (
input clk,
input rst,
input start,
input done,
output reg busy,
output reg complete
);
parameter IDLE = 2'b00;
parameter RUN = 2'b01;
parameter FINISH = 2'b10;
reg [1:0] state, next_state;
// Process 1: 상태 레지스터
always @(posedge clk or posedge rst) begin
if (rst)
state <= IDLE;
else
state <= next_state;
end
// Process 2: 다음 상태 로직
always @(*) begin
next_state = state; // 기본값
case (state)
IDLE: begin
if (start)
next_state = RUN;
end
RUN: begin
if (done)
next_state = FINISH;
end
FINISH: begin
next_state = IDLE;
end
default: begin
next_state = IDLE;
end
endcase
end
// Process 3: 출력 로직 (등록된 출력)
always @(posedge clk or posedge rst) begin
if (rst) begin
busy <= 1'b0;
complete <= 1'b0;
end
else begin
case (next_state)
IDLE: begin
busy <= 1'b0;
complete <= 1'b0;
end
RUN: begin
busy <= 1'b1;
complete <= 1'b0;
end
FINISH: begin
busy <= 1'b0;
complete <= 1'b1;
end
default: begin
busy <= 1'b0;
complete <= 1'b0;
end
endcase
end
end
endmodule
위와 같이 다음 상태로 넘기는 로직, 다음 상태 결정 로직, 출력 로직을 모두(총 3개) 분리하는 것이 좋다.