前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Verilog刷题篇】硬件工程师进阶1|序列检测

【Verilog刷题篇】硬件工程师进阶1|序列检测

作者头像
程序员洲洲
发布2024-06-07 12:27:12
630
发布2024-06-07 12:27:12
举报
文章被收录于专栏:项目文章项目文章

前言

  • 硬件工程师近年来也开始慢慢吃香,校招进大厂年薪总包不下30-40w的人数一大把!而且大厂人数并没有饱和! - 本期是【Verilog刷题篇】硬件工程师进阶1|序列检测,有不懂的地方可以评论进行讨论!
  • 推荐给大家一款刷题、面试的神器,我也是用这一款神器进行学习Verilog硬件代码的!
  • ~链接如下:刷题面试神器跳转链接
  • 也欢迎大家去牛客查看硬件工程师招聘职位的各类资料,并进行提前批投递面试!
  • 小白新手可以通过该神器进行日常的刷题、看大厂面经、学习计算机基础知识、与大牛面对面沟通~ 刷题的图片已经放在下面了~

Q1:输入序列连续的序列检测

问题描述:请编写一个序列检测模块,检测输入信号a是否满足01110001序列,当信号满足该序列,给出指示信号match。

模块的接口信号图如下:

模块的时序图如下:

输入描述: clk:系统时钟信号 rst_n:异步复位信号,低电平有效 a:单比特信号,待检测的数据

输出描述: match:当输入信号a满足目标序列,该信号为1,其余时刻该信号为0

案例代码:

代码语言:javascript
复制
`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input a,
	output reg match
	);
    parameter zero = 4'd0;
       parameter one = 4'd1;
       parameter two = 4'd2;
       parameter three = 4'd3;
       parameter four = 4'd4;
       parameter five = 4'd5;
       parameter six = 4'd6;
       parameter seven = 4'd7; 
    parameter    eight=4'd8;
   
    reg [3:0] cu_st;
    always@(posedge clk or negedge rst_n)
        begin
            if(!rst_n)begin
                cu_st<=zero;
                match<=1'b0;end
            else begin
                case(cu_st)
                    zero:if(a==1'b0)begin
                                cu_st<=one;
                                match<=1'b0;end                   
                         else begin
                                 cu_st<=zero;
                                 match<=1'b0; end
                    one:if(a==1'b1)begin
                                cu_st<=two;
                                match<=1'b0;end                   
                         else begin
                                 cu_st<=one;
                                 match<=1'b0; end
                    two:if(a==1'b1)begin
                                cu_st<=three;
                                match<=1'b0;end                   
                         else begin
                                 cu_st<=one;
                                 match<=1'b0; end
                    three:if(a==1'b1)begin
                                cu_st<=four;
                                match<=1'b0;end                   
                         else begin
                                 cu_st<=one;
                                 match<=1'b0; end
                    four:if(a==1'b0)begin
                                cu_st<=five;
                                match<=1'b0;end                   
                         else begin
                                 cu_st<=zero;
                                 match<=1'b0; end
                    five:if(a==1'b0)begin
                                cu_st<=six;
                                match<=1'b0;end                   
                         else begin
                                 cu_st<=two;
                                 match<=1'b0; end
                    six:if(a==1'b0)begin
                                cu_st<=seven;
                                match<=1'b0;end                   
                         else begin
                                 cu_st<=two;
                                 match<=1'b0; end
                    seven:if(a==1'b1)begin
                                cu_st<=eight;
                                match<=1'b0;end                   
                         else begin
                                 cu_st<=one;
                                 match<=1'b0; end
                    eight:if(a==1'b1)begin
                                cu_st<=three;
                                match<=1'b1;end                   
                         else begin
                                 cu_st<=one;
                                 match<=1'b1; end
                    default:begin cu_st<=zero;
                        match<=1'b0;end
                endcase
                
                
            end
        end
 
endmodule

Q2: 含有无关项的序列检测

问题描述:请编写一个序列检测模块,检测输入信号a是否满足011XXX110序列(长度为9位数据,前三位是011,后三位是110,中间三位不做要求),当信号满足该序列,给出指示信号match。

程序的接口信号图如下:

程序的功能时序图如下:

输入描述: clk:系统时钟信号 rst_n:异步复位信号,低电平有效 a:单比特信号,待检测的数据

输出描述: match:当输入信号a满足目标序列,该信号为1,其余时刻该信号为0

案例代码:

代码语言:javascript
复制
`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input a,
	output reg match
	);
    reg [8:0] a_temp;
    always@(posedge clk or negedge rst_n)
        begin
            if(!rst_n)
                a_temp<=9'b0;
            else
                a_temp<={a_temp[7:0],a};
            
        end
    always@(posedge clk or negedge rst_n)
        begin
            if(!rst_n)
                match<=1'b0;
            else if(a_temp[8:6]==3'b011&&a_temp[2:0]==3'b110)
                match<=1'b1;
            else
                match<=1'b0;
                end
               
endmodule

Q3:不重叠序列检测

问题描述:请编写一个序列检测模块,检测输入信号(a)是否满足011100序列, 要求以每六个输入为一组,不检测重复序列,例如第一位数据不符合,则不考虑后五位。一直到第七位数据即下一组信号的第一位开始检测。当信号满足该序列,给出指示信号match。当不满足时给出指示信号not_match。

模块的接口信号图如下:

模块的时序图如下:

输入描述: clk:系统时钟信号 rst_n:异步复位信号,低电平有效 a:单比特信号,待检测的数据

输出描述: match:当输入信号a满足目标序列,该信号为1,其余时刻该信号为0 not_match:当输入信号a不满足目标序列,该信号为1,其余时刻该信号为0

案例代码:

代码语言:javascript
复制
`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input data,
	output reg match,
	output reg not_match
	);
	reg [5:0]cs;
	reg [5:0]ns;
	
	reg [5:0]count;
	always@(posedge clk or negedge rst_n)
	   if(!rst_n)
	   count<=0;
	   else if (count>=5)
	     count<=0;
	    else
	    count<=count+1;
	   
	
	always@(posedge clk or negedge rst_n)
	if(!rst_n)begin
	   ns<=0;
	   cs<=0;
	   end
	else 
	   cs<=ns;
	parameter IDLE=0,s1=1,s2=2,s3=3,s4=4,s5=5,s6=6;
	parameter NOT_MATCH=7;
	

always@(*)
	  case(cs)
	    IDLE:ns = (data==0)? s1:NOT_MATCH;
	    s1:ns =   (data==1)? s2:NOT_MATCH;
	    s2:ns =   (data==1)? s3:NOT_MATCH;
	    s3:ns =   (data==1)? s4:NOT_MATCH;
	    s4:ns =   (data==0)? s5:NOT_MATCH;
	    s5:ns =   (data==0)? s6:NOT_MATCH;
	    s6:ns =   (data==0)? s1:NOT_MATCH;
	    NOT_MATCH:ns = (data==0&&count == 5)? s1:NOT_MATCH;
	endcase
	
	always@(posedge clk or negedge rst_n)
	  if(!rst_n)begin
	    match<=0;
	    not_match<=0;
	    end
    else if (count == 5)begin
        
        if(ns==s6)begin
	        match<=1;
	        not_match<=0;
	     end
	    else begin
            match<=0;
	       not_match<=1;  
	      end
    end
     else begin
        match <= 1'b0;
        not_match <= 1'b0;
    end
        
    endmodule

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

问题描述:请编写一个序列检测模块,输入信号端口为data,表示数据有效的指示信号端口为data_valid。当data_valid信号为高时,表示此刻的输入信号data有效,参与序列检测;当data_valid为低时,data无效,抛弃该时刻的输入。当输入序列的有效信号满足0110时,拉高序列匹配信号match。

模块的接口信号图如下:

模块的时序图如下:

输入描述: clk:系统时钟信号 rst_n:异步复位信号,低电平有效 data:单比特信号,待检测的数据 data_valid:输入信号有效标志,当该信号为1时,表示输入信号有效

输出描述: match:当输入信号data满足目标序列,该信号为1,其余时刻该信号为0

案例代码:

代码语言:javascript
复制
`timescale 1ns/1ns
module sequence_detect(
	input clk,
	input rst_n,
	input data,
	input data_valid,
	output reg match
	);
    parameter [3:0] data_ref = 4'b0110;
    reg [3:0] data_in;
    always @(posedge clk or negedge rst_n)
        if(!rst_n) data_in <= 4'b0000;
        else if(data_valid) data_in <= {data_in[2:0],data};

    always @(posedge clk or negedge rst_n) begin
        if(!rst_n) match <= 1'b0;
        else if((data_in[2:0] == 3'b011) && (!data))  match <= 1'b1;
        else match <= 1'b0;

    end
    
endmodule

Q5:信号发生器

问题描述:请编写一个信号发生器模块,根据波形选择信号wave_choise发出相应的波形:wave_choice=0时,发出方波信号;wave_choice=1时,发出锯齿波信号;wave_choice=2时,发出三角波信号。

模块的接口信号图如下:

模块的时序图如下:

输入描述: clk:系统时钟信号 rst_n:异步复位信号,低电平有效 wave_choise:2比特位宽的信号,根据该信号的取值不同,输出不同的波形信号

输出描述: wave:5比特位宽的信号,根据wave_choise的值,输出不同波形的信号

案例代码:

代码语言:javascript
复制
`timescale 1ns/1ns
module signal_generator(
	input clk,
	input rst_n,
	input [1:0] wave_choise,
	output reg [4:0]wave
	);

    reg [4:0] cnt;
    reg k;
    always @(posedge clk or negedge rst_n) begin
        if(!rst_n) begin
            cnt <= 5'd0;
            wave <= 5'd0;
        end
        else begin
            case(wave_choise)
                2'd0:begin
                    if(cnt == 5'd19) begin
                        cnt <= 5'd0;
                        wave <= 5'd0;
                    end
                    else if(cnt>=5'd9 && cnt<5'd19)begin
                        cnt <= cnt + 1'b1;
                        wave <= 5'd20;
                    end
                    else begin
                        cnt <= cnt + 1'b1;
                        wave <= 5'd00;
                    end
                end
                2'd1:begin
                    if(wave >= 5'd20)
                        wave <= 5'd0;
                    else
                        wave <= wave + 1'b1;
                end
                2'd2:begin
                    if(wave == 5'd0) begin
                        k <= 1'b0;
                        wave <= wave + 1'b1;
                    end
                    else if(wave == 5'd20) begin
                        k <= 1'b1;
                        wave <= wave - 1'b1;
                    end
                    else if(k == 1'b0 && wave < 5'd20)
                        wave <= wave + 1'b1;
                    else if(k == 1'b1 && wave > 5'd0)
                        wave <= wave - 1'b1;
                    else
                        wave <= wave - 1'b1;
                end
                default:begin wave <= wave;end
            endcase
        end
    end
  
endmodule

总结:小白跟大牛都在用的平台

  • 硬件工程师近年来也开始慢慢吃香,校招进大厂年薪总包不下30-40w的人数一大把!而且大厂人数并没有饱和! - 本期是【Verilog刷题篇】硬件工程师从0到入门3|组合逻辑复习+时序逻辑入门,有不懂的地方可以评论进行讨论!
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-06-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • Q1:输入序列连续的序列检测
  • Q2: 含有无关项的序列检测
  • Q3:不重叠序列检测
  • Q4:输入序列不连续的序列检测
  • Q5:信号发生器
  • 总结:小白跟大牛都在用的平台
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档