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

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

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

二段式状态机,第一段always过程块描述初次态的转变,第二段always过程块描述状态转移关系

完整代码:

`timescale 1ns/1ns

module fsm2(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);

//*************code***********//
    parameter S0=0, S1=1, S2=2, S3=3, S4=4;
    reg[2:0] CS,NS;
    always@(posedge clk or negedge rst)begin
        if(~rst)
            CS <= 0;
        else
            CS <= NS;
    end
    
    always@(*)begin
        if(~rst)
            flag = 0;
        else
            case(CS)
                S0:begin 	//flag为1后的状态可能是S0或S1,在这两个部分归零flag
                    flag = 0;
                    if(data) NS = S1; else NS = S0;
                end
                S1:begin
                    flag = 0;
                    if(data) NS = S2; else NS = S1;
                end
                S2:if(data) NS = S3; else NS = S2;
                S3:if(data) NS = S4; else NS = S3;
                S4:begin 
                    flag = 1;
                    if(data) NS = S1; else NS = S0;
                end
            endcase
    end
    
//*************code***********//
endmodule
全部评论

相关推荐

吴offer选手:我卡在笔试才是最好笑的,甚至没给我发过笔试链接
投递哔哩哔哩等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务