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

不重叠序列检测

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

感觉这题clk采样怪怪的,用的是移位寄存器方法,在modelsim下仿真,此代码为正确的(但在牛客的iverilog中仿真,就错误了)。为了实现时序的移位寄存器和cnt同时判断match,对cnt打了一拍。本菜鸟被困扰了一下午,欢迎指正交流
`timescale 1ns/1ns
module sequence_detect(
    input clk,
    input rst_n,
    input data,
    output reg match,
    output reg not_match
    );
    reg [5:0] seq;
reg [3:0] counter;

always@(posedge clk or negedge rst_n) begin
    if(!rst_n) begin
        seq <= 6'b0;
        match <= 1'b0;
        not_match <= 1'b0;
    end
    else
        seq <= { seq[4:0], data };
end

always@(posedge clk) begin
    if(!rst_n) begin
        counter <= 0;
    end
    else if(counter=='d5)
        counter <= 0;
    else begin
        counter <= counter + 1;
    end
end

reg flag;
always@(posedge clk) begin
    if(!rst_n) begin
        flag <= 0;
    end
    else if(counter=='d5)
        flag <= 1;
    else begin
        flag <= 0;
    end
end

always@(posedge clk)begin
    if( {seq[4:0],data}==6'b011100 && flag ) begin
        match <= 1;
    end
    else begin
        match <= 0;
    end
end

always@(posedge clk)begin
    if( {seq[4:0],data}!=6'b011100 && flag ) begin
        not_match <= 1;
    end
    else begin
        not_match <= 0;
    end
end            
   
endmodule

全部评论
可能存在两点问题,counter的最大值建议设置为6,非复位条件下,counter的初始化值设置为1.另外一点是match和not_match的判断看题目时序图的话是在当前序列的第六位进行的,可能不是下一个序列的第一位,所以always@(*)可能更适合match和not_match对应的block。假如说的有错误,还请见谅哈!
1 回复 分享
发布于 2022-03-17 10:25
你拉个flag,明显多打了一拍啊
点赞 回复 分享
发布于 2023-03-15 14:38 黑龙江

相关推荐

没有offer的呆呆:薪资有的时候也能说明一些问题,太少了活不活得下去是一方面,感觉学习也有限
点赞 评论 收藏
分享
被加薪的哈里很优秀:应该继续招人,不会给你留岗位的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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