VerilogHDL 状态机

本文介绍了四种状态机实现,包括Moore和Mealy类型,分别针对带异步和同步重置的两态及两输入一输出系统。详细展示了使用Verilog的代码实例,涵盖了状态转移逻辑、状态机翻转-跃变以及输出逻辑的编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目1

        This is a Moore state machine with two states, one input, and one output. Implement this state machine. Notice that the reset state is B.

        This exercise is the same as fsm1s, but using asynchronous reset.

module top_module(
    input clk,
    input areset,    // Asynchronous reset to state B
    input in,
    output out);//  

    parameter A=0, B=1; 
    reg state, next_state;


    always @(*) begin    // This is a combinational always block
        // State transition logic
        case(state)
            A:begin
                if(in == 1'b1)
                    next_state <= A;
                else
                    next_state <= B;
            	end
           B:begin
               if(in == 1'b1)
                   next_state <= B;
               else
                   next_state <= A;
              end
        endcase                  

    end

    always @(posedge clk, posedge areset) begin    // This is a sequential always block
        // State flip-flops with asynchronous reset
        if(areset)
            state <= B;
        else
            state <= next_state;
    end

    // Output logic
    // assign out = (state == ...);
            assign out = (state == B);
endmodule

方法2

    always@(*)begin
        case(state)
            B:begin
                out = 1'b1;
            end
            A:begin
                out = 1'b0;
            end
        endcase
    end

方法3


    always@(posedge clk or posedge areset)begin
        if(areset)begin
            out <= 1'b1;
        end
        else if(next_state == B)begin
            out <= 1'b1;
        end
        else begin
            out <= 1'b0;
        end
    end

方法4 

    always@(*)begin 
        if(state == B)begin
            out = 1'b1;
        end
        else begin
            out = 1'b0;
        end
    end

题目2

        This is a Moore state machine with two states, one input, and one output. Implement this state machine. Notice that the reset state is B.

        This exercise is the same as fsm1, but using synchronous reset.

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;
    parameter A = 1'b0;
    parameter B = 1'b1;
    always @(posedge clk) begin
        if (reset) begin  
            // Fill in reset logic
            present_state <= B;
        end
        else begin
            present_state <= next_state;
        end
    end
    always @(*)
        begin
            case(present_state)
                B:begin
                    if(in == 1'b1)
                        next_state <= B;
                    else
                        next_state <= A;
                	end
                A:begin
                    if(in == 1'b1)
                        next_state <= A;
                    else
                        next_state <= B;
                	end
            endcase
        end
          
            always @(*)
                begin
                    if(present_state == B)
                        out <= 1'b1;
                    else
                        out <= 1'b0;
                end

endmodule

题目三

        This is a Moore state machine with two states, two inputs, and one output. Implement this state machine.

        This exercise is the same as fsm2s, but using asynchronous reset.

module top_module(
    input clk,
    input areset,    // Asynchronous reset to OFF
    input j,
    input k,
    output out); //  

    parameter OFF=0, ON=1; 
    reg state, next_state;

    always @(*) begin
        // State transition logic
        case(state)
            OFF:begin
                if(j == 1'b1)
                    next_state <= ON;
                else
                    next_state <= OFF;
            end
            ON:begin
                if(k == 1'b1)
                    next_state <= OFF;
                else
                    next_state <= ON;
            end
        endcase
    end

    always @(posedge clk, posedge areset) begin
        // State flip-flops with asynchronous reset
        if(areset)
            state <= OFF;
        else
            state <= next_state;
    end

    // Output logic
    // assign out = (state == ...);
    assign out = (state == ON);

endmodule

题目四

        This is a Moore state machine with two states, two inputs, and one output. Implement this state machine.

        This exercise is the same as fsm2, but using synchronous reset.

module top_module(
    input clk,
    input reset,    // Synchronous reset to OFF
    input j,
    input k,
    output out); //  

    parameter OFF=0, ON=1; 
    reg state, next_state;

    always @(*) begin
        // State transition logic
        case(state)
            OFF:begin
                if(j == 1'b1)
                    next_state <= ON;
                else
                    next_state <= OFF;
            end
            ON:begin
                if(k == 1'b1)
                    next_state <= OFF;
                else
                    next_state <= ON;
            end
        endcase
                
    end

    always @(posedge clk) begin
        // State flip-flops with synchronous reset
        if(reset)
            state <= OFF;
        else
            state <= next_state;
    end

    // Output logic
    // assign out = (state == ...);
    assign out = (state == ON);
endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发光中请勿扰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值