2018-08-01 07:00:37 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2018-12-11 15:34:14 +03:00
|
|
|
// edge_detect.sv
|
2018-07-29 08:14:23 +03:00
|
|
|
// Konstantin Pavlov, pavlovconst@gmail.com
|
2018-08-01 07:00:37 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2018-07-29 08:14:23 +03:00
|
|
|
|
2018-08-01 07:00:37 +03:00
|
|
|
// INFO ------------------------------------------------------------------------
|
2020-05-22 15:59:50 +03:00
|
|
|
// Edge detector, ver.3
|
|
|
|
// Added parameter to select combinational implementation (zero clocks delay)
|
|
|
|
// or registered implementation (one clocks delay)
|
2018-12-04 12:33:26 +03:00
|
|
|
//
|
|
|
|
// In case when "in" port has toggle rate 100% (changes every clock period)
|
|
|
|
// "rising" and "falling" outputs will completely replicate input
|
|
|
|
// "both" output will be always active in this case
|
2020-05-22 15:59:50 +03:00
|
|
|
//
|
2018-07-29 08:14:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* --- INSTANTIATION TEMPLATE BEGIN ---
|
|
|
|
|
2020-05-22 15:59:50 +03:00
|
|
|
edge_detect #(
|
|
|
|
.REGISTER_OUTPUTS( 1'b1 )
|
|
|
|
) ED1[31:0] (
|
2018-12-04 12:33:26 +03:00
|
|
|
.clk( {32{clk}} ),
|
|
|
|
.nrst( {32{1'b1}} ),
|
|
|
|
.in( in[31:0] ),
|
|
|
|
.rising( out[31:0] ),
|
2018-07-29 08:14:23 +03:00
|
|
|
.falling( ),
|
|
|
|
.both( )
|
|
|
|
);
|
|
|
|
|
|
|
|
--- INSTANTIATION TEMPLATE END ---*/
|
|
|
|
|
|
|
|
|
2020-05-22 15:59:50 +03:00
|
|
|
module edge_detect #( parameter
|
|
|
|
bit [0:0] REGISTER_OUTPUTS = 1'b0 // 0 - comb. implementation (default)
|
|
|
|
// 1 - registered implementation
|
|
|
|
)(
|
2018-07-29 08:14:23 +03:00
|
|
|
input clk,
|
|
|
|
input nrst,
|
|
|
|
|
2018-12-04 12:33:26 +03:00
|
|
|
input in,
|
|
|
|
output logic rising,
|
|
|
|
output logic falling,
|
|
|
|
output logic both
|
2018-07-29 08:14:23 +03:00
|
|
|
);
|
|
|
|
|
2020-05-22 15:59:50 +03:00
|
|
|
// data delay line
|
2018-12-04 12:33:26 +03:00
|
|
|
logic in_d = 0;
|
2018-07-29 08:14:23 +03:00
|
|
|
always_ff @(posedge clk) begin
|
2018-08-01 07:00:37 +03:00
|
|
|
if ( ~nrst ) begin
|
2018-12-04 12:33:26 +03:00
|
|
|
in_d <= 0;
|
|
|
|
end else begin
|
|
|
|
in_d <= in;
|
2018-08-01 07:00:37 +03:00
|
|
|
end
|
2018-07-29 08:14:23 +03:00
|
|
|
end
|
|
|
|
|
2020-05-22 15:59:50 +03:00
|
|
|
logic rising_comb;
|
|
|
|
logic falling_comb;
|
|
|
|
logic both_comb;
|
2018-12-04 12:33:26 +03:00
|
|
|
always_comb begin
|
2020-05-22 15:59:50 +03:00
|
|
|
rising_comb = nrst && (in && ~in_d);
|
|
|
|
falling_comb = nrst && (~in && in_d);
|
|
|
|
both_comb = nrst && (rising_comb || falling_comb);
|
2018-12-04 12:33:26 +03:00
|
|
|
end
|
2018-07-29 08:14:23 +03:00
|
|
|
|
2020-05-22 15:59:50 +03:00
|
|
|
generate
|
|
|
|
if( REGISTER_OUTPUTS=='0 ) begin
|
|
|
|
|
|
|
|
// combinational outputs, no delay
|
|
|
|
always_comb begin
|
|
|
|
rising = rising_comb;
|
|
|
|
falling = falling_comb;
|
|
|
|
both = both_comb;
|
|
|
|
end // always
|
|
|
|
|
|
|
|
end else begin
|
|
|
|
|
|
|
|
// registered outputs, 1 cycle delay
|
|
|
|
always_ff @(posedge clk) begin
|
|
|
|
if( ~nrst ) begin
|
|
|
|
rising <= 0;
|
|
|
|
falling <= 0;
|
|
|
|
both <= 0;
|
|
|
|
end else begin
|
|
|
|
rising <= rising_comb;
|
|
|
|
falling <= falling_comb;
|
|
|
|
both <= both_comb;
|
|
|
|
end // always
|
|
|
|
end // if
|
|
|
|
|
|
|
|
end // end else
|
|
|
|
endgenerate
|
|
|
|
|
2018-07-29 08:14:23 +03:00
|
|
|
endmodule
|