1
1
// ------------------------------------------------------------------------------
2
2
// edge_detect.sv
3
+ // published as part of https://github.com/pConst/basic_verilog
3
4
// Konstantin Pavlov, pavlovconst@gmail.com
4
5
// ------------------------------------------------------------------------------
5
6
6
7
// 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
+ //
8
13
// Added parameter to select combinational implementation (zero clocks delay)
9
14
// or registered implementation (one clocks delay)
10
15
//
17
22
/* --- INSTANTIATION TEMPLATE BEGIN ---
18
23
19
24
edge_detect #(
25
+ .WIDTH( 32 ),
20
26
.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 ),
24
30
.in( in[31:0] ),
25
- .rising( out [31:0] ),
31
+ .rising( in_rise [31:0] ),
26
32
.falling( ),
27
33
.both( )
28
34
);
@@ -31,59 +37,60 @@ edge_detect #(
31
37
32
38
33
39
module edge_detect # ( parameter
40
+ bit [7 : 0 ] WIDTH = 1 , // signal width
34
41
bit [0 : 0 ] REGISTER_OUTPUTS = 1'b0 // 0 - comb. implementation (default)
35
42
// 1 - registered implementation
36
43
)(
37
44
input clk,
38
- input nrst ,
45
+ input anrst ,
39
46
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
44
51
);
45
52
46
53
// 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 ;
51
58
end else begin
52
- in_d <= in;
59
+ in_d[ WIDTH - 1 : 0 ] <= in[ WIDTH - 1 : 0 ] ;
53
60
end
54
61
end
55
62
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;
59
66
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 ] );
63
70
end
64
71
65
72
generate
66
- if ( REGISTER_OUTPUTS == '0 ) begin
73
+ if ( REGISTER_OUTPUTS == 1'b0 ) begin
67
74
68
75
// combinational outputs, no delay
69
76
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 ] ;
73
80
end // always
74
81
75
82
end else begin
76
83
77
84
// 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 ;
83
90
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 ] ;
87
94
end // always
88
95
end // if
89
96
0 commit comments