题解 | #输入序列不连续的序列检测#(最规范的段式状态机)

输入序列不连续的序列检测

https://www.nowcoder.com/practice/f96d0e94ec604592b502b0f1800ed8aa

`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input data,
	input data_valid,
	output reg match
	);
  
  reg [4:0] cur_state;
  reg [4:0] nex_state;
  
  parameter IDLE = 5'b00_001,
            S0   = 5'b00_010,
			S1   = 5'b00_100,
            S2   = 5'b01_000,
			S3   = 5'b10_000;
  
  always@(posedge clk or negedge rst_n)begin
      if(!rst_n)
          cur_state <= IDLE           ;
      else
          cur_state <= nex_state      ;	  
  end
  
  always@(*)begin
     case(cur_state)
	    IDLE:begin 
		    if(data_valid == 1'b1)begin
			    if(data == 1'b0)
				    nex_state = S0    ;
				else
				    nex_state = IDLE  ;    
			end
			else begin
			    nex_state = IDLE;
			end
		end	
		S0:begin //data 序列:0
		    if(data_valid == 1'b1)begin
			    if(data == 1'b1)
				    nex_state = S1    ;
				else
				    nex_state = S0    ;    
			end
			else begin
			    nex_state = S0;
			end
		end	
		S1:begin //data 序列:01
		    if(data_valid == 1'b1)begin
			    if(data == 1'b1)
				    nex_state = S2    ;
				else 
				    nex_state = S0    ;    
			end
			else begin
			    nex_state = S1;
			end
		end	
		S2:begin //data 序列:011
		    if(data_valid == 1'b1)begin
			    if(data == 1'b0)
				    nex_state = S3    ;
				else
				    nex_state = IDLE  ;    
			end
			else begin
			    nex_state = S1        ;
			end
		end	
		S3:begin //data 序列:0110
		    if(data_valid == 1'b1)begin
			    if(data == 1'b0)
				    nex_state = S0    ;
				else
				    nex_state = IDLE  ;    
			end
			else begin
			    nex_state = IDLE      ;
			end
		end	
		default: nex_state = IDLE     ;
	 endcase
  end
  
  always@(posedge clk or negedge rst_n)begin
      if(!rst_n)
	      match <= 1'b0;
	  else begin
	      case(nex_state)
		      S3     : match <= 1'b1 ;
			  default: match <= 1'b0 ;
		  endcase
	  end
  end
endmodule

全部评论

相关推荐

昨天 12:04
门头沟学院 Java
现在是很缺人吗
码农索隆:缺分母,不缺分子,这样好作为炫耀的资本
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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