题解 | #边沿检测#
边沿检测
https://www.nowcoder.com/practice/fed4247d5ef64ac68c20283ebace11f4
边沿检测,缓存了两次在做边沿检测测试用例就过不了。
唯一需要注意的就是寄存一次后,在上升沿或者下降沿到来的时候,是寄存的为低电平还是输入的为低电平。
比如检测上升沿:a和reg_a都是一直为低电平,当上升沿来临时,在这个时钟周期内,a变成了高电平,所以检测上升沿就是
rise = a & ~reg_a;
`timescale 1ns/1ns module edge_detect( input clk, input rst_n, input a, output reg rise, output reg down ); reg reg_a1,reg_a2; always@(posedge clk or negedge rst_n)begin if(!rst_n)begin reg_a1 <= 1'b0; end else begin reg_a1 <= a; end end always@(posedge clk or negedge rst_n)begin if(!rst_n)begin rise <= 1'b0; down <= 1'b0; end else begin if(a & ~reg_a1)begin rise <= 1'b1; down <= 1'b0; end else if(~a & reg_a1)begin rise <= 1'b0; down <= 1'b1; end else begin rise <= 1'b0; down <= 1'b0; end end end endmodule