题解 | #状态机-非重叠的序列检测#(三段式状态机)

状态机-非重叠的序列检测

https://www.nowcoder.com/practice/2e35c5c0798249aaa2e1044dbaf218f2

`timescale 1ns/1ns

module sequence_test1(
	input wire clk  ,
	input wire rst  ,
	input wire data ,
	output reg flag
);
//*************code***********//

reg [4:0] cur_state   ;
reg [4:0] nex_state   ;

localparam  S0 =  5'b00001    ,
            S1 =  5'b00010    ,
			S2 =  5'b00100    ,
			S3 =  5'b01000    ,
			S4 =  5'b10000    ;


always@(posedge clk or negedge rst)begin
    if(!rst)
	    cur_state <= S0           ;
	else 
	    cur_state <= nex_state    ;
end

always@(*)begin
	case(cur_state)
	    S0 : nex_state =  (data == 1)? S1 : S0  ;
		S1 : nex_state =  (data == 0)? S2 : S0  ;
		S2 : nex_state =  (data == 1)? S3 : S0  ;
		S3 : nex_state =  (data == 1)? S4 : S0  ;
		S4 : nex_state =  (data == 1)? S0 : S0  ;
		default : nex_state = S0                ;
	endcase
end

always@(posedge clk or negedge rst)begin
    if(!rst)
	    flag <= 1'b0               ;     
	else begin
	    case(cur_state)
		    S4: begin
				if(data == 1'b1)
				    flag <= 1'b1    ;
				else 
				    flag <= 1'b0    ;
			end
			default:flag <= 1'b0    ;
		endcase    
	end
	    
end
//*************code***********//
endmodule

全部评论

相关推荐

Java大菜狗:纯纯招黑奴,一天还不到两百那么多要求,还不迟到早退,以为啥啊,给一点工资做一堆活,还以不拖欠员工工资为荣,这是什么值得骄傲的事情吗,纯纯***公司
点赞 评论 收藏
分享
06-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
太难了,双9bg也被刷
投递韶音科技等公司10个岗位
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务