题解 | #根据状态转移写状态机-二段式#

根据状态转移写状态机-二段式

https://www.nowcoder.com/practice/5b2ff27610d04993ae92374d51bfc2e6

`timescale 1ns/1ns

module fsm2(
    input clk,
    input rst,
    input data,

    output reg flag
    );

    reg [2:0] current_state;
    reg [2:0] next_state;

    parameter   S0 = 3'b000;
    parameter   S1 = 3'b001;
    parameter   S2 = 3'b010;
    parameter   S3 = 3'b011;
    parameter   S4 = 3'b100;

    always@(posedge clk or negedge rst) begin
        if(!rst)
            current_state <= 3'd0;
        else
            current_state <= next_state;
    end

    always@(*) begin
        case(current_state) 
            S0: begin
                if(!rst)
                    next_state <= 3'd0;
                else if(data == 1)
                    next_state <= S1;
                else
                    next_state <= 3'd0;
            end
            S1: begin
                if(data == 1)
                    next_state <= S2;
                else
                    next_state <= S1;
            end
            S2: begin
                if(data == 1)
                    next_state <= S3;
                else
                    next_state <= S2;
            end
            S3: begin
                if(data == 1)
                    next_state <= S4;
                else
                    next_state <= S0;
            end
            S4: begin
                if(data == 1)
                    next_state <= S1;
                else
                    next_state <= S0;
            end
        endcase
end
    always@(posedge clk or negedge rst) begin
        if(!rst)
            flag <= 1'b0;
        else if(next_state == S4)
            flag <= 1'b1;
        else
            flag <= 1'b0;
    end
endmodule
全部评论

相关推荐

有趣的牛油果开挂了:最近这个阶段收到些杂七杂八的短信是真的烦
点赞 评论 收藏
分享
ProMonkey2024:5个oc?厉害! 但是有一个小问题:谁问你了?😡我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了(别的帖子偷来的,现学现卖😋)
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务