[Verilog 문법] 4.3 FSM

YUN·2026년 1월 23일

디지털 회로 설계

목록 보기
16/20

FSM(Finite State Machine)

유한 상태 기계로, 디지털 시스템의 제어 로직을 설계하는 핵심 기법

(1) 구성 요소

  • 상태 (State): 시스템이 있을 수 있는 조건들
  • 입력 (Input): 상태 전이를 결정하는 신호
  • 출력 (Output): 현재 상태에 따른 결과
  • 전이 (Transition): 상태 간 이동 규칙

(2) 종류

FSM 에는 무어 머신(Moore Machine) 과 밀리 머신(Mealy Machine)이 존재한다.

  • Moore Machine
    • 출력이 현재 상태에만 의존
    • 출력이 안정적
    • 상태 수가 더 많을 수 잇음
  • Mealy Machine
    • 출력이 현재 상태+ 입력에 의존
    • 더 빠른 응답
    • 상태 수가 적을 수 있음

(3) 상태 인코딩 방식

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;

(4) FSM 구현 패턴

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개) 분리하는 것이 좋다.

profile
안녕하세요. 전자공학부 학부생의 공부 기록입니다.

0개의 댓글