Skip to content

Commit 1a32f07

Browse files
committed
Added optional WIDTH parameter. Made reset signal to be asynchronous
1 parent a928a6c commit 1a32f07

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

edge_detect.sv

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
//------------------------------------------------------------------------------
22
// edge_detect.sv
3+
// published as part of https://github.com/pConst/basic_verilog
34
// Konstantin Pavlov, pavlovconst@gmail.com
45
//------------------------------------------------------------------------------
56

67
// INFO ------------------------------------------------------------------------
7-
// Edge detector, ver.3
8+
// Edge detector, ver.4
9+
//
10+
// (new!) Added WIDTH parameter to simplify instantiating arrays of edge detectors
11+
// (new!) Made reset to be asynchronous
12+
//
813
// Added parameter to select combinational implementation (zero clocks delay)
914
// or registered implementation (one clocks delay)
1015
//
@@ -17,12 +22,13 @@
1722
/* --- INSTANTIATION TEMPLATE BEGIN ---
1823
1924
edge_detect #(
25+
.WIDTH( 32 ),
2026
.REGISTER_OUTPUTS( 1'b1 )
21-
) ED1[31:0] (
22-
.clk( {32{clk}} ),
23-
.nrst( {32{1'b1}} ),
27+
) in_ed (
28+
.clk( clk ),
29+
.anrst( 1'b1 ),
2430
.in( in[31:0] ),
25-
.rising( out[31:0] ),
31+
.rising( in_rise[31:0] ),
2632
.falling( ),
2733
.both( )
2834
);
@@ -31,59 +37,60 @@ edge_detect #(
3137

3238

3339
module edge_detect #( parameter
40+
bit [7:0] WIDTH = 1, // signal width
3441
bit [0:0] REGISTER_OUTPUTS = 1'b0 // 0 - comb. implementation (default)
3542
// 1 - registered implementation
3643
)(
3744
input clk,
38-
input nrst,
45+
input anrst,
3946

40-
input in,
41-
output logic rising,
42-
output logic falling,
43-
output logic both
47+
input [WIDTH-1:0] in,
48+
output logic [WIDTH-1:0] rising,
49+
output logic [WIDTH-1:0] falling,
50+
output logic [WIDTH-1:0] both
4451
);
4552

4653
// data delay line
47-
logic in_d = 0;
48-
always_ff @(posedge clk) begin
49-
if ( ~nrst ) begin
50-
in_d <= 0;
54+
logic [WIDTH-1:0] in_d = '0;
55+
always_ff @(posedge clk or negedge anrst) begin
56+
if ( ~anrst ) begin
57+
in_d[WIDTH-1:0] <= '0;
5158
end else begin
52-
in_d <= in;
59+
in_d[WIDTH-1:0] <= in[WIDTH-1:0];
5360
end
5461
end
5562

56-
logic rising_comb;
57-
logic falling_comb;
58-
logic both_comb;
63+
logic [WIDTH-1:0] rising_comb;
64+
logic [WIDTH-1:0] falling_comb;
65+
logic [WIDTH-1:0] both_comb;
5966
always_comb begin
60-
rising_comb = nrst && (in && ~in_d);
61-
falling_comb = nrst && (~in && in_d);
62-
both_comb = nrst && (rising_comb || falling_comb);
67+
rising_comb[WIDTH-1:0] = {WIDTH{anrst}} & (in[WIDTH-1:0] & ~in_d[WIDTH-1:0]);
68+
falling_comb[WIDTH-1:0] = {WIDTH{anrst}} & (~in[WIDTH-1:0] & in_d[WIDTH-1:0]);
69+
both_comb[WIDTH-1:0] = {WIDTH{anrst}} & (rising_comb[WIDTH-1:0] | falling_comb[WIDTH-1:0]);
6370
end
6471

6572
generate
66-
if( REGISTER_OUTPUTS=='0 ) begin
73+
if( REGISTER_OUTPUTS==1'b0 ) begin
6774

6875
// combinational outputs, no delay
6976
always_comb begin
70-
rising = rising_comb;
71-
falling = falling_comb;
72-
both = both_comb;
77+
rising[WIDTH-1:0] = rising_comb[WIDTH-1:0];
78+
falling[WIDTH-1:0] = falling_comb[WIDTH-1:0];
79+
both[WIDTH-1:0] = both_comb[WIDTH-1:0];
7380
end // always
7481

7582
end else begin
7683

7784
// registered outputs, 1 cycle delay
78-
always_ff @(posedge clk) begin
79-
if( ~nrst ) begin
80-
rising <= 0;
81-
falling <= 0;
82-
both <= 0;
85+
always_ff @(posedge clk or negedge anrst) begin
86+
if( ~anrst ) begin
87+
rising[WIDTH-1:0] <= '0;
88+
falling[WIDTH-1:0] <= '0;
89+
both[WIDTH-1:0] <= '0;
8390
end else begin
84-
rising <= rising_comb;
85-
falling <= falling_comb;
86-
both <= both_comb;
91+
rising[WIDTH-1:0] <= rising_comb[WIDTH-1:0];
92+
falling[WIDTH-1:0] <= falling_comb[WIDTH-1:0];
93+
both[WIDTH-1:0] <= both_comb[WIDTH-1:0];
8794
end // always
8895
end // if
8996

0 commit comments

Comments
 (0)