题解 | #输入序列连续的序列检测#

输入序列连续的序列检测

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

`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input a,
	output reg match
	);
reg [8:0] cs;
reg [8:0] ns;

parameter IDLE = 9'b0_0000_0001,
            S0 = 9'b0_0000_0010,
            S1 = 9'b0_0000_0100,
            S2 = 9'b0_0000_1000,
            S3 = 9'b0_0001_0000,
            S4 = 9'b0_0010_0000,
            S5 = 9'b0_0100_0000,                                    
            S6 = 9'b0_1000_0000,
            S7 = 9'b1_0000_0000;


//1st
always @ (posedge clk or negedge rst_n)
begin
	if (~rst_n)
	   cs <= IDLE;
    else
	   cs <= ns;
end	   

//2nd
always @ (*)
begin
	case(cs)
	    IDLE : ns = (a == 1'b0)? S0 :IDLE;
		  S0 : ns = (a == 1'b1)? S1 :S0;
		  S1 : ns = (a == 1'b1)? S2 :S0;
		  S2 : ns = (a == 1'b1)? S3 :S0;
		  S3 : ns = (a == 1'b0)? S4 :IDLE;
		  S4 : ns = (a == 1'b0)? S5 :IDLE;
		  S5 : ns = (a == 1'b0)? S6 :IDLE;	
		  S6 : ns = (a == 1'b1)? S7 :S0;		  	  
		  S7 : ns = (a == 1'b1)? IDLE :S0;
		  default : ns = IDLE;
	endcase
end

//3rd  
reg match_tmp;  //根据波形,序列a符合要求之后还要打一拍再输出match 
always @ (posedge clk or negedge rst_n)
begin
	if (~rst_n) begin
	   match_tmp <= 1'b0;
	   match <= 1'b0;
	end   
    else begin
	   match_tmp <= (ns == S7);   //不要用错成cs
	   match <= match_tmp;
	end   
end	   

  
endmodule     

全部评论

相关推荐

不愿透露姓名的神秘牛友
08-01 18:00
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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