hdlbits.01xz.net /Circuits/Sequential Logic/Finite State Machines/Simple FSM 1(synchronous reset)

这个网站牛逼的点是不像一般教程那样, 先告诉你啥是状态机, 再给你个例程,

是告诉你这样一个功能, 你自己修改实现出来, 最后发现这就是状态机啊.

// Note the Verilog-1995 module declaration syntax here:
module top_module(clk, reset, in, out);
    input clk;
    input reset;    // Synchronous reset to state B
    input in;
    output out;//  
    reg out;

    // Fill in state name declarations

    reg present_state, next_state;

    always @(posedge clk) begin
        if (reset) begin  
            present_state = 1;
            out = 1;
        end else begin
            case (present_state)
                // Fill in state transition logic
                0: begin
                    if(in ==0)
                        next_state = 1;
                    else if (in == 1)
                        next_state = 0;
                end
                1: begin
                    if(in ==0)
                        next_state = 0;
                    else if (in == 1)
                        next_state = 1;
                end
            endcase

            // State flip-flops
            present_state = next_state;   

            case (present_state)
                // Fill in output logic
                1: out = 1;
                0: out = 0;
            endcase
        end
    end

endmodule

更新

// Note the Verilog-1995 module declaration syntax here:
module top_module(clk, reset, in, out);
    input clk;
    input reset;    // Synchronous reset to state B
    input in;
    output out;//  
    reg out;

    // Fill in state name declarations
    parameter A=1'b0, B=1'b1; 
    
    reg present_state, next_state;

    always @(posedge clk) begin
        if (reset) begin  
            // Fill in reset logic
            next_state = B;
            present_state = B;
            out = B;
        end else begin
            case (present_state)
                // Fill in state transition logic
                A: if(in == 0) next_state = B;
                B: if(in == 0) next_state = A;
                default: next_state = present_state;
            endcase

            // State flip-flops
            present_state = next_state;   

            case (present_state)
                // Fill in output logic
                A: out = A;
                B: out = B;
            endcase
        end
    end

endmodule

不用状态机

// Note the Verilog-1995 module declaration syntax here:
module top_module(clk, reset, in, out);
    input clk;
    input reset;    // Synchronous reset to state B
    input in;
    output out;//  
    reg out;

    // Fill in state name declarations

    reg present_state, next_state;

    always @(posedge clk) begin
        if (reset) begin  
            // Fill in reset logic
            out = 1;
        end else begin
            if (in == 0) out = !out; 
        end
    end

endmodule
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值