题解 | #不重叠序列检测#

不重叠序列检测

https://www.nowcoder.com/practice/9f91a38c74164f8dbdc5f953edcc49cc

//打六拍 用cnt表示
//match延迟一拍确定 所以不能用前面的a_tem得用{a_tem[4:0],data}
`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input data,
	output reg match,
	output reg not_match
	);
	reg [5:0]a_tem;
	reg[2:0]cnt;

	always@(posedge clk or negedge rst_n)begin
	if(!rst_n)
	a_tem<=0;
	else 
	a_tem<={a_tem[4:0],data};
	end

	always@(posedge clk or negedge rst_n)begin
	if(!rst_n)
	cnt<=0;
	else if(cnt==3'b101)
	cnt<=0;
	else
	cnt<=cnt+1;
	end

	always@(posedge clk or negedge rst_n)begin
	if(!rst_n)begin
	match<=0;
	not_match<=0;end
	else if((cnt==3'b101)&&({a_tem[4:0],data}==6'b011100))begin
	match<=1;
	not_match<=0;end
	else if((cnt==3'b101)&&({a_tem[4:0],data}!=6'b011100))begin
	match<=0;
	not_match<=1;end
	else begin
	match<=0;
	not_match<=0;end
	end

endmodule
`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input data,
	output reg match,
	output reg not_match
	);
    
    parameter ZERO=0, ONE=1, TWO=2, THREE=3, FOUR=4, FIVE=5, SIX=6, FAIL=7;
    reg [2:0] state, nstate;
    reg [2:0] cnt;
    
    always@(posedge clk or negedge rst_n) begin
        if(~rst_n)
            cnt <= 0;
        else
            cnt <= cnt==6? 1: cnt+1; 
    end
    
    always@(posedge clk or negedge rst_n) begin
        if(~rst_n)
            state <= ZERO;
        else
            state <= nstate;
    end
    
    always@(*) begin
        if(~rst_n)
            nstate = ZERO;
        else
            case(state)
                ZERO : nstate = data? FAIL : ONE;
                ONE  : nstate = data? TWO  : FAIL;
                TWO  : nstate = data? THREE: FAIL;
                THREE: nstate = data? FOUR : FAIL;
                FOUR : nstate = data? FAIL : FIVE;
                FIVE : nstate = data? FAIL : SIX;
                SIX  : nstate = data? FAIL : ONE;
                FAIL : nstate = cnt==6&&data==0? ONE: FAIL;
                default: nstate = ZERO;
            endcase
    end
    
    always@(*) begin
        if(~rst_n) begin
            match     = 0;
            not_match = 0;
        end
        else begin
            match     = cnt==6&&state==SIX;
            not_match = cnt==6&&state==FAIL;
        end
    end
    
endmodule

全部评论

相关推荐

点赞 评论 收藏
分享
我:“加班需要有加班工资。”&nbsp;hr:“为什么?”&nbsp;哈哈哈哈哈哈哈离大谱
juntenor:你确实太理想化了,对社会不了解呀。这个和HR没有关系,这是国内特色,不然怎么还会有外包就这种逆天的存在呢。
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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