6.有限状态机

Finite-State Machine Models

作用

  • 描述有限数量状态的系统/过程

常见FSM

  1. State Diagram

  1. 比较
比较 Moore Mealy
输出 仅与当前状态有关 当前状态输入信号有关

Analysis

Next-State Table

Next-State Equation

D-Flip-Flop

  1. 图示

  1. 方程

$$ \begin{aligned} D_1 &= Q_1’Q_0 \\ D_0 &= Q_1’Q_0’ + CQ_1’ \\ Q_{next} &= D \\ Q_{1next} &= D_1 = Q_1’Q_0 \\ Q_{2next} &= D_0 = Q_1’Q_0’ + CQ_1' \end{aligned} $$

Moore FSM Verilog for a 10101 or 10111 detector (Overlapping Considered)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module fsm (
    input clk,
    input reset,
    input x,
    output reg y
);

// Define states using Gray code
parameter S0 = 3'b000;
parameter S1 = 3'b001;
parameter S2 = 3'b011;
parameter S3 = 3'b010;
parameter S4 = 3'b110;
parameter S5 = 3'b111;

// Declare state register and next state variable
reg [1:0] state, next_state;

// Two procedural blocks for Moore-type FSM
always @(posedge clk or posedge reset) begin
    if (reset) begin
        // Reset to initial state
        state <= S0;
        y <= 0;
    end else begin
        // Update current state based on next_state value at clock edge
        state <= next_state;

always @(*) begin
    // Next-state logic based on current-state and inputs (Mealy-type)

     case(state)
         S0:
             if(x == 1) begin   // Transition to State-1 when x=1
                 next_state = S1;
             end else begin     // Stay in the same State otherwise
                 next_state = S0;
             end

         S1:
             if(x == 1) begin   // Transition to State-2 when x=1
                 next_state = S2;
             end else begin     // Stay in the same State otherwise
                 next_state = S1;
             end

         S2:
            if(x == 0) begin    // Transition to State-3 when x=0
                next_state = S3;
            end else begin      // Stay in the same State otherwise
                next_state = S2;
            end

         S3:
            if(x == 0) begin    // Transition to State-0 when x=0
                next_state = S0;
            end else begin      // Stay in the same State otherwise
                next_state = S3;
            end
         S4:
            if(x==0)
                next_state = S0;
            else
                next_state = S1;


         default:               // Default transition to initial state (S0)
             next_state = S0;
     endcase

end

endmodule

Design

101 序列检测 (考虑重叠)

State Diagram

Verilog

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module moore_101_sequence_detector(
    input clk,
    input reset,
    input data,
    output reg detected
);

parameter S0 = 2'b00; // 状态0
parameter S1 = 2'b01; // 状态1
parameter S2 = 2'b10; // 状态2
parameter S3 = 2'b11; // 状态3

reg [1:0] state; // 状态寄存器

always @(posedge clk, posedge reset) begin
    if (reset) begin
        state <= S0; // 复位状态
        detected <= 0; // 复位检测信号
    end 
    else begin
    case (state)
    S0: begin // 状态0
        if (data) begin
            state <= S1;
        end else begin
            state <= S0;
        end
    end
    S1: begin // 状态1
        if (data) begin
            state <= S1;
        end else begin
            state <= S2;
        end
    end
    S2: begin // 状态2
        if (data) begin
            state <= S3;
        end else begin
            state <= S0;
        end
    end
    S3: begin // 状态3
        if (data) begin
            state <= S1;
            detected <= 1; //检测到101
        end else begin
            state <= S2;
        end
        end
    endcase
    end
endmodule

附录

同步复位与异步复位

同步复位时钟上升沿复位;异步复位可以在任何时刻复位

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy