题解 | #边沿检测#
边沿检测
https://www.nowcoder.com/practice/fed4247d5ef64ac68c20283ebace11f4
`timescale 1ns/1ns
module edge_detect(
input clk,
input rst_n,
input a,
output reg rise,
output reg down
);
reg a_pre;
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
a_pre <= 1'b0;
end
else begin
a_pre <= a;
end
end
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
{rise,down} <= 2'b00;
end
else begin
// {rise,down} <= {(~a_pre) & a , a_pre & (~a)}; //这种方式会将输入的不确定态传递给输出
if((~a_pre) & a)
{rise,down} <= 2'b10;
else if(a_pre & (~a))
{rise,down} <= 2'b01;
else
{rise,down} <= 2'b00;
end
end
endmodule